From a60e0b3c8740032730d251fc32cb0dd2fe5b169c Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Mon, 13 Jan 2025 00:38:50 +0100 Subject: [PATCH] Clean up warnings and deprecations in extensions and formats --- novelwriter/extensions/configlayout.py | 26 ++++---- novelwriter/extensions/pagedsidebar.py | 82 +++++++++++--------------- novelwriter/extensions/statusled.py | 15 ++--- novelwriter/extensions/switch.py | 15 +++-- novelwriter/extensions/versioninfo.py | 2 +- novelwriter/formats/toqdoc.py | 9 ++- 6 files changed, 67 insertions(+), 82 deletions(-) diff --git a/novelwriter/extensions/configlayout.py b/novelwriter/extensions/configlayout.py index 47b74175..f9a0d940 100644 --- a/novelwriter/extensions/configlayout.py +++ b/novelwriter/extensions/configlayout.py @@ -33,7 +33,6 @@ from PyQt6.QtWidgets import ( QVBoxLayout, QWidget ) -from novelwriter import CONFIG from novelwriter.types import QtScrollAsNeeded DEFAULT_SCALE = 0.9 @@ -99,14 +98,14 @@ class NScrollableForm(QScrollArea): self._helpCol = QColor(0, 0, 0) self._fontScale = DEFAULT_SCALE self._first = True - self._indent = CONFIG.pxInt(12) + self._indent = 12 self._sections: dict[int, QLabel] = {} self._editable: dict[str, NColourLabel] = {} self._index: dict[str, QWidget] = {} self._layout = QVBoxLayout() - self._layout.setSpacing(CONFIG.pxInt(12)) + self._layout.setSpacing(12) self._widget = QWidget(self) self._widget.setLayout(self._layout) @@ -156,24 +155,25 @@ class NScrollableForm(QScrollArea): def scrollToSection(self, identifier: int) -> None: """Scroll to the requested section identifier.""" if identifier in self._sections: - yPos = self._sections[identifier].pos().y() - CONFIG.pxInt(8) - self.verticalScrollBar().setValue(yPos) + yPos = self._sections[identifier].pos().y() - 8 + if vBar := self.verticalScrollBar(): + vBar.setValue(yPos) return def scrollToLabel(self, label: str) -> None: """Scroll to the requested label.""" if label in self._index: - yPos = self._index[label].pos().y() - CONFIG.pxInt(8) - self.verticalScrollBar().setValue(yPos) + yPos = self._index[label].pos().y() - 8 + if vBar := self.verticalScrollBar(): + vBar.setValue(yPos) return def addGroupLabel(self, label: str, identifier: int | None = None) -> None: """Add a text label to separate groups of settings.""" - hM = CONFIG.pxInt(4) qLabel = QLabel(f"{label}", self) - qLabel.setContentsMargins(0, hM, 0, hM) + qLabel.setContentsMargins(0, 4, 0, 4) if not self._first: - self._layout.addSpacing(5*hM) + self._layout.addSpacing(20) self._layout.addWidget(qLabel) self._first = False if identifier is not None: @@ -192,7 +192,7 @@ class NScrollableForm(QScrollArea): ) -> None: """Add a label and a widget as a new row of the form.""" row = QHBoxLayout() - row.setSpacing(CONFIG.pxInt(12)) + row.setSpacing(12) if isinstance(widget, list): wBox = QHBoxLayout() @@ -205,7 +205,7 @@ class NScrollableForm(QScrollArea): icon.setPixmap(item) wBox.addWidget(icon) elif isinstance(item, int): - wBox.addSpacing(CONFIG.pxInt(item)) + wBox.addSpacing(item) qWidget = QWidget(self) qWidget.setLayout(wBox) else: @@ -252,7 +252,7 @@ class NScrollableForm(QScrollArea): def finalise(self) -> None: """Finalise the layout when the form is built.""" - self._layout.addSpacing(CONFIG.pxInt(20)) + self._layout.addSpacing(20) self._layout.addStretch(1) return diff --git a/novelwriter/extensions/pagedsidebar.py b/novelwriter/extensions/pagedsidebar.py index 6dd835f3..b58de63a 100644 --- a/novelwriter/extensions/pagedsidebar.py +++ b/novelwriter/extensions/pagedsidebar.py @@ -26,10 +26,10 @@ along with this program. If not, see . from __future__ import annotations from PyQt6.QtCore import QPoint, QRectF, QSize, Qt, pyqtSignal, pyqtSlot -from PyQt6.QtGui import QAction, QColor, QPainter, QPaintEvent, QPolygon +from PyQt6.QtGui import QColor, QPainter, QPaintEvent, QPolygon from PyQt6.QtWidgets import ( - QAbstractButton, QButtonGroup, QLabel, QStyle, QStyleOptionToolButton, - QToolBar, QToolButton, QWidget + QAbstractButton, QButtonGroup, QLabel, QStyleOptionToolButton, QToolBar, + QToolButton, QWidget ) from novelwriter.types import ( @@ -83,21 +83,19 @@ class NPagedSideBar(QToolBar): self.insertWidget(self._stretchAction, label) return - def addButton(self, text: str, buttonId: int = -1) -> QAction: + def addButton(self, text: str, buttonId: int = -1) -> None: """Add a new button to the toolbar.""" button = _PagedToolButton(self) button.setText(text) - - action = self.insertWidget(self._stretchAction, button) + self.insertWidget(self._stretchAction, button) self._group.addButton(button, id=buttonId) - self._buttons[buttonId] = button - - return action + return def setSelected(self, buttonId: int) -> None: """Set the selected button.""" - self._group.button(buttonId).setChecked(True) + if button := self._group.button(buttonId): + button.setChecked(True) return ## @@ -115,7 +113,7 @@ class NPagedSideBar(QToolBar): class _PagedToolButton(QToolButton): - __slots__ = ("_bH", "_tM", "_lM", "_cR", "_aH") + __slots__ = ("_bH", "_tM", "_aH") def __init__(self, parent: QWidget) -> None: super().__init__(parent=parent) @@ -126,8 +124,6 @@ class _PagedToolButton(QToolButton): fH = self.fontMetrics().height() self._bH = round(fH * 1.7) self._tM = (self._bH - fH)//2 - self._lM = 3*self.style().pixelMetric(QStyle.PixelMetric.PM_ButtonMargin)//2 - self._cR = self._lM//2 self._aH = 2*fH//7 self.setFixedHeight(self._bH) @@ -145,53 +141,51 @@ class _PagedToolButton(QToolButton): opt = QStyleOptionToolButton() opt.initFrom(self) - paint = QPainter(self) - paint.setRenderHint(QtPaintAntiAlias, True) - paint.setPen(QtNoPen) - paint.setBrush(QtNoBrush) + painter = QPainter(self) + painter.setRenderHint(QtPaintAntiAlias, True) + painter.setPen(QtNoPen) + painter.setBrush(QtNoBrush) width = self.width() height = self.height() palette = self.palette() if opt.state & QtMouseOver == QtMouseOver: # pragma: no cover - backCol = palette.base() - paint.setBrush(backCol) - paint.setOpacity(0.75) - paint.drawRoundedRect(0, 0, width, height, self._cR, self._cR) + painter.setBrush(palette.light()) + painter.drawRoundedRect(0, 0, width, height, 4, 4) if self.isChecked(): - backCol = palette.highlight() - paint.setBrush(backCol) - paint.setOpacity(0.35) - paint.drawRoundedRect(0, 0, width, height, self._cR, self._cR) + painter.setBrush(palette.highlight()) + painter.setOpacity(0.35) + painter.drawRoundedRect(0, 0, width, height, 4, 4) textCol = palette.highlightedText().color() else: textCol = palette.text().color() - tW = width - 2*self._lM + tW = width - 24 tH = height - 2*self._tM - paint.setPen(textCol) - paint.setOpacity(1.0) - paint.drawText(QRectF(self._lM, self._tM, tW, tH), QtAlignLeft, self.text()) + painter.setPen(textCol) + painter.setOpacity(1.0) + painter.drawText(QRectF(12, self._tM, tW, tH), QtAlignLeft, self.text()) tC = self.height()//2 - tW = self.width() - self._aH - self._lM + tW = self.width() - self._aH - 12 if self.isChecked(): - paint.setBrush(textCol) - paint.drawPolygon(QPolygon([ + painter.setBrush(textCol) + painter.drawPolygon(QPolygon([ QPoint(tW, tC - self._aH), QPoint(tW + self._aH, tC), QPoint(tW, tC + self._aH), ])) + painter.end() return class _NPagedToolLabel(QLabel): - __slots__ = ("_bH", "_tM", "_lM", "_textCol") + __slots__ = ("_bH", "_tM", "_textCol") def __init__(self, parent: QWidget, textColor: QColor | None = None) -> None: super().__init__(parent=parent) @@ -201,7 +195,6 @@ class _NPagedToolLabel(QLabel): fH = self.fontMetrics().height() self._bH = round(fH * 1.7) self._tM = (self._bH - fH)//2 - self._lM = self.style().pixelMetric(QStyle.PixelMetric.PM_ButtonMargin)//2 self.setFixedHeight(self._bH) self._textCol = textColor or self.palette().text().color() @@ -212,18 +205,15 @@ class _NPagedToolLabel(QLabel): """Overload the paint event to draw a simple, left aligned text label that matches the button style. """ - paint = QPainter(self) - paint.setRenderHint(QtPaintAntiAlias, True) - paint.setPen(QtNoPen) + tW = self.width() - 8 + tH = self.height() - 2*self._tM - width = self.width() - height = self.height() - - tW = width - 2*self._lM - tH = height - 2*self._tM - - paint.setPen(self._textCol) - paint.setOpacity(1.0) - paint.drawText(QRectF(self._lM, self._tM, tW, tH), QtAlignLeft, self.text()) + painter = QPainter(self) + painter.setRenderHint(QtPaintAntiAlias, True) + painter.setPen(QtNoPen) + painter.setPen(self._textCol) + painter.setOpacity(1.0) + painter.drawText(QRectF(4, self._tM, tW, tH), QtAlignLeft, self.text()) + painter.end() return diff --git a/novelwriter/extensions/statusled.py b/novelwriter/extensions/statusled.py index f4377823..a027a112 100644 --- a/novelwriter/extensions/statusled.py +++ b/novelwriter/extensions/statusled.py @@ -28,7 +28,6 @@ import logging from PyQt6.QtGui import QColor, QPainter, QPaintEvent from PyQt6.QtWidgets import QAbstractButton, QWidget -from novelwriter import CONFIG from novelwriter.enum import nwTrinary from novelwriter.types import QtBlack, QtPaintAntiAlias @@ -37,9 +36,7 @@ logger = logging.getLogger(__name__) class StatusLED(QAbstractButton): - __slots__ = ( - "_neutral", "_postitve", "_negative", "_color", "_state", "_bPx" - ) + __slots__ = ("_neutral", "_postitve", "_negative", "_color", "_state") def __init__(self, sW: int, sH: int, parent: QWidget | None = None) -> None: super().__init__(parent=parent) @@ -48,7 +45,6 @@ class StatusLED(QAbstractButton): self._negative = QtBlack self._color = QtBlack self._state = nwTrinary.NEUTRAL - self._bPx = CONFIG.pxInt(1) self.setFixedWidth(sW) self.setFixedHeight(sH) return @@ -82,12 +78,9 @@ class StatusLED(QAbstractButton): """Draw the LED.""" painter = QPainter(self) painter.setRenderHint(QtPaintAntiAlias, True) - painter.setPen(self.palette().windowText().color()) + painter.setPen(self.palette().text().color()) painter.setBrush(self._color) painter.setOpacity(1.0) - painter.drawEllipse( - self._bPx, self._bPx, - self.width() - 2*self._bPx, - self.height() - 2*self._bPx - ) + painter.drawEllipse(1, 1, self.width() - 2, self.height() - 2) + painter.end() return diff --git a/novelwriter/extensions/switch.py b/novelwriter/extensions/switch.py index 2b320506..ef936553 100644 --- a/novelwriter/extensions/switch.py +++ b/novelwriter/extensions/switch.py @@ -27,13 +27,13 @@ from PyQt6.QtCore import QPropertyAnimation, Qt, pyqtProperty from PyQt6.QtGui import QEnterEvent, QMouseEvent, QPainter, QPaintEvent, QResizeEvent from PyQt6.QtWidgets import QAbstractButton, QWidget -from novelwriter import CONFIG, SHARED +from novelwriter import SHARED from novelwriter.types import QtMouseLeft, QtNoPen, QtPaintAntiAlias, QtSizeFixed class NSwitch(QAbstractButton): - __slots__ = ("_xW", "_xH", "_xR", "_rB", "_rH", "_rR", "_offset") + __slots__ = ("_xW", "_xH", "_xR", "_rH", "_rR", "_offset") def __init__(self, parent: QWidget, height: int = 0) -> None: super().__init__(parent=parent) @@ -41,9 +41,8 @@ class NSwitch(QAbstractButton): self._xH = height or SHARED.theme.baseButtonHeight self._xW = 2*self._xH self._xR = int(self._xH*0.5) - self._rB = CONFIG.pxInt(2) - self._rH = self._xH - 2*self._rB - self._rR = self._xR - self._rB + self._rH = self._xH - 4 + self._rR = self._xR - 2 self.setCheckable(True) self.setSizePolicy(QtSizeFixed, QtSizeFixed) @@ -98,14 +97,14 @@ class NSwitch(QAbstractButton): trackBrush = palette.highlight() thumbBrush = palette.highlightedText() else: - trackBrush = palette.mid() + trackBrush = palette.midlight() thumbBrush = palette.light() if self.isEnabled(): trackOpacity = 1.0 else: trackOpacity = 0.6 - trackBrush = palette.dark() + trackBrush = palette.mid() thumbBrush = palette.mid() painter.setBrush(trackBrush) @@ -113,7 +112,7 @@ class NSwitch(QAbstractButton): painter.drawRoundedRect(0, 0, self._xW, self._xH, self._xR, self._xR) painter.setBrush(thumbBrush) - painter.drawEllipse(self._offset - self._rR, self._rB, self._rH, self._rH) + painter.drawEllipse(self._offset - self._rR, 2, self._rH, self._rH) painter.end() return diff --git a/novelwriter/extensions/versioninfo.py b/novelwriter/extensions/versioninfo.py index c44ce820..2f13afaa 100644 --- a/novelwriter/extensions/versioninfo.py +++ b/novelwriter/extensions/versioninfo.py @@ -70,7 +70,7 @@ class VersionInfoWidget(QWidget): self._layout = QVBoxLayout() self._layout.addWidget(self._lblInfo) self._layout.addWidget(self._lblRelease) - self._layout.setSpacing(CONFIG.pxInt(2)) + self._layout.setSpacing(2) self._layout.setContentsMargins(0, 0, 0, 0) self.setLayout(self._layout) diff --git a/novelwriter/formats/toqdoc.py b/novelwriter/formats/toqdoc.py index 13647988..731990f5 100644 --- a/novelwriter/formats/toqdoc.py +++ b/novelwriter/formats/toqdoc.py @@ -278,8 +278,10 @@ class ToQTextDocument(Tokenizer): printer.setPageMargins(self._pageMargins, QPageLayout.Unit.Millimeter) printer.setOutputFileName(str(path)) - self._document.documentLayout().setPaintDevice(printer) - self._document.setPageSize(printer.pageRect(QPrinter.Unit.Millimeter).size()) + if layout := self._document.documentLayout(): + layout.setPaintDevice(printer) + + self._document.setPageSize(printer.pageRect(QPrinter.Unit.DevicePixel).size()) self._document.print(printer) return @@ -458,7 +460,8 @@ class ToQTextDocument(Tokenizer): cursor.insertFrame(fFmt) cursor.setBlockFormat(bFmt) cursor.insertText(self._project.localLookup("New Page"), cFmt) - cursor.swap(self._document.rootFrame().lastCursorPosition()) + if root := self._document.rootFrame(): + cursor.swap(root.lastCursorPosition()) return