Skip to content

Commit

Permalink
Merge pull request #285 from vkbo/high_dpi
Browse files Browse the repository at this point in the history
High DPI Support
  • Loading branch information
vkbo authored Jun 5, 2020
2 parents 8e7c3dc + f029a0f commit a051e77
Show file tree
Hide file tree
Showing 31 changed files with 373 additions and 266 deletions.
4 changes: 2 additions & 2 deletions nw/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,15 +172,15 @@ def main(sysArgs=None):
debugLevel = logging.INFO
elif inOpt == "--debug":
debugLevel = logging.DEBUG
logFormat = "[{asctime:}] {name:>20}:{lineno:<4d} {levelname:8} {message:}"
logFormat = "[{asctime:}] {name:>22}:{lineno:<4d} {levelname:8} {message:}"
elif inOpt == "--logfile":
logFile = inArg
toFile = True
elif inOpt in ("-q","--quiet"):
toStd = False
elif inOpt == "--verbose":
debugLevel = VERBOSE
logFormat = "[{asctime:}] {name:>20}:{lineno:<4d} {levelname:8} {message:}"
logFormat = "[{asctime:}] {name:>22}:{lineno:<4d} {levelname:8} {message:}"
elif inOpt == "--style":
qtStyle = inArg
elif inOpt == "--config":
Expand Down
71 changes: 62 additions & 9 deletions nw/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ def __init__(self):
self.guiLang = "en" # Hardcoded for now
self.guiFont = ""
self.guiFontSize = 11
self.guiScale = 1.0 # Set automatically by Theme class

## Sizes
self.winGeometry = [1100, 650]
Expand Down Expand Up @@ -125,8 +126,8 @@ def __init__(self):
self.highlightQuotes = True

self.fmtApostrophe = nwUnicode.U_RSQUO
self.fmtSingleQuotes = [nwUnicode.U_LSQUO,nwUnicode.U_RSQUO]
self.fmtDoubleQuotes = [nwUnicode.U_LDQUO,nwUnicode.U_RDQUO]
self.fmtSingleQuotes = [nwUnicode.U_LSQUO, nwUnicode.U_RSQUO]
self.fmtDoubleQuotes = [nwUnicode.U_LDQUO, nwUnicode.U_RDQUO]

self.spellTool = None
self.spellLanguage = None
Expand Down Expand Up @@ -197,7 +198,23 @@ def __init__(self):
return

##
# Actions
# Methods
##

def pxInt(self, theSize):
"""Used to scale fixed gui sizes by the screen scale factor.
This function returns an int, which is always rounded down.
"""
return int(theSize*self.guiScale)

def rpxInt(self, theSize):
"""Used to un-scale fixed gui sizes by the screen scale factor.
This function returns an int, which is always rounded down.
"""
return int(theSize/self.guiScale)

##
# Config Actions
##

def initConfig(self, confPath=None, dataPath=None):
Expand Down Expand Up @@ -647,7 +664,7 @@ def removeFromRecentCache(self, thePath):
return True

##
# Setters and Getters
# Setters
##

def setConfPath(self, newPath):
Expand Down Expand Up @@ -677,6 +694,8 @@ def setLastPath(self, lastPath):
return True

def setWinSize(self, newWidth, newHeight):
newWidth = int(newWidth/self.guiScale)
newHeight = int(newHeight/self.guiScale)
if abs(self.winGeometry[0] - newWidth) > 5:
self.winGeometry[0] = newWidth
self.confChanged = True
Expand All @@ -686,27 +705,27 @@ def setWinSize(self, newWidth, newHeight):
return True

def setTreeColWidths(self, colWidths):
self.treeColWidth = colWidths
self.treeColWidth = [int(x/self.guiScale) for x in colWidths]
self.confChanged = True
return True

def setProjColWidths(self, colWidths):
self.projColWidth = colWidths
self.projColWidth = [int(x/self.guiScale) for x in colWidths]
self.confChanged = True
return True

def setMainPanePos(self, panePos):
self.mainPanePos = panePos
self.mainPanePos = [int(x/self.guiScale) for x in panePos]
self.confChanged = True
return True

