KDEUI
kdatewidget.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "kdatewidget.h"
00021
00022 #include <QtCore/QDate>
00023 #include <QtGui/QLayout>
00024 #include <QtGui/QLineEdit>
00025 #include <QtGui/QDoubleSpinBox>
00026
00027 #include <kcombobox.h>
00028
00029 #include "kcalendarsystem.h"
00030 #include "kdialog.h"
00031 #include "kglobal.h"
00032 #include "klocale.h"
00033
00034
00035 class KDateWidgetSpinBox : public QSpinBox
00036 {
00037 public:
00038 KDateWidgetSpinBox( int min, int max, QWidget *parent ) : QSpinBox( parent )
00039 {
00040 setRange( qMin( min, max ), qMax( min, max ) );
00041 setSingleStep( 1 );
00042 lineEdit()->setAlignment( Qt::AlignRight );
00043 }
00044 };
00045
00046 class KDateWidget::KDateWidgetPrivate
00047 {
00048 public:
00049 KDateWidgetSpinBox *m_day;
00050 KComboBox *m_month;
00051 KDateWidgetSpinBox *m_year;
00052 QDate m_dat;
00053 KCalendarSystem *m_cal;
00054 };
00055
00056
00057 KDateWidget::KDateWidget( QWidget *parent ) : QWidget( parent ), d( new KDateWidgetPrivate )
00058 {
00059 init( QDate() );
00060 setDate( QDate::currentDate() );
00061 }
00062
00063 KDateWidget::KDateWidget( const QDate &date, QWidget *parent )
00064 : QWidget( parent ), d( new KDateWidgetPrivate )
00065 {
00066 init( date );
00067 if ( ! setDate( date ) ) {
00068 setDate( QDate::currentDate() );
00069 }
00070 }
00071
00072 void KDateWidget::init( const QDate &date )
00073 {
00074
00075 setCalendar();
00076
00077 QDate initDate;
00078 if ( calendar()->isValid( date ) ) {
00079 initDate = date;
00080 } else {
00081 initDate = QDate::currentDate();
00082 }
00083
00084 QHBoxLayout *layout = new QHBoxLayout( this );
00085 layout->setMargin( 0 );
00086 layout->setSpacing( KDialog::spacingHint() );
00087
00088
00089 d->m_day = new KDateWidgetSpinBox( 1, 31, this );
00090 d->m_month = new KComboBox( this );
00091 d->m_month->setMaxVisibleItems( 12 );
00092
00093 for ( int i = 1; ; ++i ) {
00094 const QString str = calendar()->monthName( i, calendar()->year( initDate ) );
00095 if ( str.isEmpty() ) {
00096 break;
00097 }
00098 d->m_month->addItem( str );
00099 }
00100
00101 d->m_year = new KDateWidgetSpinBox( calendar()->year( calendar()->earliestValidDate() ),
00102 calendar()->year( calendar()->latestValidDate() ), this );
00103 layout->addWidget( d->m_day );
00104 layout->addWidget( d->m_month );
00105 layout->addWidget( d->m_year );
00106
00107 connect( d->m_day, SIGNAL( valueChanged( int ) ), this, SLOT( slotDateChanged() ) );
00108 connect( d->m_month, SIGNAL( activated( int ) ), this, SLOT( slotDateChanged() ) );
00109 connect( d->m_year, SIGNAL( valueChanged( int ) ), this, SLOT( slotDateChanged() ) );
00110
00111 d->m_dat = initDate;
00112 }
00113
00114 KDateWidget::~KDateWidget()
00115 {
00116 delete d;
00117 }
00118
00119 bool KDateWidget::setDate( const QDate &date )
00120 {
00121 if ( calendar()->isValid( date ) ) {
00122 bool dayBlocked = d->m_day->blockSignals( true );
00123 bool monthBlocked = d->m_month->blockSignals( true );
00124 bool yearBlocked = d->m_year->blockSignals( true );
00125
00126 d->m_day->setMaximum( calendar()->daysInMonth( date ) );
00127 d->m_day->setValue( calendar()->day( date ) );
00128 d->m_month->setCurrentIndex( calendar()->month( date ) - 1 );
00129 d->m_year->setValue( calendar()->year( date ) );
00130
00131 d->m_day->blockSignals( dayBlocked );
00132 d->m_month->blockSignals( monthBlocked );
00133 d->m_year->blockSignals( yearBlocked );
00134
00135 d->m_dat = date;
00136 emit changed( d->m_dat );
00137 return true;
00138 }
00139 return false;
00140 }
00141
00142 const QDate& KDateWidget::date() const
00143 {
00144 return d->m_dat;
00145 }
00146
00147 void KDateWidget::slotDateChanged( )
00148 {
00149 QDate date;
00150 int y, m, day;
00151
00152 y = d->m_year->value();
00153 y = qMin( qMax( y, calendar()->year( calendar()->earliestValidDate() ) ),
00154 calendar()->year( calendar()->latestValidDate() ) );
00155
00156 calendar()->setYMD( date, y, 1, 1 );
00157 m = d->m_month->currentIndex() + 1;
00158 m = qMin( qMax( m, 1 ), calendar()->monthsInYear( date ) );
00159
00160 calendar()->setYMD( date, y, m, 1 );
00161 day = d->m_day->value();
00162 day = qMin( qMax( day, 1 ), calendar()->daysInMonth( date ) );
00163
00164 calendar()->setYMD( date, y, m, day );
00165 setDate( date );
00166 }
00167
00168 const KCalendarSystem *KDateWidget::calendar() const
00169 {
00170 if ( d->m_cal ) {
00171 return d->m_cal;
00172 }
00173
00174 return KGlobal::locale()->calendar();
00175 }
00176
00177 bool KDateWidget::setCalendar( KCalendarSystem *calendar )
00178 {
00179 d->m_cal = calendar;
00180 return true;
00181 }
00182
00183 bool KDateWidget::setCalendar( const QString &calendarType )
00184 {
00185 d->m_cal = KCalendarSystem::create( calendarType );
00186 return d->m_cal;
00187 }
00188
00189
00190 #include "kdatewidget.moc"