Restructured the session log class and made it consistent with other source files

This commit is contained in:
Veronica K. B. Olsen
2020-06-24 21:36:08 +02:00
parent a5c83a45f5
commit 1110f40231
+79 -48
View File
@@ -34,7 +34,7 @@ from datetime import datetime
from PyQt5.QtCore import Qt from PyQt5.QtCore import Qt
from PyQt5.QtGui import QFont from PyQt5.QtGui import QFont
from PyQt5.QtWidgets import ( from PyQt5.QtWidgets import (
QDialog, QHBoxLayout, QTreeWidget, QTreeWidgetItem, QDialogButtonBox, qApp, QDialog, QHBoxLayout, QTreeWidget, QTreeWidgetItem, QDialogButtonBox,
QGridLayout, QLabel, QGroupBox, QCheckBox QGridLayout, QLabel, QGroupBox, QCheckBox
) )
@@ -59,6 +59,7 @@ class GuiSessionLogView(QDialog):
self.theTheme = theParent.theTheme self.theTheme = theParent.theTheme
self.optState = theProject.optState self.optState = theProject.optState
self.logData = []
self.timeFilter = 0.0 self.timeFilter = 0.0
self.timeTotal = 0.0 self.timeTotal = 0.0
@@ -82,6 +83,7 @@ class GuiSessionLogView(QDialog):
hHeader.setTextAlignment(self.C_COUNT, Qt.AlignRight) hHeader.setTextAlignment(self.C_COUNT, Qt.AlignRight)
self.monoFont = QFont() self.monoFont = QFont()
self.monoFont.setPointSizeF(0.9*self.theTheme.fontPointSize)
self.monoFont.setFamily(self.theTheme.guiFontFixed.family()) self.monoFont.setFamily(self.theTheme.guiFontFixed.family())
sortValid = (Qt.AscendingOrder, Qt.DescendingOrder) sortValid = (Qt.AscendingOrder, Qt.DescendingOrder)
@@ -149,42 +151,104 @@ class GuiSessionLogView(QDialog):
logger.debug("SessionLogView initialisation complete") logger.debug("SessionLogView initialisation complete")
self._loadSessionLog() qApp.processEvents()
self._loadLogFile()
self._updateListBox()
return return
def _loadSessionLog(self): ##
# Slots
##
logFile = path.join(self.theProject.projMeta, nwFiles.SESS_INFO) def _doClose(self):
if not path.isfile(logFile): """Save the state of the window, clear cache, end close.
logger.warning("No session log file found for this project.") """
return False self.logData = []
self.listBox.clear()
self.timeFilter = 0.0
self.timeTotal = 0.0
widthCol0 = self.mainConf.rpxInt(self.listBox.columnWidth(0))
widthCol1 = self.mainConf.rpxInt(self.listBox.columnWidth(1))
sortCol = self.listBox.sortColumn()
sortOrder = self.listBox.header().sortIndicatorOrder()
hideZeros = self.hideZeros.isChecked() hideZeros = self.hideZeros.isChecked()
hideNegative = self.hideNegative.isChecked() hideNegative = self.hideNegative.isChecked()
self.optState.setValue("GuiSession", "widthCol0", widthCol0)
self.optState.setValue("GuiSession", "widthCol1", widthCol1)
self.optState.setValue("GuiSession", "sortCol", sortCol)
self.optState.setValue("GuiSession", "sortOrder", sortOrder)
self.optState.setValue("GuiSession", "hideZeros", hideZeros)
self.optState.setValue("GuiSession", "hideNegative", hideNegative)
self.optState.saveSettings()
self.close()
return
def _doHideZeros(self, newState):
"""Reload the list box with the new filter settings in place.
"""
self._updateListBox()
return
def _doHideNegative(self, newState):
"""Reload the list box with the new filter settings in place.
"""
self._updateListBox()
return
##
# Internal Functions
##
def _loadLogFile(self):
"""Load the content of the log file into a buffer.
"""
self.logData = []
logger.debug("Loading session log file") logger.debug("Loading session log file")
try: try:
with open(logFile,mode="r",encoding="utf8") as inFile: logFile = path.join(self.theProject.projMeta, nwFiles.SESS_INFO)
with open(logFile, mode="r", encoding="utf8") as inFile:
for inLine in inFile: for inLine in inFile:
inData = inLine.split() inData = inLine.split()
if len(inData) != 8: if len(inData) != 8:
continue continue
dStart = datetime.strptime( dStart = datetime.strptime(
"%s %s" % (inData[1], inData[2]), nwConst.tStampFmt "%s %s" % (inData[1], inData[2]), nwConst.tStampFmt
) )
dEnd = datetime.strptime( dEnd = datetime.strptime(
"%s %s" % (inData[4], inData[5]), nwConst.tStampFmt "%s %s" % (inData[4], inData[5]), nwConst.tStampFmt
) )
nWords = int(inData[7]) nWords = int(inData[7])
tDiff = dEnd - dStart tDiff = dEnd - dStart
sDiff = tDiff.total_seconds() sDiff = tDiff.total_seconds()
self.logData.append((dStart, sDiff, nWords))
except Exception as e:
self.theParent.makeAlert(
["Failed to read session log file.",str(e)], nwAlert.ERROR
)
return False
return True
def _updateListBox(self):
"""Load/reload the content of the list box.
"""
self.listBox.clear()
self.timeFilter = 0.0
self.timeTotal = 0.0
hideZeros = self.hideZeros.isChecked()
hideNegative = self.hideNegative.isChecked()
for dStart, sDiff, nWords in self.logData:
self.timeTotal += sDiff self.timeTotal += sDiff
if hideZeros and nWords == 0: if hideZeros and nWords == 0:
@@ -209,47 +273,14 @@ class GuiSessionLogView(QDialog):
self.listBox.addTopLevelItem(newItem) self.listBox.addTopLevelItem(newItem)
except Exception as e:
self.theParent.makeAlert(
["Failed to read session log file.",str(e)], nwAlert.ERROR
)
return False
self.labelFilter.setText(self._formatTime(self.timeFilter)) self.labelFilter.setText(self._formatTime(self.timeFilter))
self.labelTotal.setText(self._formatTime(self.timeTotal)) self.labelTotal.setText(self._formatTime(self.timeTotal))
return True return True
def _doClose(self):
widthCol0 = self.mainConf.rpxInt(self.listBox.columnWidth(0))
widthCol1 = self.mainConf.rpxInt(self.listBox.columnWidth(1))
sortCol = self.listBox.sortColumn()
sortOrder = self.listBox.header().sortIndicatorOrder()
hideZeros = self.hideZeros.isChecked()
hideNegative = self.hideNegative.isChecked()
self.optState.setValue("GuiSession", "widthCol0", widthCol0)
self.optState.setValue("GuiSession", "widthCol1", widthCol1)
self.optState.setValue("GuiSession", "sortCol", sortCol)
self.optState.setValue("GuiSession", "sortOrder", sortOrder)
self.optState.setValue("GuiSession", "hideZeros", hideZeros)
self.optState.setValue("GuiSession", "hideNegative", hideNegative)
self.optState.saveSettings()
self.close()
return
def _doHideZeros(self, newState):
self._loadSessionLog()
return
def _doHideNegative(self, newState):
self._loadSessionLog()
return
def _formatTime(self, tS): def _formatTime(self, tS):
"""Format the time spent in 00:00:00 format.
"""
tM = int(tS/60) tM = int(tS/60)
tH = int(tM/60) tH = int(tM/60)
tM = tM - tH*60 tM = tM - tH*60