1

I have a custom tool to add feature points. The attribute values are provided filling a custom dialog. The code was writted following this topic.

At the end of the procedure, outside the tool, if I select a different layer and I start editing with toggle editing of QGIS (without selecting add point/polygon feature tool), I continue editing with the custom tool as if it didn't end the procedure.

How I can stop the editing session on the custom tool? This is the code:

class casePicker(QgsMapTool):       
    afterClick = pyqtSignal()

    def __init__(self, dlg, psrid, iface, tt, lab, lyr):
        QgsMapTool.__init__(self, iface.mapCanvas())
        self.iface = iface
        self.canvas = self.iface.mapCanvas()
        self.dlg = dlg
        self.psrid = psrid
        self.tt = tt
        self.lab = lab
        self.lyr = lyr
        self.funcs = qvfuncs.VetEpiGISFuncs()
        self.canvas.setCursor(mutato)

    def canvasPressEvent(self, event):
        pass

    def canvasMoveEvent(self, event):
        pass

    def canvasReleaseEvent(self, event):
        x = event.pos().x()
        y = event.pos().y()
        pt = self.canvas.getCoordinateTransform().toMapCoordinates(x, y)
        x = pt.x()
        y = pt.y()

        if self.dlg.exec_() == QDialog.Accepted:
            QApplication.setOverrideCursor(Qt.WaitCursor)
            x = float(self.dlg.lineEdit.text())
            y = float(self.dlg.lineEdit_2.text())
            self.addFeat(x, y)
            self.tt.setChecked(False)
            QApplication.restoreOverrideCursor()

    def addFeat(self, x, y):
        self.lyr.startEditing()
        feat = QgsFeature()
        feat = self.funcs.outattrPrep(self.dlg, self.lyr)
        pnt = QgsGeometry.fromPointXY(QgsPointXY(x,y))
        if self.lyr.geometryType() == QgsWkbTypes.PointGeometry:
            feat.setGeometry(pnt)       
        else:
            pass
        feat.setValid(True)
        self.lyr.addFeature(feat)
        self.lyr.commitChanges()
        self.lyr.updateExtents()

    def activate(self):
        pass

    def deactivate(self):
        pass

    def isZoomTool(self):
        return False

    def isTransient(self):
        return False

    def isEditTool(self):
        return True
3
  • What about sending a signal so that when another layer is clicked, QGIS prompts the user to save/discard the changes made to the initial layer? This question was asked recently.
    – Joseph
    Commented Nov 21, 2019 at 15:27
  • 1
    Joseph I tried to follow your suggestion. I suppose it isn't a correct solution (I'm not so able with python) but I obtained something that works activating the pan tool after the editing. In init I added the code self.iface.currentLayerChanged.connect(self.setPanTool) and the method setPanTool(self) contains self.iface.actionPan().trigger(). At the end of the method def canvasReleaseEvent(self, event): I added self.iface.actionPan().trigger(). Other solutions are welcome.
    – PaolaB
    Commented Nov 22, 2019 at 11:21
  • Glad you found a solution, please consider posting this as an answer as this could help others with similar problems :)
    – Joseph
    Commented Nov 22, 2019 at 11:22

1 Answer 1

1

As I wrote in the comment, I obtained something that works, activating the pan tool after the editing. I'm not sure if it is the correct method. I'm writing only the added code:

class casePicker(QgsMapTool): 
    ... 

    def __init__(self, dlg, psrid, iface, tt, lab, lyr):
       ...
       self.iface.currentLayerChanged.connect(self.setPanTool)

    def setPanTool(self):
        self.iface.actionPan().trigger()

    def canvasReleaseEvent(self, event):
        ...
        self.iface.actionPan().trigger()

Other solutions are welcome.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.