author | zautrix <zautrix> | 2004-06-26 19:01:18 (UTC) |
---|---|---|
committer | zautrix <zautrix> | 2004-06-26 19:01:18 (UTC) |
commit | b9aad1f15dc600e4dbe4c62d3fcced6363188ba3 (patch) (unidiff) | |
tree | 2c3d4004fb21c72cba65793859f9bcd8ffd3a49c /microkde/kdatepickernew.cpp | |
download | kdepimpi-b9aad1f15dc600e4dbe4c62d3fcced6363188ba3.zip kdepimpi-b9aad1f15dc600e4dbe4c62d3fcced6363188ba3.tar.gz kdepimpi-b9aad1f15dc600e4dbe4c62d3fcced6363188ba3.tar.bz2 |
Initial revision
-rw-r--r-- | microkde/kdatepickernew.cpp | 485 |
1 files changed, 485 insertions, 0 deletions
diff --git a/microkde/kdatepickernew.cpp b/microkde/kdatepickernew.cpp new file mode 100644 index 0000000..f60a422 --- a/dev/null +++ b/microkde/kdatepickernew.cpp | |||
@@ -0,0 +1,485 @@ | |||
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 <qlayout.h> | ||
22 | #include <qframe.h> | ||
23 | #include <qpainter.h> | ||
24 | #include <qdialog.h> | ||
25 | #include <qstyle.h> | ||
26 | #include <qtoolbutton.h> | ||
27 | #include <qcombobox.h> | ||
28 | #include <qtooltip.h> | ||
29 | #include <qfont.h> | ||
30 | #include <qvalidator.h> | ||
31 | #include <qpopupmenu.h> | ||
32 | |||
33 | #include "kdatepicker.h" | ||
34 | #include <kglobal.h> | ||
35 | #include <kapplication.h> | ||
36 | #include <klocale.h> | ||
37 | #include <kiconloader.h> | ||
38 | #include <ktoolbar.h> | ||
39 | #include <klineedit.h> | ||
40 | #include <kdebug.h> | ||
41 | #include <knotifyclient.h> | ||
42 | #include <kcalendarsystem.h> | ||
43 | |||
44 | #include "kdatetbl.h" | ||
45 | #include "kdatepicker.moc" | ||
46 | |||
47 | class KDatePicker::KDatePickerPrivate | ||
48 | { | ||
49 | public: | ||
50 | KDatePickerPrivate() : closeButton(0L), selectWeek(0L), todayButton(0), navigationLayout(0) {} | ||
51 | |||
52 | void fillWeeksCombo(const QDate &date); | ||
53 | |||
54 | KToolBar *tb; | ||
55 | QToolButton *closeButton; | ||
56 | QComboBox *selectWeek; | ||
57 | QToolButton *todayButton; | ||
58 | QBoxLayout *navigationLayout; | ||
59 | }; | ||
60 | |||
61 | void KDatePicker::fillWeeksCombo(const QDate &date) | ||
62 | { | ||
63 | // every year can have a different number of weeks | ||
64 | const KCalendarSystem * calendar = KGlobal::locale()->calendar(); | ||
65 | int i, weeks = calendar->weeksInYear(calendar->year(date)); | ||
66 | |||
67 | if ( d->selectWeek->count() == weeks ) return; // we already have the correct number | ||
68 | |||
69 | d->selectWeek->clear(); | ||
70 | |||
71 | for (i = 1; i <= weeks; i++) | ||
72 | d->selectWeek->insertItem(i18n("Week %1").arg(i)); | ||
73 | } | ||
74 | |||
75 | KDatePicker::KDatePicker(QWidget *parent, QDate dt, const char *name) | ||
76 | : QFrame(parent,name) | ||
77 | { | ||
78 | init( dt ); | ||
79 | } | ||
80 | |||
81 | KDatePicker::KDatePicker(QWidget *parent, QDate dt, const char *name, WFlags f) | ||
82 | : QFrame(parent,name, f) | ||
83 | { | ||
84 | init( dt ); | ||
85 | } | ||
86 | |||
87 | KDatePicker::KDatePicker( QWidget *parent, const char *name ) | ||
88 | : QFrame(parent,name) | ||
89 | { | ||
90 | init( QDate::currentDate() ); | ||
91 | } | ||
92 | |||
93 | void KDatePicker::init( const QDate &dt ) | ||
94 | { | ||
95 | d = new KDatePickerPrivate(); | ||
96 | |||
97 | d->tb = new KToolBar(this); | ||
98 | |||
99 | yearBackward = new QToolButton(d->tb); | ||
100 | monthBackward = new QToolButton(d->tb); | ||
101 | selectMonth = new QToolButton(d->tb); | ||
102 | selectYear = new QToolButton(d->tb); | ||
103 | monthForward = new QToolButton(d->tb); | ||
104 | yearForward = new QToolButton(d->tb); | ||
105 | line = new KLineEdit(this); | ||
106 | val = new KDateValidator(this); | ||
107 | table = new KDateTable(this); | ||
108 | fontsize = 12;//KGlobalSettings::generalFont().pointSize(); | ||
109 | |||
110 | |||
111 | fontsize++; // Make a little bigger | ||
112 | |||
113 | d->selectWeek = new QComboBox(false, this); // read only week selection | ||
114 | d->todayButton = new QToolButton(this); | ||
115 | d->todayButton->setIconSet(SmallIconSet("today")); | ||
116 | |||
117 | QToolTip::add(yearForward, i18n("Next year")); | ||
118 | QToolTip::add(yearBackward, i18n("Previous year")); | ||
119 | QToolTip::add(monthForward, i18n("Next month")); | ||
120 | QToolTip::add(monthBackward, i18n("Previous month")); | ||
121 | QToolTip::add(d->selectWeek, i18n("Select a week")); | ||
122 | QToolTip::add(selectMonth, i18n("Select a month")); | ||
123 | QToolTip::add(selectYear, i18n("Select a year")); | ||
124 | QToolTip::add(d->todayButton, i18n("Select the current day")); | ||
125 | |||
126 | // ----- | ||
127 | setFontSize(fontsize); | ||
128 | line->setValidator(val); | ||
129 | line->installEventFilter( this ); | ||
130 | // yearForward->setIconSet(BarIconSet(QString::fromLatin1("2rightarrow"))); | ||
131 | // yearBackward->setIconSet(BarIconSet(QString::fromLatin1("2leftarrow"))); | ||
132 | // monthForward->setIconSet(BarIconSet(QString::fromLatin1("1rightarrow"))); | ||
133 | // monthBackward->setIconSet(BarIconSet(QString::fromLatin1("1leftarrow"))); | ||
134 | setDate(dt); // set button texts | ||
135 | connect(table, SIGNAL(dateChanged(QDate)), SLOT(dateChangedSlot(QDate))); | ||
136 | connect(table, SIGNAL(tableClicked()), SLOT(tableClickedSlot())); | ||
137 | connect(monthForward, SIGNAL(clicked()), SLOT(monthForwardClicked())); | ||
138 | connect(monthBackward, SIGNAL(clicked()), SLOT(monthBackwardClicked())); | ||
139 | connect(yearForward, SIGNAL(clicked()), SLOT(yearForwardClicked())); | ||
140 | connect(yearBackward, SIGNAL(clicked()), SLOT(yearBackwardClicked())); | ||
141 | connect(d->selectWeek, SIGNAL(activated(int)), SLOT(weekSelected(int))); | ||
142 | connect(d->todayButton, SIGNAL(clicked()), SLOT(todayButtonClicked())); | ||
143 | connect(selectMonth, SIGNAL(clicked()), SLOT(selectMonthClicked())); | ||
144 | connect(selectYear, SIGNAL(clicked()), SLOT(selectYearClicked())); | ||
145 | connect(line, SIGNAL(returnPressed()), SLOT(lineEnterPressed())); | ||
146 | table->setFocus(); | ||
147 | |||
148 | QBoxLayout * topLayout = new QVBoxLayout(this); | ||
149 | |||
150 | d->navigationLayout = new QHBoxLayout(topLayout); | ||
151 | d->navigationLayout->addWidget(d->tb); | ||
152 | |||
153 | topLayout->addWidget(table); | ||
154 | |||
155 | QBoxLayout * bottomLayout = new QHBoxLayout(topLayout); | ||
156 | bottomLayout->addWidget(d->todayButton); | ||
157 | bottomLayout->addWidget(line); | ||
158 | bottomLayout->addWidget(d->selectWeek); | ||
159 | } | ||
160 | |||
161 | KDatePicker::~KDatePicker() | ||
162 | { | ||
163 | delete d; | ||
164 | } | ||
165 | |||
166 | bool | ||
167 | KDatePicker::eventFilter(QObject *o, QEvent *e ) | ||
168 | { | ||
169 | if ( e->type() == QEvent::KeyPress ) { | ||
170 | QKeyEvent *k = (QKeyEvent *)e; | ||
171 | |||
172 | if ( (k->key() == Qt::Key_Prior) || | ||
173 | (k->key() == Qt::Key_Next) || | ||
174 | (k->key() == Qt::Key_Up) || | ||
175 | (k->key() == Qt::Key_Down) ) | ||
176 | { | ||
177 | QApplication::sendEvent( table, e ); | ||
178 | table->setFocus(); | ||
179 | return true; // eat event | ||
180 | } | ||
181 | } | ||
182 | return QFrame::eventFilter( o, e ); | ||
183 | } | ||
184 | |||
185 | void | ||
186 | KDatePicker::resizeEvent(QResizeEvent* e) | ||
187 | { | ||
188 | QWidget::resizeEvent(e); | ||
189 | } | ||
190 | |||
191 | void | ||
192 | KDatePicker::dateChangedSlot(QDate date) | ||
193 | { | ||
194 | kdDebug(298) << "KDatePicker::dateChangedSlot: date changed (" << date.year() << "/" << date.month() << "/" << date.day() << ")." << endl; | ||
195 | |||
196 | const KCalendarSystem * calendar = KGlobal::locale()->calendar(); | ||
197 | |||
198 | line->setText(KGlobal::locale()->formatDate(date, true)); | ||
199 | selectMonth->setText(calendar->monthName(date, false)); | ||
200 | fillWeeksCombo(date); | ||
201 | d->selectWeek->setCurrentItem(calendar->weekNumber(date) - 1); | ||
202 | selectYear->setText(calendar->yearString(date, false)); | ||
203 | |||
204 | emit(dateChanged(date)); | ||
205 | } | ||
206 | |||
207 | void | ||
208 | KDatePicker::tableClickedSlot() | ||
209 | { | ||
210 | kdDebug(298) << "KDatePicker::tableClickedSlot: table clicked." << endl; | ||
211 | emit(dateSelected(table->getDate())); | ||
212 | emit(tableClicked()); | ||
213 | } | ||
214 | |||
215 | const QDate& | ||
216 | KDatePicker::getDate() const | ||
217 | { | ||
218 | return table->getDate(); | ||
219 | } | ||
220 | |||
221 | const QDate & | ||
222 | KDatePicker::date() const | ||
223 | { | ||
224 | return table->getDate(); | ||
225 | } | ||
226 | |||
227 | bool | ||
228 | KDatePicker::setDate(const QDate& date) | ||
229 | { | ||
230 | if(date.isValid()) | ||
231 | { | ||
232 | const KCalendarSystem * calendar = KGlobal::locale()->calendar(); | ||
233 | |||
234 | table->setDate(date); | ||
235 | fillWeeksCombo(date); | ||
236 | d->selectWeek->setCurrentItem(calendar->weekNumber(date) - 1); | ||
237 | selectMonth->setText(calendar->monthName(date, false)); | ||
238 | selectYear->setText(calendar->yearString(date, true)); | ||
239 | line->setText(KGlobal::locale()->formatDate(date, true)); | ||
240 | return true; | ||
241 | } | ||
242 | else | ||
243 | { | ||
244 | kdDebug(298) << "KDatePicker::setDate: refusing to set invalid date." << endl; | ||
245 | return false; | ||
246 | } | ||
247 | } | ||
248 | |||
249 | void | ||
250 | KDatePicker::monthForwardClicked() | ||
251 | { | ||
252 | QDate temp; | ||
253 | temp = KGlobal::locale()->calendar()->addMonths( table->getDate(), 1 ); | ||
254 | |||
255 | setDate( temp ); | ||
256 | } | ||
257 | |||
258 | void | ||
259 | KDatePicker::monthBackwardClicked() | ||
260 | { | ||
261 | QDate temp; | ||
262 | temp = KGlobal::locale()->calendar()->addMonths( table->getDate(), -1 ); | ||
263 | |||
264 | setDate( temp ); | ||
265 | } | ||
266 | |||
267 | void | ||
268 | KDatePicker::yearForwardClicked() | ||
269 | { | ||
270 | QDate temp; | ||
271 | temp = KGlobal::locale()->calendar()->addYears( table->getDate(), 1 ); | ||
272 | |||
273 | setDate( temp ); | ||
274 | } | ||
275 | |||
276 | void | ||
277 | KDatePicker::yearBackwardClicked() | ||
278 | { | ||
279 | QDate temp; | ||
280 | temp = KGlobal::locale()->calendar()->addYears( table->getDate(), -1 ); | ||
281 | |||
282 | setDate( temp ); | ||
283 | } | ||
284 | |||
285 | void KDatePicker::selectWeekClicked() {} // ### in 3.2 obsolete; kept for binary compatibility | ||
286 | |||
287 | void | ||
288 | KDatePicker::weekSelected(int week) | ||
289 | { | ||
290 | week++; // week number starts with 1 | ||
291 | |||
292 | const KCalendarSystem * calendar = KGlobal::locale()->calendar(); | ||
293 | |||
294 | QDate date = table->getDate(); | ||
295 | int year = calendar->year(date); | ||
296 | |||
297 | calendar->setYMD(date, year, 1, 1); | ||
298 | date = calendar->addDays(date, -7); | ||
299 | while (calendar->weekNumber(date) != 1) | ||
300 | date = calendar->addDays(date, 1); | ||
301 | |||
302 | // date is now first day in week 1 some day in week 1 | ||
303 | date = calendar->addDays(date, (week - calendar->weekNumber(date)) * 7); | ||
304 | |||
305 | setDate(date); | ||
306 | } | ||
307 | |||
308 | void | ||
309 | KDatePicker::selectMonthClicked() | ||
310 | { | ||
311 | // every year can have different month names (in some calendar systems) | ||
312 | const KCalendarSystem * calendar = KGlobal::locale()->calendar(); | ||
313 | QDate date = table->getDate(); | ||
314 | int i, month, months = calendar->monthsInYear(date); | ||
315 | |||
316 | QPopupMenu popup(selectMonth); | ||
317 | |||
318 | for (i = 1; i <= months; i++) | ||
319 | popup.insertItem(calendar->monthName(i, calendar->year(date)), i); | ||
320 | |||
321 | popup.setActiveItem(calendar->month(date) - 1); | ||
322 | |||
323 | if ( (month = popup.exec(selectMonth->mapToGlobal(QPoint(0, 0)), calendar->month(date) - 1)) == -1 ) return; // canceled | ||
324 | |||
325 | int day = calendar->day(date); | ||
326 | // ----- construct a valid date in this month: | ||
327 | //date.setYMD(date.year(), month, 1); | ||
328 | //date.setYMD(date.year(), month, QMIN(day, date.daysInMonth())); | ||
329 | calendar->setYMD(date, calendar->year(date), month, | ||
330 | QMIN(day, calendar->daysInMonth(date))); | ||
331 | // ----- set this month | ||
332 | setDate(date); | ||
333 | } | ||
334 | |||
335 | void | ||
336 | KDatePicker::selectYearClicked() | ||
337 | { | ||
338 | const KCalendarSystem * calendar = KGlobal::locale()->calendar(); | ||
339 | |||
340 | int year; | ||
341 | KPopupFrame* popup = new KPopupFrame(this); | ||
342 | KDateInternalYearSelector* picker = new KDateInternalYearSelector(popup); | ||
343 | // ----- | ||
344 | picker->resize(picker->sizeHint()); | ||
345 | popup->setMainWidget(picker); | ||
346 | connect(picker, SIGNAL(closeMe(int)), popup, SLOT(close(int))); | ||
347 | picker->setFocus(); | ||
348 | if(popup->exec(selectYear->mapToGlobal(QPoint(0, selectMonth->height())))) | ||
349 | { | ||
350 | QDate date; | ||
351 | int day; | ||
352 | // ----- | ||
353 | year=picker->getYear(); | ||
354 | date=table->getDate(); | ||
355 | day=calendar->day(date); | ||
356 | // ----- construct a valid date in this month: | ||
357 | //date.setYMD(year, date.month(), 1); | ||
358 | //date.setYMD(year, date.month(), QMIN(day, date.daysInMonth())); | ||
359 | calendar->setYMD(date, year, calendar->month(date), | ||
360 | QMIN(day, calendar->daysInMonth(date))); | ||
361 | // ----- set this month | ||
362 | setDate(date); | ||
363 | } else { | ||
364 | KNotifyClient::beep(); | ||
365 | } | ||
366 | delete popup; | ||
367 | } | ||
368 | |||
369 | void | ||
370 | KDatePicker::setEnabled(bool enable) | ||
371 | { | ||
372 | QWidget *widgets[]= { | ||
373 | yearForward, yearBackward, monthForward, monthBackward, | ||
374 | selectMonth, selectYear, | ||
375 | line, table, d->selectWeek, d->todayButton }; | ||
376 | const int Size=sizeof(widgets)/sizeof(widgets[0]); | ||
377 | int count; | ||
378 | // ----- | ||
379 | for(count=0; count<Size; ++count) | ||
380 | { | ||
381 | widgets[count]->setEnabled(enable); | ||
382 | } | ||
383 | } | ||
384 | |||
385 | void | ||
386 | KDatePicker::lineEnterPressed() | ||
387 | { | ||
388 | QDate temp; | ||
389 | // ----- | ||
390 | if(val->date(line->text(), temp)==QValidator::Acceptable) | ||
391 | { | ||
392 | kdDebug(298) << "KDatePicker::lineEnterPressed: valid date entered." << endl; | ||
393 | emit(dateEntered(temp)); | ||
394 | setDate(temp); | ||
395 | } else { | ||
396 | KNotifyClient::beep(); | ||
397 | kdDebug(298) << "KDatePicker::lineEnterPressed: invalid date entered." << endl; | ||
398 | } | ||
399 | } | ||
400 | |||
401 | void | ||
402 | KDatePicker::todayButtonClicked() | ||
403 | { | ||
404 | setDate(QDate::currentDate()); | ||
405 | } | ||
406 | |||
407 | QSize | ||
408 | KDatePicker::sizeHint() const | ||
409 | { | ||
410 | return QWidget::sizeHint(); | ||
411 | } | ||
412 | |||
413 | void | ||
414 | KDatePicker::setFontSize(int s) | ||
415 | { | ||
416 | QWidget *buttons[]= { | ||
417 | // yearBackward, | ||
418 | // monthBackward, | ||
419 | selectMonth, | ||
420 | selectYear, | ||
421 | // monthForward, | ||
422 | // yearForward | ||
423 | }; | ||
424 | const int NoOfButtons=sizeof(buttons)/sizeof(buttons[0]); | ||
425 | int count; | ||
426 | QFont font; | ||
427 | QRect r; | ||
428 | // ----- | ||
429 | fontsize=s; | ||
430 | for(count=0; count<NoOfButtons; ++count) | ||
431 | { | ||
432 | font=buttons[count]->font(); | ||
433 | font.setPointSize(s); | ||
434 | buttons[count]->setFont(font); | ||
435 | } | ||
436 | QFontMetrics metrics(selectMonth->fontMetrics()); | ||
437 | |||
438 | for (int i = 1; ; ++i) | ||
439 | { | ||
440 | QString str = KGlobal::locale()->calendar()->monthName(i, | ||
441 | KGlobal::locale()->calendar()->year(table->getDate()), false); | ||
442 | if (str.isNull()) break; | ||
443 | r=metrics.boundingRect(str); | ||
444 | maxMonthRect.setWidth(QMAX(r.width(), maxMonthRect.width())); | ||
445 | maxMonthRect.setHeight(QMAX(r.height(), maxMonthRect.height())); | ||
446 | } | ||
447 | |||
448 | QSize metricBound = style().sizeFromContents(QStyle::CT_ToolButton, | ||
449 | selectMonth, | ||
450 | maxMonthRect); | ||
451 | selectMonth->setMinimumSize(metricBound); | ||
452 | |||
453 | table->setFontSize(s); | ||
454 | } | ||
455 | |||
456 | void | ||
457 | KDatePicker::setCloseButton( bool enable ) | ||
458 | { | ||
459 | if ( enable == (d->closeButton != 0L) ) | ||
460 | return; | ||
461 | |||
462 | if ( enable ) { | ||
463 | d->closeButton = new QToolButton( d->tb ); | ||
464 | d->navigationLayout->addWidget(d->closeButton); | ||
465 | QToolTip::add(d->closeButton, i18n("Close")); | ||
466 | d->closeButton->setPixmap( SmallIcon("remove") ); | ||
467 | connect( d->closeButton, SIGNAL( clicked() ), | ||
468 | topLevelWidget(), SLOT( close() ) ); | ||
469 | } | ||
470 | else { | ||
471 | delete d->closeButton; | ||
472 | d->closeButton = 0L; | ||
473 | } | ||
474 | |||
475 | updateGeometry(); | ||
476 | } | ||
477 | |||
478 | bool KDatePicker::hasCloseButton() const | ||
479 | { | ||
480 | return (d->closeButton != 0L); | ||
481 | } | ||
482 | |||
483 | void KDatePicker::virtual_hook( int /*id*/, void* /*data*/ ) | ||
484 | { /*BASE::virtual_hook( id, data );*/ } | ||
485 | |||