From 87f6fe76cdd63b429aff93b113850782f79b9887 Mon Sep 17 00:00:00 2001 From: Laura Murgatroyd <60604372+lauramurgatroyd@users.noreply.github.com> Date: Thu, 19 Jan 2023 15:48:02 +0000 Subject: [PATCH] Add methods to UIFormWidget for saving and restoring widget states (#64) * Use pip install instead of setup.py install * begin work on saving states of widgets * Add working unit test * Always convert version to pep440 * only test dialog if we can connect to display * improve skip condition for GUI test * improve widget state interface * Add more skips * Test if we fail gh-a if test not present * Test if try-except makes test fail * Test if try-except makes test fail * Try excepthook code * Add exec to see if it makes a difference * Add many more unit tests * alter test for whether to run the test * Add example of dialog which restores its state * Add many more unit tests * Add multiple radiobuttons to example * Add authors * Update CHANGELOG.md * Update CHANGELOG.md * Update eqt/ui/UIFormWidget.py Co-authored-by: Edoardo Pasca * Update eqt/ui/UIFormWidget.py Co-authored-by: Edoardo Pasca * Apply suggestions from code review Co-authored-by: Edoardo Pasca * Apply suggestions from code review Co-authored-by: Edoardo Pasca * update docstrings re: code review * further docstring update Co-authored-by: Edoardo Pasca --- CHANGELOG.md | 20 + conda/bld.bat | 2 +- conda/build.sh | 2 +- eqt/ui/FormDialog.py | 91 +++ eqt/ui/UIFormWidget.py | 272 +++++++ examples/dialog_save_state_example.py | 69 ++ setup.py | 4 +- test/test__formUI_status_test.py | 1083 +++++++++++++++++++++++++ 8 files changed, 1539 insertions(+), 4 deletions(-) create mode 100644 examples/dialog_save_state_example.py create mode 100644 test/test__formUI_status_test.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 61dcbc1..229a25c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,23 @@ +## vx.x.x + +- Adds the following new methods to UIFormWidget, FormWidget, FormDialog and FormDockWidget: + - `saveAllWidgetStates` - Saves the state of all widgets in the form. This can be used to restore the state of the widgets using the restoreAllSavedWidgetStates method. + + - `restoreAllSavedWidgetStates` - Restores the state of all widgets in the form to the state saved by the saveAllWidgetStates method. If the saveAllWidgetStates method has not been called, this method will do nothing. + + - `getAllWidgetStates` - Returns a dictionary of the state of all widgets in the form. + + - `getWidgetState` - Returns the state of the widget. + + - `applyWidgetState` - Applies the given state to the widget with the given name. + + - `applyWidgetStates` - Applies the given state to the widgets in the form given by the keys of the state dictionary. + +- Adds an example of a FormDialog: `dialog_save_state_example.py` where all of the widgets are saved and restored if you press "Ok", whereas the previous values of the dialog are restored if you press "Cancel". +- Adds unit tests to cover: `saveAllWidgetStates`, `restoreAllSavedWidgetStates`, `getAllWidgetStates`, `getWidgetState`, `applyWidgetState`, `applyWidgetStates` +- setup.py: + - Always normalise the version from git describe to pep440 + ## v0.5.0 * Add getWidgets method to FormWidget, FormDockWidget and FormDialog * Add setWidgetVisibility method to FormWidget, FormDockWidget and FormDialog diff --git a/conda/bld.bat b/conda/bld.bat index dc75736..5950f51 100644 --- a/conda/bld.bat +++ b/conda/bld.bat @@ -1,4 +1,4 @@ cd %RECIPE_DIR%\.. -%PYTHON% setup.py install +pip install . diff --git a/conda/build.sh b/conda/build.sh index 26b09c7..e68ca1a 100644 --- a/conda/build.sh +++ b/conda/build.sh @@ -5,4 +5,4 @@ #$PYTHON -m pip install eqt --no-deps cd $RECIPE_DIR/.. -$PYTHON setup.py install +pip install . diff --git a/eqt/ui/FormDialog.py b/eqt/ui/FormDialog.py index d6fb545..5aaa7e2 100644 --- a/eqt/ui/FormDialog.py +++ b/eqt/ui/FormDialog.py @@ -1,3 +1,5 @@ +# Author: Edoardo Pasca, Laura Murgatroyd + from PySide2 import QtCore, QtGui, QtWidgets from eqt.ui import UIFormFactory @@ -106,4 +108,93 @@ def setWidgetVisible(self, name, visible): ''' self.formWidget.setWidgetVisible(name, visible) + + def saveAllWidgetStates(self): + ''' + Saves the state of all widgets in the form. + This can be used to restore the state of the widgets using the restoreAllSavedWidgetStates method. + ''' + self.formWidget.saveAllWidgetStates() + + def restoreAllSavedWidgetStates(self): + ''' + Restores the state of all widgets in the form to the state saved by the saveAllWidgetStates method. + If the saveAllWidgetStates method has not been called, this method will do nothing. + ''' + self.formWidget.restoreAllSavedWidgetStates() + + def getAllWidgetStates(self): + ''' + Returns a dictionary of the state of all widgets in the form. + Returns + ------- + state: dict + A dictionary of the state of the widget/s, with the key/s being the name of the widget/s, and the value/s + being a dictionary with the keys 'value', 'enabled', and 'visible', which store the value, enabled state, + and visible state of the widget. The value may be a string, boolean, or integer, depending on the type of widget. + E.g. {{'widget1': {'value': 1, 'enabled': True, 'visible': True}, 'widget2': {'value': 2, 'enabled': False, 'visible': False}} + ''' + return self.formWidget.getAllWidgetStates() + + def getWidgetState(self, widget, role=None): + ''' + Returns the state of the widget. + + Parameters + ---------- + widget: QWidget or str + The widget to get the state of, or the name of the widget to get the state of, in which case it will be retrieved from + the widgets dictionary using the name. + role: str, optional, default None, values: 'label', 'field', None. + The role of the widget to get the state of. This is only used if widget is a string. + If not given, the state will be returned for the widget with name: widget. + If this fails, and the role is not given, the state will be returned for the widget with name: widget_field. + If given, the state will be returned for the widget with name: widget_role. + + + Returns + ------- + dict + A dictionary of the state of the widget, with the keys 'value', 'enabled', and 'visible', + which store the value, enabled state, and visible state of the widget. + The value may be a string, boolean, or integer, depending on the type of widget. + E.g. {'value': 1, 'enabled': True, 'visible': True} + This dictionary can be used to restore the state of the widget using the setWidgetState method. + ''' + return self.formWidget.getWidgetState(widget, role) + + def applyWidgetState(self, name, state, role=None): + ''' + Applies the given state to the widget with the given name. + + Parameters + ---------- + name: str + The name of the widget to apply the state to + role: str, optional, default None, values: 'label', 'field', None. + The role of the widget to apply the state to. If not given, the state will be applied to the widget with name: name. + If this fails, and the role is not given, the state will be applied to the widget with name: name_field. + If given, the state will be applied to the widget with name: name_role. + state: dict + A dictionary of the state of the widget, with keys 'value', 'enabled', and 'visible', which store the value, enabled state, + and visible state of the widget. + The value may be a string, boolean, or integer, depending on the type of widget. + E.g. {'value': 1, 'enabled': True, 'visible': True} + ''' + return self.formWidget.applyWidgetState(name, state, role) + + def applyWidgetStates(self, state): + ''' + Applies the given state to the widgets in the form given by the keys of the state dictionary. + + Parameters + ---------- + state: dict + A dictionary of the state of the widgets, with the keys being the name of the widgets, and the value + being a dictionary with the keys 'value', 'enabled', and 'visible', which store the value, enabled state, + and visible state of the widget. + The value may be a string, boolean, or integer, depending on the type of widget. + E.g. {{'widget1': {'value': 1, 'enabled': True, 'visible': True}, 'widget2': {'value': 2, 'enabled': False, 'visible': False}} + ''' + return self.formWidget.applyWidgetStates(state) \ No newline at end of file diff --git a/eqt/ui/UIFormWidget.py b/eqt/ui/UIFormWidget.py index 55177c0..fcf06a8 100644 --- a/eqt/ui/UIFormWidget.py +++ b/eqt/ui/UIFormWidget.py @@ -1,4 +1,7 @@ +# Author: Edoardo Pasca, Laura Murgatroyd, Samuel Stock + from PySide2 import QtWidgets +from eqt.ui.UISliderWidget import UISliderWidget class UIFormWidget(object): @@ -139,6 +142,186 @@ def _addWidget(self, name, qwidget, qlabel=None): formLayout.setWidget(widgetno, field_form_role, qwidget) self.num_widgets += 1 + + def getAllWidgetStates(self): + ''' + Returns the state of all widgets in the form. + Returns + ------- + dict + A dictionary of the states of all widgets in the form, keyed by the name of the widget, + and the value being a dictionary with the state of the widget. The dictionary + containing the state of the widget has the keys 'visible', 'value' and 'enabled', and the values + + ''' + all_widget_states = {} + for name, widget in self.widgets.items(): + widget_state = self.getWidgetState(widget) + all_widget_states[name] = widget_state + return all_widget_states + + def getWidgetState(self, widget, role=None): + ''' + Returns the state of the widget. + + Parameters + ---------- + widget: QWidget or str + The widget to get the state of, or the name of the widget to get the state of, in which case it will be retrieved from + the widgets dictionary using the name. + role: str, optional, default None, values: 'label', 'field', None. + The role of the widget to get the state of. This is only used if widget is a string. + If not given, the state will be returned for the widget with name: widget. + If this fails, and the role is not given, the state will be returned for the widget with name: widget_field. + If given, the state will be returned for the widget with name: widget_role. + + + Returns + ------- + dict + A dictionary of the state of the widget, with the keys 'value', 'enabled', and 'visible', + which store the value, enabled state, and visible state of the widget. + The value may be a string, boolean, or integer, depending on the type of widget. + E.g. {'value': 1, 'enabled': True, 'visible': True} + This dictionary can be used to restore the state of the widget using the setWidgetState method. + ''' + if widget is None: + raise ValueError('The widget (or name of widget) must be given') + + if isinstance(widget, str): + if role is not None: + if role not in ['label', 'field']: + raise ValueError('role must be either "label", "field" or None') + name = widget + '_' + role + else: + name = widget + + try: + widget = self.widgets[name] + except KeyError: + if role is None: + try: + widget = self.widgets[name + '_field'] + except KeyError: + raise KeyError('No widget with name: ' + name + ' or ' + name + '_field') + else: + raise KeyError('No widget with name: ' + name) + + widget_state = {} + widget_state['enabled'] = widget.isEnabled() + widget_state['visible'] = widget.isVisible() + + if isinstance(widget, QtWidgets.QLabel): + widget_state['value'] = widget.text() + elif isinstance(widget, (QtWidgets.QCheckBox, QtWidgets.QPushButton)): + widget_state['value'] = widget.isChecked() + elif isinstance(widget, QtWidgets.QComboBox): + widget_state['value'] = widget.currentIndex() + elif isinstance(widget, UISliderWidget) or isinstance(widget, QtWidgets.QSlider): + widget_state['value'] = widget.value() + elif isinstance(widget, (QtWidgets.QDoubleSpinBox, QtWidgets.QSpinBox)): + widget_state['value'] = widget.value() + elif isinstance(widget, QtWidgets.QLineEdit): + widget_state['value'] = widget.text() + elif isinstance(widget, QtWidgets.QRadioButton): + widget_state['value'] = widget.isChecked() + elif isinstance(widget, (QtWidgets.QTextEdit, QtWidgets.QPlainTextEdit)): + widget_state['value'] = widget.toPlainText() + + return widget_state + + + def applyWidgetState(self, name, state, role=None): + ''' + Applies the given state to the widget with the given name. + + Parameters + ---------- + name: str + The name of the widget to apply the state to + role: str, optional, default None, values: 'label', 'field', None. + The role of the widget to apply the state to. If not given, the state will be applied to the widget with name: name. + If this fails, and the role is not given, the state will be applied to the widget with name: name_field. + If given, the state will be applied to the widget with name: name_role. + state: dict + A dictionary of the state of the widget, with keys 'value', 'enabled', and 'visible', which store the value, enabled state, + and visible state of the widget. + The value may be a string, boolean, or integer, depending on the type of widget. + E.g. {'value': 1, 'enabled': True, 'visible': True} + ''' + if role is not None: + if role not in ['label', 'field']: + raise ValueError('role must be either "label", "field" or None') + name = name + '_' + role + + try: + widget = self.widgets[name] + except KeyError: + if role is None: + try: + widget = self.widgets[name + '_field'] + except KeyError: + raise KeyError('No widget with name: ' + name + ' or ' + name + '_field') + else: + raise KeyError('No widget with name: ' + name) + + for key, value in state.items(): + if key == 'enabled': + widget.setEnabled(value) + elif key == 'visible': + widget.setVisible(value) + elif key == 'value': + if isinstance(widget, QtWidgets.QLabel): + widget.setText(value) + elif isinstance(widget, QtWidgets.QCheckBox): + widget.setChecked(value) + elif isinstance(widget, QtWidgets.QComboBox): + widget.setCurrentIndex(value) + elif isinstance(widget, (UISliderWidget, QtWidgets.QSlider)): + widget.setValue(value) + elif isinstance(widget, (QtWidgets.QDoubleSpinBox, QtWidgets.QSpinBox)): + widget.setValue(value) + elif isinstance(widget, QtWidgets.QPushButton): + widget.setChecked(value) + elif isinstance(widget, QtWidgets.QLineEdit): + widget.setText(value) + elif isinstance(widget, QtWidgets.QRadioButton): + widget.setChecked(value) + elif isinstance(widget, (QtWidgets.QTextEdit, QtWidgets.QPlainTextEdit)): + widget.setPlainText(value) + + + def applyWidgetStates(self, state): + ''' + Applies the given state to the widgets in the form. + + Parameters + ---------- + state: dict + A dictionary of the state of the widgets, with the key being the name of the widget, and the value + being a dictionary with the keys 'value', 'enabled', and 'visible', which store the value, enabled state, + and visible state of the widget. + The value may be a string, boolean, or integer, depending on the type of widget. + E.g. {{'widget1': {'value': 1, 'enabled': True, 'visible': True}, 'widget2': {'value': 2, 'enabled': False, 'visible': False}} + ''' + for name, widget_state in state.items(): + self.applyWidgetState(name, widget_state) + + def saveAllWidgetStates(self): + ''' + Saves the state of all widgets in the form. + This can be used to restore the state of the widgets using the restoreAllSavedWidgetStates method. + ''' + self.widget_states = self.getAllWidgetStates() + + def restoreAllSavedWidgetStates(self): + ''' + Restores the state of all widgets in the form to the state saved by the saveAllWidgetStates method. + If the saveAllWidgetStates method has not been called, this method will do nothing. + ''' + if hasattr(self, 'widget_states'): + self.applyWidgetStates(self.widget_states) + class FormWidget(QtWidgets.QWidget, UIFormWidget): def __init__(self, parent=None): # dockWidgetContents = QtWidgets.QWidget() @@ -185,7 +368,96 @@ def setWidgetVisible(self, name, visible): self.widget().setWidgetVisible(name, visible) + def saveAllWidgetStates(self): + ''' + Saves the state of all widgets in the form. + This can be used to restore the state of the widgets using the restoreAllSavedWidgetStates method. + ''' + self.widget().saveAllWidgetStates() + + def restoreAllSavedWidgetStates(self): + ''' + Restores the state of all widgets in the form to the state saved by the saveAllWidgetStates method. + If the saveAllWidgetStates method has not been called, this method will do nothing. + ''' + self.widget().restoreAllSavedWidgetStates() + + def getAllWidgetStates(self): + ''' + Returns a dictionary of the state of all widgets in the form. + Returns + ------- + state: dict + A dictionary of the state of the widget/s, with the key/s being the name of the widget/s, and the value/s + being a dictionary with the keys 'value', 'enabled', and 'visible', which store the value, enabled state, + and visible state of the widget. The value may be a string, boolean, or integer, depending on the type of widget. + E.g. {{'widget1': {'value': 1, 'enabled': True, 'visible': True}, 'widget2': {'value': 2, 'enabled': False, 'visible': False}} + ''' + return self.widget().getAllWidgetStates() + + def getWidgetState(self, widget, role=None): + ''' + Returns the state of the widget. + + Parameters + ---------- + widget: QWidget or str + The widget to get the state of, or the name of the widget to get the state of, in which case it will be retrieved from + the widgets dictionary using the name. + role: str, optional, default None, values: 'label', 'field', None. + The role of the widget to get the state of. This is only used if widget is a string. + If not given, the state will be returned for the widget with name: widget. + If this fails, and the role is not given, the state will be returned for the widget with name: widget_field. + If given, the state will be returned for the widget with name: widget_role. + + + Returns + ------- + dict + A dictionary of the state of the widget, with the keys 'value', 'enabled', and 'visible', + which store the value, enabled state, and visible state of the widget. + The value may be a string, boolean, or integer, depending on the type of widget. + E.g. {'value': 1, 'enabled': True, 'visible': True} + This dictionary can be used to restore the state of the widget using the setWidgetState method. + ''' + return self.widget().getWidgetState(widget, role) + + def applyWidgetState(self, name, state, role=None): + ''' + Applies the given state to the widget with the given name. + + Parameters + ---------- + name: str + The name of the widget to apply the state to + role: str, optional, default None, values: 'label', 'field', None. + The role of the widget to apply the state to. If not given, the state will be applied to the widget with name: name. + If this fails, and the role is not given, the state will be applied to the widget with name: name_field. + If given, the state will be applied to the widget with name: name_role. + state: dict + A dictionary of the state of the widget, with keys 'value', 'enabled', and 'visible', which store the value, enabled state, + and visible state of the widget. + The value may be a string, boolean, or integer, depending on the type of widget. + E.g. {'value': 1, 'enabled': True, 'visible': True} + ''' + return self.widget().applyWidgetState(name, state, role) + + def applyWidgetStates(self, state): + ''' + Applies the given state to the widgets in the form given by the keys of the state dictionary. + + Parameters + ---------- + state: dict + A dictionary of the state of the widgets, with the keys being the name of the widgets, and the value + being a dictionary with the keys 'value', 'enabled', and 'visible', which store the value, enabled state, + and visible state of the widget. + The value may be a string, boolean, or integer, depending on the type of widget. + E.g. {{'widget1': {'value': 1, 'enabled': True, 'visible': True}, 'widget2': {'value': 2, 'enabled': False, 'visible': False}} + ''' + return self.widget().applyWidgetStates(state) + class UIFormFactory(QtWidgets.QWidget): diff --git a/examples/dialog_save_state_example.py b/examples/dialog_save_state_example.py new file mode 100644 index 0000000..b1bc374 --- /dev/null +++ b/examples/dialog_save_state_example.py @@ -0,0 +1,69 @@ +# Author: Laura Murgatroyd + +from PySide2 import QtWidgets +import sys +from eqt.ui import FormDialog +from eqt.ui.UISliderWidget import UISliderWidget + +class MainUI(QtWidgets.QMainWindow): + + def __init__(self, parent = None): + QtWidgets.QMainWindow.__init__(self, parent) + + pb = QtWidgets.QPushButton(self) + pb.setText("Open Dialog with form layout") + pb.clicked.connect(lambda: self.openFormDialog()) + + layout = QtWidgets.QHBoxLayout() + layout.addWidget(pb) + widg = QtWidgets.QWidget() + widg.setLayout(layout) + + self.setCentralWidget(widg) + + # create dialog to be opened later: + dialog = FormDialog(parent=self, title='Example') + dialog.Ok.clicked.connect(lambda: self.accepted()) + dialog.Cancel.clicked.connect(lambda: self.rejected()) + + ### Example on how to add elements to the + dialog.addWidget(QtWidgets.QLabel('test label'), 'Label: ', 'label') + dialog.addWidget(QtWidgets.QCheckBox('test checkbox'), 'CheckBox: ', 'checkBox') + combobox = QtWidgets.QComboBox() + combobox.addItems(['test1', 'test2']) + dialog.addWidget(combobox, 'ComboBox: ', 'comboBox') + dialog.addWidget(QtWidgets.QDoubleSpinBox(), 'DoubleSpinBox: ', 'doubleSpinBox') + dialog.addWidget(QtWidgets.QSpinBox(), 'SpinBox: ', 'spinBox') + dialog.addWidget(QtWidgets.QSlider(), 'Slider: ', 'slider') + dialog.addWidget(UISliderWidget(QtWidgets.QLabel()), 'UISliderWidget: ', 'uiSliderWidget') + dialog.addWidget(QtWidgets.QRadioButton('test 1'), 'RadioButton 1: ', 'radioButton') + dialog.addWidget(QtWidgets.QRadioButton('test 2'), 'RadioButton 2: ', 'radioButton') + dialog.addWidget(QtWidgets.QTextEdit('test'), 'TextEdit: ', 'textEdit') + dialog.addWidget(QtWidgets.QPlainTextEdit('test'), 'PlainTextEdit: ', 'plainTextEdit') + dialog.addWidget(QtWidgets.QLineEdit('test'), 'LineEdit: ', 'lineEdit') + button = QtWidgets.QPushButton('test') + button.setCheckable(True) + dialog.addWidget(button, 'Button: ', 'button') + + # store a reference + self.dialog = dialog + + self.show() + + def openFormDialog(self): + self.dialog.restoreAllSavedWidgetStates() + self.dialog.exec() + + def accepted(self): + self.dialog.saveAllWidgetStates() + self.dialog.close() + + def rejected(self): + self.dialog.close() + +if __name__ == "__main__": + app = QtWidgets.QApplication(sys.argv) + + window = MainUI() + + sys.exit(app.exec_()) \ No newline at end of file diff --git a/setup.py b/setup.py index 37db5b3..37d6a08 100644 --- a/setup.py +++ b/setup.py @@ -40,7 +40,6 @@ def version2pep440(version): if os.environ.get('CONDA_BUILD', 0) == '1': # if it is a conda build requirements are going to be satisfied by conda install_requires = [] - version = git_version_string cwd = os.path.join(os.environ.get('RECIPE_DIR'),'..') else: install_requires = [ @@ -49,9 +48,10 @@ def version2pep440(version): 'pyside2' ] - version = version2pep440(git_version_string) cwd = os.getcwd() +version = version2pep440(git_version_string) + print ('version {}'.format(version)) print(cwd) diff --git a/test/test__formUI_status_test.py b/test/test__formUI_status_test.py new file mode 100644 index 0000000..61bd5c3 --- /dev/null +++ b/test/test__formUI_status_test.py @@ -0,0 +1,1083 @@ +import os +import sys +import unittest +from unittest import mock + +from eqt.ui.FormDialog import FormDialog +from eqt.ui.UIFormWidget import FormWidget, FormDockWidget +from PySide2 import QtWidgets +from PySide2.QtWidgets import QApplication +from eqt.ui.UISliderWidget import UISliderWidget + +# TODO: +# test using role names for retrieval vs normal names + +# skip the tests on GitHub actions +if os.environ.get('CONDA_BUILD', '0') == '1': + skip_test = True +else: + skip_test = False + +print("skip_test is set to ", skip_test) + + +if not skip_test: + app = QApplication(sys.argv) +else: + skip_test = True + + +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 add_two_widgets_to_form(form): + ''' + Generate two widgets and add them 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') + + +@unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") +class FormDialogStatusTest(unittest.TestCase): + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def setUp(self): + self.form = FormDialog() + add_every_widget_to_form(self.form) + self.simple_form = FormDialog() + add_two_widgets_to_form(self.simple_form) + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_getWidgetState_returns_visibility(self): + # Check that the visibility of the widget is saved to the state + # Have to use magic mock as we can't set the visibility of the QLabel + # to be True, because the FormDialog is not visible + initial_label_visibility = True + self.form.getWidget('label').isVisible = mock.MagicMock() + self.form.getWidget('label').isVisible.return_value = initial_label_visibility + + self.assertEqual(self.form.getWidgetState('label_field')['visible'], initial_label_visibility) + + final_label_visibility = False + self.form.getWidget('label').isVisible.return_value = False + + self.assertEqual(self.form.getWidgetState('label_field')['visible'], final_label_visibility) + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_getWidgetState_returns_enabled_state(self): + # Check that the enabled state of the widget is saved to the state + + initial_label_enabled_state = True + + self.assertEqual(self.form.getWidgetState('label_field')['enabled'], initial_label_enabled_state) + + self.form.getWidget('label').setEnabled(False) + final_label_enabled_state = False + + self.assertEqual(self.form.getWidgetState('label_field')['enabled'], final_label_enabled_state) + + # Test value is saved for all widget types ---------------------------------------------------------------- + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_getWidgetState_returns_QLabel_value(self): + # Check that the value of the QLabel is saved to the state + + initial_label_value ='Label: ' + + self.assertEqual(self.form.getWidgetState('label_label')['value'], initial_label_value) + + final_label_value = 'final test label' + self.form.getWidget('label', 'label').setText(final_label_value) + + self.assertEqual(self.form.getWidgetState('label_label')['value'], final_label_value) + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_getWidgetState_returns_value_using_role_parameter_field(self): + # Check that the value of the QLabel is saved to the state + + initial_label_value ='test label' + + self.assertEqual(self.form.getWidgetState('label', 'field')['value'], initial_label_value) + + final_label_value = 'final test label' + self.form.getWidget('label').setText(final_label_value) + + self.assertEqual(self.form.getWidgetState('label','field')['value'], final_label_value) + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_getWidgetState_returns_value_using_role_parameter_label(self): + # Check that the value of the QLabel is saved to the state + + initial_label_value ='test label' + + self.assertEqual(self.form.getWidgetState('label', 'field')['value'], initial_label_value) + + final_label_value = 'final test label' + self.form.getWidget('label').setText(final_label_value) + + self.assertEqual(self.form.getWidgetState('label','field')['value'], final_label_value) + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_getWidgetState_returns_value_using_default_role_parameter(self): + # Check that the value of the QLabel is saved to the state + initial_label_value ='test label' + + # In getWidgetState we do not specify if we want the 'field' or 'label' role, so it should default to 'field': + self.assertEqual(self.form.getWidgetState('label')['value'], initial_label_value) + + final_label_value = 'final test label' + self.form.getWidget('label').setText(final_label_value) + + self.assertEqual(self.form.getWidgetState('label')['value'], final_label_value) + + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_getWidgetState_returns_QCheckBox_value(self): + # Check that the value of the QCheckBox is saved to the state + + initial_checkbox_value = False + + self.assertEqual(self.form.getWidgetState('checkBox_field')['value'], initial_checkbox_value) + + final_checkbox_value = True + self.form.getWidget('checkBox').setChecked(final_checkbox_value) + + self.assertEqual(self.form.getWidgetState('checkBox_field')['value'], final_checkbox_value) + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_getWidgetState_returns_QComboBox_value(self): + # Check that the value of the QComboBox is saved to the state + + combobox_list = ['test', 'test2'] + self.form.getWidget('comboBox').addItems(combobox_list) + + initial_combobox_value = 0 + + self.assertEqual(self.form.getWidgetState('comboBox_field')['value'], initial_combobox_value) + + final_combobox_value = 1 + self.form.getWidget('comboBox').setCurrentIndex(final_combobox_value) + + self.assertEqual(self.form.getWidgetState('comboBox_field')['value'], final_combobox_value) + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_getWidgetState_returns_QDoubleSpinBox_value(self): + # Check that the value of the QDoubleSpinBox is saved to the state + + initial_doubleSpinBox_value = 0.0 + + self.assertEqual(self.form.getWidgetState('doubleSpinBox_field')['value'], initial_doubleSpinBox_value) + + final_doubleSpinBox_value = 1.0 + self.form.getWidget('doubleSpinBox').setValue(final_doubleSpinBox_value) + + self.assertEqual(self.form.getWidgetState('doubleSpinBox_field')['value'], final_doubleSpinBox_value) + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_getWidgetState_returns_QSpinBox_value(self): + # Check that the value of the QSpinBox is saved to the state + + initial_spinBox_value = 0 + + self.assertEqual(self.form.getWidgetState('spinBox_field')['value'], initial_spinBox_value) + + final_spinBox_value = 1 + self.form.getWidget('spinBox').setValue(final_spinBox_value) + + self.assertEqual(self.form.getWidgetState('spinBox_field')['value'], final_spinBox_value) + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_getWidgetState_returns_QSlider_value(self): + # Check that the value of the QSlider is saved to the state + + initial_slider_value = 0 + + self.assertEqual(self.form.getWidgetState('slider_field')['value'], initial_slider_value) + + final_slider_value = 1 + self.form.getWidget('slider').setValue(final_slider_value) + + self.assertEqual(self.form.getWidgetState('slider_field')['value'], final_slider_value) + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_getWidgetState_returns_UISliderWidget_value(self): + # Check that the value of the UISliderWidget is returned in the state + + initial_slider_value = 0 + + self.assertEqual(self.form.getWidgetState('uiSliderWidget_field')['value'], initial_slider_value) + + final_slider_value = 1 + self.form.getWidget('uiSliderWidget').setValue(final_slider_value) + + self.assertEqual(self.form.getWidgetState('uiSliderWidget_field')['value'], final_slider_value) + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_getWidgetState_returns_QLineEdit_value(self): + # Check that the value of the QLineEdit is saved to the state + + initial_lineEdit_value = '' + self.form.getWidget('lineEdit').setText(initial_lineEdit_value) + + self.assertEqual(self.form.getWidgetState('lineEdit_field')['value'], initial_lineEdit_value) + + final_lineEdit_value = 'test' + self.form.getWidget('lineEdit').setText(final_lineEdit_value) + + self.assertEqual(self.form.getWidgetState('lineEdit_field')['value'], final_lineEdit_value) + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_getWidgetState_returns_QTextEdit_value(self): + # Check that the value of the QTextEdit is saved to the state + + initial_textEdit_value = '' + self.form.getWidget('textEdit').setText(initial_textEdit_value) + + self.assertEqual(self.form.getWidgetState('textEdit_field')['value'], initial_textEdit_value) + + final_textEdit_value = 'test' + self.form.getWidget('textEdit').setText(final_textEdit_value) + + self.assertEqual(self.form.getWidgetState('textEdit_field')['value'], final_textEdit_value) + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_getWidgetState_returns_QPlainTextEdit_value(self): + # Check that the value of the QPlainTextEdit is saved to the state + + initial_plainTextEdit_value = '' + self.form.getWidget('plainTextEdit').setPlainText(initial_plainTextEdit_value) + + self.assertEqual(self.form.getWidgetState('plainTextEdit_field')['value'], initial_plainTextEdit_value) + + final_plainTextEdit_value = 'test' + self.form.getWidget('plainTextEdit').setPlainText(final_plainTextEdit_value) + + self.assertEqual(self.form.getWidgetState('plainTextEdit_field')['value'], final_plainTextEdit_value) + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_getWidgetState_returns_QPushButton_value(self): + # Check that the value of the QPushButton is saved to the state + + initial_button_value = False + + self.assertEqual(self.form.getWidgetState('button_field')['value'], initial_button_value) + + final_button_value = True + self.form.getWidget('button').setCheckable(True) + self.form.getWidget('button').setChecked(final_button_value) + + self.assertEqual(self.form.getWidgetState('button_field')['value'], final_button_value) + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_getWidgetState_returns_QRadioButton_value(self): + # Check that the value of the QRadioButton is saved to the state + + initial_radio_value = False + + self.assertEqual(self.form.getWidgetState('radioButton_field')['value'], initial_radio_value) + + final_radio_value = True + self.form.getWidget('radioButton').setChecked(final_radio_value) + + self.assertEqual(self.form.getWidgetState('radioButton_field')['value'], final_radio_value) + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_applyWidgetStates(self): + state_to_set = { + 'checkBox_field': {'value': True, 'enabled': False, 'visible': False}, + 'label_field': {'value': 'applyWidgetStates Test', 'enabled': True, 'visible': False} + } + + self.simple_form.applyWidgetStates(state_to_set) + + self.assertEqual(self.simple_form.getWidgetState('checkBox_field'), state_to_set['checkBox_field']) + self.assertEqual(self.simple_form.getWidgetState('label_field'), state_to_set['label_field']) + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_applyWidgetState(self): + state_to_set = {'value': True, 'enabled': False, 'visible': False} + self.simple_form.applyWidgetState('checkBox_field', state_to_set) + self.assertEqual(self.simple_form.getWidgetState('checkBox_field'), state_to_set) + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_applyWidgetState_using_role_parameter_field(self): + state_to_set = {'value': True, 'enabled': False, 'visible': False} + self.simple_form.applyWidgetState('checkBox', state_to_set, role='field') + self.assertEqual(self.simple_form.getWidgetState('checkBox','field'), state_to_set) + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_applyWidgetState_using_role_parameter_label(self): + state_to_set = {'value': 'test the checkbox:', 'enabled': False, 'visible': False} + self.simple_form.applyWidgetState('checkBox', state_to_set, role='label') + self.assertEqual(self.simple_form.getWidgetState('checkBox','label'), state_to_set) + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_applyWidgetState_using_role_parameter_default(self): + state_to_set = {'value': True, 'enabled': False, 'visible': False} + self.simple_form.applyWidgetState('checkBox', state_to_set) + self.assertEqual(self.simple_form.getWidgetState('checkBox'), state_to_set) + + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_getAllWidgetStates(self): + # Check that the state of all widgets is returned + + expected_state = { + 'checkBox_field': {'value': False, 'enabled': True, 'visible': False}, + 'label_field': {'value': 'test label', 'enabled': True, 'visible': False}, + 'checkBox_label': {'value': 'CheckBox: ', 'enabled': True, 'visible': False}, + 'label_label': {'value': 'Label: ', 'enabled': True, 'visible': False} + } + + self.assertEqual(self.simple_form.getAllWidgetStates(), expected_state) + + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_saveAllWidgetStates(self): + # Check that the state of all widgets is saved to the state variable + + expected_state = { + 'checkBox_field': {'value': False, 'enabled': True, 'visible': False}, + 'label_field': {'value': 'test label', 'enabled': True, 'visible': False}, + 'checkBox_label': {'value': 'CheckBox: ', 'enabled': True, 'visible': False}, + 'label_label': {'value': 'Label: ', 'enabled': True, 'visible': False} + } + + self.simple_form.saveAllWidgetStates() + + self.assertEqual(self.simple_form.formWidget.widget_states, expected_state) + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_restoreAllSavedWidgetStates(self): + # Check that the state of all widgets is restored from the state variable + + state_to_restore= { + 'checkBox_field': {'value': True, 'enabled': False, 'visible': False}, + 'label_field': {'value': 'applyWidgetStates Test', 'enabled': True, 'visible': False}, + 'checkBox_label': {'value': 'CheckBox Test: ', 'enabled': True, 'visible': False}, + 'label_label': {'value': 'Label Test: ', 'enabled': True, 'visible': False} + } + + self.simple_form.formWidget.widget_states = state_to_restore + + self.simple_form.restoreAllSavedWidgetStates() + + self.assertEqual(self.simple_form.getWidget('checkBox').isChecked(), state_to_restore['checkBox_field']['value']) + self.assertEqual(self.simple_form.getWidget('checkBox').isEnabled(), state_to_restore['checkBox_field']['enabled']) + self.assertEqual(self.simple_form.getWidget('checkBox').isVisible(), state_to_restore['checkBox_field']['visible']) + self.assertEqual(self.simple_form.getWidget('checkBox', 'label').text(), state_to_restore['checkBox_label']['value']) + self.assertEqual(self.simple_form.getWidget('checkBox', 'label').isEnabled(), state_to_restore['checkBox_label']['enabled']) + self.assertEqual(self.simple_form.getWidget('checkBox', 'label').isVisible(), state_to_restore['checkBox_label']['visible']) + self.assertEqual(self.simple_form.getWidget('label').text(), state_to_restore['label_field']['value']) + self.assertEqual(self.simple_form.getWidget('label').isEnabled(), state_to_restore['label_field']['enabled']) + self.assertEqual(self.simple_form.getWidget('label').isVisible(), state_to_restore['label_field']['visible']) + self.assertEqual(self.simple_form.getWidget('label', 'label').text(), state_to_restore['label_label']['value']) + self.assertEqual(self.simple_form.getWidget('label', 'label').isEnabled(), state_to_restore['label_label']['enabled']) + self.assertEqual(self.simple_form.getWidget('label', 'label').isVisible(), state_to_restore['label_label']['visible']) + + + +@unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") +class FormWidgetStateTest(unittest.TestCase): + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def setUp(self): + self.form = FormWidget() + add_every_widget_to_form(self.form) + self.simple_form = FormWidget() + add_two_widgets_to_form(self.simple_form) + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_getWidgetState_returns_visibility(self): + # Check that the visibility of the widget is saved to the state + # Have to use magic mock as we can't set the visibility of the QLabel + # to be True, because the FormDialog is not visible + initial_label_visibility = True + self.form.getWidget('label').isVisible = mock.MagicMock() + self.form.getWidget('label').isVisible.return_value = initial_label_visibility + + self.assertEqual(self.form.getWidgetState('label_field')['visible'], initial_label_visibility) + + final_label_visibility = False + self.form.getWidget('label').isVisible.return_value = False + + self.assertEqual(self.form.getWidgetState('label_field')['visible'], final_label_visibility) + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_getWidgetState_returns_enabled_state(self): + # Check that the enabled state of the widget is saved to the state + + initial_label_enabled_state = True + + self.assertEqual(self.form.getWidgetState('label_field')['enabled'], initial_label_enabled_state) + + self.form.getWidget('label').setEnabled(False) + final_label_enabled_state = False + + self.assertEqual(self.form.getWidgetState('label_field')['enabled'], final_label_enabled_state) + + # Test value is saved for all widget types ---------------------------------------------------------------- + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_getWidgetState_returns_value_using_role_parameter_field(self): + # Check that the value of the QLabel is saved to the state + + initial_label_value ='test label' + + self.assertEqual(self.form.getWidgetState('label', 'field')['value'], initial_label_value) + + final_label_value = 'final test label' + self.form.getWidget('label').setText(final_label_value) + + self.assertEqual(self.form.getWidgetState('label','field')['value'], final_label_value) + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_getWidgetState_returns_value_using_role_parameter_label(self): + # Check that the value of the QLabel is saved to the state + + initial_label_value ='test label' + + self.assertEqual(self.form.getWidgetState('label', 'field')['value'], initial_label_value) + + final_label_value = 'final test label' + self.form.getWidget('label').setText(final_label_value) + + self.assertEqual(self.form.getWidgetState('label','field')['value'], final_label_value) + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_getWidgetState_returns_value_using_default_role_parameter(self): + # Check that the value of the QLabel is saved to the state + initial_label_value ='test label' + + # In getWidgetState we do not specify if we want the 'field' or 'label' role, so it should default to 'field': + self.assertEqual(self.form.getWidgetState('label')['value'], initial_label_value) + + final_label_value = 'final test label' + self.form.getWidget('label').setText(final_label_value) + + self.assertEqual(self.form.getWidgetState('label')['value'], final_label_value) + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_getWidgetState_returns_QLabel_value(self): + # Check that the value of the QLabel is saved to the state + + initial_label_value ='test label' + + self.assertEqual(self.form.getWidgetState('label_field')['value'], initial_label_value) + + final_label_value = 'final test label' + self.form.getWidget('label').setText(final_label_value) + + self.assertEqual(self.form.getWidgetState('label_field')['value'], final_label_value) + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_getWidgetState_returns_QCheckBox_value(self): + # Check that the value of the QCheckBox is saved to the state + + initial_checkbox_value = False + + self.assertEqual(self.form.getWidgetState('checkBox_field')['value'], initial_checkbox_value) + + final_checkbox_value = True + self.form.getWidget('checkBox').setChecked(final_checkbox_value) + + self.assertEqual(self.form.getWidgetState('checkBox_field')['value'], final_checkbox_value) + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_getWidgetState_returns_QComboBox_value(self): + # Check that the value of the QComboBox is saved to the state + + combobox_list = ['test', 'test2'] + self.form.getWidget('comboBox').addItems(combobox_list) + + initial_combobox_value = 0 + + self.assertEqual(self.form.getWidgetState('comboBox_field')['value'], initial_combobox_value) + + final_combobox_value = 1 + self.form.getWidget('comboBox').setCurrentIndex(final_combobox_value) + + self.assertEqual(self.form.getWidgetState('comboBox_field')['value'], final_combobox_value) + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_getWidgetState_returns_QDoubleSpinBox_value(self): + # Check that the value of the QDoubleSpinBox is saved to the state + + initial_doubleSpinBox_value = 0.0 + + self.assertEqual(self.form.getWidgetState('doubleSpinBox_field')['value'], initial_doubleSpinBox_value) + + final_doubleSpinBox_value = 1.0 + self.form.getWidget('doubleSpinBox').setValue(final_doubleSpinBox_value) + + self.assertEqual(self.form.getWidgetState('doubleSpinBox_field')['value'], final_doubleSpinBox_value) + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_getWidgetState_returns_QSpinBox_value(self): + # Check that the value of the QSpinBox is saved to the state + + initial_spinBox_value = 0 + + self.assertEqual(self.form.getWidgetState('spinBox_field')['value'], initial_spinBox_value) + + final_spinBox_value = 1 + self.form.getWidget('spinBox').setValue(final_spinBox_value) + + self.assertEqual(self.form.getWidgetState('spinBox_field')['value'], final_spinBox_value) + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_getWidgetState_returns_QSlider_value(self): + # Check that the value of the QSlider is saved to the state + + initial_slider_value = 0 + + self.assertEqual(self.form.getWidgetState('slider_field')['value'], initial_slider_value) + + final_slider_value = 1 + self.form.getWidget('slider').setValue(final_slider_value) + + self.assertEqual(self.form.getWidgetState('slider_field')['value'], final_slider_value) + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_getWidgetState_returns_UISliderWidget_value(self): + # Check that the value of the UISliderWidget is returned in the state + + initial_slider_value = 0 + + self.assertEqual(self.form.getWidgetState('uiSliderWidget_field')['value'], initial_slider_value) + + final_slider_value = 1 + self.form.getWidget('uiSliderWidget').setValue(final_slider_value) + + self.assertEqual(self.form.getWidgetState('uiSliderWidget_field')['value'], final_slider_value) + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_getWidgetState_returns_QLineEdit_value(self): + # Check that the value of the QLineEdit is saved to the state + + initial_lineEdit_value = '' + self.form.getWidget('lineEdit').setText(initial_lineEdit_value) + + self.assertEqual(self.form.getWidgetState('lineEdit_field')['value'], initial_lineEdit_value) + + final_lineEdit_value = 'test' + self.form.getWidget('lineEdit').setText(final_lineEdit_value) + + self.assertEqual(self.form.getWidgetState('lineEdit_field')['value'], final_lineEdit_value) + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_getWidgetState_returns_QTextEdit_value(self): + # Check that the value of the QTextEdit is saved to the state + + initial_textEdit_value = '' + self.form.getWidget('textEdit').setText(initial_textEdit_value) + + self.assertEqual(self.form.getWidgetState('textEdit_field')['value'], initial_textEdit_value) + + final_textEdit_value = 'test' + self.form.getWidget('textEdit').setText(final_textEdit_value) + + self.assertEqual(self.form.getWidgetState('textEdit_field')['value'], final_textEdit_value) + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_getWidgetState_returns_QPlainTextEdit_value(self): + # Check that the value of the QPlainTextEdit is saved to the state + + initial_plainTextEdit_value = '' + self.form.getWidget('plainTextEdit').setPlainText(initial_plainTextEdit_value) + + self.assertEqual(self.form.getWidgetState('plainTextEdit_field')['value'], initial_plainTextEdit_value) + + final_plainTextEdit_value = 'test' + self.form.getWidget('plainTextEdit').setPlainText(final_plainTextEdit_value) + + self.assertEqual(self.form.getWidgetState('plainTextEdit_field')['value'], final_plainTextEdit_value) + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_getWidgetState_returns_QPushButton_value(self): + # Check that the value of the QPushButton is saved to the state + + initial_button_value = False + + self.assertEqual(self.form.getWidgetState('button_field')['value'], initial_button_value) + + final_button_value = True + self.form.getWidget('button').setCheckable(True) + self.form.getWidget('button').setChecked(final_button_value) + + self.assertEqual(self.form.getWidgetState('button_field')['value'], final_button_value) + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_getWidgetState_returns_QRadioButton_value(self): + # Check that the value of the QRadioButton is saved to the state + + initial_radio_value = False + + self.assertEqual(self.form.getWidgetState('radioButton_field')['value'], initial_radio_value) + + final_radio_value = True + self.form.getWidget('radioButton').setChecked(final_radio_value) + + self.assertEqual(self.form.getWidgetState('radioButton_field')['value'], final_radio_value) + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_applyWidgetStates(self): + state_to_set = { + 'checkBox_field': {'value': True, 'enabled': False, 'visible': False}, + 'label_field': {'value': 'applyWidgetStates Test', 'enabled': True, 'visible': False} + } + + self.simple_form.applyWidgetStates(state_to_set) + + self.assertEqual(self.simple_form.getWidgetState('checkBox_field'), state_to_set['checkBox_field']) + self.assertEqual(self.simple_form.getWidgetState('label_field'), state_to_set['label_field']) + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_applyWidgetState(self): + state_to_set = {'value': True, 'enabled': False, 'visible': False} + self.simple_form.applyWidgetState('checkBox_field', state_to_set) + self.assertEqual(self.simple_form.getWidgetState('checkBox_field'), state_to_set) + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_applyWidgetState_using_role_parameter_field(self): + state_to_set = {'value': True, 'enabled': False, 'visible': False} + self.simple_form.applyWidgetState('checkBox', state_to_set, role='field') + self.assertEqual(self.simple_form.getWidgetState('checkBox','field'), state_to_set) + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_applyWidgetState_using_role_parameter_label(self): + state_to_set = {'value': 'test the checkbox:', 'enabled': False, 'visible': False} + self.simple_form.applyWidgetState('checkBox', state_to_set, role='label') + self.assertEqual(self.simple_form.getWidgetState('checkBox','label'), state_to_set) + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_applyWidgetState_using_role_parameter_default(self): + state_to_set = {'value': True, 'enabled': False, 'visible': False} + self.simple_form.applyWidgetState('checkBox', state_to_set) + self.assertEqual(self.simple_form.getWidgetState('checkBox'), state_to_set) + + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_getAllWidgetStates(self): + # Check that the state of all widgets is returned + + expected_state = { + 'checkBox_field': {'value': False, 'enabled': True, 'visible': False}, + 'label_field': {'value': 'test label', 'enabled': True, 'visible': False}, + 'checkBox_label': {'value': 'CheckBox: ', 'enabled': True, 'visible': False}, + 'label_label': {'value': 'Label: ', 'enabled': True, 'visible': False} + } + + self.assertEqual(self.simple_form.getAllWidgetStates(), expected_state) + + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_saveAllWidgetStates(self): + # Check that the state of all widgets is saved to the state variable + + expected_state = { + 'checkBox_field': {'value': False, 'enabled': True, 'visible': False}, + 'label_field': {'value': 'test label', 'enabled': True, 'visible': False}, + 'checkBox_label': {'value': 'CheckBox: ', 'enabled': True, 'visible': False}, + 'label_label': {'value': 'Label: ', 'enabled': True, 'visible': False} + } + + self.simple_form.saveAllWidgetStates() + + self.assertEqual(self.simple_form.widget_states, expected_state) + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_restoreAllSavedWidgetStates(self): + # Check that the state of all widgets is restored from the state variable + + state_to_restore= { + 'checkBox_field': {'value': True, 'enabled': False, 'visible': False}, + 'label_field': {'value': 'applyWidgetStates Test', 'enabled': True, 'visible': False}, + 'checkBox_label': {'value': 'CheckBox Test: ', 'enabled': True, 'visible': False}, + 'label_label': {'value': 'Label Test: ', 'enabled': True, 'visible': False} + } + + self.simple_form.widget_states = state_to_restore + + self.simple_form.restoreAllSavedWidgetStates() + + self.assertEqual(self.simple_form.getWidget('checkBox').isChecked(), state_to_restore['checkBox_field']['value']) + self.assertEqual(self.simple_form.getWidget('checkBox').isEnabled(), state_to_restore['checkBox_field']['enabled']) + self.assertEqual(self.simple_form.getWidget('checkBox').isVisible(), state_to_restore['checkBox_field']['visible']) + self.assertEqual(self.simple_form.getWidget('checkBox', 'label').text(), state_to_restore['checkBox_label']['value']) + self.assertEqual(self.simple_form.getWidget('checkBox', 'label').isEnabled(), state_to_restore['checkBox_label']['enabled']) + self.assertEqual(self.simple_form.getWidget('checkBox', 'label').isVisible(), state_to_restore['checkBox_label']['visible']) + self.assertEqual(self.simple_form.getWidget('label').text(), state_to_restore['label_field']['value']) + self.assertEqual(self.simple_form.getWidget('label').isEnabled(), state_to_restore['label_field']['enabled']) + self.assertEqual(self.simple_form.getWidget('label').isVisible(), state_to_restore['label_field']['visible']) + self.assertEqual(self.simple_form.getWidget('label', 'label').text(), state_to_restore['label_label']['value']) + self.assertEqual(self.simple_form.getWidget('label', 'label').isEnabled(), state_to_restore['label_label']['enabled']) + self.assertEqual(self.simple_form.getWidget('label', 'label').isVisible(), state_to_restore['label_label']['visible']) + + +@unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") +class FormDockWidgetStateTest(unittest.TestCase): + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def setUp(self): + self.form = FormDockWidget() + add_every_widget_to_form(self.form) + self.simple_form = FormDockWidget() + add_two_widgets_to_form(self.simple_form) + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_getWidgetState_returns_visibility(self): + # Check that the visibility of the widget is saved to the state + # Have to use magic mock as we can't set the visibility of the QLabel + # to be True, because the FormDialog is not visible + initial_label_visibility = True + self.form.getWidget('label').isVisible = mock.MagicMock() + self.form.getWidget('label').isVisible.return_value = initial_label_visibility + + self.assertEqual(self.form.getWidgetState('label_field')['visible'], initial_label_visibility) + + final_label_visibility = False + self.form.getWidget('label').isVisible.return_value = False + + self.assertEqual(self.form.getWidgetState('label_field')['visible'], final_label_visibility) + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_getWidgetState_returns_enabled_state(self): + # Check that the enabled state of the widget is saved to the state + + initial_label_enabled_state = True + + self.assertEqual(self.form.getWidgetState('label_field')['enabled'], initial_label_enabled_state) + + self.form.getWidget('label').setEnabled(False) + final_label_enabled_state = False + + self.assertEqual(self.form.getWidgetState('label_field')['enabled'], final_label_enabled_state) + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_getWidgetState_returns_value_using_role_parameter_field(self): + # Check that the value of the QLabel is saved to the state + + initial_label_value ='test label' + + self.assertEqual(self.form.getWidgetState('label', 'field')['value'], initial_label_value) + + final_label_value = 'final test label' + self.form.getWidget('label').setText(final_label_value) + + self.assertEqual(self.form.getWidgetState('label','field')['value'], final_label_value) + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_getWidgetState_returns_value_using_role_parameter_label(self): + # Check that the value of the QLabel is saved to the state + + initial_label_value ='test label' + + self.assertEqual(self.form.getWidgetState('label', 'field')['value'], initial_label_value) + + final_label_value = 'final test label' + self.form.getWidget('label').setText(final_label_value) + + self.assertEqual(self.form.getWidgetState('label','field')['value'], final_label_value) + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_getWidgetState_returns_value_using_default_role_parameter(self): + # Check that the value of the QLabel is saved to the state + initial_label_value ='test label' + + # In getWidgetState we do not specify if we want the 'field' or 'label' role, so it should default to 'field': + self.assertEqual(self.form.getWidgetState('label')['value'], initial_label_value) + + final_label_value = 'final test label' + self.form.getWidget('label').setText(final_label_value) + + self.assertEqual(self.form.getWidgetState('label')['value'], final_label_value) + + # Test value is saved for all widget types ---------------------------------------------------------------- + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_getWidgetState_returns_QLabel_value(self): + # Check that the value of the QLabel is saved to the state + + initial_label_value ='test label' + + self.assertEqual(self.form.getWidgetState('label_field')['value'], initial_label_value) + + final_label_value = 'final test label' + self.form.getWidget('label').setText(final_label_value) + + self.assertEqual(self.form.getWidgetState('label_field')['value'], final_label_value) + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_getWidgetState_returns_QCheckBox_value(self): + # Check that the value of the QCheckBox is saved to the state + + initial_checkbox_value = False + + self.assertEqual(self.form.getWidgetState('checkBox_field')['value'], initial_checkbox_value) + + final_checkbox_value = True + self.form.getWidget('checkBox').setChecked(final_checkbox_value) + + self.assertEqual(self.form.getWidgetState('checkBox_field')['value'], final_checkbox_value) + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_getWidgetState_returns_QComboBox_value(self): + # Check that the value of the QComboBox is saved to the state + + combobox_list = ['test', 'test2'] + self.form.getWidget('comboBox').addItems(combobox_list) + + initial_combobox_value = 0 + + self.assertEqual(self.form.getWidgetState('comboBox_field')['value'], initial_combobox_value) + + final_combobox_value = 1 + self.form.getWidget('comboBox').setCurrentIndex(final_combobox_value) + + self.assertEqual(self.form.getWidgetState('comboBox_field')['value'], final_combobox_value) + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_getWidgetState_returns_QDoubleSpinBox_value(self): + # Check that the value of the QDoubleSpinBox is saved to the state + + initial_doubleSpinBox_value = 0.0 + + self.assertEqual(self.form.getWidgetState('doubleSpinBox_field')['value'], initial_doubleSpinBox_value) + + final_doubleSpinBox_value = 1.0 + self.form.getWidget('doubleSpinBox').setValue(final_doubleSpinBox_value) + + self.assertEqual(self.form.getWidgetState('doubleSpinBox_field')['value'], final_doubleSpinBox_value) + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_getWidgetState_returns_QSpinBox_value(self): + # Check that the value of the QSpinBox is saved to the state + + initial_spinBox_value = 0 + + self.assertEqual(self.form.getWidgetState('spinBox_field')['value'], initial_spinBox_value) + + final_spinBox_value = 1 + self.form.getWidget('spinBox').setValue(final_spinBox_value) + + self.assertEqual(self.form.getWidgetState('spinBox_field')['value'], final_spinBox_value) + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_getWidgetState_returns_QSlider_value(self): + # Check that the value of the QSlider is saved to the state + + initial_slider_value = 0 + + self.assertEqual(self.form.getWidgetState('slider_field')['value'], initial_slider_value) + + final_slider_value = 1 + self.form.getWidget('slider').setValue(final_slider_value) + + self.assertEqual(self.form.getWidgetState('slider_field')['value'], final_slider_value) + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_getWidgetState_returns_UISliderWidget_value(self): + # Check that the value of the UISliderWidget is returned in the state + + initial_slider_value = 0 + + self.assertEqual(self.form.getWidgetState('uiSliderWidget_field')['value'], initial_slider_value) + + final_slider_value = 1 + self.form.getWidget('uiSliderWidget').setValue(final_slider_value) + + self.assertEqual(self.form.getWidgetState('uiSliderWidget_field')['value'], final_slider_value) + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_getWidgetState_returns_QLineEdit_value(self): + # Check that the value of the QLineEdit is saved to the state + + initial_lineEdit_value = '' + self.form.getWidget('lineEdit').setText(initial_lineEdit_value) + + self.assertEqual(self.form.getWidgetState('lineEdit_field')['value'], initial_lineEdit_value) + + final_lineEdit_value = 'test' + self.form.getWidget('lineEdit').setText(final_lineEdit_value) + + self.assertEqual(self.form.getWidgetState('lineEdit_field')['value'], final_lineEdit_value) + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_getWidgetState_returns_QTextEdit_value(self): + # Check that the value of the QTextEdit is saved to the state + + initial_textEdit_value = '' + self.form.getWidget('textEdit').setText(initial_textEdit_value) + + self.assertEqual(self.form.getWidgetState('textEdit_field')['value'], initial_textEdit_value) + + final_textEdit_value = 'test' + self.form.getWidget('textEdit').setText(final_textEdit_value) + + self.assertEqual(self.form.getWidgetState('textEdit_field')['value'], final_textEdit_value) + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_getWidgetState_returns_QPlainTextEdit_value(self): + # Check that the value of the QPlainTextEdit is saved to the state + + initial_plainTextEdit_value = '' + self.form.getWidget('plainTextEdit').setPlainText(initial_plainTextEdit_value) + + self.assertEqual(self.form.getWidgetState('plainTextEdit_field')['value'], initial_plainTextEdit_value) + + final_plainTextEdit_value = 'test' + self.form.getWidget('plainTextEdit').setPlainText(final_plainTextEdit_value) + + self.assertEqual(self.form.getWidgetState('plainTextEdit_field')['value'], final_plainTextEdit_value) + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_getWidgetState_returns_QPushButton_value(self): + # Check that the value of the QPushButton is saved to the state + + initial_button_value = False + + self.assertEqual(self.form.getWidgetState('button_field')['value'], initial_button_value) + + final_button_value = True + self.form.getWidget('button').setCheckable(True) + self.form.getWidget('button').setChecked(final_button_value) + + self.assertEqual(self.form.getWidgetState('button_field')['value'], final_button_value) + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_getWidgetState_returns_QRadioButton_value(self): + # Check that the value of the QRadioButton is saved to the state + + initial_radio_value = False + + self.assertEqual(self.form.getWidgetState('radioButton_field')['value'], initial_radio_value) + + final_radio_value = True + self.form.getWidget('radioButton').setChecked(final_radio_value) + + self.assertEqual(self.form.getWidgetState('radioButton_field')['value'], final_radio_value) + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_applyWidgetStates(self): + state_to_set = { + 'checkBox_field': {'value': True, 'enabled': False, 'visible': False}, + 'label_field': {'value': 'applyWidgetStates Test', 'enabled': True, 'visible': False} + } + + self.simple_form.applyWidgetStates(state_to_set) + + self.assertEqual(self.simple_form.getWidgetState('checkBox_field'), state_to_set['checkBox_field']) + self.assertEqual(self.simple_form.getWidgetState('label_field'), state_to_set['label_field']) + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_applyWidgetState(self): + state_to_set = {'value': True, 'enabled': False, 'visible': False} + self.simple_form.applyWidgetState('checkBox_field', state_to_set) + self.assertEqual(self.simple_form.getWidgetState('checkBox_field'), state_to_set) + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_applyWidgetState_using_role_parameter_field(self): + state_to_set = {'value': True, 'enabled': False, 'visible': False} + self.simple_form.applyWidgetState('checkBox', state_to_set, role='field') + self.assertEqual(self.simple_form.getWidgetState('checkBox','field'), state_to_set) + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_applyWidgetState_using_role_parameter_label(self): + state_to_set = {'value': 'test the checkbox:', 'enabled': False, 'visible': False} + self.simple_form.applyWidgetState('checkBox', state_to_set, role='label') + self.assertEqual(self.simple_form.getWidgetState('checkBox','label'), state_to_set) + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_applyWidgetState_using_role_parameter_default(self): + state_to_set = {'value': True, 'enabled': False, 'visible': False} + self.simple_form.applyWidgetState('checkBox', state_to_set) + self.assertEqual(self.simple_form.getWidgetState('checkBox'), state_to_set) + + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_getAllWidgetStates(self): + # Check that the state of all widgets is returned + + expected_state = { + 'checkBox_field': {'value': False, 'enabled': True, 'visible': False}, + 'label_field': {'value': 'test label', 'enabled': True, 'visible': False}, + 'checkBox_label': {'value': 'CheckBox: ', 'enabled': True, 'visible': False}, + 'label_label': {'value': 'Label: ', 'enabled': True, 'visible': False} + } + + self.assertEqual(self.simple_form.getAllWidgetStates(), expected_state) + + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_saveAllWidgetStates(self): + # Check that the state of all widgets is saved to the state variable + + expected_state = { + 'checkBox_field': {'value': False, 'enabled': True, 'visible': False}, + 'label_field': {'value': 'test label', 'enabled': True, 'visible': False}, + 'checkBox_label': {'value': 'CheckBox: ', 'enabled': True, 'visible': False}, + 'label_label': {'value': 'Label: ', 'enabled': True, 'visible': False} + } + + self.simple_form.saveAllWidgetStates() + + self.assertEqual(self.simple_form.widget().widget_states, expected_state) + + @unittest.skipIf(skip_test, "Can't test interfaces if we can't connect to the display") + def test_restoreAllSavedWidgetStates(self): + # Check that the state of all widgets is restored from the state variable + + state_to_restore= { + 'checkBox_field': {'value': True, 'enabled': False, 'visible': False}, + 'label_field': {'value': 'applyWidgetStates Test', 'enabled': True, 'visible': False}, + 'checkBox_label': {'value': 'CheckBox Test: ', 'enabled': True, 'visible': False}, + 'label_label': {'value': 'Label Test: ', 'enabled': True, 'visible': False} + } + + self.simple_form.widget().widget_states = state_to_restore + + self.simple_form.restoreAllSavedWidgetStates() + + self.assertEqual(self.simple_form.getWidget('checkBox').isChecked(), state_to_restore['checkBox_field']['value']) + self.assertEqual(self.simple_form.getWidget('checkBox').isEnabled(), state_to_restore['checkBox_field']['enabled']) + self.assertEqual(self.simple_form.getWidget('checkBox').isVisible(), state_to_restore['checkBox_field']['visible']) + self.assertEqual(self.simple_form.getWidget('checkBox', 'label').text(), state_to_restore['checkBox_label']['value']) + self.assertEqual(self.simple_form.getWidget('checkBox', 'label').isEnabled(), state_to_restore['checkBox_label']['enabled']) + self.assertEqual(self.simple_form.getWidget('checkBox', 'label').isVisible(), state_to_restore['checkBox_label']['visible']) + self.assertEqual(self.simple_form.getWidget('label').text(), state_to_restore['label_field']['value']) + self.assertEqual(self.simple_form.getWidget('label').isEnabled(), state_to_restore['label_field']['enabled']) + self.assertEqual(self.simple_form.getWidget('label').isVisible(), state_to_restore['label_field']['visible']) + self.assertEqual(self.simple_form.getWidget('label', 'label').text(), state_to_restore['label_label']['value']) + self.assertEqual(self.simple_form.getWidget('label', 'label').isEnabled(), state_to_restore['label_label']['enabled']) + self.assertEqual(self.simple_form.getWidget('label', 'label').isVisible(), state_to_restore['label_label']['visible']) + + +if __name__ == '__main__': + unittest.main() +