korganizer

multiagendaview.cpp

00001 /*
00002     Copyright (c) 2007 Volker Krause <vkrause@kde.org>
00003 
00004     This program is free software; you can redistribute it and/or modify
00005     it under the terms of the GNU General Public License as published by
00006     the Free Software Foundation; either version 2 of the License, or
00007     (at your option) any later version.
00008 
00009     This program is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00012     GNU General Public License for more details.
00013 
00014     You should have received a copy of the GNU General Public License
00015     along with this program; if not, write to the Free Software
00016     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017 */
00018 
00019 #include "multiagendaview.h"
00020 
00021 #include "koagendaview.h"
00022 #include "koagenda.h"
00023 #include "koprefs.h"
00024 #include "timelabels.h"
00025 
00026 #include <libkcal/calendarresources.h>
00027 
00028 #include <kglobalsettings.h>
00029 
00030 #include <qlayout.h>
00031 #include <qvbox.h>
00032 #include <qobjectlist.h>
00033 
00034 #define FOREACH_VIEW(av) \
00035 for(QValueList<KOAgendaView*>::ConstIterator it = mAgendaViews.constBegin(); \
00036   it != mAgendaViews.constEnd();) \
00037   for(KOAgendaView* av = (it != mAgendaViews.constEnd() ? (*it) : 0); \
00038       it != mAgendaViews.constEnd(); ++it, av = (*it)  )
00039 
00040 using namespace KOrg;
00041 
00042 MultiAgendaView::MultiAgendaView(Calendar * cal, QWidget * parent, const char *name ) :
00043     AgendaView( cal, parent, name ),
00044     mLastMovedSplitter( 0 ),
00045     mUpdateOnShow( false ),
00046     mPendingChanges( true )
00047 {
00048   QBoxLayout *topLevelLayout = new QHBoxLayout( this );
00049 
00050   QFontMetrics fm( font() );
00051   int topLabelHeight = 2 * fm.height();
00052 
00053   QVBox *topSideBox = new QVBox( this );
00054   QWidget *topSideSpacer = new QWidget( topSideBox );
00055   topSideSpacer->setFixedHeight( topLabelHeight );
00056   mLeftSplitter = new QSplitter( Qt::Vertical, topSideBox );
00057   mLeftSplitter->setOpaqueResize( KGlobalSettings::opaqueResize() );
00058   QLabel *label = new QLabel( i18n("All Day"), mLeftSplitter );
00059   label->setAlignment( Qt::AlignRight | Qt::AlignVCenter | Qt::WordBreak );
00060   QVBox *sideBox = new QVBox( mLeftSplitter );
00061   EventIndicator *eiSpacer = new EventIndicator( EventIndicator::Top, sideBox );
00062   eiSpacer->changeColumns( 0 );
00063   mTimeLabels = new TimeLabels( 24, sideBox );
00064   eiSpacer = new EventIndicator( EventIndicator::Bottom, sideBox );
00065   eiSpacer->changeColumns( 0 );
00066   mLeftBottomSpacer = new QWidget( topSideBox );
00067   topLevelLayout->addWidget( topSideBox );
00068 
00069   mScrollView = new QScrollView( this );
00070   mScrollView->setResizePolicy( QScrollView::Manual );
00071   mScrollView->setVScrollBarMode( QScrollView::AlwaysOff );
00072   mScrollView->setFrameShape( QFrame::NoFrame );
00073   topLevelLayout->addWidget( mScrollView, 100 );
00074   mTopBox = new QHBox( mScrollView->viewport() );
00075   mScrollView->addChild( mTopBox );
00076 
00077   topSideBox = new QVBox( this );
00078   topSideSpacer = new QWidget( topSideBox );
00079   topSideSpacer->setFixedHeight( topLabelHeight );
00080   mRightSplitter = new QSplitter( Qt::Vertical, topSideBox );
00081   mRightSplitter->setOpaqueResize( KGlobalSettings::opaqueResize() );
00082   new QWidget( mRightSplitter );
00083   sideBox = new QVBox( mRightSplitter );
00084   eiSpacer = new EventIndicator( EventIndicator::Top, sideBox );
00085   eiSpacer->setFixedHeight( eiSpacer->minimumHeight() );
00086   eiSpacer->changeColumns( 0 );
00087   mScrollBar = new QScrollBar( Qt::Vertical, sideBox );
00088   eiSpacer = new EventIndicator( EventIndicator::Bottom, sideBox );
00089   eiSpacer->setFixedHeight( eiSpacer->minimumHeight() );
00090   eiSpacer->changeColumns( 0 );
00091   mRightBottomSpacer = new QWidget( topSideBox );
00092   topLevelLayout->addWidget( topSideBox );
00093 
00094   recreateViews();
00095 }
00096 
00097 void MultiAgendaView::recreateViews()
00098 {
00099   if ( !mPendingChanges )
00100     return;
00101   mPendingChanges = false;
00102 
00103   deleteViews();
00104 
00105   CalendarResources *calres = dynamic_cast<CalendarResources*>( calendar() );
00106   if ( !calres ) {
00107     // fallback to single-agenda
00108     KOAgendaView* av = new KOAgendaView( calendar(), mTopBox );
00109     mAgendaViews.append( av );
00110     mAgendaWidgets.append( av );
00111     av->show();
00112   } else {
00113     CalendarResourceManager *manager = calres->resourceManager();
00114     for ( CalendarResourceManager::ActiveIterator it = manager->activeBegin(); it != manager->activeEnd(); ++it ) {
00115       if ( (*it)->canHaveSubresources() ) {
00116         QStringList subResources = (*it)->subresources();
00117         for ( QStringList::ConstIterator subit = subResources.constBegin(); subit != subResources.constEnd(); ++subit ) {
00118           QString type = (*it)->subresourceType( *subit );
00119           if ( !(*it)->subresourceActive( *subit ) || (!type.isEmpty() && type != "event") )
00120             continue;
00121           addView( (*it)->labelForSubresource( *subit ), *it, *subit );
00122         }
00123       } else {
00124         addView( (*it)->resourceName(), *it );
00125       }
00126     }
00127   }
00128 
00129   // no resources activated, so stop here to avoid crashing somewhere down the line, TODO: show a nice message instead
00130   if ( mAgendaViews.isEmpty() )
00131     return;
00132 
00133   setupViews();
00134   QTimer::singleShot( 0, this, SLOT(slotResizeScrollView()) );
00135   mTimeLabels->updateConfig();
00136 
00137   QScrollBar *scrollBar = mAgendaViews.first()->agenda()->verticalScrollBar();
00138   mScrollBar->setMinValue( scrollBar->minValue() );
00139   mScrollBar->setMaxValue( scrollBar->maxValue() );
00140   mScrollBar->setLineStep( scrollBar->lineStep() );
00141   mScrollBar->setPageStep( scrollBar->pageStep() );
00142   mScrollBar->setValue( scrollBar->value() );
00143   connect( mTimeLabels->verticalScrollBar(), SIGNAL(valueChanged(int)),
00144            mScrollBar, SLOT(setValue(int)) );
00145   connect( mScrollBar, SIGNAL(valueChanged(int)),
00146            mTimeLabels, SLOT(positionChanged(int)) );
00147 
00148   installSplitterEventFilter( mLeftSplitter );
00149   installSplitterEventFilter( mRightSplitter );
00150   resizeSplitters();
00151   mTimeLabels->positionChanged();
00152 }
00153 
00154 void MultiAgendaView::deleteViews()
00155 {
00156   for ( QValueList<QWidget*>::ConstIterator it = mAgendaWidgets.constBegin();
00157         it != mAgendaWidgets.constEnd(); ++it ) {
00158     delete *it;
00159   }
00160   mAgendaViews.clear();
00161   mAgendaWidgets.clear();
00162   mLastMovedSplitter = 0;
00163 }
00164 
00165 void MultiAgendaView::setupViews()
00166 {
00167   FOREACH_VIEW( agenda ) {
00168     connect( agenda, SIGNAL( newEventSignal() ),
00169              SIGNAL( newEventSignal() ) );
00170     connect( agenda, SIGNAL( editIncidenceSignal( Incidence * ) ),
00171              SIGNAL( editIncidenceSignal( Incidence * ) ) );
00172     connect( agenda, SIGNAL( showIncidenceSignal( Incidence * ) ),
00173              SIGNAL( showIncidenceSignal( Incidence * ) ) );
00174     connect( agenda, SIGNAL( deleteIncidenceSignal( Incidence * ) ),
00175              SIGNAL( deleteIncidenceSignal( Incidence * ) ) );
00176     connect( agenda, SIGNAL( startMultiModify( const QString & ) ),
00177              SIGNAL( startMultiModify( const QString & ) ) );
00178     connect( agenda, SIGNAL( endMultiModify() ),
00179              SIGNAL( endMultiModify() ) );
00180 
00181     connect( agenda, SIGNAL( incidenceSelected( Incidence * ) ),
00182              SIGNAL( incidenceSelected( Incidence * ) ) );
00183 
00184     connect( agenda, SIGNAL(cutIncidenceSignal(Incidence*)),
00185              SIGNAL(cutIncidenceSignal(Incidence*)) );
00186     connect( agenda, SIGNAL(copyIncidenceSignal(Incidence*)),
00187              SIGNAL(copyIncidenceSignal(Incidence*)) );
00188     connect( agenda, SIGNAL(pasteIncidenceSignal()),
00189              SIGNAL(pasteIncidenceSignal()) );
00190     connect( agenda, SIGNAL(toggleAlarmSignal(Incidence*)),
00191              SIGNAL(toggleAlarmSignal(Incidence*)) );
00192     connect( agenda, SIGNAL(dissociateOccurrenceSignal(Incidence*, const QDate&)),
00193              SIGNAL(dissociateOccurrenceSignal(Incidence*, const QDate&)) );
00194     connect( agenda, SIGNAL(dissociateFutureOccurrenceSignal(Incidence*, const QDate&)),
00195              SIGNAL(dissociateFutureOccurrenceSignal(Incidence*, const QDate&)) );
00196 
00197     connect( agenda, SIGNAL(newEventSignal(const QDate&)),
00198              SIGNAL(newEventSignal(const QDate&)) );
00199     connect( agenda, SIGNAL(newEventSignal(const QDateTime&)),
00200              SIGNAL(newEventSignal(const QDateTime&)) );
00201     connect( agenda, SIGNAL(newEventSignal(const QDateTime&, const QDateTime&)),
00202              SIGNAL(newEventSignal(const QDateTime&, const QDateTime&)) );
00203     connect( agenda, SIGNAL(newTodoSignal(const QDate&)),
00204              SIGNAL(newTodoSignal(const QDate&)) );
00205 
00206     connect( agenda, SIGNAL(incidenceSelected(Incidence*)),
00207              SLOT(slotSelectionChanged()) );
00208 
00209     connect( agenda, SIGNAL(timeSpanSelectionChanged()),
00210              SLOT(slotClearTimeSpanSelection()) );
00211 
00212     disconnect( agenda->agenda(), SIGNAL(zoomView(const int,const QPoint&,const Qt::Orientation)), agenda, 0 );
00213     connect( agenda->agenda(), SIGNAL(zoomView(const int,const QPoint&,const Qt::Orientation)),
00214              SLOT(zoomView(const int,const QPoint&,const Qt::Orientation)) );
00215   }
00216 
00217   FOREACH_VIEW( agenda ) {
00218     agenda->readSettings();
00219   }
00220 
00221   int minWidth = 0;
00222   for ( QValueList<QWidget*>::ConstIterator it = mAgendaWidgets.constBegin(); it != mAgendaWidgets.constEnd(); ++it )
00223     minWidth = QMAX( minWidth, (*it)->minimumSizeHint().width() );
00224   for ( QValueList<QWidget*>::ConstIterator it = mAgendaWidgets.constBegin(); it != mAgendaWidgets.constEnd(); ++it )
00225     (*it)->setMinimumWidth( minWidth );
00226 }
00227 
00228 MultiAgendaView::~ MultiAgendaView()
00229 {
00230 }
00231 
00232 Incidence::List MultiAgendaView::selectedIncidences()
00233 {
00234   Incidence::List list;
00235   FOREACH_VIEW(agendaView) {
00236     list += agendaView->selectedIncidences();
00237   }
00238   return list;
00239 }
00240 
00241 DateList MultiAgendaView::selectedDates()
00242 {
00243   DateList list;
00244   FOREACH_VIEW(agendaView) {
00245     list += agendaView->selectedDates();
00246   }
00247   return list;
00248 }
00249 
00250 int MultiAgendaView::currentDateCount()
00251 {
00252   FOREACH_VIEW( agendaView )
00253     return agendaView->currentDateCount();
00254   return 0;
00255 }
00256 
00257 void MultiAgendaView::showDates(const QDate & start, const QDate & end)
00258 {
00259   mStartDate = start;
00260   mEndDate = end;
00261   recreateViews();
00262   FOREACH_VIEW( agendaView )
00263     agendaView->showDates( start, end );
00264 }
00265 
00266 void MultiAgendaView::showIncidences(const Incidence::List & incidenceList)
00267 {
00268   FOREACH_VIEW( agendaView )
00269     agendaView->showIncidences( incidenceList );
00270 }
00271 
00272 void MultiAgendaView::updateView()
00273 {
00274   recreateViews();
00275   FOREACH_VIEW( agendaView )
00276     agendaView->updateView();
00277 }
00278 
00279 void MultiAgendaView::changeIncidenceDisplay(Incidence * incidence, int mode)
00280 {
00281   FOREACH_VIEW( agendaView )
00282     agendaView->changeIncidenceDisplay( incidence, mode );
00283 }
00284 
00285 int MultiAgendaView::maxDatesHint()
00286 {
00287   FOREACH_VIEW( agendaView )
00288     return agendaView->maxDatesHint();
00289   return 0;
00290 }
00291 
00292 void MultiAgendaView::slotSelectionChanged()
00293 {
00294   FOREACH_VIEW( agenda ) {
00295     if ( agenda != sender() )
00296       agenda->clearSelection();
00297   }
00298 }
00299 
00300 bool MultiAgendaView::eventDurationHint(QDateTime & startDt, QDateTime & endDt, bool & allDay)
00301 {
00302   FOREACH_VIEW( agenda ) {
00303     bool valid = agenda->eventDurationHint( startDt, endDt, allDay );
00304     if ( valid )
00305       return true;
00306   }
00307   return false;
00308 }
00309 
00310 void MultiAgendaView::slotClearTimeSpanSelection()
00311 {
00312   FOREACH_VIEW( agenda ) {
00313     if ( agenda != sender() )
00314       agenda->clearTimeSpanSelection();
00315   }
00316 }
00317 
00318 void MultiAgendaView::setTypeAheadReceiver(QObject * o)
00319 {
00320   FOREACH_VIEW( agenda )
00321     agenda->setTypeAheadReceiver( o );
00322 }
00323 
00324 void MultiAgendaView::finishTypeAhead()
00325 {
00326   FOREACH_VIEW( agenda )
00327     agenda->finishTypeAhead();
00328 }
00329 
00330 void MultiAgendaView::addView( const QString &label, KCal::ResourceCalendar * res, const QString & subRes )
00331 {
00332   QVBox *box = new QVBox( mTopBox );
00333   QLabel *l = new QLabel( label, box );
00334   l->setAlignment( AlignVCenter | AlignHCenter );
00335   KOAgendaView* av = new KOAgendaView( calendar(), box, 0, true );
00336   av->setResource( res, subRes );
00337   av->setIncidenceChanger( mChanger );
00338   av->agenda()->setVScrollBarMode( QScrollView::AlwaysOff );
00339   mAgendaViews.append( av );
00340   mAgendaWidgets.append( box );
00341   box->show();
00342   mTimeLabels->setAgenda( av->agenda() );
00343 
00344   connect( av->agenda()->verticalScrollBar(), SIGNAL(valueChanged(int)),
00345            mTimeLabels, SLOT(positionChanged(int)) );
00346   connect( mTimeLabels->verticalScrollBar(), SIGNAL(valueChanged(int)),
00347            av, SLOT(setContentsPos(int)) );
00348 
00349   installSplitterEventFilter( av->splitter() );
00350 }
00351 
00352 void MultiAgendaView::resizeEvent(QResizeEvent * ev)
00353 {
00354   resizeScrollView( ev->size() );
00355   AgendaView::resizeEvent( ev );
00356 }
00357 
00358 void MultiAgendaView::resizeScrollView(const QSize & size)
00359 {
00360   const int widgetWidth = size.width() - mTimeLabels->width() - mScrollBar->width();
00361   int width = QMAX( mTopBox->sizeHint().width(), widgetWidth );
00362   int height = size.height();
00363   if ( width > widgetWidth ) {
00364     const int sbHeight = mScrollView->horizontalScrollBar()->height();
00365     height -= sbHeight;
00366     mLeftBottomSpacer->setFixedHeight( sbHeight );
00367     mRightBottomSpacer->setFixedHeight( sbHeight );
00368   } else {
00369     mLeftBottomSpacer->setFixedHeight( 0 );
00370     mRightBottomSpacer->setFixedHeight( 0 );
00371   }
00372   mScrollView->resizeContents( width, height );
00373   mTopBox->resize( width, height );
00374 }
00375 
00376 void MultiAgendaView::setIncidenceChanger(IncidenceChangerBase * changer)
00377 {
00378   AgendaView::setIncidenceChanger( changer );
00379   FOREACH_VIEW( agenda )
00380     agenda->setIncidenceChanger( changer );
00381 }
00382 
00383 void MultiAgendaView::updateConfig()
00384 {
00385   AgendaView::updateConfig();
00386   mTimeLabels->updateConfig();
00387   FOREACH_VIEW( agenda )
00388     agenda->updateConfig();
00389 }
00390 
00391 // KDE4: not needed anymore, QSplitter has a moved signal there
00392 bool MultiAgendaView::eventFilter(QObject * obj, QEvent * event)
00393 {
00394   if ( obj->className() == QCString("QSplitterHandle") ) {
00395     if ( (event->type() == QEvent::MouseMove && KGlobalSettings::opaqueResize())
00396            || event->type() == QEvent::MouseButtonRelease ) {
00397       FOREACH_VIEW( agenda ) {
00398         if ( agenda->splitter() == obj->parent() )
00399           mLastMovedSplitter = agenda->splitter();
00400       }
00401       if ( mLeftSplitter == obj->parent() )
00402         mLastMovedSplitter = mLeftSplitter;
00403       else if ( mRightSplitter == obj->parent() )
00404         mLastMovedSplitter = mRightSplitter;
00405       QTimer::singleShot( 0, this, SLOT(resizeSplitters()) );
00406     }
00407   }
00408   return AgendaView::eventFilter( obj, event );
00409 }
00410 
00411 void MultiAgendaView::resizeSplitters()
00412 {
00413   if ( !mLastMovedSplitter )
00414     mLastMovedSplitter = mAgendaViews.first()->splitter();
00415   FOREACH_VIEW( agenda ) {
00416     if ( agenda->splitter() == mLastMovedSplitter )
00417       continue;
00418     agenda->splitter()->setSizes( mLastMovedSplitter->sizes() );
00419   }
00420   if ( mLastMovedSplitter != mLeftSplitter )
00421     mLeftSplitter->setSizes( mLastMovedSplitter->sizes() );
00422   if ( mLastMovedSplitter != mRightSplitter )
00423     mRightSplitter->setSizes( mLastMovedSplitter->sizes() );
00424 }
00425 
00426 void MultiAgendaView::zoomView( const int delta, const QPoint & pos, const Qt::Orientation ori )
00427 {
00428   if ( ori == Qt::Vertical ) {
00429     if ( delta > 0 ) {
00430       if ( KOPrefs::instance()->mHourSize > 4 )
00431         KOPrefs::instance()->mHourSize--;
00432     } else {
00433       KOPrefs::instance()->mHourSize++;
00434     }
00435   }
00436 
00437   FOREACH_VIEW( agenda )
00438     agenda->zoomView( delta, pos, ori );
00439 
00440   mTimeLabels->updateConfig();
00441   mTimeLabels->positionChanged();
00442   mTimeLabels->repaint();
00443 }
00444 
00445 // KDE4: not needed, use existing QSplitter signals instead
00446 void MultiAgendaView::installSplitterEventFilter(QSplitter * splitter)
00447 {
00448   QObjectList *objlist = splitter->queryList( "QSplitterHandle" );
00449   // HACK: when not being visible, the splitter handle is sometimes not found
00450   // for unknown reasons, so trigger an update when we are shown again
00451   if ( objlist->count() == 0 && !isVisible() )
00452     mUpdateOnShow = true;
00453   QObjectListIt it( *objlist );
00454   QObject *obj;
00455   while ( (obj = it.current()) != 0 ) {
00456     obj->removeEventFilter( this );
00457     obj->installEventFilter( this );
00458     ++it;
00459   }
00460   delete objlist;
00461 }
00462 
00463 void MultiAgendaView::slotResizeScrollView()
00464 {
00465   resizeScrollView( size() );
00466 }
00467 
00468 void MultiAgendaView::show()
00469 {
00470   AgendaView::show();
00471   if ( mUpdateOnShow ) {
00472     mUpdateOnShow = false;
00473     mPendingChanges = true; // force a full view recreation
00474     showDates( mStartDate, mEndDate );
00475   }
00476 }
00477 
00478 void MultiAgendaView::resourcesChanged()
00479 {
00480   mPendingChanges = true;
00481   FOREACH_VIEW( agenda )
00482     agenda->resourcesChanged();
00483 }
00484 
00485 #include "multiagendaview.moc"