Skip to content

Commit 22b8c0a

Browse files
authored
refactor: Prop widget 2 (#485)
* initial * typing * fix: refactor _WheelBlocker to inherit from QObject and simplify instance management * add tests * use v2 and fix tests * less test * remove unneded examples * remove unneeded wheel stuff
1 parent a7ec621 commit 22b8c0a

7 files changed

Lines changed: 515 additions & 481 deletions

File tree

src/pymmcore_widgets/_util.py

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,15 @@
66

77
from psygnal import SignalInstance
88
from pymmcore_plus import CMMCorePlus
9-
from qtpy.QtCore import QEvent, QMarginsF, QObject, Qt
10-
from qtpy.QtGui import QPainter, QPaintEvent, QPen, QResizeEvent, QWheelEvent
9+
from qtpy.QtCore import QMarginsF, QObject, Qt
10+
from qtpy.QtGui import QPainter, QPaintEvent, QPen, QResizeEvent
1111
from qtpy.QtWidgets import (
12-
QAbstractSlider,
13-
QAbstractSpinBox,
1412
QComboBox,
1513
QDialog,
1614
QDialogButtonBox,
1715
QGraphicsScene,
1816
QGraphicsView,
1917
QLabel,
20-
QScrollBar,
21-
QTableWidget,
2218
QVBoxLayout,
2319
QWidget,
2420
)
@@ -30,41 +26,6 @@
3026
from typing import Any
3127

3228

33-
class NoWheelTableWidget(QTableWidget):
34-
"""QTableWidget that prevents scrolling behavior of child widgets."""
35-
36-
def __init__(self, *args: Any, **kwargs: Any) -> None:
37-
super().__init__(*args, **kwargs)
38-
if view := self.viewport():
39-
view.installEventFilter(self)
40-
41-
def setCellWidget(self, row: int, column: int, widget: QWidget | None) -> None:
42-
if widget is not None:
43-
# Note that PySide does not allow a tuple as an arg to findChildren
44-
for widget_type in (QAbstractSpinBox, QAbstractSlider, QComboBox):
45-
for child in widget.findChildren(widget_type):
46-
child.installEventFilter(self)
47-
widget.installEventFilter(self)
48-
super().setCellWidget(row, column, widget)
49-
50-
def eventFilter(self, object: QObject | None, event: QEvent | None) -> bool:
51-
# Many "value widgets" (e.g. Combo boxes, sliders) use scroll events to
52-
# change their value. The purpose of this widget is to prevent that.
53-
# Note that if that widget provides a scrollbar (e.g. the ListView
54-
# dropdown of a Combo Box), scroll events going to that scrollbar should
55-
# be let through.
56-
if isinstance(event, QWheelEvent) and not isinstance(object, QScrollBar):
57-
# Scroll the vertical scrollbar manually, to avoid recursion error.
58-
if sb := self.verticalScrollBar():
59-
delta = event.angleDelta().y()
60-
sb.setValue(sb.value() - delta)
61-
# Consume the event so child widgets don't process it
62-
return True
63-
64-
# otherwise process normally
65-
return False
66-
67-
6829
class ComboMessageBox(QDialog):
6930
"""Dialog that presents a combo box of `items`."""
7031

src/pymmcore_widgets/config_presets/_group_preset_widget/_group_preset_table_widget.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
QWidget,
2121
)
2222

23-
from pymmcore_widgets._util import NoWheelTableWidget, block_core, load_system_config
23+
from pymmcore_widgets._util import block_core, load_system_config
2424
from pymmcore_widgets.control._presets_widget import PresetsWidget
2525
from pymmcore_widgets.device_properties._property_widget import PropertyWidget
2626

@@ -32,7 +32,7 @@
3232
UNNAMED_PRESET = "NewPreset"
3333

3434

35-
class _MainTable(NoWheelTableWidget):
35+
class _MainTable(QTableWidget):
3636
"""Set table properties for Group and Preset TableWidget."""
3737

3838
def __init__(self) -> None:
@@ -229,7 +229,7 @@ def _populate_table(self) -> None:
229229
if isinstance(wdg, PresetsWidget):
230230
wdg = wdg._combo
231231
elif isinstance(wdg, PropertyWidget):
232-
wdg = wdg._value_widget # type: ignore
232+
wdg = wdg.inner_widget
233233

234234
# resize to contents the table
235235
self.table_wdg.resizeColumnToContents(0)

src/pymmcore_widgets/config_presets/_pixel_configuration_widget.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ def setValue(self, value: list[Setting]) -> None:
660660
# get the value of the PropertyWidget from value
661661
val = value_dict[(dev_prop.device, dev_prop.name)]
662662
# update the value of the PropertyWidget
663-
with signals_blocked(val_wdg._value_widget):
663+
with signals_blocked(val_wdg.inner_widget):
664664
val_wdg.setValue(val)
665665

666666
self._prop_table.item(row, 0).setCheckState(Qt.CheckState.Checked)
@@ -701,7 +701,7 @@ def _on_item_changed(self) -> None:
701701
# connect the valueChanged signal of the PropertyWidget to the
702702
# _update_property_table method that will update the value of the
703703
# PropertyWidget in the DevicePropertyTable when the PropertyWidget changes.
704-
wdg._value_widget.valueChanged.connect(self._update_property_table)
704+
wdg.inner_widget.valueChanged.connect(self._update_property_table)
705705
# to_view_table.append((dev, prop, val, wdg))
706706
to_view_table.append((dev, prop, wdg))
707707

@@ -726,7 +726,7 @@ def _update_property_table(self, value: Any) -> None:
726726
table_prop_row = table_prop_item[0].row()
727727
# get property widget and update the value
728728
wdg = cast("PropertyWidget", self._prop_table.cellWidget(table_prop_row, 1))
729-
with signals_blocked(wdg._value_widget):
729+
with signals_blocked(wdg.inner_widget):
730730
wdg.setValue(value)
731731

732732
self.valueChanged.emit(self.value())

src/pymmcore_widgets/device_properties/_device_property_table.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from qtpy.QtWidgets import QAbstractScrollArea, QTableWidget, QTableWidgetItem, QWidget
1010

1111
from pymmcore_widgets._icons import StandardIcon
12-
from pymmcore_widgets._util import NoWheelTableWidget
1312

1413
from ._property_widget import PropertyWidget
1514

@@ -19,7 +18,7 @@
1918
logger = getLogger(__name__)
2019

2120

22-
class DevicePropertyTable(NoWheelTableWidget):
21+
class DevicePropertyTable(QTableWidget):
2322
"""Table of all currently loaded device properties.
2423
2524
This table is used by `PropertyBrowser` to display all properties in the system,

0 commit comments

Comments
 (0)