def setDocPanePos(self, panePos):
self.docPanePos = panePos
self.docPanePos = [int(x/self.guiScale) for x in panePos]
self.confChanged = True
return True

def setOutlinePanePos(self, panePos):
self.outlnPanePos = panePos
self.outlnPanePos = [int(x/self.guiScale) for x in panePos]
self.confChanged = True
return True

Expand All @@ -726,6 +745,40 @@ def getErrData(self):
self.errData = []
return errMessage

##
# Getters
##

def getWinSize(self):
return [int(x*self.guiScale) for x in self.winGeometry]

def getTreeColWidths(self):
return [int(x*self.guiScale) for x in self.treeColWidth]

def getProjColWidths(self):
return [int(x*self.guiScale) for x in self.projColWidth]

def getMainPanePos(self):
return [int(x*self.guiScale) for x in self.mainPanePos]

def getDocPanePos(self):
return [int(x*self.guiScale) for x in self.docPanePos]

def getOutlinePanePos(self):
return [int(x*self.guiScale) for x in self.outlnPanePos]

def getTextWidth(self):
return self.pxInt(self.textWidth)

def getTextMargin(self):
return self.pxInt(self.textMargin)

def getTabWidth(self):
return self.pxInt(self.tabWidth)

def getZenWidth(self):
return self.pxInt(self.zenWidth)

##
# Internal Functions
##
Expand Down
50 changes: 33 additions & 17 deletions nw/core/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,11 @@ def clearProject(self):
self.bookAuthors = []
self.autoReplace = {}
self.titleFormat = {
"title" : r"%title%",
"chapter" : r"Chapter %ch%: %title%",
"unnumbered" : r"%title%",
"scene" : r"* * *",
"section" : r"",
"withSynopsis" : False,
"withComments" : False,
"withKeywords" : False,
"title" : r"%title%",
"chapter" : r"Chapter %ch%: %title%",
"unnumbered" : r"%title%",
"scene" : r"* * *",
"section" : r"",
}
self.spellCheck = False
self.autoOutline = True
Expand Down Expand Up @@ -835,10 +832,8 @@ def setTitleFormat(self, titleFormat):
"""Set the formatting of titles in the project.
"""
for valKey, valEntry in titleFormat.items():
if valKey in ("title","chapter","unnumbered","scene","section"):
if valKey in self.titleFormat:
self.titleFormat[valKey] = checkString(valEntry, self.titleFormat[valKey], False)
elif valKey in ("withSynopsis","withComments","withKeywords"):
self.titleFormat[valKey] = checkBool(valEntry, False, False)
return

def setProjectChanged(self, bValue):
Expand Down Expand Up @@ -2018,6 +2013,7 @@ class OptionState():

def __init__(self, theProject):

self.mainConf = nw.CONFIG
self.theProject = theProject
self.theState = {}
self.stringOpt = ()
Expand All @@ -2026,6 +2022,10 @@ def __init__(self, theProject):

return

##
# Load and Save Cache
##

def loadSettings(self):
"""Load the options dictionary from the project settings file.
"""
Expand All @@ -2038,7 +2038,7 @@ def loadSettings(self):
if path.isfile(stateFile):
logger.debug("Loading GUI options file")
try:
with open(stateFile,mode="r",encoding="utf8") as inFile:
with open(stateFile, mode="r", encoding="utf8") as inFile:
theJson = inFile.read()
theState = json.loads(theJson)
except Exception as e:
Expand All @@ -2060,7 +2060,7 @@ def saveSettings(self):
logger.debug("Saving GUI options file")

try:
with open(stateFile,mode="w+",encoding="utf8") as outFile:
with open(stateFile, mode="w+", encoding="utf8") as outFile:
outFile.write(json.dumps(self.theState, indent=2))
except Exception as e:
logger.error("Failed to save GUI options file")
Expand All @@ -2069,6 +2069,10 @@ def saveSettings(self):

return True

##
# Setters
##

def setValue(self, setGroup, setName, setValue):
"""Saves a value, with a given group and name.
"""
Expand All @@ -2077,6 +2081,10 @@ def setValue(self, setGroup, setName, setValue):
self.theState[setGroup][setName] = setValue
return True

##
# Getters
##

