00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include <qpopupmenu.h>
00027 #include <qfont.h>
00028 #include <qfontmetrics.h>
00029 #include <qkeycode.h>
00030 #include <qhbox.h>
00031 #include <qvbox.h>
00032 #include <qpushbutton.h>
00033 #include <qtooltip.h>
00034 #include <qpainter.h>
00035 #include <qcursor.h>
00036 #include <qlistbox.h>
00037 #include <qlayout.h>
00038 #include <qlabel.h>
00039
00040 #include <kdebug.h>
00041 #include <klocale.h>
00042 #include <kglobal.h>
00043 #include <kconfig.h>
00044 #include <kiconloader.h>
00045 #include <kwordwrap.h>
00046
00047 #include <kcalendarsystem.h>
00048 #include <libkcal/calfilter.h>
00049 #include <libkcal/calendar.h>
00050 #include <libkcal/incidenceformatter.h>
00051 #include <libkcal/calendarresources.h>
00052
00053 #include "koprefs.h"
00054 #include "koglobals.h"
00055 #include "koincidencetooltip.h"
00056 #include "koeventpopupmenu.h"
00057 #include "kohelper.h"
00058
00059 #include "komonthview.h"
00060 #include "komonthview.moc"
00061
00062
00063
00064 KOMonthCellToolTip::KOMonthCellToolTip( QWidget *parent,
00065 Calendar *calendar,
00066 KNoScrollListBox *lv )
00067 : QToolTip( parent ), mCalendar( calendar )
00068 {
00069 eventlist = lv;
00070 }
00071
00072 void KOMonthCellToolTip::maybeTip( const QPoint &pos )
00073 {
00074 QRect r;
00075 QListBoxItem *it = eventlist->itemAt( pos );
00076 MonthViewItem *i = static_cast<MonthViewItem*>( it );
00077
00078 if( i && KOPrefs::instance()->mEnableToolTips ) {
00079
00080 r=eventlist->itemRect( it );
00081
00082 QString tipText( IncidenceFormatter::toolTipStr( mCalendar, i->incidence() ) );
00083 if ( !tipText.isEmpty() ) {
00084 tip( r, tipText );
00085 }
00086 }
00087 }
00088
00089 KNoScrollListBox::KNoScrollListBox( QWidget *parent, const char *name )
00090 : QListBox( parent, name ),
00091 mSqueezing( false )
00092 {
00093 QPalette pal = palette();
00094 pal.setColor( QColorGroup::Foreground, KOPrefs::instance()->agendaBgColor().dark( 150 ) );
00095 pal.setColor( QColorGroup::Base, KOPrefs::instance()->agendaBgColor() );
00096 setPalette( pal );
00097 }
00098
00099 void KNoScrollListBox::setBackground( bool primary, bool workDay )
00100 {
00101 QColor color;
00102 if ( workDay ) {
00103 color = KOPrefs::instance()->workingHoursColor();
00104 } else {
00105 color = KOPrefs::instance()->agendaBgColor();
00106 }
00107
00108 QPalette pal = palette();
00109 if ( primary ) {
00110 pal.setColor( QColorGroup::Base, color );
00111 } else {
00112 pal.setColor( QColorGroup::Base, color.dark( 115 ) );
00113 }
00114 setPalette( pal );
00115 }
00116
00117 void KNoScrollListBox::keyPressEvent( QKeyEvent *e )
00118 {
00119 switch( e->key() ) {
00120 case Key_Right:
00121 scrollBy( 4, 0 );
00122 break;
00123 case Key_Left:
00124 scrollBy( -4, 0 );
00125 break;
00126 case Key_Up:
00127 if ( !count() ) break;
00128 setCurrentItem( ( currentItem() + count() - 1 ) % count() );
00129 if ( !itemVisible( currentItem() ) ) {
00130 if ( (unsigned int)currentItem() == ( count() - 1 ) ) {
00131 setTopItem( currentItem() - numItemsVisible() + 1 );
00132 } else {
00133 setTopItem( topItem() - 1 );
00134 }
00135 }
00136 break;
00137 case Key_Down:
00138 if ( !count() ) break;
00139 setCurrentItem( ( currentItem() + 1 ) % count() );
00140 if( !itemVisible( currentItem() ) ) {
00141 if( currentItem() == 0 ) {
00142 setTopItem( 0 );
00143 } else {
00144 setTopItem( topItem() + 1 );
00145 }
00146 }
00147 case Key_Shift:
00148 emit shiftDown();
00149 break;
00150 default:
00151 break;
00152 }
00153 }
00154
00155 void KNoScrollListBox::keyReleaseEvent( QKeyEvent *e )
00156 {
00157 switch( e->key() ) {
00158 case Key_Shift:
00159 emit shiftUp();
00160 break;
00161 default:
00162 break;
00163 }
00164 }
00165
00166 void KNoScrollListBox::mousePressEvent( QMouseEvent *e )
00167 {
00168 QListBox::mousePressEvent( e );
00169
00170 if ( e->button() == RightButton ) {
00171 emit rightClick();
00172 }
00173 }
00174
00175 void KNoScrollListBox::contentsMouseDoubleClickEvent ( QMouseEvent * e )
00176 {
00177 QListBox::contentsMouseDoubleClickEvent( e );
00178 QListBoxItem *item = itemAt( e->pos() );
00179 if ( !item ) {
00180 emit doubleClicked( item );
00181 }
00182 }
00183
00184 void KNoScrollListBox::resizeEvent( QResizeEvent *e )
00185 {
00186 bool s = count() && ( maxItemWidth() > e->size().width() );
00187 if ( mSqueezing || s )
00188 triggerUpdate( false );
00189
00190 mSqueezing = s;
00191 QListBox::resizeEvent( e );
00192 }
00193
00194 MonthViewItem::MonthViewItem( Incidence *incidence, const QDateTime &qd,
00195 const QString & s ) : QListBoxItem()
00196 {
00197 setText( s );
00198
00199 mIncidence = incidence;
00200 mDateTime = qd;
00201
00202 mEventPixmap = KOGlobals::self()->smallIcon( "appointment" );
00203 mBirthdayPixmap = KOGlobals::self()->smallIcon( "calendarbirthday" );
00204 mAnniversaryPixmap= KOGlobals::self()->smallIcon( "calendaranniversary" );
00205 mTodoPixmap = KOGlobals::self()->smallIcon( "todo" );
00206 mTodoDonePixmap = KOGlobals::self()->smallIcon( "checkedbox" );
00207 mAlarmPixmap = KOGlobals::self()->smallIcon( "bell" );
00208 mRecurPixmap = KOGlobals::self()->smallIcon( "recur" );
00209 mReplyPixmap = KOGlobals::self()->smallIcon( "mail_reply" );
00210
00211 mEvent = false;
00212 mTodo = false;
00213 mTodoDone = false;
00214 mRecur = false;
00215 mAlarm = false;
00216 mReply = false;
00217 }
00218
00219 QColor MonthViewItem::catColor() const
00220 {
00221 QColor retColor;
00222 if ( !mIncidence ) {
00223 return retColor;
00224 }
00225
00226 QStringList categories = mIncidence->categories();
00227 QString cat;
00228 if ( !categories.isEmpty() ) {
00229 cat = categories.first();
00230 }
00231 if ( cat.isEmpty() ) {
00232 retColor = KOPrefs::instance()->unsetCategoryColor();
00233 } else {
00234 retColor = *( KOPrefs::instance()->categoryColor( cat ) );
00235 }
00236 return retColor;
00237 }
00238
00239 void MonthViewItem::paint( QPainter *p )
00240 {
00241 #if QT_VERSION >= 0x030000
00242 bool sel = isSelected();
00243 #else
00244 bool sel = selected();
00245 #endif
00246
00247 QColor bgColor = QColor();
00248 if ( mIncidence && mTodo ) {
00249 if ( static_cast<Todo*>( mIncidence )->isOverdue() ) {
00250 bgColor = KOPrefs::instance()->todoOverdueColor();
00251 } else if ( static_cast<Todo*>( mIncidence )->dtDue().date() == QDate::currentDate() ) {
00252 bgColor = KOPrefs::instance()->todoDueTodayColor();
00253 }
00254 }
00255
00256 if ( !bgColor.isValid() ) {
00257 if ( KOPrefs::instance()->monthItemColors() == KOPrefs::MonthItemResourceOnly ||
00258 KOPrefs::instance()->monthItemColors() == KOPrefs::MonthItemResourceInsideCategoryOutside ) {
00259 bgColor = resourceColor();
00260 } else {
00261 bgColor = catColor();
00262 }
00263
00264 if ( !bgColor.isValid() ) {
00265 bgColor = palette().color( QPalette::Normal,
00266 sel ? QColorGroup::Highlight :
00267 QColorGroup::Background );
00268 }
00269 }
00270
00271 QColor frameColor;
00272 if ( KOPrefs::instance()->monthItemColors() == KOPrefs::MonthItemResourceOnly ||
00273 KOPrefs::instance()->monthItemColors() == KOPrefs::MonthItemCategoryInsideResourceOutside ) {
00274 frameColor = resourceColor();
00275 } else {
00276 frameColor = catColor();
00277 }
00278
00279 if ( mIncidence ) {
00280 if ( mIncidence->categories().isEmpty() &&
00281 KOPrefs::instance()->monthItemColors() == KOPrefs::MonthItemResourceInsideCategoryOutside ) {
00282 frameColor = bgColor;
00283 }
00284
00285 if ( mIncidence->categories().isEmpty() &&
00286 KOPrefs::instance()->monthItemColors() == KOPrefs::MonthItemCategoryInsideResourceOutside ) {
00287 bgColor = frameColor;
00288 }
00289 }
00290
00291 if ( !frameColor.isValid() ) {
00292 frameColor = palette().color( QPalette::Normal,
00293 sel ? QColorGroup::Highlight :
00294 QColorGroup::Foreground );
00295 } else {
00296 frameColor = frameColor.dark( 115 );
00297 }
00298
00299
00300 p->setBackgroundColor( frameColor );
00301 p->eraseRect( 0, 0, listBox()->maxItemWidth(), height( listBox() ) );
00302 int offset = 2;
00303 p->setBackgroundColor( bgColor );
00304 p->eraseRect( offset, offset, listBox()->maxItemWidth()-2*offset, height( listBox() )-2*offset );
00305
00306 int x = 3;
00307
00308 bool specialEvent = false;
00309 if ( mEvent ) {
00310 if ( mIncidence->customProperty( "KABC", "BIRTHDAY" ) == "YES" ) {
00311 specialEvent = true;
00312 if ( mIncidence->customProperty( "KABC", "ANNIVERSARY" ) == "YES" ) {
00313 p->drawPixmap( x, 0, mAnniversaryPixmap );
00314 x += mAnniversaryPixmap.width() + 2;
00315 } else {
00316 p->drawPixmap( x, 0, mBirthdayPixmap );
00317 x += mBirthdayPixmap.width() + 2;
00318 }
00319
00320
00321
00322
00323
00324 }
00325 }
00326
00327 if ( mTodo ) {
00328 p->drawPixmap( x, 0, mTodoPixmap );
00329 x += mTodoPixmap.width() + 2;
00330 }
00331 if ( mTodoDone ) {
00332 p->drawPixmap( x, 0, mTodoDonePixmap );
00333 x += mTodoPixmap.width() + 2;
00334 }
00335 if ( mRecur && !specialEvent ) {
00336 p->drawPixmap( x, 0, mRecurPixmap );
00337 x += mRecurPixmap.width() + 2;
00338 }
00339 if ( mAlarm && !specialEvent ) {
00340 p->drawPixmap( x, 0, mAlarmPixmap );
00341 x += mAlarmPixmap.width() + 2;
00342 }
00343 if ( mReply ) {
00344 p->drawPixmap(x, 0, mReplyPixmap );
00345 x += mReplyPixmap.width() + 2;
00346 }
00347 QFontMetrics fm = p->fontMetrics();
00348 int yPos;
00349 int pmheight = QMAX( mRecurPixmap.height(),
00350 QMAX( mAlarmPixmap.height(), mReplyPixmap.height() ) );
00351 if( pmheight < fm.height() )
00352 yPos = fm.ascent() + fm.leading()/2;
00353 else
00354 yPos = pmheight/2 - fm.height()/2 + fm.ascent();
00355 QColor textColor = getTextColor( bgColor );
00356 p->setPen( textColor );
00357
00358 KWordWrap::drawFadeoutText( p, x, yPos, listBox()->width() - x, text() );
00359 }
00360
00361 int MonthViewItem::height( const QListBox *lb ) const
00362 {
00363 return QMAX( QMAX( mRecurPixmap.height(), mReplyPixmap.height() ),
00364 QMAX( mAlarmPixmap.height(), lb->fontMetrics().lineSpacing()+1) );
00365 }
00366
00367 int MonthViewItem::width( const QListBox *lb ) const
00368 {
00369 int x = 3;
00370 if( mRecur ) {
00371 x += mRecurPixmap.width()+2;
00372 }
00373 if( mAlarm ) {
00374 x += mAlarmPixmap.width()+2;
00375 }
00376 if( mReply ) {
00377 x += mReplyPixmap.width()+2;
00378 }
00379
00380 return( x + lb->fontMetrics().boundingRect( text() ).width() + 1 );
00381 }
00382
00383
00384 MonthViewCell::MonthViewCell( KOMonthView *parent)
00385 : QWidget( parent ),
00386 mMonthView( parent ), mPrimary( false ), mHoliday( false )
00387 {
00388 QVBoxLayout *topLayout = new QVBoxLayout( this );
00389
00390 mLabel = new QLabel( this );
00391 mLabel->setFrameStyle( QFrame::Panel | QFrame::Plain );
00392 mLabel->setLineWidth( 1 );
00393 mLabel->setAlignment( AlignCenter );
00394
00395 mItemList = new KNoScrollListBox( this );
00396 mItemList->setMinimumSize( 10, 10 );
00397 mItemList->setFrameStyle( QFrame::Panel | QFrame::Plain );
00398 mItemList->setLineWidth( 1 );
00399
00400 new KOMonthCellToolTip( mItemList->viewport(),
00401 monthView()->calendar(),
00402 static_cast<KNoScrollListBox *>( mItemList ) );
00403
00404 topLayout->addWidget( mItemList );
00405
00406 mLabel->raise();
00407
00408 mStandardPalette = palette();
00409
00410 enableScrollBars( false );
00411
00412 updateConfig();
00413
00414 connect( mItemList, SIGNAL( doubleClicked( QListBoxItem *) ),
00415 SLOT( defaultAction( QListBoxItem * ) ) );
00416 connect( mItemList, SIGNAL( rightButtonPressed( QListBoxItem *,
00417 const QPoint &) ),
00418 SLOT( contextMenu( QListBoxItem * ) ) );
00419 connect( mItemList, SIGNAL( clicked( QListBoxItem * ) ),
00420 SLOT( select() ) );
00421 }
00422
00423 void MonthViewCell::setDate( const QDate &date )
00424 {
00425
00426
00427 mDate = date;
00428
00429 setFrameWidth();
00430
00431 QString text;
00432 if ( KOGlobals::self()->calendarSystem()->day( date ) == 1 ) {
00433 text = i18n("'Month day' for month view cells", "%1 %2")
00434 .arg( KOGlobals::self()->calendarSystem()->monthName( date, true ) )
00435 .arg( KOGlobals::self()->calendarSystem()->day(mDate) );
00436 QFontMetrics fm( mLabel->font() );
00437 mLabel->resize( mLabelSize + QSize( fm.width( text ), 0 ) );
00438 } else {
00439 mLabel->resize( mLabelSize );
00440 text = QString::number( KOGlobals::self()->calendarSystem()->day(mDate) );
00441 }
00442 mLabel->setText( text );
00443
00444 resizeEvent( 0 );
00445 }
00446
00447 QDate MonthViewCell::date() const
00448 {
00449 return mDate;
00450 }
00451
00452 void MonthViewCell::setFrameWidth()
00453 {
00454
00455 if ( mDate == QDate::currentDate() )
00456 mItemList->setLineWidth( 3 );
00457 else
00458 mItemList->setLineWidth( 1 );
00459 }
00460
00461 void MonthViewCell::setPrimary( bool primary )
00462 {
00463 mPrimary = primary;
00464
00465 if ( mPrimary ) {
00466 mLabel->setBackgroundMode( PaletteBase );
00467 } else {
00468 mLabel->setBackgroundMode( PaletteBackground );
00469 }
00470
00471 mItemList->setBackground( mPrimary, KOGlobals::self()->isWorkDay( mDate ) );
00472 }
00473
00474 bool MonthViewCell::isPrimary() const
00475 {
00476 return mPrimary;
00477 }
00478
00479 void MonthViewCell::setHoliday( bool holiday )
00480 {
00481 mHoliday = holiday;
00482
00483 if ( holiday ) {
00484 setPalette( mHolidayPalette );
00485 } else {
00486 setPalette( mStandardPalette );
00487 }
00488 }
00489
00490 void MonthViewCell::setHolidayString( const QString &holiday )
00491 {
00492 mHolidayString = holiday;
00493 }
00494
00495 void MonthViewCell::updateCell()
00496 {
00497 setFrameWidth();
00498
00499 if ( mDate == QDate::currentDate() ) {
00500 setPalette( mTodayPalette );
00501
00502 QPalette pal = mItemList->palette();
00503 pal.setColor( QColorGroup::Foreground, KOPrefs::instance()->highlightColor() );
00504 mItemList->setPalette( pal );
00505 }
00506 else {
00507 if ( mHoliday )
00508 setPalette( mHolidayPalette );
00509 else
00510 setPalette( mStandardPalette );
00511
00512 QPalette pal = mItemList->palette();
00513 pal.setColor( QColorGroup::Foreground, KOPrefs::instance()->agendaBgColor().dark( 150 ) );
00514 mItemList->setPalette( pal );
00515 }
00516
00517 mItemList->clear();
00518
00519 if ( !mHolidayString.isEmpty() ) {
00520 MonthViewItem *item = new MonthViewItem( 0, QDateTime( mDate ), mHolidayString );
00521 item->setPalette( mHolidayPalette );
00522 mItemList->insertItem( item );
00523 }
00524 }
00525
00526 class MonthViewCell::CreateItemVisitor :
00527 public IncidenceBase::Visitor
00528 {
00529 public:
00530 CreateItemVisitor() : mItem(0) { emails = KOPrefs::instance()->allEmails(); }
00531
00532 bool act( IncidenceBase *incidence, QDate date, QPalette stdPal, int multiDay )
00533 {
00534 mItem = 0;
00535 mDate = date;
00536 mStandardPalette = stdPal;
00537 mMultiDay = multiDay;
00538 return incidence->accept( *this );
00539 }
00540 MonthViewItem *item() const { return mItem; }
00541 QStringList emails;
00542
00543 protected:
00544 bool visit( Event *event ) {
00545 QString text;
00546 QDateTime dt( mDate );
00547
00548 QDate dtEnd = event->dtEnd().addSecs( event->doesFloat() ? 0 : -1).date();
00549 int length = event->dtStart().daysTo( dtEnd );
00550 if ( event->isMultiDay() ) {
00551 if ( mDate == event->dtStart().date()
00552 || ( mMultiDay == 0 && event->recursOn( mDate ) ) ) {
00553 text = "(-- " + event->summary();
00554 dt = event->dtStart();
00555 } else if ( !event->doesRecur() && mDate == dtEnd
00556
00557 || ( mMultiDay == length && event->recursOn( mDate.addDays( -length ) ) ) ) {
00558 text = event->summary() + " --)";
00559 } else if (!(event->dtStart().date().daysTo(mDate) % 7) && length > 7 ) {
00560 text = "-- " + event->summary() + " --";
00561 } else {
00562 text = "----------------";
00563 dt = QDateTime( mDate );
00564 }
00565 } else {
00566 if (event->doesFloat())
00567 text = event->summary();
00568 else {
00569 text = KGlobal::locale()->formatTime(event->dtStart().time());
00570 dt.setTime( event->dtStart().time() );
00571 text += ' ' + event->summary();
00572 }
00573 }
00574
00575 mItem = new MonthViewItem( event, dt, text );
00576 mItem->setEvent( true );
00577 if ( KOPrefs::instance()->monthItemColors() == KOPrefs::MonthItemCategoryOnly ||
00578 KOPrefs::instance()->monthItemColors() == KOPrefs::MonthItemCategoryInsideResourceOutside ) {
00579 QStringList categories = event->categories();
00580 QString cat = categories.first();
00581 if (cat.isEmpty()) {
00582 mItem->setPalette(QPalette(KOPrefs::instance()->unsetCategoryColor(),
00583 KOPrefs::instance()->unsetCategoryColor()) );
00584 } else {
00585 mItem->setPalette(QPalette(*(KOPrefs::instance()->categoryColor(cat)),
00586 *(KOPrefs::instance()->categoryColor(cat))));
00587 }
00588 } else {
00589 mItem->setPalette( mStandardPalette );
00590 }
00591
00592 Attendee *me = event->attendeeByMails( emails );
00593 if ( me != 0 ) {
00594 mItem->setReply( me->status() == Attendee::NeedsAction && me->RSVP() );
00595 } else
00596 mItem->setReply(false);
00597 return true;
00598 }
00599 bool visit( Todo *todo ) {
00600 QString text;
00601 if ( !KOPrefs::instance()->showAllDayTodo() )
00602 return false;
00603 QDateTime dt( mDate );
00604 if ( todo->hasDueDate() && !todo->doesFloat() ) {
00605 text += KGlobal::locale()->formatTime( todo->dtDue().time() );
00606 text += ' ';
00607 dt.setTime( todo->dtDue().time() );
00608 }
00609 text += todo->summary();
00610
00611 mItem = new MonthViewItem( todo, dt, text );
00612 if ( todo->doesRecur() ) {
00613 mDate < todo->dtDue().date() ?
00614 mItem->setTodoDone( true ) : mItem->setTodo( true );
00615 }
00616 else
00617 todo->isCompleted() ? mItem->setTodoDone( true ) : mItem->setTodo( true );
00618 mItem->setPalette( mStandardPalette );
00619 return true;
00620 }
00621 protected:
00622 MonthViewItem *mItem;
00623 QDate mDate;
00624 QPalette mStandardPalette;
00625 int mMultiDay;
00626 };
00627
00628
00629 void MonthViewCell::addIncidence( Incidence *incidence, CreateItemVisitor& v, int multiDay )
00630 {
00631 if ( v.act( incidence, mDate, mStandardPalette, multiDay ) ) {
00632 MonthViewItem *item = v.item();
00633 if ( item ) {
00634 item->setAlarm( incidence->isAlarmEnabled() );
00635 item->setRecur( incidence->recurrenceType() );
00636
00637 QColor resourceColor = KOHelper::resourceColor( monthView()->calendar(), incidence );
00638 if ( !resourceColor.isValid() )
00639 resourceColor = KOPrefs::instance()->unsetCategoryColor();
00640 item->setResourceColor( resourceColor );
00641
00642
00643
00644
00645 uint i = 0;
00646 int pos = -1;
00647 QDateTime dt( item->incidenceDateTime() );
00648
00649 while ( i < mItemList->count() && pos<0 ) {
00650 QListBoxItem *item = mItemList->item( i );
00651 MonthViewItem *mvitem = dynamic_cast<MonthViewItem*>( item );
00652 if ( mvitem && mvitem->incidenceDateTime()>dt ) {
00653 pos = i;
00654 }
00655 ++i;
00656 }
00657 mItemList->insertItem( item, pos );
00658 }
00659 }
00660 }
00661
00662 void MonthViewCell::removeIncidence( Incidence *incidence )
00663 {
00664 for ( uint i = 0; i < mItemList->count(); ++i ) {
00665 MonthViewItem *item = static_cast<MonthViewItem *>(mItemList->item( i ) );
00666 if ( item && item->incidence() &&
00667 item->incidence()->uid() == incidence->uid() ) {
00668 mItemList->removeItem( i );
00669 --i;
00670 }
00671 }
00672 }
00673
00674 void MonthViewCell::updateConfig()
00675 {
00676 setFont( KOPrefs::instance()->mMonthViewFont );
00677
00678 QFontMetrics fm( font() );
00679 mLabelSize = fm.size( 0, "30" ) +
00680 QSize( mLabel->frameWidth() * 2, mLabel->frameWidth() * 2 ) +
00681 QSize( 2, 2 );
00682
00683 QColor bg = mStandardPalette.color( QPalette::Active, QColorGroup::Background );
00684 int h,s,v;
00685 bg.getHsv( &h, &s, &v );
00686 if ( date().month() %2 == 0 ) {
00687 if ( v < 128 ) {
00688 bg = bg.light( 125 );
00689 } else {
00690 bg = bg.dark( 125 );
00691 }
00692 }
00693 setPaletteBackgroundColor( bg );
00694
00695
00696 mHolidayPalette = mStandardPalette;
00697 mHolidayPalette.setColor( QColorGroup::Foreground,
00698 KOPrefs::instance()->holidayColor() );
00699 mHolidayPalette.setColor( QColorGroup::Text,
00700 KOPrefs::instance()->holidayColor() );
00701 mTodayPalette = mStandardPalette;
00702 mTodayPalette.setColor( QColorGroup::Foreground,
00703 KOPrefs::instance()->highlightColor() );
00704 mTodayPalette.setColor( QColorGroup::Text,
00705 KOPrefs::instance()->highlightColor() );
00706 updateCell();
00707
00708 mItemList->setBackground( mPrimary, KOGlobals::self()->isWorkDay( mDate ) );
00709 }
00710
00711 void MonthViewCell::enableScrollBars( bool enabled )
00712 {
00713 if ( enabled ) {
00714 mItemList->setVScrollBarMode( QScrollView::Auto );
00715 mItemList->setHScrollBarMode( QScrollView::Auto );
00716 } else {
00717 mItemList->setVScrollBarMode( QScrollView::AlwaysOff );
00718 mItemList->setHScrollBarMode( QScrollView::AlwaysOff );
00719 }
00720 }
00721
00722 Incidence *MonthViewCell::selectedIncidence()
00723 {
00724 int index = mItemList->currentItem();
00725 if ( index < 0 ) return 0;
00726
00727 MonthViewItem *item =
00728 static_cast<MonthViewItem *>( mItemList->item( index ) );
00729
00730 if ( !item ) return 0;
00731
00732 return item->incidence();
00733 }
00734
00735 QDate MonthViewCell::selectedIncidenceDate()
00736 {
00737 QDate qd;
00738 int index = mItemList->currentItem();
00739 if ( index < 0 ) return qd;
00740
00741 MonthViewItem *item =
00742 static_cast<MonthViewItem *>( mItemList->item( index ) );
00743
00744 if ( !item ) return qd;
00745
00746 return item->incidenceDateTime().date();
00747 }
00748
00749 void MonthViewCell::select()
00750 {
00751
00752 mMonthView->setSelectedCell( this );
00753
00754 if( KOPrefs::instance()->enableMonthScroll() )
00755 enableScrollBars( true );
00756
00757
00758 if( mDate != QDate::currentDate() ) {
00759 mItemList->setFrameStyle( QFrame::Sunken | QFrame::Panel );
00760 mItemList->setLineWidth( 3 );
00761 }
00762 }
00763
00764 void MonthViewCell::deselect()
00765 {
00766 mItemList->clearSelection();
00767 mItemList->setFrameStyle( QFrame::Plain | QFrame::Panel );
00768 setFrameWidth();
00769
00770 enableScrollBars( false );
00771 }
00772
00773 void MonthViewCell::resizeEvent ( QResizeEvent * )
00774 {
00775 mLabel->move( width() - mLabel->width(), height() - mLabel->height() );
00776 }
00777
00778 void MonthViewCell::defaultAction( QListBoxItem *item )
00779 {
00780 select();
00781
00782 if ( !item ) {
00783 emit newEventSignal( date() );
00784 } else {
00785 MonthViewItem *eventItem = static_cast<MonthViewItem *>( item );
00786 Incidence *incidence = eventItem->incidence();
00787 if ( incidence ) mMonthView->defaultAction( incidence );
00788 }
00789 }
00790
00791 void MonthViewCell::contextMenu( QListBoxItem *item )
00792 {
00793 select();
00794
00795 if ( item ) {
00796 MonthViewItem *eventItem = static_cast<MonthViewItem *>( item );
00797 Incidence *incidence = eventItem->incidence();
00798 if ( incidence ) mMonthView->showEventContextMenu( incidence, date() );
00799 }
00800 else {
00801 mMonthView->showGeneralContextMenu();
00802 }
00803 }
00804
00805
00806 KOMonthView::KOMonthView( Calendar *calendar, QWidget *parent, const char *name )
00807 : KOEventView( calendar, parent, name ),
00808 mDaysPerWeek( 7 ), mNumWeeks( 6 ), mNumCells( mDaysPerWeek * mNumWeeks ),
00809 mShortDayLabels( false ), mWidthLongDayLabel( 0 ), mSelectedCell( 0 )
00810 {
00811 mCells.setAutoDelete( true );
00812
00813 QGridLayout *dayLayout = new QGridLayout( this );
00814
00815 QFont bfont = font();
00816 bfont.setBold( true );
00817
00818 QFont mfont = bfont;
00819 mfont.setPointSize( 20 );
00820
00821
00822 mLabel = new QLabel( this );
00823 mLabel->setFont( mfont );
00824 mLabel->setAlignment( AlignCenter );
00825 mLabel->setLineWidth( 0 );
00826 mLabel->setFrameStyle( QFrame::Plain );
00827
00828 dayLayout->addMultiCellWidget( mLabel, 0, 0, 0, mDaysPerWeek );
00829
00830
00831
00832 mDayLabels.resize( mDaysPerWeek );
00833 int i;
00834 for( i = 0; i < mDaysPerWeek; i++ ) {
00835 QLabel *label = new QLabel( this );
00836 label->setFont( bfont );
00837 label->setFrameStyle( QFrame::Panel | QFrame::Raised );
00838 label->setLineWidth( 1 );
00839 label->setAlignment( AlignCenter );
00840
00841 mDayLabels.insert( i, label );
00842
00843 dayLayout->addWidget( label, 1, i );
00844 dayLayout->addColSpacing( i, 10 );
00845 dayLayout->setColStretch( i, 1 );
00846 }
00847
00848 int row, col;
00849
00850 mCells.resize( mNumCells );
00851 for( row = 0; row < mNumWeeks; ++row ) {
00852 for( col = 0; col < mDaysPerWeek; ++col ) {
00853 MonthViewCell *cell = new MonthViewCell( this );
00854 mCells.insert( row * mDaysPerWeek + col, cell );
00855 dayLayout->addWidget( cell, row + 2, col );
00856
00857 connect( cell, SIGNAL( defaultAction( Incidence * ) ),
00858 SLOT( defaultAction( Incidence * ) ) );
00859 connect( cell, SIGNAL( newEventSignal( const QDate & ) ),
00860 SIGNAL( newEventSignal( const QDate & ) ) );
00861 }
00862 dayLayout->setRowStretch( row + 2, 1 );
00863 }
00864
00865 mEventContextMenu = eventPopup();
00866
00867 updateConfig();
00868
00869 emit incidenceSelected( 0 );
00870 }
00871
00872 KOMonthView::~KOMonthView()
00873 {
00874 delete mEventContextMenu;
00875 }
00876
00877 int KOMonthView::maxDatesHint()
00878 {
00879 return mNumCells;
00880 }
00881
00882 int KOMonthView::currentDateCount()
00883 {
00884 return mNumCells;
00885 }
00886
00887 Incidence::List KOMonthView::selectedIncidences()
00888 {
00889 Incidence::List selected;
00890
00891 if ( mSelectedCell ) {
00892 Incidence *incidence = mSelectedCell->selectedIncidence();
00893 if ( incidence ) selected.append( incidence );
00894 }
00895
00896 return selected;
00897 }
00898
00899 DateList KOMonthView::selectedDates()
00900 {
00901 DateList selected;
00902
00903 if ( mSelectedCell ) {
00904 QDate qd = mSelectedCell->selectedIncidenceDate();
00905 if ( qd.isValid() ) selected.append( qd );
00906 }
00907
00908 return selected;
00909 }
00910
00911 bool KOMonthView::eventDurationHint( QDateTime &startDt, QDateTime &endDt, bool &allDay )
00912 {
00913 if ( mSelectedCell ) {
00914 startDt.setDate( mSelectedCell->date() );
00915 endDt.setDate( mSelectedCell->date() );
00916 allDay = true;
00917 return true;
00918 }
00919 return false;
00920 }
00921
00922 void KOMonthView::updateConfig()
00923 {
00924 mWeekStartDay = KGlobal::locale()->weekStartDay();
00925
00926 QFontMetrics fontmetric( mDayLabels[0]->font() );
00927 mWidthLongDayLabel = 0;
00928
00929 for ( int i = 0; i < 7; ++i ) {
00930 int width =
00931 fontmetric.width( KOGlobals::self()->calendarSystem()->weekDayName( i + 1 ) );
00932 if ( width > mWidthLongDayLabel ) mWidthLongDayLabel = width;
00933 }
00934
00935 updateDayLabels();
00936
00937 for ( uint i = 0; i < mCells.count(); ++i ) {
00938 mCells[i]->updateConfig();
00939 }
00940 }
00941
00942 void KOMonthView::updateDayLabels()
00943 {
00944 kdDebug(5850) << "KOMonthView::updateDayLabels()" << endl;
00945
00946 const KCalendarSystem*calsys=KOGlobals::self()->calendarSystem();
00947 int currDay;
00948 for ( int i = 0; i < 7; i++ ) {
00949 currDay = i+mWeekStartDay;
00950 if ( currDay > 7 ) currDay -= 7;
00951 mDayLabels[i]->setText( calsys->weekDayName( currDay, mShortDayLabels ) );
00952 }
00953 }
00954
00955 void KOMonthView::showDates( const QDate &start, const QDate & )
00956 {
00957
00958
00959 const KCalendarSystem *calSys = KOGlobals::self()->calendarSystem();
00960
00961 mDateToCell.clear();
00962
00963
00964 mStartDate = start.addDays( -start.day() + 1 );
00965
00966 int weekdayCol=( mStartDate.dayOfWeek() + 7 - mWeekStartDay ) % 7;
00967 mStartDate = mStartDate.addDays( -weekdayCol );
00968
00969 mLabel->setText( i18n( "monthname year", "%1 %2" )
00970 .arg( calSys->monthName( start ) )
00971 .arg( calSys->year( start ) ) );
00972 if ( !KOPrefs::instance()->fullViewMonth() ) {
00973 mLabel->show();
00974 } else {
00975 mLabel->hide();
00976 }
00977
00978 bool primary = false;
00979 uint i;
00980 for( i = 0; i < mCells.size(); ++i ) {
00981 QDate date = mStartDate.addDays( i );
00982 if ( calSys->day( date ) == 1 ) {
00983 primary = !primary;
00984 }
00985
00986 mCells[i]->setDate( date );
00987 mDateToCell[ date ] = mCells[ i ];
00988 if( date == start )
00989 mCells[i]->select();
00990
00991 mCells[i]->setPrimary( primary );
00992
00993 bool isHoliday = calSys->dayOfWeek( date ) == calSys->weekDayOfPray()
00994 || !KOGlobals::self()->isWorkDay( date );
00995 mCells[i]->setHoliday( isHoliday );
00996
00997
00998 QStringList holidays( KOGlobals::self()->holiday( date ) );
00999 mCells[i]->setHolidayString( holidays.join( i18n("delimiter for joining holiday names", ", " ) ) );
01000 }
01001
01002 updateView();
01003 }
01004
01005 void KOMonthView::showIncidences( const Incidence::List & )
01006 {
01007 kdDebug(5850) << "KOMonthView::showIncidences( const Incidence::List & ) is not implemented yet." << endl;
01008 }
01009
01010 class KOMonthView::GetDateVisitor : public IncidenceBase::Visitor
01011 {
01012 public:
01013 GetDateVisitor() {}
01014
01015 bool act( IncidenceBase *incidence )
01016 {
01017 return incidence->accept( *this );
01018 }
01019 QDateTime startDate() const { return mStartDate; }
01020 QDateTime endDate() const { return mEndDate; }
01021
01022 protected:
01023 bool visit( Event *event ) {
01024 mStartDate = event->dtStart();
01025 mEndDate = event->dtEnd();
01026 return true;
01027 }
01028 bool visit( Todo *todo ) {
01029 if ( todo->hasDueDate() ) {
01030 mStartDate = todo->dtDue();
01031 mEndDate = todo->dtDue();
01032 }
01033
01034 return true;
01035 }
01036 bool visit( Journal *journal ) {
01037 mStartDate = journal->dtStart();
01038 mEndDate = journal->dtStart();
01039 return true;
01040 }
01041 protected:
01042 QDateTime mStartDate;
01043 QDateTime mEndDate;
01044 };
01045
01046 void KOMonthView::changeIncidenceDisplayAdded( Incidence *incidence, MonthViewCell::CreateItemVisitor& v)
01047 {
01048 GetDateVisitor gdv;
01049
01050 if ( !gdv.act( incidence ) ) {
01051 kdDebug(5850) << "Visiting GetDateVisitor failed." << endl;
01052 return;
01053 }
01054
01055 bool floats = incidence->doesFloat();
01056
01057 if ( incidence->doesRecur() ) {
01058 for ( uint i = 0; i < mCells.count(); ++i ) {
01059 if ( incidence->recursOn( mCells[i]->date() ) ) {
01060
01061
01062 int length = gdv.startDate().daysTo( gdv.endDate().addSecs( floats ? 0 : -1 ).date() );
01063 for ( int j = 0; j <= length && i+j < mCells.count(); ++j ) {
01064 mCells[i+j]->addIncidence( incidence, v, j );
01065 }
01066 }
01067 }
01068 } else {
01069
01070 if ( gdv.endDate().isValid() ) {
01071 QDate endDate = gdv.endDate().addSecs( floats ? 0 : -1).date();
01072 for ( QDate date = gdv.startDate().date();
01073 date <= endDate; date = date.addDays( 1 ) ) {
01074 MonthViewCell *mvc = mDateToCell[ date ];
01075 if ( mvc ) mvc->addIncidence( incidence, v );
01076 }
01077 }
01078 }
01079 }
01080
01081 void KOMonthView::changeIncidenceDisplay( Incidence *incidence, int action )
01082 {
01083 MonthViewCell::CreateItemVisitor v;
01084 switch ( action ) {
01085 case KOGlobals::INCIDENCEADDED:
01086 changeIncidenceDisplayAdded( incidence, v );
01087 break;
01088 case KOGlobals::INCIDENCEEDITED:
01089 for( uint i = 0; i < mCells.count(); i++ )
01090 mCells[i]->removeIncidence( incidence );
01091 changeIncidenceDisplayAdded( incidence, v );
01092 break;
01093 case KOGlobals::INCIDENCEDELETED:
01094 for( uint i = 0; i < mCells.count(); i++ )
01095 mCells[i]->removeIncidence( incidence );
01096 break;
01097 default:
01098 return;
01099 }
01100 }
01101
01102 void KOMonthView::updateView()
01103 {
01104 for( uint i = 0; i < mCells.count(); ++i ) {
01105 mCells[i]->updateCell();
01106 }
01107
01108 Incidence::List incidences = calendar()->incidences();
01109 Incidence::List::ConstIterator it;
01110
01111 MonthViewCell::CreateItemVisitor v;
01112 for ( it = incidences.begin(); it != incidences.end(); ++it )
01113 changeIncidenceDisplayAdded( *it, v );
01114
01115 processSelectionChange();
01116 }
01117
01118 void KOMonthView::resizeEvent( QResizeEvent * )
01119 {
01120
01121
01122
01123
01124 if( mDayLabels[0]->width() < mWidthLongDayLabel ) {
01125 if ( !mShortDayLabels ) {
01126 mShortDayLabels = true;
01127 updateDayLabels();
01128 }
01129 } else {
01130 if ( mShortDayLabels ) {
01131 mShortDayLabels = false;
01132 updateDayLabels();
01133 }
01134 }
01135 }
01136
01137 void KOMonthView::showEventContextMenu( Incidence *incidence, const QDate &qd )
01138 {
01139 mEventContextMenu->showIncidencePopup( incidence, qd );
01140 }
01141
01142 void KOMonthView::showGeneralContextMenu()
01143 {
01144 showNewEventPopup();
01145 }
01146
01147 void KOMonthView::setSelectedCell( MonthViewCell *cell )
01148 {
01149 if ( mSelectedCell && cell != mSelectedCell )
01150 mSelectedCell->deselect();
01151
01152 mSelectedCell = cell;
01153
01154 if ( !mSelectedCell )
01155 emit incidenceSelected( 0 );
01156 else
01157 emit incidenceSelected( mSelectedCell->selectedIncidence() );
01158 }
01159
01160 void KOMonthView::processSelectionChange()
01161 {
01162 Incidence::List incidences = selectedIncidences();
01163 if (incidences.count() > 0) {
01164 emit incidenceSelected( incidences.first() );
01165 } else {
01166 emit incidenceSelected( 0 );
01167 }
01168 }
01169
01170 void KOMonthView::clearSelection()
01171 {
01172 if ( mSelectedCell ) {
01173 mSelectedCell->deselect();
01174 mSelectedCell = 0;
01175 }
01176 }