22#include "kptdatetable.h"
25#include <tdeapplication.h>
26#include <knotifyclient.h>
27#include <kcalendarsystem.h>
29#include <tqdatetime.h>
36#include <tqvaluelist.h>
37#include <tdeglobalsettings.h>
44DateValidator::DateValidator(TQWidget* parent,
const char* name)
45 : TQValidator(parent, name)
50DateValidator::validate(TQString& text,
int&)
const
54 return date(text, temp);
58DateValidator::date(
const TQString& text, TQDate& d)
const
60 TQDate tmp = TDEGlobal::locale()->readDate(text);
70DateValidator::fixup( TQString& )
const
76DateTable::DateTable(TQWidget *parent, TQDate date_,
const char* name, WFlags f)
77 : TQGridView(parent, name, f),
82 m_selectedDates.clear();
83 m_selectedWeekdays.clear();
85 TQPair<int, int> p(0,0);
89 if(!date_.isValid()) {
90 kdError() <<k_funcinfo<<
"Given date is invalid, using current date." << endl;
91 date_=TQDate::currentDate();
93 setFocusPolicy( TQWidget::StrongFocus );
94 setNumCols(7+m_dateStartCol);
97 setHScrollBarMode(AlwaysOff);
98 setVScrollBarMode(AlwaysOff);
99 viewport()->setEraseColor(TDEGlobalSettings::baseColor());
102 colorBackgroundHoliday = TQColor(0, 245, 255, TQColor::Hsv);
104 colorBackgroundWorkday = TQColor(208, 230, 240, TQColor::Hsv);;
106 colorTextHoliday = black;
107 colorTextWorkday = black;
109 backgroundSelectColor = TDEGlobalSettings::highlightColor();
110 penSelectColor=TDEGlobalSettings::baseColor();
114void DateTable::paintWeekday(TQPainter *painter,
int col) {
119 TQFont font = TDEGlobalSettings::generalFont();
122 font.setItalic(
true);
123 painter->setFont(font);
125 int day = weekday(col);
129 painter->setBrush(TDEGlobalSettings::baseColor());
130 painter->setPen(TDEGlobalSettings::baseColor());
131 painter->drawRect(0, 0, w, h);
132 painter->setPen(TDEGlobalSettings::textColor());
134 if (m_markedWeekdays.state(day) == Map::Working) {
135 painter->setPen(colorBackgroundWorkday);
136 painter->setBrush(colorBackgroundWorkday);
137 painter->drawRect(0, 0, w, h);
138 painter->setPen(colorTextWorkday);
139 }
else if (m_markedWeekdays.state(day) == Map::NonWorking) {
140 painter->setPen(colorBackgroundHoliday);
141 painter->setBrush(colorBackgroundHoliday);
142 painter->drawRect(0, 0, w, h);
143 painter->setPen(colorTextHoliday);
145 if (m_selectedWeekdays.contains(day)) {
146 painter->setPen(backgroundSelectColor);
147 painter->setBrush(backgroundSelectColor);
148 painter->drawRect(2, 2, w-4, h-4);
149 painter->setPen(penSelectColor);
151 painter->drawText(0, 0, w, h-1, AlignCenter, TDEGlobal::locale()->calendar()->weekDayName(day,
true), -1, &rect);
152 painter->setPen(colorLine);
153 painter->moveTo(0, h-1);
154 painter->lineTo(w-1, h-1);
156 if(rect.width()>maxCell.width()) maxCell.setWidth(rect.width());
157 if(rect.height()>maxCell.height()) maxCell.setHeight(rect.height());
162void DateTable::paintWeekNumber(TQPainter *painter,
int row) {
167 TQFont font=TDEGlobalSettings::generalFont();
170 font.setItalic(
true);
171 painter->setFont(font);
173 painter->setBrush(TDEGlobalSettings::baseColor());
174 painter->setPen(TDEGlobalSettings::baseColor());
175 painter->drawRect(0, 0, w, h);
176 painter->setPen(TDEGlobalSettings::textColor());
178 painter->drawText(0, 0, w, h-1, AlignCenter, TQString(
"%1").arg(m_weeks[row].first), -1, &rect);
179 painter->setPen(colorLine);
180 painter->moveTo(w-1, 0);
181 painter->lineTo(w-1, h-1);
183 if(rect.width()>maxCell.width()) maxCell.setWidth(rect.width());
184 if(rect.height()>maxCell.height()) maxCell.setHeight(rect.height());
187void DateTable::paintDay(TQPainter *painter,
int row,
int col) {
193 TQFont font=TDEGlobalSettings::generalFont();
194 font.setPointSize(fontsize);
196 font.setItalic(
true);
197 painter->setFont(font);
199 TQDate d = getDate(position(row, col));
201 painter->setBrush(TDEGlobalSettings::baseColor());
202 painter->setPen(TDEGlobalSettings::baseColor());
203 painter->drawRect(0, 0, w, h);
206 if (m_markedDates.state(d) == Map::NonWorking) {
208 painter->setPen(colorBackgroundHoliday);
209 painter->setBrush(colorBackgroundHoliday);
210 painter->drawRect(0, 0, w, h);
211 }
else if (m_markedDates.state(d) == Map::Working) {
213 painter->setPen(colorBackgroundWorkday);
214 painter->setBrush(colorBackgroundWorkday);
215 painter->drawRect(0, 0, w, h);
217 if(m_selectedDates.contains(d)) {
219 painter->setPen(backgroundSelectColor);
220 painter->setBrush(backgroundSelectColor);
221 painter->drawRect(2, 2, w-4, h-4);
224 TQPen pen = painter->pen();
225 if (m_markedWeekdays.state(weekday(col)) == Map::Working) {
227 pen.setColor(colorBackgroundWorkday);
228 painter->setPen(pen);
229 painter->moveTo(0, 0);
230 painter->lineTo(0, h-1);
231 painter->moveTo(w-1, 0);
232 painter->lineTo(w-1, h-1);
235 if (d == TQDate::currentDate()) {
236 painter->setPen(colorLine);
237 painter->drawRect(1, 1, w-2, h-2);
241 d.month() == date.month() ? painter->setPen(TDEGlobalSettings::textColor()) : painter->setPen(gray);
242 painter->drawText(0, 0, w, h, AlignCenter, TQString().setNum(d.day()), -1, &rect);
244 if(rect.width()>maxCell.width()) maxCell.setWidth(rect.width());
245 if(rect.height()>maxCell.height()) maxCell.setHeight(rect.height());
248void DateTable::paintCell(TQPainter *painter,
int row,
int col) {
250 if (row == 0 && col == 0) {
254 painter->setPen(colorLine);
255 painter->setBrush(TDEGlobalSettings::baseColor());
256 painter->moveTo(w-1, 0);
257 painter->lineTo(w-1, h-1);
258 painter->lineTo(0, h-1);
264 paintWeekday(painter, col);
265 }
else if (col == 0) {
266 paintWeekNumber(painter, row);
268 paintDay(painter, row, col);
274void DateTable::keyPressEvent( TQKeyEvent *e ) {
277 if ( e->key() == TQt::Key_Prior ) {
278 setDate(date.addMonths(-1));
281 if ( e->key() == TQt::Key_Next ) {
282 setDate(date.addMonths(1));
286 if ( e->key() == TQt::Key_Up ) {
287 if ( date.day() > 7 ) {
288 setDate(date.addDays(-7));
292 if ( e->key() == TQt::Key_Down ) {
293 if ( date.day() <= date.daysInMonth()-7 ) {
294 setDate(date.addDays(7));
298 if ( e->key() == TQt::Key_Left ) {
299 if ( date.day() > 1 ) {
300 setDate(date.addDays(-1));
304 if ( e->key() == TQt::Key_Right ) {
305 if ( date.day() < date.daysInMonth() ) {
306 setDate(date.addDays(1));
311 if ( e->key() == TQt::Key_Minus ) {
312 setDate(date.addDays(-1));
315 if ( e->key() == TQt::Key_Plus ) {
316 setDate(date.addDays(1));
319 if ( e->key() == TQt::Key_N ) {
320 setDate(TQDate::currentDate());
323 if ( e->key() == TQt::Key_Control ) {
326 if ( e->key() == TQt::Key_Shift ) {
330 KNotifyClient::beep();
333void DateTable::viewportResizeEvent(TQResizeEvent * e) {
334 TQGridView::viewportResizeEvent(e);
336 setCellWidth(viewport()->width()/numCols());
337 setCellHeight(viewport()->height()/numRows());
340void DateTable::setFontSize(
int size) {
342 TQFontMetrics metrics(fontMetrics());
348 maxCell.setHeight(0);
349 for(count=0; count<7; ++count)
351 rect=metrics.boundingRect(TDEGlobal::locale()->calendar()->weekDayName(count+1,
true));
352 maxCell.setWidth(TQMAX(maxCell.width(), rect.width()));
353 maxCell.setHeight(TQMAX(maxCell.height(), rect.height()));
356 rect=metrics.boundingRect(TQString::fromLatin1(
"88"));
357 maxCell.setWidth(TQMAX(maxCell.width()+2, rect.width()));
358 maxCell.setHeight(TQMAX(maxCell.height()+4, rect.height()));
362void DateTable::wheelEvent ( TQWheelEvent * e ) {
363 setDate(date.addMonths( -(
int)(e->delta()/120)) );
368void DateTable::contentsMousePressEvent(TQMouseEvent *e) {
372 if(e->type()!=TQEvent::MouseButtonPress) {
375 TQPoint mouseCoord = e->pos();
376 int row=rowAt(mouseCoord.y());
377 int col=columnAt(mouseCoord.x());
378 if (row == 0 && col == 0) {
379 updateSelectedCells();
380 m_selectedWeekdays.clear();
381 m_selectedDates.clear();
382 repaintContents(
false);
383 emit selectionCleared();
387 updateSelectedCells();
388 m_selectedWeekdays.clear();
389 m_selectedDates.clear();
390 updateSelectedCells();
391 repaintContents(
false);
394 if (row==0 && col>0) {
395 updateSelectedCells();
396 m_selectedDates.clear();
397 int day = weekday(col);
398 if (e->state() & ShiftButton) {
402 for(
int i=m_dateStartCol; i < col; ++i) {
404 if (m_selectedWeekdays.contains(weekday(i))) {
407 m_selectedWeekdays.toggle(weekday(i));
410 bool selected = select;
412 for(
int i=7; i > col; --i) {
414 if (m_selectedWeekdays.contains(weekday(i))) {
415 if (selected) m_selectedWeekdays.toggle(weekday(i));
418 m_selectedWeekdays.toggle(weekday(i));
421 if (!m_selectedWeekdays.contains(day)) {
422 m_selectedWeekdays.toggle(day);
424 }
else if (e->state() & ControlButton) {
426 m_selectedWeekdays.toggle(day);
429 m_selectedWeekdays.toggleClear(day);
431 updateSelectedCells();
432 repaintContents(
false);
435 emit weekdaySelected(day);
440 if (contentsMousePressEvent_internal(e)) {
442 m_selectedWeekdays.clear();
443 if (e->state() & ShiftButton) {
447 DateMap::ConstIterator it;
448 for (it = m_selectedDates.constBegin(); it != m_selectedDates.constEnd(); ++it) {
450 TQDate d = TQDate::fromString(it.key(), TQt::ISODate);
453 if (!first.isValid() || first > d)
455 if (!last.isValid() || last < d)
459 m_selectedDates.clear();
460 if (first.isValid() && last.isValid()) {
461 TQDate anchor = first < date ? first : last;
462 int i = anchor > date ? -1 : 1;
463 while (anchor != date) {
465 m_selectedDates.toggle(anchor);
466 anchor = anchor.addDays(i);
469 m_selectedDates.toggle(date);
470 }
else if (e->state() & ControlButton) {
472 m_selectedDates.toggle(date);
476 m_selectedDates.clear();
477 m_selectedDates.toggleClear(date);
481 repaintContents(
false);
484bool DateTable::contentsMousePressEvent_internal(TQMouseEvent *e) {
485 TQPoint mouseCoord = e->pos();
486 int row=rowAt(mouseCoord.y());
487 int col=columnAt(mouseCoord.x());
492 selectDate(getDate(position(row, col)));
496bool DateTable::selectDate(
const TQDate& date_) {
501 if(!date_.isValid()) {
509 temp.setYMD(date.year(), date.month(), 1);
510 firstday=column(TDEGlobal::locale()->calendar()->dayOfWeek(temp));
511 if(firstday==1) firstday=8;
512 numdays=date.daysInMonth();
513 if(date.month()==1) {
514 temp.setYMD(date.year()-1, 12, 1);
515 setWeekNumbers(TQDate(date.year()-1, 12, 31));
517 temp.setYMD(date.year(), date.month()-1, 1);
518 TQDate d(date.year(), date.month()-1,1);
519 setWeekNumbers(d.addDays(d.daysInMonth()-1));
521 numDaysPrevMonth=temp.daysInMonth();
523 repaintContents(
false);
526 emit(dateChanged(date));
530bool DateTable::setDate(
const TQDate& date_,
bool repaint) {
535 if(!date_.isValid()) {
545 temp.setYMD(date.year(), date.month(), 1);
546 firstday=column(TDEGlobal::locale()->calendar()->dayOfWeek(temp));
547 if(firstday==1) firstday=8;
549 numdays=date.daysInMonth();
550 if(date.month()==1) {
551 temp.setYMD(date.year()-1, 12, 1);
552 setWeekNumbers(TQDate(date.year()-1, 12, 31));
554 temp.setYMD(date.year(), date.month()-1, 1);
555 TQDate d(date.year(), date.month()-1,1);
556 setWeekNumbers(d.addDays(d.daysInMonth()-1));
564 numDaysPrevMonth=temp.daysInMonth();
565 if(changed && repaint) {
566 repaintContents(
false);
569 emit(dateChanged(date));
573const TQDate& DateTable::getDate()
const {
577void DateTable::focusInEvent( TQFocusEvent *e ) {
578 TQGridView::focusInEvent( e );
581void DateTable::focusOutEvent( TQFocusEvent *e ) {
582 TQGridView::focusOutEvent( e );
585TQSize DateTable::sizeHint()
const {
586 if(maxCell.height()>0 && maxCell.width()>0) {
587 return TQSize(maxCell.width()*numCols()+2*frameWidth(),
588 (maxCell.height()+2)*numRows()+2*frameWidth());
591 return TQSize(-1, -1);
595void DateTable::setWeekNumbers(TQDate date) {
596 if (!date.isValid()) {
597 kdError()<<k_funcinfo<<
"Invalid date"<<endl;
600 for (
int i = 1; i < 7; ++i) {
601 m_weeks[i].first = d.weekNumber(&(m_weeks[i].second));
607void DateTable::updateCells() {
609 for (
int row=0; row < numRows(); ++row) {
610 for (
int col=0; col < numCols(); ++col) {
611 updateCell(row, col);
616void DateTable::updateSelectedCells() {
618 TQDate dt(date.year(), date.month(), 1);
619 dt = dt.addDays(-firstday);
620 for (
int pos=0; pos < 42; ++pos) {
621 if (m_selectedDates.contains(dt.addDays(pos)) ||
622 m_selectedWeekdays.contains(pos%7+1))
624 updateCell(pos/7+1, pos%7+1);
630void DateTable::updateMarkedCells() {
631 TQDate dt(date.year(), date.month(), 1);
632 dt = dt.addDays(-firstday);
633 for (
int pos=0; pos < 42; ++pos) {
634 if (m_markedDates.contains(dt.addDays(pos)) ||
635 m_markedWeekdays.contains(pos%7+1))
637 updateCell(pos/7+1, pos%7+1);
643void DateTable::setMarkedWeekdays(
const IntMap days) {
645 m_markedWeekdays.clear();
646 m_markedWeekdays = days;
648 repaintContents(
false);
651bool DateTable::weekdayMarked(
int day) {
652 return m_markedWeekdays.contains(day);
655bool DateTable::dateMarked(TQDate date) {
656 return m_markedDates[date.toString()];
659TQDate DateTable::getDate(
int pos)
const {
660 return TQDate(date.year(), date.month(), 1).addDays(pos-firstday);
663int DateTable::weekday(
int col)
const {
664 int day = col - m_dateStartCol + TDEGlobal::locale()->weekStartDay();
665 if (day > 7) day %= 7;
670int DateTable::column(
int weekday)
const {
671 int col = weekday - TDEGlobal::locale()->weekStartDay();
672 if (col < 0) col += 7;
674 return col + m_dateStartCol;
677void DateTable::clear() {
679 m_markedDates.clear();
680 m_markedWeekdays.clear();
681 repaintContents(
false);
684void DateTable::clearSelection() {
685 m_selectedDates.clear();
686 m_selectedWeekdays.clear();
687 repaintContents(
false);
690 void DateTable::setEnabled(
bool yes) {
691 if (m_enabled == yes)
697void DateTable::markSelected(
int state) {
698 if (!m_selectedDates.isEmpty()) {
699 DateMap::iterator it;
700 for(it = m_selectedDates.begin(); it != m_selectedDates.end(); ++it) {
701 m_markedDates.insert(it.key(), state);
704 }
else if (!m_selectedWeekdays.isEmpty()) {
706 for(it = m_selectedWeekdays.begin(); it != m_selectedWeekdays.end(); ++it) {
707 m_markedWeekdays.insert(it.key(), state);
711 updateSelectedCells();
712 repaintContents(
false);
715DateInternalWeekSelector::DateInternalWeekSelector
716(
int fontsize, TQWidget* parent,
const char* name)
717 : TQLineEdit(parent, name),
718 val(new TQIntValidator(this)),
723 font=TDEGlobalSettings::generalFont();
724 font.setPointSize(fontsize);
726 setFrameStyle(TQFrame::NoFrame);
727 val->setRange(1, 53);
729 connect(
this, TQ_SIGNAL(returnPressed()), TQ_SLOT(weekEnteredSlot()));
733DateInternalWeekSelector::weekEnteredSlot()
738 week=text().toInt(&ok);
741 KNotifyClient::beep();
749DateInternalWeekSelector::getWeek()
const
755DateInternalWeekSelector::setWeek(
int week)
763DateInternalMonthPicker::DateInternalMonthPicker
764(
int fontsize, TQWidget* parent,
const char* name)
765 : TQGridView(parent, name),
773 font=TDEGlobalSettings::generalFont();
774 font.setPointSize(fontsize);
776 setHScrollBarMode(AlwaysOff);
777 setVScrollBarMode(AlwaysOff);
778 setFrameStyle(TQFrame::NoFrame);
783 viewport()->setEraseColor(TDEGlobalSettings::baseColor());
786 TQFontMetrics metrics(font);
787 for(
int i=1; i <= 12; ++i)
789 rect=metrics.boundingRect(TDEGlobal::locale()->calendar()->monthName(i,
false));
790 if(
max.width()<rect.width())
max.setWidth(rect.width());
791 if(
max.height()<rect.height())
max.setHeight(rect.height());
799 return TQSize((
max.width()+6)*numCols()+2*frameWidth(),
800 (
max.height()+6)*numRows()+2*frameWidth());
812 p->setPen(TDEGlobalSettings::textColor());
818 setCellWidth(width()/3);
819 setCellHeight(height()/4);
829 text=TDEGlobal::locale()->calendar()->monthName(index,
false);
830 painter->drawText(0, 0, cellWidth(), cellHeight(), AlignCenter, text);
831 if (
activeCol == col && activeRow == row )
832 painter->drawRect( 0, 0, cellWidth(), cellHeight() );
838 if(!isEnabled() || e->button() != TQt::LeftButton)
840 KNotifyClient::beep();
847 mouseCoord = e->pos();
848 row=rowAt(mouseCoord.y());
849 col=columnAt(mouseCoord.x());
858 updateCell( row, col );
863DateInternalMonthPicker::contentsMouseMoveEvent(TQMouseEvent *e)
865 if (e->state() & TQt::LeftButton)
870 mouseCoord = e->pos();
871 row=rowAt(mouseCoord.y());
872 col=columnAt(mouseCoord.x());
873 int tmpRow = -1, tmpCol = -1;
884 bool differentCell = (activeRow != row ||
activeCol != col);
894 updateCell( row, col );
898 updateCell( tmpRow, tmpCol );
913 mouseCoord = e->pos();
914 row=rowAt(mouseCoord.y());
915 col=columnAt(mouseCoord.x());
927DateInternalYearSelector::DateInternalYearSelector
928(
int fontsize, TQWidget* parent,
const char* name)
929 : TQLineEdit(parent, name),
930 val(new TQIntValidator(this)),
935 font=TDEGlobalSettings::generalFont();
936 font.setPointSize(fontsize);
938 setFrameStyle(TQFrame::NoFrame);
940 val->setRange(0, 8000);
942 connect(
this, TQ_SIGNAL(returnPressed()), TQ_SLOT(yearEnteredSlot()));
946DateInternalYearSelector::yearEnteredSlot()
952 year=text().toInt(&ok);
955 KNotifyClient::beep();
958 date.setYMD(year, 1, 1);
961 KNotifyClient::beep();
969DateInternalYearSelector::getYear()
const
975DateInternalYearSelector::setYear(
int year)
984 : TQFrame(parent, name, WType_Popup),
988 setFrameStyle(TQFrame::Box|TQFrame::Raised);
995 if(e->key()==Key_Escape)
1015 resize(
main->width()+2*frameWidth(),
main->height()+2*frameWidth());
1024 main->setGeometry(frameWidth(), frameWidth(),
1025 width()-2*frameWidth(), height()-2*frameWidth());
1033 TQRect d = TQApplication::desktop()->screenGeometry(TQApplication::desktop()->screenNumber(pos));
1038 if (x+w > d.x()+d.width())
1040 if (y+h > d.y()+d.height())
1057 tqApp->enter_loop();
1065 return exec(TQPoint(x, y));
1068void PopupFrame::virtual_hook(
int,
void* )
1071void DateTable::virtual_hook(
int,
void* )
1076#include "kptdatetable.moc"
TQSize sizeHint() const
The size hint.
virtual void contentsMouseReleaseEvent(TQMouseEvent *e)
Emit monthSelected(int) when a cell has been released.
void closeMe(int)
This is send from the mouse click event handler.
int result
Store the month that has been clicked [1..12].
TQRect max
Contains the largest rectangle needed by the month names.
int getResult() const
Return the result.
virtual void contentsMousePressEvent(TQMouseEvent *e)
Catch mouse click and move events to paint a rectangle around the item.
virtual void viewportResizeEvent(TQResizeEvent *)
The resize event.
void setupPainter(TQPainter *p)
Set up the painter.
virtual void paintCell(TQPainter *painter, int row, int col)
Paint a cell.
short int activeCol
the cell under mouse cursor when LBM is pressed