How to use iface.actionVertexTool() in a pyqgis plugin with custom QgsMapCanvas?

89 views Asked by At

I need to edit features in my pyqgis plugin's QgsMapCanvas instantiated as self.canvas . I have enabled snapping options and triggered vertex tool via python code, but other than the cursor changing to CrossCursor, nothing happens when it hovers over the layer to be edited on the custom QgsMapCanvas . There is no QgsActionVertexTool which is similar to QgsActionPan. I feel that iface.actionVertexTool() might not be suitable because that might be only for the QgisInterface(), but am open for corrections.

Here is the code.

from qgis.core import QgsSnappingConfig, QgsTolerance, QgsMapLayer
from qgis.gui import QgsMapTool
from qgis.utils import iface

from .feedback import Feedback

class GeoManipulation():
    def __init__(self, canvas, project, view, console, button):
        super().__init__()
        self.canvas = canvas
        self.project = project
        self.view = view
        self.console_out = console
        self.gm_edit_button = button
        self.setSnapConfig()
        self.vertexEditor()
        
    def setSnapConfig(self):
        snap_config = QgsSnappingConfig()

        snap_config.setEnabled(True)
        snap_config.setMode(snap_config.SnappingMode.AllLayers)
        snap_config.setType(snap_config.Vertex)
        snap_config.setTolerance(12)
        snap_config.setUnits(QgsTolerance.Pixels)

        snap_config.setIntersectionSnapping(True)
        snap_config.setSelfSnapping(True)

        self.project.setTopologicalEditing(True)
        self.project.setSnappingConfig(snap_config)
        
    def vertexEditor(self):
        self.cur_lyr = self.view.currentLayer()
        
        vertex_action = iface.actionVertexTool()
        vertex_tool = QgsMapTool(self.canvas)
        vertex_tool.setAction(vertex_action)
        self.canvas.setMapTool(vertex_tool)
        
        if self.gm_edit_button.isChecked():
            if self.cur_lyr == None or self.cur_lyr.type() != QgsMapLayer.VectorLayer:
                Feedback().error(self.console_out, "Please Select an Asset Layer")
                self.gm_edit_button.setChecked(False)
            else:
                print(self.cur_lyr)
                self.cur_lyr.startEditing()
                # vertex_action.trigger()
                vertex_tool.activate()
                
        else:
            Feedback().success(self.console_out, "Edits to Current Layer Successfully Saved")
            self.cur_lyr.commitChanges()
            vertex_tool.deactivate()
            
            

I also tried with QgsMapToolEdit, but it did not work.

0

There are 0 answers