def getValue(self, getGroup, getName, defaultValue):
"""Return an arbitrary type value, if it exists. Otherwise,
return the default value.
Expand All @@ -2085,7 +2093,8 @@ def getValue(self, getGroup, getName, defaultValue):
if getName in self.theState[getGroup]:
try:
return self.theState[getGroup][getName]
except:
except Exception as e:
logger.warning(str(e))
return defaultValue
return defaultValue

Expand All @@ -2109,7 +2118,8 @@ def getInt(self, getGroup, getName, defaultValue):
if getName in self.theState[getGroup]:
try:
return int(self.theState[getGroup][getName])
except:
except Exception as e:
logger.warning(str(e))
return defaultValue
return defaultValue

Expand All @@ -2121,7 +2131,8 @@ def getFloat(self, getGroup, getName, defaultValue):
if getName in self.theState[getGroup]:
try:
return float(self.theState[getGroup][getName])
except:
except Exception as e:
logger.warning(str(e))
return defaultValue
return defaultValue

Expand All @@ -2133,10 +2144,15 @@ def getBool(self, getGroup, getName, defaultValue):
if getName in self.theState[getGroup]:
try:
return bool(self.theState[getGroup][getName])
except:
except Exception as e:
logger.warning(str(e))
return defaultValue
return defaultValue

##
# Validators
##

def validIntRange(self, theValue, intA, intB, intDefault):
"""Check that an int is in a given range. If it isn't, return
the default value.
Expand Down
23 changes: 10 additions & 13 deletions nw/gui/about.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,24 @@ def __init__(self, theParent):

self.mainConf = nw.CONFIG
self.theParent = theParent
self.theTheme = theParent.theTheme

self.outerBox = QVBoxLayout()
self.innerBox = QHBoxLayout()
self.innerBox.setSpacing(16)
self.innerBox.setSpacing(self.mainConf.pxInt(16))

self.setWindowTitle("About %s" % nw.__package__)
self.setMinimumWidth(700)
self.setMinimumHeight(600)
self.setMinimumWidth(self.mainConf.pxInt(650))
self.setMinimumHeight(self.mainConf.pxInt(600))

self.guiDeco = self.theParent.theTheme.loadDecoration("nwicon", (96, 96))
iPx = self.mainConf.pxInt(96)
self.guiDeco = self.theParent.theTheme.loadDecoration("nwicon", (iPx, iPx))
self.lblName = QLabel("<b>%s</b>" % nw.__package__)
self.lblVers = QLabel("v%s" % nw.__version__)
self.lblDate = QLabel(datetime.strptime(nw.__date__, "%Y-%m-%d").strftime("%x"))

self.leftBox = QVBoxLayout()
self.leftBox.setSpacing(4)
self.leftBox = QVBoxLayout()
self.leftBox.setSpacing(self.mainConf.pxInt(4))
self.leftBox.addWidget(self.guiDeco, 0, Qt.AlignCenter)
self.leftBox.addWidget(self.lblName, 0, Qt.AlignCenter)
self.leftBox.addWidget(self.lblVers, 0, Qt.AlignCenter)
Expand All @@ -74,20 +76,15 @@ def __init__(self, theParent):
# Pages
self.pageAbout = QTextBrowser()
self.pageAbout.setOpenExternalLinks(True)
self.pageAbout.document().setDocumentMargin(16)

# self.pageCredit = QTextBrowser()
# self.pageCredit.setOpenExternalLinks(True)
# self.pageCredit.document().setDocumentMargin(16)
self.pageAbout.document().setDocumentMargin(self.mainConf.pxInt(16))

self.pageLicense = QTextBrowser()
self.pageLicense.setOpenExternalLinks(True)
self.pageLicense.document().setDocumentMargin(16)
self.pageLicense.document().setDocumentMargin(self.mainConf.pxInt(16))

# Main Tab Area
self.tabBox = QTabWidget()
self.tabBox.addTab(self.pageAbout, "About")
# self.tabBox.addTab(self.pageCredit, "Credit")
self.tabBox.addTab(self.pageLicense, "License")
self.innerBox.addWidget(self.tabBox)

Expand Down
Loading

0 comments on commit a051e77

Please sign in to comment.