24#include "kptcalendarpanel.h"
25#include "kptdatetable.h"
26#include "kptcalendar.h"
30#include <tdeapplication.h>
32#include <kcalendarsystem.h>
33#include <kiconloader.h>
38#include <tqtoolbutton.h>
42#include <tqvalidator.h>
44#include <knotifyclient.h>
49class CalendarPanel::CalendarPanelPrivate
52 CalendarPanelPrivate() : closeButton(0L), selectWeek(0L) {}
54 TQToolButton *closeButton;
55 TQToolButton *selectWeek;
60 : TQFrame(parent,name, f)
66 : TQFrame(parent,name)
68 init( TQDate::currentDate() );
71void CalendarPanel::init(
const TQDate &dt )
79 line =
new KLineEdit(
this);
81 table =
new DateTable(
this, dt,
"Calendar table", 0);
84 d =
new CalendarPanelPrivate();
85 d->selectWeek =
new TQToolButton(
this );
91 TQToolTip::add(d->selectWeek, i18n(
"Select a week"));
92 TQToolTip::add(
selectMonth, i18n(
"Select a month"));
93 TQToolTip::add(
selectYear, i18n(
"Select a year"));
98 line->installEventFilter(
this );
99 yearForward->setPixmap(BarIcon(TQString::fromLatin1(
"2rightarrow")));
100 yearBackward->setPixmap(BarIcon(TQString::fromLatin1(
"2leftarrow")));
101 monthForward->setPixmap(BarIcon(TQString::fromLatin1(
"1rightarrow")));
102 monthBackward->setPixmap(BarIcon(TQString::fromLatin1(
"1leftarrow")));
104 connect(
table, TQ_SIGNAL(
dateChanged(TQDate)), TQ_SLOT(dateChangedSlot(TQDate)));
106 connect(
monthForward, TQ_SIGNAL(clicked()), TQ_SLOT(monthForwardClicked()));
107 connect(
monthBackward, TQ_SIGNAL(clicked()), TQ_SLOT(monthBackwardClicked()));
108 connect(
yearForward, TQ_SIGNAL(clicked()), TQ_SLOT(yearForwardClicked()));
109 connect(
yearBackward, TQ_SIGNAL(clicked()), TQ_SLOT(yearBackwardClicked()));
110 connect(d->selectWeek, TQ_SIGNAL(clicked()), TQ_SLOT(selectWeekClicked()));
111 connect(
selectMonth, TQ_SIGNAL(clicked()), TQ_SLOT(selectMonthClicked()));
112 connect(
selectYear, TQ_SIGNAL(clicked()), TQ_SLOT(selectYearClicked()));
113 connect(
line, TQ_SIGNAL(returnPressed()), TQ_SLOT(lineEnterPressed()));
115 connect(
table, TQ_SIGNAL(weekdaySelected(
int)), TQ_SLOT(slotWeekdaySelected(
int)));
116 connect(
table, TQ_SIGNAL(weekSelected(
int,
int)), TQ_SLOT(slotWeekSelected(
int,
int)));
130 if ( e->type() == TQEvent::KeyPress ) {
131 TQKeyEvent *k = (TQKeyEvent *)e;
133 if ( (k->key() == TQt::Key_Prior) ||
134 (k->key() == TQt::Key_Next) ||
135 (k->key() == TQt::Key_Up) ||
136 (k->key() == TQt::Key_Down) )
138 TQApplication::sendEvent(
table, e );
143 return TQFrame::eventFilter( o, e );
149 TQWidget *buttons[] = {
158 const int NoOfButtons=
sizeof(buttons)/
sizeof(buttons[0]);
159 TQSize sizes[NoOfButtons];
165 for(count=0; count<NoOfButtons; ++count) {
166 if ( buttons[count] ) {
167 sizes[count]=buttons[count]->sizeHint();
168 buttonHeight=TQMAX(buttonHeight, sizes[count].height());
171 sizes[count] = TQSize(0,0);
175 for(count=0; count<NoOfButtons; ++count) {
177 TQSize metricBound = style().sizeFromContents(TQStyle::CT_ToolButton,
selectMonth, maxMonthRect);
178 sizes[count].setWidth(TQMAX(metricBound.width(), maxMonthRect.width()+2*TQApplication::style().pixelMetric(TQStyle::PM_ButtonMargin)));
183 for(count=0; count<NoOfButtons; ++count)
185 w +=sizes[count].width();
187 x = (TQMAX(w, width())-w)/2;
190 for(count=0; count<NoOfButtons; ++count)
192 w=sizes[count].width();
193 if ( buttons[count] )
194 buttons[count]->setGeometry(x, 0, w, buttonHeight);
198 sizes[0]=
line->sizeHint();
199 int week_width=d->selectWeek->fontMetrics().width(i18n(
"Week XX"))+((d->closeButton != 0L) ? 50 : 20);
200 line->setGeometry(0, height()-sizes[0].height(), width()-week_width, sizes[0].height());
201 d->selectWeek->setGeometry(width()-week_width, height()-sizes[0].height(), week_width, sizes[0].height());
203 table->setGeometry(0, buttonHeight, width(),
204 height()-buttonHeight-sizes[0].height());
208CalendarPanel::dateChangedSlot(TQDate date)
211 line->setText(TDEGlobal::locale()->formatDate(
date,
true));
212 d->selectWeek->setText(i18n(
"Week %1").arg(weekOfYear(
date)));
213 selectMonth->setText(TDEGlobal::locale()->calendar()->monthName(
date.month(),
false));
219CalendarPanel::tableClickedSlot()
229 return table->getDate();
235 return table->getDate();
245 d->selectWeek->setText(i18n(
"Week %1").arg(weekOfYear(
date)));
246 selectMonth->setText(TDEGlobal::locale()->calendar()->monthName(
date.month(),
false));
247 temp.setNum(
date.year());
249 line->setText(TDEGlobal::locale()->formatDate(
date,
true));
252 kdDebug() <<
"CalendarPanel::setDate: refusing to set invalid date." << endl;
258CalendarPanel::monthForwardClicked()
264CalendarPanel::monthBackwardClicked()
270CalendarPanel::yearForwardClicked()
276CalendarPanel::yearBackwardClicked()
282CalendarPanel::selectWeekClicked()
285 PopupFrame* popup =
new PopupFrame(
this);
286 DateInternalWeekSelector* picker =
new DateInternalWeekSelector(fontsize, popup);
288 picker->resize(picker->sizeHint());
289 popup->setMainWidget(picker);
290 connect(picker, TQ_SIGNAL(closeMe(
int)), popup, TQ_SLOT(close(
int)));
292 if(popup->exec(d->selectWeek->mapToGlobal(TQPoint(0, d->selectWeek->height()))))
297 week=picker->getWeek();
301 date.setYMD(year, 1, 1);
302 while (weekOfYear(
date)>50)
304 while (weekOfYear(
date)<week && (week!=53 || (week==53 &&
305 (weekOfYear(
date)!=52 || weekOfYear(
date.addDays(1))!=1))))
307 if (week==53 && weekOfYear(
date)==52)
308 while (weekOfYear(
date.addDays(-1))==52)
313 KNotifyClient::beep();
319CalendarPanel::selectMonthClicked()
322 PopupFrame* popup =
new PopupFrame(
this);
323 DateInternalMonthPicker* picker =
new DateInternalMonthPicker(fontsize, popup);
325 picker->resize(picker->sizeHint());
326 popup->setMainWidget(picker);
328 connect(picker, TQ_SIGNAL(closeMe(
int)), popup, TQ_SLOT(close(
int)));
334 month=picker->getResult();
339 date.setYMD(
date.year(), month, TQMIN(day,
date.daysInMonth()));
343 KNotifyClient::beep();
349CalendarPanel::selectYearClicked()
352 PopupFrame* popup =
new PopupFrame(
this);
353 DateInternalYearSelector* picker =
new DateInternalYearSelector(fontsize, popup);
355 picker->resize(picker->sizeHint());
356 popup->setMainWidget(picker);
357 connect(picker, TQ_SIGNAL(closeMe(
int)), popup, TQ_SLOT(close(
int)));
364 year=picker->getYear();
369 date.setYMD(year,
date.month(), TQMIN(day,
date.daysInMonth()));
373 KNotifyClient::beep();
381 TQWidget *widgets[]= {
385 const int Size=
sizeof(widgets)/
sizeof(widgets[0]);
388 for(count=0; count<Size; ++count)
390 widgets[count]->setEnabled(enable);
392 table->setEnabled(enable);
396CalendarPanel::lineEnterPressed()
400 if(
val->date(
line->text(), temp)==TQValidator::Acceptable)
406 KNotifyClient::beep();
414 TQSize tableSize=
table->sizeHint();
415 TQWidget *buttons[]={
424 const int NoOfButtons=
sizeof(buttons)/
sizeof(buttons[0]);
425 TQSize sizes[NoOfButtons];
426 int cx=0, cy=0, count;
428 for(count=0; count<NoOfButtons; ++count) {
429 if ( buttons[count] )
430 sizes[count]=buttons[count]->sizeHint();
432 sizes[count] = TQSize(0,0);
435 TQSize metricBound = style().sizeFromContents(TQStyle::CT_ToolButton,
selectMonth, maxMonthRect);
436 cx+=TQMAX(metricBound.width(), maxMonthRect.width()+2*TQApplication::style().pixelMetric(TQStyle::PM_ButtonMargin));
438 cx+=sizes[count].width();
440 cy=TQMAX(sizes[count].height(), cy);
443 cx=TQMAX(cx, tableSize.width());
445 cy+=tableSize.height()+
line->sizeHint().height();
446 return TQSize(cx, cy);
452 TQWidget *buttons[]= {
460 const int NoOfButtons=
sizeof(buttons)/
sizeof(buttons[0]);
466 for(count=0; count<NoOfButtons; ++count)
468 font=buttons[count]->font();
469 font.setPointSize(s);
470 buttons[count]->setFont(font);
473 for(
int i=1; i <= 12; ++i)
475 r=metrics.boundingRect(TDEGlobal::locale()->calendar()->monthName(i,
false));
476 maxMonthRect.setWidth(TQMAX(r.width(), maxMonthRect.width()));
477 maxMonthRect.setHeight(TQMAX(r.height(), maxMonthRect.height()));
479 table->setFontSize(s);
485 if ( enable == (d->closeButton != 0L) )
489 d->closeButton =
new TQToolButton(
this );
490 TQToolTip::add(d->closeButton, i18n(
"Close"));
491 d->closeButton->setPixmap( SmallIcon(
"remove") );
492 connect( d->closeButton, TQ_SIGNAL( clicked() ),
493 topLevelWidget(), TQ_SLOT( close() ) );
496 delete d->closeButton;
505 return (d->closeButton != 0L);
508int CalendarPanel::weekOfYear(TQDate date)
511 int year, week, wday, jan1wday, nextjan1wday;
512 TQDate jan1date, nextjan1date;
515 wday=
date.dayOfWeek();
517 jan1date=TQDate(year,1,1);
518 jan1wday=jan1date.dayOfWeek();
520 week = (
date.dayOfYear()-1 + jan1wday-1)/7 + ((jan1wday-1) == 0 ? 1 : 0);
523 if ((week == 0) && (jan1wday > 4 )) {
524 TQDate tmpdate=TQDate(year-1,12,31);
525 return weekOfYear(tmpdate);
528 if ((jan1wday <= 4 ) && (jan1wday > 1 ))
532 nextjan1date=TQDate(year+1, 1, 1);
533 nextjan1wday = nextjan1date.dayOfWeek();
534 if (nextjan1wday <= 4 )
541void CalendarPanel::slotWeekdaySelected(
int day) {
543 emit weekdaySelected(day);
546void CalendarPanel::slotWeekSelected(
int week,
int year) {
548 emit weekSelected(week, year);
551void CalendarPanel::setCalendar(
Calendar *cal) {
555 table->setMarkedWeekdays(cal->weekdaysMap());
556 TQPtrListIterator<CalendarDay> it = cal->days();
558 for (; it.current(); ++it) {
559 if (it.current()->state() != Map::None) {
560 table->addMarkedDate(it.current()->date(), it.current()->state());
569DateMap CalendarPanel::selectedDates() {
570 return table->selectedDates();
573IntMap CalendarPanel::selectedWeekdays() {
574 return table->selectedWeekdays();
577DateMap CalendarPanel::markedDates() {
578 return table->markedDates();
581IntMap CalendarPanel::markedWeekdays() {
582 return table->markedWeekdays();
585void CalendarPanel::clear() {
590void CalendarPanel::markSelected(
int state) {
591 table->markSelected(state);
594void CalendarPanel::slotSelectionCleared() {
598void CalendarPanel::virtual_hook(
int ,
void* )
603#include "kptcalendarpanel.moc"
TQSize sizeHint() const
The size hint for date pickers.
void setCloseButton(bool enable)
By calling this method with enable = true, CalendarPanel will show a little close-button in the upper...
void setFontSize(int)
Sets the font size of the widgets elements.
CalendarPanel(TQWidget *parent=0, TQDate=TQDate::currentDate(), const char *name=0, WFlags f=0)
The usual constructor.
bool hasCloseButton() const
const TQDate & date() const
virtual void resizeEvent(TQResizeEvent *)
the resize event
virtual ~CalendarPanel()
The destructor.
TQToolButton * monthForward
the month forward button
TQLineEdit * line
the line edit to enter the date directly
void dateSelected(TQDate)
This signal is emitted each time a day has been selected by clicking on the table (hitting a day in t...
TQToolButton * selectMonth
the button for selecting the month directly
void dateEntered(TQDate)
This signal is emitted when enter is pressed and a VALID date has been entered before into the line e...
TQToolButton * yearBackward
the year backward button
TQToolButton * selectYear
the button for selecting the year directly
bool setDate(const TQDate &)
Sets the date.
TQToolButton * yearForward
the year forward button
const TQDate & getDate() const
Returns the selected date.
void setEnabled(bool)
Enables or disables the widget.
virtual bool eventFilter(TQObject *o, TQEvent *e)
to catch move keyEvents when TQLineEdit has keyFocus
void dateChanged(TQDate)
This signal is emitted each time the selected date is changed.
TQToolButton * monthBackward
the month backward button
DateTable * table
the date table
void tableClicked()
This signal is emitted when the day has been selected by clicking on it in the table.
DateValidator * val
the validator for the line edit:
void selectionCleared()
All selections have been cleared.
Calendar defines the working and nonworking days and hours.
Validates user-entered dates.