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
PaolaB
  • 287
  • 4
  • 14
  • 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 Nov 21 '19 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 Nov 22 '19 at 11:21
  • Glad you found a solution, please consider posting this as an answer as this could help others with similar problems :) – Joseph Nov 22 '19 at 11:22

1 Answers1

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.

PaolaB
  • 287
  • 4
  • 14