TimeTracker/TimeTrackEntry.py

Wed, 23 Apr 2014 23:25:17 +0200

author
T.Rzepka <Tobias.Rzepka@gmail.com>
date
Wed, 23 Apr 2014 23:25:17 +0200
changeset 47
f201a23a8cd7
parent 44
fe7ddb709c6a
child 51
d0afa82c3deb
permissions
-rw-r--r--

python2Compatible flag added.

# -*- coding: utf-8 -*-

# Copyright (c) 2012 - 2014 Detlev Offenbach <detlev@die-offenbachs.de>
#

"""
Module implementing the time track entry class.
"""

from __future__ import unicode_literals

from PyQt4.QtCore import Qt, QDateTime, QTime


class TimeTrackEntry(object):
    """
    Class implementing the time track entry.
    """
    LineMarker = "Entry: "
    Separator = "@@"
    
    def __init__(self, plugin):
        """
        Constructor
        
        @param plugin reference to the plugin object (TimeTrackerPlugin)
        """
        self.__plugin = plugin
        
        self.__entryMembersCount = 5
        self.__id = -1
        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 __lt__(self, other):
        """
        Special method implementing the less than function.
        
        @param other reference to the other object (TimeTrackEntry)
        @return flag indicating that self is less than other (boolean)
        """
        return self.__startDateTime < other.getStartDateTime()
    
    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([
                str(self.__id),
                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 ID of the tracker entry; -1 indicates an error (integer)
        """
        if not line.startswith(TimeTrackEntry.LineMarker):
            return -1
        
        line = line.replace(TimeTrackEntry.LineMarker, "")
        dataList = line.split(TimeTrackEntry.Separator)
        if len(dataList) != self.__entryMembersCount:
            return -1
        
        try:
            self.__id = int(dataList[0])
        except ValueError:
            return -1
        
        dt = QDateTime.fromString(dataList[1], Qt.ISODate)
        if not dt.isValid():
            return -1
        self.__startDateTime = dt
        
        try:
            dt = int(dataList[2])
        except ValueError:
            return -1
        self.__duration = dt
        
        self.__task = dataList[3]
        self.__comment = dataList[4]
        
        self.__valid = True
        return self.__id
    
    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 = self.__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 >= self.__plugin.getPreferences("MinimumDuration"):
            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 = self.__currentDateTime()
            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 __currentDateTime(self):
        """
        Private method to get the current date and time without milliseconds.
        
        @return current date and time (QDateTime)
        """
        dt = QDateTime.currentDateTime()
        t = dt.time()
        t2 = QTime(t.hour(), t.minute(), t.second())
        dt.setTime(t2)
        return dt
    
    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 getID(self):
        """
        Public method to get the ID of the entry.
        
        @return ID of the entry (integer)
        """
        return self.__id
    
    def setID(self, eid):
        """
        Public method to assign an ID to the entry.
        
        @param eid ID for the entry (integer)
        """
        self.__id = eid
    
    def getStartDateTime(self):
        """
        Public method to get the start date and time.
        
        @return start date and time (QDateTime)
        """
        return self.__startDateTime
    
    def setStartDateTime(self, startDateTime):
        """
        Public method to set the start date and time.
        
        @param startDateTime start date and time (QDateTime)
        """
        if startDateTime.isValid():
            self.__startDateTime = startDateTime
            self.__valid = self.__startDateTime.isValid() and \
                self.__duration >= self.__plugin.getPreferences(
                    "MinimumDuration")
    
    def getDuration(self):
        """
        Public slot to get the duration.
        
        @return duration (integer)
        """
        return self.__duration
    
    def setDuration(self, duration):
        """
        Public method to set the duration.
        
        @param duration duration in minutes (integer)
        """
        if duration >= self.__plugin.getPreferences("MinimumDuration"):
            self.__duration = duration
            self.__valid = self.__startDateTime.isValid() and \
                self.__duration >= self.__plugin.getPreferences(
                    "MinimumDuration")
    
    def addDuration(self, duration):
        """
        Public method to add a duration.
        
        @param duration duration to be added in minutes (integer). Negative
            values are ignored.
        """
        if duration > 0:
            self.__duration += duration
    
    def getTask(self):
        """
        Public method to get the task description.
        
        @return task description (string)
        """
        return self.__task
    
    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 getComment(self):
        """
        Public method to get the comment.
        
        @return comment (string)
        """
        return self.__comment
    
    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.__id,
            self.__startDateTime.toString("yyyy-MM-dd"),
            self.__startDateTime.toString("hh:mm"),
            self.__duration,
            self.__task,
            self.__comment,
            self.__paused,
        )

eric ide

mercurial