korganizer

kotodoviewitem.cpp

00001 /*
00002     This file is part of KOrganizer.
00003 
00004     Copyright (c) 2000,2001 Cornelius Schumacher <schumacher@kde.org>
00005     Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
00006     Copyright (c) 2005 Rafal Rzepecki <divide@users.sourceforge.net>
00007 
00008     This program is free software; you can redistribute it and/or modify
00009     it under the terms of the GNU General Public License as published by
00010     the Free Software Foundation; either version 2 of the License, or
00011     (at your option) any later version.
00012 
00013     This program is distributed in the hope that it will be useful,
00014     but WITHOUT ANY WARRANTY; without even the implied warranty of
00015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016     GNU General Public License for more details.
00017 
00018     You should have received a copy of the GNU General Public License
00019     along with this program; if not, write to the Free Software
00020     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00021 
00022     As a special exception, permission is given to link this program
00023     with any edition of Qt, and distribute the resulting executable,
00024     without including the source code for Qt in the source distribution.
00025 */
00026 
00027 #include <qpainter.h>
00028 
00029 #include <klocale.h>
00030 #include <kdebug.h>
00031 #include <qpainter.h>
00032 #include <qpixmap.h>
00033 
00034 #include "kotodoviewitem.h"
00035 #include "kotodoview.h"
00036 #include "koprefs.h"
00037 #include "koglobals.h"
00038 
00039 KOTodoViewItem::KOTodoViewItem( QListView *parent, Todo *todo, KOTodoView *kotodo)
00040   : QCheckListItem( parent , "", CheckBox ), mTodo( todo ), mTodoView( kotodo )
00041 {
00042   construct();
00043 }
00044 
00045 KOTodoViewItem::KOTodoViewItem( KOTodoViewItem *parent, Todo *todo, KOTodoView *kotodo )
00046   : QCheckListItem( parent, "", CheckBox ), mTodo( todo ), mTodoView( kotodo )
00047 {
00048   construct();
00049 }
00050 
00051 inline int KOTodoViewItem::compareDueDates( const KOTodoViewItem *b ) const
00052 {
00053   if ( mEffectiveDueDate.isValid() && 
00054        !b->mEffectiveDueDate.isValid() )
00055     return -1;
00056   else if ( !mEffectiveDueDate.isValid() &&
00057             b->mEffectiveDueDate.isValid() )
00058     return 1;
00059   else
00060     return b->mEffectiveDueDate.secsTo( mEffectiveDueDate );
00061 }
00062 
00063 int KOTodoViewItem::compare( QListViewItem *it, int col, bool ascending ) const
00064 {
00065   KOTodoViewItem *i = dynamic_cast<KOTodoViewItem *>( it );
00066   if ( !i )
00067     return QListViewItem::compare( it, col, ascending );
00068   
00069   // throw completed todos to the bottom
00070   if ( mTodo->isCompleted() && !i->todo()->isCompleted() )
00071     return ascending ? 1 : -1;
00072   else if ( !mTodo->isCompleted() && i->todo()->isCompleted() )
00073     return ascending ? -1 : 1;
00074   
00075   int c;
00076   switch ( col ) {
00077     case ( KOTodoView::eSummaryColumn ):
00078       return mTodo->summary().localeAwareCompare( i->todo()->summary() );
00079     case ( KOTodoView::eRecurColumn ):
00080       return ( mTodo->doesRecur() ? 1 : 0 ) - (i->todo()->doesRecur() ? 1 : 0 );
00081     case ( KOTodoView::ePriorityColumn ):
00082       c = mTodo->priority() - i->todo()->priority();
00083       if ( c )
00084         return c;
00085       return compareDueDates( i );
00086     case ( KOTodoView::ePercentColumn ):
00087       return mTodo->percentComplete() - i->todo()->percentComplete();
00088     case ( KOTodoView::eDueDateColumn ):
00089       c = compareDueDates( i );
00090       if ( c )
00091         return c;
00092       return mTodo->priority() - i->todo()->priority();
00093     case ( KOTodoView::eCategoriesColumn ):
00094       return mTodo->categoriesStr().localeAwareCompare( 
00095                                                   i->todo()->categoriesStr() );
00096     case ( KOTodoView::eDescriptionColumn ):
00097       return mTodo->description().localeAwareCompare( i->todo()->description() );
00098     default:
00099       Q_ASSERT( false && "unknown column to compare" );
00100       return QListViewItem::compare( it, col, ascending );
00101   }
00102 }
00103 
00104 #if QT_VERSION >= 300
00105 void KOTodoViewItem::paintBranches(QPainter *p,const QColorGroup & cg,int w,
00106                                    int y,int h)
00107 {
00108   QListViewItem::paintBranches(p,cg,w,y,h);
00109 }
00110 #else
00111 #endif
00112 
00113 void KOTodoViewItem::construct()
00114 {
00115   if ( !mTodo ) return;
00116   m_init = true;
00117 
00118   setOn( mTodo->isCompleted() );
00119   setText( KOTodoView::eSummaryColumn, mTodo->summary());
00120   static const QPixmap recurPxmp = KOGlobals::self()->smallIcon("recur");
00121   if ( mTodo->doesRecur() )
00122     setPixmap( KOTodoView::eRecurColumn, recurPxmp );
00123   
00124   if ( mTodo->priority()==0 ) {
00125     setText( KOTodoView::ePriorityColumn, i18n("--") );
00126   } else {
00127     setText( KOTodoView::ePriorityColumn, QString::number(mTodo->priority()) );
00128   }
00129   setText( KOTodoView::ePercentColumn, QString::number(mTodo->percentComplete()) );
00130   
00131   if (mTodo->hasDueDate()) {
00132     QString dtStr = mTodo->dtDueDateStr();
00133     if (!mTodo->doesFloat()) {
00134       dtStr += " " + mTodo->dtDueTimeStr();
00135     }
00136     setText( KOTodoView::eDueDateColumn, dtStr );
00137     mEffectiveDueDate = mTodo->dtDue();
00138     KOTodoViewItem *myParent;
00139     if ( ( myParent = dynamic_cast<KOTodoViewItem *>( parent() ) ) )
00140       if ( !myParent->mEffectiveDueDate.isValid() ||
00141           myParent->mEffectiveDueDate > mEffectiveDueDate ) {
00142         myParent->mEffectiveDueDate = mEffectiveDueDate;
00143       }
00144   } else
00145     setText( KOTodoView::eDueDateColumn, "" );
00146   
00147   setText( KOTodoView::eCategoriesColumn, mTodo->categoriesStr() );
00148 
00149 #if 0
00150   // Find sort id in description. It's the text behind the last '#' character
00151   // found in the description. White spaces are removed from beginning and end
00152   // of sort id.
00153   int pos = mTodo->description().findRev('#');
00154   if (pos < 0) {
00155     setText( KOTodoView::eDescriptionColumn, "" );
00156   } else {
00157     QString str = mTodo->description().mid(pos+1);
00158     str.stripWhiteSpace();
00159     setText( KOTodoView::eDescriptionColumn, str );
00160   }
00161 #endif
00162 
00163   m_known = false;
00164   m_init = false;
00165 }
00166 
00167 void KOTodoViewItem::stateChange( bool state )
00168 {
00169   // do not change setting on startup or if no valid todo item is given
00170   if ( m_init || !mTodo ) return;
00171 
00172   if ( mTodo->isReadOnly() ) {
00173     setOn( mTodo->isCompleted() );
00174     return;
00175   }
00176   
00177   kdDebug(5850) << "State changed, modified " << state << endl;
00178   mTodoView->setNewPercentageDelayed( this, state ? 100 : 0 );
00179 }
00180 
00181 bool KOTodoViewItem::isAlternate()
00182 {
00183 #ifndef KORG_NOLVALTERNATION
00184   KOTodoListView *lv = static_cast<KOTodoListView *>(listView());
00185   if (lv && lv->alternateBackground().isValid())
00186   {
00187     KOTodoViewItem *above = 0;
00188     above = dynamic_cast<KOTodoViewItem *>(itemAbove());
00189     m_known = above ? above->m_known : true;
00190     if (m_known)
00191     {
00192        m_odd = above ? !above->m_odd : false;
00193     }
00194     else
00195     {
00196        KOTodoViewItem *item;
00197        bool previous = true;
00198        if (QListViewItem::parent())
00199        {
00200           item = dynamic_cast<KOTodoViewItem *>(QListViewItem::parent());
00201           if (item)
00202              previous = item->m_odd;
00203           item = dynamic_cast<KOTodoViewItem *>(QListViewItem::parent()->firstChild());
00204        }
00205        else
00206        {
00207           item = dynamic_cast<KOTodoViewItem *>(lv->firstChild());
00208        }
00209 
00210        while(item)
00211        {
00212           item->m_odd = previous = !previous;
00213           item->m_known = true;
00214           item = dynamic_cast<KOTodoViewItem *>(item->nextSibling());
00215        }
00216     }
00217     return m_odd;
00218   }
00219   return false;
00220 #else
00221   return false;
00222 #endif
00223 }
00224 
00225 void KOTodoViewItem::paintCell(QPainter *p, const QColorGroup &cg, int column, int width, int alignment)
00226 {
00227   QColorGroup _cg = cg;
00228   // If no todo is set, just don't paint anything...
00229   if ( !mTodo ) return;
00230 #ifndef KORG_NOLVALTERNATION
00231   if (isAlternate())
00232         _cg.setColor(QColorGroup::Base, static_cast< KOTodoListView* >(listView())->alternateBackground());
00233   if (mTodo->hasDueDate()) {
00234     if (mTodo->dtDue().date()==QDate::currentDate() &&
00235         !mTodo->isCompleted()) {
00236       _cg.setColor(QColorGroup::Base, KOPrefs::instance()->mTodoDueTodayColor);
00237       _cg.setColor(QColorGroup::Text, getTextColor(KOPrefs::instance()->mTodoDueTodayColor));
00238     }
00239     if (mTodo->dtDue().date() < QDate::currentDate() &&
00240         !mTodo->isCompleted()) {
00241       _cg.setColor(QColorGroup::Base, KOPrefs::instance()->mTodoOverdueColor);
00242       _cg.setColor(QColorGroup::Text, getTextColor(KOPrefs::instance()->mTodoOverdueColor));
00243     }
00244   }
00245 #endif
00246 
00247   // show the progess by a horizontal bar
00248   if ( column == KOTodoView::ePercentColumn ) {
00249     p->save();
00250     int progress = (int)(( (width-6)*mTodo->percentComplete())/100.0 + 0.5);
00251 
00252     p->fillRect( 0, 0, width, height(), _cg.base() ); // background
00253     p->setPen( KGlobalSettings::textColor() );  //border
00254     p->setBrush( KGlobalSettings::baseColor() );  //filling
00255     p->drawRect( 2, 2, width-4, height()-4);
00256     p->fillRect( 3, 3, progress, height()-6,
00257         KGlobalSettings::highlightColor() );
00258     p->restore();
00259   } else {
00260     QCheckListItem::paintCell(p, _cg, column, width, alignment);
00261   }
00262 }
KDE Home | KDE Accessibility Home | Description of Access Keys