summaryrefslogtreecommitdiff
path: root/core/pim/todo/todotable.h
blob: 39e00d1d6fb17ab690bb2fd7175f3917e02d72cd (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
/**********************************************************************
** Copyright (C) 2000 Trolltech AS.  All rights reserved.
**
** This file is part of Qtopia Environment.
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.trolltech.com/gpl/ for GPL licensing information.
**
** Contact info@trolltech.com if any conditions of this licensing are
** not clear to you.
**
**********************************************************************/

#ifndef TODOTABLE_H
#define TODOTABLE_H

#include <qpe/categories.h>
#include <qpe/stringutil.h>
//#include <qpe/task.h>
#include <opie/todoevent.h>

#include <qtable.h>
#include <qmap.h>
#include <qguardedptr.h>

class Node;
class QComboBox;
class QTimer;

class CheckItem : public QTableItem
{
public:
    CheckItem( QTable *t, const QString &sortkey );

    void setChecked( bool b );
    void toggle();
    bool isChecked() const;
    void setKey( const QString &key ) { sortKey = key; }
    QString key() const;

    void paint( QPainter *p, const QColorGroup &cg, const QRect &cr, bool selected );

private:
    bool checked;
    QString sortKey;
};

class ComboItem : public QTableItem
{
public:
    ComboItem( QTable *t, EditType et );
    QWidget *createEditor() const;
    void setContentFromEditor( QWidget *w );
    void setText( const QString &s );
    int alignment() const { return Qt::AlignCenter; }

    QString text() const;

private:
    QGuardedPtr<QComboBox> cb;

};

class TodoTextItem : public QTableItem
{
public:
    TodoTextItem( QTable *t, const QString & str ) 
	:QTableItem( t, QTableItem::Never, str ) {}

    QString key () const { return Qtopia::buildSortKey( text() ); }
};

class DueTextItem : public QTableItem
{
 public:
  DueTextItem( QTable *t, ToDoEvent *ev );
  QString key() const;
  void setToDoEvent( const ToDoEvent *ev );
  void paint( QPainter *p, const QColorGroup &cg, const QRect &cr, bool selected );
 private:
  int m_off;
  bool m_hasDate:1;
  bool m_completed:1;

};


enum journal_action { ACTION_ADD=0, ACTION_REMOVE, ACTION_REPLACE };

namespace Opie
{
class XMLElement;
};

class TodoTable : public QTable
{
    Q_OBJECT

public:
    TodoTable( QWidget *parent = 0, const char * name = 0 );
    void addEntry( const ToDoEvent &todo );
    void clearFindRow() { currFindRow = -2; }

    ToDoEvent currentEntry() const;
    void replaceCurrentEntry( const ToDoEvent &todo, bool fromTableItem = false );

    QStringList categories();

    void setShowCompleted( bool sc ) { showComp = sc; updateVisible(); }
    bool showCompleted() const { return showComp; }

    void setShowDeadline (bool sd) {showDeadl = sd; updateVisible();}
    bool showDeadline() const { return showDeadl;}

    void setShowCategory( const QString &c ) { showCat = c; updateVisible(); }
    const QString &showCategory() const { return showCat; }
    int showCategoryId() const;

    bool save( const QString &fn );
    void load( const QString &fn );
    void applyJournal( );
    void clear();
    void removeCurrentEntry();
    void removeAllEntries() { clear(); };
    //void removeAllEntriesInCategory(const QString &category );

    void setPaintingEnabled( bool e );

    virtual void sortColumn( int col, bool ascending, bool /*wholeRows*/ );

//     int rowHeight( int ) const;
//     int rowPos( int row ) const;
//     virtual int rowAt( int pos ) const;

signals:
    void signalEdit();
    void signalDoneChanged( bool b );
    void signalPriorityChanged( int i );
    void signalShowMenu( const QPoint & );
    void signalNotFound();
    void signalWrapAround();
    void showDetails( const ToDoEvent & );
protected:
    void keyPressEvent( QKeyEvent *e );

private:
    void updateVisible();
    void viewportPaintEvent( QPaintEvent * );
    void internalAddEntries( QList<ToDoEvent> &list);
    inline void insertIntoTable( ToDoEvent *todo, int row );
    void updateJournal( const ToDoEvent &todo, journal_action action);
    void mergeJournal();
    void journalFreeReplaceEntry( const ToDoEvent &todo, int row );
    void journalFreeRemoveEntry( int row );
    inline void realignTable( int row );
    void loadFile( const QString &strFile);

private slots:
    void slotClicked( int row, int col, int button, const QPoint &pos );
    void slotPressed( int row, int col, int button, const QPoint &pos );
    void slotCheckPriority(int row, int col );
    void slotCurrentChanged(int row, int col );
    void slotDoFind( const QString &findString, bool caseSensetive,
		     bool backwards, int category );
    void slotShowMenu();
    void rowHeightChanged( int row );
    void slotCheckDay(); // check the day

private:
    friend class TodoWindow;

    QMap<CheckItem*, ToDoEvent *> todoList;
    QStringList categoryList;
    bool showComp;
    QString showCat;
    QTimer *menuTimer;
    QDate mDay;
    QTimer *mDayTimer; // see if the day changed
    bool enablePainting;
    Categories mCat;
    int currFindRow;
    bool showDeadl:1;
};


inline void TodoTable::insertIntoTable( ToDoEvent *todo, int row )
{
    QString sortKey = (char) ((todo->isCompleted() ? 'a' : 'A') 
			      + todo->priority() ) 
		      + Qtopia::buildSortKey( todo->description() );
    CheckItem *chk = new CheckItem( this, sortKey );
    chk->setChecked( todo->isCompleted() );
    ComboItem *cmb = new ComboItem( this, QTableItem::WhenCurrent );
    cmb->setText( QString::number( todo->priority() ) );
    QString sum = todo->summary();
    QTableItem *ti = new TodoTextItem( this, sum.isEmpty() ? todo->description().left(40).simplifyWhiteSpace() : sum );
    ti->setReplaceable( false );

    DueTextItem *due = new DueTextItem(this, todo );
    setItem( row, 3, due);
    
    setItem( row, 0, chk );
    setItem( row, 1, cmb );
    setItem( row, 2, ti );


    todoList.insert( chk, todo );
}

inline void TodoTable::realignTable( int row )
{
  QTableItem *ti1,
    *ti2,
    *ti3,
    *ti4;
    int totalRows = numRows();
    for ( int curr = row; curr < totalRows - 1; curr++ ) {
	// this is bad, we must take the item out and then
	// set it. In the end, it behaves no worse (time wise)
	// then the old way of saving the entries to file, clearing
	// the table re-reading in the file and resetting the table
	ti1 = item( curr + 1, 0 );
	ti2 = item( curr + 1, 1 );
	ti3 = item( curr + 1, 2 );
	ti4 = item( curr + 1, 3 );
	takeItem( ti1 );
	takeItem( ti2 );
	takeItem( ti3 );
	takeItem( ti4 );
	setItem( curr, 0, ti1 );
	setItem( curr, 1, ti2 );
	setItem( curr, 2, ti3 );
	setItem( curr, 3, ti4 );
    }
    setNumRows( totalRows - 1 );
}

#endif