Skip to content

Commit

Permalink
Merge pull request #226 from tangkong/enh_paged_table
Browse files Browse the repository at this point in the history
ENH: Add and apply PagedTableWidget
  • Loading branch information
tangkong authored Dec 14, 2023
2 parents 27163da + a2bc8cb commit 920f76c
Showing 13 changed files with 952 additions and 243 deletions.
49 changes: 49 additions & 0 deletions atef/config.py
Original file line number Diff line number Diff line change
@@ -99,6 +99,31 @@ def replace_comparison(
else:
break

def move_comparison(
self,
comp: Comparison,
new_attr: str,
comp_attrs: List[str],
) -> None:
if not any(hasattr(self, att) for att in comp_attrs + ["shared"]):
logger.debug('cannot find a requested attr in dataclass')
return

# remove from all attrs
util.remove_by_id(self.shared, comp)
for attr in comp_attrs:
for comp_list in getattr(self, attr, {}).values():
util.remove_by_id(comp_list, comp)

# place into new_attr
if new_attr == "shared":
self.shared.append(comp)
else:
for attr in comp_attrs:
attr_dict = getattr(self, attr, {})
if new_attr in attr_dict:
attr_dict[new_attr].append(comp)


@dataclass
class ConfigurationGroup(Configuration):
@@ -175,6 +200,14 @@ def replace_comparison(
comp_attrs = ['by_attr']
super().replace_comparison(old_comp, new_comp, comp_attrs)

def move_comparison(
self,
comp: Comparison,
new_attr: str,
comp_attrs: Optional[List[str]] = None
) -> None:
super().move_comparison(comp, new_attr, comp_attrs=['by_attr'])


@dataclass
class PVConfiguration(Configuration):
@@ -215,6 +248,14 @@ def replace_comparison(
comp_attrs = ['by_pv']
super().replace_comparison(old_comp, new_comp, comp_attrs)

def move_comparison(
self,
comp: Comparison,
new_attr: str,
comp_attrs: Optional[List[str]] = None
) -> None:
super().move_comparison(comp, new_attr, comp_attrs=['by_pv'])


@dataclass
class ToolConfiguration(Configuration):
@@ -261,6 +302,14 @@ def replace_comparison(
comp_attrs = ['by_attr']
super().replace_comparison(old_comp, new_comp, comp_attrs)

def move_comparison(
self,
comp: Comparison,
new_attr: str,
comp_attrs: Optional[List[str]] = None
) -> None:
super().move_comparison(comp, new_attr, comp_attrs=['by_attr'])


AnyConfiguration = Union[
PVConfiguration,
41 changes: 30 additions & 11 deletions atef/tests/test_page.py
Original file line number Diff line number Diff line change
@@ -85,7 +85,11 @@ def test_add_delete_comparison(
new_comp_list = gather_comparisons(cfg)
assert len(new_comp_list) == len(orig_comp_list) + 1

widget = group_page.comparisons_table.cellWidget(0, 0)
table = group_page.comparisons_table
table.update_table()
index = table.proxy_model.index(0, 0)
widget = table.table_view.indexWidget(index)

deleted_comparison = widget.data

# mock to auto-confirm deletion
@@ -112,17 +116,20 @@ def test_change_attr(
group_page = make_page(cfg)
qtbot.addWidget(group_page)

row_widget = group_page.comparisons_table.cellWidget(0, 0)
table = group_page.comparisons_table
table.update_table()
index = table.proxy_model.index(0, 0)
row_widget = table.table_view.indexWidget(index)

new_idxs = get_different_combo_options(row_widget.attr_combo)
if not new_idxs:
return

for idx in new_idxs:
row_widget.attr_combo.setCurrentIndex(idx)
row_widget.attr_combo.activated.emit(idx)
new_comps = gather_comparisons(cfg)
assert len(new_comps) == len(orig_comps)
assert new_comps != orig_comps
qtbot.waitUntil(lambda: gather_comparisons(cfg) != orig_comps, timeout=10000)
assert len(gather_comparisons(cfg)) == len(orig_comps)


@pytest.mark.parametrize(
@@ -138,24 +145,36 @@ def test_change_comparison(
):
cfg = request.getfixturevalue(group)
group_page = make_page(cfg)
qtbot.addWidget(group_page)
group_data = group_page.data
full_tree = group_page.full_tree

# get comparison page
row_widget = group_page.comparisons_table.cellWidget(0, 0)
table = group_page.comparisons_table
table.update_table()
index = table.proxy_model.index(0, 0)
row_widget = table.table_view.indexWidget(index)

row_widget.child_button.clicked.emit()
qtbot.wait_until(lambda: isinstance(group_page.full_tree.current_widget,
ComparisonPage))
comp_page = group_page.full_tree.current_widget
old_comp = comp_page.data

new_idxs = get_different_combo_options(comp_page.specific_combo)

monkeypatch.setattr(QtWidgets.QMessageBox, 'question',
lambda *args, **kwargs: QtWidgets.QMessageBox.Yes)
for idx in new_idxs:
qtbot.addWidget(group_page)
qtbot.addWidget(comp_page)
comp_page.specific_combo.setCurrentIndex(idx)
comp_page.specific_combo.activated.emit(idx)

new_comp = group_page.comparisons_table.cellWidget(0, 0).data
qtbot.waitUntil(lambda: new_comp != old_comp, timeout=10000)
assert new_comp != old_comp
def condition():
assert full_tree.current_widget.data != old_comp

qtbot.waitUntil(condition, timeout=10000)
new_data = full_tree.current_widget.data
# ensure group_page still exists even if it falls out of cache
full_tree.select_by_data(group_data)
full_tree.select_by_data(new_data)
comp_page = full_tree.current_widget
22 changes: 9 additions & 13 deletions atef/ui/device_configuration_page.ui
Original file line number Diff line number Diff line change
@@ -35,19 +35,7 @@
</widget>
</item>
<item>
<widget class="QTableWidget" name="comparisons_table">
<attribute name="horizontalHeaderStretchLastSection">
<bool>true</bool>
</attribute>
<attribute name="verticalHeaderVisible">
<bool>false</bool>
</attribute>
<column>
<property name="text">
<string>Comparisons</string>
</property>
</column>
</widget>
<widget class="PagedTableWidget" name="comparisons_table" native="true"/>
</item>
<item>
<widget class="QPushButton" name="add_comparison_button">
@@ -58,6 +46,14 @@
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>PagedTableWidget</class>
<extends>QWidget</extends>
<header>atef.widgets.config.paged_table</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>
70 changes: 70 additions & 0 deletions atef/ui/paged_table.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form</class>
<widget class="QWidget" name="Form">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>421</width>
<height>436</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QTableView" name="table_view"/>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QToolButton" name="prev_button">
<property name="text">
<string>&lt;</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="page_label">
<property name="text">
<string>Page</string>
</property>
</widget>
</item>
<item>
<widget class="QSpinBox" name="page_spinbox">
<property name="minimum">
<number>1</number>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="page_count_label">
<property name="text">
<string>/ 0</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="search_edit">
<property name="placeholderText">
<string>Search by name</string>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="next_button">
<property name="text">
<string>&gt;</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
28 changes: 9 additions & 19 deletions atef/ui/pv_configuration_page.ui
Original file line number Diff line number Diff line change
@@ -144,25 +144,7 @@
<number>0</number>
</property>
<item>
<widget class="QTableWidget" name="comparisons_table">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<attribute name="horizontalHeaderStretchLastSection">
<bool>true</bool>
</attribute>
<attribute name="verticalHeaderVisible">
<bool>false</bool>
</attribute>
<column>
<property name="text">
<string>Comparisons</string>
</property>
</column>
</widget>
<widget class="PagedTableWidget" name="comparisons_table" native="true"/>
</item>
<item>
<widget class="QPushButton" name="add_comparison_button">
@@ -178,6 +160,14 @@
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>PagedTableWidget</class>
<extends>QWidget</extends>
<header>atef.widgets.config.paged_table</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>
22 changes: 9 additions & 13 deletions atef/ui/tool_configuration_page.ui
Original file line number Diff line number Diff line change
@@ -69,19 +69,7 @@
</widget>
</item>
<item>
<widget class="QTableWidget" name="comparisons_table">
<attribute name="horizontalHeaderStretchLastSection">
<bool>true</bool>
</attribute>
<attribute name="verticalHeaderVisible">
<bool>false</bool>
</attribute>
<column>
<property name="text">
<string>Comparisons</string>
</property>
</column>
</widget>
<widget class="PagedTableWidget" name="comparisons_table" native="true"/>
</item>
<item>
<widget class="QPushButton" name="add_comparison_button">
@@ -92,6 +80,14 @@
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>PagedTableWidget</class>
<extends>QWidget</extends>
<header>atef.widgets.config.paged_table</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>
9 changes: 8 additions & 1 deletion atef/util.py
Original file line number Diff line number Diff line change
@@ -153,6 +153,13 @@ def wrapped():
return await loop.run_in_executor(executor, wrapped)


def replace_in_list(old: T, new: T, item_list: List[T]):
def replace_in_list(old: T, new: T, item_list: List[T]) -> None:
index = item_list.index(old)
item_list[index] = new


def remove_by_id(series: List[T], item: T) -> None:
for i in range(len(series)):
if series[i] is item:
series.pop(i)
return
Loading

0 comments on commit 920f76c

Please sign in to comment.