diff --git a/novelwriter/dialogs/projdetails.py b/novelwriter/dialogs/projdetails.py index b3d942ca..5544ec7d 100644 --- a/novelwriter/dialogs/projdetails.py +++ b/novelwriter/dialogs/projdetails.py @@ -36,9 +36,9 @@ from PyQt5.QtWidgets import ( from novelwriter import CONFIG from novelwriter.common import formatTime, numberToRoman from novelwriter.constants import nwUnicode -from novelwriter.gui.components import NovelSelector from novelwriter.extensions.switch import NSwitch from novelwriter.extensions.pageddialog import NPagedDialog +from novelwriter.extensions.novelselector import NovelSelector logger = logging.getLogger(__name__) diff --git a/novelwriter/gui/components.py b/novelwriter/extensions/novelselector.py similarity index 59% rename from novelwriter/gui/components.py rename to novelwriter/extensions/novelselector.py index 762f0b0c..8bb2c4c2 100644 --- a/novelwriter/gui/components.py +++ b/novelwriter/extensions/novelselector.py @@ -1,10 +1,9 @@ """ -novelWriter – GUI Components Module -=================================== +novelWriter – Custom Widget: Novel Selector +=========================================== File History: -Created: 2020-05-17 [0.5.1] StatusLED -Created: 2022-11-17 [2.0] NovelSelector +Created: 2022-11-17 [2.0] This file is a part of novelWriter Copyright 2018–2023, Veronica Berglyd Olsen @@ -26,14 +25,18 @@ from __future__ import annotations import logging -from PyQt5.QtGui import QPainter +from typing import TYPE_CHECKING + from PyQt5.QtCore import pyqtSignal, pyqtSlot -from PyQt5.QtWidgets import QAbstractButton, QComboBox +from PyQt5.QtWidgets import QComboBox, QWidget from novelwriter import CONFIG from novelwriter.enum import nwItemClass from novelwriter.constants import nwLabels +if TYPE_CHECKING: # pragma: no cover + from novelwriter.guimain import GuiMain + logger = logging.getLogger(__name__) @@ -41,15 +44,12 @@ class NovelSelector(QComboBox): novelSelectionChanged = pyqtSignal(str) - def __init__(self, parent, mainGui): + def __init__(self, parent: QWidget, mainGui: GuiMain) -> None: super().__init__(parent=parent) - self._mainGui = mainGui self._blockSignal = False self._firstHandle = None - self.currentIndexChanged.connect(self._indexChanged) - return ## @@ -57,20 +57,19 @@ class NovelSelector(QComboBox): ## @property - def handle(self): + def handle(self) -> str: return self.currentData() @property - def firstHandle(self): + def firstHandle(self) -> str | None: return self._firstHandle ## # Methods ## - def setHandle(self, tHandle, blockSignal=True): - """Set the currently selected handle. - """ + def setHandle(self, tHandle: str, blockSignal: bool = True) -> None: + """Set the currently selected handle.""" self._blockSignal = blockSignal if tHandle is None: index = self.count() - 1 @@ -81,9 +80,8 @@ class NovelSelector(QComboBox): self._blockSignal = False return - def updateList(self, includeAll=False, prefix=None): - """Rebuild the list of novel items. - """ + def updateList(self, includeAll: bool = False, prefix: str | None = None) -> None: + """Rebuild the list of novel items.""" self._blockSignal = True self._firstHandle = None self.clear() @@ -115,67 +113,10 @@ class NovelSelector(QComboBox): ## @pyqtSlot(int) - def _indexChanged(self, index): - """Re-emit the change of selected novel signal, unless blocked. - """ + def _indexChanged(self, index: int) -> None: + """Re-emit the change of selection signal, unless blocked.""" if not self._blockSignal: self.novelSelectionChanged.emit(self.currentData()) return # END Class NovelSelector - - -class StatusLED(QAbstractButton): - - S_NONE = 0 - S_BAD = 1 - S_GOOD = 2 - - def __init__(self, colNone, colGood, colBad, sW, sH, parent=None): - super().__init__(parent=parent) - - self._colNone = colNone - self._colGood = colGood - self._colBad = colBad - self._theCol = colNone - - self.setFixedWidth(sW) - self.setFixedHeight(sH) - - return - - ## - # Setters - ## - - def setState(self, theState): - """Set the colour state. - """ - if theState == self.S_GOOD: - self._theCol = self._colGood - elif theState == self.S_BAD: - self._theCol = self._colBad - else: - self._theCol = self._colNone - - self.update() - - return - - ## - # Events - ## - - def paintEvent(self, _): - """Drawing the LED. - """ - qPalette = self.palette() - qPaint = QPainter(self) - qPaint.setRenderHint(QPainter.Antialiasing, True) - qPaint.setPen(qPalette.dark().color()) - qPaint.setBrush(self._theCol) - qPaint.setOpacity(1.0) - qPaint.drawEllipse(1, 1, self.width() - 2, self.height() - 2) - return - -# END Class StatusLED diff --git a/novelwriter/extensions/statusled.py b/novelwriter/extensions/statusled.py new file mode 100644 index 00000000..8a5ba4e6 --- /dev/null +++ b/novelwriter/extensions/statusled.py @@ -0,0 +1,77 @@ +""" +novelWriter – Custom Widget: Status LED +======================================= + +File History: +Created: 2020-05-17 [0.5.1] + +This file is a part of novelWriter +Copyright 2018–2023, Veronica Berglyd Olsen + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +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 . +""" +from __future__ import annotations + +import logging +from typing import Literal + +from PyQt5.QtGui import QColor, QPaintEvent, QPainter +from PyQt5.QtWidgets import QAbstractButton, QWidget + +logger = logging.getLogger(__name__) + + +class StatusLED(QAbstractButton): + + S_NONE = 0 + S_BAD = 1 + S_GOOD = 2 + + def __init__(self, colNone: QColor, colGood: QColor, colBad: QColor, + sW: int, sH: int, parent: QWidget | None = None) -> None: + super().__init__(parent=parent) + + self._colNone = colNone + self._colGood = colGood + self._colBad = colBad + self._theCol = colNone + + self.setFixedWidth(sW) + self.setFixedHeight(sH) + + return + + def setState(self, state: Literal[0, 1, 2]) -> None: + """Set the colour state.""" + if state == self.S_GOOD: + self._theCol = self._colGood + elif state == self.S_BAD: + self._theCol = self._colBad + else: + self._theCol = self._colNone + self.update() + return + + def paintEvent(self, event: QPaintEvent) -> None: + """Drawing the LED.""" + qPalette = self.palette() + qPaint = QPainter(self) + qPaint.setRenderHint(QPainter.Antialiasing, True) + qPaint.setPen(qPalette.dark().color()) + qPaint.setBrush(self._theCol) + qPaint.setOpacity(1.0) + qPaint.drawEllipse(1, 1, self.width() - 2, self.height() - 2) + return + +# END Class StatusLED diff --git a/novelwriter/gui/noveltree.py b/novelwriter/gui/noveltree.py index 877dc1bd..98d36fe9 100644 --- a/novelwriter/gui/noveltree.py +++ b/novelwriter/gui/noveltree.py @@ -42,7 +42,7 @@ from novelwriter import CONFIG from novelwriter.enum import nwDocMode, nwItemClass, nwOutline from novelwriter.common import minmax from novelwriter.constants import nwHeaders, nwKeyWords, nwLabels, trConst -from novelwriter.gui.components import NovelSelector +from novelwriter.extensions.novelselector import NovelSelector logger = logging.getLogger(__name__) diff --git a/novelwriter/gui/outline.py b/novelwriter/gui/outline.py index 0c75a6b1..73440f1f 100644 --- a/novelwriter/gui/outline.py +++ b/novelwriter/gui/outline.py @@ -48,7 +48,7 @@ from novelwriter.enum import ( from novelwriter.error import logException from novelwriter.common import checkInt from novelwriter.constants import nwHeaders, trConst, nwKeyWords, nwLabels -from novelwriter.gui.components import NovelSelector +from novelwriter.extensions.novelselector import NovelSelector logger = logging.getLogger(__name__) diff --git a/novelwriter/gui/statusbar.py b/novelwriter/gui/statusbar.py index e3512270..138d1cd3 100644 --- a/novelwriter/gui/statusbar.py +++ b/novelwriter/gui/statusbar.py @@ -34,7 +34,7 @@ from PyQt5.QtWidgets import qApp, QStatusBar, QLabel from novelwriter import CONFIG from novelwriter.common import formatTime -from novelwriter.gui.components import StatusLED +from novelwriter.extensions.statusled import StatusLED logger = logging.getLogger(__name__) diff --git a/tests/test_gui/test_gui_statusbar.py b/tests/test_gui/test_gui_statusbar.py index 73572160..b227fb61 100644 --- a/tests/test_gui/test_gui_statusbar.py +++ b/tests/test_gui/test_gui_statusbar.py @@ -25,7 +25,7 @@ import pytest from tools import C, buildTestProject from novelwriter import CONFIG -from novelwriter.gui.statusbar import StatusLED +from novelwriter.extensions.statusled import StatusLED @pytest.mark.gui