23 #include <tqpainter.h>
26 #include <tqtoolbutton.h>
27 #include <tqcombobox.h>
28 #include <tqtooltip.h>
30 #include <tqvalidator.h>
31 #include <tqpopupmenu.h>
34 #include "kdatepicker.h"
35 #include <tdeglobal.h>
36 #include <tdeapplication.h>
39 #include <kiconloader.h>
40 #include <tdetoolbar.h>
41 #include <klineedit.h>
43 #include <knotifyclient.h>
44 #include <kcalendarsystem.h>
47 #include "kdatepicker.moc"
52 class KDatePicker::KDatePickerPrivate
55 KDatePickerPrivate() : closeButton(0L), selectWeek(0L), todayButton(0), navigationLayout(0) {}
57 void fillWeeksCombo(
const TQDate &
date);
59 TQToolButton *closeButton;
60 TQComboBox *selectWeek;
61 TQToolButton *todayButton;
62 TQBoxLayout *navigationLayout;
65 void KDatePicker::fillWeeksCombo(
const TQDate &date)
73 d->selectWeek->clear();
79 int year = calendar->
year(date);
80 calendar->
setYMD(day, year, 1, 1);
82 TQDate lastDay, firstDayOfLastMonth;
83 calendar->
setYMD(firstDayOfLastMonth, year, lastMonth, 1);
84 calendar->
setYMD(lastDay, year, lastMonth, calendar->
daysInMonth(firstDayOfLastMonth));
86 for (; day <= lastDay ; day = calendar->
addDays(day, 7 ) )
88 TQString week = i18n(
"Week %1").arg(calendar->
weekNumber(day, &year));
89 if ( year != calendar->
year(day) ) week +=
"*";
90 d->selectWeek->insertItem(week);
94 if(day < lastDay && day.daysTo(lastDay) < 7 && calendar->
weekNumber(day) != calendar->
weekNumber(lastDay))
95 day = TQT_TQDATE_OBJECT(lastDay.addDays(-7));
100 : TQFrame(parent,name)
106 : TQFrame(parent,name, f)
112 : TQFrame(parent,name)
114 init( TQDate::currentDate() );
117 void KDatePicker::init(
const TQDate &dt )
119 d =
new KDatePickerPrivate();
121 TQBoxLayout * topLayout =
new TQVBoxLayout(
this);
123 d->navigationLayout =
new TQHBoxLayout(topLayout);
124 d->navigationLayout->addStretch();
148 d->navigationLayout->addStretch();
159 d->selectWeek =
new TQComboBox(
false,
this);
160 d->todayButton =
new TQToolButton(
this);
161 d->todayButton->setIconSet(SmallIconSet(
"today"));
167 TQToolTip::add(d->selectWeek, i18n(
"Select a week"));
168 TQToolTip::add(
selectMonth, i18n(
"Select a month"));
169 TQToolTip::add(
selectYear, i18n(
"Select a year"));
170 TQToolTip::add(d->todayButton, i18n(
"Select the current day"));
175 line->installEventFilter(
this );
176 if ( TQApplication::reverseLayout() )
178 yearForward->setIconSet(BarIconSet(TQString::fromLatin1(
"2leftarrow")));
179 yearBackward->setIconSet(BarIconSet(TQString::fromLatin1(
"2rightarrow")));
180 monthForward->setIconSet(BarIconSet(TQString::fromLatin1(
"1leftarrow")));
181 monthBackward->setIconSet(BarIconSet(TQString::fromLatin1(
"1rightarrow")));
185 yearForward->setIconSet(BarIconSet(TQString::fromLatin1(
"2rightarrow")));
186 yearBackward->setIconSet(BarIconSet(TQString::fromLatin1(
"2leftarrow")));
187 monthForward->setIconSet(BarIconSet(TQString::fromLatin1(
"1rightarrow")));
188 monthBackward->setIconSet(BarIconSet(TQString::fromLatin1(
"1leftarrow")));
190 connect(
table, TQT_SIGNAL(
dateChanged(TQDate)), TQT_SLOT(dateChangedSlot(TQDate)));
192 connect(
monthForward, TQT_SIGNAL(clicked()), TQT_SLOT(monthForwardClicked()));
193 connect(
monthBackward, TQT_SIGNAL(clicked()), TQT_SLOT(monthBackwardClicked()));
194 connect(
yearForward, TQT_SIGNAL(clicked()), TQT_SLOT(yearForwardClicked()));
195 connect(
yearBackward, TQT_SIGNAL(clicked()), TQT_SLOT(yearBackwardClicked()));
196 connect(d->selectWeek, TQT_SIGNAL(activated(
int)), TQT_SLOT(
weekSelected(
int)));
204 topLayout->addWidget(
table);
206 TQBoxLayout * bottomLayout =
new TQHBoxLayout(topLayout);
207 bottomLayout->addWidget(d->todayButton);
208 bottomLayout->addWidget(
line);
209 bottomLayout->addWidget(d->selectWeek);
223 if ( e->type() == TQEvent::KeyPress ) {
224 TQKeyEvent *k = (TQKeyEvent *)e;
226 if ( (k->key() == TQt::Key_Prior) ||
227 (k->key() == TQt::Key_Next) ||
228 (k->key() == Qt::Key_Up) ||
229 (k->key() == Qt::Key_Down) )
231 TQApplication::sendEvent(
table, e );
236 return TQFrame::eventFilter( o, e );
242 TQWidget::resizeEvent(e);
246 KDatePicker::dateChangedSlot(TQDate date)
248 kdDebug(298) <<
"KDatePicker::dateChangedSlot: date changed (" << date.year() <<
"/" << date.month() <<
"/" << date.day() <<
")." <<
endl;
254 fillWeeksCombo(date);
258 calendar->
setYMD(firstDay, calendar->
year(date), 1, 1);
259 d->selectWeek->setCurrentItem((calendar->
dayOfYear(date) + calendar->
dayOfWeek(firstDay) - 2) / 7);
266 KDatePicker::tableClickedSlot()
268 kdDebug(298) <<
"KDatePicker::tableClickedSlot: table clicked." <<
endl;
276 return table->getDate();
282 return table->getDate();
295 kdDebug(298) <<
"KDatePicker::setDate: refusing to set invalid date." <<
endl;
301 KDatePicker::monthForwardClicked()
310 KDatePicker::monthBackwardClicked()
319 KDatePicker::yearForwardClicked()
328 KDatePicker::yearBackwardClicked()
343 TQDate date =
table->getDate();
344 int year = calendar->
year(date);
346 calendar->
setYMD(date, year, 1, 1);
359 TQDate date =
table->getDate();
364 for (i = 1; i <= months; i++)
365 popup.insertItem(calendar->
monthName(i, calendar->
year(date)), i);
367 popup.setActiveItem(calendar->
month(date) - 1);
369 if ( (month = popup.exec(
selectMonth->mapToGlobal(TQPoint(0, 0)), calendar->
month(date) - 1)) == -1 )
return;
371 int day = calendar->
day(date);
373 calendar->
setYMD(date, calendar->
year(date), month, 1);
374 date = TQT_TQDATE_OBJECT(date.addDays(TQMIN(day, calendar->
daysInMonth(date)) - 1));
393 picker->resize(picker->sizeHint());
394 picker->setYear(
table->getDate().year() );
397 connect(picker, TQT_SIGNAL(closeMe(
int)), popup, TQT_SLOT(close(
int)));
404 year=picker->getYear();
405 date=
table->getDate();
406 day=calendar->
day(date);
410 calendar->
setYMD(date, year, calendar->
month(date),
419 TQTimer::singleShot(0,
this, TQT_SLOT(ensureSelectYearIsUp()));
423 KDatePicker::ensureSelectYearIsUp()
434 TQWidget *widgets[]= {
437 line,
table, d->selectWeek, d->todayButton };
438 const int Size=
sizeof(widgets)/
sizeof(widgets[0]);
441 for(count=0; count<Size; ++count)
443 widgets[count]->setEnabled(enable);
452 if(
val->date(
line->text(), temp)==TQValidator::Acceptable)
454 kdDebug(298) <<
"KDatePicker::lineEnterPressed: valid date entered." <<
endl;
459 kdDebug(298) <<
"KDatePicker::lineEnterPressed: invalid date entered." <<
endl;
466 setDate(TQDate::currentDate());
472 return TQWidget::sizeHint();
478 TQWidget *buttons[]= {
486 const int NoOfButtons=
sizeof(buttons)/
sizeof(buttons[0]);
492 for(count=0; count<NoOfButtons; ++count)
494 font=buttons[count]->font();
495 font.setPointSize(s);
496 buttons[count]->setFont(font);
500 for (
int i = 1; ; ++i)
504 if (str.isNull())
break;
505 r=metrics.boundingRect(str);
510 TQSize metricBound = style().tqsizeFromContents(TQStyle::CT_ToolButton,
521 if ( enable == (d->closeButton != 0L) )
525 d->closeButton =
new TQToolButton(
this );
526 d->closeButton->setAutoRaise(
true);
528 d->navigationLayout->addWidget(d->closeButton);
529 TQToolTip::add(d->closeButton, i18n(
"Close"));
530 d->closeButton->setPixmap( SmallIcon(
"remove") );
531 connect( d->closeButton, TQT_SIGNAL( clicked() ),
532 topLevelWidget(), TQT_SLOT( close() ) );
535 delete d->closeButton;
544 return (d->closeButton);
547 void KDatePicker::virtual_hook(
int ,
void* )
TQSize sizeHint() const
The size hint for date pickers.
virtual TQString monthName(int month, int year, bool shortName=false) const =0
virtual TQString yearString(const TQDate &pDate, bool bShort) const
void todayButtonClicked()
void setFontSize(int size)
Set the font size of the date table.
virtual TQDate addMonths(const TQDate &date, int nmonths) const =0
const TQDate & getDate() const KDE_DEPRECATED
Returns the selected date.
void beep(const TQString &reason=TQString::null)
virtual TQDate addYears(const TQDate &date, int nyears) const =0
const TQDate & date() const
virtual int year(const TQDate &date) const =0
virtual int dayOfWeek(const TQDate &date) const =0
virtual int monthsInYear(const TQDate &date) const =0
TQToolButton * monthForward
the month forward button
void setEnabled(bool)
Enables or disables the widget.
TQToolButton * selectYear
the button for selecting the year directly
virtual int weekNumber(const TQDate &date, int *yearNum=0) const =0
KDatePicker(TQWidget *parent=0, TQDate=TQDate::currentDate(), const char *name=0)
The usual constructor.
kdbgstream kdDebug(int area=0)
TQLineEdit * line
the line edit to enter the date directly
bool setDate(const TQDate &)
Select and display this date.
virtual int daysInMonth(const TQDate &date) const =0
virtual int dayOfYear(const TQDate &date) const =0
TQToolButton * monthBackward
the month backward button
static int spacingHint()
Return the number of pixels you shall use between widgets inside a dialog according to the KDE standa...
static TQFont generalFont()
KDateTable * table
the date table
virtual bool eventFilter(TQObject *o, TQEvent *e)
to catch move keyEvents when TQLineEdit has keyFocus
bool hasCloseButton() const
virtual ~KDatePicker()
The destructor.
void dateSelected(TQDate)
This signal is emitted each time a day has been selected by clicking on the table (hitting a day in t...
virtual int month(const TQDate &date) const =0
Validates user-entered dates.
const KCalendarSystem * calendar() const
KDateValidator * val
the validator for the line edit:
virtual int day(const TQDate &date) const =0
An enhanced TQLineEdit widget for inputting text.
static TDELocale * locale()
void setCloseButton(bool enable)
By calling this method with enable = true, KDatePicker will show a little close-button in the upper b...
bool setDate(const TQDate &)
Sets the date.
void dateEntered(TQDate)
This signal is emitted when enter is pressed and a VALID date has been entered before into the line e...
void selectMonthClicked()
TQToolButton * yearBackward
the year backward button
kndbgstream & endl(kndbgstream &s)
void dateChanged(TQDate)
This signal is emitted each time the selected date is changed.
TQSize maxMonthRect
the size calculated during resize events
virtual TQDate addDays(const TQDate &date, int ndays) const =0
void setFontSize(int)
Sets the font size of the widgets elements.
void tableClicked()
This signal is emitted when the day has been selected by clicking on it in the table.
TQToolButton * selectMonth
the button for selecting the month directly
virtual void resizeEvent(TQResizeEvent *)
the resize event
TQToolButton * yearForward
the year forward button
virtual bool setYMD(TQDate &date, int y, int m, int d) const =0