summaryrefslogtreecommitdiffabout
path: root/korganizer/komonthview.cpp
Unidiff
Diffstat (limited to 'korganizer/komonthview.cpp') (more/less context) (show whitespace changes)
-rw-r--r--korganizer/komonthview.cpp1057
1 files changed, 1057 insertions, 0 deletions
diff --git a/korganizer/komonthview.cpp b/korganizer/komonthview.cpp
new file mode 100644
index 0000000..c4bc51b
--- a/dev/null
+++ b/korganizer/komonthview.cpp
@@ -0,0 +1,1057 @@
1/*
2 This file is part of KOrganizer.
3 Copyright (c) 2000,2001 Cornelius Schumacher <schumacher@kde.org>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18*/
19
20#include <qpopupmenu.h>
21#include <qfont.h>
22#include <qfontmetrics.h>
23#include <qkeycode.h>
24#include <qhbox.h>
25#include <qvbox.h>
26#include <qpushbutton.h>
27#include <qtooltip.h>
28#include <qpainter.h>
29#include <qwhatsthis.h>
30#ifndef DESKTOP_VERSION
31#include <qpe/qpeapplication.h>
32#else
33#include <qapplication.h>
34#endif
35
36#include <kdebug.h>
37#include <klocale.h>
38#include <kglobal.h>
39#include <kconfig.h>
40#include <kiconloader.h>
41
42#include <kcalendarsystem.h>
43
44#ifndef KORG_NOPRINTER
45#include "calprinter.h"
46#endif
47#include "koprefs.h"
48#ifndef KORG_NOPLUGINS
49#include "kocore.h"
50#endif
51#include "koglobals.h"
52#include <libkdepim/kincidenceformatter.h>
53
54#include "komonthview.h"
55#include "komonthview.moc"
56
57#define PIXMAP_SIZE 5
58
59class KNOWhatsThis :public QWhatsThis
60{
61public:
62 KNOWhatsThis( KNoScrollListBox* sbox ) : QWhatsThis( sbox ), _wid( sbox) { };
63
64protected:
65 virtual QString text( const QPoint& p)
66 {
67 return _wid->getWhatsThisText(p) ;
68 };
69private:
70 KNoScrollListBox* _wid;
71
72};
73
74
75KNoScrollListBox::KNoScrollListBox(QWidget *parent,const char *name)
76 : QListBox(parent, name)
77{
78#ifndef DESKTOP_VERSION
79 QPEApplication::setStylusOperation( viewport(), QPEApplication::RightOnHold );
80#endif
81 new KNOWhatsThis(this);
82}
83
84QString KNoScrollListBox::getWhatsThisText(QPoint p)
85{
86 QListBoxItem* item = itemAt ( p );
87 if ( ! item ) {
88 return i18n("Click in the cell or\non the date label\nto add an event!");
89 }
90 return KIncidenceFormatter::instance()->getFormattedText(((MonthViewItem*) item)->incidence());
91}
92void KNoScrollListBox::keyPressEvent(QKeyEvent *e)
93{
94
95 switch(e->key()) {
96 case Key_Right:
97 // if ( e->state() == Qt::ControlButton )
98 {
99 e->ignore();
100 return;
101 }
102 scrollBy(4,0);
103 break;
104 case Key_Left:
105 // if ( e->state() == Qt::ControlButton )
106 {
107 e->ignore();
108 return;
109 }
110 scrollBy(-4,0);
111 break;
112 case Key_Up:
113 if(!count()) break;
114 setCurrentItem((currentItem()+count()-1)%count());
115 if(!itemVisible(currentItem())) {
116 if((unsigned int) currentItem() == (count()-1)) {
117 setTopItem(currentItem()-numItemsVisible()+1);
118 } else {
119 setTopItem(topItem()-1);
120 }
121 }
122 break;
123 case Key_Down:
124 if(!count()) break;
125 setCurrentItem((currentItem()+1)%count());
126 if(!itemVisible(currentItem())) {
127 if(currentItem() == 0) {
128 setTopItem(0);
129 } else {
130 setTopItem(topItem()+1);
131 }
132 }
133 break;
134 case Key_Shift:
135 emit shiftDown();
136 break;
137 default:
138 e->ignore();
139 break;
140 }
141}
142
143void KNoScrollListBox::keyReleaseEvent(QKeyEvent *e)
144{
145 switch(e->key()) {
146 case Key_Shift:
147 emit shiftUp();
148 break;
149 default:
150 break;
151 }
152}
153
154void KNoScrollListBox::mousePressEvent(QMouseEvent *e)
155{
156 QListBox::mousePressEvent(e);
157
158 if(e->button() == RightButton) {
159 emit rightClick();
160 }
161}
162
163MonthViewItem::MonthViewItem( Incidence *incidence, QDate qd, const QString & s)
164 : QListBoxItem()
165{
166 setText( s );
167
168 mIncidence = incidence;
169 mDate = qd;
170 // QWhatsThis::add(this,KIncidenceFormatter::instance()->getFormattedText( mIncidence ));
171 mRecur = false;
172 mAlarm = false;
173 mReply = false;
174 mInfo = false;
175}
176
177void MonthViewItem::paint(QPainter *p)
178{
179#if QT_VERSION >= 0x030000
180 bool sel = isSelected();
181#else
182 bool sel = selected();
183#endif
184
185
186 if (KOPrefs::instance()->mMonthViewUsesCategoryColor)
187 {
188 p->setBackgroundColor( palette().color( QPalette::Normal, \
189 sel ? QColorGroup::Highlight : QColorGroup::Background ) );
190 p->eraseRect( 0, 0, listBox()->maxItemWidth(), height( listBox() ) );
191 }
192 int x = 1;
193 int y = 3;//(height() - mRecurPixmap.height()) /2;
194 int size = PIXMAP_SIZE;
195 if ( QApplication::desktop()->width() < 300 )
196 size = 3;
197 if ( KOPrefs::instance()->mMonthShowIcons ) {
198 if ( mInfo ) {
199 p->fillRect ( x, y,size,size, Qt::darkGreen );
200 x += size + 1;
201 }
202 if ( mRecur ) {
203 p->fillRect ( x, y,size,size, Qt::blue );
204 x += size + 1;
205 }
206 if ( mAlarm ) {
207 p->fillRect ( x, y,size,size, Qt::red );
208 x += size + 1;
209 }
210 if ( mReply ) {
211 p->fillRect ( x, y,size,size, Qt::yellow );
212 x += size + 1;
213 }
214 }
215 QFontMetrics fm = p->fontMetrics();
216 int yPos;
217 int pmheight = size;
218 if( pmheight < fm.height() )
219 yPos = fm.ascent() + fm.leading()/2;
220 else
221 yPos = pmheight/2 - fm.height()/2 + fm.ascent();
222 p->setPen( palette().color( QPalette::Normal, sel ? \
223 QColorGroup::HighlightedText : QColorGroup::Foreground ) );
224 p->drawText( x, yPos, text() );
225 if ( mIncidence->cancelled() ) {
226 int wid = fm.width( text() );
227 p->drawLine( x, yPos- fm.height()/2+2,x+wid, yPos- fm.height()/2 +2);
228 }
229
230}
231
232int MonthViewItem::height(const QListBox *lb) const
233{
234 return lb->fontMetrics().lineSpacing()+1;
235}
236
237int MonthViewItem::width(const QListBox *lb) const
238{
239 int size = PIXMAP_SIZE;
240 if ( QApplication::desktop()->width() < 300 )
241 size = 3;
242 int x = 1;
243 if ( mInfo ) {
244 x += size + 1;
245 }
246 if( mRecur ) {
247 x += size+1;
248 }
249 if( mAlarm ) {
250 x += size+1;
251 }
252 if( mReply ) {
253 x += size+1;
254 }
255
256 return( x + lb->fontMetrics().width( text() ) + 1 );
257}
258
259
260MonthViewCell::MonthViewCell( KOMonthView *parent)
261 : QWidget( parent ),
262 mMonthView( parent )
263{
264
265 QVBoxLayout *topLayout = new QVBoxLayout( this );
266
267 // mLabel = new QLabel( this );QPushButton
268 mLabel = new QPushButton( this );
269 //mLabel->setFrameStyle( QFrame::Panel | QFrame::Plain );
270 //mLabel->setLineWidth( 1 );
271 //mLabel->setAlignment( AlignCenter );
272 mLabel->setFlat( true );
273 mItemList = new KNoScrollListBox( this );
274 mItemList->setMinimumSize( 10, 10 );
275 mItemList->setFrameStyle( QFrame::Panel | QFrame::Plain );
276 mItemList->setLineWidth( 1 );
277 topLayout->addWidget( mItemList );
278 mLabel->raise();
279 // QColor( 0,0,255 ) QColor( 160,1600,255 )
280 mStandardPalette = palette();
281 mStandardPalette.setColor(QColorGroup::Base, mStandardPalette.color( QPalette::Normal, QColorGroup::Background ) );
282
283 enableScrollBars( false );
284 updateConfig();
285 connect( mLabel, SIGNAL( clicked( )),
286 SLOT( newEvent() ));
287 connect( mItemList, SIGNAL( doubleClicked( QListBoxItem *) ),
288 SLOT( defaultAction( QListBoxItem * ) ) );
289 connect( mItemList, SIGNAL( rightButtonPressed( QListBoxItem *,
290 const QPoint &) ),
291 SLOT( contextMenu( QListBoxItem * ) ) );
292 connect( mItemList, SIGNAL( highlighted( QListBoxItem *) ),
293 SLOT( selection( QListBoxItem * ) ) );
294 connect( mItemList, SIGNAL( clicked( QListBoxItem * ) ),
295 SLOT( cellClicked( QListBoxItem * ) ) );
296 connect( mItemList, SIGNAL( clicked( QListBoxItem * ) ),
297 SLOT( selection( QListBoxItem * ) ) );
298}
299
300void MonthViewCell::setDate( const QDate &date )
301{
302// kdDebug() << "MonthViewCell::setDate(): " << date.toString() << endl;
303
304 mDate = date;
305
306 QString text;
307 bool smallDisplay = QApplication::desktop()->width() < 320 && KOPrefs::instance()->mMonthViewSatSunTog;
308 if ( KOGlobals::self()->calendarSystem()->day( date ) == 1 || (date.dayOfWeek() == 7 && !smallDisplay ) || KOPrefs::instance()->mMonthShowShort) {
309 text = KOGlobals::self()->calendarSystem()->monthName( date, true ) + " ";
310 mLabel->resize( mLabelBigSize );
311 text += QString::number( KOGlobals::self()->calendarSystem()->day(mDate) );
312 } else {
313 mLabel->resize( mLabelSize );
314 text += QString::number( KOGlobals::self()->calendarSystem()->day(mDate) );
315 }
316 mLabel->setText( text );
317
318 //resizeEvent( 0 );
319}
320
321QDate MonthViewCell::date() const
322{
323 return mDate;
324}
325
326void MonthViewCell::setPrimary( bool primary )
327{
328 mPrimary = primary;
329 //setMyPalette();
330}
331void MonthViewCell::setMyPalette()
332{
333
334 if ( mHoliday) {
335 setPalette( mHolidayPalette );
336 } else {
337 if ( mPrimary ) {
338 setPalette( mPrimaryPalette );
339 } else {
340 setPalette( mNonPrimaryPalette );
341 }
342 }
343 QPalette pal = palette();
344
345 mLabel->setPalette( QPalette ( pal.color( QPalette::Normal,QColorGroup::Base),pal.color(QPalette::Normal,QColorGroup::Base ) ));
346}
347QPalette MonthViewCell::getPalette ()
348{
349 if ( !KOPrefs::instance()->mMonthViewUsesDayColors )
350 return mStandardPalette;
351 if ( mHoliday) {
352 return mHolidayPalette ;
353 } else {
354 if ( mPrimary ) {
355 return mPrimaryPalette ;
356 }
357 }
358 return mNonPrimaryPalette;
359}
360bool MonthViewCell::isPrimary() const
361{
362 return mPrimary;
363}
364
365void MonthViewCell::setHoliday( bool holiday )
366{
367 mHoliday = holiday;
368 //setMyPalette();
369}
370
371void MonthViewCell::setHoliday( const QString &holiday )
372{
373 mHolidayString = holiday;
374
375 if ( !holiday.isEmpty() ) {
376 setHoliday( true );
377 }
378}
379void MonthViewCell::keyPressEvent ( QKeyEvent * e )
380{
381
382 e->ignore();
383
384}
385void MonthViewCell::updateCell()
386{
387
388 setPrimary( mDate.month()%2 );
389 setHoliday( KOGlobals::self()->calendarSystem()->dayOfWeek(mDate) == KOGlobals::self()->calendarSystem()->weekDayOfPray() );
390 if ( mDate == QDate::currentDate() ) {
391 mItemList->setLineWidth( 3 );
392 } else {
393 mItemList->setLineWidth( 1 );
394 }
395 mItemList->clear();
396 //qApp->processEvents();
397 if ( !mHolidayString.isEmpty() ) {
398 MonthViewItem *item = new MonthViewItem( 0, mDate, mHolidayString );
399 item->setPalette( mHolidayPalette );
400 mItemList->insertItem( item );
401 }
402 //mLabel->setMaximumWidth( width() - mItemList->lineWidth()*2);
403 QPtrList<Event> events = mMonthView->calendar()->events( mDate, true );
404 Event *event;
405 for( event = events.first(); event; event = events.next() ) {
406 if ( event->categories().contains("Holiday") ||
407 event->categories().contains(i18n("Holiday"))) {
408 setHoliday( true );
409 if ( mDate.dayOfWeek() == 7 )
410 mItemList->setLineWidth( 3 );
411 }
412 QString text;
413 if (event->isMultiDay()) {
414 QString prefix = "<->";
415 if ( event->doesRecur() ) {
416 if ( event->recursOn( mDate) )
417 prefix ="->" ;
418 else {
419 int days = event->dtStart().date().daysTo ( event->dtEnd().date() );
420 if ( event->recursOn( mDate.addDays( -days)) )
421 prefix ="<-" ;
422 }
423
424 } else {
425 if (mDate == event->dtStart().date()) {
426 prefix ="->" ;
427 } else if (mDate == event->dtEnd().date()) {
428 prefix ="<-" ;
429 }
430 }
431 text = prefix + event->summary();
432 } else {
433 if (event->doesFloat())
434 text = event->summary();
435 else {
436 text = KGlobal::locale()->formatTime(event->dtStart().time());
437 text += " " + event->summary();
438 }
439 }
440
441 MonthViewItem *item = new MonthViewItem( event, mDate, text );
442 QPalette pal;
443 if (KOPrefs::instance()->mMonthViewUsesCategoryColor) {
444 QStringList categories = event->categories();
445 QString cat = categories.first();
446 if ( KOPrefs::instance()->mMonthViewUsesForegroundColor ) {
447 pal = getPalette();
448 if (cat.isEmpty()) {
449 pal.setColor(QColorGroup::Foreground,KOPrefs::instance()->mEventColor);
450 } else {
451 pal.setColor(QColorGroup::Foreground, *(KOPrefs::instance()->categoryColor(cat)));
452 }
453
454 } else {
455 if (cat.isEmpty()) {
456 pal = QPalette(KOPrefs::instance()->mEventColor, KOPrefs::instance()->mEventColor);
457 } else {
458 pal = QPalette(*(KOPrefs::instance()->categoryColor(cat)), *(KOPrefs::instance()->categoryColor(cat)));
459 }
460 }
461
462 } else {
463 pal = mStandardPalette ;
464 }
465 item->setPalette( pal );
466 item->setRecur( event->recurrence()->doesRecur() );
467 item->setAlarm( event->isAlarmEnabled() );
468 item->setMoreInfo( event->description().length() > 0 );
469 Attendee *me = event->attendeeByMails(KOPrefs::instance()->mAdditionalMails,
470 KOPrefs::instance()->email());
471 if ( me != 0 ) {
472 if ( me->status() == Attendee::NeedsAction && me->RSVP())
473 item->setReply(true);
474 else
475 item->setReply(false);
476 } else
477 item->setReply(false);
478 bool insert = true;
479 if ( !(event->doesRecur() == Recurrence::rNone) ) {
480 if ( !KOPrefs::instance()->mMonthDailyRecur && event->doesRecur() == Recurrence::rDaily )
481 insert = false;
482 else
483 if ( !KOPrefs::instance()->mMonthWeeklyRecur && event->doesRecur() == Recurrence::rWeekly )
484 insert = false;
485
486 }
487 if ( insert )
488 mItemList->insertItem( item );
489 }
490
491 // insert due todos
492 QPtrList<Todo> todos = mMonthView->calendar()->todos( mDate );
493 Todo *todo;
494 for(todo = todos.first(); todo; todo = todos.next()) {
495 QString text;
496 if (todo->hasDueDate()) {
497 if (!todo->doesFloat()) {
498 text += KGlobal::locale()->formatTime(todo->dtDue().time());
499 text += " ";
500 }
501 }
502 text += i18n("To-Do: %1").arg(todo->summary());
503
504 MonthViewItem *item = new MonthViewItem( todo, mDate, text );
505 //item->setPalette( mStandardPalette );
506 QPalette pal;
507 if (KOPrefs::instance()->mMonthViewUsesCategoryColor) {
508 QStringList categories = todo->categories();
509 QString cat = categories.first();
510 if ( KOPrefs::instance()->mMonthViewUsesForegroundColor ) {
511 pal = getPalette();
512 if (cat.isEmpty()) {
513 pal.setColor(QColorGroup::Foreground,KOPrefs::instance()->mEventColor);
514 } else {
515 pal.setColor(QColorGroup::Foreground, *(KOPrefs::instance()->categoryColor(cat)));
516 }
517
518 } else {
519 if (cat.isEmpty()) {
520 pal = QPalette(KOPrefs::instance()->mEventColor, KOPrefs::instance()->mEventColor);
521 } else {
522 pal = QPalette(*(KOPrefs::instance()->categoryColor(cat)), *(KOPrefs::instance()->categoryColor(cat)));
523 }
524 }
525
526 } else {
527 pal = mStandardPalette ;
528 }
529 item->setPalette( pal );
530 mItemList->insertItem( item );
531 }
532 //setMyPalette();
533 setMyPalette();
534 resizeEvent( 0 );
535 // if ( isVisible())
536 // qApp->processEvents();
537}
538
539void MonthViewCell::updateConfig()
540{
541
542 setFont( KOPrefs::instance()->mMonthViewFont );
543
544 QFontMetrics fm( font() );
545 mLabelSize = fm.size( 0, "30" ) + QSize( 4, 2 );
546 mLabelBigSize = fm.size( 0, "Mag 30" ) + QSize( 2, 2 );
547 mHolidayPalette = mStandardPalette;
548 mPrimaryPalette = mStandardPalette;
549 mNonPrimaryPalette = mStandardPalette;
550 if ( KOPrefs::instance()->mMonthViewUsesDayColors ) {
551 mHolidayPalette.setColor(QColorGroup::Base, KOPrefs::instance()->mMonthViewHolidayColor );
552 mHolidayPalette.setColor(QColorGroup::Background, KOPrefs::instance()->mMonthViewHolidayColor );
553 mHolidayPalette.setColor(QColorGroup::Foreground, KOPrefs::instance()->mMonthViewHolidayColor.dark());
554 mPrimaryPalette.setColor(QColorGroup::Foreground,KOPrefs::instance()->mMonthViewOddColor.dark());
555 mPrimaryPalette.setColor(QColorGroup::Base,KOPrefs::instance()->mMonthViewOddColor);
556 mPrimaryPalette.setColor(QColorGroup::Background,KOPrefs::instance()->mMonthViewOddColor);
557 mNonPrimaryPalette.setColor(QColorGroup::Foreground,KOPrefs::instance()->mMonthViewEvenColor.dark());
558 mNonPrimaryPalette.setColor(QColorGroup::Base,KOPrefs::instance()->mMonthViewEvenColor);
559 mNonPrimaryPalette.setColor(QColorGroup::Background,KOPrefs::instance()->mMonthViewEvenColor);
560 }
561 updateCell();
562}
563
564void MonthViewCell::enableScrollBars( bool enabled )
565{
566 if ( enabled ) {
567 mItemList->setVScrollBarMode(QScrollView::Auto);
568 mItemList->setHScrollBarMode(QScrollView::Auto);
569 } else {
570 mItemList->setVScrollBarMode(QScrollView::AlwaysOff);
571 mItemList->setHScrollBarMode(QScrollView::AlwaysOff);
572 }
573}
574
575Incidence *MonthViewCell::selectedIncidence()
576{
577 int index = mItemList->currentItem();
578 if ( index < 0 ) return 0;
579
580 MonthViewItem *item =
581 static_cast<MonthViewItem *>( mItemList->item( index ) );
582
583 if ( !item ) return 0;
584
585 return item->incidence();
586}
587
588QDate MonthViewCell::selectedIncidenceDate()
589{
590 QDate qd;
591 int index = mItemList->currentItem();
592 if ( index < 0 ) return qd;
593
594 MonthViewItem *item =
595 static_cast<MonthViewItem *>( mItemList->item( index ) );
596
597 if ( !item ) return qd;
598
599 return item->incidenceDate();
600}
601
602void MonthViewCell::deselect()
603{
604 mItemList->clearSelection();
605 enableScrollBars( false );
606 // updateCell();
607}
608void MonthViewCell::select()
609{
610 ;// updateCell();
611}
612
613void MonthViewCell::resizeEvent ( QResizeEvent * )
614{
615 int size = height() - mLabel->height();
616 if ( size > 0 )
617 mItemList->verticalScrollBar()->setMaximumHeight( size );
618 size = width() - mLabel->width();
619 if ( size > 0 )
620 mItemList->horizontalScrollBar()->setMaximumWidth( size );
621 mLabel->move( width()-mItemList->lineWidth() - mLabel->width(), height()-mItemList->lineWidth() - mLabel->height() );
622 //mLabel->setMaximumWidth( width() - mItemList->lineWidth()*2);
623}
624
625void MonthViewCell::defaultAction( QListBoxItem *item )
626{
627 if ( !item ) return;
628
629 MonthViewItem *eventItem = static_cast<MonthViewItem *>( item );
630 Incidence *incidence = eventItem->incidence();
631 if ( incidence ) mMonthView->defaultAction( incidence );
632}
633void MonthViewCell::newEvent()
634{
635 QDateTime dt( date(), QTime( KOPrefs::instance()->mStartTime, 0 ) );
636 emit newEventSignal( dt );
637}
638void MonthViewCell::cellClicked( QListBoxItem *item )
639{
640 static QListBoxItem * lastClicked = 0;
641 if ( item == 0 ) {
642 QDateTime dt( date(), QTime( KOPrefs::instance()->mStartTime, 0 ) );
643 emit newEventSignal( dt );
644 return;
645 }
646 /*
647 if ( lastClicked )
648 if ( ! item ) {
649 if ( lastClicked->listBox() != item->listBox() )
650 lastClicked->listBox()->clearSelection();
651 }
652 */
653
654 mMonthView->setSelectedCell( this );
655 if( KOPrefs::instance()->mEnableMonthScroll ) enableScrollBars( true );
656 select();
657}
658
659void MonthViewCell::contextMenu( QListBoxItem *item )
660{
661 if ( !item ) return;
662
663 MonthViewItem *eventItem = static_cast<MonthViewItem *>( item );
664 Incidence *incidence = eventItem->incidence();
665 if ( incidence ) mMonthView->showContextMenu( incidence );
666}
667
668void MonthViewCell::selection( QListBoxItem *item )
669{
670 if ( !item ) return;
671
672 mMonthView->setSelectedCell( this );
673}
674
675
676// *******************************************************************************
677// *******************************************************************************
678// *******************************************************************************
679
680
681KOMonthView::KOMonthView(Calendar *calendar, QWidget *parent, const char *name)
682 : KOEventView( calendar, parent, name ),
683 mDaysPerWeek( 7 ), mNumWeeks( 6 ), mNumCells( mDaysPerWeek * mNumWeeks ),
684 mShortDayLabels( false ), mWidthLongDayLabel( 0 ), mSelectedCell( 0 )
685{
686 mCells.setAutoDelete( true );
687 mShowSatSunComp = KOPrefs::instance()->mMonthViewSatSunTog ;
688 // mDayLayout = new QGridLayout( this );
689 // create the day of the week labels (Sun, Mon, etc) and add them to
690 // the layout.
691 mDayLabels.resize( mDaysPerWeek );
692 QFont bfont = font();
693 if ( QApplication::desktop()->width() < 650 ) {
694 bfont.setPointSize( bfont.pointSize() - 2 );
695 }
696 bfont.setBold( true );
697 int i;
698
699 for( i = 0; i < mDaysPerWeek; i++ ) {
700 QLabel *label = new QLabel( this );
701 label->setFont(bfont);
702 label->setFrameStyle(QFrame::Panel|QFrame::Raised);
703 label->setLineWidth(1);
704 label->setAlignment(AlignCenter);
705 mDayLabels.insert( i, label );
706 }
707
708 bfont.setBold( false );
709 mWeekLabels.resize( mNumWeeks+1 );
710 for( i = 0; i < mNumWeeks+1; i++ ) {
711 KOWeekButton *label = new KOWeekButton( this );
712 label->setFont(bfont);
713 connect( label, SIGNAL( selectWeekNum ( int )),this, SIGNAL( selectWeekNum ( int )) );
714 label->setFlat(true);
715 QWhatsThis::add(label,i18n("Click on the week number to\nshow week in agenda view"));
716 //label->setFrameStyle(QFrame::Panel|QFrame::Raised);
717 //label->setLineWidth(1);
718 //label->setAlignment(AlignCenter);
719 mWeekLabels.insert( i, label );
720 }
721 mWeekLabels[mNumWeeks]->setText( i18n("W"));
722 int row, col;
723 mCells.resize( mNumCells );
724 for( row = 0; row < mNumWeeks; ++row ) {
725 for( col = 0; col < mDaysPerWeek; ++col ) {
726 MonthViewCell *cell = new MonthViewCell( this );
727 mCells.insert( row * mDaysPerWeek + col, cell );
728
729 connect( cell, SIGNAL( defaultAction( Incidence * ) ),
730 SLOT( defaultAction( Incidence * ) ) );
731 connect( cell, SIGNAL( newEventSignal( QDateTime ) ),
732 SIGNAL( newEventSignal( QDateTime ) ) );
733 }
734 }
735
736 mContextMenu = eventPopup();
737 // updateConfig(); //useless here
738
739 emit incidenceSelected( 0 );
740}
741
742KOMonthView::~KOMonthView()
743{
744 delete mContextMenu;
745}
746
747int KOMonthView::maxDatesHint()
748{
749 return mNumCells;
750}
751
752int KOMonthView::currentDateCount()
753{
754 return mNumCells;
755}
756
757QPtrList<Incidence> KOMonthView::selectedIncidences()
758{
759 QPtrList<Incidence> selected;
760
761 if ( mSelectedCell ) {
762 Incidence *incidence = mSelectedCell->selectedIncidence();
763 if ( incidence ) selected.append( incidence );
764 }
765
766 return selected;
767}
768
769DateList KOMonthView::selectedDates()
770{
771 DateList selected;
772
773 if ( mSelectedCell ) {
774 QDate qd = mSelectedCell->selectedIncidenceDate();
775 if ( qd.isValid() ) selected.append( qd );
776 }
777
778 return selected;
779}
780
781void KOMonthView::printPreview(CalPrinter *calPrinter, const QDate &fd,
782 const QDate &td)
783{
784#ifndef KORG_NOPRINTER
785 calPrinter->preview(CalPrinter::Month, fd, td);
786#endif
787}
788
789void KOMonthView::updateConfig()
790{
791
792 mWeekStartsMonday = KGlobal::locale()->weekStartsMonday();
793
794 QFontMetrics fontmetric(mDayLabels[0]->font());
795 mWidthLongDayLabel = 0;
796
797 for (int i = 0; i < 7; i++) {
798 int width = fontmetric.width(KOGlobals::self()->calendarSystem()->weekDayName(i+1));
799 if ( width > mWidthLongDayLabel ) mWidthLongDayLabel = width;
800 }
801 bool temp = mShowSatSunComp ;
802 mShowSatSunComp = KOPrefs::instance()->mMonthViewSatSunTog ;
803 if ( temp != KOPrefs::instance()->mMonthViewSatSunTog )
804 computeLayout();
805 updateDayLabels();
806 //qDebug("KOMonthView::updateConfig() %d %d %d ",height(), mDayLabels[0]->sizeHint().height() ,mNumWeeks);
807 int cellHeight = (height() - mDayLabels[0]->sizeHint().height()) /mNumWeeks;
808 //resizeEvent( 0 );
809 for (uint i = 0; i < mCells.count(); ++i) {
810 mCells[i]->updateConfig();
811 }
812}
813
814void KOMonthView::updateDayLabels()
815{
816
817 for (int i = 0; i < 7; i++) {
818 if (mWeekStartsMonday) {
819 bool show = mShortDayLabels;
820 if ( i > 4 && mShowSatSunComp && mWidthLongDayLabel > mDayLabels[i]->width() )
821 show = true;
822 mDayLabels[i]->setText(KOGlobals::self()->calendarSystem()->weekDayName(i+1,show));
823 } else {
824 if (i==0) mDayLabels[i]->setText(KOGlobals::self()->calendarSystem()->weekDayName(7,mShortDayLabels));
825 else mDayLabels[i]->setText(KOGlobals::self()->calendarSystem()->weekDayName(i,mShortDayLabels));
826
827 }
828 }
829}
830
831void KOMonthView::showDates(const QDate &start, const QDate &)
832{
833// kdDebug() << "KOMonthView::showDates(): " << start.toString() << endl;
834
835
836 mStartDate = start;
837
838 int startWeekDay = mWeekStartsMonday ? 1 : 7;
839
840 while( KOGlobals::self()->calendarSystem()->dayOfWeek(mStartDate) != startWeekDay ) {
841 mStartDate = mStartDate.addDays( -1 );
842 }
843
844 bool primary = false;
845 uint i;
846 for( i = 0; i < mCells.size(); ++i ) {
847 QDate date = mStartDate.addDays( i );
848 mCells[i]->setDate( date );
849
850#ifndef KORG_NOPLUGINS
851 // add holiday, if present
852 QString hstring(KOCore::self()->holiday(date));
853 mCells[i]->setHoliday( hstring );
854#endif
855
856 }
857 QDate date = mStartDate.addDays( mWeekStartsMonday ? 3 : 4 );
858 for( i = 0; i < 6; ++i ) {
859 int wno;
860 // remember, according to ISO 8601, the first week of the year is the
861 // first week that contains a thursday. Thus we must subtract off 4,
862 // not just 1.
863 int dayOfYear = date.dayOfYear();
864 if (dayOfYear % 7 != 0)
865 wno = dayOfYear / 7 + 1;
866 else
867 wno =dayOfYear / 7;
868 mWeekLabels[i]->setWeekNum( wno );
869 date = date.addDays( 7 );
870 }
871 updateView();
872}
873
874void KOMonthView::showEvents(QPtrList<Event>)
875{
876 qDebug("KOMonthView::selectEvents is not implemented yet. ");
877}
878
879void KOMonthView::changeEventDisplay(Event *, int)
880{
881 // this should be re-written to be much more efficient, but this
882 // quick-and-dirty-hack gets the job done for right now.
883 updateView();
884}
885
886void KOMonthView::updateView()
887{
888
889 uint i;
890 for( i = 0; i < mCells.count(); ++i ) {
891 mCells[i]->updateCell();
892 }
893 //qDebug("KOMonthView::updateView() ");
894 processSelectionChange();
895}
896
897void KOMonthView::resizeEvent(QResizeEvent * e)
898{
899 computeLayout();
900}
901void KOMonthView::computeLayout()
902{
903 // select the appropriate heading string size. E.g. "Wednesday" or "Wed".
904 // note this only changes the text if the requested size crosses the
905 // threshold between big enough to support the full name and not big
906 // enough.
907
908 int daysToShow = 7;
909 bool combinedSatSun = false;
910 if (mShowSatSunComp = KOPrefs::instance()->mMonthViewSatSunTog ) {
911 daysToShow = 6;
912 combinedSatSun = true;
913 }
914 int tWid = topLevelWidget()->size().width();
915 int tHei = topLevelWidget()->size().height();
916
917 int wid = size().width();//e
918 int hei = size().height()-1;
919
920 if ( ((wid *3)/2) < tWid && (( hei *3) /2) < tHei )
921 return;
922
923 QFontMetrics fm ( mWeekLabels[0]->font() );
924 int weeklabelwid = fm.width( "888" );
925 wid -= weeklabelwid;
926
927 int colWid = wid / daysToShow;
928 int lastCol = wid - ( colWid*6 );
929 int dayLabelHei = mDayLabels[0]->sizeHint().height();
930 int cellHei = (hei - dayLabelHei) /6;
931 int colModulo = wid % daysToShow;
932 int rowModulo = (hei- dayLabelHei) % 6;
933 //qDebug("rowmod %d ", rowModulo);
934 int i;
935 int x,y,w,h;
936 x= 0;
937 y= 0;
938 w = colWid;
939 h = dayLabelHei ;
940 for ( i = 0; i < 7; i++) {
941 if ( i == daysToShow-colModulo )
942 ++w;
943 if ( combinedSatSun ) {
944 if ( i >= daysToShow-1 ) {
945 mDayLabels[i]->setGeometry( x+weeklabelwid,y,w/2,h);
946 x -= w/2 ;
947 }
948 else
949 mDayLabels[i]->setGeometry( x+weeklabelwid,y,w,h);
950 } else
951 mDayLabels[i]->setGeometry( x+weeklabelwid,y,w,h);
952 x += w;
953 }
954 x= 0;
955 y= dayLabelHei;
956 w = colWid;
957 h = cellHei ;
958 for ( i = 0; i < mCells.count(); ++i) {
959
960 w = colWid;
961 if ( ((i) % 7) >= 7-colModulo ) {
962 ++w;
963 }
964 if ( i == (6-rowModulo)*7)
965 ++h;
966 if ( combinedSatSun ) {
967 if ( (i)%7 >= daysToShow-1 ) {
968 if ( (i)%7 == daysToShow-1 ) {
969 mCells[i]->setGeometry ( x+weeklabelwid,y,w,h/2 );
970 x -= w ;y += h/2;
971 } else {
972 mCells[i]->setGeometry ( x+weeklabelwid,y,w,h-h/2 );
973 y -= h/2;
974 }
975 } else
976 mCells[i]->setGeometry ( x+weeklabelwid,y,w,h );
977
978 }
979 else
980 mCells[i]->setGeometry ( x+weeklabelwid,y,w,h );
981 x += w;
982 if ( x + w/2 > wid ) {
983 x = 0;
984 y += h;
985 }
986 }
987 y= dayLabelHei;
988 h = cellHei ;
989 for ( i = 0; i < 6; i++) {
990 if ( i == (6-rowModulo))
991 ++h;
992 mWeekLabels[i]->setGeometry( 0,y,weeklabelwid,h);
993 y += h;
994 }
995 mWeekLabels[6]->setGeometry( 0,0,weeklabelwid,dayLabelHei);
996 // qDebug("RRRRRRRRRRRRR %d %d old %d %d", e->size().width(),e->size().height() , e->oldSize().width(),e->oldSize().height());
997 //qDebug("parent %d %d ", topLevelWidget()->size().width(), topLevelWidget()->size().height());
998 mShortDayLabels = mDayLabels[0]->width() < mWidthLongDayLabel ;
999 updateDayLabels();
1000}
1001
1002void KOMonthView::showContextMenu( Incidence *incidence )
1003{
1004 mContextMenu->showIncidencePopup(incidence);
1005 /*
1006 if( incidence && incidence->type() == "Event" ) {
1007 Event *event = static_cast<Event *>(incidence);
1008 mContextMenu->showEventPopup(event);
1009 } else {
1010 kdDebug() << "MonthView::showContextMenu(): cast failed." << endl;
1011 }
1012 */
1013}
1014MonthViewCell * KOMonthView::selectedCell( )
1015{
1016 return mSelectedCell;
1017}
1018void KOMonthView::setSelectedCell( MonthViewCell *cell )
1019{
1020 // qDebug("KOMonthView::setSelectedCell ");
1021 if ( mSelectedCell && mSelectedCell != cell ) {
1022 MonthViewCell * mvc = mSelectedCell;
1023 mSelectedCell = cell;
1024 mvc->deselect();
1025 } else
1026 mSelectedCell = cell;
1027 // if ( mSelectedCell )
1028// mSelectedCell->select();
1029 if ( !mSelectedCell )
1030 emit incidenceSelected( 0 );
1031 else
1032 emit incidenceSelected( mSelectedCell->selectedIncidence() );
1033}
1034
1035void KOMonthView::processSelectionChange()
1036{
1037 QPtrList<Incidence> incidences = selectedIncidences();
1038 if (incidences.count() > 0) {
1039 emit incidenceSelected( incidences.first() );
1040 } else {
1041 emit incidenceSelected( 0 );
1042 }
1043}
1044
1045void KOMonthView::clearSelection()
1046{
1047 if ( mSelectedCell ) {
1048 mSelectedCell->deselect();
1049 mSelectedCell = 0;
1050 }
1051}
1052void KOMonthView::keyPressEvent ( QKeyEvent * e )
1053{
1054
1055 e->ignore();
1056
1057}