23 #include "kdatepicker_p.h"
26 #include <QtGui/QApplication>
27 #include <QtGui/QFont>
28 #include <QtGui/QLayout>
30 #include <QtGui/QMenu>
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"
53 KDatePickerPrivateYearSelector::KDatePickerPrivateYearSelector(
55 :
QLineEdit( parent ), val( new QIntValidator( this ) ), result( 0 )
58 oldDate = currentDate;
70 connect(
this, SIGNAL(
returnPressed()), SLOT(yearEnteredSlot()) );
73 void KDatePickerPrivateYearSelector::yearEnteredSlot()
80 newYear = text().toInt( &ok );
87 if ( calendar->setDate( newDate, newYear, calendar->month( oldDate ), calendar->day( oldDate ) ) ) {
96 int KDatePickerPrivateYearSelector::year()
101 void KDatePickerPrivateYearSelector::setYear(
int year )
103 setText( QString::number( year ) );
110 q( q ),
closeButton( 0L ), selectWeek( 0L ), todayButton( 0 ), navigationLayout( 0 )
114 void fillWeeksCombo();
115 QDate validDateInYearMonth(
int year,
int month );
123 QBoxLayout *navigationLayout;
150 void KDatePicker::KDatePickerPrivate::fillWeeksCombo()
159 int thisYear = thisDate.
year();
172 int weekYear = thisYear;
173 day.
week( &weekYear );
177 if ( weekYear != thisYear ) {
183 QDate targetDate = day.
addDays( thisDate.dayOfWeek() - day.
dayOfWeek() ).date();
184 selectWeek->addItem( weekString, targetDate );
188 if ( day < lastDayOfYear &&
190 lastDayOfYear.
week() != day.
week() ) {
191 day = lastDayOfYear.
addDays( - thisDate.daysInWeek() );
196 QDate 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 ) )
225 void 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 ) {
487 newDate.
setDate( newDate.year(), newDate.month(), qMin( thisDate.
day(), newDate.daysInMonth() ) );
490 if ( !
setDate( newDate.date() ) ) {
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() ) ) ) ) {
518 newDate.
setDate( newDate.year(), newDate.month(), qMin( thisDate.
day(), newDate.daysInMonth() ) );
521 if ( !
setDate( newDate.date() ) ) {
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();
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() );
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 );
QString i18n(const char *text)
void dateChanged(const QDate &date)
This signal is emitted each time the selected date is changed.
void todayButtonClicked()
bool setDate(const QDate &date)
Sets the date.
virtual QString monthName(int month, int year, MonthNameFormat format=LongName) const =0
KLocalizedDate lastDayOfYear() const
bool setCalendar(KCalendarSystem *calendar=0)
Changes the calendar system to use.
virtual QDate earliestValidDate() const
friend class KDatePickerPrivate
const KCalendarSystem * calendar() const
Returns the currently selected calendar system.
QString i18nc(const char *ctxt, const char *text)
KLocalizedDate firstDayOfYear() const
void yearForwardClicked()
const KCalendarSystem * calendar() const
Returns a pointer to the Calendar System object used by this widget.
bool setCalendarSystem(KLocale::CalendarSystem calendarSystem)
QSize sizeHint() const
The size hint for date pickers.
int week(int *yearNum=0) const
KDatePicker(QWidget *parent=0)
The constructor.
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...
static int spacingHint()
Returns the number of pixels that should be used between widgets inside a dialog according to the KDE...
bool setDate(const QDate &date)
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...
A wrapper around QIcon that provides KDE icon features.
int fontSize() const
Returns the font size of the widget elements.
static void beep(const QString &reason=QString(), QWidget *widget=0L)
This is a simple substitution for QApplication::beep()
void monthBackwardClicked()
bool hasCloseButton() const
virtual void resizeEvent(QResizeEvent *)
the resize event
void returnPressed()
Emitted when the user presses the Enter key.
virtual ~KDatePicker()
The destructor.
const KCalendarSystem * calendar() const
void uncheckYearSelector()
const QDate & date() const
Validates user-entered dates.
QString formatDate(KLocale::DateFormat dateFormat=KLocale::LongDate) const
virtual bool isValid(int year, int month, int day) const =0
KGuiItem ok()
Returns the 'Ok' gui item.
virtual QDate latestValidDate() const
virtual int year(const QDate &date) const
void yearBackwardClicked()
void dateChangedSlot(const QDate &date)
An enhanced QLineEdit widget for inputting text.
void setCloseButton(bool enable)
By calling this method with enable = true, KDatePicker will show a little close-button in the upper b...
KLocalizedDate addDays(int days) const
virtual QDate readDate(const QString &str, bool *ok=0) const
static QFont generalFont()
Returns the default general font.
void selectMonthClicked()
QPixmap SmallIcon(const QString &name, int force_size, int state, const QStringList &overlays)
int daysDifference(const KLocalizedDate &toDate) const
KDateTable * dateTable() const
void setEnabled(bool enable)
Enables or disables the widget.
void setFontSize(int)
Sets the font size of the widgets elements.
void tableClicked()
This signal is emitted when the day has been selected by clicking on it in the table.
virtual bool setDate(QDate &date, int year, int month, int day) const
virtual bool eventFilter(QObject *o, QEvent *e)
to catch move keyEvents when QLineEdit has keyFocus
KAction * close(const QObject *recvr, const char *slot, QObject *parent)
Close the current document.
void monthForwardClicked()