author | zautrix <zautrix> | 2004-09-17 12:38:04 (UTC) |
---|---|---|
committer | zautrix <zautrix> | 2004-09-17 12:38:04 (UTC) |
commit | 053b3550aa2b987d7aeaf74cc458754d7e80a67b (patch) (side-by-side diff) | |
tree | 4190b96b6b581c18943ac7e1cdd5cbca96741eae | |
parent | dd33ac512eb4b4b647fb3423f7cdb39ec322221b (diff) | |
download | kdepimpi-053b3550aa2b987d7aeaf74cc458754d7e80a67b.zip kdepimpi-053b3550aa2b987d7aeaf74cc458754d7e80a67b.tar.gz kdepimpi-053b3550aa2b987d7aeaf74cc458754d7e80a67b.tar.bz2 |
Better datepicker
-rw-r--r-- | microkde/kdatetbl.cpp | 28 | ||||
-rw-r--r-- | microkde/kdatetbl.h | 1 |
2 files changed, 21 insertions, 8 deletions
diff --git a/microkde/kdatetbl.cpp b/microkde/kdatetbl.cpp index 0a2d1f5..146291b 100644 --- a/microkde/kdatetbl.cpp +++ b/microkde/kdatetbl.cpp @@ -50,185 +50,195 @@ KDateValidator::KDateValidator(QWidget* parent, const char* name) : QValidator(parent, name) { } QValidator::State KDateValidator::validate(QString& text, int&) const { QDate temp; // ----- everything is tested in date(): return date(text, temp); } QValidator::State KDateValidator::date(const QString& text, QDate& d) const { QDate tmp = KGlobal::locale()->readDate(text); if (!tmp.isNull()) { d = tmp; return Acceptable; } else return Valid; } void KDateValidator::fixup( QString& ) const { } KDateTable::KDateTable(QWidget *parent, QDate date_, const char* name, WFlags f) : QGridView(parent, name, f) { setFontSize(10); if(!date_.isValid()) { date_=QDate::currentDate(); } setFocusPolicy( QWidget::StrongFocus ); setNumRows(7); // 6 weeks max + headline setNumCols(7); // 7 days a week setHScrollBarMode(AlwaysOff); setVScrollBarMode(AlwaysOff); viewport()->setBackgroundColor(QColor(220,245,255)); #if 0 viewport()->setEraseColor(lightGray); #endif + mMarkCurrent = false; setDate(date_); // this initializes firstday, numdays, numDaysPrevMonth } void KDateTable::paintCell(QPainter *painter, int row, int col) { QRect rect; QString text; QPen pen; int w=cellWidth(); int h=cellHeight(); int pos; QBrush brushBlue(blue); QBrush brushLightblue(QColor(220,245,255)); QFont font=KGlobalSettings::generalFont(); // ----- font.setPointSize(fontsize); if(row==0) { // we are drawing the headline font.setBold(true); painter->setFont(font); bool normalday = true; QString daystr; if (KGlobal::locale()->weekStartsMonday()) { daystr = KGlobal::locale()->weekDayName(col+1, true); if (col == 5 || col == 6) normalday = false; } else { daystr = KGlobal::locale()->weekDayName(col==0? 7 : col, true); if (col == 0 || col == 6) normalday = false; } if (!normalday) { painter->setPen(QColor(220,245,255)); painter->setBrush(brushLightblue); painter->drawRect(0, 0, w, h); painter->setPen(blue); } else { painter->setPen(blue); painter->setBrush(brushBlue); painter->drawRect(0, 0, w, h); painter->setPen(white); } painter->drawText(0, 0, w, h-1, AlignCenter, daystr, -1, &rect); painter->setPen(black); painter->moveTo(0, h-1); painter->lineTo(w-1, h-1); // ----- draw the weekday: } else { painter->setFont(font); pos=7*(row-1)+col; if (KGlobal::locale()->weekStartsMonday()) pos++; if(pos<firstday || (firstday+numdays<=pos)) { // we are either // ° painting a day of the previous month or // ° painting a day of the following month if(pos<firstday) { // previous month text.setNum(numDaysPrevMonth+pos-firstday+1); } else { // following month text.setNum(pos-firstday-numdays+1); } painter->setPen(gray); } else { // paint a day of the current month text.setNum(pos-firstday+1); painter->setPen(black); } pen=painter->pen(); if(firstday+date.day()-1==pos) { + if(mMarkCurrent && firstday+QDate::currentDate().day()-1==pos) + painter->setPen(green); + else + painter->setPen(red); if(hasFocus()) - { // draw the currently selected date - painter->setPen(red); - painter->setBrush(darkRed); - pen=white; + { + painter->setBrush(darkRed); + pen=white; } else { - painter->setPen(darkGray); painter->setBrush(darkGray); pen=white; } } else { - painter->setBrush(QColor(220,245,255)); - painter->setPen(QColor(220,245,255)); - } + if(mMarkCurrent && firstday+QDate::currentDate().day()-1==pos) + { + painter->setPen(green); + painter->setBrush(darkGreen); + pen=white; + } else { + painter->setBrush(QColor(220,245,255)); + painter->setPen(QColor(220,245,255)); + } + } painter->drawRect(0, 0, w, h); painter->setPen(pen); painter->drawText(0, 0, w, h, AlignCenter, text, -1, &rect); } if(rect.width()>maxCell.width()) maxCell.setWidth(rect.width()); if(rect.height()>maxCell.height()) maxCell.setHeight(rect.height()); } void KDateTable::keyPressEvent( QKeyEvent *e ) { /* // not working properly if ( e->key() == Qt::Key_Prior ) { if ( date.month() == 1 ) { KNotifyClient::beep(); return; } int day = date.day(); if ( day > 27 ) while ( !QDate::isValid( date.year(), date.month()-1, day ) ) day--; setDate(QDate(date.year(), date.month()-1, day)); return; } if ( e->key() == Qt::Key_Next ) { if ( date.month() == 12 ) { KNotifyClient::beep(); return; } int day = date.day(); if ( day > 27 ) while ( !QDate::isValid( date.year(), date.month()+1, day ) ) day--; setDate(QDate(date.year(), date.month()+1, day)); return; } */ int dayoff = KGlobal::locale()->weekStartsMonday() ? 1 : 0; int temp=firstday+date.day()-dayoff; int pos = temp; bool irgnore = true; if ( e->state() != Qt::ControlButton ) { if ( e->key() == Qt::Key_Up ) { pos -= 7; irgnore = false; } @@ -312,107 +322,109 @@ void KDateTable::contentsMousePressEvent(QMouseEvent *e) { if(e->type()!=QEvent::MouseButtonPress) { // the KDatePicker only reacts on mouse press events: return; } if(!isEnabled()) { KNotifyClient::beep(); return; } int dayoff = KGlobal::locale()->weekStartsMonday() ? 1 : 0; // ----- int row, col, pos, temp; QPoint mouseCoord; // ----- mouseCoord = e->pos(); row=rowAt(mouseCoord.y()); col=columnAt(mouseCoord.x()); if(row<0 || col<0) { // the user clicked on the frame of the table return; } pos=7*(row-1)+col+1; if(pos+dayoff<=firstday) { // this day is in the previous month KNotifyClient::beep(); return; } if(firstday+numdays<pos+dayoff) { // this date is in the next month KNotifyClient::beep(); return; } temp=firstday+date.day()-dayoff-1; setDate(QDate(date.year(), date.month(), pos-firstday+dayoff)); updateCell(temp/7+1, temp%7); // Update the previously selected cell updateCell(row, col); // Update the selected cell // assert(QDate(date.year(), date.month(), pos-firstday+dayoff).isValid()); emit(tableClicked()); } bool KDateTable::setDate(const QDate& date_) { bool changed=false; QDate temp; + mMarkCurrent = false; // ----- if(!date_.isValid()) { kdDebug() << "KDateTable::setDate: refusing to set invalid date." << endl; return false; } if(date!=date_) { date=date_; changed=true; } + mMarkCurrent = ( date.month() == QDate::currentDate().month() && date.year() == QDate::currentDate().year() ); temp.setYMD(date.year(), date.month(), 1); firstday=temp.dayOfWeek(); if(firstday==1) firstday=8; numdays=date.daysInMonth(); if(date.month()==1) { // set to december of previous year temp.setYMD(date.year()-1, 12, 1); } else { // set to previous month temp.setYMD(date.year(), date.month()-1, 1); } numDaysPrevMonth=temp.daysInMonth(); if(changed) { repaintContents(false); } emit(dateChanged(date)); return true; } const QDate& KDateTable::getDate() const { return date; } void KDateTable::focusInEvent( QFocusEvent *e ) { repaintContents(false); QGridView::focusInEvent( e ); } void KDateTable::focusOutEvent( QFocusEvent *e ) { repaintContents(false); QGridView::focusOutEvent( e ); } QSize KDateTable::sizeHint() const { if(maxCell.height()>0 && maxCell.width()>0) { return QSize((maxCell.width()+2)*numCols()+2*frameWidth(), (maxCell.height()+4)*numRows()+2*frameWidth()); } else { return QSize(-1, -1); } } diff --git a/microkde/kdatetbl.h b/microkde/kdatetbl.h index df7b7ef..b4d3e16 100644 --- a/microkde/kdatetbl.h +++ b/microkde/kdatetbl.h @@ -200,96 +200,97 @@ public: virtual State validate(QString&, int&) const; virtual void fixup ( QString & input ) const; State date(const QString&, QDate&) const; }; /** * Date selection table. * This is a support class for the KDatePicker class. It just * draws the calender table without titles, but could theoretically * be used as a standalone. * * When a date is selected by the user, it emits a signal: * dateSelected(QDate) * * @internal * @version $Id$ * @author Tim Gilman, Mirko Boehm */ class KDateTable : public QGridView { Q_OBJECT public: /** * The constructor. */ KDateTable(QWidget *parent=0, QDate date=QDate::currentDate(), const char* name=0, WFlags f=0); /** * Returns a recommended size for the widget. * To save some time, the size of the largest used cell content is * calculated in each paintCell() call, since all calculations have * to be done there anyway. The size is stored in maxCell. The * sizeHint() simply returns a multiple of maxCell. */ virtual QSize sizeHint() const; /** * Set the font size of the date table. */ void setFontSize(int size); /** * Select and display this date. */ bool setDate(const QDate&); const QDate& getDate() const; protected: + bool mMarkCurrent; /** * Paint a cell. */ virtual void paintCell(QPainter*, int, int); /** * Handle the resize events. */ virtual void viewportResizeEvent(QResizeEvent *); /** * React on mouse clicks that select a date. */ virtual void contentsMousePressEvent(QMouseEvent *); virtual void keyPressEvent( QKeyEvent *e ); virtual void focusInEvent( QFocusEvent *e ); virtual void focusOutEvent( QFocusEvent *e ); /** * The font size of the displayed text. */ int fontsize; /** * The currently selected date. */ QDate date; /** * The day of the first day in the month [1..7]. */ int firstday; /** * The number of days in the current month. */ int numdays; /** * The number of days in the previous month. */ int numDaysPrevMonth; /** * unused */ bool unused_hasSelection; /** * Save the size of the largest used cell content. */ QRect maxCell; signals: /** * The selected date changed. */ void dateChanged(QDate); |