-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathnode_with_dimension_selector_widget.py
More file actions
104 lines (78 loc) · 3.07 KB
/
Copy pathnode_with_dimension_selector_widget.py
File metadata and controls
104 lines (78 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
"""A simple script that illustrates how to use the :class:`.MultiDimensionSelector` widget
in a node to select axes in a dataset.
This example does the following:
* create a flowchart with one node, that has a node widget.
* selected axes in the node widget will be deleted from the data when the
selection is changed, and the remaining data is printed to stdout.
"""
from typing import List, Optional
from pprint import pprint
from plottr import QtWidgets
from plottr.data import DataDict
from plottr.node.node import Node, NodeWidget, updateOption, updateGuiQuietly
from plottr.node.tools import linearFlowchart
from plottr.gui.widgets import MultiDimensionSelector
from plottr.gui.tools import widgetDialog
from plottr.utils import testdata
class DummyNodeWidget(NodeWidget):
"""Node widget for this dummy node"""
def __init__(self, node: Node):
super().__init__(embedWidgetClass=MultiDimensionSelector)
assert isinstance(self.widget, MultiDimensionSelector) # this is for mypy
# allow selection of axis dimensions. See :class:`.MultiDimensionSelector`.
self.widget.dimensionType = 'axes'
# specify the functions that link node property to GUI elements
self.optSetters = {
'selectedAxes': self.setSelected,
}
self.optGetters = {
'selectedAxes': self.getSelected,
}
# make sure the widget is populated with the right dimensions
self.widget.connectNode(node)
# when the user selects an option, notify the node
self.widget.dimensionSelectionMade.connect(lambda x: self.signalOption('selectedAxes'))
@updateGuiQuietly
def setSelected(self, selected: List[str]) -> None:
self.widget.setSelected(selected)
def getSelected(self) -> List[str]:
return self.widget.getSelected()
class DummyNode(Node):
useUi = True
uiClass = DummyNodeWidget
def __init__(self, name: str):
super().__init__(name)
self._selectedAxes: List[str] = []
@property
def selectedAxes(self):
return self._selectedAxes
@selectedAxes.setter
@updateOption('selectedAxes')
def selectedAxes(self, value: List[str]):
self._selectedAxes = value
def process(self, dataIn = None) -> Dict[str, Optional[DataDict]]:
if super().process(dataIn) is None:
return None
data = dataIn.copy()
for k, v in data.items():
for s in self.selectedAxes:
if s in v.get('axes', []):
idx = v['axes'].index(s)
v['axes'].pop(idx)
for a in self.selectedAxes:
if a in data:
del data[a]
pprint(data)
return dict(dataOut=data)
def main():
fc = linearFlowchart(('dummy', DummyNode))
node = fc.nodes()['dummy']
dialog = widgetDialog(node.ui, title='dummy node')
data = testdata.get_2d_scalar_cos_data(2, 2, 1)
fc.setInput(dataIn=data)
return dialog, fc
if __name__ == '__main__':
app = QtWidgets.QApplication([])
dialog, fc = main()
dialog.show()
app.exec_()