00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "odatebookmonth.h"
00024 #include "datebooktypes.h"
00025
00026 #include <qpe/config.h>
00027 #include <qpe/datebookmonth.h>
00028
00029 #include <qpe/qpeapplication.h>
00030
00031 #include <qtoolbutton.h>
00032 #include <qspinbox.h>
00033 #include <qcombobox.h>
00034 #include <qvaluestack.h>
00035 #include <qwhatsthis.h>
00036
00037
00038
00039
00040 class ODateBookMonthTablePrivate
00041 {
00042 public:
00043 ODateBookMonthTablePrivate() {};
00044 ~ODateBookMonthTablePrivate() { mMonthEvents.clear(); };
00045
00046 QValueList<EffectiveEvent> mMonthEvents;
00047 bool onMonday;
00048 };
00049
00050 ODateBookMonthTable::ODateBookMonthTable( QWidget *parent, const char *name,
00051 DateBookDBHoliday *newDb )
00052 : QTable( 6, 7, parent, name ),
00053 db( newDb )
00054 {
00055 d = new ODateBookMonthTablePrivate();
00056 selYear = -1;
00057 selMonth = -1;
00058 selDay = -1;
00059
00060
00061 year = -1;
00062 month = -1;
00063 day = -1;
00064
00065 Config cfg( "qpe" );
00066 cfg.setGroup( "Time" );
00067 d->onMonday = cfg.readBoolEntry( "MONDAY" );
00068
00069 horizontalHeader()->setResizeEnabled( FALSE );
00070
00071 for ( int i = 0; i < 7; i++ ){
00072 horizontalHeader()->resizeSection( i, 30 );
00073 setColumnStretchable( i, TRUE );
00074 }
00075 setupLabels();
00076
00077 verticalHeader()->hide();
00078 setLeftMargin( 0 );
00079 for ( int i = 0; i < 6; ++i )
00080 setRowStretchable( i, TRUE );
00081
00082 setSelectionMode( NoSelection );
00083
00084 connect( this, SIGNAL( clicked(int,int,int,const QPoint&) ),
00085 this, SLOT( dayClicked(int,int) ) );
00086 connect( this, SIGNAL( currentChanged(int,int) ),
00087 this, SLOT( dragDay(int,int) ) );
00088 setVScrollBarMode( AlwaysOff );
00089 setHScrollBarMode( AlwaysOff );
00090 }
00091
00092 ODateBookMonthTable::~ODateBookMonthTable()
00093 {
00094 monthsEvents.clear();
00095 delete d;
00096 }
00097
00098 void ODateBookMonthTable::setDate(int y, int m, int d)
00099 {
00100 if (month == m && year == y) {
00101 if ( selYear == -1 )
00102 year = selYear;
00103 if ( selMonth == -1 )
00104 month = selMonth;
00105 int r1, c1, r2, c2;
00106 findDay(selDay, r1, c1);
00107 selDay = day = d;
00108 findDay(selDay, r2, c2);
00109 setCurrentCell( r2, c2 );
00110
00111
00112 } else {
00113 selYear = year = y;
00114 selMonth = month = m;
00115 selDay = day = d;
00116 setupTable();
00117 }
00118 }
00119
00120 void ODateBookMonthTable::redraw()
00121 {
00122 setupLabels();
00123 setupTable();
00124 }
00125
00126 void ODateBookMonthTable::setWeekStart( bool onMonday )
00127 {
00128 d->onMonday = onMonday;
00129 setupLabels();
00130 setupTable();
00131 }
00132
00133 void ODateBookMonthTable::setupTable()
00134 {
00135 QValueList<Calendar::Day> days = Calendar::daysOfMonth( year, month, d->onMonday );
00136 QValueList<Calendar::Day>::Iterator it = days.begin();
00137 int row = 0, col = 0;
00138 int crow = 0;
00139 int ccol = 0;
00140 for ( ; it != days.end(); ++it ) {
00141 DayItemMonth *i = (DayItemMonth *)item( row, col );
00142 if ( !i ) {
00143 i = new DayItemMonth( this, QTableItem::Never, "" );
00144 setItem( row, col, i );
00145 }
00146 Calendar::Day calDay = *it;
00147 i->clearEffEvents();
00148 i->setDay( calDay.date );
00149 i->setType( calDay.type );
00150 if ( i->day() == day && calDay.type == Calendar::Day::ThisMonth ) {
00151 crow = row;
00152 ccol = col;
00153 }
00154
00155 updateCell( row, col );
00156
00157 if ( col == 6 ) {
00158 ++row;
00159 col = 0;
00160 } else {
00161 ++col;
00162 }
00163 }
00164 setCurrentCell( crow, ccol );
00165 getEvents();
00166 }
00167
00168 void ODateBookMonthTable::findDay( int day, int &row, int &col )
00169 {
00170 QDate dtBegin( year, month, 1 );
00171 int skips = dtBegin.dayOfWeek();
00172 int effective_day = day + skips - 1;
00173
00174 if ( d->onMonday )
00175 effective_day--;
00176 row = effective_day / 7;
00177 col = effective_day % 7;
00178 }
00179
00180 void ODateBookMonthTable::dayClicked( int row, int col )
00181 {
00182 changeDaySelection( row, col );
00183 emit dateClicked( selYear, selMonth, selDay );
00184 }
00185
00186 void ODateBookMonthTable::dragDay( int row, int col )
00187 {
00188 changeDaySelection( row, col );
00189 }
00190
00191 void ODateBookMonthTable::changeDaySelection( int row, int col )
00192 {
00193 DayItemMonth *i = (DayItemMonth*)item( row, col );
00194 if ( !i )
00195 return;
00196 switch ( i->type() ) {
00197 case Calendar::Day::ThisMonth:
00198 selMonth = month;
00199 break;
00200 case Calendar::Day::PrevMonth:
00201 selMonth = month-1;
00202 break;
00203 default:
00204 selMonth = month+1;
00205 }
00206
00207 selYear = year;
00208 if ( selMonth <= 0 ) {
00209 selMonth = 12;
00210 selYear--;
00211 } else if ( selMonth > 12 ) {
00212 selMonth = 1;
00213 selYear++;
00214 }
00215 selDay = i->day();
00216 }
00217
00218
00219 void ODateBookMonthTable::viewportMouseReleaseEvent( QMouseEvent * )
00220 {
00221 dayClicked( currentRow(), currentColumn() );
00222 }
00223
00224 void ODateBookMonthTable::getEvents()
00225 {
00226 if ( !db )
00227 return;
00228
00229 QDate dtStart( year, month, 1 );
00230 d->mMonthEvents = db->getEffectiveEvents( dtStart,
00231 QDate( year, month,
00232 dtStart.daysInMonth() ) );
00233 QValueListIterator<EffectiveEvent> it = d->mMonthEvents.begin();
00234
00235
00236
00237 while ( it != d->mMonthEvents.end() ) {
00238 QValueList<EffectiveEvent> dayEvent;
00239 EffectiveEvent e = *it;
00240 ++it;
00241 dayEvent.append( e );
00242 while ( it != d->mMonthEvents.end()
00243 && e.date() == (*it).date() ) {
00244 dayEvent.append( *it );
00245 ++it;
00246 }
00247 int row, col;
00248 findDay( e.date().day(), row, col );
00249 DayItemMonth* w = static_cast<DayItemMonth*>( item( row, col ) );
00250 w->setEvents( dayEvent );
00251 updateCell( row, col );
00252 dayEvent.clear();
00253 }
00254 }
00255
00256
00257 void ODateBookMonthTable::setupLabels()
00258 {
00259 for ( int i = 0; i < 7; ++i ) {
00260
00261
00262 if ( d->onMonday )
00263 horizontalHeader()->setLabel( i, Calendar::nameOfDay( i + 1 ) );
00264 else {
00265 if ( i == 0 )
00266 horizontalHeader()->setLabel( i, Calendar::nameOfDay( 7 ) );
00267 else
00268 horizontalHeader()->setLabel( i, Calendar::nameOfDay( i ) );
00269 }
00270 }
00271 }
00272
00273
00274
00275
00276 ODateBookMonth::ODateBookMonth( QWidget *parent, const char *name, bool ac,
00277 DateBookDBHoliday *data )
00278 : QVBox( parent, name ),
00279 autoClose( ac )
00280 {
00281 setFocusPolicy(StrongFocus);
00282 year = QDate::currentDate().year();
00283 month = QDate::currentDate().month();
00284 day = QDate::currentDate().day();
00285 header = new DateBookMonthHeader( this, "DateBookMonthHeader" );
00286 table = new ODateBookMonthTable( this, "DateBookMonthTable", data );
00287 header->setDate( year, month );
00288 table->setDate( year, month, QDate::currentDate().day() );
00289 header->setFocusPolicy(NoFocus);
00290 table->setFocusPolicy(NoFocus);
00291 connect( header, SIGNAL( dateChanged(int,int) ),
00292 this, SLOT( setDate(int,int) ) );
00293 connect( table, SIGNAL( dateClicked(int,int,int) ),
00294 this, SLOT( finalDate(int,int,int) ) );
00295 connect( qApp, SIGNAL(weekChanged(bool)), this,
00296 SLOT(slotWeekChange(bool)) );
00297 table->setFocus();
00298 }
00299
00300 ODateBookMonth::~ODateBookMonth()
00301 {
00302
00303 }
00304
00305 void ODateBookMonth::setDate( int y, int m )
00306 {
00307
00308
00309 if ( (y != year) || (m != month) ) {
00310 year = y;
00311 month = m;
00312 QDate nd( y, m, 1 );
00313 if ( nd.daysInMonth() < day )
00314 day = nd.daysInMonth();
00315 table->setDate( year, month, day );
00316 }
00317 }
00318
00319 void ODateBookMonth::setDate( int y, int m, int d )
00320 {
00321 header->setDate( y, m);
00322 table->setDate( y, m, d);
00323 year = y;
00324 month = m;
00325 day = d;
00326 }
00327
00328
00329 void ODateBookMonth::finalDate(int y, int m, int d)
00330 {
00331 setDate( y, m, d );
00332
00333 emit dateClicked(y, m, d);
00334
00335
00336 if ( autoClose && parentWidget() )
00337 parentWidget()->close();
00338 }
00339
00340 void ODateBookMonth::setDate( QDate d)
00341 {
00342 setDate(d.year(), d.month(), d.day());
00343 }
00344
00345 void ODateBookMonth::redraw()
00346 {
00347 table->setDate( year, month, day );
00348 table->redraw();
00349 }
00350
00351 QDate ODateBookMonth::selectedDate() const
00352 {
00353 if ( !table )
00354 return QDate::currentDate();
00355 int y, m, d;
00356 table->getDate( y, m, d );
00357 return QDate( y, m, d );
00358 }
00359
00360 void ODateBookMonth::slotWeekChange( bool startOnMonday )
00361 {
00362 table->setWeekStart( startOnMonday );
00363 }
00364
00365 void ODateBookMonth::keyPressEvent( QKeyEvent *e )
00366 {
00367 switch(e->key()) {
00368 case Key_Up:
00369 setDate(QDate(year, month, day).addDays(-7));
00370 break;
00371 case Key_Down:
00372 setDate(QDate(year, month, day).addDays(7));
00373 break;
00374 case Key_Left:
00375 setDate(QDate(year, month, day).addDays(-1));
00376 break;
00377 case Key_Right:
00378 setDate(QDate(year, month, day).addDays(1));
00379 break;
00380 case Key_Space:
00381 qWarning("space");
00382 emit dateClicked(year, month, day);
00383 if ( autoClose && parentWidget() )
00384 parentWidget()->close();
00385 break;
00386 default:
00387 qWarning("ignore");
00388 e->ignore();
00389 break;
00390 }
00391 }