--- a/TimeTracker/TimeTracker.py Fri Oct 19 22:26:57 2012 +0200 +++ b/TimeTracker/TimeTracker.py Sat Oct 20 12:34:26 2012 +0200 @@ -55,7 +55,7 @@ self.__trackerFilePath = '' self.__projectOpen = False - self.__entries = [] + self.__entries = {} # key: entry ID, value tracker entry self.__currentEntry = None self.__widget.clear() @@ -74,23 +74,16 @@ TimeTracker.FileName) self.__readTrackerEntries() - self.__widget.showTrackerEntries(sorted(self.__entries, reverse=True)) + self.__widget.showTrackerEntries(sorted(self.__entries.values(), reverse=True)) - self.__currentEntry = TimeTrackEntry() - self.__currentEntry.start() - self.__widget.setCurrentEntry(self.__currentEntry) + self.startTrackerEntry() def projectClosed(self): """ Public slot to handle the projectClosed signal. """ - if self.__currentEntry is not None: - self.__currentEntry.stop() - if self.__currentEntry.isValid(): - self.__entries.append(self.__currentEntry) - + self.stopTrackerEntry() self.__saveTrackerEntries() - self.__initialize() def __readTrackerEntries(self): @@ -113,8 +106,9 @@ invalidCount = 0 for line in data.splitlines(): entry = TimeTrackEntry() - if entry.fromString(line.strip()): - self.__entries.append(entry) + eid = entry.fromString(line.strip()) + if eid > -1: + self.__entries[eid] = entry else: invalidCount += 1 @@ -131,7 +125,7 @@ """ try: f = open(self.__trackerFilePath, "w", encoding="utf-8") - for entry in self.__entries: + for entry in self.__entries.values(): if entry.isValid(): f.write(entry.toString() + "\n") f.close() @@ -154,25 +148,39 @@ """ self.__currentEntry.continue_() + def stopTrackerEntry(self): + """ + Public method to stop the current tracker entry. + + @return tuple of the ID assigned to the stopped tracker entry and + the duration (integer, integer) + """ + duration = 0 + nextID = -1 + if self.__currentEntry is not None: + self.__currentEntry.stop() + if self.__currentEntry.isValid(): + if len(self.__entries.keys()): + nextID = max(self.__entries.keys()) + 1 + else: + nextID = 0 + self.__currentEntry.setID(nextID) + self.__entries[nextID] = self.__currentEntry + duration = self.__currentEntry.getDuration() + self.__currentEntry = None + + return nextID, duration + + def startTrackerEntry(self): + """ + Public method to start a new tracker entry. + """ + self.__currentEntry = TimeTrackEntry() + self.__currentEntry.start() + self.__widget.setCurrentEntry(self.__currentEntry) + def getCurrentEntry(self): """ Public method to get a reference to the current tracker entry. """ return self.__currentEntry - - def newEntry(self): - """ - Public method to stop the current tracker entry and start a new one. - """ - # stop the current time tracker entry - if self.__currentEntry is not None: - self.__currentEntry.stop() - if self.__currentEntry.isValid(): - self.__entries.append(self.__currentEntry) - - # save the tracker entries - self.__saveTrackerEntries() - - # start a new time tracker entry - self.__currentEntry = TimeTrackEntry() - self.__currentEntry.start()