Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Age of Project Build #336

Merged
merged 4 commits into from
Jun 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 27 additions & 0 deletions nw/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,30 @@ 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 < 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"
100 changes: 93 additions & 7 deletions nw/gui/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@

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
Expand All @@ -43,6 +44,7 @@
QFileDialog, QFontDialog, QSpinBox
)

from nw.common import fuzzyTime
from nw.gui.custom import QSwitch
from nw.core import ToHtml
from nw.constants import (
Expand Down Expand Up @@ -76,6 +78,7 @@ def __init__(self, theParent, theProject):
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))
Expand Down Expand Up @@ -411,11 +414,12 @@ def __init__(self, theParent, theProject):
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

Expand Down Expand Up @@ -509,6 +513,7 @@ def _buildPreview(self):
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)
Expand All @@ -517,7 +522,7 @@ def _buildPreview(self):
self.docView.clearStyleSheet()
else:
self.docView.setStyleSheet(self.htmlStyle)
self.docView.setContent(self.htmlText)
self.docView.setContent(self.htmlText, self.buildTime)

self._saveCache()

Expand Down Expand Up @@ -694,6 +699,7 @@ def _saveDocument(self, theFormat):
"workingTitle" : self.theProject.projName,
"novelTitle" : self.theProject.bookTitle,
"authors" : self.theProject.bookAuthors,
"buildTime" : self.buildTime,
}
}

Expand Down Expand Up @@ -808,6 +814,8 @@ def _loadCache(self):
if "nwdText" in theData.keys():
self.nwdText = theData["nwdText"]
dataCount += 1
if "buildTime" in theData.keys():
self.buildTime = theData["buildTime"]

return dataCount == 3

Expand All @@ -828,6 +836,7 @@ def _saveCache(self):
"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")
Expand Down Expand Up @@ -924,12 +933,14 @@ def __init__(self, theParent, theProject):
self.mainConf = nw.CONFIG
self.theProject = theProject
self.theParent = theParent
self.theTheme = theParent.theTheme
self.buildTime = 0

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\" "
Expand All @@ -946,12 +957,35 @@ def __init__(self, theParent, theProject):

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)

lblPalette = self.palette()
lblPalette.setColor(QPalette.Background, lblPalette.toolTipBase().color())
lblPalette.setColor(QPalette.Foreground, lblPalette.toolTipText().color())

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()

self.show()
# Age Timer
self.ageTimer = QTimer()
self.ageTimer.setInterval(10000)
self.ageTimer.timeout.connect(self._updateBuildAge)
self.ageTimer.start()

logger.debug("GuiBuildNovelDocView initialisation complete")

Expand All @@ -977,15 +1011,20 @@ def setTextFont(self, textFont, textSize):
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)

self.buildTime = timeStamp

theText = theText.replace("&emsp;", "&nbsp;"*4)
theText = theText.replace("<del>", "<span style='text-decoration: line-through;'>")
theText = theText.replace("</del>", "</span>")
self.setHtml(theText)
self._updateBuildAge()

return

def setStyleSheet(self, theStyles=[]):
Expand All @@ -1007,4 +1046,51 @@ def clearStyleSheet(self):
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 _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("<b>Build Time:</b> %s" % strBuildTime)


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