00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
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
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
00151
00152
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
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
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
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() );
00253 p->setPen( KGlobalSettings::textColor() );
00254 p->setBrush( KGlobalSettings::baseColor() );
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 }