|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2005 - 2019 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a class to store task data. |
|
8 """ |
|
9 |
|
10 from __future__ import unicode_literals |
|
11 |
|
12 import os |
|
13 import time |
|
14 |
|
15 from PyQt5.QtCore import Qt, QUuid |
|
16 from PyQt5.QtWidgets import QTreeWidgetItem |
|
17 |
|
18 import UI.PixmapCache |
|
19 import Preferences |
|
20 |
|
21 |
|
22 class Task(QTreeWidgetItem): |
|
23 """ |
|
24 Class implementing the task data structure. |
|
25 """ |
|
26 TypeNone = -1 |
|
27 TypeFixme = 0 |
|
28 TypeTodo = 1 |
|
29 TypeWarning = 2 |
|
30 TypeNote = 3 |
|
31 |
|
32 def __init__(self, summary, priority=1, filename="", lineno=0, |
|
33 completed=False, _time=0, isProjectTask=False, |
|
34 taskType=TypeTodo, project=None, description="", |
|
35 uid="", parentUid=""): |
|
36 """ |
|
37 Constructor |
|
38 |
|
39 @param summary summary text of the task (string) |
|
40 @param priority priority of the task (0=high, 1=normal, 2=low) |
|
41 @param filename filename containing the task (string) |
|
42 @param lineno line number containing the task (integer) |
|
43 @param completed flag indicating completion status (boolean) |
|
44 @param _time creation time of the task (float, if 0 use current time) |
|
45 @param isProjectTask flag indicating a task related to the current |
|
46 project (boolean) |
|
47 @param taskType type of the task (one of TypeFixme, TypeTodo, |
|
48 TypeWarning, TypeNote) |
|
49 @param project reference to the project object (Project) |
|
50 @param description explanatory text of the task (string) |
|
51 @param uid unique id of the task (string) |
|
52 @param parentUid unique id of the parent task (string) |
|
53 """ |
|
54 super(Task, self).__init__() |
|
55 |
|
56 self.summary = summary |
|
57 self.description = description |
|
58 if priority in [0, 1, 2]: |
|
59 self.priority = priority |
|
60 else: |
|
61 self.priority = 1 |
|
62 self.filename = filename |
|
63 self.lineno = lineno |
|
64 self.completed = completed |
|
65 self.created = _time and _time or time.time() |
|
66 self._isProjectTask = isProjectTask |
|
67 self.taskType = taskType |
|
68 self.project = project |
|
69 if uid: |
|
70 self.uid = uid |
|
71 else: |
|
72 self.uid = QUuid.createUuid().toString() |
|
73 self.parentUid = parentUid |
|
74 |
|
75 if isProjectTask: |
|
76 self.filename = self.project.getRelativePath(self.filename) |
|
77 |
|
78 self.setData(0, Qt.DisplayRole, "") |
|
79 self.setData(1, Qt.DisplayRole, "") |
|
80 self.setData(2, Qt.DisplayRole, self.summary) |
|
81 self.setData(3, Qt.DisplayRole, self.filename) |
|
82 self.setData(4, Qt.DisplayRole, self.lineno or "") |
|
83 |
|
84 if self.completed: |
|
85 self.setIcon(0, UI.PixmapCache.getIcon("taskCompleted.png")) |
|
86 strikeOut = True |
|
87 else: |
|
88 self.setIcon(0, UI.PixmapCache.getIcon("empty.png")) |
|
89 strikeOut = False |
|
90 for column in range(2, 5): |
|
91 f = self.font(column) |
|
92 f.setStrikeOut(strikeOut) |
|
93 self.setFont(column, f) |
|
94 |
|
95 if self.priority == 1: |
|
96 self.setIcon(1, UI.PixmapCache.getIcon("empty.png")) |
|
97 elif self.priority == 0: |
|
98 self.setIcon(1, UI.PixmapCache.getIcon("taskPrioHigh.png")) |
|
99 elif self.priority == 2: |
|
100 self.setIcon(1, UI.PixmapCache.getIcon("taskPrioLow.png")) |
|
101 else: |
|
102 self.setIcon(1, UI.PixmapCache.getIcon("empty.png")) |
|
103 |
|
104 if self.taskType == Task.TypeFixme: |
|
105 self.setIcon(2, UI.PixmapCache.getIcon("taskFixme.png")) |
|
106 elif self.taskType == Task.TypeWarning: |
|
107 self.setIcon(2, UI.PixmapCache.getIcon("taskWarning.png")) |
|
108 elif self.taskType == Task.TypeTodo: |
|
109 self.setIcon(2, UI.PixmapCache.getIcon("taskTodo.png")) |
|
110 else: |
|
111 self.setIcon(2, UI.PixmapCache.getIcon("taskNote.png")) |
|
112 |
|
113 self.colorizeTask() |
|
114 self.setTextAlignment(4, Qt.AlignRight) |
|
115 |
|
116 def colorizeTask(self): |
|
117 """ |
|
118 Public slot to set the colors of the task item. |
|
119 """ |
|
120 boldFont = self.font(0) |
|
121 boldFont.setBold(True) |
|
122 nonBoldFont = self.font(0) |
|
123 nonBoldFont.setBold(False) |
|
124 for col in range(5): |
|
125 if self.taskType == Task.TypeFixme: |
|
126 self.setBackground( |
|
127 col, Preferences.getTasks("TasksFixmeColor")) |
|
128 elif self.taskType == Task.TypeWarning: |
|
129 self.setBackground( |
|
130 col, Preferences.getTasks("TasksWarningColor")) |
|
131 elif self.taskType == Task.TypeTodo: |
|
132 self.setBackground( |
|
133 col, Preferences.getTasks("TasksTodoColor")) |
|
134 else: |
|
135 self.setBackground( |
|
136 col, Preferences.getTasks("TasksNoteColor")) |
|
137 if self._isProjectTask: |
|
138 self.setFont(col, boldFont) |
|
139 else: |
|
140 self.setFont(col, nonBoldFont) |
|
141 |
|
142 def setSummary(self, summary): |
|
143 """ |
|
144 Public slot to update the description. |
|
145 |
|
146 @param summary summary text of the task (string) |
|
147 """ |
|
148 self.summary = summary |
|
149 self.setText(2, self.summary) |
|
150 |
|
151 def setDescription(self, description): |
|
152 """ |
|
153 Public slot to update the description field. |
|
154 |
|
155 @param description descriptive text of the task (string) |
|
156 """ |
|
157 self.description = description |
|
158 |
|
159 def setPriority(self, priority): |
|
160 """ |
|
161 Public slot to update the priority. |
|
162 |
|
163 @param priority priority of the task (0=high, 1=normal, 2=low) |
|
164 """ |
|
165 if priority in [0, 1, 2]: |
|
166 self.priority = priority |
|
167 else: |
|
168 self.priority = 1 |
|
169 |
|
170 if self.priority == 1: |
|
171 self.setIcon(1, UI.PixmapCache.getIcon("empty.png")) |
|
172 elif self.priority == 0: |
|
173 self.setIcon(1, UI.PixmapCache.getIcon("taskPrioHigh.png")) |
|
174 elif self.priority == 2: |
|
175 self.setIcon(1, UI.PixmapCache.getIcon("taskPrioLow.png")) |
|
176 else: |
|
177 self.setIcon(1, UI.PixmapCache.getIcon("empty.png")) |
|
178 |
|
179 def setCompleted(self, completed): |
|
180 """ |
|
181 Public slot to update the completed flag. |
|
182 |
|
183 @param completed flag indicating completion status (boolean) |
|
184 """ |
|
185 self.completed = completed |
|
186 if self.completed: |
|
187 self.setIcon(0, UI.PixmapCache.getIcon("taskCompleted.png")) |
|
188 strikeOut = True |
|
189 else: |
|
190 self.setIcon(0, UI.PixmapCache.getIcon("empty.png")) |
|
191 strikeOut = False |
|
192 for column in range(2, 5): |
|
193 f = self.font(column) |
|
194 f.setStrikeOut(strikeOut) |
|
195 self.setFont(column, f) |
|
196 |
|
197 # set the completion status for all children |
|
198 for index in range(self.childCount()): |
|
199 self.child(index).setCompleted(completed) |
|
200 |
|
201 def isCompleted(self): |
|
202 """ |
|
203 Public slot to return the completion status. |
|
204 |
|
205 @return flag indicating the completion status (boolean) |
|
206 """ |
|
207 return self.completed |
|
208 |
|
209 def getFilename(self): |
|
210 """ |
|
211 Public method to retrieve the task's filename. |
|
212 |
|
213 @return filename (string) |
|
214 """ |
|
215 if self._isProjectTask and self.filename: |
|
216 return os.path.join(self.project.getProjectPath(), self.filename) |
|
217 else: |
|
218 return self.filename |
|
219 |
|
220 def isFileTask(self): |
|
221 """ |
|
222 Public slot to get an indication, if this task is related to a file. |
|
223 |
|
224 @return flag indicating a file task (boolean) |
|
225 """ |
|
226 return self.filename != "" |
|
227 |
|
228 def getLineno(self): |
|
229 """ |
|
230 Public method to retrieve the task's linenumber. |
|
231 |
|
232 @return linenumber (integer) |
|
233 """ |
|
234 return self.lineno |
|
235 |
|
236 def getUuid(self): |
|
237 """ |
|
238 Public method to get the task's uid. |
|
239 |
|
240 @return uid (string) |
|
241 """ |
|
242 return self.uid |
|
243 |
|
244 def getParentUuid(self): |
|
245 """ |
|
246 Public method to get the parent task's uid. |
|
247 |
|
248 @return parent uid (string) |
|
249 """ |
|
250 return self.parentUid |
|
251 |
|
252 def setProjectTask(self, pt): |
|
253 """ |
|
254 Public method to set the project relation flag. |
|
255 |
|
256 @param pt flag indicating a project task (boolean) |
|
257 """ |
|
258 self._isProjectTask = pt |
|
259 self.colorizeTask() |
|
260 |
|
261 def isProjectTask(self): |
|
262 """ |
|
263 Public slot to return the project relation status. |
|
264 |
|
265 @return flag indicating the project relation status (boolean) |
|
266 """ |
|
267 return self._isProjectTask |
|
268 |
|
269 def isProjectFileTask(self): |
|
270 """ |
|
271 Public slot to get an indication, if this task is related to a |
|
272 project file. |
|
273 |
|
274 @return flag indicating a project file task (boolean) |
|
275 """ |
|
276 return self._isProjectTask and self.filename != "" |