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