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/dialogs.py ADDED
@@ -0,0 +1,492 @@
1
+ import re
2
+ import qtility
3
+ from Qt import QtWidgets, QtCore, QtGui
4
+
5
+ from . import options
6
+
7
+
8
+ # --------------------------------------------------------------------------------------
9
+ # noinspection PyUnresolvedReferences
10
+ class AddComponentWidget(QtWidgets.QDialog):
11
+ """
12
+ This widget is used to present a user with a list of components currently
13
+ available to them.
14
+
15
+ If that component has any pre-exposed options or inputs they will also
16
+ be displayed.
17
+ """
18
+
19
+ # -- This signal is used to track when a component is added to the stack
20
+ component_added = QtCore.Signal()
21
+
22
+ # ----------------------------------------------------------------------------------
23
+ def __init__(self, stack, component_parent, app_config, parent=None):
24
+ super().__init__(parent)
25
+
26
+ # -- We track the currently selected component in a variable, as we
27
+ # -- want to reflect changes in options/inputs of a component
28
+ self.active_component = None
29
+
30
+ # -- We store a reference to the parent we are being asked to add a component
31
+ # -- under. This is to ensure we can inherit values correctly.
32
+ self.component_parent = component_parent
33
+
34
+ # -- The app config is critical to ensuring we provide the user with a
35
+ # -- consistent and contextually relevant experience.
36
+ self.app_config = app_config
37
+
38
+ # -- Store the stack that we're adding to
39
+ self.stack = stack
40
+
41
+ # -- Now we can start building our ui
42
+ self.setLayout(QtWidgets.QVBoxLayout())
43
+
44
+ # -- The search filter allows the user to restrict
45
+ # -- what they see in the list
46
+ self.search_filter = QtWidgets.QLineEdit()
47
+ self.search_filter.setPlaceholderText("Search Filter")
48
+
49
+ # -- This is the widget where we will show the user all the
50
+ # -- available components
51
+ self.component_list = QtWidgets.QListWidget()
52
+
53
+ # -- The help info is where we show the user any information
54
+ # -- the component can give us
55
+ self.help_info = QtWidgets.QLabel()
56
+
57
+ # -- The option panel where we will show options adn inputs
58
+ self.options_widget = options.OptionsWidget(pre_expose_only=True)
59
+
60
+ # -- Our logical buttons
61
+ self.add_button = QtWidgets.QPushButton('Add')
62
+ self.cancel_button = QtWidgets.QPushButton('Cancel')
63
+
64
+ # -- Add our items to our layout
65
+ self.layout().addWidget(self.search_filter)
66
+ self.layout().addWidget(self.component_list)
67
+ self.layout().addWidget(self.help_info)
68
+ self.layout().addWidget(self.options_widget)
69
+
70
+ # -- Add a horizontal layout for the buttons
71
+ h_layout = QtWidgets.QHBoxLayout()
72
+ h_layout.addWidget(self.add_button)
73
+ h_layout.addWidget(self.cancel_button)
74
+ self.layout().addLayout(h_layout)
75
+
76
+ # -- Pre-populate the list
77
+ self.populate()
78
+
79
+ # -- Ensure that we re-populate whenever the user
80
+ # -- updates the search filter
81
+ self.search_filter.textChanged.connect(self.populate)
82
+ self.add_button.clicked.connect(self.add_component)
83
+ self.cancel_button.clicked.connect(self.cancel)
84
+ self.component_list.itemClicked.connect(self.set_component)
85
+
86
+ # -- Set modality
87
+ self.setWindowModality(QtCore.Qt.WindowModal)
88
+
89
+ # -- Set our window branding
90
+ self.setWindowTitle(f"Add {self.app_config.component_label}")
91
+
92
+ self.setWindowIcon(
93
+ QtGui.QIcon(
94
+ self.app_config.icon,
95
+ ),
96
+ )
97
+
98
+ # ----------------------------------------------------------------------------------
99
+ def set_component(self):
100
+ """
101
+ This will set the active component (the selected component).
102
+
103
+ When this happens, we display any options and inputs which are flagged
104
+ as should pre-expose themselves.
105
+
106
+ Note: This does NOT add the component to the stack.
107
+ """
108
+
109
+ # -- Get the current item
110
+ item = self.component_list.currentItem()
111
+
112
+ # -- Reset the help text
113
+ self.help_info.setText("")
114
+
115
+ # -- If there is no item, clear the options and do nothing else.
116
+ if not item:
117
+ self.options_widget.set_component(None)
118
+ return
119
+
120
+ # -- Get a component instance, so we cna populate the options for it
121
+ component = self.stack.component_library.request(item.text())(
122
+ label="",
123
+ stack=self.stack,
124
+ )
125
+
126
+ # -- If we have a component parent, we should check for any attributes
127
+ # -- we expect to have their values inherit from the parent
128
+ if self.component_parent:
129
+ for option in component.options():
130
+ if option.should_inherit() and self.component_parent.option(option.name()):
131
+ option.set(self.component_parent.option(option.name()).get())
132
+
133
+ for input_ in component.inputs():
134
+ if input_.should_inherit() and self.component_parent.input(input_.name()):
135
+ input_.set(self.component_parent.input(input_.name()).get())
136
+
137
+ # -- Ask the option widget to update itself for this component
138
+ self.options_widget.set_component(component)
139
+
140
+ # -- If there is a doc string for this component, show that as help text
141
+ if component.__doc__:
142
+ self.help_info.setText(component.__doc__.strip())
143
+
144
+ # -- Finally we mark this component as being the active component
145
+ self.active_component = component
146
+
147
+ # ----------------------------------------------------------------------------------
148
+ def populate(self):
149
+ """
150
+ This will re-populate the list of components available to the user
151
+ """
152
+ # -- Clear the current list
153
+ self.component_list.clear()
154
+
155
+ # -- We allow the user to filter the list, so get their current filter text
156
+ search_text = self.search_filter.text().lower()
157
+
158
+ # -- Cycle all the components available
159
+ for component_type in sorted(self.stack.component_library.identifiers()):
160
+
161
+ # -- If we're given a filter, and this does not match, then
162
+ # -- skip it
163
+ if search_text and search_text not in component_type.lower():
164
+ continue
165
+
166
+ # -- Request the component
167
+ component = self.stack.component_library.request(component_type)
168
+
169
+ if component.deprecated:
170
+ continue
171
+
172
+ # -- Add a widget item which also shows the icon
173
+ item = QtWidgets.QListWidgetItem(
174
+ QtGui.QIcon(
175
+ component.icon or self.app_config.component_icon,
176
+ ),
177
+ component_type,
178
+ )
179
+
180
+ # -- Add the item
181
+ self.component_list.addItem(item)
182
+
183
+
184
+ # ----------------------------------------------------------------------------------
185
+ def add_component(self):
186
+ """
187
+ This is the user commiting to adding the component to the stack.
188
+
189
+ Note that we do not directly add the instanced component, but we read the data
190
+ from it and ask the stack to add a component with all the same data.
191
+ """
192
+ # -- Get the identifier for the component
193
+ component_type = self.component_list.currentItem().text()
194
+
195
+ # -- If that is not valid, do nothing
196
+ if not component_type:
197
+ return
198
+
199
+ # -- Ask the user to provide a label for the component
200
+ label = qtility.request.text(
201
+ title=f"Add {self.app_config.component_label}",
202
+ message=f"Please give a label for your {self.app_config.component_label}",
203
+ text=self.active_component.suggested_label(),
204
+ parent=self,
205
+ )
206
+
207
+ # -- If the user cancelled, do nothing more
208
+ if not label:
209
+ return
210
+
211
+ # -- We need to extract any option and requirement data that the user
212
+ # -- predefined
213
+ user_set_options = dict()
214
+
215
+ if self.active_component:
216
+ for option in self.active_component.options():
217
+ user_set_options[option.name()] = option.get()
218
+
219
+ user_set_inputs = dict()
220
+
221
+ if self.active_component:
222
+ for input_ in self.active_component.inputs():
223
+ user_set_inputs[input_.name()] = input_.get()
224
+
225
+ # -- Ask the stack to add a new component to the stack with those options
226
+ # -- and requirements.
227
+ self.stack.add_component(
228
+ component_type=component_type,
229
+ label=label,
230
+ parent=self.component_parent,
231
+ options=user_set_options,
232
+ inputs=user_set_inputs,
233
+ )
234
+ self.component_added.emit()
235
+
236
+ # -- Close this window as the process is complete
237
+ self.window().close()
238
+
239
+ # ----------------------------------------------------------------------------------
240
+ def cancel(self):
241
+ """
242
+ Triggered when the user cancels
243
+ """
244
+ self.window().close()
245
+
246
+
247
+ # --------------------------------------------------------------------------------------
248
+ # noinspection PyUnresolvedReferences
249
+ class SwitchComponentTypeDialog(QtWidgets.QDialog):
250
+ """
251
+ This widget is used to present a user with a list of components currently
252
+ available to them.
253
+
254
+ If that component has any pre-exposed options or inputs they will also
255
+ be displayed.
256
+ """
257
+
258
+ # -- This signal is used to track when a component is added to the stack
259
+ component_switched = QtCore.Signal()
260
+
261
+ # ----------------------------------------------------------------------------------
262
+ def __init__(self, component, app_config, parent=None):
263
+ super().__init__(parent)
264
+
265
+ self.component = component
266
+ self.app_config = app_config
267
+ self.stack = component.stack
268
+ self.active_component_type = None
269
+
270
+ # -- Now we can start building our ui
271
+ self.setLayout(QtWidgets.QVBoxLayout())
272
+
273
+ # -- The search filter allows the user to restrict
274
+ # -- what they see in the list
275
+ self.search_filter = QtWidgets.QLineEdit()
276
+ self.search_filter.setPlaceholderText("Search Filter")
277
+
278
+ # -- This is the widget where we will show the user all the
279
+ # -- available components
280
+ self.component_list = QtWidgets.QListWidget()
281
+
282
+ # -- Our logical buttons
283
+ self.switch_button = QtWidgets.QPushButton('Switch')
284
+ self.cancel_button = QtWidgets.QPushButton('Cancel')
285
+
286
+ # -- The help info is where we show the user any information
287
+ # -- the component can give us
288
+ self.help_info = QtWidgets.QLabel()
289
+
290
+ # -- Add our items to our layout
291
+ self.layout().addWidget(self.search_filter)
292
+ self.layout().addWidget(self.component_list)
293
+ self.layout().addWidget(self.help_info)
294
+
295
+ # -- Add a horizontal layout for the buttons
296
+ h_layout = QtWidgets.QHBoxLayout()
297
+ h_layout.addWidget(self.switch_button)
298
+ h_layout.addWidget(self.cancel_button)
299
+ self.layout().addLayout(h_layout)
300
+
301
+ # -- Pre-populate the list
302
+ self.populate()
303
+
304
+ # -- Ensure that we re-populate whenever the user
305
+ # -- updates the search filter
306
+ self.search_filter.textChanged.connect(self.populate)
307
+ self.switch_button.clicked.connect(self.switch_component)
308
+ self.cancel_button.clicked.connect(self.cancel)
309
+ self.component_list.itemClicked.connect(self.set_component)
310
+
311
+ # -- Set modality
312
+ self.setWindowModality(QtCore.Qt.WindowModal)
313
+
314
+ # -- Set our window branding
315
+ self.setWindowTitle(f"Switch {self.app_config.component_label}")
316
+
317
+ self.setWindowIcon(
318
+ QtGui.QIcon(
319
+ self.app_config.icon,
320
+ ),
321
+ )
322
+
323
+ # ----------------------------------------------------------------------------------
324
+ def set_component(self):
325
+
326
+ # -- Get the current item
327
+ item = self.component_list.currentItem()
328
+
329
+ # -- Reset the help text
330
+ self.help_info.setText("")
331
+
332
+ # -- If there is no item, clear the options and do nothing else.
333
+ if not item:
334
+ self.active_component_type = None
335
+ return
336
+
337
+ # -- Get a component class
338
+ component = self.stack.component_library.request(item.text())
339
+
340
+ # -- If there is a doc string for this component, show that as help text
341
+ if component.__doc__:
342
+ self.help_info.setText(component.__doc__.strip())
343
+
344
+ self.active_component_type = item.text()
345
+
346
+ # ----------------------------------------------------------------------------------
347
+ def populate(self):
348
+ """
349
+ This will re-populate the list of components available to the user
350
+ """
351
+ # -- Clear the current list
352
+ self.component_list.clear()
353
+
354
+ # -- We allow the user to filter the list, so get their current filter text
355
+ search_text = self.search_filter.text().lower()
356
+
357
+ # -- Cycle all the components available
358
+ for component_type in sorted(self.stack.component_library.identifiers()):
359
+
360
+ # -- If we're given a filter, and this does not match, then
361
+ # -- skip it
362
+ if search_text and search_text not in component_type.lower():
363
+ continue
364
+
365
+ # -- Request the component
366
+ component = self.stack.component_library.request(component_type)
367
+
368
+ # -- Add a widget item which also shows the icon
369
+ item = QtWidgets.QListWidgetItem(
370
+ QtGui.QIcon(
371
+ component.icon or self.app_config.component_icon,
372
+ ),
373
+ component_type,
374
+ )
375
+
376
+ # -- Add the item
377
+ self.component_list.addItem(item)
378
+
379
+ # ----------------------------------------------------------------------------------
380
+ def switch_component(self):
381
+ self.stack.replace_component(
382
+ component=self.component,
383
+ new_component_type=self.active_component_type,
384
+ )
385
+ # -- Close this window as the process is complete
386
+ self.window().close()
387
+
388
+ # ----------------------------------------------------------------------------------
389
+ def cancel(self):
390
+ """
391
+ Triggered when the user cancels
392
+ """
393
+ self.window().close()
394
+
395
+ # --------------------------------------------------------------------------------------
396
+ # noinspection PyUnresolvedReferences
397
+ class DuplicateComponentDialog(QtWidgets.QDialog):
398
+ """
399
+ Asks the user for the name of a duplicated component, along with an
400
+ optional search and replace to apply to the duplicate's values.
401
+
402
+ Read the results from label(), search_for() and replace_with() once the
403
+ dialog has been accepted.
404
+ """
405
+
406
+ # ----------------------------------------------------------------------------------
407
+ def __init__(self, component, app_config, parent=None):
408
+ super().__init__(parent)
409
+
410
+ self.component = component
411
+ self.app_config = app_config
412
+
413
+ self.setLayout(QtWidgets.QVBoxLayout())
414
+
415
+ # -- The name of the duplicate, defaulting to the source component's label
416
+ self.label_field = QtWidgets.QLineEdit()
417
+ self.label_field.setText(component.label())
418
+ self.label_field.setPlaceholderText("Name")
419
+
420
+ # -- The search and replace sit side by side beneath the name
421
+ self.search_field = QtWidgets.QLineEdit()
422
+ self.search_field.setPlaceholderText("Search For")
423
+
424
+ self.replace_field = QtWidgets.QLineEdit()
425
+ self.replace_field.setPlaceholderText("Replace With")
426
+
427
+ # -- Our logical buttons
428
+ self.duplicate_button = QtWidgets.QPushButton("Duplicate")
429
+ self.cancel_button = QtWidgets.QPushButton("Cancel")
430
+ self.duplicate_button.setDefault(True)
431
+
432
+ # -- Add our items to our layout
433
+ self.layout().addWidget(QtWidgets.QLabel("Name"))
434
+ self.layout().addWidget(self.label_field)
435
+
436
+ search_layout = QtWidgets.QHBoxLayout()
437
+ search_layout.addWidget(self.search_field)
438
+ search_layout.addWidget(self.replace_field)
439
+ self.layout().addLayout(search_layout)
440
+
441
+ button_layout = QtWidgets.QHBoxLayout()
442
+ button_layout.addWidget(self.duplicate_button)
443
+ button_layout.addWidget(self.cancel_button)
444
+ self.layout().addLayout(button_layout)
445
+
446
+ self.duplicate_button.clicked.connect(self.validate_and_accept)
447
+ self.cancel_button.clicked.connect(self.reject)
448
+
449
+ # -- Start with the name selected so the user can type straight over it
450
+ self.label_field.selectAll()
451
+ self.label_field.setFocus()
452
+
453
+ # -- Set our window branding
454
+ self.setWindowTitle(f"Duplicate {self.app_config.component_label}")
455
+
456
+ self.setWindowIcon(
457
+ QtGui.QIcon(
458
+ self.app_config.icon,
459
+ ),
460
+ )
461
+
462
+ # ----------------------------------------------------------------------------------
463
+ def label(self):
464
+ return self.label_field.text()
465
+
466
+ # ----------------------------------------------------------------------------------
467
+ def search_for(self):
468
+ return self.search_field.text()
469
+
470
+ # ----------------------------------------------------------------------------------
471
+ def replace_with(self):
472
+ return self.replace_field.text()
473
+
474
+ # ----------------------------------------------------------------------------------
475
+ def validate_and_accept(self):
476
+ """
477
+ The search is treated as a regular expression, so an invalid pattern
478
+ is reported here rather than failing part way through the duplicate.
479
+ """
480
+ if self.search_for():
481
+ try:
482
+ re.compile(self.search_for())
483
+
484
+ except re.error as error:
485
+ qtility.request.message(
486
+ title="Invalid Search",
487
+ message=f"The search is not a valid regular expression: {error}",
488
+ parent=self,
489
+ )
490
+ return
491
+
492
+ self.accept()