Skip to content

Commit

Permalink
Merge pull request #18 from opengisch/toggleEditinDocumentSide
Browse files Browse the repository at this point in the history
Toggle editing and Save edits on document side
  • Loading branch information
domi4484 authored Aug 6, 2021
2 parents 1769c7a + 4ec1b15 commit d1f5057
Show file tree
Hide file tree
Showing 7 changed files with 2,107 additions and 2,028 deletions.
Binary file modified demo_projects/bees.gpkg
Binary file not shown.
Binary file removed demo_projects/bees.gpkg-shm
Binary file not shown.
Empty file removed demo_projects/bees.gpkg-wal
Empty file.
3,943 changes: 1,973 additions & 1,970 deletions demo_projects/simple_bee_farming_polymorphic_relation.qgs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
QgsAttributeDialog,
QgsFeatureSelectionDlg
)
from document_management_system.core.plugin_helper import PluginHelper

WidgetUi, _ = loadUiType(os.path.join(os.path.dirname(__file__), '../ui/relation_editor_document_side_widget.ui'))

Expand Down Expand Up @@ -79,10 +80,16 @@ def __init__(self, config, parent):

self._nmRelation = QgsRelation()
self._polymorphicRelation = QgsPolymorphicRelation()
self._layerInSameTransactionGroup = False

self.cardinality = Cardinality.ManyToOne

# Actions
self.actionToggleEditing = QAction(QIcon(":/images/themes/default/mActionToggleEditing.svg"),
self.tr("Toggle editing mode for child layers"))
self.actionToggleEditing.setCheckable(True)
self.actionSaveEdits = QAction(QIcon(":/images/themes/default/mActionSaveEdits.svg"),
self.tr("Save child layer edits"))
self.actionShowForm = QAction(QIcon(":/images/themes/default/mActionMultiEdit.svg"),
self.tr("Show form"))
self.actionLinkFeature = QAction(QIcon(":/images/themes/default/mActionLink.svg"),
Expand All @@ -91,6 +98,8 @@ def __init__(self, config, parent):
self.tr("Unlink feature"))

# Tool buttons
self.mToggleEditingToolButton.setDefaultAction(self.actionToggleEditing)
self.mSaveEditsToolButton.setDefaultAction(self.actionSaveEdits)
self.mShowFormToolButton.setDefaultAction(self.actionShowForm)
self.mShowFormToolButton.setToolButtonStyle(Qt.ToolButtonIconOnly)
self.mLinkFeaturesToolButton.setDefaultAction(self.actionLinkFeature)
Expand All @@ -103,14 +112,16 @@ def __init__(self, config, parent):
self.mFeaturesTreeWidget.addAction(self.actionShowForm)
self.mFeaturesTreeWidget.addAction(self.actionUnlinkFeature)

# Set initial state for add / remove etc.buttons
self.updateButtons()

# Signal slots
self.actionToggleEditing.triggered.connect(self.toggleEditing)
self.actionSaveEdits.triggered.connect(self.saveChildLayerEdits)
self.actionShowForm.triggered.connect(self.actionShowFormTriggered)
self.actionLinkFeature.triggered.connect(self.actionLinkFeatureTriggered)
self.actionUnlinkFeature.triggered.connect(self.actionUnlinkFeatureTriggered)
self.mFeaturesTreeWidget.currentItemChanged.connect(self.featuresTreeWidgetCurrentItemChanged)

# Initialize actions enabled states
self.featuresTreeWidgetCurrentItemChanged()
self.mFeaturesTreeWidget.currentItemChanged.connect(self.updateButtons)

def nmRelation(self):
return self._nmRelation
Expand Down Expand Up @@ -238,8 +249,12 @@ def afterSetRelations(self):

self._setCardinality()

self._checkTransactionGroup()

self.updateButtons()

def parentFormValueChanged(self, attribute, newValue):
pass
self.updateUi()

def checkLayerEditingMode(self):

Expand Down Expand Up @@ -283,6 +298,17 @@ def _setCardinality(self):
else:
print("WARNING invalid cardinality set")

