Check for empty geometry before applying new selection
authorThierry Florac <tflorac@ulthar.net>
Wed, 13 Feb 2019 17:20:31 +0100
changeset 195 07c9c58698fb
parent 194 46f90755209e
child 196 9f7381236c89
Check for empty geometry before applying new selection
src/pyams_file/image.py
src/pyams_file/interfaces/__init__.py
src/pyams_file/zmi/image.py
--- a/src/pyams_file/image.py	Mon Feb 11 14:12:29 2019 +0100
+++ b/src/pyams_file/image.py	Wed Feb 13 17:20:31 2019 +0100
@@ -49,6 +49,9 @@
         else:
             return False
 
+    def is_empty(self):
+        return (self.x2 <= self.x1) or (self.y2 <= self.y1)
+
 
 @adapter_config(context=IImage, provides=IThumbnailer)
 class ImageThumbnailer(ContextAdapter):
--- a/src/pyams_file/interfaces/__init__.py	Mon Feb 11 14:12:29 2019 +0100
+++ b/src/pyams_file/interfaces/__init__.py	Wed Feb 13 17:20:31 2019 +0100
@@ -253,6 +253,9 @@
              required=True,
              min=0)
 
+    def is_empty(self):
+        """Check if geometry is not empty"""
+
 
 class IThumbnailer(Interface):
     """Interface of adapter used to generate image thumbnails"""
--- a/src/pyams_file/zmi/image.py	Mon Feb 11 14:12:29 2019 +0100
+++ b/src/pyams_file/zmi/image.py	Wed Feb 13 17:20:31 2019 +0100
@@ -26,9 +26,10 @@
 from zope.schema import Bool, Int
 from zope.schema.fieldproperty import FieldProperty
 
+from pyams_file import _
 from pyams_file.image import ThumbnailGeometry
 from pyams_file.interfaces import IFileInfo, IFileModifierForm, IImage, IMediaWidget, IResponsiveImage, ISVGImage, \
-    IThumbnailForm, IThumbnailer, IThumbnails
+    IThumbnailForm, IThumbnailGeometry, IThumbnailer, IThumbnails
 from pyams_file.zmi import FileModifierAction
 from pyams_form.form import DialogDisplayForm, ajax_config
 from pyams_form.help import FormHelp
@@ -49,8 +50,6 @@
 from pyams_zmi.form import AdminDialogEditForm
 from pyams_zmi.layer import IAdminLayer
 
-from pyams_file import _
-
 
 #
 # SVG utilities
@@ -293,13 +292,21 @@
         geometry.y1 = int(self.request.params.get('selection.y1'))
         geometry.x2 = int(self.request.params.get('selection.x2'))
         geometry.y2 = int(self.request.params.get('selection.y2'))
-        IThumbnails(self.context).set_geometry(self.selection_name, geometry)
+        if not geometry.is_empty():
+            IThumbnails(self.context).set_geometry(self.selection_name, geometry)
+            return {IThumbnailGeometry: ('x1', 'y1', 'x2', 'y2')}
 
     def get_ajax_output(self, changes):
-        return {
-            'status': 'success',
-            'message': self.request.localizer.translate(self.successMessage)
-        }
+        if changes:
+            return {
+                'status': 'success',
+                'message': self.request.localizer.translate(self.successMessage)
+            }
+        else:
+            return {
+                'status': 'info',
+                'message': self.request.localizer.translate(self.noChangesMessage)
+            }
 
 
 @viewlet_config(name='thumbnail.selection.widgets-prefix', context=IImage, layer=IAdminLayer,