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