Move icons generatino to new module

chart_mod_breakup
Tyler Goodlet 2021-09-13 18:40:12 -04:00
parent 66199bfe6f
commit 335e72bf32
2 changed files with 99 additions and 59 deletions

88
piker/ui/_icons.py 100644
View File

@ -0,0 +1,88 @@
# piker: trading gear for hackers
# Copyright (C) Tyler Goodlet (in stewardship for pikers)
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero 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 Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
'''
``QIcon`` hackery.
'''
from PyQt5.QtWidgets import QStyle
from PyQt5.QtGui import (
QIcon, QPixmap, QColor
)
from PyQt5.QtCore import QSize
from ._style import hcolor
# https://www.pythonguis.com/faq/built-in-qicons-pyqt/
# account status icons taken from built-in set
_icon_names: dict[str, str] = {
# these two seem to work for our mask hack done below to
# change the coloring.
'long_pp': 'SP_TitleBarShadeButton',
'short_pp': 'SP_TitleBarUnshadeButton',
'ready': 'SP_MediaPlay',
}
_icons: dict[str, QIcon] = {}
def mk_icons(
style: QStyle,
size: QSize,
) -> dict[str, QIcon]:
'''This helper is indempotent.
'''
global _icons, _icon_names
if _icons:
return _icons
# load account selection using current style
for name, icon_name in _icon_names.items():
stdpixmap = getattr(QStyle, icon_name)
stdicon = style.standardIcon(stdpixmap)
pixmap = stdicon.pixmap(size)
# fill hack from SO to change icon color:
# https://stackoverflow.com/a/38369468
out_pixmap = QPixmap(size)
out_pixmap.fill(QColor(hcolor('default_spotlight')))
out_pixmap.setMask(pixmap.createHeuristicMask())
# TODO: not idea why this doesn't work / looks like
# trash. Sure would be nice to just generate our own
# pixmaps on the fly..
# p = QPainter(out_pixmap)
# p.setOpacity(1)
# p.setBrush(QColor(hcolor('papas_special')))
# p.setPen(QColor(hcolor('default_lightest')))
# path = mk_marker_path(style='|<')
# p.scale(6, 6)
# # p.translate(0, 0)
# p.drawPath(path)
# p.save()
# p.end()
# del p
# icon = QIcon(out_pixmap)
icon = QIcon()
icon.addPixmap(out_pixmap)
_icons[name] = icon
return _icons

View File

@ -25,10 +25,10 @@ from math import floor, copysign
from typing import Optional from typing import Optional
from PyQt5.QtWidgets import QStyle # from PyQt5.QtWidgets import QStyle
from PyQt5.QtGui import ( # from PyQt5.QtGui import (
QIcon, QPixmap, QColor # QIcon, QPixmap, QColor
) # )
from pyqtgraph import functions as fn from pyqtgraph import functions as fn
from ._annotate import LevelMarker from ._annotate import LevelMarker
@ -42,7 +42,8 @@ from ..data._normalize import iterticks
from ..data.feed import Feed from ..data.feed import Feed
from ._label import Label from ._label import Label
from ._lines import LevelLine, order_line from ._lines import LevelLine, order_line
from ._style import _font, hcolor from ._icons import mk_icons
from ._style import _font
from ._forms import FieldsForm, FillStatusBar, QLabel from ._forms import FieldsForm, FillStatusBar, QLabel
from ..log import get_logger from ..log import get_logger
@ -113,18 +114,6 @@ async def display_pnl(
assert _pnl_tasks.pop(key) assert _pnl_tasks.pop(key)
# https://www.pythonguis.com/faq/built-in-qicons-pyqt/
# account status icons taken from built-in set
_icon_names: dict[str, str] = {
# these two seem to work for our mask hack done below to
# change the coloring.
'long_pp': 'SP_TitleBarShadeButton',
'short_pp': 'SP_TitleBarUnshadeButton',
'ready': 'SP_MediaPlay',
}
_icons: dict[str, QIcon] = {}
@dataclass @dataclass
class SettingsPane: class SettingsPane:
'''Composite set of widgets plus an allocator model for configuring '''Composite set of widgets plus an allocator model for configuring
@ -152,52 +141,15 @@ class SettingsPane:
) -> None: ) -> None:
form = self.form form = self.form
icons = mk_icons(
global _icons, _icon_name form.style(),
if not _icons: form.fields['account'].iconSize()
# load account selection using current style )
for name, icon_name in _icon_names.items():
stdpixmap = getattr(QStyle, icon_name)
stdicon = form.style().standardIcon(stdpixmap)
combo = form.fields['account']
size = combo.iconSize()
pixmap = stdicon.pixmap(combo.iconSize())
# fill hack from SO to change icon color:
# https://stackoverflow.com/a/38369468
out_pixmap = QPixmap(size)
out_pixmap.fill(QColor(hcolor('default_spotlight')))
out_pixmap.setMask(pixmap.createHeuristicMask())
# TODO: not idea why this doesn't work / looks like
# trash. Sure would be nice to just generate our own
# pixmaps on the fly..
# p = QPainter(out_pixmap)
# p.setOpacity(1)
# p.setBrush(QColor(hcolor('papas_special')))
# p.setPen(QColor(hcolor('default_lightest')))
# path = mk_marker_path(style='|<')
# p.scale(6, 6)
# # p.translate(0, 0)
# p.drawPath(path)
# p.save()
# p.end()
# del p
# icon = QIcon(out_pixmap)
icon = QIcon()
icon.addPixmap(out_pixmap)
_icons[name] = icon
acct_select = form.fields['account'] acct_select = form.fields['account']
keys = list(keys or acct_select._items.keys()) keys = list(keys or acct_select._items.keys())
# breakpoint()
for key in keys: for key in keys:
i = acct_select._items[key] i = acct_select._items[key]
icon = _icons[status] icon = icons[status]
acct_select.setItemIcon(i, icon) acct_select.setItemIcon(i, icon)
def on_selection_change( def on_selection_change(