korganizer Library API Documentation

komonthview.cpp

00001 /*
00002     This file is part of KOrganizer.
00003 
00004     Copyright (c) 2000,2001 Cornelius Schumacher <schumacher@kde.org>
00005 
00006     This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00019 
00020     As a special exception, permission is given to link this program
00021     with any edition of Qt, and distribute the resulting executable,
00022     without including the source code for Qt in the source distribution.
00023 */
00024 
00025 #include <qpopupmenu.h>
00026 #include <qfont.h>
00027 #include <qfontmetrics.h>
00028 #include <qkeycode.h>
00029 #include <qhbox.h>
00030 #include <qvbox.h>
00031 #include <qpushbutton.h>
00032 #include <qtooltip.h>
00033 #include <qpainter.h>
00034 #include <qcursor.h>
00035 #include <qlistbox.h>
00036 #include <qlayout.h>
00037 #include <qlabel.h>
00038 
00039 #include <kdebug.h>
00040 #include <klocale.h>
00041 #include <kglobal.h>
00042 #include <kconfig.h>
00043 #include <kiconloader.h>
00044 #include <kwordwrap.h>
00045 
00046 #include <kcalendarsystem.h>
00047 #include <libkcal/calfilter.h>
00048 
00049 #ifndef KORG_NOPRINTER
00050 #include "calprinter.h"
00051 #endif
00052 #include "koprefs.h"
00053 #ifndef KORG_NOPLUGINS
00054 #include "kocore.h"
00055 #endif
00056 #include "koglobals.h"
00057 #include "kohelper.h"
00058 #include "koincidencetooltip.h"
00059 #include "koeventpopupmenu.h"
00060 
00061 #include "komonthview.h"
00062 #include "komonthview.moc"
00063 
00064 //--------------------------------------------------------------------------
00065 
00066 KOMonthCellToolTip::KOMonthCellToolTip( QWidget *parent,
00067                                         KNoScrollListBox *lv )
00068   : QToolTip( parent )
00069 {
00070   eventlist = lv;
00071 }
00072 
00073 void KOMonthCellToolTip::maybeTip( const QPoint & pos )
00074 {
00075   QRect r;
00076   QListBoxItem *it = eventlist->itemAt( pos );
00077   MonthViewItem *i = static_cast<MonthViewItem*>( it );
00078 
00079   if( i && KOPrefs::instance()->mEnableToolTips ) {
00080     /* Calculate the rectangle. */
00081     r=eventlist->itemRect( it );
00082     /* Show the tip */
00083     QString tipText;
00084     ToolTipVisitor<MonthViewItem> v;
00085     if ( v.act( i, &tipText, true ) ) {
00086       tip( r, tipText );
00087     }
00088   }
00089 }
00090 
00091 KNoScrollListBox::KNoScrollListBox( QWidget *parent, const char *name )
00092   : QListBox( parent, name ),
00093     mSqueezing( false )
00094 {
00095   QPalette pal = palette();
00096   pal.setColor( QColorGroup::Foreground, KOPrefs::instance()->agendaBgColor().dark( 150 ) );
00097   pal.setColor( QColorGroup::Base, KOPrefs::instance()->agendaBgColor() );
00098   setPalette( pal );
00099 }
00100 
00101 void KNoScrollListBox::setBackground( bool primary, bool workDay )
00102 {
00103   QColor color;
00104   if ( workDay ) {
00105     color = KOPrefs::instance()->workingHoursColor();
00106   } else {
00107     color = KOPrefs::instance()->agendaBgColor();
00108   }
00109 
00110   QPalette pal = palette();
00111   if ( primary ) {
00112     pal.setColor( QColorGroup::Base, color );
00113   } else {
00114     pal.setColor( QColorGroup::Base, color.dark( 115 ) );
00115   }
00116   setPalette( pal );
00117 }
00118 
00119 void KNoScrollListBox::keyPressEvent( QKeyEvent *e )
00120 {
00121   switch( e->key() ) {
00122     case Key_Right:
00123       scrollBy( 4, 0 );
00124       break;
00125     case Key_Left:
00126       scrollBy( -4, 0 );
00127       break;
00128     case Key_Up:
00129       if ( !count() ) break;
00130       setCurrentItem( ( currentItem() + count() - 1 ) % count() );
00131       if ( !itemVisible( currentItem() ) ) {
00132         if ( (unsigned int)currentItem() == ( count() - 1 ) ) {
00133           setTopItem( currentItem() - numItemsVisible() + 1 );
00134         } else {
00135           setTopItem( topItem() - 1 );
00136         }
00137       }
00138       break;
00139     case Key_Down:
00140       if ( !count() ) break;
00141       setCurrentItem( ( currentItem() + 1 ) % count() );
00142       if( !itemVisible( currentItem() ) ) {
00143         if( currentItem() == 0 ) {
00144           setTopItem( 0 );
00145         } else {
00146           setTopItem( topItem() + 1 );
00147         }
00148       }
00149     case Key_Shift:
00150       emit shiftDown();
00151       break;
00152     default:
00153       break;
00154   }
00155 }
00156 
00157 void KNoScrollListBox::keyReleaseEvent( QKeyEvent *e )
00158 {
00159   switch( e->key() ) {
00160     case Key_Shift:
00161       emit shiftUp();
00162       break;
00163     default:
00164       break;
00165   }
00166 }
00167 
00168 void KNoScrollListBox::mousePressEvent( QMouseEvent *e )
00169 {
00170   QListBox::mousePressEvent( e );
00171 
00172   if ( e->button() == RightButton ) {
00173     emit rightClick();
00174   }
00175 }
00176 
00177 void KNoScrollListBox::contentsMouseDoubleClickEvent ( QMouseEvent * e )
00178 {
00179   QListBox::contentsMouseDoubleClickEvent( e );
00180   QListBoxItem *item = itemAt( e->pos() );
00181   if ( !item ) {
00182     emit doubleClicked( item );
00183   }
00184 }
00185 
00186 void KNoScrollListBox::resizeEvent( QResizeEvent *e )
00187 {
00188   bool s = count() && ( maxItemWidth() > e->size().width() );
00189   if ( mSqueezing || s )
00190     triggerUpdate( false );
00191 
00192   mSqueezing = s;
00193   QListBox::resizeEvent( e );
00194 }
00195 
00196 MonthViewItem::MonthViewItem( Incidence *incidence, QDate qd, const QString & s)
00197   : QListBoxItem()
00198 {
00199   setText( s );
00200 
00201   mIncidence = incidence;
00202   mDate = qd;
00203 
00204   mTodoPixmap      = KOGlobals::self()->smallIcon("todo");
00205   mTodoDonePixmap  = KOGlobals::self()->smallIcon("checkedbox");
00206   mAlarmPixmap     = KOGlobals::self()->smallIcon("bell");
00207   mRecurPixmap     = KOGlobals::self()->smallIcon("recur");
00208   mReplyPixmap     = KOGlobals::self()->smallIcon("mail_reply");
00209 
00210   mTodo      = false;
00211   mTodoDone  = false;
00212   mRecur     = false;
00213   mAlarm     = false;
00214   mReply     = false;
00215 }
00216 
00217 void MonthViewItem::paint( QPainter *p )
00218 {
00219 #if QT_VERSION >= 0x030000
00220   bool sel = isSelected();
00221 #else
00222   bool sel = selected();
00223 #endif
00224 
00225   QColor bgColor = palette().color( QPalette::Normal,
00226             sel ? QColorGroup::Highlight : QColorGroup::Background );
00227   int offset=0;
00228   if ( KOPrefs::instance()->monthViewUsesResourceColor() &&
00229     mResourceColor.isValid() ) {
00230     p->setBackgroundColor( mResourceColor );
00231     p->eraseRect( 0, 0, listBox()->maxItemWidth(), height( listBox() ) );
00232     offset=2;
00233   }
00234   if ( KOPrefs::instance()->monthViewUsesCategoryColor() ) {
00235     p->setBackgroundColor( bgColor );
00236     p->eraseRect( 0, 0, listBox()->maxItemWidth(), height( listBox() ) );
00237   }
00238   int x = 3;
00239   if ( mTodo ) {
00240     p->drawPixmap( x, 0, mTodoPixmap );
00241     x += mTodoPixmap.width() + 2;
00242   }
00243   if ( mTodoDone ) {
00244     p->drawPixmap( x, 0, mTodoDonePixmap );
00245     x += mTodoPixmap.width() + 2;
00246   }
00247   if ( mRecur ) {
00248     p->drawPixmap( x, 0, mRecurPixmap );
00249     x += mRecurPixmap.width() + 2;
00250   }
00251   if ( mAlarm ) {
00252     p->drawPixmap( x, 0, mAlarmPixmap );
00253     x += mAlarmPixmap.width() + 2;
00254   }
00255   if ( mReply ) {
00256     p->drawPixmap(x, 0, mReplyPixmap );
00257     x += mReplyPixmap.width() + 2;
00258   }
00259   QFontMetrics fm = p->fontMetrics();
00260   int yPos;
00261   int pmheight = QMAX( mRecurPixmap.height(),
00262                        QMAX( mAlarmPixmap.height(), mReplyPixmap.height() ) );
00263   if( pmheight < fm.height() )
00264     yPos = fm.ascent() + fm.leading()/2;
00265   else
00266     yPos = pmheight/2 - fm.height()/2  + fm.ascent();
00267   QColor textColor = palette().color( QPalette::Normal, sel ? \
00268           QColorGroup::HighlightedText : QColorGroup::Text );
00269   p->setPen( textColor );
00270 
00271   KWordWrap::drawFadeoutText( p, x, yPos, listBox()->width() - x, text() );
00272 }
00273 
00274 int MonthViewItem::height( const QListBox *lb ) const
00275 {
00276   return QMAX( QMAX( mRecurPixmap.height(), mReplyPixmap.height() ),
00277                QMAX( mAlarmPixmap.height(), lb->fontMetrics().lineSpacing()+1) );
00278 }
00279 
00280 int MonthViewItem::width( const QListBox *lb ) const
00281 {
00282   int x = 3;
00283   if( mRecur ) {
00284     x += mRecurPixmap.width()+2;
00285   }
00286   if( mAlarm ) {
00287     x += mAlarmPixmap.width()+2;
00288   }
00289   if( mReply ) {
00290     x += mReplyPixmap.width()+2;
00291   }
00292 
00293   return( x + lb->fontMetrics().boundingRect( text() ).width() + 1 );
00294 }
00295 
00296 
00297 MonthViewCell::MonthViewCell( KOMonthView *parent)
00298   : QWidget( parent ),
00299     mMonthView( parent ), mPrimary( false ), mHoliday( false )
00300 {
00301   QVBoxLayout *topLayout = new QVBoxLayout( this );
00302 
00303   mLabel = new QLabel( this );
00304   mLabel->setFrameStyle( QFrame::Panel | QFrame::Plain );
00305   mLabel->setLineWidth( 1 );
00306   mLabel->setAlignment( AlignCenter );
00307 
00308   mItemList = new KNoScrollListBox( this );
00309   mItemList->setMinimumSize( 10, 10 );
00310   mItemList->setFrameStyle( QFrame::Panel | QFrame::Plain );
00311   mItemList->setLineWidth( 1 );
00312 
00313   new KOMonthCellToolTip( mItemList->viewport(),
00314                           static_cast<KNoScrollListBox *>( mItemList ) );
00315 
00316   topLayout->addWidget( mItemList );
00317 
00318   mLabel->raise();
00319 
00320   mStandardPalette = palette();
00321 
00322   enableScrollBars( false );
00323 
00324   updateConfig();
00325 
00326   connect( mItemList, SIGNAL( doubleClicked( QListBoxItem *) ),
00327            SLOT( defaultAction( QListBoxItem * ) ) );
00328   connect( mItemList, SIGNAL( rightButtonPressed( QListBoxItem *,
00329                                                   const QPoint &) ),
00330            SLOT( contextMenu( QListBoxItem * ) ) );
00331   connect( mItemList, SIGNAL( highlighted( QListBoxItem *) ),
00332            SLOT( selection( QListBoxItem * ) ) );
00333   connect( mItemList, SIGNAL( clicked( QListBoxItem * ) ),
00334            SLOT( cellClicked( QListBoxItem * ) ) );
00335 }
00336 
00337 void MonthViewCell::setDate( const QDate &date )
00338 {
00339 //  kdDebug(5850) << "MonthViewCell::setDate(): " << date.toString() << endl;
00340 
00341   mDate = date;
00342 
00343   QString text;
00344    if ( KOGlobals::self()->calendarSystem()->day( date ) == 1 ) {
00345      text = KOGlobals::self()->calendarSystem()->monthName( date, true ) + " ";
00346     QFontMetrics fm( mLabel->font() );
00347     mLabel->resize( mLabelSize + QSize( fm.width( text ), 0 ) );
00348   } else {
00349     mLabel->resize( mLabelSize );
00350   }
00351   text += QString::number( KOGlobals::self()->calendarSystem()->day(mDate) );
00352   mLabel->setText( text );
00353 
00354   resizeEvent( 0 );
00355 }
00356 
00357 QDate MonthViewCell::date() const
00358 {
00359   return mDate;
00360 }
00361 
00362 void MonthViewCell::setPrimary( bool primary )
00363 {
00364   mPrimary = primary;
00365 
00366   if ( mPrimary ) {
00367     mLabel->setBackgroundMode( PaletteBase );
00368   } else {
00369     mLabel->setBackgroundMode( PaletteBackground );
00370   }
00371 
00372   mItemList->setBackground( mPrimary, KOCore::self()->isWorkDay( mDate ) );
00373 }
00374 
00375 bool MonthViewCell::isPrimary() const
00376 {
00377   return mPrimary;
00378 }
00379 
00380 void MonthViewCell::setHoliday( bool holiday )
00381 {
00382   mHoliday = holiday;
00383 
00384   if ( holiday ) {
00385     setPalette( mHolidayPalette );
00386   } else {
00387     setPalette( mStandardPalette );
00388   }
00389 }
00390 
00391 void MonthViewCell::setHoliday( const QString &holiday )
00392 {
00393   mHolidayString = holiday;
00394 
00395   if ( !holiday.isEmpty() ) {
00396     setHoliday( true );
00397   }
00398 }
00399 
00400 void MonthViewCell::updateCell()
00401 {
00402   if ( mDate == QDate::currentDate() ) {
00403     setPalette( mTodayPalette );
00404   }
00405   else {
00406     if ( mHoliday )
00407       setPalette( mHolidayPalette );
00408     else
00409       setPalette( mStandardPalette );
00410   }
00411 
00412   mItemList->clear();
00413 
00414   if ( !mHolidayString.isEmpty() ) {
00415     MonthViewItem *item = new MonthViewItem( 0, mDate, mHolidayString );
00416     item->setPalette( mHolidayPalette );
00417     mItemList->insertItem( item );
00418   }
00419 }
00420 
00421 void MonthViewCell::addIncidence( Incidence *incidence )
00422 {
00423   QString text;
00424   MonthViewItem *item = 0;
00425   if ( incidence->type() == "Event" ) {
00426     Event *event = static_cast<Event *>(incidence);
00427     if (event->isMultiDay()) {
00428       if (mDate == event->dtStart().date()) {
00429         text = "(-- " + event->summary();
00430       } else if (mDate == event->dtEnd().date()) {
00431         text = event->summary() + " --)";
00432       } else if (!(event->dtStart().date().daysTo(mDate) % 7)) {
00433         text = "-- " + event->summary() + " --";
00434       } else {
00435         text = "----------------";
00436       }
00437     } else {
00438       if (event->doesFloat())
00439         text = event->summary();
00440       else {
00441         text = KGlobal::locale()->formatTime(event->dtStart().time());
00442         text += " " + event->summary();
00443       }
00444     }
00445 
00446     item = new MonthViewItem( event, mDate, text );
00447     if (KOPrefs::instance()->monthViewUsesCategoryColor()) {
00448       QStringList categories = event->categories();
00449       QString cat = categories.first();
00450       if (cat.isEmpty()) {
00451         item->setPalette(QPalette(KOPrefs::instance()->mEventColor, KOPrefs::instance()->mEventColor));
00452       } else {
00453         item->setPalette(QPalette(*(KOPrefs::instance()->categoryColor(cat)), *(KOPrefs::instance()->categoryColor(cat))));
00454       }
00455     } else {
00456       item->setPalette( mStandardPalette );
00457     }
00458 
00459     Attendee *me = event->attendeeByMails( KOPrefs::instance()->allEmails() );
00460     if ( me != 0 ) {
00461       if ( me->status() == Attendee::NeedsAction && me->RSVP())
00462         item->setReply(true);
00463       else
00464         item->setReply(false);
00465     } else
00466       item->setReply(false);
00467   }
00468 
00469   if ( incidence->type() == "Todo" &&
00470        KOPrefs::instance()->showAllDayTodo() ) {
00471     Todo *todo = static_cast<Todo *>(incidence);
00472     if (todo->hasDueDate()) {
00473       if (!todo->doesFloat()) {
00474         text += KGlobal::locale()->formatTime(todo->dtDue().time());
00475         text += " ";
00476       }
00477     }
00478     text += todo->summary();
00479 
00480     item = new MonthViewItem( todo, mDate, text );
00481     if ( todo->doesRecur() ) {
00482       mDate < todo->dtDue().date() ?
00483       item->setTodoDone( true ) : item->setTodo( true );
00484     }
00485     else
00486       todo->isCompleted() ? item->setTodoDone( true ) : item->setTodo( true );
00487     item->setPalette( mStandardPalette );
00488   }
00489 
00490   if ( item ) {
00491     item->setAlarm( incidence->isAlarmEnabled() );
00492     item->setRecur( incidence->doesRecur() );
00493     mItemList->insertItem( item );
00494     QColor resourceColor = KOHelper::resourceColor( mCalendar, incidence );
00495     if ( !resourceColor.isValid() )
00496       resourceColor = KOPrefs::instance()->mEventColor;
00497     item->setResourceColor( resourceColor );
00498   }
00499 }
00500 
00501 bool MonthViewCell::removeIncidence( Incidence *incidence )
00502 {
00503   for ( uint i = 0; i < mItemList->count(); i++ ) {
00504     MonthViewItem *item = static_cast<MonthViewItem *>(mItemList->item( i ) );
00505     if ( item && item->incidence() &&
00506          item->incidence()->uid() == incidence->uid() ) {
00507       mItemList->removeItem( i );
00508       return true;
00509     }
00510   }
00511 
00512   return false;
00513 }
00514 
00515 void MonthViewCell::updateConfig()
00516 {
00517   setFont( KOPrefs::instance()->mMonthViewFont );
00518 
00519   QFontMetrics fm( font() );
00520   mLabelSize = fm.size( 0, "30" ) +
00521                QSize( mLabel->frameWidth() * 2, mLabel->frameWidth() * 2 ) +
00522                QSize( 2, 2 );
00523 //  mStandardPalette = mOriginalPalette;
00524   QColor bg = mStandardPalette.color( QPalette::Active, QColorGroup::Background );
00525   int h,s,v;
00526   bg.getHsv( &h, &s, &v );
00527   if ( date().month() %2 == 0 ) {
00528     if ( v < 128 ) {
00529       bg = bg.light( 125 );
00530     } else {
00531       bg = bg.dark( 125 );
00532     }
00533   }
00534   setPaletteBackgroundColor( bg );
00535 //  mStandardPalette.setColor( QColorGroup::Background, bg);*/
00536 
00537   mHolidayPalette = mStandardPalette;
00538   mHolidayPalette.setColor( QColorGroup::Foreground,
00539                             KOPrefs::instance()->holidayColor() );
00540   mHolidayPalette.setColor( QColorGroup::Text,
00541                             KOPrefs::instance()->holidayColor() );
00542   mTodayPalette = mStandardPalette;
00543   mTodayPalette.setColor( QColorGroup::Foreground,
00544                           KOPrefs::instance()->highlightColor() );
00545   mTodayPalette.setColor( QColorGroup::Text,
00546                           KOPrefs::instance()->highlightColor() );
00547   updateCell();
00548 
00549   mItemList->setBackground( mPrimary, KOCore::self()->isWorkDay( mDate ) );
00550 }
00551 
00552 void MonthViewCell::enableScrollBars( bool enabled )
00553 {
00554   if ( enabled ) {
00555     mItemList->setVScrollBarMode( QScrollView::Auto );
00556     mItemList->setHScrollBarMode( QScrollView::Auto );
00557   } else {
00558     mItemList->setVScrollBarMode( QScrollView::AlwaysOff );
00559     mItemList->setHScrollBarMode( QScrollView::AlwaysOff );
00560   }
00561 }
00562 
00563 Incidence *MonthViewCell::selectedIncidence()
00564 {
00565   int index = mItemList->currentItem();
00566   if ( index < 0 ) return 0;
00567 
00568   MonthViewItem *item =
00569       static_cast<MonthViewItem *>( mItemList->item( index ) );
00570 
00571   if ( !item ) return 0;
00572 
00573   return item->incidence();
00574 }
00575 
00576 QDate MonthViewCell::selectedIncidenceDate()
00577 {
00578   QDate qd;
00579   int index = mItemList->currentItem();
00580   if ( index < 0 ) return qd;
00581 
00582   MonthViewItem *item =
00583       static_cast<MonthViewItem *>( mItemList->item( index ) );
00584 
00585   if ( !item ) return qd;
00586 
00587   return item->itemDate();
00588 }
00589 
00590 void MonthViewCell::deselect()
00591 {
00592   mItemList->clearSelection();
00593 
00594   enableScrollBars( false );
00595 }
00596 
00597 void MonthViewCell::resizeEvent ( QResizeEvent * )
00598 {
00599   mLabel->move( width() - mLabel->width(), height() - mLabel->height() );
00600 }
00601 
00602 void MonthViewCell::defaultAction( QListBoxItem *item )
00603 {
00604   if ( !item ) {
00605     emit newEventSignal( date() );
00606   } else {
00607     MonthViewItem *eventItem = static_cast<MonthViewItem *>( item );
00608     Incidence *incidence = eventItem->incidence();
00609     if ( incidence ) mMonthView->defaultAction( incidence );
00610   }
00611 }
00612 
00613 void MonthViewCell::cellClicked( QListBoxItem * )
00614 {
00615   if( KOPrefs::instance()->enableMonthScroll() ) enableScrollBars( true );
00616 }
00617 
00618 void MonthViewCell::contextMenu( QListBoxItem *item )
00619 {
00620   mMonthView->setSelectedCell( this );
00621   if ( item ) {
00622     MonthViewItem *eventItem = static_cast<MonthViewItem *>( item );
00623     Incidence *incidence = eventItem->incidence();
00624     if ( incidence ) mMonthView->showEventContextMenu( incidence, date() );
00625   }
00626   else {
00627     mMonthView->showGeneralContextMenu();
00628   }
00629 }
00630 
00631 void MonthViewCell::selection( QListBoxItem *item )
00632 {
00633   if ( !item ) return;
00634 
00635   mMonthView->setSelectedCell( this );
00636 }
00637 
00638 KOMonthView::KOMonthView( Calendar *calendar, QWidget *parent, const char *name )
00639     : KOEventView( calendar, parent, name ),
00640       mDaysPerWeek( 7 ), mNumWeeks( 6 ), mNumCells( mDaysPerWeek * mNumWeeks ),
00641       mShortDayLabels( false ), mWidthLongDayLabel( 0 ), mSelectedCell( 0 )
00642 {
00643   mCells.setAutoDelete( true );
00644 
00645   QGridLayout *dayLayout = new QGridLayout( this );
00646 
00647   // create the day of the week labels (Sun, Mon, etc) and add them to
00648   // the layout.
00649   mDayLabels.resize( mDaysPerWeek );
00650   QFont bfont = font();
00651   bfont.setBold( true );
00652   int i;
00653   for( i = 0; i < mDaysPerWeek; i++ ) {
00654     QLabel *label = new QLabel( this );
00655     label->setFont( bfont );
00656     label->setFrameStyle( QFrame::Panel | QFrame::Raised );
00657     label->setLineWidth( 1 );
00658     label->setAlignment( AlignCenter );
00659 
00660     mDayLabels.insert( i, label );
00661 
00662     dayLayout->addWidget( label, 0, i );
00663     dayLayout->addColSpacing( i, 10 );
00664     dayLayout->setColStretch( i, 1 );
00665   }
00666 
00667   int row, col;
00668 
00669   mCells.resize( mNumCells );
00670   for( row = 0; row < mNumWeeks; ++row ) {
00671     for( col = 0; col < mDaysPerWeek; ++col ) {
00672       MonthViewCell *cell = new MonthViewCell( this );
00673       cell->setCalendar(calendar);
00674       mCells.insert( row * mDaysPerWeek + col, cell );
00675       dayLayout->addWidget( cell, row + 1, col );
00676 
00677       connect( cell, SIGNAL( defaultAction( Incidence * ) ),
00678                SLOT( defaultAction( Incidence * ) ) );
00679       connect( cell, SIGNAL( newEventSignal( QDate ) ),
00680                SIGNAL( newEventSignal( QDate ) ) );
00681     }
00682     dayLayout->setRowStretch( row + 1, 1 );
00683   }
00684 
00685   mEventContextMenu = eventPopup();
00686 
00687   updateConfig();
00688 
00689   emit incidenceSelected( 0 );
00690 }
00691 
00692 KOMonthView::~KOMonthView()
00693 {
00694   delete mEventContextMenu;
00695 }
00696 
00697 int KOMonthView::maxDatesHint()
00698 {
00699   return mNumCells;
00700 }
00701 
00702 int KOMonthView::currentDateCount()
00703 {
00704   return mNumCells;
00705 }
00706 
00707 Incidence::List KOMonthView::selectedIncidences()
00708 {
00709   Incidence::List selected;
00710 
00711   if ( mSelectedCell ) {
00712     Incidence *incidence = mSelectedCell->selectedIncidence();
00713     if ( incidence ) selected.append( incidence );
00714   }
00715 
00716   return selected;
00717 }
00718 
00719 DateList KOMonthView::selectedDates()
00720 {
00721   DateList selected;
00722 
00723   if ( mSelectedCell ) {
00724     QDate qd = mSelectedCell->selectedIncidenceDate();
00725     if ( qd.isValid() ) selected.append( qd );
00726   }
00727 
00728   return selected;
00729 }
00730 
00731 bool KOMonthView::eventDurationHint( QDateTime &startDt, QDateTime &endDt, bool &allDay )
00732 {
00733   if ( mSelectedCell ) {
00734     startDt.setDate( mSelectedCell->date() );
00735     endDt.setDate( mSelectedCell->date() );
00736     allDay = true;
00737     return true;
00738   }
00739   return false;
00740 }
00741 
00742 void KOMonthView::printPreview( CalPrinter *calPrinter, const QDate &fd,
00743                                 const QDate &td )
00744 {
00745 #ifndef KORG_NOPRINTER
00746   calPrinter->preview( CalPrinter::Month, fd, td );
00747 #endif
00748 }
00749 
00750 void KOMonthView::updateConfig()
00751 {
00752   mWeekStartDay = KGlobal::locale()->weekStartDay();
00753 
00754   QFontMetrics fontmetric( mDayLabels[0]->font() );
00755   mWidthLongDayLabel = 0;
00756 
00757   for (int i = 0; i < 7; i++) {
00758     int width =
00759         fontmetric.width( KOGlobals::self()->calendarSystem()->weekDayName( i + 1 ) );
00760     if ( width > mWidthLongDayLabel ) mWidthLongDayLabel = width;
00761   }
00762 
00763   updateDayLabels();
00764 
00765   for ( uint i = 0; i < mCells.count(); ++i ) {
00766     mCells[i]->updateConfig();
00767   }
00768 }
00769 
00770 void KOMonthView::updateDayLabels()
00771 {
00772   kdDebug(5850) << "KOMonthView::updateDayLabels()" << endl;
00773 
00774   const KCalendarSystem*calsys=KOGlobals::self()->calendarSystem();
00775   int currDay;
00776   for ( int i = 0; i < 7; i++ ) {
00777     currDay = i+mWeekStartDay;
00778     if ( currDay > 7 ) currDay -= 7;
00779     mDayLabels[i]->setText( calsys->weekDayName( currDay, mShortDayLabels ) );
00780   }
00781 }
00782 
00783 void KOMonthView::showDates( const QDate &start, const QDate & )
00784 {
00785 //  kdDebug(5850) << "KOMonthView::showDates(): " << start.toString() << endl;
00786 
00787   mStartDate = start;
00788 
00789   // correct begin of week
00790   int weekdayCol=( mStartDate.dayOfWeek() + 7 - mWeekStartDay ) % 7;
00791   mStartDate = mStartDate.addDays( -weekdayCol );
00792 
00793   bool primary = false;
00794   uint i;
00795   for( i = 0; i < mCells.size(); ++i ) {
00796     QDate date = mStartDate.addDays( i );
00797     if ( KOGlobals::self()->calendarSystem()->day( date ) == 1 ) {
00798       primary = !primary;
00799     }
00800     mCells[i]->setDate( date );
00801 
00802     mCells[i]->setPrimary( primary );
00803 
00804     if ( KOGlobals::self()->calendarSystem()->dayOfWeek( date ) ==
00805          KOGlobals::self()->calendarSystem()->weekDayOfPray() ) {
00806       mCells[i]->setHoliday( true );
00807     } else {
00808       mCells[i]->setHoliday( false );
00809     }
00810 
00811 #ifndef KORG_NOPLUGINS
00812     // add holiday, if present
00813     QString hstring( KOCore::self()->holiday( date ) );
00814     mCells[i]->setHoliday( hstring );
00815 #endif
00816   }
00817 
00818   updateView();
00819 }
00820 
00821 void KOMonthView::showIncidences( const Incidence::List & )
00822 {
00823   kdDebug(5850) << "KOMonthView::showIncidences( const Incidence::List & ) is not implemented yet." << endl;
00824 }
00825 
00826 void KOMonthView::changeIncidenceDisplayAdded( Incidence *incidence )
00827 {
00828   MonthViewCell *mvc;
00829   Event *event = 0;
00830   Todo *todo = 0;
00831   QDate date;
00832   if ( incidence->type() == "Event" ) {
00833     event = static_cast<Event *>( incidence );
00834     date = event->dtStart().date();
00835   }
00836   if ( incidence->type() == "Todo" ) {
00837     todo = static_cast<Todo *>( incidence );
00838     if ( !todo->hasDueDate() ) return;
00839     date = todo->dtDue().date();
00840   }
00841 
00842   if ( incidence->doesRecur() ) {
00843      for ( uint i = 0; i < mCells.count(); i++ ) {
00844        if ( incidence->recursOn( mCells[i]->date() ) ) {
00845          mCells[i]->addIncidence( incidence );
00846        }
00847      }
00848   } else if ( event ) {
00849       for ( QDateTime _date = date;
00850             _date <= event->dtEnd(); _date = _date.addDays( 1 ) ) {
00851         mvc = lookupCellByDate( _date.date() );
00852         if ( mvc ) mvc->addIncidence( event );
00853       }
00854     } else if ( todo ) {
00855         mvc = lookupCellByDate( date );
00856         if ( mvc ) mvc->addIncidence( todo );
00857       }
00858 }
00859 
00860 void KOMonthView::changeIncidenceDisplay( Incidence *incidence, int action )
00861 {
00862   switch ( action ) {
00863     case KOGlobals::INCIDENCEADDED:
00864       changeIncidenceDisplayAdded( incidence );
00865       break;
00866     case KOGlobals::INCIDENCEEDITED:
00867       for( uint i = 0; i < mCells.count(); i++ )
00868         mCells[i]->removeIncidence( incidence );
00869       changeIncidenceDisplayAdded( incidence );
00870       break;
00871     case KOGlobals::INCIDENCEDELETED:
00872       for( uint i = 0; i < mCells.count(); i++ )
00873         mCells[i]->removeIncidence( incidence );
00874       break;
00875     default:
00876       return;
00877   }
00878 }
00879 
00880 void KOMonthView::updateView()
00881 {
00882   for( uint i = 0; i < mCells.count(); ++i ) {
00883     mCells[i]->updateCell();
00884   }
00885 
00886   Incidence::List incidences = calendar()->incidences();
00887   Incidence::List::ConstIterator it;
00888 
00889   for ( it = incidences.begin(); it != incidences.end(); ++it )
00890     changeIncidenceDisplayAdded( *it );
00891 
00892   processSelectionChange();
00893 }
00894 
00895 void KOMonthView::resizeEvent( QResizeEvent * )
00896 {
00897   // select the appropriate heading string size. E.g. "Wednesday" or "Wed".
00898   // note this only changes the text if the requested size crosses the
00899   // threshold between big enough to support the full name and not big
00900   // enough.
00901   if( mDayLabels[0]->width() < mWidthLongDayLabel ) {
00902     if ( !mShortDayLabels ) {
00903       mShortDayLabels = true;
00904       updateDayLabels();
00905     }
00906   } else {
00907     if ( mShortDayLabels ) {
00908       mShortDayLabels = false;
00909       updateDayLabels();
00910     }
00911   }
00912 }
00913 
00914 void KOMonthView::showEventContextMenu( Incidence *incidence, QDate qd )
00915 {
00916   mEventContextMenu->showIncidencePopup( incidence, qd );
00917   /*
00918   if( incidence && incidence->type() == "Event" ) {
00919     Event *event = static_cast<Event *>(incidence);
00920     mContextMenu->showEventPopup(event);
00921   } else {
00922     kdDebug(5850) << "MonthView::showContextMenu(): cast failed." << endl;
00923   }
00924   */
00925 }
00926 
00927 void KOMonthView::showGeneralContextMenu()
00928 {
00929   showNewEventPopup();
00930 }
00931 
00932 void KOMonthView::setSelectedCell( MonthViewCell *cell )
00933 {
00934   if ( cell == mSelectedCell ) return;
00935 
00936   if ( mSelectedCell ) mSelectedCell->deselect();
00937 
00938   mSelectedCell = cell;
00939 
00940   if ( !mSelectedCell )
00941     emit incidenceSelected( 0 );
00942   else
00943     emit incidenceSelected( mSelectedCell->selectedIncidence() );
00944 }
00945 
00946 void KOMonthView::processSelectionChange()
00947 {
00948   Incidence::List incidences = selectedIncidences();
00949   if (incidences.count() > 0) {
00950     emit incidenceSelected( incidences.first() );
00951   } else {
00952     emit incidenceSelected( 0 );
00953   }
00954 }
00955 
00956 void KOMonthView::clearSelection()
00957 {
00958   if ( mSelectedCell ) {
00959     mSelectedCell->deselect();
00960     mSelectedCell = 0;
00961   }
00962 }
00963 
00964 MonthViewCell *KOMonthView::lookupCellByDate ( const QDate &date )
00965 {
00966   for( uint i = 0; i < mCells.count(); i++ ) {
00967     if ( mCells[i]->date() == date )
00968       return mCells[i];
00969   }
00970   return 0;
00971 }
KDE Logo
This file is part of the documentation for korganizer Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Oct 17 09:56:25 2007 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003