~ubuntu-branches/ubuntu/saucy/digikam/saucy

« back to all changes in this revision

Viewing changes to digikam/kdateedit.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Scott Kitterman
  • Date: 2010-12-21 23:19:11 UTC
  • mfrom: (1.2.33 upstream) (3.1.7 experimental)
  • Revision ID: james.westby@ubuntu.com-20101221231911-z9jip7s5aht1jqn9
Tags: 2:1.7.0-1ubuntu1
* Merge from Debian Experimental. Remaining Ubuntu changes:
  - Export .pot name and copy to plugins in debian/rules
  - Version build-depends on kipi-plugins-dev to ensure build is against the
    same version on all archs
* Drop debian/patches/kubuntu_01_linker.diff, incoporated upstream
* Remove patches directory and unused patches

Show diffs side-by-side

added added

removed removed

Lines of Context:
49
49
 
50
50
class DateValidator : public QValidator
51
51
{
52
 
    public:
53
 
 
54
 
        DateValidator( const QStringList& keywords, QWidget* parent )
55
 
            : QValidator( parent ), mKeywords( keywords )
56
 
        {}
57
 
 
58
 
        virtual State validate( QString& str, int& ) const
59
 
        {
60
 
            int length = str.length();
61
 
 
62
 
            // empty string is intermediate so one can clear the edit line and start from scratch
63
 
            if ( length <= 0 )
64
 
                return Intermediate;
65
 
 
66
 
            if ( mKeywords.contains( str.toLower() ) )
67
 
                return Acceptable;
68
 
 
69
 
            bool ok = false;
70
 
            KGlobal::locale()->readDate( str, &ok );
71
 
            if ( ok )
72
 
                return Acceptable;
73
 
            else
74
 
                return Intermediate;
75
 
        }
76
 
 
77
 
    private:
78
 
 
79
 
        QStringList mKeywords;
 
52
public:
 
53
 
 
54
    DateValidator( const QStringList& keywords, QWidget* parent )
 
55
        : QValidator( parent ), mKeywords( keywords )
 
56
    {}
 
57
 
 
58
    virtual State validate( QString& str, int& ) const
 
59
    {
 
60
        int length = str.length();
 
61
 
 
62
        // empty string is intermediate so one can clear the edit line and start from scratch
 
63
        if ( length <= 0 )
 
64
        {
 
65
            return Intermediate;
 
66
        }
 
67
 
 
68
        if ( mKeywords.contains( str.toLower() ) )
 
69
        {
 
70
            return Acceptable;
 
71
        }
 
72
 
 
73
        bool ok = false;
 
74
        KGlobal::locale()->readDate( str, &ok );
 
75
 
 
76
        if ( ok )
 
77
        {
 
78
            return Acceptable;
 
79
        }
 
80
        else
 
81
        {
 
82
            return Intermediate;
 
83
        }
 
84
    }
 
85
 
 
86
private:
 
87
 
 
88
    QStringList mKeywords;
80
89
};
81
90
 
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)
84
93
{
85
94
    setObjectName(name);
86
95
    // need at least one entry for popup to work
95
104
    setMinimumSize( sizeHint() );
96
105
 
97
106
    connect( lineEdit(), SIGNAL( returnPressed() ),
98
 
            this, SLOT( lineEnterPressed() ) );
 
107
             this, SLOT( lineEnterPressed() ) );
99
108
 
100
109
    connect( this, SIGNAL( textChanged( const QString& ) ),
101
 
            SLOT( slotTextChanged( const QString& ) ) );
 
110
             SLOT( slotTextChanged( const QString& ) ) );
102
111
 
103
112
    mPopup = new KDatePickerPopup( KDatePickerPopup::DatePicker | KDatePickerPopup::Words );
104
113
    mPopup->hide();
