Skip to content

Commit

Permalink
Multiple schematisation handling enhancements (#259)
Browse files Browse the repository at this point in the history
* Fix for the issue #257.

* docker-compose -> docker compose

* Added active schematisation combobox.

---------

Co-authored-by: leendertvanwolfswinkel <[email protected]>
  • Loading branch information
ldebek and leendertvanwolfswinkel authored Sep 23, 2024
1 parent 8ac3fb7 commit 43da745
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 18 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ jobs:

- name: build
run: |
docker-compose build schema
docker-compose run schema make test
docker-compose run schema make zip
docker compose build schema
docker compose run schema make test
docker compose run schema make zip
- name: Publish package
if: startsWith(github.event.ref, 'refs/tags')
Expand Down
2 changes: 1 addition & 1 deletion HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ History
1.12.1 (unreleased)
-------------------

- Fixes/enhancements: #116
- Fixes/enhancements: #116, #250, #257
- Added handling of the multiple schematisations.
- Implemented import of the linear structures from the point datasets (#167).

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ Development

Testing happens within a docker container, build (if necessary) and run your docker as follows::

$ docker-compose build schema
$ docker-compose run schema make test
$ docker compose build schema
$ docker compose run schema make test


Deployment
Expand Down
52 changes: 44 additions & 8 deletions threedi_schematisation_editor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from qgis.core import QgsApplication, QgsLayerTreeNode, QgsProject
from qgis.PyQt.QtGui import QCursor, QIcon
from qgis.PyQt.QtWidgets import QAction, QDialog, QMenu
from qgis.PyQt.QtWidgets import QAction, QComboBox, QDialog, QMenu

import threedi_schematisation_editor.data_models as dm
from threedi_schematisation_editor.communication import UICommunication
Expand Down Expand Up @@ -38,14 +38,15 @@ class ThreediSchematisationEditorPlugin:
def __init__(self, iface):
self.iface = iface
self.uc = UICommunication(self.iface, self.PLUGIN_NAME)
self.active_schematisation_combo = None
self.toolbar = None
self.action_open = None
self.action_import = None
self.action_export = None
self.action_export_as = None
self.action_remove = None
self.action_import_culverts = None
self.workspace_context_manager = WorkspaceContextManager()
self.workspace_context_manager = WorkspaceContextManager(self)
self.provider = ThreediSchematisationEditorProcessingProvider()
self.project = QgsProject.instance()
self.project.removeAll.connect(self.on_project_close)
Expand All @@ -56,6 +57,11 @@ def __init__(self, iface):
def initGui(self):
QgsApplication.processingRegistry().addProvider(self.provider)
self.toolbar = self.iface.addToolBar("Schematisation Editor")
self.active_schematisation_combo = QComboBox()
self.active_schematisation_combo.setPlaceholderText("No active schematisation")
self.active_schematisation_combo.currentIndexChanged.connect(self.active_schematisation_changed)
self.toolbar.addWidget(self.active_schematisation_combo)
self.toolbar.addSeparator()
self.action_open = QAction("Open 3Di Geopackage", self.iface.mainWindow())
self.action_open.triggered.connect(self.open_model_from_geopackage)
self.action_import = QAction("Load from Spatialite", self.iface.mainWindow())
Expand Down Expand Up @@ -84,10 +90,13 @@ def initGui(self):
self.toolbar.addAction(self.action_remove)
self.toolbar.addAction(self.action_import_culverts)
self.toggle_active_project_actions()
self.active_schematisation_changed()

def unload(self):
QgsApplication.processingRegistry().removeProvider(self.provider)
self.active_schematisation_combo.currentIndexChanged.disconnect(self.active_schematisation_changed)
del self.toolbar
del self.active_schematisation_combo
del self.action_open
del self.action_import
del self.action_export
Expand All @@ -107,14 +116,32 @@ def layer_manager(self):
def model_layers_map(self):
model_layers = defaultdict(set)
root_node = QgsProject.instance().layerTreeRoot()
for node in root_node.children():
if not (node.nodeType() == QgsLayerTreeNode.NodeType.NodeGroup and node.name().startswith("3Di model:")):
model_nodes = [
node
for node in root_node.children()
if node.nodeType() == QgsLayerTreeNode.NodeType.NodeGroup and node.name().startswith("3Di model:")
]
for model_node in model_nodes:
model_groups = {
node.name(): node
for node in model_node.children()
if node.nodeType() == QgsLayerTreeNode.NodeType.NodeGroup
}
try:
group_1d_node = model_groups["1D"]
except KeyError:
continue
layer_1d_nodes = {
node.name(): node
for node in group_1d_node.children()
if node.nodeType() == QgsLayerTreeNode.NodeType.NodeLayer
}
connection_node_tree_layer = layer_1d_nodes[dm.ConnectionNode.__layername__]
if connection_node_tree_layer is None:
continue
connection_node_tree_layer = node.children()[0].children()[0]
connection_node_layer = connection_node_tree_layer.layer()
model_gpkg = os.path.normpath(connection_node_layer.source().rsplit("|", 1)[0])
sub_groups = [n for n in node.children() if n.nodeType() == QgsLayerTreeNode.NodeType.NodeGroup]
for sub_grp in sub_groups:
for sub_grp in model_groups.values():
for nested_node in sub_grp.children():
if nested_node.nodeType() == QgsLayerTreeNode.NodeType.NodeLayer:
model_layer = nested_node.layer()
Expand All @@ -133,6 +160,16 @@ def switch_workspace_context(self, active_layer):
if lm.model_gpkg_path != self.model_gpkg:
self.workspace_context_manager.set_active_layer_manager(lm)

def active_schematisation_changed(self):
combo_model_gpkg = self.active_schematisation_combo.currentData()
if self.model_gpkg != combo_model_gpkg:
lm = self.workspace_context_manager.layer_managers[combo_model_gpkg]
self.iface.setActiveLayer(lm.model_handlers[dm.ConnectionNode].layer)
if self.model_gpkg is not None:
self.active_schematisation_combo.setToolTip(f"Currently active schematisation: {self.model_gpkg}")
else:
self.active_schematisation_combo.setToolTip("No active schematisation")

def add_multi_action_button(self, name, icon_path, actions_specification):
parent_window = self.iface.mainWindow()
action_arguments = [name, parent_window]
Expand Down Expand Up @@ -185,7 +222,6 @@ def select_sqlite_database(self, title):
return filename

def on_3di_project_read(self):
self.action_export.setDisabled(True)
custom_vars = self.project.customVariables()
try:
project_model_gpkgs_str = custom_vars[self.THREEDI_GPKG_VAR_NAMES]
Expand Down
2 changes: 1 addition & 1 deletion threedi_schematisation_editor/metadata.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ about=View and edit 3Di schematisations in the Modeller Interface.
tracker=https://github.com/nens/threedi-schematisation-editor/issues
repository=https://github.com/nens/threedi-schematisation-editor/issues

changelog=https://github.com/nens/threedi-schematisation-editor/blob/master/HISTORY.rst
changelog=https://docs.3di.live/a_releasenotes_3di_mi.html

# Tags are comma separated with spaces allowed
tags=3di, edit, 1D, hydraulics
Expand Down
8 changes: 6 additions & 2 deletions threedi_schematisation_editor/user_layer_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,16 @@ def common_editing_group(self):
)
return linked_models

@property
def model_name(self):
"""Name of the model."""
return os.path.basename(self.model_gpkg_path).rsplit(".", 1)[0]

@property
def main_group(self):
"""Main model group."""
model_file_dir = os.path.basename(os.path.dirname(self.model_gpkg_path))
model_name = os.path.basename(self.model_gpkg_path).rsplit(".", 1)[0]
model_group_name = f"3Di model: {model_file_dir}/{model_name}"
model_group_name = f"3Di model: {model_file_dir}/{self.model_name}"
return model_group_name

@property
Expand Down
9 changes: 8 additions & 1 deletion threedi_schematisation_editor/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
class WorkspaceContextManager:
"""Class with methods for managing 3Di model schematisation context."""

def __init__(self):
def __init__(self, plugin):
self.plugin = plugin
self._active_lm_gpkg = None
self.layer_managers = {}

Expand Down Expand Up @@ -43,6 +44,7 @@ def register_layer_manager(self, layer_manager, set_active=True):
"""Register a Layer Manager instance."""
if layer_manager not in self:
self.layer_managers[layer_manager.model_gpkg_path] = layer_manager
self.plugin.active_schematisation_combo.addItem(layer_manager.model_name, layer_manager.model_gpkg_path)
if set_active:
self.set_active_layer_manager(layer_manager)

Expand All @@ -52,14 +54,19 @@ def unregister_layer_manager(self, layer_manager):
if layer_manager == self.active_layer_manager:
del self.active_layer_manager
del self.layer_managers[layer_manager.model_gpkg_path]
lm_idx = self.plugin.active_schematisation_combo.findData(layer_manager.model_gpkg_path)
self.plugin.active_schematisation_combo.removeItem(lm_idx)
if self.layer_managers:
self.set_active_layer_manager(next(iter(self.layer_managers.values())))

def unregister_all(self):
"""Unregister all Layer Manager instances."""
del self.active_layer_manager
self.layer_managers.clear()
self.plugin.active_schematisation_combo.clear()

def set_active_layer_manager(self, layer_manager):
"""Set the active Layer Manager instance."""
self.active_layer_manager = layer_manager
lm_idx = self.plugin.active_schematisation_combo.findData(layer_manager.model_gpkg_path)
self.plugin.active_schematisation_combo.setCurrentIndex(lm_idx)

0 comments on commit 43da745

Please sign in to comment.