Graphics Reference
In-Depth Information
Deleting Points and other features
Fortunately, the code required to delete a Point feature will also work for other types of
geometries, so we don't need to implement separate DeletePointTool , De-
leteLineTool , and DeletePolygonTool classes. Instead, we only need a generic
DeleteTool . The following code implements this map tool:
class DeleteTool(QgsMapToolIdentify):
def __init__(self, mapCanvas, layer):
QgsMapToolIdentify.__init__(self, mapCanvas)
self.setCursor(Qt.CrossCursor)
self.layer = layer
self.feature = None
def canvasPressEvent(self, event):
found_features = self.identify(event.x(), event.y(),
[self.layer],
self.TopDownAll)
if len(found_features) > 0:
self.feature = found_features[0].mFeature
else:
self.feature = None
def canvasReleaseEvent(self, event):
found_features = self.identify(event.x(), event.y(),
[self.layer],
self.TopDownAll)
if len(found_features) > 0:
if self.feature.id() ==
found_features[0].mFeature.id():
self.layer.deleteFeature(self.feature.id())
Once again, we are using the QgsMapToolIdentify class to let us quickly find the fea-
ture the user clicked on. We use the canvasPressEvent() and can-
vasReleaseEvent() methods to ensure that the user clicked and released the mouse
over the same feature; this ensures that the map tool works in a more user-friendly way
than simply deleting the feature when the user clicks on it. If both the mouse click and the
mouse release were over the same feature, we would delete it.
Search WWH ::




Custom Search