1 #ifndef TODOMODEL_H
2 #define TODOMODEL_H
3 
4 #include <QDomNode>
5 #include <QHash>
6 
7 #include <QAbstractItemModel>
8 #include <QSortFilterProxyModel>
9 #include <QDomDocument>
10 #include <QModelIndex>
11 #include <QDateTime>
12 #include <QVariant>
13 #include <QFile>
14 
15 #include <QItemDelegate>
16 
17 class Task
18 {
19 public:
20 	Task(QDomDocument* document, QDomNode &node, int row, Task *parent = 0);
21 	~Task();
22 
23 	enum Priority
24 	{
25 		medium, low, high
26 	};
27 
subtasks()28 	inline const QList<Task*>& subtasks() const { return _subtasks; }
parent()29 	inline Task* parent() const { return _parent; }
row()30 	inline int row() const { return _row; };
31 
node()32 	inline const QDomNode& node() const { return _node; }
33 
title()34 	inline const QString& title() const { return _title; }
comment()35 	inline const QString& comment() const { return _comment; }
dateStart()36 	inline const QDateTime& dateStart() const { return _date_start; }
dateStop()37 	inline const QDateTime& dateStop() const { return _date_stop; }
dateLimit()38 	inline const QDateTime& dateLimit() const { return _date_limit; }
done()39 	inline bool done() const { return _done; }
limited()40 	inline bool limited() const { return !_date_limit.isNull(); }
priority()41 	inline Priority priority() const { return _priority; }
42 
43 	void setTitle(const QString& v);
44 	void setComment(const QString& v);
45 	void setDateStart(const QDateTime& v);
46 	void setDateStop(const QDateTime& v);
47 	void setDateLimit(const QDateTime& v);
48 	void setDone(bool v);
49 	void setPriority(Priority v);
50 
51 	void insertSubTask(int pos=-1);
52 	void removeSubTask(int pos);
53 
54 private:
55 	QDomDocument* _document;
56 
57 	QDomNode _node;
58 	int _row;
59 	Task* _parent;
60 	QList<Task*> _subtasks;
61 
62 	QString _title;
63 	QString _comment;
64 	QDateTime _date_start, _date_stop, _date_limit;
65 	Priority _priority;
66 	bool _done;
67 };
68 
69 class TodoModel : public QAbstractItemModel
70 {
71 	Q_OBJECT
72 
73 public:
74 	TodoModel(QObject *parent = 0);
75 	~TodoModel();
76 
77 	QVariant data(const QModelIndex& index, int role) const;
78 	bool setData(const QModelIndex& index, const QVariant& data, int role);
79 	Qt::ItemFlags flags(const QModelIndex &index) const;
80 	QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
81 	QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
82 	QModelIndex parent(const QModelIndex &child) const;
83 	int rowCount(const QModelIndex &parent = QModelIndex()) const;
84 	int columnCount(const QModelIndex &parent = QModelIndex()) const;
85 
86 	QDomDocument* load(QFile& file);
87 
88 	bool insertRows(int row, int count, const QModelIndex& parent = QModelIndex());
89 	bool removeRows(int row, int count, const QModelIndex& parent = QModelIndex());
90 
91 	Qt::DropActions supportedDropActions() const;
92 	QStringList mimeTypes() const;
93 	QMimeData* mimeData(const QModelIndexList& indexes) const;
94 	bool dropMimeData(const QMimeData* data, Qt::DropAction action, int row, int column, const QModelIndex& parent);
95 
96 	Task* getTask(const QModelIndex &index) const;
97 
98 private:
99 	QDomDocument* _document;
100 	Task* _root_task;
101 };
102 
103 class TodoProxyModel : public QSortFilterProxyModel
104 {
105 	Q_OBJECT
106 public:
107 	TodoProxyModel();
108 	bool filterAcceptsRow ( int source_row, const QModelIndex & source_parent ) const;
109 	void hideDoneTasks(bool hide = true);
110 private:
111 	bool hide_done_tasks;
112 };
113 
114 #endif // TODOMODEL_H
115