xstack 2.0.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
xstack/app/options.py ADDED
@@ -0,0 +1,664 @@
1
+ import string
2
+ import typing
3
+ import logging
4
+ import weakref
5
+ import qtility
6
+ import functools
7
+ from Qt import QtWidgets, QtCore, QtGui
8
+
9
+ from . import resources
10
+
11
+ log = logging.getLogger(__name__)
12
+
13
+
14
+ # ----------------------------------------------------------------------------------
15
+ # noinspection PyUnresolvedReferences
16
+ class ComponentEditor(QtWidgets.QWidget):
17
+ """
18
+ The component editor shows the name of a component, along with the tabs for
19
+ its inputs, options and outputs.
20
+ """
21
+
22
+ # -- Emitted when the user changes the component name through the editor
23
+ label_changed = QtCore.Signal()
24
+
25
+ # ----------------------------------------------------------------------------------
26
+ def __init__(self, read_only_name=False, parent=None):
27
+ super().__init__(parent=parent)
28
+
29
+ self.setLayout(
30
+ qtility.layouts.slimify(
31
+ QtWidgets.QVBoxLayout(),
32
+ ),
33
+ )
34
+ self.component = None
35
+
36
+ # -- The name field lets the user see and change the component label,
37
+ # -- unless it is read only, where it only shows it
38
+ self.name_edit = QtWidgets.QLineEdit()
39
+ self.name_edit.setAlignment(QtCore.Qt.AlignCenter)
40
+ self.name_edit.setEnabled(False)
41
+ self.name_edit.setReadOnly(read_only_name)
42
+ self.name_edit.editingFinished.connect(self._on_name_edited)
43
+ self.layout().addWidget(self.name_edit)
44
+
45
+ # -- We are hosting an unknown amount of widgets, so lets create
46
+ # -- a scrollable area to place the tabs within.
47
+ self.scroll_area = QtWidgets.QScrollArea()
48
+ self.scroll_area.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded)
49
+ self.scroll_area.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded)
50
+ self.scroll_area.setWidgetResizable(True)
51
+ self.layout().addWidget(self.scroll_area)
52
+
53
+ # -- Now start populating the content
54
+ self.tab_widget = QtWidgets.QTabWidget()
55
+ self.scroll_area.setWidget(self.tab_widget)
56
+
57
+ self.options_widget = OptionsWidget()
58
+ self.inputs_widget = InputsWidget(parent=self)
59
+ self.outputs_widget = OutputsWidget(parent=self)
60
+
61
+ self.tab_widget.addTab(
62
+ self.inputs_widget,
63
+ "Inputs",
64
+ )
65
+
66
+ self.tab_widget.addTab(
67
+ self.options_widget,
68
+ "Options"
69
+ )
70
+
71
+ self.tab_widget.addTab(
72
+ self.outputs_widget,
73
+ "Outputs"
74
+ )
75
+
76
+ for i in range(3):
77
+ self.tab_widget.setTabVisible(i, False)
78
+
79
+ # ----------------------------------------------------------------------------------
80
+ def set_component(self, component: "xstack.Component"):
81
+ """
82
+ Applies the component to the options and the inputs widgets
83
+ """
84
+
85
+ inputs_visibility = True if (component and len(component.inputs()) > 0) else False
86
+ options_visibility = True if (component and len(component.options()) > 0) else False
87
+ outputs_visibility = True if (component and len(component.outputs()) > 0) else False
88
+
89
+ self.tab_widget.setTabVisible(0, inputs_visibility)
90
+ self.tab_widget.setTabVisible(1, options_visibility)
91
+ self.tab_widget.setTabVisible(2, outputs_visibility)
92
+
93
+ self.options_widget.set_component(component)
94
+ self.inputs_widget.set_component(component)
95
+ self.outputs_widget.set_component(component)
96
+
97
+ self.name_edit.setText(component.label() if component else "")
98
+ self.name_edit.setEnabled(component is not None)
99
+
100
+ self.component = weakref.ref(component) if component else None
101
+
102
+ # ----------------------------------------------------------------------------------
103
+ def redraw(self):
104
+ if self.component and self.component():
105
+ self.set_component(self.component())
106
+
107
+ # ----------------------------------------------------------------------------------
108
+ def _on_name_edited(self):
109
+ """
110
+ Applies the name typed by the user as the label of the component. An empty
111
+ or unchanged name restores the current label.
112
+ """
113
+ component = self.component() if self.component else None
114
+
115
+ if not component:
116
+ return
117
+
118
+ label = self.name_edit.text().strip()
119
+
120
+ if not label or label == component.label():
121
+ self.name_edit.setText(component.label())
122
+ return
123
+
124
+ component.set_label(label)
125
+ self.label_changed.emit()
126
+
127
+ # --------------------------------------------------------------------------------------
128
+ # noinspection PyUnresolvedReferences,DuplicatedCode
129
+ class OptionsWidget(QtWidgets.QWidget):
130
+ """
131
+ This widget manages the display of the options
132
+ """
133
+
134
+ # ----------------------------------------------------------------------------------
135
+ def __init__(self, pre_expose_only=False, parent: QtWidgets.QWidget = None):
136
+ super().__init__(parent=parent)
137
+
138
+ # -- This determines if we show all options, or only options that
139
+ # -- are marked as pre-expsure
140
+ self._pre_expose_only = pre_expose_only
141
+
142
+ self.setLayout(
143
+ qtility.layouts.slimify(
144
+ QtWidgets.QVBoxLayout(),
145
+ ),
146
+ )
147
+
148
+ self.option_layout = QtWidgets.QVBoxLayout()
149
+ self.option_layout.setContentsMargins(10, 10, 10, 10)
150
+
151
+ self.help_text = QtWidgets.QLabel()
152
+ self.help_text.setWordWrap(True)
153
+
154
+ self.layout().addWidget(self.help_text)
155
+ self.layout().addLayout(self.option_layout)
156
+
157
+ # ----------------------------------------------------------------------------------
158
+ # noinspection PyUnresolvedReferences
159
+ def set_component(self, component: typing.Optional["xstack.Component"]):
160
+ """
161
+ This sets the target component for the widget and populates the widget
162
+ with all the options.
163
+ """
164
+ # -- Clear the current layout of options
165
+ qtility.layouts.empty(self.option_layout)
166
+
167
+ # -- If we do not have a component we dont need to populate anything
168
+ if not component:
169
+ self.help_text.setText("")
170
+ return
171
+
172
+ # -- Update the help text
173
+ self.help_text.setText(component.documentation())
174
+
175
+ group_layouts = dict()
176
+
177
+ for option in component.options():
178
+
179
+ if option.hidden():
180
+ continue
181
+
182
+ if self._pre_expose_only and not option.should_pre_expose():
183
+ continue
184
+
185
+ # -- Get the option object
186
+ option_widget = component.option_widget(option.name())
187
+
188
+ # -- Get the raw value, as we want to see the address
189
+ # -- if its an address
190
+ option_value = option.get(resolved=False)
191
+
192
+ # -- If we were not given a widget, then as qute to derive one
193
+ # -- for us
194
+ if not option_widget:
195
+ option_widget = qtility.derive.qwidget(
196
+ option_value,
197
+ )
198
+
199
+ if not option_widget:
200
+ log.warning(f"Could not resolve a widget for : {option.name()}")
201
+ continue
202
+
203
+ option_widget.setToolTip(
204
+ option.description(),
205
+ )
206
+
207
+ # -- Set the value of the widget
208
+ qtility.derive.apply(
209
+ option_widget,
210
+ option_value,
211
+ )
212
+
213
+ # -- Hook up a connection such that when this option ui changes we
214
+ # -- reflect that change into the component
215
+ qtility.derive.connect(
216
+ option_widget,
217
+ functools.partial(
218
+ self.reflect_option_change,
219
+ option_widget,
220
+ option.name(),
221
+ component,
222
+ ),
223
+ )
224
+
225
+ layout_to_add_into = self.option_layout
226
+
227
+ if option.group():
228
+ if option.group() in group_layouts:
229
+ layout_to_add_into = group_layouts[option.group()]
230
+
231
+ else:
232
+ # -- Create the new layout
233
+ layout = QtWidgets.QVBoxLayout()
234
+
235
+ # -- Create the group box
236
+ group_box = QtWidgets.QGroupBox(option.group())
237
+ group_box.setLayout(layout)
238
+
239
+ # -- Add the group box to the main layout
240
+ self.option_layout.addWidget(group_box)
241
+
242
+ # -- Store this so that we only create one instance of it
243
+ # -- and mark this as the layout to add the widget into
244
+ layout_to_add_into = layout
245
+ group_layouts[option.group()] = layout
246
+
247
+ # # -- Add the widget into the ui
248
+ if hasattr(option_widget, "dont_label") and option_widget.dont_label:
249
+ layout_to_add_into.addWidget(option_widget)
250
+
251
+ else:
252
+ layout_to_add_into.addLayout(
253
+ qtility.widgets.addLabel(
254
+ option_widget,
255
+ string.capwords(option.name().replace('_', ' ')),
256
+ 150,
257
+ slim=False,
258
+ ),
259
+ )
260
+
261
+ # -- Always add in a spacer to push all the widgets up to the top
262
+ self.option_layout.addSpacerItem(
263
+ QtWidgets.QSpacerItem(
264
+ 10,
265
+ 0,
266
+ QtWidgets.QSizePolicy.Expanding,
267
+ QtWidgets.QSizePolicy.Expanding,
268
+ ),
269
+ )
270
+
271
+ # ----------------------------------------------------------------------------------
272
+ # noinspection PyUnusedLocal
273
+ @staticmethod
274
+ def reflect_option_change(widget, option_name, component, *args, **kwargs):
275
+ """
276
+ This is called whenever an option ui element is changed. Within this function
277
+ we must push the changed value back into the component
278
+
279
+ Args:
280
+ widget: Widget to read the value from
281
+ option_name: Name of the option to update
282
+ component: xstack.Component instance to update
283
+ *args:
284
+ **kwargs:
285
+
286
+ Returns:
287
+ None
288
+ """
289
+ component.option(option_name).set(
290
+ qtility.derive.value(
291
+ widget,
292
+ ),
293
+ )
294
+
295
+
296
+ # --------------------------------------------------------------------------------------
297
+ # noinspection PyUnresolvedReferences
298
+ class InputsWidget(QtWidgets.QWidget):
299
+ """
300
+ This widget manages the display of the inputs of a component.
301
+
302
+ Note: This is currently largely a copy of the Options Widget, however both
303
+ serve a unique purpose and will likely branch in the future, so they are each
304
+ getting their own classes.
305
+ """
306
+
307
+ # ----------------------------------------------------------------------------------
308
+ def __init__(self, pre_expose_only=False, parent: QtWidgets.QWidget = None):
309
+ super().__init__(parent=parent)
310
+
311
+ # -- This determines if we show all inputs, or only inputs that
312
+ # -- are marked as pre-expsure
313
+ self._pre_expose_only = pre_expose_only
314
+
315
+ self.setLayout(
316
+ qtility.layouts.slimify(
317
+ QtWidgets.QVBoxLayout(),
318
+ ),
319
+ )
320
+
321
+ self.inputs_layout = QtWidgets.QVBoxLayout()
322
+ self.inputs_layout.setContentsMargins(10, 10, 10, 10)
323
+
324
+ self.help_text = QtWidgets.QLabel()
325
+ self.help_text.setWordWrap(True)
326
+
327
+ self.layout().addWidget(self.help_text)
328
+ self.layout().addLayout(self.inputs_layout)
329
+
330
+ # ----------------------------------------------------------------------------------
331
+ # noinspection DuplicatedCode
332
+ def set_component(self, component: "xstack.Component"):
333
+ """
334
+ This sets the target component for the widget and populates the widget
335
+ with all hte inputs.
336
+ """
337
+ # -- Clear the current layout of options
338
+ qtility.layouts.empty(self.inputs_layout)
339
+
340
+ # -- If we do not have a component we dont need to populate anything
341
+ if not component:
342
+ self.help_text.setText("")
343
+ return
344
+
345
+ # -- Update the help text
346
+ self.help_text.setText(component.documentation())
347
+
348
+ group_layouts = dict()
349
+
350
+ for input_ in component.inputs():
351
+
352
+ if input_.hidden():
353
+ continue
354
+
355
+ if self._pre_expose_only and not input_.should_pre_expose():
356
+ continue
357
+
358
+ # -- Get the input widget
359
+ input_widget = component.input_widget(input_.name())
360
+
361
+ # -- If we were not given a widget, then as qute to derive one
362
+ # -- for us
363
+ if not input_widget:
364
+ input_widget = qtility.derive.qwidget(
365
+ input_.get()
366
+ )
367
+
368
+ if not input_widget:
369
+ log.warning(f"Could not resolve a widget for : {input_.name()}, {input_.get()}")
370
+ continue
371
+
372
+ input_widget.setToolTip(
373
+ input_.description(),
374
+ )
375
+
376
+ # -- Get the raw value. If its an address, we want to specifically
377
+ # -- get the address back and not the resolved value
378
+ input_value = input_.get(resolved=False)
379
+
380
+ # -- Set the value of the widget
381
+ qtility.derive.apply(
382
+ input_widget,
383
+ input_value,
384
+ )
385
+
386
+ # -- Hook up a connection such that when this input ui changes we
387
+ # -- reflect that change into the component
388
+ qtility.derive.connect(
389
+ input_widget,
390
+ functools.partial(
391
+ self.reflect_input_change,
392
+ input_widget,
393
+ input_.name(),
394
+ component,
395
+ ),
396
+ )
397
+
398
+ layout_to_add_into = self.inputs_layout
399
+
400
+ if input_.group():
401
+ if input_.group() in group_layouts:
402
+ layout_to_add_into = group_layouts[input_.group()]
403
+
404
+ else:
405
+ # -- Create the new layout
406
+ layout = QtWidgets.QVBoxLayout()
407
+
408
+ # -- Create the group box
409
+ group_box = QtWidgets.QGroupBox(input_.group())
410
+ group_box.setLayout(layout)
411
+
412
+ # -- Add the group box to the main layout
413
+ self.inputs_layout.addWidget(group_box)
414
+
415
+ # -- Store this so that we only create one instance of it
416
+ # -- and mark this as the layout to add the widget into
417
+ layout_to_add_into = layout
418
+ group_layouts[input_.group()] = layout
419
+
420
+ if hasattr(input_widget, "HIDE_LABEL") and input_widget.HIDE_LABEL:
421
+ # # -- Add the widget into the ui
422
+ layout_to_add_into.addWidget(
423
+ input_widget,
424
+ )
425
+
426
+ else:
427
+ # # -- Add the widget into the ui
428
+ layout_to_add_into.addLayout(
429
+ qtility.widgets.addLabel(
430
+ input_widget,
431
+ string.capwords(input_.name().replace('_', ' ')),
432
+ 150,
433
+ slim=False,
434
+ ),
435
+ )
436
+
437
+ self.inputs_layout.addSpacerItem(
438
+ QtWidgets.QSpacerItem(
439
+ 10,
440
+ 0,
441
+ QtWidgets.QSizePolicy.Expanding,
442
+ QtWidgets.QSizePolicy.Expanding,
443
+ ),
444
+ )
445
+
446
+ # ----------------------------------------------------------------------------------
447
+ # noinspection PyUnusedLocal
448
+ @staticmethod
449
+ def reflect_input_change(widget, option_name, component, *args, **kwargs):
450
+ """
451
+ This is called whenever an option ui element is changed. Within this function
452
+ we must push the changed value back into the component
453
+
454
+ Args:
455
+ widget: Widget to read the value from
456
+ option_name: Name of the option to update
457
+ component: xstack.Component instance to update
458
+ *args:
459
+ **kwargs:
460
+
461
+ Returns:
462
+ None
463
+ """
464
+ component.input(option_name).set(
465
+ qtility.derive.value(
466
+ widget,
467
+ ),
468
+ )
469
+
470
+
471
+ # --------------------------------------------------------------------------------------
472
+ # noinspection PyUnresolvedReferences
473
+ class OutputWidget(QtWidgets.QWidget):
474
+
475
+ changed = QtCore.Signal()
476
+
477
+ # ----------------------------------------------------------------------------------
478
+ def __init__(self, output, parent: QtWidgets.QWidget = None):
479
+ super().__init__(parent=parent)
480
+
481
+ self._output_attribute = output
482
+
483
+ self.setLayout(
484
+ qtility.layouts.slimify(
485
+ QtWidgets.QHBoxLayout(),
486
+ ),
487
+ )
488
+
489
+ self.field = QtWidgets.QLineEdit()
490
+ self.field.setReadOnly(True)
491
+ self.field.setMinimumHeight(30)
492
+ self.copy_button = QtWidgets.QPushButton()
493
+ self.copy_button.setIcon(
494
+ QtGui.QIcon(
495
+ resources.get(
496
+ "address.png",
497
+ ),
498
+ ),
499
+ )
500
+ self.copy_button.setIconSize(
501
+ QtCore.QSize(
502
+ 20,
503
+ 20,
504
+ ),
505
+ )
506
+
507
+ self.layout().addWidget(self.field)
508
+ self.layout().addWidget(self.copy_button)
509
+
510
+ self.copy_button.clicked.connect(self.copy_link)
511
+
512
+ # ----------------------------------------------------------------------------------
513
+ def set_value(self, value):
514
+ self.field.setText(value)
515
+
516
+ # ----------------------------------------------------------------------------------
517
+ def get_value(self):
518
+ return self.field.text()
519
+
520
+ # ----------------------------------------------------------------------------------
521
+ def copy_link(self):
522
+ qtility.app.get().clipboard().setText(
523
+ self._output_attribute.address()
524
+ )
525
+
526
+
527
+ # --------------------------------------------------------------------------------------
528
+ # noinspection PyUnresolvedReferences,DuplicatedCode
529
+ class OutputsWidget(QtWidgets.QWidget):
530
+ """
531
+ This widget manages the display of the options
532
+ """
533
+
534
+ # ----------------------------------------------------------------------------------
535
+ def __init__(self, parent: QtWidgets.QWidget = None):
536
+ super().__init__(parent=parent)
537
+
538
+ self.setLayout(
539
+ qtility.layouts.slimify(
540
+ QtWidgets.QVBoxLayout(),
541
+ ),
542
+ )
543
+
544
+ self.output_layout = QtWidgets.QVBoxLayout()
545
+ self.output_layout.setContentsMargins(10, 10, 10, 10)
546
+
547
+ self.help_text = QtWidgets.QLabel()
548
+ self.help_text.setWordWrap(True)
549
+
550
+ self.layout().addWidget(self.help_text)
551
+ self.layout().addLayout(self.output_layout)
552
+
553
+ # ----------------------------------------------------------------------------------
554
+ def reflect_output_change(self, widget, output):
555
+ if widget():
556
+ widget().set_value(output.get())
557
+
558
+ # ----------------------------------------------------------------------------------
559
+ # noinspection PyUnresolvedReferences
560
+ def set_component(self, component: "xstack.Component"):
561
+ """
562
+ This sets the target component for the widget and populates the widget
563
+ with all the options.
564
+ """
565
+
566
+ # -- Clear the current layout of options
567
+ qtility.layouts.empty(self.output_layout)
568
+
569
+ # -- If we do not have a component we dont need to populate anything
570
+ if not component:
571
+ self.help_text.setText("")
572
+ return
573
+
574
+ # -- Update the help text
575
+ self.help_text.setText(component.documentation())
576
+
577
+ group_layouts = dict()
578
+
579
+ for output in component.outputs():
580
+
581
+ # -- Get the option object
582
+ output_widget = OutputWidget(output=output, parent=self)
583
+
584
+ output_widget.setToolTip(
585
+ output.description(),
586
+ )
587
+
588
+ output_widget.set_value(output.get())
589
+
590
+ # -- Hook up a connection such that when this option ui changes we
591
+ # -- reflect that change into the component
592
+ output.value_changed.connect(
593
+ functools.partial(
594
+ self.reflect_output_change,
595
+ weakref.ref(output_widget),
596
+ output,
597
+ )
598
+ )
599
+
600
+ layout_to_add_into = self.output_layout
601
+
602
+ if output.group():
603
+ if output.group() in group_layouts:
604
+ layout_to_add_into = group_layouts[output.group()]
605
+
606
+ else:
607
+ # -- Create the new layout
608
+ layout = QtWidgets.QVBoxLayout()
609
+
610
+ # -- Create the group box
611
+ group_box = QtWidgets.QGroupBox(output.group())
612
+ group_box.setLayout(layout)
613
+
614
+ # -- Add the group box to the main layout
615
+ self.output_layout.addWidget(group_box)
616
+
617
+ # -- Store this so that we only create one instance of it
618
+ # -- and mark this as the layout to add the widget into
619
+ layout_to_add_into = layout
620
+ group_layouts[output.group()] = layout
621
+
622
+ layout_to_add_into.addLayout(
623
+ qtility.widgets.addLabel(
624
+ output_widget,
625
+ string.capwords(output.name().replace('_', ' ')),
626
+ 150,
627
+ slim=False,
628
+ ),
629
+ )
630
+
631
+ # -- Always add in a spacer to push all the widgets up to the top
632
+ self.output_layout.addSpacerItem(
633
+ QtWidgets.QSpacerItem(
634
+ 10,
635
+ 0,
636
+ QtWidgets.QSizePolicy.Expanding,
637
+ QtWidgets.QSizePolicy.Expanding,
638
+ ),
639
+ )
640
+
641
+ # ----------------------------------------------------------------------------------
642
+ # noinspection PyUnusedLocal
643
+ @staticmethod
644
+ def reflect_option_change(widget, option_name, component, *args, **kwargs):
645
+ """
646
+ This is called whenever an option ui element is changed. Within this function
647
+ we must push the changed value back into the component
648
+
649
+ Args:
650
+ widget: Widget to read the value from
651
+ option_name: Name of the option to update
652
+ component: xstack.Component instance to update
653
+ *args:
654
+ **kwargs:
655
+
656
+ Returns:
657
+ None
658
+ """
659
+ component.option(option_name).set(
660
+ qtility.derive.value(
661
+ widget,
662
+ ),
663
+ )
664
+
@@ -0,0 +1,20 @@
1
+ import os
2
+ import functools
3
+
4
+
5
+ # --------------------------------------------------------------------------------------
6
+ @functools.lru_cache()
7
+ def get(resource_name: str) -> str:
8
+ """
9
+ Returns a file from the _res directory.
10
+
11
+ Args:
12
+ resource_name:
13
+
14
+ :return:
15
+ """
16
+ return os.path.join(
17
+ os.path.dirname(__file__),
18
+ '_res',
19
+ resource_name,
20
+ )