def load_form(self, data: OrderedDict) -> None: """A dictionary to load in the UI.""" for key, definition in self.config.layer_config.items(): value = data.get(key) if definition['type'] == InputType.Layer: layer = QgsProject.instance().mapLayer(value) definition['widget'].setLayer(layer) elif definition['type'] == InputType.Field: definition['widget'].setField(value) elif definition['type'] == InputType.Fields: definition['widget'].set_selection(value.split(',')) elif definition['type'] == InputType.CheckBox: definition['widget'].setChecked(value) elif definition['type'] == InputType.Color: color = QColor(value) if color.isValid(): definition['widget'].setDefaultColor(color) definition['widget'].setColor(color) else: definition['widget'].setToNull() elif definition['type'] == InputType.List: index = definition['widget'].findData(value) definition['widget'].setCurrentIndex(index) elif definition['type'] == InputType.SpinBox: definition['widget'].setValue(value) elif definition['type'] == InputType.Text: definition['widget'].setText(value) else: raise Exception('InputType "{}" not implemented'.format( definition['type']))
def color_from_name(clr_name, default='#000000'): """ Creates a QColor object from the corresponding color code. """ clr = QColor() clr.setNamedColor(clr_name) if not clr.isValid(): clr.setNamedColor(default) return clr
def load_form(self, data: OrderedDict) -> None: """A dictionary to load in the UI.""" layer_properties = OrderedDict() for key, definition in self.config.layer_config.items(): if definition.get('plural') is None: layer_properties[key] = definition for key, definition in layer_properties.items(): value = data.get(key) if definition['type'] == InputType.Layer: layer = QgsProject.instance().mapLayer(value) definition['widget'].setLayer(layer) elif definition['type'] == InputType.Layers: definition['widget'].set_selection(value) elif definition['type'] == InputType.Field: definition['widget'].setField(value) elif definition['type'] == InputType.Fields: definition['widget'].set_selection(value.split(',')) elif definition['type'] == InputType.CheckBox: definition['widget'].setChecked(value) elif definition['type'] == InputType.Color: color = QColor(value) if color.isValid(): definition['widget'].setDefaultColor(color) definition['widget'].setColor(color) else: definition['widget'].setToNull() elif definition['type'] == InputType.List: index = definition['widget'].findData(value) definition['widget'].setCurrentIndex(index) elif definition['type'] == InputType.SpinBox: definition['widget'].setValue(value) elif definition['type'] == InputType.Text: definition['widget'].setText(value) elif definition['type'] == InputType.Json: if value: definition['widget'].setText(json.dumps(value)) elif definition['type'] == InputType.MultiLine: widget = definition['widget'] if isinstance(widget, QPlainTextEdit): widget.setPlainText(value) else: widget.setText(value) elif definition['type'] == InputType.Collection: self.load_collection(value) else: raise Exception('InputType "{}" not implemented'.format( definition['type'])) self.post_load_form()
def load_form(self, data: OrderedDict) -> None: """Load a dictionary into the form.""" for key in self.config.layer_config['traces']['items']: definition = self.config.layer_config[key] value = data.get(key) if definition['type'] == InputType.Field: definition['widget'].setField(value) elif definition['type'] == InputType.Color: color = QColor(value) if color.isValid(): definition['widget'].setDefaultColor(color) definition['widget'].setColor(color) else: definition['widget'].setToNull() else: raise Exception('InputType "{}" not implemented'.format( definition['type']))
def validateQColor(self, colorToEvaluate): """ This method receives a string and evaluate if its an valid QColor. """ colorStrWithoutSpaces = re.sub(r'\s', '', str(colorToEvaluate)) hexColor = re.search(r"^#[0-9a-fA-F]{3,6}$", colorStrWithoutSpaces) listColor = re.search(r"\d{1,3},\d{1,3},\d{1,3}", colorStrWithoutSpaces) if hexColor is not None: if QColor(hexColor[0]).isValid(): return True elif listColor is not None: color = listColor[0].split(",") rgb = QColor(int(color[0]), int(color[1]), int(color[2])) if rgb.isValid(): return True return False