File indexing completed on 2024-05-05 04:49:26

0001 /****************************************************************************************
0002  * Copyright (c) 2008 Daniel Caleb Jones <danielcjones@gmail.com>                       *
0003  *                                                                                      *
0004  * This program is free software; you can redistribute it and/or modify it under        *
0005  * the terms of the GNU General Public License as published by the Free Software        *
0006  * Foundation; either version 2 of the License, or (at your option) any later           *
0007  * version.                                                                             *
0008  *                                                                                      *
0009  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0011  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0012  *                                                                                      *
0013  * You should have received a copy of the GNU General Public License along with         *
0014  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0015  ****************************************************************************************/
0016 
0017 #include "kdatecombo.h"
0018 
0019 
0020 #include <QTimer>
0021 //Added by qt3to4:
0022 #include <QKeyEvent>
0023 #include <QEvent>
0024 #include <QVBoxLayout>
0025 
0026 
0027 #include <KLocalizedString>
0028 #include <KDatePicker>
0029 #include <KPopupFrame>
0030 #include <QDebug>
0031 #include <KConfigGroup>
0032 
0033 KDateCombo::KDateCombo(QWidget *parent) : QComboBox(parent)
0034 {
0035   setEditable( false );
0036 
0037   QDate date = QDate::currentDate();
0038   initObject(date);
0039 }
0040 
0041 KDateCombo::KDateCombo(const QDate & date, QWidget *parent) : QComboBox(parent)
0042 {
0043   setEditable( false );
0044 
0045   initObject(date);
0046 }
0047 
0048 void KDateCombo::initObject(const QDate & date)
0049 {
0050   setValidator(nullptr);
0051   popupFrame = new KPopupFrame(this);
0052   popupFrame->installEventFilter(this);
0053   datePicker = new KDatePicker(date, popupFrame);
0054   datePicker->setMinimumSize(datePicker->sizeHint());
0055   datePicker->installEventFilter(this);
0056   QVBoxLayout *mainLayout = new QVBoxLayout;
0057   popupFrame->setLayout(mainLayout);
0058   mainLayout->addWidget(datePicker);
0059   setDate(date);
0060 
0061   connect(datePicker, &KDatePicker::dateSelected, this, &KDateCombo::dateEnteredEvent);
0062   connect(datePicker, &KDatePicker::dateEntered, this, &KDateCombo::dateEnteredEvent);
0063 }
0064 
0065 KDateCombo::~KDateCombo()
0066 {
0067   delete datePicker;
0068   delete popupFrame;
0069 }
0070 
0071 QString KDateCombo::date2String(const QDate & date)
0072 {
0073   return(QLocale().toString(date, QLocale::ShortFormat));
0074 }
0075 
0076 QDate & KDateCombo::string2Date(const QString & str, QDate *qd)
0077 {
0078   return *qd = QLocale().toDate(str, QLocale::ShortFormat);
0079 }
0080 
0081 QDate & KDateCombo::getDate(QDate *currentDate)
0082 {
0083   return string2Date(currentText(), currentDate);
0084 }
0085 
0086 bool KDateCombo::setDate(const QDate & newDate)
0087 {
0088   if (newDate.isValid())
0089   {
0090     if (count())
0091       clear();
0092     addItem(date2String(newDate));
0093     return true;
0094   }
0095   return false;
0096 }
0097 
0098 void KDateCombo::dateEnteredEvent(const QDate &newDate)
0099 {
0100   QDate tempDate = newDate;
0101   if (!tempDate.isValid())
0102      tempDate = datePicker->date();
0103   popupFrame->hide();
0104   setDate(tempDate);
0105 }
0106 
0107 void KDateCombo::nullDateEnteredEvent()
0108 {
0109     dateEnteredEvent(QDate());
0110 }
0111 
0112 void KDateCombo::mousePressEvent (QMouseEvent * e)
0113 {
0114   if (e->button() & Qt::LeftButton)
0115   {
0116     if  (rect().contains( e->pos()))
0117     {
0118       QDate tempDate;
0119       getDate(& tempDate);
0120       datePicker->setDate(tempDate);
0121       popupFrame->popup(mapToGlobal(QPoint(0, height())));
0122     }
0123   }
0124 }
0125 
0126 bool KDateCombo::eventFilter (QObject*, QEvent* e)
0127 {
0128   if ( e->type() == QEvent::MouseButtonPress )
0129   {
0130       QMouseEvent *me = (QMouseEvent *)e;
0131       QPoint p = mapFromGlobal( me->globalPos() );
0132       if (rect().contains( p ) )
0133       {
0134           QTimer::singleShot(10, this, &KDateCombo::nullDateEnteredEvent);
0135         return true;
0136       }
0137   }
0138   else if ( e->type() == QEvent::KeyRelease )
0139   {
0140       QKeyEvent *k = (QKeyEvent *)e;
0141 
0142       if (k->key()==Qt::Key_Escape) {
0143         popupFrame->hide();
0144         return true;
0145       }
0146       else {
0147         return false;
0148       }
0149   }
0150 
0151   return false;
0152 }