Commit 9fc8c523 authored by Samuel GAIST's avatar Samuel GAIST
Browse files

[widgets][parameterwidget] Improve widget size handling

The widget had much empty space because the QStackedWidget
used to contain the editors calculates the size based on
the biggest widget within it. This patch changes that
so that we can adjust the size of the QStackedWidget to
be more compact whathever the current widget shown.
parent 2ae9a636
Pipeline #31576 passed with stage
in 25 minutes and 17 seconds
......@@ -88,6 +88,37 @@ class InputType(Enum):
return info
class StackedWidget(QStackedWidget):
"""Subclass that will use the current widget height to adjust the stack
size.
This allows to keep a compact widget.
"""
def __init__(self, parent=None):
super(StackedWidget, self).__init__(parent)
def sizeHint(self):
"""Returns a size hint based on the current widget in place of the
biggest of all widgets added.
"""
size = super(StackedWidget, self).size()
current_size_hint = self.currentWidget().sizeHint()
size.setHeight(current_size_hint.height())
return size
def minimumSizeHint(self):
"""Returns a minimum size hint base on the current widget in place of
the biggest of all widgets added.
"""
size = super(StackedWidget, self).size()
current_size_hint = self.currentWidget().minimumSizeHint()
size.setHeight(current_size_hint.height())
return size
class ChoicesDialog(QDialog):
"""Dialog to retrieve a value to to add to a choice list"""
......@@ -192,6 +223,7 @@ class StringSetupWidget(QWidget):
"""Editor to setup a string parameter"""
dataChanged = pyqtSignal()
editorChanged = pyqtSignal()
def __init__(self, parent=None):
"""Constructor
......@@ -229,7 +261,7 @@ class StringSetupWidget(QWidget):
self.choices_conf_widget = QWidget()
# Stacked widget
self.stacked_widget = QStackedWidget()
self.stacked_widget = StackedWidget()
self.stacked_widget.addWidget(self.single_conf_widget)
self.stacked_widget.addWidget(self.choices_conf_widget)
......@@ -257,6 +289,9 @@ class StringSetupWidget(QWidget):
choices_model.rowsInserted.connect(self.dataChanged)
choices_model.rowsRemoved.connect(self.dataChanged)
self.stacked_widget.currentChanged.connect(lambda: self.adjustSize())
self.stacked_widget.currentChanged.connect(self.editorChanged)
def load(self, data):
"""Load the json object passed as parameter"""
......@@ -364,6 +399,7 @@ class NumericalSetupWidget(QWidget):
"""Editor to setup a numerical parameter"""
dataChanged = pyqtSignal()
editorChanged = pyqtSignal()
def __init__(self, selected_type, parent=None):
"""Constructor
......@@ -428,7 +464,7 @@ class NumericalSetupWidget(QWidget):
self.range_conf_widget = QWidget()
# Stacked widget
self.stacked_widget = QStackedWidget(self)
self.stacked_widget = StackedWidget(self)
self.stacked_widget.addWidget(self.single_conf_widget)
self.stacked_widget.addWidget(self.choices_conf_widget)
self.stacked_widget.addWidget(self.range_conf_widget)
......@@ -467,6 +503,8 @@ class NumericalSetupWidget(QWidget):
self.range_default_spinbox.valueChanged.connect(self.dataChanged)
self.range_minimum_spinbox.valueChanged.connect(self.dataChanged)
self.range_maximum_spinbox.valueChanged.connect(self.dataChanged)
self.stacked_widget.currentChanged.connect(lambda: self.adjustSize())
self.stacked_widget.currentChanged.connect(self.editorChanged)
def load(self, data):
"""Load the json object passed as parameter"""
......@@ -707,8 +745,7 @@ class ParameterWidget(QWidget):
self.basetype_comboxbox = QComboBox()
self.basetype_comboxbox.addItems(basetypes)
self.selected_type = InputType[basetypes[0].upper()]
self.setup_stackedwidget = QStackedWidget()
self.setup_stackedwidget.currentChanged.connect(lambda: self.adjustSize())
self.setup_stackedwidget = StackedWidget()
# Stacked widgets
self.numerical_setup_widget = NumericalSetupWidget(self.selected_type)
......@@ -729,24 +766,32 @@ class ParameterWidget(QWidget):
self.basetype_comboxbox.currentIndexChanged.connect(self.__onTypeWidgetChanged)
self.setup_stackedwidget.currentChanged.connect(self.dataChanged)
self.setup_stackedwidget.currentChanged.connect(lambda: self.adjustSize())
self.numerical_setup_widget.dataChanged.connect(self.dataChanged)
self.numerical_setup_widget.editorChanged.connect(lambda: self.adjustSize())
self.string_setup_widget.dataChanged.connect(self.dataChanged)
self.string_setup_widget.editorChanged.connect(lambda: self.adjustSize())
self.bool_setup_widget.dataChanged.connect(self.dataChanged)
@pyqtSlot()
def __onTypeWidgetChanged(self):
self.selected_type = InputType[self.basetype_comboxbox.currentText().upper()]
selected_widget = None
if self.selected_type == InputType.BOOL:
self.bool_setup_widget.reset()
self.setup_stackedwidget.setCurrentWidget(self.bool_setup_widget)
selected_widget = self.bool_setup_widget
elif self.selected_type == InputType.STRING:
self.string_setup_widget.reset()
self.setup_stackedwidget.setCurrentWidget(self.string_setup_widget)
selected_widget = self.string_setup_widget
else:
self.numerical_setup_widget.reset(self.selected_type)
self.setup_stackedwidget.setCurrentWidget(self.numerical_setup_widget)
selected_widget = self.numerical_setup_widget
self.setup_stackedwidget.setCurrentWidget(selected_widget)
def dump(self):
"""Returns the json representation of this editor"""
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment