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() );
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
00092 for ( int i = 1; ; ++i ) {
00093 const QString str = calendar()->monthName( i, calendar()->year( initDate ) );
00094 if ( str.isEmpty() ) {
00095 break;
00096 }
00097 d->m_month->addItem( str );
00098 }
00099
00100 d->m_year = new KDateWidgetSpinBox( calendar()->year( calendar()->earliestValidDate() ),
00101 calendar()->year( calendar()->latestValidDate() ), this );
00102 layout->addWidget( d->m_day );
00103 layout->addWidget( d->m_month );
00104 layout->addWidget( d->m_year );
00105
00106 connect( d->m_day, SIGNAL( valueChanged( int ) ), this, SLOT( slotDateChanged() ) );
00107 connect( d->m_month, SIGNAL( activated( int ) ), this, SLOT( slotDateChanged() ) );
00108 connect( d->m_year, SIGNAL( valueChanged( int ) ), this, SLOT( slotDateChanged() ) );
00109 }
00110
00111 KDateWidget::~KDateWidget()
00112 {
00113 delete d;
00114 }
00115
00116 bool KDateWidget::setDate( const QDate &date )
00117 {
00118 if ( calendar()->isValid( date ) ) {
00119 bool dayBlocked = d->m_day->blockSignals( true );
00120 bool monthBlocked = d->m_month->blockSignals( true );
00121 bool yearBlocked = d->m_year->blockSignals( true );
00122
00123 d->m_day->setMaximum( calendar()->daysInMonth( date ) );
00124 d->m_day->setValue( calendar()->day( date ) );
00125 d->m_month->setCurrentIndex( calendar()->month( date ) - 1 );
00126 d->m_year->setValue( calendar()->year( date ) );
00127
00128 d->m_day->blockSignals( dayBlocked );
00129 d->m_month->blockSignals( monthBlocked );
00130 d->m_year->blockSignals( yearBlocked );
00131
00132 d->m_dat = date;
00133 emit changed( d->m_dat );
00134 return true;
00135 }
00136 return false;
00137 }
00138
00139 const QDate& KDateWidget::date() const
00140 {
00141 return d->m_dat;
00142 }
00143
00144 void KDateWidget::slotDateChanged( )
00145 {
00146 QDate date;
00147 int y, m, day;
00148
00149 y = d->m_year->value();
00150 y = qMin( qMax( y, calendar()->year( calendar()->earliestValidDate() ) ),
00151 calendar()->year( calendar()->latestValidDate() ) );
00152
00153 calendar()->setYMD( date, y, 1, 1 );
00154 m = d->m_month->currentIndex() + 1;
00155 m = qMin( qMax( m, 1 ), calendar()->monthsInYear( date ) );
00156
00157 calendar()->setYMD( date, y, m, 1 );
00158 day = d->m_day->value();
00159 day = qMin( qMax( day, 1 ), calendar()->daysInMonth( date ) );
00160
00161 calendar()->setYMD( date, y, m, day );
00162 setDate( date );
00163 }
00164
00165 const KCalendarSystem *KDateWidget::calendar() const
00166 {
00167 if ( d->m_cal ) {
00168 return d->m_cal;
00169 }
00170
00171 return KGlobal::locale()->calendar();
00172 }
00173
00174 bool KDateWidget::setCalendar( KCalendarSystem *calendar )
00175 {
00176 d->m_cal = calendar;
00177 return true;
00178 }
00179
00180 bool KDateWidget::setCalendar( const QString &calendarType )
00181 {
00182 d->m_cal = KCalendarSystem::create( calendarType );
00183 return d->m_cal;
00184 }
00185
00186
00187 #include "kdatewidget.moc"