50
50
class DateValidator : public QValidator
54
DateValidator( const QStringList& keywords, QWidget* parent )
55
: QValidator( parent ), mKeywords( keywords )
58
virtual State validate( QString& str, int& ) const
60
int length = str.length();
62
// empty string is intermediate so one can clear the edit line and start from scratch
66
if ( mKeywords.contains( str.toLower() ) )
70
KGlobal::locale()->readDate( str, &ok );
79
QStringList mKeywords;
54
DateValidator( const QStringList& keywords, QWidget* parent )
55
: QValidator( parent ), mKeywords( keywords )
58
virtual State validate( QString& str, int& ) const
60
int length = str.length();
62
// empty string is intermediate so one can clear the edit line and start from scratch
68
if ( mKeywords.contains( str.toLower() ) )
74
KGlobal::locale()->readDate( str, &ok );
88
QStringList mKeywords;
82
KDateEdit::KDateEdit(QWidget *parent, const char* name)
83
: KComboBox(parent), mReadOnly(false), mDiscardNextMousePress(false)
91
KDateEdit::KDateEdit(QWidget* parent, const char* name)
92
: KComboBox(parent), mReadOnly(false), mDiscardNextMousePress(false)
85
94
setObjectName(name);
86
95
// need at least one entry for popup to work
95
104
setMinimumSize( sizeHint() );
97
106
connect( lineEdit(), SIGNAL( returnPressed() ),
98
this, SLOT( lineEnterPressed() ) );
107
this, SLOT( lineEnterPressed() ) );
100
109
connect( this, SIGNAL( textChanged( const QString& ) ),
101
SLOT( slotTextChanged( const QString& ) ) );
110
SLOT( slotTextChanged( const QString& ) ) );
103
112
mPopup = new KDatePickerPopup( KDatePickerPopup::DatePicker | KDatePickerPopup::Words );
147
156
void KDateEdit::showPopup()
152
163
QRect desk = KGlobalSettings::desktopGeometry( this );
153
164
QPoint popupPoint = mapToGlobal( QPoint( 0,0 ) );
155
166
int dateFrameHeight = mPopup->sizeHint().height();
156
168
if ( popupPoint.y() + height() + dateFrameHeight > desk.bottom() )
157
170
popupPoint.setY( popupPoint.y() - dateFrameHeight );
159
174
popupPoint.setY( popupPoint.y() + height() );
161
177
int dateFrameWidth = mPopup->sizeHint().width();
162
179
if ( popupPoint.x() + dateFrameWidth > desk.right() )
163
181
popupPoint.setX( desk.right() - dateFrameWidth );
165
184
if ( popupPoint.x() < desk.left() )
166
186
popupPoint.setX( desk.left() );
168
189
if ( popupPoint.y() < desk.top() )
169
191
popupPoint.setY( desk.top() );
171
194
if ( mDate.isValid() )
172
196
mPopup->setDate( mDate );
174
200
mPopup->setDate( QDate::currentDate() );
176
203
mPopup->popup( popupPoint );
182
209
assignDate( date );
184
211
// Now, simulate an Enter to unpress it
185
QAbstractItemView *lb = view();
212
QAbstractItemView* lb = view();
188
216
lb->setCurrentIndex( lb->model()->index( 0, 0 ) );
223
251
if (assignDate( date ) )
228
258
emit dateChanged( date );
232
QDate KDateEdit::parseDate( bool *replaced ) const
262
QDate KDateEdit::parseDate( bool* replaced ) const
234
264
QString text = currentText();
238
269
(*replaced) = false;
240
272
if ( text.isEmpty() )
241
274
result = QDate();
242
276
else if ( mKeywordMap.contains( text.toLower() ) )
244
278
QDate today = QDate::currentDate();
245
279
int i = mKeywordMap[ text.toLower() ];
248
283
/* A day name has been entered. Convert to offset from today.
276
bool KDateEdit::eventFilter( QObject *object, QEvent *event )
319
bool KDateEdit::eventFilter( QObject* object, QEvent* event )
278
if ( object == lineEdit() )
321
if ( object == lineEdit() )
280
323
// We only process the focus out event if the text has changed
281
324
// since we got focus
299
343
if ( keyEvent->key() == Qt::Key_Up )
301
347
else if ( keyEvent->key() == Qt::Key_Down )
303
352
if ( step && !mReadOnly )
305
354
QDate date = parseDate();
306
356
if ( date.isValid() )
308
358
date = date.addDays( step );
309
360
if ( assignDate( date ) )
321
372
// It's a date picker event
322
switch ( event->type() )
373
switch ( event->type() )
324
375
case QEvent::MouseButtonDblClick:
325
case QEvent::MouseButtonPress:
376
case QEvent::MouseButtonPress:
327
QMouseEvent *mouseEvent = (QMouseEvent*)event;
328
if ( !mPopup->rect().contains( mouseEvent->pos() ) )
378
QMouseEvent* mouseEvent = (QMouseEvent*)event;
380
if ( !mPopup->rect().contains( mouseEvent->pos() ) )
330
382
QPoint globalPos = mPopup->mapToGlobal( mouseEvent->pos() );
331
if ( QApplication::widgetAt( globalPos ) == this )
384
if ( QApplication::widgetAt( globalPos ) == this )
333
386
// The date picker is being closed by a click on the
334
387
// KDateEdit widget. Avoid popping it up again immediately.
335
388
mDiscardNextMousePress = true;
392
449
void KDateEdit::updateView()
394
451
QString dateString;
395
453
if ( mDate.isValid() )
396
455
dateString = KGlobal::locale()->formatDate( mDate, KLocale::ShortDate );
398
458
// We do not want to generate a signal here,
399
459
// since we explicitly setting the date