summaryrefslogtreecommitdiffabout
path: root/libkdepim/kdateedit.cpp
blob: bf384791a516159d05c3cffd4bf2c9001fbc0521 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
/*
    This file is part of libkdepim.

    Copyright (c) 2002 Cornelius Schumacher <schumacher@kde.org>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

    As a special exception, permission is given to link this program
    with any edition of Qt, and distribute the resulting executable,
    without including the source code for Qt in the source distribution.
*/

#include <qapplication.h>
#include <qevent.h>
#include <qlineedit.h>
#include <qpixmap.h>
#include <qpushbutton.h>

#include <kdatepicker.h>
#include <kdebug.h>
#include <kglobal.h>
#include <kiconloader.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <knotifyclient.h>
#include <qpalette.h>

#include "kdateedit.h"
//#include "kdateedit.moc"

KDateEdit::KDateEdit(QWidget *parent, const char *name, bool withoutDP )
  : QHBox(parent, name)
{
  dateFormShort = true;
  withoutDp = withoutDP;
  mDateEdit = new QLineEdit(this);
  mDateEdit->setText(KGlobal::locale()->formatDate(QDate::currentDate(),dateFormShort));
  setFocusProxy(mDateEdit);
  mDateEdit->installEventFilter(this);

  // Highlight Background and Textcolor
  QPalette palette = QWidget::palette();
  unsigned char red, green, blue;
  red = palette.color( QPalette::Normal , QColorGroup::Background ).red() - 10;
  green = palette.color( QPalette::Normal , QColorGroup::Background ).green() - 10;
  blue = palette.color( QPalette::Normal , QColorGroup::Background ).blue() - 10;
  palette.setColor( QColorGroup::Highlight, QColor(red,green,blue) );
  palette.setColor( QColorGroup::HighlightedText, palette.color( QPalette::Normal , QColorGroup::Foreground ) );
  mDateEdit->setPalette( palette );

  if ( withoutDP ) {
      mDateFrame = 0;
      mDateButton = 0;
      mDatePicker = 0;
  } else {
      QPixmap pixmap = SmallIcon("smallcal");
      mDateButton = new QPushButton(this);
      mDateButton->setPixmap(pixmap);
      QPixmap pixmap2 = SmallIcon("today_small");
      QPushButton*  nowButton = new QPushButton(this);
      nowButton->setPixmap(pixmap2);

      mDateFrame = new QVBox(0,0,WType_Popup);
      // mDateFrame->setFrameStyle(QFrame::PopupPanel | QFrame::Raised);
      mDateFrame->setFrameStyle( QFrame::WinPanel |QFrame::Raised );
      mDateFrame->setLineWidth(3);
      mDateFrame->hide();

      mDatePicker = new KDatePicker(mDateFrame,QDate::currentDate());
      connect(mDatePicker,SIGNAL(dateEntered(QDate)),SLOT(setDate(QDate)));
      connect(mDatePicker,SIGNAL(dateEntered(QDate)),SIGNAL(dateChanged(QDate)));
      connect(mDatePicker,SIGNAL(dateSelected(QDate)),SLOT(setDate(QDate)));
      connect(mDatePicker,SIGNAL(dateSelected(QDate)),SIGNAL(dateChanged(QDate)));
      connect(mDatePicker,SIGNAL(dateSelected(QDate)),mDateFrame,SLOT(hide()));
      connect(mDateButton,SIGNAL(clicked()),SLOT(toggleDatePicker()));
      connect(nowButton,SIGNAL(clicked()),SLOT(goToNow()));
      mDateButton->setFocusPolicy( QWidget::NoFocus ); 
      mDateButton->setAutoDefault( false ); 
      //mDateFrame->resize( 400, 300 );
      
  }
      connect(mDateEdit,SIGNAL(returnPressed()),SLOT(lineEnterPressed()));
      connect(mDateEdit,SIGNAL(textChanged(const QString &)),
              SLOT(textChanged(const QString &)));

  // Create the keyword list. This will be used to match against when the user
  // enters information.
  mKeywordMap[i18n("tomorrow")] = 1;
  mKeywordMap[i18n("today")] = 0;
  mKeywordMap[i18n("yesterday")] = -1;

  /*
   * This loop uses some math tricks to figure out the offset in days
   * to the next date the given day of the week occurs. There
   * are two cases, that the new day is >= the current day, which means
   * the new day has not occured yet or that the new day < the current day,
   * which means the new day is already passed (so we need to find the
   * day in the next week).
   */
  QString dayName;
  int currentDay = QDate::currentDate().dayOfWeek();
  for (int i = 1; i <= 7; ++i)
  {
    dayName = KGlobal::locale()->weekDayName(i).lower();
    if (i >= currentDay)
      mKeywordMap[dayName] = i - currentDay;
    else
      mKeywordMap[dayName] = 7 - currentDay + i;
  }

  mTextChanged = false;
  mHandleInvalid = false;
  // QWidget::setTabOrder( mDateEdit, mDateButton );
}

KDateEdit::~KDateEdit()
{
  delete mDateFrame;
}
void KDateEdit::clear()
{
  bool b = mDateEdit->signalsBlocked();
  mDateEdit->blockSignals(true);
  mDateEdit->setText("");
  mDateEdit->blockSignals(b);
}
void KDateEdit::goToNow()
{
    setDate(QDate::currentDate() );
    emit setTimeTo( QTime::currentTime() );
}
void KDateEdit::setDate(QDate newDate)
{
  if (!newDate.isValid() && !mHandleInvalid)
    return;
  if ( readDate() ==  newDate )
      return;
  QString dateString = "";
  if(newDate.isValid())
    dateString = KGlobal::locale()->formatDate( newDate, dateFormShort );

  mTextChanged = false;

  // We do not want to generate a signal here, since we explicity setting
  // the date
  bool b = mDateEdit->signalsBlocked();
  mDateEdit->blockSignals(true);
  mDateEdit->setText(dateString);
  mDateEdit->blockSignals(b);
}

void KDateEdit::setDate( QDate date,int *cpos,const int key ,const bool dateFormShort)
{
  QString dateForm = dateFormShort ?
                     KGlobal::locale()->dateFormatShort() :
                     KGlobal::locale()->dateFormat();

  int begin = dateForm.find("%");
  int space = 0;
  int allStrLength = 0;
  int strLength = 0;
  int repeat = 0;

  // witch? Day, Month or Year switch?
  while(1){
    switch ( dateForm.at(begin + 1).latin1() )
    {
    case 'd':// 16 (month day)
      strLength = 2; //Ok
      break;
    case 'm':// 01 (month)
      strLength = 2; //Ok
      break;
    case 'a':// Mon (Weekday)
      strLength = KGlobal::locale()->weekDayName(date.dayOfWeek(), true).length();
      break;
    case 'A':// Monday (Weekday)
      strLength = KGlobal::locale()->weekDayName(date.dayOfWeek(), false).length();
      break;
    case 'b':// Jan (monthName)
      strLength = KGlobal::locale()->monthName(date.month(), true).length();
      break;
    case 'B':// January (monthName)
      strLength = KGlobal::locale()->monthName(date.month(), false).length();
      break;
    case 'y':// 04 (year short)
      strLength = 2; //Ok
      break;
    case 'Y':// 2004 (year)
      strLength = 4; //Ok
      break;
    default:
      break;
    }
    space = begin - (repeat++ * 2);
    // all select? then dayswitch
    if( (mDateEdit->text().length() == mDateEdit->markedText().length() ) &&
                               ( (dateForm.at(begin + 1).latin1() == 'd') ||
                                 (dateForm.at(begin + 1).latin1() == 'a') ||
                                 (dateForm.at(begin + 1).latin1() == 'A') ) ) {
      break;
    }
    // mDateEdit-StringPos == CursorPosition(cpos) then break and set date
    if( ( (space + allStrLength) <= *cpos && *cpos <= (space + allStrLength + strLength) ) || *cpos < begin ) {
      break;
    }
    allStrLength += strLength;
    begin = dateForm.find("%", begin +1);
  }

  // set date
  switch ( dateForm.at(begin + 1).latin1() ) {
    case 'd':
    case 'a':
    case 'A':
      if(key == Key_Up) {
        setDate( date.addDays( 1 ) );
      }
      else if(key == Key_Down) {
        setDate( date.addDays( -1 ) );
      }
      maxDay = readDate().day();
      break;
    case 'm':
    case 'b':
    case 'B':
      if(key == Key_Up) {
          int year = ((date.month()+1)>12)?date.year()+1:date.year();
          int month = ((date.month()+1)>12)?1:date.month()+1;
          int day = (QDate(year,month,1).daysInMonth()<maxDay)?QDate(year,month,1).daysInMonth():maxDay;
          setDate( QDate( year, month, day ) );
      } else if(key == Key_Down) {
          int year = ((date.month()-1)<1)?date.year()-1:date.year();
          int month = ((date.month()-1)<1)?12:date.month()-1;
          int day = (QDate(year,month,1).daysInMonth()<maxDay)?QDate(year,month,1).daysInMonth():maxDay;
          setDate( QDate( year, month, day ) );
      }
      break;
    case 'y':
    case 'Y':
      if(key == Key_Up) {
        setDate( QDate( date.year() + 1, date.month() , date.day()) );
      }
      else if(key == Key_Down) {
        setDate( QDate( date.year() - 1, date.month() , date.day()) );
      }
      break;
/*    default:
      if(key == Key_Up) {
        setDate( date.addDays( 1 ) );
      } else if(key == Key_Down) {
        setDate( date.addDays( -1 ) );
      }
      break;*/
  }

  date = readDate();
  begin = dateForm.find("%");
  int allSelectStrLength = 0;
  int selectStrLength = 0;

  // set selection do new date an set cursor at end of selection
  for(int i = 0; i < repeat; i++){
    switch ( dateForm.at(begin + 1).latin1() )
    {
    case 'd':// 16 (month day)
      selectStrLength = 2; //Ok
      break;
    case 'm':// 01 (month)
      selectStrLength = 2; //Ok
      break;
    case 'a':// Mon (Weekday short)
      selectStrLength = KGlobal::locale()->weekDayName(date.dayOfWeek(), true).length();
      break;
    case 'A':// Monday (Weekday)
      selectStrLength = KGlobal::locale()->weekDayName(date.dayOfWeek(), false).length();
      break;
    case 'b':// Jan (monthName short)
      selectStrLength = KGlobal::locale()->monthName(date.month(), true).length();
      break;
    case 'B':// January (monthName)
      selectStrLength = KGlobal::locale()->monthName(date.month(), false).length();
      break;
    case 'y':// 04 (year short)
      selectStrLength = 2; //Ok
      break;
    case 'Y':// 2004 (year)
      selectStrLength = 4; //Ok
      break;
    default:
      break;
    }
    space = begin - (i * 2);
    allSelectStrLength += selectStrLength;
    begin = dateForm.find("%", begin +1);
  }
  // set selection from begin of date
  setSelect(  space + allSelectStrLength - selectStrLength , selectStrLength);
  *cpos = space + allSelectStrLength;
  emit(dateChanged(date));

  return;
}

void KDateEdit::setHandleInvalid(bool handleInvalid)
{
  mHandleInvalid = handleInvalid;
}

void KDateEdit::setEnabled(bool on)
{
  mDateEdit->setEnabled(on);
  mDateButton->setEnabled(on);
}

QDate KDateEdit::date() const
{
  QDate date = readDate();

  if (date.isValid() || mHandleInvalid) {
    return date;
  } else {
    KNotifyClient::beep();
    return QDate::currentDate();
  }
}

void KDateEdit::keyPressEvent(QKeyEvent *e)
{
  QDate date = readDate();
  int cpos = mDateEdit->cursorPosition();

  switch(e->key())
  {
  case Key_Escape:
      mDateEdit->deselect();
  case Key_Tab:
      QHBox::keyPressEvent(e);
      break;
  case Key_Up:
      // when date invalid then set to currend and return
      if(!date.isValid()) {
        date = QDate::currentDate();
        setDate(date);
        mDateEdit->setCursorPosition(cpos);
        emit(dateChanged(date));
        QString text = i18n( "You entered an invalid date!\n Date changed  to current date." );
        KMessageBox::information( 0, text );
        return;
      }
      setDate(date, &cpos, Key_Up, dateFormShort);
      break;
  case Key_Down:
      // when date invalid then set to current and return
      if(!date.isValid()) {
        date = QDate::currentDate();
        setDate(date);
        mDateEdit->setCursorPosition(cpos);
        emit(dateChanged(date));
        QString text = i18n( "You entered an invalid date!\n Date changed  to current date." );
        KMessageBox::information( 0, text );
        return;
      }
      setDate(date, &cpos, Key_Down, dateFormShort);
      break;
  default:
      QHBox::keyPressEvent(e);
      break;
  } // switch
  mDateEdit->setCursorPosition(cpos);
}

