Skip to content

Commit

Permalink
Merge pull request #12 from sitn/#10
Browse files Browse the repository at this point in the history
Gestion valeur générées en BD
  • Loading branch information
maltaesousa authored Jul 13, 2021
2 parents a5e10e4 + 664b29e commit b0c24be
Showing 1 changed file with 31 additions and 7 deletions.
38 changes: 31 additions & 7 deletions Historisation/maplayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,16 @@ def setListenToCommit(self, listen: bool):

if listen:
self.getVectorLayer().beforeCommitChanges.connect(self.onBeforeCommitChanges)
self.getVectorLayer().committedFeaturesAdded.connect(self.onCommittedFeaturesAdded)
self.getVectorLayer().committedAttributeValuesChanges.connect(self.onCommittedAttributeValuesChanges)
self.getVectorLayer().committedGeometriesChanges.connect(self.onCommittedGeometriesChanges)
self.getVectorLayer().afterCommitChanges.connect(self.onAfterCommitChanges)
else:
self.getVectorLayer().beforeCommitChanges.disconnect(self.onBeforeCommitChanges)
self.getVectorLayer().committedFeaturesAdded.disconnect(self.onCommittedFeaturesAdded)
self.getVectorLayer().committedAttributeValuesChanges.disconnect(self.onCommittedAttributeValuesChanges)
self.getVectorLayer().committedGeometriesChanges.disconnect(self.onCommittedGeometriesChanges)
self.getVectorLayer().afterCommitChanges.disconnect(self.onAfterCommitChanges)

def historizeTable(self, displayField: str, idField: str):
now = QDateTime.currentDateTime()
Expand All @@ -99,18 +107,34 @@ def historizeTable(self, displayField: str, idField: str):
self.histoLayer.initializeTable(now, eventId, self.valueMaps)

def onBeforeCommitChanges(self):
# Get modifications
# For added and modified, process after commit for DB default values
self.added = []
self.modifiedGeometries = []
self.modifiedAttributes = []

# For deleted, process before commit
editBuffer = self.getVectorLayer().editBuffer()
deletedIds = editBuffer.deletedFeatureIds()
self.deleted = [] if len(deletedIds) == 0 else list(self.getDatabaseFeatures(QgsFeatureRequest(deletedIds)))

def onCommittedFeaturesAdded(self, layerId, addedFeatures):
self.added = addedFeatures

added = list(editBuffer.addedFeatures().values())
def onCommittedAttributeValuesChanges(self, layerId, changedAttributesValues):
self.modifiedAttributes = changedAttributesValues

modifiedGeometries = editBuffer.changedGeometries()
modifiedAttributes = editBuffer.changedAttributeValues()
modifiedIds = list(set(list(modifiedGeometries)+list(modifiedAttributes)))
def onCommittedGeometriesChanges(self, layerId, changedGeometries):
self.modifiedGeometries = changedGeometries

def onAfterCommitChanges(self):
# Get modifications
addedIds = list(map(lambda x: x.id(), self.added))
added = [] if len(addedIds) == 0 else list(self.getFeatures(QgsFeatureRequest(addedIds)))

modifiedIds = list(set(list(self.modifiedGeometries)+list(self.modifiedAttributes)))
modified = [] if len(modifiedIds) == 0 else list(self.getFeatures(QgsFeatureRequest(modifiedIds)))

deletedIds = editBuffer.deletedFeatureIds()
deleted = [] if len(deletedIds) == 0 else list(self.getDatabaseFeatures(QgsFeatureRequest(deletedIds)))
deleted = list(self.deleted)

# Get user events
dlg = SaveManagementToolDialog(self.getVectorLayer().name(), self.uri.table(), self.histoParamTable, self.histoEventTypeTable, added, modified, deleted)
Expand Down

0 comments on commit b0c24be

Please sign in to comment.