korganizer Library API Documentation

kotodoviewitem.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 <qpainter.h>
00026 
00027 #include <klocale.h>
00028 #include <kdebug.h>
00029 #include <qpainter.h>
00030 #include <qpixmap.h>
00031 
00032 #include "kotodoviewitem.h"
00033 #include "kotodoview.h"
00034 #include "koprefs.h"
00035 #include "koglobals.h"
00036 
00037 KOTodoViewItem::KOTodoViewItem( QListView *parent, Todo *todo, KOTodoView *kotodo)
00038   : QCheckListItem( parent , "", CheckBox ), mTodo( todo ), mTodoView( kotodo )
00039 {
00040   construct();
00041 }
00042 
00043 KOTodoViewItem::KOTodoViewItem( KOTodoViewItem *parent, Todo *todo, KOTodoView *kotodo )
00044   : QCheckListItem( parent, "", CheckBox ), mTodo( todo ), mTodoView( kotodo )
00045 {
00046   construct();
00047 }
00048 
00049 // TODO: Is this the best way to sort the items on due dates?
00050 int KOTodoViewItem::compare( QListViewItem *i, int col, bool ascending ) const
00051 {
00052   if ( i && ( col == eDueDateColumn ) ) {
00053     QString thiskey( key( col, ascending ) );
00054     QString ikey( i->key( col, ascending ) );
00055     if ( thiskey.isEmpty() ) { // no due date set
00056       if ( ikey.isEmpty() )
00057         return 0;
00058       else
00059         if ( ascending ) return 1;
00060         else return -1;
00061     } else {
00062       if ( ikey.isEmpty() ) // i has not due date set, but this has
00063         if ( ascending ) return -1;
00064         else return 1;
00065       else
00066         return QCheckListItem::compare( i, col, ascending );
00067     }
00068   } else return QCheckListItem::compare( i, col, ascending );
00069 }
00070 
00071 QString KOTodoViewItem::key( int column, bool ) const
00072 {
00073   QMap<int,QString>::ConstIterator it = mKeyMap.find( column );
00074   if ( it == mKeyMap.end() ) {
00075     return text( column );
00076   } else {
00077     return *it;
00078   }
00079 }
00080 
00081 void KOTodoViewItem::setSortKey(int column,const QString &key)
00082 {
00083   mKeyMap.insert(column,key);
00084 }
00085 
00086 #if QT_VERSION >= 300
00087 void KOTodoViewItem::paintBranches(QPainter *p,const QColorGroup & cg,int w,
00088                                    int y,int h)
00089 {
00090   QListViewItem::paintBranches(p,cg,w,y,h);
00091 }
00092 #else
00093 #endif
00094 
00095 void KOTodoViewItem::construct()
00096 {
00097   if ( !mTodo ) return;
00098   m_init = true;
00099   QString keyd = "9";
00100 
00101   setOn( mTodo->isCompleted() );
00102   setText( eSummaryColumn, mTodo->summary());
00103   static const QPixmap recurPxmp = KOGlobals::self()->smallIcon("recur");
00104   if ( mTodo->doesRecur() ) {
00105     setPixmap( eRecurColumn, recurPxmp );
00106     setSortKey( eRecurColumn, "1" );
00107   }
00108   else setSortKey( eRecurColumn, "0" );
00109   setText( ePriorityColumn, QString::number(mTodo->priority()) );
00110   setText( ePercentColumn, QString::number(mTodo->percentComplete()) );
00111   if ( mTodo->percentComplete()<100 ) {
00112     if (mTodo->isCompleted()) setSortKey( ePercentColumn, QString::number(999) );
00113     else setSortKey( ePercentColumn, QString::number( mTodo->percentComplete() ) );
00114   }
00115   else {
00116     if (mTodo->isCompleted()) setSortKey( ePercentColumn, QString::number(999) );
00117     else setSortKey( ePercentColumn, QString::number(99) );
00118   }
00119 
00120   if (mTodo->hasDueDate()) {
00121     QString dtStr = mTodo->dtDueDateStr();
00122     QString keyt = "";
00123     if (!mTodo->doesFloat()) {
00124       dtStr += " " + mTodo->dtDueTimeStr();
00125     }
00126     setText( eDueDateColumn, dtStr );
00127     keyd = mTodo->dtDue().toString(Qt::ISODate);
00128   } else {
00129     keyd = "";
00130     setText( eDueDateColumn, "" );
00131   }
00132   keyd += QString::number( mTodo->priority() );
00133   setSortKey( eDueDateColumn, keyd );
00134 
00135   QString priorityKey = QString::number( mTodo->priority() ) + keyd;
00136   if ( mTodo->isCompleted() ) setSortKey( ePriorityColumn, "1" + priorityKey );
00137   else setSortKey( ePriorityColumn, "0" + priorityKey );
00138 
00139   setText( eCategoriesColumn, mTodo->categoriesStr() );
00140 
00141 #if 0
00142   // Find sort id in description. It's the text behind the last '#' character
00143   // found in the description. White spaces are removed from beginning and end
00144   // of sort id.
00145   int pos = mTodo->description().findRev('#');
00146   if (pos < 0) {
00147     setText( eDescriptionColumn, "" );
00148   } else {
00149     QString str = mTodo->description().mid(pos+1);
00150     str.stripWhiteSpace();
00151     setText( eDescriptionColumn, str );
00152   }
00153 #endif
00154 
00155   m_known = false;
00156   m_init = false;
00157 }
00158 
00159 void KOTodoViewItem::stateChange(bool state)
00160 {
00161   // do not change setting on startup or if no valid todo item is given
00162   if ( m_init || !mTodo ) return;
00163 
00164   if ( mTodo->isReadOnly() ) return;
00165 
00166   kdDebug(5850) << "State changed, modified " << state << endl;
00167   QString keyd = "9";
00168 
00169   Todo*oldTodo = mTodo->clone();
00170 
00171   if (state)
00172     mTodoView->emitCompletedSignal( mTodo );
00173   else mTodo->setPercentComplete(0);
00174 
00175   if (mTodo->hasDueDate()) {
00176     QString dtStr = mTodo->dtDueDateStr();
00177     QString keyt = "";
00178     if (!mTodo->doesFloat()) {
00179       dtStr += " " + mTodo->dtDueTimeStr();
00180     }
00181     setText( eDueDateColumn, dtStr );
00182     keyd = mTodo->dtDue().toString(Qt::ISODate);
00183   } else {
00184     setText( eDueDateColumn, "" );
00185   }
00186   setSortKey( eDueDateColumn, keyd );
00187 
00188   QString priorityKey = QString::number( mTodo->priority() ) + keyd;
00189   if ( mTodo->isCompleted() ) setSortKey( ePriorityColumn, "1" + priorityKey );
00190   else setSortKey( ePriorityColumn, "0" + priorityKey );
00191 
00192   setText( ePercentColumn, QString::number(mTodo->percentComplete()));
00193   if (mTodo->percentComplete()<100) {
00194     if (mTodo->isCompleted()) setSortKey( ePercentColumn, QString::number(999) );
00195     else setSortKey( ePercentColumn, QString::number(mTodo->percentComplete()) );
00196   }
00197   else {
00198     if (mTodo->isCompleted()) setSortKey( ePercentColumn, QString::number(999) );
00199     else setSortKey( ePercentColumn, QString::number(99) );
00200   }
00201   // TODO_RK: Find a way to emit startMultiModify( "..." ) somewhere so that checking all subitems will belong to the same undo item
00202   QListViewItem *myChild = firstChild();
00203   KOTodoViewItem *item;
00204   while( myChild ) {
00205     item = static_cast<KOTodoViewItem*>(myChild);
00206     item->stateChange(state);
00207     myChild = myChild->nextSibling();
00208   }
00209   mTodoView->setTodoModified( oldTodo, mTodo );
00210   delete oldTodo;
00211 }
00212 
00213 bool KOTodoViewItem::isAlternate()
00214 {
00215 #ifndef KORG_NOLVALTERNATION
00216   KOTodoListView *lv = static_cast<KOTodoListView *>(listView());
00217   if (lv && lv->alternateBackground().isValid())
00218   {
00219     KOTodoViewItem *above = 0;
00220     above = dynamic_cast<KOTodoViewItem *>(itemAbove());
00221     m_known = above ? above->m_known : true;
00222     if (m_known)
00223     {
00224        m_odd = above ? !above->m_odd : false;
00225     }
00226     else
00227     {
00228        KOTodoViewItem *item;
00229        bool previous = true;
00230        if (QListViewItem::parent())
00231        {
00232           item = dynamic_cast<KOTodoViewItem *>(QListViewItem::parent());
00233           if (item)
00234              previous = item->m_odd;
00235           item = dynamic_cast<KOTodoViewItem *>(QListViewItem::parent()->firstChild());
00236        }
00237        else
00238        {
00239           item = dynamic_cast<KOTodoViewItem *>(lv->firstChild());
00240        }
00241 
00242        while(item)
00243        {
00244           item->m_odd = previous = !previous;
00245           item->m_known = true;
00246           item = dynamic_cast<KOTodoViewItem *>(item->nextSibling());
00247        }
00248     }
00249     return m_odd;
00250   }
00251   return false;
00252 #else
00253   return false;
00254 #endif
00255 }
00256 
00257 void KOTodoViewItem::paintCell(QPainter *p, const QColorGroup &cg, int column, int width, int alignment)
00258 {
00259   QColorGroup _cg = cg;
00260   // If no todo is set, just don't paint anything...
00261   if ( !mTodo ) return;
00262 #ifndef KORG_NOLVALTERNATION
00263   if (isAlternate())
00264         _cg.setColor(QColorGroup::Base, static_cast< KOTodoListView* >(listView())->alternateBackground());
00265   if (mTodo->hasDueDate()) {
00266     if (mTodo->dtDue().date()==QDate::currentDate() &&
00267         !mTodo->isCompleted()) {
00268       _cg.setColor(QColorGroup::Base, KOPrefs::instance()->mTodoDueTodayColor);
00269       _cg.setColor(QColorGroup::Text, getTextColor(KOPrefs::instance()->mTodoDueTodayColor));
00270     }
00271     if (mTodo->dtDue().date() < QDate::currentDate() &&
00272         !mTodo->isCompleted()) {
00273       _cg.setColor(QColorGroup::Base, KOPrefs::instance()->mTodoOverdueColor);
00274       _cg.setColor(QColorGroup::Text, getTextColor(KOPrefs::instance()->mTodoOverdueColor));
00275     }
00276   }
00277 #endif
00278 
00279   // show the progess by a horizontal bar
00280   if ( column == ePercentColumn ) {
00281     p->save();
00282     int progress = (int)(( (width-6)*mTodo->percentComplete())/100.0 + 0.5);
00283 
00284     p->fillRect( 0, 0, width, height(), _cg.base() ); // background
00285     p->setPen( KGlobalSettings::textColor() );  //border
00286     p->setBrush( KGlobalSettings::baseColor() );  //filling
00287     p->drawRect( 2, 2, width-4, height()-4);
00288     p->fillRect( 3, 3, progress, height()-6,
00289         KGlobalSettings::highlightColor() );
00290     p->restore();
00291   } else {
00292     QCheckListItem::paintCell(p, _cg, column, width, alignment);
00293   }
00294 }
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 Mar 23 22:45:26 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003