diff --git a/dexbot/__init__.py b/dexbot/__init__.py index e257fd117..5ac261b83 100644 --- a/dexbot/__init__.py +++ b/dexbot/__init__.py @@ -1,4 +1,4 @@ APP_NAME = 'dexbot' -VERSION = '0.5.5' +VERSION = '0.5.6' AUTHOR = 'Codaone Oy' __version__ = VERSION diff --git a/dexbot/controllers/strategy_controller.py b/dexbot/controllers/strategy_controller.py index 70cc9e0c2..c4ff42275 100644 --- a/dexbot/controllers/strategy_controller.py +++ b/dexbot/controllers/strategy_controller.py @@ -28,8 +28,8 @@ def set_values(self, configure, worker_config): else: value = option.default - element = self.elements[option.key] - if not element: + element = self.elements.get(option.key) + if element is None: continue if option.type in ('int', 'float', 'string'): @@ -62,7 +62,7 @@ def values(self): @property def elements(self): - """ Use ConfigElement of the strategy to find the elements + """ Use ConfigElements of the strategy to find the input elements """ elements = {} types = ( @@ -75,7 +75,9 @@ def elements(self): for option in self.configure: element_name = ''.join([option.key, '_input']) - elements[option.key] = self.view.findChild(types, element_name) + element = self.view.findChild(types, element_name) + if element is not None: + elements[option.key] = element return elements diff --git a/dexbot/views/strategy_form.py b/dexbot/views/strategy_form.py index 7ce45ca76..981937159 100644 --- a/dexbot/views/strategy_form.py +++ b/dexbot/views/strategy_form.py @@ -17,7 +17,7 @@ def __init__(self, controller, strategy_module, worker_config=None): 'Strategy' ) configure = strategy_class.configure(False) - form_module = controller.strategies[strategy_module]['form_module'] + form_module = controller.strategies[strategy_module].get('form_module') try: widget = getattr( importlib.import_module(form_module), @@ -27,7 +27,7 @@ def __init__(self, controller, strategy_module, worker_config=None): self.strategy_widget.setupUi(self) except (ValueError, AttributeError): # Generate the strategy form widget automatically - self.strategy_widget = AutoStrategyFormGenerator(self, configure, worker_config) + self.strategy_widget = AutoStrategyFormGenerator(self, configure) # Assemble the controller class name parts = self.module_name.split('_') @@ -51,7 +51,7 @@ def __init__(self, controller, strategy_module, worker_config=None): @property def values(self): - """ Returns all the form values based on selected strategy + """ Returns all the form values based on the selected strategy """ return self.strategy_controller.values @@ -60,7 +60,8 @@ class AutoStrategyFormGenerator: """ Automatic strategy form UI generator """ - def __init__(self, view, configure, worker_config): + def __init__(self, view, configure): + self.view = view self.index = 0 self.elements = {} @@ -75,41 +76,54 @@ def __init__(self, view, configure, worker_config): for option in configure: self.add_element(option) + def __getattr__(self, item): + element = self.view.findChild(QtWidgets.QWidget, item) + if element is None: + raise AttributeError + return element + def add_element(self, option): + key = option.key + element_type = option.type + title = option.title + description = option.description extra = option.extra - if option.type == 'float': + + if element_type == 'float': element = self.add_double_spin_box( - option.title, extra[0], extra[1], extra[2], extra[3], option.description) - elif option.type == 'int': + key, title, extra[0], extra[1], extra[2], extra[3], description) + elif element_type == 'int': element = self.add_spin_box( - option.title, extra[0], extra[1], extra[2], option.description) - elif option.type == 'string': - element = self.add_line_edit(option.title, option.description) - elif option.type == 'bool': - element = self.add_checkbox(option.title, option.description) - elif option.type == 'choice': - element = self.add_combo_box(option.title, option.description) + key, title, extra[0], extra[1], extra[2], description) + elif element_type == 'string': + element = self.add_line_edit(key, title, description) + elif element_type == 'bool': + element = self.add_checkbox(key, title, description) + elif element_type == 'choice': + element = self.add_combo_box(key, title, description) else: return - element_name = ''.join([option.key, '_input']) + element_name = ''.join([key, '_input']) element.setObjectName(element_name) + self.elements[key] = element self.index += 1 - self.elements[option.key] = element - def _add_label_wrap(self): + def _add_label_wrap(self, key): wrap = QtWidgets.QWidget(self.group_box) - wrap.setMinimumSize(QtCore.QSize(110, 0)) - wrap.setMaximumSize(QtCore.QSize(110, 16777215)) + wrap.setMinimumSize(QtCore.QSize(120, 0)) + wrap.setMaximumSize(QtCore.QSize(120, 16777215)) layout = QtWidgets.QHBoxLayout(wrap) layout.setContentsMargins(0, 0, 0, 0) layout.setSpacing(0) + element_name = ''.join([key, '_wrap']) + wrap.setObjectName(element_name) self.form_layout.setWidget(self.index, QtWidgets.QFormLayout.LabelRole, wrap) return wrap - def _add_tooltip(self, tooltip_text, container): + def _add_tooltip(self, key, tooltip_text, container): tooltip = QtWidgets.QLabel(container) font = QtGui.QFont() font.setBold(True) @@ -122,26 +136,30 @@ def _add_tooltip(self, tooltip_text, container): tooltip.setText('?') tooltip.setToolTip(tooltip_text) + element_name = ''.join([key, '_tooltip']) + tooltip.setObjectName(element_name) layout = container.layout() layout.addWidget(tooltip) - def add_label(self, text, description=''): - wrap = self._add_label_wrap() + def add_label(self, key, text, description=''): + wrap = self._add_label_wrap(key) label = QtWidgets.QLabel(wrap) label.setWordWrap(True) size_policy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Fixed) label.setSizePolicy(size_policy) + element_name = ''.join([key, '_label']) + label.setObjectName(element_name) layout = wrap.layout() layout.addWidget(label) label.setText(text) if description: - self._add_tooltip(description, wrap) + self._add_tooltip(key, description, wrap) - def add_double_spin_box(self, text, minimum, maximum, precision, suffix='', description=''): - self.add_label(text, description) + def add_double_spin_box(self, key, text, minimum, maximum, precision, suffix='', description=''): + self.add_label(key, text, description) input_field = QtWidgets.QDoubleSpinBox(self.group_box) input_field.setDecimals(precision) @@ -161,8 +179,8 @@ def add_double_spin_box(self, text, minimum, maximum, precision, suffix='', desc self.form_layout.setWidget(self.index, QtWidgets.QFormLayout.FieldRole, input_field) return input_field - def add_spin_box(self, text, minimum, maximum, suffix='', description=''): - self.add_label(text, description) + def add_spin_box(self, key, text, minimum, maximum, suffix='', description=''): + self.add_label(key, text, description) input_field = QtWidgets.QSpinBox(self.group_box) if minimum is not None: @@ -180,15 +198,15 @@ def add_spin_box(self, text, minimum, maximum, suffix='', description=''): self.form_layout.setWidget(self.index, QtWidgets.QFormLayout.FieldRole, input_field) return input_field - def add_line_edit(self, text, description=''): - self.add_label(text, description) + def add_line_edit(self, key, text, description=''): + self.add_label(key, text, description) input_field = QtWidgets.QLineEdit(self.group_box) self.form_layout.setWidget(self.index, QtWidgets.QFormLayout.FieldRole, input_field) return input_field - def add_checkbox(self, text, description=''): - self.add_label('', description) + def add_checkbox(self, key, text, description=''): + self.add_label(key, '', description) input_field = QtWidgets.QCheckBox(self.group_box) input_field.setText(text) @@ -196,8 +214,8 @@ def add_checkbox(self, text, description=''): self.form_layout.setWidget(self.index, QtWidgets.QFormLayout.FieldRole, input_field) return input_field - def add_combo_box(self, text, description=''): - self.add_label(text, description) + def add_combo_box(self, key, text, description=''): + self.add_label(key, text, description) input_field = QtWidgets.QComboBox(self.group_box) size_policy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)