• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdepim
  • Sitemap
  • Contact Us
 

korganizer

kdatenavigator.cpp

Go to the documentation of this file.
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 along
00018   with this program; if not, write to the Free Software Foundation, Inc.,
00019   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 "kdatenavigator.h"
00027 #include "navigatorbar.h"
00028 #include "koglobals.h"
00029 #include "koprefs.h"
00030 #include "kodaymatrix.h"
00031 
00032 #include <kdebug.h>
00033 #include <kcalendarsystem.h>
00034 #include <klocale.h>
00035 #include <kglobal.h>
00036 #include <kglobalsettings.h>
00037 
00038 #include <QString>
00039 #include <qnamespace.h>
00040 #include <QLayout>
00041 #include <QTimer>
00042 #include <QFrame>
00043 #include <QLabel>
00044 #include <QWheelEvent>
00045 #include <QGridLayout>
00046 #include <QEvent>
00047 
00048 KDateNavigator::KDateNavigator( QWidget *parent )
00049   : QFrame( parent ), mBaseDate( 1970, 1, 1 )
00050 {
00051   QGridLayout *topLayout = new QGridLayout( this );
00052   topLayout->setMargin( 0 );
00053   topLayout->setSpacing( 0 );
00054 
00055   mNavigatorBar = new NavigatorBar( this );
00056   topLayout->addWidget( mNavigatorBar, 0, 0, 1, 8 );
00057 
00058   connect( mNavigatorBar, SIGNAL(goPrevYear()), SIGNAL(goPrevYear()) );
00059   connect( mNavigatorBar, SIGNAL(goPrevMonth()), SIGNAL(goPrevMonth()) );
00060   connect( mNavigatorBar, SIGNAL(goNextMonth()), SIGNAL(goNextMonth()) );
00061   connect( mNavigatorBar, SIGNAL(goNextYear()), SIGNAL(goNextYear()) );
00062   connect( mNavigatorBar, SIGNAL(goMonth(int)), SIGNAL(goMonth(int)) );
00063   connect( mNavigatorBar, SIGNAL(goYear(int)), SIGNAL(goYear(int)) );
00064 
00065   int i;
00066   QString generalFont = KGlobalSettings::generalFont().family();
00067 
00068   // Set up the heading fields.
00069   for ( i = 0; i < 7; i++ ) {
00070     mHeadings[i] = new QLabel( this );
00071     mHeadings[i]->setFont( QFont( generalFont, 10, QFont::Bold ) );
00072     mHeadings[i]->setAlignment( Qt::AlignCenter );
00073 
00074     topLayout->addWidget( mHeadings[i], 1, i + 1 );
00075   }
00076 
00077   // Create the weeknumber labels
00078   for ( i = 0; i < 6; i++ ) {
00079     mWeeknos[i] = new QLabel( this );
00080     mWeeknos[i]->setAlignment( Qt::AlignCenter );
00081     mWeeknos[i]->setFont( QFont( generalFont, 10 ) );
00082     mWeeknos[i]->installEventFilter( this );
00083 
00084     topLayout->addWidget( mWeeknos[i], i + 2, 0 );
00085   }
00086 
00087   mDayMatrix = new KODayMatrix( this );
00088   mDayMatrix->setObjectName( "KDateNavigator::dayMatrix" );
00089 
00090   connect( mDayMatrix, SIGNAL(selected(const KCal::DateList &)),
00091            SIGNAL(datesSelected(const KCal::DateList &)) );
00092 
00093   connect( mDayMatrix, SIGNAL(incidenceDropped(Incidence *,const QDate &)),
00094            SIGNAL(incidenceDropped(Incidence *,const QDate &)) );
00095   connect( mDayMatrix, SIGNAL(incidenceDroppedMove(Incidence *,const QDate &)),
00096            SIGNAL(incidenceDroppedMove(Incidence *,const QDate &)) );
00097 
00098   topLayout->addWidget( mDayMatrix, 2, 1, 6, 7 );
00099 
00100   // read settings from configuration file.
00101   updateConfig();
00102 }
00103 
00104 KDateNavigator::~KDateNavigator()
00105 {
00106 }
00107 
00108 void KDateNavigator::setCalendar( Calendar *cal )
00109 {
00110   mDayMatrix->setCalendar( cal );
00111 }
00112 
00113 void KDateNavigator::setBaseDate( const QDate &date )
00114 {
00115   if ( date != mBaseDate ) {
00116     mBaseDate = date;
00117 
00118     updateDates();
00119     updateView();
00120 
00121     // Use the base date to show the monthname and year in the header
00122     KCal::DateList dates;
00123     dates.append( date );
00124     mNavigatorBar->selectDates( dates );
00125 
00126     repaint();
00127     mDayMatrix->repaint();
00128   }
00129 }
00130 
00131 QSizePolicy KDateNavigator::sizePolicy () const
00132 {
00133   return QSizePolicy( QSizePolicy::MinimumExpanding,
00134                       QSizePolicy::MinimumExpanding );
00135 }
00136 
00137 void KDateNavigator::updateToday()
00138 {
00139   mDayMatrix->recalculateToday();
00140   mDayMatrix->repaint();
00141 }
00142 
00143 QDate KDateNavigator::startDate() const
00144 {
00145   // Find the first day of the week of the current month.
00146   QDate dayone( mBaseDate.year(), mBaseDate.month(), mBaseDate.day() );
00147   int d2 = KOGlobals::self()->calendarSystem()->day( dayone );
00148   //int di = d1 - d2 + 1;
00149   dayone = dayone.addDays( -d2 + 1 );
00150 
00151   const KCalendarSystem *calsys = KOGlobals::self()->calendarSystem();
00152   int m_fstDayOfWkCalsys = calsys->dayOfWeek( dayone );
00153   int weekstart = KGlobal::locale()->weekStartDay();
00154 
00155   // If month begins on Monday and Monday is first day of week,
00156   // month should begin on second line. Sunday doesn't have this problem.
00157   int nextLine = m_fstDayOfWkCalsys <= weekstart ? 7 : 0;
00158 
00159   // update the matrix dates
00160   int index = weekstart - m_fstDayOfWkCalsys - nextLine;
00161 
00162   dayone = dayone.addDays( index );
00163 
00164   return dayone;
00165 }
00166 
00167 QDate KDateNavigator::endDate() const
00168 {
00169   return startDate().addDays( 6 * 7 );
00170 }
00171 
00172 void KDateNavigator::updateDates()
00173 {
00174   QDate dayone = startDate();
00175 
00176   mDayMatrix->updateView( dayone );
00177 
00178   const KCalendarSystem *calsys = KOGlobals::self()->calendarSystem();
00179 
00180   // set the week numbers.
00181   for ( int i = 0; i < 6; i++ ) {
00182     // Use QDate's weekNumber method to determine the week number!
00183     QDate dtStart = mDayMatrix->getDate( i * 7 );
00184     QDate dtEnd = mDayMatrix->getDate( ( i + 1 ) * 7 - 1 );
00185     int weeknumstart = calsys->weekNumber( dtStart );
00186     int weeknumend = calsys->weekNumber( dtEnd );
00187     QString weeknum;
00188 
00189     if ( weeknumstart != weeknumend ) {
00190       weeknum = i18nc( "start/end week number of line in date picker", "%1/%2",
00191                        weeknumstart, weeknumend );
00192     } else {
00193       weeknum.setNum( weeknumstart );
00194     }
00195     mWeeknos[i]->setText( weeknum );
00196     mWeeknos[i]->setToolTip( i18n( "Scroll to week number %1", weeknum ) );
00197     mWeeknos[i]->setWhatsThis(
00198       i18n( "Click here to scroll the display to week number %1 "
00199             "of the currently displayed year.", weeknum ) );
00200   }
00201 
00202 // each updateDates is followed by an updateView -> repaint is issued there !
00203 //  mDayMatrix->repaint();
00204 }
00205 
00206 void KDateNavigator::updateDayMatrix()
00207 {
00208   mDayMatrix->updateView();
00209   mDayMatrix->repaint();
00210 }
00211 
00212 void KDateNavigator::updateView()
00213 {
00214   updateDayMatrix();
00215   repaint();
00216 }
00217 
00218 void KDateNavigator::updateConfig()
00219 {
00220   int day;
00221   int weekstart = KGlobal::locale()->weekStartDay();
00222   for ( int i = 0; i < 7; i++ ) {
00223     day = weekstart + i <= 7 ? weekstart + i : ( weekstart + i ) % 7;
00224     QString dayName =
00225       KOGlobals::self()->calendarSystem()->weekDayName( day, KCalendarSystem::ShortDayName );
00226     if ( KOPrefs::instance()->mCompactDialogs ) {
00227       dayName = dayName.left( 1 );
00228     }
00229     QString longDayName =
00230       KOGlobals::self()->calendarSystem()->weekDayName( day, KCalendarSystem::LongDayName );
00231     mHeadings[i]->setText( dayName );
00232     mHeadings[i]->setToolTip( i18n( "%1", longDayName ) );
00233     mHeadings[i]->setWhatsThis( i18n( "A column header of the %1 dates in the month.", longDayName ) );
00234   }
00235 
00236   // FIXME: Use actual config setting here
00237 //  setShowWeekNums( true );
00238 }
00239 
00240 void KDateNavigator::setShowWeekNums( bool enabled )
00241 {
00242   for ( int i = 0; i < 6; i++ ) {
00243     if( enabled ) {
00244       mWeeknos[i]->show();
00245     } else {
00246       mWeeknos[i]->hide();
00247     }
00248   }
00249 }
00250 
00251 void KDateNavigator::selectMonthHelper( int monthDifference )
00252 {
00253   QDate baseDateNextMonth = KOGlobals::self()->calendarSystem()->addMonths(
00254                                             mBaseDate, monthDifference );
00255 
00256   DateList newSelection = mSelectedDates;
00257   for ( int i = 0; i < mSelectedDates.count(); i++ ) {
00258     newSelection[i] = KOGlobals::self()->calendarSystem()->addMonths(
00259                                       newSelection[i], monthDifference );
00260   }
00261 
00262   setBaseDate( baseDateNextMonth );
00263   mSelectedDates = newSelection;
00264   mDayMatrix->setSelectedDaysFrom( *( newSelection.begin() ),
00265                                    *( --newSelection.end() ) );
00266   updateView();
00267 }
00268 
00269 void KDateNavigator::selectNextMonth()
00270 {
00271   selectMonthHelper( 1 );
00272 }
00273 
00274 void KDateNavigator::selectPreviousMonth()
00275 {
00276   selectMonthHelper( -1 );
00277 }
00278 
00279 void KDateNavigator::selectDates( const DateList &dateList )
00280 {
00281   if ( dateList.count() > 0 ) {
00282     mSelectedDates = dateList;
00283 
00284     updateDates();
00285 
00286     mDayMatrix->setSelectedDaysFrom( *( dateList.begin() ),
00287                                      *( --dateList.end() ) );
00288 
00289     updateView();
00290   }
00291 }
00292 
00293 void KDateNavigator::wheelEvent ( QWheelEvent *e )
00294 {
00295   if ( e->delta() > 0 ) {
00296     emit goPrevious();
00297   } else {
00298     emit goNext();
00299   }
00300   e->accept();
00301 }
00302 
00303 bool KDateNavigator::eventFilter ( QObject *o, QEvent *e )
00304 {
00305   if ( e->type() == QEvent::MouseButtonPress ) {
00306     int i;
00307     for ( i = 0; i < 6; ++i ) {
00308       if ( o == mWeeknos[ i ] ) {
00309         QDate weekstart = mDayMatrix->getDate( i * 7 );
00310         emit weekClicked( weekstart );
00311         break;
00312       }
00313     }
00314     return true;
00315   } else {
00316     return false;
00317   }
00318 }
00319 
00320 #include "kdatenavigator.moc"

korganizer

Skip menu "korganizer"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members

kdepim

Skip menu "kdepim"
  • akonadi
  •   clients
  •   kabc
  •   kcal
  •   kcm
  • akregator
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt
  • kdgantt1
  • kjots
  • kleopatra
  • kmail
  • kmobiletools
  • knode
  • knotes
  • kontact
  • kontactinterfaces
  • korganizer
  •   korgac
  • kpilot
  • ktimetracker
  • libkdepim
  • libkholidays
  • libkleo
  • libkpgp
  • maildir
Generated for kdepim by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal