Remove redundant cssCol helper

This commit is contained in:
Veronica Berglyd Olsen
2025-01-22 21:52:50 +01:00
parent eb34a32760
commit 34645be975
6 changed files with 41 additions and 54 deletions
+1 -6
View File
@@ -38,7 +38,7 @@ from urllib.parse import urljoin
from urllib.request import pathname2url from urllib.request import pathname2url
from PyQt6.QtCore import QCoreApplication, QMimeData, QUrl from PyQt6.QtCore import QCoreApplication, QMimeData, QUrl
from PyQt6.QtGui import QAction, QColor, QDesktopServices, QFont, QFontDatabase, QFontInfo from PyQt6.QtGui import QAction, QDesktopServices, QFont, QFontDatabase, QFontInfo
from PyQt6.QtWidgets import QMenu, QMenuBar, QWidget from PyQt6.QtWidgets import QMenu, QMenuBar, QWidget
from novelwriter.constants import nwConst, nwLabels, nwUnicode, trConst from novelwriter.constants import nwConst, nwLabels, nwUnicode, trConst
@@ -417,11 +417,6 @@ def numberToRoman(value: int, toLower: bool = False) -> str:
# Qt Helpers # Qt Helpers
## ##
def cssCol(col: QColor, alpha: int | None = None) -> str:
"""Convert a QColor object to an rgba entry to use in CSS."""
return f"rgba({col.red()}, {col.green()}, {col.blue()}, {alpha or col.alpha()})"
def describeFont(font: QFont) -> str: def describeFont(font: QFont) -> str:
"""Describe a font in a way that can be displayed on the GUI.""" """Describe a font in a way that can be displayed on the GUI."""
if isinstance(font, QFont): if isinstance(font, QFont):
+3 -3
View File
@@ -31,11 +31,11 @@ from PyQt6.QtWidgets import (
) )
from novelwriter import CONFIG, SHARED from novelwriter import CONFIG, SHARED
from novelwriter.common import cssCol, readTextFile from novelwriter.common import readTextFile
from novelwriter.extensions.configlayout import NColorLabel from novelwriter.extensions.configlayout import NColorLabel
from novelwriter.extensions.modified import NDialog from novelwriter.extensions.modified import NDialog
from novelwriter.extensions.versioninfo import VersionInfoWidget from novelwriter.extensions.versioninfo import VersionInfoWidget
from novelwriter.types import QtAlignRightTop, QtDialogClose from novelwriter.types import QtAlignRightTop, QtDialogClose, QtHexArgb
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -135,7 +135,7 @@ class GuiAbout(NDialog):
def _setStyleSheet(self) -> None: def _setStyleSheet(self) -> None:
"""Set stylesheet text document.""" """Set stylesheet text document."""
baseCol = cssCol(self.palette().window().color()) baseCol = self.palette().window().color().name(QtHexArgb)
self.txtCredits.setStyleSheet( self.txtCredits.setStyleSheet(
f"QTextBrowser {{border: none; background: {baseCol};}} " f"QTextBrowser {{border: none; background: {baseCol};}} "
) )
+9 -7
View File
@@ -37,12 +37,12 @@ from PyQt6.QtGui import (
from PyQt6.QtWidgets import QApplication from PyQt6.QtWidgets import QApplication
from novelwriter import CONFIG from novelwriter import CONFIG
from novelwriter.common import NWConfigParser, cssCol, minmax from novelwriter.common import NWConfigParser, minmax
from novelwriter.config import DEF_GUI, DEF_ICONS, DEF_SYNTAX from novelwriter.config import DEF_GUI, DEF_ICONS, DEF_SYNTAX
from novelwriter.constants import nwLabels from novelwriter.constants import nwLabels
from novelwriter.enum import nwItemClass, nwItemLayout, nwItemType from novelwriter.enum import nwItemClass, nwItemLayout, nwItemType
from novelwriter.error import logException from novelwriter.error import logException
from novelwriter.types import QtBlack, QtPaintAntiAlias, QtTransparent from novelwriter.types import QtBlack, QtHexArgb, QtPaintAntiAlias, QtTransparent
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -528,27 +528,29 @@ class GuiTheme:
"""Build default style sheets.""" """Build default style sheets."""
self._styleSheets = {} self._styleSheets = {}
tCol = palette.text().color() text = palette.text().color()
hCol = palette.highlight().color() text.setAlpha(48)
tCol = text.name(QtHexArgb)
hCol = palette.highlight().color().name(QtHexArgb)
# Flat Tab Widget and Tab Bar: # Flat Tab Widget and Tab Bar:
self._styleSheets[STYLES_FLAT_TABS] = ( self._styleSheets[STYLES_FLAT_TABS] = (
"QTabWidget::pane {border: 0;} " "QTabWidget::pane {border: 0;} "
"QTabWidget QTabBar::tab {border: 0; padding: 4px 8px;} " "QTabWidget QTabBar::tab {border: 0; padding: 4px 8px;} "
f"QTabWidget QTabBar::tab:selected {{color: {cssCol(hCol)};}} " f"QTabWidget QTabBar::tab:selected {{color: {hCol};}} "
) )
# Minimal Tool Button # Minimal Tool Button
self._styleSheets[STYLES_MIN_TOOLBUTTON] = ( self._styleSheets[STYLES_MIN_TOOLBUTTON] = (
"QToolButton {padding: 2px; margin: 0; border: none; background: transparent;} " "QToolButton {padding: 2px; margin: 0; border: none; background: transparent;} "
f"QToolButton:hover {{border: none; background: {cssCol(tCol, 48)};}} " f"QToolButton:hover {{border: none; background: {tCol};}} "
"QToolButton::menu-indicator {image: none;} " "QToolButton::menu-indicator {image: none;} "
) )
# Big Tool Button # Big Tool Button
self._styleSheets[STYLES_BIG_TOOLBUTTON] = ( self._styleSheets[STYLES_BIG_TOOLBUTTON] = (
"QToolButton {padding: 6px; margin: 0; border: none; background: transparent;} " "QToolButton {padding: 6px; margin: 0; border: none; background: transparent;} "
f"QToolButton:hover {{border: none; background: {cssCol(tCol, 48)};}} " f"QToolButton:hover {{border: none; background: {tCol};}} "
"QToolButton::menu-indicator {image: none;} " "QToolButton::menu-indicator {image: none;} "
) )
+12 -18
View File
@@ -36,10 +36,10 @@ from PyQt6.QtWidgets import (
) )
from novelwriter import CONFIG, SHARED from novelwriter import CONFIG, SHARED
from novelwriter.common import cssCol, formatFileFilter, formatInt, getFileSize, openExternalPath from novelwriter.common import formatFileFilter, formatInt, getFileSize, openExternalPath
from novelwriter.error import formatException from novelwriter.error import formatException
from novelwriter.extensions.modified import NIconToolButton, NNonBlockingDialog from novelwriter.extensions.modified import NIconToolButton, NNonBlockingDialog
from novelwriter.types import QtDialogClose from novelwriter.types import QtDialogClose, QtHexArgb
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -101,7 +101,6 @@ class GuiDictionaries(NNonBlockingDialog):
# Info Box # Info Box
self.infoBox = QPlainTextEdit(self) self.infoBox = QPlainTextEdit(self)
self.infoBox.setReadOnly(True) self.infoBox.setReadOnly(True)
self.infoBox.setFixedHeight(4*SHARED.theme.fontPixelSize)
self.infoBox.setFrameStyle(QFrame.Shape.NoFrame) self.infoBox.setFrameStyle(QFrame.Shape.NoFrame)
# Buttons # Buttons
@@ -109,21 +108,16 @@ class GuiDictionaries(NNonBlockingDialog):
self.buttonBox.rejected.connect(self.reject) self.buttonBox.rejected.connect(self.reject)
# Assemble # Assemble
self.innerBox = QVBoxLayout()
self.innerBox.addWidget(self.huInfo)
self.innerBox.addLayout(self.huPathBox)
self.innerBox.addLayout(self.huAddBox)
self.innerBox.addSpacing(8)
self.innerBox.addWidget(self.inInfo)
self.innerBox.addLayout(self.inBox)
self.innerBox.addWidget(self.infoBox)
self.innerBox.setSpacing(4)
self.outerBox = QVBoxLayout() self.outerBox = QVBoxLayout()
self.outerBox.addLayout(self.innerBox, 0) self.outerBox.addWidget(self.huInfo, 0)
self.outerBox.addStretch(1) self.outerBox.addLayout(self.huPathBox, 0)
self.outerBox.addLayout(self.huAddBox, 0)
self.outerBox.addSpacing(8)
self.outerBox.addWidget(self.inInfo, 0)
self.outerBox.addLayout(self.inBox, 0)
self.outerBox.addWidget(self.infoBox, 1)
self.outerBox.addSpacing(8)
self.outerBox.addWidget(self.buttonBox, 0) self.outerBox.addWidget(self.buttonBox, 0)
self.outerBox.setSpacing(16)
self.setLayout(self.outerBox) self.setLayout(self.outerBox)
@@ -248,8 +242,8 @@ class GuiDictionaries(NNonBlockingDialog):
cursor.movePosition(QTextCursor.MoveOperation.End) cursor.movePosition(QTextCursor.MoveOperation.End)
if cursor.position() > 0: if cursor.position() > 0:
cursor.insertText("\n") cursor.insertText("\n")
textCol = cssCol(SHARED.theme.errorText if err else self.palette().text().color()) textCol = SHARED.theme.errorText if err else self.palette().text().color()
cursor.insertHtml(f"<font style='color: {textCol}'>{text}</font>") cursor.insertHtml(f"<font style='color: {textCol.name(QtHexArgb)}'>{text}</font>")
cursor.movePosition(QTextCursor.MoveOperation.End) cursor.movePosition(QTextCursor.MoveOperation.End)
cursor.deleteChar() cursor.deleteChar()
self.infoBox.setTextCursor(cursor) self.infoBox.setTextCursor(cursor)
+8 -4
View File
@@ -40,7 +40,7 @@ from PyQt6.QtWidgets import (
) )
from novelwriter import CONFIG, SHARED from novelwriter import CONFIG, SHARED
from novelwriter.common import cssCol, formatInt, makeFileNameSafe, qtAddAction, qtLambda from novelwriter.common import formatInt, makeFileNameSafe, qtAddAction, qtLambda
from novelwriter.constants import nwFiles from novelwriter.constants import nwFiles
from novelwriter.core.coretools import ProjectBuilder from novelwriter.core.coretools import ProjectBuilder
from novelwriter.enum import nwItemClass from novelwriter.enum import nwItemClass
@@ -48,7 +48,7 @@ from novelwriter.extensions.configlayout import NWrappedWidgetBox
from novelwriter.extensions.modified import NDialog, NIconToolButton, NSpinBox from novelwriter.extensions.modified import NDialog, NIconToolButton, NSpinBox
from novelwriter.extensions.switch import NSwitch from novelwriter.extensions.switch import NSwitch
from novelwriter.extensions.versioninfo import VersionInfoWidget from novelwriter.extensions.versioninfo import VersionInfoWidget
from novelwriter.types import QtAlignLeft, QtAlignRightTop, QtScrollAsNeeded, QtSelected from novelwriter.types import QtAlignLeft, QtAlignRightTop, QtHexArgb, QtScrollAsNeeded, QtSelected
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -301,7 +301,9 @@ class _OpenProjectPage(QWidget):
self._selectFirstItem() self._selectFirstItem()
baseCol = cssCol(self.palette().base().color(), PANEL_ALPHA) base = self.palette().base().color()
base.setAlpha(PANEL_ALPHA)
baseCol = base.name(QtHexArgb)
self.setStyleSheet( self.setStyleSheet(
f"QListView {{border: none; background: {baseCol};}} " f"QListView {{border: none; background: {baseCol};}} "
f"QLineEdit {{border: none; background: {baseCol}; padding: 4px;}} " f"QLineEdit {{border: none; background: {baseCol}; padding: 4px;}} "
@@ -507,7 +509,9 @@ class _NewProjectPage(QWidget):
# Styles # Styles
# ====== # ======
baseCol = cssCol(self.palette().base().color(), PANEL_ALPHA) base = self.palette().base().color()
base.setAlpha(PANEL_ALPHA)
baseCol = base.name(QtHexArgb)
self.setStyleSheet( self.setStyleSheet(
f"QScrollArea {{border: none; background: {baseCol};}} " f"QScrollArea {{border: none; background: {baseCol};}} "
f"_NewProjectForm {{border: none; background: {baseCol};}} " f"_NewProjectForm {{border: none; background: {baseCol};}} "
+8 -16
View File
@@ -28,18 +28,17 @@ from xml.etree import ElementTree as ET
import pytest import pytest
from PyQt6.QtCore import QMimeData, QUrl from PyQt6.QtCore import QMimeData, QUrl
from PyQt6.QtGui import QColor, QDesktopServices, QFont, QFontDatabase, QFontInfo from PyQt6.QtGui import QDesktopServices, QFont, QFontDatabase, QFontInfo
from novelwriter.common import ( from novelwriter.common import (
NWConfigParser, checkBool, checkFloat, checkInt, checkIntTuple, checkPath, NWConfigParser, checkBool, checkFloat, checkInt, checkIntTuple, checkPath,
checkString, checkStringNone, checkUuid, compact, cssCol, checkString, checkStringNone, checkUuid, compact, decodeMimeHandles,
decodeMimeHandles, describeFont, elide, encodeMimeHandles, firstFloat, describeFont, elide, encodeMimeHandles, firstFloat, fontMatcher,
fontMatcher, formatFileFilter, formatInt, formatTime, formatTimeStamp, formatFileFilter, formatInt, formatTime, formatTimeStamp, formatVersion,
formatVersion, fuzzyTime, getFileSize, hexToInt, isHandle, isItemClass, fuzzyTime, getFileSize, hexToInt, isHandle, isItemClass, isItemLayout,
isItemLayout, isItemType, isListInstance, isTitleTag, jsonEncode, isItemType, isListInstance, isTitleTag, jsonEncode, makeFileNameSafe,
makeFileNameSafe, minmax, numberToRoman, openExternalPath, readTextFile, minmax, numberToRoman, openExternalPath, readTextFile, simplified,
simplified, transferCase, uniqueCompact, xmlElement, xmlIndent, xmlSubElem, transferCase, uniqueCompact, xmlElement, xmlIndent, xmlSubElem, yesNo
yesNo
) )
from tests.mocked import causeOSError from tests.mocked import causeOSError
@@ -512,13 +511,6 @@ def testBaseCommon_numberToRoman():
assert numberToRoman(999, True) == "cmxcix" assert numberToRoman(999, True) == "cmxcix"
@pytest.mark.base
def testBaseCommon_cssCol():
"""Test the cssCol function."""
assert cssCol(QColor(0, 0, 0, 0)) == "rgba(0, 0, 0, 0)"
assert cssCol(QColor(10, 20, 30, 40)) == "rgba(10, 20, 30, 40)"
@pytest.mark.base @pytest.mark.base
def testBaseCommon_describeFont(): def testBaseCommon_describeFont():
"""Test the describeFont function.""" """Test the describeFont function."""