korganizer

kotodoviewitem.cpp
1 /*
2  This file is part of KOrganizer.
3 
4  Copyright (c) 2000,2001 Cornelius Schumacher <schumacher@kde.org>
5  Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
6  Copyright (c) 2005 Rafal Rzepecki <divide@users.sourceforge.net>
7 
8  This program is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 2 of the License, or
11  (at your option) any later version.
12 
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 
22  As a special exception, permission is given to link this program
23  with any edition of TQt, and distribute the resulting executable,
24  without including the source code for TQt in the source distribution.
25 */
26 
27 #include "kotodoviewitem.h"
28 #include "kotodoview.h"
29 #include "koprefs.h"
30 #include "koglobals.h"
31 
32 #include <libkcal/incidenceformatter.h>
33 
34 #include <tdelocale.h>
35 #include <kdebug.h>
36 #include <tqpainter.h>
37 #include <tqpixmap.h>
38 
39 #include <tqpainter.h>
40 
41 KOTodoViewItem::KOTodoViewItem( TQListView *parent, Todo *todo, KOTodoView *kotodo)
42  : TQCheckListItem( parent , "", CheckBox ), mTodo( todo ), mTodoView( kotodo )
43 {
44  construct();
45 }
46 
48  : TQCheckListItem( parent, "", CheckBox ), mTodo( todo ), mTodoView( kotodo )
49 {
50  construct();
51 }
52 
53 inline int KOTodoViewItem::compareDueDates( const KOTodoViewItem *b ) const
54 {
55  if ( mEffectiveDueDate.isValid() &&
56  !b->mEffectiveDueDate.isValid() )
57  return -1;
58  else if ( !mEffectiveDueDate.isValid() &&
59  b->mEffectiveDueDate.isValid() )
60  return 1;
61  else
62  return b->mEffectiveDueDate.secsTo( mEffectiveDueDate );
63 }
64 
65 int KOTodoViewItem::compare( TQListViewItem *it, int col, bool ascending ) const
66 {
67  KOTodoViewItem *i = dynamic_cast<KOTodoViewItem *>( it );
68  if ( !i ) {
69  return TQListViewItem::compare( it, col, ascending );
70  }
71 
72  // throw completed todos to the bottom
73  if ( mTodo->isCompleted() && !i->todo()->isCompleted() ) {
74  return ascending ? 1 : -1;
75  } else if ( !mTodo->isCompleted() && i->todo()->isCompleted() ) {
76  return ascending ? -1 : 1;
77  }
78 
79  int c;
80  switch ( col ) {
81  case KOTodoView::eSummaryColumn:
82  return mTodo->summary().localeAwareCompare( i->todo()->summary() );
83 
84  case KOTodoView::eRecurColumn:
85  return ( mTodo->doesRecur() ? 1 : 0 ) - ( i->todo()->doesRecur() ? 1 : 0 );
86 
87  case KOTodoView::ePriorityColumn:
88  c = mTodo->priority() - i->todo()->priority();
89  if ( c ) {
90  return c;
91  } else {
92  return compareDueDates( i );
93  }
94 
95  case KOTodoView::ePercentColumn:
96  return mTodo->percentComplete() - i->todo()->percentComplete();
97 
98  case KOTodoView::eDueDateColumn:
99  c = compareDueDates( i );
100  if ( c ) {
101  return c;
102  } else {
103  return mTodo->priority() - i->todo()->priority();
104  }
105  case KOTodoView::eCategoriesColumn:
106  return mTodo->categoriesStr().localeAwareCompare( i->todo()->categoriesStr() );
107 
108  case KOTodoView::eFolderColumn:
109  return TQListViewItem::compare( it, col, ascending );
110 
111 #if 0
112  case KOTodoView::eDescriptionColumn:
113  return mTodo->description().localeAwareCompare( i->todo()->description() );
114 #endif
115 
116  default:
117  Q_ASSERT( false && "unknown column to compare" );
118  return TQListViewItem::compare( it, col, ascending );
119  }
120 }
121 
122 void KOTodoViewItem::paintBranches(TQPainter *p,const TQColorGroup & cg,int w,
123  int y,int h)
124 {
125  TQListViewItem::paintBranches(p,cg,w,y,h);
126 }
127 
128 void KOTodoViewItem::construct()
129 {
130  if ( !mTodo ) return;
131  m_init = true;
132 
133  setOn( mTodo->isCompleted() );
134  setText( KOTodoView::eSummaryColumn, mTodo->summary());
135  static const TQPixmap recurPxmp = KOGlobals::self()->smallIcon("recur");
136  if ( mTodo->doesRecur() )
137  setPixmap( KOTodoView::eRecurColumn, recurPxmp );
138 
139  if ( mTodo->priority()==0 ) {
140  setText( KOTodoView::ePriorityColumn, i18n("--") );
141  } else {
142  setText( KOTodoView::ePriorityColumn, TQString::number(mTodo->priority()) );
143  }
144  setText( KOTodoView::ePercentColumn, TQString::number(mTodo->percentComplete()) );
145 
146  if (mTodo->hasDueDate()) {
147  TQString dtStr = mTodo->dtDueDateStr();
148  if (!mTodo->doesFloat()) {
149  dtStr += " " + mTodo->dtDueTimeStr();
150  }
151  setText( KOTodoView::eDueDateColumn, dtStr );
152  mEffectiveDueDate = mTodo->dtDue();
153  KOTodoViewItem *myParent;
154  if ( ( myParent = dynamic_cast<KOTodoViewItem *>( parent() ) ) )
155  if ( !myParent->mEffectiveDueDate.isValid() ||
156  myParent->mEffectiveDueDate > mEffectiveDueDate ) {
157  myParent->mEffectiveDueDate = mEffectiveDueDate;
158  }
159  } else
160  setText( KOTodoView::eDueDateColumn, "" );
161 
162  setText( KOTodoView::eCategoriesColumn, mTodo->categoriesStr() );
163 
164  setText( KOTodoView::eFolderColumn,
165  IncidenceFormatter::resourceString( mTodoView->calendar(), mTodo ) );
166 
167 #if 0
168  // Find sort id in description. It's the text behind the last '#' character
169  // found in the description. White spaces are removed from beginning and end
170  // of sort id.
171  int pos = mTodo->description().findRev('#');
172  if (pos < 0) {
173  setText( KOTodoView::eDescriptionColumn, "" );
174  } else {
175  TQString str = mTodo->description().mid(pos+1);
176  str.stripWhiteSpace();
177  setText( KOTodoView::eDescriptionColumn, str );
178  }
179 #endif
180 
181  m_known = false;
182  m_init = false;
183 }
184 
185 void KOTodoViewItem::stateChange( bool state )
186 {
187  // do not change setting on startup or if no valid todo item is given
188  if ( m_init || !mTodo ) return;
189 
190  if ( mTodo->isReadOnly() ) {
191  setOn( mTodo->isCompleted() );
192  return;
193  }
194 
195  kdDebug(5850) << "State changed, modified " << state << endl;
196  mTodoView->setNewPercentageDelayed( this, state ? 100 : 0 );
197 }
198 
199 bool KOTodoViewItem::isAlternate()
200 {
201 #ifndef KORG_NOLVALTERNATION
202  KOTodoListView *lv = static_cast<KOTodoListView *>(listView());
203  if (lv && lv->alternateBackground().isValid())
204  {
205  KOTodoViewItem *above = 0;
206  above = dynamic_cast<KOTodoViewItem *>(itemAbove());
207  m_known = above ? above->m_known : true;
208  if (m_known)
209  {
210  m_odd = above ? !above->m_odd : false;
211  }
212  else
213  {
214  KOTodoViewItem *item;
215  bool previous = true;
216  if (TQListViewItem::parent())
217  {
218  item = dynamic_cast<KOTodoViewItem *>(TQListViewItem::parent());
219  if (item)
220  previous = item->m_odd;
221  item = dynamic_cast<KOTodoViewItem *>(TQListViewItem::parent()->firstChild());
222  }
223  else
224  {
225  item = dynamic_cast<KOTodoViewItem *>(lv->firstChild());
226  }
227 
228  while(item)
229  {
230  item->m_odd = previous = !previous;
231  item->m_known = true;
232  item = dynamic_cast<KOTodoViewItem *>(item->nextSibling());
233  }
234  }
235  return m_odd;
236  }
237  return false;
238 #else
239  return false;
240 #endif
241 }
242 
243 void KOTodoViewItem::paintCell(TQPainter *p, const TQColorGroup &cg, int column, int width, int alignment)
244 {
245  TQColorGroup _cg = cg;
246  // If no todo is set, just don't paint anything...
247  if ( !mTodo ) return;
248 #ifndef KORG_NOLVALTERNATION
249  if (isAlternate())
250  _cg.setColor(TQColorGroup::Base, static_cast< KOTodoListView* >(listView())->alternateBackground());
251  if (mTodo->hasDueDate()) {
252  if (mTodo->dtDue().date()==TQDate::currentDate() &&
253  !mTodo->isCompleted()) {
254  _cg.setColor(TQColorGroup::Base, KOPrefs::instance()->mTodoDueTodayColor);
255  _cg.setColor(TQColorGroup::Text, getTextColor(KOPrefs::instance()->mTodoDueTodayColor));
256  }
257  if (mTodo->dtDue().date() < TQDate::currentDate() &&
258  !mTodo->isCompleted()) {
259  _cg.setColor(TQColorGroup::Base, KOPrefs::instance()->mTodoOverdueColor);
260  _cg.setColor(TQColorGroup::Text, getTextColor(KOPrefs::instance()->mTodoOverdueColor));
261  }
262  }
263 #endif
264 
265  // show the progess by a horizontal bar
266  if ( column == KOTodoView::ePercentColumn ) {
267  p->save();
268  int progress = (int)(( (width-6)*mTodo->percentComplete())/100.0 + 0.5);
269 
270  p->fillRect( 0, 0, width, height(), _cg.base() ); // background
271  p->setPen( TDEGlobalSettings::textColor() ); //border
272  p->setBrush( TDEGlobalSettings::baseColor() ); //filling
273  p->drawRect( 2, 2, width-4, height()-4);
274  p->fillRect( 3, 3, progress, height()-6,
275  TDEGlobalSettings::highlightColor() );
276  p->restore();
277  } else {
278  TQCheckListItem::paintCell(p, _cg, column, width, alignment);
279  }
280 }
bool doesFloat() const
bool isReadOnly() const
TQString description() const
int priority() const
bool doesRecur() const
TQString categoriesStr() const
TQString summary() const
bool hasDueDate() const
bool isCompleted() const
TDE_DEPRECATED TQString dtDueTimeStr() const
TDE_DEPRECATED TQString dtDueDateStr(bool shortfmt=true) const
int percentComplete() const
TQDateTime dtDue(bool first=false) const
This class provides a way of displaying a single Event of Todo-Type in a KTodoView.
KOTodoViewItem(TQListView *parent, Todo *todo, KOTodoView *kotodo)
Constructor.
This class provides a multi-column list view of todo events.
Definition: kotodoview.h:114
virtual Calendar * calendar()
Return calendar object of this view.
Definition: baseview.h:89