12 |
12 |
13 class TimeTrackEntry: |
13 class TimeTrackEntry: |
14 """ |
14 """ |
15 Class implementing the time track entry. |
15 Class implementing the time track entry. |
16 """ |
16 """ |
|
17 |
17 LineMarker = "Entry: " |
18 LineMarker = "Entry: " |
18 Separator = "@@" |
19 Separator = "@@" |
19 |
20 |
20 def __init__(self, plugin): |
21 def __init__(self, plugin): |
21 """ |
22 """ |
22 Constructor |
23 Constructor |
23 |
24 |
24 @param plugin reference to the plugin object |
25 @param plugin reference to the plugin object |
25 @type TimeTrackerPlugin |
26 @type TimeTrackerPlugin |
26 """ |
27 """ |
27 self.__plugin = plugin |
28 self.__plugin = plugin |
28 |
29 |
29 self.__entryMembersCount = 5 |
30 self.__entryMembersCount = 5 |
30 self.__id = -1 |
31 self.__id = -1 |
31 self.__startDateTime = QDateTime() # start date and time |
32 self.__startDateTime = QDateTime() # start date and time |
32 self.__duration = 0 # duration in minutes |
33 self.__duration = 0 # duration in minutes |
33 self.__task = "" # task specification |
34 self.__task = "" # task specification |
34 self.__comment = "" # comment string |
35 self.__comment = "" # comment string |
35 self.__valid = False # flag for a valid entry |
36 self.__valid = False # flag for a valid entry |
36 |
37 |
37 self.__continueDateTime = QDateTime() |
38 self.__continueDateTime = QDateTime() |
38 self.__paused = False |
39 self.__paused = False |
39 |
40 |
40 def __lt__(self, other): |
41 def __lt__(self, other): |
41 """ |
42 """ |
42 Special method implementing the less than function. |
43 Special method implementing the less than function. |
43 |
44 |
44 @param other reference to the other object |
45 @param other reference to the other object |
45 @type TimeTrackEntry |
46 @type TimeTrackEntry |
46 @return flag indicating that self is less than other |
47 @return flag indicating that self is less than other |
47 @rtype bool |
48 @rtype bool |
48 """ |
49 """ |
49 return self.__startDateTime < other.getStartDateTime() |
50 return self.__startDateTime < other.getStartDateTime() |
50 |
51 |
51 def toDict(self): |
52 def toDict(self): |
52 """ |
53 """ |
53 Public method to convert the time track entry into a dictionary. |
54 Public method to convert the time track entry into a dictionary. |
54 |
55 |
55 @return dictionary containing the time track entry data |
56 @return dictionary containing the time track entry data |
56 @rtype dict |
57 @rtype dict |
57 """ |
58 """ |
58 if self.__valid: |
59 if self.__valid: |
59 return { |
60 return { |
63 "task": self.__task, |
64 "task": self.__task, |
64 "comment": self.__comment, |
65 "comment": self.__comment, |
65 } |
66 } |
66 else: |
67 else: |
67 return {} |
68 return {} |
68 |
69 |
69 def fromDict(self, data): |
70 def fromDict(self, data): |
70 """ |
71 """ |
71 Public method to populate the time track entry from a dictionary. |
72 Public method to populate the time track entry from a dictionary. |
72 |
73 |
73 @param data dictionary containing the time track entry data |
74 @param data dictionary containing the time track entry data |
74 @type dict |
75 @type dict |
75 @return ID of the tracker entry; -1 indicates an error |
76 @return ID of the tracker entry; -1 indicates an error |
76 @rtype int |
77 @rtype int |
77 """ |
78 """ |
78 if len(data) != self.__entryMembersCount: |
79 if len(data) != self.__entryMembersCount: |
79 return -1 |
80 return -1 |
80 |
81 |
81 self.__id = data["id"] |
82 self.__id = data["id"] |
82 |
83 |
83 dt = QDateTime.fromString(data["start"], Qt.DateFormat.ISODate) |
84 dt = QDateTime.fromString(data["start"], Qt.DateFormat.ISODate) |
84 if not dt.isValid(): |
85 if not dt.isValid(): |
85 return -1 |
86 return -1 |
86 self.__startDateTime = dt |
87 self.__startDateTime = dt |
87 |
88 |
88 self.__duration = data["duration"] |
89 self.__duration = data["duration"] |
89 self.__task = data["task"] |
90 self.__task = data["task"] |
90 self.__comment = data["comment"] |
91 self.__comment = data["comment"] |
91 |
92 |
92 self.__valid = True |
93 self.__valid = True |
93 return self.__id |
94 return self.__id |
94 |
95 |
95 def isValid(self): |
96 def isValid(self): |
96 """ |
97 """ |
97 Public method to check the validity of the entry. |
98 Public method to check the validity of the entry. |
98 |
99 |
99 @return validity of the entry |
100 @return validity of the entry |
100 @rtype bool |
101 @rtype bool |
101 """ |
102 """ |
102 return self.__valid |
103 return self.__valid |
103 |
104 |
104 def start(self): |
105 def start(self): |
105 """ |
106 """ |
106 Public method to set the start time of this entry. |
107 Public method to set the start time of this entry. |
107 """ |
108 """ |
108 self.__startDateTime = self.__currentDateTime() |
109 self.__startDateTime = self.__currentDateTime() |
109 self.__continueDateTime = QDateTime(self.__startDateTime) |
110 self.__continueDateTime = QDateTime(self.__startDateTime) |
110 |
111 |
111 def stop(self): |
112 def stop(self): |
112 """ |
113 """ |
113 Public method to stop this entry. |
114 Public method to stop this entry. |
114 """ |
115 """ |
115 if not self.__paused: |
116 if not self.__paused: |
116 minutes = self.__calculateDuration( |
117 minutes = self.__calculateDuration( |
117 self.__continueDateTime, QDateTime.currentDateTime()) |
118 self.__continueDateTime, QDateTime.currentDateTime() |
|
119 ) |
118 self.__duration += minutes |
120 self.__duration += minutes |
119 |
121 |
120 if self.__duration >= self.__plugin.getPreferences("MinimumDuration"): |
122 if self.__duration >= self.__plugin.getPreferences("MinimumDuration"): |
121 self.__valid = True |
123 self.__valid = True |
122 else: |
124 else: |
123 self.__duration = 0 |
125 self.__duration = 0 |
124 self.__valid = False |
126 self.__valid = False |
125 |
127 |
126 def pause(self): |
128 def pause(self): |
127 """ |
129 """ |
128 Public method to pause the entry. |
130 Public method to pause the entry. |
129 """ |
131 """ |
130 if not self.__paused: |
132 if not self.__paused: |
131 minutes = self.__calculateDuration( |
133 minutes = self.__calculateDuration( |
132 self.__continueDateTime, QDateTime.currentDateTime()) |
134 self.__continueDateTime, QDateTime.currentDateTime() |
|
135 ) |
133 self.__duration += minutes |
136 self.__duration += minutes |
134 self.__paused = True |
137 self.__paused = True |
135 |
138 |
136 def continue_(self): |
139 def continue_(self): |
137 """ |
140 """ |
138 Public method to continue the entry. |
141 Public method to continue the entry. |
139 """ |
142 """ |
140 if self.__paused: |
143 if self.__paused: |
141 self.__continueDateTime = self.__currentDateTime() |
144 self.__continueDateTime = self.__currentDateTime() |
142 self.__paused = False |
145 self.__paused = False |
143 |
146 |
144 def isPaused(self): |
147 def isPaused(self): |
145 """ |
148 """ |
146 Public method to check for a paused state. |
149 Public method to check for a paused state. |
147 |
150 |
148 @return flag indicating a paused state |
151 @return flag indicating a paused state |
149 @rtype bool |
152 @rtype bool |
150 """ |
153 """ |
151 return self.__paused |
154 return self.__paused |
152 |
155 |
153 def __currentDateTime(self): |
156 def __currentDateTime(self): |
154 """ |
157 """ |
155 Private method to get the current date and time without milliseconds. |
158 Private method to get the current date and time without milliseconds. |
156 |
159 |
157 @return current date and time |
160 @return current date and time |
158 @rtype QDateTime |
161 @rtype QDateTime |
159 """ |
162 """ |
160 dt = QDateTime.currentDateTime() |
163 dt = QDateTime.currentDateTime() |
161 t = dt.time() |
164 t = dt.time() |
162 t2 = QTime(t.hour(), t.minute(), t.second()) |
165 t2 = QTime(t.hour(), t.minute(), t.second()) |
163 dt.setTime(t2) |
166 dt.setTime(t2) |
164 return dt |
167 return dt |
165 |
168 |
166 def __calculateDuration(self, start, stop): |
169 def __calculateDuration(self, start, stop): |
167 """ |
170 """ |
168 Private method to calculate the duration in minutes. |
171 Private method to calculate the duration in minutes. |
169 |
172 |
170 @param start start date and time |
173 @param start start date and time |
171 @type QDateTime |
174 @type QDateTime |
172 @param stop end date and time |
175 @param stop end date and time |
173 @type QDateTime |
176 @type QDateTime |
174 @return duration in minutes |
177 @return duration in minutes |
177 secs = start.secsTo(stop) |
180 secs = start.secsTo(stop) |
178 minutes = secs // 60 |
181 minutes = secs // 60 |
179 secsRemaining = secs % 60 |
182 secsRemaining = secs % 60 |
180 if secsRemaining >= 30: |
183 if secsRemaining >= 30: |
181 minutes += 1 |
184 minutes += 1 |
182 |
185 |
183 return minutes |
186 return minutes |
184 |
187 |
185 def getID(self): |
188 def getID(self): |
186 """ |
189 """ |
187 Public method to get the ID of the entry. |
190 Public method to get the ID of the entry. |
188 |
191 |
189 @return ID of the entry |
192 @return ID of the entry |
190 @rtype int |
193 @rtype int |
191 """ |
194 """ |
192 return self.__id |
195 return self.__id |
193 |
196 |
194 def setID(self, eid): |
197 def setID(self, eid): |
195 """ |
198 """ |
196 Public method to assign an ID to the entry. |
199 Public method to assign an ID to the entry. |
197 |
200 |
198 @param eid ID for the entry |
201 @param eid ID for the entry |
199 @type int |
202 @type int |
200 """ |
203 """ |
201 self.__id = eid |
204 self.__id = eid |
202 |
205 |
203 def getStartDateTime(self): |
206 def getStartDateTime(self): |
204 """ |
207 """ |
205 Public method to get the start date and time. |
208 Public method to get the start date and time. |
206 |
209 |
207 @return start date and time |
210 @return start date and time |
208 @rtype QDateTime |
211 @rtype QDateTime |
209 """ |
212 """ |
210 return self.__startDateTime |
213 return self.__startDateTime |
211 |
214 |
212 def setStartDateTime(self, startDateTime): |
215 def setStartDateTime(self, startDateTime): |
213 """ |
216 """ |
214 Public method to set the start date and time. |
217 Public method to set the start date and time. |
215 |
218 |
216 @param startDateTime start date and time |
219 @param startDateTime start date and time |
217 @type QDateTime |
220 @type QDateTime |
218 """ |
221 """ |
219 if startDateTime.isValid(): |
222 if startDateTime.isValid(): |
220 self.__startDateTime = startDateTime |
223 self.__startDateTime = startDateTime |
221 self.__valid = ( |
224 self.__valid = ( |
222 self.__startDateTime.isValid() and |
225 self.__startDateTime.isValid() |
223 self.__duration >= self.__plugin.getPreferences( |
226 and self.__duration >= self.__plugin.getPreferences("MinimumDuration") |
224 "MinimumDuration") |
|
225 ) |
227 ) |
226 |
228 |
227 def getDuration(self): |
229 def getDuration(self): |
228 """ |
230 """ |
229 Public slot to get the duration. |
231 Public slot to get the duration. |
230 |
232 |
231 @return duration |
233 @return duration |
232 @rtype int |
234 @rtype int |
233 """ |
235 """ |
234 return self.__duration |
236 return self.__duration |
235 |
237 |
236 def setDuration(self, duration): |
238 def setDuration(self, duration): |
237 """ |
239 """ |
238 Public method to set the duration. |
240 Public method to set the duration. |
239 |
241 |
240 @param duration duration in minutes |
242 @param duration duration in minutes |
241 @type int |
243 @type int |
242 """ |
244 """ |
243 if duration >= self.__plugin.getPreferences("MinimumDuration"): |
245 if duration >= self.__plugin.getPreferences("MinimumDuration"): |
244 self.__duration = duration |
246 self.__duration = duration |
245 self.__valid = ( |
247 self.__valid = ( |
246 self.__startDateTime.isValid() and |
248 self.__startDateTime.isValid() |
247 self.__duration >= self.__plugin.getPreferences( |
249 and self.__duration >= self.__plugin.getPreferences("MinimumDuration") |
248 "MinimumDuration") |
|
249 ) |
250 ) |
250 |
251 |
251 def addDuration(self, duration): |
252 def addDuration(self, duration): |
252 """ |
253 """ |
253 Public method to add a duration. |
254 Public method to add a duration. |
254 |
255 |
255 @param duration duration to be added in minutes. Negative values are |
256 @param duration duration to be added in minutes. Negative values are |
256 ignored. |
257 ignored. |
257 @type int |
258 @type int |
258 """ |
259 """ |
259 if duration > 0: |
260 if duration > 0: |
260 self.__duration += duration |
261 self.__duration += duration |
261 |
262 |
262 def getTask(self): |
263 def getTask(self): |
263 """ |
264 """ |
264 Public method to get the task description. |
265 Public method to get the task description. |
265 |
266 |
266 @return task description |
267 @return task description |
267 @rtype str |
268 @rtype str |
268 """ |
269 """ |
269 return self.__task |
270 return self.__task |
270 |
271 |
271 def setTask(self, description): |
272 def setTask(self, description): |
272 """ |
273 """ |
273 Public method to set the task description. |
274 Public method to set the task description. |
274 |
275 |
275 @param description task description |
276 @param description task description |
276 @type str |
277 @type str |
277 """ |
278 """ |
278 self.__task = ( |
279 self.__task = ( |
279 description.replace("\r\n", " ").replace("\n", " ") |
280 description.replace("\r\n", " ").replace("\n", " ").replace("\r", " ") |
280 .replace("\r", " ") |
|
281 ) |
281 ) |
282 |
282 |
283 def getComment(self): |
283 def getComment(self): |
284 """ |
284 """ |
285 Public method to get the comment. |
285 Public method to get the comment. |
286 |
286 |
287 @return comment |
287 @return comment |
288 @rtype str |
288 @rtype str |
289 """ |
289 """ |
290 return self.__comment |
290 return self.__comment |
291 |
291 |
292 def setComment(self, comment): |
292 def setComment(self, comment): |
293 """ |
293 """ |
294 Public method to set a comment. |
294 Public method to set a comment. |
295 |
295 |
296 @param comment comment to set |
296 @param comment comment to set |
297 @type str |
297 @type str |
298 """ |
298 """ |
299 self.__comment = ( |
299 self.__comment = ( |
300 comment.replace("\r\n", " ").replace("\n", " ").replace("\r", " ") |
300 comment.replace("\r\n", " ").replace("\n", " ").replace("\r", " ") |
301 ) |
301 ) |
302 |
302 |
303 def getEntryData(self): |
303 def getEntryData(self): |
304 """ |
304 """ |
305 Public method to get the entry data. |
305 Public method to get the entry data. |
306 |
306 |
307 @return entry data as a dictionary with keys 'id', 'paused', |
307 @return entry data as a dictionary with keys 'id', 'paused', |
308 'start_date', 'start_time', 'duration', 'task' and 'comment' |
308 'start_date', 'start_time', 'duration', 'task' and 'comment' |
309 containing the entry ID, a flag indicating a paused |
309 containing the entry ID, a flag indicating a paused |
310 state, the start date as a string, the start time as a string, |
310 state, the start date as a string, the start time as a string, |
311 the duration, the task and a comment |
311 the duration, the task and a comment |