-rw-r--r-- | core/pim/datebook/datebookweek.cpp | 35 |
1 files changed, 15 insertions, 20 deletions
diff --git a/core/pim/datebook/datebookweek.cpp b/core/pim/datebook/datebookweek.cpp index ddd41af..e30c776 100644 --- a/core/pim/datebook/datebookweek.cpp +++ b/core/pim/datebook/datebookweek.cpp | |||
@@ -1,679 +1,674 @@ | |||
1 | /********************************************************************** | 1 | /********************************************************************** |
2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. | 2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. |
3 | ** | 3 | ** |
4 | ** This file is part of Qtopia Environment. | 4 | ** This file is part of Qtopia Environment. |
5 | ** | 5 | ** |
6 | ** This file may be distributed and/or modified under the terms of the | 6 | ** This file may be distributed and/or modified under the terms of the |
7 | ** GNU General Public License version 2 as published by the Free Software | 7 | ** GNU General Public License version 2 as published by the Free Software |
8 | ** Foundation and appearing in the file LICENSE.GPL included in the | 8 | ** Foundation and appearing in the file LICENSE.GPL included in the |
9 | ** packaging of this file. | 9 | ** packaging of this file. |
10 | ** | 10 | ** |
11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | 11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE |
12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | 12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
13 | ** | 13 | ** |
14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | 14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. |
15 | ** | 15 | ** |
16 | ** Contact info@trolltech.com if any conditions of this licensing are | 16 | ** Contact info@trolltech.com if any conditions of this licensing are |
17 | ** not clear to you. | 17 | ** not clear to you. |
18 | ** | 18 | ** |
19 | **********************************************************************/ | 19 | **********************************************************************/ |
20 | #include "datebookweek.h" | 20 | #include "datebookweek.h" |
21 | #include "datebookweekheaderimpl.h" | 21 | #include "datebookweekheaderimpl.h" |
22 | 22 | ||
23 | #include <qpe/calendar.h> | 23 | #include <qpe/calendar.h> |
24 | #include <qpe/datebookdb.h> | 24 | #include <qpe/datebookdb.h> |
25 | #include <qpe/event.h> | 25 | #include <qpe/event.h> |
26 | #include <qpe/qpeapplication.h> | 26 | #include <qpe/qpeapplication.h> |
27 | #include <qpe/timestring.h> | 27 | #include <qpe/timestring.h> |
28 | 28 | ||
29 | #include <qdatetime.h> | 29 | #include <qdatetime.h> |
30 | #include <qheader.h> | 30 | #include <qheader.h> |
31 | #include <qlabel.h> | 31 | #include <qlabel.h> |
32 | #include <qlayout.h> | 32 | #include <qlayout.h> |
33 | #include <qpainter.h> | 33 | #include <qpainter.h> |
34 | #include <qpopupmenu.h> | 34 | #include <qpopupmenu.h> |
35 | #include <qtimer.h> | 35 | #include <qtimer.h> |
36 | #include <qspinbox.h> | 36 | #include <qspinbox.h> |
37 | #include <qstyle.h> | 37 | #include <qstyle.h> |
38 | 38 | ||
39 | //----------------------------------------------------------------- | 39 | //----------------------------------------------------------------- |
40 | 40 | ||
41 | 41 | ||
42 | DateBookWeekItem::DateBookWeekItem( const EffectiveEvent e ) | 42 | DateBookWeekItem::DateBookWeekItem( const EffectiveEvent e ) |
43 | : ev( e ) | 43 | : ev( e ) |
44 | { | 44 | { |
45 | // with the current implementation change the color for all day events | 45 | // with the current implementation change the color for all day events |
46 | if ( ev.event().type() == Event::AllDay && !ev.event().hasAlarm() ) { | 46 | if ( ev.event().type() == Event::AllDay && !ev.event().hasAlarm() ) { |
47 | c = Qt::green; | 47 | c = Qt::green; |
48 | } else { | 48 | } else { |
49 | c = ev.event().hasAlarm() ? Qt::red : Qt::blue; | 49 | c = ev.event().hasAlarm() ? Qt::red : Qt::blue; |
50 | } | 50 | } |
51 | } | 51 | } |
52 | 52 | ||
53 | void DateBookWeekItem::setGeometry( int x, int y, int w, int h ) | 53 | void DateBookWeekItem::setGeometry( int x, int y, int w, int h ) |
54 | { | 54 | { |
55 | r.setRect( x, y, w, h ); | 55 | r.setRect( x, y, w, h ); |
56 | } | 56 | } |
57 | 57 | ||
58 | 58 | ||
59 | //------------------=--------------------------------------------- | 59 | //------------------=--------------------------------------------- |
60 | 60 | ||
61 | DateBookWeekView::DateBookWeekView( bool ap, bool startOnMonday, | 61 | DateBookWeekView::DateBookWeekView( bool ap, bool startOnMonday, |
62 | QWidget *parent, const char *name ) | 62 | QWidget *parent, const char *name ) |
63 | : QScrollView( parent, name ), ampm( ap ), bOnMonday( startOnMonday ), | 63 | : QScrollView( parent, name ), ampm( ap ), bOnMonday( startOnMonday ), |
64 | showingEvent( false ) | 64 | showingEvent( false ) |
65 | { | 65 | { |
66 | items.setAutoDelete( true ); | 66 | items.setAutoDelete( true ); |
67 | 67 | ||
68 | viewport()->setBackgroundMode( PaletteBase ); | 68 | viewport()->setBackgroundMode( PaletteBase ); |
69 | 69 | ||
70 | header = new QHeader( this ); | 70 | header = new QHeader( this ); |
71 | header->addLabel( "" ); | 71 | header->addLabel( "" ); |
72 | 72 | ||
73 | header->setMovingEnabled( false ); | 73 | header->setMovingEnabled( false ); |
74 | header->setResizeEnabled( false ); | 74 | header->setResizeEnabled( false ); |
75 | header->setClickEnabled( false, 0 ); | 75 | header->setClickEnabled( false, 0 ); |
76 | initNames(); | 76 | initNames(); |
77 | 77 | ||
78 | 78 | ||
79 | connect( header, SIGNAL(clicked(int)), this, SIGNAL(showDay(int)) ); | 79 | connect( header, SIGNAL(clicked(int)), this, SIGNAL(showDay(int)) ); |
80 | 80 | ||
81 | QObject::connect(qApp, SIGNAL(clockChanged(bool)), | 81 | QObject::connect(qApp, SIGNAL(clockChanged(bool)), |
82 | this, SLOT(slotChangeClock(bool))); | 82 | this, SLOT(slotChangeClock(bool))); |
83 | 83 | ||
84 | QFontMetrics fm( font() ); | 84 | QFontMetrics fm( font() ); |
85 | rowHeight = fm.height()+2; | 85 | rowHeight = fm.height()+2; |
86 | 86 | ||
87 | resizeContents( width(), 24*rowHeight ); | 87 | resizeContents( width(), 24*rowHeight ); |
88 | } | 88 | } |
89 | 89 | ||
90 | void DateBookWeekView::initNames() | 90 | void DateBookWeekView::initNames() |
91 | { | 91 | { |
92 | static bool bFirst = true; | 92 | static bool bFirst = true; |
93 | if ( bFirst ) { | 93 | if ( bFirst ) { |
94 | if ( bOnMonday ) { | 94 | if ( bOnMonday ) { |
95 | header->addLabel( tr("M", "Monday" ) ); | 95 | header->addLabel( tr("M", "Monday" ) ); |
96 | header->addLabel( tr("T", "Tuesday") ); | 96 | header->addLabel( tr("T", "Tuesday") ); |
97 | header->addLabel( tr("W", "Wednesday" ) ); | 97 | header->addLabel( tr("W", "Wednesday" ) ); |
98 | header->addLabel( tr("T", "Thursday" ) ); | 98 | header->addLabel( tr("T", "Thursday" ) ); |
99 | header->addLabel( tr("F", "Friday" ) ); | 99 | header->addLabel( tr("F", "Friday" ) ); |
100 | header->addLabel( tr("S", "Saturday" ) ); | 100 | header->addLabel( tr("S", "Saturday" ) ); |
101 | header->addLabel( tr("S", "Sunday" ) ); | 101 | header->addLabel( tr("S", "Sunday" ) ); |
102 | } else { | 102 | } else { |
103 | header->addLabel( tr("S", "Sunday" ) ); | 103 | header->addLabel( tr("S", "Sunday" ) ); |
104 | header->addLabel( tr("M", "Monday") ); | 104 | header->addLabel( tr("M", "Monday") ); |
105 | header->addLabel( tr("T", "Tuesday") ); | 105 | header->addLabel( tr("T", "Tuesday") ); |
106 | header->addLabel( tr("W", "Wednesday" ) ); | 106 | header->addLabel( tr("W", "Wednesday" ) ); |
107 | header->addLabel( tr("T", "Thursday" ) ); | 107 | header->addLabel( tr("T", "Thursday" ) ); |
108 | header->addLabel( tr("F", "Friday" ) ); | 108 | header->addLabel( tr("F", "Friday" ) ); |
109 | header->addLabel( tr("S", "Saturday" ) ); | 109 | header->addLabel( tr("S", "Saturday" ) ); |
110 | } | 110 | } |
111 | bFirst = false; | 111 | bFirst = false; |
112 | } else { | 112 | } else { |
113 | // we are change things... | 113 | // we are change things... |
114 | if ( bOnMonday ) { | 114 | if ( bOnMonday ) { |
115 | header->setLabel( 1, tr("M", "Monday") ); | 115 | header->setLabel( 1, tr("M", "Monday") ); |
116 | header->setLabel( 2, tr("T", "Tuesday") ); | 116 | header->setLabel( 2, tr("T", "Tuesday") ); |
117 | header->setLabel( 3, tr("W", "Wednesday" ) ); | 117 | header->setLabel( 3, tr("W", "Wednesday" ) ); |
118 | header->setLabel( 4, tr("T", "Thursday" ) ); | 118 | header->setLabel( 4, tr("T", "Thursday" ) ); |
119 | header->setLabel( 5, tr("F", "Friday" ) ); | 119 | header->setLabel( 5, tr("F", "Friday" ) ); |
120 | header->setLabel( 6, tr("S", "Saturday" ) ); | 120 | header->setLabel( 6, tr("S", "Saturday" ) ); |
121 | header->setLabel( 7, tr("S", "Sunday" ) ); | 121 | header->setLabel( 7, tr("S", "Sunday" ) ); |
122 | } else { | 122 | } else { |
123 | header->setLabel( 1, tr("S", "Sunday" ) ); | 123 | header->setLabel( 1, tr("S", "Sunday" ) ); |
124 | header->setLabel( 2, tr("M", "Monday") ); | 124 | header->setLabel( 2, tr("M", "Monday") ); |
125 | header->setLabel( 3, tr("T", "Tuesday") ); | 125 | header->setLabel( 3, tr("T", "Tuesday") ); |
126 | header->setLabel( 4, tr("W", "Wednesday" ) ); | 126 | header->setLabel( 4, tr("W", "Wednesday" ) ); |
127 | header->setLabel( 5, tr("T", "Thursday" ) ); | 127 | header->setLabel( 5, tr("T", "Thursday" ) ); |
128 | header->setLabel( 6, tr("F", "Friday" ) ); | 128 | header->setLabel( 6, tr("F", "Friday" ) ); |
129 | header->setLabel( 7, tr("S", "Saturday" ) ); | 129 | header->setLabel( 7, tr("S", "Saturday" ) ); |
130 | } | 130 | } |
131 | } | 131 | } |
132 | } | 132 | } |
133 | 133 | ||
134 | 134 | ||
135 | 135 | ||
136 | void DateBookWeekView::showEvents( QValueList<EffectiveEvent> &ev ) | 136 | void DateBookWeekView::showEvents( QValueList<EffectiveEvent> &ev ) |
137 | { | 137 | { |
138 | items.clear(); | 138 | items.clear(); |
139 | QValueListIterator<EffectiveEvent> it; | 139 | QValueListIterator<EffectiveEvent> it; |
140 | for ( it = ev.begin(); it != ev.end(); ++it ) { | 140 | for ( it = ev.begin(); it != ev.end(); ++it ) { |
141 | DateBookWeekItem *i = new DateBookWeekItem( *it ); | 141 | DateBookWeekItem *i = new DateBookWeekItem( *it ); |
142 | positionItem( i ); | 142 | positionItem( i ); |
143 | items.append( i ); | 143 | items.append( i ); |
144 | } | 144 | } |
145 | viewport()->update(); | 145 | viewport()->update(); |
146 | } | 146 | } |
147 | 147 | ||
148 | void DateBookWeekView::moveToHour( int h ) | 148 | void DateBookWeekView::moveToHour( int h ) |
149 | { | 149 | { |
150 | int offset = h*rowHeight; | 150 | int offset = h*rowHeight; |
151 | setContentsPos( 0, offset ); | 151 | setContentsPos( 0, offset ); |
152 | } | 152 | } |
153 | 153 | ||
154 | void DateBookWeekView::keyPressEvent( QKeyEvent *e ) | 154 | void DateBookWeekView::keyPressEvent( QKeyEvent *e ) |
155 | { | 155 | { |
156 | e->ignore(); | 156 | e->ignore(); |
157 | } | 157 | } |
158 | 158 | ||
159 | void DateBookWeekView::slotChangeClock( bool c ) | 159 | void DateBookWeekView::slotChangeClock( bool c ) |
160 | { | 160 | { |
161 | ampm = c; | 161 | ampm = c; |
162 | viewport()->update(); | 162 | viewport()->update(); |
163 | } | 163 | } |
164 | 164 | ||
165 | static inline int db_round30min( int m ) | 165 | static inline int db_round30min( int m ) |
166 | { | 166 | { |
167 | if ( m < 15 ) | 167 | if ( m < 15 ) |
168 | m = 0; | 168 | m = 0; |
169 | else if ( m < 45 ) | 169 | else if ( m < 45 ) |
170 | m = 1; | 170 | m = 1; |
171 | else | 171 | else |
172 | m = 2; | 172 | m = 2; |
173 | 173 | ||
174 | return m; | 174 | return m; |
175 | } | 175 | } |
176 | 176 | ||
177 | void DateBookWeekView::alterDay( int day ) | 177 | void DateBookWeekView::alterDay( int day ) |
178 | { | 178 | { |
179 | if ( !bOnMonday ) { | 179 | if ( !bOnMonday ) { |
180 | day--; | 180 | day--; |
181 | } | 181 | } |
182 | emit showDay( day ); | 182 | emit showDay( day ); |
183 | } | 183 | } |
184 | 184 | ||
185 | void DateBookWeekView::positionItem( DateBookWeekItem *i ) | 185 | void DateBookWeekView::positionItem( DateBookWeekItem *i ) |
186 | { | 186 | { |
187 | const int Width = 8; | 187 | const int Width = 8; |
188 | const EffectiveEvent ev = i->event(); | 188 | const EffectiveEvent ev = i->event(); |
189 | 189 | ||
190 | // 30 minute intervals | 190 | // 30 minute intervals |
191 | int y = ev.start().hour() * 2; | 191 | int y = ev.start().hour() * 2; |
192 | y += db_round30min( ev.start().minute() ); | 192 | y += db_round30min( ev.start().minute() ); |
193 | int y2 = ev.end().hour() * 2; | ||
194 | y2 += db_round30min( ev.end().minute() ); | ||
193 | if ( y > 47 ) | 195 | if ( y > 47 ) |
194 | y = 47; | 196 | y = 47; |
197 | if ( y2 > 48 ) | ||
198 | y2 = 48; | ||
195 | y = y * rowHeight / 2; | 199 | y = y * rowHeight / 2; |
200 | y2 = y2 * rowHeight / 2; | ||
196 | 201 | ||
197 | int h; | 202 | int h; |
198 | if ( ev.event().type() == Event::AllDay ) { | 203 | if ( ev.event().type() == Event::AllDay ) { |
199 | h = 48; | 204 | h = 48 * rowHeight / 2; |
200 | y = 0; | 205 | y = 0; |
201 | } else { | 206 | } else { |
202 | h = ( ev.end().hour() - ev.start().hour() ) * 2; | 207 | h=y2-y; |
203 | h += db_round30min( ev.end().minute() - ev.start().minute() ); | ||
204 | if ( h < 1 ) h = 1; | 208 | if ( h < 1 ) h = 1; |
205 | } | 209 | } |
206 | h = h * rowHeight / 2; | ||
207 | 210 | ||
208 | int dow = ev.date().dayOfWeek(); | 211 | int dow = ev.date().dayOfWeek(); |
209 | if ( !bOnMonday ) { | 212 | if ( !bOnMonday ) { |
210 | if ( dow == 7 ) | 213 | if ( dow == 7 ) |
211 | dow = 1; | 214 | dow = 1; |
212 | else | 215 | else |
213 | dow++; | 216 | dow++; |
214 | } | 217 | } |
215 | int x = header->sectionPos( dow ) - 1; | 218 | int x = header->sectionPos( dow ) - 1; |
216 | int xlim = header->sectionPos( dow ) + header->sectionSize( dow ); | 219 | int xlim = header->sectionPos( dow ) + header->sectionSize( dow ); |
217 | DateBookWeekItem *isect = 0; | 220 | DateBookWeekItem *isect = 0; |
218 | do { | 221 | do { |
219 | i->setGeometry( x, y, Width, h ); | 222 | i->setGeometry( x, y, Width, h ); |
220 | isect = intersects( i ); | 223 | isect = intersects( i ); |
221 | x += Width - 1; | 224 | x += Width - 1; |
222 | } while ( isect && x < xlim ); | 225 | } while ( isect && x < xlim ); |
223 | } | 226 | } |
224 | 227 | ||
225 | DateBookWeekItem *DateBookWeekView::intersects( const DateBookWeekItem *item ) | 228 | DateBookWeekItem *DateBookWeekView::intersects( const DateBookWeekItem *item ) |
226 | { | 229 | { |
227 | QRect geom = item->geometry(); | 230 | QRect geom = item->geometry(); |
228 | 231 | ||
229 | // We allow the edges to overlap | 232 | // We allow the edges to overlap |
230 | geom.moveBy( 1, 1 ); | 233 | geom.moveBy( 1, 1 ); |
231 | geom.setSize( geom.size()-QSize(2,2) ); | 234 | geom.setSize( geom.size()-QSize(2,2) ); |
232 | 235 | ||
233 | QListIterator<DateBookWeekItem> it(items); | 236 | QListIterator<DateBookWeekItem> it(items); |
234 | for ( ; it.current(); ++it ) { | 237 | for ( ; it.current(); ++it ) { |
235 | DateBookWeekItem *i = it.current(); | 238 | DateBookWeekItem *i = it.current(); |
236 | if ( i != item ) { | 239 | if ( i != item ) { |
237 | if ( i->geometry().intersects( geom ) ) { | 240 | if ( i->geometry().intersects( geom ) ) { |
238 | return i; | 241 | return i; |
239 | } | 242 | } |
240 | } | 243 | } |
241 | } | 244 | } |
242 | 245 | ||
243 | return 0; | 246 | return 0; |
244 | } | 247 | } |
245 | 248 | ||
246 | void DateBookWeekView::contentsMousePressEvent( QMouseEvent *e ) | 249 | void DateBookWeekView::contentsMousePressEvent( QMouseEvent *e ) |
247 | { | 250 | { |
248 | QListIterator<DateBookWeekItem> it(items); | 251 | QListIterator<DateBookWeekItem> it(items); |
249 | for ( ; it.current(); ++it ) { | 252 | for ( ; it.current(); ++it ) { |
250 | DateBookWeekItem *i = it.current(); | 253 | DateBookWeekItem *i = it.current(); |
251 | if ( i->geometry().contains( e->pos() ) ) { | 254 | if ( i->geometry().contains( e->pos() ) ) { |
252 | showingEvent = true; | 255 | showingEvent = true; |
253 | emit signalShowEvent( i->event() ); | 256 | emit signalShowEvent( i->event() ); |
254 | break; | 257 | break; |
255 | } | 258 | } |
256 | } | 259 | } |
257 | } | 260 | } |
258 | 261 | ||
259 | void DateBookWeekView::contentsMouseReleaseEvent( QMouseEvent *e ) | 262 | void DateBookWeekView::contentsMouseReleaseEvent( QMouseEvent *e ) |
260 | { | 263 | { |
261 | if ( showingEvent ) { | 264 | if ( showingEvent ) { |
262 | showingEvent = false; | 265 | showingEvent = false; |
263 | emit signalHideEvent(); | 266 | emit signalHideEvent(); |
264 | } else { | 267 | } else { |
265 | int d = header->sectionAt( e->pos().x() ); | 268 | int d = header->sectionAt( e->pos().x() ); |
266 | if ( d > 0 ) { | 269 | if ( d > 0 ) { |
267 | // if ( !bOnMonday ) | 270 | // if ( !bOnMonday ) |
268 | // d--; | 271 | // d--; |
269 | emit showDay( d ); | 272 | emit showDay( d ); |
270 | } | 273 | } |
271 | } | 274 | } |
272 | } | 275 | } |
273 | 276 | ||
274 | void DateBookWeekView::drawContents( QPainter *p, int cx, int cy, int cw, int ch ) | 277 | void DateBookWeekView::drawContents( QPainter *p, int cx, int cy, int cw, int ch ) |
275 | { | 278 | { |
276 | QRect ur( cx, cy, cw, ch ); | 279 | QRect ur( cx, cy, cw, ch ); |
277 | p->setPen( lightGray ); | 280 | p->setPen( lightGray ); |
278 | for ( int i = 1; i <= 7; i++ ) | 281 | for ( int i = 1; i <= 7; i++ ) |
279 | p->drawLine( header->sectionPos(i)-2, cy, header->sectionPos(i)-2, cy+ch ); | 282 | p->drawLine( header->sectionPos(i)-2, cy, header->sectionPos(i)-2, cy+ch ); |
280 | 283 | ||
281 | p->setPen( black ); | 284 | p->setPen( black ); |
282 | for ( int t = 0; t < 24; t++ ) { | 285 | for ( int t = 0; t < 24; t++ ) { |
283 | int y = t*rowHeight; | 286 | int y = t*rowHeight; |
284 | if ( QRect( 1, y, 20, rowHeight ).intersects( ur ) ) { | 287 | if ( QRect( 1, y, 20, rowHeight ).intersects( ur ) ) { |
285 | QString s; | 288 | QString s; |
286 | if ( ampm ) { | 289 | if ( ampm ) { |
287 | if ( t == 0 ) | 290 | if ( t == 0 ) |
288 | s = QString::number( 12 ); | 291 | s = QString::number( 12 ); |
289 | else if ( t == 12 ) | 292 | else if ( t == 12 ) |
290 | s = QString::number(12) + tr( "p" ); | 293 | s = QString::number(12) + tr( "p" ); |
291 | else if ( t > 12 ) { | 294 | else if ( t > 12 ) { |
292 | if ( t - 12 < 10 ) | 295 | if ( t - 12 < 10 ) |
293 | s = " "; | 296 | s = " "; |
294 | else | 297 | else |
295 | s = ""; | 298 | s = ""; |
296 | s += QString::number( t - 12 ) + tr("p"); | 299 | s += QString::number( t - 12 ) + tr("p"); |
297 | } else { | 300 | } else { |
298 | if ( 12 - t < 3 ) | 301 | if ( 12 - t < 3 ) |
299 | s = ""; | 302 | s = ""; |
300 | else | 303 | else |
301 | s = " "; | 304 | s = " "; |
302 | s += QString::number( t ); | 305 | s += QString::number( t ); |
303 | } | 306 | } |
304 | } else { | 307 | } else { |
305 | s = QString::number( t ); | 308 | s = QString::number( t ); |
306 | if ( s.length() == 1 ) | 309 | if ( s.length() == 1 ) |
307 | s.prepend( "0" ); | 310 | s.prepend( "0" ); |
308 | } | 311 | } |
309 | p->drawText( 1, y+p->fontMetrics().ascent()+1, s ); | 312 | p->drawText( 1, y+p->fontMetrics().ascent()+1, s ); |
310 | } | 313 | } |
311 | } | 314 | } |
312 | 315 | ||
313 | QListIterator<DateBookWeekItem> it(items); | 316 | QListIterator<DateBookWeekItem> it(items); |
314 | for ( ; it.current(); ++it ) { | 317 | for ( ; it.current(); ++it ) { |
315 | DateBookWeekItem *i = it.current(); | 318 | DateBookWeekItem *i = it.current(); |
316 | if ( i->geometry().intersects( ur ) ) { | 319 | if ( i->geometry().intersects( ur ) ) { |
317 | p->setBrush( i->color() ); | 320 | p->setBrush( i->color() ); |
318 | p->drawRect( i->geometry() ); | 321 | p->drawRect( i->geometry() ); |
319 | } | 322 | } |
320 | } | 323 | } |
321 | } | 324 | } |
322 | 325 | ||
323 | void DateBookWeekView::resizeEvent( QResizeEvent *e ) | 326 | void DateBookWeekView::resizeEvent( QResizeEvent *e ) |
324 | { | 327 | { |
325 | const int hourWidth = 20; | 328 | const int hourWidth = 20; |
326 | QScrollView::resizeEvent( e ); | 329 | QScrollView::resizeEvent( e ); |
327 | int avail = width()-qApp->style().scrollBarExtent().width()-1; | 330 | int avail = width()-qApp->style().scrollBarExtent().width()-1; |
328 | header->setGeometry( 0, 0, avail, header->sizeHint().height() ); | 331 | header->setGeometry( 0, 0, avail, header->sizeHint().height() ); |
329 | setMargins( 0, header->height(), 0, 0 ); | 332 | setMargins( 0, header->height(), 0, 0 ); |
330 | header->resizeSection( 0, hourWidth ); | 333 | header->resizeSection( 0, hourWidth ); |
331 | int sw = (avail - hourWidth) / 7; | 334 | int sw = (avail - hourWidth) / 7; |
332 | for ( int i = 1; i < 7; i++ ) | 335 | for ( int i = 1; i < 7; i++ ) |
333 | header->resizeSection( i, sw ); | 336 | header->resizeSection( i, sw ); |
334 | header->resizeSection( 7, avail - hourWidth - sw*6 ); | 337 | header->resizeSection( 7, avail - hourWidth - sw*6 ); |
335 | } | 338 | } |
336 | 339 | ||
337 | void DateBookWeekView::setStartOfWeek( bool bStartOnMonday ) | 340 | void DateBookWeekView::setStartOfWeek( bool bStartOnMonday ) |
338 | { | 341 | { |
339 | bOnMonday = bStartOnMonday; | 342 | bOnMonday = bStartOnMonday; |
340 | initNames(); | 343 | initNames(); |
341 | } | 344 | } |
342 | 345 | ||
343 | //------------------------------------------------------------------- | 346 | //------------------------------------------------------------------- |
344 | 347 | ||
345 | DateBookWeek::DateBookWeek( bool ap, bool startOnMonday, DateBookDB *newDB, | 348 | DateBookWeek::DateBookWeek( bool ap, bool startOnMonday, DateBookDB *newDB, |
346 | QWidget *parent, const char *name ) | 349 | QWidget *parent, const char *name ) |
347 | : QWidget( parent, name ), | 350 | : QWidget( parent, name ), |
348 | db( newDB ), | 351 | db( newDB ), |
349 | startTime( 0 ), | 352 | startTime( 0 ), |
350 | ampm( ap ), | 353 | ampm( ap ), |
351 | bStartOnMonday( startOnMonday ) | 354 | bStartOnMonday( startOnMonday ) |
352 | { | 355 | { |
353 | setFocusPolicy(StrongFocus); | 356 | setFocusPolicy(StrongFocus); |
354 | QVBoxLayout *vb = new QVBoxLayout( this ); | 357 | QVBoxLayout *vb = new QVBoxLayout( this ); |
355 | header = new DateBookWeekHeader( bStartOnMonday, this ); | 358 | header = new DateBookWeekHeader( bStartOnMonday, this ); |
356 | view = new DateBookWeekView( ampm, startOnMonday, this ); | 359 | view = new DateBookWeekView( ampm, startOnMonday, this ); |
357 | vb->addWidget( header ); | 360 | vb->addWidget( header ); |
358 | vb->addWidget( view ); | 361 | vb->addWidget( view ); |
359 | 362 | ||
360 | lblDesc = new QLabel( this, "event label" ); | 363 | lblDesc = new QLabel( this, "event label" ); |
361 | lblDesc->setFrameStyle( QFrame::Plain | QFrame::Box ); | 364 | lblDesc->setFrameStyle( QFrame::Plain | QFrame::Box ); |
362 | lblDesc->setBackgroundColor( yellow ); | 365 | lblDesc->setBackgroundColor( yellow ); |
363 | lblDesc->hide(); | 366 | lblDesc->hide(); |
364 | 367 | ||
365 | tHide = new QTimer( this ); | 368 | tHide = new QTimer( this ); |
366 | 369 | ||
367 | connect( view, SIGNAL( showDay( int ) ), | 370 | connect( view, SIGNAL( showDay( int ) ), |
368 | this, SLOT( showDay( int ) ) ); | 371 | this, SLOT( showDay( int ) ) ); |
369 | connect( view, SIGNAL(signalShowEvent(const EffectiveEvent&)), | 372 | connect( view, SIGNAL(signalShowEvent(const EffectiveEvent&)), |
370 | this, SLOT(slotShowEvent(const EffectiveEvent&)) ); | 373 | this, SLOT(slotShowEvent(const EffectiveEvent&)) ); |
371 | connect( view, SIGNAL(signalHideEvent()), | 374 | connect( view, SIGNAL(signalHideEvent()), |
372 | this, SLOT(slotHideEvent()) ); | 375 | this, SLOT(slotHideEvent()) ); |
373 | connect( header, SIGNAL( dateChanged( int, int ) ), | 376 | connect( header, SIGNAL( dateChanged( int, int ) ), |
374 | this, SLOT( dateChanged( int, int ) ) ); | 377 | this, SLOT( dateChanged( int, int ) ) ); |
375 | connect( tHide, SIGNAL( timeout() ), | 378 | connect( tHide, SIGNAL( timeout() ), |
376 | lblDesc, SLOT( hide() ) ); | 379 | lblDesc, SLOT( hide() ) ); |
377 | connect( header->spinYear, SIGNAL(valueChanged(int)), | 380 | connect( header->spinYear, SIGNAL(valueChanged(int)), |
378 | this, SLOT(slotYearChanged(int)) ); | 381 | this, SLOT(slotYearChanged(int)) ); |
379 | connect( qApp, SIGNAL(weekChanged(bool)), | 382 | connect( qApp, SIGNAL(weekChanged(bool)), |
380 | this, SLOT(slotWeekChanged(bool)) ); | 383 | this, SLOT(slotWeekChanged(bool)) ); |
381 | connect( qApp, SIGNAL(clockChanged(bool)), | 384 | connect( qApp, SIGNAL(clockChanged(bool)), |
382 | this, SLOT(slotClockChanged(bool))); | 385 | this, SLOT(slotClockChanged(bool))); |
383 | setDate(QDate::currentDate()); | 386 | setDate(QDate::currentDate()); |
384 | 387 | ||
385 | } | 388 | } |
386 | 389 | ||
387 | void DateBookWeek::keyPressEvent(QKeyEvent *e) | 390 | void DateBookWeek::keyPressEvent(QKeyEvent *e) |
388 | { | 391 | { |
389 | switch(e->key()) { | 392 | switch(e->key()) { |
390 | case Key_Up: | 393 | case Key_Up: |
391 | view->scrollBy(0, -20); | 394 | view->scrollBy(0, -20); |
392 | break; | 395 | break; |
393 | case Key_Down: | 396 | case Key_Down: |
394 | view->scrollBy(0, 20); | 397 | view->scrollBy(0, 20); |
395 | break; | 398 | break; |
396 | case Key_Left: | 399 | case Key_Left: |
397 | setDate(date().addDays(-7)); | 400 | setDate(date().addDays(-7)); |
398 | break; | 401 | break; |
399 | case Key_Right: | 402 | case Key_Right: |
400 | setDate(date().addDays(7)); | 403 | setDate(date().addDays(7)); |
401 | break; | 404 | break; |
402 | default: | 405 | default: |
403 | e->ignore(); | 406 | e->ignore(); |
404 | } | 407 | } |
405 | } | 408 | } |
406 | 409 | ||
407 | void DateBookWeek::showDay( int day ) | 410 | void DateBookWeek::showDay( int day ) |
408 | { | 411 | { |
409 | QDate d; | 412 | QDate d; |
410 | d = dateFromWeek( _week, year, bStartOnMonday ); | 413 | d = dateFromWeek( _week, year, bStartOnMonday ); |
411 | day--; | 414 | day--; |
412 | d = d.addDays( day ); | 415 | d = d.addDays( day ); |
413 | emit showDate( d.year(), d.month(), d.day() ); | 416 | emit showDate( d.year(), d.month(), d.day() ); |
414 | } | 417 | } |
415 | 418 | ||
416 | void DateBookWeek::setDate( int y, int m, int d ) | 419 | void DateBookWeek::setDate( int y, int m, int d ) |
417 | { | 420 | { |
418 | QDate date; | 421 | QDate date; |
419 | date.setYMD( y, m, d ); | 422 | date.setYMD( y, m, d ); |
420 | setDate(QDate(y, m, d)); | 423 | setDate(QDate(y, m, d)); |
421 | } | 424 | } |
422 | 425 | ||
423 | void DateBookWeek::setDate(QDate date) | 426 | void DateBookWeek::setDate(QDate date) |
424 | { | 427 | { |
425 | dow = date.dayOfWeek(); | 428 | dow = date.dayOfWeek(); |
426 | int w, y; | 429 | int w, y; |
427 | calcWeek( date, w, y, bStartOnMonday ); | 430 | calcWeek( date, w, y, bStartOnMonday ); |
428 | header->setDate( y, w ); | 431 | header->setDate( y, w ); |
429 | } | 432 | } |
430 | 433 | ||
431 | void DateBookWeek::dateChanged( int y, int w ) | 434 | void DateBookWeek::dateChanged( int y, int w ) |
432 | { | 435 | { |
433 | year = y; | 436 | year = y; |
434 | _week = w; | 437 | _week = w; |
435 | getEvents(); | 438 | getEvents(); |
436 | } | 439 | } |
437 | 440 | ||
438 | QDate DateBookWeek::date() const | 441 | QDate DateBookWeek::date() const |
439 | { | 442 | { |
440 | QDate d; | 443 | QDate d; |
441 | d = dateFromWeek( _week - 1, year, bStartOnMonday ); | 444 | d = dateFromWeek( _week - 1, year, bStartOnMonday ); |
442 | if ( bStartOnMonday ) | 445 | if ( bStartOnMonday ) |
443 | d = d.addDays( 7 + dow - 1 ); | 446 | d = d.addDays( 7 + dow - 1 ); |
444 | else { | 447 | else { |
445 | if ( dow == 7 ) | 448 | if ( dow == 7 ) |
446 | d = d.addDays( dow ); | 449 | d = d.addDays( dow ); |
447 | else | 450 | else |
448 | d = d.addDays( 7 + dow ); | 451 | d = d.addDays( 7 + dow ); |
449 | } | 452 | } |
450 | return d; | 453 | return d; |
451 | } | 454 | } |
452 | 455 | ||
453 | void DateBookWeek::getEvents() | 456 | void DateBookWeek::getEvents() |
454 | { | 457 | { |
455 | QDate startWeek = weekDate(); | 458 | QDate startWeek = weekDate(); |
456 | 459 | ||
457 | QDate endWeek = startWeek.addDays( 6 ); | 460 | QDate endWeek = startWeek.addDays( 6 ); |
458 | QValueList<EffectiveEvent> eventList = db->getEffectiveEvents(startWeek, | 461 | QValueList<EffectiveEvent> eventList = db->getEffectiveEvents(startWeek, |
459 | endWeek); | 462 | endWeek); |
460 | view->showEvents( eventList ); | 463 | view->showEvents( eventList ); |
461 | view->moveToHour( startTime ); | 464 | view->moveToHour( startTime ); |
462 | } | 465 | } |
463 | 466 | ||
464 | void DateBookWeek::generateAllDayTooltext( QString& text ) { | 467 | void DateBookWeek::generateAllDayTooltext( QString& text ) { |
465 | text += "<b>" + tr("This is an all day event.") + "</b><br>"; | 468 | text += "<b>" + tr("This is an all day event.") + "</b><br>"; |
466 | } | 469 | } |
467 | 470 | ||
468 | void DateBookWeek::generateNormalTooltext( QString& str, | 471 | void DateBookWeek::generateNormalTooltext( QString& str, |
469 | const EffectiveEvent &ev ) { | 472 | const EffectiveEvent &ev ) { |
470 | str += "<b>" + QObject::tr("Start") + "</b>: "; | 473 | str += "<b>" + QObject::tr("Start") + "</b>: "; |
471 | 474 | str += TimeString::timeString( ev.event().start().time(), ampm, FALSE ); | |
472 | if ( ev.startDate() != ev.date() ) { | 475 | if( ev.startDate()!=ev.endDate() ) { |
473 | // multi-day event. Show start date | 476 | str += " <i>" + TimeString::longDateString( ev.startDate() )+"</i>"; |
474 | str += TimeString::longDateString( ev.startDate() ); | ||
475 | } else { | ||
476 | // Show start time. | ||
477 | str += TimeString::timeString(ev.start(), ampm, FALSE ); | ||
478 | } | 477 | } |
479 | 478 | str += "<br>"; | |
480 | 479 | str += "<b>" + QObject::tr("End") + "</b>: "; | |
481 | str += "<br><b>" + QObject::tr("End") + "</b>: "; | 480 | str += TimeString::timeString( ev.event().end().time(), ampm, FALSE ); |
482 | if ( ev.endDate() != ev.date() ) { | 481 | if( ev.startDate()!=ev.endDate() ) { |
483 | // multi-day event. Show end date | 482 | str += " <i>" + TimeString::longDateString( ev.endDate() ) + "</i>"; |
484 | str += TimeString::longDateString( ev.endDate() ); | ||
485 | } else { | ||
486 | // Show end time. | ||
487 | str += TimeString::timeString( ev.end(), ampm, FALSE ); | ||
488 | } | 483 | } |
489 | } | 484 | } |
490 | 485 | ||
491 | void DateBookWeek::slotShowEvent( const EffectiveEvent &ev ) | 486 | void DateBookWeek::slotShowEvent( const EffectiveEvent &ev ) |
492 | { | 487 | { |
493 | if ( tHide->isActive() ) | 488 | if ( tHide->isActive() ) |
494 | tHide->stop(); | 489 | tHide->stop(); |
495 | 490 | ||
496 | // why would someone use "<"? Oh well, fix it up... | 491 | // why would someone use "<"? Oh well, fix it up... |
497 | // I wonder what other things may be messed up... | 492 | // I wonder what other things may be messed up... |
498 | QString strDesc = ev.description(); | 493 | QString strDesc = ev.description(); |
499 | int where = strDesc.find( "<" ); | 494 | int where = strDesc.find( "<" ); |
500 | while ( where != -1 ) { | 495 | while ( where != -1 ) { |
501 | strDesc.remove( where, 1 ); | 496 | strDesc.remove( where, 1 ); |
502 | strDesc.insert( where, "<" ); | 497 | strDesc.insert( where, "<" ); |
503 | where = strDesc.find( "<", where ); | 498 | where = strDesc.find( "<", where ); |
504 | } | 499 | } |
505 | 500 | ||
506 | QString strCat; | 501 | QString strCat; |
507 | // ### FIX later... | 502 | // ### FIX later... |
508 | // QString strCat = ev.category(); | 503 | // QString strCat = ev.category(); |
509 | // where = strCat.find( "<" ); | 504 | // where = strCat.find( "<" ); |
510 | // while ( where != -1 ) { | 505 | // while ( where != -1 ) { |
511 | // strCat.remove( where, 1 ); | 506 | // strCat.remove( where, 1 ); |
512 | // strCat.insert( where, "<" ); | 507 | // strCat.insert( where, "<" ); |
513 | // where = strCat.find( "<", where ); | 508 | // where = strCat.find( "<", where ); |
514 | // } | 509 | // } |
515 | 510 | ||
516 | QString strLocation = ev.location(); | 511 | QString strLocation = ev.location(); |
517 | while ( where != -1 ) { | 512 | while ( where != -1 ) { |
518 | strLocation.remove( where, 1 ); | 513 | strLocation.remove( where, 1 ); |
519 | strLocation.insert( where, "<" ); | 514 | strLocation.insert( where, "<" ); |
520 | where = strLocation.find( "<", where ); | 515 | where = strLocation.find( "<", where ); |
521 | } | 516 | } |
522 | 517 | ||
523 | QString strNote = ev.notes(); | 518 | QString strNote = ev.notes(); |
524 | where = strNote.find( "<" ); | 519 | where = strNote.find( "<" ); |
525 | while ( where != -1 ) { | 520 | while ( where != -1 ) { |
526 | strNote.remove( where, 1 ); | 521 | strNote.remove( where, 1 ); |
527 | strNote.insert( where, "<" ); | 522 | strNote.insert( where, "<" ); |
528 | where = strNote.find( "<", where ); | 523 | where = strNote.find( "<", where ); |
529 | } | 524 | } |
530 | 525 | ||
531 | QString str = "<b>" + strDesc + "</b><br>" | 526 | QString str = "<b>" + strDesc + "</b><br>" |
532 | + strLocation + "<br>" | 527 | + strLocation + "<br>" |
533 | + "<i>" + strCat + "</i>" | 528 | + "<i>" + strCat + "</i>" |
534 | + "<br>" + TimeString::longDateString( ev.date() ) | 529 | + "<br>" + TimeString::longDateString( ev.date() ) |
535 | + "<br>"; | 530 | + "<br>"; |
536 | 531 | ||
537 | if (ev.event().type() == Event::Normal ) | 532 | if (ev.event().type() == Event::Normal ) |
538 | generateNormalTooltext( str, ev ); | 533 | generateNormalTooltext( str, ev ); |
539 | else | 534 | else |
540 | generateAllDayTooltext( str ); | 535 | generateAllDayTooltext( str ); |
541 | 536 | ||
542 | str += "<br><br>" + strNote; | 537 | str += "<br><br>" + strNote; |
543 | 538 | ||
544 | lblDesc->setText( str ); | 539 | lblDesc->setText( str ); |
545 | lblDesc->resize( lblDesc->sizeHint() ); | 540 | lblDesc->resize( lblDesc->sizeHint() ); |
546 | // move the label so it is "centerd" horizontally... | 541 | // move the label so it is "centerd" horizontally... |
547 | lblDesc->move( QMAX(0,(width() - lblDesc->width()) / 2), 0 ); | 542 | lblDesc->move( QMAX(0,(width() - lblDesc->width()) / 2), 0 ); |
548 | lblDesc->show(); | 543 | lblDesc->show(); |
549 | } | 544 | } |
550 | 545 | ||
551 | void DateBookWeek::slotHideEvent() | 546 | void DateBookWeek::slotHideEvent() |
552 | { | 547 | { |
553 | tHide->start( 2000, true ); | 548 | tHide->start( 2000, true ); |
554 | } | 549 | } |
555 | 550 | ||
556 | void DateBookWeek::setStartViewTime( int startHere ) | 551 | void DateBookWeek::setStartViewTime( int startHere ) |
557 | { | 552 | { |
558 | startTime = startHere; | 553 | startTime = startHere; |
559 | view->moveToHour( startTime ); | 554 | view->moveToHour( startTime ); |
560 | } | 555 | } |
561 | 556 | ||
562 | int DateBookWeek::startViewTime() const | 557 | int DateBookWeek::startViewTime() const |
563 | { | 558 | { |
564 | return startTime; | 559 | return startTime; |
565 | } | 560 | } |
566 | 561 | ||
567 | void DateBookWeek::redraw() | 562 | void DateBookWeek::redraw() |
568 | { | 563 | { |
569 | getEvents(); | 564 | getEvents(); |
570 | } | 565 | } |
571 | 566 | ||
572 | void DateBookWeek::slotYearChanged( int y ) | 567 | void DateBookWeek::slotYearChanged( int y ) |
573 | { | 568 | { |
574 | int totWeek; | 569 | int totWeek; |
575 | QDate d( y, 12, 31 ); | 570 | QDate d( y, 12, 31 ); |
576 | int throwAway; | 571 | int throwAway; |
577 | calcWeek( d, totWeek, throwAway, bStartOnMonday ); | 572 | calcWeek( d, totWeek, throwAway, bStartOnMonday ); |
578 | while ( totWeek == 1 ) { | 573 | while ( totWeek == 1 ) { |
579 | d = d.addDays( -1 ); | 574 | d = d.addDays( -1 ); |
580 | calcWeek( d, totWeek, throwAway, bStartOnMonday ); | 575 | calcWeek( d, totWeek, throwAway, bStartOnMonday ); |
581 | } | 576 | } |
582 | if ( totWeek != totalWeeks() ) | 577 | if ( totWeek != totalWeeks() ) |
583 | setTotalWeeks( totWeek ); | 578 | setTotalWeeks( totWeek ); |
584 | } | 579 | } |
585 | 580 | ||
586 | 581 | ||
587 | void DateBookWeek::setTotalWeeks( int numWeeks ) | 582 | void DateBookWeek::setTotalWeeks( int numWeeks ) |
588 | { | 583 | { |
589 | header->spinWeek->setMaxValue( numWeeks ); | 584 | header->spinWeek->setMaxValue( numWeeks ); |
590 | } | 585 | } |
591 | 586 | ||
592 | int DateBookWeek::totalWeeks() const | 587 | int DateBookWeek::totalWeeks() const |
593 | { | 588 | { |
594 | return header->spinWeek->maxValue(); | 589 | return header->spinWeek->maxValue(); |
595 | } | 590 | } |
596 | 591 | ||
597 | void DateBookWeek::slotWeekChanged( bool onMonday ) | 592 | void DateBookWeek::slotWeekChanged( bool onMonday ) |
598 | { | 593 | { |
599 | bStartOnMonday = onMonday; | 594 | bStartOnMonday = onMonday; |
600 | view->setStartOfWeek( bStartOnMonday ); | 595 | view->setStartOfWeek( bStartOnMonday ); |
601 | header->setStartOfWeek( bStartOnMonday ); | 596 | header->setStartOfWeek( bStartOnMonday ); |
602 | redraw(); | 597 | redraw(); |
603 | } | 598 | } |
604 | 599 | ||
605 | void DateBookWeek::slotClockChanged( bool ap ) | 600 | void DateBookWeek::slotClockChanged( bool ap ) |
606 | { | 601 | { |
607 | ampm = ap; | 602 | ampm = ap; |
608 | } | 603 | } |
609 | 604 | ||
610 | // return the date at the beginning of the week... | 605 | // return the date at the beginning of the week... |
611 | QDate DateBookWeek::weekDate() const | 606 | QDate DateBookWeek::weekDate() const |
612 | { | 607 | { |
613 | return dateFromWeek( _week, year, bStartOnMonday ); | 608 | return dateFromWeek( _week, year, bStartOnMonday ); |
614 | } | 609 | } |
615 | 610 | ||
616 | // this used to only be needed by datebook.cpp, but now we need it inside | 611 | // this used to only be needed by datebook.cpp, but now we need it inside |
617 | // week view since | 612 | // week view since |
618 | // we need to be able to figure out our total number of weeks on the fly... | 613 | // we need to be able to figure out our total number of weeks on the fly... |
619 | // this is probably the best place to put it.. | 614 | // this is probably the best place to put it.. |
620 | 615 | ||
621 | // For Weeks that start on Monday... (EASY!) | 616 | // For Weeks that start on Monday... (EASY!) |
622 | // At the moment we will use ISO 8601 method for computing | 617 | // At the moment we will use ISO 8601 method for computing |
623 | // the week. Granted, other countries use other methods, | 618 | // the week. Granted, other countries use other methods, |
624 | // bet we aren't doing any Locale stuff at the moment. So, | 619 | // bet we aren't doing any Locale stuff at the moment. So, |
625 | // this should pass. This Algorithim is public domain and | 620 | // this should pass. This Algorithim is public domain and |
626 | // available at: | 621 | // available at: |
627 | // http://personal.ecu.edu/mccartyr/ISOwdALG.txt | 622 | // http://personal.ecu.edu/mccartyr/ISOwdALG.txt |
628 | // the week number is return, and the year number is returned in year | 623 | // the week number is return, and the year number is returned in year |
629 | // for Instance 2001/12/31 is actually the first week in 2002. | 624 | // for Instance 2001/12/31 is actually the first week in 2002. |
630 | // There is a more mathematical definition, but I will implement it when | 625 | // There is a more mathematical definition, but I will implement it when |
631 | // we are pass our deadline. | 626 | // we are pass our deadline. |
632 | 627 | ||
633 | // For Weeks that start on Sunday... (ahh... home rolled) | 628 | // For Weeks that start on Sunday... (ahh... home rolled) |
634 | // okay, if Jan 1 is on Friday or Saturday, | 629 | // okay, if Jan 1 is on Friday or Saturday, |
635 | // it will go to the pervious | 630 | // it will go to the pervious |
636 | // week... | 631 | // week... |
637 | 632 | ||
638 | bool calcWeek( const QDate &d, int &week, int &year, | 633 | bool calcWeek( const QDate &d, int &week, int &year, |
639 | bool startOnMonday ) | 634 | bool startOnMonday ) |
640 | { | 635 | { |
641 | int weekNumber; | 636 | int weekNumber; |
642 | int yearNumber; | 637 | int yearNumber; |
643 | 638 | ||
644 | // remove a pesky warning, (Optimizations on g++) | 639 | // remove a pesky warning, (Optimizations on g++) |
645 | weekNumber = -1; | 640 | weekNumber = -1; |
646 | int jan1WeekDay = QDate(d.year(), 1, 1).dayOfWeek(); | 641 | int jan1WeekDay = QDate(d.year(), 1, 1).dayOfWeek(); |
647 | int dayOfWeek = d.dayOfWeek(); | 642 | int dayOfWeek = d.dayOfWeek(); |
648 | 643 | ||
649 | if ( !d.isValid() ) | 644 | if ( !d.isValid() ) |
650 | return false; | 645 | return false; |
651 | 646 | ||
652 | if ( startOnMonday ) { | 647 | if ( startOnMonday ) { |
653 | // find the Jan1Weekday; | 648 | // find the Jan1Weekday; |
654 | if ( d.dayOfYear() <= ( 8 - jan1WeekDay) && jan1WeekDay > 4 ) { | 649 | if ( d.dayOfYear() <= ( 8 - jan1WeekDay) && jan1WeekDay > 4 ) { |
655 | yearNumber = d.year() - 1; | 650 | yearNumber = d.year() - 1; |
656 | if ( jan1WeekDay == 5 || ( jan1WeekDay == 6 && QDate::leapYear(yearNumber) ) ) | 651 | if ( jan1WeekDay == 5 || ( jan1WeekDay == 6 && QDate::leapYear(yearNumber) ) ) |
657 | weekNumber = 53; | 652 | weekNumber = 53; |
658 | else | 653 | else |
659 | weekNumber = 52; | 654 | weekNumber = 52; |
660 | } else | 655 | } else |
661 | yearNumber = d.year(); | 656 | yearNumber = d.year(); |
662 | if ( yearNumber == d.year() ) { | 657 | if ( yearNumber == d.year() ) { |
663 | int totalDays = 365; | 658 | int totalDays = 365; |
664 | if ( QDate::leapYear(yearNumber) ) | 659 | if ( QDate::leapYear(yearNumber) ) |
665 | totalDays++; | 660 | totalDays++; |
666 | if ( ((totalDays - d.dayOfYear()) < (4 - dayOfWeek) ) | 661 | if ( ((totalDays - d.dayOfYear()) < (4 - dayOfWeek) ) |
667 | || (jan1WeekDay == 7) && (totalDays - d.dayOfYear()) < 3) { | 662 | || (jan1WeekDay == 7) && (totalDays - d.dayOfYear()) < 3) { |
668 | yearNumber++; | 663 | yearNumber++; |
669 | weekNumber = 1; | 664 | weekNumber = 1; |
670 | } | 665 | } |
671 | } | 666 | } |
672 | if ( yearNumber == d.year() ) { | 667 | if ( yearNumber == d.year() ) { |
673 | int j = d.dayOfYear() + (7 - dayOfWeek) + ( jan1WeekDay - 1 ); | 668 | int j = d.dayOfYear() + (7 - dayOfWeek) + ( jan1WeekDay - 1 ); |
674 | weekNumber = j / 7; | 669 | weekNumber = j / 7; |
675 | if ( jan1WeekDay > 4 ) | 670 | if ( jan1WeekDay > 4 ) |
676 | weekNumber--; | 671 | weekNumber--; |
677 | } | 672 | } |
678 | } else { | 673 | } else { |
679 | // it's better to keep these cases separate... | 674 | // it's better to keep these cases separate... |