calendarsystem Library API Documentation

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