Skip to content

Commit

Permalink
Prevent sharing maps when layers have unsaved changes
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Dec 5, 2023
1 parent 4b290fc commit a5120f2
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 1 deletion.
10 changes: 10 additions & 0 deletions felt/core/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class LayerSupport(Enum):
NotImplementedProvider = auto()
NotImplementedLayerType = auto()
EmptyLayer = auto()
UnsavedEdits = auto()

def should_report(self) -> bool:
"""
Expand All @@ -74,6 +75,15 @@ def should_report(self) -> bool:
LayerSupport.EmptyLayer
)

def should_prevent_sharing_maps(self) -> bool:
"""
Returns True if the layer support should completely block sharing
maps
"""
return self in (
LayerSupport.UnsavedEdits,
)


class UsageType(Enum):
"""
Expand Down
3 changes: 3 additions & 0 deletions felt/core/layer_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ def can_export_layer(layer: QgsMapLayer) \
string if not
"""
if isinstance(layer, QgsVectorLayer):
if layer.editBuffer() and layer.editBuffer().isModified():
return LayerSupport.UnsavedEdits, 'Layer has unsaved changes'

# Vector layers must have some features
if layer.featureCount() == 0:
return LayerSupport.EmptyLayer, 'Layer is empty'
Expand Down
3 changes: 3 additions & 0 deletions felt/core/map_uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ def _build_unsupported_layer_details(self,
unsupported_layer_names = set()
for layer in layers:
support, reason = LayerExporter.can_export_layer(layer)
if support == LayerSupport.Supported:
continue

self.unsupported_layers.append((layer.name(), reason))
if not support.should_report():
continue
Expand Down
17 changes: 16 additions & 1 deletion felt/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
)
from qgis.PyQt.QtWidgets import (
QMenu,
QAction
QAction,
QMessageBox
)

from qgis.core import (
Expand Down Expand Up @@ -209,6 +210,20 @@ def _create_map_authorized(self,
self._create_map_dialog.raise_()
return

export_layers = layers if layers else \
QgsProject.instance().mapLayers().values()
for layer in export_layers:
support, reason = LayerExporter.can_export_layer(layer)
if support == LayerSupport.UnsavedEdits:
QMessageBox.warning(
self.iface.mainWindow(),
self.tr('Share to Felt'),
self.tr(
'Layer "{}" has unsaved changes. Please save '
'the layer before sharing to Felt.').format(
layer.name()))
return

def _cleanup_dialog(_dialog):
"""
Remove references to outdated dialogs
Expand Down
6 changes: 6 additions & 0 deletions felt/test/test_layer_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ def test_can_export_layer(self):
self.assertTrue(layer.isValid())
self.assertEqual(
LayerExporter.can_export_layer(layer)[0], LayerSupport.Supported)
self.assertTrue(layer.startEditing())
layer.deleteFeature(next(layer.getFeatures()).id())
self.assertEqual(
LayerExporter.can_export_layer(layer)[0], LayerSupport.UnsavedEdits)

layer.rollBack()

file = str(TEST_DATA_PATH / 'dem.tif')
layer = QgsRasterLayer(file, 'test')
Expand Down

0 comments on commit a5120f2

Please sign in to comment.