korganizer

kdatenavigator.cpp

00001 /*
00002     This file is part of KOrganizer.
00003 
00004     Copyright (c) 2001,2002,2003 Cornelius Schumacher <schumacher@kde.org>
00005     Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
00006 
00007     This program is free software; you can redistribute it and/or modify
00008     it under the terms of the GNU General Public License as published by
00009     the Free Software Foundation; either version 2 of the License, or
00010     (at your option) any later version.
00011 
00012     This program is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00015     GNU General Public License for more details.
00016 
00017     You should have received a copy of the GNU General Public License
00018     along with this program; if not, write to the Free Software
00019     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020 
00021     As a special exception, permission is given to link this program
00022     with any edition of Qt, and distribute the resulting executable,
00023     without including the source code for Qt in the source distribution.
00024 */
00025 
00026 #include <qstring.h>
00027 #include <qkeycode.h>
00028 #include <qlayout.h>
00029 #include <qtimer.h>
00030 #include <qframe.h>
00031 #include <qlabel.h>
00032 
00033 #include <kdebug.h>
00034 #include <klocale.h>
00035 #include <kglobal.h>
00036 #include <kglobalsettings.h>
00037 
00038 #include "koglobals.h"
00039 #include "koprefs.h"
00040 #include "kodaymatrix.h"
00041 
00042 #include <kcalendarsystem.h>
00043 
00044 #include "navigatorbar.h"
00045 
00046 #include "kdatenavigator.h"
00047 
00048 KDateNavigator::KDateNavigator( QWidget *parent, const char *name )
00049   : QFrame( parent, name ), mBaseDate( 1970, 1, 1 )
00050 {
00051   QGridLayout* topLayout = new QGridLayout( this, 8, 8 );
00052 
00053   mNavigatorBar = new NavigatorBar( this );
00054   topLayout->addMultiCellWidget( mNavigatorBar, 0, 0, 0, 7 );
00055 
00056   connect( mNavigatorBar, SIGNAL( goPrevYear() ), SIGNAL( goPrevYear() ) );
00057   connect( mNavigatorBar, SIGNAL( goPrevMonth() ), SIGNAL( goPrevMonth() ) );
00058   connect( mNavigatorBar, SIGNAL( goNextMonth() ), SIGNAL( goNextMonth() ) );
00059   connect( mNavigatorBar, SIGNAL( goNextYear() ), SIGNAL( goNextYear() ) );
00060   connect( mNavigatorBar, SIGNAL( goMonth( int ) ), SIGNAL( goMonth( int ) ) );
00061 
00062   int i;
00063   QString generalFont = KGlobalSettings::generalFont().family();
00064 
00065   // Set up the heading fields.
00066   for( i = 0; i < 7; i++ ) {
00067     headings[i] = new QLabel( this );
00068     headings[i]->setFont( QFont( generalFont, 10, QFont::Bold ) );
00069     headings[i]->setAlignment( AlignCenter );
00070 
00071     topLayout->addWidget( headings[i], 1, i + 1 );
00072   }
00073 
00074   // Create the weeknumber labels
00075   for( i = 0; i < 6; i++ ) {
00076     weeknos[i] = new QLabel( this );
00077     weeknos[i]->setAlignment( AlignCenter );
00078     weeknos[i]->setFont( QFont( generalFont, 10 ) );
00079     weeknos[i]->installEventFilter( this );
00080 
00081     topLayout->addWidget( weeknos[i], i + 2, 0 );
00082   }
00083 
00084   mDayMatrix = new KODayMatrix( this, "KDateNavigator::dayMatrix" );
00085 
00086   connect( mDayMatrix, SIGNAL( selected( const KCal::DateList & ) ),
00087            SIGNAL( datesSelected( const KCal::DateList & ) ) );
00088 
00089   connect( mDayMatrix, SIGNAL( incidenceDropped( Incidence *, const QDate & ) ),
00090            SIGNAL( incidenceDropped( Incidence *, const QDate & ) ) );
00091   connect( mDayMatrix, SIGNAL( incidenceDroppedMove( Incidence * , const QDate & ) ),
00092            SIGNAL( incidenceDroppedMove( Incidence *, const QDate & ) ) );
00093 
00094 
00095   topLayout->addMultiCellWidget( mDayMatrix, 2, 7, 1, 7 );
00096 
00097   // read settings from configuration file.
00098   updateConfig();
00099 }
00100 
00101 KDateNavigator::~KDateNavigator()
00102 {
00103 }
00104 
00105 void KDateNavigator::setCalendar( Calendar *cal )
00106 {
00107   mDayMatrix->setCalendar( cal );
00108 }
00109 
00110 void KDateNavigator::setBaseDate( const QDate &date )
00111 {
00112   if ( date != mBaseDate ) {
00113     mBaseDate = date;
00114 
00115     updateDates();
00116     updateView();
00117 
00118     // Use the base date to show the monthname and year in the header
00119     KCal::DateList dates;
00120     dates.append( date );
00121     mNavigatorBar->selectDates( dates );
00122 
00123     repaint();
00124     mDayMatrix->repaint();
00125   }
00126 }
00127 
00128 QSizePolicy KDateNavigator::sizePolicy () const
00129 {
00130   return QSizePolicy( QSizePolicy::MinimumExpanding,
00131                       QSizePolicy::MinimumExpanding );
00132 }
00133 
00134 void KDateNavigator::updateToday()
00135 {
00136   mDayMatrix->recalculateToday();
00137   mDayMatrix->repaint();
00138 }
00139 QDate KDateNavigator::startDate() const
00140 {
00141   // Find the first day of the week of the current month.
00142   QDate dayone( mBaseDate.year(), mBaseDate.month(), mBaseDate.day() );
00143   int d2 = KOGlobals::self()->calendarSystem()->day( dayone );
00144   //int di = d1 - d2 + 1;
00145   dayone = dayone.addDays( -d2 + 1 );
00146 
00147 
00148   const KCalendarSystem *calsys = KOGlobals::self()->calendarSystem();
00149   int m_fstDayOfWkCalsys = calsys->dayOfWeek( dayone );
00150 
00151   // If month begins on Monday and Monday is first day of week,
00152   // month should begin on second line. Sunday doesn't have this problem.
00153   int nextLine = ( ( m_fstDayOfWkCalsys == 1) &&
00154                    ( KGlobal::locale()->weekStartDay() == 1 ) ) ? 7 : 0;
00155 
00156   // update the matrix dates
00157   int index = ( KGlobal::locale()->weekStartDay() == 1 ? 1 : 0 ) -
00158               m_fstDayOfWkCalsys - nextLine;
00159 
00160   dayone = dayone.addDays( index );
00161 
00162   return dayone;
00163 }
00164 QDate KDateNavigator::endDate() const
00165 {
00166   return startDate().addDays( 6*7 );
00167 }
00168 
00169 void KDateNavigator::updateDates()
00170 {
00171 // kdDebug(5850) << "KDateNavigator::updateDates(), this=" << this << endl;
00172   QDate dayone = startDate();
00173 
00174   mDayMatrix->updateView( dayone );
00175 
00176   const KCalendarSystem *calsys = KOGlobals::self()->calendarSystem();
00177 
00178   // set the week numbers.
00179   for( int i = 0; i < 6; i++ ) {
00180     // Use QDate's weekNumber method to determine the week number!
00181     QDate dtStart = mDayMatrix->getDate( i * 7 );
00182     QDate dtEnd = mDayMatrix->getDate( ( i + 1 ) * 7 - 1 );
00183     int weeknumstart = calsys->weekNumber( dtStart );
00184     int weeknumend = calsys->weekNumber( dtEnd );
00185     QString weeknum;
00186 
00187     if ( weeknumstart != weeknumend ) {
00188       weeknum = i18n("start/end week number of line in date picker", "%1/%2")
00189                 .arg( weeknumstart ).arg( weeknumend );
00190     } else {
00191       weeknum.setNum( weeknumstart );
00192     }
00193     weeknos[i]->setText( weeknum );
00194   }
00195 
00196 // each updateDates is followed by an updateView -> repaint is issued there !
00197 //  mDayMatrix->repaint();
00198 }
00199 
00200 void KDateNavigator::updateDayMatrix()
00201 {
00202   mDayMatrix->updateView();
00203   mDayMatrix->repaint();
00204 }
00205 
00206 
00207 void KDateNavigator::updateView()
00208 {
00209 //   kdDebug(5850) << "KDateNavigator::updateView(), view " << this << endl;
00210 
00211   updateDayMatrix();
00212   repaint();
00213 }
00214 
00215 void KDateNavigator::updateConfig()
00216 {
00217   int day;
00218   for( int i = 0; i < 7; i++ ) {
00219     // take the first letter of the day name to be the abbreviation
00220     if ( KGlobal::locale()->weekStartDay() == 1 ) {
00221       day = i + 1;
00222     } else {
00223       if ( i == 0 ) day = 7;
00224       else day = i;
00225     }
00226     QString dayName = KOGlobals::self()->calendarSystem()->weekDayName( day,
00227                                                                         true );
00228     if ( KOPrefs::instance()->mCompactDialogs ) dayName = dayName.left( 1 );
00229     headings[i]->setText( dayName );
00230   }
00231 
00232   // FIXME: Use actual config setting here
00233 //  setShowWeekNums( true );
00234 }
00235 
00236 void KDateNavigator::setShowWeekNums( bool enabled )
00237 {
00238   for( int i = 0; i < 6; i++ ) {
00239     if( enabled )
00240       weeknos[i]->show();
00241     else
00242       weeknos[i]->hide();
00243   }
00244 }
00245 
00246 void KDateNavigator::selectDates( const DateList &dateList )
00247 {
00248   if ( dateList.count() > 0 ) {
00249     mSelectedDates = dateList;
00250 
00251     updateDates();
00252 
00253     mDayMatrix->setSelectedDaysFrom( *( dateList.begin() ),
00254                                      *( --dateList.end() ) );
00255 
00256     updateView();
00257   }
00258 }
00259 
00260 void KDateNavigator::wheelEvent ( QWheelEvent *e )
00261 {
00262   if( e->delta() > 0 ) emit goPrevious();
00263   else emit goNext();
00264 
00265   e->accept();
00266 }
00267 
00268 bool KDateNavigator::eventFilter ( QObject *o, QEvent *e )
00269 {
00270   if ( e->type() == QEvent::MouseButtonPress ) {
00271     int i;
00272     for( i = 0; i < 6; ++i ) {
00273       if ( o == weeknos[ i ] ) {
00274         QDate weekstart = mDayMatrix->getDate( i * 7 );
00275         emit weekClicked( weekstart );
00276         break;
00277       }
00278     }
00279     return true;
00280   } else {
00281     return false;
00282   }
00283 }
00284 
00285 #include "kdatenavigator.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys