summaryrefslogtreecommitdiffstats
path: root/libtdepim/kdateedit.cpp
blob: 392e53eeeff40e29dd5dfbe6957b04e535572313 (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
/*
    This file is part of libtdepim.

    Copyright (c) 2002 Cornelius Schumacher <schumacher@kde.org>
    Copyright (c) 2002 David Jarvie <software@astrojar.org.uk>
    Copyright (c) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
    Copyright (c) 2004 Tobias Koenig <tokoe@kde.org>

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

    This library 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
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#include <tqapplication.h>
#include <tqlineedit.h>
#include <tqlistbox.h>
#include <tqvalidator.h>

#include <kcalendarsystem.h>
#include <tdeglobal.h>
#include <tdeglobalsettings.h>
#include <tdelocale.h>

#include "kdateedit.h"

class DateValidator : public TQValidator
{
  public:
    DateValidator( const TQStringList &keywords, TQWidget* parent, const char* name = 0 )
      : TQValidator( TQT_TQOBJECT(parent), name ), mKeywords( keywords )
    {}

    virtual State validate( TQString &str, int& ) const
    {
      int length = str.length();

      // empty string is intermediate so one can clear the edit line and start from scratch
      if ( length <= 0 )
        return Intermediate;

      if ( mKeywords.contains( str.lower() ) )
        return Acceptable;

      bool ok = false;
      TDEGlobal::locale()->readDate( str, &ok );
      if ( ok )
        return Acceptable;
      else
        return Intermediate;
    }

  private:
    TQStringList mKeywords;
};

KDateEdit::KDateEdit( TQWidget *parent, const char *name )
  : TQComboBox( true, parent, name ),
    mReadOnly( false ),
    mDiscardNextMousePress( false )
{
  // need at least one entry for popup to work
  setMaxCount( 1 );

  mDate = TQDate::currentDate();
  TQString today = TDEGlobal::locale()->formatDate( mDate, true );

  insertItem( today );
  setCurrentItem( 0 );
  changeItem( today, 0 );
  setMinimumSize( sizeHint() );

  connect( lineEdit(), TQT_SIGNAL( returnPressed() ),
           this, TQT_SLOT( lineEnterPressed() ) );
  connect( this, TQT_SIGNAL( textChanged( const TQString& ) ),
           TQT_SLOT( slotTextChanged( const TQString& ) ) );

  mPopup = new KDatePickerPopup( KDatePickerPopup::DatePicker | KDatePickerPopup::Words );
  mPopup->hide();
  mPopup->installEventFilter( this );

  connect( mPopup, TQT_SIGNAL( dateChanged( TQDate ) ),
           TQT_SLOT( dateSelected( TQDate ) ) );

  // handle keyword entry
  setupKeywords();
  lineEdit()->installEventFilter( this );

  setValidator( new DateValidator( mKeywordMap.keys(), this ) );

  mTextChanged = false;
}

KDateEdit::~KDateEdit()
{
  delete mPopup;
  mPopup = 0;
}

void KDateEdit::setDate( const TQDate& date )
{
  assignDate( date );
  updateView();
}

TQDate KDateEdit::date() const
{
  return mDate;
}

void KDateEdit::setReadOnly( bool readOnly )
{
  mReadOnly = readOnly;
  lineEdit()->setReadOnly( readOnly );
}

bool KDateEdit::isReadOnly() const
{
  return mReadOnly;
}

void KDateEdit::popup()
{
  if ( mReadOnly )
    return;

  TQRect desk = TDEGlobalSettings::desktopGeometry( this );

  TQPoint popupPoint = mapToGlobal( TQPoint( 0,0 ) );

  int dateFrameHeight = mPopup->sizeHint().height();
  if ( popupPoint.y() + height() + dateFrameHeight > desk.bottom() )
    popupPoint.setY( popupPoint.y() - dateFrameHeight );
  else
    popupPoint.setY( popupPoint.y() + height() );

  int dateFrameWidth = mPopup->sizeHint().width();
  if ( popupPoint.x() + dateFrameWidth > desk.right() )
    popupPoint.setX( desk.right() - dateFrameWidth );

  if ( popupPoint.x() < desk.left() )
    popupPoint.setX( desk.left() );

  if ( popupPoint.y() < desk.top() )
    popupPoint.setY( desk.top() );

  if ( mDate.isValid() )
    mPopup->setDate( mDate );
  else
    mPopup->setDate( TQDate::currentDate() );

  mPopup->popup( popupPoint );

  // The combo box is now shown pressed. Make it show not pressed again
  // by causing its (invisible) list box to emit a 'selected' signal.
  // First, ensure that the list box contains the date currently displayed.
  TQDate date = parseDate();
  assignDate( date );
  updateView();
  // Now, simulate an Enter to unpress it
  TQListBox *lb = listBox();
  if (lb) {
    lb->setCurrentItem(0);
    TQKeyEvent* keyEvent = new TQKeyEvent(TQEvent::KeyPress, TQt::Key_Enter, 0, 0);
    TQApplication::postEvent(lb, keyEvent);
  }
}

void KDateEdit::dateSelected( TQDate date )
{
  if (assignDate( date ) ) {
    updateView();
    emit dateChanged( date );
    emit dateEntered( date );

    if ( date.isValid() ) {
      mPopup->hide();
    }
  }
}

void KDateEdit::lineEnterPressed()
{
  bool replaced = false;

  TQDate date = parseDate( &replaced );

  if (assignDate( date ) ) {
    if ( replaced )
      updateView();

    emit dateChanged( date );
    emit dateEntered( date );
  }
}

TQDate KDateEdit::parseDate( bool *replaced ) const
{
  TQString text = currentText();
  TQDate result;

  if ( replaced )
    (*replaced) = false;

  if ( text.isEmpty() )
    result = TQDate();
  else if ( mKeywordMap.contains( text.lower() ) ) {
    TQDate today = TQDate::currentDate();
    int i = mKeywordMap[ text.lower() ];
    if ( i >= 100 ) {
      /* A day name has been entered. Convert to offset from today.
       * This 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 occurred 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).
       */
      i -= 100;
      int currentDay = today.dayOfWeek();
      if ( i >= currentDay )
        i -= currentDay;
      else
        i += 7 - currentDay;
    }

    result = today.addDays( i );
    if ( replaced )
      (*replaced) = true;
  } else {
    result = TDEGlobal::locale()->readDate( text );
  }

  return result;
}

bool KDateEdit::eventFilter( TQObject *object, TQEvent *event )
{
  if ( TQT_BASE_OBJECT(object) == TQT_BASE_OBJECT(lineEdit()) ) {
    // We only process the focus out event if the text has changed
    // since we got focus
    if ( (event->type() == TQEvent::FocusOut) && mTextChanged ) {
      lineEnterPressed();
      mTextChanged = false;
    } else if ( event->type() == TQEvent::KeyPress ) {
      // Up and down arrow keys step the date
      TQKeyEvent* keyEvent = (TQKeyEvent*)event;

      if ( keyEvent->key() == TQt::Key_Return ) {
        lineEnterPressed();
        return true;
      }

      int step = 0;
      if ( keyEvent->key() == TQt::Key_Up )
        step = 1;
      else if ( keyEvent->key() == TQt::Key_Down )
        step = -1;
      // TODO: If it's not an input key, but something like Return, Enter, Tab, etc..., don't eat the keypress, but handle it through to the default eventfilter!
      if ( step && !mReadOnly ) {
        TQDate date = parseDate();
        if ( date.isValid() ) {
          date = date.addDays( step );
          if ( assignDate( date ) ) {
            updateView();
            emit dateChanged( date );
            emit dateEntered( date );
            return true;
          }
        }
      }
    }
  } else {
    // It's a date picker event
    switch ( event->type() ) {
      case TQEvent::MouseButtonDblClick:
      case TQEvent::MouseButtonPress: {
        TQMouseEvent *mouseEvent = (TQMouseEvent*)event;
        if ( !mPopup->rect().contains( mouseEvent->pos() ) ) {
          TQPoint globalPos = mPopup->mapToGlobal( mouseEvent->pos() );
          if ( TQApplication::widgetAt( globalPos, true ) == this ) {
            // The date picker is being closed by a click on the
            // KDateEdit widget. Avoid popping it up again immediately.
            mDiscardNextMousePress = true;
          }
        }

        break;
      }
      default:
        break;
    }
  }

  return false;
}

void KDateEdit::mousePressEvent( TQMouseEvent *event )
{
  if ( event->button() == Qt::LeftButton && mDiscardNextMousePress ) {
    mDiscardNextMousePress = false;
    return;
  }

  TQComboBox::mousePressEvent( event );
}

void KDateEdit::slotTextChanged( const TQString& )
{
  TQDate date = parseDate();

  if ( assignDate( date ) )
    emit dateChanged( date );

  mTextChanged = true;
}

void KDateEdit::setupKeywords()
{
  // Create the keyword list. This will be used to match against when the user
  // enters information.
  mKeywordMap.insert( i18n( "tomorrow" ), 1 );
  mKeywordMap.insert( i18n( "today" ), 0 );
  mKeywordMap.insert( i18n( "yesterday" ), -1 );

  TQString dayName;
  for ( int i = 1; i <= 7; ++i ) {
    dayName = TDEGlobal::locale()->calendar()->weekDayName( i ).lower();
    mKeywordMap.insert( dayName, i + 100 );
  }
}

bool KDateEdit::assignDate( const TQDate& date )
{
  mDate = date;
  mTextChanged = false;
  return true;
}

void KDateEdit::updateView()
{
  TQString dateString;
  if ( mDate.isValid() )
    dateString = TDEGlobal::locale()->formatDate( mDate, true );

  // We do not want to generate a signal here,
  // since we explicitly setting the date
  bool blocked = signalsBlocked();
  blockSignals( true );
  changeItem( dateString, 0 );
  blockSignals( blocked );
}

#include "kdateedit.moc"