From 2fe001b3361df4910a73d7db069fe46043cda573 Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Sun, 20 Oct 2019 15:52:25 +0200 Subject: [PATCH] Session log is now printed in the table --- nw/constants.py | 6 ++++ nw/gui/sessionlog.py | 67 +++++++++++++++++++++++++++++++++++++++++-- nw/project/project.py | 6 ++-- 3 files changed, 73 insertions(+), 6 deletions(-) diff --git a/nw/constants.py b/nw/constants.py index a5244d46..36484f00 100644 --- a/nw/constants.py +++ b/nw/constants.py @@ -12,6 +12,12 @@ from nw.enum import nwItemClass, nwItemLayout +class nwConst(): + + tStampFmt = "%Y-%m-%d %H:%M:%S" + +# END Class nwConst + class nwFiles(): APP_ICON = "novelWriter.svg" diff --git a/nw/gui/sessionlog.py b/nw/gui/sessionlog.py index c31b2f25..17553722 100644 --- a/nw/gui/sessionlog.py +++ b/nw/gui/sessionlog.py @@ -13,13 +13,17 @@ import logging import nw +from os import path +from datetime import datetime from PyQt5.QtCore import Qt -from PyQt5.QtGui import QIcon, QColor, QPixmap +from PyQt5.QtGui import QIcon, QColor, QPixmap, QFont from PyQt5.QtWidgets import ( - QDialog, QVBoxLayout, QHBoxLayout, QTableWidget, QTableWidgetItem, - QDialogButtonBox, QLabel, QPushButton, QHeaderView + QDialog, QVBoxLayout, QHBoxLayout, QTreeWidget, QTreeWidgetItem, QDialogButtonBox, QHeaderView ) +from nw.enum import nwAlert +from nw.constants import nwConst, nwFiles + logger = logging.getLogger(__name__) class GuiSessionLogView(QDialog): @@ -37,6 +41,31 @@ class GuiSessionLogView(QDialog): self.bottomBox = QHBoxLayout() self.setWindowTitle("Session Log") + self.setMinimumWidth(420) + self.setMinimumHeight(400) + + self.listBox = QTreeWidget() + self.listBox.setHeaderLabels(["Session Start","Length","Words",""]) + self.listBox.setIndentation(0) + self.listBox.setColumnWidth(0,180) + self.listBox.setColumnWidth(1,80) + self.listBox.setColumnWidth(2,80) + self.listBox.setColumnWidth(3,0) + + hHeader = self.listBox.headerItem() + hHeader.setTextAlignment(1,Qt.AlignRight) + hHeader.setTextAlignment(2,Qt.AlignRight) + + self.listFont = QFont("Monospace",10) + + self.listBox.sortByColumn(0, Qt.DescendingOrder) + self.listBox.setSortingEnabled(True) + + self.buttonBox = QDialogButtonBox(QDialogButtonBox.Close) + self.buttonBox.rejected.connect(self._doClose) + + self.outerBox.addWidget(self.listBox) + self.outerBox.addWidget(self.buttonBox) self.setLayout(self.outerBox) @@ -44,8 +73,40 @@ class GuiSessionLogView(QDialog): logger.debug("SessionLogView initialisation complete") + self._loadSessionLog() + return + def _loadSessionLog(self): + + logFile = path.join(self.theProject.projMeta, nwFiles.SESS_INFO) + if not path.isfile(logFile): + logger.warning("No session log file found for this project.") + return False + + logger.debug("Loading session log file") + try: + with open(logFile,mode="r") as inFile: + for inLine in inFile: + inData = inLine.split() + if len(inData) != 8: + continue + dStart = datetime.strptime("%s %s" % (inData[1],inData[2]),nwConst.tStampFmt) + dEnd = datetime.strptime("%s %s" % (inData[4],inData[5]),nwConst.tStampFmt) + nWords = int(inData[7]) + newItem = QTreeWidgetItem([str(dStart),str(dEnd-dStart),str(nWords),""]) + newItem.setTextAlignment(1,Qt.AlignRight) + newItem.setTextAlignment(2,Qt.AlignRight) + newItem.setFont(0,self.listFont) + newItem.setFont(1,self.listFont) + newItem.setFont(2,self.listFont) + self.listBox.addTopLevelItem(newItem) + except Exception as e: + self.theParent.makeAlert(["Failed to read session log file.",str(e)], nwAlert.ERROR) + return False + + return True + def _doClose(self): self.close() return diff --git a/nw/project/project.py b/nw/project/project.py index 7aea4790..603113dd 100644 --- a/nw/project/project.py +++ b/nw/project/project.py @@ -23,7 +23,7 @@ from nw.project.item import NWItem from nw.project.status import NWStatus from nw.enum import nwItemType, nwItemClass, nwItemLayout, nwAlert from nw.common import checkString, checkBool, checkInt -from nw.constants import nwFiles +from nw.constants import nwFiles, nwConst logger = logging.getLogger(__name__) @@ -663,8 +663,8 @@ class NWProject(): "End: {closed:s} " "Words: {words:8d}" ).format( - opened = datetime.fromtimestamp(self.projOpened).strftime("%Y-%m-%d %H:%M:%S"), - closed = datetime.now().strftime("%Y-%m-%d %H:%M:%S"), + opened = datetime.fromtimestamp(self.projOpened).strftime(nwConst.tStampFmt), + closed = datetime.now().strftime(nwConst.tStampFmt), words = self.getSessionWordCount(), ), file=outFile)