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   connect( mNavigatorBar, SIGNAL( goYear( int ) ), SIGNAL( goYear( int ) ) );
00062 
00063   int i;
00064   QString generalFont = KGlobalSettings::generalFont().family();
00065 
00066   // Set up the heading fields.
00067   for( i = 0; i < 7; i++ ) {
00068     mHeadings[i] = new QLabel( this );
00069     mHeadings[i]->setFont( QFont( generalFont, 10, QFont::Bold ) );
00070     mHeadings[i]->setAlignment( AlignCenter );
00071 
00072     topLayout->addWidget( mHeadings[i], 1, i + 1 );
00073   }
00074 
00075   // Create the weeknumber labels
00076   for( i = 0; i < 6; i++ ) {
00077     mWeeknos[i] = new QLabel( this );
00078     mWeeknos[i]->setAlignment( AlignCenter );
00079     mWeeknos[i]->setFont( QFont( generalFont, 10 ) );
00080     mWeeknos[i]->installEventFilter( this );
00081 
00082     topLayout->addWidget( mWeeknos[i], i + 2, 0 );
00083   }
00084 
00085   mDayMatrix = new KODayMatrix( this, "KDateNavigator::dayMatrix" );
00086 
00087   connect( mDayMatrix, SIGNAL( selected( const KCal::DateList & ) ),
00088            SIGNAL( datesSelected( const KCal::DateList & ) ) );
00089 
00090   connect( mDayMatrix, SIGNAL( incidenceDropped( Incidence *, const QDate & ) ),
00091            SIGNAL( incidenceDropped( Incidence *, const QDate & ) ) );
00092   connect( mDayMatrix, SIGNAL( incidenceDroppedMove( Incidence * , const QDate & ) ),
00093            SIGNAL( incidenceDroppedMove( Incidence *, const QDate & ) ) );
00094 
00095 
00096   topLayout->addMultiCellWidget( mDayMatrix, 2, 7, 1, 7 );
00097 
00098   // read settings from configuration file.
00099   updateConfig();
00100 }
00101 
00102 KDateNavigator::~KDateNavigator()
00103 {
00104 }
00105 
00106 void KDateNavigator::setCalendar( Calendar *cal )
00107 {
00108   mDayMatrix->setCalendar( cal );
00109 }
00110 
00111 void KDateNavigator::setBaseDate( const QDate &date )
00112 {
00113   if ( date != mBaseDate ) {
00114     mBaseDate = date;
00115 
00116     updateDates();
00117     updateView();
00118 
00119     // Use the base date to show the monthname and year in the header
00120     KCal::DateList dates;
00121     dates.append( date );
00122     mNavigatorBar->selectDates( dates );
00123 
00124     repaint();
00125     mDayMatrix->repaint();
00126   }
00127 }
00128 
00129 QSizePolicy KDateNavigator::sizePolicy () const
00130 {
00131   return QSizePolicy( QSizePolicy::MinimumExpanding,
00132                       QSizePolicy::MinimumExpanding );
00133 }
00134 
00135 void KDateNavigator::updateToday()
00136 {
00137   mDayMatrix->recalculateToday();
00138   mDayMatrix->repaint();
00139 }
00140 QDate KDateNavigator::startDate() const
00141 {
00142   // Find the first day of the week of the current month.
00143   QDate dayone( mBaseDate.year(), mBaseDate.month(), mBaseDate.day() );
00144   int d2 = KOGlobals::self()->calendarSystem()->day( dayone );
00145   //int di = d1 - d2 + 1;
00146   dayone = dayone.addDays( -d2 + 1 );
00147 
00148 
00149   const KCalendarSystem *calsys = KOGlobals::self()->calendarSystem();
00150   int m_fstDayOfWkCalsys = calsys->dayOfWeek( dayone );
00151   int weekstart = KGlobal::locale()->weekStartDay();
00152 
00153   // If month begins on Monday and Monday is first day of week,
00154   // month should begin on second line. Sunday doesn't have this problem.
00155   int nextLine = m_fstDayOfWkCalsys <= weekstart ? 7 : 0;
00156 
00157   // update the matrix dates
00158   int index = weekstart - 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     mWeeknos[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 void KDateNavigator::setUpdateNeeded()
00207 {
00208   mDayMatrix->setUpdateNeeded();
00209 }
00210 
00211 void KDateNavigator::updateView()
00212 {
00213 //   kdDebug(5850) << "KDateNavigator::updateView(), view " << this << endl;
00214 
00215   updateDayMatrix();
00216   repaint();
00217 }
00218 
00219 void KDateNavigator::updateConfig()
00220 {
00221   int day;
00222   int weekstart = KGlobal::locale()->weekStartDay();
00223   for( int i = 0; i < 7; i++ ) {
00224     day = weekstart + i <= 7 ? weekstart + i : ( weekstart + i ) % 7;
00225     QString dayName = KOGlobals::self()->calendarSystem()->weekDayName( day,
00226                                                                         true );
00227     if ( KOPrefs::instance()->mCompactDialogs ) dayName = dayName.left( 1 );
00228     mHeadings[i]->setText( dayName );
00229   }
00230 
00231   // FIXME: Use actual config setting here
00232 //  setShowWeekNums( true );
00233 }
00234 
00235 void KDateNavigator::setShowWeekNums( bool enabled )
00236 {
00237   for( int i = 0; i < 6; i++ ) {
00238     if( enabled )
00239       mWeeknos[i]->show();
00240     else
00241       mWeeknos[i]->hide();
00242   }
00243 }
00244 
00245 void KDateNavigator::selectDates( const DateList &dateList )
00246 {
00247   if ( dateList.count() > 0 ) {
00248     mSelectedDates = dateList;
00249 
00250     updateDates();
00251 
00252     mDayMatrix->setSelectedDaysFrom( *( dateList.begin() ),
00253                                      *( --dateList.end() ) );
00254 
00255     updateView();
00256   }
00257 }
00258 
00259 void KDateNavigator::wheelEvent ( QWheelEvent *e )
00260 {
00261   if( e->delta() > 0 ) emit goPrevious();
00262   else emit goNext();
00263 
00264   e->accept();
00265 }
00266 
00267 bool KDateNavigator::eventFilter ( QObject *o, QEvent *e )
00268 {
00269   if ( e->type() == QEvent::MouseButtonPress ) {
00270     int i;
00271     for( i = 0; i < 6; ++i ) {
00272       if ( o == mWeeknos[ i ] ) {
00273         QDate weekstart = mDayMatrix->getDate( i * 7 );
00274         emit weekClicked( weekstart );
00275         break;
00276       }
00277     }
00278     return true;
00279   } else {
00280     return false;
00281   }
00282 }
00283 
00284 #include "kdatenavigator.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys