Skip to content

Commit

Permalink
Add methods to UIFormWidget for saving and restoring widget states (#64)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* Update eqt/ui/UIFormWidget.py

Co-authored-by: Edoardo Pasca <[email protected]>

* Apply suggestions from code review

Co-authored-by: Edoardo Pasca <[email protected]>

* Apply suggestions from code review

Co-authored-by: Edoardo Pasca <[email protected]>

* update docstrings re: code review

* further docstring update

Co-authored-by: Edoardo Pasca <[email protected]>
  • Loading branch information
lauramurgatroyd and paskino authored Jan 19, 2023
1 parent 3efa58c commit 87f6fe7
Show file tree
Hide file tree
Showing 8 changed files with 1,539 additions and 4 deletions.
20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion conda/bld.bat
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

cd %RECIPE_DIR%\..

%PYTHON% setup.py install
pip install .
2 changes: 1 addition & 1 deletion conda/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@

#$PYTHON -m pip install eqt --no-deps
cd $RECIPE_DIR/..
$PYTHON setup.py install
pip install .
91 changes: 91 additions & 0 deletions eqt/ui/FormDialog.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Author: Edoardo Pasca, Laura Murgatroyd

from PySide2 import QtCore, QtGui, QtWidgets
from eqt.ui import UIFormFactory

Expand Down Expand Up @@ -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)

Loading

0 comments on commit 87f6fe7

Please sign in to comment.