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

slight tidy #129

Merged
merged 21 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
53104e8
Merge branch 'main' of github.com:TomographicImaging/eqt
DanicaSTFC Dec 4, 2023
e6d7b1e
Merge branch 'main' of github.com:TomographicImaging/eqt
DanicaSTFC Dec 7, 2023
58b4fa3
Merge branch 'main' of github.com:TomographicImaging/eqt
DanicaSTFC Jan 23, 2024
de75f65
slight tidy
casperdcl Feb 14, 2024
5786f3b
Merge branch 'main' of github.com:TomographicImaging/eqt
DanicaSTFC Feb 14, 2024
e18171d
Merge branch 'slight-tidy' of github.com:TomographicImaging/eqt into …
DanicaSTFC Feb 20, 2024
4cf969b
Merge branch 'main' of github.com:TomographicImaging/eqt
DanicaSTFC Feb 22, 2024
6570d9a
Merge branch 'main' of github.com:TomographicImaging/eqt
DanicaSTFC Feb 22, 2024
bf59656
Merge branch 'main' of github.com:TomographicImaging/eqt
DanicaSTFC Feb 22, 2024
c90c4ec
Merge branch 'main' of github.com:TomographicImaging/eqt
DanicaSTFC Feb 22, 2024
340e956
Merge branch 'main' of github.com:TomographicImaging/eqt
DanicaSTFC Mar 26, 2024
535c4c3
merge main
DanicaSTFC Mar 26, 2024
cbe7b0b
Fix tests, minor changes and change order in the widget states
DanicaSTFC Mar 26, 2024
836fe59
edit changelog
DanicaSTFC Mar 26, 2024
741399d
pre-commit run
DanicaSTFC Mar 26, 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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new changes are not passing the tests, could you fix this before we look at it? (101 fail, 36 pass on my local machine)

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
- Adds methods to insert widgets in the forms & tests/example
Removes `_addWidget`
Adds `getWidgetRow`and updates states dictionary and related methods
Adds `getNameAndRole*`.
Adds `getNameAndRole*`
Changes `num_widgets` to be a property (#109)
- Reinstates changelog (#99)
- Adds `title` to `FormDockWidget` & update tests/examples (#102)
Expand Down
55 changes: 27 additions & 28 deletions eqt/ui/FormDialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,13 @@

class FormDialog(QtWidgets.QDialog):
def __init__(self, parent=None, title=None):
super().__init__(self, parent)

QtWidgets.QDialog.__init__(self, parent)

# button box
bb = QtWidgets.QDialogButtonBox(QtWidgets.QDialogButtonBox.Ok
| QtWidgets.QDialogButtonBox.Cancel)
self.buttonBox = bb

formWidget = UIFormFactory.getQWidget(parent=self)
self.formWidget = formWidget

self.buttonBox = bb = QtWidgets.QDialogButtonBox(QtWidgets.QDialogButtonBox.Ok
| QtWidgets.QDialogButtonBox.Cancel)
self.formWidget = UIFormFactory.getQWidget(parent=self)
# set the layout of the dialog
self.setLayout(formWidget.uiElements['verticalLayout'])
self.setLayout(self.formWidget.uiElements['verticalLayout'])

if title is not None:
self.setWindowTitle(title)
Expand Down Expand Up @@ -75,7 +69,7 @@ def addWidget(self, qwidget, qlabel=None, name=None, layout='form'):

Parameters
----------
qwidget : widget
qwidget : QWidget
qlabel : qlabel widget or str
only supported when layout is 'form'
name : str
Expand All @@ -100,9 +94,15 @@ def addWidget(self, qwidget, qlabel=None, name=None, layout='form'):
def addSpanningWidget(self, qwidget, name=None, layout='form'):
'''
Adds a spanning widget occupying the full row in the layout.
layout = 'form' - adds the widget to the form layout
layout = 'vertical' - adds the widget to the vertical layout below the form.
To add to the form layout, name must be passed.

Parameters
----------
qwidget : QWidget
name : str
only supported when layout is 'form'
layout : 'form' or 'vertical'
'form' - adds to the `groupBoxFormLayout`,
'vertical' - adds to the `verticalLayout` below the form.
'''
if layout == 'vertical':
if name is not None:
Expand Down Expand Up @@ -131,7 +131,7 @@ def insertWidget(self, row, name, qwidget, qlabel=None):
The position in the form where the widget is added.
name : str
The string associated to the qwidget and qlabel.
qwidget : widget
qwidget : QWidget
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
Expand All @@ -141,17 +141,14 @@ def insertWidget(self, row, name, qwidget, qlabel=None):
self.formWidget.insertWidget(row, name, qwidget, qlabel)

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

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

def removeWidget(self, 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
Expand All @@ -168,20 +165,22 @@ def removeWidget(self, widget):
If the widget has a corresponding label, a tuple containing the widget
and label is returned. Otherwise, only the widget is returned.
'''
return self.formWidget.removeWidget(widget)
return self.formWidget.removeWidget(name)

def removeWidgetFromVerticalLayout(self, widget):
'''
Removes a widget from the vertical layout.
'''Removes a widget from the vertical layout.

Parameters
----------
widget : QWidget
The widget to be removed.
'''
self.formWidget.uiElements['verticalLayout'].removeWidget(widget)
widget.setParent(None)
return widget

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

def getWidget(self, name, role='field'):
Expand Down
47 changes: 15 additions & 32 deletions eqt/ui/UIFormWidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ class UIFormWidget:
| |
+----------------------------------------------------------+
'''
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

def createForm(self):
# Add vertical layout to dock contents
verticalLayout = QtWidgets.QVBoxLayout(self)
Expand Down Expand Up @@ -71,7 +68,7 @@ def insertWidget(self, row, name, qwidget, qlabel=None):
The position in the form where the widget is added.
name : str
The string associated to the qwidget and qlabel.
qwidget : widget
qwidget : QWidget
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
Expand All @@ -91,7 +88,6 @@ def insertWidget(self, row, name, qwidget, qlabel=None):
formLayout.insertRow(row, qlabel, qwidget)
self.widgets[f'{name}_label'] = qlabel
self.default_widget_states[f'{name}_label'] = self.getWidgetState(name, 'label')

else:
formLayout.insertRow(row, qwidget)

Expand Down Expand Up @@ -140,7 +136,7 @@ def addWidget(self, qwidget, qlabel, name):

Parameters
----------
qwidget : widget
qwidget : QWidget
The widget to be added on the right hand side of the form.
qlabel : qlabel widget or str
The qlabel widget, or a str from which a qlabel widget is created, to be added
Expand Down Expand Up @@ -272,10 +268,7 @@ def getAllWidgetStates(self):
e.g. {'widget1': {'value': 1, 'enabled': True, 'visible': True, 'widget_row': 0},
'widget2': {'value': 2, 'enabled': False, 'visible': False, 'widget_row': 1}}.
'''
all_widget_states = {}
for key, widget in self.widgets.items():
all_widget_states[key] = self.getWidgetState(widget)
return all_widget_states
return {key: self.getWidgetState(widget) for key, widget in self.widgets.items()}

def getWidgetState(self, widget, role=None):
'''
Expand Down Expand Up @@ -317,9 +310,9 @@ def getWidgetState(self, widget, role=None):
raise KeyError(f'No widget associated with the dictionary key `{name_role}`.')
else:
name, role = self._getNameAndRoleFromWidget(widget)
widget_state = {}
widget_state['enabled'] = widget.isEnabled()
widget_state['visible'] = widget.isVisible()
widget_state = {
'enabled': widget.isEnabled(), 'visible': widget.isVisible(),
'widget_row': self.getWidgetRow(name, role)}

if isinstance(widget, QtWidgets.QLabel):
widget_state['value'] = widget.text()
Expand All @@ -337,8 +330,6 @@ def getWidgetState(self, widget, role=None):
widget_state['value'] = widget.isChecked()
elif isinstance(widget, (QtWidgets.QTextEdit, QtWidgets.QPlainTextEdit)):
widget_state['value'] = widget.toPlainText()

widget_state['widget_row'] = self.getWidgetRow(name, role)
return widget_state

def _getNameAndRoleFromKey(self, key):
Expand All @@ -352,15 +343,10 @@ def _getNameAndRoleFromKey(self, key):
Format: name or name_field or name_label
'''
if key.endswith('_field'):
name = key.removesuffix('_field')
role = 'field'
return key.removesuffix('_field'), 'field'
elif key.endswith('_label'):
name = key.removesuffix('_label')
role = 'label'
else:
name = key
role = 'field'
return name, role
return key.removesuffix('_label'), 'label'
return key, 'field'

def _getNameAndRoleFromWidget(self, widget):
'''
Expand Down Expand Up @@ -392,18 +378,15 @@ def applyWidgetState(self, name, state, role=None):
e.g. {'value': 1, 'enabled': True, 'visible': True, 'widget_row' : 0}.
'''
if role is not None:
if role in ['label', 'field']:
if role in ('label', 'field'):
name_role = name + '_' + role
else:
raise ValueError(f'Role must be either "label", "field" or None. Got {role}.')
else:
name_role = f'{name}_field'

# retrieve widget
try:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we removing this KeyError?

widget = self.widgets[name_role]
except KeyError:
raise KeyError(f'No widget associated with the dictionary key `{name_role}`.')
widget = self.widgets[name_role]
# apply state
for key, value in state.items():
if key == 'enabled':
Expand Down Expand Up @@ -446,7 +429,7 @@ def applyWidgetStates(self, states):
'widget2': {'value': 2, 'enabled': False, 'visible': False, 'widget_row': 1}}.
'''
if set(self.widgets) != set(states):
raise KeyError("states={set(states)} do not match form widgets ({set(self.widgets)})")
raise KeyError(f"states={set(states)} do not match form widgets ({set(self.widgets)})")
for key, widget_state in states.items():
name, role = self._getNameAndRoleFromKey(key)
self.applyWidgetState(name, widget_state, role)
Expand Down Expand Up @@ -488,10 +471,10 @@ def __init__(self, parent=None):


class FormDockWidget(QtWidgets.QDockWidget):
def __init__(self, parent=None, title=None):
def __init__(self, parent=None, title=''):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this default and accept title=None?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

honestly I'd prefer removing the lines below:

        if title is None:
            title = ''

but I left them in just for backwards compatibility.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

honestly I'd prefer removing the lines below:

        if title is None:
            title = ''

but I left them in just for backwards compatibility.

we added these lines in https://github.com/TomographicImaging/eqt/pull/102/files
to fix some problems. the default title = None was already set then.

if title is None:
title = ''
QtWidgets.QDockWidget.__init__(self, title, parent)
super().__init__(self, title, parent)
widget = FormWidget(parent)
self.setWidget(widget)
if title is not None:
Expand Down Expand Up @@ -542,7 +525,7 @@ def insertWidget(self, row, name, qwidget, qlabel=None):
The position in the form where the widget is added.
name : str
The string associated to the qwidget and qlabel.
qwidget : widget
qwidget : QWidget
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
Expand Down