korganizer

datenavigator.cpp

00001 /*
00002     This file is part of KOrganizer.
00003     Copyright (c) 2002 Cornelius Schumacher <schumacher@kde.org>
00004     Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
00005 
00006     This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019 
00020     As a special exception, permission is given to link this program
00021     with any edition of Qt, and distribute the resulting executable,
00022     without including the source code for Qt in the source distribution.
00023 */
00024 
00025 #include "datenavigator.h"
00026 
00027 #include "koglobals.h"
00028 
00029 #include <kcalendarsystem.h>
00030 
00031 #include <kdebug.h>
00032 #include <kglobal.h>
00033 #include <klocale.h>
00034 
00035 using namespace KCal;
00036 
00037 DateNavigator::DateNavigator( QObject *parent, const char *name )
00038   : QObject( parent, name )
00039 {
00040   mSelectedDates.append( QDate::currentDate() );
00041 }
00042 
00043 DateNavigator::~DateNavigator()
00044 {
00045 }
00046 
00047 DateList DateNavigator::selectedDates()
00048 {
00049   return mSelectedDates;
00050 }
00051 
00052 int DateNavigator::datesCount() const
00053 {
00054   return mSelectedDates.count();
00055 }
00056 
00057 void DateNavigator::selectDates( const DateList &dateList )
00058 {
00059   if (dateList.count() > 0) {
00060     mSelectedDates = dateList;
00061 
00062     emitSelected();
00063   }
00064 }
00065 
00066 void DateNavigator::selectDate( const QDate &date )
00067 {
00068   QDate d = date;
00069 
00070   if ( !d.isValid() ) {
00071     kdDebug(5850) << "DateNavigator::selectDates(QDate): an invalid date was passed as a parameter!" << endl;
00072     d = QDate::currentDate();
00073   }
00074 
00075   mSelectedDates.clear();
00076   mSelectedDates.append( d );
00077 
00078   emitSelected();
00079 }
00080 
00081 void DateNavigator::selectDates( int count )
00082 {
00083   selectDates( mSelectedDates.first(), count );
00084 }
00085 
00086 void DateNavigator::selectDates( const QDate &d, int count, const QDate &preferredMonth )
00087 {
00088   DateList dates;
00089 
00090   int i;
00091   for( i = 0; i < count; ++i ) {
00092     dates.append( d.addDays( i ) );
00093   }
00094 
00095   mSelectedDates = dates;
00096 
00097   emitSelected( preferredMonth );
00098 }
00099 
00100 void DateNavigator::selectWeekByDay( int weekDay, const QDate &d, const QDate &preferredMonth )
00101 {
00102   int dateCount = mSelectedDates.count();
00103   bool weekStart = ( weekDay == KGlobal::locale()->weekStartDay() );
00104   if ( weekStart && dateCount == 7 ) {
00105     selectWeek( d, preferredMonth );
00106   } else {
00107     selectDates( d, dateCount, preferredMonth );
00108   }
00109 }
00110 
00111 void DateNavigator::selectWeek()
00112 {
00113   selectWeek( mSelectedDates.first() );
00114 }
00115 
00116 void DateNavigator::selectWeek( const QDate &d, const QDate &preferredMonth )
00117 {
00118   int dayOfWeek = KOGlobals::self()->calendarSystem()->dayOfWeek( d );
00119 
00120   int weekStart = KGlobal::locale()->weekStartDay();
00121 
00122   QDate firstDate = d.addDays( weekStart - dayOfWeek );
00123 
00124   if ( weekStart != 1 && dayOfWeek < weekStart ) {
00125     firstDate = firstDate.addDays( -7 );
00126   }
00127 
00128   selectDates( firstDate, 7, preferredMonth );
00129 }
00130 
00131 void DateNavigator::selectWorkWeek()
00132 {
00133   selectWorkWeek( mSelectedDates.first() );
00134 }
00135 
00136 void DateNavigator::selectWorkWeek( const QDate &d )
00137 {
00138   int weekStart = KGlobal::locale()->weekStartDay();
00139 
00140   int dayOfWeek = KOGlobals::self()->calendarSystem()->dayOfWeek( d );
00141 
00142   QDate currentDate = d.addDays( weekStart - dayOfWeek );
00143 
00144   if ( weekStart != 1 && dayOfWeek < weekStart ) {
00145     currentDate = currentDate.addDays( -7 );
00146   }
00147 
00148   mSelectedDates.clear();
00149   int mask = KOGlobals::self()->getWorkWeekMask();
00150 
00151   for ( int i = 0; i < 7; ++i ) {
00152     if( (1<< ((i + weekStart + 6) % 7)) & (mask) ) {
00153     mSelectedDates.append( currentDate.addDays(i) );
00154     }
00155   }
00156 
00157   emitSelected();
00158 }
00159 
00160 void DateNavigator::selectToday()
00161 {
00162   QDate d = QDate::currentDate();
00163 
00164   int dateCount = mSelectedDates.count();
00165 
00166   if ( dateCount == 7 ) {
00167     selectWeek( d );
00168   } else if ( dateCount == 5 ) {
00169     selectWorkWeek( d );
00170   } else {
00171     selectDates( d, dateCount );
00172   }
00173 }
00174 
00175 void DateNavigator::selectPreviousYear()
00176 {
00177   QDate firstSelected = mSelectedDates.first();
00178   int weekDay = firstSelected.dayOfWeek();
00179   firstSelected = KOGlobals::self()->calendarSystem()->addYears( firstSelected, -1 );
00180 
00181   selectWeekByDay( weekDay, firstSelected );
00182 }
00183 
00184 void DateNavigator::selectPreviousMonth( const QDate &currentMonth,
00185                                          const QDate &selectionLowerLimit,
00186                                          const QDate &selectionUpperLimit )
00187 {
00188   shiftMonth( currentMonth,
00189               selectionLowerLimit,
00190               selectionUpperLimit,
00191               -1 );
00192 }
00193 
00194 void DateNavigator::selectPreviousWeek()
00195 {
00196   QDate firstSelected = mSelectedDates.first();
00197   int weekDay = firstSelected.dayOfWeek();
00198   firstSelected = KOGlobals::self()->calendarSystem()->addDays( firstSelected, -7 );
00199 
00200   selectWeekByDay( weekDay, firstSelected );
00201 }
00202 
00203 void DateNavigator::selectNextWeek()
00204 {
00205   QDate firstSelected = mSelectedDates.first();
00206   int weekDay = firstSelected.dayOfWeek();
00207 
00208   firstSelected = KOGlobals::self()->calendarSystem()->addDays( firstSelected, 7 );
00209 
00210   selectWeekByDay( weekDay, firstSelected );
00211 }
00212 
00213 void DateNavigator::shiftMonth( const QDate &currentMonth,
00214                                 const QDate &selectionLowerLimit,
00215                                 const QDate &selectionUpperLimit,
00216                                 int offset )
00217 {
00218   const KCalendarSystem *calSys = KOGlobals::self()->calendarSystem();
00219 
00220   QDate firstSelected = mSelectedDates.first();
00221   int weekDay = firstSelected.dayOfWeek();
00222   firstSelected = calSys->addMonths( firstSelected, offset );
00223 
00224   /* Don't trust firstSelected to calculate the nextMonth. firstSelected
00225      can belong to a month other than currentMonth because KDateNavigator
00226      displays 7*6 days. firstSelected should only be used for selection
00227      purposes */
00228   const QDate nextMonth = currentMonth.isValid() ?
00229                           calSys->addMonths( currentMonth, offset ) : firstSelected;
00230 
00231   /* When firstSelected doesn't belong to currentMonth it can happen
00232      that the new selection won't be visible on our KDateNavigators
00233      so we must adjust it */
00234   if ( selectionLowerLimit.isValid() &&
00235        firstSelected < selectionLowerLimit ) {
00236     firstSelected = selectionLowerLimit;
00237   } else if ( selectionUpperLimit.isValid() &&
00238               firstSelected > selectionUpperLimit ) {
00239     firstSelected = selectionUpperLimit.addDays( -6 );
00240   }
00241 
00242   selectWeekByDay( weekDay, firstSelected, nextMonth );
00243 }
00244 
00245 void DateNavigator::selectNextMonth( const QDate &currentMonth,
00246                                      const QDate &selectionLowerLimit,
00247                                      const QDate &selectionUpperLimit )
00248 {
00249   shiftMonth( currentMonth,
00250               selectionLowerLimit,
00251               selectionUpperLimit,
00252               1 );
00253 }
00254 
00255 void DateNavigator::selectNextYear()
00256 {
00257   QDate firstSelected = mSelectedDates.first();
00258   int weekDay = firstSelected.dayOfWeek();
00259   firstSelected = KOGlobals::self()->calendarSystem()->addYears( firstSelected, 1 );
00260 
00261   selectWeekByDay( weekDay, firstSelected );
00262 }
00263 
00264 void DateNavigator::selectPrevious()
00265 {
00266   int offset = -7;
00267   if ( datesCount() == 1 ) {
00268     offset = -1;
00269   }
00270 
00271   selectDates( mSelectedDates.first().addDays( offset ), datesCount() );
00272 }
00273 
00274 void DateNavigator::selectNext()
00275 {
00276   int offset = 7;
00277   if ( datesCount() == 1 ) {
00278     offset = 1;
00279   }
00280 
00281   selectDates( mSelectedDates.first().addDays( offset ), datesCount() );
00282 }
00283 
00284 void DateNavigator::selectMonth( int month )
00285 {
00286   const KCalendarSystem *calSys = KOGlobals::self()->calendarSystem();
00287 
00288   QDate firstSelected = mSelectedDates.first();
00289   int weekDay = firstSelected.dayOfWeek();
00290 
00291   int day = calSys->day( firstSelected );
00292   calSys->setYMD( firstSelected, calSys->year( firstSelected ), month, 1 );
00293   int days = calSys->daysInMonth( firstSelected );
00294   // As day we use either the selected date, or if the month has less days
00295   // than that, we use the max day of that month
00296   if ( day > days ) {
00297     day = days;
00298   }
00299   QDate requestedMonth;
00300   calSys->setYMD( firstSelected, calSys->year( firstSelected ), month, day );
00301   calSys->setYMD( requestedMonth, calSys->year( firstSelected ), month, 1 );
00302 
00303   selectWeekByDay( weekDay, firstSelected, requestedMonth );
00304 }
00305 
00306 void DateNavigator::selectYear( int year )
00307 {
00308   QDate firstSelected = mSelectedDates.first();
00309   int deltaYear = year - KOGlobals::self()->calendarSystem()->year( firstSelected );
00310   firstSelected = KOGlobals::self()->calendarSystem()->addYears( firstSelected, deltaYear );
00311 
00312   int weekDay = firstSelected.dayOfWeek();
00313   selectWeekByDay( weekDay, firstSelected );
00314 }
00315 
00316 void DateNavigator::emitSelected( const QDate &preferredMonth )
00317 {
00318   emit datesSelected( mSelectedDates, preferredMonth );
00319 }
00320 
00321 #include "datenavigator.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys