35 #include <tdeconfig.h>
36 #include <tdeglobal.h>
37 #include <tdeglobalsettings.h>
38 #include <tdeapplication.h>
42 #include <knotifyclient.h>
43 #include <kcalendarsystem.h>
44 #include <tdeshortcut.h>
45 #include <tdestdaccel.h>
46 #include "kdatepicker.h"
48 #include "tdepopupmenu.h"
49 #include <tqdatetime.h>
50 #include <tqguardedptr.h>
53 #include <tqpainter.h>
59 class KDateTable::KDateTablePrivate
65 useCustomColors=
false;
75 struct DatePaintingMode
79 BackgroundMode bgMode;
81 TQDict <DatePaintingMode> customPaintingModes;
86 KDateValidator::KDateValidator(TQWidget* parent,
const char* name)
87 : TQValidator(parent,
name)
92 KDateValidator::validate(TQString& text,
int&)
const
96 return date(text, temp);
100 KDateValidator::date(
const TQString& text, TQDate& d)
const
112 KDateValidator::fixup( TQString& )
const
118 : TQGridView(parent, name, (f | TQt::WNoAutoErase))
120 d =
new KDateTablePrivate;
124 kdDebug() <<
"KDateTable ctor: WARNING: Given date is invalid, using current date." <<
endl;
125 date_=TQDate::currentDate();
127 setFocusPolicy( TQWidget::StrongFocus );
130 setHScrollBarMode(AlwaysOff);
131 setVScrollBarMode(AlwaysOff);
139 : TQGridView(parent, name, (f | TQt::WNoAutoErase))
141 d =
new KDateTablePrivate;
143 setFocusPolicy( TQWidget::StrongFocus );
146 setHScrollBarMode(AlwaysOff);
147 setVScrollBarMode(AlwaysOff);
149 setDate(TQDate::currentDate());
158 void KDateTable::initAccels()
161 accel->
insert(TDEStdAccel::Next,
this, TQ_SLOT(nextMonth()));
162 accel->
insert(TDEStdAccel::Prior,
this, TQ_SLOT(previousMonth()));
163 accel->
insert(TDEStdAccel::Home,
this, TQ_SLOT(beginningOfMonth()));
164 accel->
insert(TDEStdAccel::End,
this, TQ_SLOT(endOfMonth()));
174 int pos = calendar->
day( dt );
175 int offset = (
firstday - firstWeekDay + 7) % 7;
178 if ( offset < 1 ) offset += 7;
189 int offset = (
firstday - firstWeekDay + 7) % 7;
192 if ( offset < 1 ) offset += 7;
193 pCellDate = calendar->
addDays( pCellDate, pos - offset );
201 TQRect unusedRight = frameRect();
202 unusedRight.setLeft(gridSize().width());
204 TQRect unusedBottom = frameRect();
205 unusedBottom.setTop(gridSize().height());
207 paint->eraseRect(unusedRight);
208 paint->eraseRect(unusedBottom);
227 painter->setFont(font);
228 bool normalday =
true;
230 int daynum = ( col+firstWeekDay < 8 ) ? col+firstWeekDay :
233 ( daynum == 6 && calendar->
calendarName() ==
"gregorian" ) )
236 TQBrush brushInvertTitle(colorGroup().base());
241 painter->setPen(textColor);
242 painter->setBrush(textColor);
243 painter->drawRect(0, 0, w, h);
244 painter->setPen(titleColor);
246 painter->setPen(titleColor);
247 painter->setBrush(titleColor);
248 painter->drawRect(0, 0, w, h);
249 painter->setPen(textColor);
251 painter->drawText(0, 0, w, h-1, AlignCenter,
253 painter->setPen(colorGroup().text());
254 painter->moveTo(0, h-1);
255 painter->lineTo(w-1, h-1);
259 painter->setFont(font);
260 int pos=7*(row-1)+col;
264 text = calendar->
dayString(pCellDate,
true);
270 painter->setPen( colorGroup().mid() );
273 if ( d->useCustomColors )
275 KDateTablePrivate::DatePaintingMode *mode=d->customPaintingModes[pCellDate.toString()];
278 if (mode->bgMode != NoBgMode)
280 TQBrush oldbrush=painter->brush();
281 painter->setBrush( mode->bgColor );
284 case(CircleMode) : painter->drawEllipse(0,0,w,h);
break;
285 case(RectangleMode) : painter->drawRect(0,0,w,h);
break;
290 painter->setBrush( oldbrush );
293 painter->setPen( mode->fgColor );
295 painter->setPen(colorGroup().text());
297 painter->setPen(colorGroup().text());
306 if( (offset+d) == (pos+1))
311 painter->setPen(colorGroup().highlight());
312 painter->setBrush(colorGroup().highlight());
316 painter->setPen(colorGroup().text());
317 painter->setBrush(colorGroup().text());
319 pen=TQPen(colorGroup().highlightedText());
321 painter->setBrush(paletteBackgroundColor());
322 painter->setPen(paletteBackgroundColor());
327 if ( pCellDate == TQDate::currentDate() )
329 painter->setPen(colorGroup().text());
332 if ( paintRect ) painter->drawRect(0, 0, w, h);
333 painter->setPen(pen);
334 painter->drawText(0, 0, w, h, AlignCenter, text, -1, &rect);
337 if(rect.height()>
maxCell.height())
maxCell.setHeight(rect.height());
340 void KDateTable::nextMonth()
346 void KDateTable::previousMonth()
352 void KDateTable::beginningOfMonth()
357 void KDateTable::endOfMonth()
362 void KDateTable::beginningOfWeek()
367 void KDateTable::endOfWeek()
373 KDateTable::keyPressEvent( TQKeyEvent *e )
395 setDate(TQDate::currentDate());
417 TQGridView::viewportResizeEvent(e);
419 setCellWidth(viewport()->width()/7);
420 setCellHeight(viewport()->height()/7);
427 TQFontMetrics metrics(fontMetrics());
434 for(count=0; count<7; ++count)
437 ->weekDayName(count+1,
true));
442 rect=metrics.boundingRect(TQString::fromLatin1(
"88"));
448 KDateTable::wheelEvent ( TQWheelEvent * e )
458 if(e->type()!=TQEvent::MouseButtonPress)
469 int row, col, pos, temp;
472 mouseCoord = e->pos();
473 row=rowAt(mouseCoord.y());
474 col=columnAt(mouseCoord.x());
486 pos = (7 * (row - 1)) + col;
495 updateCell( temp/7+1, temp%7 );
496 updateCell( row, col );
500 if ( e->button() == TQt::RightButton && d->popupMenuEnabled )
505 menu->popup(e->globalPos());
517 kdDebug() <<
"KDateTable::setDate: refusing to set invalid date." <<
endl;
539 repaintContents(
false);
545 KDateTable::getDate()
const
551 void KDateTable::focusInEvent( TQFocusEvent *e )
554 TQGridView::focusInEvent( e );
557 void KDateTable::focusOutEvent( TQFocusEvent *e )
560 TQGridView::focusOutEvent( e );
568 return TQSize(
maxCell.width()*numCols()+2*frameWidth(),
569 (
maxCell.height()+2)*numRows()+2*frameWidth());
571 kdDebug() <<
"KDateTable::sizeHint: obscure failure - " <<
endl;
572 return TQSize(-1, -1);
578 d->popupMenuEnabled=enable;
583 return d->popupMenuEnabled;
588 if (!fgColor.isValid())
594 KDateTablePrivate::DatePaintingMode *mode=
new KDateTablePrivate::DatePaintingMode;
596 mode->fgColor=fgColor;
597 mode->bgColor=bgColor;
599 d->customPaintingModes.replace(
date.toString(), mode );
600 d->useCustomColors=
true;
606 d->customPaintingModes.remove(
date.toString() );
609 KDateInternalWeekSelector::KDateInternalWeekSelector
610 (TQWidget* parent,
const char* name)
611 : TQLineEdit(parent, name),
612 val(new TQIntValidator(this)),
619 setFrameStyle(TQFrame::NoFrame);
621 connect(
this, TQ_SIGNAL(returnPressed()), TQ_SLOT(weekEnteredSlot()));
625 KDateInternalWeekSelector::weekEnteredSlot()
630 week=text().toInt(&ok);
642 KDateInternalWeekSelector::getWeek()
648 KDateInternalWeekSelector::setWeek(
int week)
657 KDateInternalWeekSelector::setMaxWeek(
int max)
659 val->setRange(1, max);
665 class KDateInternalMonthPicker::KDateInternalMonthPrivate {
667 KDateInternalMonthPrivate (
int y,
int m,
int d)
668 : year(y), month(m), day(d)
680 (
const TQDate & date, TQWidget* parent,
const char* name)
681 : TQGridView(parent, name),
691 setHScrollBarMode(AlwaysOff);
692 setVScrollBarMode(AlwaysOff);
693 setFrameStyle(TQFrame::NoFrame);
695 d =
new KDateInternalMonthPrivate(date.year(), date.month(), date.day());
703 TQFontMetrics metrics(font);
704 for(
int i = 1; ; ++i)
708 if (str.isNull())
break;
709 rect=metrics.boundingRect(str);
710 if(
max.width()<rect.width())
max.setWidth(rect.width());
711 if(
max.height()<rect.height())
max.setHeight(rect.height());
718 return TQSize((
max.width()+6)*numCols()+2*frameWidth(),
719 (
max.height()+6)*numRows()+2*frameWidth());
737 setCellWidth(width() / numCols());
738 setCellHeight(height() / numRows());
751 painter->drawText(0, 0, cellWidth(), cellHeight(), AlignCenter, text);
752 if (
activeCol == col && activeRow == row )
753 painter->drawRect( 0, 0, cellWidth(), cellHeight() );
759 if(!isEnabled() || e->button() != TQt::LeftButton)
768 mouseCoord = e->pos();
769 row=rowAt(mouseCoord.y());
770 col=columnAt(mouseCoord.x());
779 updateCell( row, col );
784 KDateInternalMonthPicker::contentsMouseMoveEvent(TQMouseEvent *e)
786 if (e->state() & TQt::LeftButton)
791 mouseCoord = e->pos();
792 row=rowAt(mouseCoord.y());
793 col=columnAt(mouseCoord.x());
794 int tmpRow = -1, tmpCol = -1;
805 bool differentCell = (activeRow != row ||
activeCol != col);
815 updateCell( row, col );
819 updateCell( tmpRow, tmpCol );
834 mouseCoord = e->pos();
835 row=rowAt(mouseCoord.y());
836 col=columnAt(mouseCoord.x());
850 KDateInternalYearSelector::KDateInternalYearSelector
851 (TQWidget* parent,
const char* name)
852 : TQLineEdit(parent, name),
853 val(new TQIntValidator(this)),
860 setFrameStyle(TQFrame::NoFrame);
862 val->setRange(0, 8000);
864 connect(
this, TQ_SIGNAL(returnPressed()), TQ_SLOT(yearEnteredSlot()));
868 KDateInternalYearSelector::yearEnteredSlot()
874 year=text().toInt(&ok);
894 KDateInternalYearSelector::getYear()
900 KDateInternalYearSelector::setYear(
int year)
908 class TDEPopupFrame::TDEPopupFramePrivate
911 TDEPopupFramePrivate() :
exec(false) {}
917 : TQFrame(parent, name, (WFlags)WType_Popup),
920 d(new TDEPopupFramePrivate)
922 setFrameStyle(TQFrame::Box|TQFrame::Raised);
934 if(e->key()==Key_Escape)
967 resize(
main->width()+2*frameWidth(),
main->height()+2*frameWidth());
976 main->setGeometry(frameWidth(), frameWidth(),
977 width()-2*frameWidth(), height()-2*frameWidth());
991 if (x+w > d.x()+d.width())
993 if (y+h > d.y()+d.height())
1011 const TQGuardedPtr<TQObject> that =
this;
1012 tqApp->enter_loop();
1014 return TQDialog::Rejected;
1022 return exec(TQPoint(x, y));
1025 void TDEPopupFrame::virtual_hook(
int,
void* )
1028 void KDateTable::virtual_hook(
int,
void* )
1031 #include "kdatetbl.moc"
virtual TQString weekDayName(int weekDay, bool shortName=false) const=0
virtual int weekDayOfPray() const=0
virtual int daysInMonth(const TQDate &date) const=0
virtual int day(const TQDate &date) const=0
virtual TQString calendarName() const=0
virtual TQDate addDays(const TQDate &date, int ndays) const=0
virtual int month(const TQDate &date) const=0
virtual TQString dayString(const TQDate &pDate, bool bShort) const
virtual TQString monthName(int month, int year, bool shortName=false) const=0
virtual bool setYMD(TQDate &date, int y, int m, int d) const=0
virtual int year(const TQDate &date) const=0
virtual TQDate addMonths(const TQDate &date, int nmonths) const=0
virtual void contentsMousePressEvent(TQMouseEvent *e)
Catch mouse click and move events to paint a rectangle around the item.
int result
Store the month that has been clicked [1..12].
virtual void viewportResizeEvent(TQResizeEvent *)
The resize event.
~KDateInternalMonthPicker()
The destructor.
TQSize sizeHint() const
The size hint.
virtual void paintCell(TQPainter *painter, int row, int col)
Paint a cell.
KDateInternalMonthPicker(const TQDate &date, TQWidget *parent, const char *name=0)
The constructor.
int getResult() const
Return the result.
short int activeCol
the cell under mouse cursor when LBM is pressed
TQRect max
Contains the largest rectangle needed by the month names.
void closeMe(int)
This is send from the mouse click event handler.
void setupPainter(TQPainter *p)
Set up the painter.
virtual void contentsMouseReleaseEvent(TQMouseEvent *e)
Emit monthSelected(int) when a cell has been released.
int firstday
The day of the first day in the month [1..7].
virtual void paintCell(TQPainter *, int, int)
Paint a cell.
int numDaysPrevMonth
The number of days in the previous month.
TQDate date
The currently selected date.
void setFontSize(int size)
Set the font size of the date table.
~KDateTable()
The destructor.
virtual void viewportResizeEvent(TQResizeEvent *)
Handle the resize events.
virtual void contentsMousePressEvent(TQMouseEvent *)
React on mouse clicks that select a date.
TQRect maxCell
Save the size of the largest used cell content.
void setPopupMenuEnabled(bool enable)
Enables a popup menu when right clicking on a date.
void unsetCustomDatePainting(const TQDate &date)
Unsets the custom painting of a date so that the date is painted as usual.
void dateChanged(TQDate)
The selected date changed.
int posFromDate(const TQDate &date)
calculate the position of the cell in the matrix for the given date.
KDateTable(TQWidget *parent=0, TQDate date=TQDate::currentDate(), const char *name=0, WFlags f=0)
The constructor.
virtual void paintEmptyArea(TQPainter *, int, int, int, int)
Paint the empty area (background).
int fontsize
The font size of the displayed text.
bool popupMenuEnabled() const
Returns if the popup menu is enabled or not.
void setCustomDatePainting(const TQDate &date, const TQColor &fgColor, BackgroundMode bgMode=NoBgMode, const TQColor &bgColor=TQColor())
Makes a given date be painted with a given foregroundColor, and background (a rectangle,...
void tableClicked()
A date has been selected by clicking on the table.
bool setDate(const TQDate &)
Select and display this date.
virtual TQSize sizeHint() const
Returns a recommended size for the widget.
TQDate dateFromPos(int pos)
calculate the date that is displayed at a given cell in the matrix.
int numdays
The number of days in the current month.
void aboutToShowContextMenu(TDEPopupMenu *menu, const TQDate &date)
A popup menu for a given date is about to be shown (as when the user right clicks on that date and th...
bool readSettings(TDEConfigBase *pConfig=0)
TDEAccelAction * insert(const TQString &sAction, const TQString &sLabel, const TQString &sWhatsThis, const TDEShortcut &cutDef, const TQObject *pObjSlot, const char *psMethodSlot, bool bConfigurable=true, bool bEnabled=true)
static TQColor inactiveTextColor()
static TQColor baseColor()
static TQFont generalFont()
static TQColor activeTextColor()
static TQColor activeTitleColor()
static TQColor textColor()
static TQRect desktopGeometry(const TQPoint &point)
static TQColor inactiveTitleColor()
static TDELocale * locale()
const KCalendarSystem * calendar() const
TQDate readDate(const TQString &str, bool *ok=0) const
kndbgstream & endl(kndbgstream &s)
kdbgstream kdDebug(int area=0)
void beep(const TQString &reason=TQString::null)
TQString name(StdAccel id)