--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_gis/widget/point.py Thu May 18 17:23:48 2017 +0200
@@ -0,0 +1,141 @@
+#
+# 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_gis.interfaces import IGeoPointZ
+from pyams_form.interfaces.form import IFormLayer, IForm
+from pyams_gis.interfaces.widget import IGeoPointWidget, IGeoPointZWidget
+from pyams_gis.schema import IGeoPoint, IGeoPointField, IGeoPointZField
+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.point import GeoPoint, GeoPointZ
+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(IGeoPoint),
+ context=(Interface, IFormLayer, IForm, IGeoPointWidget), provides=IObjectFactory)
+class GeoPointObjectFactory(object):
+ """GeoPointZ 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 = GeoPoint()
+ for name, value in data.items():
+ setattr(result, name, value)
+ return result
+
+
+@widgettemplate_config(mode='input', template='templates/geopoint-input.pt', layer=IFormLayer)
+@implementer_only(IGeoPointWidget)
+class GeoPointWidget(ObjectWidget):
+ """GeoPoint widget"""
+
+ def updateWidgets(self, setErrors=True):
+ super(GeoPointWidget, self).updateWidgets(setErrors)
+ widgets = self.subform.widgets
+ widgets['longitude'].input_css_class = 'col-md-2'
+ widgets['latitude'].input_css_class = 'col-md-2'
+ widgets['longitude'].object_data = {'ams-change-handler': 'PyAMS_GIS.position.changedCoordinate'}
+ widgets['latitude'].object_data = {'ams-change-handler': 'PyAMS_GIS.position.changedCoordinate'}
+ widgets['projection'].object_data = {'ams-events-handlers': {'change.select2': 'PyAMS_GIS.position.changedProjection'}}
+ alsoProvides(widgets['longitude'], IObjectData)
+ alsoProvides(widgets['latitude'], 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({'longitude': None,
+ 'latitude': None})
+ else:
+ point = value.wgs_coordinates
+ return json.dumps({'longitude': float(point[0]),
+ 'latitude': float(point[1])})
+
+
+@adapter_config(context=(IGeoPointField, IFormLayer), provides=IFieldWidget)
+def GeoPointFieldWidget(field, request):
+ """GeoPoint field widget factory"""
+ return FieldWidget(field, GeoPointWidget(request))
+
+
+@adapter_config(name=getIfName(IGeoPointZ),
+ context=(Interface, IFormLayer, IForm, IGeoPointZWidget), provides=IObjectFactory)
+class GeoPointZObjectFactory(object):
+ """GeoPointZ 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 = GeoPointZ()
+ for name, value in data.items():
+ setattr(result, name, value)
+ return result
+
+
+@widgettemplate_config(mode='input', template='templates/geopoint-input.pt', layer=IFormLayer)
+@implementer_only(IGeoPointZWidget)
+class GeoPointZWidget(ObjectWidget):
+ """GeoPointZ widget"""
+
+ def updateWidgets(self, setErrors=True):
+ super(GeoPointZWidget, self).updateWidgets(setErrors)
+ widgets = self.subform.widgets
+ widgets['longitude'].input_css_class = 'col-md-2'
+ widgets['latitude'].input_css_class = 'col-md-2'
+ widgets['altitude'].input_css_class = 'col-md-2'
+ widgets['longitude'].object_data = {'ams-change-handler': 'PyAMS_GIS.position.changedCoordinate'}
+ widgets['latitude'].object_data = {'ams-change-handler': 'PyAMS_GIS.position.changedCoordinate'}
+ widgets['projection'].object_data = {'ams-events-handlers': {'change.select2': 'PyAMS_GIS.position.changedProjection'}}
+ alsoProvides(widgets['longitude'], IObjectData)
+ alsoProvides(widgets['latitude'], 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({'longitude': None,
+ 'latitude': None})
+ else:
+ point = value.wgs_coordinates
+ return json.dumps({'longitude': float(point[0]),
+ 'latitude': float(point[1])})
+
+
+@adapter_config(context=(IGeoPointZField, IFormLayer), provides=IFieldWidget)
+def GeoPointZFieldWidget(field, request):
+ """GeoPointZ field widget factory"""
+ return FieldWidget(field, GeoPointZWidget(request))