Update config layout extension class

This commit is contained in:
Veronica Berglyd Olsen
2023-05-23 16:39:05 +02:00
parent 2592423637
commit 06b46bb0d3
2 changed files with 125 additions and 80 deletions
+19 -19
View File
@@ -230,7 +230,7 @@ class GuiPreferencesGeneral(QWidget):
self.tr("Font family"), self.tr("Font family"),
self.guiFont, self.guiFont,
self.tr("Requires restart to take effect."), self.tr("Requires restart to take effect."),
theButton=self.fontButton button=self.fontButton
) )
# Font Size # Font Size
@@ -243,7 +243,7 @@ class GuiPreferencesGeneral(QWidget):
self.tr("Font size"), self.tr("Font size"),
self.guiFontSize, self.guiFontSize,
self.tr("Requires restart to take effect."), self.tr("Requires restart to take effect."),
theUnit=self.tr("pt") unit=self.tr("pt")
) )
# GUI Settings # GUI Settings
@@ -360,7 +360,7 @@ class GuiPreferencesProjects(QWidget):
self.tr("Save document interval"), self.tr("Save document interval"),
self.autoSaveDoc, self.autoSaveDoc,
self.tr("How often the document is automatically saved."), self.tr("How often the document is automatically saved."),
theUnit=self.tr("seconds") unit=self.tr("seconds")
) )
# Project Save Timer # Project Save Timer
@@ -373,7 +373,7 @@ class GuiPreferencesProjects(QWidget):
self.tr("Save project interval"), self.tr("Save project interval"),
self.autoSaveProj, self.autoSaveProj,
self.tr("How often the project is automatically saved."), self.tr("How often the project is automatically saved."),
theUnit=self.tr("seconds") unit=self.tr("seconds")
) )
# Project Backup # Project Backup
@@ -435,7 +435,7 @@ class GuiPreferencesProjects(QWidget):
self.tr("Editor inactive time before pausing timer"), self.tr("Editor inactive time before pausing timer"),
self.userIdleTime, self.userIdleTime,
self.tr("User activity includes typing and changing the content."), self.tr("User activity includes typing and changing the content."),
theUnit=self.tr("minutes") unit=self.tr("minutes")
) )
return return
@@ -517,7 +517,7 @@ class GuiPreferencesDocuments(QWidget):
self.tr("Font family"), self.tr("Font family"),
self.textFont, self.textFont,
self.tr("Applies to both document editor and viewer."), self.tr("Applies to both document editor and viewer."),
theButton=self.fontButton button=self.fontButton
) )
# Font Size # Font Size
@@ -530,7 +530,7 @@ class GuiPreferencesDocuments(QWidget):
self.tr("Font size"), self.tr("Font size"),
self.textSize, self.textSize,
self.tr("Applies to both document editor and viewer."), self.tr("Applies to both document editor and viewer."),
theUnit=self.tr("pt") unit=self.tr("pt")
) )
# Text Flow # Text Flow
@@ -547,7 +547,7 @@ class GuiPreferencesDocuments(QWidget):
self.tr("Maximum text width in \"Normal Mode\""), self.tr("Maximum text width in \"Normal Mode\""),
self.textWidth, self.textWidth,
self.tr("Set to 0 to disable this feature."), self.tr("Set to 0 to disable this feature."),
theUnit=self.tr("px") unit=self.tr("px")
) )
# Max Text Width in Focus Mode # Max Text Width in Focus Mode
@@ -560,7 +560,7 @@ class GuiPreferencesDocuments(QWidget):
self.tr("Maximum text width in \"Focus Mode\""), self.tr("Maximum text width in \"Focus Mode\""),
self.focusWidth, self.focusWidth,
self.tr("The maximum width cannot be disabled."), self.tr("The maximum width cannot be disabled."),
theUnit=self.tr("px") unit=self.tr("px")
) )
# Focus Mode Footer # Focus Mode Footer
@@ -591,7 +591,7 @@ class GuiPreferencesDocuments(QWidget):
self.tr("Minimum text margin"), self.tr("Minimum text margin"),
self.textMargin, self.textMargin,
self.tr("Applies to both document editor and viewer."), self.tr("Applies to both document editor and viewer."),
theUnit=self.tr("px") unit=self.tr("px")
) )
# Tab Width # Tab Width
@@ -604,7 +604,7 @@ class GuiPreferencesDocuments(QWidget):
self.tr("Tab width"), self.tr("Tab width"),
self.tabWidth, self.tabWidth,
self.tr("The width of a tab key press in the editor and viewer."), self.tr("The width of a tab key press in the editor and viewer."),
theUnit=self.tr("px") unit=self.tr("px")
) )
return return
@@ -703,7 +703,7 @@ class GuiPreferencesEditor(QWidget):
self.tr("Big document limit"), self.tr("Big document limit"),
self.bigDocLimit, self.bigDocLimit,
self.tr("Full spell checking is disabled above this limit."), self.tr("Full spell checking is disabled above this limit."),
theUnit=self.tr("kB") unit=self.tr("kB")
) )
# Word Count # Word Count
@@ -720,7 +720,7 @@ class GuiPreferencesEditor(QWidget):
self.mainForm.addRow( self.mainForm.addRow(
self.tr("Word count interval"), self.tr("Word count interval"),
self.wordCountTimer, self.wordCountTimer,
theUnit=self.tr("seconds") unit=self.tr("seconds")
) )
# Include Notes in Word Count # Include Notes in Word Count
@@ -765,7 +765,7 @@ class GuiPreferencesEditor(QWidget):
self.tr("Scroll past end of the document"), self.tr("Scroll past end of the document"),
self.scrollPastEnd, self.scrollPastEnd,
self.tr("Set to 0 to disable this feature."), self.tr("Set to 0 to disable this feature."),
theUnit=self.tr("lines") unit=self.tr("lines")
) )
# Typewriter Scrolling # Typewriter Scrolling
@@ -787,7 +787,7 @@ class GuiPreferencesEditor(QWidget):
self.tr("Minimum position for Typewriter scrolling"), self.tr("Minimum position for Typewriter scrolling"),
self.autoScrollPos, self.autoScrollPos,
self.tr("Percentage of the editor height from the top."), self.tr("Percentage of the editor height from the top."),
theUnit="%" unit="%"
) )
return return
@@ -1112,7 +1112,7 @@ class GuiPreferencesQuotes(QWidget):
self.tr("Single quote open style"), self.tr("Single quote open style"),
self.quoteSym["SO"], self.quoteSym["SO"],
self.tr("The symbol to use for a leading single quote."), self.tr("The symbol to use for a leading single quote."),
theButton=self.btnSingleStyleO button=self.btnSingleStyleO
) )
self.quoteSym["SC"] = QLineEdit() self.quoteSym["SC"] = QLineEdit()
@@ -1128,7 +1128,7 @@ class GuiPreferencesQuotes(QWidget):
self.tr("Single quote close style"), self.tr("Single quote close style"),
self.quoteSym["SC"], self.quoteSym["SC"],
self.tr("The symbol to use for a trailing single quote."), self.tr("The symbol to use for a trailing single quote."),
theButton=self.btnSingleStyleC button=self.btnSingleStyleC
) )
# Double Quote Style # Double Quote Style
@@ -1145,7 +1145,7 @@ class GuiPreferencesQuotes(QWidget):
self.tr("Double quote open style"), self.tr("Double quote open style"),
self.quoteSym["DO"], self.quoteSym["DO"],
self.tr("The symbol to use for a leading double quote."), self.tr("The symbol to use for a leading double quote."),
theButton=self.btnDoubleStyleO button=self.btnDoubleStyleO
) )
self.quoteSym["DC"] = QLineEdit() self.quoteSym["DC"] = QLineEdit()
@@ -1161,7 +1161,7 @@ class GuiPreferencesQuotes(QWidget):
self.tr("Double quote close style"), self.tr("Double quote close style"),
self.quoteSym["DC"], self.quoteSym["DC"],
self.tr("The symbol to use for a trailing double quote."), self.tr("The symbol to use for a trailing double quote."),
theButton=self.btnDoubleStyleC button=self.btnDoubleStyleC
) )
return return
+106 -61
View File
@@ -22,16 +22,19 @@ General Public License for more details.
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
""" """
from __future__ import annotations
from PyQt5.QtGui import QColor, QPalette from PyQt5.QtGui import QColor, QPalette
from PyQt5.QtCore import Qt from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import ( from PyQt5.QtWidgets import (
QGridLayout, QHBoxLayout, QLabel, QLineEdit, QSizePolicy, QVBoxLayout, QAbstractButton, QGridLayout, QHBoxLayout, QLabel, QLineEdit, QSizePolicy,
QWidget QVBoxLayout, QWidget
) )
from novelwriter import CONFIG from novelwriter import CONFIG
FONT_SCALE = 0.9
class NConfigLayout(QGridLayout): class NConfigLayout(QGridLayout):
@@ -40,8 +43,7 @@ class NConfigLayout(QGridLayout):
self._nextRow = 0 self._nextRow = 0
self._helpCol = QColor(0, 0, 0) self._helpCol = QColor(0, 0, 0)
self._fontScale = 0.9 self._fontScale = FONT_SCALE
self._itemMap = {} self._itemMap = {}
wSp = CONFIG.pxInt(8) wSp = CONFIG.pxInt(8)
@@ -55,81 +57,65 @@ class NConfigLayout(QGridLayout):
# Getters and Setters # Getters and Setters
## ##
def setHelpTextStyle(self, helpCol, fontScale=0.9): def setHelpTextStyle(self, color: QColor | list | tuple, fontScale: float = FONT_SCALE):
"""Set the text color for the help text. """Set the text color for the help text.
""" """
if isinstance(helpCol, QColor): if isinstance(color, QColor):
self._helpCol = helpCol self._helpCol = color
else: else:
self._helpCol = QColor(*helpCol) self._helpCol = QColor(*color)
self._fontScale = fontScale self._fontScale = fontScale
return return
def setHelpText(self, intRow, theText): def setHelpText(self, row: int, text: str):
"""Set the text for the help label. """Set the text for the help label.
""" """
if intRow in self._itemMap: if row in self._itemMap:
self._itemMap[intRow]["help"].setText(theText) qHelp = self._itemMap[row][1]
if isinstance(qHelp, NHelpLabel):
qHelp.setText(text)
return return
def setLabelText(self, intRow, theText): def setLabelText(self, row: int, text: str):
"""Set the text for the main label. """Set the text for the main label.
""" """
if intRow in self._itemMap: if row in self._itemMap:
self._itemMap[intRow]["label"].setText(theText) self._itemMap[row](0).setText(text)
return return
## ##
# Class Methods # Class Methods
## ##
def addGroupLabel(self, theLabel): def addGroupLabel(self, label: str):
"""Adds a text label to separate groups of settings. """Adds a text label to separate groups of settings.
""" """
if isinstance(theLabel, QLabel):
qLabel = theLabel
elif isinstance(theLabel, str):
qLabel = QLabel("<b>%s</b>" % theLabel)
else:
qLabel = None
raise ValueError("theLabel must be a QLabel")
hM = CONFIG.pxInt(4) hM = CONFIG.pxInt(4)
qLabel = QLabel("<b>%s</b>" % label)
qLabel.setContentsMargins(0, hM, 0, hM) qLabel.setContentsMargins(0, hM, 0, hM)
self.addWidget(qLabel, self._nextRow, 0, 1, 2, Qt.AlignLeft) self.addWidget(qLabel, self._nextRow, 0, 1, 2, Qt.AlignLeft)
self.setRowStretch(self._nextRow, 0) self.setRowStretch(self._nextRow, 0)
self.setRowStretch(self._nextRow + 1, 1) self.setRowStretch(self._nextRow + 1, 1)
self._nextRow += 1 self._nextRow += 1
return return
def addRow(self, theLabel, theWidget, helpText=None, theUnit=None, theButton=None): def addRow(
self, label: str, widget: QWidget, helpText: str | None = None,
unit: str | None = None, button: QWidget | None = None
) -> int:
"""Add a label and a widget as a new row of the grid. """Add a label and a widget as a new row of the grid.
""" """
thisEntry = { if isinstance(widget, QWidget):
"label": None, qWidget = widget
"help": None,
"widget": None,
}
if isinstance(theLabel, QLabel):
qLabel = theLabel
elif isinstance(theLabel, str):
qLabel = QLabel(theLabel)
else:
qLabel = None
raise ValueError("theLabel must be a QLabel")
if isinstance(theWidget, QWidget):
qWidget = theWidget
else: else:
qWidget = None qWidget = None
raise ValueError("theWidget must be a QWidget") raise ValueError("The widget must be a QWidget")
wSp = CONFIG.pxInt(8) wSp = CONFIG.pxInt(8)
qLabel = QLabel(label)
qLabel.setIndent(wSp) qLabel.setIndent(wSp)
qHelp = None
if helpText is not None: if helpText is not None:
qHelp = NHelpLabel(str(helpText), self._helpCol, self._fontScale) qHelp = NHelpLabel(str(helpText), self._helpCol, self._fontScale)
qHelp.setIndent(wSp) qHelp.setIndent(wSp)
@@ -140,30 +126,29 @@ class NConfigLayout(QGridLayout):
labelBox.setSpacing(0) labelBox.setSpacing(0)
labelBox.addStretch(1) labelBox.addStretch(1)
thisEntry["help"] = qHelp
self.addLayout(labelBox, self._nextRow, 0, 1, 1, Qt.AlignLeft | Qt.AlignTop) self.addLayout(labelBox, self._nextRow, 0, 1, 1, Qt.AlignLeft | Qt.AlignTop)
else: else:
self.addWidget(qLabel, self._nextRow, 0, 1, 1, Qt.AlignLeft | Qt.AlignTop) self.addWidget(qLabel, self._nextRow, 0, 1, 1, Qt.AlignLeft | Qt.AlignTop)
if theUnit is not None: if isinstance(unit, str):
controlBox = QHBoxLayout() controlBox = QHBoxLayout()
controlBox.addWidget(qWidget, 0, Qt.AlignVCenter) controlBox.addWidget(qWidget, 0, Qt.AlignVCenter)
controlBox.addWidget(QLabel(theUnit), 0, Qt.AlignVCenter) controlBox.addWidget(QLabel(unit), 0, Qt.AlignVCenter)
controlBox.setSpacing(wSp) controlBox.setSpacing(wSp)
self.addLayout(controlBox, self._nextRow, 1, 1, 1, Qt.AlignRight | Qt.AlignTop) self.addLayout(controlBox, self._nextRow, 1, 1, 1, Qt.AlignRight | Qt.AlignTop)
elif theButton is not None: elif isinstance(button, QAbstractButton):
controlBox = QHBoxLayout() controlBox = QHBoxLayout()
controlBox.addWidget(qWidget, 0, Qt.AlignVCenter) controlBox.addWidget(qWidget, 0, Qt.AlignVCenter)
controlBox.addWidget(theButton, 0, Qt.AlignVCenter) controlBox.addWidget(button, 0, Qt.AlignVCenter)
controlBox.setSpacing(wSp) controlBox.setSpacing(wSp)
self.addLayout(controlBox, self._nextRow, 1, 1, 1, Qt.AlignRight | Qt.AlignTop) self.addLayout(controlBox, self._nextRow, 1, 1, 1, Qt.AlignRight | Qt.AlignTop)
else: else:
if isinstance(theWidget, QLineEdit): if isinstance(widget, QLineEdit):
qLayout = QHBoxLayout() qLayout = QHBoxLayout()
qLayout.addWidget(theWidget) qLayout.addWidget(widget)
self.addLayout(qLayout, self._nextRow, 1, 1, 1, Qt.AlignRight | Qt.AlignTop) self.addLayout(qLayout, self._nextRow, 1, 1, 1, Qt.AlignRight | Qt.AlignTop)
else: else:
self.addWidget(qWidget, self._nextRow, 1, 1, 1, Qt.AlignRight | Qt.AlignTop) self.addWidget(qWidget, self._nextRow, 1, 1, 1, Qt.AlignRight | Qt.AlignTop)
@@ -173,11 +158,7 @@ class NConfigLayout(QGridLayout):
self.setRowStretch(self._nextRow, 0) self.setRowStretch(self._nextRow, 0)
self.setRowStretch(self._nextRow+1, 1) self.setRowStretch(self._nextRow+1, 1)
thisEntry["label"] = qLabel self._itemMap[self._nextRow] = (qLabel, qHelp, qWidget)
thisEntry["widget"] = qWidget
self._itemMap[self._nextRow] = thisEntry
self._nextRow += 1 self._nextRow += 1
return self._nextRow - 1 return self._nextRow - 1
@@ -185,15 +166,79 @@ class NConfigLayout(QGridLayout):
# END Class NConfigLayout # END Class NConfigLayout
class NSimpleLayout(QGridLayout):
"""Similar to NConfigLayout, but only has a label + widget two
column layout.
"""
def __init__(self):
super().__init__()
self._nextRow = 0
wSp = CONFIG.pxInt(8)
self.setHorizontalSpacing(wSp)
self.setVerticalSpacing(wSp)
self.setColumnStretch(0, 1)
return
##
# Methods
##
def addGroupLabel(self, label: str):
"""Adds a text label to separate groups of settings.
"""
hM = CONFIG.pxInt(4)
qLabel = QLabel("<b>%s</b>" % label)
qLabel.setContentsMargins(0, hM, 0, hM)
self.addWidget(qLabel, self._nextRow, 0, 1, 2, Qt.AlignLeft)
self.setRowStretch(self._nextRow, 0)
self.setRowStretch(self._nextRow + 1, 1)
self._nextRow += 1
return
def addRow(self, label: str, widget: QWidget):
"""Add a label and a widget as a new row of the grid.
"""
if isinstance(widget, QWidget):
qWidget = widget
else:
qWidget = None
raise ValueError("The widget must be a QWidget")
wSp = CONFIG.pxInt(8)
qLabel = QLabel(label)
qLabel.setIndent(wSp)
self.addWidget(qLabel, self._nextRow, 0, 1, 1, Qt.AlignLeft | Qt.AlignTop)
if isinstance(widget, QLineEdit):
qLayout = QHBoxLayout()
qLayout.addWidget(widget)
self.addLayout(qLayout, self._nextRow, 1, 1, 1, Qt.AlignRight | Qt.AlignTop)
else:
self.addWidget(qWidget, self._nextRow, 1, 1, 1, Qt.AlignRight | Qt.AlignTop)
qLabel.setBuddy(qWidget)
self.setRowStretch(self._nextRow, 0)
self.setRowStretch(self._nextRow+1, 1)
self._nextRow += 1
return
# END Class NSimpleLayout
class NHelpLabel(QLabel): class NHelpLabel(QLabel):
def __init__(self, theText, textCol, fontSize=0.9): def __init__(self, text: str, color: QColor | list | tuple, fontSize: float = FONT_SCALE):
super().__init__(theText) super().__init__(text)
if isinstance(textCol, QColor): if isinstance(color, QColor):
qCol = textCol qCol = color
else: else:
qCol = QColor(*textCol) qCol = QColor(*color)
lblCol = self.palette() lblCol = self.palette()
lblCol.setColor(QPalette.WindowText, qCol) lblCol.setColor(QPalette.WindowText, qCol)