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 }
00152 
00153 void MultiAgendaView::deleteViews()
00154 {
00155   for ( QValueList<QWidget*>::ConstIterator it = mAgendaWidgets.constBegin();
00156         it != mAgendaWidgets.constEnd(); ++it ) {
00157     delete *it;
00158   }
00159   mAgendaViews.clear();
00160   mAgendaWidgets.clear();
00161   mLastMovedSplitter = 0;
00162 }
00163 
00164 void MultiAgendaView::setupViews()
00165 {
00166   FOREACH_VIEW( agenda ) {
00167     connect( agenda, SIGNAL( newEventSignal() ),
00168              SIGNAL( newEventSignal() ) );
00169     connect( agenda, SIGNAL( editIncidenceSignal( Incidence * ) ),
00170              SIGNAL( editIncidenceSignal( Incidence * ) ) );
00171     connect( agenda, SIGNAL( showIncidenceSignal( Incidence * ) ),
00172              SIGNAL( showIncidenceSignal( Incidence * ) ) );
00173     connect( agenda, SIGNAL( deleteIncidenceSignal( Incidence * ) ),
00174              SIGNAL( deleteIncidenceSignal( Incidence * ) ) );
00175     connect( agenda, SIGNAL( startMultiModify( const QString & ) ),
00176              SIGNAL( startMultiModify( const QString & ) ) );
00177     connect( agenda, SIGNAL( endMultiModify() ),
00178              SIGNAL( endMultiModify() ) );
00179 
00180     connect( agenda, SIGNAL( incidenceSelected( Incidence * ) ),
00181              SIGNAL( incidenceSelected( Incidence * ) ) );
00182 
00183     connect( agenda, SIGNAL(cutIncidenceSignal(Incidence*)),
00184              SIGNAL(cutIncidenceSignal(Incidence*)) );
00185     connect( agenda, SIGNAL(copyIncidenceSignal(Incidence*)),
00186              SIGNAL(copyIncidenceSignal(Incidence*)) );
00187     connect( agenda, SIGNAL(pasteIncidenceSignal()),
00188              SIGNAL(pasteIncidenceSignal()) );
00189     connect( agenda, SIGNAL(toggleAlarmSignal(Incidence*)),
00190              SIGNAL(toggleAlarmSignal(Incidence*)) );
00191     connect( agenda, SIGNAL(dissociateOccurrenceSignal(Incidence*, const QDate&)),
00192              SIGNAL(dissociateOccurrenceSignal(Incidence*, const QDate&)) );
00193     connect( agenda, SIGNAL(dissociateFutureOccurrenceSignal(Incidence*, const QDate&)),
00194              SIGNAL(dissociateFutureOccurrenceSignal(Incidence*, const QDate&)) );
00195 
00196     connect( agenda, SIGNAL(newEventSignal(const QDate&)),
00197              SIGNAL(newEventSignal(const QDate&)) );
00198     connect( agenda, SIGNAL(newEventSignal(const QDateTime&)),
00199              SIGNAL(newEventSignal(const QDateTime&)) );
00200     connect( agenda, SIGNAL(newEventSignal(const QDateTime&, const QDateTime&)),
00201              SIGNAL(newEventSignal(const QDateTime&, const QDateTime&)) );
00202     connect( agenda, SIGNAL(newTodoSignal(const QDate&)),
00203              SIGNAL(newTodoSignal(const QDate&)) );
00204 
00205     connect( agenda, SIGNAL(incidenceSelected(Incidence*)),
00206              SLOT(slotSelectionChanged()) );
00207 
00208     connect( agenda, SIGNAL(timeSpanSelectionChanged()),
00209              SLOT(slotClearTimeSpanSelection()) );
00210 
00211     disconnect( agenda->agenda(), SIGNAL(zoomView(const int,const QPoint&,const Qt::Orientation)), agenda, 0 );
00212     connect( agenda->agenda(), SIGNAL(zoomView(const int,const QPoint&,const Qt::Orientation)),
00213              SLOT(zoomView(const int,const QPoint&,const Qt::Orientation)) );
00214   }
00215 
00216   FOREACH_VIEW( agenda ) {
00217     agenda->readSettings();
00218   }
00219 
00220   int minWidth = 0;
00221   for ( QValueList<QWidget*>::ConstIterator it = mAgendaWidgets.constBegin(); it != mAgendaWidgets.constEnd(); ++it )
00222     minWidth = QMAX( minWidth, (*it)->minimumSizeHint().width() );
00223   for ( QValueList<QWidget*>::ConstIterator it = mAgendaWidgets.constBegin(); it != mAgendaWidgets.constEnd(); ++it )
00224     (*it)->setMinimumWidth( minWidth );
00225 }
00226 
00227 MultiAgendaView::~ MultiAgendaView()
00228 {
00229 }
00230 
00231 Incidence::List MultiAgendaView::selectedIncidences()
00232 {
00233   Incidence::List list;
00234   FOREACH_VIEW(agendaView) {
00235     list += agendaView->selectedIncidences();
00236   }
00237   return list;
00238 }
00239 
00240 DateList MultiAgendaView::selectedDates()
00241 {
00242   DateList list;
00243   FOREACH_VIEW(agendaView) {
00244     list += agendaView->selectedDates();
00245   }
00246   return list;
00247 }
00248 
00249 int MultiAgendaView::currentDateCount()
00250 {
00251   FOREACH_VIEW( agendaView )
00252     return agendaView->currentDateCount();
00253   return 0;
00254 }
00255 
00256 void MultiAgendaView::showDates(const QDate & start, const QDate & end)
00257 {
00258   mStartDate = start;
00259   mEndDate = end;
00260   recreateViews();
00261   FOREACH_VIEW( agendaView )
00262     agendaView->showDates( start, end );
00263 }
00264 
00265 void MultiAgendaView::showIncidences(const Incidence::List & incidenceList)
00266 {
00267   FOREACH_VIEW( agendaView )
00268     agendaView->showIncidences( incidenceList );
00269 }
00270 
00271 void MultiAgendaView::updateView()
00272 {
00273   recreateViews();
00274   FOREACH_VIEW( agendaView )
00275     agendaView->updateView();
00276 }
00277 
00278 void MultiAgendaView::changeIncidenceDisplay(Incidence * incidence, int mode)
00279 {
00280   FOREACH_VIEW( agendaView )
00281     agendaView->changeIncidenceDisplay( incidence, mode );
00282 }
00283 
00284 int MultiAgendaView::maxDatesHint()
00285 {
00286   FOREACH_VIEW( agendaView )
00287     return agendaView->maxDatesHint();
00288   return 0;
00289 }
00290 
00291 void MultiAgendaView::slotSelectionChanged()
00292 {
00293   FOREACH_VIEW( agenda ) {
00294     if ( agenda != sender() )
00295       agenda->clearSelection();
00296   }
00297 }
00298 
00299 bool MultiAgendaView::eventDurationHint(QDateTime & startDt, QDateTime & endDt, bool & allDay)
00300 {
00301   FOREACH_VIEW( agenda ) {
00302     bool valid = agenda->eventDurationHint( startDt, endDt, allDay );
00303     if ( valid )
00304       return true;
00305   }
00306   return false;
00307 }
00308 
00309 void MultiAgendaView::slotClearTimeSpanSelection()
00310 {
00311   FOREACH_VIEW( agenda ) {
00312     if ( agenda != sender() )
00313       agenda->clearTimeSpanSelection();
00314   }
00315 }
00316 
00317 void MultiAgendaView::setTypeAheadReceiver(QObject * o)
00318 {
00319   FOREACH_VIEW( agenda )
00320     agenda->setTypeAheadReceiver( o );
00321 }
00322 
00323 void MultiAgendaView::finishTypeAhead()
00324 {
00325   FOREACH_VIEW( agenda )
00326     agenda->finishTypeAhead();
00327 }
00328 
00329 void MultiAgendaView::addView( const QString &label, KCal::ResourceCalendar * res, const QString & subRes )
00330 {
00331   QVBox *box = new QVBox( mTopBox );
00332   QLabel *l = new QLabel( label, box );
00333   l->setAlignment( AlignVCenter | AlignHCenter );
00334   KOAgendaView* av = new KOAgendaView( calendar(), box, 0, true );
00335   av->setResource( res, subRes );
00336   av->setIncidenceChanger( mChanger );
00337   av->agenda()->setVScrollBarMode( QScrollView::AlwaysOff );
00338   mAgendaViews.append( av );
00339   mAgendaWidgets.append( box );
00340   box->show();
00341   mTimeLabels->setAgenda( av->agenda() );
00342 
00343   connect( av->agenda()->verticalScrollBar(), SIGNAL(valueChanged(int)),
00344            mTimeLabels, SLOT(positionChanged(int)) );
00345   connect( mTimeLabels->verticalScrollBar(), SIGNAL(valueChanged(int)),
00346            av, SLOT(setContentsPos(int)) );
00347 
00348   installSplitterEventFilter( av->splitter() );
00349 }
00350 
00351 void MultiAgendaView::resizeEvent(QResizeEvent * ev)
00352 {
00353   resizeScrollView( ev->size() );
00354   AgendaView::resizeEvent( ev );
00355 }
00356 
00357 void MultiAgendaView::resizeScrollView(const QSize & size)
00358 {
00359   const int widgetWidth = size.width() - mTimeLabels->width() - mScrollBar->width();
00360   int width = QMAX( mTopBox->sizeHint().width(), widgetWidth );
00361   int height = size.height();
00362   if ( width > widgetWidth ) {
00363     const int sbHeight = mScrollView->horizontalScrollBar()->height();
00364     height -= sbHeight;
00365     mLeftBottomSpacer->setFixedHeight( sbHeight );
00366     mRightBottomSpacer->setFixedHeight( sbHeight );
00367   } else {
00368     mLeftBottomSpacer->setFixedHeight( 0 );
00369     mRightBottomSpacer->setFixedHeight( 0 );
00370   }
00371   mScrollView->resizeContents( width, height );
00372   mTopBox->resize( width, height );
00373 }
00374 
00375 void MultiAgendaView::setIncidenceChanger(IncidenceChangerBase * changer)
00376 {
00377   AgendaView::setIncidenceChanger( changer );
00378   FOREACH_VIEW( agenda )
00379     agenda->setIncidenceChanger( changer );
00380 }
00381 
00382 void MultiAgendaView::updateConfig()
00383 {
00384   AgendaView::updateConfig();
00385   mTimeLabels->updateConfig();
00386   FOREACH_VIEW( agenda )
00387     agenda->updateConfig();
00388 }
00389 
00390 // KDE4: not needed anymore, QSplitter has a moved signal there
00391 bool MultiAgendaView::eventFilter(QObject * obj, QEvent * event)
00392 {
00393   if ( obj->className() == QCString("QSplitterHandle") ) {
00394     if ( (event->type() == QEvent::MouseMove && KGlobalSettings::opaqueResize())
00395            || event->type() == QEvent::MouseButtonRelease ) {
00396       FOREACH_VIEW( agenda ) {
00397         if ( agenda->splitter() == obj->parent() )
00398           mLastMovedSplitter = agenda->splitter();
00399       }
00400       if ( mLeftSplitter == obj->parent() )
00401         mLastMovedSplitter = mLeftSplitter;
00402       else if ( mRightSplitter == obj->parent() )
00403         mLastMovedSplitter = mRightSplitter;
00404       QTimer::singleShot( 0, this, SLOT(resizeSplitters()) );
00405     }
00406   }
00407   return AgendaView::eventFilter( obj, event );
00408 }
00409 
00410 void MultiAgendaView::resizeSplitters()
00411 {
00412   if ( !mLastMovedSplitter )
00413     mLastMovedSplitter = mAgendaViews.first()->splitter();
00414   FOREACH_VIEW( agenda ) {
00415     if ( agenda->splitter() == mLastMovedSplitter )
00416       continue;
00417     agenda->splitter()->setSizes( mLastMovedSplitter->sizes() );
00418   }
00419   if ( mLastMovedSplitter != mLeftSplitter )
00420     mLeftSplitter->setSizes( mLastMovedSplitter->sizes() );
00421   if ( mLastMovedSplitter != mRightSplitter )
00422     mRightSplitter->setSizes( mLastMovedSplitter->sizes() );
00423 }
00424 
00425 void MultiAgendaView::zoomView( const int delta, const QPoint & pos, const Qt::Orientation ori )
00426 {
00427   if ( ori == Qt::Vertical ) {
00428     if ( delta > 0 ) {
00429       if ( KOPrefs::instance()->mHourSize > 4 )
00430         KOPrefs::instance()->mHourSize--;
00431     } else {
00432       KOPrefs::instance()->mHourSize++;
00433     }
00434   }
00435 
00436   FOREACH_VIEW( agenda )
00437     agenda->zoomView( delta, pos, ori );
00438 
00439   mTimeLabels->updateConfig();
00440   mTimeLabels->positionChanged();
00441   mTimeLabels->repaint();
00442 }
00443 
00444 // KDE4: not needed, use existing QSplitter signals instead
00445 void MultiAgendaView::installSplitterEventFilter(QSplitter * splitter)
00446 {
00447   QObjectList *objlist = splitter->queryList( "QSplitterHandle" );
00448   // HACK: when not being visible, the splitter handle is sometimes not found
00449   // for unknown reasons, so trigger an update when we are shown again
00450   if ( objlist->count() == 0 && !isVisible() )
00451     mUpdateOnShow = true;
00452   QObjectListIt it( *objlist );
00453   QObject *obj;
00454   while ( (obj = it.current()) != 0 ) {
00455     obj->removeEventFilter( this );
00456     obj->installEventFilter( this );
00457     ++it;
00458   }
00459   delete objlist;
00460 }
00461 
00462 void MultiAgendaView::slotResizeScrollView()
00463 {
00464   resizeScrollView( size() );
00465 }
00466 
00467 void MultiAgendaView::show()
00468 {
00469   AgendaView::show();
00470   if ( mUpdateOnShow ) {
00471     mUpdateOnShow = false;
00472     mPendingChanges = true; // force a full view recreation
00473     showDates( mStartDate, mEndDate );
00474   }
00475 }
00476 
00477 void MultiAgendaView::resourcesChanged()
00478 {
00479   mPendingChanges = true;
00480   FOREACH_VIEW( agenda )
00481     agenda->resourcesChanged();
00482 }
00483 
00484 #include "multiagendaview.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys