kdatewidget.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
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
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 { }
00131
00132 #include "kdatewidget.moc"
This file is part of the documentation for kdelibs Version 3.1.0.