--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_gis/widget/area.py Thu May 18 17:23:48 2017 +0200
@@ -0,0 +1,97 @@
+#
+# Copyright (c) 2008-2017 Thierry Florac <tflorac AT ulthar.net>
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+
+__docformat__ = 'restructuredtext'
+
+
+# import standard library
+import json
+
+# import interfaces
+from pyams_form.interfaces.form import IFormLayer, IForm
+from pyams_gis.interfaces import IGeoArea
+from pyams_gis.interfaces.widget import IGeoAreaWidget
+from pyams_gis.schema import IGeoAreaField
+from pyams_utils.interfaces.data import IObjectData
+from z3c.form.interfaces import IFieldWidget, IObjectFactory
+
+# import packages
+from pyams_form.widget import widgettemplate_config
+from pyams_gis.area import GeoArea
+from pyams_utils.adapter import adapter_config
+from z3c.form.browser.object import ObjectWidget
+from z3c.form.object import getIfName
+from z3c.form.widget import FieldWidget
+from zope.interface import implementer_only, alsoProvides, Interface
+
+
+@adapter_config(name=getIfName(IGeoArea),
+ context=(Interface, IFormLayer, IForm, IGeoAreaWidget), provides=IObjectFactory)
+class GeoAreaObjectFactory(object):
+ """GeoArea object factory"""
+
+ def __init__(self, context, request, form, widget):
+ self.context = context
+ self.request = request
+ self.form = form
+ self.widget = widget
+
+ def __call__(self, data):
+ result = GeoArea()
+ for name, value in data.items():
+ setattr(result, name, value)
+ return result
+
+
+@widgettemplate_config(mode='input', template='templates/geoarea-input.pt', layer=IFormLayer)
+@implementer_only(IGeoAreaWidget)
+class GeoAreaWidget(ObjectWidget):
+ """GeoArea widget"""
+
+ def updateWidgets(self, setErrors=True):
+ super(GeoAreaWidget, self).updateWidgets()
+ widgets = self.subform.widgets
+ widgets['x1'].input_css_class = 'col-md-2'
+ widgets['y1'].input_css_class = 'col-md-2'
+ widgets['x2'].input_css_class = 'col-md-2'
+ widgets['y2'].input_css_class = 'col-md-2'
+ widgets['x1'].object_data = {'ams-change-handler': 'PyAMS_GIS.area.changedCoordinate'}
+ widgets['y1'].object_data = {'ams-change-handler': 'PyAMS_GIS.area.changedCoordinate'}
+ widgets['x2'].object_data = {'ams-change-handler': 'PyAMS_GIS.area.changedCoordinate'}
+ widgets['y2'].object_data = {'ams-change-handler': 'PyAMS_GIS.area.changedCoordinate'}
+ widgets['projection'].object_data = {'ams-events-handlers': {'change.select2': 'PyAMS_GIS.area.changedProjection'}}
+ alsoProvides(widgets['x1'], IObjectData)
+ alsoProvides(widgets['y1'], IObjectData)
+ alsoProvides(widgets['x2'], IObjectData)
+ alsoProvides(widgets['y2'], IObjectData)
+ alsoProvides(widgets['projection'], IObjectData)
+
+ @property
+ def wgs_coordinates(self):
+ value = self.field.get(self.field.interface(self.context))
+ if not value:
+ return json.dumps({'x1': None,
+ 'y1': None,
+ 'x2': None,
+ 'y2': None})
+ else:
+ point1, point2 = value.wgs_coordinates
+ return json.dumps({'x1': float(point1[0]),
+ 'y1': float(point1[1]),
+ 'x2': float(point2[0]),
+ 'y2': float(point2[1])})
+
+
+@adapter_config(context=(IGeoAreaField, IFormLayer), provides=IFieldWidget)
+def GeoAreaFieldWidget(field, request):
+ """GeoArea field widget factory"""
+ return FieldWidget(field, GeoAreaWidget(request))