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