/
TodoItemModel.py
230 lines (191 loc) · 7.38 KB
/
TodoItemModel.py
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
''' This is the model for the todoList '''
import sys
import time
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from TodoItem import *
from SqlHelper import *
#ID, FINISH, WHAT, WHEN, NOTES, WHERE, REMINDTIME, CONTEXT, \
# FATHERID, ANCESTERS = range(10)
WHAT, WHEN = range(2)
maxtime = 29999999999
APPEND_NEW_ITEM, PREPEND_NEW_iTEM, ADD_AND_SORT_NEW_ITEM = range(3)
class TodoItemModel(QAbstractItemModel):
def __init__(self):
super(TodoItemModel, self).__init__()
self.root = TodoItem(0, 0, 'root', 0, '', '', 0, '', 0, '')
self.root.parent = None
self.headers = ['Task', 'Time']
self.context = set()
self.whereTodo = set()
self.SqlHlp = SqliteHelper()
self.SqlHlp.initDatabase()
def flags(self, index):
'''
Returns the item flags for the given index.
All the items is editable and drag & drop enabled.
'''
if not index.isValid():
return (Qt.ItemIsEnabled | Qt.ItemIsDropEnabled)
if index.column() == WHAT:
return (QAbstractItemModel.flags(self, index)
|Qt.ItemIsEditable|Qt.ItemIsDragEnabled
|Qt.ItemIsDropEnabled|Qt.ItemIsUserCheckable)
return (QAbstractItemModel.flags(self, index)
| Qt.ItemIsEditable | Qt.ItemIsDragEnabled \
| Qt.ItemIsDropEnabled )
def load(self):
items = [TodoItem(*item) for item \
in self.SqlHlp.getAllTodoItems()]
items.sort()
for todoItem in items:
#print todoItem
self.addTodoItemToTree(todoItem)
def addTodoItemToTree(self, todoItem):
'''
Add a todo item into the datas in memor.
Build a tree from the root node.
'''
root = self.root
ancesters = todoItem.getAncestersList()
#print ancesters
if ancesters == [0]:
self.root.addChild(todoItem)
todoItem.parent = self.root
else:
for ancstID in ancesters:
ancst = root.getChildByID(ancstID)
if not ancst:
# The path of ancesters does not exist!
self.root.addChild(todoItem) # Add the item to root
todoItem.fatherID = self.root.ID
todoItem.ancesters = str(self.root.ID)
todoItem.parent = self.root
self.SqlHlp.updateTodoItem(todoItem.ID, \
t_father_id=todoItem.fatherID,\
t_ancesters=todoItem.ancesters)
else:
root = ancst
root.addChild(todoItem)
todoItem.parent = root
def nodeFromIndex(self, index):
return index.internalPointer() \
if index.isValid() else self.root
def index(self, row, column, parent):
assert self.root
father = self.nodeFromIndex(parent)
assert father is not None
return self.createIndex(row, column, \
father.childAtRow(row))
def parent(self, child):
''' If the child is the top level, i.e., its
parent is self.root, return None. '''
child = self.nodeFromIndex(child)
if child is None:
return QModelIndex()
parent = child.parent
if parent is None:
return QModelIndex()
grandparent = parent.parent
if grandparent is None:
return QModelIndex()
row = grandparent.rowOfChild(parent)
assert row != -1
return self.createIndex(row, 0, parent)
def data(self, index, role=Qt.DisplayRole):
item = self.nodeFromIndex(index)
if not item:
return QVariant()
column = index.column()
if role == Qt.DisplayRole:
if column == WHAT:
return QVariant(item.what)
if column == WHEN:
timeFomat = '%Y-%m-%d %H-%m'
sTime = time.strftime(timeFomat, time.gmtime(item.when))
return QVariant(sTime)
if role == Qt.CheckStateRole and column == WHAT:
return item.finish
return QVariant()
def headerData(self, section, orientation, \
role=Qt.DisplayRole):
if orientation == Qt.Horizontal and \
role == Qt.DisplayRole:
assert 0<= section < len(self.headers)
return QVariant(self.headers[section])
return QVariant()
def rowCount(self, index=QModelIndex()):
item = self.nodeFromIndex(index)
if item is None:
return 0
return item.lenOfChildren()
def columnCount(self, index=QModelIndex()):
''' The columns to show are: FINISH, WHAT, WHEN. '''
return len(self.headers)
def setData(self, index, value, role=Qt.EditRole):
''' setData '''
item = self.nodeFromIndex(index)
if not item:
return False
column = index.column()
print 'setData:', str(role), str(index.row()), \
str(index.column()), str(item.what), str(item.finish)
if role == Qt.CheckStateRole and column == WHAT:
item.finish = value.toInt()[0]
if role == Qt.EditRole:
if column == WHAT:
item.what = str(value.toString())
elif column == WHEN:
item.when = value.toInt()
#item.parent.updateOrderKeys()
item.parent.sortChildren()
self.emit(SIGNAL("dataChanged(QModelIndex,QModelIndex)"),
index, index)
datas = item.dataToUpdate()
self.SqlHlp.updateTodoItem(*datas)
#self.reset()
return True
def insertRows(self, position=APPEND_NEW_ITEM, count=1,
parent=QModelIndex()):
''' Used to insert new rows into the tree and the database.
Insert count rows in the parent's positionth row.'''
parentItem = self.nodeFromIndex(parent)
if parentItem == None:
return False
fatherID = parentItem.ID
ancesters = parentItem.ancesters + str(fatherID)
if position == APPEND_NEW_ITEM:
startRow = parentItem.lenOfChildren()
self.beginInsertRows(parent, startRow,
startRow + count - 1)
for row in range(count):
ID = self.SqlHlp.newTodoItem(t_father_id = fatherID, \
t_ancesters = ancesters)
item = TodoItem(ID)
item.parent = parentItem
parentItem.appendNewChild(item)
index = self.createIndex(startRow+row, 0, parentItem)
self.endInsertRows()
return True
def removeRows(self, startRow, count, parent = QModelIndex()):
''' Used to remove specified rows.'''
parentItem = self.nodeFromIndex(parent)
print parentItem, parentItem == None
print startRow, count
print self.root.children
if parentItem == None:
return False
self.beginRemoveRows(parent, startRow, startRow + count - 1)
for row in range(count):
IDs = parentItem.deleteChild(startRow + row)
self.SqlHlp.deleteTodoItems(IDs)
print IDs, 'are deleted.'
self.endRemoveRows()
return True
'''
#def sortByName(self):
'''
if __name__ == '__main__':
model = TodoItemModel()
model.load()
print model.root