Skip to content

Commit 2d7e4af

Browse files
committed
Add support for standard icon names in get_icon function
1 parent f183472 commit 2d7e4af

File tree

2 files changed

+29
-12
lines changed

2 files changed

+29
-12
lines changed

guidata/configtools.py

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,24 @@ def get_image_file_path(name: str, default: str = "not_found.png") -> str:
182182
ICON_CACHE = {}
183183

184184

185+
def get_std_icon(name: str) -> QG.QIcon | None:
186+
"""Get standard icon by name"""
187+
# Importing Qt objects here because this module should not depend on them
188+
# pylint: disable=import-outside-toplevel
189+
190+
# Try to get standard icon first
191+
from guidata.qthelpers import get_std_icon
192+
193+
try:
194+
return get_std_icon(name)
195+
except AttributeError:
196+
return None
197+
198+
185199
def get_icon(name: str, default: str = "not_found.png") -> QG.QIcon:
186200
"""
187201
Construct a QIcon from the file with specified name
188-
name, default: filenames with extensions
202+
name, default: filenames with extensions or standard Qt icon names
189203
190204
Args:
191205
name (str): name of the icon
@@ -197,22 +211,25 @@ def get_icon(name: str, default: str = "not_found.png") -> QG.QIcon:
197211
try:
198212
return ICON_CACHE[name]
199213
except KeyError:
200-
# Importing Qt objects here because this module should not depend on them
201-
# pylint: disable=import-outside-toplevel
214+
std_icon = get_std_icon(name)
215+
if std_icon is not None:
216+
return std_icon
202217

203-
# Try to get standard icon first
204-
from guidata.qthelpers import get_std_icon
218+
std_default_icon = get_std_icon(default)
205219

206-
try:
207-
return get_std_icon(name)
208-
except AttributeError:
209-
pass
220+
# Importing Qt objects here because this module should not depend on them
221+
# pylint: disable=import-outside-toplevel
210222

211223
# Retrieve icon from file (original implementation)
212224
from qtpy import QtGui as QG
213225

214-
icon = QG.QIcon(get_image_file_path(name, default))
215-
ICON_CACHE[name] = icon
226+
try:
227+
icon = QG.QIcon(get_image_file_path(name, default))
228+
ICON_CACHE[name] = icon
229+
except RuntimeError: # default is a standard icon name
230+
if std_default_icon is not None:
231+
return std_default_icon
232+
raise
216233
return icon
217234

218235

guidata/dataset/qtitemwidgets.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -967,7 +967,7 @@ def __init__(
967967
self.filedialog = filedialog
968968
self.button = QPushButton()
969969
fmt = item.get_prop_value("data", "formats")
970-
self.button.setIcon(get_icon("%s.png" % fmt[0].lower(), default="file.png"))
970+
self.button.setIcon(get_icon("%s.png" % fmt[0].lower(), default="FileIcon"))
971971
self.button.clicked.connect(self.select_file) # type:ignore
972972
self.group.addWidget(self.button)
973973
self.basedir = item.get_prop_value("data", "basedir")

0 commit comments

Comments
 (0)