Fix crash if key bindings specified in profile cannot be found. Improve
[personal-kdebase.git] / apps / kfind / kdatecombo.cpp
blob0317b9d90d8d0ea41b25e50c8593cb5236de338d
1 /***********************************************************************
3 * kdatecombo.cpp
5 ***********************************************************************/
6 #include "kdatecombo.h"
8 #include "kdatecombo.moc"
10 #include <QtCore/QTimer>
11 //Added by qt3to4:
12 #include <QtGui/QKeyEvent>
13 #include <QtCore/QEvent>
15 #include <kglobal.h>
16 #include <klocale.h>
17 #include <kdatepicker.h>
18 #include <kdatetable.h>
19 #include <kdebug.h>
21 KDateCombo::KDateCombo(QWidget *parent) : QComboBox(parent)
23 setEditable( false );
25 QDate date = QDate::currentDate();
26 initObject(date);
29 KDateCombo::KDateCombo(const QDate & date, QWidget *parent) : QComboBox(parent)
31 setEditable( false );
33 initObject(date);
36 void KDateCombo::initObject(const QDate & date)
38 setValidator(0);
39 popupFrame = new KPopupFrame(this);
40 popupFrame->installEventFilter(this);
41 datePicker = new KDatePicker(date, popupFrame);
42 datePicker->setMinimumSize(datePicker->sizeHint());
43 datePicker->installEventFilter(this);
44 popupFrame->setMainWidget(datePicker);
45 setDate(date);
47 connect(datePicker, SIGNAL(dateSelected(QDate)), this, SLOT(dateEnteredEvent(QDate)));
48 connect(datePicker, SIGNAL(dateEntered(QDate)), this, SLOT(dateEnteredEvent(QDate)));
51 KDateCombo::~KDateCombo()
53 delete datePicker;
54 delete popupFrame;
57 QString KDateCombo::date2String(const QDate & date)
59 return(KGlobal::locale()->formatDate(date, KLocale::ShortDate));
62 QDate & KDateCombo::string2Date(const QString & str, QDate *qd)
64 return *qd = KGlobal::locale()->readDate(str);
67 QDate & KDateCombo::getDate(QDate *currentDate)
69 return string2Date(currentText(), currentDate);
72 bool KDateCombo::setDate(const QDate & newDate)
74 if (newDate.isValid())
76 if (count())
77 clear();
78 addItem(date2String(newDate));
79 return true;
81 return false;
84 void KDateCombo::dateEnteredEvent(const QDate &newDate)
86 QDate tempDate = newDate;
87 if (!tempDate.isValid())
88 tempDate = datePicker->date();
89 popupFrame->hide();
90 setDate(tempDate);
93 void KDateCombo::mousePressEvent (QMouseEvent * e)
95 if (e->button() & Qt::LeftButton)
97 if (rect().contains( e->pos()))
99 QDate tempDate;
100 getDate(& tempDate);
101 datePicker->setDate(tempDate);
102 popupFrame->popup(mapToGlobal(QPoint(0, height())));
107 bool KDateCombo::eventFilter (QObject*, QEvent* e)
109 if ( e->type() == QEvent::MouseButtonPress )
111 QMouseEvent *me = (QMouseEvent *)e;
112 QPoint p = mapFromGlobal( me->globalPos() );
113 if (rect().contains( p ) )
115 QTimer::singleShot(10, this, SLOT(dateEnteredEvent()));
116 return true;
119 else if ( e->type() == QEvent::KeyRelease )
121 QKeyEvent *k = (QKeyEvent *)e;
123 if (k->key()==Qt::Key_Escape) {
124 popupFrame->hide();
125 return true;
127 else {
128 return false;
132 return false;