|
1 # |
|
2 # Copyright (c) 2008-2017 Thierry Florac <tflorac AT ulthar.net> |
|
3 # All Rights Reserved. |
|
4 # |
|
5 # This software is subject to the provisions of the Zope Public License, |
|
6 # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. |
|
7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED |
|
8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|
9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS |
|
10 # FOR A PARTICULAR PURPOSE. |
|
11 # |
|
12 |
|
13 __docformat__ = 'restructuredtext' |
|
14 |
|
15 |
|
16 # import standard library |
|
17 import json |
|
18 |
|
19 # import interfaces |
|
20 from pyams_gis.interfaces import IGeoPointZ |
|
21 from pyams_form.interfaces.form import IFormLayer, IForm |
|
22 from pyams_gis.interfaces.widget import IGeoPointWidget, IGeoPointZWidget |
|
23 from pyams_gis.schema import IGeoPoint, IGeoPointField, IGeoPointZField |
|
24 from pyams_utils.interfaces.data import IObjectData |
|
25 from z3c.form.interfaces import IFieldWidget, IObjectFactory |
|
26 |
|
27 # import packages |
|
28 from pyams_form.widget import widgettemplate_config |
|
29 from pyams_gis.point import GeoPoint, GeoPointZ |
|
30 from pyams_utils.adapter import adapter_config |
|
31 from z3c.form.browser.object import ObjectWidget |
|
32 from z3c.form.object import getIfName |
|
33 from z3c.form.widget import FieldWidget |
|
34 from zope.interface import implementer_only, alsoProvides, Interface |
|
35 |
|
36 |
|
37 @adapter_config(name=getIfName(IGeoPoint), |
|
38 context=(Interface, IFormLayer, IForm, IGeoPointWidget), provides=IObjectFactory) |
|
39 class GeoPointObjectFactory(object): |
|
40 """GeoPointZ object factory""" |
|
41 |
|
42 def __init__(self, context, request, form, widget): |
|
43 self.context = context |
|
44 self.request = request |
|
45 self.form = form |
|
46 self.widget = widget |
|
47 |
|
48 def __call__(self, data): |
|
49 return GeoPoint() |
|
50 |
|
51 |
|
52 @widgettemplate_config(mode='input', template='templates/geopoint-input.pt', layer=IFormLayer) |
|
53 @implementer_only(IGeoPointWidget) |
|
54 class GeoPointWidget(ObjectWidget): |
|
55 """GeoPoint widget""" |
|
56 |
|
57 def updateWidgets(self, setErrors=True): |
|
58 super(GeoPointWidget, self).updateWidgets(setErrors) |
|
59 widgets = self.subform.widgets |
|
60 longitude = widgets['longitude'] |
|
61 longitude.label_css_class = 'control-label col-md-3' |
|
62 longitude.input_css_class = 'col-md-2' |
|
63 longitude.object_data = {'ams-change-handler': 'PyAMS_GIS.position.changedCoordinate'} |
|
64 alsoProvides(longitude, IObjectData) |
|
65 latitude = widgets['latitude'] |
|
66 latitude.label_css_class = 'control-label col-md-3' |
|
67 latitude.input_css_class = 'col-md-2' |
|
68 latitude.object_data = {'ams-change-handler': 'PyAMS_GIS.position.changedCoordinate'} |
|
69 alsoProvides(latitude, IObjectData) |
|
70 projection = widgets['projection'] |
|
71 projection.label_css_class = 'control-label col-md-3' |
|
72 projection.input_css_class = 'col-md-9' |
|
73 projection.object_data = {'ams-events-handlers': {'change.select2': 'PyAMS_GIS.position.changedProjection'}} |
|
74 alsoProvides(projection, IObjectData) |
|
75 |
|
76 @property |
|
77 def wgs_coordinates(self): |
|
78 value = self.field.get(self.field.interface(self.context)) |
|
79 if not value: |
|
80 return json.dumps({'longitude': None, |
|
81 'latitude': None}) |
|
82 else: |
|
83 point = value.wgs_coordinates |
|
84 return json.dumps({'longitude': float(point[0]), |
|
85 'latitude': float(point[1])}) |
|
86 |
|
87 |
|
88 @adapter_config(context=(IGeoPointField, IFormLayer), provides=IFieldWidget) |
|
89 def GeoPointFieldWidget(field, request): |
|
90 """GeoPoint field widget factory""" |
|
91 return FieldWidget(field, GeoPointWidget(request)) |
|
92 |
|
93 |
|
94 @adapter_config(name=getIfName(IGeoPointZ), |
|
95 context=(Interface, IFormLayer, IForm, IGeoPointZWidget), provides=IObjectFactory) |
|
96 class GeoPointZObjectFactory(object): |
|
97 """GeoPointZ object factory""" |
|
98 |
|
99 def __init__(self, context, request, form, widget): |
|
100 self.context = context |
|
101 self.request = request |
|
102 self.form = form |
|
103 self.widget = widget |
|
104 |
|
105 def __call__(self, data): |
|
106 return GeoPointZ() |
|
107 |
|
108 |
|
109 @widgettemplate_config(mode='input', template='templates/geopoint-input.pt', layer=IFormLayer) |
|
110 @implementer_only(IGeoPointZWidget) |
|
111 class GeoPointZWidget(ObjectWidget): |
|
112 """GeoPointZ widget""" |
|
113 |
|
114 def updateWidgets(self, setErrors=True): |
|
115 super(GeoPointZWidget, self).updateWidgets(setErrors) |
|
116 widgets = self.subform.widgets |
|
117 longitude = widgets['longitude'] |
|
118 longitude.label_css_class = 'control-label col-md-3' |
|
119 longitude.input_css_class = 'col-md-2' |
|
120 longitude.object_data = {'ams-change-handler': 'PyAMS_GIS.position.changedCoordinate'} |
|
121 alsoProvides(longitude, IObjectData) |
|
122 latitude = widgets['latitude'] |
|
123 latitude.label_css_class = 'control-label col-md-3' |
|
124 latitude.input_css_class = 'col-md-2' |
|
125 latitude.object_data = {'ams-change-handler': 'PyAMS_GIS.position.changedCoordinate'} |
|
126 alsoProvides(latitude, IObjectData) |
|
127 projection = widgets['projection'] |
|
128 projection.label_css_class = 'control-label col-md-3' |
|
129 latitude.input_css_class = 'col-md-9' |
|
130 projection.object_data = {'ams-events-handlers': {'change.select2': 'PyAMS_GIS.position.changedProjection'}} |
|
131 alsoProvides(projection, IObjectData) |
|
132 altitude = widgets['altitude'] |
|
133 altitude.label_css_class = 'control-label col-md-3' |
|
134 altitude.input_css_class = 'col-md-2' |
|
135 |
|
136 @property |
|
137 def wgs_coordinates(self): |
|
138 value = self.field.get(self.field.interface(self.context)) |
|
139 if not value: |
|
140 return json.dumps({'longitude': None, |
|
141 'latitude': None}) |
|
142 else: |
|
143 point = value.wgs_coordinates |
|
144 return json.dumps({'longitude': float(point[0]), |
|
145 'latitude': float(point[1])}) |
|
146 |
|
147 |
|
148 @adapter_config(context=(IGeoPointZField, IFormLayer), provides=IFieldWidget) |
|
149 def GeoPointZFieldWidget(field, request): |
|
150 """GeoPointZ field widget factory""" |
|
151 return FieldWidget(field, GeoPointZWidget(request)) |