def __init__(self, iface): # QGIS self.iface = iface self.settings = MySettings() self.linkRubber = QgsRubberBand(self.iface.mapCanvas()) self.featureHighlight = None # Relation management self.relationManager = QgsProject.instance().relationManager() self.relationManager.changed.connect(self.loadRelations) self.relation = QgsRelation() self.referencingFeature = QgsFeature() self.relationWidgetWrapper = None self.editorContext = QgsAttributeEditorContext() self.editorContext.setVectorLayerTools(self.iface.vectorLayerTools()) # GUI QDockWidget.__init__(self) self.setupUi(self) SettingDialog.__init__(self, MySettings(), False, True) self.drawButton.setChecked(self.settings.value("drawEnabled")) self.relationReferenceWidget.setAllowMapIdentification(True) self.relationReferenceWidget.setEmbedForm(False) self.mapTool = QgsMapToolIdentifyFeature(self.iface.mapCanvas()) self.mapTool.setButton(self.identifyReferencingFeatureButton) # Connect signal/slot self.relationComboBox.currentIndexChanged.connect( self.currentRelationChanged) self.mapTool.featureIdentified.connect(self.setReferencingFeature) # load relations at start self.loadRelations()
def open_feature_form(self, ilayer, ifeature): """"open feature form based on clicked object on the canvas""" ilayer.startEditing() context = QgsAttributeEditorContext() context.setVectorLayerTools(self.iface.vectorLayerTools()) self.attributeForm = QgsAttributeForm(ilayer, ifeature, context) self.stackedWidget.addWidget(self.attributeForm) self.stackedWidget.setCurrentWidget(self.attributeForm) self.iface.setActiveLayer(ilayer) self.terug.clicked.connect(lambda: self.close_stacked(ilayer, ifeature))
def __init__(self, layer, canvas, request): """ Constructor """ QDialog.__init__(self) self.setWindowTitle(layer.name()) self.__layout = QVBoxLayout() self.__menu = QMenu() for a in layer.actions().listActions(): self.__menu.addAction(a) self.__layout.addWidget(self.__menu) self.__dual = QgsDualView() self.__context = QgsAttributeEditorContext() self.__dual.init(layer, canvas, request, self.__context) self.__dual.setView(QgsDualView.AttributeTable) self.__layout.addWidget(self.__dual) self.setLayout(self.__layout)
def _newDialog(self, cloneFeature): feature = QgsFeature() if (cloneFeature): feature = QgsFeature(self._feature) else: feature = self._feature context = QgsAttributeEditorContext() myDa = QgsDistanceArea() myDa.setSourceCrs(self._layer.crs()) myDa.setEllipsoidalMode( self._iface.mapCanvas().mapSettings().hasCrsTransformEnabled()) myDa.setEllipsoid(QgsProject.instance().readEntry( 'Measure', '/Ellipsoid', GEO_NONE)[0]) context.setDistanceArea(myDa) context.setVectorLayerTools(self._iface.vectorLayerTools()) dialog = QgsAttributeDialog(self._layer, feature, cloneFeature, None, True, context) if (self._layer.actions().size() > 0): dialog.setContextMenuPolicy(Qt.ActionsContextMenu) a = QAction(self.tr('Run actions'), dialog) a.setEnabled(False) dialog.addAction(a) i = 0 for action in self._layer.actions(): if (action.runable()): a = FeatureAction(action.name(), feature, self._layer, i, -1, self._iface, dialog) dialog.addAction(a) a.triggered.connect(a.execute) pb = dialog.findChild(action.name()) if (pb): pb.clicked.connect(a.execute) i += 1 return dialog
def setLayer(self, layer): if layer == self.layer: return if self.layer: try: self.layer.destroyed.disconnect(self.onLayerRemoved) except TypeError: # Do not care if it is not connected pass self.layer = layer if self.layer: self.layer.destroyed.connect(self.onLayerRemoved) self.layerComboBox.setLayer(layer) if self.attributeForm: try: self.attributeForm.deleteLater() except RuntimeError: # Sometimes the form has already been deleted, that's ok for us pass if self.layer is not None: context = QgsAttributeEditorContext() context.setVectorLayerTools(self.iface.vectorLayerTools()) context.setFormMode(QgsAttributeEditorContext.StandaloneDialog) self.attributeForm = QgsAttributeForm(self.layer, QgsFeature(), context) self.attributeForm.hideButtonBox() try: self.layer.updatedFields.disconnect( self.attributeForm.onUpdatedFields) except TypeError: pass fields = self.layer.fields() self.feature = QgsFeature(fields) for idx in range(self.layer.fields().count()): self.feature.setAttribute(idx, self.layer.defaultValue(idx)) self.feature.setValid(True) self.attributeForm.setFeature(self.feature) self.attributeForm.widgetValueChanged.connect( self.onAttributeChanged) self.formWidget.layout().addWidget(self.attributeForm) self.layerChanged.emit(self.layer)
def createWrapper(self, layer, filter=None): """ Basic setup of a relation widget wrapper. Will create a new wrapper and set its feature to the one and only book in the table. It will also assign some instance variables to help * self.widget The created widget * self.table_view The table view of the widget :return: The created wrapper """ if layer == self.vl_books: relation = self.rel_b nmrel = self.rel_a else: relation = self.rel_a nmrel = self.rel_b self.wrapper = QgsRelationWidgetWrapper(layer, relation) self.wrapper.setConfig({'nm-rel': nmrel.id()}) context = QgsAttributeEditorContext() context.setMapCanvas(self.mapCanvas) context.setVectorLayerTools(self.vltools) self.wrapper.setContext(context) self.widget = self.wrapper.widget() self.widget.show() request = QgsFeatureRequest() if filter: request.setFilterExpression(filter) book = next(layer.getFeatures(request)) self.wrapper.setFeature(book) self.table_view = self.widget.findChild(QTableView) return self.wrapper
def test_add_feature_geometry(self): """ Test to add a feature with a geometry """ vl_pipes = QgsVectorLayer( self.dbconn + ' sslmode=disable key=\'pk\' table="qgis_test"."pipes" (geom) sql=', 'pipes', 'postgres') vl_leaks = QgsVectorLayer( self.dbconn + ' sslmode=disable key=\'pk\' table="qgis_test"."leaks" (geom) sql=', 'leaks', 'postgres') vl_leaks.startEditing() QgsProject.instance().addMapLayer(vl_pipes) QgsProject.instance().addMapLayer(vl_leaks) self.assertEqual(vl_pipes.featureCount(), 2) self.assertEqual(vl_leaks.featureCount(), 3) rel = QgsRelation() rel.setReferencingLayer(vl_leaks.id()) rel.setReferencedLayer(vl_pipes.id()) rel.addFieldPair('pipe', 'id') rel.setId('rel_pipe_leak') self.assertTrue(rel.isValid()) self.relMgr.addRelation(rel) # Mock vector layer tool to just set default value on created feature class DummyVlTools(QgsVectorLayerTools): def addFeature(self, layer, defaultValues, defaultGeometry): f = QgsFeature(layer.fields()) for idx, value in defaultValues.items(): f.setAttribute(idx, value) f.setGeometry(defaultGeometry) ok = layer.addFeature(f) return ok, f wrapper = QgsRelationWidgetWrapper(vl_leaks, rel) context = QgsAttributeEditorContext() vltool = DummyVlTools() context.setVectorLayerTools(vltool) context.setMapCanvas(self.mapCanvas) cadDockWidget = QgsAdvancedDigitizingDockWidget(self.mapCanvas) context.setCadDockWidget(cadDockWidget) wrapper.setContext(context) widget = wrapper.widget() widget.show() pipe = next(vl_pipes.getFeatures()) self.assertEqual(pipe.id(), 1) wrapper.setFeature(pipe) table_view = widget.findChild(QTableView) self.assertEqual(table_view.model().rowCount(), 1) btn = widget.findChild(QToolButton, 'mAddFeatureGeometryButton') self.assertTrue(btn.isVisible()) self.assertTrue(btn.isEnabled()) btn.click() self.assertTrue(self.mapCanvas.mapTool()) feature = QgsFeature(vl_leaks.fields()) feature.setGeometry(QgsGeometry.fromWkt('POINT(0 0.8)')) self.mapCanvas.mapTool().digitizingCompleted.emit(feature) self.assertEqual(table_view.model().rowCount(), 2) self.assertEqual(vl_leaks.featureCount(), 4) request = QgsFeatureRequest() request.addOrderBy("id", False) # get new created feature feat = next(vl_leaks.getFeatures('"id" is NULL')) self.assertTrue(feat.isValid()) self.assertTrue(feat.geometry().equals( QgsGeometry.fromWkt('POINT(0 0.8)'))) vl_leaks.rollBack()