Skip to content

Commit

Permalink
WIP: support for setting Font Awesome icons in designer
Browse files Browse the repository at this point in the history
  • Loading branch information
nstelter-slac committed Oct 31, 2023
1 parent cdb8ee8 commit a08d810
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 14 deletions.
13 changes: 12 additions & 1 deletion pydm/tests/widgets/test_pushbutton.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,18 @@ def test_construct(qtbot, label, press_value, relative, init_channel, icon_font_
test_icon = style.standardIcon(style.SP_DesktopIcon)
test_icon_image = test_icon.pixmap(size).toImage()

pydm_pushbutton.standardIcon = "SP_DesktopIcon"
pydm_pushbutton.PyDMIcon = "SP_DesktopIcon"
push_btn_icon = pydm_pushbutton.icon()
push_btn_icon_image = push_btn_icon.pixmap(size).toImage()

assert test_icon_image == push_btn_icon_image

# verify that "Font Awesome" icons can be set through our custom property
icon_f = IconFont()
test_icon = icon_f.icon("eye-slash", color=None)
test_icon_image = test_icon.pixmap(size).toImage()

pydm_pushbutton.PyDMIcon = "eye-slash"
push_btn_icon = pydm_pushbutton.icon()
push_btn_icon_image = push_btn_icon.pixmap(size).toImage()

Expand Down
66 changes: 53 additions & 13 deletions pydm/widgets/pushbutton.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import hashlib

from qtpy.QtGui import QColor
from qtpy.QtWidgets import QPushButton, QMessageBox, QInputDialog, QLineEdit, QStyle
from qtpy.QtCore import Slot, Property
from .base import PyDMWritableWidget

from pydm.utilities import iconfont
import logging

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -73,38 +74,77 @@ def __init__(
self.clicked.connect(self.sendValue)

# Standard icons (which come with the qt install, and work cross-platform),
# and icons from the "Font Awesome" icon set (https://fontawesome.com/)
# can not be set with a widget's "icon" property in designer, only in python.
# so we provide our own propery to specify standard icons and set them with python in the prop's setter.
self._standard_icon_name = ""
# so we provide our own property to specify standard icons and set them with python in the prop's setter.
self._pydm_icon_name = ""
# The color of "Font Awesome" icons can be set,
# but standard icons are already colored and can not be set.
self._pydm_icon_color = QColor(90, 90, 90)

@Property(str)
def standardIcon(self) -> str:
def PyDMIcon(self) -> str:
"""
Name of icon to be set from Qt provided standard icons.
See "enum QStyle::StandardPixmap" in Qt's QStyle documentation for full list of usable icons.
Name of icon to be set from Qt provided standard icons or from the fontawesome icon-set.
See "enum QStyle::StandardPixmap" in Qt's QStyle documentation for full list of usable standard icons.
See https://fontawesome.com/icons?d=gallery for list of usable fontawesome icons.
Returns
-------
str
"""
return self._standard_icon_name
return self._pydm_icon_name

@standardIcon.setter
def standardIcon(self, value: str) -> None:
@PyDMIcon.setter
def PyDMIcon(self, value: str) -> None:
"""
Name of icon to be set from Qt provided standard icons.
See "enum QStyle::StandardPixmap" in Qt's QStyle documentation for full list of usable icons.
Name of icon to be set from Qt provided standard icons or from the "Font Awesome" icon-set.
See "enum QStyle::StandardPixmap" in Qt's QStyle documentation for full list of usable standard icons.
See https://fontawesome.com/icons?d=gallery for list of usable "Font Awesome" icons.
Parameters
----------
value : str
"""
if self._standard_icon_name != value:
self._standard_icon_name = value
if self._pydm_icon_name == value:
return

# We don't know if user is trying to use a standard icon or an icon from "Font Awesome",
# so 1st try to create a Font Awesome one, which hits exception if icon name is not valid.
try:
icon_f = iconfont.IconFont()
i = icon_f.icon(value, color=self._pydm_icon_color)
self.setIcon(i)
except Exception:
icon = getattr(QStyle, value, None)
if icon:
self.setIcon(self.style().standardIcon(icon))

self._pydm_icon_name = value

@Property(QColor)
def PyDMIconColor(self) -> QColor:
"""
The color of the icon (color is only applied if using icon from the "Font Awesome" set)
Returns
-------
QColor
"""
return self._pydm_icon_color

@PyDMIconColor.setter
def PyDMIconColor(self, state_color: QColor) -> None:
"""
The color of the icon (color is only applied if using icon from the "Font Awesome" set)
Parameters
----------
new_color : QColor
"""
if state_color != self._pydm_icon_color:
self._pydm_icon_color = state_color
# call setter to apply new color
self.pydmIcon = self._pydm_icon_name

@Property(bool)
def passwordProtected(self):
"""
Expand Down

0 comments on commit a08d810

Please sign in to comment.