Generate nicer status icons

This commit is contained in:
Veronica Berglyd Olsen
2022-10-30 02:33:00 +02:00
parent e80536405e
commit dbade05e46
+32 -15
View File
@@ -26,11 +26,13 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
import random import random
import logging import logging
import novelwriter import novelwriter
from lxml import etree from lxml import etree
from PyQt5.QtGui import QIcon, QPixmap, QColor from PyQt5.QtGui import QIcon, QPainter, QPainterPath, QPixmap, QColor
from PyQt5.QtCore import QRectF, Qt
from novelwriter.common import checkInt, minmax, simplified from novelwriter.common import checkInt, minmax, simplified
@@ -49,10 +51,15 @@ class NWStatus:
self._reverse = {} self._reverse = {}
self._default = None self._default = None
self._iconSize = novelwriter.CONFIG.pxInt(32) self._iPX = novelwriter.CONFIG.pxInt(24)
pixmap = QPixmap(self._iconSize, self._iconSize)
pixmap.fill(QColor(100, 100, 100)) pA = novelwriter.CONFIG.pxInt(2)
self._defaultIcon = QIcon(pixmap) pB = novelwriter.CONFIG.pxInt(20)
pR = float(novelwriter.CONFIG.pxInt(4))
self._iconPath = QPainterPath()
self._iconPath.addRoundedRect(QRectF(pA, pA, pB, pB), pR, pR)
self._defaultIcon = self._createIcon([100, 100, 100])
if self._type == self.STATUS: if self._type == self.STATUS:
self._prefix = "s" self._prefix = "s"
@@ -63,19 +70,16 @@ class NWStatus:
return return
def write(self, key, name, cols, count=None): def write(self, key, name, col, count=None):
"""Add or update a status entry. If the key is invalid, a new """Add or update a status entry. If the key is invalid, a new
key is generated. key is generated.
""" """
if not self._isKey(key): if not self._isKey(key):
key = self._newKey() key = self._newKey()
if not isinstance(cols, tuple): if not isinstance(col, tuple):
cols = (100, 100, 100) col = (100, 100, 100)
if len(cols) != 3: if len(col) != 3:
cols = (100, 100, 100) col = (100, 100, 100)
pixmap = QPixmap(self._iconSize, self._iconSize)
pixmap.fill(QColor(*cols))
name = simplified(name) name = simplified(name)
if count is None: if count is None:
@@ -83,8 +87,8 @@ class NWStatus:
self._store[key] = { self._store[key] = {
"name": name, "name": name,
"icon": QIcon(pixmap), "icon": self._createIcon(col),
"cols": cols, "cols": col,
"count": count, "count": count,
} }
self._reverse[name] = key self._reverse[name] = key
@@ -267,6 +271,19 @@ class NWStatus:
return False return False
return True return True
def _createIcon(self, col):
"""Generate an icon for a status label.
"""
pixmap = QPixmap(self._iPX, self._iPX)
pixmap.fill(Qt.transparent)
painter = QPainter(pixmap)
painter.setRenderHint(QPainter.Antialiasing)
painter.fillPath(self._iconPath, QColor(*col))
painter.end()
return QIcon(pixmap)
## ##
# Iterator Bits # Iterator Bits
## ##