TimeTracker/TimeTrackEntry.py

branch
eric7
changeset 98
693e42176007
parent 94
a3d936304e51
child 105
44bfd9e7e65e
equal deleted inserted replaced
97:44ec4168b442 98:693e42176007
5 5
6 """ 6 """
7 Module implementing the time track entry class. 7 Module implementing the time track entry class.
8 """ 8 """
9 9
10 from PyQt5.QtCore import Qt, QDateTime, QTime 10 from PyQt6.QtCore import Qt, QDateTime, QTime
11 11
12 12
13 class TimeTrackEntry: 13 class TimeTrackEntry:
14 """ 14 """
15 Class implementing the time track entry. 15 Class implementing the time track entry.
19 19
20 def __init__(self, plugin): 20 def __init__(self, plugin):
21 """ 21 """
22 Constructor 22 Constructor
23 23
24 @param plugin reference to the plugin object (TimeTrackerPlugin) 24 @param plugin reference to the plugin object
25 @type TimeTrackerPlugin
25 """ 26 """
26 self.__plugin = plugin 27 self.__plugin = plugin
27 28
28 self.__entryMembersCount = 5 29 self.__entryMembersCount = 5
29 self.__id = -1 30 self.__id = -1
38 39
39 def __lt__(self, other): 40 def __lt__(self, other):
40 """ 41 """
41 Special method implementing the less than function. 42 Special method implementing the less than function.
42 43
43 @param other reference to the other object (TimeTrackEntry) 44 @param other reference to the other object
44 @return flag indicating that self is less than other (boolean) 45 @type TimeTrackEntry
46 @return flag indicating that self is less than other
47 @rtype bool
45 """ 48 """
46 return self.__startDateTime < other.getStartDateTime() 49 return self.__startDateTime < other.getStartDateTime()
47 50
48 def toString(self): 51 def toDict(self):
49 """ 52 """
50 Public method to get a string representation of the entry. 53 Public method to convert the time track entry into a dictionary.
51 54
52 @return string representation of the entry (string) 55 @return dictionary containing the time track entry data
56 @rtype dict
53 """ 57 """
54 if self.__valid: 58 if self.__valid:
55 dataLine = TimeTrackEntry.Separator.join([ 59 return {
56 str(self.__id), 60 "id": self.__id,
57 self.__startDateTime.toString(Qt.ISODate), 61 "start": self.__startDateTime.toString(Qt.DateFormat.ISODate),
58 str(self.__duration), 62 "duration": self.__duration,
59 self.__task, 63 "task": self.__task,
60 self.__comment, 64 "comment": self.__comment,
61 ]) 65 }
62 return "{0}{1}".format(TimeTrackEntry.LineMarker, dataLine)
63 else: 66 else:
64 return "" 67 return {}
65 68
66 def fromString(self, line): 69 def fromDict(self, data):
67 """ 70 """
68 Public method to populate the entry from the given string. 71 Public method to populate the time track entry from a dictionary.
69 72
70 @param line stringified entry data as generated by toString() (string) 73 @param data dictionary containing the time track entry data
71 @return ID of the tracker entry; -1 indicates an error (integer) 74 @type dict
72 """ 75 @return ID of the tracker entry; -1 indicates an error
73 if not line.startswith(TimeTrackEntry.LineMarker): 76 @rtype int
77 """
78 if len(data) != self.__entryMembersCount:
74 return -1 79 return -1
75 80
76 line = line.replace(TimeTrackEntry.LineMarker, "") 81 self.__id = data["id"]
77 dataList = line.split(TimeTrackEntry.Separator) 82
78 if len(dataList) != self.__entryMembersCount: 83 dt = QDateTime.fromString(data["start"], Qt.DateFormat.ISODate)
79 return -1
80
81 try:
82 self.__id = int(dataList[0])
83 except ValueError:
84 return -1
85
86 dt = QDateTime.fromString(dataList[1], Qt.ISODate)
87 if not dt.isValid(): 84 if not dt.isValid():
88 return -1 85 return -1
89 self.__startDateTime = dt 86 self.__startDateTime = dt
90 87
91 try: 88 self.__duration = data["duration"]
92 dt = int(dataList[2]) 89 self.__task = data["task"]
93 except ValueError: 90 self.__comment = data["comment"]
94 return -1
95 self.__duration = dt
96
97 self.__task = dataList[3]
98 self.__comment = dataList[4]
99 91
100 self.__valid = True 92 self.__valid = True
101 return self.__id 93 return self.__id
102 94
95 ## def toString(self):
96 ## """
97 ## Public method to get a string representation of the entry.
98 ##
99 ## @return string representation of the entry (string)
100 ## """
101 ## if self.__valid:
102 ## dataLine = TimeTrackEntry.Separator.join([
103 ## str(self.__id),
104 ## self.__startDateTime.toString(Qt.DateFormat.ISODate),
105 ## str(self.__duration),
106 ## self.__task,
107 ## self.__comment,
108 ## ])
109 ## return "{0}{1}".format(TimeTrackEntry.LineMarker, dataLine)
110 ## else:
111 ## return ""
112 ##
113 ## def fromString(self, line):
114 ## """
115 ## Public method to populate the entry from the given string.
116 ##
117 ## @param line stringified entry data as generated by toString() (string)
118 ## @return ID of the tracker entry; -1 indicates an error (integer)
119 ## """
120 ## if not line.startswith(TimeTrackEntry.LineMarker):
121 ## return -1
122 ##
123 ## line = line.replace(TimeTrackEntry.LineMarker, "")
124 ## dataList = line.split(TimeTrackEntry.Separator)
125 ## if len(dataList) != self.__entryMembersCount:
126 ## return -1
127 ##
128 ## try:
129 ## self.__id = int(dataList[0])
130 ## except ValueError:
131 ## return -1
132 ##
133 ## dt = QDateTime.fromString(dataList[1], Qt.DateFormat.ISODate)
134 ## if not dt.isValid():
135 ## return -1
136 ## self.__startDateTime = dt
137 ##
138 ## try:
139 ## dt = int(dataList[2])
140 ## except ValueError:
141 ## return -1
142 ## self.__duration = dt
143 ##
144 ## self.__task = dataList[3]
145 ## self.__comment = dataList[4]
146 ##
147 ## self.__valid = True
148 ## return self.__id
149 ##
103 def isValid(self): 150 def isValid(self):
104 """ 151 """
105 Public method to check the validity of the entry. 152 Public method to check the validity of the entry.
106 153
107 @return validity of the entry (boolean) 154 @return validity of the entry
155 @rtype bool
108 """ 156 """
109 return self.__valid 157 return self.__valid
110 158
111 def start(self): 159 def start(self):
112 """ 160 """
150 198
151 def isPaused(self): 199 def isPaused(self):
152 """ 200 """
153 Public method to check for a paused state. 201 Public method to check for a paused state.
154 202
155 @return flag indicating a paused state (boolean) 203 @return flag indicating a paused state
204 @rtype bool
156 """ 205 """
157 return self.__paused 206 return self.__paused
158 207
159 def __currentDateTime(self): 208 def __currentDateTime(self):
160 """ 209 """
161 Private method to get the current date and time without milliseconds. 210 Private method to get the current date and time without milliseconds.
162 211
163 @return current date and time (QDateTime) 212 @return current date and time
213 @rtype QDateTime
164 """ 214 """
165 dt = QDateTime.currentDateTime() 215 dt = QDateTime.currentDateTime()
166 t = dt.time() 216 t = dt.time()
167 t2 = QTime(t.hour(), t.minute(), t.second()) 217 t2 = QTime(t.hour(), t.minute(), t.second())
168 dt.setTime(t2) 218 dt.setTime(t2)
170 220
171 def __calculateDuration(self, start, stop): 221 def __calculateDuration(self, start, stop):
172 """ 222 """
173 Private method to calculate the duration in minutes. 223 Private method to calculate the duration in minutes.
174 224
175 @param start start date and time (QDateTime) 225 @param start start date and time
176 @param stop end date and time (QDateTime) 226 @type QDateTime
177 @return duration in minutes (int) 227 @param stop end date and time
228 @type QDateTime
229 @return duration in minutes
230 @rtype int
178 """ 231 """
179 secs = start.secsTo(stop) 232 secs = start.secsTo(stop)
180 minutes = secs // 60 233 minutes = secs // 60
181 secsRemaining = secs % 60 234 secsRemaining = secs % 60
182 if secsRemaining >= 30: 235 if secsRemaining >= 30:
186 239
187 def getID(self): 240 def getID(self):
188 """ 241 """
189 Public method to get the ID of the entry. 242 Public method to get the ID of the entry.
190 243
191 @return ID of the entry (integer) 244 @return ID of the entry
245 @rtype int
192 """ 246 """
193 return self.__id 247 return self.__id
194 248
195 def setID(self, eid): 249 def setID(self, eid):
196 """ 250 """
197 Public method to assign an ID to the entry. 251 Public method to assign an ID to the entry.
198 252
199 @param eid ID for the entry (integer) 253 @param eid ID for the entry
254 @type int
200 """ 255 """
201 self.__id = eid 256 self.__id = eid
202 257
203 def getStartDateTime(self): 258 def getStartDateTime(self):
204 """ 259 """
205 Public method to get the start date and time. 260 Public method to get the start date and time.
206 261
207 @return start date and time (QDateTime) 262 @return start date and time
263 @rtype QDateTime
208 """ 264 """
209 return self.__startDateTime 265 return self.__startDateTime
210 266
211 def setStartDateTime(self, startDateTime): 267 def setStartDateTime(self, startDateTime):
212 """ 268 """
213 Public method to set the start date and time. 269 Public method to set the start date and time.
214 270
215 @param startDateTime start date and time (QDateTime) 271 @param startDateTime start date and time
272 @type QDateTime
216 """ 273 """
217 if startDateTime.isValid(): 274 if startDateTime.isValid():
218 self.__startDateTime = startDateTime 275 self.__startDateTime = startDateTime
219 self.__valid = ( 276 self.__valid = (
220 self.__startDateTime.isValid() and 277 self.__startDateTime.isValid() and
224 281
225 def getDuration(self): 282 def getDuration(self):
226 """ 283 """
227 Public slot to get the duration. 284 Public slot to get the duration.
228 285
229 @return duration (integer) 286 @return duration
287 @rtype int
230 """ 288 """
231 return self.__duration 289 return self.__duration
232 290
233 def setDuration(self, duration): 291 def setDuration(self, duration):
234 """ 292 """
235 Public method to set the duration. 293 Public method to set the duration.
236 294
237 @param duration duration in minutes (integer) 295 @param duration duration in minutes
296 @type int
238 """ 297 """
239 if duration >= self.__plugin.getPreferences("MinimumDuration"): 298 if duration >= self.__plugin.getPreferences("MinimumDuration"):
240 self.__duration = duration 299 self.__duration = duration
241 self.__valid = ( 300 self.__valid = (
242 self.__startDateTime.isValid() and 301 self.__startDateTime.isValid() and
246 305
247 def addDuration(self, duration): 306 def addDuration(self, duration):
248 """ 307 """
249 Public method to add a duration. 308 Public method to add a duration.
250 309
251 @param duration duration to be added in minutes (integer). Negative 310 @param duration duration to be added in minutes. Negative values are
252 values are ignored. 311 ignored.
312 @type int
253 """ 313 """
254 if duration > 0: 314 if duration > 0:
255 self.__duration += duration 315 self.__duration += duration
256 316
257 def getTask(self): 317 def getTask(self):
258 """ 318 """
259 Public method to get the task description. 319 Public method to get the task description.
260 320
261 @return task description (string) 321 @return task description
322 @rtype str
262 """ 323 """
263 return self.__task 324 return self.__task
264 325
265 def setTask(self, description): 326 def setTask(self, description):
266 """ 327 """
267 Public method to set the task description. 328 Public method to set the task description.
268 329
269 @param description task description (string) 330 @param description task description
331 @type str
270 """ 332 """
271 self.__task = ( 333 self.__task = (
272 description.replace("\r\n", " ").replace("\n", " ") 334 description.replace("\r\n", " ").replace("\n", " ")
273 .replace("\r", " ") 335 .replace("\r", " ")
274 ) 336 )
275 337
276 def getComment(self): 338 def getComment(self):
277 """ 339 """
278 Public method to get the comment. 340 Public method to get the comment.
279 341
280 @return comment (string) 342 @return comment
343 @rtype str
281 """ 344 """
282 return self.__comment 345 return self.__comment
283 346
284 def setComment(self, comment): 347 def setComment(self, comment):
285 """ 348 """
286 Public method to set a comment. 349 Public method to set a comment.
287 350
288 @param comment comment to set (string) 351 @param comment comment to set
352 @type str
289 """ 353 """
290 self.__comment = ( 354 self.__comment = (
291 comment.replace("\r\n", " ").replace("\n", " ").replace("\r", " ") 355 comment.replace("\r\n", " ").replace("\n", " ").replace("\r", " ")
292 ) 356 )
293 357
294 def getEntryData(self): 358 def getEntryData(self):
295 """ 359 """
296 Public method to get the entry data. 360 Public method to get the entry data.
297 361
298 @return entry data as a tuple of start date (string), start time 362 @return entry data as a dictionary with keys 'id', 'paused',
299 (string), duration (integer), task (string), comment (string) 363 'start_date', 'start_time', 'duration', 'task' and 'comment'
300 and flag indicating a paused state (boolean) 364 containing the entry ID, a flag indicating a paused
301 """ 365 state, the start date as a string, the start time as a string,
302 return ( 366 the duration, the task and a comment
303 self.__id, 367 @rtype dict
304 self.__startDateTime.toString("yyyy-MM-dd"), 368 """
305 self.__startDateTime.toString("hh:mm"), 369 return {
306 self.__duration, 370 "id": self.__id,
307 self.__task, 371 "paused": self.__paused,
308 self.__comment, 372 "start_date": self.__startDateTime.toString("yyyy-MM-dd"),
309 self.__paused, 373 "start_time": self.__startDateTime.toString("hh:mm:ss"),
310 ) 374 "duration": self.__duration,
375 "task": self.__task,
376 "comment": self.__comment,
377 }

eric ide

mercurial