Skip to content

Commit

Permalink
Simplify text width settings (#943)
Browse files Browse the repository at this point in the history
* Remove the fixed text width switch, resolves #924
* Update tests
* Clean up code to set text width and add to viewer
  • Loading branch information
vkbo authored Dec 31, 2021
1 parent 6ac9e1a commit 7024254
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 56 deletions.
17 changes: 7 additions & 10 deletions novelwriter/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ def __init__(self):
# Text Editor
self.textFont = None # Editor font
self.textSize = 12 # Editor font size
self.textFixedW = True # Keep editor text fixed width
self.textWidth = 600 # Editor text width
self.textMargin = 40 # Editor/viewer text margin
self.tabWidth = 40 # Editor tabulator width
Expand Down Expand Up @@ -468,7 +467,6 @@ def loadConfig(self):
cnfSec = "Editor"
self.textFont = theConf.rdStr(cnfSec, "textfont", self.textFont)
self.textSize = theConf.rdInt(cnfSec, "textsize", self.textSize)
self.textFixedW = theConf.rdBool(cnfSec, "fixedwidth", self.textFixedW)
self.textWidth = theConf.rdInt(cnfSec, "width", self.textWidth)
self.textMargin = theConf.rdInt(cnfSec, "margin", self.textMargin)
self.tabWidth = theConf.rdInt(cnfSec, "tabwidth", self.tabWidth)
Expand Down Expand Up @@ -590,7 +588,6 @@ def saveConfig(self):
theConf["Editor"] = {
"textfont": str(self.textFont),
"textsize": str(self.textSize),
"fixedwidth": str(self.textFixedW),
"width": str(self.textWidth),
"margin": str(self.textMargin),
"tabwidth": str(self.tabWidth),
Expand Down Expand Up @@ -922,17 +919,17 @@ def getViewPanePos(self):
def getOutlinePanePos(self):
return [int(x*self.guiScale) for x in self.outlnPanePos]

def getTextWidth(self):
return self.pxInt(self.textWidth)
def getTextWidth(self, focusMode=False):
if focusMode:
return self.pxInt(max(self.focusWidth, 200))
else:
return self.pxInt(max(self.textWidth, 200))

def getTextMargin(self):
return self.pxInt(self.textMargin)
return self.pxInt(max(self.textMargin, 0))

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

def getFocusWidth(self):
return self.pxInt(self.focusWidth)
return self.pxInt(max(self.tabWidth, 0))

def getErrData(self):
"""Compile and return error messages from the initialisation of
Expand Down
20 changes: 5 additions & 15 deletions novelwriter/dialogs/preferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -537,39 +537,30 @@ def __init__(self, theParent):

# Max Text Width in Normal Mode
self.textWidth = QSpinBox(self)
self.textWidth.setMinimum(300)
self.textWidth.setMinimum(0)
self.textWidth.setMaximum(10000)
self.textWidth.setSingleStep(10)
self.textWidth.setValue(self.mainConf.textWidth)
self.mainForm.addRow(
self.tr("Maximum text width in \"Normal Mode\""),
self.textWidth,
self.tr("Horizontal margins are scaled automatically."),
self.tr("Set to 0 to disable this feature."),
theUnit=self.tr("px")
)

# Max Text Width in Focus Mode
self.focusWidth = QSpinBox(self)
self.focusWidth.setMinimum(300)
self.focusWidth.setMinimum(200)
self.focusWidth.setMaximum(10000)
self.focusWidth.setSingleStep(10)
self.focusWidth.setValue(self.mainConf.focusWidth)
self.mainForm.addRow(
self.tr("Maximum text width in \"Focus Mode\""),
self.focusWidth,
self.tr("Horizontal margins are scaled automatically."),
self.tr("The maximum width cannot be disabled."),
theUnit=self.tr("px")
)

# Document Fixed Width
self.textFixedW = QSwitch()
self.textFixedW.setChecked(not self.mainConf.textFixedW)
self.mainForm.addRow(
self.tr("Disable maximum text width in \"Normal Mode\""),
self.textFixedW,
self.tr("Text width is defined by the margins only.")
)

# Focus Mode Footer
self.hideFocusFooter = QSwitch()
self.hideFocusFooter.setChecked(self.mainConf.hideFocusFooter)
Expand Down Expand Up @@ -597,7 +588,7 @@ def __init__(self, theParent):
self.mainForm.addRow(
self.tr("Text margin"),
self.textMargin,
self.tr("If maximum width is set, this becomes the minimum margin."),
self.tr("The minimum margin around the text in the editor and viewer."),
theUnit=self.tr("px")
)

Expand Down Expand Up @@ -626,7 +617,6 @@ def saveValues(self):
# Text Flow
self.mainConf.textWidth = self.textWidth.value()
self.mainConf.focusWidth = self.focusWidth.value()
self.mainConf.textFixedW = not self.textFixedW.isChecked()
self.mainConf.hideFocusFooter = self.hideFocusFooter.isChecked()
self.mainConf.doJustify = self.doJustify.isChecked()
self.mainConf.textMargin = self.textMargin.value()
Expand Down
21 changes: 7 additions & 14 deletions novelwriter/gui/doceditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,8 +509,8 @@ def saveText(self):

def updateDocMargins(self):
"""Automatically adjust the margins so the text is centred if
Config.textFixedW is enabled or we're in Focus Mode. Otherwise,
just ensure the margins are set correctly.
we have a text width set or we're in Focus Mode. Otherwise, just
ensure the margins are set correctly.
"""
wW = self.width()
wH = self.height()
Expand All @@ -522,16 +522,10 @@ def updateDocMargins(self):
hBar = self.horizontalScrollBar()
sH = hBar.height() if hBar.isVisible() else 0

if self.mainConf.textFixedW or self.theParent.isFocusMode:
if self.theParent.isFocusMode:
tW = self.mainConf.getFocusWidth()
else:
tW = self.mainConf.getTextWidth()
tM = (wW - sW - tW)//2
if tM < cM:
tM = cM
else:
tM = cM
tM = cM
if self.mainConf.textWidth > 0 or self.theParent.isFocusMode:
tW = self.mainConf.getTextWidth(self.theParent.isFocusMode)
tM = max((wW - sW - tW)//2, cM)

tB = self.frameWidth()
tW = wW - 2*tB - sW
Expand All @@ -541,13 +535,12 @@ def updateDocMargins(self):
self.docHeader.setGeometry(tB, tB, tW, tH)
self.docFooter.setGeometry(tB, fY, tW, fH)

rH = 0
if self.docSearch.isVisible():
rH = self.docSearch.height()
rW = self.docSearch.width()
rL = wW - sW - rW - 2*tB
self.docSearch.move(rL, 2*tB)
else:
rH = 0

uM = max(cM, tH, rH)
lM = max(cM, fH)
Expand Down
16 changes: 12 additions & 4 deletions novelwriter/gui/docviewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,22 +313,30 @@ def clearNavHistory(self):
def updateDocMargins(self):
"""Automatically adjust the margins so the text is centred.
"""
wW = self.width()
wH = self.height()
cM = self.mainConf.getTextMargin()

vBar = self.verticalScrollBar()
sW = vBar.width() if vBar.isVisible() else 0

hBar = self.horizontalScrollBar()
sH = hBar.height() if hBar.isVisible() else 0

cM = self.mainConf.getTextMargin()
tM = cM
if self.mainConf.textWidth > 0:
tW = self.mainConf.getTextWidth()
tM = max((wW - sW - tW)//2, cM)

tB = self.frameWidth()
tW = self.width() - 2*tB - sW
tW = wW - 2*tB - sW
tH = self.docHeader.height()
fH = self.docFooter.height()
fY = self.height() - fH - tB - sH
fY = wH - fH - tB - sH

self.docHeader.setGeometry(tB, tB, tW, tH)
self.docFooter.setGeometry(tB, fY, tW, fH)
self.setViewportMargins(cM, max(cM, tH), cM, max(cM, fH))
self.setViewportMargins(tM, max(cM, tH), tM, max(cM, fH))

return

Expand Down
3 changes: 1 addition & 2 deletions tests/reference/baseConfig_novelwriter.conf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[Main]
timestamp = 2021-09-15 14:39:52
timestamp = 2021-12-31 16:45:32
theme = default
syntax = default_light
icons = typicons_light
Expand Down Expand Up @@ -30,7 +30,6 @@ emphlabels = True
[Editor]
textfont = None
textsize = 12
fixedwidth = True
width = 600
margin = 40
tabwidth = 40
Expand Down
3 changes: 1 addition & 2 deletions tests/reference/guiPreferences_novelwriter.conf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[Main]
timestamp = 2021-09-15 14:29:30
timestamp = 2021-12-31 16:45:34
theme = default
syntax = default_light
icons = typicons_light
Expand Down Expand Up @@ -30,7 +30,6 @@ emphlabels = True
[Editor]
textfont = None
textsize = 13
fixedwidth = False
width = 700
margin = 45
tabwidth = 45
Expand Down
8 changes: 4 additions & 4 deletions tests/test_base/test_base_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,16 +505,16 @@ def testBaseConfig_SettersGetters(tmpConf, tmpDir, outDir, refDir):
# ============

tmpConf.guiScale = 1.0
assert tmpConf.getTextWidth() == 600
assert tmpConf.getTextWidth(False) == 600
assert tmpConf.getTextWidth(True) == 800
assert tmpConf.getTextMargin() == 40
assert tmpConf.getTabWidth() == 40
assert tmpConf.getFocusWidth() == 800

tmpConf.guiScale = 2.0
assert tmpConf.getTextWidth() == 1200
assert tmpConf.getTextWidth(False) == 1200
assert tmpConf.getTextWidth(True) == 1600
assert tmpConf.getTextMargin() == 80
assert tmpConf.getTabWidth() == 80
assert tmpConf.getFocusWidth() == 1600

# Flag Setters
# ============
Expand Down
5 changes: 0 additions & 5 deletions tests/test_dialogs/test_dlg_preferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,6 @@ def testDlgPreferences_Main(qtbot, monkeypatch, fncDir, outDir, refDir):
tabDocs.textMargin.setValue(45)
tabDocs.tabWidth.setValue(45)

qtbot.wait(keyDelay)
assert not tabDocs.textFixedW.isChecked()
qtbot.mouseClick(tabDocs.textFixedW, Qt.LeftButton)
assert tabDocs.textFixedW.isChecked()

qtbot.wait(keyDelay)
assert not tabDocs.hideFocusFooter.isChecked()
qtbot.mouseClick(tabDocs.hideFocusFooter, Qt.LeftButton)
Expand Down

0 comments on commit 7024254

Please sign in to comment.