@pyqtSlot(bool)
def toggleEditing(self, state):

super().toggleEditing(state)
self.updateButtons()

@pyqtSlot()
def saveChildLayerEdits(self):

super().saveEdits()

def actionShowFormTriggered(self):

if self.mFeaturesTreeWidget.currentItem() is None:
Expand Down Expand Up @@ -412,12 +438,58 @@ def actionUnlinkFeatureTriggered(self):
self.relation().referencingLayer().deleteFeature(self.mFeaturesTreeWidget.currentItem().data(0, TreeWidgetItemRole.LinkFeature).id())
self.updateUi()

def featuresTreeWidgetCurrentItemChanged(self):
def updateButtons(self):

toggleEditingButtonEnabled = False
editable = False
linkable = False
spatial = False

selectionNotEmpty = True
if (self.mFeaturesTreeWidget.currentItem() is None or
self.mFeaturesTreeWidget.currentItem().data(0, TreeWidgetItemRole.Type) != TreeWidgetItemType.Feature):
self.actionShowForm.setEnabled(False)
self.actionUnlinkFeature.setEnabled(False)
selectionNotEmpty = False

if self.relation().isValid():
toggleEditingButtonEnabled = self.relation().referencingLayer().supportsEditing()
editable = self.relation().referencingLayer().isEditable()
linkable = self.relation().referencingLayer().isEditable()
spatial = self.relation().referencingLayer().isSpatial()

if self.nmRelation().isValid():
toggleEditingButtonEnabled |= self.nmRelation().referencedLayer().supportsEditing()
editable = self.nmRelation().referencedLayer().isEditable()
spatial = self.nmRelation().referencedLayer().isSpatial()

self.actionToggleEditing.setEnabled(toggleEditingButtonEnabled)
self.actionLinkFeature.setEnabled(linkable)
self.actionUnlinkFeature.setEnabled(linkable and selectionNotEmpty)
self.actionToggleEditing.setChecked(editable)
self.actionSaveEdits.setEnabled(editable or linkable)

self.actionShowForm.setEnabled(selectionNotEmpty)

self.mToggleEditingToolButton.setVisible(self._layerInSameTransactionGroup is False)
self.mSaveEditsToolButton.setVisible(self._layerInSameTransactionGroup is False)

def _checkTransactionGroup(self):

self._layerInSameTransactionGroup = False
connectionString = PluginHelper.connectionString(self.relation().referencedLayer().source())
transactionGroup = QgsProject.instance().transactionGroup(self.relation().referencedLayer().providerType(),
connectionString)

if transactionGroup is None:
self.updateButtons()
return

if self.nmRelation().isValid():
if (self.relation().referencedLayer() in transactionGroup.layers() and
self.relation().referencingLayer() in transactionGroup.layers() and
self.nmRelation().referencedLayer() in transactionGroup.layers()):
self._layerInSameTransactionGroup = True
else:
self.actionShowForm.setEnabled(True)
self.actionUnlinkFeature.setEnabled(True)
if (self.relation().referencedLayer() in transactionGroup.layers() and
self.relation().referencingLayer() in transactionGroup.layers()):
self._layerInSameTransactionGroup = True

Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def __init__(self, config, parent):
self.model = DocumentModel()

self._nmRelation = QgsRelation()
self.mLayerInSameTransactionGroup = False
self._layerInSameTransactionGroup = False

self._currentDocumentId = None

Expand Down Expand Up @@ -205,15 +205,21 @@ def updateButtons(self):
self.mToggleEditingToolButton.setChecked(editable)
self.mSaveEditsToolButton.setEnabled(editable or linkable)

self.mShowFormToolButton.setEnabled(self._currentDocumentId is not None)
self.mShowFormToolButton.setEnabled(selectionNotEmpty)

self.mToggleEditingToolButton.setVisible(self.mLayerInSameTransactionGroup is False)
self.mSaveEditsToolButton.setVisible(self.mLayerInSameTransactionGroup is False)
self.mToggleEditingToolButton.setVisible(self._layerInSameTransactionGroup is False)
self.mSaveEditsToolButton.setVisible(self._layerInSameTransactionGroup is False)

