summaryrefslogtreecommitdiffabout
path: root/korganizer/kdatenavigator.cpp
Unidiff
Diffstat (limited to 'korganizer/kdatenavigator.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--korganizer/kdatenavigator.cpp364
1 files changed, 364 insertions, 0 deletions
diff --git a/korganizer/kdatenavigator.cpp b/korganizer/kdatenavigator.cpp
new file mode 100644
index 0000000..12e983b
--- a/dev/null
+++ b/korganizer/kdatenavigator.cpp
@@ -0,0 +1,364 @@
1/*
2 This file is part of KOrganizer.
3 Copyright (c) 2001,2002 Cornelius Schumacher <schumacher@kde.org>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19 As a special exception, permission is given to link this program
20 with any edition of Qt, and distribute the resulting executable,
21 without including the source code for Qt in the source distribution.
22*/
23
24#include <qstring.h>
25#include <qkeycode.h>
26#include <qlayout.h>
27#include <qtimer.h>
28#include <qframe.h>
29#include <qlabel.h>
30#include <qapplication.h>
31
32#include <kdebug.h>
33#include <klocale.h>
34#include <kglobal.h>
35
36#include "koglobals.h"
37#include "koprefs.h"
38#ifndef KORG_NOPLUGINS
39#include "kocore.h"
40#endif
41
42#include <kcalendarsystem.h>
43
44#include "navigatorbar.h"
45
46#include "kdatenavigator.h"
47
48KDateNavigator::KDateNavigator( QWidget *parent, Calendar *calendar,
49 bool show_week_nums, const char *name,
50 QDate startDate )
51 : QFrame(parent, name),
52 updateTimer(0L)
53{
54 mCalendar = calendar;
55
56 setFrameStyle(QFrame::NoFrame);
57
58 QGridLayout *topLayout = new QGridLayout(this,8,8);
59
60 if (! startDate.isValid()) {
61 kdDebug() << "KDateNavigator::KDateNavigator(): an invalid date was passed as a parameter!" << endl;
62 startDate = QDate::currentDate();
63 }
64
65 mSelectedDates.append(startDate);
66 m_MthYr = startDate;
67 m_bShowWeekNums = show_week_nums;
68
69 setFont( KOPrefs::instance()->mDateNavigatorFont );
70 mNavigatorBar = new NavigatorBar( startDate, this );
71 topLayout->addMultiCellWidget( mNavigatorBar, 0, 0, 0, 7 );
72 //mNavigatorBar->resize( 1,1);
73 connect( mNavigatorBar, SIGNAL( goPrevYear() ), SIGNAL( goPrevYear() ) );
74 connect( mNavigatorBar, SIGNAL( goPrevMonth() ), SIGNAL( goPrevMonth() ) );
75 connect( mNavigatorBar, SIGNAL( goNextMonth() ), SIGNAL( goNextMonth() ) );
76 connect( mNavigatorBar, SIGNAL( goNextYear() ), SIGNAL( goNextYear() ) );
77 connect( mNavigatorBar, SIGNAL( monthSelected( int ) ), SIGNAL( monthSelected( int ) ) );
78
79 // get the day of the week on the first day
80 QDate dayone(m_MthYr.year(), m_MthYr.month(), 1);
81 m_fstDayOfWk = dayone.dayOfWeek();
82
83 int i;
84
85 // Set up the heading fields.
86 for( i = 0; i < 7; i++ ) {
87 headings[i] = new QLabel("",this);
88 //headings[i]->setFont(QFont("Arial", 10, QFont::Bold));
89 headings[i]->setAlignment(AlignCenter);
90
91 topLayout->addWidget(headings[i],1,i+1);
92 }
93
94 // Create the weeknumber labels
95 for( i = 0; i < 6; i++ ) {
96 weeknos[i] = new QLabel(this);
97 weeknos[i]->setAlignment(AlignCenter);
98 //weeknos[i]->setFont(QFont("Arial", 10));
99 if(!show_week_nums) {
100 weeknos[i]->hide();
101 }
102 weeknos[i]->installEventFilter(this);
103
104 topLayout->addWidget(weeknos[i],i+2,0);
105 }
106
107 daymatrix = new KODayMatrix( this, mCalendar, dayone,
108 "KDateNavigator::DayMatrix");
109 daymatrix->setFrameStyle(QFrame::Panel|QFrame::Sunken);
110 daymatrix->setLineWidth(1);
111
112 connect( daymatrix, SIGNAL( selected( const KCal::DateList & ) ),
113 SIGNAL( datesSelected( const KCal::DateList & ) ) );
114
115 connect( daymatrix, SIGNAL( eventDropped( Event * ) ),
116 SIGNAL( eventDropped( Event * ) ) );
117
118 topLayout->addMultiCellWidget(daymatrix,2,7,1,7);
119
120 // read settings from configuration file.
121 updateConfig();
122 enableRollover(FollowMonth);
123 setFixedSize ( sizeHint() );
124}
125
126void KDateNavigator::enableRollover(RolloverType r)
127{
128 switch(r)
129 {
130 case None :
131 if (updateTimer)
132 {
133 updateTimer->stop();
134 delete updateTimer;
135 updateTimer=0L;
136 }
137 break;
138 case FollowDay :
139 case FollowMonth :
140 if (!updateTimer)
141 {
142 updateTimer = new QTimer(this);
143 QObject::connect(updateTimer,SIGNAL(timeout()),
144 this,SLOT(possiblyPastMidnight()));
145 }
146 updateTimer->start(0,true);
147 lastDayChecked = QDate::currentDate();
148 }
149 updateRollover=r;
150}
151
152
153KDateNavigator::~KDateNavigator()
154{
155}
156
157
158void KDateNavigator::passedMidnight()
159{
160 QDate today = QDate::currentDate();
161 bool emitMonth = false;
162
163 if (today.month() != lastDayChecked.month())
164 {
165 if (updateRollover==FollowMonth &&
166 daymatrix->isEndOfMonth()) {
167 goNextMonth();
168 emitMonth=true;
169 }
170 }
171 daymatrix->recalculateToday();
172 daymatrix->repaint();
173 emit dayPassed(today);
174 if (emitMonth) { emit monthPassed(today); }
175}
176
177/* slot */ void KDateNavigator::possiblyPastMidnight()
178{
179 if (lastDayChecked!=QDate::currentDate())
180 {
181 passedMidnight();
182 lastDayChecked=QDate::currentDate();
183 }
184 // Set the timer to go off 1 second after midnight
185 // or after 8 minutes, whichever comes first.
186 if (updateTimer)
187 {
188 QTime now = QTime::currentTime();
189 QTime midnight = QTime(23,59,59);
190 int msecsWait = QMIN(480000,now.msecsTo(midnight)+2000);
191
192 // qDebug(QString("Waiting %1 msec from %2 to %3.").arg(msecsWait))
193 //.arg(now.toString()).arg(midnight.toString()));
194
195 updateTimer->stop();
196 updateTimer->start(msecsWait,true);
197 }
198}
199
200void KDateNavigator::updateDates()
201{
202 // Find the first day of the week of the current month.
203 //int d1 = KOGlobals::self()->calendarSystem()->day( m_MthYr );
204 QDate dayone( m_MthYr.year(), m_MthYr.month(), m_MthYr.day() );
205 int d2 = KOGlobals::self()->calendarSystem()->day( dayone );
206 //int di = d1 - d2 + 1;
207 dayone = dayone.addDays( -d2 + 1 );
208
209 int m_fstDayOfWkCalsys = KOGlobals::self()->calendarSystem()->dayOfWeek( dayone );
210
211 // If month begins on Monday and Monday is first day of week,
212 // month should begin on second line. Sunday doesn't have this problem.
213 int nextLine = ( ( m_fstDayOfWkCalsys == 1) &&
214 ( KGlobal::locale()->weekStartsMonday() == 1 ) ) ? 7 : 0;
215
216 // update the matrix dates
217 int index = (KGlobal::locale()->weekStartsMonday() ? 1 : 0) - m_fstDayOfWkCalsys - nextLine;
218
219
220 daymatrix->updateView(dayone.addDays(index));
221//each updateDates is followed by an updateView -> repaint is issued there !
222// daymatrix->repaint();
223}
224
225void KDateNavigator::updateDayMatrix()
226{
227 daymatrix->updateView();
228 daymatrix->repaint();
229}
230
231
232void KDateNavigator::updateView()
233{
234
235 setUpdatesEnabled( false );
236
237 int i;
238
239// kdDebug() << "updateView() -> daymatrix->updateView()" << endl;
240 daymatrix->updateView();
241
242 // set the week numbers.
243 for(i = 0; i < 6; i++) {
244 QString weeknum;
245 // remember, according to ISO 8601, the first week of the year is the
246 // first week that contains a thursday. Thus we must subtract off 4,
247 // not just 1.
248
249 //ET int dayOfYear = buttons[(i + 1) * 7 - 4]->date().dayOfYear();
250 int dayOfYear = KOGlobals::self()->calendarSystem()->dayOfYear((daymatrix->getDate((i+1)*7-4)));
251
252 if (dayOfYear % 7 != 0)
253 weeknum.setNum(dayOfYear / 7 + 1);
254 else
255 weeknum.setNum(dayOfYear / 7);
256 weeknos[i]->setText(weeknum);
257 }
258
259 setUpdatesEnabled( true );
260// kdDebug() << "updateView() -> repaint()" << endl;
261 repaint();
262 daymatrix->repaint();
263}
264
265void KDateNavigator::updateConfig()
266{
267 int day;
268 for(int i=0; i<7; i++) {
269 // take the first letter of the day name to be the abbreviation
270 if (KGlobal::locale()->weekStartsMonday()) {
271 day = i+1;
272 } else {
273 if (i==0) day = 7;
274 else day = i;
275 }
276 QString dayName = KOGlobals::self()->calendarSystem()->weekDayName( day,
277 true );
278 if ( KOPrefs::instance()->mCompactDialogs ) dayName = dayName.left( 1 );
279 headings[i]->setText( dayName );
280 }
281 updateDates();
282 updateView();
283}
284
285void KDateNavigator::setShowWeekNums(bool enabled)
286{
287 m_bShowWeekNums = enabled;
288 for(int i=0; i<6; i++) {
289 if(enabled)
290 weeknos[i]->show();
291 else
292 weeknos[i]->hide();
293 }
294 resize(size());
295}
296
297void KDateNavigator::selectDates(const DateList& dateList)
298{
299
300 if (dateList.count() > 0) {
301 mNavigatorBar->selectDates( dateList );
302 mSelectedDates = dateList;
303
304 // set our record of the month and year that this datetbl is
305 // displaying.
306 m_MthYr = mSelectedDates.first();
307
308
309 // set our record of the first day of the week of the current
310 // month. This needs to be done before calling dayToIndex, since it
311 // relies on this information being up to date.
312 QDate dayone(m_MthYr.year(), m_MthYr.month(), 1);
313 m_fstDayOfWk = dayone.dayOfWeek();
314
315 updateDates();
316
317 daymatrix->setSelectedDaysFrom(*(dateList.begin()), *(--dateList.end()));
318
319 updateView();
320 }
321}
322
323int KDateNavigator::dayNum(int row, int col)
324{
325 return 7 * (row - 1) + (col + 1) - m_fstDayOfWk;
326}
327
328int KDateNavigator::dayToIndex(int dayNum)
329{
330 int row, col;
331
332 row = (dayNum+m_fstDayOfWk-1-(KGlobal::locale()->weekStartsMonday() ? 1 : 0)) / 7;
333 if (KGlobal::locale()->weekStartsMonday() && (m_fstDayOfWk == 1))
334 row++;
335 col = (dayNum+m_fstDayOfWk-1-(KGlobal::locale()->weekStartsMonday() ? 1 : 0)) % 7;
336 return row * 7 + col;
337}
338
339void KDateNavigator::wheelEvent (QWheelEvent *e)
340{
341 if(e->delta()>0) emit goPrevious();
342 else emit goNext();
343
344 e->accept();
345}
346
347bool KDateNavigator::eventFilter (QObject *o,QEvent *e)
348{
349 if (e->type() == QEvent::MouseButtonPress) {
350 int i;
351 for(i=0;i<6;++i) {
352 if (o == weeknos[i]) {
353 QDate weekstart = daymatrix->getDate(i*7);
354 emit weekClicked(weekstart);
355 break;
356 }
357 }
358 return true;
359 } else {
360 return false;
361 }
362}
363
364#include "kdatenavigator.moc"