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     mHeadings[i] = new QLabel( this );
00068     mHeadings[i]->setFont( QFont( generalFont, 10, QFont::Bold ) );
00069     mHeadings[i]->setAlignment( AlignCenter );
00070 
00071     topLayout->addWidget( mHeadings[i], 1, i + 1 );
00072   }
00073 
00074   // Create the weeknumber labels
00075   for( i = 0; i < 6; i++ ) {
00076     mWeeknos[i] = new QLabel( this );
00077     mWeeknos[i]->setAlignment( AlignCenter );
00078     mWeeknos[i]->setFont( QFont( generalFont, 10 ) );
00079     mWeeknos[i]->installEventFilter( this );
00080 
00081     topLayout->addWidget( mWeeknos[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   int weekstart = KGlobal::locale()->weekStartDay();
00151 
00152   // If month begins on Monday and Monday is first day of week,
00153   // month should begin on second line. Sunday doesn't have this problem.
00154   int nextLine = m_fstDayOfWkCalsys <= weekstart ? 7 : 0;
00155 
00156   // update the matrix dates
00157   int index = weekstart - m_fstDayOfWkCalsys - nextLine;
00158 
00159   dayone = dayone.addDays( index );
00160 
00161   return dayone;
00162 }
00163 QDate KDateNavigator::endDate() const
00164 {
00165   return startDate().addDays( 6*7 );
00166 }
00167 
00168 void KDateNavigator::updateDates()
00169 {
00170 // kdDebug(5850) << "KDateNavigator::updateDates(), this=" << this << endl;
00171   QDate dayone = startDate();
00172 
00173   mDayMatrix->updateView( dayone );
00174 
00175   const KCalendarSystem *calsys = KOGlobals::self()->calendarSystem();
00176 
00177   // set the week numbers.
00178   for( int i = 0; i < 6; i++ ) {
00179     // Use QDate's weekNumber method to determine the week number!
00180     QDate dtStart = mDayMatrix->getDate( i * 7 );
00181     QDate dtEnd = mDayMatrix->getDate( ( i + 1 ) * 7 - 1 );
00182     int weeknumstart = calsys->weekNumber( dtStart );
00183     int weeknumend = calsys->weekNumber( dtEnd );
00184     QString weeknum;
00185 
00186     if ( weeknumstart != weeknumend ) {
00187       weeknum = i18n("start/end week number of line in date picker", "%1/%2")
00188                 .arg( weeknumstart ).arg( weeknumend );
00189     } else {
00190       weeknum.setNum( weeknumstart );
00191     }
00192     mWeeknos[i]->setText( weeknum );
00193   }
00194 
00195 // each updateDates is followed by an updateView -> repaint is issued there !
00196 //  mDayMatrix->repaint();
00197 }
00198 
00199 void KDateNavigator::updateDayMatrix()
00200 {
00201   mDayMatrix->updateView();
00202   mDayMatrix->repaint();
00203 }
00204 
00205 
00206 void KDateNavigator::updateView()
00207 {
00208 //   kdDebug(5850) << "KDateNavigator::updateView(), view " << this << endl;
00209 
00210   updateDayMatrix();
00211   repaint();
00212 }
00213 
00214 void KDateNavigator::updateConfig()
00215 {
00216   int day;
00217   int weekstart = KGlobal::locale()->weekStartDay();
00218   for( int i = 0; i < 7; i++ ) {
00219     day = weekstart + i <= 7 ? weekstart + i : ( weekstart + i ) % 7;
00220     QString dayName = KOGlobals::self()->calendarSystem()->weekDayName( day,
00221                                                                         true );
00222     if ( KOPrefs::instance()->mCompactDialogs ) dayName = dayName.left( 1 );
00223     mHeadings[i]->setText( dayName );
00224   }
00225 
00226   // FIXME: Use actual config setting here
00227 //  setShowWeekNums( true );
00228 }
00229 
00230 void KDateNavigator::setShowWeekNums( bool enabled )
00231 {
00232   for( int i = 0; i < 6; i++ ) {
00233     if( enabled )
00234       mWeeknos[i]->show();
00235     else
00236       mWeeknos[i]->hide();
00237   }
00238 }
00239 
00240 void KDateNavigator::selectDates( const DateList &dateList )
00241 {
00242   if ( dateList.count() > 0 ) {
00243     mSelectedDates = dateList;
00244 
00245     updateDates();
00246 
00247     mDayMatrix->setSelectedDaysFrom( *( dateList.begin() ),
00248                                      *( --dateList.end() ) );
00249 
00250     updateView();
00251   }
00252 }
00253 
00254 void KDateNavigator::wheelEvent ( QWheelEvent *e )
00255 {
00256   if( e->delta() > 0 ) emit goPrevious();
00257   else emit goNext();
00258 
00259   e->accept();
00260 }
00261 
00262 bool KDateNavigator::eventFilter ( QObject *o, QEvent *e )
00263 {
00264   if ( e->type() == QEvent::MouseButtonPress ) {
00265     int i;
00266     for( i = 0; i < 6; ++i ) {
00267       if ( o == mWeeknos[ i ] ) {
00268         QDate weekstart = mDayMatrix->getDate( i * 7 );
00269         emit weekClicked( weekstart );
00270         break;
00271       }
00272     }
00273     return true;
00274   } else {
00275     return false;
00276   }
00277 }
00278 
00279 #include "kdatenavigator.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys