summaryrefslogtreecommitdiffstats
path: root/korganizer/kotodoviewitem.cpp
blob: 02369d91a914453df44d1ff0d1ae581330fe3bee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
    This file is part of KOrganizer.

    Copyright (c) 2000,2001 Cornelius Schumacher <[email protected]>
    Copyright (C) 2003-2004 Reinhold Kainhofer <[email protected]>
    Copyright (c) 2005 Rafal Rzepecki <[email protected]>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

    As a special exception, permission is given to link this program
    with any edition of TQt, and distribute the resulting executable,
    without including the source code for TQt in the source distribution.
*/

#include "kotodoviewitem.h"
#include "kotodoview.h"
#include "koprefs.h"
#include "koglobals.h"

#include <libkcal/incidenceformatter.h>

#include <klocale.h>
#include <kdebug.h>
#include <tqpainter.h>
#include <tqpixmap.h>

#include <tqpainter.h>

KOTodoViewItem::KOTodoViewItem( TQListView *tqparent, Todo *todo, KOTodoView *kotodo)
  : TQCheckListItem( tqparent , "", CheckBox ), mTodo( todo ), mTodoView( kotodo )
{
  construct();
}

KOTodoViewItem::KOTodoViewItem( KOTodoViewItem *tqparent, Todo *todo, KOTodoView *kotodo )
  : TQCheckListItem( tqparent, "", CheckBox ), mTodo( todo ), mTodoView( kotodo )
{
  construct();
}

inline int KOTodoViewItem::compareDueDates( const KOTodoViewItem *b ) const
{
  if ( mEffectiveDueDate.isValid() &&
       !b->mEffectiveDueDate.isValid() )
    return -1;
  else if ( !mEffectiveDueDate.isValid() &&
            b->mEffectiveDueDate.isValid() )
    return 1;
  else
    return b->mEffectiveDueDate.secsTo( mEffectiveDueDate );
}

int KOTodoViewItem::compare( TQListViewItem *it, int col, bool ascending ) const
{
  KOTodoViewItem *i = dynamic_cast<KOTodoViewItem *>( it );
  if ( !i ) {
    return TQListViewItem::compare( it, col, ascending );
  }

  // throw completed todos to the bottom
  if ( mTodo->isCompleted() && !i->todo()->isCompleted() ) {
    return ascending ? 1 : -1;
  } else if ( !mTodo->isCompleted() && i->todo()->isCompleted() ) {
    return ascending ? -1 : 1;
  }

  int c;
  switch ( col ) {
  case KOTodoView::eSummaryColumn:
    return mTodo->summary().localeAwareCompare( i->todo()->summary() );

  case KOTodoView::eRecurColumn:
    return ( mTodo->doesRecur() ? 1 : 0 ) - ( i->todo()->doesRecur() ? 1 : 0 );

  case KOTodoView::ePriorityColumn:
    c = mTodo->priority() - i->todo()->priority();
    if ( c ) {
      return c;
    } else {
      return compareDueDates( i );
    }

  case KOTodoView::ePercentColumn:
    return mTodo->percentComplete() - i->todo()->percentComplete();

  case KOTodoView::eDueDateColumn:
    c = compareDueDates( i );
    if ( c ) {
      return c;
    } else {
      return mTodo->priority() - i->todo()->priority();
    }
  case KOTodoView::eCategoriesColumn:
    return mTodo->categoriesStr().localeAwareCompare( i->todo()->categoriesStr() );

  case KOTodoView::eFolderColumn:
    return TQListViewItem::compare( it, col, ascending );

#if 0
  case KOTodoView::eDescriptionColumn:
    return mTodo->description().localeAwareCompare( i->todo()->description() );
#endif

  default:
    Q_ASSERT( false && "unknown column to compare" );
    return TQListViewItem::compare( it, col, ascending );
  }
}

void KOTodoViewItem::paintBranches(TQPainter *p,const TQColorGroup & cg,int w,
                                   int y,int h)
{
  TQListViewItem::paintBranches(p,cg,w,y,h);
}

void KOTodoViewItem::construct()
{
  if ( !mTodo ) return;
  m_init = true;

  setOn( mTodo->isCompleted() );
  setText( KOTodoView::eSummaryColumn, mTodo->summary());
  static const TQPixmap recurPxmp = KOGlobals::self()->smallIcon("recur");
  if ( mTodo->doesRecur() )
    setPixmap( KOTodoView::eRecurColumn, recurPxmp );

  if ( mTodo->priority()==0 ) {
    setText( KOTodoView::ePriorityColumn, i18n("--") );
  } else {
    setText( KOTodoView::ePriorityColumn, TQString::number(mTodo->priority()) );
  }
  setText( KOTodoView::ePercentColumn, TQString::number(mTodo->percentComplete()) );

  if (mTodo->hasDueDate()) {
    TQString dtStr = mTodo->dtDueDateStr();
    if (!mTodo->doesFloat()) {
      dtStr += " " + mTodo->dtDueTimeStr();
    }
    setText( KOTodoView::eDueDateColumn, dtStr );
    mEffectiveDueDate = mTodo->dtDue();
    KOTodoViewItem *myParent;
    if ( ( myParent = dynamic_cast<KOTodoViewItem *>( tqparent() ) ) )
      if ( !myParent->mEffectiveDueDate.isValid() ||
          myParent->mEffectiveDueDate > mEffectiveDueDate ) {
        myParent->mEffectiveDueDate = mEffectiveDueDate;
      }
  } else
    setText( KOTodoView::eDueDateColumn, "" );

  setText( KOTodoView::eCategoriesColumn, mTodo->categoriesStr() );

  setText( KOTodoView::eFolderColumn,
           IncidenceFormatter::resourceString( mTodoView->calendar(), mTodo ) );

#if 0
  // Find sort id in description. It's the text behind the last '#' character
  // found in the description. White spaces are removed from beginning and end
  // of sort id.
  int pos = mTodo->description().findRev('#');
  if (pos < 0) {
    setText( KOTodoView::eDescriptionColumn, "" );
  } else {
    TQString str = mTodo->description().mid(pos+1);
    str.stripWhiteSpace();
    setText( KOTodoView::eDescriptionColumn, str );
  }
#endif

  m_known = false;
  m_init = false;
}

void KOTodoViewItem::stateChange( bool state )
{
  // do not change setting on startup or if no valid todo item is given
  if ( m_init || !mTodo ) return;

  if ( mTodo->isReadOnly() ) {
    setOn( mTodo->isCompleted() );
    return;
  }

  kdDebug(5850) << "State changed, modified " << state << endl;
  mTodoView->setNewPercentageDelayed( this, state ? 100 : 0 );
}

bool KOTodoViewItem::isAlternate()
{
#ifndef KORG_NOLVALTERNATION
  KOTodoListView *lv = static_cast<KOTodoListView *>(listView());
  if (lv && lv->alternateBackground().isValid())
  {
    KOTodoViewItem *above = 0;
    above = dynamic_cast<KOTodoViewItem *>(itemAbove());
    m_known = above ? above->m_known : true;
    if (m_known)
    {
       m_odd = above ? !above->m_odd : false;
    }
    else
    {
       KOTodoViewItem *item;
       bool previous = true;
       if (TQListViewItem::tqparent())
       {
          item = dynamic_cast<KOTodoViewItem *>(TQListViewItem::tqparent());
          if (item)
             previous = item->m_odd;
          item = dynamic_cast<KOTodoViewItem *>(TQListViewItem::tqparent()->firstChild());
       }
       else
       {
          item = dynamic_cast<KOTodoViewItem *>(lv->firstChild());
       }

       while(item)
       {
          item->m_odd = previous = !previous;
          item->m_known = true;
          item = dynamic_cast<KOTodoViewItem *>(item->nextSibling());
       }
    }
    return m_odd;
  }
  return false;
#else
  return false;
#endif
}

void KOTodoViewItem::paintCell(TQPainter *p, const TQColorGroup &cg, int column, int width, int tqalignment)
{
  TQColorGroup _cg = cg;
  // If no todo is set, just don't paint anything...
  if ( !mTodo ) return;
#ifndef KORG_NOLVALTERNATION
  if (isAlternate())
        _cg.setColor(TQColorGroup::Base, static_cast< KOTodoListView* >(listView())->alternateBackground());
  if (mTodo->hasDueDate()) {
    if (mTodo->dtDue().date()==TQDate::tqcurrentDate() &&
        !mTodo->isCompleted()) {
      _cg.setColor(TQColorGroup::Base, KOPrefs::instance()->mTodoDueTodayColor);
      _cg.setColor(TQColorGroup::Text, getTextColor(KOPrefs::instance()->mTodoDueTodayColor));
    }
    if (mTodo->dtDue().date() < TQDate::tqcurrentDate() &&
        !mTodo->isCompleted()) {
      _cg.setColor(TQColorGroup::Base, KOPrefs::instance()->mTodoOverdueColor);
      _cg.setColor(TQColorGroup::Text, getTextColor(KOPrefs::instance()->mTodoOverdueColor));
    }
  }
#endif

  // show the progess by a horizontal bar
  if ( column == KOTodoView::ePercentColumn ) {
    p->save();
    int progress = (int)(( (width-6)*mTodo->percentComplete())/100.0 + 0.5);

    p->fillRect( 0, 0, width, height(), _cg.base() ); // background
    p->setPen( KGlobalSettings::textColor() );  //border
    p->setBrush( KGlobalSettings::baseColor() );  //filling
    p->drawRect( 2, 2, width-4, height()-4);
    p->fillRect( 3, 3, progress, height()-6,
        KGlobalSettings::highlightColor() );
    p->restore();
  } else {
    TQCheckListItem::paintCell(p, _cg, column, width, tqalignment);
  }
}