Skip to content

Commit

Permalink
ImageChoiceItem/ButtonItem: new size argument
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreRaybaut committed Nov 21, 2023
1 parent f365f0e commit 30d709c
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 3 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Changelog #

## Version 3.3.0 ##

🛠️ Bug fixes:

* Tests only: `qthelpers.close_widgets_and_quit` now ignores deleted widgets

💥 Changes:

* `dataset.ImageChoiceItem` and `dataset.ButtonItem`: added `size` argument to set the icon size

## Version 3.2.0 ##

🛠️ Bug fixes:
Expand Down
2 changes: 1 addition & 1 deletion guidata/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
and application development tools for Qt.
"""

__version__ = "3.2.0"
__version__ = "3.3.0"


# Dear (Debian, RPM, ...) package makers, please feel free to customize the
Expand Down
6 changes: 6 additions & 0 deletions guidata/dataset/dataitems.py
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,7 @@ class ChoiceItem(DataItem):
check: if False, value is not checked (optional, default=True)
radio: if True, shows radio buttons instead of a combo box
(default is False)
size: size (optional) of the combo box or button widget (for radio buttons)
"""

def __init__(
Expand All @@ -747,6 +748,7 @@ def __init__(
help: str = "",
check: bool = True,
radio: bool = False,
size: tuple[int, int] | None = None,
) -> None:
_choices_data: Any
if isinstance(choices, Callable):
Expand All @@ -762,6 +764,7 @@ def __init__(
super().__init__(label, default=default, help=help, check=check)
self.set_prop("data", choices=_choices_data)
self.set_prop("display", radio=radio)
self.set_prop("display", size=size)

def _normalize_choice(
self, idx: int, choice_tuple: tuple[Any, ...]
Expand Down Expand Up @@ -1011,6 +1014,7 @@ class ButtonItem(DataItem):
default: default value passed to the callback (optional)
help: text shown in button's tooltip (optional)
check: if False, value is not checked (optional, default=True)
size: size (optional) of the button widget
The value of this item is unspecified but is passed to the callback along
with the whole dataset. The value is assigned the callback`s return value.
Expand All @@ -1024,10 +1028,12 @@ def __init__(
default: Any | None = None,
help: str = "",
check: bool = True,
size: tuple[int, int] | None = None,
) -> None:
super().__init__(label, default=default, help=help, check=check)
self.set_prop("display", callback=callback)
self.set_prop("display", icon=icon)
self.set_prop("display", size=size)

def serialize(
self,
Expand Down
13 changes: 12 additions & 1 deletion guidata/dataset/qtitemwidgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

import numpy
from qtpy.compat import getexistingdirectory
from qtpy.QtCore import Qt
from qtpy.QtCore import QSize, Qt
from qtpy.QtGui import QColor, QIcon, QPixmap
from qtpy.QtWidgets import (
QAbstractButton,
Expand Down Expand Up @@ -899,6 +899,7 @@ def __init__(
super().__init__(item, parent_layout)
self._first_call = True
self.is_radio = item.get_prop_value("display", "radio")
self.image_size = item.get_prop_value("display", "size", None)
self.store = self.item.get_prop("display", "store", None)
if self.is_radio:
self.group = QGroupBox()
Expand All @@ -908,6 +909,9 @@ def __init__(
self._buttons: list[QAbstractButton] = []
else:
self.combobox = self.group = QComboBox()
if self.image_size is not None:
width, height = self.image_size
self.combobox.setIconSize(QSize(width, height))
self.combobox.setToolTip(item.get_help())
self.combobox.currentIndexChanged.connect(self.index_changed) # type:ignore

Expand All @@ -933,6 +937,9 @@ def initialize_widget(self) -> None:
for key, lbl, img in _choices:
if self.is_radio:
button = QRadioButton(lbl, self.group)
if self.image_size is not None:
width, height = self.image_size
button.setIconSize(QSize(width, height))
if img:
if isinstance(img, str):
if not osp.isfile(img):
Expand Down Expand Up @@ -1228,6 +1235,10 @@ def __init__(
_label = self.item.get_prop_value("display", "label")
self.button = self.group = QPushButton(_label)
self.button.setToolTip(item.get_help())
image_size = item.get_prop_value("display", "size", None)
if image_size is not None:
width, height = image_size
self.button.setIconSize(QSize(width, height))
_icon = self.item.get_prop_value("display", "icon")
if _icon is not None:
if isinstance(_icon, str):
Expand Down
3 changes: 2 additions & 1 deletion guidata/tests/dataset/test_all_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,12 @@ class Parameters(gds.DataSet):
[
("rect", "first choice", "gif.png"),
("ell", "second choice", "txt.png"),
("qcq", "third choice", "file.png"),
("qcq", "third choice", "html.png"),
],
)
.set_pos(col=1)
.set_prop("display", icon="file.png")
.set_prop("display", size=(32, 32))
)
mchoice3 = gds.MultipleChoiceItem(
"MC type 3", [str(i) for i in range(10)]
Expand Down

0 comments on commit 30d709c

Please sign in to comment.