00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
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
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
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
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
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
00142 QDate dayone( mBaseDate.year(), mBaseDate.month(), mBaseDate.day() );
00143 int d2 = KOGlobals::self()->calendarSystem()->day( dayone );
00144
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
00153
00154 int nextLine = m_fstDayOfWkCalsys <= weekstart ? 7 : 0;
00155
00156
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
00171 QDate dayone = startDate();
00172
00173 mDayMatrix->updateView( dayone );
00174
00175 const KCalendarSystem *calsys = KOGlobals::self()->calendarSystem();
00176
00177
00178 for( int i = 0; i < 6; i++ ) {
00179
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
00196
00197 }
00198
00199 void KDateNavigator::updateDayMatrix()
00200 {
00201 mDayMatrix->updateView();
00202 mDayMatrix->repaint();
00203 }
00204
00205
00206 void KDateNavigator::updateView()
00207 {
00208
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
00227
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"