summaryrefslogtreecommitdiffabout
path: root/libkdepim/kdateedit.cpp
authorMichael Krelin <hacker@klever.net>2007-07-04 11:23:42 (UTC)
committer Michael Krelin <hacker@klever.net>2007-07-04 11:23:42 (UTC)
commita08aff328d4393031d5ba7d622c2b05705a89d73 (patch) (unidiff)
tree8ee90d686081c52e7c69b5ce946e9b1a7d690001 /libkdepim/kdateedit.cpp
parent11edc920afe4f274c0964436633aa632c8288a40 (diff)
downloadkdepimpi-p1.zip
kdepimpi-p1.tar.gz
kdepimpi-p1.tar.bz2
initial public commit of qt4 portp1
Diffstat (limited to 'libkdepim/kdateedit.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--libkdepim/kdateedit.cpp40
1 files changed, 22 insertions, 18 deletions
diff --git a/libkdepim/kdateedit.cpp b/libkdepim/kdateedit.cpp
index bf38479..28c88d4 100644
--- a/libkdepim/kdateedit.cpp
+++ b/libkdepim/kdateedit.cpp
@@ -1,510 +1,514 @@
1/* 1/*
2 This file is part of libkdepim. 2 This file is part of libkdepim.
3 3
4 Copyright (c) 2002 Cornelius Schumacher <schumacher@kde.org> 4 Copyright (c) 2002 Cornelius Schumacher <schumacher@kde.org>
5 5
6 This program is free software; you can redistribute it and/or modify 6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by 7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or 8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version. 9 (at your option) any later version.
10 10
11 This program is distributed in the hope that it will be useful, 11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details. 14 GNU General Public License for more details.
15 15
16 You should have received a copy of the GNU General Public License 16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software 17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 19
20 As a special exception, permission is given to link this program 20 As a special exception, permission is given to link this program
21 with any edition of Qt, and distribute the resulting executable, 21 with any edition of Qt, and distribute the resulting executable,
22 without including the source code for Qt in the source distribution. 22 without including the source code for Qt in the source distribution.
23*/ 23*/
24 24
25#include <qapplication.h> 25#include <qapplication.h>
26#include <qevent.h> 26#include <qevent.h>
27#include <qlineedit.h> 27#include <qlineedit.h>
28#include <qpixmap.h> 28#include <qpixmap.h>
29#include <qpushbutton.h> 29#include <qpushbutton.h>
30//Added by qt3to4:
31#include <Q3Frame>
32#include <QKeyEvent>
30 33
31#include <kdatepicker.h> 34#include <kdatepicker.h>
32#include <kdebug.h> 35#include <kdebug.h>
33#include <kglobal.h> 36#include <kglobal.h>
34#include <kiconloader.h> 37#include <kiconloader.h>
35#include <klocale.h> 38#include <klocale.h>
36#include <kmessagebox.h> 39#include <kmessagebox.h>
37#include <knotifyclient.h> 40#include <knotifyclient.h>
38#include <qpalette.h> 41#include <qpalette.h>
42#include <QDesktopWidget>
39 43
40#include "kdateedit.h" 44#include "kdateedit.h"
41//#include "kdateedit.moc" 45//#include "kdateedit.moc"
42 46
43KDateEdit::KDateEdit(QWidget *parent, const char *name, bool withoutDP ) 47KDateEdit::KDateEdit(QWidget *parent, const char *name, bool withoutDP )
44 : QHBox(parent, name) 48 : Q3HBox(parent, name)
45{ 49{
46 dateFormShort = true; 50 dateFormShort = true;
47 withoutDp = withoutDP; 51 withoutDp = withoutDP;
48 mDateEdit = new QLineEdit(this); 52 mDateEdit = new QLineEdit(this);
49 mDateEdit->setText(KGlobal::locale()->formatDate(QDate::currentDate(),dateFormShort)); 53 mDateEdit->setText(KGlobal::locale()->formatDate(QDate::currentDate(),dateFormShort));
50 setFocusProxy(mDateEdit); 54 setFocusProxy(mDateEdit);
51 mDateEdit->installEventFilter(this); 55 mDateEdit->installEventFilter(this);
52 56
53 // Highlight Background and Textcolor 57 // Highlight Background and Textcolor
54 QPalette palette = QWidget::palette(); 58 QPalette palette = QWidget::palette();
55 unsigned char red, green, blue; 59 unsigned char red, green, blue;
56 red = palette.color( QPalette::Normal , QColorGroup::Background ).red() - 10; 60 red = palette.color( QPalette::Normal , QColorGroup::Background ).red() - 10;
57 green = palette.color( QPalette::Normal , QColorGroup::Background ).green() - 10; 61 green = palette.color( QPalette::Normal , QColorGroup::Background ).green() - 10;
58 blue = palette.color( QPalette::Normal , QColorGroup::Background ).blue() - 10; 62 blue = palette.color( QPalette::Normal , QColorGroup::Background ).blue() - 10;
59 palette.setColor( QColorGroup::Highlight, QColor(red,green,blue) ); 63 palette.setColor( QColorGroup::Highlight, QColor(red,green,blue) );
60 palette.setColor( QColorGroup::HighlightedText, palette.color( QPalette::Normal , QColorGroup::Foreground ) ); 64 palette.setColor( QColorGroup::HighlightedText, palette.color( QPalette::Normal , QColorGroup::Foreground ) );
61 mDateEdit->setPalette( palette ); 65 mDateEdit->setPalette( palette );
62 66
63 if ( withoutDP ) { 67 if ( withoutDP ) {
64 mDateFrame = 0; 68 mDateFrame = 0;
65 mDateButton = 0; 69 mDateButton = 0;
66 mDatePicker = 0; 70 mDatePicker = 0;
67 } else { 71 } else {
68 QPixmap pixmap = SmallIcon("smallcal"); 72 QPixmap pixmap = SmallIcon("smallcal");
69 mDateButton = new QPushButton(this); 73 mDateButton = new QPushButton(this);
70 mDateButton->setPixmap(pixmap); 74 mDateButton->setPixmap(pixmap);
71 QPixmap pixmap2 = SmallIcon("today_small"); 75 QPixmap pixmap2 = SmallIcon("today_small");
72 QPushButton* nowButton = new QPushButton(this); 76 QPushButton* nowButton = new QPushButton(this);
73 nowButton->setPixmap(pixmap2); 77 nowButton->setPixmap(pixmap2);
74 78
75 mDateFrame = new QVBox(0,0,WType_Popup); 79 mDateFrame = new Q3VBox(0,0,Qt::WType_Popup);
76 // mDateFrame->setFrameStyle(QFrame::PopupPanel | QFrame::Raised); 80 // mDateFrame->setFrameStyle(QFrame::PopupPanel | QFrame::Raised);
77 mDateFrame->setFrameStyle( QFrame::WinPanel |QFrame::Raised ); 81 mDateFrame->setFrameStyle( Q3Frame::WinPanel |Q3Frame::Raised );
78 mDateFrame->setLineWidth(3); 82 mDateFrame->setLineWidth(3);
79 mDateFrame->hide(); 83 mDateFrame->hide();
80 84
81 mDatePicker = new KDatePicker(mDateFrame,QDate::currentDate()); 85 mDatePicker = new KDatePicker(mDateFrame,QDate::currentDate());
82 connect(mDatePicker,SIGNAL(dateEntered(QDate)),SLOT(setDate(QDate))); 86 connect(mDatePicker,SIGNAL(dateEntered(QDate)),SLOT(setDate(QDate)));
83 connect(mDatePicker,SIGNAL(dateEntered(QDate)),SIGNAL(dateChanged(QDate))); 87 connect(mDatePicker,SIGNAL(dateEntered(QDate)),SIGNAL(dateChanged(QDate)));
84 connect(mDatePicker,SIGNAL(dateSelected(QDate)),SLOT(setDate(QDate))); 88 connect(mDatePicker,SIGNAL(dateSelected(QDate)),SLOT(setDate(QDate)));
85 connect(mDatePicker,SIGNAL(dateSelected(QDate)),SIGNAL(dateChanged(QDate))); 89 connect(mDatePicker,SIGNAL(dateSelected(QDate)),SIGNAL(dateChanged(QDate)));
86 connect(mDatePicker,SIGNAL(dateSelected(QDate)),mDateFrame,SLOT(hide())); 90 connect(mDatePicker,SIGNAL(dateSelected(QDate)),mDateFrame,SLOT(hide()));
87 connect(mDateButton,SIGNAL(clicked()),SLOT(toggleDatePicker())); 91 connect(mDateButton,SIGNAL(clicked()),SLOT(toggleDatePicker()));
88 connect(nowButton,SIGNAL(clicked()),SLOT(goToNow())); 92 connect(nowButton,SIGNAL(clicked()),SLOT(goToNow()));
89 mDateButton->setFocusPolicy( QWidget::NoFocus ); 93 mDateButton->setFocusPolicy( Qt::NoFocus );
90 mDateButton->setAutoDefault( false ); 94 mDateButton->setAutoDefault( false );
91 //mDateFrame->resize( 400, 300 ); 95 //mDateFrame->resize( 400, 300 );
92 96
93 } 97 }
94 connect(mDateEdit,SIGNAL(returnPressed()),SLOT(lineEnterPressed())); 98 connect(mDateEdit,SIGNAL(returnPressed()),SLOT(lineEnterPressed()));
95 connect(mDateEdit,SIGNAL(textChanged(const QString &)), 99 connect(mDateEdit,SIGNAL(textChanged(const QString &)),
96 SLOT(textChanged(const QString &))); 100 SLOT(textChanged(const QString &)));
97 101
98 // Create the keyword list. This will be used to match against when the user 102 // Create the keyword list. This will be used to match against when the user
99 // enters information. 103 // enters information.
100 mKeywordMap[i18n("tomorrow")] = 1; 104 mKeywordMap[i18n("tomorrow")] = 1;
101 mKeywordMap[i18n("today")] = 0; 105 mKeywordMap[i18n("today")] = 0;
102 mKeywordMap[i18n("yesterday")] = -1; 106 mKeywordMap[i18n("yesterday")] = -1;
103 107
104 /* 108 /*
105 * This loop uses some math tricks to figure out the offset in days 109 * This loop uses some math tricks to figure out the offset in days
106 * to the next date the given day of the week occurs. There 110 * to the next date the given day of the week occurs. There
107 * are two cases, that the new day is >= the current day, which means 111 * are two cases, that the new day is >= the current day, which means
108 * the new day has not occured yet or that the new day < the current day, 112 * the new day has not occured yet or that the new day < the current day,
109 * which means the new day is already passed (so we need to find the 113 * which means the new day is already passed (so we need to find the
110 * day in the next week). 114 * day in the next week).
111 */ 115 */
112 QString dayName; 116 QString dayName;
113 int currentDay = QDate::currentDate().dayOfWeek(); 117 int currentDay = QDate::currentDate().dayOfWeek();
114 for (int i = 1; i <= 7; ++i) 118 for (int i = 1; i <= 7; ++i)
115 { 119 {
116 dayName = KGlobal::locale()->weekDayName(i).lower(); 120 dayName = KGlobal::locale()->weekDayName(i).lower();
117 if (i >= currentDay) 121 if (i >= currentDay)
118 mKeywordMap[dayName] = i - currentDay; 122 mKeywordMap[dayName] = i - currentDay;
119 else 123 else
120 mKeywordMap[dayName] = 7 - currentDay + i; 124 mKeywordMap[dayName] = 7 - currentDay + i;
121 } 125 }
122 126
123 mTextChanged = false; 127 mTextChanged = false;
124 mHandleInvalid = false; 128 mHandleInvalid = false;
125 // QWidget::setTabOrder( mDateEdit, mDateButton ); 129 // QWidget::setTabOrder( mDateEdit, mDateButton );
126} 130}
127 131
128KDateEdit::~KDateEdit() 132KDateEdit::~KDateEdit()
129{ 133{
130 delete mDateFrame; 134 delete mDateFrame;
131} 135}
132void KDateEdit::clear() 136void KDateEdit::clear()
133{ 137{
134 bool b = mDateEdit->signalsBlocked(); 138 bool b = mDateEdit->signalsBlocked();
135 mDateEdit->blockSignals(true); 139 mDateEdit->blockSignals(true);
136 mDateEdit->setText(""); 140 mDateEdit->setText("");
137 mDateEdit->blockSignals(b); 141 mDateEdit->blockSignals(b);
138} 142}
139void KDateEdit::goToNow() 143void KDateEdit::goToNow()
140{ 144{
141 setDate(QDate::currentDate() ); 145 setDate(QDate::currentDate() );
142 emit setTimeTo( QTime::currentTime() ); 146 emit setTimeTo( QTime::currentTime() );
143} 147}
144void KDateEdit::setDate(QDate newDate) 148void KDateEdit::setDate(QDate newDate)
145{ 149{
146 if (!newDate.isValid() && !mHandleInvalid) 150 if (!newDate.isValid() && !mHandleInvalid)
147 return; 151 return;
148 if ( readDate() == newDate ) 152 if ( readDate() == newDate )
149 return; 153 return;
150 QString dateString = ""; 154 QString dateString = "";
151 if(newDate.isValid()) 155 if(newDate.isValid())
152 dateString = KGlobal::locale()->formatDate( newDate, dateFormShort ); 156 dateString = KGlobal::locale()->formatDate( newDate, dateFormShort );
153 157
154 mTextChanged = false; 158 mTextChanged = false;
155 159
156 // We do not want to generate a signal here, since we explicity setting 160 // We do not want to generate a signal here, since we explicity setting
157 // the date 161 // the date
158 bool b = mDateEdit->signalsBlocked(); 162 bool b = mDateEdit->signalsBlocked();
159 mDateEdit->blockSignals(true); 163 mDateEdit->blockSignals(true);
160 mDateEdit->setText(dateString); 164 mDateEdit->setText(dateString);
161 mDateEdit->blockSignals(b); 165 mDateEdit->blockSignals(b);
162} 166}
163 167
164void KDateEdit::setDate( QDate date,int *cpos,const int key ,const bool dateFormShort) 168void KDateEdit::setDate( QDate date,int *cpos,const int key ,const bool dateFormShort)
165{ 169{
166 QString dateForm = dateFormShort ? 170 QString dateForm = dateFormShort ?
167 KGlobal::locale()->dateFormatShort() : 171 KGlobal::locale()->dateFormatShort() :
168 KGlobal::locale()->dateFormat(); 172 KGlobal::locale()->dateFormat();
169 173
170 int begin = dateForm.find("%"); 174 int begin = dateForm.find("%");
171 int space = 0; 175 int space = 0;
172 int allStrLength = 0; 176 int allStrLength = 0;
173 int strLength = 0; 177 int strLength = 0;
174 int repeat = 0; 178 int repeat = 0;
175 179
176 // witch? Day, Month or Year switch? 180 // witch? Day, Month or Year switch?
177 while(1){ 181 while(1){
178 switch ( dateForm.at(begin + 1).latin1() ) 182 switch ( dateForm.at(begin + 1).latin1() )
179 { 183 {
180 case 'd':// 16 (month day) 184 case 'd':// 16 (month day)
181 strLength = 2; //Ok 185 strLength = 2; //Ok
182 break; 186 break;
183 case 'm':// 01 (month) 187 case 'm':// 01 (month)
184 strLength = 2; //Ok 188 strLength = 2; //Ok
185 break; 189 break;
186 case 'a':// Mon (Weekday) 190 case 'a':// Mon (Weekday)
187 strLength = KGlobal::locale()->weekDayName(date.dayOfWeek(), true).length(); 191 strLength = KGlobal::locale()->weekDayName(date.dayOfWeek(), true).length();
188 break; 192 break;
189 case 'A':// Monday (Weekday) 193 case 'A':// Monday (Weekday)
190 strLength = KGlobal::locale()->weekDayName(date.dayOfWeek(), false).length(); 194 strLength = KGlobal::locale()->weekDayName(date.dayOfWeek(), false).length();
191 break; 195 break;
192 case 'b':// Jan (monthName) 196 case 'b':// Jan (monthName)
193 strLength = KGlobal::locale()->monthName(date.month(), true).length(); 197 strLength = KGlobal::locale()->monthName(date.month(), true).length();
194 break; 198 break;
195 case 'B':// January (monthName) 199 case 'B':// January (monthName)
196 strLength = KGlobal::locale()->monthName(date.month(), false).length(); 200 strLength = KGlobal::locale()->monthName(date.month(), false).length();
197 break; 201 break;
198 case 'y':// 04 (year short) 202 case 'y':// 04 (year short)
199 strLength = 2; //Ok 203 strLength = 2; //Ok
200 break; 204 break;
201 case 'Y':// 2004 (year) 205 case 'Y':// 2004 (year)
202 strLength = 4; //Ok 206 strLength = 4; //Ok
203 break; 207 break;
204 default: 208 default:
205 break; 209 break;
206 } 210 }
207 space = begin - (repeat++ * 2); 211 space = begin - (repeat++ * 2);
208 // all select? then dayswitch 212 // all select? then dayswitch
209 if( (mDateEdit->text().length() == mDateEdit->markedText().length() ) && 213 if( (mDateEdit->text().length() == mDateEdit->markedText().length() ) &&
210 ( (dateForm.at(begin + 1).latin1() == 'd') || 214 ( (dateForm.at(begin + 1).latin1() == 'd') ||
211 (dateForm.at(begin + 1).latin1() == 'a') || 215 (dateForm.at(begin + 1).latin1() == 'a') ||
212 (dateForm.at(begin + 1).latin1() == 'A') ) ) { 216 (dateForm.at(begin + 1).latin1() == 'A') ) ) {
213 break; 217 break;
214 } 218 }
215 // mDateEdit-StringPos == CursorPosition(cpos) then break and set date 219 // mDateEdit-StringPos == CursorPosition(cpos) then break and set date
216 if( ( (space + allStrLength) <= *cpos && *cpos <= (space + allStrLength + strLength) ) || *cpos < begin ) { 220 if( ( (space + allStrLength) <= *cpos && *cpos <= (space + allStrLength + strLength) ) || *cpos < begin ) {
217 break; 221 break;
218 } 222 }
219 allStrLength += strLength; 223 allStrLength += strLength;
220 begin = dateForm.find("%", begin +1); 224 begin = dateForm.find("%", begin +1);
221 } 225 }
222 226
223 // set date 227 // set date
224 switch ( dateForm.at(begin + 1).latin1() ) { 228 switch ( dateForm.at(begin + 1).latin1() ) {
225 case 'd': 229 case 'd':
226 case 'a': 230 case 'a':
227 case 'A': 231 case 'A':
228 if(key == Key_Up) { 232 if(key == Qt::Key_Up) {
229 setDate( date.addDays( 1 ) ); 233 setDate( date.addDays( 1 ) );
230 } 234 }
231 else if(key == Key_Down) { 235 else if(key == Qt::Key_Down) {
232 setDate( date.addDays( -1 ) ); 236 setDate( date.addDays( -1 ) );
233 } 237 }
234 maxDay = readDate().day(); 238 maxDay = readDate().day();
235 break; 239 break;
236 case 'm': 240 case 'm':
237 case 'b': 241 case 'b':
238 case 'B': 242 case 'B':
239 if(key == Key_Up) { 243 if(key == Qt::Key_Up) {
240 int year = ((date.month()+1)>12)?date.year()+1:date.year(); 244 int year = ((date.month()+1)>12)?date.year()+1:date.year();
241 int month = ((date.month()+1)>12)?1:date.month()+1; 245 int month = ((date.month()+1)>12)?1:date.month()+1;
242 int day = (QDate(year,month,1).daysInMonth()<maxDay)?QDate(year,month,1).daysInMonth():maxDay; 246 int day = (QDate(year,month,1).daysInMonth()<maxDay)?QDate(year,month,1).daysInMonth():maxDay;
243 setDate( QDate( year, month, day ) ); 247 setDate( QDate( year, month, day ) );
244 } else if(key == Key_Down) { 248 } else if(key == Qt::Key_Down) {
245 int year = ((date.month()-1)<1)?date.year()-1:date.year(); 249 int year = ((date.month()-1)<1)?date.year()-1:date.year();
246 int month = ((date.month()-1)<1)?12:date.month()-1; 250 int month = ((date.month()-1)<1)?12:date.month()-1;
247 int day = (QDate(year,month,1).daysInMonth()<maxDay)?QDate(year,month,1).daysInMonth():maxDay; 251 int day = (QDate(year,month,1).daysInMonth()<maxDay)?QDate(year,month,1).daysInMonth():maxDay;
248 setDate( QDate( year, month, day ) ); 252 setDate( QDate( year, month, day ) );
249 } 253 }
250 break; 254 break;
251 case 'y': 255 case 'y':
252 case 'Y': 256 case 'Y':
253 if(key == Key_Up) { 257 if(key == Qt::Key_Up) {
254 setDate( QDate( date.year() + 1, date.month() , date.day()) ); 258 setDate( QDate( date.year() + 1, date.month() , date.day()) );
255 } 259 }
256 else if(key == Key_Down) { 260 else if(key == Qt::Key_Down) {
257 setDate( QDate( date.year() - 1, date.month() , date.day()) ); 261 setDate( QDate( date.year() - 1, date.month() , date.day()) );
258 } 262 }
259 break; 263 break;
260/* default: 264/* default:
261 if(key == Key_Up) { 265 if(key == Key_Up) {
262 setDate( date.addDays( 1 ) ); 266 setDate( date.addDays( 1 ) );
263 } else if(key == Key_Down) { 267 } else if(key == Key_Down) {
264 setDate( date.addDays( -1 ) ); 268 setDate( date.addDays( -1 ) );
265 } 269 }
266 break;*/ 270 break;*/
267 } 271 }
268 272
269 date = readDate(); 273 date = readDate();
270 begin = dateForm.find("%"); 274 begin = dateForm.find("%");
271 int allSelectStrLength = 0; 275 int allSelectStrLength = 0;
272 int selectStrLength = 0; 276 int selectStrLength = 0;
273 277
274 // set selection do new date an set cursor at end of selection 278 // set selection do new date an set cursor at end of selection
275 for(int i = 0; i < repeat; i++){ 279 for(int i = 0; i < repeat; i++){
276 switch ( dateForm.at(begin + 1).latin1() ) 280 switch ( dateForm.at(begin + 1).latin1() )
277 { 281 {
278 case 'd':// 16 (month day) 282 case 'd':// 16 (month day)
279 selectStrLength = 2; //Ok 283 selectStrLength = 2; //Ok
280 break; 284 break;
281 case 'm':// 01 (month) 285 case 'm':// 01 (month)
282 selectStrLength = 2; //Ok 286 selectStrLength = 2; //Ok
283 break; 287 break;
284 case 'a':// Mon (Weekday short) 288 case 'a':// Mon (Weekday short)
285 selectStrLength = KGlobal::locale()->weekDayName(date.dayOfWeek(), true).length(); 289 selectStrLength = KGlobal::locale()->weekDayName(date.dayOfWeek(), true).length();
286 break; 290 break;
287 case 'A':// Monday (Weekday) 291 case 'A':// Monday (Weekday)
288 selectStrLength = KGlobal::locale()->weekDayName(date.dayOfWeek(), false).length(); 292 selectStrLength = KGlobal::locale()->weekDayName(date.dayOfWeek(), false).length();
289 break; 293 break;
290 case 'b':// Jan (monthName short) 294 case 'b':// Jan (monthName short)
291 selectStrLength = KGlobal::locale()->monthName(date.month(), true).length(); 295 selectStrLength = KGlobal::locale()->monthName(date.month(), true).length();
292 break; 296 break;
293 case 'B':// January (monthName) 297 case 'B':// January (monthName)
294 selectStrLength = KGlobal::locale()->monthName(date.month(), false).length(); 298 selectStrLength = KGlobal::locale()->monthName(date.month(), false).length();
295 break; 299 break;
296 case 'y':// 04 (year short) 300 case 'y':// 04 (year short)
297 selectStrLength = 2; //Ok 301 selectStrLength = 2; //Ok
298 break; 302 break;
299 case 'Y':// 2004 (year) 303 case 'Y':// 2004 (year)
300 selectStrLength = 4; //Ok 304 selectStrLength = 4; //Ok
301 break; 305 break;
302 default: 306 default:
303 break; 307 break;
304 } 308 }
305 space = begin - (i * 2); 309 space = begin - (i * 2);
306 allSelectStrLength += selectStrLength; 310 allSelectStrLength += selectStrLength;
307 begin = dateForm.find("%", begin +1); 311 begin = dateForm.find("%", begin +1);
308 } 312 }
309 // set selection from begin of date 313 // set selection from begin of date
310 setSelect( space + allSelectStrLength - selectStrLength , selectStrLength); 314 setSelect( space + allSelectStrLength - selectStrLength , selectStrLength);
311 *cpos = space + allSelectStrLength; 315 *cpos = space + allSelectStrLength;
312 emit(dateChanged(date)); 316 emit(dateChanged(date));
313 317
314 return; 318 return;
315} 319}
316 320
317void KDateEdit::setHandleInvalid(bool handleInvalid) 321void KDateEdit::setHandleInvalid(bool handleInvalid)
318{ 322{
319 mHandleInvalid = handleInvalid; 323 mHandleInvalid = handleInvalid;
320} 324}
321 325
322void KDateEdit::setEnabled(bool on) 326void KDateEdit::setEnabled(bool on)
323{ 327{
324 mDateEdit->setEnabled(on); 328 mDateEdit->setEnabled(on);
325 mDateButton->setEnabled(on); 329 mDateButton->setEnabled(on);
326} 330}
327 331
328QDate KDateEdit::date() const 332QDate KDateEdit::date() const
329{ 333{
330 QDate date = readDate(); 334 QDate date = readDate();
331 335
332 if (date.isValid() || mHandleInvalid) { 336 if (date.isValid() || mHandleInvalid) {
333 return date; 337 return date;
334 } else { 338 } else {
335 KNotifyClient::beep(); 339 KNotifyClient::beep();
336 return QDate::currentDate(); 340 return QDate::currentDate();
337 } 341 }
338} 342}
339 343
340void KDateEdit::keyPressEvent(QKeyEvent *e) 344void KDateEdit::keyPressEvent(QKeyEvent *e)
341{ 345{
342 QDate date = readDate(); 346 QDate date = readDate();
343 int cpos = mDateEdit->cursorPosition(); 347 int cpos = mDateEdit->cursorPosition();
344 348
345 switch(e->key()) 349 switch(e->key())
346 { 350 {
347 case Key_Escape: 351 case Qt::Key_Escape:
348 mDateEdit->deselect(); 352 mDateEdit->deselect();
349 case Key_Tab: 353 case Qt::Key_Tab:
350 QHBox::keyPressEvent(e); 354 Q3HBox::keyPressEvent(e);
351 break; 355 break;
352 case Key_Up: 356 case Qt::Key_Up:
353 // when date invalid then set to currend and return 357 // when date invalid then set to currend and return
354 if(!date.isValid()) { 358 if(!date.isValid()) {
355 date = QDate::currentDate(); 359 date = QDate::currentDate();
356 setDate(date); 360 setDate(date);
357 mDateEdit->setCursorPosition(cpos); 361 mDateEdit->setCursorPosition(cpos);
358 emit(dateChanged(date)); 362 emit(dateChanged(date));
359 QString text = i18n( "You entered an invalid date!\n Date changed to current date." ); 363 QString text = i18n( "You entered an invalid date!\n Date changed to current date." );
360 KMessageBox::information( 0, text ); 364 KMessageBox::information( 0, text );
361 return; 365 return;
362 } 366 }
363 setDate(date, &cpos, Key_Up, dateFormShort); 367 setDate(date, &cpos, Qt::Key_Up, dateFormShort);
364 break; 368 break;
365 case Key_Down: 369 case Qt::Key_Down:
366 // when date invalid then set to current and return 370 // when date invalid then set to current and return
367 if(!date.isValid()) { 371 if(!date.isValid()) {
368 date = QDate::currentDate(); 372 date = QDate::currentDate();
369 setDate(date); 373 setDate(date);
370 mDateEdit->setCursorPosition(cpos); 374 mDateEdit->setCursorPosition(cpos);
371 emit(dateChanged(date)); 375 emit(dateChanged(date));
372 QString text = i18n( "You entered an invalid date!\n Date changed to current date." ); 376 QString text = i18n( "You entered an invalid date!\n Date changed to current date." );
373 KMessageBox::information( 0, text ); 377 KMessageBox::information( 0, text );
374 return; 378 return;
375 } 379 }
376 setDate(date, &cpos, Key_Down, dateFormShort); 380 setDate(date, &cpos, Qt::Key_Down, dateFormShort);
377 break; 381 break;
378 default: 382 default:
379 QHBox::keyPressEvent(e); 383 Q3HBox::keyPressEvent(e);
380 break; 384 break;
381 } // switch 385 } // switch
382 mDateEdit->setCursorPosition(cpos); 386 mDateEdit->setCursorPosition(cpos);
383} 387}
384 388
385void KDateEdit::setSelect( int from, int to ) 389void KDateEdit::setSelect( int from, int to )
386{ 390{
387// return; 391// return;
388 mDateEdit->setSelection( from , to ); 392 mDateEdit->setSelection( from , to );
389} 393}
390 394
391void KDateEdit::toggleDatePicker() 395void KDateEdit::toggleDatePicker()
392{ 396{
393 if( mDateFrame->isVisible() ) { 397 if( mDateFrame->isVisible() ) {
394 mDateFrame->hide(); 398 mDateFrame->hide();
395 } else { 399 } else {
396 QPoint tmpPoint = mapToGlobal(mDateButton->geometry().bottomRight()); 400 QPoint tmpPoint = mapToGlobal(mDateButton->geometry().bottomRight());
397 QSize datepickersize = mDatePicker->sizeHint(); 401 QSize datepickersize = mDatePicker->sizeHint();
398 402
399 if ( tmpPoint.x() < 7+datepickersize.width() ) tmpPoint.setX( 7+datepickersize.width() ); 403 if ( tmpPoint.x() < 7+datepickersize.width() ) tmpPoint.setX( 7+datepickersize.width() );
400 404
401 int h = QApplication::desktop()->height(); 405 int h = QApplication::desktop()->height();
402 406
403 if ( tmpPoint.y() + datepickersize.height() > h ) tmpPoint.setY( h - datepickersize.height() ); 407 if ( tmpPoint.y() + datepickersize.height() > h ) tmpPoint.setY( h - datepickersize.height() );
404 408
405 mDateFrame->setGeometry(tmpPoint.x()-datepickersize.width()-7, tmpPoint.y(), 409 mDateFrame->setGeometry(tmpPoint.x()-datepickersize.width()-7, tmpPoint.y(),
406 datepickersize.width()+2*mDateFrame->lineWidth(), datepickersize.height()+2*mDateFrame->lineWidth()); 410 datepickersize.width()+2*mDateFrame->lineWidth(), datepickersize.height()+2*mDateFrame->lineWidth());
407 411
408 QDate date = readDate(); 412 QDate date = readDate();
409 if(date.isValid()) { 413 if(date.isValid()) {
410 mDatePicker->setDate(date); 414 mDatePicker->setDate(date);
411 } else { 415 } else {
412 mDatePicker->setDate(QDate::currentDate()); 416 mDatePicker->setDate(QDate::currentDate());
413 } 417 }
414 mDateFrame->show(); 418 mDateFrame->show();
415 } 419 }
416} 420}
417 421
418 422
419void KDateEdit::lineEnterPressed() 423void KDateEdit::lineEnterPressed()
420{ 424{
421 QDate date = readDate(); 425 QDate date = readDate();
422 426
423 if(date.isValid()) 427 if(date.isValid())
424 { 428 {
425 // Update the edit. This is needed if the user has entered a 429 // Update the edit. This is needed if the user has entered a
426 // word rather than the actual date. 430 // word rather than the actual date.
427 setDate(date); 431 setDate(date);
428 emit(dateChanged(date)); 432 emit(dateChanged(date));
429 emit returnPressed(); 433 emit returnPressed();
430 } 434 }
431 else 435 else
432 { 436 {
433 if ( withoutDp ) { 437 if ( withoutDp ) {
434 KNotifyClient::beep(); 438 KNotifyClient::beep();
435 } else { 439 } else {
436 if ( !mDateEdit->text().isEmpty() ) { 440 if ( !mDateEdit->text().isEmpty() ) {
437 mTextChanged = false; 441 mTextChanged = false;
438 QString text = i18n( "You entered an invalid date!\n Will use current date instead." ); 442 QString text = i18n( "You entered an invalid date!\n Will use current date instead." );
439 if ( isVisible() ) 443 if ( isVisible() )
440 if ( KMessageBox::warningContinueCancel( 0, text ) == KMessageBox::Continue ) { 444 if ( KMessageBox::warningContinueCancel( 0, text ) == KMessageBox::Continue ) {
441 setDate( QDate::currentDate() ); 445 setDate( QDate::currentDate() );
442 emit dateChanged( QDate::currentDate() ); 446 emit dateChanged( QDate::currentDate() );
443 } 447 }
444 } 448 }
445 } 449 }
446 } 450 }
447} 451}
448 452
449bool KDateEdit::inputIsValid() 453bool KDateEdit::inputIsValid()
450{ 454{
451 return readDate().isValid(); 455 return readDate().isValid();
452} 456}
453 457
454QDate KDateEdit::readDate() const 458QDate KDateEdit::readDate() const
455{ 459{
456 QString text = mDateEdit->text(); 460 QString text = mDateEdit->text();
457 QDate date; 461 QDate date;
458 462
459 if (mKeywordMap.contains(text.lower())) 463 if (mKeywordMap.contains(text.lower()))
460 { 464 {
461 date = QDate::currentDate().addDays(mKeywordMap[text.lower()]); 465 date = QDate::currentDate().addDays(mKeywordMap[text.lower()]);
462 } 466 }
463 else 467 else
464 { 468 {
465 date = KGlobal::locale()->readDate(text); 469 date = KGlobal::locale()->readDate(text);
466 } 470 }
467 471
468 return date; 472 return date;
469} 473}
470 474
471bool KDateEdit::eventFilter(QObject *, QEvent *e) 475bool KDateEdit::eventFilter(QObject *, QEvent *e)
472{ 476{
473 // We only process the focus out event if the text has changed 477 // We only process the focus out event if the text has changed
474 // since we got focus 478 // since we got focus
475 if ((e->type() == QEvent::FocusOut) ) 479 if ((e->type() == QEvent::FocusOut) )
476 { 480 {
477 if ( mTextChanged ) { 481 if ( mTextChanged ) {
478 lineEnterPressed(); 482 lineEnterPressed();
479 mTextChanged = false; 483 mTextChanged = false;
480 } 484 }
481 } 485 }
482 // switch dateFormShort by double klick with mouse 486 // switch dateFormShort by double klick with mouse
483 else if (e->type() == QEvent::MouseButtonDblClick) 487 else if (e->type() == QEvent::MouseButtonDblClick)
484 { 488 {
485 toggleDateFormat(); 489 toggleDateFormat();
486 } 490 }
487 else if (e->type() == QEvent::FocusIn) 491 else if (e->type() == QEvent::FocusIn)
488 { 492 {
489 maxDay = readDate().day(); 493 maxDay = readDate().day();
490 } 494 }
491 495
492 return false; 496 return false;
493} 497}
494void KDateEdit::toggleDateFormat() 498void KDateEdit::toggleDateFormat()
495{ 499{
496 dateFormShort = ! dateFormShort; 500 dateFormShort = ! dateFormShort;
497 mDateEdit->setText(KGlobal::locale()->formatDate(readDate(),dateFormShort)); 501 mDateEdit->setText(KGlobal::locale()->formatDate(readDate(),dateFormShort));
498 502
499} 503}
500 504
501void KDateEdit::textChanged(const QString &) 505void KDateEdit::textChanged(const QString &)
502{ 506{
503 if(mHandleInvalid && mDateEdit->text().stripWhiteSpace().isEmpty()) { 507 if(mHandleInvalid && mDateEdit->text().stripWhiteSpace().isEmpty()) {
504 QDate date; //invalid date 508 QDate date; //invalid date
505 emit(dateChanged(date)); 509 emit(dateChanged(date));
506 } else { 510 } else {
507 mTextChanged = true; 511 mTextChanged = true;
508 } 512 }
509 maxDay = readDate().day(); 513 maxDay = readDate().day();
510} 514}