-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMainWindowWithSessionManagement_example.py
101 lines (79 loc) · 3.5 KB
/
MainWindowWithSessionManagement_example.py
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
import sys
from qtpy import QtWidgets
from qtpy.QtWidgets import QApplication
from eqt import __version__
from eqt.ui.MainWindowWithSessionManagement import MainWindowWithSessionManagement
from eqt.ui.UIFormWidget import FormWidget
from eqt.ui.UISliderWidget import UISliderWidget
class ExampleMainWindowWithSessionManagement(MainWindowWithSessionManagement):
'''
This is an example of a MainWindowWithSessionManagement.
It is used to show how to use the MainWindowWithSessionManagement class.
To test out this example, change the state of the widgets and save the session.
Then load the session again and check if the state of the widgets is the same as before.
'''
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
form_widget = FormWidget(self)
add_every_widget_to_form(form_widget)
self.setCentralWidget(form_widget)
self.addToMenu()
def addToMenu(self):
''' Adds an example extra menu.'''
extra_menu = self.menu_bar.addMenu("Extra Menu")
self.menu_bar.addMenu(extra_menu)
self.menus["Extra"] = extra_menu
def getSessionConfig(self):
'''
This function is called when the user wants to save the current session.
We need to add the state of the widgets to the config.
Returns
-------
config : dict
The config of the current session.
'''
form_widget = self.centralWidget()
all_widget_states = form_widget.getAllWidgetStates()
config = {'form_widget': all_widget_states}
return config
def finishLoadConfig(self, process_name):
'''
This function is called after the config of the chosen session is loaded.
It is used to set the state of the widgets.
'''
form_widget_config = self.config['form_widget']
form_widget = self.centralWidget()
form_widget.applyWidgetStates(form_widget_config)
super().finishLoadConfig(process_name)
def add_every_widget_to_form(form):
'''
Generate every widget and add it to the form
Parameters
----------
form : FormWidget, FormDialog or FormDockWidget
The form to add the widgets to
'''
form.addWidget(QtWidgets.QLabel('test label'), 'Label: ', 'label')
form.addWidget(QtWidgets.QCheckBox('test checkbox'), 'CheckBox: ', 'checkBox')
form.addWidget(QtWidgets.QComboBox(), 'ComboBox: ', 'comboBox')
form.addWidget(QtWidgets.QDoubleSpinBox(), 'DoubleSpinBox: ', 'doubleSpinBox')
form.addWidget(QtWidgets.QSpinBox(), 'SpinBox: ', 'spinBox')
form.addWidget(QtWidgets.QSlider(), 'Slider: ', 'slider')
form.addWidget(UISliderWidget(QtWidgets.QLabel()), 'UISliderWidget: ', 'uiSliderWidget')
form.addWidget(QtWidgets.QRadioButton('test'), 'RadioButton: ', 'radioButton')
form.addWidget(QtWidgets.QTextEdit('test'), 'TextEdit: ', 'textEdit')
form.addWidget(QtWidgets.QPlainTextEdit('test'), 'PlainTextEdit: ', 'plainTextEdit')
form.addWidget(QtWidgets.QLineEdit('test'), 'LineEdit: ', 'lineEdit')
form.addWidget(QtWidgets.QPushButton('test'), 'Button: ', 'button')
def create_main_window():
window = ExampleMainWindowWithSessionManagement(f"Example Session Main Window{__version__}",
"Example0", settings_name="Example0")
return window
def main():
# open main window:
app = QApplication(sys.argv)
window = create_main_window()
window.show()
app.exec_()
if __name__ == "__main__":
main()