# kdeui Library API Documentation

Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Namespaces | Source Files

# 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   delete d;
    00123 }
    00124
    00125 // ### HPB change QDate to const QDate & in KDE 4.0
    00126 void KDateWidget::setDate( QDate date )
    00127 {
    00128   const KCalendarSystem * calendar = KGlobal::locale()->calendar();
    00129
    00130   d->m_day->blockSignals(true);
    00131   d->m_month->blockSignals(true);
    00132   d->m_year->blockSignals(true);
    00133
    00134   d->m_day->setMaxValue(calendar->daysInMonth(date));
    00135   d->m_day->setValue(calendar->day(date));
    00136   d->m_month->setCurrentItem(calendar->month(date)-1);
    00137   d->m_year->setValue(calendar->year(date));
    00138
    00139   d->m_day->blockSignals(false);
    00140   d->m_month->blockSignals(false);
    00141   d->m_year->blockSignals(false);
    00142
    00143   d->m_dat = date;
    00144   emit changed(d->m_dat);
    00145 }
    00146
    00147 QDate KDateWidget::date() const
    00148 {
    00149   return d->m_dat;
    00150 }
    00151
    00152 void KDateWidget::slotDateChanged( )
    00153 {
    00154   const KCalendarSystem * calendar = KGlobal::locale()->calendar();
    00155
    00156   QDate date;
    00157   int y,m,day;
    00158
    00159   y = d->m_year->value();
    00160   y = QMIN(QMAX(y, calendar->minValidYear()), calendar->maxValidYear());
    00161
    00162   calendar->setYMD(date, y, 1, 1);
    00163   m = d->m_month->currentItem()+1;
    00164   m = QMIN(QMAX(m,1), calendar->monthsInYear(date));
    00165
    00166   calendar->setYMD(date, y, m, 1);
    00167   day = d->m_day->value();
    00168   day = QMIN(QMAX(day,1), calendar->daysInMonth(date));
    00169
    00170   calendar->setYMD(date, y, m, day);
    00171   setDate(date);
    00172 }
    00173
    00174 void KDateWidget::virtual_hook( int, void* )
    00175 { /*BASE::virtual_hook( id, data );*/ }
    00176
    00177 #include "kdatewidget.moc"

This file is part of the documentation for kdeui Library Version 3.4.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Jun 11 20:20:15 2005 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003