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