Skip to content

Commit

Permalink
Allow setting edge_type for Edge in QGIS.
Browse files Browse the repository at this point in the history
  • Loading branch information
evetion committed Aug 10, 2023
1 parent 178ac72 commit 2e4e8e9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
24 changes: 16 additions & 8 deletions qgis/core/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,21 @@ class Edge(Input):
def is_spatial(cls):
return True

def set_editor_widget(self) -> None:
layer = self.layer
index = layer.fields().indexFromName("edge_type")
setup = QgsEditorWidgetSetup(
"ValueMap",
{"map": {node: node for node in EDGETYPES}},
)
layer.setEditorWidgetSetup(index, setup)

layer_form_config = layer.editFormConfig()
layer_form_config.setReuseLastValue(1, True)
layer.setEditFormConfig(layer_form_config)

return

@property
def renderer(self) -> QgsCategorizedSymbolRenderer:
MARKERS = {
Expand Down Expand Up @@ -264,14 +279,6 @@ def renderer(self) -> QgsCategorizedSymbolRenderer:
)
return renderer

def set_read_only(self) -> None:
layer = self.layer
config = layer.editFormConfig()
for index in range(len(layer.fields())):
config.setReadOnly(index, True)
layer.setEditFormConfig(config)
return


class BasinProfile(Input):
input_type = "Basin / profile"
Expand Down Expand Up @@ -447,6 +454,7 @@ class PidControlStatic(Input):
NONSPATIALNODETYPES = {
cls.nodetype() for cls in Input.__subclasses__() if not cls.is_spatial()
}
EDGETYPES = {"flow", "control"}


def load_nodes_from_geopackage(path: str) -> Dict[str, Input]:
Expand Down
13 changes: 3 additions & 10 deletions qgis/widgets/dataset_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ def explode_and_connect(self) -> None:
fields = edge.fields()
field1 = fields.indexFromName("from_node_id")
field2 = fields.indexFromName("to_node_id")
field3 = fields.indexFromName("edge_type")
try:
# Avoid infinite recursion
edge.blockSignals(True)
Expand All @@ -201,7 +200,7 @@ def explode_and_connect(self) -> None:
# Nota bene: will fail with numpy integers, has to be Python type!
edge.changeAttributeValue(fid, field1, int(id1))
edge.changeAttributeValue(fid, field2, int(id2))
edge.changeAttributeValue(fid, field3, "flow")

finally:
edge.blockSignals(False)

Expand All @@ -212,7 +211,7 @@ def add_layer(
layer: Any,
destination: Any,
renderer: Any = None,
suppress: bool = None,
suppress: bool = False,
on_top: bool = False,
labels: Any = None,
) -> QgsMapLayer:
Expand All @@ -229,8 +228,6 @@ def add_item_to_qgis(self, item) -> None:
element = item.element
layer, renderer, labels = element.from_geopackage()
suppress = self.suppress_popup_checkbox.isChecked()
if element.input_type == "Edge":
suppress = True
self.add_layer(layer, "Ribasim Input", renderer, suppress, labels=labels)
element.set_editor_widget()
element.set_read_only()
Expand Down Expand Up @@ -294,11 +291,7 @@ def suppress_popup_changed(self):
layer = item.element.layer
if layer is not None:
config = layer.editFormConfig()
# Always suppress the attribute form pop-up for edges.
if item.element.input_type == "Edge":
config.setSuppress(True)
else:
config.setSuppress(suppress)
config.setSuppress(suppress)
layer.setEditFormConfig(config)

def active_nodes(self):
Expand Down

0 comments on commit 2e4e8e9

Please sign in to comment.