diff -r 944024997123 -r a0beac325e5a TimeTracker/TimeTrackEntry.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TimeTracker/TimeTrackEntry.py Fri Oct 19 13:06:56 2012 +0200 @@ -0,0 +1,190 @@ +# -*- coding: utf-8 -*- + +# Copyright (c) 2012 Detlev Offenbach <detlev@die-offenbachs.de> +# + +""" +Module implementing the time track entry class. +""" + +from PyQt4.QtCore import Qt, QDateTime + + +class TimeTrackEntry(object): + """ + Class implementing the time track entry. + """ + LineMarker = "Entry: " + Separator = "@@" + + def __init__(self): + """ + Constructor + """ + self.__entryMembersCount = 4 + self.__startDateTime = QDateTime() # start date and time + self.__duration = 0 # duration in minutes + self.__task = "" # task specification + self.__comment = "" # comment string + self.__valid = False # flag for a valid entry + + self.__continueDateTime = QDateTime() + self.__paused = False + + def toString(self): + """ + Public method to get a string representation of the entry. + + @return string representation of the entry (string) + """ + if self.__valid: + dataLine = TimeTrackEntry.Separator.join([ + self.__startDateTime.toString(Qt.ISODate), + str(self.__duration), + self.__task, + self.__comment, + ]) + return "{0}{1}".format(TimeTrackEntry.LineMarker, dataLine) + else: + return "" + + def fromString(self, line): + """ + Public method to populate the entry from the given string. + + @param line stringified entry data as generated by toString() (string) + @return flag indicating valid data + """ + if not line.startswith(TimeTrackEntry.LineMarker): + return False + + line = line.replace(TimeTrackEntry.LineMarker, "") + dataList = line.split(TimeTrackEntry.Separator) + if len(dataList) != self.__entryMembersCount: + return False + + dt = QDateTime.fromString(dataList[0], Qt.ISODate) + if not dt.isValid(): + return False + self.__startDateTime = dt + + try: + dt = int(dataList[1]) + except ValueError: + return False + self.__duration = dt + + self.__task = dataList[2] + self.__comment = dataList[3] + + self.__valid = True + return True + + def isValid(self): + """ + Public method to check the validity of the entry. + + @return validity of the entry (boolean) + """ + return self.__valid + + def start(self): + """ + Public method to set the start time of this entry. + """ + self.__startDateTime = QDateTime.currentDateTime() + self.__continueDateTime = QDateTime(self.__startDateTime) + + def stop(self): + """ + Public method to stop this entry. + """ + if not self.__paused: + minutes = self.__calculateDuration( + self.__continueDateTime, QDateTime.currentDateTime()) + self.__duration += minutes + + if self.__duration >= 2: + self.__valid = True + else: + self.__duration = 0 + self.__valid = False + + def pause(self): + """ + Public method to pause the entry. + """ + if not self.__paused: + minutes = self.__calculateDuration( + self.__continueDateTime, QDateTime.currentDateTime()) + self.__duration += minutes + self.__paused = True + + def continue_(self): + """ + Public method to continue the entry. + """ + if self.__paused: + self.__continueDateTime = QDateTime(self.__startDateTime) + self.__paused = False + + def isPaused(self): + """ + Public method to check for a paused state. + + @return flag indicating a paused state (boolean) + """ + return self.__paused + + def __calculateDuration(self, start, stop): + """ + Private method to calculate the duration in minutes. + + @param start start date and time (QDateTime) + @param stop end date and time (QDateTime) + @return duration in minutes (int) + """ + secs = start.secsTo(stop) + minutes = secs // 60 + secsRemaining = secs % 60 + if secsRemaining >= 30: + minutes += 1 + + return minutes + + def setTask(self, description): + """ + Public method to set the task description. + + @param description task description (string) + """ + self.__task = description.replace("\r\n", " ")\ + .replace("\n", " ")\ + .replace("\r", " ") + + def setComment(self, comment): + """ + Public method to set a comment. + + @param comment comment to set (string) + """ + self.__comment = comment.replace("\r\n", " ")\ + .replace("\n", " ")\ + .replace("\r", " ") + + def getEntryData(self): + """ + Public method to get the entry data. + + @return entry data as a tuple of start date (string), start time (string), + duration (integer), task (string), comment (string) and flag indicating + a paused state (boolean) + """ + return ( + self.__startDateTime.toString("yyyy-MM-dd"), + self.__startDateTime.toString("hh:mm"), + self.__duration, + self.__task, + self.__comment, + self.__paused, + )