kdeui Library API Documentation

kdatepicker.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 
00021 #include <qlayout.h>
00022 #include <qframe.h>
00023 #include <qpainter.h>
00024 #include <qdialog.h>
00025 #include <qstyle.h>
00026 #include <qtoolbutton.h>
00027 #include <qcombobox.h>
00028 #include <qtooltip.h>
00029 #include <qfont.h>
00030 #include <qvalidator.h>
00031 #include <qpopupmenu.h>
00032 
00033 #include "kdatepicker.h"
00034 #include <kglobal.h>
00035 #include <kapplication.h>
00036 #include <klocale.h>
00037 #include <kiconloader.h>
00038 #include <ktoolbar.h>
00039 #include <klineedit.h>
00040 #include <kdebug.h>
00041 #include <knotifyclient.h>
00042 #include <kcalendarsystem.h>
00043 
00044 #include "kdatetbl.h"
00045 #include "kdatepicker.moc"
00046 
00047 // Week numbers are defined by ISO 8601
00048 // See http://www.merlyn.demon.co.uk/weekinfo.htm for details
00049 
00050 class KDatePicker::KDatePickerPrivate
00051 {
00052 public:
00053     KDatePickerPrivate() : closeButton(0L), selectWeek(0L), todayButton(0), navigationLayout(0) {}
00054 
00055     void fillWeeksCombo(const QDate &date);
00056 
00057     KToolBar *tb;
00058     QToolButton *closeButton;
00059     QComboBox *selectWeek;
00060     QToolButton *todayButton;
00061     QBoxLayout *navigationLayout;
00062 };
00063 
00064 void KDatePicker::fillWeeksCombo(const QDate &date)
00065 {
00066   // every year can have a different number of weeks
00067   const KCalendarSystem * calendar = KGlobal::locale()->calendar();
00068 
00069   // it could be that we had 53,1..52 and now 1..53 which is the same number but different
00070   // so always fill with new values
00071 
00072   d->selectWeek->clear();
00073 
00074   // We show all week numbers for all weeks between first day of year to last day of year
00075   // This of course can be a list like 53,1,2..52
00076 
00077   QDate day(date.year(), 1, 1);
00078   int lastMonth = calendar->monthsInYear(day);
00079   QDate lastDay(date.year(), lastMonth, calendar->daysInMonth(QDate(date.year(), lastMonth, 1)));
00080 
00081   for (; day <= lastDay; day = calendar->addDays(day, 7 /*calendar->daysOfWeek()*/) )
00082   {
00083     int year = 0;
00084     QString week = i18n("Week %1").arg(calendar->weekNumber(day, &year));
00085     if ( year != date.year() ) week += "*";  // show that this is a week from a different year
00086     d->selectWeek->insertItem(week);
00087   }
00088 }
00089 
00090 KDatePicker::KDatePicker(QWidget *parent, QDate dt, const char *name)
00091   : QFrame(parent,name)
00092 {
00093   init( dt );
00094 }
00095 
00096 KDatePicker::KDatePicker(QWidget *parent, QDate dt, const char *name, WFlags f)
00097   : QFrame(parent,name, f)
00098 {
00099   init( dt );
00100 }
00101 
00102 KDatePicker::KDatePicker( QWidget *parent, const char *name )
00103   : QFrame(parent,name)
00104 {
00105   init( QDate::currentDate() );
00106 }
00107 
00108 void KDatePicker::init( const QDate &dt )
00109 {
00110   d = new KDatePickerPrivate();
00111 
00112   d->tb = new KToolBar(this);
00113 
00114   yearBackward = new QToolButton(d->tb);
00115   monthBackward = new QToolButton(d->tb);
00116   selectMonth = new QToolButton(d->tb);
00117   selectYear = new QToolButton(d->tb);
00118   monthForward = new QToolButton(d->tb);
00119   yearForward = new QToolButton(d->tb);
00120   line = new KLineEdit(this);
00121   val = new KDateValidator(this);
00122   table = new KDateTable(this);
00123   fontsize = KGlobalSettings::generalFont().pointSize();
00124   if (fontsize == -1)
00125      fontsize = QFontInfo(KGlobalSettings::generalFont()).pointSize();
00126 
00127   fontsize++; // Make a little bigger
00128 
00129   d->selectWeek = new QComboBox(false, this);  // read only week selection
00130   d->todayButton = new QToolButton(this);
00131   d->todayButton->setIconSet(SmallIconSet("today"));
00132 
00133   QToolTip::add(yearForward, i18n("Next year"));
00134   QToolTip::add(yearBackward, i18n("Previous year"));
00135   QToolTip::add(monthForward, i18n("Next month"));
00136   QToolTip::add(monthBackward, i18n("Previous month"));
00137   QToolTip::add(d->selectWeek, i18n("Select a week"));
00138   QToolTip::add(selectMonth, i18n("Select a month"));
00139   QToolTip::add(selectYear, i18n("Select a year"));
00140   QToolTip::add(d->todayButton, i18n("Select the current day"));
00141 
00142   // -----
00143   setFontSize(fontsize);
00144   line->setValidator(val);
00145   line->installEventFilter( this );
00146   if (  QApplication::reverseLayout() )
00147   {
00148       yearForward->setIconSet(BarIconSet(QString::fromLatin1("2leftarrow")));
00149       yearBackward->setIconSet(BarIconSet(QString::fromLatin1("2rightarrow")));
00150       monthForward->setIconSet(BarIconSet(QString::fromLatin1("1leftarrow")));
00151       monthBackward->setIconSet(BarIconSet(QString::fromLatin1("1rightarrow")));
00152   }
00153   else
00154   {
00155       yearForward->setIconSet(BarIconSet(QString::fromLatin1("2rightarrow")));
00156       yearBackward->setIconSet(BarIconSet(QString::fromLatin1("2leftarrow")));
00157       monthForward->setIconSet(BarIconSet(QString::fromLatin1("1rightarrow")));
00158       monthBackward->setIconSet(BarIconSet(QString::fromLatin1("1leftarrow")));
00159   }
00160   connect(table, SIGNAL(dateChanged(QDate)), SLOT(dateChangedSlot(QDate)));
00161   connect(table, SIGNAL(tableClicked()), SLOT(tableClickedSlot()));
00162   connect(monthForward, SIGNAL(clicked()), SLOT(monthForwardClicked()));
00163   connect(monthBackward, SIGNAL(clicked()), SLOT(monthBackwardClicked()));
00164   connect(yearForward, SIGNAL(clicked()), SLOT(yearForwardClicked()));
00165   connect(yearBackward, SIGNAL(clicked()), SLOT(yearBackwardClicked()));
00166   connect(d->selectWeek, SIGNAL(activated(int)), SLOT(weekSelected(int)));
00167   connect(d->todayButton, SIGNAL(clicked()), SLOT(todayButtonClicked()));
00168   connect(selectMonth, SIGNAL(clicked()), SLOT(selectMonthClicked()));
00169   connect(selectYear, SIGNAL(clicked()), SLOT(selectYearClicked()));
00170   connect(line, SIGNAL(returnPressed()), SLOT(lineEnterPressed()));
00171   table->setFocus();
00172 
00173   QBoxLayout * topLayout = new QVBoxLayout(this);
00174 
00175   d->navigationLayout = new QHBoxLayout(topLayout);
00176   d->navigationLayout->addWidget(d->tb);
00177 
00178   topLayout->addWidget(table);
00179 
00180   QBoxLayout * bottomLayout = new QHBoxLayout(topLayout);
00181   bottomLayout->addWidget(d->todayButton);
00182   bottomLayout->addWidget(line);
00183   bottomLayout->addWidget(d->selectWeek);
00184 
00185   table->setDate(dt);
00186   dateChangedSlot(dt);  // needed because table emits changed only when newDate != oldDate
00187 }
00188 
00189 KDatePicker::~KDatePicker()
00190 {
00191   delete d;
00192 }
00193 
00194 bool
00195 KDatePicker::eventFilter(QObject *o, QEvent *e )
00196 {
00197    if ( e->type() == QEvent::KeyPress ) {
00198       QKeyEvent *k = (QKeyEvent *)e;
00199 
00200       if ( (k->key() == Qt::Key_Prior) ||
00201            (k->key() == Qt::Key_Next)  ||
00202            (k->key() == Qt::Key_Up)    ||
00203            (k->key() == Qt::Key_Down) )
00204        {
00205           QApplication::sendEvent( table, e );
00206           table->setFocus();
00207           return true; // eat event
00208        }
00209    }
00210    return QFrame::eventFilter( o, e );
00211 }
00212 
00213 void
00214 KDatePicker::resizeEvent(QResizeEvent* e)
00215 {
00216   QWidget::resizeEvent(e);
00217 }
00218 
00219 void
00220 KDatePicker::dateChangedSlot(QDate date)
00221 {
00222     kdDebug(298) << "KDatePicker::dateChangedSlot: date changed (" << date.year() << "/" << date.month() << "/" << date.day() << ")." << endl;
00223 
00224     const KCalendarSystem * calendar = KGlobal::locale()->calendar();
00225 
00226     line->setText(KGlobal::locale()->formatDate(date, true));
00227     selectMonth->setText(calendar->monthName(date, false));
00228     fillWeeksCombo(date);
00229 
00230     // calculate the item num in the week combo box; normalize selected day so as if 1.1. is the first day of the week
00231     QDate firstDay(date.year(), 1, 1);
00232     d->selectWeek->setCurrentItem((calendar->dayOfYear(date) + calendar->dayOfWeek(firstDay) - 2) / 7/*calendar->daysInWeek()*/);
00233 
00234     selectYear->setText(calendar->yearString(date, false));
00235 
00236     emit(dateChanged(date));
00237 }
00238 
00239 void
00240 KDatePicker::tableClickedSlot()
00241 {
00242   kdDebug(298) << "KDatePicker::tableClickedSlot: table clicked." << endl;
00243   emit(dateSelected(table->getDate()));
00244   emit(tableClicked());
00245 }
00246 
00247 const QDate&
00248 KDatePicker::getDate() const
00249 {
00250   return table->getDate();
00251 }
00252 
00253 const QDate &
00254 KDatePicker::date() const
00255 {
00256     return table->getDate();
00257 }
00258 
00259 bool
00260 KDatePicker::setDate(const QDate& date)
00261 {
00262     if(date.isValid())
00263     {
00264         table->setDate(date);  // this also emits dateChanged() which then calls our dateChangedSlot()
00265         return true;
00266     }
00267     else
00268     {
00269         kdDebug(298) << "KDatePicker::setDate: refusing to set invalid date." << endl;
00270         return false;
00271     }
00272 }
00273 
00274 void
00275 KDatePicker::monthForwardClicked()
00276 {
00277     QDate temp;
00278     temp = KGlobal::locale()->calendar()->addMonths( table->getDate(), 1 );
00279 
00280     setDate( temp );
00281 }
00282 
00283 void
00284 KDatePicker::monthBackwardClicked()
00285 {
00286     QDate temp;
00287     temp = KGlobal::locale()->calendar()->addMonths( table->getDate(), -1 );
00288 
00289     setDate( temp );
00290 }
00291 
00292 void
00293 KDatePicker::yearForwardClicked()
00294 {
00295     QDate temp;
00296     temp = KGlobal::locale()->calendar()->addYears( table->getDate(), 1 );
00297 
00298     setDate( temp );
00299 }
00300 
00301 void
00302 KDatePicker::yearBackwardClicked()
00303 {
00304     QDate temp;
00305     temp = KGlobal::locale()->calendar()->addYears( table->getDate(), -1 );
00306 
00307     setDate( temp );
00308 }
00309 
00310 void KDatePicker::selectWeekClicked() {}  // ### in 3.2 obsolete; kept for binary compatibility
00311 
00312 void
00313 KDatePicker::weekSelected(int week)
00314 {
00315   const KCalendarSystem * calendar = KGlobal::locale()->calendar();
00316 
00317   QDate date = table->getDate();
00318   int year = calendar->year(date);
00319 
00320   calendar->setYMD(date, year, 1, 1);  // first day of selected year
00321 
00322   // calculate the first day in the selected week (day 1 is first day of week)
00323   date = calendar->addDays(date, week * 7/*calendar->daysOfWeek()*/ -calendar->dayOfWeek(date) + 1);
00324 
00325   setDate(date);
00326 }
00327 
00328 void
00329 KDatePicker::selectMonthClicked()
00330 {
00331   // every year can have different month names (in some calendar systems)
00332   const KCalendarSystem * calendar = KGlobal::locale()->calendar();
00333   QDate date = table->getDate();
00334   int i, month, months = calendar->monthsInYear(date);
00335 
00336   QPopupMenu popup(selectMonth);
00337 
00338   for (i = 1; i <= months; i++)
00339     popup.insertItem(calendar->monthName(i, calendar->year(date)), i);
00340 
00341   popup.setActiveItem(calendar->month(date) - 1);
00342 
00343   if ( (month = popup.exec(selectMonth->mapToGlobal(QPoint(0, 0)), calendar->month(date) - 1)) == -1 ) return;  // canceled
00344 
00345   int day = calendar->day(date);
00346   // ----- construct a valid date in this month:
00347   //date.setYMD(date.year(), month, 1);
00348   //date.setYMD(date.year(), month, QMIN(day, date.daysInMonth()));
00349   calendar->setYMD(date, calendar->year(date), month,
00350                    QMIN(day, calendar->daysInMonth(date)));
00351   // ----- set this month
00352   setDate(date);
00353 }
00354 
00355 void
00356 KDatePicker::selectYearClicked()
00357 {
00358   const KCalendarSystem * calendar = KGlobal::locale()->calendar();
00359 
00360   int year;
00361   KPopupFrame* popup = new KPopupFrame(this);
00362   KDateInternalYearSelector* picker = new KDateInternalYearSelector(popup);
00363   // -----
00364   picker->resize(picker->sizeHint());
00365   popup->setMainWidget(picker);
00366   connect(picker, SIGNAL(closeMe(int)), popup, SLOT(close(int)));
00367   picker->setFocus();
00368   if(popup->exec(selectYear->mapToGlobal(QPoint(0, selectMonth->height()))))
00369     {
00370       QDate date;
00371       int day;
00372       // -----
00373       year=picker->getYear();
00374       date=table->getDate();
00375       day=calendar->day(date);
00376       // ----- construct a valid date in this month:
00377       //date.setYMD(year, date.month(), 1);
00378       //date.setYMD(year, date.month(), QMIN(day, date.daysInMonth()));
00379       calendar->setYMD(date, year, calendar->month(date),
00380                        QMIN(day, calendar->daysInMonth(date)));
00381       // ----- set this month
00382       setDate(date);
00383     } else {
00384       KNotifyClient::beep();
00385     }
00386   delete popup;
00387 }
00388 
00389 void
00390 KDatePicker::setEnabled(bool enable)
00391 {
00392   QWidget *widgets[]= {
00393     yearForward, yearBackward, monthForward, monthBackward,
00394     selectMonth, selectYear,
00395     line, table, d->selectWeek, d->todayButton };
00396   const int Size=sizeof(widgets)/sizeof(widgets[0]);
00397   int count;
00398   // -----
00399   for(count=0; count<Size; ++count)
00400     {
00401       widgets[count]->setEnabled(enable);
00402     }
00403 }
00404 
00405 void
00406 KDatePicker::lineEnterPressed()
00407 {
00408   QDate temp;
00409   // -----
00410   if(val->date(line->text(), temp)==QValidator::Acceptable)
00411     {
00412         kdDebug(298) << "KDatePicker::lineEnterPressed: valid date entered." << endl;
00413         emit(dateEntered(temp));
00414         setDate(temp);
00415     } else {
00416       KNotifyClient::beep();
00417       kdDebug(298) << "KDatePicker::lineEnterPressed: invalid date entered." << endl;
00418     }
00419 }
00420 
00421 void
00422 KDatePicker::todayButtonClicked()
00423 {
00424   setDate(QDate::currentDate());
00425 }
00426 
00427 QSize
00428 KDatePicker::sizeHint() const
00429 {
00430   return QWidget::sizeHint();
00431 }
00432 
00433 void
00434 KDatePicker::setFontSize(int s)
00435 {
00436   QWidget *buttons[]= {
00437     // yearBackward,
00438     // monthBackward,
00439     selectMonth,
00440     selectYear,
00441     // monthForward,
00442     // yearForward
00443   };
00444   const int NoOfButtons=sizeof(buttons)/sizeof(buttons[0]);
00445   int count;
00446   QFont font;
00447   QRect r;
00448   // -----
00449   fontsize=s;
00450   for(count=0; count<NoOfButtons; ++count)
00451     {
00452       font=buttons[count]->font();
00453       font.setPointSize(s);
00454       buttons[count]->setFont(font);
00455     }
00456   QFontMetrics metrics(selectMonth->fontMetrics());
00457 
00458   for (int i = 1; ; ++i)
00459     {
00460       QString str = KGlobal::locale()->calendar()->monthName(i,
00461          KGlobal::locale()->calendar()->year(table->getDate()), false);
00462       if (str.isNull()) break;
00463       r=metrics.boundingRect(str);
00464       maxMonthRect.setWidth(QMAX(r.width(), maxMonthRect.width()));
00465       maxMonthRect.setHeight(QMAX(r.height(),  maxMonthRect.height()));
00466     }
00467 
00468   QSize metricBound = style().sizeFromContents(QStyle::CT_ToolButton,
00469                                                selectMonth,
00470                                                maxMonthRect);
00471   selectMonth->setMinimumSize(metricBound);
00472 
00473   table->setFontSize(s);
00474 }
00475 
00476 void
00477 KDatePicker::setCloseButton( bool enable )
00478 {
00479     if ( enable == (d->closeButton != 0L) )
00480         return;
00481 
00482     if ( enable ) {
00483         d->closeButton = new QToolButton( d->tb );
00484         QToolTip::add(d->closeButton, i18n("Close"));
00485         d->closeButton->setPixmap( SmallIcon("remove") );
00486         connect( d->closeButton, SIGNAL( clicked() ),
00487                  topLevelWidget(), SLOT( close() ) );
00488     }
00489     else {
00490         delete d->closeButton;
00491         d->closeButton = 0L;
00492     }
00493 
00494     updateGeometry();
00495 }
00496 
00497 bool KDatePicker::hasCloseButton() const
00498 {
00499     return (d->closeButton != 0L);
00500 }
00501 
00502 void KDatePicker::virtual_hook( int /*id*/, void* /*data*/ )
00503 { /*BASE::virtual_hook( id, data );*/ }
00504 
KDE Logo
This file is part of the documentation for kdeui Library Version 3.2.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Feb 4 12:34:15 2004 by doxygen 1.2.18 written by Dimitri van Heesch, © 1997-2003