void KDateEdit::setSelect( int from, int to )
{
//    return;
    mDateEdit->setSelection( from , to );
}

void KDateEdit::toggleDatePicker()
{
  if( mDateFrame->isVisible() ) {
    mDateFrame->hide();
  } else {
    QPoint tmpPoint = mapToGlobal(mDateButton->geometry().bottomRight());
    QSize datepickersize = mDatePicker->sizeHint();

    if ( tmpPoint.x() < 7+datepickersize.width() ) tmpPoint.setX( 7+datepickersize.width() );

    int h = QApplication::desktop()->height();

    if ( tmpPoint.y() + datepickersize.height() > h ) tmpPoint.setY( h - datepickersize.height() );

    mDateFrame->setGeometry(tmpPoint.x()-datepickersize.width()-7, tmpPoint.y(),
     datepickersize.width()+2*mDateFrame->lineWidth(), datepickersize.height()+2*mDateFrame->lineWidth());

    QDate date = readDate();
    if(date.isValid()) {
      mDatePicker->setDate(date);
    } else {
      mDatePicker->setDate(QDate::currentDate());
    }
    mDateFrame->show();
  }
}


void KDateEdit::lineEnterPressed()
{
    QDate date = readDate();

    if(date.isValid())
        {
            // Update the edit. This is needed if the user has entered a
            // word rather than the actual date.
            setDate(date);
            emit(dateChanged(date));
            emit returnPressed();
        }
    else
      {
          if ( withoutDp ) {
              KNotifyClient::beep();
          } else {
              if ( !mDateEdit->text().isEmpty() ) {
                  mTextChanged = false;
                  QString text = i18n( "You entered an invalid date!\n Will use current date instead." );
                  if ( isVisible() )
                      if ( KMessageBox::warningContinueCancel( 0, text ) == KMessageBox::Continue ) {
                          setDate( QDate::currentDate() );
                          emit dateChanged( QDate::currentDate() );
                      }
              }
          }
      }
}

bool KDateEdit::inputIsValid()
{
  return readDate().isValid();
}

QDate KDateEdit::readDate() const
{
  QString text = mDateEdit->text();
  QDate date;

  if (mKeywordMap.contains(text.lower()))
  {
    date = QDate::currentDate().addDays(mKeywordMap[text.lower()]);
  }
  else
  {
    date = KGlobal::locale()->readDate(text);
  }

  return date;
}

bool KDateEdit::eventFilter(QObject *, QEvent *e)
{
  // We only process the focus out event if the text has changed
  // since we got focus
  if ((e->type() == QEvent::FocusOut) )
  {
      if ( mTextChanged ) {
          lineEnterPressed();
          mTextChanged = false;
      }
  }
  // switch dateFormShort by double klick with mouse
  else if (e->type() == QEvent::MouseButtonDblClick)
  {
      toggleDateFormat();
  }
  else if (e->type() == QEvent::FocusIn) 
  {
    maxDay = readDate().day();
  }

  return false;
}
void KDateEdit::toggleDateFormat()
{
    dateFormShort = ! dateFormShort;
    mDateEdit->setText(KGlobal::locale()->formatDate(readDate(),dateFormShort));
    
}

void KDateEdit::textChanged(const QString &)
{
  if(mHandleInvalid && mDateEdit->text().stripWhiteSpace().isEmpty()) {
    QDate date; //invalid date
    emit(dateChanged(date));
  } else  {
    mTextChanged = true;
  }
    maxDay = readDate().day();
}