Move GUI components module to extensions subpackage

This commit is contained in:
Veronica Berglyd Olsen
2023-08-08 22:47:24 +02:00
parent eeadfc95e3
commit 5d817ae51f
7 changed files with 100 additions and 82 deletions
+122
View File
@@ -0,0 +1,122 @@
"""
novelWriter Custom Widget: Novel Selector
===========================================
File History:
Created: 2022-11-17 [2.0]
This file is a part of novelWriter
Copyright 20182023, 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 <https://www.gnu.org/licenses/>.
"""
from __future__ import annotations
import logging
from typing import TYPE_CHECKING
from PyQt5.QtCore import pyqtSignal, pyqtSlot
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__)
class NovelSelector(QComboBox):
novelSelectionChanged = pyqtSignal(str)
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
##
# Properties
##
@property
def handle(self) -> str:
return self.currentData()
@property
def firstHandle(self) -> str | None:
return self._firstHandle
##
# Methods
##
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
else:
index = self.findData(tHandle)
if index >= 0:
self.setCurrentIndex(index)
self._blockSignal = False
return
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()
icon = CONFIG.theme.getIcon(nwLabels.CLASS_ICON[nwItemClass.NOVEL])
handle = self.currentData()
for tHandle, nwItem in self._mainGui.project.tree.iterRoots(nwItemClass.NOVEL):
if prefix:
name = prefix.format(nwItem.itemName)
self.addItem(name, tHandle)
else:
name = nwItem.itemName
self.addItem(icon, nwItem.itemName, tHandle)
if self._firstHandle is None:
self._firstHandle = tHandle
if includeAll:
self.insertSeparator(self.count())
self.addItem(icon, self.tr("All Novel Folders"), "")
self.setHandle(handle)
self.setEnabled(self.count() > 1)
self._blockSignal = False
return
##
# Private Slots
##
@pyqtSlot(int)
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
+77
View File
@@ -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 20182023, 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 <https://www.gnu.org/licenses/>.
"""
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