Diffstat (limited to 'noncore/apps/tinykate/libkate/microkde/kdatepicker.cpp') (more/less context) (show whitespace changes)
-rw-r--r-- | noncore/apps/tinykate/libkate/microkde/kdatepicker.cpp | 405 |
1 files changed, 405 insertions, 0 deletions
diff --git a/noncore/apps/tinykate/libkate/microkde/kdatepicker.cpp b/noncore/apps/tinykate/libkate/microkde/kdatepicker.cpp new file mode 100644 index 0000000..2cdd609 --- a/dev/null +++ b/noncore/apps/tinykate/libkate/microkde/kdatepicker.cpp | |||
@@ -0,0 +1,405 @@ | |||
1 | /* -*- C++ -*- | ||
2 | This file is part of the KDE libraries | ||
3 | Copyright (C) 1997 Tim D. Gilman (tdgilman@best.org) | ||
4 | (C) 1998-2001 Mirko Boehm (mirko@kde.org) | ||
5 | This library is free software; you can redistribute it and/or | ||
6 | modify it under the terms of the GNU Library General Public | ||
7 | License as published by the Free Software Foundation; either | ||
8 | version 2 of the License, or (at your option) any later version. | ||
9 | |||
10 | This library is distributed in the hope that it will be useful, | ||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Library General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Library General Public License | ||
16 | along with this library; see the file COPYING.LIB. If not, write to | ||
17 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | #include "kdatepicker.h" | ||
22 | #include <kglobal.h> | ||
23 | #include <kapplication.h> | ||
24 | #include <klocale.h> | ||
25 | #include <kiconloader.h> | ||
26 | #include <qframe.h> | ||
27 | #include <qpainter.h> | ||
28 | #include <qdialog.h> | ||
29 | #include <qtoolbutton.h> | ||
30 | #include <qfont.h> | ||
31 | #include <qlineedit.h> | ||
32 | #include <qvalidator.h> | ||
33 | #include <kdebug.h> | ||
34 | #include <knotifyclient.h> | ||
35 | #include "kdatetbl.h" | ||
36 | #include "kdatepicker.moc" | ||
37 | |||
38 | |||
39 | KDatePicker::KDatePicker(QWidget *parent, QDate dt, const char *name) | ||
40 | : QFrame(parent,name), | ||
41 | yearForward(new QToolButton(this)), | ||
42 | yearBackward(new QToolButton(this)), | ||
43 | monthForward(new QToolButton(this)), | ||
44 | monthBackward(new QToolButton(this)), | ||
45 | selectMonth(new QToolButton(this)), | ||
46 | selectYear(new QToolButton(this)), | ||
47 | line(new QLineEdit(this)), | ||
48 | val(new KDateValidator(this)), | ||
49 | table(new KDateTable(this)), | ||
50 | fontsize(10) | ||
51 | { | ||
52 | // ----- | ||
53 | setFontSize(10); | ||
54 | line->setValidator(val); | ||
55 | yearForward->setPixmap(BarIcon(QString::fromLatin1("2rightarrow"))); | ||
56 | yearBackward->setPixmap(BarIcon(QString::fromLatin1("2leftarrow"))); | ||
57 | monthForward->setPixmap(BarIcon(QString::fromLatin1("1rightarrow"))); | ||
58 | monthBackward->setPixmap(BarIcon(QString::fromLatin1("1leftarrow"))); | ||
59 | setDate(dt); // set button texts | ||
60 | connect(table, SIGNAL(dateChanged(QDate)), SLOT(dateChangedSlot(QDate))); | ||
61 | connect(table, SIGNAL(tableClicked()), SLOT(tableClickedSlot())); | ||
62 | connect(monthForward, SIGNAL(clicked()), SLOT(monthForwardClicked())); | ||
63 | connect(monthBackward, SIGNAL(clicked()), SLOT(monthBackwardClicked())); | ||
64 | connect(yearForward, SIGNAL(clicked()), SLOT(yearForwardClicked())); | ||
65 | connect(yearBackward, SIGNAL(clicked()), SLOT(yearBackwardClicked())); | ||
66 | connect(selectMonth, SIGNAL(clicked()), SLOT(selectMonthClicked())); | ||
67 | connect(selectYear, SIGNAL(clicked()), SLOT(selectYearClicked())); | ||
68 | connect(line, SIGNAL(returnPressed()), SLOT(lineEnterPressed())); | ||
69 | } | ||
70 | |||
71 | KDatePicker::~KDatePicker() | ||
72 | { | ||
73 | } | ||
74 | |||
75 | void | ||
76 | KDatePicker::resizeEvent(QResizeEvent*) | ||
77 | { | ||
78 | QWidget *buttons[] = { | ||
79 | yearBackward, | ||
80 | monthBackward, | ||
81 | selectMonth, | ||
82 | selectYear, | ||
83 | monthForward, | ||
84 | yearForward }; | ||
85 | const int NoOfButtons=sizeof(buttons)/sizeof(buttons[0]); | ||
86 | QSize sizes[NoOfButtons]; | ||
87 | int buttonHeight=0; | ||
88 | int count; | ||
89 | int w; | ||
90 | int x=0; | ||
91 | // ----- calculate button row height: | ||
92 | for(count=0; count<NoOfButtons; ++count) { | ||
93 | sizes[count]=buttons[count]->sizeHint(); | ||
94 | buttonHeight=QMAX(buttonHeight, sizes[count].height()); | ||
95 | } | ||
96 | // ----- calculate size of the month button: | ||
97 | w=0; | ||
98 | for(count=0; count<NoOfButtons; ++count) { | ||
99 | if(buttons[count]!=selectMonth) | ||
100 | { | ||
101 | w+=sizes[count].width(); | ||
102 | } else { | ||
103 | x=count; | ||
104 | } | ||
105 | } | ||
106 | sizes[x].setWidth(width()-w); // stretch the month button | ||
107 | // ----- place the buttons: | ||
108 | x=0; | ||
109 | for(count=0; count<NoOfButtons; ++count) | ||
110 | { | ||
111 | w=sizes[count].width(); | ||
112 | buttons[count]->setGeometry(x, 0, w, buttonHeight); | ||
113 | x+=w; | ||
114 | } | ||
115 | // ----- place the line edit for direct input: | ||
116 | sizes[0]=line->sizeHint(); | ||
117 | line->setGeometry(0, height()-sizes[0].height(), width(), sizes[0].height()); | ||
118 | // ----- adjust the table: | ||
119 | table->setGeometry(0, buttonHeight, width(), | ||
120 | height()-buttonHeight-sizes[0].height()); | ||
121 | } | ||
122 | |||
123 | void | ||
124 | KDatePicker::dateChangedSlot(QDate date) | ||
125 | { | ||
126 | kdDebug() << "KDatePicker::dateChangedSlot: date changed (" << date.year() << "/" << date.month() << "/" << date.day() << ")." << endl; | ||
127 | line->setText(KGlobal::locale()->formatDate(date, true)); | ||
128 | emit(dateChanged(date)); | ||
129 | } | ||
130 | |||
131 | void | ||
132 | KDatePicker::tableClickedSlot() | ||
133 | { | ||
134 | kdDebug() << "KDatePicker::tableClickedSlot: table clicked." << endl; | ||
135 | emit(dateSelected(table->getDate())); | ||
136 | emit(tableClicked()); | ||
137 | } | ||
138 | |||
139 | const QDate& | ||
140 | KDatePicker::getDate() const | ||
141 | { | ||
142 | return table->getDate(); | ||
143 | } | ||
144 | |||
145 | const QDate & | ||
146 | KDatePicker::date() const | ||
147 | { | ||
148 | return table->getDate(); | ||
149 | } | ||
150 | |||
151 | bool | ||
152 | KDatePicker::setDate(const QDate& date) | ||
153 | { | ||
154 | if(date.isValid()) { | ||
155 | QString temp; | ||
156 | // ----- | ||
157 | table->setDate(date); | ||
158 | selectMonth->setText(KGlobal::locale()->monthName(date.month(), false)); | ||
159 | temp.setNum(date.year()); | ||
160 | selectYear->setText(temp); | ||
161 | line->setText(KGlobal::locale()->formatDate(date, true)); | ||
162 | return true; | ||
163 | } else { | ||
164 | kdDebug() << "KDatePicker::setDate: refusing to set invalid date." << endl; | ||
165 | return false; | ||
166 | } | ||
167 | } | ||
168 | |||
169 | void | ||
170 | KDatePicker::monthForwardClicked() | ||
171 | { | ||
172 | QDate temp=table->getDate(); | ||
173 | int day=temp.day(); | ||
174 | // ----- | ||
175 | if(temp.month()==12) { | ||
176 | temp.setYMD(temp.year()+1, 1, 1); | ||
177 | } else { | ||
178 | temp.setYMD(temp.year(), temp.month()+1, 1); | ||
179 | } | ||
180 | if(temp.daysInMonth()<day) { | ||
181 | temp.setYMD(temp.year(), temp.month(), temp.daysInMonth()); | ||
182 | } else { | ||
183 | temp.setYMD(temp.year(), temp.month(), day); | ||
184 | } | ||
185 | // assert(temp.isValid()); | ||
186 | setDate(temp); | ||
187 | } | ||
188 | |||
189 | void | ||
190 | KDatePicker::monthBackwardClicked() | ||
191 | { | ||
192 | QDate temp=table->getDate(); | ||
193 | int day=temp.day(); | ||
194 | // ----- | ||
195 | if(temp.month()==1) | ||
196 | { | ||
197 | temp.setYMD(temp.year()-1, 12, 1); | ||
198 | } else { | ||
199 | temp.setYMD(temp.year(), temp.month()-1, 1); | ||
200 | } | ||
201 | if(temp.daysInMonth()<day) | ||
202 | { | ||
203 | temp.setYMD(temp.year(), temp.month(), temp.daysInMonth()); | ||
204 | } else { | ||
205 | temp.setYMD(temp.year(), temp.month(), day); | ||
206 | } | ||
207 | // assert(temp.isValid()); | ||
208 | setDate(temp); | ||
209 | } | ||
210 | |||
211 | void | ||
212 | KDatePicker::yearForwardClicked() | ||
213 | { | ||
214 | QDate temp=table->getDate(); | ||
215 | int day=temp.day(); | ||
216 | // ----- | ||
217 | temp.setYMD(temp.year()+1, temp.month(), 1); | ||
218 | if(temp.daysInMonth()<day) | ||
219 | { | ||
220 | temp.setYMD(temp.year(), temp.month(), temp.daysInMonth()); | ||
221 | } else { | ||
222 | temp.setYMD(temp.year(), temp.month(), day); | ||
223 | } | ||
224 | // assert(temp.isValid()); | ||
225 | setDate(temp); | ||
226 | } | ||
227 | |||
228 | void | ||
229 | KDatePicker::yearBackwardClicked() | ||
230 | { | ||
231 | QDate temp=table->getDate(); | ||
232 | int day=temp.day(); | ||
233 | // ----- | ||
234 | temp.setYMD(temp.year()-1, temp.month(), 1); | ||
235 | if(temp.daysInMonth()<day) | ||
236 | { | ||
237 | temp.setYMD(temp.year(), temp.month(), temp.daysInMonth()); | ||
238 | } else { | ||
239 | temp.setYMD(temp.year(), temp.month(), day); | ||
240 | } | ||
241 | // assert(temp.isValid()); | ||
242 | setDate(temp); | ||
243 | } | ||
244 | |||
245 | void | ||
246 | KDatePicker::selectMonthClicked() | ||
247 | { | ||
248 | int month; | ||
249 | KPopupFrame* popup = new KPopupFrame(this); | ||
250 | KDateInternalMonthPicker* picker = new KDateInternalMonthPicker(fontsize, popup); | ||
251 | // ----- | ||
252 | picker->resize(picker->sizeHint()); | ||
253 | popup->setMainWidget(picker); | ||
254 | picker->setFocus(); | ||
255 | connect(picker, SIGNAL(closeMe(int)), popup, SLOT(close(int))); | ||
256 | if(popup->exec(selectMonth->mapToGlobal(QPoint(0, selectMonth->height())))) | ||
257 | { | ||
258 | QDate date; | ||
259 | int day; | ||
260 | // ----- | ||
261 | month=picker->getResult(); | ||
262 | date=table->getDate(); | ||
263 | day=date.day(); | ||
264 | // ----- construct a valid date in this month: | ||
265 | date.setYMD(date.year(), month, 1); | ||
266 | date.setYMD(date.year(), month, QMIN(day, date.daysInMonth())); | ||
267 | // ----- set this month | ||
268 | setDate(date); | ||
269 | } else { | ||
270 | KNotifyClient::beep(); | ||
271 | } | ||
272 | delete popup; | ||
273 | } | ||
274 | |||
275 | void | ||
276 | KDatePicker::selectYearClicked() | ||
277 | { | ||
278 | int year; | ||
279 | KPopupFrame* popup = new KPopupFrame(this); | ||
280 | KDateInternalYearSelector* picker = new KDateInternalYearSelector(fontsize, popup); | ||
281 | // ----- | ||
282 | picker->resize(picker->sizeHint()); | ||
283 | popup->setMainWidget(picker); | ||
284 | connect(picker, SIGNAL(closeMe(int)), popup, SLOT(close(int))); | ||
285 | picker->setFocus(); | ||
286 | if(popup->exec(selectYear->mapToGlobal(QPoint(0, selectMonth->height())))) | ||
287 | { | ||
288 | QDate date; | ||
289 | int day; | ||
290 | // ----- | ||
291 | year=picker->getYear(); | ||
292 | date=table->getDate(); | ||
293 | day=date.day(); | ||
294 | // ----- construct a valid date in this month: | ||
295 | date.setYMD(year, date.month(), 1); | ||
296 | date.setYMD(year, date.month(), QMIN(day, date.daysInMonth())); | ||
297 | // ----- set this month | ||
298 | setDate(date); | ||
299 | } else { | ||
300 | KNotifyClient::beep(); | ||
301 | } | ||
302 | delete popup; | ||
303 | } | ||
304 | |||
305 | void | ||
306 | KDatePicker::setEnabled(bool enable) | ||
307 | { | ||
308 | QWidget *widgets[]= { | ||
309 | yearForward, yearBackward, monthForward, monthBackward, | ||
310 | selectMonth, selectYear, | ||
311 | line, table }; | ||
312 | const int Size=sizeof(widgets)/sizeof(widgets[0]); | ||
313 | int count; | ||
314 | // ----- | ||
315 | for(count=0; count<Size; ++count) | ||
316 | { | ||
317 | widgets[count]->setEnabled(enable); | ||
318 | } | ||
319 | } | ||
320 | |||
321 | void | ||
322 | KDatePicker::lineEnterPressed() | ||
323 | { | ||
324 | QDate temp; | ||
325 | // ----- | ||
326 | if(val->date(line->text(), temp)==QValidator::Acceptable) | ||
327 | { | ||
328 | kdDebug() << "KDatePicker::lineEnterPressed: valid date entered." << endl; | ||
329 | emit(dateEntered(temp)); | ||
330 | setDate(temp); | ||
331 | } else { | ||
332 | KNotifyClient::beep(); | ||
333 | kdDebug() << "KDatePicker::lineEnterPressed: invalid date entered." << endl; | ||
334 | } | ||
335 | } | ||
336 | |||
337 | QSize | ||
338 | KDatePicker::sizeHint() const | ||
339 | { | ||
340 | QSize tableSize=table->sizeHint(); | ||
341 | QWidget *buttons[]={ | ||
342 | yearBackward, | ||
343 | monthBackward, | ||
344 | selectMonth, | ||
345 | selectYear, | ||
346 | monthForward, | ||
347 | yearForward }; | ||
348 | const int NoOfButtons=sizeof(buttons)/sizeof(buttons[0]); | ||
349 | QSize sizes[NoOfButtons]; | ||
350 | int cx=0, cy=0, count; | ||
351 | // ----- store the size hints: | ||
352 | for(count=0; count<NoOfButtons; ++count) | ||
353 | { | ||
354 | sizes[count]=buttons[count]->sizeHint(); | ||
355 | if(buttons[count]==selectMonth) | ||
356 | { | ||
357 | cx+=maxMonthRect.width(); | ||
358 | } else { | ||
359 | cx+=sizes[count].width(); | ||
360 | } | ||
361 | cy=QMAX(sizes[count].height(), cy); | ||
362 | } | ||
363 | // ----- calculate width hint: | ||
364 | cx=QMAX(cx, tableSize.width()); // line edit ignored | ||
365 | // ----- calculate height hint: | ||
366 | cy+=tableSize.height()+line->sizeHint().height(); | ||
367 | return QSize(cx, cy); | ||
368 | } | ||
369 | |||
370 | void | ||
371 | KDatePicker::setFontSize(int s) | ||
372 | { | ||
373 | QWidget *buttons[]= { | ||
374 | // yearBackward, | ||
375 | // monthBackward, | ||
376 | selectMonth, | ||
377 | selectYear, | ||
378 | // monthForward, | ||
379 | // yearForward | ||
380 | }; | ||
381 | const int NoOfButtons=sizeof(buttons)/sizeof(buttons[0]); | ||
382 | int count; | ||
383 | QFont font; | ||
384 | QRect r; | ||
385 | // ----- | ||
386 | fontsize=s; | ||
387 | for(count=0; count<NoOfButtons; ++count) | ||
388 | { | ||
389 | font=buttons[count]->font(); | ||
390 | font.setPointSize(s); | ||
391 | buttons[count]->setFont(font); | ||
392 | } | ||
393 | QFontMetrics metrics(selectMonth->fontMetrics()); | ||
394 | for(int i=1; i <= 12; ++i) | ||
395 | { // maxMonthRect is used by sizeHint() | ||
396 | r=metrics.boundingRect(KGlobal::locale()->monthName(i, false)); | ||
397 | maxMonthRect.setWidth(QMAX(r.width(), maxMonthRect.width())); | ||
398 | maxMonthRect.setHeight(QMAX(r.height(), maxMonthRect.height())); | ||
399 | } | ||
400 | table->setFontSize(s); | ||
401 | } | ||
402 | |||
403 | void KDatePicker::virtual_hook( int id, void* data ) | ||
404 | { /*BASE::virtual_hook( id, data );*/ } | ||
405 | |||