kdatepickerpopup.cpp
00001 /* 00002 This file is part of Akonadi Contact. 00003 00004 Copyright (c) 2004 Bram Schoenmakers <bramschoenmakers@kde.nl> 00005 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 "kdatepickerpopup_p.h" 00023 00024 #include <KDatePicker> 00025 #include <KLocale> 00026 00027 #include <QtCore/QDateTime> 00028 #include <QtGui/QWidgetAction> 00029 00030 class KDatePickerAction : public QWidgetAction 00031 { 00032 public: 00033 KDatePickerAction( KDatePicker *widget, QObject *parent ) 00034 : QWidgetAction( parent ), 00035 mDatePicker( widget ), mOriginalParent( widget->parentWidget() ) 00036 { 00037 } 00038 00039 protected: 00040 QWidget *createWidget( QWidget *parent ) 00041 { 00042 mDatePicker->setParent( parent ); 00043 return mDatePicker; 00044 } 00045 00046 void deleteWidget( QWidget *widget ) 00047 { 00048 if ( widget != mDatePicker ) { 00049 return; 00050 } 00051 00052 mDatePicker->setParent( mOriginalParent ); 00053 } 00054 00055 private: 00056 KDatePicker *mDatePicker; 00057 QWidget *mOriginalParent; 00058 }; 00059 00060 KDatePickerPopup::KDatePickerPopup( Items items, const QDate &date, QWidget *parent ) 00061 : QMenu( parent ) 00062 { 00063 mItems = items; 00064 00065 mDatePicker = new KDatePicker( this ); 00066 mDatePicker->setCloseButton( false ); 00067 00068 connect( mDatePicker, SIGNAL(dateEntered(QDate)), 00069 SLOT(slotDateChanged(QDate)) ); 00070 connect( mDatePicker, SIGNAL(dateSelected(QDate)), 00071 SLOT(slotDateChanged(QDate)) ); 00072 00073 mDatePicker->setDate( date ); 00074 00075 buildMenu(); 00076 } 00077 00078 void KDatePickerPopup::buildMenu() 00079 { 00080 if ( isVisible() ) { 00081 return; 00082 } 00083 clear(); 00084 00085 if ( mItems & DatePicker ) { 00086 addAction( new KDatePickerAction( mDatePicker, this ) ); 00087 00088 if ( ( mItems & NoDate ) || ( mItems & Words ) ) { 00089 addSeparator(); 00090 } 00091 } 00092 00093 if ( mItems & Words ) { 00094 addAction( i18nc( "@option today", "&Today" ), this, SLOT(slotToday()) ); 00095 addAction( i18nc( "@option tomorrow", "To&morrow" ), this, SLOT(slotTomorrow()) ); 00096 addAction( i18nc( "@option next week", "Next &Week" ), this, SLOT(slotNextWeek()) ); 00097 addAction( i18nc( "@option next month", "Next M&onth" ), this, SLOT(slotNextMonth()) ); 00098 00099 if ( mItems & NoDate ) { 00100 addSeparator(); 00101 } 00102 } 00103 00104 if ( mItems & NoDate ) { 00105 addAction( i18nc( "@option do not specify a date", "No Date" ), this, SLOT(slotNoDate()) ); 00106 } 00107 } 00108 00109 KDatePicker *KDatePickerPopup::datePicker() const 00110 { 00111 return mDatePicker; 00112 } 00113 00114 void KDatePickerPopup::setDate( const QDate &date ) 00115 { 00116 mDatePicker->setDate( date ); 00117 } 00118 00119 #if 0 00120 void KDatePickerPopup::setItems( int items ) 00121 { 00122 mItems = items; 00123 buildMenu(); 00124 } 00125 #endif 00126 00127 void KDatePickerPopup::slotDateChanged( const QDate &date ) 00128 { 00129 emit dateChanged( date ); 00130 hide(); 00131 } 00132 00133 void KDatePickerPopup::slotToday() 00134 { 00135 emit dateChanged( QDate::currentDate() ); 00136 } 00137 00138 void KDatePickerPopup::slotTomorrow() 00139 { 00140 emit dateChanged( QDate::currentDate().addDays( 1 ) ); 00141 } 00142 00143 void KDatePickerPopup::slotNoDate() 00144 { 00145 emit dateChanged( QDate() ); 00146 } 00147 00148 void KDatePickerPopup::slotNextWeek() 00149 { 00150 emit dateChanged( QDate::currentDate().addDays( 7 ) ); 00151 } 00152 00153 void KDatePickerPopup::slotNextMonth() 00154 { 00155 emit dateChanged( QDate::currentDate().addMonths( 1 ) ); 00156 } 00157 00158 #include "kdatepickerpopup_p.moc"