Skip to content

Commit

Permalink
Merge branch 'graph_select_multiple_features' into refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
benvanbasten-ns committed Sep 4, 2023
2 parents b66b5f2 + 7a74518 commit 3ba9c8f
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 13 deletions.
2 changes: 1 addition & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
3.1.8 (unreleased)
------------------

- Nothing changed yet.
- Graph: added option to add multiple selected features at once (#888)


3.1.7 (2023-08-21)
Expand Down
89 changes: 77 additions & 12 deletions tool_graph/graph_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from qgis.PyQt.QtWidgets import QDockWidget
from qgis.PyQt.QtWidgets import QHBoxLayout
from qgis.PyQt.QtWidgets import QMessageBox
from qgis.PyQt.QtWidgets import QPushButton
from qgis.PyQt.QtWidgets import QActionGroup, QToolButton
from qgis.PyQt.QtWidgets import QSizePolicy
from qgis.PyQt.QtWidgets import QSpacerItem
from qgis.PyQt.QtWidgets import QTableView
Expand Down Expand Up @@ -905,12 +905,42 @@ def setup_ui(self):

self.buttonBarHLayout.setSpacing(10)

self.addFlowlinePumpButton = QPushButton(text="Add flowlines/pumps", parent=self.dockWidgetContent)
selection_flowline_pump_menu = QMenu(self)
action_group = QActionGroup(self)
action_group.setExclusive(True)
self.flowline_single_pick = selection_flowline_pump_menu.addAction("Pick single flowline/pump")
self.flowline_single_pick.setCheckable(True)
self.flowline_single_pick.setChecked(True)
self.flowline_single_pick.toggled.connect(self._changeFlowlineSelectionMode)
action_group.addAction(self.flowline_single_pick)
selected_pick = selection_flowline_pump_menu.addAction("Add all selected flowlines/pump")
selected_pick.setCheckable(True)
action_group.addAction(selected_pick)

selection_node_cell_menu = QMenu(self)
action_group = QActionGroup(self)
action_group.setExclusive(True)
self.node_single_pick = selection_node_cell_menu.addAction("Pick single node/cell")
self.node_single_pick.setCheckable(True)
self.node_single_pick.setChecked(True)
self.node_single_pick.toggled.connect(self._changeNodeCellSelectionMode)
action_group.addAction(self.node_single_pick)
selected_pick = selection_node_cell_menu.addAction("Add all selected nodes/cells")
selected_pick.setCheckable(True)
action_group.addAction(selected_pick)

self.addFlowlinePumpButton = QToolButton(parent=self.dockWidgetContent)
self.addFlowlinePumpButton.setCheckable(True)
self.addFlowlinePumpButton.setText("Pick flowlines/pumps")
self.addFlowlinePumpButton.setPopupMode(QToolButton.MenuButtonPopup)
self.addFlowlinePumpButton.setMenu(selection_flowline_pump_menu)
self.buttonBarHLayout.addWidget(self.addFlowlinePumpButton)

self.addNodeCellButton = QPushButton(text="Add nodes/cells", parent=self.dockWidgetContent)
self.addNodeCellButton = QToolButton(parent=self.dockWidgetContent)
self.addNodeCellButton.setText("Pick nodes/cells")
self.addNodeCellButton.setCheckable(True)
self.addNodeCellButton.setPopupMode(QToolButton.MenuButtonPopup)
self.addNodeCellButton.setMenu(selection_node_cell_menu)
self.buttonBarHLayout.addWidget(self.addNodeCellButton)

self.absoluteCheckbox = QCheckBox("Absolute", parent=self.dockWidgetContent)
Expand Down Expand Up @@ -940,26 +970,60 @@ def setup_ui(self):
self.setWindowTitle("3Di Time Series Plot %i" % self.nr)
QMetaObject.connectSlotsByName(self)

def _changeFlowlineSelectionMode(self, single_pick_selected: bool) -> None:
if not single_pick_selected:
if self.iface.mapCanvas().mapTool() is self.map_tool_add_flowline_pump:
self.iface.mapCanvas().unsetMapTool(self.map_tool_add_flowline_pump)
self.addFlowlinePumpButton.setCheckable(False)
self.addFlowlinePumpButton.setText("Add flowlines/pumps")
else:
self.addFlowlinePumpButton.setCheckable(True)
self.addFlowlinePumpButton.setText("Pick flowlines/pumps")

def _changeNodeCellSelectionMode(self, single_pick_selected: bool) -> None:
if not single_pick_selected:
if self.iface.mapCanvas().mapTool() is self.map_tool_add_node_cell:
self.iface.mapCanvas().unsetMapTool(self.map_tool_add_node_cell)
self.addNodeCellButton.setCheckable(False)
self.addNodeCellButton.setText("Add nodes/cells")
else:
self.addNodeCellButton.setCheckable(True)
self.addNodeCellButton.setText("Pick nodes/cells")

def add_flowline_pump_button_clicked(self):
self.iface.mapCanvas().setMapTool(
self.map_tool_add_flowline_pump,
)
if self.flowline_single_pick.isChecked():
self.iface.mapCanvas().setMapTool(self.map_tool_add_flowline_pump)
else:
current_layer = self.iface.mapCanvas().currentLayer()
if not current_layer or current_layer.objectName() not in ['flowline', 'pump_linestring', 'pump']:
logger.error("Select features from flowline or pump layer first.")
return

self.add_results([QgsMapToolIdentify.IdentifyResult(current_layer, f, dict()) for f in current_layer.selectedFeatures()], feature_type=FLOWLINE_OR_PUMP)

def add_node_cell_button_clicked(self):
self.iface.mapCanvas().setMapTool(
self.map_tool_add_node_cell,
)
if self.node_single_pick.isChecked():
self.iface.mapCanvas().setMapTool(self.map_tool_add_node_cell)
else:
current_layer = self.iface.mapCanvas().currentLayer()

if not current_layer or current_layer.objectName() not in ['node', 'cell']:
logger.error("Select features from node or cell layer first.")
return

self.add_results([QgsMapToolIdentify.IdentifyResult(current_layer, f, dict()) for f in current_layer.selectedFeatures()], feature_type=NODE_OR_CELL)

def unset_map_tools(self):
if self.iface.mapCanvas().mapTool() is self.map_tool_add_node_cell:
self.iface.mapCanvas().unsetMapTool(self.map_tool_add_node_cell)
elif self.iface.mapCanvas().mapTool() is self.map_tool_add_flowline_pump:
self.iface.mapCanvas().unsetMapTool(self.map_tool_add_flowline_pump)

def add_results(self, results, feature_type):
def add_results(self, results, feature_type, single_feature_per_layer=False):
"""
Add results for features of specific types.
"""
logger.error(len(results))
if feature_type == FLOWLINE_OR_PUMP:
layer_keys = ['flowline', 'pump_linestring', 'pump']
graph_widget = self.q_graph_widget
Expand All @@ -979,8 +1043,9 @@ def add_results(self, results, feature_type):
layer_id = result.mLayer.id()
if layer_id not in relevant_grid_layer_ids:
continue
if layer_id in layers_added:
if single_feature_per_layer and layer_id in layers_added:
continue
logger.error(f"Adding result {result.mFeature.id()}")
graph_widget.add_objects(result.mLayer, [result.mFeature])
layers_added.add(layer_id)

Expand All @@ -1002,7 +1067,7 @@ def canvasReleaseEvent(self, event):
layerList=self.parent().layers(),
)
self.widget.add_results(
results=results, feature_type=self.feature_type,
results=results, feature_type=self.feature_type, single_feature_per_layer=True
)


Expand Down

0 comments on commit 3ba9c8f

Please sign in to comment.