summaryrefslogtreecommitdiff
path: root/libopie2/opiepim/ui/opimrecurrencewidget.cpp
Unidiff
Diffstat (limited to 'libopie2/opiepim/ui/opimrecurrencewidget.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--libopie2/opiepim/ui/opimrecurrencewidget.cpp634
1 files changed, 634 insertions, 0 deletions
diff --git a/libopie2/opiepim/ui/opimrecurrencewidget.cpp b/libopie2/opiepim/ui/opimrecurrencewidget.cpp
new file mode 100644
index 0000000..569bdd5
--- a/dev/null
+++ b/libopie2/opiepim/ui/opimrecurrencewidget.cpp
@@ -0,0 +1,634 @@
1#include <qapplication.h>
2#include <qlabel.h>
3#include <qpopupmenu.h>
4#include <qspinbox.h>
5
6#include <qpe/timestring.h>
7
8#include "opimrecurrencewidget.h"
9
10// Global Templates for use in setting up the repeat label...
11// the problem is these strings get initialized before QPEApplication can install the translator -zecke
12namespace {
13QString strDayTemplate;
14QString strYearTemplate;
15QString strMonthDateTemplate;
16QString strMonthDayTemplate;
17QString strWeekTemplate;
18QString dayLabel[7];
19}
20
21/*
22 * static linkage to not polute the symbol table...
23 * The problem is that const and static linkage are resolved prior to installing a translator
24 * leading to that the above strings are translted but to the original we delay the init of these strings...
25 * -zecke
26 */
27static void fillStrings() {
28 strDayTemplate = QObject::tr("Every");
29 strYearTemplate = QObject::tr("%1 %2 every ");
30 strMonthDateTemplate = QObject::tr("The %1 every ");
31 strMonthDayTemplate = QObject::tr("The %1 %2 of every");
32 strWeekTemplate = QObject::tr("Every ");
33 dayLabel[0] = QObject::tr("Monday");
34 dayLabel[1] = QObject::tr("Tuesday");
35 dayLabel[2] = QObject::tr("Wednesday");
36 dayLabel[3] = QObject::tr("Thursday");
37 dayLabel[4] = QObject::tr("Friday");
38 dayLabel[5] = QObject::tr("Saturday");
39 dayLabel[6] = QObject::tr("Sunday");
40}
41
42 static QString numberPlacing( int x );// return the proper word format for
43 // x (1st, 2nd, etc)
44static int week( const QDate &dt ); // what week in the month is dt?
45
46/**
47 * Constructs the Widget
48 * @param startOnMonday Does the week start on monday
49 * @param newStart The start date of the recurrence
50 * @param parent The parent widget
51 * @param name the name of object
52 * @param modal if the dialog should be modal
53 * @param fl Additional window flags
54 */
55OPimRecurrenceWidget::OPimRecurrenceWidget( bool startOnMonday,
56 const QDate& newStart,
57 QWidget* parent,
58 const char* name,
59 bool modal,
60 WFlags fl )
61 : OPimRecurrenceBase( parent, name, modal, fl ),
62 start( newStart ),
63 currInterval( None ),
64 startWeekOnMonday( startOnMonday )
65{
66 if (strDayTemplate.isEmpty() )
67 fillStrings();
68
69 init();
70 fraType->setButton( currInterval );
71 chkNoEnd->setChecked( TRUE );
72 setupNone();
73}
74
75/**
76 * Different constructor
77 * @param startOnMonday Does the week start on monday?
78 * @param rp Already set OPimRecurrence object
79 * @param startDate The start date
80 * @param parent The parent widget
81 * @param name The name of the object
82 * @param modal
83 * @param fl The flags for window
84 */
85OPimRecurrenceWidget::OPimRecurrenceWidget( bool startOnMonday,
86 const OPimRecurrence& rp, const QDate& startDate,
87 QWidget* parent, const char* name,
88 bool modal, WFlags fl)
89 : OPimRecurrenceBase( parent, name, modal, fl ),
90 start( startDate ),
91 end( rp.endDate() ),
92 startWeekOnMonday( startOnMonday )
93{
94 if (strDayTemplate.isEmpty() )
95 fillStrings();
96 // do some stuff with the repeat pattern
97 init();
98 setRecurrence( rp );
99}
100
101OPimRecurrenceWidget::~OPimRecurrenceWidget() {
102}
103
104/**
105 * set the start date
106 * @param date the new start date
107 */
108void OPimRecurrenceWidget::setStartDate( const QDate& date ) {
109 setRecurrence( recurrence(), date );
110}
111/**
112 * set the recurrence
113 * @param rp The OPimRecurrence object with the new recurrence rules
114 */
115void OPimRecurrenceWidget::setRecurrence( const OPimRecurrence& rp ) {
116 setRecurrence( rp, start );
117}
118
119/**
120 * overloaded method taking OPimRecurrence and a new start date
121 * @param rp Recurrence rule
122 * @param date The new start date
123 */
124void OPimRecurrenceWidget::setRecurrence( const OPimRecurrence& rp, const QDate& date ) {
125 start = date;
126 end = rp.endDate();
127 switch ( rp.type() ) {
128 default:
129 case OPimRecurrence::NoRepeat:
130 currInterval = None;
131 setupNone();
132 break;
133 case OPimRecurrence::Daily:
134 currInterval = Day;
135 setupDaily();
136 break;
137 case OPimRecurrence::Weekly:
138 currInterval = Week;
139 setupWeekly();
140 int day, buttons;
141 for ( day = 0x01, buttons = 0; buttons < 7;
142 day = day << 1, buttons++ ) {
143 if ( rp.days() & day ) {
144 if ( startWeekOnMonday )
145 fraExtra->setButton( buttons );
146 else {
147 if ( buttons == 7 )
148 fraExtra->setButton( 0 );
149 else
150 fraExtra->setButton( buttons + 1 );
151 }
152 }
153 }
154 slotWeekLabel();
155 break;
156 case OPimRecurrence::MonthlyDay:
157 currInterval = Month;
158 setupMonthly();
159 fraExtra->setButton( 0 );
160 slotMonthLabel( 0 );
161 break;
162 case OPimRecurrence::MonthlyDate:
163 currInterval = Month;
164 setupMonthly();
165 fraExtra->setButton( 1 );
166 slotMonthLabel( 1 );
167 break;
168 case OPimRecurrence::Yearly:
169 currInterval = Year;
170 setupYearly();
171 break;
172 }
173 fraType->setButton( currInterval );
174 spinFreq->setValue( rp.frequency() );
175 if ( !rp.hasEndDate() ) {
176 cmdEnd->setText( tr("No End Date") );
177 chkNoEnd->setChecked( TRUE );
178 } else
179 cmdEnd->setText( TimeString::shortDate( end ) );
180}
181
182/**
183 * the user selected recurrence rule.
184 * @return The recurrence rule.
185 */
186OPimRecurrence OPimRecurrenceWidget::recurrence()const {
187 QListIterator<QToolButton> it( listRTypeButtons );
188 QListIterator<QToolButton> itExtra( listExtra );
189 OPimRecurrence rpTmp;
190 int i;
191 for ( i = 0; *it; ++it, i++ ) {
192 if ( (*it)->isOn() ) {
193 switch ( i ) {
194 case None:
195 rpTmp.setType( OPimRecurrence::NoRepeat );
196 break;
197 case Day:
198 rpTmp.setType( OPimRecurrence::Daily );
199 break;
200 case Week:{
201 rpTmp.setType( OPimRecurrence::Weekly );
202 int day;
203 int day2 = 0;
204 for ( day = 1; *itExtra; ++itExtra, day = day << 1 ) {
205 if ( (*itExtra)->isOn() ) {
206 if ( startWeekOnMonday )
207 day2 |= day;
208 else {
209 if ( day == 1 )
210 day2 |= Event::SUN;
211 else
212 day2 |= day >> 1;
213 }
214 }
215 }
216 rpTmp.setDays( day2 );
217 }
218 break;
219 case Month:
220 if ( cmdExtra1->isOn() )
221 rpTmp.setType( OPimRecurrence::MonthlyDay );
222 else if ( cmdExtra2->isOn() )
223 rpTmp.setType( OPimRecurrence::MonthlyDate );
224 // figure out the montly day...
225 rpTmp.setPosition( week( start ) );
226 break;
227 case Year:
228 rpTmp.setType( OPimRecurrence::Yearly );
229 break;
230 }
231 break; // no need to keep looking!
232 }
233 }
234 rpTmp.setFrequency(spinFreq->value() );
235 rpTmp.setHasEndDate( !chkNoEnd->isChecked() );
236 if ( rpTmp.hasEndDate() ) {
237 rpTmp.setEndDate( end );
238 }
239 // timestamp it...
240// rpTmp.setCreateTime( ); current DateTime is already set -zecke
241 return rpTmp;
242}
243
244/**
245 * Return the end date of the recurrence. This is only
246 * valid if the recurrence rule does contain an enddate
247 */
248QDate OPimRecurrenceWidget::endDate()const {
249 return end;
250}
251void OPimRecurrenceWidget::slotSetRType(int rtype) {
252 // now call the right function based on the type...
253 currInterval = static_cast<repeatButtons>(rtype);
254 switch ( currInterval ) {
255 case None:
256 setupNone();
257 break;
258 case Day:
259 setupDaily();
260 break;
261 case Week:
262 setupWeekly();
263 slotWeekLabel();
264 break;
265 case Month:
266 setupMonthly();
267 cmdExtra2->setOn( TRUE );
268 slotMonthLabel( 1 );
269 break;
270 case Year:
271 setupYearly();
272 break;
273 }
274}
275void OPimRecurrenceWidget::endDateChanged(int y, int m, int d) {
276 end.setYMD( y, m, d );
277 if ( end < start )
278 end = start;
279 cmdEnd->setText( TimeString::shortDate( end ) );
280 repeatPicker->setDate( end.year(), end.month(), end.day() );
281}
282void OPimRecurrenceWidget::slotNoEnd( bool unused) {
283 // if the item was toggled, then go ahead and set it to the maximum date
284 if ( unused ) {
285 end.setYMD( 3000, 12, 31 );
286 cmdEnd->setText( tr("No End Date") );
287 } else {
288 end = start;
289 cmdEnd->setText( TimeString::shortDate(end) );
290 }
291}
292void OPimRecurrenceWidget::setupRepeatLabel( const QString& s) {
293 lblVar1->setText( s );
294}
295void OPimRecurrenceWidget::setupRepeatLabel( int x) {
296 // change the spelling based on the value of x
297 QString strVar2;
298
299 if ( x > 1 )
300 lblVar1->show();
301 else
302 lblVar1->hide();
303
304 switch ( currInterval ) {
305 case None:
306 break;
307 case Day:
308 if ( x > 1 )
309 strVar2 = tr( "days" );
310 else
311 strVar2 = tr( "day" );
312 break;
313 case Week:
314 if ( x > 1 )
315 strVar2 = tr( "weeks" );
316 else
317 strVar2 = tr( "week" );
318 break;
319 case Month:
320 if ( x > 1 )
321 strVar2 = tr( "months" );
322 else
323 strVar2 = tr( "month" );
324 break;
325 case Year:
326 if ( x > 1 )
327 strVar2 = tr( "years" );
328 else
329 strVar2 = tr( "year" );
330 break;
331 }
332 if ( !strVar2.isNull() )
333 lblVar2->setText( strVar2 );
334}
335void OPimRecurrenceWidget::slotWeekLabel() {
336 QString str;
337 QListIterator<QToolButton> it( listExtra );
338 unsigned int i;
339 unsigned int keepMe;
340 bool bNeedCarriage = FALSE;
341 // don't do something we'll regret!!!
342 if ( currInterval != Week )
343 return;
344
345 if ( startWeekOnMonday )
346 keepMe = start.dayOfWeek() - 1;
347 else
348 keepMe = start.dayOfWeek() % 7;
349
350 QStringList list;
351 for ( i = 0; *it; ++it, i++ ) {
352 // a crazy check, if you are repeating weekly, the current day
353 // must be selected!!!
354 if ( i == keepMe && !( (*it)->isOn() ) )
355 (*it)->setOn( TRUE );
356 if ( (*it)->isOn() ) {
357 if ( startWeekOnMonday )
358 list.append( dayLabel[i] );
359 else {
360 if ( i == 0 )
361 list.append( dayLabel[6] );
362 else
363 list.append( dayLabel[i - 1] );
364 }
365 }
366 }
367 QStringList::Iterator itStr;
368 for ( i = 0, itStr = list.begin(); itStr != list.end(); ++itStr, i++ ) {
369 if ( i == 3 )
370 bNeedCarriage = TRUE;
371 else
372 bNeedCarriage = FALSE;
373 if ( str.isNull() )
374 str = *itStr;
375 else if ( i == list.count() - 1 ) {
376 if ( i < 2 )
377 str += tr(" and ") + *itStr;
378 else {
379 if ( bNeedCarriage )
380 str += tr( ",\nand " ) + *itStr;
381 else
382 str += tr( ", and " ) + *itStr;
383 }
384 } else {
385 if ( bNeedCarriage )
386 str += ",\n" + *itStr;
387 else
388 str += ", " + *itStr;
389 }
390 }
391 str = str.prepend( tr("on ") );
392
393 lblWeekVar->setText( str );
394}
395void OPimRecurrenceWidget::slotMonthLabel(int type) {
396 QString str;
397 if ( currInterval != Month || type > 1 )
398 return;
399 if ( type == 1 )
400 str = strMonthDateTemplate.arg( numberPlacing(start.day()) );
401 else
402 str = strMonthDayTemplate.arg( numberPlacing(week(start)))
403 .arg( dayLabel[start.dayOfWeek() - 1] );
404 lblRepeat->setText( str );
405}
406void OPimRecurrenceWidget::slotChangeStartOfWeek( bool onMonday ) {
407 startWeekOnMonday = onMonday;
408 // we need to make this unintrusive as possible...
409 int saveSpin = spinFreq->value();
410 char days = 0;
411 int day;
412 QListIterator<QToolButton> itExtra( listExtra );
413 for ( day = 1; *itExtra; ++itExtra, day = day << 1 ) {
414 if ( (*itExtra)->isOn() ) {
415 if ( !startWeekOnMonday )
416 days |= day;
417 else {
418 if ( day == 1 )
419 days |= OPimRecurrence::SUN;
420 else
421 days |= day >> 1;
422 }
423 }
424 }
425 setupWeekly();
426 spinFreq->setValue( saveSpin );
427 int buttons;
428 for ( day = 0x01, buttons = 0; buttons < 7;
429 day = day << 1, buttons++ ) {
430 if ( days & day ) {
431 if ( startWeekOnMonday )
432 fraExtra->setButton( buttons );
433 else {
434 if ( buttons == 7 )
435 fraExtra->setButton( 0 );
436 else
437 fraExtra->setButton( buttons + 1 );
438 }
439 }
440 }
441 slotWeekLabel();
442}
443void OPimRecurrenceWidget::setupNone() {
444 lblRepeat->setText( tr("No Repeat") );
445 lblVar1->hide();
446 lblVar2->hide();
447 hideExtras();
448 cmdEnd->hide();
449 lblFreq->hide();
450 lblEvery->hide();
451 lblFreq->hide();
452 spinFreq->hide();
453 lblEnd->hide();
454 lblWeekVar->hide();
455}
456void OPimRecurrenceWidget::setupDaily() {
457 hideExtras();
458 lblWeekVar->hide();
459 spinFreq->setValue( 1 );
460 lblFreq->setText( tr("day(s)") );
461 lblVar2->show();
462 showRepeatStuff();
463 lblRepeat->setText( strDayTemplate );
464 setupRepeatLabel( 1 );
465}
466void OPimRecurrenceWidget::setupWeekly() {
467// reshow the buttons...
468 fraExtra->setTitle( tr("Repeat On") );
469 fraExtra->setExclusive( FALSE );
470 fraExtra->show();
471 if ( startWeekOnMonday ) {
472 cmdExtra1->setText( tr("Mon") );
473 cmdExtra2->setText( tr("Tue") );
474 cmdExtra3->setText( tr("Wed") );
475 cmdExtra4->setText( tr("Thu") );
476 cmdExtra5->setText( tr("Fri") );
477 cmdExtra6->setText( tr("Sat") );
478 cmdExtra7->setText( tr("Sun") );
479 } else {
480 cmdExtra1->setText( tr("Sun") );
481 cmdExtra2->setText( tr("Mon") );
482 cmdExtra3->setText( tr("Tue") );
483 cmdExtra4->setText( tr("Wed") );
484 cmdExtra5->setText( tr("Thu") );
485 cmdExtra6->setText( tr("Fri") );
486 cmdExtra7->setText( tr("Sat") );
487 }
488 // I hope clustering these improve performance....
489 cmdExtra1->setOn( FALSE );
490 cmdExtra2->setOn( FALSE );
491 cmdExtra3->setOn( FALSE );
492 cmdExtra4->setOn( FALSE );
493 cmdExtra5->setOn( FALSE );
494 cmdExtra6->setOn( FALSE );
495 cmdExtra7->setOn( FALSE );
496
497 cmdExtra1->show();
498 cmdExtra2->show();
499 cmdExtra3->show();
500 cmdExtra4->show();
501 cmdExtra5->show();
502 cmdExtra6->show();
503 cmdExtra7->show();
504
505 lblWeekVar->show();
506 spinFreq->setValue( 1 );
507 // might as well set the day too...
508 if ( startWeekOnMonday ) {
509 fraExtra->setButton( start.dayOfWeek() - 1 );
510 } else {
511 fraExtra->setButton( start.dayOfWeek() % 7 );
512 }
513 lblFreq->setText( tr("week(s)") );
514 lblVar2->show();
515 showRepeatStuff();
516 setupRepeatLabel( 1 );
517}
518void OPimRecurrenceWidget::setupMonthly() {
519 hideExtras();
520 lblWeekVar->hide();
521 fraExtra->setTitle( tr("Repeat By") );
522 fraExtra->setExclusive( TRUE );
523 fraExtra->show();
524 cmdExtra1->setText( tr("Day") );
525 cmdExtra1->show();
526 cmdExtra2->setText( tr("Date") );
527 cmdExtra2->show();
528 spinFreq->setValue( 1 );
529 lblFreq->setText( tr("month(s)") );
530 lblVar2->show();
531 showRepeatStuff();
532 setupRepeatLabel( 1 );
533}
534void OPimRecurrenceWidget::setupYearly() {
535hideExtras();
536 lblWeekVar->hide();
537 spinFreq->setValue( 1 );
538 lblFreq->setText( tr("year(s)") );
539 lblFreq->show();
540 lblFreq->show();
541 showRepeatStuff();
542 lblVar2->show();
543 QString strEvery = strYearTemplate.arg( start.monthName(start.month()) ).arg( numberPlacing(start.day()) );
544 lblRepeat->setText( strEvery );
545 setupRepeatLabel( 1 );
546
547}
548void OPimRecurrenceWidget::init() {
549 QPopupMenu *m1 = new QPopupMenu( this );
550 repeatPicker = new DateBookMonth( m1, 0, TRUE );
551 m1->insertItem( repeatPicker );
552 cmdEnd->setPopup( m1 );
553 cmdEnd->setPopupDelay( 0 );
554
555 QObject::connect( repeatPicker, SIGNAL(dateClicked(int, int, int)),
556 this, SLOT(endDateChanged(int, int, int)) );
557 QObject::connect( qApp, SIGNAL(weekChanged(bool)),
558 this, SLOT(slotChangeStartOfWeek(bool)) );
559
560 listRTypeButtons.setAutoDelete( TRUE );
561 listRTypeButtons.append( cmdNone );
562 listRTypeButtons.append( cmdDay );
563 listRTypeButtons.append( cmdWeek );
564 listRTypeButtons.append( cmdMonth );
565 listRTypeButtons.append( cmdYear );
566
567 listExtra.setAutoDelete( TRUE );
568 listExtra.append( cmdExtra1 );
569 listExtra.append( cmdExtra2 );
570 listExtra.append( cmdExtra3 );
571 listExtra.append( cmdExtra4 );
572 listExtra.append( cmdExtra5 );
573 listExtra.append( cmdExtra6 );
574 listExtra.append( cmdExtra7 );
575}
576void OPimRecurrenceWidget::hideExtras() {
577 // hide the extra buttons...
578 fraExtra->hide();
579 chkNoEnd->hide();
580 QListIterator<QToolButton> it( listExtra );
581 for ( ; *it; ++it ) {
582 (*it)->hide();
583 (*it)->setOn( FALSE );
584 }
585}
586void OPimRecurrenceWidget::showRepeatStuff() {
587 cmdEnd->show();
588 chkNoEnd->show();
589 lblFreq->show();
590 lblEvery->show();
591 lblFreq->show();
592 spinFreq->show();
593 lblEnd->show();
594 lblRepeat->setText( tr("Every") );
595}
596
597
598static int week( const QDate &start )
599{
600 // figure out the week...
601 int stop = start.day(),
602 sentinel = start.dayOfWeek(),
603 dayOfWeek = QDate( start.year(), start.month(), 1 ).dayOfWeek(),
604 week = 1,
605 i;
606 for ( i = 1; i < stop; i++ ) {
607 if ( dayOfWeek++ == sentinel )
608 week++;
609 if ( dayOfWeek > 7 )
610 dayOfWeek = 0;
611 }
612 return week;
613}
614
615static QString numberPlacing( int x )
616{
617 // I hope this works in other languages besides english...
618 QString str = QString::number( x );
619 switch ( x % 10 ) {
620 case 1:
621 str += QWidget::tr( "st" );
622 break;
623 case 2:
624 str += QWidget::tr( "nd" );
625 break;
626 case 3:
627 str += QWidget::tr( "rd" );
628 break;
629 default:
630 str += QWidget::tr( "th" );
631 break;
632 }
633 return str;
634}