Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

kdatepicker.cpp

Go to the documentation of this file.
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 "kdatepicker.h"
00022 #include <kglobal.h>
00023 #include <kapplication.h>
00024 #include <klocale.h>
00025 #include <kiconloader.h>
00026 #include <qframe.h>
00027 #include <qpainter.h>
00028 #include <qdialog.h>
00029 #include <qtoolbutton.h>
00030 #include <qfont.h>
00031 #include <qlineedit.h>
00032 #include <qvalidator.h>
00033 #include <kdebug.h>
00034 #include <knotifyclient.h>
00035 #include "kdatetbl.h"
00036 #include "kdatepicker.moc"
00037 
00038 
00039 KDatePicker::KDatePicker(QWidget *parent, QDate dt, const char *name)
00040   : QFrame(parent,name),
00041     yearForward(new QToolButton(this)),
00042     yearBackward(new QToolButton(this)),
00043     monthForward(new QToolButton(this)),
00044     monthBackward(new QToolButton(this)),
00045     selectMonth(new QToolButton(this)),
00046     selectYear(new QToolButton(this)),
00047     line(new QLineEdit(this)),
00048     val(new KDateValidator(this)),
00049     table(new KDateTable(this)),
00050     fontsize(10)
00051 {
00052   // -----
00053   setFontSize(10);
00054   line->setValidator(val);
00055   yearForward->setPixmap(BarIcon(QString::fromLatin1("2rightarrow")));
00056   yearBackward->setPixmap(BarIcon(QString::fromLatin1("2leftarrow")));
00057   monthForward->setPixmap(BarIcon(QString::fromLatin1("1rightarrow")));
00058   monthBackward->setPixmap(BarIcon(QString::fromLatin1("1leftarrow")));
00059   setDate(dt); // set button texts
00060   connect(table, SIGNAL(dateChanged(QDate)), SLOT(dateChangedSlot(QDate)));
00061   connect(table, SIGNAL(tableClicked()), SLOT(tableClickedSlot()));
00062   connect(monthForward, SIGNAL(clicked()), SLOT(monthForwardClicked()));
00063   connect(monthBackward, SIGNAL(clicked()), SLOT(monthBackwardClicked()));
00064   connect(yearForward, SIGNAL(clicked()), SLOT(yearForwardClicked()));
00065   connect(yearBackward, SIGNAL(clicked()), SLOT(yearBackwardClicked()));
00066   connect(selectMonth, SIGNAL(clicked()), SLOT(selectMonthClicked()));
00067   connect(selectYear, SIGNAL(clicked()), SLOT(selectYearClicked()));
00068   connect(line, SIGNAL(returnPressed()), SLOT(lineEnterPressed()));
00069 }
00070 
00071 KDatePicker::~KDatePicker()
00072 {
00073 }
00074 
00075 void
00076 KDatePicker::resizeEvent(QResizeEvent*)
00077 {
00078     QWidget *buttons[] = {
00079         yearBackward,
00080             monthBackward,
00081             selectMonth,
00082             selectYear,
00083             monthForward,
00084             yearForward };
00085     const int NoOfButtons=sizeof(buttons)/sizeof(buttons[0]);
00086     QSize sizes[NoOfButtons];
00087     int buttonHeight=0;
00088     int count;
00089     int w;
00090     int x=0;
00091     // ----- calculate button row height:
00092     for(count=0; count<NoOfButtons; ++count) {
00093         sizes[count]=buttons[count]->sizeHint();
00094         buttonHeight=QMAX(buttonHeight, sizes[count].height());
00095     }
00096     // ----- calculate size of the month button:
00097     w=0;
00098     for(count=0; count<NoOfButtons; ++count) {
00099         if(buttons[count]!=selectMonth)
00100         {
00101             w+=sizes[count].width();
00102         } else {
00103             x=count;
00104         }
00105     }
00106     sizes[x].setWidth(width()-w); // stretch the month button
00107     // ----- place the buttons:
00108     x=0;
00109     for(count=0; count<NoOfButtons; ++count)
00110     {
00111         w=sizes[count].width();
00112         buttons[count]->setGeometry(x, 0, w, buttonHeight);
00113         x+=w;
00114     }
00115     // ----- place the line edit for direct input:
00116     sizes[0]=line->sizeHint();
00117     line->setGeometry(0, height()-sizes[0].height(), width(), sizes[0].height());
00118     // ----- adjust the table:
00119     table->setGeometry(0, buttonHeight, width(),
00120                        height()-buttonHeight-sizes[0].height());
00121 }
00122 
00123 void
00124 KDatePicker::dateChangedSlot(QDate date)
00125 {
00126     kdDebug() << "KDatePicker::dateChangedSlot: date changed (" << date.year() << "/" << date.month() << "/" << date.day() << ")." << endl;
00127     line->setText(KGlobal::locale()->formatDate(date, true));
00128     emit(dateChanged(date));
00129 }
00130 
00131 void
00132 KDatePicker::tableClickedSlot()
00133 {
00134   kdDebug() << "KDatePicker::tableClickedSlot: table clicked." << endl;
00135   emit(dateSelected(table->getDate()));
00136   emit(tableClicked());
00137 }
00138 
00139 const QDate&
00140 KDatePicker::getDate() const
00141 {
00142   return table->getDate();
00143 }
00144 
00145 const QDate &
00146 KDatePicker::date() const
00147 {
00148     return table->getDate();
00149 }
00150 
00151 bool
00152 KDatePicker::setDate(const QDate& date)
00153 {
00154     if(date.isValid()) {
00155         QString temp;
00156         // -----
00157         table->setDate(date);
00158         selectMonth->setText(KGlobal::locale()->monthName(date.month(), false));
00159         temp.setNum(date.year());
00160         selectYear->setText(temp);
00161         line->setText(KGlobal::locale()->formatDate(date, true));
00162         return true;
00163     } else {
00164         kdDebug() << "KDatePicker::setDate: refusing to set invalid date." << endl;
00165         return false;
00166     }
00167 }
00168 
00169 void
00170 KDatePicker::monthForwardClicked()
00171 {
00172     QDate temp=table->getDate();
00173     int day=temp.day();
00174     // -----
00175     if(temp.month()==12) {
00176         temp.setYMD(temp.year()+1, 1, 1);
00177     } else {
00178         temp.setYMD(temp.year(), temp.month()+1, 1);
00179     }
00180     if(temp.daysInMonth()<day) {
00181         temp.setYMD(temp.year(), temp.month(), temp.daysInMonth());
00182     } else {
00183         temp.setYMD(temp.year(), temp.month(), day);
00184     }
00185     // assert(temp.isValid());
00186     setDate(temp);
00187 }
00188 
00189 void
00190 KDatePicker::monthBackwardClicked()
00191 {
00192   QDate temp=table->getDate();
00193   int day=temp.day();
00194   // -----
00195   if(temp.month()==1)
00196     {
00197       temp.setYMD(temp.year()-1, 12, 1);
00198     } else {
00199       temp.setYMD(temp.year(), temp.month()-1, 1);
00200     }
00201   if(temp.daysInMonth()<day)
00202     {
00203       temp.setYMD(temp.year(), temp.month(), temp.daysInMonth());
00204     } else {
00205       temp.setYMD(temp.year(), temp.month(), day);
00206     }
00207   // assert(temp.isValid());
00208   setDate(temp);
00209 }
00210 
00211 void
00212 KDatePicker::yearForwardClicked()
00213 {
00214   QDate temp=table->getDate();
00215   int day=temp.day();
00216   // -----
00217   temp.setYMD(temp.year()+1, temp.month(), 1);
00218   if(temp.daysInMonth()<day)
00219     {
00220       temp.setYMD(temp.year(), temp.month(), temp.daysInMonth());
00221     } else {
00222       temp.setYMD(temp.year(), temp.month(), day);
00223     }
00224   // assert(temp.isValid());
00225   setDate(temp);
00226 }
00227 
00228 void
00229 KDatePicker::yearBackwardClicked()
00230 {
00231   QDate temp=table->getDate();
00232   int day=temp.day();
00233   // -----
00234   temp.setYMD(temp.year()-1, temp.month(), 1);
00235   if(temp.daysInMonth()<day)
00236     {
00237       temp.setYMD(temp.year(), temp.month(), temp.daysInMonth());
00238     } else {
00239       temp.setYMD(temp.year(), temp.month(), day);
00240     }
00241   // assert(temp.isValid());
00242   setDate(temp);
00243 }
00244 
00245 void
00246 KDatePicker::selectMonthClicked()
00247 {
00248   int month;
00249   KPopupFrame* popup = new KPopupFrame(this);
00250   KDateInternalMonthPicker* picker = new KDateInternalMonthPicker(fontsize, popup);
00251   // -----
00252   picker->resize(picker->sizeHint());
00253   popup->setMainWidget(picker);
00254   picker->setFocus();
00255   connect(picker, SIGNAL(closeMe(int)), popup, SLOT(close(int)));
00256   if(popup->exec(selectMonth->mapToGlobal(QPoint(0, selectMonth->height()))))
00257     {
00258       QDate date;
00259       int day;
00260       // -----
00261       month=picker->getResult();
00262       date=table->getDate();
00263       day=date.day();
00264       // ----- construct a valid date in this month:
00265       date.setYMD(date.year(), month, 1);
00266       date.setYMD(date.year(), month, QMIN(day, date.daysInMonth()));
00267       // ----- set this month
00268       setDate(date);
00269     } else {
00270       KNotifyClient::beep();
00271     }
00272   delete popup;
00273 }
00274 
00275 void
00276 KDatePicker::selectYearClicked()
00277 {
00278   int year;
00279   KPopupFrame* popup = new KPopupFrame(this);
00280   KDateInternalYearSelector* picker = new KDateInternalYearSelector(fontsize, popup);
00281   // -----
00282   picker->resize(picker->sizeHint());
00283   popup->setMainWidget(picker);
00284   connect(picker, SIGNAL(closeMe(int)), popup, SLOT(close(int)));
00285   picker->setFocus();
00286   if(popup->exec(selectYear->mapToGlobal(QPoint(0, selectMonth->height()))))
00287     {
00288       QDate date;
00289       int day;
00290       // -----
00291       year=picker->getYear();
00292       date=table->getDate();
00293       day=date.day();
00294       // ----- construct a valid date in this month:
00295       date.setYMD(year, date.month(), 1);
00296       date.setYMD(year, date.month(), QMIN(day, date.daysInMonth()));
00297       // ----- set this month
00298       setDate(date);
00299     } else {
00300       KNotifyClient::beep();
00301     }
00302   delete popup;
00303 }
00304 
00305 void
00306 KDatePicker::setEnabled(bool enable)
00307 {
00308   QWidget *widgets[]= {
00309     yearForward, yearBackward, monthForward, monthBackward,
00310     selectMonth, selectYear,
00311     line, table };
00312   const int Size=sizeof(widgets)/sizeof(widgets[0]);
00313   int count;
00314   // -----
00315   for(count=0; count<Size; ++count)
00316     {
00317       widgets[count]->setEnabled(enable);
00318     }
00319 }
00320 
00321 void
00322 KDatePicker::lineEnterPressed()
00323 {
00324   QDate temp;
00325   // -----
00326   if(val->date(line->text(), temp)==QValidator::Acceptable)
00327     {
00328         kdDebug() << "KDatePicker::lineEnterPressed: valid date entered." << endl;
00329         emit(dateEntered(temp));
00330         setDate(temp);
00331     } else {
00332       KNotifyClient::beep();
00333       kdDebug() << "KDatePicker::lineEnterPressed: invalid date entered." << endl;
00334     }
00335 }
00336 
00337 QSize
00338 KDatePicker::sizeHint() const
00339 {
00340   QSize tableSize=table->sizeHint();
00341   QWidget *buttons[]={
00342     yearBackward,
00343     monthBackward,
00344     selectMonth,
00345     selectYear,
00346     monthForward,
00347     yearForward };
00348   const int NoOfButtons=sizeof(buttons)/sizeof(buttons[0]);
00349   QSize sizes[NoOfButtons];
00350   int cx=0, cy=0, count;
00351   // ----- store the size hints:
00352   for(count=0; count<NoOfButtons; ++count)
00353     {
00354       sizes[count]=buttons[count]->sizeHint();
00355       if(buttons[count]==selectMonth)
00356         {
00357           cx+=maxMonthRect.width();
00358         } else {
00359           cx+=sizes[count].width();
00360         }
00361       cy=QMAX(sizes[count].height(), cy);
00362     }
00363   // ----- calculate width hint:
00364   cx=QMAX(cx, tableSize.width()); // line edit ignored
00365   // ----- calculate height hint:
00366   cy+=tableSize.height()+line->sizeHint().height();
00367   return QSize(cx, cy);
00368 }
00369 
00370 void
00371 KDatePicker::setFontSize(int s)
00372 {
00373   QWidget *buttons[]= {
00374     // yearBackward,
00375     // monthBackward,
00376     selectMonth,
00377     selectYear,
00378     // monthForward,
00379     // yearForward
00380   };
00381   const int NoOfButtons=sizeof(buttons)/sizeof(buttons[0]);
00382   int count;
00383   QFont font;
00384   QRect r;
00385   // -----
00386   fontsize=s;
00387   for(count=0; count<NoOfButtons; ++count)
00388     {
00389       font=buttons[count]->font();
00390       font.setPointSize(s);
00391       buttons[count]->setFont(font);
00392     }
00393   QFontMetrics metrics(selectMonth->fontMetrics());
00394   for(int i=1; i <= 12; ++i)
00395     { // maxMonthRect is used by sizeHint()
00396       r=metrics.boundingRect(KGlobal::locale()->monthName(i, false));
00397       maxMonthRect.setWidth(QMAX(r.width(), maxMonthRect.width()));
00398       maxMonthRect.setHeight(QMAX(r.height(),  maxMonthRect.height()));
00399     }
00400   table->setFontSize(s);
00401 }
00402 
00403 void KDatePicker::virtual_hook( int id, void* data )
00404 { /*BASE::virtual_hook( id, data );*/ }
00405 

Generated on Sat Nov 5 16:17:11 2005 for OPIE by  doxygen 1.4.2