Skip to content

Commit

Permalink
Merge branch 'main' into changelog-style
Browse files Browse the repository at this point in the history
  • Loading branch information
DanicaSTFC committed Feb 22, 2024
2 parents ca32c64 + 6570d9a commit a576b53
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Version x.x.x
- Adds unit test for `addWidget` and `addSpanningWidget`; - Adds `getIndexFromVerticalLayout`
to `FormDialog` (#123).
- Initialises `widget_states` in init (#132).
- Adds methods to insert widgets in the forms & tests/example
Removes `_addWidget`
Adds `getWidgetRow`and updates states dictionary and related methods
Expand Down
16 changes: 16 additions & 0 deletions eqt/ui/FormDialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,22 @@ def getWidgetFromVerticalLayout(self, index):
Returns the widget in the vertical layout located at position index.'''
return self.formWidget.uiElements['verticalLayout'].itemAt(index).widget()

def getIndexFromVerticalLayout(self, widget):
'''
Returns the index of the widget in the vertical layout.
Parameters
-------------
widget : QWidget
The widget in the layout.
Return
------------
int
The index of the widget in the layout.
'''
return self.formWidget.uiElements['verticalLayout'].indexOf(widget)

def removeWidget(self, widget):
'''
Removes the widget with the specified name from the form layout.
Expand Down
10 changes: 8 additions & 2 deletions eqt/ui/UIFormWidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def createForm(self):
'verticalLayout': verticalLayout, 'groupBox': groupBox,
'groupBoxFormLayout': groupBoxFormLayout}
self.widgets = {}
self.widget_states = {}
self.default_widget_states = {}

@property
Expand Down Expand Up @@ -80,14 +81,19 @@ def insertWidget(self, row, name, qwidget, qlabel=None):
'''
if f'{name}_field' in self.widgets:
raise KeyError(f"Widget name ({name}) already defined. Choose another name.")

formLayout = self.uiElements['groupBoxFormLayout']
if formLayout.indexOf(qwidget) != -1:
raise KeyError(f"The widget {qwidget} is already in use. Create another QWidget.")

if qlabel is not None:
if isinstance(qlabel, str):
txt = qlabel
qlabel = QtWidgets.QLabel(self)
qlabel.setText(txt)
else:
if formLayout.indexOf(qlabel) != -1:
raise KeyError(
f"The widget {qlabel} is already in use. Create another QLabel.")
formLayout.insertRow(row, qlabel, qwidget)
self.widgets[f'{name}_label'] = qlabel
self.default_widget_states[f'{name}_label'] = self.getWidgetState(name, 'label')
Expand Down Expand Up @@ -472,7 +478,7 @@ def restoreAllSavedWidgetStates(self):
`saveAllWidgetStates` was previously invoked. If there are no previously saved states,
`default_widget_states` are used instead, after being made visible.
'''
if not hasattr(self, 'widget_states'):
if not self.widget_states:
self.setDefaultWidgetStatesVisibleTrue()
self.applyWidgetStates(self.default_widget_states)
else:
Expand Down
69 changes: 68 additions & 1 deletion test/test__formUI_status_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,37 @@ def set_state(self, i):
self.form.getWidget('button').setCheckable(True)
self.form.getWidget('button').setChecked(state[i]['pushButton_value'])

def _test_add_one_widget(self, name, qwidget, qlabel):
"""
Invokes `addWidget`, therefore inserts the qwidget and the qlabel
at the end of the layout. Checks the position of the widget in the form is the last one.
"""
self.form.addWidget(qwidget, qlabel, name)
position = self.layout.getWidgetPosition(self.form.getWidget(name, 'field'))[0]
self.assertEqual(position, self.form.getNumWidgets() - 1)

def _test_add_one_spanning_widget(self, name, qwidget):
"""
Invokes `addSpanningWidget`, therefore inserts the qwidget
at the end of the layout. Checks the position of the widget in the form is the last one.
"""
name = f'{name}_spanning'
self.form.addSpanningWidget(qwidget, name)
position = self.layout.getWidgetPosition(self.form.getWidget(name, 'field'))[0]
self.assertEqual(position, self.form.getNumWidgets() - 1)

def test_add_every_widget(self):
"""
Adds each widget, and then each spanning widget, in the end of the form layout.
Tests the position of the widgets in the layout is the last one.
"""
for key in self.list_all_widgets:
qwidget = self.list_all_widgets[key]
name = f'{key}_added'
self._test_add_one_widget(name, qwidget, name)
qwidget = self.list_all_widgets[key]
self._test_add_one_spanning_widget(name + '_spanning', qwidget)

def _test_insert_one_widget(self, row, name, qwidget, qlabel=None):
"""
Invokes `insertWidget`, therefore inserts the qwidget (and the qlabel)
Expand All @@ -139,7 +170,7 @@ def test_insert_every_widget(self):

def _test_remove_one_widget(self, name):
"""
Remove one widget.
Removes one widget.
Checks the number of widgets in the form before and after deletion are consistent.
Checks the number of rows in the layout and number of widgets in the form are
consistent.
Expand Down Expand Up @@ -470,6 +501,34 @@ def _test_insert_one_widget_to_vertical_layout(self, row, qwidget):
position = self.vertical_layout.indexOf(qwidget)
self.assertEqual(position, row)

def _test_add_one_widget_to_vertical_layout(self, qwidget):
"""
Invokes `addWidget` with vertical layout. Therefore adds the qwidget
at the end of the layout. Checks that the widget inserted is the inputted one and
its position in the layout corresponds to the number of widgets in the layout
before insertion.
"""
num_widgets = self.vertical_layout.count()
self.form.addWidget(qwidget, layout='vertical')
index = self.form.getIndexFromVerticalLayout(qwidget)
self.assertEqual(index, num_widgets)
widget = self.vertical_layout.itemAt(num_widgets).widget()
self.assertEqual(qwidget, widget)

def _test_add_one_spanning_widget_to_vertical_layout(self, qwidget):
"""
Invokes `addSpanningWidget` with vertical layout. Therefore adds the qwidget
at the end of the layout. Checks that the widget inserted is the inputted one and
its position in the layout corresponds to the number of widgets in the layout
before insertion.
"""
num_widgets = self.vertical_layout.count()
self.form.addSpanningWidget(qwidget, layout='vertical')
index = self.form.getIndexFromVerticalLayout(qwidget)
self.assertEqual(index, num_widgets)
widget = self.vertical_layout.itemAt(num_widgets).widget()
self.assertEqual(qwidget, widget)

def test_insert_every_widget_to_vertical_layout(self):
"""
Inserts each widget in position 0 of the vertical layout and tests its position in
Expand All @@ -478,6 +537,14 @@ def test_insert_every_widget_to_vertical_layout(self):
for key in self.list_all_widgets:
self._test_insert_one_widget_to_vertical_layout(0, self.list_all_widgets[key])

def test_add_every_widget_to_vertical_layout(self):
"""
Adds each widget and each spanning widget at the end of the vertical layout and tests them.
"""
for key in self.list_all_widgets:
self._test_add_one_widget_to_vertical_layout(self.list_all_widgets[key])
self._test_add_one_spanning_widget_to_vertical_layout(self.list_all_widgets[key])

def _test_remove_one_widget_from_vertical_layout(self, widget):
"""
Removes one widget from the vertical layout.
Expand Down

0 comments on commit a576b53

Please sign in to comment.