korganizer Library API Documentation

kotodoviewitem.cpp

00001 /*
00002     This file is part of KOrganizer.
00003     Copyright (c) 2000,2001 Cornelius Schumacher <schumacher@kde.org>
00004 
00005     This program is free software; you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation; either version 2 of the License, or
00008     (at your option) any later version.
00009 
00010     This program is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with this program; if not, write to the Free Software
00017     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00018 */
00019 
00020 #include <qpainter.h>
00021 
00022 #include <klocale.h>
00023 #include <kdebug.h>
00024 #include <qpainter.h>
00025 #include <qpixmap.h>
00026 
00027 #include "kotodoviewitem.h"
00028 #include "kotodoview.h"
00029 #include "koprefs.h"
00030 
00031 KOTodoViewItem::KOTodoViewItem( QListView *parent, Todo *todo, KOTodoView *kotodo)
00032   : QCheckListItem( parent , "", CheckBox ), mTodo( todo ), mTodoView( kotodo )
00033 {
00034   construct();
00035 }
00036 
00037 KOTodoViewItem::KOTodoViewItem( KOTodoViewItem *parent, Todo *todo, KOTodoView *kotodo )
00038   : QCheckListItem( parent, "", CheckBox ), mTodo( todo ), mTodoView( kotodo )
00039 {
00040   construct();
00041 }
00042 
00043 QString KOTodoViewItem::key(int column,bool) const
00044 {
00045   QMap<int,QString>::ConstIterator it = mKeyMap.find(column);
00046   if (it == mKeyMap.end()) {
00047     return text(column);
00048   } else {
00049     return *it;
00050   }
00051 }
00052 
00053 void KOTodoViewItem::setSortKey(int column,const QString &key)
00054 {
00055   mKeyMap.insert(column,key);
00056 }
00057 
00058 #if QT_VERSION >= 300
00059 void KOTodoViewItem::paintBranches(QPainter *p,const QColorGroup & cg,int w,
00060                                    int y,int h)
00061 {
00062   QListViewItem::paintBranches(p,cg,w,y,h);
00063 }
00064 #else
00065 #endif
00066 
00067 void KOTodoViewItem::construct()
00068 {
00069   m_init = true;
00070   QString keyd = "==";
00071   QString keyt = "==";
00072 
00073   setOn(mTodo->isCompleted());
00074   setText(0,mTodo->summary());
00075   setText(1,QString::number(mTodo->priority()));
00076   setText(2,QString::number(mTodo->percentComplete()));
00077   if (mTodo->percentComplete()<100) {
00078     if (mTodo->isCompleted()) setSortKey(2,QString::number(999));
00079     else setSortKey(2,QString::number(mTodo->percentComplete()));
00080   }
00081   else {
00082     if (mTodo->isCompleted()) setSortKey(2,QString::number(999));
00083     else setSortKey(2,QString::number(99));
00084   }
00085   if (mTodo->hasDueDate()) {
00086     setText(3, mTodo->dtDueDateStr());
00087     QDate d = mTodo->dtDue().date();
00088     keyd.sprintf("%04d%02d%02d",d.year(),d.month(),d.day());
00089     setSortKey(3,keyd);
00090     if (mTodo->doesFloat()) {
00091       setText(4,"");
00092     }
00093     else {
00094       setText(4,mTodo->dtDueTimeStr());
00095       QTime t = mTodo->dtDue().time();
00096       keyt.sprintf("%02d%02d",t.hour(),t.minute());
00097       setSortKey(4,keyt);
00098     }
00099   } else {
00100     setText(3,"");
00101     setText(4,"");
00102   }
00103   setSortKey(3,keyd);
00104   setSortKey(4,keyt);
00105 
00106   QString priorityKey = QString::number( mTodo->priority() ) + keyd + keyt;
00107   if ( mTodo->isCompleted() ) setSortKey( 1, "1" + priorityKey );
00108   else setSortKey( 1, "0" + priorityKey );
00109 
00110   setText(5,mTodo->categoriesStr());
00111 
00112 #if 0
00113   // Find sort id in description. It's the text behind the last '#' character
00114   // found in the description. White spaces are removed from beginning and end
00115   // of sort id.
00116   int pos = mTodo->description().findRev('#');
00117   if (pos < 0) {
00118     setText(6,"");
00119   } else {
00120     QString str = mTodo->description().mid(pos+1);
00121     str.stripWhiteSpace();
00122     setText(6,str);
00123   }
00124 #endif
00125 
00126   m_known = false;
00127   m_init = false;
00128 }
00129 
00130 void KOTodoViewItem::stateChange(bool state)
00131 {
00132   // do not change setting on startup
00133   if ( m_init ) return;
00134 
00135   kdDebug(5850) << "State changed, modified " << state << endl;
00136   QString keyd = "==";
00137   QString keyt = "==";
00138 
00139   Todo*oldTodo = mTodo->clone();
00140 
00141   if (state) mTodo->setCompleted(state);
00142   else mTodo->setPercentComplete(0);
00143   if (isOn()!=state) {
00144     setOn(state);
00145   }
00146 
00147   if (mTodo->hasDueDate()) {
00148     setText(3, mTodo->dtDueDateStr());
00149     QDate d = mTodo->dtDue().date();
00150     keyd.sprintf("%04d%02d%02d",d.year(),d.month(),d.day());
00151     setSortKey(3,keyd);
00152     if (mTodo->doesFloat()) {
00153       setText(4,"");
00154     }
00155     else {
00156       setText(4,mTodo->dtDueTimeStr());
00157       QTime t = mTodo->dtDue().time();
00158       keyt.sprintf("%02d%02d",t.hour(),t.minute());
00159       setSortKey(4,keyt);
00160     }
00161   }
00162 
00163   QString priorityKey = QString::number( mTodo->priority() ) + keyd + keyt;
00164   if ( mTodo->isCompleted() ) setSortKey( 1, "1" + priorityKey );
00165   else setSortKey( 1, "0" + priorityKey );
00166 
00167   setText(2, QString::number(mTodo->percentComplete()));
00168   if (mTodo->percentComplete()<100) {
00169     if (mTodo->isCompleted()) setSortKey(2,QString::number(999));
00170     else setSortKey(2,QString::number(mTodo->percentComplete()));
00171   }
00172   else {
00173     if (mTodo->isCompleted()) setSortKey(2,QString::number(999));
00174     else setSortKey(2,QString::number(99));
00175   }
00176   QListViewItem *myChild = firstChild();
00177   KOTodoViewItem *item;
00178   while( myChild ) {
00179     item = static_cast<KOTodoViewItem*>(myChild);
00180     item->stateChange(state);
00181     myChild = myChild->nextSibling();
00182   }
00183   mTodoView->modified(true);
00184   mTodoView->setTodoModified( oldTodo, mTodo );
00185   delete oldTodo;
00186 }
00187 
00188 bool KOTodoViewItem::isAlternate()
00189 {
00190 #ifndef KORG_NOLVALTERNATION
00191   KOTodoListView *lv = static_cast<KOTodoListView *>(listView());
00192   if (lv && lv->alternateBackground().isValid())
00193   {
00194     KOTodoViewItem *above = 0;
00195     above = dynamic_cast<KOTodoViewItem *>(itemAbove());
00196     m_known = above ? above->m_known : true;
00197     if (m_known)
00198     {
00199        m_odd = above ? !above->m_odd : false;
00200     }
00201     else
00202     {
00203        KOTodoViewItem *item;
00204        bool previous = true;
00205        if (QListViewItem::parent())
00206        {
00207           item = dynamic_cast<KOTodoViewItem *>(QListViewItem::parent());
00208           if (item)
00209              previous = item->m_odd;
00210           item = dynamic_cast<KOTodoViewItem *>(QListViewItem::parent()->firstChild());
00211        }
00212        else
00213        {
00214           item = dynamic_cast<KOTodoViewItem *>(lv->firstChild());
00215        }
00216 
00217        while(item)
00218        {
00219           item->m_odd = previous = !previous;
00220           item->m_known = true;
00221           item = dynamic_cast<KOTodoViewItem *>(item->nextSibling());
00222        }
00223     }
00224     return m_odd;
00225   }
00226   return false;
00227 #else
00228   return false;
00229 #endif
00230 }
00231 
00232 void KOTodoViewItem::paintCell(QPainter *p, const QColorGroup &cg, int column, int width, int alignment)
00233 {
00234   QColorGroup _cg = cg;
00235 #ifndef KORG_NOLVALTERNATION
00236   if (isAlternate())
00237         _cg.setColor(QColorGroup::Base, static_cast< KOTodoListView* >(listView())->alternateBackground());
00238   if (mTodo->hasDueDate()) {
00239     if (mTodo->dtDue().date()==QDate::currentDate() &&
00240         !mTodo->isCompleted()) {
00241       _cg.setColor(QColorGroup::Base, KOPrefs::instance()->mTodoDueTodayColor);
00242       _cg.setColor(QColorGroup::Text, getTextColor(KOPrefs::instance()->mTodoDueTodayColor));
00243     }
00244     if (mTodo->dtDue().date() < QDate::currentDate() &&
00245         !mTodo->isCompleted()) {
00246       _cg.setColor(QColorGroup::Base, KOPrefs::instance()->mTodoOverdueColor);
00247       _cg.setColor(QColorGroup::Text, getTextColor(KOPrefs::instance()->mTodoOverdueColor));
00248     }
00249   }
00250 #endif
00251 
00252   // show the progess by a horizontal bar
00253   if ( column == 2 ) {
00254     p->save();
00255     int progress = (int)(( (width-6)*mTodo->percentComplete())/100.0 + 0.5);
00256 
00257     p->fillRect( 0, 0, width, height(), _cg.base() ); // background
00258     p->setPen( KGlobalSettings::textColor() );  //border
00259     p->setBrush( KGlobalSettings::baseColor() );  //filling
00260     p->drawRect( 2, 2, width-4, height()-4);
00261     p->fillRect( 3, 3, progress, height()-6, 
00262         KGlobalSettings::highlightColor() );
00263     p->restore();
00264   } else {
00265     QCheckListItem::paintCell(p, _cg, column, width, alignment);
00266   }
00267 }
KDE Logo
This file is part of the documentation for korganizer Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat May 1 11:38:31 2004 by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2003