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 "kdatepicker.h"
00029 #include "kdialog.h"
00030 
00031 #include "kdatewidget.h"
00032 
00033 class KDateWidgetSpinBox : public QSpinBox
00034 {
00035 public:
00036   KDateWidgetSpinBox(int min, int max, QWidget *parent)
00037     : QSpinBox(min, max, 1, parent)
00038   {
00039      editor()->setAlignment(AlignRight);
00040   }
00041 };
00042 
00043 class KDateWidget::KDateWidgetPrivate
00044 {
00045 public:
00046    KDateWidgetSpinBox *m_day;
00047    QComboBox *m_month;
00048    KDateWidgetSpinBox *m_year;
00049    QDate m_dat;
00050 };
00051 
00052 
00053 KDateWidget::KDateWidget( QWidget *parent, const char *name )
00054   : QWidget( parent, name )
00055 {
00056   init();
00057   setDate(QDate());
00058 }
00059 
00060 KDateWidget::KDateWidget( QDate date, QWidget *parent,
00061                 const char *name )
00062   : QWidget( parent, name )
00063 {
00064   init();
00065   setDate(date);
00066 }
00067 
00068 void KDateWidget::init()
00069 {
00070   d = new KDateWidgetPrivate;
00071   KLocale *locale = KGlobal::locale();
00072   QHBoxLayout *layout = new QHBoxLayout(this, 0, KDialog::spacingHint());
00073   layout->setAutoAdd(true);
00074   d->m_day = new KDateWidgetSpinBox(1, 31, this);
00075   d->m_month = new QComboBox(false, this);
00076   for(int i = 1; i <= 12; i++)
00077     d->m_month->insertItem(locale->monthName(i));
00078 
00079   d->m_year = new KDateWidgetSpinBox(1970, 2038, this);
00080 
00081   connect(d->m_day, SIGNAL(valueChanged(int)), this, SLOT(slotDateChanged()));
00082   connect(d->m_month, SIGNAL(activated(int)), this, SLOT(slotDateChanged()));
00083   connect(d->m_year, SIGNAL(valueChanged(int)), this, SLOT(slotDateChanged()));
00084 }
00085 
00086 KDateWidget::~KDateWidget()
00087 {
00088 }
00089 
00090 void KDateWidget::setDate( QDate date )
00091 {
00092   d->m_day->blockSignals(true);
00093   d->m_month->blockSignals(true);
00094   d->m_year->blockSignals(true);
00095 
00096   d->m_day->setMaxValue(date.daysInMonth());
00097   d->m_day->setValue(date.day());
00098   d->m_month->setCurrentItem(date.month()-1);
00099   d->m_year->setValue(date.year());
00100 
00101   d->m_day->blockSignals(false);
00102   d->m_month->blockSignals(false);
00103   d->m_year->blockSignals(false);
00104 
00105   d->m_dat = date;
00106   emit changed(d->m_dat);
00107 }
00108 
00109 QDate KDateWidget::date() const
00110 {
00111   return d->m_dat;
00112 }
00113 
00114 void KDateWidget::slotDateChanged( )
00115 {
00116   QDate date;
00117   int y,m,day;
00118   y = d->m_year->value();
00119   y = QMIN(QMAX(y, 1970), 2038);
00120   m = d->m_month->currentItem()+1;
00121   m = QMIN(QMAX(m,1), 12);
00122   date.setYMD(y,m,1);
00123   day = d->m_day->value();
00124   day = QMIN(QMAX(day,1), date.daysInMonth());
00125   date.setYMD(y,m,day);
00126   setDate(date);
00127 }
00128 
00129 void KDateWidget::virtual_hook( int, void* )
00130 { /*BASE::virtual_hook( id, data );*/ }
00131 
00132 #include "kdatewidget.moc"
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.0.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Oct 8 12:20:58 2003 by doxygen 1.2.18 written by Dimitri van Heesch, © 1997-2001