korganizer

koviewmanager.cpp

00001 /*
00002     This file is part of KOrganizer.
00003 
00004     Copyright (c) 2001,2003 Cornelius Schumacher <schumacher@kde.org>
00005     Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
00006 
00007     This program is free software; you can redistribute it and/or modify
00008     it under the terms of the GNU General Public License as published by
00009     the Free Software Foundation; either version 2 of the License, or
00010     (at your option) any later version.
00011 
00012     This program is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00015     GNU General Public License for more details.
00016 
00017     You should have received a copy of the GNU General Public License
00018     along with this program; if not, write to the Free Software
00019     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020 
00021     As a special exception, permission is given to link this program
00022     with any edition of Qt, and distribute the resulting executable,
00023     without including the source code for Qt in the source distribution.
00024 */
00025 
00026 #include <qwidgetstack.h>
00027 #include <qtabwidget.h>
00028 
00029 #include <kactioncollection.h>
00030 #include <kconfig.h>
00031 #include <kglobal.h>
00032 
00033 #include "actionmanager.h"
00034 #include "calendarview.h"
00035 #include "datenavigator.h"
00036 #include "kotodoview.h"
00037 #include "koagendaview.h"
00038 #include "komonthview.h"
00039 #include "kolistview.h"
00040 #include "kowhatsnextview.h"
00041 #include "kojournalview.h"
00042 #include "kotimelineview.h"
00043 #include "koprefs.h"
00044 #include "koglobals.h"
00045 #include "navigatorbar.h"
00046 #include "multiagendaview.h"
00047 #include <korganizer/mainwindow.h>
00048 
00049 #include "koviewmanager.h"
00050 #include "koviewmanager.moc"
00051 
00052 KOViewManager::KOViewManager( CalendarView *mainView ) :
00053   QObject(), mMainView( mainView )
00054 {
00055   mCurrentView = 0;
00056 
00057   mLastEventView = 0;
00058 
00059   mWhatsNextView = 0;
00060   mTodoView = 0;
00061   mAgendaView = 0;
00062   mAgendaSideBySideView = 0;
00063   mMonthView = 0;
00064   mListView = 0;
00065   mJournalView = 0;
00066   mTimelineView = 0;
00067   mAgendaViewTabs = 0;
00068   mAgendaMode = AGENDA_NONE;
00069 }
00070 
00071 KOViewManager::~KOViewManager()
00072 {
00073 }
00074 
00075 
00076 KOrg::BaseView *KOViewManager::currentView()
00077 {
00078   return mCurrentView;
00079 }
00080 
00081 void KOViewManager::readSettings(KConfig *config)
00082 {
00083   config->setGroup("General");
00084   QString view = config->readEntry("Current View");
00085 
00086   if (view == "WhatsNext") showWhatsNextView();
00087   else if (view == "Month") showMonthView();
00088   else if (view == "List") showListView();
00089   else if (view == "Journal") showJournalView();
00090   else if (view == "Todo") showTodoView();
00091   else if (view == "Timeline") showTimelineView();
00092   else {
00093     mAgendaMode = AgendaMode( config->readNumEntry( "Agenda Mode", AGENDA_OTHER ) );
00094 
00095     switch ( mAgendaMode ) {
00096       case AGENDA_WORK_WEEK:
00097         showWorkWeekView();
00098         break;
00099       case AGENDA_WEEK:
00100         showWeekView();
00101         break;
00102       case AGENDA_NEXTX:
00103         showNextXView();
00104         break;
00105       case AGENDA_DAY:
00106         showDayView();
00107         break;
00108       case AGENDA_NONE:
00109         // Someone has been playing with the config file.
00110       default:
00111         mAgendaMode = AGENDA_OTHER;
00112         showAgendaView();
00113     }
00114   }
00115 }
00116 
00117 void KOViewManager::writeSettings(KConfig *config)
00118 {
00119   config->setGroup("General");
00120 
00121   QString view;
00122   if (mCurrentView == mWhatsNextView) view = "WhatsNext";
00123   else if (mCurrentView == mMonthView) view = "Month";
00124   else if (mCurrentView == mListView) view = "List";
00125   else if (mCurrentView == mJournalView) view = "Journal";
00126   else if (mCurrentView == mTodoView) view = "Todo";
00127   else if (mCurrentView == mTimelineView) view = "Timeline";
00128   else {
00129     view = "Agenda";
00130     config->writeEntry( "Agenda Mode", mAgendaMode );
00131   }
00132 
00133   config->writeEntry("Current View",view);
00134 
00135   if (mAgendaView) {
00136     mAgendaView->writeSettings(config);
00137   }
00138   if (mListView) {
00139     mListView->writeSettings(config);
00140   }
00141   if (mTodoView) {
00142     mTodoView->saveLayout(config,"Todo View");
00143   }
00144 }
00145 
00146 void KOViewManager::showView(KOrg::BaseView *view)
00147 {
00148   if( view == mCurrentView ) return;
00149 
00150   mCurrentView = view;
00151 
00152   if ( mCurrentView && mCurrentView->isEventView() ) {
00153     mLastEventView = mCurrentView;
00154   }
00155 
00156   if ( mAgendaView ) mAgendaView->deleteSelectedDateTime();
00157 
00158   raiseCurrentView();
00159 
00160   mMainView->processIncidenceSelection( 0, QDate() );
00161 
00162   mMainView->updateView();
00163 
00164   mMainView->adaptNavigationUnits();
00165 }
00166 
00167 void KOViewManager::goMenu( bool enable )
00168 {
00169   KOrg::MainWindow *w = ActionManager::findInstance( KURL() );
00170   if ( w ) {
00171     KActionCollection *ac = w->getActionCollection();
00172     if ( ac ) {
00173       KAction *action;
00174       action = ac->action( "go_today" );
00175       if ( action ) {
00176         action->setEnabled( enable );
00177       }
00178       action = ac->action( "go_previous" );
00179       if ( action ) {
00180         action->setEnabled( enable );
00181       }
00182       action = ac->action( "go_next" );
00183       if ( action ) {
00184         action->setEnabled( enable );
00185       }
00186     }
00187   }
00188 }
00189 
00190 void KOViewManager::raiseCurrentView()
00191 {
00192   if ((mMonthView && KOPrefs::instance()->mFullViewMonth && mCurrentView == mMonthView) ||
00193       (mTodoView && KOPrefs::instance()->mFullViewTodo && mCurrentView == mTodoView)) {
00194     mMainView->showLeftFrame( false );
00195     if ( mCurrentView == mTodoView ) {
00196       mMainView->navigatorBar()->hide();
00197     } else {
00198       mMainView->navigatorBar()->show();
00199     }
00200   } else {
00201     mMainView->showLeftFrame( true );
00202     mMainView->navigatorBar()->hide();
00203   }
00204   mMainView->viewStack()->raiseWidget( widgetForView( mCurrentView  ) );
00205 }
00206 
00207 void KOViewManager::updateView()
00208 {
00209   if ( mCurrentView ) mCurrentView->updateView();
00210 }
00211 
00212 void KOViewManager::updateView(const QDate &start, const QDate &end)
00213 {
00214 //  kdDebug(5850) << "KOViewManager::updateView()" << endl;
00215 
00216   if (mCurrentView) mCurrentView->showDates(start, end);
00217 
00218   if (mTodoView) mTodoView->updateView();
00219 }
00220 
00221 void KOViewManager::connectView(KOrg::BaseView *view)
00222 {
00223   if (!view) return;
00224 
00225   // selecting an incidence
00226   connect( view, SIGNAL( incidenceSelected( Incidence *,const QDate & ) ),
00227            mMainView, SLOT( processMainViewSelection( Incidence *,const QDate & ) ) );
00228 
00229   // showing/editing/deleting an incidence. The calendar view takes care of the action.
00230   connect(view, SIGNAL(showIncidenceSignal(Incidence *)),
00231           mMainView, SLOT(showIncidence(Incidence *)));
00232   connect(view, SIGNAL(editIncidenceSignal(Incidence *)),
00233           mMainView, SLOT(editIncidence(Incidence *)));
00234   connect(view, SIGNAL(deleteIncidenceSignal(Incidence *)),
00235           mMainView, SLOT(deleteIncidence(Incidence *)));
00236   connect(view, SIGNAL(copyIncidenceSignal(Incidence *)),
00237           mMainView, SLOT(copyIncidence(Incidence *)));
00238   connect(view, SIGNAL(cutIncidenceSignal(Incidence *)),
00239           mMainView, SLOT(cutIncidence(Incidence *)));
00240   connect(view, SIGNAL(pasteIncidenceSignal()),
00241           mMainView, SLOT(pasteIncidence()));
00242   connect(view, SIGNAL(toggleAlarmSignal(Incidence *)),
00243           mMainView, SLOT(toggleAlarm(Incidence *)));
00244   connect(view,SIGNAL(dissociateOccurrenceSignal( Incidence *, const QDate & )),
00245           mMainView, SLOT(dissociateOccurrence( Incidence *, const QDate & )));
00246   connect(view,SIGNAL(dissociateFutureOccurrenceSignal( Incidence *, const QDate & )),
00247           mMainView, SLOT(dissociateFutureOccurrence( Incidence *, const QDate & )));
00248 
00249   // signals to create new incidences
00250   connect( view, SIGNAL(newEventSignal(ResourceCalendar *,const QString &)),
00251            mMainView, SLOT(newEvent(ResourceCalendar *,const QString &)) );
00252   connect( view, SIGNAL(newEventSignal(ResourceCalendar *,const QString &,const QDate &)),
00253            mMainView, SLOT(newEvent(ResourceCalendar *,const QString &,const QDate &)) );
00254   connect( view, SIGNAL(newEventSignal(ResourceCalendar *,const QString &,const QDateTime &)),
00255            mMainView, SLOT(newEvent(ResourceCalendar *,const QString &,const QDateTime &)) );
00256   connect( view, SIGNAL(newEventSignal(ResourceCalendar *,const QString &,const QDateTime &,const QDateTime &)),
00257            mMainView, SLOT(newEvent(ResourceCalendar *,const QString &,const QDateTime &,const QDateTime &)) );
00258 
00259   connect( view, SIGNAL(newTodoSignal(ResourceCalendar *,const QString &,const QDate &)),
00260            mMainView, SLOT(newTodo(ResourceCalendar *,const QString &,const QDate &)) );
00261   connect( view, SIGNAL(newSubTodoSignal(Todo *)),
00262            mMainView, SLOT(newSubTodo(Todo *)) );
00263 
00264   connect( view, SIGNAL(newJournalSignal(ResourceCalendar *,const QString &,const QDate &)),
00265            mMainView, SLOT(newJournal(ResourceCalendar *,const QString &,const QDate &)) );
00266 
00267   // reload settings
00268   connect(mMainView, SIGNAL(configChanged()), view, SLOT(updateConfig()));
00269 
00270   // Notifications about added, changed and deleted incidences
00271   connect( mMainView, SIGNAL( dayPassed( const QDate & ) ),
00272            view, SLOT( dayPassed( const QDate & ) ) );
00273   connect( view, SIGNAL( startMultiModify( const QString & ) ),
00274            mMainView, SLOT( startMultiModify( const QString & ) ) );
00275   connect( view, SIGNAL( endMultiModify() ),
00276            mMainView, SLOT( endMultiModify() ) );
00277 
00278   connect( mMainView, SIGNAL( newIncidenceChanger( IncidenceChangerBase* ) ),
00279            view, SLOT( setIncidenceChanger( IncidenceChangerBase * ) ) );
00280   view->setIncidenceChanger( mMainView->incidenceChanger() );
00281 }
00282 
00283 void KOViewManager::connectTodoView( KOTodoView* todoView )
00284 {
00285   if (!todoView) return;
00286 
00287   // SIGNALS/SLOTS FOR TODO VIEW
00288   connect( todoView, SIGNAL( purgeCompletedSignal() ),
00289            mMainView, SLOT( purgeCompleted() ) );
00290   connect( todoView, SIGNAL( unSubTodoSignal() ),
00291            mMainView, SLOT( todo_unsub() ) );
00292   connect( todoView, SIGNAL( unAllSubTodoSignal() ),
00293            mMainView, SLOT( makeSubTodosIndependents() ) );
00294 }
00295 
00296 void KOViewManager::zoomInHorizontally()
00297 {
00298   if( mAgendaView == mCurrentView ) mAgendaView->zoomInHorizontally();
00299 }
00300 void KOViewManager::zoomOutHorizontally()
00301 {
00302   if( mAgendaView== mCurrentView ) mAgendaView->zoomOutHorizontally();
00303 }
00304 void KOViewManager::zoomInVertically()
00305 {
00306   if( mAgendaView== mCurrentView ) mAgendaView->zoomInVertically();
00307 }
00308 void KOViewManager::zoomOutVertically()
00309 {
00310   if( mAgendaView== mCurrentView ) mAgendaView->zoomOutVertically();
00311 }
00312 
00313 void KOViewManager::addView(KOrg::BaseView *view)
00314 {
00315   connectView( view );
00316 #if QT_VERSION >= 300
00317   mMainView->viewStack()->addWidget( view );
00318 #else
00319   mMainView->viewStack()->addWidget( view, 1 );
00320 #endif
00321 }
00322 
00323 void KOViewManager::showWhatsNextView()
00324 {
00325   if (!mWhatsNextView) {
00326     mWhatsNextView = new KOWhatsNextView(mMainView->calendar(),mMainView->viewStack(),
00327                                          "KOViewManager::WhatsNextView");
00328     addView(mWhatsNextView);
00329   }
00330   goMenu( true );
00331   showView(mWhatsNextView);
00332 }
00333 
00334 void KOViewManager::showListView()
00335 {
00336   if (!mListView) {
00337     mListView = new KOListView(mMainView->calendar(), mMainView->viewStack(), "KOViewManager::ListView");
00338     addView(mListView);
00339   }
00340   goMenu( true );
00341   showView(mListView);
00342 }
00343 
00344 void KOViewManager::showAgendaView()
00345 {
00346   const bool showBoth = KOPrefs::instance()->agendaViewCalendarDisplay() == KOPrefs::AllCalendarViews;
00347   const bool showMerged = showBoth || KOPrefs::instance()->agendaViewCalendarDisplay() == KOPrefs::CalendarsMerged;
00348   const bool showSideBySide = showBoth || KOPrefs::instance()->agendaViewCalendarDisplay() == KOPrefs::CalendarsSideBySide;
00349 
00350   QWidget *parent = mMainView->viewStack();
00351   if ( !mAgendaViewTabs && showBoth ) {
00352     mAgendaViewTabs = new QTabWidget( mMainView->viewStack() );
00353     connect( mAgendaViewTabs, SIGNAL( currentChanged( QWidget* ) ),
00354              this, SLOT( currentAgendaViewTabChanged( QWidget* ) ) );
00355     parent = mAgendaViewTabs;
00356   }
00357 
00358   if ( !mAgendaView && showMerged ) {
00359     mAgendaView = new KOAgendaView( mMainView->calendar(),
00360                                     mMainView,
00361                                     parent,
00362                                     "KOViewManager::AgendaView" );
00363 
00364     addView(mAgendaView);
00365 
00366     connect(mAgendaView, SIGNAL( toggleExpand() ),
00367             mMainView, SLOT( toggleExpand() ) );
00368     connect(mMainView, SIGNAL( calendarViewExpanded( bool ) ),
00369             mAgendaView, SLOT( setExpandedButton( bool ) ) );
00370 
00371     connect( mAgendaView,SIGNAL( zoomViewHorizontally(const QDate &, int )),
00372              mMainView->dateNavigator(),SLOT( selectDates( const QDate &, int ) ) );
00373     mAgendaView->readSettings();
00374   }
00375 
00376   if ( !mAgendaSideBySideView && showSideBySide ) {
00377     mAgendaSideBySideView =
00378       new MultiAgendaView( mMainView->calendar(), mMainView, parent,
00379                         "KOViewManager::AgendaSideBySideView" );
00380 
00381     addView(mAgendaSideBySideView);
00382 
00383 /*    connect(mAgendaSideBySideView, SIGNAL( toggleExpand() ),
00384             mMainView, SLOT( toggleExpand() ) );
00385     connect(mMainView, SIGNAL( calendarViewExpanded( bool ) ),
00386             mAgendaSideBySideView, SLOT( setExpandedButton( bool ) ) );
00387 
00388     connect( mAgendaSideBySideView,SIGNAL( zoomViewHorizontally(const QDate &, int )),
00389              mMainView->dateNavigator(),SLOT( selectDates( const QDate &, int ) ) );*/
00390   }
00391 
00392   if ( showBoth && mAgendaViewTabs ) {
00393     if ( mAgendaView && mAgendaViewTabs->indexOf( mAgendaView ) < 0 )
00394       mAgendaViewTabs->addTab( mAgendaView, i18n("Merged calendar") );
00395     if ( mAgendaSideBySideView  && mAgendaViewTabs->indexOf( mAgendaSideBySideView ) < 0 )
00396       mAgendaViewTabs->addTab( mAgendaSideBySideView, i18n("Calendars Side by Side") );
00397   } else {
00398     if ( mAgendaView && mMainView->viewStack()->id( mAgendaView ) < 0 )
00399       mMainView->viewStack()->addWidget( mAgendaView );
00400     if ( mAgendaSideBySideView && mMainView->viewStack()->id( mAgendaSideBySideView ) < 0 )
00401       mMainView->viewStack()->addWidget( mAgendaSideBySideView );
00402   }
00403 
00404   goMenu( true );
00405   if ( mAgendaViewTabs && showBoth )
00406     showView( static_cast<KOrg::BaseView*>( mAgendaViewTabs->currentPage() ) );
00407   else if ( mAgendaView && showMerged )
00408     showView( mAgendaView );
00409   else if ( mAgendaSideBySideView && showSideBySide )
00410     showView( mAgendaSideBySideView );
00411 }
00412 
00413 void KOViewManager::showDayView()
00414 {
00415   mAgendaMode = AGENDA_DAY;
00416   showAgendaView();
00417   mMainView->dateNavigator()->selectDates( 1 );
00418 }
00419 
00420 void KOViewManager::showWorkWeekView()
00421 {
00422   mAgendaMode = AGENDA_WORK_WEEK;
00423   showAgendaView();
00424   mMainView->dateNavigator()->selectWorkWeek();
00425 }
00426 
00427 void KOViewManager::showWeekView()
00428 {
00429   mAgendaMode = AGENDA_WEEK;
00430   showAgendaView();
00431   mMainView->dateNavigator()->selectWeek();
00432 }
00433 
00434 void KOViewManager::showNextXView()
00435 {
00436   mAgendaMode = AGENDA_NEXTX;
00437   showAgendaView();
00438   mMainView->dateNavigator()->selectDates( QDate::currentDate(),
00439                                            KOPrefs::instance()->mNextXDays );
00440 }
00441 
00442 void KOViewManager::showMonthView()
00443 {
00444   if (!mMonthView) {
00445     mMonthView = new KOMonthView(mMainView->calendar(), mMainView->viewStack(), "KOViewManager::MonthView");
00446     addView(mMonthView);
00447   }
00448 
00449   goMenu( true );
00450   showView(mMonthView);
00451 }
00452 
00453 void KOViewManager::showTodoView()
00454 {
00455   if ( !mTodoView ) {
00456     mTodoView = new KOTodoView( mMainView->calendar(), mMainView->viewStack(),
00457                                 "KOViewManager::TodoView" );
00458     mTodoView->setCalendar( mMainView->calendar() );
00459     addView( mTodoView );
00460     connectTodoView( mTodoView );
00461 
00462     KConfig *config = KOGlobals::self()->config();
00463     mTodoView->restoreLayout( config, "Todo View" );
00464   }
00465 
00466   goMenu( false );
00467   showView( mTodoView );
00468 }
00469 
00470 void KOViewManager::showJournalView()
00471 {
00472   if (!mJournalView) {
00473     mJournalView = new KOJournalView(mMainView->calendar(),mMainView->viewStack(),
00474                                      "KOViewManager::JournalView");
00475     addView(mJournalView);
00476   }
00477 
00478   goMenu( true );
00479   showView(mJournalView);
00480 }
00481 
00482 
00483 void KOViewManager::showTimelineView()
00484 {
00485   if (!mTimelineView) {
00486     mTimelineView = new KOTimelineView(mMainView->calendar(),mMainView->viewStack(),
00487                                      "KOViewManager::TimelineView");
00488     addView(mTimelineView);
00489   }
00490   goMenu( true );
00491   showView(mTimelineView);
00492 }
00493 
00494 void KOViewManager::showEventView()
00495 {
00496   if ( mLastEventView ) {
00497     goMenu( true );
00498     showView( mLastEventView );
00499   } else {
00500     showWeekView();
00501   }
00502 }
00503 
00504 Incidence *KOViewManager::currentSelection()
00505 {
00506   if ( !mCurrentView ) return 0;
00507   Incidence::List incidenceList = mCurrentView->selectedIncidences();
00508   if ( incidenceList.isEmpty() ) return 0;
00509 
00510   return incidenceList.first();
00511 }
00512 
00513 QDate KOViewManager::currentSelectionDate()
00514 {
00515   QDate qd;
00516   if (mCurrentView) {
00517     DateList qvl = mCurrentView->selectedDates();
00518     if (!qvl.isEmpty()) qd = qvl.first();
00519   }
00520   return qd;
00521 }
00522 
00523 void KOViewManager::setDocumentId( const QString &id )
00524 {
00525   if (mTodoView) mTodoView->setDocumentId( id );
00526 }
00527 
00528 
00529 QWidget* KOViewManager::widgetForView( KOrg::BaseView* view ) const
00530 {
00531   const bool showBoth = KOPrefs::instance()->agendaViewCalendarDisplay() == KOPrefs::AllCalendarViews;
00532   if ( (view == mAgendaView || view == mAgendaSideBySideView) && mAgendaViewTabs && showBoth ) {
00533     return mAgendaViewTabs;
00534   }
00535   return view;
00536 }
00537 
00538 
00539 void KOViewManager::currentAgendaViewTabChanged( QWidget* widget )
00540 {
00541   goMenu( true );
00542   showView( static_cast<KOrg::BaseView*>( widget ) );
00543 }
00544 
00545 void KOViewManager::resourcesChanged()
00546 {
00547   if ( mAgendaView )
00548     mAgendaView->resourcesChanged();
00549   if ( mAgendaSideBySideView )
00550     mAgendaSideBySideView->resourcesChanged();
00551 }
00552 
00553 void KOViewManager::updateMultiCalendarDisplay()
00554 {
00555   if ( mCurrentView == mAgendaView            ||
00556        mCurrentView == mAgendaSideBySideView  ||
00557        ( mAgendaViewTabs && mCurrentView == mAgendaViewTabs->currentPage() ) ) {
00558     showAgendaView();
00559   } else {
00560     updateView();
00561   }
00562 }
KDE Home | KDE Accessibility Home | Description of Access Keys