Skip to content

Commit

Permalink
Delete _addwidget and other changes from 2nd review
Browse files Browse the repository at this point in the history
  • Loading branch information
DanicaSTFC committed Jan 22, 2024
1 parent 11caf76 commit 8bd14a5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 36 deletions.
19 changes: 13 additions & 6 deletions eqt/ui/FormDialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,21 @@ def widgets(self):
def groupBox(self):
return self.formWidget.groupBox

def addWidget(self, qwidget, qlabel=None, name=None, layout='form'):
def addWidget(self, qwidget, qlabel = None, name = None, layout = 'form'):
'''
layout = 'vertical' - adds to the Vertical layout below the form.
`qlabel` and `name` are unsupported when layout is vertical.
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'.
layout = 'form' - adds to the FormLayout
Name and qlabel must be passed to add to the form layout .
Adds a qwidget and a qlabel widget in the same row of the form layout.
Parameters
----------------
qwidget : widget
qlabel : qlabel widget or str
only supported when layout is 'form'
name : str
only supported when layout is 'form'
layout : 'form' or 'vertical'
'form' - adds to the FormLayout, 'vertical' - adds to the Vertical layout below the form.
'''
if layout == 'vertical':
if name is not None or qlabel is not None:
Expand Down
43 changes: 13 additions & 30 deletions eqt/ui/UIFormWidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def createForm(self):
'verticalLayout': verticalLayout, 'groupBox': groupBox,
'groupBoxFormLayout': groupBoxFormLayout}
self.widgets = {}
self.removed_widgets_dictionary = {}

@property
def num_widgets(self):
Expand Down Expand Up @@ -84,20 +85,7 @@ def insertWidgetToFormLayout(self, row, name, qwidget, qlabel=None):
formLayout.insertRow(row, qwidget)
self._addToWidgetDictionary(self.widgets, name, qwidget, qlabel)
self._addToWidgetNumberDictionary(name, row)
self.addToDefaultWidgetStatesDictionary(name)

def _addWidget(self, name, qwidget, qlabel=None):
'''
Adds a widget and a label widget, or a spanning widget, at the the end of
the form layout.
Parameters:
----------
name: str
qwidget: widget
qlabel: qlabel widget or str
'''
self.insertWidgetToFormLayout(-1, name, qwidget, qlabel)
self._addToDefaultWidgetStatesDictionary(name)

def _addToWidgetDictionary(self, dictionary, name, qwidget, qlabel = None):
'''Adds the field, and label if present, in the widget dictionary.'''
Expand Down Expand Up @@ -172,11 +160,11 @@ def addWidget(self, qwidget, qlabel, name):
qwidget: widget
qlabel: qlabel widget or str
'''
self._addWidget(name, qwidget, qlabel)
self.insertWidgetToFormLayout(-1, name, qwidget, qlabel)

def addSpanningWidget(self, qwidget, name):
'''Adds a spanning qwidget occupying the full row in the form layout.'''
self._addWidget(name, qwidget)
self.insertWidgetToFormLayout(-1, name, qwidget)

def getNumWidgets(self):
'''
Expand All @@ -186,21 +174,17 @@ def getNumWidgets(self):

def removeWidget(self, name):
'''
If not present already, creates a dictionary to store the removed qwidgets.
Sets the parent of the qwidget, and qlabel if present, to `None` and
stores the widgets in the removed-widgets dictionary.
Deletes the row in the form layout.
Deletes the qwidget and qlabel from the widgets dictionary.
Deletes the widget number from the widget-number dictionary.
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.
Parameters:
--------------
name : str
name of the widget to be removed
'''
formLayout = self.uiElements['groupBoxFormLayout']
if not hasattr(self, 'removed_widgets_dictionary'):
self.removed_widgets_dictionary = {}
widget_number = self.getWidgetNumber(name)
qwidget = self.getWidget(name, role='field')
if f'{name}_label' in self.getWidgets().keys():
Expand Down Expand Up @@ -267,16 +251,16 @@ def addTitle(self, qlabel, name):
qlabel = QtWidgets.QLabel(self.uiElements['groupBox'])
qlabel.setText(txt)
qlabel.setStyleSheet("font-weight: bold")
self._addWidget(name, qlabel)
self.insertWidgetToFormLayout(-1, name, qlabel)

def addSeparator(self, name):
# Adds horizontal separator to the form
frame = QtWidgets.QFrame()
frame.setFrameShape(QtWidgets.QFrame.HLine)
frame.setFrameShadow(QtWidgets.QFrame.Raised)
self._addWidget(name, frame)
self.insertWidgetToFormLayout(-1, name, frame)

def addToDefaultWidgetStatesDictionary(self, name):
def _addToDefaultWidgetStatesDictionary(self, name):
'''
If not present already, creates an attribute dictionary of default widget states. The entries are in the
format: {'value': str | bool | int, 'enabled': bool, 'visible': bool, 'widget_number': int}.
Expand Down Expand Up @@ -434,9 +418,8 @@ def applyWidgetState(self, name, state, role=None):
try:
if name_role in self.widgets.keys():
widget = self.widgets[name_role]
elif hasattr(self, 'removed_widgets_dictionary'):
if name_role in self.removed_widgets_dictionary.keys():
widget = self.removed_widgets_dictionary[name_role]
elif name_role in self.removed_widgets_dictionary.keys():
widget = self.removed_widgets_dictionary[name_role]
except KeyError:
raise KeyError('No widget associated with the dictionary key `'+name_role+'`')
#apply state
Expand Down

0 comments on commit 8bd14a5

Please sign in to comment.