/* This file is part of the KDE project Copyright (C) 1997 Tim D. Gilman (tdgilman@best.org) (C) 1998-2001 Mirko Boehm (mirko@kde.org) (C) 2004-2006 Dag Andersen This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ #include "kptdatetable.h" #include "kptmap.h" #include #include #include #include #include #include #include #include #include #include #include #include #include namespace KPlato { DateValidator::DateValidator(TQWidget* tqparent, const char* name) : TQValidator(TQT_TQOBJECT(tqparent), name) { } TQValidator::State DateValidator::validate(TQString& text, int&) const { TQDate temp; // ----- everything is tested in date(): return date(text, temp); } TQValidator::State DateValidator::date(const TQString& text, TQDate& d) const { TQDate tmp = KGlobal::locale()->readDate(text); if (!tmp.isNull()) { d = tmp; return Acceptable; } else return Valid; } void DateValidator::fixup( TQString& ) const { } DateTable::DateTable(TQWidget *tqparent, TQDate date_, const char* name, WFlags f) : TQGridView(tqparent, name, f), m_enabled(true) { //kdDebug()< p(0,0); m_weeks.fill(p, 7); setFontSize(10); if(!date_.isValid()) { kdError() <setEraseColor(KGlobalSettings::baseColor()); setDate(date_); // this initializes firstday, numdays, numDaysPrevMonth colorBackgroundHoliday = TQColor(0, 245, 255, TQColor::Hsv); //colorBackgroundHoliday = colorBackgroundHoliday.light(); colorBackgroundWorkday = TQColor(208, 230, 240, TQColor::Hsv);; //colorBackgroundWorkday = colorBackgroundWorkday.light(); colorTextHoliday = black; colorTextWorkday = black; colorLine = black; backgroundSelectColor = KGlobalSettings::highlightColor(); penSelectColor=KGlobalSettings::baseColor(); } void DateTable::paintWeekday(TQPainter *painter, int col) { TQRect rect; int w=cellWidth(); int h=cellHeight(); TQFont font = KGlobalSettings::generalFont(); font.setBold(true); if (!m_enabled) font.setItalic(true); painter->setFont(font); int day = weekday(col); //kdDebug()<setPen(colorLine); painter->moveTo(w-1, 0); painter->lineTo(w-1, h-1); if(rect.width()>maxCell.width()) maxCell.setWidth(rect.width()); if(rect.height()>maxCell.height()) maxCell.setHeight(rect.height()); } void DateTable::paintDay(TQPainter *painter, int row, int col) { //kdDebug()<setPen(colorBackgroundHoliday); painter->setBrush(colorBackgroundHoliday); painter->drawRect(0, 0, w, h); } else if (m_markedDates.state(d) == Map::Working) { //kdDebug()<setPen(colorBackgroundWorkday); painter->setBrush(colorBackgroundWorkday); painter->drawRect(0, 0, w, h); } if(m_selectedDates.tqcontains(d)) { //kdDebug()<setPen(backgroundSelectColor); painter->setBrush(backgroundSelectColor); painter->drawRect(2, 2, w-4, h-4); } // If weeks or weekdays are selected/marked we draw lines around the date TQPen pen = painter->pen(); if (m_markedWeekdays.state(weekday(col)) == Map::Working) { //kdDebug()<setPen(pen); painter->moveTo(0, 0); painter->lineTo(0, h-1); painter->moveTo(w-1, 0); painter->lineTo(w-1, h-1); } // then paint square if current date if (d == TQDate::tqcurrentDate()) { painter->setPen(colorLine); painter->drawRect(1, 1, w-2, h-2); } // and now the day number d.month() == date.month() ? painter->setPen(KGlobalSettings::textColor()) : painter->setPen(gray); painter->drawText(0, 0, w, h, AlignCenter, TQString().setNum(d.day()), -1, &rect); if(rect.width()>maxCell.width()) maxCell.setWidth(rect.width()); if(rect.height()>maxCell.height()) maxCell.setHeight(rect.height()); } void DateTable::paintCell(TQPainter *painter, int row, int col) { //kdDebug()<save(); int w=cellWidth(); int h=cellHeight(); painter->setPen(colorLine); painter->setBrush(KGlobalSettings::baseColor()); painter->moveTo(w-1, 0); painter->lineTo(w-1, h-1); painter->lineTo(0, h-1); painter->restore(); return; } painter->save(); if(row==0) { // we are drawing the weekdays paintWeekday(painter, col); } else if (col == 0) { // draw week numbers paintWeekNumber(painter, row); } else { // draw the day paintDay(painter, row, col); } painter->restore(); } //FIXME void DateTable::keyPressEvent( TQKeyEvent *e ) { if (!m_enabled) return; if ( e->key() == TQt::Key_Prior ) { setDate(date.addMonths(-1)); return; } if ( e->key() == TQt::Key_Next ) { setDate(date.addMonths(1)); return; } if ( e->key() == TQt::Key_Up ) { if ( date.day() > 7 ) { setDate(date.addDays(-7)); return; } } if ( e->key() == TQt::Key_Down ) { if ( date.day() <= date.daysInMonth()-7 ) { setDate(date.addDays(7)); return; } } if ( e->key() == TQt::Key_Left ) { if ( date.day() > 1 ) { setDate(date.addDays(-1)); return; } } if ( e->key() == TQt::Key_Right ) { if ( date.day() < date.daysInMonth() ) { setDate(date.addDays(1)); return; } } if ( e->key() == TQt::Key_Minus ) { setDate(date.addDays(-1)); return; } if ( e->key() == TQt::Key_Plus ) { setDate(date.addDays(1)); return; } if ( e->key() == TQt::Key_N ) { setDate(TQDate::tqcurrentDate()); return; } if ( e->key() == TQt::Key_Control ) { return; } if ( e->key() == TQt::Key_Shift ) { return; } KNotifyClient::beep(); } void DateTable::viewportResizeEvent(TQResizeEvent * e) { TQGridView::viewportResizeEvent(e); setCellWidth(viewport()->width()/numCols()); setCellHeight(viewport()->height()/numRows()); } void DateTable::setFontSize(int size) { int count; TQFontMetrics metrics(fontMetrics()); TQRect rect; // ----- store rectangles: fontsize=size; // ----- find largest day name: maxCell.setWidth(0); maxCell.setHeight(0); for(count=0; count<7; ++count) { rect=metrics.boundingRect(KGlobal::locale()->calendar()->weekDayName(count+1, true)); maxCell.setWidth(TQMAX(maxCell.width(), rect.width())); maxCell.setHeight(TQMAX(maxCell.height(), rect.height())); } // ----- compare with a real wide number and add some space: rect=metrics.boundingRect(TQString::tqfromLatin1("88")); maxCell.setWidth(TQMAX(maxCell.width()+2, rect.width())); maxCell.setHeight(TQMAX(maxCell.height()+4, rect.height())); } //FIXME void DateTable::wheelEvent ( TQWheelEvent * e ) { setDate(date.addMonths( -(int)(e->delta()/120)) ); e->accept(); } void DateTable::contentsMousePressEvent(TQMouseEvent *e) { if (!m_enabled) return; //kdDebug()<type()!=TQEvent::MouseButtonPress) { return; } TQPoint mouseCoord = e->pos(); int row=rowAt(mouseCoord.y()); int col=columnAt(mouseCoord.x()); if (row == 0 && col == 0) { // user clicked on (unused) upper left square updateSelectedCells(); m_selectedWeekdays.clear(); m_selectedDates.clear(); repaintContents(false); emit selectionCleared(); return; } if (col == 0) { // user clicked on week numbers updateSelectedCells(); m_selectedWeekdays.clear(); m_selectedDates.clear(); updateSelectedCells(); repaintContents(false); return; } if (row==0 && col>0) { // the user clicked on weekdays updateSelectedCells(); m_selectedDates.clear(); int day = weekday(col); if (e->state() & ShiftButton) { // select all days between this and the furthest away selected day, // check first downside - then upside, clear all others bool select = false; for(int i=m_dateStartCol; i < col; ++i) { //kdDebug()<<"Down["<state() & ShiftButton) { // find first&last date TQDate first; TQDate last; DateMap::ConstIterator it; for (it = m_selectedDates.constBegin(); it != m_selectedDates.constEnd(); ++it) { //kdDebug()< d) first = d; if (!last.isValid() || last < d) last = d; } // select between anchor and pressed date inclusive m_selectedDates.clear(); if (first.isValid() && last.isValid()) { TQDate anchor = first < date ? first : last; int i = anchor > date ? -1 : 1; while (anchor != date) { //kdDebug()<state() & ControlButton) { // toggle select this date m_selectedDates.toggle(date); //kdDebug()<calendar()->dayOfWeek(temp)); if(firstday==1) firstday=8; //kdDebug()<calendar()->dayOfWeek(temp))<<" firstday="<0 && maxCell.width()>0) { return TQSize(maxCell.width()*numCols()+2*frameWidth(), (maxCell.height()+2)*numRows()+2*frameWidth()); } else { //kdDebug() << "DateTable::tqsizeHint: obscure failure - " << endl; return TQSize(-1, -1); } } void DateTable::setWeekNumbers(TQDate date) { if (!date.isValid()) { kdError()<