From b47d6b65461f1ca7d4060a5f7761b0f1de4e810d Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Tue, 23 Jun 2020 19:55:19 +0200 Subject: [PATCH 1/4] Added a document header to the build tool document stating when it was generated --- nw/gui/build.py | 84 ++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 76 insertions(+), 8 deletions(-) diff --git a/nw/gui/build.py b/nw/gui/build.py index e949a846..b7126580 100644 --- a/nw/gui/build.py +++ b/nw/gui/build.py @@ -31,6 +31,7 @@ import nw from os import path from time import time +from datetime import datetime from PyQt5.QtCore import Qt, QByteArray from PyQt5.QtPrintSupport import QPrinter, QPrintPreviewDialog @@ -39,7 +40,7 @@ from PyQt5.QtGui import ( ) from PyQt5.QtWidgets import ( QDialog, QVBoxLayout, QHBoxLayout, QTextBrowser, QPushButton, QLabel, - QLineEdit, QGroupBox, QGridLayout, QProgressBar, QMenu, QAction, + QLineEdit, QGroupBox, QGridLayout, QProgressBar, QMenu, QAction, QFrame, QFileDialog, QFontDialog, QSpinBox ) @@ -76,6 +77,7 @@ class GuiBuildNovel(QDialog): self.htmlText = [] # List of html document self.htmlStyle = [] # List of html styles self.nwdText = [] # List of markdown documents + self.buildTime = 0 # The timestamp of the last build self.setWindowTitle("Build Novel Project") self.setMinimumWidth(self.mainConf.pxInt(900)) @@ -411,11 +413,12 @@ class GuiBuildNovel(QDialog): self.docView.clearStyleSheet() else: self.docView.setStyleSheet(self.htmlStyle) - self.docView.setContent(self.htmlText) + self.docView.setContent(self.htmlText, self.buildTime) else: self.htmlText = [] self.htmlStyle = [] self.nwdText = [] + self.buildTime = 0 return @@ -509,6 +512,7 @@ class GuiBuildNovel(QDialog): tEnd = time() logger.debug("Built project in %.3f ms" % (1000*(tEnd-tStart))) self.htmlStyle = makeHtml.getStyleSheet() + self.buildTime = tEnd # Load the preview document with the html data self.docView.setTextFont(textFont, textSize) @@ -517,7 +521,7 @@ class GuiBuildNovel(QDialog): self.docView.clearStyleSheet() else: self.docView.setStyleSheet(self.htmlStyle) - self.docView.setContent(self.htmlText) + self.docView.setContent(self.htmlText, self.buildTime) self._saveCache() @@ -694,6 +698,7 @@ class GuiBuildNovel(QDialog): "workingTitle" : self.theProject.projName, "novelTitle" : self.theProject.bookTitle, "authors" : self.theProject.bookAuthors, + "buildTime" : self.buildTime, } } @@ -808,6 +813,8 @@ class GuiBuildNovel(QDialog): if "nwdText" in theData.keys(): self.nwdText = theData["nwdText"] dataCount += 1 + if "buildTime" in theData.keys(): + self.buildTime = theData["buildTime"] return dataCount == 3 @@ -828,6 +835,7 @@ class GuiBuildNovel(QDialog): "htmlText" : self.htmlText, "htmlStyle" : self.htmlStyle, "nwdText" : self.nwdText, + "buildTime" : self.buildTime, }, indent=nIndent)) except Exception as e: logger.error("Failed to save build cache") @@ -924,12 +932,13 @@ class GuiBuildNovelDocView(QTextBrowser): self.mainConf = nw.CONFIG self.theProject = theProject self.theParent = theParent + self.theTheme = theParent.theTheme self.setMinimumWidth(40*self.theParent.theTheme.textNWidth) self.setOpenExternalLinks(False) self.qDocument = self.document() - self.qDocument.setDocumentMargin(self.mainConf.textMargin) + self.qDocument.setDocumentMargin(self.mainConf.getTextMargin()) self.setPlaceholderText( "This area will show the content of the document to be " "exported or printed. Press the \"Build Novel Project\" " @@ -946,12 +955,29 @@ class GuiBuildNovelDocView(QTextBrowser): docPalette = self.palette() docPalette.setColor(QPalette.Base, QColor(255, 255, 255)) - docPalette.setColor(QPalette.Text, QColor( 0, 0, 0)) + docPalette.setColor(QPalette.Text, QColor(0, 0, 0)) self.setPalette(docPalette) - self.setStyleSheet() + lblPalette = self.palette() + lblPalette.setColor(QPalette.Background, lblPalette.toolTipBase().color()) + lblPalette.setColor(QPalette.Foreground, lblPalette.toolTipText().color()) - self.show() + lblFont = self.font() + lblFont.setPointSizeF(0.9*self.theTheme.fontPointSize) + + fPx = int(1.1*self.theTheme.fontPixelSize) + mPx = self.mainConf.pxInt(4) + + self.theTitle = QLabel("", self) + self.theTitle.setIndent(0) + self.theTitle.setAutoFillBackground(True) + self.theTitle.setAlignment(Qt.AlignCenter) + self.theTitle.setFixedHeight(fPx) + self.theTitle.setPalette(lblPalette) + self.theTitle.setFont(lblFont) + + self._updateDocMargins() + self.setStyleSheet() logger.debug("GuiBuildNovelDocView initialisation complete") @@ -977,15 +1003,23 @@ class GuiBuildNovelDocView(QTextBrowser): self.setFont(theFont) return - def setContent(self, theText): + def setContent(self, theText, timeStamp): """Set the content, either from text or list of text. """ if isinstance(theText, list): theText = "".join(theText) + theText = theText.replace(" ", " "*4) theText = theText.replace("", "") theText = theText.replace("", "") self.setHtml(theText) + + if timeStamp > 0: + strBuildTime = datetime.fromtimestamp(timeStamp).strftime("%x %X") + else: + strBuildTime = "Unknown" + self.theTitle.setText("Build Time: %s" % strBuildTime) + return def setStyleSheet(self, theStyles=[]): @@ -1007,4 +1041,38 @@ class GuiBuildNovelDocView(QTextBrowser): self.qDocument.setDefaultStyleSheet("") return + ## + # Events + ## + + def resizeEvent(self, theEvent): + """Make sure the document title is the same width as the window. + """ + QTextBrowser.resizeEvent(self, theEvent) + self._updateDocMargins() + return + + ## + # Internal Functions + ## + + def _updateDocMargins(self): + """Automatically adjust the header to fill the top of the + document within the viewport. + """ + vBar = self.verticalScrollBar() + if vBar.isVisible(): + sW = vBar.width() + else: + sW = 0 + + tB = self.frameWidth() + tW = self.width() - 2*tB - sW + tH = self.theTitle.height() + + self.theTitle.setGeometry(tB, tB, tW, tH) + self.setViewportMargins(0, tH, 0, 0) + + return + # END Class GuiBuildNovelDocView From bc8873fdd44b9392e2ef0eedc6d0e88ca0639323 Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Tue, 23 Jun 2020 20:23:52 +0200 Subject: [PATCH 2/4] Also added a fuzzy time string to the build doc header to make it easier to see the age of the build --- nw/common.py | 24 ++++++++++++++++++++++++ nw/gui/build.py | 32 +++++++++++++++++++++++++------- 2 files changed, 49 insertions(+), 7 deletions(-) diff --git a/nw/common.py b/nw/common.py index 0019c38a..8af90084 100644 --- a/nw/common.py +++ b/nw/common.py @@ -193,3 +193,27 @@ def transferCase(theSource, theTarget): theResult = theTarget.lower() return theResult + +def fuzzyTime(secDiff): + """Converts a time difference in seconds into a fuzzy time string. + """ + if secDiff < 0: + return "in the future" + elif secDiff < 15: + return "just now" + elif secDiff < 45: + return "a few seconds ago" + elif secDiff < 90: + return "a minute ago" + elif secDiff < 3300: # 55 minutes + return "%d minutes ago" % int(round(secDiff/60)) + elif secDiff < 5400: # 90 minutes + return "an hour ago" + elif secDiff < 84600: # 23.5 hours + return "%d hours ago" % int(round(secDiff/3600)) + elif secDiff < 129600: # 1.5 days + return "a day ago" + elif secDiff < 8640000: # 100 days + return "%d days ago" % int(round(secDiff/86400)) + else: + return "ages ago" diff --git a/nw/gui/build.py b/nw/gui/build.py index b7126580..4765bd7e 100644 --- a/nw/gui/build.py +++ b/nw/gui/build.py @@ -33,7 +33,7 @@ from os import path from time import time from datetime import datetime -from PyQt5.QtCore import Qt, QByteArray +from PyQt5.QtCore import Qt, QByteArray, QTimer from PyQt5.QtPrintSupport import QPrinter, QPrintPreviewDialog from PyQt5.QtGui import ( QPalette, QColor, QTextDocumentWriter, QFont @@ -44,6 +44,7 @@ from PyQt5.QtWidgets import ( QFileDialog, QFontDialog, QSpinBox ) +from nw.common import fuzzyTime from nw.gui.custom import QSwitch from nw.core import ToHtml from nw.constants import ( @@ -933,6 +934,7 @@ class GuiBuildNovelDocView(QTextBrowser): self.theProject = theProject self.theParent = theParent self.theTheme = theParent.theTheme + self.buildTime = 0 self.setMinimumWidth(40*self.theParent.theTheme.textNWidth) self.setOpenExternalLinks(False) @@ -979,6 +981,12 @@ class GuiBuildNovelDocView(QTextBrowser): self._updateDocMargins() self.setStyleSheet() + # Age Timer + self.ageTimer = QTimer() + self.ageTimer.setInterval(5000) + self.ageTimer.timeout.connect(self._updateBuildAge) + self.ageTimer.start() + logger.debug("GuiBuildNovelDocView initialisation complete") return @@ -1009,16 +1017,13 @@ class GuiBuildNovelDocView(QTextBrowser): if isinstance(theText, list): theText = "".join(theText) + self.buildTime = timeStamp + theText = theText.replace(" ", " "*4) theText = theText.replace("", "") theText = theText.replace("", "") self.setHtml(theText) - - if timeStamp > 0: - strBuildTime = datetime.fromtimestamp(timeStamp).strftime("%x %X") - else: - strBuildTime = "Unknown" - self.theTitle.setText("Build Time: %s" % strBuildTime) + self._updateBuildAge() return @@ -1056,6 +1061,19 @@ class GuiBuildNovelDocView(QTextBrowser): # Internal Functions ## + def _updateBuildAge(self): + """ + """ + if self.buildTime > 0: + strBuildTime = "%s (%s)" % ( + datetime.fromtimestamp(self.buildTime).strftime("%x %X"), + fuzzyTime(time() - self.buildTime) + ) + else: + strBuildTime = "Unknown" + self.theTitle.setText("Build Time: %s" % strBuildTime) + + def _updateDocMargins(self): """Automatically adjust the header to fill the top of the document within the viewport. From c19c93d60c726b052b63f7240cad4b7a607afa6b Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Tue, 23 Jun 2020 20:41:10 +0200 Subject: [PATCH 3/4] Some last tweaks --- nw/common.py | 5 ++++- nw/gui/build.py | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/nw/common.py b/nw/common.py index 8af90084..7cc2fbd3 100644 --- a/nw/common.py +++ b/nw/common.py @@ -213,7 +213,10 @@ def fuzzyTime(secDiff): return "%d hours ago" % int(round(secDiff/3600)) elif secDiff < 129600: # 1.5 days return "a day ago" - elif secDiff < 8640000: # 100 days + elif secDiff < 31104000: # 360 days return "%d days ago" % int(round(secDiff/86400)) + elif secDiff < 36288000: # 420 days + return "a year ago" else: return "ages ago" + return "beyond time and space" diff --git a/nw/gui/build.py b/nw/gui/build.py index 4765bd7e..4fc0e42d 100644 --- a/nw/gui/build.py +++ b/nw/gui/build.py @@ -983,7 +983,7 @@ class GuiBuildNovelDocView(QTextBrowser): # Age Timer self.ageTimer = QTimer() - self.ageTimer.setInterval(5000) + self.ageTimer.setInterval(10000) self.ageTimer.timeout.connect(self._updateBuildAge) self.ageTimer.start() @@ -1071,7 +1071,7 @@ class GuiBuildNovelDocView(QTextBrowser): ) else: strBuildTime = "Unknown" - self.theTitle.setText("Build Time: %s" % strBuildTime) + self.theTitle.setText("Build Time: %s" % strBuildTime) def _updateDocMargins(self): From 918d9c6470f69ce0da0de893523b3ba2723822dd Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Tue, 23 Jun 2020 20:45:34 +0200 Subject: [PATCH 4/4] Forgot to clean up imports --- nw/gui/build.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nw/gui/build.py b/nw/gui/build.py index 4fc0e42d..bb800c28 100644 --- a/nw/gui/build.py +++ b/nw/gui/build.py @@ -40,7 +40,7 @@ from PyQt5.QtGui import ( ) from PyQt5.QtWidgets import ( QDialog, QVBoxLayout, QHBoxLayout, QTextBrowser, QPushButton, QLabel, - QLineEdit, QGroupBox, QGridLayout, QProgressBar, QMenu, QAction, QFrame, + QLineEdit, QGroupBox, QGridLayout, QProgressBar, QMenu, QAction, QFileDialog, QFontDialog, QSpinBox )