kplato

kptdatetable.cc

00001 /* This file is part of the KDE project
00002     Copyright (C) 1997 Tim D. Gilman (tdgilman@best.org)
00003               (C) 1998-2001 Mirko Boehm (mirko@kde.org)
00004               (C) 2004 Dag Andersen <danders@get2net.dk>
00005 
00006    This library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Library General Public
00008    License as published by the Free Software Foundation; either
00009    version 2 of the License, or (at your option) any later version.
00010 
00011    This library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Library General Public License for more details.
00015 
00016    You should have received a copy of the GNU Library General Public License
00017    along with this library; see the file COPYING.LIB.  If not, write to
00018    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019  * Boston, MA 02110-1301, USA.
00020 */
00021 
00022 #include "kptdatetable.h"
00023 #include "kptmap.h"
00024 
00025 #include <kapplication.h>
00026 #include <knotifyclient.h>
00027 #include <kcalendarsystem.h>
00028 
00029 #include <qdatetime.h>
00030 #include <qstring.h>
00031 #include <qpen.h>
00032 #include <qpainter.h>
00033 #include <qdialog.h>
00034 #include <assert.h>
00035 #include <qlayout.h>
00036 #include <qvaluelist.h>
00037 #include <kglobalsettings.h>
00038 
00039 #include <kdebug.h>
00040 
00041 namespace KPlato
00042 {
00043 
00044 DateValidator::DateValidator(QWidget* parent, const char* name)
00045     : QValidator(parent, name)
00046 {
00047 }
00048 
00049 QValidator::State
00050 DateValidator::validate(QString& text, int&) const
00051 {
00052   QDate temp;
00053   // ----- everything is tested in date():
00054   return date(text, temp);
00055 }
00056 
00057 QValidator::State
00058 DateValidator::date(const QString& text, QDate& d) const
00059 {
00060   QDate tmp = KGlobal::locale()->readDate(text);
00061   if (!tmp.isNull())
00062     {
00063       d = tmp;
00064       return Acceptable;
00065     } else
00066       return Valid;
00067 }
00068 
00069 void
00070 DateValidator::fixup( QString& ) const
00071 {
00072 
00073 }
00074 
00075 
00076 DateTable::DateTable(QWidget *parent, QDate date_, const char* name, WFlags f)
00077     : QGridView(parent, name, f),
00078       m_enabled(true)
00079 {
00080     //kdDebug()<<k_funcinfo<<endl;
00081     m_dateStartCol = 1;
00082     m_selectedDates.clear();
00083     m_selectedWeekdays.clear();
00084 
00085     QPair<int, int> p(0,0);
00086     m_weeks.fill(p, 7);
00087 
00088     setFontSize(10);
00089     if(!date_.isValid()) {
00090         kdError() <<k_funcinfo<<"Given date is invalid, using current date." << endl;
00091         date_=QDate::currentDate();
00092     }
00093     setFocusPolicy( QWidget::StrongFocus );
00094     setNumCols(7+m_dateStartCol); // 7 days a week + maybe 1 for weeknumbers
00095     setNumRows(7); // 6 weeks max + headline
00096 
00097     setHScrollBarMode(AlwaysOff);
00098     setVScrollBarMode(AlwaysOff);
00099     viewport()->setEraseColor(KGlobalSettings::baseColor());
00100     setDate(date_); // this initializes firstday, numdays, numDaysPrevMonth
00101 
00102     colorBackgroundHoliday = QColor(0, 245, 255, QColor::Hsv);
00103     //colorBackgroundHoliday = colorBackgroundHoliday.light();
00104     colorBackgroundWorkday = QColor(208, 230, 240, QColor::Hsv);;
00105     //colorBackgroundWorkday = colorBackgroundWorkday.light();
00106     colorTextHoliday = black;
00107     colorTextWorkday = black;
00108     colorLine = black;
00109     backgroundSelectColor = KGlobalSettings::highlightColor();
00110     penSelectColor=KGlobalSettings::baseColor();
00111 
00112 }
00113 
00114 void DateTable::paintWeekday(QPainter *painter, int col) {
00115     QRect rect;
00116     int w=cellWidth();
00117     int h=cellHeight();
00118 
00119     QFont font = KGlobalSettings::generalFont();
00120     font.setBold(true);
00121     if (!m_enabled)
00122         font.setItalic(true);
00123     painter->setFont(font);
00124 
00125     int day = weekday(col);
00126 
00127     //kdDebug()<<k_funcinfo<<" col="<<col<<" day="<<day<<" name="<<daystr<<endl;
00128 
00129     painter->setBrush(KGlobalSettings::baseColor());
00130     painter->setPen(KGlobalSettings::baseColor());
00131     painter->drawRect(0, 0, w, h);
00132     painter->setPen(KGlobalSettings::textColor());
00133 
00134     if (m_markedWeekdays.state(day) == Map::Working) {
00135         painter->setPen(colorBackgroundWorkday);
00136         painter->setBrush(colorBackgroundWorkday);
00137         painter->drawRect(0, 0, w, h);
00138         painter->setPen(colorTextWorkday);
00139     } else if (m_markedWeekdays.state(day) == Map::NonWorking) {
00140         painter->setPen(colorBackgroundHoliday);
00141         painter->setBrush(colorBackgroundHoliday);
00142         painter->drawRect(0, 0, w, h);
00143         painter->setPen(colorTextHoliday);
00144     }
00145     if (m_selectedWeekdays.contains(day)) {
00146         painter->setPen(backgroundSelectColor);
00147         painter->setBrush(backgroundSelectColor);
00148         painter->drawRect(2, 2, w-4, h-4);
00149         painter->setPen(penSelectColor);
00150     }
00151     painter->drawText(0, 0, w, h-1, AlignCenter, KGlobal::locale()->calendar()->weekDayName(day, true), -1, &rect);
00152     painter->setPen(colorLine);
00153     painter->moveTo(0, h-1);
00154     painter->lineTo(w-1, h-1);
00155 
00156     if(rect.width()>maxCell.width()) maxCell.setWidth(rect.width());
00157     if(rect.height()>maxCell.height()) maxCell.setHeight(rect.height());
00158 
00159     //kdDebug()<<k_funcinfo<<"headline: row,col=("<<row<<","<<col<<")"<<" day="<<daystr<<endl;
00160 }
00161 
00162 void DateTable::paintWeekNumber(QPainter *painter, int row) {
00163     QRect rect;
00164     int w=cellWidth();
00165     int h=cellHeight();
00166 
00167     QFont font=KGlobalSettings::generalFont();
00168     font.setBold(true);
00169     if (!m_enabled)
00170         font.setItalic(true);
00171     painter->setFont(font);
00172 
00173     painter->setBrush(KGlobalSettings::baseColor());
00174     painter->setPen(KGlobalSettings::baseColor());
00175     painter->drawRect(0, 0, w, h);
00176     painter->setPen(KGlobalSettings::textColor());
00177     
00178     painter->drawText(0, 0, w, h-1, AlignCenter, QString("%1").arg(m_weeks[row].first), -1, &rect);
00179     painter->setPen(colorLine);
00180     painter->moveTo(w-1, 0);
00181     painter->lineTo(w-1, h-1);
00182 
00183     if(rect.width()>maxCell.width()) maxCell.setWidth(rect.width());
00184     if(rect.height()>maxCell.height()) maxCell.setHeight(rect.height());
00185 }
00186 
00187 void DateTable::paintDay(QPainter *painter, int row, int col) {
00188     //kdDebug()<<k_funcinfo<<"row,col=("<<row<<","<<col<<")"<<" num col="<<numCols()<<endl;
00189     QRect rect;
00190     int w=cellWidth();
00191     int h=cellHeight();
00192 
00193     QFont font=KGlobalSettings::generalFont();
00194     font.setPointSize(fontsize);
00195     if (!m_enabled)
00196         font.setItalic(true);
00197     painter->setFont(font);
00198 
00199     QDate d = getDate(position(row, col));
00200 
00201     painter->setBrush(KGlobalSettings::baseColor());
00202     painter->setPen(KGlobalSettings::baseColor());
00203     painter->drawRect(0, 0, w, h);
00204 
00205     // First paint the dates background
00206     if (m_markedDates.state(d) == Map::NonWorking) {
00207         //kdDebug()<<k_funcinfo<<"Marked date: "<<d<<"  row,col=("<<row<<","<<col<<")=NonWorking"<<endl;
00208         painter->setPen(colorBackgroundHoliday);
00209         painter->setBrush(colorBackgroundHoliday);
00210         painter->drawRect(0, 0, w, h);
00211     } else if (m_markedDates.state(d) == Map::Working) {
00212         //kdDebug()<<k_funcinfo<<"Marked date: "<<d<<"  row,col=("<<row<<","<<col<<")=Working"<<endl;
00213         painter->setPen(colorBackgroundWorkday);
00214         painter->setBrush(colorBackgroundWorkday);
00215         painter->drawRect(0, 0, w, h);
00216     }
00217     if(m_selectedDates.contains(d)) {
00218         //kdDebug()<<k_funcinfo<<"Selected: "<<d<<" row,col=("<<row<<","<<col<<")"<<endl;
00219         painter->setPen(backgroundSelectColor);
00220         painter->setBrush(backgroundSelectColor);
00221         painter->drawRect(2, 2, w-4, h-4);
00222     }
00223     // If weeks or weekdays are selected/marked we draw lines around the date
00224     QPen pen = painter->pen();
00225     if (m_markedWeekdays.state(weekday(col)) == Map::Working) {
00226         //kdDebug()<<k_funcinfo<<"Marked weekday: row,dayCol=("<<row<<","<<dayCol<<")=Working"<<endl;
00227         pen.setColor(colorBackgroundWorkday);
00228         painter->setPen(pen);
00229         painter->moveTo(0, 0);
00230         painter->lineTo(0, h-1);
00231         painter->moveTo(w-1, 0);
00232         painter->lineTo(w-1, h-1);
00233     }
00234     // then paint square if current date
00235     if (d  == QDate::currentDate()) {
00236         painter->setPen(colorLine);
00237         painter->drawRect(1, 1, w-2, h-2);
00238     }
00239 
00240     // and now the day number
00241     d.month() == date.month() ? painter->setPen(KGlobalSettings::textColor()) : painter->setPen(gray);
00242     painter->drawText(0, 0, w, h, AlignCenter, QString().setNum(d.day()), -1, &rect);
00243 
00244     if(rect.width()>maxCell.width()) maxCell.setWidth(rect.width());
00245     if(rect.height()>maxCell.height()) maxCell.setHeight(rect.height());
00246 }
00247 
00248 void DateTable::paintCell(QPainter *painter, int row, int col) {
00249     //kdDebug()<<k_funcinfo<<"row,col=("<<row<<","<<col<<")"<<"enabled="<<m_enabled<<endl;
00250     if (row == 0 && col == 0) {
00251         painter->save();
00252         int w=cellWidth();
00253         int h=cellHeight();
00254         painter->setPen(colorLine);
00255         painter->setBrush(KGlobalSettings::baseColor());
00256         painter->moveTo(w-1, 0);
00257         painter->lineTo(w-1, h-1);
00258         painter->lineTo(0, h-1);
00259         painter->restore();
00260         return;
00261     }
00262     painter->save();
00263     if(row==0) { // we are drawing the weekdays
00264         paintWeekday(painter, col);
00265     } else if (col == 0) { // draw week numbers
00266         paintWeekNumber(painter, row);
00267     } else { // draw the day
00268         paintDay(painter, row, col);
00269     }
00270     painter->restore();
00271 }
00272 
00273 //FIXME
00274 void DateTable::keyPressEvent( QKeyEvent *e ) {
00275     if (!m_enabled)
00276         return;
00277     if ( e->key() == Qt::Key_Prior ) {
00278         setDate(date.addMonths(-1));
00279         return;
00280     }
00281     if ( e->key() == Qt::Key_Next ) {
00282         setDate(date.addMonths(1));
00283         return;
00284     }
00285 
00286     if ( e->key() == Qt::Key_Up ) {
00287         if ( date.day() > 7 ) {
00288             setDate(date.addDays(-7));
00289             return;
00290         }
00291     }
00292     if ( e->key() == Qt::Key_Down ) {
00293         if ( date.day() <= date.daysInMonth()-7 ) {
00294             setDate(date.addDays(7));
00295             return;
00296         }
00297     }
00298     if ( e->key() == Qt::Key_Left ) {
00299         if ( date.day() > 1 ) {
00300             setDate(date.addDays(-1));
00301             return;
00302         }
00303     }
00304     if ( e->key() == Qt::Key_Right ) {
00305         if ( date.day() < date.daysInMonth() ) {
00306             setDate(date.addDays(1));
00307             return;
00308         }
00309     }
00310 
00311     if ( e->key() == Qt::Key_Minus ) {
00312         setDate(date.addDays(-1));
00313         return;
00314     }
00315     if ( e->key() == Qt::Key_Plus ) {
00316         setDate(date.addDays(1));
00317         return;
00318     }
00319     if ( e->key() == Qt::Key_N ) {
00320         setDate(QDate::currentDate());
00321         return;
00322     }
00323     if ( e->key() == Qt::Key_Control ) {
00324         return;
00325     }
00326     if ( e->key() == Qt::Key_Shift ) {
00327         return;
00328     }
00329 
00330     KNotifyClient::beep();
00331 }
00332 
00333 void DateTable::viewportResizeEvent(QResizeEvent * e) {
00334   QGridView::viewportResizeEvent(e);
00335 
00336   setCellWidth(viewport()->width()/numCols());
00337   setCellHeight(viewport()->height()/numRows());
00338 }
00339 
00340 void DateTable::setFontSize(int size) {
00341   int count;
00342   QFontMetrics metrics(fontMetrics());
00343   QRect rect;
00344   // ----- store rectangles:
00345   fontsize=size;
00346   // ----- find largest day name:
00347   maxCell.setWidth(0);
00348   maxCell.setHeight(0);
00349   for(count=0; count<7; ++count)
00350     {
00351       rect=metrics.boundingRect(KGlobal::locale()->calendar()->weekDayName(count+1, true));
00352       maxCell.setWidth(QMAX(maxCell.width(), rect.width()));
00353       maxCell.setHeight(QMAX(maxCell.height(), rect.height()));
00354     }
00355   // ----- compare with a real wide number and add some space:
00356   rect=metrics.boundingRect(QString::fromLatin1("88"));
00357   maxCell.setWidth(QMAX(maxCell.width()+2, rect.width()));
00358   maxCell.setHeight(QMAX(maxCell.height()+4, rect.height()));
00359 }
00360 
00361 //FIXME
00362 void DateTable::wheelEvent ( QWheelEvent * e ) {
00363     setDate(date.addMonths( -(int)(e->delta()/120)) );
00364     e->accept();
00365 }
00366 
00367 
00368 void DateTable::contentsMousePressEvent(QMouseEvent *e) {
00369     if (!m_enabled)
00370         return;
00371     //kdDebug()<<k_funcinfo<<endl;
00372     if(e->type()!=QEvent::MouseButtonPress) {
00373         return;
00374     }
00375     QPoint mouseCoord = e->pos();
00376     int row=rowAt(mouseCoord.y());
00377     int col=columnAt(mouseCoord.x());
00378     if (row == 0 && col == 0) { // user clicked on (unused) upper left square
00379         updateSelectedCells();
00380         m_selectedWeekdays.clear();
00381         m_selectedDates.clear();
00382         repaintContents(false);
00383         emit selectionCleared();
00384         return;
00385     }
00386     if (col == 0) { // user clicked on week numbers
00387         updateSelectedCells();
00388         m_selectedWeekdays.clear();
00389         m_selectedDates.clear();
00390         updateSelectedCells();
00391         repaintContents(false);
00392         return;
00393     }
00394     if (row==0 && col>0) { // the user clicked on weekdays
00395         updateSelectedCells();
00396         m_selectedDates.clear();
00397         int day = weekday(col);
00398         if (e->state() & ShiftButton) {
00399             // select all days between this and the furthest away selected day,
00400             // check first down - then up, clear all others
00401             bool select = false;
00402             for(int i=0; i < col; ++i) {
00403                 if (m_selectedWeekdays.contains(i)) {
00404                     select = true;
00405                 } else if (select) {
00406                     m_selectedWeekdays.toggle(i); // select
00407                 }
00408             }
00409             bool selected = select;
00410             select = false;
00411             for(int i=7; i > col; --i) {
00412                 if (m_selectedWeekdays.contains(i)) {
00413                     if (selected) m_selectedWeekdays.toggle(i); // deselect
00414                     else select = true;
00415                 } else if (select) {
00416                     m_selectedWeekdays.toggle(i); // select
00417                 }
00418             }
00419             if (!m_selectedWeekdays.contains(col)) {
00420                 m_selectedWeekdays.toggle(col); // always select
00421             }
00422         } else if (e->state() & ControlButton) {
00423             // toggle select this date
00424             m_selectedWeekdays.toggle(day);
00425         } else {
00426             // toggle select this, clear all others
00427             m_selectedWeekdays.toggleClear(day);
00428         }
00429         updateSelectedCells();
00430         repaintContents(false);
00431         if (m_enabled) {
00432             //kdDebug()<<k_funcinfo<<"emit weekdaySelected("<<day<<")"<<endl;
00433             emit weekdaySelected(day); // day= 1..7
00434         }
00435         return;
00436     }
00437 
00438     if (contentsMousePressEvent_internal(e)) {
00439         // Date hit,
00440         m_selectedWeekdays.clear();
00441         if (e->state() & ShiftButton) {
00442             // find first&last date
00443             QDate first;
00444             QDate last;
00445             DateMap::ConstIterator it;
00446             for (it = m_selectedDates.constBegin(); it != m_selectedDates.constEnd(); ++it) {
00447                 //kdDebug()<<k_funcinfo<<it.key()<<endl;
00448                 QDate d = QDate::fromString(it.key(), Qt::ISODate);
00449                 if (!d.isValid())
00450                     continue;
00451                 if (!first.isValid() || first > d)
00452                     first = d;
00453                 if (!last.isValid() || last < d)
00454                     last = d;
00455             }
00456             // select between anchor and pressed date inclusive
00457             m_selectedDates.clear();
00458             if (first.isValid() && last.isValid()) {
00459                 QDate anchor = first < date ? first : last;
00460                 int i = anchor > date ? -1 : 1;
00461                 while (anchor != date) {
00462                     //kdDebug()<<k_funcinfo<<anchor.toString(Qt::ISODate)<<endl;
00463                     m_selectedDates.toggle(anchor);
00464                     anchor = anchor.addDays(i);
00465                 }
00466             }
00467             m_selectedDates.toggle(date);
00468         } else if (e->state() & ControlButton) {
00469             // toggle select this date
00470             m_selectedDates.toggle(date);
00471             //kdDebug()<<k_funcinfo<<"toggle date: "<<date.toString()<<" state="<<m_selectedDates.state(date)<<endl;
00472         } else {
00473             // Select this, clear all others
00474             m_selectedDates.clear();
00475             m_selectedDates.toggleClear(date);
00476             //kdDebug()<<k_funcinfo<<"toggleClear date: "<<date.toString()<<" state="<<m_selectedDates.state(date)<<endl;
00477         }
00478     }
00479     repaintContents(false);
00480 }
00481 
00482 bool DateTable::contentsMousePressEvent_internal(QMouseEvent *e) {
00483     QPoint mouseCoord = e->pos();
00484     int row=rowAt(mouseCoord.y());
00485     int col=columnAt(mouseCoord.x());
00486     if(row<1 || col<0) { // the user clicked on the frame of the table
00487         return false;
00488     }
00489     //kdDebug()<<k_funcinfo<<"pos["<<row<<","<<col<<"]="<<position(row,col)<<" firstday="<<firstday<<endl;
00490     selectDate(getDate(position(row, col)));
00491     return true;
00492 }
00493 
00494 bool DateTable::selectDate(const QDate& date_) {
00495     //kdDebug()<<k_funcinfo<<"date="<<date_.toString()<<endl;
00496     bool changed=false;
00497     QDate temp;
00498     // -----
00499     if(!date_.isValid()) {
00500         return false;
00501     }
00502     if(date!=date_) {
00503         date=date_;
00504         changed=true;
00505     }
00506 
00507     temp.setYMD(date.year(), date.month(), 1);
00508     firstday=temp.dayOfWeek();
00509     if(firstday==1) firstday=8; // Reserve row 1 for previous month
00510     numdays=date.daysInMonth();
00511     if(date.month()==1) { // set to december of previous year
00512         temp.setYMD(date.year()-1, 12, 1);
00513         setWeekNumbers(QDate(date.year()-1, 12, 31));
00514     } else { // set to previous month
00515         temp.setYMD(date.year(), date.month()-1, 1);
00516         QDate d(date.year(), date.month()-1,1);
00517         setWeekNumbers(d.addDays(d.daysInMonth()-1));
00518     }
00519     numDaysPrevMonth=temp.daysInMonth();
00520     if(changed) {
00521         repaintContents(false);
00522     }
00523     if (m_enabled)
00524         emit(dateChanged(date));
00525     return true;
00526 }
00527 
00528 bool DateTable::setDate(const QDate& date_, bool repaint) {
00529     //kdDebug()<<k_funcinfo<<"date="<<date_.toString()<<endl;
00530     bool changed=false;
00531     QDate temp;
00532     // -----
00533     if(!date_.isValid()) {
00534         //kdDebug() << "DateTable::setDate: refusing to set invalid date." << endl;
00535         return false;
00536         }
00537     if(date!=date_) {
00538         date=date_;
00539         changed=true;
00540     }
00541     //m_selectedDates.clear();
00542 
00543     temp.setYMD(date.year(), date.month(), 1);
00544     firstday=temp.dayOfWeek();
00545     if(firstday==1) firstday=8;
00546     //kdDebug()<<k_funcinfo<<"date="<<temp.toString()<<"day="<<temp.dayOfWeek()<<" firstday="<<firstday<<endl;
00547     numdays=date.daysInMonth();
00548     if(date.month()==1) { // set to december of previous year
00549         temp.setYMD(date.year()-1, 12, 1);
00550         setWeekNumbers(QDate(date.year()-1, 12, 31));
00551     } else { // set to previous month
00552         temp.setYMD(date.year(), date.month()-1, 1);
00553         QDate d(date.year(), date.month()-1,1);
00554         setWeekNumbers(d.addDays(d.daysInMonth()-1));
00555     }
00556 /*    if (m_selectedWeekdays.isEmpty() &&
00557         !m_selectedDates.isEmpty() && !m_selectedDates.contains(date))
00558     {
00559         //kdDebug()<<k_funcinfo<<"date inserted"<<endl;
00560         m_selectedDates.insert(date);
00561     }*/
00562     numDaysPrevMonth=temp.daysInMonth();
00563     if(changed && repaint) {
00564         repaintContents(false);
00565     }
00566     if (m_enabled)
00567       emit(dateChanged(date));
00568     return true;
00569 }
00570 
00571 const QDate& DateTable::getDate() const {
00572   return date;
00573 }
00574 
00575 void DateTable::focusInEvent( QFocusEvent *e ) {
00576     QGridView::focusInEvent( e );
00577 }
00578 
00579 void DateTable::focusOutEvent( QFocusEvent *e ) {
00580     QGridView::focusOutEvent( e );
00581 }
00582 
00583 QSize DateTable::sizeHint() const {
00584     if(maxCell.height()>0 && maxCell.width()>0) {
00585       return QSize(maxCell.width()*numCols()+2*frameWidth(),
00586              (maxCell.height()+2)*numRows()+2*frameWidth());
00587     } else {
00588       //kdDebug() << "DateTable::sizeHint: obscure failure - " << endl;
00589       return QSize(-1, -1);
00590     }
00591 }
00592 
00593 void DateTable::setWeekNumbers(QDate date) {
00594     if (!date.isValid()) {
00595         kdError()<<k_funcinfo<<"Invalid date"<<endl;
00596     }
00597     QDate d(date);
00598     for (int i = 1; i < 7; ++i) {
00599         m_weeks[i].first = d.weekNumber(&(m_weeks[i].second));
00600         //kdDebug()<<k_funcinfo<<"date="<<d.toString()<<" week=("<<m_weeks[i].first<<","<<m_weeks[i].second<<")"<<endl;
00601         d = d.addDays(7);
00602     }
00603 }
00604 
00605 void DateTable::updateCells() {
00606     //kdDebug()<<k_funcinfo<<endl;
00607     for (int row=0; row < numRows(); ++row) {
00608         for (int col=0; col < numCols(); ++col) {
00609             updateCell(row, col);
00610         }
00611     }
00612 }
00613 
00614 void DateTable::updateSelectedCells() {
00615     //kdDebug()<<k_funcinfo<<endl;
00616     QDate dt(date.year(), date.month(), 1);
00617     dt = dt.addDays(-firstday);
00618     for (int pos=0; pos < 42; ++pos) {
00619         if (m_selectedDates.contains(dt.addDays(pos)) ||
00620             m_selectedWeekdays.contains(pos%7+1))
00621         {
00622             updateCell(pos/7+1, pos%7+1);
00623             //kdDebug()<<k_funcinfo<<" update cell ("<<pos/7+1<<","<<pos%7+1<<") date="<<dt.addDays(pos).toString()<<endl;
00624         }
00625     }
00626 }
00627 
00628 void DateTable::updateMarkedCells() {
00629     QDate dt(date.year(), date.month(), 1);
00630     dt = dt.addDays(-firstday);
00631     for (int pos=0; pos < 42; ++pos) {
00632         if (m_markedDates.contains(dt.addDays(pos)) ||
00633             m_markedWeekdays.contains(pos%7+1))
00634         {
00635             updateCell(pos/7+1, pos%7+1);
00636             //kdDebug()<<k_funcinfo<<" update cell ("<<pos/7+1<<","<<pos%7+1<<") date="<<dt.addDays(pos).toString()<<endl;
00637         }
00638     }
00639 }
00640 
00641 void DateTable::setMarkedWeekdays(const IntMap days) {
00642     updateMarkedCells();
00643     m_markedWeekdays.clear();
00644     m_markedWeekdays = days;
00645     updateMarkedCells();
00646     repaintContents(false);
00647 }
00648 
00649 bool DateTable::weekdayMarked(int day) {
00650     return m_markedWeekdays.contains(day);
00651 }
00652 
00653 bool DateTable::dateMarked(QDate date) {
00654     return m_markedDates[date.toString()];
00655 }
00656 
00657 void DateTable::clear() {
00658     clearSelection();
00659     m_markedDates.clear();
00660     m_markedWeekdays.clear();
00661     repaintContents(false);
00662 }
00663 
00664 void DateTable::clearSelection() {
00665     m_selectedDates.clear();
00666     m_selectedWeekdays.clear();
00667     repaintContents(false);
00668 }
00669 
00670  void DateTable::setEnabled(bool yes) {
00671     if (m_enabled == yes)
00672         return;
00673     m_enabled=yes;
00674     updateCells();
00675 }
00676 
00677 void DateTable::markSelected(int state) {
00678     if (!m_selectedDates.isEmpty()) {
00679         DateMap::iterator it;
00680         for(it = m_selectedDates.begin(); it != m_selectedDates.end(); ++it) {
00681             m_markedDates.insert(it.key(), state);
00682             //kdDebug()<<k_funcinfo<<"marked date: "<<it.key()<<"="<<state<<endl;
00683         }
00684     } else if (!m_selectedWeekdays.isEmpty()) {
00685         IntMap::iterator it;
00686         for(it = m_selectedWeekdays.begin(); it != m_selectedWeekdays.end(); ++it) {
00687             m_markedWeekdays.insert(it.key(), state);
00688             //kdDebug()<<k_funcinfo<<"marked weekday: "<<it.key()<<"="<<state<<endl;
00689         }
00690     }
00691     updateSelectedCells();
00692     repaintContents(false);
00693 }
00694 
00695 DateInternalWeekSelector::DateInternalWeekSelector
00696 (int fontsize, QWidget* parent, const char* name)
00697   : QLineEdit(parent, name),
00698     val(new QIntValidator(this)),
00699     result(0)
00700 {
00701   QFont font;
00702   // -----
00703   font=KGlobalSettings::generalFont();
00704   font.setPointSize(fontsize);
00705   setFont(font);
00706   setFrameStyle(QFrame::NoFrame);
00707   val->setRange(1, 53);
00708   setValidator(val);
00709   connect(this, SIGNAL(returnPressed()), SLOT(weekEnteredSlot()));
00710 }
00711 
00712 void
00713 DateInternalWeekSelector::weekEnteredSlot()
00714 {
00715   bool ok;
00716   int week;
00717   // ----- check if this is a valid week:
00718   week=text().toInt(&ok);
00719   if(!ok)
00720     {
00721       KNotifyClient::beep();
00722       return;
00723     }
00724   result=week;
00725   emit(closeMe(1));
00726 }
00727 
00728 int
00729 DateInternalWeekSelector::getWeek() const
00730 {
00731   return result;
00732 }
00733 
00734 void
00735 DateInternalWeekSelector::setWeek(int week)
00736 {
00737   QString temp;
00738   // -----
00739   temp.setNum(week);
00740   setText(temp);
00741 }
00742 
00743 DateInternalMonthPicker::DateInternalMonthPicker
00744 (int fontsize, QWidget* parent, const char* name)
00745   : QGridView(parent, name),
00746     result(0) // invalid
00747 {
00748   QRect rect;
00749   QFont font;
00750   // -----
00751   activeCol = -1;
00752   activeRow = -1;
00753   font=KGlobalSettings::generalFont();
00754   font.setPointSize(fontsize);
00755   setFont(font);
00756   setHScrollBarMode(AlwaysOff);
00757   setVScrollBarMode(AlwaysOff);
00758   setFrameStyle(QFrame::NoFrame);
00759   setNumRows(4);
00760   setNumCols(3);
00761   // enable to find drawing failures:
00762   // setTableFlags(Tbl_clipCellPainting);
00763   viewport()->setEraseColor(KGlobalSettings::baseColor()); // for consistency with the datepicker
00764   // ----- find the preferred size
00765   //       (this is slow, possibly, but unfortunatly it is needed here):
00766   QFontMetrics metrics(font);
00767   for(int i=1; i <= 12; ++i)
00768     {
00769       rect=metrics.boundingRect(KGlobal::locale()->calendar()->monthName(i, false));
00770       if(max.width()<rect.width()) max.setWidth(rect.width());
00771       if(max.height()<rect.height()) max.setHeight(rect.height());
00772     }
00773 
00774 }
00775 
00776 QSize
00777 DateInternalMonthPicker::sizeHint() const
00778 {
00779   return QSize((max.width()+6)*numCols()+2*frameWidth(),
00780          (max.height()+6)*numRows()+2*frameWidth());
00781 }
00782 
00783 int
00784 DateInternalMonthPicker::getResult() const
00785 {
00786   return result;
00787 }
00788 
00789 void
00790 DateInternalMonthPicker::setupPainter(QPainter *p)
00791 {
00792   p->setPen(KGlobalSettings::textColor());
00793 }
00794 
00795 void
00796 DateInternalMonthPicker::viewportResizeEvent(QResizeEvent*)
00797 {
00798   setCellWidth(width()/3);
00799   setCellHeight(height()/4);
00800 }
00801 
00802 void
00803 DateInternalMonthPicker::paintCell(QPainter* painter, int row, int col)
00804 {
00805   int index;
00806   QString text;
00807   // ----- find the number of the cell:
00808   index=3*row+col+1;
00809   text=KGlobal::locale()->calendar()->monthName(index, false);
00810   painter->drawText(0, 0, cellWidth(), cellHeight(), AlignCenter, text);
00811   if ( activeCol == col && activeRow == row )
00812       painter->drawRect( 0, 0, cellWidth(), cellHeight() );
00813 }
00814 
00815 void
00816 DateInternalMonthPicker::contentsMousePressEvent(QMouseEvent *e)
00817 {
00818   if(!isEnabled() || e->button() != LeftButton)
00819     {
00820       KNotifyClient::beep();
00821       return;
00822     }
00823   // -----
00824   int row, col;
00825   QPoint mouseCoord;
00826   // -----
00827   mouseCoord = e->pos();
00828   row=rowAt(mouseCoord.y());
00829   col=columnAt(mouseCoord.x());
00830 
00831   if(row<0 || col<0)
00832     { // the user clicked on the frame of the table
00833       activeCol = -1;
00834       activeRow = -1;
00835     } else {
00836       activeCol = col;
00837       activeRow = row;
00838       updateCell( row, col /*, false */ );
00839   }
00840 }
00841 
00842 void
00843 DateInternalMonthPicker::contentsMouseMoveEvent(QMouseEvent *e)
00844 {
00845   if (e->state() & LeftButton)
00846     {
00847       int row, col;
00848       QPoint mouseCoord;
00849       // -----
00850       mouseCoord = e->pos();
00851       row=rowAt(mouseCoord.y());
00852       col=columnAt(mouseCoord.x());
00853       int tmpRow = -1, tmpCol = -1;
00854       if(row<0 || col<0)
00855         { // the user clicked on the frame of the table
00856           if ( activeCol > -1 )
00857             {
00858               tmpRow = activeRow;
00859               tmpCol = activeCol;
00860             }
00861           activeCol = -1;
00862           activeRow = -1;
00863         } else {
00864           bool differentCell = (activeRow != row || activeCol != col);
00865           if ( activeCol > -1 && differentCell)
00866             {
00867               tmpRow = activeRow;
00868               tmpCol = activeCol;
00869             }
00870           if ( differentCell)
00871             {
00872               activeRow = row;
00873               activeCol = col;
00874               updateCell( row, col /*, false */ ); // mark the new active cell
00875             }
00876         }
00877       if ( tmpRow > -1 ) // repaint the former active cell
00878           updateCell( tmpRow, tmpCol /*, true */ );
00879     }
00880 }
00881 
00882 void
00883 DateInternalMonthPicker::contentsMouseReleaseEvent(QMouseEvent *e)
00884 {
00885   if(!isEnabled())
00886     {
00887       return;
00888     }
00889   // -----
00890   int row, col, pos;
00891   QPoint mouseCoord;
00892   // -----
00893   mouseCoord = e->pos();
00894   row=rowAt(mouseCoord.y());
00895   col=columnAt(mouseCoord.x());
00896   if(row<0 || col<0)
00897     { // the user clicked on the frame of the table
00898       emit(closeMe(0));
00899     }
00900   pos=3*row+col+1;
00901   result=pos;
00902   emit(closeMe(1));
00903 }
00904 
00905 
00906 
00907 DateInternalYearSelector::DateInternalYearSelector
00908 (int fontsize, QWidget* parent, const char* name)
00909   : QLineEdit(parent, name),
00910     val(new QIntValidator(this)),
00911     result(0)
00912 {
00913   QFont font;
00914   // -----
00915   font=KGlobalSettings::generalFont();
00916   font.setPointSize(fontsize);
00917   setFont(font);
00918   setFrameStyle(QFrame::NoFrame);
00919   // we have to respect the limits of QDate here, I fear:
00920   val->setRange(0, 8000);
00921   setValidator(val);
00922   connect(this, SIGNAL(returnPressed()), SLOT(yearEnteredSlot()));
00923 }
00924 
00925 void
00926 DateInternalYearSelector::yearEnteredSlot()
00927 {
00928   bool ok;
00929   int year;
00930   QDate date;
00931   // ----- check if this is a valid year:
00932   year=text().toInt(&ok);
00933   if(!ok)
00934     {
00935       KNotifyClient::beep();
00936       return;
00937     }
00938   date.setYMD(year, 1, 1);
00939   if(!date.isValid())
00940     {
00941       KNotifyClient::beep();
00942       return;
00943     }
00944   result=year;
00945   emit(closeMe(1));
00946 }
00947 
00948 int
00949 DateInternalYearSelector::getYear() const
00950 {
00951   return result;
00952 }
00953 
00954 void
00955 DateInternalYearSelector::setYear(int year)
00956 {
00957   QString temp;
00958   // -----
00959   temp.setNum(year);
00960   setText(temp);
00961 }
00962 
00963 PopupFrame::PopupFrame(QWidget* parent, const char*  name)
00964   : QFrame(parent, name, WType_Popup),
00965     result(0), // rejected
00966     main(0)
00967 {
00968   setFrameStyle(QFrame::Box|QFrame::Raised);
00969   setMidLineWidth(2);
00970 }
00971 
00972 void
00973 PopupFrame::keyPressEvent(QKeyEvent* e)
00974 {
00975   if(e->key()==Key_Escape)
00976     {
00977       result=0; // rejected
00978       qApp->exit_loop();
00979     }
00980 }
00981 
00982 void
00983 PopupFrame::close(int r)
00984 {
00985   result=r;
00986   qApp->exit_loop();
00987 }
00988 
00989 void
00990 PopupFrame::setMainWidget(QWidget* m)
00991 {
00992   main=m;
00993   if(main!=0)
00994     {
00995       resize(main->width()+2*frameWidth(), main->height()+2*frameWidth());
00996     }
00997 }
00998 
00999 void
01000 PopupFrame::resizeEvent(QResizeEvent*)
01001 {
01002   if(main!=0)
01003     {
01004       main->setGeometry(frameWidth(), frameWidth(),
01005           width()-2*frameWidth(), height()-2*frameWidth());
01006     }
01007 }
01008 
01009 void
01010 PopupFrame::popup(const QPoint &pos)
01011 {
01012   // Make sure the whole popup is visible.
01013   QRect d = QApplication::desktop()->screenGeometry(QApplication::desktop()->screenNumber(pos));
01014   int x = pos.x();
01015   int y = pos.y();
01016   int w = width();
01017   int h = height();
01018   if (x+w > d.x()+d.width())
01019     x = d.width() - w;
01020   if (y+h > d.y()+d.height())
01021     y = d.height() - h;
01022   if (x < d.x())
01023     x = 0;
01024   if (y < d.y())
01025     y = 0;
01026 
01027   // Pop the thingy up.
01028   move(x, y);
01029   show();
01030 }
01031 
01032 int
01033 PopupFrame::exec(QPoint pos)
01034 {
01035   popup(pos);
01036   repaint();
01037   qApp->enter_loop();
01038   hide();
01039   return result;
01040 }
01041 
01042 int
01043 PopupFrame::exec(int x, int y)
01044 {
01045   return exec(QPoint(x, y));
01046 }
01047 
01048 void PopupFrame::virtual_hook( int, void* )
01049 { /*BASE::virtual_hook( id, data );*/ }
01050 
01051 void DateTable::virtual_hook( int, void* )
01052 { /*BASE::virtual_hook( id, data );*/ }
01053 
01054 }  //KPlato namespace
01055 
01056 #include "kptdatetable.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys