25 #include <tqapplication.h>
26 #include <tqlineedit.h>
27 #include <tqlistbox.h>
28 #include <tqvalidator.h>
30 #include <kcalendarsystem.h>
31 #include <tdeglobal.h>
32 #include <tdeglobalsettings.h>
33 #include <tdelocale.h>
35 #include "kdateedit.h"
37 class DateValidator :
public TQValidator
40 DateValidator(
const TQStringList &keywords, TQWidget* parent,
const char* name = 0 )
41 : TQValidator( parent, name ), mKeywords( keywords )
44 virtual State validate( TQString &str,
int& )
const
46 int length = str.length();
52 if ( mKeywords.contains( str.lower() ) )
56 TDEGlobal::locale()->readDate( str, &ok );
64 TQStringList mKeywords;
67 KDateEdit::KDateEdit( TQWidget *parent,
const char *name )
68 : TQComboBox( true, parent, name ),
70 mDiscardNextMousePress( false )
75 mDate = TQDate::currentDate();
76 TQString today = TDEGlobal::locale()->formatDate( mDate,
true );
80 changeItem( today, 0 );
81 setMinimumSize( sizeHint() );
83 connect( lineEdit(), TQ_SIGNAL( returnPressed() ),
84 this, TQ_SLOT( lineEnterPressed() ) );
85 connect(
this, TQ_SIGNAL( textChanged(
const TQString& ) ),
86 TQ_SLOT( slotTextChanged(
const TQString& ) ) );
88 mPopup =
new KDatePickerPopup( KDatePickerPopup::DatePicker | KDatePickerPopup::Words );
90 mPopup->installEventFilter(
this );
92 connect( mPopup, TQ_SIGNAL( dateChanged( TQDate ) ),
93 TQ_SLOT( dateSelected( TQDate ) ) );
97 lineEdit()->installEventFilter(
this );
99 setValidator(
new DateValidator( mKeywordMap.keys(),
this ) );
101 mTextChanged =
false;
104 KDateEdit::~KDateEdit()
123 mReadOnly = readOnly;
124 lineEdit()->setReadOnly( readOnly );
132 void KDateEdit::popup()
137 TQRect desk = TDEGlobalSettings::desktopGeometry(
this );
139 TQPoint popupPoint = mapToGlobal( TQPoint( 0,0 ) );
141 int dateFrameHeight = mPopup->sizeHint().height();
142 if ( popupPoint.y() + height() + dateFrameHeight > desk.bottom() )
143 popupPoint.setY( popupPoint.y() - dateFrameHeight );
145 popupPoint.setY( popupPoint.y() + height() );
147 int dateFrameWidth = mPopup->sizeHint().width();
148 if ( popupPoint.x() + dateFrameWidth > desk.right() )
149 popupPoint.setX( desk.right() - dateFrameWidth );
151 if ( popupPoint.x() < desk.left() )
152 popupPoint.setX( desk.left() );
154 if ( popupPoint.y() < desk.top() )
155 popupPoint.setY( desk.top() );
157 if ( mDate.isValid() )
158 mPopup->setDate( mDate );
160 mPopup->setDate( TQDate::currentDate() );
162 mPopup->popup( popupPoint );
167 TQDate
date = parseDate();
171 TQListBox *lb = listBox();
173 lb->setCurrentItem(0);
174 TQKeyEvent* keyEvent =
new TQKeyEvent(TQEvent::KeyPress, TQt::Key_Enter, 0, 0);
175 TQApplication::postEvent(lb, keyEvent);
179 void KDateEdit::dateSelected( TQDate date )
186 if (
date.isValid() ) {
192 void KDateEdit::lineEnterPressed()
194 bool replaced =
false;
196 TQDate
date = parseDate( &replaced );
207 TQDate KDateEdit::parseDate(
bool *replaced )
const
209 TQString text = currentText();
215 if ( text.isEmpty() )
217 else if ( mKeywordMap.contains( text.lower() ) ) {
218 TQDate today = TQDate::currentDate();
219 int i = mKeywordMap[ text.lower() ];
230 int currentDay = today.dayOfWeek();
231 if ( i >= currentDay )
237 result = today.addDays( i );
241 result = TDEGlobal::locale()->readDate( text );
247 bool KDateEdit::eventFilter( TQObject *
object, TQEvent *event )
249 if (
object == lineEdit() ) {
252 if ( (event->type() == TQEvent::FocusOut) && mTextChanged ) {
254 mTextChanged =
false;
255 }
else if ( event->type() == TQEvent::KeyPress ) {
257 TQKeyEvent* keyEvent = (TQKeyEvent*)event;
259 if ( keyEvent->key() == TQt::Key_Return ) {
265 if ( keyEvent->key() == TQt::Key_Up )
267 else if ( keyEvent->key() == TQt::Key_Down )
270 if ( step && !mReadOnly ) {
271 TQDate
date = parseDate();
272 if (
date.isValid() ) {
285 switch ( event->type() ) {
286 case TQEvent::MouseButtonDblClick:
287 case TQEvent::MouseButtonPress: {
288 TQMouseEvent *mouseEvent = (TQMouseEvent*)event;
289 if ( !mPopup->rect().contains( mouseEvent->pos() ) ) {
290 TQPoint globalPos = mPopup->mapToGlobal( mouseEvent->pos() );
291 if ( TQApplication::widgetAt( globalPos,
true ) == this ) {
294 mDiscardNextMousePress =
true;
308 void KDateEdit::mousePressEvent( TQMouseEvent *event )
310 if ( event->button() == TQt::LeftButton && mDiscardNextMousePress ) {
311 mDiscardNextMousePress =
false;
315 TQComboBox::mousePressEvent( event );
318 void KDateEdit::slotTextChanged(
const TQString& )
320 TQDate
date = parseDate();
332 mKeywordMap.insert( i18n(
"tomorrow" ), 1 );
333 mKeywordMap.insert( i18n(
"today" ), 0 );
334 mKeywordMap.insert( i18n(
"yesterday" ), -1 );
337 for (
int i = 1; i <= 7; ++i ) {
338 dayName = TDEGlobal::locale()->calendar()->weekDayName( i ).lower();
339 mKeywordMap.insert( dayName, i + 100 );
346 mTextChanged =
false;
350 void KDateEdit::updateView()
353 if ( mDate.isValid() )
354 dateString = TDEGlobal::locale()->formatDate( mDate,
true );
358 bool blocked = signalsBlocked();
359 blockSignals(
true );
360 changeItem( dateString, 0 );
361 blockSignals( blocked );
364 #include "kdateedit.moc"
virtual bool assignDate(const TQDate &date)
Sets the date, without altering the display.
void setupKeywords()
Fills the keyword map.
void setDate(const TQDate &date)
Sets the date.
void setReadOnly(bool readOnly)
Sets whether the widget is read-only for the user.
void dateEntered(const TQDate &date)
This signal is emitted whenever the user has entered a new date.
void dateChanged(const TQDate &date)
This signal is emitted whenever the user modifies the date.