1 /***********************************************************************
5 ***********************************************************************/
6 #include "kdatecombo.h"
8 #include "kdatecombo.moc"
10 #include <QtCore/QTimer>
12 #include <QtGui/QKeyEvent>
13 #include <QtCore/QEvent>
17 #include <kdatepicker.h>
18 #include <kdatetable.h>
21 KDateCombo::KDateCombo(QWidget
*parent
) : QComboBox(parent
)
25 QDate date
= QDate::currentDate();
29 KDateCombo::KDateCombo(const QDate
& date
, QWidget
*parent
) : QComboBox(parent
)
36 void KDateCombo::initObject(const QDate
& date
)
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
);
47 connect(datePicker
, SIGNAL(dateSelected(QDate
)), this, SLOT(dateEnteredEvent(QDate
)));
48 connect(datePicker
, SIGNAL(dateEntered(QDate
)), this, SLOT(dateEnteredEvent(QDate
)));
51 KDateCombo::~KDateCombo()
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())
78 addItem(date2String(newDate
));
84 void KDateCombo::dateEnteredEvent(const QDate
&newDate
)
86 QDate tempDate
= newDate
;
87 if (!tempDate
.isValid())
88 tempDate
= datePicker
->date();
93 void KDateCombo::mousePressEvent (QMouseEvent
* e
)
95 if (e
->button() & Qt::LeftButton
)
97 if (rect().contains( e
->pos()))
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()));
119 else if ( e
->type() == QEvent::KeyRelease
)
121 QKeyEvent
*k
= (QKeyEvent
*)e
;
123 if (k
->key()==Qt::Key_Escape
) {