summaryrefslogtreecommitdiffabout
path: root/korganizer/ktimeedit.cpp
authorzautrix <zautrix>2005-01-16 08:49:00 (UTC)
committer zautrix <zautrix>2005-01-16 08:49:00 (UTC)
commit62ff81d5d292ddf3c6032b48f27a6daedf6a6cb0 (patch) (unidiff)
treeef89d1577b47959c172e644701bf9dc7541c2987 /korganizer/ktimeedit.cpp
parent32795d059fa22cc9fd213db4b4d87e30866a7cb9 (diff)
downloadkdepimpi-62ff81d5d292ddf3c6032b48f27a6daedf6a6cb0.zip
kdepimpi-62ff81d5d292ddf3c6032b48f27a6daedf6a6cb0.tar.gz
kdepimpi-62ff81d5d292ddf3c6032b48f27a6daedf6a6cb0.tar.bz2
beter time edit
Diffstat (limited to 'korganizer/ktimeedit.cpp') (more/less context) (show whitespace changes)
-rw-r--r--korganizer/ktimeedit.cpp82
1 files changed, 14 insertions, 68 deletions
diff --git a/korganizer/ktimeedit.cpp b/korganizer/ktimeedit.cpp
index f5a1c50..5222ac9 100644
--- a/korganizer/ktimeedit.cpp
+++ b/korganizer/ktimeedit.cpp
@@ -20,77 +20,39 @@
20 with any edition of Qt, and distribute the resulting executable, 20 with any edition of Qt, and distribute the resulting executable,
21 without including the source code for Qt in the source distribution. 21 without including the source code for Qt in the source distribution.
22*/ 22*/
23 23
24#include <qkeycode.h> 24#include <qkeycode.h>
25#include <qcombobox.h> 25#include <qcombobox.h>
26#include <qdatetime.h> 26#include <qdatetime.h>
27#include <qlineedit.h> 27#include <qlineedit.h>
28#include <qlistbox.h>
28#include <qapplication.h> 29#include <qapplication.h>
29 30
30#include <kmessagebox.h> 31#include <kmessagebox.h>
31#include <kglobal.h> 32#include <kglobal.h>
32#include <kdebug.h> 33#include <kdebug.h>
33#include <klocale.h> 34#include <klocale.h>
34#include <kpimglobalprefs.h> 35#include <kpimglobalprefs.h>
35 36
36#include "ktimeedit.h" 37#include "ktimeedit.h"
37#include "koprefs.h" 38#include "koprefs.h"
38#include <qvalidator.h> 39#include <qvalidator.h>
39 40
40// Validator for a time value with only hours and minutes (no seconds) 41// Validator for a time value with only hours and minutes (no seconds)
41// Mostly locale aware. Author: David Faure <faure@kde.org> 42// Mostly locale aware. Author: David Faure <faure@kde.org>
42class KOTimeValidator : public QValidator
43{
44public:
45 KOTimeValidator(QWidget* parent, const char* name=0) : QValidator(parent, name) {}
46
47 virtual State validate(QString& str, int& /*cursorPos*/) const
48 {
49 return Acceptable;
50 bool ok = false;
51 // TODO use KLocale::WithoutSeconds in HEAD
52 /*QTime time =*/ KGlobal::locale()->readTime(str, &ok);
53 if ( ok )
54 return Acceptable;
55 // readTime doesn't help knowing when the string is "Intermediate".
56 int length = str.length();
57 if ( !str ) // empty string?
58 return Invalid; // there should always be a ':' in it, right?
59 // HACK. Not fully locale aware etc. (esp. the separator is '.' in sv_SE...)
60 QChar sep = ':';
61 // I want to allow "HH:", ":MM" and ":" to make editing easier
62 if ( str[0] == sep )
63 {
64 if ( length == 1 ) // just ":"
65 return Intermediate;
66 QString minutes = str.mid(1);
67 int m = minutes.toInt(&ok);
68 if ( ok && m >= 0 && m < 60 )
69 return Intermediate;
70 } else if ( str.at(str.length()-1) == sep )
71 {
72 QString hours = str.left(length-1);
73 int h = hours.toInt(&ok);
74 if ( ok && h >= 0 && h < 24 )
75 return Intermediate;
76 }
77 return Invalid;
78 }
79};
80 43
81// KTimeWidget/QTimeEdit provide nicer editing, but don't provide a combobox. 44// KTimeWidget/QTimeEdit provide nicer editing, but don't provide a combobox.
82// Difficult to get all in one... 45// Difficult to get all in one...
83// But Qt-3.2 will offer QLineEdit::setMask, so a "99:99" mask would help. 46// But Qt-3.2 will offer QLineEdit::setMask, so a "99:99" mask would help.
84KOTimeEdit::KOTimeEdit(QWidget *parent, QTime qt, const char *name) 47KOTimeEdit::KOTimeEdit(QWidget *parent, QTime qt, const char *name)
85 : QComboBox(TRUE, parent, name) 48 : QComboBox(TRUE, parent, name)
86{ 49{
87 setInsertionPolicy(NoInsertion); 50 setInsertionPolicy(NoInsertion);
88 setValidator( new KOTimeValidator( this ) );
89 mFlagKeyPressed = false; 51 mFlagKeyPressed = false;
90 52
91 if ( QApplication::desktop()->width() < 650 ) 53 if ( QApplication::desktop()->width() < 650 )
92 setSizeLimit ( 6 ); 54 setSizeLimit ( 6 );
93 mTime = qt; 55 mTime = qt;
94 56
95// mNoTimeString = i18n("No Time"); 57// mNoTimeString = i18n("No Time");
96// insertItem( mNoTimeString ); 58// insertItem( mNoTimeString );
@@ -140,38 +102,32 @@ bool KOTimeEdit::hasTime() const
140 if ( currentText().isEmpty() ) return false; 102 if ( currentText().isEmpty() ) return false;
141 //if ( currentText() == mNoTimeString ) return false; 103 //if ( currentText() == mNoTimeString ) return false;
142 104
143 return true; // always 105 return true; // always
144} 106}
145 107
146QTime KOTimeEdit::getTime() const 108QTime KOTimeEdit::getTime() const
147{ 109{
148 //kdDebug(5850) << "KOTimeEdit::getTime(), currentText() = " << currentText() << endl; 110 return KGlobal::locale()->readTime(currentText());
149 // TODO use KLocale::WithoutSeconds in HEAD
150 QTime time = KGlobal::locale()->readTime(currentText());
151 // kdDebug(5850) << "KOTimeEdit::getTime(): " << time.toString() << endl;
152 return time;
153} 111}
154/* 112/*
155QSizePolicy KOTimeEdit::sizePolicy() const 113QSizePolicy KOTimeEdit::sizePolicy() const
156{ 114{
157 // Set size policy to Fixed, because edit cannot contain more text than the 115 // Set size policy to Fixed, because edit cannot contain more text than the
158 // string representing the time. It doesn't make sense to provide more space. 116 // string representing the time. It doesn't make sense to provide more space.
159 QSizePolicy sizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed); 117 QSizePolicy sizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
160 118
161 return sizePolicy; 119 return sizePolicy;
162} 120}
163*/ 121*/
164void KOTimeEdit::setTime(QTime newTime) 122void KOTimeEdit::setTime(QTime newTime)
165{ 123{
166 if ( mTime != newTime ) 124 if ( mTime != newTime )
167 { 125 {
168 kdDebug(5850) << "KOTimeEdit::setTime(): " << newTime.toString() << endl;
169
170 mTime = newTime; 126 mTime = newTime;
171 updateText(); 127 updateText();
172 } 128 }
173 129
174} 130}
175 131
176void KOTimeEdit::activ(int i) 132void KOTimeEdit::activ(int i)
177{ 133{
@@ -374,26 +330,28 @@ void KOTimeEdit::keyPressEvent(QKeyEvent *e)
374 // rest 330 // rest
375 case Key_Prior: 331 case Key_Prior:
376 subTime(QTime(1,0,0)); 332 subTime(QTime(1,0,0));
377 break; 333 break;
378 case Key_Next: 334 case Key_Next:
379 addTime(QTime(1,0,0)); 335 addTime(QTime(1,0,0));
380 break; 336 break;
381 case Key_Backspace: 337 case Key_Backspace:
338 qDebug("+++++++++++back ");
382 if ( cpos > 0) { 339 if ( cpos > 0) {
383 if ( cpos == 3 ) 340 if ( cpos == 3 )
384 --cpos; 341 --cpos;
385 if ( cpos > 5) 342 if ( cpos > 5)
386 cpos = 5; 343 cpos = 5;
387 text.at( cpos-1 ) = '0'; 344 text.at( cpos-1 ) = '0';
388 lineEdit()->setText( text ); 345 lineEdit()->setText( text );
389 lineEdit()->setCursorPosition(--cpos); 346 lineEdit()->setCursorPosition(--cpos);
390 setSelect ( cpos , 1 ); 347 setSelect ( cpos , 1 );
391 changedText(); 348 changedText();
349 qDebug("---------back ");
392 } 350 }
393 break; 351 break;
394 } // switch arrows 352 } // switch arrows
395 353
396 // if cursor at string end, alltext market and keyEvent don't ArrowLeft -> deselect and cpos 354 // if cursor at string end, alltext market and keyEvent don't ArrowLeft -> deselect and cpos
397 if( cpos > 4 && lineEdit()->markedText().length() == 5 && e->key() != Key_Left ) { 355 if( cpos > 4 && lineEdit()->markedText().length() == 5 && e->key() != Key_Left ) {
398 lineEdit()->deselect(); 356 lineEdit()->deselect();
399 cpos = 0; 357 cpos = 0;
@@ -481,48 +439,36 @@ void KOTimeEdit::keyPressEvent(QKeyEvent *e)
481 } 439 }
482 } 440 }
483 441
484 442
485} 443}
486 444
487void KOTimeEdit::updateText() 445void KOTimeEdit::updateText()
488{ 446{
489// kdDebug(5850) << "KOTimeEdit::updateText() " << endl 447 listBox()->blockSignals( true );
448 blockSignals( true );
490 QString s = KGlobal::locale()->formatTime(mTime); 449 QString s = KGlobal::locale()->formatTime(mTime);
491 // Set the text but without emitting signals, nor losing the cursor position 450 // Set the text but without emitting signals, nor losing the cursor position
492 QLineEdit *line = lineEdit(); 451 QLineEdit *line = lineEdit();
493 line->blockSignals(true); 452 line->blockSignals(true);
494 int pos = line->cursorPosition(); 453 int pos = line->cursorPosition();
495 // qDebug(" settext *%s* ", s.latin1());
496 line->setText(s);
497 // line->setCursorPosition(pos);
498// line->blockSignals(false);
499
500// kdDebug(5850) << "KOTimeEdit::updateText(): " << s << endl;
501
502 if (!mTime.minute() % 15) {
503 setCurrentItem((mTime.hour()*4)+(mTime.minute()/15)); 454 setCurrentItem((mTime.hour()*4)+(mTime.minute()/15));
504 } 455 line->setText(s);
505 line->setCursorPosition(pos); 456 line->setCursorPosition(pos);
506 line->blockSignals(false); 457 line->blockSignals(false);
458 blockSignals( false );
459 listBox()->blockSignals( false );
507 460
508} 461}
509 462
510bool KOTimeEdit::inputIsValid() const
511{
512 int cursorPos = lineEdit()->cursorPosition();
513 QString str = currentText();
514 return validator()->validate( str, cursorPos ) == QValidator::Acceptable;
515}
516
517void KOTimeEdit::changedText() 463void KOTimeEdit::changedText()
518{ 464{
519 //kdDebug(5850) << "KOTimeEdit::changedText()" << endl;
520 if ( inputIsValid() )
521 {
522 int pos = lineEdit()->cursorPosition(); 465 int pos = lineEdit()->cursorPosition();
523 mTime = getTime(); 466 mTime = getTime();
524 // updateText(); 467 blockSignals( true );
468 QString text = lineEdit()->text();
469 setCurrentItem((mTime.hour()*4)+(mTime.minute()/15));
470 lineEdit()->setText(text);
471 blockSignals( false );
525 emit timeChanged(mTime); 472 emit timeChanged(mTime);
526 lineEdit()->setCursorPosition(pos); 473 lineEdit()->setCursorPosition(pos);
527 } 474 }
528}