def afterSetRelations(self):
self._nmRelation = QgsProject.instance().relationManager().relation(str(self.nmRelationId()))

self.mLayerInSameTransactionGroup = False
self._checkTransactionGroup()

self.updateButtons()

def _checkTransactionGroup(self):

self._layerInSameTransactionGroup = False
connectionString = PluginHelper.connectionString(self.relation().referencedLayer().source())
transactionGroup = QgsProject.instance().transactionGroup(self.relation().referencedLayer().providerType(),
connectionString)
Expand All @@ -226,13 +232,11 @@ def afterSetRelations(self):
if (self.relation().referencedLayer() in transactionGroup.layers() and
self.relation().referencingLayer() in transactionGroup.layers() and
self.nmRelation().referencedLayer() in transactionGroup.layers()):
self.mLayerInSameTransactionGroup = True
self._layerInSameTransactionGroup = True
else:
if (self.relation().referencedLayer() in transactionGroup.layers() and
self.relation().referencingLayer() in transactionGroup.layers()):
self.mLayerInSameTransactionGroup = True

self.updateButtons()
self._layerInSameTransactionGroup = True

def parentFormValueChanged(self, attribute, newValue):
pass
Expand All @@ -257,8 +261,6 @@ def checkLayerEditingMode(self):
@pyqtSlot(bool)
def toggleEditing(self, state):

print("Toggle editing {0}".format(state))

super().toggleEditing(state)
self.updateButtons()

Expand All @@ -270,17 +272,11 @@ def saveChildLayerEdits(self):
@pyqtSlot()
def addDocument(self):

if self.checkLayerEditingMode() is False:
return

self.addFeature()

@pyqtSlot()
def dropDocument(self):

if self.checkLayerEditingMode() is False:
return

if self._currentDocumentId is None:
return

Expand Down Expand Up @@ -379,17 +375,11 @@ def addDroppedDocument(self, fileUrl):
@pyqtSlot()
def linkDocument(self):

if self.checkLayerEditingMode() is False:
return

self.linkFeature()

@pyqtSlot()
def unlinkDocument(self):

if self.checkLayerEditingMode() is False:
return

if self._currentDocumentId is None:
return

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,51 +26,65 @@
<string>Form</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="1" column="0" colspan="6">
<widget class="QTreeWidget" name="mFeaturesTreeWidget">
<attribute name="headerVisible">
<bool>false</bool>
</attribute>
<column>
<property name="text">
<string notr="true">1</string>
</property>
</column>
<item row="0" column="3">
<widget class="QToolButton" name="mLinkFeaturesToolButton">
<property name="text">
<string>...</string>
</property>
</widget>
</item>
<item row="0" column="7">
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="0" column="0">
<widget class="QToolButton" name="mShowFormToolButton">
<widget class="QToolButton" name="mToggleEditingToolButton">
<property name="text">
<string>...</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QToolButton" name="mLinkFeaturesToolButton">
<item row="0" column="4">
<widget class="QToolButton" name="mUnlinkFeaturesToolButton">
<property name="text">
<string>...</string>
</property>
</widget>
</item>
<item row="0" column="2">
<widget class="QToolButton" name="mUnlinkFeaturesToolButton">
<widget class="QToolButton" name="mShowFormToolButton">
<property name="text">
<string>...</string>
</property>
</widget>
</item>
<item row="0" column="5">
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
<item row="0" column="1">
<widget class="QToolButton" name="mSaveEditsToolButton">
<property name="text">
<string>...</string>
</property>
</spacer>
</widget>
</item>
<item row="1" column="0" colspan="8">
<widget class="QTreeWidget" name="mFeaturesTreeWidget">
<attribute name="headerVisible">
<bool>false</bool>
</attribute>
<column>
<property name="text">
<string notr="true">1</string>
</property>
</column>
</widget>
</item>
</layout>
</widget>
Expand Down

0 comments on commit d1f5057

Please sign in to comment.