KDEUI
kdatepicker.cpp
Go to the documentation of this file.
00001 /* -*- C++ -*- 00002 This file is part of the KDE libraries 00003 Copyright (C) 1997 Tim D. Gilman (tdgilman@best.org) 00004 (C) 1998-2001 Mirko Boehm (mirko@kde.org) 00005 (C) 2007 John Layt <john@layt.net> 00006 This library is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU Library General Public 00008 License as published by the Free Software Foundation; either 00009 version 2 of the License, or (at your option) any later version. 00010 00011 This library is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 Library General Public License for more details. 00015 00016 You should have received a copy of the GNU Library General Public License 00017 along with this library; see the file COPYING.LIB. If not, write to 00018 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00019 Boston, MA 02110-1301, USA. 00020 */ 00021 00022 #include "kdatepicker.h" 00023 #include "kdatepicker_p.h" 00024 #include "kdatetable.h" 00025 00026 #include <QtGui/QApplication> 00027 #include <QtGui/QFont> 00028 #include <QtGui/QLayout> 00029 #include <QKeyEvent> 00030 #include <QtGui/QMenu> 00031 #include <QtGui/QPainter> 00032 #include <QtGui/QStyle> 00033 #include <QtGui/QToolButton> 00034 #include <QtGui/QDoubleValidator> 00035 00036 #include <kcalendarsystem.h> 00037 #include <klocalizeddate.h> 00038 #include <kcombobox.h> 00039 #include <kdebug.h> 00040 #include <kdialog.h> 00041 #include <kglobal.h> 00042 #include <kicon.h> 00043 #include <kiconloader.h> 00044 #include <klineedit.h> 00045 #include <knotification.h> 00046 00047 #include "kdatepicker.moc" 00048 #include "kdatepicker_p.moc" 00049 00050 // Week numbers are defined by ISO 8601 00051 // See http://www.merlyn.demon.co.uk/weekinfo.htm for details 00052 00053 KDatePickerPrivateYearSelector::KDatePickerPrivateYearSelector( 00054 const KCalendarSystem *cal, const QDate ¤tDate, QWidget* parent ) 00055 : QLineEdit( parent ), val( new QIntValidator( this ) ), result( 0 ) 00056 { 00057 calendar = cal; 00058 oldDate = currentDate; 00059 00060 QFont font; 00061 font = KGlobalSettings::generalFont(); 00062 setFont( font ); 00063 00064 setFrame( false ); 00065 00066 val->setRange( calendar->year( calendar->earliestValidDate() ), 00067 calendar->year( calendar->latestValidDate() ) ); 00068 setValidator( val ); 00069 00070 connect( this, SIGNAL( returnPressed() ), SLOT( yearEnteredSlot() ) ); 00071 } 00072 00073 void KDatePickerPrivateYearSelector::yearEnteredSlot() 00074 { 00075 bool ok; 00076 int newYear; 00077 QDate newDate; 00078 00079 // check if entered value is a number 00080 newYear = text().toInt( &ok ); 00081 if( !ok ) { 00082 KNotification::beep(); 00083 return; 00084 } 00085 00086 // check if new year will lead to a valid date 00087 if ( calendar->setDate( newDate, newYear, calendar->month( oldDate ), calendar->day( oldDate ) ) ) { 00088 result = newYear; 00089 emit( closeMe( 1 ) ); 00090 } else { 00091 KNotification::beep(); 00092 } 00093 00094 } 00095 00096 int KDatePickerPrivateYearSelector::year() 00097 { 00098 return result; 00099 } 00100 00101 void KDatePickerPrivateYearSelector::setYear( int year ) 00102 { 00103 setText( QString::number( year ) ); 00104 } 00105 00106 class KDatePicker::KDatePickerPrivate 00107 { 00108 public: 00109 KDatePickerPrivate( KDatePicker *q ) : 00110 q( q ), closeButton( 0L ), selectWeek( 0L ), todayButton( 0 ), navigationLayout( 0 ) 00111 { 00112 } 00113 00114 void fillWeeksCombo(); 00115 QDate validDateInYearMonth( int year, int month ); 00116 00118 KDatePicker *q; 00119 00120 QToolButton *closeButton; 00121 KComboBox *selectWeek; 00122 QToolButton *todayButton; 00123 QBoxLayout *navigationLayout; 00124 00126 QToolButton *yearForward; 00128 QToolButton *yearBackward; 00130 QToolButton *monthForward; 00132 QToolButton *monthBackward; 00134 QToolButton *selectMonth; 00136 QToolButton *selectYear; 00138 QLineEdit *line; 00140 KDateValidator *val; 00142 KDateTable *table; 00144 QSize maxMonthRect; 00145 00147 int fontsize; 00148 }; 00149 00150 void KDatePicker::KDatePickerPrivate::fillWeeksCombo() 00151 { 00152 // every year can have a different number of weeks 00153 // it could be that we had 53,1..52 and now 1..53 which is the same number but different 00154 // so always fill with new values 00155 // We show all week numbers for all weeks between first day of year to last day of year 00156 // This of course can be a list like 53,1,2..52 00157 00158 KLocalizedDate thisDate( q->date(), q->calendar() ); 00159 int thisYear = thisDate.year(); 00160 KLocalizedDate day = thisDate.firstDayOfYear(); 00161 KLocalizedDate lastDayOfYear = thisDate.lastDayOfYear(); 00162 00163 selectWeek->clear(); 00164 00165 // Starting from the first day in the year, loop through the year a week at a time 00166 // adding an entry to the week combo for each week in the year 00167 00168 for ( ; day.isValid() && day <= lastDayOfYear; day.addDaysTo( day.daysInWeek() ) ) { 00169 00170 // Get the ISO week number for the current day and what year that week is in 00171 // e.g. 1st day of this year may fall in week 53 of previous year 00172 int weekYear = thisYear; 00173 day.week( &weekYear ); 00174 QString weekString = i18n( "Week %1", day.formatDate( KLocale::Week, KLocale::ShortNumber ) ); 00175 00176 // show that this is a week from a different year 00177 if ( weekYear != thisYear ) { 00178 weekString += '*'; 00179 } 00180 00181 // when the week is selected, go to the same weekday as the one 00182 // that is currently selected in the date table 00183 QDate targetDate = day.addDays( thisDate.dayOfWeek() - day.dayOfWeek() ).date(); 00184 selectWeek->addItem( weekString, targetDate ); 00185 00186 // make sure that the week of the lastDayOfYear is always inserted: in Chinese calendar 00187 // system, this is not always the case 00188 if ( day < lastDayOfYear && 00189 day.daysDifference( lastDayOfYear ) < day.daysInWeek() && 00190 lastDayOfYear.week() != day.week() ) { 00191 day = lastDayOfYear.addDays( - thisDate.daysInWeek() ); 00192 } 00193 } 00194 } 00195 00196 QDate KDatePicker::KDatePickerPrivate::validDateInYearMonth( int year, int month ) 00197 { 00198 QDate newDate; 00199 00200 // Try to create a valid date in this year and month 00201 // First try the first of the month, then try last of month 00202 if ( q->calendar()->isValid( year, month, 1 ) ) { 00203 q->calendar()->setDate( newDate, year, month, 1 ); 00204 } else if ( q->calendar()->isValid( year, month + 1, 1 ) ) { 00205 q->calendar()->setDate( newDate, year, month, 1 ); 00206 q->calendar()->addDays( newDate, -1 ); 00207 } else { 00208 newDate = QDate::fromJulianDay( 0 ); 00209 } 00210 00211 return newDate; 00212 } 00213 00214 KDatePicker::KDatePicker( QWidget* parent ) : QFrame( parent ), d( new KDatePickerPrivate( this ) ) 00215 { 00216 init( QDate::currentDate() ); 00217 } 00218 00219 KDatePicker::KDatePicker( const QDate& date_, QWidget* parent ) 00220 : QFrame( parent ), d( new KDatePickerPrivate( this ) ) 00221 { 00222 init( date_ ); 00223 } 00224 00225 void KDatePicker::init( const QDate &date_ ) 00226 { 00227 QBoxLayout * topLayout = new QVBoxLayout( this ); 00228 topLayout->setSpacing( 0 ); 00229 topLayout->setMargin( 0 ); 00230 00231 d->navigationLayout = new QHBoxLayout(); 00232 d->navigationLayout->setSpacing( 0 ); 00233 d->navigationLayout->setMargin( 0 ); 00234 topLayout->addLayout( d->navigationLayout ); 00235 d->navigationLayout->addStretch(); 00236 d->yearBackward = new QToolButton( this ); 00237 d->yearBackward->setAutoRaise( true ); 00238 d->navigationLayout->addWidget( d->yearBackward ); 00239 d->monthBackward = new QToolButton( this ); 00240 d->monthBackward ->setAutoRaise( true ); 00241 d->navigationLayout->addWidget( d->monthBackward ); 00242 d->navigationLayout->addSpacing( KDialog::spacingHint() ); 00243 00244 d->selectMonth = new QToolButton( this ); 00245 d->selectMonth ->setAutoRaise( true ); 00246 d->navigationLayout->addWidget( d->selectMonth ); 00247 d->selectYear = new QToolButton( this ); 00248 d->selectYear->setCheckable( true ); 00249 d->selectYear->setAutoRaise( true ); 00250 d->navigationLayout->addWidget( d->selectYear ); 00251 d->navigationLayout->addSpacing( KDialog::spacingHint() ); 00252 00253 d->monthForward = new QToolButton( this ); 00254 d->monthForward ->setAutoRaise( true ); 00255 d->navigationLayout->addWidget( d->monthForward ); 00256 d->yearForward = new QToolButton( this ); 00257 d->yearForward ->setAutoRaise( true ); 00258 d->navigationLayout->addWidget( d->yearForward ); 00259 d->navigationLayout->addStretch(); 00260 00261 d->line = new KLineEdit( this ); 00262 d->val = new KDateValidator( this ); 00263 d->table = new KDateTable( this ); 00264 setFocusProxy( d->table ); 00265 00266 d->fontsize = KGlobalSettings::generalFont().pointSize(); 00267 if ( d->fontsize == -1 ) { 00268 d->fontsize = QFontInfo( KGlobalSettings::generalFont() ).pointSize(); 00269 } 00270 00271 d->fontsize++; // Make a little bigger 00272 00273 d->selectWeek = new KComboBox( this ); // read only week selection 00274 d->selectWeek->setFocusPolicy( Qt::NoFocus ); 00275 d->todayButton = new QToolButton( this ); 00276 d->todayButton->setIcon( KIcon( "go-jump-today" ) ); 00277 00278 d->yearForward->setToolTip( i18n( "Next year" ) ); 00279 d->yearBackward->setToolTip( i18n( "Previous year" ) ); 00280 d->monthForward->setToolTip( i18n( "Next month" ) ); 00281 d->monthBackward->setToolTip( i18n( "Previous month" ) ); 00282 d->selectWeek->setToolTip( i18n( "Select a week" ) ); 00283 d->selectMonth->setToolTip( i18n( "Select a month" ) ); 00284 d->selectYear->setToolTip( i18n( "Select a year" ) ); 00285 d->todayButton->setToolTip( i18n( "Select the current day" ) ); 00286 00287 // ----- 00288 setFontSize( d->fontsize ); 00289 d->line->setValidator( d->val ); 00290 d->line->installEventFilter( this ); 00291 if ( QApplication::isRightToLeft() ) { 00292 d->yearForward->setIcon( KIcon( QLatin1String( "arrow-left-double" ) ) ); 00293 d->yearBackward->setIcon( KIcon( QLatin1String( "arrow-right-double" ) ) ); 00294 d->monthForward->setIcon( KIcon( QLatin1String( "arrow-left" ) ) ); 00295 d->monthBackward->setIcon( KIcon( QLatin1String( "arrow-right" ) ) ); 00296 } else { 00297 d->yearForward->setIcon( KIcon( QLatin1String( "arrow-right-double" ) ) ); 00298 d->yearBackward->setIcon( KIcon( QLatin1String( "arrow-left-double" ) ) ); 00299 d->monthForward->setIcon( KIcon( QLatin1String( "arrow-right" ) ) ); 00300 d->monthBackward->setIcon( KIcon( QLatin1String( "arrow-left" ) ) ); 00301 } 00302 00303 connect( d->table, SIGNAL( dateChanged( const QDate& ) ), SLOT( dateChangedSlot( const QDate& ) ) ); 00304 connect( d->table, SIGNAL( tableClicked() ), SLOT( tableClickedSlot() ) ); 00305 connect( d->monthForward, SIGNAL( clicked() ), SLOT( monthForwardClicked() ) ); 00306 connect( d->monthBackward, SIGNAL( clicked() ), SLOT( monthBackwardClicked() ) ); 00307 connect( d->yearForward, SIGNAL( clicked() ), SLOT( yearForwardClicked() ) ); 00308 connect( d->yearBackward, SIGNAL( clicked() ), SLOT( yearBackwardClicked() ) ); 00309 connect( d->selectWeek, SIGNAL( activated( int ) ), SLOT( weekSelected( int ) ) ); 00310 connect( d->todayButton, SIGNAL( clicked() ), SLOT( todayButtonClicked() ) ); 00311 connect( d->selectMonth, SIGNAL( clicked() ), SLOT( selectMonthClicked() ) ); 00312 connect( d->selectYear, SIGNAL( toggled( bool ) ), SLOT( selectYearClicked() ) ); 00313 connect( d->line, SIGNAL( returnPressed() ), SLOT( lineEnterPressed() ) ); 00314 00315 00316 topLayout->addWidget( d->table ); 00317 00318 QBoxLayout * bottomLayout = new QHBoxLayout(); 00319 bottomLayout->setMargin( 0 ); 00320 bottomLayout->setSpacing( 0 ); 00321 topLayout->addLayout( bottomLayout ); 00322 00323 bottomLayout->addWidget( d->todayButton ); 00324 bottomLayout->addWidget( d->line ); 00325 bottomLayout->addWidget( d->selectWeek ); 00326 00327 d->table->setDate( date_ ); 00328 dateChangedSlot( date_ ); // needed because table emits changed only when newDate != oldDate 00329 } 00330 00331 KDatePicker::~KDatePicker() 00332 { 00333 delete d; 00334 } 00335 00336 bool KDatePicker::eventFilter( QObject *o, QEvent *e ) 00337 { 00338 if ( e->type() == QEvent::KeyPress ) { 00339 QKeyEvent * k = ( QKeyEvent * )e; 00340 00341 if ( ( k->key() == Qt::Key_PageUp ) || 00342 ( k->key() == Qt::Key_PageDown ) || 00343 ( k->key() == Qt::Key_Up ) || 00344 ( k->key() == Qt::Key_Down ) ) { 00345 QApplication::sendEvent( d->table, e ); 00346 d->table->setFocus(); 00347 return true; // eat event 00348 } 00349 } 00350 return QFrame::eventFilter( o, e ); 00351 } 00352 00353 void KDatePicker::resizeEvent( QResizeEvent* e ) 00354 { 00355 QWidget::resizeEvent( e ); 00356 } 00357 00358 void KDatePicker::dateChangedSlot( const QDate &date_ ) 00359 { 00360 KLocalizedDate thisDate( date_, calendar() ); 00361 d->line->setText( thisDate.formatDate( KLocale::ShortDate ) ); 00362 d->selectMonth->setText( thisDate.formatDate( KLocale::Month, KLocale::LongName ) ); 00363 d->fillWeeksCombo(); 00364 00365 // calculate the item num in the week combo box; normalize selected day so as if 1.1. is the first day of the week 00366 KLocalizedDate firstDay = thisDate.firstDayOfYear(); 00367 // If we cannot successfully create the 1st of the year, this can only mean that 00368 // the 1st is before the earliest valid date in the current calendar system, so use 00369 // the earliestValidDate as the first day. 00370 // In particular covers the case of Gregorian where 1/1/-4713 is not a valid QDate 00371 d->selectWeek->setCurrentIndex( ( thisDate.dayOfYear() + firstDay.dayOfWeek() - 2 ) / 00372 thisDate.daysInWeek() ); 00373 d->selectYear->setText( thisDate.formatDate( KLocale::Year, KLocale::LongNumber ) ); 00374 00375 emit( dateChanged( date_ ) ); 00376 } 00377 00378 void KDatePicker::tableClickedSlot() 00379 { 00380 emit( dateSelected( date() ) ); 00381 emit( tableClicked() ); 00382 } 00383 00384 const QDate & KDatePicker::date() const 00385 { 00386 return d->table->date(); 00387 } 00388 00389 bool KDatePicker::setDate( const QDate &date_ ) 00390 { 00391 // the table setDate does validity checking for us 00392 // this also emits dateChanged() which then calls our dateChangedSlot() 00393 return d->table->setDate( date_ ); 00394 } 00395 00396 const KCalendarSystem *KDatePicker::calendar() const 00397 { 00398 return d->table->calendar(); 00399 } 00400 00401 bool KDatePicker::setCalendar( KCalendarSystem *calendar ) 00402 { 00403 return d->table->setCalendar( calendar ); 00404 } 00405 00406 bool KDatePicker::setCalendar( const QString &calendarType ) 00407 { 00408 return d->table->setCalendar( calendarType ); 00409 } 00410 00411 bool KDatePicker::setCalendarSystem( KLocale::CalendarSystem calendarSystem ) 00412 { 00413 return d->table->setCalendarSystem( calendarSystem ); 00414 } 00415 00416 void KDatePicker::monthForwardClicked() 00417 { 00418 if ( ! setDate( calendar()->addMonths( date(), 1 ) ) ) { 00419 KNotification::beep(); 00420 } 00421 d->table->setFocus(); 00422 } 00423 00424 void KDatePicker::monthBackwardClicked() 00425 { 00426 if ( ! setDate( calendar()->addMonths( date(), -1 ) ) ) { 00427 KNotification::beep(); 00428 } 00429 d->table->setFocus(); 00430 } 00431 00432 void KDatePicker::yearForwardClicked() 00433 { 00434 if ( ! setDate( calendar()->addYears( d->table->date(), 1 ) ) ) { 00435 KNotification::beep(); 00436 } 00437 d->table->setFocus(); 00438 } 00439 00440 void KDatePicker::yearBackwardClicked() 00441 { 00442 if ( ! setDate( calendar()->addYears( d->table->date(), -1 ) ) ) { 00443 KNotification::beep(); 00444 } 00445 d->table->setFocus(); 00446 } 00447 00448 void KDatePicker::weekSelected( int index ) 00449 { 00450 QDate targetDay = d->selectWeek->itemData( index ).toDateTime().date(); 00451 00452 if ( ! setDate( targetDay ) ) { 00453 KNotification::beep(); 00454 } 00455 d->table->setFocus(); 00456 } 00457 00458 void KDatePicker::selectMonthClicked() 00459 { 00460 KLocalizedDate thisDate( date(), calendar() ); 00461 d->table->setFocus(); 00462 00463 QMenu popup( d->selectMonth ); 00464 // Populate the pick list with all the month names, this may change by year 00465 // JPL do we need to do somethng here for months that fall outside valid range? 00466 for ( int m = 1; m <= thisDate.monthsInYear(); m++ ) { 00467 popup.addAction( calendar()->monthName( m, thisDate.year() ) )->setData( m ); 00468 } 00469 00470 QAction *item = popup.actions()[ thisDate.month() - 1 ]; 00471 // if this happens the above should already given an assertion 00472 if ( item ) { 00473 popup.setActiveAction( item ); 00474 } 00475 00476 // cancelled 00477 if ( ( item = popup.exec( d->selectMonth->mapToGlobal( QPoint( 0, 0 ) ), item ) ) == 0 ) { 00478 return; 00479 } 00480 00481 // We need to create a valid date in the month selected so we can find out how many days are 00482 // in the month. 00483 KLocalizedDate newDate( thisDate.year(), item->data().toInt(), 1, calendar() ); 00484 00485 // If we have succeeded in creating a date in the new month, then try to create the new date, 00486 // checking we don't set a day after the last day of the month 00487 newDate.setDate( newDate.year(), newDate.month(), qMin( thisDate.day(), newDate.daysInMonth() ) ); 00488 00489 // Set the date, if it's invalid in any way then alert user and don't update 00490 if ( ! setDate( newDate.date() ) ) { 00491 KNotification::beep(); 00492 } 00493 } 00494 00495 void KDatePicker::selectYearClicked() 00496 { 00497 if ( !d->selectYear->isChecked() ) 00498 return; 00499 00500 KLocalizedDate thisDate( date(), calendar() ); 00501 00502 KPopupFrame *popup = new KPopupFrame( this ); 00503 KDatePickerPrivateYearSelector *picker = new KDatePickerPrivateYearSelector( calendar(), date(), popup ); 00504 picker->resize( picker->sizeHint() ); 00505 picker->setYear( thisDate.year() ); 00506 picker->selectAll(); 00507 popup->setMainWidget( picker ); 00508 connect( picker, SIGNAL( closeMe( int ) ), popup, SLOT( close( int ) ) ); 00509 picker->setFocus(); 00510 00511 if( popup->exec( d->selectYear->mapToGlobal( QPoint( 0, d->selectMonth->height() ) ) ) ) { 00512 // We need to create a valid date in the year/month selected so we can find out how many 00513 // days are in the month. 00514 KLocalizedDate newDate( picker->year(), thisDate.month(), 1, calendar() ); 00515 00516 // If we have succeeded in creating a date in the new month, then try to create the new 00517 // date, checking we don't set a day after the last day of the month 00518 newDate.setDate( newDate.year(), newDate.month(), qMin( thisDate.day(), newDate.daysInMonth() ) ); 00519 00520 // Set the date, if it's invalid in any way then alert user and don't update 00521 if ( ! setDate( newDate.date() ) ) { 00522 KNotification::beep(); 00523 } 00524 } 00525 delete popup; 00526 00527 d->selectYear->setChecked( false ); 00528 } 00529 00530 void KDatePicker::uncheckYearSelector() 00531 { 00532 d->selectYear->setChecked(false); 00533 d->selectYear->update(); 00534 } 00535 00536 // ####### KDE4: setEnabled isn't virtual anymore, so this isn't polymorphic. 00537 // Better reimplement changeEvent() instead. 00538 void KDatePicker::setEnabled( bool enable ) 00539 { 00540 QWidget * const widgets[] = { 00541 d->yearForward, d->yearBackward, d->monthForward, d->monthBackward, 00542 d->selectMonth, d->selectYear, 00543 d->line, d->table, d->selectWeek, d->todayButton 00544 }; 00545 const int Size = sizeof( widgets ) / sizeof( widgets[0] ); 00546 int count; 00547 00548 for( count = 0; count < Size; ++count ) { 00549 widgets[count]->setEnabled( enable ); 00550 } 00551 d->table->setFocus(); 00552 } 00553 00554 KDateTable *KDatePicker::dateTable() const 00555 { 00556 return d->table; 00557 } 00558 00559 void KDatePicker::lineEnterPressed() 00560 { 00561 QDate newDate = calendar()->readDate( d->line->text() ); 00562 00563 if ( calendar()->isValid( newDate ) ) { 00564 emit( dateEntered( newDate ) ); 00565 setDate( newDate ); 00566 d->table->setFocus(); 00567 } else { 00568 KNotification::beep(); 00569 } 00570 } 00571 00572 void KDatePicker::todayButtonClicked() 00573 { 00574 setDate( QDate::currentDate() ); 00575 d->table->setFocus(); 00576 } 00577 00578 QSize KDatePicker::sizeHint() const 00579 { 00580 return QWidget::sizeHint(); 00581 } 00582 00583 void KDatePicker::setFontSize( int s ) 00584 { 00585 QWidget * const buttons[] = { 00586 d->selectMonth, 00587 d->selectYear, 00588 }; 00589 const int NoOfButtons = sizeof( buttons ) / sizeof( buttons[0] ); 00590 int count; 00591 QFont font; 00592 QRect r; 00593 // ----- 00594 d->fontsize = s; 00595 for( count = 0; count < NoOfButtons; ++count ) { 00596 font = buttons[count]->font(); 00597 font.setPointSize( s ); 00598 buttons[count]->setFont( font ); 00599 } 00600 d->table->setFontSize( s ); 00601 00602 QFontMetrics metrics( d->selectMonth->fontMetrics() ); 00603 QString longestMonth; 00604 00605 for ( int i = 1; ; ++i ) { 00606 QString str = calendar()->monthName( i, calendar()->year( date() ), KCalendarSystem::LongName ); 00607 if ( str.isNull() ) { 00608 break; 00609 } 00610 r = metrics.boundingRect( str ); 00611 00612 if ( r.width() > d->maxMonthRect.width() ) { 00613 d->maxMonthRect.setWidth( r.width() ); 00614 longestMonth = str; 00615 } 00616 if ( r.height() > d->maxMonthRect.height() ) { 00617 d->maxMonthRect.setHeight( r.height() ); 00618 } 00619 } 00620 00621 QStyleOptionToolButton opt; 00622 opt.initFrom( d->selectMonth ); 00623 opt.text = longestMonth; 00624 00625 // stolen from QToolButton 00626 QSize textSize = metrics.size( Qt::TextShowMnemonic, longestMonth ); 00627 textSize.setWidth( textSize.width() + metrics.width( QLatin1Char(' ') ) * 2 ); 00628 int w = textSize.width(); 00629 int h = textSize.height(); 00630 opt.rect.setHeight( h ); // PM_MenuButtonIndicator depends on the height 00631 00632 QSize metricBound = style()->sizeFromContents( 00633 QStyle::CT_ToolButton, &opt, QSize( w, h ), d->selectMonth 00634 ).expandedTo( QApplication::globalStrut() ); 00635 00636 d->selectMonth->setMinimumSize( metricBound ); 00637 } 00638 00639 int KDatePicker::fontSize() const 00640 { 00641 return d->fontsize; 00642 } 00643 00644 00645 void KDatePicker::setCloseButton( bool enable ) 00646 { 00647 if ( enable == ( d->closeButton != 0L ) ) { 00648 return; 00649 } 00650 00651 if ( enable ) { 00652 d->closeButton = new QToolButton( this ); 00653 d->closeButton->setAutoRaise( true ); 00654 d->navigationLayout->addSpacing( KDialog::spacingHint() ); 00655 d->navigationLayout->addWidget( d->closeButton ); 00656 d->closeButton->setToolTip( i18nc( "@action:button", "Close" ) ); 00657 d->closeButton->setIcon( SmallIcon( "window-close" ) ); 00658 connect( d->closeButton, SIGNAL( clicked() ), 00659 topLevelWidget(), SLOT( close() ) ); 00660 } else { 00661 delete d->closeButton; 00662 d->closeButton = 0L; 00663 } 00664 00665 updateGeometry(); 00666 } 00667 00668 bool KDatePicker::hasCloseButton() const 00669 { 00670 return ( d->closeButton ); 00671 }
KDE 4.6 API Reference