kdeui Library API Documentation

kdatewidget.cpp

00001 /*  This file is part of the KDE libraries
00002     Copyright (C) 2001 Waldo Bastian (bastian@kde.org)
00003 
00004     This library is free software; you can redistribute it and/or
00005     modify it under the terms of the GNU Library General Public
00006     License version 2 as published by the Free Software Foundation.
00007 
00008     This library is distributed in the hope that it will be useful,
00009     but WITHOUT ANY WARRANTY; without even the implied warranty of
00010     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011     Library General Public License for more details.
00012 
00013     You should have received a copy of the GNU Library General Public License
00014     along with this library; see the file COPYING.LIB.  If not, write to
00015     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00016     Boston, MA 02111-1307, USA.
00017 */
00018 
00019 
00020 #include <qpopupmenu.h>
00021 #include <qcombobox.h>
00022 #include <qlayout.h>
00023 #include <qlineedit.h>
00024 
00025 #include "knuminput.h"
00026 #include "kglobal.h"
00027 #include "klocale.h"
00028 #include "kcalendarsystem.h"
00029 //#include "kdatepicker.h"
00030 #include "kdialog.h"
00031 
00032 #include "kdatewidget.h"
00033 
00034 class KDateWidgetSpinBox : public QSpinBox
00035 {
00036 public:
00037   KDateWidgetSpinBox(int min, int max, QWidget *parent)
00038     : QSpinBox(min, max, 1, parent)
00039   {
00040      editor()->setAlignment(AlignRight);
00041   }
00042 };
00043 
00044 class KDateWidget::KDateWidgetPrivate
00045 {
00046 public:
00047    KDateWidgetSpinBox *m_day;
00048    QComboBox *m_month;
00049    KDateWidgetSpinBox *m_year;
00050    QDate m_dat;
00051 };
00052 
00053 
00054 KDateWidget::KDateWidget( QWidget *parent, const char *name )
00055   : QWidget( parent, name )
00056 {
00057   init(QDate());
00058   setDate(QDate());
00059 }
00060 
00061 // ### HPB change QDate to const QDate & in KDE 4.0
00062 KDateWidget::KDateWidget( QDate date, QWidget *parent,
00063                 const char *name )
00064   : QWidget( parent, name )
00065 {
00066   init(date);
00067   setDate(date);
00068 }
00069 
00070 // ### CFM Repaced by init(const QDate&). Can be safely removed
00071 //     when no risk of BIC
00072 void KDateWidget::init()
00073 {
00074   d = new KDateWidgetPrivate;
00075   KLocale *locale = KGlobal::locale();
00076   QHBoxLayout *layout = new QHBoxLayout(this, 0, KDialog::spacingHint());
00077   layout->setAutoAdd(true);
00078   d->m_day = new KDateWidgetSpinBox(1, 1, this);
00079   d->m_month = new QComboBox(false, this);
00080   for (int i = 1; ; ++i)
00081   {
00082     QString str = locale->calendar()->monthName(i,
00083        locale->calendar()->year(QDate()));
00084     if (str.isNull()) break;
00085     d->m_month->insertItem(str);
00086   }
00087 
00088   d->m_year = new KDateWidgetSpinBox(locale->calendar()->minValidYear(),
00089                      locale->calendar()->maxValidYear(), this);
00090 
00091   connect(d->m_day, SIGNAL(valueChanged(int)), this, SLOT(slotDateChanged()));
00092   connect(d->m_month, SIGNAL(activated(int)), this, SLOT(slotDateChanged()));
00093   connect(d->m_year, SIGNAL(valueChanged(int)), this, SLOT(slotDateChanged()));
00094 }
00095 
00096 void KDateWidget::init(const QDate& date)
00097 {
00098   d = new KDateWidgetPrivate;
00099   KLocale *locale = KGlobal::locale();
00100   QHBoxLayout *layout = new QHBoxLayout(this, 0, KDialog::spacingHint());
00101   layout->setAutoAdd(true);
00102   d->m_day = new KDateWidgetSpinBox(1, 1, this);
00103   d->m_month = new QComboBox(false, this);
00104   for (int i = 1; ; ++i)
00105   {
00106     QString str = locale->calendar()->monthName(i,
00107        locale->calendar()->year(date));
00108     if (str.isNull()) break;
00109     d->m_month->insertItem(str);
00110   }
00111 
00112   d->m_year = new KDateWidgetSpinBox(locale->calendar()->minValidYear(),
00113                      locale->calendar()->maxValidYear(), this);
00114 
00115   connect(d->m_day, SIGNAL(valueChanged(int)), this, SLOT(slotDateChanged()));
00116   connect(d->m_month, SIGNAL(activated(int)), this, SLOT(slotDateChanged()));
00117   connect(d->m_year, SIGNAL(valueChanged(int)), this, SLOT(slotDateChanged()));
00118 }
00119 
00120 KDateWidget::~KDateWidget()
00121 {
00122 }
00123 
00124 // ### HPB change QDate to const QDate & in KDE 4.0
00125 void KDateWidget::setDate( QDate date )
00126 {
00127   const KCalendarSystem * calendar = KGlobal::locale()->calendar();
00128 
00129   d->m_day->blockSignals(true);
00130   d->m_month->blockSignals(true);
00131   d->m_year->blockSignals(true);
00132 
00133   d->m_day->setMaxValue(calendar->daysInMonth(date));
00134   d->m_day->setValue(calendar->day(date));
00135   d->m_month->setCurrentItem(calendar->month(date)-1);
00136   d->m_year->setValue(calendar->year(date));
00137 
00138   d->m_day->blockSignals(false);
00139   d->m_month->blockSignals(false);
00140   d->m_year->blockSignals(false);
00141 
00142   d->m_dat = date;
00143   emit changed(d->m_dat);
00144 }
00145 
00146 QDate KDateWidget::date() const
00147 {
00148   return d->m_dat;
00149 }
00150 
00151 void KDateWidget::slotDateChanged( )
00152 {
00153   const KCalendarSystem * calendar = KGlobal::locale()->calendar();
00154 
00155   QDate date;
00156   int y,m,day;
00157 
00158   y = d->m_year->value();
00159   y = QMIN(QMAX(y, calendar->minValidYear()), calendar->maxValidYear());
00160 
00161   calendar->setYMD(date, y, 1, 1);
00162   m = d->m_month->currentItem()+1;
00163   m = QMIN(QMAX(m,1), calendar->monthsInYear(date));
00164 
00165   calendar->setYMD(date, y, m, 1);
00166   day = d->m_day->value();
00167   day = QMIN(QMAX(day,1), calendar->daysInMonth(date));
00168 
00169   calendar->setYMD(date, y, m, day);
00170   setDate(date);
00171 }
00172 
00173 void KDateWidget::virtual_hook( int, void* )
00174 { /*BASE::virtual_hook( id, data );*/ }
00175 
00176 #include "kdatewidget.moc"
KDE Logo
This file is part of the documentation for kdeui Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Apr 21 18:43:15 2004 by doxygen 1.3.6-20040222 written by Dimitri van Heesch, © 1997-2003