23#include "kdatepicker_p.h"
26#include <QtGui/QApplication>
28#include <QtGui/QLayout>
31#include <QtGui/QPainter>
32#include <QtGui/QStyle>
33#include <QtGui/QToolButton>
34#include <QtGui/QDoubleValidator>
47#include "kdatepicker.moc"
48#include "kdatepicker_p.moc"
53KDatePickerPrivateYearSelector::KDatePickerPrivateYearSelector(
55 :
QLineEdit( parent ), val( new QIntValidator( this ) ), result( 0 )
58 oldDate = currentDate;
66 val->setRange( calendar->year( calendar->earliestValidDate() ),
67 calendar->year( calendar->latestValidDate() ) );
70 connect(
this, SIGNAL(returnPressed()), SLOT(yearEnteredSlot()) );
73void KDatePickerPrivateYearSelector::yearEnteredSlot()
80 newYear = text().toInt( &ok );
87 if ( calendar->setDate( newDate, newYear, calendar->month( oldDate ), calendar->day( oldDate ) ) ) {
96int KDatePickerPrivateYearSelector::year()
101void KDatePickerPrivateYearSelector::setYear(
int year )
103 setText( QString::number( year ) );
106class KDatePicker::KDatePickerPrivate
110 q( q ), closeButton( 0L ), selectWeek( 0L ), todayButton( 0 ), navigationLayout( 0 )
114 void fillWeeksCombo();
115 QDate validDateInYearMonth(
int year,
int month );
123 QBoxLayout *navigationLayout;
150void KDatePicker::KDatePickerPrivate::fillWeeksCombo()
159 int thisYear = thisDate.year();
172 int weekYear = thisYear;
173 day.
week( &weekYear );
177 if ( weekYear != thisYear ) {
184 selectWeek->addItem( weekString, targetDate );
188 if ( day < lastDayOfYear &&
190 lastDayOfYear.
week() != day.
week() ) {
191 day = lastDayOfYear.
addDays( - thisDate.daysInWeek() );
196QDate KDatePicker::KDatePickerPrivate::validDateInYearMonth(
int year,
int month )
202 if ( q->calendar()->isValid( year, month, 1 ) ) {
204 }
else if ( q->calendar()->isValid( year, month + 1, 1 ) ) {
205 q->calendar()->setDate( newDate, year, month, 1 );
206 q->calendar()->addDays( newDate, -1 );
208 newDate = QDate::fromJulianDay( 0 );
216 init( QDate::currentDate() );
220 :
QFrame( parent ), d( new KDatePickerPrivate( this ) )
225void KDatePicker::init(
const QDate &date_ )
227 QBoxLayout * topLayout =
new QVBoxLayout(
this );
228 topLayout->setSpacing( 0 );
229 topLayout->setMargin( 0 );
231 d->navigationLayout =
new QHBoxLayout();
232 d->navigationLayout->setSpacing( 0 );
233 d->navigationLayout->setMargin( 0 );
234 topLayout->addLayout( d->navigationLayout );
235 d->navigationLayout->addStretch();
237 d->yearBackward->setAutoRaise(
true );
238 d->navigationLayout->addWidget( d->yearBackward );
240 d->monthBackward ->setAutoRaise(
true );
241 d->navigationLayout->addWidget( d->monthBackward );
245 d->selectMonth ->setAutoRaise(
true );
246 d->navigationLayout->addWidget( d->selectMonth );
248 d->selectYear->setCheckable(
true );
249 d->selectYear->setAutoRaise(
true );
250 d->navigationLayout->addWidget( d->selectYear );
254 d->monthForward ->setAutoRaise(
true );
255 d->navigationLayout->addWidget( d->monthForward );
257 d->yearForward ->setAutoRaise(
true );
258 d->navigationLayout->addWidget( d->yearForward );
259 d->navigationLayout->addStretch();
264 setFocusProxy( d->table );
267 if ( d->fontsize == -1 ) {
274 d->selectWeek->setFocusPolicy( Qt::NoFocus );
276 d->todayButton->setIcon(
KIcon(
"go-jump-today" ) );
278 d->yearForward->setToolTip(
i18n(
"Next year" ) );
279 d->yearBackward->setToolTip(
i18n(
"Previous year" ) );
280 d->monthForward->setToolTip(
i18n(
"Next month" ) );
281 d->monthBackward->setToolTip(
i18n(
"Previous month" ) );
282 d->selectWeek->setToolTip(
i18n(
"Select a week" ) );
283 d->selectMonth->setToolTip(
i18n(
"Select a month" ) );
284 d->selectYear->setToolTip(
i18n(
"Select a year" ) );
285 d->todayButton->setToolTip(
i18n(
"Select the current day" ) );
289 d->line->setValidator( d->val );
290 d->line->installEventFilter(
this );
291 if ( QApplication::isRightToLeft() ) {
292 d->yearForward->setIcon(
KIcon( QLatin1String(
"arrow-left-double" ) ) );
293 d->yearBackward->setIcon(
KIcon( QLatin1String(
"arrow-right-double" ) ) );
294 d->monthForward->setIcon(
KIcon( QLatin1String(
"arrow-left" ) ) );
295 d->monthBackward->setIcon(
KIcon( QLatin1String(
"arrow-right" ) ) );
297 d->yearForward->setIcon(
KIcon( QLatin1String(
"arrow-right-double" ) ) );
298 d->yearBackward->setIcon(
KIcon( QLatin1String(
"arrow-left-double" ) ) );
299 d->monthForward->setIcon(
KIcon( QLatin1String(
"arrow-right" ) ) );
300 d->monthBackward->setIcon(
KIcon( QLatin1String(
"arrow-left" ) ) );
309 connect( d->selectWeek, SIGNAL(activated(
int)), SLOT(
weekSelected(
int)) );
316 topLayout->addWidget( d->table );
318 QBoxLayout * bottomLayout =
new QHBoxLayout();
319 bottomLayout->setMargin( 0 );
320 bottomLayout->setSpacing( 0 );
321 topLayout->addLayout( bottomLayout );
323 bottomLayout->addWidget( d->todayButton );
324 bottomLayout->addWidget( d->line );
325 bottomLayout->addWidget( d->selectWeek );
327 d->table->setDate( date_ );
338 if ( e->type() == QEvent::KeyPress ) {
339 QKeyEvent * k = ( QKeyEvent * )e;
341 if ( ( k->key() == Qt::Key_PageUp ) ||
342 ( k->key() == Qt::Key_PageDown ) ||
343 ( k->key() == Qt::Key_Up ) ||
344 ( k->key() == Qt::Key_Down ) ) {
345 QApplication::sendEvent( d->table, e );
346 d->table->setFocus();
350 return QFrame::eventFilter( o, e );
355 QWidget::resizeEvent( e );
386 return d->table->date();
393 return d->table->setDate( date_ );
398 return d->table->calendar();
403 return d->table->setCalendar( calendar );
408 return d->table->setCalendar( calendarType );
413 return d->table->setCalendarSystem( calendarSystem );
421 d->table->setFocus();
429 d->table->setFocus();
437 d->table->setFocus();
445 d->table->setFocus();
450 QDate targetDay = d->selectWeek->itemData( index ).toDateTime().date();
452 if ( !
setDate( targetDay ) ) {
455 d->table->setFocus();
461 d->table->setFocus();
463 QMenu popup( d->selectMonth );
467 popup.addAction(
calendar()->monthName( m, thisDate.
year() ) )->setData( m );
470 QAction *item = popup.actions()[ thisDate.
month() - 1 ];
473 popup.setActiveAction( item );
477 if ( ( item = popup.exec( d->selectMonth->mapToGlobal( QPoint( 0, 0 ) ), item ) ) == 0 ) {
497 if ( !d->selectYear->isChecked() )
503 KDatePickerPrivateYearSelector *picker =
new KDatePickerPrivateYearSelector(
calendar(),
date(), popup );
504 picker->resize( picker->sizeHint() );
505 picker->setYear( thisDate.
year() );
508 connect( picker, SIGNAL(closeMe(
int)), popup, SLOT(close(
int)) );
511 if( popup->
exec( d->selectYear->mapToGlobal( QPoint( 0, d->selectMonth->height() ) ) ) ) {
527 d->selectYear->setChecked(
false );
532 d->selectYear->setChecked(
false);
533 d->selectYear->update();
541 d->yearForward, d->yearBackward, d->monthForward, d->monthBackward,
542 d->selectMonth, d->selectYear,
543 d->line, d->table, d->selectWeek, d->todayButton
545 const int Size =
sizeof( widgets ) /
sizeof( widgets[0] );
548 for( count = 0; count < Size; ++count ) {
549 widgets[count]->setEnabled( enable );
551 d->table->setFocus();
563 if (
calendar()->isValid( newDate ) ) {
566 d->table->setFocus();
574 setDate( QDate::currentDate() );
575 d->table->setFocus();
580 return QWidget::sizeHint();
589 const int NoOfButtons =
sizeof( buttons ) /
sizeof( buttons[0] );
595 for( count = 0; count < NoOfButtons; ++count ) {
596 font = buttons[count]->font();
597 font.setPointSize( s );
598 buttons[count]->setFont( font );
600 d->table->setFontSize( s );
602 QFontMetrics metrics( d->selectMonth->fontMetrics() );
603 QString longestMonth;
605 for (
int i = 1; ; ++i ) {
607 if ( str.isNull() ) {
610 r = metrics.boundingRect( str );
612 if ( r.width() > d->maxMonthRect.width() ) {
613 d->maxMonthRect.setWidth( r.width() );
616 if ( r.height() > d->maxMonthRect.height() ) {
617 d->maxMonthRect.setHeight( r.height() );
621 QStyleOptionToolButton opt;
622 opt.initFrom( d->selectMonth );
623 opt.text = longestMonth;
626 QSize textSize = metrics.size( Qt::TextShowMnemonic, longestMonth );
627 textSize.setWidth( textSize.width() + metrics.width( QLatin1Char(
' ') ) * 2 );
628 int w = textSize.width();
629 int h = textSize.height();
630 opt.rect.setHeight( h );
632 QSize metricBound = style()->sizeFromContents(
633 QStyle::CT_ToolButton, &opt, QSize( w, h ), d->selectMonth
634 ).expandedTo( QApplication::globalStrut() );
636 d->selectMonth->setMinimumSize( metricBound );
647 if ( enable == ( d->closeButton != 0L ) ) {
653 d->closeButton->setAutoRaise(
true );
655 d->navigationLayout->addWidget( d->closeButton );
656 d->closeButton->setToolTip(
i18nc(
"@action:button",
"Close" ) );
657 d->closeButton->setIcon(
SmallIcon(
"window-close" ) );
658 connect( d->closeButton, SIGNAL(clicked()),
659 topLevelWidget(), SLOT(close()) );
661 delete d->closeButton;
670 return ( d->closeButton );
bool setDate(QDate &date, int year, int dayOfYear) const
QDate readDate(const QString &dateString, const QString &dateFormat, bool *ok, KLocale::DateTimeFormatStandard formatStandard) const
virtual QString monthName(const QDate &date, MonthNameFormat format=LongName) const
bool setDate(const QDate &date)
Sets the date.
QSize sizeHint() const
The size hint for date pickers.
void dateChangedSlot(const QDate &date)
void yearBackwardClicked()
bool setCalendar(KCalendarSystem *calendar=0)
Changes the calendar system to use.
void monthBackwardClicked()
void todayButtonClicked()
virtual ~KDatePicker()
The destructor.
void uncheckYearSelector()
KDatePicker(QWidget *parent=0)
The constructor.
void dateEntered(const QDate &date)
This signal is emitted when enter is pressed and a VALID date has been entered before into the line e...
bool hasCloseButton() const
void dateSelected(const QDate &date)
This signal is emitted each time a day has been selected by clicking on the table (hitting a day in t...
void setFontSize(int)
Sets the font size of the widgets elements.
void monthForwardClicked()
bool setCalendarSystem(KLocale::CalendarSystem calendarSystem)
void dateChanged(const QDate &date)
This signal is emitted each time the selected date is changed.
void yearForwardClicked()
void setCloseButton(bool enable)
By calling this method with enable = true, KDatePicker will show a little close-button in the upper b...
KDateTable * dateTable() const
void selectMonthClicked()
virtual bool eventFilter(QObject *o, QEvent *e)
to catch move keyEvents when QLineEdit has keyFocus
const KCalendarSystem * calendar() const
Returns the currently selected calendar system.
virtual void resizeEvent(QResizeEvent *)
the resize event
void setEnabled(bool enable)
Enables or disables the widget.
void tableClicked()
This signal is emitted when the day has been selected by clicking on it in the table.
Validates user-entered dates.
static int spacingHint()
Returns the number of pixels that should be used between widgets inside a dialog according to the KDE...
static QFont generalFont()
Returns the default general font.
A wrapper around QIcon that provides KDE icon features.
An enhanced QLineEdit widget for inputting text.
int week(int *yearNum=0) const
KLocalizedDate firstDayOfYear() const
KLocalizedDate lastDayOfYear() const
KLocalizedDate addDays(int days) const
const KCalendarSystem * calendar() const
bool setDate(const QDate &date)
int daysDifference(const KLocalizedDate &toDate) const
QString formatDate(const QString &formatString, KLocale::DateTimeFormatStandard formatStandard=KLocale::KdeFormat) const
static void beep(const QString &reason=QString(), QWidget *widget=0L)
This is a simple substitution for QApplication::beep()
QPixmap SmallIcon(const QString &name, int force_size, int state, const QStringList &overlays)
QString i18n(const char *text)
QString i18nc(const char *ctxt, const char *text)
KGuiItem ok()
Returns the 'Ok' gui item.