akonadi
dateeditwidget.cpp
00001 /* 00002 This file is part of Akonadi Contact. 00003 00004 Copyright (c) 2009 Tobias Koenig <tokoe@kde.org> 00005 00006 This library is free software; you can redistribute it and/or modify it 00007 under the terms of the GNU Library General Public License as published by 00008 the Free Software Foundation; either version 2 of the License, or (at your 00009 option) any later version. 00010 00011 This library is distributed in the hope that it will be useful, but WITHOUT 00012 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00013 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 00014 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 the 00018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 00019 02110-1301, USA. 00020 */ 00021 00022 #include "dateeditwidget.h" 00023 00024 #include "kdatepickerpopup_p.h" 00025 00026 #include <kdatepicker.h> 00027 #include <kglobal.h> 00028 #include <kicon.h> 00029 #include <klocale.h> 00030 00031 #include <QtGui/QContextMenuEvent> 00032 #include <QtGui/QHBoxLayout> 00033 #include <QtGui/QLabel> 00034 #include <QtGui/QToolButton> 00035 00036 DateView::DateView( QWidget *parent ) 00037 : QLabel( parent ) 00038 { 00039 setTextInteractionFlags( Qt::TextSelectableByMouse ); 00040 setFrameShape( QFrame::Panel ); 00041 setFrameShadow( QFrame::Sunken ); 00042 } 00043 00044 void DateView::contextMenuEvent( QContextMenuEvent *event ) 00045 { 00046 if ( text().isEmpty() ) 00047 return; 00048 00049 QMenu menu; 00050 menu.addAction( i18n( "Remove" ), this, SLOT( emitSignal() ) ); 00051 00052 menu.exec( event->globalPos() ); 00053 } 00054 00055 void DateView::emitSignal() 00056 { 00057 emit resetDate(); 00058 } 00059 00060 DateEditWidget::DateEditWidget( Type type, QWidget *parent ) 00061 : QWidget( parent ), mReadOnly( false ) 00062 { 00063 QHBoxLayout *layout = new QHBoxLayout( this ); 00064 layout->setMargin( 0 ); 00065 00066 mView = new DateView; 00067 layout->addWidget( mView ); 00068 00069 mClearButton = new QToolButton; 00070 if ( layoutDirection() == Qt::LeftToRight ) 00071 mClearButton->setIcon( KIcon( QLatin1String( "edit-clear-locationbar-rtl" ) ) ); 00072 else 00073 mClearButton->setIcon( KIcon( QLatin1String( "edit-clear-locationbar-ltr" ) ) ); 00074 layout->addWidget( mClearButton ); 00075 00076 mSelectButton = new QToolButton; 00077 mSelectButton->setPopupMode( QToolButton::InstantPopup ); 00078 switch ( type ) { 00079 case General: mSelectButton->setIcon( KIcon( QLatin1String( "view-calendar-day" ) ) ); break; 00080 case Birthday: mSelectButton->setIcon( KIcon( QLatin1String( "view-calendar-birthday" ) ) ); break; 00081 case Anniversary: mSelectButton->setIcon( KIcon( QLatin1String( "view-calendar-wedding-anniversary" ) ) ); break; 00082 } 00083 00084 layout->addWidget( mSelectButton ); 00085 00086 mMenu = new KDatePickerPopup( KDatePickerPopup::DatePicker, QDate(), this ); 00087 mSelectButton->setMenu( mMenu ); 00088 00089 connect( mClearButton, SIGNAL( clicked() ), SLOT( resetDate() ) ); 00090 connect( mMenu, SIGNAL( dateChanged( const QDate& ) ), SLOT( dateSelected( const QDate& ) ) ); 00091 connect( mView, SIGNAL( resetDate() ), SLOT( resetDate() ) ); 00092 00093 updateView(); 00094 } 00095 00096 DateEditWidget::~DateEditWidget() 00097 { 00098 } 00099 00100 void DateEditWidget::setDate( const QDate &date ) 00101 { 00102 mDate = date; 00103 mMenu->setDate( mDate ); 00104 updateView(); 00105 } 00106 00107 QDate DateEditWidget::date() const 00108 { 00109 return mDate; 00110 } 00111 00112 void DateEditWidget::setReadOnly( bool readOnly ) 00113 { 00114 mReadOnly = readOnly; 00115 00116 mSelectButton->setEnabled( !readOnly ); 00117 mClearButton->setEnabled( !readOnly ); 00118 } 00119 00120 void DateEditWidget::dateSelected(const QDate &date) 00121 { 00122 mDate = date; 00123 updateView(); 00124 } 00125 00126 void DateEditWidget::resetDate() 00127 { 00128 mDate = QDate(); 00129 updateView(); 00130 } 00131 00132 void DateEditWidget::updateView() 00133 { 00134 if ( mDate.isValid() ) { 00135 mView->setText( KGlobal::locale()->formatDate( mDate ) ); 00136 mClearButton->show(); 00137 } else { 00138 mView->setText( QString() ); 00139 mClearButton->hide(); 00140 } 00141 } 00142 00143 #include "dateeditwidget.moc"