korganizer

kdatenavigator.cpp
1/*
2 This file is part of KOrganizer.
3
4 Copyright (c) 2001,2002,2003 Cornelius Schumacher <schumacher@kde.org>
5 Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20
21 As a special exception, permission is given to link this program
22 with any edition of TQt, and distribute the resulting executable,
23 without including the source code for TQt in the source distribution.
24*/
25
26#include <tqstring.h>
27#include <tqkeycode.h>
28#include <tqlayout.h>
29#include <tqtimer.h>
30#include <tqframe.h>
31#include <tqlabel.h>
32
33#include <kdebug.h>
34#include <tdelocale.h>
35#include <tdeglobal.h>
36#include <tdeglobalsettings.h>
37
38#include "koglobals.h"
39#include "koprefs.h"
40#include "kodaymatrix.h"
41
42#include <kcalendarsystem.h>
43
44#include "navigatorbar.h"
45
46#include "kdatenavigator.h"
47
48KDateNavigator::KDateNavigator( TQWidget *parent, const char *name )
49 : TQFrame( parent, name ), mBaseDate( 1970, 1, 1 )
50{
51 TQGridLayout* topLayout = new TQGridLayout( this, 8, 8 );
52
53 mNavigatorBar = new NavigatorBar( this );
54 topLayout->addMultiCellWidget( mNavigatorBar, 0, 0, 0, 7 );
55
56 connect( mNavigatorBar, TQ_SIGNAL( prevYearClicked() ), TQ_SIGNAL( prevYearClicked() ) );
57 connect( mNavigatorBar, TQ_SIGNAL( prevMonthClicked() ), TQ_SIGNAL( prevMonthClicked() ) );
58 connect( mNavigatorBar, TQ_SIGNAL( nextMonthClicked() ), TQ_SIGNAL( nextMonthClicked() ) );
59 connect( mNavigatorBar, TQ_SIGNAL( nextYearClicked() ), TQ_SIGNAL( nextYearClicked() ) );
60 connect( mNavigatorBar, TQ_SIGNAL( monthSelected( int ) ), TQ_SIGNAL( monthSelected( int ) ) );
61 connect( mNavigatorBar, TQ_SIGNAL( yearSelected( int ) ), TQ_SIGNAL( yearSelected( int ) ) );
62
63 int i;
64 TQString generalFont = TDEGlobalSettings::generalFont().family();
65
66 // Set up the heading fields.
67 for( i = 0; i < 7; i++ ) {
68 mHeadings[i] = new TQLabel( this );
69 mHeadings[i]->setFont( TQFont( generalFont, 10, TQFont::Bold ) );
70 mHeadings[i]->setAlignment( AlignCenter );
71
72 topLayout->addWidget( mHeadings[i], 1, i + 1 );
73 }
74
75 // Create the weeknumber labels
76 for( i = 0; i < 6; i++ ) {
77 mWeeknos[i] = new TQLabel( this );
78 mWeeknos[i]->setAlignment( AlignCenter );
79 mWeeknos[i]->setFont( TQFont( generalFont, 10 ) );
80 mWeeknos[i]->installEventFilter( this );
81
82 topLayout->addWidget( mWeeknos[i], i + 2, 0 );
83 }
84
85 mDayMatrix = new KODayMatrix( this, "KDateNavigator::dayMatrix" );
86
87 connect( mDayMatrix, TQ_SIGNAL( selected( const KCal::DateList & ) ),
88 TQ_SIGNAL( datesSelected( const KCal::DateList & ) ) );
89
90 connect( mDayMatrix, TQ_SIGNAL( incidenceDropped( Incidence *, const TQDate & ) ),
91 TQ_SIGNAL( incidenceDropped( Incidence *, const TQDate & ) ) );
92 connect( mDayMatrix, TQ_SIGNAL( incidenceDroppedMove( Incidence * , const TQDate & ) ),
93 TQ_SIGNAL( incidenceDroppedMove( Incidence *, const TQDate & ) ) );
94
95
96 topLayout->addMultiCellWidget( mDayMatrix, 2, 7, 1, 7 );
97
98 // read settings from configuration file.
99 updateConfig();
100}
101
102KDateNavigator::~KDateNavigator()
103{
104}
105
106void KDateNavigator::setCalendar( Calendar *cal )
107{
108 mDayMatrix->setCalendar( cal );
109}
110
111void KDateNavigator::setBaseDate( const TQDate &date )
112{
113 if ( date != mBaseDate ) {
114 mBaseDate = date;
115
116 updateDates();
117 updateView();
118
119 // Use the base date to show the monthname and year in the header
120 KCal::DateList dates;
121 dates.append( date );
122 mNavigatorBar->selectDates( dates );
123
124 repaint();
125 mDayMatrix->repaint();
126 }
127}
128
129TQSizePolicy KDateNavigator::sizePolicy () const
130{
131 return TQSizePolicy( TQSizePolicy::MinimumExpanding,
132 TQSizePolicy::MinimumExpanding );
133}
134
135void KDateNavigator::updateToday()
136{
137 mDayMatrix->recalculateToday();
138 mDayMatrix->repaint();
139}
140
141TQDate KDateNavigator::startDate() const
142{
143 // Find the first day of the week of the current month.
144 TQDate dayone( mBaseDate.year(), mBaseDate.month(), mBaseDate.day() );
145 int d2 = KOGlobals::self()->calendarSystem()->day( dayone );
146 //int di = d1 - d2 + 1;
147 dayone = dayone.addDays( -d2 + 1 );
148
149
150 const KCalendarSystem *calsys = KOGlobals::self()->calendarSystem();
151 int m_fstDayOfWkCalsys = calsys->dayOfWeek( dayone );
152 int weekstart = TDEGlobal::locale()->weekStartDay();
153
154 // If month begins on Monday and Monday is first day of week,
155 // month should begin on second line. Sunday doesn't have this problem.
156 int nextLine = m_fstDayOfWkCalsys <= weekstart ? 7 : 0;
157
158 // update the matrix dates
159 int index = weekstart - m_fstDayOfWkCalsys - nextLine;
160
161 dayone = dayone.addDays( index );
162
163 return dayone;
164}
165
166TQDate KDateNavigator::endDate() const
167{
168 return startDate().addDays( 6*7 );
169}
170
171void KDateNavigator::updateDates()
172{
173// kdDebug(5850) << "KDateNavigator::updateDates(), this=" << this << endl;
174 TQDate dayone = startDate();
175
176 mDayMatrix->updateView( dayone );
177
178 const KCalendarSystem *calsys = KOGlobals::self()->calendarSystem();
179
180 // set the week numbers.
181 for( int i = 0; i < 6; i++ ) {
182 // Use TQDate's weekNumber method to determine the week number!
183 TQDate dtStart = mDayMatrix->getDate( i * 7 );
184 TQDate dtEnd = mDayMatrix->getDate( ( i + 1 ) * 7 - 1 );
185 int weeknumstart = calsys->weekNumber( dtStart );
186 int weeknumend = calsys->weekNumber( dtEnd );
187 TQString weeknum;
188
189 if ( weeknumstart != weeknumend ) {
190 weeknum = i18n("start/end week number of line in date picker", "%1/%2")
191 .arg( weeknumstart ).arg( weeknumend );
192 } else {
193 weeknum.setNum( weeknumstart );
194 }
195 mWeeknos[i]->setText( weeknum );
196 }
197
198// each updateDates is followed by an updateView -> repaint is issued there !
199// mDayMatrix->repaint();
200}
201
202void KDateNavigator::updateDayMatrix()
203{
204 mDayMatrix->updateView();
205 mDayMatrix->repaint();
206}
207
208void KDateNavigator::setUpdateNeeded()
209{
210 mDayMatrix->setUpdateNeeded();
211}
212
213TQDate KDateNavigator::month() const
214{
215 TQDate firstCell = startDate();
216 const KCalendarSystem *calSys = KOGlobals::self()->calendarSystem();
217
218 if ( calSys->day( firstCell ) == 1 ) {
219 return firstCell;
220 } else {
221 calSys->setYMD( firstCell, calSys->year( firstCell ), calSys->month( firstCell ), 1 );
222 return calSys->addMonths( firstCell, 1 );
223 }
224}
225
226void KDateNavigator::updateView()
227{
228// kdDebug(5850) << "KDateNavigator::updateView(), view " << this << endl;
229
230 updateDayMatrix();
231 repaint();
232}
233
234void KDateNavigator::updateConfig()
235{
236 int day;
237 int weekstart = TDEGlobal::locale()->weekStartDay();
238 for( int i = 0; i < 7; i++ ) {
239 day = weekstart + i <= 7 ? weekstart + i : ( weekstart + i ) % 7;
240 TQString dayName = KOGlobals::self()->calendarSystem()->weekDayName( day,
241 true );
242 if ( KOPrefs::instance()->mCompactDialogs ) dayName = dayName.left( 1 );
243 mHeadings[i]->setText( dayName );
244 }
245
246 // FIXME: Use actual config setting here
247// setShowWeekNums( true );
248}
249
250void KDateNavigator::setShowWeekNums( bool enabled )
251{
252 for( int i = 0; i < 6; i++ ) {
253 if ( enabled ) {
254 mWeeknos[i]->show();
255 } else {
256 mWeeknos[i]->hide();
257 }
258 }
259}
260
261void KDateNavigator::selectDates( const DateList &dateList )
262{
263 if ( dateList.count() > 0 ) {
264 mSelectedDates = dateList;
265
266 updateDates();
267
268 mDayMatrix->setSelectedDaysFrom( *( dateList.begin() ),
269 *( --dateList.end() ) );
270
271 updateView();
272 }
273}
274
275void KDateNavigator::wheelEvent( TQWheelEvent *e )
276{
277 if ( e->delta() > 0 ) {
278 emit goPrevious();
279 } else {
280 emit goNext();
281 }
282
283 e->accept();
284}
285
286bool KDateNavigator::eventFilter( TQObject *o, TQEvent *e )
287{
288 if ( e->type() == TQEvent::MouseButtonPress ) {
289 int i;
290 for( i = 0; i < 6; ++i ) {
291 if ( o == mWeeknos[ i ] ) {
292 TQDate weekstart = mDayMatrix->getDate( i * 7 );
293 emit weekClicked( weekstart );
294 break;
295 }
296 }
297 return true;
298 } else {
299 return false;
300 }
301}
302
303#include "kdatenavigator.moc"
Replacement for kdpdatebuton.cpp that used 42 widgets for the day matrix to be displayed.
Definition: kodaymatrix.h:106