From 4aed2c8219774f5d797760606b8489a92ddc5163 Mon Sep 17 00:00:00 2001 From: toma Date: Wed, 25 Nov 2009 17:56:58 +0000 Subject: Copy the KDE 3.5 branch to branches/trinity for new KDE 3.5 features. BUG:215923 git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/kdebase@1054174 283d02a7-25f6-0310-bc7c-ecb5cbfe19da --- kfind/kdatecombo.cpp | 131 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 kfind/kdatecombo.cpp (limited to 'kfind/kdatecombo.cpp') diff --git a/kfind/kdatecombo.cpp b/kfind/kdatecombo.cpp new file mode 100644 index 000000000..c5f9e4553 --- /dev/null +++ b/kfind/kdatecombo.cpp @@ -0,0 +1,131 @@ +/*********************************************************************** + * + * kdatecombo.cpp + * + ***********************************************************************/ + +#include + +#include +#include +#include +#include +#include + +#include "kdatecombo.h" + +#include "kdatecombo.moc" + +KDateCombo::KDateCombo(QWidget *parent, const char *name ) : QComboBox(FALSE, parent,name) +{ + QDate date = QDate::currentDate(); + initObject(date, parent, name); +} + +KDateCombo::KDateCombo(const QDate & date, QWidget *parent, const char *name) : QComboBox(FALSE, parent,name) +{ + initObject(date, parent, name); +} + +void KDateCombo::initObject(const QDate & date, QWidget *, const char *) +{ + clearValidator(); + popupFrame = new KPopupFrame(this, "popupFrame"); + popupFrame->installEventFilter(this); + datePicker = new KDatePicker(popupFrame, date, "datePicker"); + datePicker->setMinimumSize(datePicker->sizeHint()); + datePicker->installEventFilter(this); + popupFrame->setMainWidget(datePicker); + setDate(date); + + connect(datePicker, SIGNAL(dateSelected(QDate)), this, SLOT(dateEnteredEvent(QDate))); +} + +KDateCombo::~KDateCombo() +{ + delete datePicker; + delete popupFrame; +} + +QString KDateCombo::date2String(const QDate & date) +{ + return(KGlobal::locale()->formatDate(date, true)); +} + +QDate & KDateCombo::string2Date(const QString & str, QDate *qd) +{ + return *qd = KGlobal::locale()->readDate(str); +} + +QDate & KDateCombo::getDate(QDate *currentDate) +{ + return string2Date(currentText(), currentDate); +} + +bool KDateCombo::setDate(const QDate & newDate) +{ + if (newDate.isValid()) + { + if (count()) + clear(); + insertItem(date2String(newDate)); + return TRUE; + } + return FALSE; +} + +void KDateCombo::dateEnteredEvent(QDate newDate) +{ + if (!newDate.isValid()) + newDate = datePicker->date(); + popupFrame->hide(); + setDate(newDate); +} + +void KDateCombo::mousePressEvent (QMouseEvent * e) +{ + if (e->button() & QMouseEvent::LeftButton) + { + if (rect().contains( e->pos())) + { + QDate tempDate; + getDate(& tempDate); + datePicker->setDate(tempDate); + popupFrame->popup(mapToGlobal(QPoint(0, height()))); + //datePicker->setFocus(); + } + } +} + +bool KDateCombo::eventFilter (QObject*, QEvent* e) +{ + if ( e->type() == QEvent::MouseButtonPress ) + { + QMouseEvent *me = (QMouseEvent *)e; + QPoint p = mapFromGlobal( me->globalPos() ); + if (rect().contains( p ) ) + { + QTimer::singleShot(10, this, SLOT(dateEnteredEvent())); + return true; + } + } + else if ( e->type() == QEvent::KeyRelease ) + { + QKeyEvent *k = (QKeyEvent *)e; + //Press return == pick selected date and close the combo + if((k->key()==Qt::Key_Return)||(k->key()==Qt::Key_Enter)) + { + dateEnteredEvent(datePicker->date()); + return true; + } + else if (k->key()==Qt::Key_Escape) + { + popupFrame->hide(); + return true; + } + else + return false; + } + + return false; +} -- cgit v1.2.1