Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR Insert widget #109

Merged
merged 105 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from 92 commits
Commits
Show all changes
105 commits
Select commit Hold shift + click to select a range
906dd76
Add functionality of default saving in a new example
DanicaSTFC Oct 19, 2023
c143250
Save states and restore states as a default in FormDialog
DanicaSTFC Oct 20, 2023
071aa0c
Add all widgets to example 3 and set state method
DanicaSTFC Oct 20, 2023
9473391
Add unit test for state changes in form dialog --- not finished
DanicaSTFC Oct 20, 2023
2e05d97
Unit tests working
DanicaSTFC Oct 26, 2023
0538ef5
Modify and tidy example
DanicaSTFC Oct 26, 2023
46231b5
Add minor changes
DanicaSTFC Oct 26, 2023
d2cfb9a
Add changes from pre-commit
DanicaSTFC Oct 26, 2023
ffe74e4
Fix first cancel call bug
DanicaSTFC Oct 30, 2023
00dbb7a
Save default values one by one in `_addWidget`
DanicaSTFC Oct 30, 2023
6be5a9c
Close dialog when Ok is clicked and add example file for methods
DanicaSTFC Oct 31, 2023
4c5ca41
Add example state to tests, add docstring to set_state
DanicaSTFC Oct 31, 2023
540f27e
ii changed to i in test set_state
DanicaSTFC Nov 17, 2023
006e4a9
Improve applyDefaultWidgetValuesToState with docstring and new name
DanicaSTFC Nov 17, 2023
54235ec
Change docstring to restoreAllSavedWidgetStates
DanicaSTFC Nov 17, 2023
424d6b4
Pre-commit run
DanicaSTFC Nov 17, 2023
78ec108
add insert widget working with add row and extra example
DanicaSTFC Nov 20, 2023
92d1332
Fix issues with unit tests
lauramurgatroyd Nov 21, 2023
94f172e
Fix issues with session label
lauramurgatroyd Nov 21, 2023
d6d2cc6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 21, 2023
73f419c
Fix formatting
lauramurgatroyd Nov 21, 2023
ccdf0fa
Remove unused imports
lauramurgatroyd Nov 21, 2023
05ff037
remove prints
lauramurgatroyd Nov 21, 2023
ff08cf4
Working example and improved insert methods
DanicaSTFC Nov 21, 2023
a3b5c40
Add unit test
DanicaSTFC Nov 21, 2023
40d4350
Improve unit test
DanicaSTFC Nov 22, 2023
ca5d09b
Improve docstring etc
DanicaSTFC Nov 22, 2023
d7de5dd
run pre-commit
DanicaSTFC Nov 22, 2023
847a7aa
delete temporary example for testing
DanicaSTFC Nov 22, 2023
431a29a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 22, 2023
96abaee
Changed pre-commit suggestions
DanicaSTFC Nov 22, 2023
d3bf171
Add default widget state as a larger dictionary
DanicaSTFC Nov 23, 2023
90ea8e8
Polish test_dialog_buttons_default_behaviour
DanicaSTFC Nov 23, 2023
1ecc16a
pre-commit run
DanicaSTFC Nov 23, 2023
69701cd
improve docstring and other minor things
DanicaSTFC Nov 23, 2023
1afac70
merge origin main (remove widgets)
DanicaSTFC Nov 23, 2023
6643167
Attempt to make remove widget and save default states compatible, not…
DanicaSTFC Nov 24, 2023
970be18
Polish code and run pre-commit
DanicaSTFC Dec 4, 2023
53104e8
Merge branch 'main' of github.com:TomographicImaging/eqt
DanicaSTFC Dec 4, 2023
e99a648
Merge fix_unit_tests
DanicaSTFC Dec 4, 2023
f9a342b
Change method name and remove qlabel from default state dictionary
DanicaSTFC Dec 4, 2023
04723ca
merge save-entries-by-default branch
DanicaSTFC Dec 5, 2023
e9c6649
Add unit test for insertWidgetToVerticalLayout
DanicaSTFC Dec 5, 2023
9660836
Change dictionaries
DanicaSTFC Dec 5, 2023
ed5904a
Add dictionary for widget number
DanicaSTFC Dec 5, 2023
bdcab4c
Widget number dictionary works well
DanicaSTFC Dec 5, 2023
ce92bf7
Modify various methods so remove widgets works
DanicaSTFC Dec 6, 2023
0239649
modify unit test to meet new widget number
DanicaSTFC Dec 6, 2023
087eed8
Some polishing of the code
DanicaSTFC Dec 7, 2023
e6d7b1e
Merge branch 'main' of github.com:TomographicImaging/eqt
DanicaSTFC Dec 7, 2023
d4e04ac
Improve docstrings and namings
DanicaSTFC Dec 7, 2023
13446af
Improve formatting
DanicaSTFC Dec 7, 2023
313aeac
Merge main
DanicaSTFC Dec 8, 2023
0fbf7ca
Minor change
DanicaSTFC Dec 8, 2023
e031583
Add widget number to label state
DanicaSTFC Jan 9, 2024
56e226b
Num_widgets as a property
DanicaSTFC Jan 9, 2024
fc2fba7
Add reviewers changes
DanicaSTFC Jan 9, 2024
7a30b42
Fix save state example
DanicaSTFC Jan 9, 2024
93dc36e
Copy docstring 3 times
DanicaSTFC Jan 9, 2024
1c183b5
Modify example insert widgets and fix problems with default states
DanicaSTFC Jan 10, 2024
3b7cd9a
Add remove widget and get widget from vertical layout in form dialog
DanicaSTFC Jan 10, 2024
8b34ce7
Add unit tests for some methods
DanicaSTFC Jan 11, 2024
457b6aa
temporary commit
DanicaSTFC Jan 11, 2024
42f6141
correct qlabel widget in applywidgetstates
DanicaSTFC Jan 11, 2024
75db82b
Add test for apply widgets remove and insert
DanicaSTFC Jan 12, 2024
c93c7d3
Add test for apply widgets remove and insert
DanicaSTFC Jan 12, 2024
37156a7
Delete extra example
DanicaSTFC Jan 12, 2024
548358f
merge head
DanicaSTFC Jan 18, 2024
a098fc9
Change applywidgetstates and add adaptFormToStates
DanicaSTFC Jan 18, 2024
918fa26
Change example insert widget with adaptFormToStates
DanicaSTFC Jan 18, 2024
7a9776e
Add advanced dialog to form dialog and its example file
DanicaSTFC Jan 19, 2024
cd7559b
Add button argument to advanced dialog
DanicaSTFC Jan 19, 2024
b63bf36
Improve docstrings and other minor things
DanicaSTFC Jan 22, 2024
6c5e408
Change pop widget number dictionary dictionary arguments
DanicaSTFC Jan 22, 2024
11caf76
Fixed insert widget example
DanicaSTFC Jan 22, 2024
8bd14a5
Delete _addwidget and other changes from 2nd review
DanicaSTFC Jan 22, 2024
d77ba26
Run pre-commit
DanicaSTFC Jan 22, 2024
2ad7405
Change docstring in insert widget
DanicaSTFC Jan 22, 2024
58b4fa3
Merge branch 'main' of github.com:TomographicImaging/eqt
DanicaSTFC Jan 23, 2024
9ebe624
Change docstring remove widget
DanicaSTFC Jan 25, 2024
bfb31f5
Change error message
DanicaSTFC Jan 25, 2024
00d751a
Change insert widget docstring
DanicaSTFC Jan 25, 2024
bdecb27
Change test list
DanicaSTFC Jan 25, 2024
c2dc3c6
Merge branch 'main' into insert-widget-branch
DanicaSTFC Jan 25, 2024
86659da
Update changelog
DanicaSTFC Jan 25, 2024
d6e556a
Delete removed widget dict and widget number dict and change getWidge…
DanicaSTFC Jan 30, 2024
3199989
Change docstring
DanicaSTFC Jan 30, 2024
28a82f6
Change docstring
DanicaSTFC Jan 30, 2024
07dd2c1
Update changelog
DanicaSTFC Jan 30, 2024
1c93d2c
fixup docstrings
casperdcl Jan 31, 2024
77b56a7
misc tidy
casperdcl Jan 31, 2024
7f490fe
more whitespace fixes
casperdcl Jan 31, 2024
325d45e
Change docstring
DanicaSTFC Feb 1, 2024
b19c095
Fix tests
DanicaSTFC Feb 1, 2024
1f4bd80
Change docstring
DanicaSTFC Feb 1, 2024
3c320d2
Merge the removed methods and change example. Change widget number to…
DanicaSTFC Feb 1, 2024
88c8889
run pre-commit
DanicaSTFC Feb 1, 2024
2246569
commit string
DanicaSTFC Feb 1, 2024
99de23e
Update changelog
DanicaSTFC Feb 1, 2024
e581079
Add tests for remove widgets from vertical layout
DanicaSTFC Feb 1, 2024
4825df8
Change names to remove widget and insert widget
DanicaSTFC Feb 5, 2024
6609812
Change getNameAndRoleFromWidget error
DanicaSTFC Feb 14, 2024
e6e6876
run pre-commit
DanicaSTFC Feb 14, 2024
61c5560
run pre-commit
DanicaSTFC Feb 14, 2024
e7d039c
fix flake8
casperdcl Feb 14, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
# Version x.x.x
- Adds methods to insert widgets in the forms & tests/example
Removes `_addWidget`
Adds `getWidgetNumber`and updates states dictionary and related methods
Adds `getNameAndRole*`.
Changes `num_widgets` to be a property (#109)
- Reinstates changelog (#99)
- Adds `title` to `FormDockWidget` & update tests/examples (#102)
- Stops `pre-commit` committing corrections to user PRs (#112)
Expand Down
202 changes: 141 additions & 61 deletions eqt/ui/FormDialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,30 @@ def __init__(self, parent=None, title=None):

@property
def Ok(self):
'''returns a reference to the Dialog Ok button to connect its signals'''
'''Returns a reference to the Dialog Ok button to connect its signals.'''
return self.buttonBox.button(QtWidgets.QDialogButtonBox.Ok)

@property
def Cancel(self):
'''returns a reference to the Dialog Cancel button to connect its signals'''
'''Returns a reference to the Dialog Cancel button to connect its signals.'''
return self.buttonBox.button(QtWidgets.QDialogButtonBox.Cancel)

def _onOk(self):
'''saves the widget states and calls `onOk`'''
'''Saves the widget states and calls `onOk`.'''
self.saveAllWidgetStates()
self.onOk()
self.close()

def _onCancel(self):
'''calls `onCancel`, closes the FormDialog and restores the previously saved states
'''Calls `onCancel`, closes the FormDialog and restores the previously saved states
or the default states.'''
self.onCancel()
self.close()
self.restoreAllSavedWidgetStates()

def onOk(self):
'''Called when the dialog's "Ok" button is clicked.
Can be redefined to add additional functionality on "Ok"'''
Can be redefined to add additional functionality on "Ok".'''
pass

def onCancel(self):
Expand All @@ -69,12 +69,21 @@ def groupBox(self):

def addWidget(self, qwidget, qlabel=None, name=None, layout='form'):
'''
Adds a widget to the layout.
layout = 'form' - adds to the FormLayout
layout = 'vertical' - adds to the Vertical layout below the form.
To add to the form layout, qlabel and name must be passed.
'''
Adds a widget to the layout. In particular, adds a qwidget and a qlabel widget
in the same row of the form layout if layout is 'form' and adds a spanning widget
to the vertical layout if layout is 'vertical'.

Parameters
----------
qwidget: widget
qlabel: qlabel widget or str
Only supported for layout='form'.
name: str
Only supported for layout='form'.
layout: 'form' or 'vertical'
'form' adds to the `FormLayout`, 'vertical' adds to the `VerticalLayout` below
the form.
'''
if layout == 'vertical':
if name is not None or qlabel is not None:
raise ValueError('`qlabel` and `name` are unsupported when `layout=vertical`')
Expand All @@ -88,20 +97,17 @@ def addWidget(self, qwidget, qlabel=None, name=None, layout='form'):
else:
raise ValueError(f"layout '{layout}' unrecognised: expected 'form' or 'vertical'")

def removeWidget(self, name):
'''
Removes a widget (and its label if present) from the layout.
Decreases the counter for the number of widgets in the layout.
Deletes the field (and label) from the dictionary.
'''
self.formWidget.removeWidget(name)

def addSpanningWidget(self, qwidget, name=None, layout='form'):
'''
Adds a spanning widget to the layout.
layout = 'form' - adds the widget to the FormLayout
layout = 'vertical' - adds the widget to the Vertical layout below the form.
To add to the form layout, name must be passed.
Adds a widget spanning the full row in the layout.

Parameters
----------
name: str
Required for `layout='form'`.
layout: 'form' or 'vertical'
'form': adds the widget to the `FormLayout` (requires `name`).
'vertical': adds the widget to the `VerticalLayout` below the form.
'''
if layout == 'vertical':
if name is not None:
Expand All @@ -115,49 +121,115 @@ def addSpanningWidget(self, qwidget, name=None, layout='form'):
raise ValueError(
f"layout {layout} is not recognised, must be set to 'form' or 'vertical'")

def insertWidgetToFormLayout(self, row, name, qwidget, qlabel=None):
'''
Inserts a labelled widget, or a spanning widget, to the form layout.
The position in the form is specified by row. If row is out of bounds, the widget
is added at the end of the form. An error is raised if `name` is already in use
by another widget in the form. The entries associated with the widget are added
to the widget dictionary and the default-widget-states
dictionary.

Parameters
----------
row: int
The position in the form where the widget is added.
name: str
The string associated to the qwidget and qlabel.
qwidget: widget
The widget to be added on the right hand side of the form or as a spanning widget.
qlabel: qlabel widget or str
The qlabel widget, or a str from which a qlabel widget is created, to be added
on the left hand side of the form. If qlabel is `None` the widget spans the full
width of the form.
'''
self.formWidget.insertWidgetToFormLayout(row, name, qwidget, qlabel)

def insertWidgetToVerticalLayout(self, row, qwidget):
'''
Inserts a widget to the vertical layout at position specified by row.
'''
self.formWidget.uiElements['verticalLayout'].insertWidget(row, qwidget)

def removeWidgetFromVerticalLayout(self, qwidget):
'''
Removes a widget from the vertical layout.
'''
self.formWidget.uiElements['verticalLayout'].removeWidget(qwidget)
qwidget.setParent(None)

def getWidgetFromVerticalLayout(self, index):
'''
Returns the widget in the vertical layout located at position index.
'''
return self.formWidget.uiElements['verticalLayout'].itemAt(index).widget()

def removeWidget(self, name):
'''
Removes the widget with the specified name from the form layout.
This method delete the qwidget, and qlabel if present, from the widgets dictionary
and sets their parent to `None`.

Parameters
----------
name: str
The name of the widget to be removed.

Returns
-------
tuple or QWidget
If the widget has a corresponding label, a tuple containing the widget
and label is returned. Otherwise, only the widget is returned.
'''
self.formWidget.removeWidget(name)

def getNumWidgets(self):
'''
Returns the number of widgets in the form.
'''
return self.formWidget.getNumWidgets()

def insertWidget(self, index, qwidget):
'''inserts a widget to the vertical layout at the specific index'''
self.formWidget.uiElements['verticalLayout'].insertWidget(index, qwidget)

def getWidget(self, name, role='field'):
'''returns the Widget by the name with which it has been added

'''
Returns the widget by the name with which it has been added.
By default it returns the widget that is the field in the form.
The user can get the label by specifying the role to be label

The user can get the label by specifying the role to be label.
Raises ValueError if the role is not field or label.
'''
return self.formWidget.getWidget(name, role)

def getWidgets(self):
'''returns a dictionary of all the widgets in the form'''
'''Returns a dictionary of the widgets currently present in the form.'''
return self.formWidget.getWidgets()

def setWidgetVisible(self, name, visible):
def getWidgetNumber(self, name, role='field'):
'''
Sets the visibility of the widget and associated label with the given name.
Parameters:
visible: bool
True to set the widget visible, False to hide it
name: str
The name of the widget to set visible/invisible
Returns the widget number by the widget name.
This is the row of the widget in the form layout.
'''
return self.formWidget.getWidgetNumber(name, role)

def setWidgetVisible(self, name, visible):
'''
Sets the visibility of the named widget (and associated label).
'''
self.formWidget.setWidgetVisible(name, visible)

def saveAllWidgetStates(self):
'''
Saves the state of all widgets in the form.
Saves the state of all widgets currently present in the form.
To later restore the states, use `restoreAllSavedWidgetStates()`.
'''
self.formWidget.saveAllWidgetStates()

def getWidgetStates(self):
'''Returns the saved widget states.'''
return self.formWidget.getWidgetStates()

def getDefaultWidgetStates(self):
'''Returns the saved default widget states.'''
return self.formWidget.getDefaultWidgetStates()

def restoreAllSavedWidgetStates(self):
'''
Restore all widgets in the form to the state saved by `saveAllWidgetStates()`.
Expand All @@ -170,59 +242,67 @@ def getAllWidgetStates(self):
Returns
-------
states: dict
Format: {'widget_name': {'value': str | bool | int, 'enabled': bool, 'visible': bool},
...},
e.g. {{'widget1': {'value': 1, 'enabled': True, 'visible': True},
'widget2': {'value': 2, 'enabled': False, 'visible': False}}.
Format: {'widget_name': {'value': str | bool | int, 'enabled': bool, 'visible': bool,
'widget_number': int}, ...},
e.g. {'widget1': {'value': 1, 'enabled': True, 'visible': True, 'widget_number': 0},
'widget2': {'value': 2, 'enabled': False, 'visible': False, 'widget_number': 1}}.
'''
return self.formWidget.getAllWidgetStates()

def getWidgetState(self, widget, role=None):
'''
Returns the current state of the widget in the form.

Parameters
----------
widget: QWidget or str
The (name of) widget to get the state of.
The widget or its name (or its name + '_field' or '_label', when role is None) to get
the state of.
role: str, optional, default None, values: 'label', 'field', None.
The role of the widget to get the state of (only if `widget` is a `str`).
If unspecified, the widget is chosen based on `name=widget`.
The role of the widget to apply the state to. This is used only if `widget` is the
widget name string.

Returns
-------
dict
Widget state, format: {'value': str | bool | int, 'enabled': bool, 'visible': bool},
e.g. {'value': 1, 'enabled': True, 'visible': True}.
state: dict
Format: {'value': str | bool | int, 'enabled': bool, 'visible': bool,
'widget_number' : int},
e.g. {'value': 1, 'enabled': True, 'visible': True, 'widget_number' : 0}.
This can be used to restore the state of the widget using `setWidgetState()`.
'''
return self.formWidget.getWidgetState(widget, role)

def applyWidgetState(self, name, state, role=None):
'''
Applies the given state to the widget with the given name.
Applies the given `state` to the widget associated with `name` and `role`.
If role is None, the role is assigned to be 'field'.

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 unspecified, the widget is chosen based on `name`.
state: dict
Format: {'value': str | bool | int, 'enabled': bool, 'visible': bool},
e.g. {'value': 1, 'enabled': True, 'visible': True}.
Format: {'value': str | bool | int, 'enabled': bool, 'visible': bool,
'widget_number' : int},
e.g. {'value': 1, 'enabled': True, 'visible': True, 'widget_number' : 0}.
'''
return self.formWidget.applyWidgetState(name, state, role)

def applyWidgetStates(self, state):
def applyWidgetStates(self, states):
'''
Applies the given states to the form's widgets.
Applies the given states to the form's widgets. It raises an error if the keys in the
dicitonary of states and the keys in the dictionary of widgets in the form are not the
same.

Parameters
----------
state: dict
Format: {'widget_name': {'value': str | bool | int, 'enabled': bool, 'visible': bool},
...},
e.g. {{'widget1': {'value': 1, 'enabled': True, 'visible': True},
'widget2': {'value': 2, 'enabled': False, 'visible': False}}.
states: dict
Format: {'name_field': {'value': str | bool | int, 'enabled': bool, 'visible': bool,
'widget_number' : int}, 'name_label': {'value': str | bool | int,
'enabled': bool, 'visible': bool, 'widget_number' : int}, ...}.
e.g. {'widget1': {'value': 1, 'enabled': True, 'visible': True, 'widget_number': 0},
'widget2': {'value': 2, 'enabled': False, 'visible': False, 'widget_number': 1}}.
'''
return self.formWidget.applyWidgetStates(state)
return self.formWidget.applyWidgetStates(states)
Loading