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 TQt, and distribute the resulting executable, 00023 without including the source code for TQt in the source distribution. 00024 */ 00025 00026 #include <tqstring.h> 00027 #include <tqkeycode.h> 00028 #include <tqlayout.h> 00029 #include <tqtimer.h> 00030 #include <tqframe.h> 00031 #include <tqlabel.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( TQWidget *parent, const char *name ) 00049 : TQFrame( parent, name ), mBaseDate( 1970, 1, 1 ) 00050 { 00051 TQGridLayout* topLayout = new TQGridLayout( this, 8, 8 ); 00052 00053 mNavigatorBar = new NavigatorBar( this ); 00054 topLayout->addMultiCellWidget( mNavigatorBar, 0, 0, 0, 7 ); 00055 00056 connect( mNavigatorBar, TQT_SIGNAL( prevYearClicked() ), TQT_SIGNAL( prevYearClicked() ) ); 00057 connect( mNavigatorBar, TQT_SIGNAL( prevMonthClicked() ), TQT_SIGNAL( prevMonthClicked() ) ); 00058 connect( mNavigatorBar, TQT_SIGNAL( nextMonthClicked() ), TQT_SIGNAL( nextMonthClicked() ) ); 00059 connect( mNavigatorBar, TQT_SIGNAL( nextYearClicked() ), TQT_SIGNAL( nextYearClicked() ) ); 00060 connect( mNavigatorBar, TQT_SIGNAL( monthSelected( int ) ), TQT_SIGNAL( monthSelected( int ) ) ); 00061 connect( mNavigatorBar, TQT_SIGNAL( yearSelected( int ) ), TQT_SIGNAL( yearSelected( int ) ) ); 00062 00063 int i; 00064 TQString generalFont = KGlobalSettings::generalFont().family(); 00065 00066 // Set up the heading fields. 00067 for( i = 0; i < 7; i++ ) { 00068 mHeadings[i] = new TQLabel( this ); 00069 mHeadings[i]->setFont( TQFont( generalFont, 10, TQFont::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 TQLabel( this ); 00078 mWeeknos[i]->setAlignment( AlignCenter ); 00079 mWeeknos[i]->setFont( TQFont( 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, TQT_SIGNAL( selected( const KCal::DateList & ) ), 00088 TQT_SIGNAL( datesSelected( const KCal::DateList & ) ) ); 00089 00090 connect( mDayMatrix, TQT_SIGNAL( incidenceDropped( Incidence *, const TQDate & ) ), 00091 TQT_SIGNAL( incidenceDropped( Incidence *, const TQDate & ) ) ); 00092 connect( mDayMatrix, TQT_SIGNAL( incidenceDroppedMove( Incidence * , const TQDate & ) ), 00093 TQT_SIGNAL( incidenceDroppedMove( Incidence *, const TQDate & ) ) ); 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 TQDate &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 TQSizePolicy KDateNavigator::sizePolicy () const 00130 { 00131 return TQSizePolicy( TQSizePolicy::MinimumExpanding, 00132 TQSizePolicy::MinimumExpanding ); 00133 } 00134 00135 void KDateNavigator::updateToday() 00136 { 00137 mDayMatrix->recalculateToday(); 00138 mDayMatrix->repaint(); 00139 } 00140 00141 TQDate KDateNavigator::startDate() const 00142 { 00143 // Find the first day of the week of the current month. 00144 TQDate dayone( mBaseDate.year(), mBaseDate.month(), mBaseDate.day() ); 00145 int d2 = KOGlobals::self()->calendarSystem()->day( dayone ); 00146 //int di = d1 - d2 + 1; 00147 dayone = dayone.addDays( -d2 + 1 ); 00148 00149 00150 const KCalendarSystem *calsys = KOGlobals::self()->calendarSystem(); 00151 int m_fstDayOfWkCalsys = calsys->dayOfWeek( dayone ); 00152 int weekstart = KGlobal::locale()->weekStartDay(); 00153 00154 // If month begins on Monday and Monday is first day of week, 00155 // month should begin on second line. Sunday doesn't have this problem. 00156 int nextLine = m_fstDayOfWkCalsys <= weekstart ? 7 : 0; 00157 00158 // update the matrix dates 00159 int index = weekstart - m_fstDayOfWkCalsys - nextLine; 00160 00161 dayone = dayone.addDays( index ); 00162 00163 return dayone; 00164 } 00165 00166 TQDate KDateNavigator::endDate() const 00167 { 00168 return startDate().addDays( 6*7 ); 00169 } 00170 00171 void KDateNavigator::updateDates() 00172 { 00173 // kdDebug(5850) << "KDateNavigator::updateDates(), this=" << this << endl; 00174 TQDate 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 TQDate's weekNumber method to determine the week number! 00183 TQDate dtStart = mDayMatrix->getDate( i * 7 ); 00184 TQDate dtEnd = mDayMatrix->getDate( ( i + 1 ) * 7 - 1 ); 00185 int weeknumstart = calsys->weekNumber( dtStart ); 00186 int weeknumend = calsys->weekNumber( dtEnd ); 00187 TQString weeknum; 00188 00189 if ( weeknumstart != weeknumend ) { 00190 weeknum = i18n("start/end week number of line in date picker", "%1/%2") 00191 .arg( weeknumstart ).arg( weeknumend ); 00192 } else { 00193 weeknum.setNum( weeknumstart ); 00194 } 00195 mWeeknos[i]->setText( weeknum ); 00196 } 00197 00198 // each updateDates is followed by an updateView -> repaint is issued there ! 00199 // mDayMatrix->repaint(); 00200 } 00201 00202 void KDateNavigator::updateDayMatrix() 00203 { 00204 mDayMatrix->updateView(); 00205 mDayMatrix->repaint(); 00206 } 00207 00208 void KDateNavigator::setUpdateNeeded() 00209 { 00210 mDayMatrix->setUpdateNeeded(); 00211 } 00212 00213 TQDate KDateNavigator::month() const 00214 { 00215 TQDate firstCell = startDate(); 00216 const KCalendarSystem *calSys = KOGlobals::self()->calendarSystem(); 00217 00218 if ( calSys->day( firstCell ) == 1 ) { 00219 return firstCell; 00220 } else { 00221 calSys->setYMD( firstCell, calSys->year( firstCell ), calSys->month( firstCell ), 1 ); 00222 return calSys->addMonths( firstCell, 1 ); 00223 } 00224 } 00225 00226 void KDateNavigator::updateView() 00227 { 00228 // kdDebug(5850) << "KDateNavigator::updateView(), view " << this << endl; 00229 00230 updateDayMatrix(); 00231 repaint(); 00232 } 00233 00234 void KDateNavigator::updateConfig() 00235 { 00236 int day; 00237 int weekstart = KGlobal::locale()->weekStartDay(); 00238 for( int i = 0; i < 7; i++ ) { 00239 day = weekstart + i <= 7 ? weekstart + i : ( weekstart + i ) % 7; 00240 TQString dayName = KOGlobals::self()->calendarSystem()->weekDayName( day, 00241 true ); 00242 if ( KOPrefs::instance()->mCompactDialogs ) dayName = dayName.left( 1 ); 00243 mHeadings[i]->setText( dayName ); 00244 } 00245 00246 // FIXME: Use actual config setting here 00247 // setShowWeekNums( true ); 00248 } 00249 00250 void KDateNavigator::setShowWeekNums( bool enabled ) 00251 { 00252 for( int i = 0; i < 6; i++ ) { 00253 if ( enabled ) { 00254 mWeeknos[i]->show(); 00255 } else { 00256 mWeeknos[i]->hide(); 00257 } 00258 } 00259 } 00260 00261 void KDateNavigator::selectDates( const DateList &dateList ) 00262 { 00263 if ( dateList.count() > 0 ) { 00264 mSelectedDates = dateList; 00265 00266 updateDates(); 00267 00268 mDayMatrix->setSelectedDaysFrom( *( dateList.begin() ), 00269 *( --dateList.end() ) ); 00270 00271 updateView(); 00272 } 00273 } 00274 00275 void KDateNavigator::wheelEvent( TQWheelEvent *e ) 00276 { 00277 if ( e->delta() > 0 ) { 00278 emit goPrevious(); 00279 } else { 00280 emit goNext(); 00281 } 00282 00283 e->accept(); 00284 } 00285 00286 bool KDateNavigator::eventFilter( TQObject *o, TQEvent *e ) 00287 { 00288 if ( e->type() == TQEvent::MouseButtonPress ) { 00289 int i; 00290 for( i = 0; i < 6; ++i ) { 00291 if ( TQT_BASE_OBJECT(o) == TQT_BASE_OBJECT(mWeeknos[ i ]) ) { 00292 TQDate weekstart = mDayMatrix->getDate( i * 7 ); 00293 emit weekClicked( weekstart ); 00294 break; 00295 } 00296 } 00297 return true; 00298 } else { 00299 return false; 00300 } 00301 } 00302 00303 #include "kdatenavigator.moc"