kdeui Library API Documentation

kdatetbl.cpp

00001 /*  -*- C++ -*-
00002     This file is part of the KDE libraries
00003     Copyright (C) 1997 Tim D. Gilman (tdgilman@best.org)
00004               (C) 1998-2001 Mirko Boehm (mirko@kde.org)
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Library General Public
00007     License as published by the Free Software Foundation; either
00008     version 2 of the License, or (at your option) any later version.
00009 
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013     Library General Public License for more details.
00014 
00015     You should have received a copy of the GNU Library General Public License
00016     along with this library; see the file COPYING.LIB.  If not, write to
00017     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018     Boston, MA 02111-1307, USA.
00019 */
00020 
00022 //
00023 // Copyright (C) 1997 Tim D. Gilman
00024 //           (C) 1998-2001 Mirko Boehm
00025 // Written using Qt (http://www.troll.no) for the
00026 // KDE project (http://www.kde.org)
00027 //
00028 // This is a support class for the KDatePicker class.  It just
00029 // draws the calender table without titles, but could theoretically
00030 // be used as a standalone.
00031 //
00032 // When a date is selected by the user, it emits a signal:
00033 //      dateSelected(QDate)
00034 
00035 #include <kconfig.h>
00036 #include <kglobal.h>
00037 #include <kglobalsettings.h>
00038 #include <kapplication.h>
00039 #include <klocale.h>
00040 #include <kdebug.h>
00041 #include <knotifyclient.h>
00042 #include <kcalendarsystem.h>
00043 #include "kdatepicker.h"
00044 #include "kdatetbl.h"
00045 #include "kpopupmenu.h"
00046 #include <qdatetime.h>
00047 #include <qstring.h>
00048 #include <qpen.h>
00049 #include <qpainter.h>
00050 #include <qdialog.h>
00051 #include <qdict.h>
00052 #include <assert.h>
00053 
00054 
00055 class KDateTable::KDateTablePrivate
00056 {
00057 public:
00058    KDateTablePrivate()
00059    {
00060       popupMenuEnabled=false;
00061       useCustomColors=false;
00062    }
00063 
00064    ~KDateTablePrivate()
00065    {
00066    }
00067 
00068    bool popupMenuEnabled;
00069    bool useCustomColors;
00070 
00071    struct DatePaintingMode
00072    {
00073      QColor fgColor;
00074      QColor bgColor;
00075      BackgroundMode bgMode;
00076    };
00077    QDict <DatePaintingMode> customPaintingModes;
00078 
00079 };
00080 
00081 
00082 KDateValidator::KDateValidator(QWidget* parent, const char* name)
00083     : QValidator(parent, name)
00084 {
00085 }
00086 
00087 QValidator::State
00088 KDateValidator::validate(QString& text, int&) const
00089 {
00090   QDate temp;
00091   // ----- everything is tested in date():
00092   return date(text, temp);
00093 }
00094 
00095 QValidator::State
00096 KDateValidator::date(const QString& text, QDate& d) const
00097 {
00098   QDate tmp = KGlobal::locale()->readDate(text);
00099   if (!tmp.isNull())
00100     {
00101       d = tmp;
00102       return Acceptable;
00103     } else
00104       return Valid;
00105 }
00106 
00107 void
00108 KDateValidator::fixup( QString& ) const
00109 {
00110 
00111 }
00112 
00113 KDateTable::KDateTable(QWidget *parent, QDate date_, const char* name, WFlags f)
00114   : QGridView(parent, name, f)
00115 {
00116   d = new KDateTablePrivate;
00117   setFontSize(10);
00118   if(!date_.isValid())
00119     {
00120       kdDebug() << "KDateTable ctor: WARNING: Given date is invalid, using current date." << endl;
00121       date_=QDate::currentDate();
00122     }
00123   setFocusPolicy( QWidget::StrongFocus );
00124   setNumRows(7); // 6 weeks max + headline
00125   setNumCols(7); // 7 days a week
00126   setHScrollBarMode(AlwaysOff);
00127   setVScrollBarMode(AlwaysOff);
00128   viewport()->setEraseColor(KGlobalSettings::baseColor());
00129   setDate(date_); // this initializes firstday, numdays, numDaysPrevMonth
00130 }
00131 
00132 KDateTable::~KDateTable()
00133 {
00134   delete d;
00135 }
00136 
00137 int KDateTable::posFromDate( const QDate &dt )
00138 {
00139   const KCalendarSystem * calendar = KGlobal::locale()->calendar();
00140   const int firstWeekDay = KGlobal::locale()->weekStartDay();
00141   int pos = calendar->day( dt );
00142   int offset = (firstday - firstWeekDay + 7) % 7;
00143   // make sure at least one day of the previous month is visible.
00144   // adjust this <1 if more days should be forced visible:
00145   if ( offset < 1 ) offset += 7;
00146   return pos + offset;
00147 }
00148 
00149 QDate KDateTable::dateFromPos( int pos )
00150 {
00151   QDate pCellDate;
00152   const KCalendarSystem * calendar = KGlobal::locale()->calendar();
00153   calendar->setYMD(pCellDate, calendar->year(date), calendar->month(date), 1);
00154 
00155   int firstWeekDay = KGlobal::locale()->weekStartDay();
00156   int offset = (firstday - firstWeekDay + 7) % 7;
00157   // make sure at least one day of the previous month is visible.
00158   // adjust this <1 if more days should be forced visible:
00159   if ( offset < 1 ) offset += 7;
00160   pCellDate = calendar->addDays( pCellDate, pos - offset );
00161   return pCellDate;
00162 }
00163 
00164 void
00165 KDateTable::paintCell(QPainter *painter, int row, int col)
00166 {
00167   const KCalendarSystem * calendar = KGlobal::locale()->calendar();
00168 
00169   QRect rect;
00170   QString text;
00171   QPen pen;
00172   int w=cellWidth();
00173   int h=cellHeight();
00174   QFont font=KGlobalSettings::generalFont();
00175   // -----
00176 
00177   if(row==0)
00178     { // we are drawing the headline
00179       font.setBold(true);
00180       painter->setFont(font);
00181       bool normalday = true;
00182       int firstWeekDay = KGlobal::locale()->weekStartDay();
00183       int daynum = ( col+firstWeekDay < 8 ) ? col+firstWeekDay :
00184                                               col+firstWeekDay-7;
00185       if ( daynum == calendar->weekDayOfPray() ||
00186          ( daynum == 6 && calendar->calendarName() == "gregorian" ) )
00187           normalday=false;
00188 
00189             QBrush brushTitle();
00190             QBrush brushInvertTitle(colorGroup().base());
00191             QColor titleColor(isEnabled()?( KGlobalSettings::activeTitleColor() ):( KGlobalSettings::inactiveTitleColor() ) );
00192             QColor textColor(isEnabled()?( KGlobalSettings::activeTextColor() ):( KGlobalSettings::inactiveTextColor() ) );
00193       if (!normalday)
00194         {
00195           painter->setPen(textColor);
00196           painter->setBrush(textColor);
00197           painter->drawRect(0, 0, w, h);
00198           painter->setPen(titleColor);
00199         } else {
00200           painter->setPen(titleColor);
00201           painter->setBrush(titleColor);
00202           painter->drawRect(0, 0, w, h);
00203           painter->setPen(textColor);
00204         }
00205       painter->drawText(0, 0, w, h-1, AlignCenter,
00206                         calendar->weekDayName(daynum, true), -1, &rect);
00207       painter->setPen(colorGroup().text());
00208       painter->moveTo(0, h-1);
00209       painter->lineTo(w-1, h-1);
00210       // ----- draw the weekday:
00211     } else {
00212       bool paintRect=true;
00213       painter->setFont(font);
00214       int pos=7*(row-1)+col;
00215 
00216       QDate pCellDate = dateFromPos( pos );
00217       // First day of month
00218       text = calendar->dayString(pCellDate, true);
00219       if( calendar->month(pCellDate) != calendar->month(date) )
00220         { // we are either
00221           // ° painting a day of the previous month or
00222           // ° painting a day of the following month
00223           // TODO: don't hardcode gray here! Use a color with less contrast to the background than normal text.
00224           painter->setPen( colorGroup().mid() );
00225 //          painter->setPen(gray);
00226         } else { // paint a day of the current month
00227           if ( d->useCustomColors )
00228           {
00229             KDateTablePrivate::DatePaintingMode *mode=d->customPaintingModes[pCellDate.toString()];
00230             if (mode)
00231             {
00232               if (mode->bgMode != NoBgMode)
00233               {
00234                 QBrush oldbrush=painter->brush();
00235                 painter->setBrush( mode->bgColor );
00236                 switch(mode->bgMode)
00237                 {
00238                   case(CircleMode) : painter->drawEllipse(0,0,w,h);break;
00239                   case(RectangleMode) : painter->drawRect(0,0,w,h);break;
00240                   case(NoBgMode) : // Should never be here, but just to get one
00241                                    // less warning when compiling
00242                   default: break;
00243                 }
00244                 painter->setBrush( oldbrush );
00245                 paintRect=false;
00246               }
00247               painter->setPen( mode->fgColor );
00248             } else
00249               painter->setPen(colorGroup().text());
00250           } else //if ( firstWeekDay < 4 ) // <- this doesn' make sense at all!
00251           painter->setPen(colorGroup().text());
00252         }
00253 
00254       pen=painter->pen();
00255       int firstWeekDay=KGlobal::locale()->weekStartDay();
00256       int offset=firstday-firstWeekDay;
00257       if(offset<1)
00258         offset+=7;
00259       int d = calendar->day(date);
00260            if( (offset+d) == (pos+1))
00261         {
00262            // draw the currently selected date
00263        if (isEnabled())
00264        {
00265            painter->setPen(colorGroup().highlight());
00266            painter->setBrush(colorGroup().highlight());
00267        }
00268        else 
00269        {
00270        painter->setPen(colorGroup().text());
00271            painter->setBrush(colorGroup().text());
00272        }
00273            pen=colorGroup().highlightedText();
00274         } else {
00275           painter->setBrush(paletteBackgroundColor());
00276           painter->setPen(paletteBackgroundColor());
00277 //          painter->setBrush(colorGroup().base());
00278 //          painter->setPen(colorGroup().base());
00279         }
00280 
00281       if ( pCellDate == QDate::currentDate() )
00282       {
00283          painter->setPen(colorGroup().text());
00284       }
00285 
00286       if ( paintRect ) painter->drawRect(0, 0, w, h);
00287       painter->setPen(pen);
00288       painter->drawText(0, 0, w, h, AlignCenter, text, -1, &rect);
00289     }
00290   if(rect.width()>maxCell.width()) maxCell.setWidth(rect.width());
00291   if(rect.height()>maxCell.height()) maxCell.setHeight(rect.height());
00292 }
00293 
00294 void
00295 KDateTable::keyPressEvent( QKeyEvent *e )
00296 {
00297     const KCalendarSystem * calendar = KGlobal::locale()->calendar();
00298     QDate temp = date;
00299 
00300     switch( e->key() ) {
00301     case Key_Prior:
00302         temp = calendar->addMonths( date, -1 );
00303         setDate(temp);
00304         return;
00305     case Key_Next:
00306         temp = calendar->addMonths( date, 1 );
00307         setDate(temp);
00308         return;
00309     case Key_Up:
00310         if ( calendar->day(date) > 7 ) {
00311             setDate(date.addDays(-7));
00312             return;
00313         }
00314         break;
00315     case Key_Down:
00316         if ( calendar->day(date) <= calendar->daysInMonth(date)-7 ) {
00317             setDate(date.addDays(7));
00318             return;
00319         }
00320         break;
00321     case Key_Left:
00322         if ( calendar->day(date) > 1 ) {
00323             setDate(date.addDays(-1));
00324             return;
00325         }
00326         break;
00327     case Key_Right:
00328         if ( calendar->day(date) < calendar->daysInMonth(date) ) {
00329             setDate(date.addDays(1));
00330             return;
00331         }
00332         break;
00333     case Key_Minus:
00334         setDate(date.addDays(-1));
00335         return;
00336     case Key_Plus:
00337         setDate(date.addDays(1));
00338         return;
00339     case Key_N:
00340         setDate(QDate::currentDate());
00341         return;
00342     case Key_Return:
00343     case Key_Enter:
00344         emit tableClicked();
00345         return;
00346     default:
00347         break;
00348     }
00349 
00350     KNotifyClient::beep();
00351 }
00352 
00353 void
00354 KDateTable::viewportResizeEvent(QResizeEvent * e)
00355 {
00356   QGridView::viewportResizeEvent(e);
00357 
00358   setCellWidth(viewport()->width()/7);
00359   setCellHeight(viewport()->height()/7);
00360 }
00361 
00362 void
00363 KDateTable::setFontSize(int size)
00364 {
00365   int count;
00366   QFontMetrics metrics(fontMetrics());
00367   QRect rect;
00368   // ----- store rectangles:
00369   fontsize=size;
00370   // ----- find largest day name:
00371   maxCell.setWidth(0);
00372   maxCell.setHeight(0);
00373   for(count=0; count<7; ++count)
00374     {
00375       rect=metrics.boundingRect(KGlobal::locale()->calendar()
00376                                 ->weekDayName(count+1, true));
00377       maxCell.setWidth(QMAX(maxCell.width(), rect.width()));
00378       maxCell.setHeight(QMAX(maxCell.height(), rect.height()));
00379     }
00380   // ----- compare with a real wide number and add some space:
00381   rect=metrics.boundingRect(QString::fromLatin1("88"));
00382   maxCell.setWidth(QMAX(maxCell.width()+2, rect.width()));
00383   maxCell.setHeight(QMAX(maxCell.height()+4, rect.height()));
00384 }
00385 
00386 void
00387 KDateTable::wheelEvent ( QWheelEvent * e )
00388 {
00389     setDate(date.addMonths( -(int)(e->delta()/120)) );
00390     e->accept();
00391 }
00392 
00393 void
00394 KDateTable::contentsMousePressEvent(QMouseEvent *e)
00395 {
00396 
00397   if(e->type()!=QEvent::MouseButtonPress)
00398     { // the KDatePicker only reacts on mouse press events:
00399       return;
00400     }
00401   if(!isEnabled())
00402     {
00403       KNotifyClient::beep();
00404       return;
00405     }
00406 
00407   // -----
00408   int row, col, pos, temp;
00409   QPoint mouseCoord;
00410   // -----
00411   mouseCoord = e->pos();
00412   row=rowAt(mouseCoord.y());
00413   col=columnAt(mouseCoord.x());
00414   if(row<1 || col<0)
00415     { // the user clicked on the frame of the table
00416       return;
00417     }
00418 
00419   // Rows and columns are zero indexed.  The (row - 1) below is to avoid counting
00420   // the row with the days of the week in the calculation.
00421 
00422   // old selected date:
00423   temp = posFromDate( date );
00424   // new position and date
00425   pos = (7 * (row - 1)) + col; 
00426   QDate clickedDate = dateFromPos( pos );
00427 
00428   // set the new date. If it is in the previous or next month, the month will
00429   // automatically be changed, no need to do that manually...
00430   setDate( clickedDate );
00431 
00432   // call updateCell on the old and new selection. If setDate switched to a different 
00433   // month, these cells will be painted twice, but that's no problem.
00434   updateCell( temp/7+1, temp%7 );
00435   updateCell( row, col );
00436 
00437   emit tableClicked();
00438 
00439   if (  e->button() == Qt::RightButton && d->popupMenuEnabled )
00440   {
00441         KPopupMenu *menu = new KPopupMenu();
00442         menu->insertTitle( KGlobal::locale()->formatDate(clickedDate) );
00443         emit aboutToShowContextMenu( menu, clickedDate );
00444         menu->popup(e->globalPos());
00445   }
00446 }
00447 
00448 bool
00449 KDateTable::setDate(const QDate& date_)
00450 {
00451   bool changed=false;
00452   QDate temp;
00453   // -----
00454   if(!date_.isValid())
00455     {
00456       kdDebug() << "KDateTable::setDate: refusing to set invalid date." << endl;
00457       return false;
00458     }
00459   if(date!=date_)
00460     {
00461       emit(dateChanged(date, date_));
00462       date=date_;
00463       emit(dateChanged(date));
00464       changed=true;
00465     }
00466   const KCalendarSystem * calendar = KGlobal::locale()->calendar();
00467 
00468   calendar->setYMD(temp, calendar->year(date), calendar->month(date), 1);
00469   //temp.setYMD(date.year(), date.month(), 1);
00470   //kdDebug() << "firstDayInWeek: " << temp.toString() << endl;
00471   firstday=temp.dayOfWeek();
00472   numdays=calendar->daysInMonth(date);
00473 
00474   temp = calendar->addMonths(temp, -1);
00475   numDaysPrevMonth=calendar->daysInMonth(temp);
00476   if(changed)
00477     {
00478       repaintContents(false);
00479     }
00480   return true;
00481 }
00482 
00483 const QDate&
00484 KDateTable::getDate() const
00485 {
00486   return date;
00487 }
00488 
00489 // what are those repaintContents() good for? (pfeiffer)
00490 void KDateTable::focusInEvent( QFocusEvent *e )
00491 {
00492 //    repaintContents(false);
00493     QGridView::focusInEvent( e );
00494 }
00495 
00496 void KDateTable::focusOutEvent( QFocusEvent *e )
00497 {
00498 //    repaintContents(false);
00499     QGridView::focusOutEvent( e );
00500 }
00501 
00502 QSize
00503 KDateTable::sizeHint() const
00504 {
00505   if(maxCell.height()>0 && maxCell.width()>0)
00506     {
00507       return QSize(maxCell.width()*numCols()+2*frameWidth(),
00508              (maxCell.height()+2)*numRows()+2*frameWidth());
00509     } else {
00510       kdDebug() << "KDateTable::sizeHint: obscure failure - " << endl;
00511       return QSize(-1, -1);
00512     }
00513 }
00514 
00515 void KDateTable::setPopupMenuEnabled( bool enable )
00516 {
00517    d->popupMenuEnabled=enable;
00518 }
00519 
00520 bool KDateTable::popupMenuEnabled() const
00521 {
00522    return d->popupMenuEnabled;
00523 }
00524 
00525 void KDateTable::setCustomDatePainting(const QDate &date, const QColor &fgColor, BackgroundMode bgMode, const QColor &bgColor)
00526 {
00527     if (!fgColor.isValid())
00528     {
00529         unsetCustomDatePainting( date );
00530         return;
00531     }
00532 
00533     KDateTablePrivate::DatePaintingMode *mode=new KDateTablePrivate::DatePaintingMode;
00534     mode->bgMode=bgMode;
00535     mode->fgColor=fgColor;
00536     mode->bgColor=bgColor;
00537 
00538     d->customPaintingModes.replace( date.toString(), mode );
00539     d->useCustomColors=true;
00540     update();
00541 }
00542 
00543 void KDateTable::unsetCustomDatePainting( const QDate &date )
00544 {
00545     d->customPaintingModes.remove( date.toString() );
00546 }
00547 
00548 KDateInternalWeekSelector::KDateInternalWeekSelector
00549 (QWidget* parent, const char* name)
00550   : QLineEdit(parent, name),
00551     val(new QIntValidator(this)),
00552     result(0)
00553 {
00554   QFont font;
00555   // -----
00556   font=KGlobalSettings::generalFont();
00557   setFont(font);
00558   setFrameStyle(QFrame::NoFrame);
00559   setValidator(val);
00560   connect(this, SIGNAL(returnPressed()), SLOT(weekEnteredSlot()));
00561 }
00562 
00563 void
00564 KDateInternalWeekSelector::weekEnteredSlot()
00565 {
00566   bool ok;
00567   int week;
00568   // ----- check if this is a valid week:
00569   week=text().toInt(&ok);
00570   if(!ok)
00571     {
00572       KNotifyClient::beep();
00573       return;
00574     }
00575   result=week;
00576   emit(closeMe(1));
00577 }
00578 
00579 int
00580 KDateInternalWeekSelector::getWeek()
00581 {
00582   return result;
00583 }
00584 
00585 void
00586 KDateInternalWeekSelector::setWeek(int week)
00587 {
00588   QString temp;
00589   // -----
00590   temp.setNum(week);
00591   setText(temp);
00592 }
00593 
00594 void
00595 KDateInternalWeekSelector::setMaxWeek(int max)
00596 {
00597   val->setRange(1, max);
00598 }
00599 
00600 // ### CFM To avoid binary incompatibility.
00601 //     In future releases, remove this and replace by  a QDate
00602 //     private member, needed in KDateInternalMonthPicker::paintCell
00603 class KDateInternalMonthPicker::KDateInternalMonthPrivate {
00604 public:
00605         KDateInternalMonthPrivate (int y, int m, int d)
00606         : year(y), month(m), day(d)
00607         {};
00608         int year;
00609         int month;
00610         int day;
00611 };
00612 
00613 KDateInternalMonthPicker::~KDateInternalMonthPicker() {
00614    delete d;
00615 }
00616 
00617 KDateInternalMonthPicker::KDateInternalMonthPicker
00618 (const QDate & date, QWidget* parent, const char* name)
00619   : QGridView(parent, name),
00620     result(0) // invalid
00621 {
00622   QRect rect;
00623   QFont font;
00624   // -----
00625   activeCol = -1;
00626   activeRow = -1;
00627   font=KGlobalSettings::generalFont();
00628   setFont(font);
00629   setHScrollBarMode(AlwaysOff);
00630   setVScrollBarMode(AlwaysOff);
00631   setFrameStyle(QFrame::NoFrame);
00632   setNumCols(3);
00633   d = new KDateInternalMonthPrivate(date.year(), date.month(), date.day());
00634   // For monthsInYear != 12
00635   setNumRows( (KGlobal::locale()->calendar()->monthsInYear(date) + 2) / 3);
00636   // enable to find drawing failures:
00637   // setTableFlags(Tbl_clipCellPainting);
00638   viewport()->setEraseColor(KGlobalSettings::baseColor()); // for consistency with the datepicker
00639   // ----- find the preferred size
00640   //       (this is slow, possibly, but unfortunately it is needed here):
00641   QFontMetrics metrics(font);
00642   for(int i = 1; ; ++i)
00643     {
00644       QString str = KGlobal::locale()->calendar()->monthName(i,
00645          KGlobal::locale()->calendar()->year(date), false);
00646       if (str.isNull()) break;
00647       rect=metrics.boundingRect(str);
00648       if(max.width()<rect.width()) max.setWidth(rect.width());
00649       if(max.height()<rect.height()) max.setHeight(rect.height());
00650     }
00651 }
00652 
00653 QSize
00654 KDateInternalMonthPicker::sizeHint() const
00655 {
00656   return QSize((max.width()+6)*numCols()+2*frameWidth(),
00657          (max.height()+6)*numRows()+2*frameWidth());
00658 }
00659 
00660 int
00661 KDateInternalMonthPicker::getResult() const
00662 {
00663   return result;
00664 }
00665 
00666 void
00667 KDateInternalMonthPicker::setupPainter(QPainter *p)
00668 {
00669   p->setPen(KGlobalSettings::textColor());
00670 }
00671 
00672 void
00673 KDateInternalMonthPicker::viewportResizeEvent(QResizeEvent*)
00674 {
00675   setCellWidth(width() / numCols());
00676   setCellHeight(height() / numRows());
00677 }
00678 
00679 void
00680 KDateInternalMonthPicker::paintCell(QPainter* painter, int row, int col)
00681 {
00682   int index;
00683   QString text;
00684   // ----- find the number of the cell:
00685   index=3*row+col+1;
00686   text=KGlobal::locale()->calendar()->monthName(index,
00687     KGlobal::locale()->calendar()->year(QDate(d->year, d->month,
00688     d->day)), false);
00689   painter->drawText(0, 0, cellWidth(), cellHeight(), AlignCenter, text);
00690   if ( activeCol == col && activeRow == row )
00691       painter->drawRect( 0, 0, cellWidth(), cellHeight() );
00692 }
00693 
00694 void
00695 KDateInternalMonthPicker::contentsMousePressEvent(QMouseEvent *e)
00696 {
00697   if(!isEnabled() || e->button() != LeftButton)
00698     {
00699       KNotifyClient::beep();
00700       return;
00701     }
00702   // -----
00703   int row, col;
00704   QPoint mouseCoord;
00705   // -----
00706   mouseCoord = e->pos();
00707   row=rowAt(mouseCoord.y());
00708   col=columnAt(mouseCoord.x());
00709 
00710   if(row<0 || col<0)
00711     { // the user clicked on the frame of the table
00712       activeCol = -1;
00713       activeRow = -1;
00714     } else {
00715       activeCol = col;
00716       activeRow = row;
00717       updateCell( row, col /*, false */ );
00718   }
00719 }
00720 
00721 void
00722 KDateInternalMonthPicker::contentsMouseMoveEvent(QMouseEvent *e)
00723 {
00724   if (e->state() & LeftButton)
00725     {
00726       int row, col;
00727       QPoint mouseCoord;
00728       // -----
00729       mouseCoord = e->pos();
00730       row=rowAt(mouseCoord.y());
00731       col=columnAt(mouseCoord.x());
00732       int tmpRow = -1, tmpCol = -1;
00733       if(row<0 || col<0)
00734         { // the user clicked on the frame of the table
00735           if ( activeCol > -1 )
00736             {
00737               tmpRow = activeRow;
00738               tmpCol = activeCol;
00739             }
00740           activeCol = -1;
00741           activeRow = -1;
00742         } else {
00743           bool differentCell = (activeRow != row || activeCol != col);
00744           if ( activeCol > -1 && differentCell)
00745             {
00746               tmpRow = activeRow;
00747               tmpCol = activeCol;
00748             }
00749           if ( differentCell)
00750             {
00751               activeRow = row;
00752               activeCol = col;
00753               updateCell( row, col /*, false */ ); // mark the new active cell
00754             }
00755         }
00756       if ( tmpRow > -1 ) // repaint the former active cell
00757           updateCell( tmpRow, tmpCol /*, true */ );
00758     }
00759 }
00760 
00761 void
00762 KDateInternalMonthPicker::contentsMouseReleaseEvent(QMouseEvent *e)
00763 {
00764   if(!isEnabled())
00765     {
00766       return;
00767     }
00768   // -----
00769   int row, col, pos;
00770   QPoint mouseCoord;
00771   // -----
00772   mouseCoord = e->pos();
00773   row=rowAt(mouseCoord.y());
00774   col=columnAt(mouseCoord.x());
00775   if(row<0 || col<0)
00776     { // the user clicked on the frame of the table
00777       emit(closeMe(0));
00778     }
00779 
00780   pos=3*row+col+1;
00781   result=pos;
00782   emit(closeMe(1));
00783 }
00784 
00785 
00786 
00787 KDateInternalYearSelector::KDateInternalYearSelector
00788 (QWidget* parent, const char* name)
00789   : QLineEdit(parent, name),
00790     val(new QIntValidator(this)),
00791     result(0)
00792 {
00793   QFont font;
00794   // -----
00795   font=KGlobalSettings::generalFont();
00796   setFont(font);
00797   setFrameStyle(QFrame::NoFrame);
00798   // we have to respect the limits of QDate here, I fear:
00799   val->setRange(0, 8000);
00800   setValidator(val);
00801   connect(this, SIGNAL(returnPressed()), SLOT(yearEnteredSlot()));
00802 }
00803 
00804 void
00805 KDateInternalYearSelector::yearEnteredSlot()
00806 {
00807   bool ok;
00808   int year;
00809   QDate date;
00810   // ----- check if this is a valid year:
00811   year=text().toInt(&ok);
00812   if(!ok)
00813     {
00814       KNotifyClient::beep();
00815       return;
00816     }
00817   //date.setYMD(year, 1, 1);
00818   KGlobal::locale()->calendar()->setYMD(date, year, 1, 1);
00819   if(!date.isValid())
00820     {
00821       KNotifyClient::beep();
00822       return;
00823     }
00824   result=year;
00825   emit(closeMe(1));
00826 }
00827 
00828 int
00829 KDateInternalYearSelector::getYear()
00830 {
00831   return result;
00832 }
00833 
00834 void
00835 KDateInternalYearSelector::setYear(int year)
00836 {
00837   QString temp;
00838   // -----
00839   temp.setNum(year);
00840   setText(temp);
00841 }
00842 
00843 KPopupFrame::KPopupFrame(QWidget* parent, const char*  name)
00844   : QFrame(parent, name, WType_Popup),
00845     result(0), // rejected
00846     main(0)
00847 {
00848   setFrameStyle(QFrame::Box|QFrame::Raised);
00849   setMidLineWidth(2);
00850 }
00851 
00852 void
00853 KPopupFrame::keyPressEvent(QKeyEvent* e)
00854 {
00855   if(e->key()==Key_Escape)
00856     {
00857       result=0; // rejected
00858       qApp->exit_loop();
00859     }
00860 }
00861 
00862 void
00863 KPopupFrame::close(int r)
00864 {
00865   result=r;
00866   qApp->exit_loop();
00867 }
00868 
00869 void
00870 KPopupFrame::setMainWidget(QWidget* m)
00871 {
00872   main=m;
00873   if(main!=0)
00874     {
00875       resize(main->width()+2*frameWidth(), main->height()+2*frameWidth());
00876     }
00877 }
00878 
00879 void
00880 KPopupFrame::resizeEvent(QResizeEvent*)
00881 {
00882   if(main!=0)
00883     {
00884       main->setGeometry(frameWidth(), frameWidth(),
00885           width()-2*frameWidth(), height()-2*frameWidth());
00886     }
00887 }
00888 
00889 void
00890 KPopupFrame::popup(const QPoint &pos)
00891 {
00892   // Make sure the whole popup is visible.
00893   QRect d = KGlobalSettings::desktopGeometry(pos);
00894 
00895   int x = pos.x();
00896   int y = pos.y();
00897   int w = width();
00898   int h = height();
00899   if (x+w > d.x()+d.width())
00900     x = d.width() - w;
00901   if (y+h > d.y()+d.height())
00902     y = d.height() - h;
00903   if (x < d.x())
00904     x = 0;
00905   if (y < d.y())
00906     y = 0;
00907 
00908   // Pop the thingy up.
00909   move(x, y);
00910   show();
00911 }
00912 
00913 int
00914 KPopupFrame::exec(QPoint pos)
00915 {
00916   popup(pos);
00917   repaint();
00918   qApp->enter_loop();
00919   hide();
00920   return result;
00921 }
00922 
00923 int
00924 KPopupFrame::exec(int x, int y)
00925 {
00926   return exec(QPoint(x, y));
00927 }
00928 
00929 void KPopupFrame::virtual_hook( int, void* )
00930 { /*BASE::virtual_hook( id, data );*/ }
00931 
00932 void KDateTable::virtual_hook( int, void* )
00933 { /*BASE::virtual_hook( id, data );*/ }
00934 
00935 #include "kdatetbl.moc"
KDE Logo
This file is part of the documentation for kdeui Library Version 3.3.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Sep 23 17:11:53 2004 by doxygen 1.3.8-20040913 written by Dimitri van Heesch, © 1997-2003