147
156
void KDateEdit::showPopup()
148
157
{
149
158
    if ( mReadOnly )
 
159
    {
150
160
        return;
 
161
    }
151
162
 
152
163
    QRect desk        = KGlobalSettings::desktopGeometry( this );
153
164
    QPoint popupPoint = mapToGlobal( QPoint( 0,0 ) );
154
165
 
155
166
    int dateFrameHeight = mPopup->sizeHint().height();
 
167
 
156
168
    if ( popupPoint.y() + height() + dateFrameHeight > desk.bottom() )
 
169
    {
157
170
        popupPoint.setY( popupPoint.y() - dateFrameHeight );
 
171
    }
158
172
    else
 
173
    {
159
174
        popupPoint.setY( popupPoint.y() + height() );
 
175
    }
160
176
 
161
177
    int dateFrameWidth = mPopup->sizeHint().width();
 
178
 
162
179
    if ( popupPoint.x() + dateFrameWidth > desk.right() )
 
180
    {
163
181
        popupPoint.setX( desk.right() - dateFrameWidth );
 
182
    }
164
183
 
165
184
    if ( popupPoint.x() < desk.left() )
 
185
    {
166
186
        popupPoint.setX( desk.left() );
 
187
    }
167
188
 
168
189
    if ( popupPoint.y() < desk.top() )
 
190
    {
169
191
        popupPoint.setY( desk.top() );
 
192
    }
170
193
 
171
194
    if ( mDate.isValid() )
 
195
    {
172
196
        mPopup->setDate( mDate );
 
197
    }
173
198
    else
 
199
    {
174
200
        mPopup->setDate( QDate::currentDate() );
 
201
    }
175
202
 
176
203
    mPopup->popup( popupPoint );
177
204
 
182
209
    assignDate( date );
183
210
    updateView();
184
211
    // Now, simulate an Enter to unpress it
185
 
    QAbstractItemView *lb = view();
 
212
    QAbstractItemView* lb = view();
 
213
 
186
214
    if (lb)
187
215
    {
188
216
        lb->setCurrentIndex( lb->model()->index( 0, 0 ) );
223
251
    if (assignDate( date ) )
224
252
    {
225
253
        if ( replaced )
226
 
        updateView();
 
254
        {
 
255
            updateView();
 
256
        }
227
257
 
228
258
        emit dateChanged( date );
229
259
    }
230
260
}
231
261
 
232
 
QDate KDateEdit::parseDate( bool *replaced ) const
 
262
QDate KDateEdit::parseDate( bool* replaced ) const
233
263
{
234
264
    QString text = currentText();
235
265
    QDate result;
236
266
 
237
267
    if ( replaced )
 
268
    {
238
269
        (*replaced) = false;
 
270
    }
239
271
 
240
272
    if ( text.isEmpty() )
 
273
    {
241
274
        result = QDate();
 
275
    }
242
276
    else if ( mKeywordMap.contains( text.toLower() ) )
243
277
    {
244
278
        QDate today = QDate::currentDate();
245
279
        int i = mKeywordMap[ text.toLower() ];
 
280
 
246
281
        if ( i >= 100 )
247
282
        {
248
283
            /* A day name has been entered. Convert to offset from today.
255
290
            */
256
291
            i -= 100;
257
292
            int currentDay = today.dayOfWeek();
 
293
 
258
294
            if ( i >= currentDay )
 
295
            {
259
296
                i -= currentDay;
 
297
            }
260
298
            else
 
299
            {
261
300
                i += 7 - currentDay;
 
301
            }
262
302
        }
263
303
 
264
304
        result = today.addDays( i );
 
305
 
265
306
        if ( replaced )
266
 
        (*replaced) = true;
 
307
        {
 
308
            (*replaced) = true;
 
309
        }
267
310
    }
268
311
    else
269
312
    {
273
316
    return result;
274
317
}
275
318
 
276
 
bool KDateEdit::eventFilter( QObject *object, QEvent *event )
 
319
bool KDateEdit::eventFilter( QObject* object, QEvent* event )
277
320
{
278
 
    if ( object == lineEdit() ) 
 
321
    if ( object == lineEdit() )
279
322
    {
280
323
        // We only process the focus out event if the text has changed
281
324
        // since we got focus
296
339
            }
297
340
 
298
341
            int step = 0;
 
342
 
299
343
            if ( keyEvent->key() == Qt::Key_Up )
 
344
            {
300
345
                step = 1;
 
346
            }
301
347
            else if ( keyEvent->key() == Qt::Key_Down )
 
348
            {
302
349
                step = -1;
 
350
            }
 
351
 
303
352
            if ( step && !mReadOnly )
304
353
            {
305
354
                QDate date = parseDate();
 
355
 
306
356
                if ( date.isValid() )
307
357
                {
308
358
                    date = date.addDays( step );
 
359
 
309
360
                    if ( assignDate( date ) )
310
361
                    {
311
362
                        updateView();
319
370
    else
320
371
    {
321
372
        // It's a date picker event
322
 
        switch ( event->type() ) 
 
373
        switch ( event->type() )
323
374
        {
324
375
            case QEvent::MouseButtonDblClick:
325
 
            case QEvent::MouseButtonPress: 
 
376
            case QEvent::MouseButtonPress:
326
377
            {
327
 
                QMouseEvent *mouseEvent = (QMouseEvent*)event;
328
 
                if ( !mPopup->rect().contains( mouseEvent->pos() ) ) 
 
378
                QMouseEvent* mouseEvent = (QMouseEvent*)event;
 
379
 
 
380
                if ( !mPopup->rect().contains( mouseEvent->pos() ) )
329
381
                {
330
382
                    QPoint globalPos = mPopup->mapToGlobal( mouseEvent->pos() );
331
 
                    if ( QApplication::widgetAt( globalPos ) == this ) 
 
383
 
 
384
                    if ( QApplication::widgetAt( globalPos ) == this )
332
385
                    {
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;
336
389
                    }
337
390
                }
 
391
 
338
392
                break;
339
393
            }
340
394
            default:
345
399
    return false;
346
400
}
347
401
 
348
 
void KDateEdit::mousePressEvent( QMouseEvent *event )
 
402
void KDateEdit::mousePressEvent( QMouseEvent* event )
349
403
{
350
404
    if ( event->button() == Qt::LeftButton && mDiscardNextMousePress )
351
405
    {
361
415
    QDate date = parseDate();
362
416
 
363
417
    if ( assignDate( date ) )
 
418
    {
364
419
        emit dateChanged( date );
 
420
    }
365
421
 
366
422
    mTextChanged = true;
367
423
}
375
431
    mKeywordMap.insert( i18n( "yesterday" ), -1 );
376
432
 
377
433
    QString dayName;
 
434
 
378
435
    for ( int i = 1; i <= 7; ++i )
379
436
    {
380
437
        dayName = KGlobal::locale()->calendar()->weekDayName( i ).toLower();
392
449
void KDateEdit::updateView()
393
450
{
394
451
    QString dateString;
 
452
 
395
453
    if ( mDate.isValid() )
 
454
    {
396
455
        dateString = KGlobal::locale()->formatDate( mDate, KLocale::ShortDate );
 
456
    }
397
457
 
398
458
    // We do not want to generate a signal here,
399
459
    // since we explicitly setting the date