korganizer Library API Documentation

kolistview.cpp

00001 /*
00002     This file is part of KOrganizer.
00003     Copyright (c) 1999 Preston Brown
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 <qlistview.h>
00026 #include <qlayout.h>
00027 #include <qpopupmenu.h>
00028 #include <qcursor.h>
00029 
00030 #include <klocale.h>
00031 #include <kdebug.h>
00032 #include <kiconloader.h>
00033 #include <kglobal.h>
00034 
00035 #include <libkcal/calendar.h>
00036 
00037 #ifndef KORG_NOPRINTER
00038 #include "calprinter.h"
00039 #endif
00040 #include "koglobals.h"
00041 #include "koprefs.h"
00042 #include "koincidencetooltip.h"
00043 #include "koeventpopupmenu.h"
00044 
00045 #include "kolistview.h"
00046 #include "kolistview.moc"
00047 
00048 
00049 KOListViewToolTip::KOListViewToolTip( QWidget* parent,
00050                                       KListView* lv )
00051   :QToolTip(parent)
00052 {
00053   eventlist=lv;
00054 }
00055 
00056 void KOListViewToolTip::maybeTip( const QPoint & pos)
00057 {
00058   QRect r;
00059   QListViewItem *it = eventlist->itemAt(pos);
00060   KOListViewItem *i = static_cast<KOListViewItem*>(it);
00061 
00062   if( i && KOPrefs::instance()->mEnableToolTips ) {
00063     /* Calculate the rectangle. */
00064     r=eventlist->itemRect( it );
00065     /* Show the tip */
00066     QString tipText;
00067     ToolTipVisitor<KOListViewItem> v;
00068     if (v.act(i, &tipText, true)) {
00069       tip(r, tipText);
00070     }
00071   }
00072 
00073 }
00074 
00075 ListItemVisitor::ListItemVisitor(KOListViewItem *item)
00076 {
00077   mItem = item;
00078 }
00079 
00080 ListItemVisitor::~ListItemVisitor()
00081 {
00082 }
00083 
00084 bool ListItemVisitor::visit(Event *e)
00085 {
00086   mItem->setText(0,e->summary());
00087   if ( e->isAlarmEnabled() ) {
00088     static const QPixmap alarmPxmp = KOGlobals::self()->smallIcon("bell");
00089     mItem->setPixmap(1,alarmPxmp);
00090     mItem->setSortKey(1,"1");
00091   }
00092   else
00093     mItem->setSortKey(1,"0");
00094 
00095   if ( e->doesRecur() ) {
00096     static const QPixmap recurPxmp = KOGlobals::self()->smallIcon("recur");
00097     mItem->setPixmap(2,recurPxmp);
00098     mItem->setSortKey(2,"1");
00099   }
00100   else
00101     mItem->setSortKey(2,"0");
00102 
00103   mItem->setText( 3,e->dtStartDateStr());
00104   if (e->doesFloat()) mItem->setText(4, i18n("---")); else mItem->setText( 4, e->dtStartTimeStr() );
00105   mItem->setText( 5,e->dtEndDateStr());
00106   if (e->doesFloat()) mItem->setText(6, i18n("---")); else mItem->setText( 6, e->dtEndTimeStr() );
00107   mItem->setText( 7, i18n( "---" ) );
00108   mItem->setText( 8, i18n( "---" ) );
00109   mItem->setText( 9,e->categoriesStr());
00110 
00111   QString key = e->dtStart().toString(Qt::ISODate);
00112   mItem->setSortKey(3,key);
00113 
00114   key = e->dtEnd().toString(Qt::ISODate);
00115   mItem->setSortKey(5,key);
00116 
00117   return true;
00118 }
00119 
00120 bool ListItemVisitor::visit(Todo *t)
00121 {
00122   static const QPixmap todoPxmp = KOGlobals::self()->smallIcon("todo");
00123   static const QPixmap todoDonePxmp = KOGlobals::self()->smallIcon("checkedbox");
00124   mItem->setPixmap(0, t->isCompleted() ? todoDonePxmp : todoPxmp );
00125   mItem->setText(0,t->summary());
00126   if ( t->isAlarmEnabled() ) {
00127     static const QPixmap alarmPxmp = KOGlobals::self()->smallIcon("bell");
00128     mItem->setPixmap(1,alarmPxmp);
00129     mItem->setSortKey(1, "1");
00130   }
00131   else
00132     mItem->setSortKey(1, "0");
00133 
00134   if ( t->doesRecur() ) {
00135     static const QPixmap recurPxmp = KOGlobals::self()->smallIcon("recur");
00136     mItem->setPixmap(2,recurPxmp);
00137     mItem->setSortKey(2, "1");
00138   }
00139   else
00140     mItem->setSortKey(2, "0");
00141 
00142   if (t->hasStartDate()) {
00143     mItem->setText(3,t->dtStartDateStr());
00144     mItem->setSortKey(3,t->dtStart().toString(Qt::ISODate));
00145     if (t->doesFloat()) {
00146       mItem->setText(4,"---");
00147     } else {
00148       mItem->setText(4,t->dtStartTimeStr());
00149     }
00150   } else {
00151     mItem->setText(3,"---");
00152     mItem->setText(4,"---");
00153   }
00154 
00155   if (t->hasDueDate()) {
00156     mItem->setText(5,t->dtDueDateStr());
00157     if (t->doesFloat()) {
00158       mItem->setText(6,"---");
00159     } else {
00160       mItem->setText(6,t->dtDueTimeStr());
00161     }
00162   } else {
00163     mItem->setText(5,"---");
00164     mItem->setText(6,"---");
00165   }
00166   mItem->setText(7,t->categoriesStr());
00167 
00168   mItem->setSortKey(5,t->dtDue().toString(Qt::ISODate));
00169 
00170   return true;
00171 }
00172 
00173 bool ListItemVisitor::visit(Journal *t)
00174 {
00175   static const QPixmap jrnalPxmp = KOGlobals::self()->smallIcon("journal");
00176   mItem->setPixmap(0,jrnalPxmp);
00177   // Just use the first line
00178   mItem->setText( 0, t->description().section( "\n", 0, 0 ) );
00179   mItem->setText( 3, t->dtStartDateStr() );
00180 
00181   return true;
00182 }
00183 
00184 KOListView::KOListView( Calendar *calendar, QWidget *parent,
00185                         const char *name)
00186   : KOEventView(calendar, parent, name)
00187 {
00188   mActiveItem = 0;
00189 
00190   mListView = new KListView(this);
00191   mListView->addColumn(i18n("Summary"));
00192   mListView->addColumn(i18n("Alarm")); // alarm set?
00193   mListView->addColumn(i18n("Recurs")); // recurs?
00194   mListView->addColumn(i18n("Start Date"));
00195   mListView->setColumnAlignment(3,AlignHCenter);
00196   mListView->addColumn(i18n("Start Time"));
00197   mListView->setColumnAlignment(4,AlignHCenter);
00198   mListView->addColumn(i18n("End Date"));
00199   mListView->setColumnAlignment(5,AlignHCenter);
00200   mListView->addColumn(i18n("End Time"));
00201   mListView->setColumnAlignment(6,AlignHCenter);
00202   mListView->addColumn(i18n("Categories"));
00203   mListView->setColumnAlignment(7,AlignHCenter);
00204 
00205   QBoxLayout *layoutTop = new QVBoxLayout(this);
00206   layoutTop->addWidget(mListView);
00207 
00208   mPopupMenu = eventPopup();
00209 /*
00210   mPopupMenu->insertSeparator();
00211   mPopupMenu->insertItem(i18n("Show Dates"), this,
00212                       SLOT(showDates()));
00213   mPopupMenu->insertItem(i18n("Hide Dates"), this,
00214                       SLOT(hideDates()));
00215 */
00216 
00217   QObject::connect( mListView, SIGNAL( doubleClicked( QListViewItem * ) ),
00218                     SLOT( defaultItemAction( QListViewItem * ) ) );
00219   QObject::connect( mListView, SIGNAL( returnPressed( QListViewItem * ) ),
00220                     SLOT( defaultItemAction( QListViewItem * ) ) );
00221   QObject::connect( mListView, SIGNAL( rightButtonClicked ( QListViewItem *,
00222                                                             const QPoint &,
00223                                                             int ) ),
00224                     SLOT( popupMenu( QListViewItem *, const QPoint &, int ) ) );
00225   QObject::connect( mListView, SIGNAL( selectionChanged() ),
00226                     SLOT( processSelectionChange() ) );
00227 
00228 //  setMinimumSize(100,100);
00229   mListView->restoreLayout(KOGlobals::self()->config(),"KOListView Layout");
00230 
00231   new KOListViewToolTip( mListView->viewport(), mListView );
00232 
00233   mSelectedDates.append( QDate::currentDate() );
00234 }
00235 
00236 KOListView::~KOListView()
00237 {
00238   delete mPopupMenu;
00239 }
00240 
00241 int KOListView::maxDatesHint()
00242 {
00243   return 0;
00244 }
00245 
00246 int KOListView::currentDateCount()
00247 {
00248   return mSelectedDates.count();
00249 }
00250 
00251 Incidence::List KOListView::selectedIncidences()
00252 {
00253   Incidence::List eventList;
00254 
00255   QListViewItem *item = mListView->selectedItem();
00256   if (item) eventList.append(((KOListViewItem *)item)->data());
00257 
00258   return eventList;
00259 }
00260 
00261 DateList KOListView::selectedDates()
00262 {
00263   return mSelectedDates;
00264 }
00265 
00266 void KOListView::showDates(bool show)
00267 {
00268   // Shouldn't we set it to a value greater 0? When showDates is called with
00269   // show == true at first, then the columnwidths are set to zero.
00270   static int oldColWidth1 = 0;
00271   static int oldColWidth3 = 0;
00272 
00273   if (!show) {
00274     oldColWidth1 = mListView->columnWidth(1);
00275     oldColWidth3 = mListView->columnWidth(3);
00276     mListView->setColumnWidth(1, 0);
00277     mListView->setColumnWidth(3, 0);
00278   } else {
00279     mListView->setColumnWidth(1, oldColWidth1);
00280     mListView->setColumnWidth(3, oldColWidth3);
00281   }
00282   mListView->repaint();
00283 }
00284 
00285 void KOListView::printPreview(CalPrinter *calPrinter, const QDate &fd,
00286                                const QDate &td)
00287 {
00288 #ifndef KORG_NOPRINTER
00289   calPrinter->preview(CalPrinter::Day, fd, td);
00290 #endif
00291 }
00292 
00293 void KOListView::showDates()
00294 {
00295   showDates(true);
00296 }
00297 
00298 void KOListView::hideDates()
00299 {
00300   showDates(false);
00301 }
00302 
00303 void KOListView::updateView()
00304 {
00305   kdDebug(5850) << "KOListView::updateView() does nothing" << endl;
00306 }
00307 
00308 void KOListView::showDates(const QDate &start, const QDate &end)
00309 {
00310   clear();
00311 
00312   QDate date = start;
00313   while( date <= end ) {
00314     addIncidences( calendar()->incidences(date) );
00315     mSelectedDates.append( date );
00316     date = date.addDays( 1 );
00317   }
00318 
00319   emit incidenceSelected( 0 );
00320 }
00321 
00322 void KOListView::addIncidences( const Incidence::List &incidenceList )
00323 {
00324   Incidence::List::ConstIterator it;
00325   for( it = incidenceList.begin(); it != incidenceList.end(); ++it ) {
00326     addIncidence( *it );
00327   }
00328 }
00329 
00330 void KOListView::addIncidence(Incidence *incidence)
00331 {
00332   if ( mUidDict.find( incidence->uid() ) ) return;
00333 
00334   mUidDict.insert( incidence->uid(), incidence );
00335 
00336   KOListViewItem *item = new KOListViewItem( incidence, mListView );
00337   ListItemVisitor v(item);
00338   if (incidence->accept(v)) return;
00339   else delete item;
00340 }
00341 
00342 void KOListView::showIncidences( const Incidence::List &incidenceList )
00343 {
00344   clear();
00345 
00346   addIncidences( incidenceList );
00347 
00348   // After new creation of list view no events are selected.
00349   emit incidenceSelected( 0 );
00350 }
00351 
00352 void KOListView::changeIncidenceDisplay(Incidence *incidence, int action)
00353 {
00354   KOListViewItem *item;
00355   QDate f = mSelectedDates.first();
00356   QDate l = mSelectedDates.last();
00357 
00358   QDate date;
00359   if ( incidence->type() == "Todo" )
00360     date = static_cast<Todo *>(incidence)->dtDue().date();
00361   else
00362     date = incidence->dtStart().date();
00363 
00364   switch(action) {
00365     case KOGlobals::INCIDENCEADDED: {
00366       if ( date >= f && date <= l )
00367         addIncidence( incidence );
00368       break;
00369     }
00370     case KOGlobals::INCIDENCEEDITED: {
00371       item = getItemForIncidence(incidence);
00372       if (item) {
00373         delete item;
00374         mUidDict.remove( incidence->uid() );
00375       }
00376       if ( date >= f && date <= l )
00377         addIncidence( incidence );
00378     }
00379     break;
00380     case KOGlobals::INCIDENCEDELETED: {
00381       item = getItemForIncidence(incidence);
00382       if (item) {
00383         delete item;
00384         mUidDict.remove( incidence->uid() );
00385       }
00386       break;
00387     }
00388     default:
00389       kdDebug(5850) << "KOListView::changeIncidenceDisplay(): Illegal action " << action << endl;
00390   }
00391 }
00392 
00393 KOListViewItem *KOListView::getItemForIncidence(Incidence *incidence)
00394 {
00395   KOListViewItem *item = (KOListViewItem *)mListView->firstChild();
00396   while (item) {
00397 //    kdDebug(5850) << "Item " << item->text(0) << " found" << endl;
00398     if (item->data() == incidence) return item;
00399     item = (KOListViewItem *)item->nextSibling();
00400   }
00401   return 0;
00402 }
00403 
00404 void KOListView::defaultItemAction(QListViewItem *i)
00405 {
00406   KOListViewItem *item = static_cast<KOListViewItem *>( i );
00407   if ( item ) defaultAction( item->data() );
00408 }
00409 
00410 void KOListView::popupMenu(QListViewItem *item,const QPoint &,int)
00411 {
00412   mActiveItem = (KOListViewItem *)item;
00413   if (mActiveItem) {
00414     Incidence *incidence = mActiveItem->data();
00415     // TODO: For recurring incidences we don't know the date of this 
00416     // occurence, there's no reference to it at all!
00417     mPopupMenu->showIncidencePopup( incidence, QDate() );
00418   }
00419   else {
00420     showNewEventPopup();
00421   }
00422 }
00423 
00424 void KOListView::readSettings(KConfig *config)
00425 {
00426   mListView->restoreLayout(config,"KOListView Layout");
00427 }
00428 
00429 void KOListView::writeSettings(KConfig *config)
00430 {
00431   mListView->saveLayout(config,"KOListView Layout");
00432 }
00433 
00434 void KOListView::processSelectionChange()
00435 {
00436   kdDebug(5850) << "KOListView::processSelectionChange()" << endl;
00437 
00438   KOListViewItem *item =
00439     static_cast<KOListViewItem *>( mListView->selectedItem() );
00440 
00441   if ( !item ) {
00442     emit incidenceSelected( 0 );
00443   } else {
00444     emit incidenceSelected( item->data() );
00445   }
00446 }
00447 
00448 void KOListView::clearSelection()
00449 {
00450   mListView->selectAll( false );
00451 }
00452 
00453 void KOListView::clear()
00454 {
00455   mSelectedDates.clear();
00456   mListView->clear();
00457   mUidDict.clear();
00458 }
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 Jan 31 15:55:49 2007 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003