From d6e556ace56938927229ae6b80ef5a4d0f299e24 Mon Sep 17 00:00:00 2001 From: Danica Sugic Date: Tue, 30 Jan 2024 13:43:41 +0000 Subject: [PATCH] Delete removed widget dict and widget number dict and change getWidgetNumber --- eqt/ui/FormDialog.py | 32 +++--- eqt/ui/UIFormWidget.py | 166 ++++++++++++------------------- test/test__formUI_status_test.py | 1 - 3 files changed, 81 insertions(+), 118 deletions(-) diff --git a/eqt/ui/FormDialog.py b/eqt/ui/FormDialog.py index ea3f014..a0ea1ac 100644 --- a/eqt/ui/FormDialog.py +++ b/eqt/ui/FormDialog.py @@ -160,16 +160,20 @@ def getWidgetFromVerticalLayout(self, index): def removeWidget(self, name): ''' - Removes the widget with name `name` from the widgets in the form layout. In particular, - it deletes the row in the form layout, the qwidget and qlabel from the widgets dictionary - and the widget number from the widget-number dictionary. Sets the parent of the qwidget, - and qlabel if present, to `None` allowing to store the removed widget in the - removed-widgets dictionary. + 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 - name of the widget to be removed + 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) @@ -192,17 +196,11 @@ def getWidgets(self): '''Returns a dictionary of the widgets currently present in the form.''' return self.formWidget.getWidgets() - def getRemovedWidgets(self): - '''Returns the dictionary of the removed widgets previously present in the form.''' - return self.formWidget.getRemovedWidgets() - - def getWidgetNumber(self, name): - '''Returns the widget number by the widget name.''' - return self.formWidget.getWidgetNumber(name) - - def getWidgetNumberDictionary(self): - '''Returns the widget number dictionary.''' - return self.formWidget.getWidgetNumberDictionary() + def getWidgetNumber(self, name, role = 'field'): + ''' + 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): ''' diff --git a/eqt/ui/UIFormWidget.py b/eqt/ui/UIFormWidget.py index 5d3f918..f366241 100644 --- a/eqt/ui/UIFormWidget.py +++ b/eqt/ui/UIFormWidget.py @@ -39,13 +39,10 @@ def createForm(self): # Add elements to layout verticalLayout.addWidget(groupBox) - # number of widgets currently present in the groupBoxFormLayout - self.widget_number_dictionary = {} self.uiElements = { 'verticalLayout': verticalLayout, 'groupBox': groupBox, 'groupBoxFormLayout': groupBoxFormLayout} self.widgets = {} - self.removed_widgets_dictionary = {} @property def num_widgets(self): @@ -92,7 +89,6 @@ def insertWidgetToFormLayout(self, row, name, qwidget, qlabel=None): else: formLayout.insertRow(row, qwidget) self._addToWidgetDictionary(self.widgets, name, qwidget, qlabel) - self._addToWidgetNumberDictionary(name, row) self._addToDefaultWidgetStatesDictionary(name) def _addToWidgetDictionary(self, dictionary, name, qwidget, qlabel=None): @@ -101,61 +97,42 @@ def _addToWidgetDictionary(self, dictionary, name, qwidget, qlabel=None): if qlabel is not None: dictionary[f'{name}_label'] = qlabel - def _addToWidgetNumberDictionary(self, name, widget_number): + def _popWidgetFromDictionary(self, dictionary, name): ''' - Adds one item in the widget-number dictionary whose key is name and value is - the current widget number (i.e. row) in the form layout. - As one widget is inserted, the widget numbers associated to the other widgets - in the layout are updated. + Removes the item(s) associated with `name` from a dictionary. Parameters: - --------------- - name: string - name of the widget - widget_number : int - position of the widget in the form layout, i.e. row, in the current state - ''' - if widget_number == -1: - self.widget_number_dictionary[name] = self.num_widgets - 1 - else: - for key, value in self.widget_number_dictionary.items(): - if value >= widget_number: - self.widget_number_dictionary[key] = value + 1 - self.widget_number_dictionary[name] = widget_number + ----------------- + dictionary : dict + The dictionary from which to remove the items. + name : str + The name of the item(s) to be removed. - def _popWidgetNumberDictionary(self, name): - ''' - Removes one item in the widget-number dictionary whose key is name. - As one widget is removed, the widget numbers associated to the other widgets - in the layout are updated. + Returns: + ------- + qwidget : QWidget + The removed widget associated with `name`, if it exists in the dictionary. - Parameters: - --------------- - name: string - name of the widget - ''' - widget_number = self.getWidgetNumber(name) - for key, value in self.widget_number_dictionary.items(): - if value > widget_number: - self.widget_number_dictionary[key] = value - 1 - self.widget_number_dictionary.pop(name) + qlabel : QLabel, optional + The removed label associated with `name`, if it exists in the dictionary. - def _popWidgetFromDictionary(self, dictionary, name): - ''' - Removes the item(s) associated with `name` from a dictionary. + Raises: + ------ + KeyError + If no widget associated with the dictionary key `name` or `{name}_field` is found. - Parameters: - ----------------- - dictionary : dict - name: str - Format: {name} ''' if name in dictionary.keys(): - dictionary.pop(name) - if f'{name}_field' in dictionary.keys(): - dictionary.pop(f'{name}_field') + qwidget = dictionary.pop(name) + elif f'{name}_field' in dictionary.keys(): + qwidget = dictionary.pop(f'{name}_field') + else: + raise KeyError(f'No widget associated with the dictionary key `{name}` or `{name}_field`.') if f'{name}_label' in dictionary.keys(): - dictionary.pop(f'{name}_label') + qlabel = dictionary.pop(f'{name}_label') + return qwidget, qlabel + else: + return qwidget def addWidget(self, qwidget, qlabel, name): ''' @@ -195,29 +172,32 @@ def getNumWidgets(self): def removeWidget(self, name): ''' - Removes the widget with name `name` from the widgets in the form layout. In particular, - it deletes the row in the form layout, the qwidget and qlabel from the widgets dictionary - and the widget number from the widget-number dictionary. Sets the parent of the qwidget, - and qlabel if present, to `None` allowing to store the removed widget in the - removed-widgets dictionary. + 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 - name of the widget to be removed + 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. ''' - formLayout = self.uiElements['groupBoxFormLayout'] - qwidget = self.getWidget(name, role='field') + widget_number = self.getWidgetNumber(name) if f'{name}_label' in self.getWidgets().keys(): - qlabel = self.getWidget(name, role='label') - self._addToWidgetDictionary(self.removed_widgets_dictionary, name, qwidget, qlabel) self.getWidget(name, 'label').setParent(None) - else: - self._addToWidgetDictionary(self.removed_widgets_dictionary, name, qwidget) + qwidget, qlabel = self._popWidgetFromDictionary(self.getWidgets(), name) + self.uiElements['groupBoxFormLayout'].removeRow(widget_number) + return qwidget, qlabel self.getWidget(name, 'field').setParent(None) - formLayout.removeRow(self.widget_number_dictionary[name]) - self._popWidgetFromDictionary(self.getWidgets(), name) - self._popWidgetNumberDictionary(name) + qwidget = self._popWidgetFromDictionary(self.getWidgets(), name) + self.uiElements['groupBoxFormLayout'].removeRow(widget_number) + return qwidget + def getWidget(self, name, role='field'): ''' @@ -231,13 +211,11 @@ def getWidget(self, name, role='field'): return self.widgets[f'{name}_{role}'] raise ValueError(f'Unexpected role: expected any of {allowed_roles}, got {role}') - def getWidgetNumber(self, name): - '''Returns the widget number by the widget name.''' - return self.widget_number_dictionary[f'{name}'] - - def getWidgetNumberDictionary(self): - '''Returns the widget number dictionary.''' - return self.widget_number_dictionary + def getWidgetNumber(self, name, role = 'field'): + ''' + Returns the widget number by the widget name. This is the row of the widget in the form layout. + ''' + return self.uiElements['groupBoxFormLayout'].getWidgetPosition(self.getWidget(name, role))[0] def setWidgetVisible(self, name, visible): ''' @@ -262,10 +240,6 @@ def getWidgets(self): '''Returns a dictionary of the widgets currently present in the form.''' return self.widgets - def getRemovedWidgets(self): - '''Returns the dictionary of the removed widgets previously present in the form.''' - return self.removed_widgets_dictionary - def addTitle(self, qlabel, name): if isinstance(qlabel, str): txt = qlabel @@ -379,7 +353,7 @@ def getWidgetState(self, widget, role=None): elif isinstance(widget, (QtWidgets.QTextEdit, QtWidgets.QPlainTextEdit)): widget_state['value'] = widget.toPlainText() - widget_state['widget_number'] = self.widget_number_dictionary[name] + widget_state['widget_number'] = self.getWidgetNumber(name, role) return widget_state def _getNameAndRoleFromKey(self, key): @@ -442,10 +416,7 @@ def applyWidgetState(self, name, state, role=None): # retrieve widget try: - if name_role in self.widgets.keys(): - widget = self.widgets[name_role] - elif name_role in self.removed_widgets_dictionary.keys(): - widget = self.removed_widgets_dictionary[name_role] + widget = self.widgets[name_role] except KeyError: raise KeyError(f'No widget associated with the dictionary key `{name_role}`.') # apply state @@ -473,9 +444,6 @@ def applyWidgetState(self, name, state, role=None): widget.setChecked(value) elif isinstance(widget, (QtWidgets.QTextEdit, QtWidgets.QPlainTextEdit)): widget.setPlainText(value) - elif key == 'widget_number': - if value != self.widget_number_dictionary[name]: - self.widget_number_dictionary[name] = value def applyWidgetStates(self, states): ''' @@ -602,16 +570,20 @@ def insertWidgetToFormLayout(self, row, name, qwidget, qlabel=None): def removeWidget(self, name): ''' - Removes the widget with name `name` from the widgets in the form layout. In particular, - it deletes the row in the form layout, the qwidget and qlabel from the widgets dictionary - and the widget number from the widget-number dictionary. Sets the parent of the qwidget, - and qlabel if present, to `None` allowing to store the removed widget in the - removed-widgets dictionary. + 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 - name of the widget to be removed + 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.widget().removeWidget(name) @@ -634,17 +606,11 @@ def getWidgets(self): '''Returns a dictionary of the widgets currently present in the form.''' return self.widget().getWidgets() - def getWidgetNumber(self, name): - '''Returns the widget number by the widget name.''' - return self.widget().getWidgetNumber(name) - - def getWidgetNumberDictionary(self): - '''Returns the widget number dictionary.''' - return self.widget().getWidgetNumberDictionary() - - def getRemovedWidgets(self): - '''Returns the dictionary of the removed widgets previously present in the form.''' - return self.widget().getRemovedWidgets() + def getWidgetNumber(self, name, role = 'field'): + ''' + Returns the widget number by the widget name. This is the row of the widget in the form layout. + ''' + return self.widget().getWidgetNumber(name, role) def setWidgetVisible(self, name, visible): ''' diff --git a/test/test__formUI_status_test.py b/test/test__formUI_status_test.py index eca6904..2e3ee94 100644 --- a/test/test__formUI_status_test.py +++ b/test/test__formUI_status_test.py @@ -161,7 +161,6 @@ def _test_remove_one_widget(self, name): self.assertEqual(prenumwidgets, postnumwidgets + 1) self.assertEqual(prerowcount, postrowcount + 1) self.assertEqual(postrowcount, postnumwidgets) - self.assertEqual(self.form.getRemovedWidgets()[f'{name}_field'], qwidget) def test_remove_every_widget(self): """Remove every widget from the form."""