/[thuban]/branches/WIP-pyshapelib-bramz/Thuban/UI/legend.py
ViewVC logotype

Contents of /branches/WIP-pyshapelib-bramz/Thuban/UI/legend.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 542 - (show annotations)
Thu Mar 20 09:43:16 2003 UTC (21 years, 11 months ago) by jonathan
Original Path: trunk/thuban/Thuban/UI/legend.py
File MIME type: text/x-python
File size: 9372 byte(s)
New. Creates a window that shows the map's
        Legend information and allows the user to add/modify classifications
        and how the layers are drawn on the map.

1 # Copyright (c) 2001, 2002, 2003 by Intevation GmbH
2 # Authors:
3 # Jonathan Coles <[email protected]>
4 #
5 # This program is free software under the GPL (>=v2)
6 # Read the file COPYING coming with Thuban for details.
7
8 __version__ = "$Revision$"
9
10 from Thuban import _
11
12 from wxPython.wx import *
13 from dialogs import NonModalDialog
14
15 from Thuban.Model.layer import Layer
16 from Thuban.Model.map import Map
17
18 from Thuban.Model.messages import *
19
20 from Thuban.Model.classification import ClassGroup
21
22 from Thuban.UI.classifier import ClassDataPreviewer
23
24 ID_LEGEND_MOVEUP = 4001
25 ID_LEGEND_MOVEDOWN = 4002
26 ID_LEGEND_TREE = 4003
27 ID_LEGEND_CLASSIFY = 4004
28 ID_LEGEND_SHOWLAYER = 4005
29 ID_LEGEND_HIDELAYER = 4006
30
31 BMP_SIZE_W = 30
32 BMP_SIZE_H = 15
33
34 class Legend(NonModalDialog):
35
36 def __init__(self, parent, name, map):
37 NonModalDialog.__init__(self, parent, name,
38 _("Legend: %s") % map.Title())
39
40 self.parent = parent
41
42
43 panel = wxPanel(self, -1)
44
45 topBox = wxBoxSizer(wxVERTICAL)
46 panelBox = wxBoxSizer(wxVERTICAL)
47
48 buttonBox = wxGridSizer(2, 3, 0, 0)
49
50 self.buttons = []
51
52 button = wxButton(self, ID_LEGEND_MOVEUP, _("Move Up"))
53 buttonBox.Add(button, 0, wxGROW | wxLEFT | wxRIGHT, 0)
54 self.buttons.append(button)
55
56 button = wxButton(self, ID_LEGEND_SHOWLAYER, _("Show Layer"))
57 buttonBox.Add(button, 0, wxGROW | wxLEFT | wxRIGHT, 0)
58 self.buttons.append(button)
59
60 button = wxButton(self, ID_LEGEND_CLASSIFY, _("Classify"))
61 buttonBox.Add(button, 0, wxGROW | wxLEFT | wxRIGHT, 0)
62 self.buttons.append(button)
63
64 button = wxButton(self, ID_LEGEND_MOVEDOWN, _("Move Down"))
65 buttonBox.Add(button, 0, wxGROW | wxLEFT | wxRIGHT, 0)
66 self.buttons.append(button)
67
68 button = wxButton(self, ID_LEGEND_HIDELAYER, _("Hide Layer"))
69 buttonBox.Add(button, 0, wxGROW | wxLEFT | wxRIGHT, 0)
70 self.buttons.append(button)
71
72
73 EVT_BUTTON(self, ID_LEGEND_MOVEUP, self._OnMoveUp)
74 EVT_BUTTON(self, ID_LEGEND_MOVEDOWN, self._OnMoveDown)
75 EVT_BUTTON(self, ID_LEGEND_CLASSIFY, self._OnClassify)
76 EVT_BUTTON(self, ID_LEGEND_SHOWLAYER, self._OnShowLayer)
77 EVT_BUTTON(self, ID_LEGEND_HIDELAYER, self._OnHideLayer)
78
79 panelBox.Add(buttonBox, 0, 0, 4)
80
81 self.tree = LegendTree(self, ID_LEGEND_TREE, map)
82
83 panelBox.Add(self.tree, 1, wxGROW, 4)
84
85 panel.SetAutoLayout(True)
86 panel.SetSizer(panelBox)
87 panelBox.SetSizeHints(panel)
88
89 topBox.Add(panel, 1, wxGROW, 0)
90 panelBox.SetSizeHints(self)
91
92 self.SetAutoLayout(True)
93 self.SetSizer(topBox)
94
95
96 def DoOnSelChanged(self):
97 self.__EnableButtons(self.tree.GetSelection().IsOk())
98
99 def _OnClassify(self, event):
100 self.tree.DoOnClassify()
101
102 def _OnMoveUp(self, event):
103 self.tree.MoveCurrentItemUp()
104
105 def _OnMoveDown(self, event):
106 self.tree.MoveCurrentItemDown()
107
108 def _OnShowLayer(self, event):
109 self.tree.DoOnShowLayer()
110 pass
111
112 def _OnHideLayer(self, event):
113 self.tree.DoOnHideLayer()
114 pass
115
116
117 def __EnableButtons(self, on):
118 for b in self.buttons:
119 b.Enable(on)
120
121 class LegendTree(wxTreeCtrl):
122
123 def __init__(self, parent, id, map):
124 wxTreeCtrl.__init__(self, parent, id,
125 style = wxTR_DEFAULT_STYLE | wxTR_HIDE_ROOT,
126 size = (200, 200))
127
128 self.parent = parent
129 self.map = map
130 self.layer2id = None
131
132
133 self.image_list = None
134 self.emptyImageIndex = 0
135
136 self.previewer = ClassDataPreviewer()
137
138
139 EVT_TREE_ITEM_ACTIVATED(self, ID_LEGEND_TREE, self._OnItemActivated)
140 EVT_TREE_SEL_CHANGED(self, ID_LEGEND_TREE, self._OnSelChanged)
141
142 map.Subscribe(MAP_STACKING_CHANGED, self._OnMsgMapStackingChanged)
143 map.Subscribe(MAP_LAYERS_CHANGED, self._OnMsgMapLayersChanged)
144
145 self.__FillTree(map)
146
147 def MoveCurrentItemUp(self):
148 cur_id = self.GetSelection()
149 assert(cur_id.IsOk())
150
151 cur_data = self.GetPyData(cur_id)
152
153 #prev_id = self.GetPrevSibling(cur_id)
154
155 #
156 # Get out if there's nowhere to go
157 #
158 #if prev_id == INVALID_TREE_ID: return
159
160 if isinstance(cur_data, Layer):
161 self.map.RaiseLayer(cur_data)
162 elif isinstance(cur_data, ClassGroup):
163 pass
164 else:
165 assert(False, "Shouldn't be here.")
166 pass
167
168 def MoveCurrentItemDown(self):
169 cur_id = self.GetSelection()
170 assert(cur_id.IsOk())
171
172 cur_data = self.GetPyData(cur_id)
173
174 if isinstance(cur_data, Layer):
175 self.map.LowerLayer(cur_data)
176 elif isinstance(cur_data, ClassGroup):
177 pass
178 else:
179 assert(False, "Shouldn't be here.")
180 pass
181
182
183 def OnCompareItems(self, item1, item2):
184
185 data1 = self.GetPyData(item1)
186 data2 = self.GetPyData(item2)
187
188 if isinstance(data1, Layer):
189 layers = self.map.Layers()
190 return layers.index(data2) - layers.index(data1)
191 else:
192 return wxTreeCtrl.OnCompareItems(self, item1, item2)
193
194
195 def DoOnShowLayer(self):
196 self.__ShowHideLayer(True)
197
198 def DoOnHideLayer(self):
199 self.__ShowHideLayer(False)
200
201 def DoOnClassify(self):
202 id = self.GetSelection()
203 assert(id.IsOk())
204
205 item = self.GetPyData(id)
206 if isinstance(item, ClassGroup):
207 id = self.GetItemParent(id)
208 assert(id.IsOk())
209 item = self.GetPyData(id)
210
211 self.parent.parent.OpenClassifier(item)
212
213 def Sort(self):
214 self.SortChildren(self.GetRootItem())
215
216 def _OnSelChanged(self, event):
217 self.parent.DoOnSelChanged()
218
219 def _OnItemActivated(self, event):
220 self.DoOnClassify()
221
222 def _OnMsgLayerChanged(self, layer):
223 assert(isinstance(layer, Layer))
224
225 id = self.layer2id[layer]
226
227 self.__FillTreeLayer(id)
228
229 def _OnMsgMapStackingChanged(self, *args):
230 self.Sort()
231
232 def _OnMsgMapLayersChanged(self, map):
233 assert(id(map) == id(self.map))
234
235 self.__FillTree(self.map)
236
237 def __FillTree(self, map):
238
239 assert(isinstance(map, Map))
240
241 self.Freeze()
242
243 self.__DeleteAllItems()
244
245 if map.HasLayers():
246
247 self.image_list = wxImageList(BMP_SIZE_W, BMP_SIZE_H, False, 0)
248
249 bmp = wxEmptyBitmap(BMP_SIZE_W, BMP_SIZE_H)
250 dc = wxMemoryDC()
251 dc.SelectObject(bmp)
252 dc.SetBrush(wxBLACK_BRUSH)
253 dc.Clear()
254 dc.SelectObject(wxNullBitmap)
255
256 self.emptyImageIndex = \
257 self.image_list.AddWithColourMask(bmp, wxColour(0, 0, 0))
258
259 self.AssignImageList(self.image_list)
260
261 root = self.AddRoot("")
262
263 for l in map.Layers():
264 id = self.PrependItem(root, l.Title())
265 l.Subscribe(LAYER_CHANGED, self._OnMsgLayerChanged)
266 self.SetPyData(id, l)
267 font = self.GetItemFont(id)
268 if not l.Visible():
269 font.SetStyle(wxITALIC)
270 self.SetItemFont(id, font)
271
272 self.layer2id[l] = id
273
274 self.__FillTreeLayer(id)
275 self.Expand(id)
276
277 self.Thaw()
278
279 def __FillTreeLayer(self, pid):
280 layer = self.GetPyData(pid)
281
282 self.Freeze()
283
284 self.DeleteChildren(pid)
285
286 clazz = layer.GetClassification()
287
288 shapeType = layer.ShapeType()
289
290 for g in clazz:
291 id = self.AppendItem(pid, g.GetDisplayText())
292 self.SetPyData(id, g)
293
294 bmp = self.__BuildGroupImage(g, shapeType)
295
296 if bmp is None:
297 self.SetItemImage(id, self.emptyImageIndex)
298 else:
299 i = self.image_list.Add(bmp)
300 self.SetItemImage(id, i)
301
302 #self.layer2id[g] = id
303
304 self.Thaw()
305
306 def __BuildGroupImage(self, group, shapeType):
307 assert(isinstance(group, ClassGroup))
308
309 bmp = wxEmptyBitmap(BMP_SIZE_W, BMP_SIZE_H)
310 #brush = wxBrush(Color2wxColour(item[1]), wxSOLID)
311 dc = wxMemoryDC()
312 dc.SelectObject(bmp)
313 dc.Clear()
314
315 self.previewer.Draw(dc, None, group.GetProperties(), shapeType)
316
317 return bmp
318
319 def __DeleteAllItems(self):
320 self.DeleteAllItems()
321 self.layer2id = {}
322
323
324 def __ShowHideLayer(self, show):
325 id = self.GetSelection()
326 assert(id.IsOk())
327
328 item = self.GetPyData(id)
329 if isinstance(item, ClassGroup):
330 id = self.GetItemParent(id)
331 assert(id.IsOk())
332 item = self.GetPyData(id)
333
334
335 if show != item.Visible():
336
337 item.SetVisible(show)
338
339 font = self.GetItemFont(id)
340 if show:
341 font.SetStyle(wxNORMAL)
342 self.SetItemFont(id, font)
343 else:
344 font.SetStyle(wxITALIC)
345 self.SetItemFont(id, font)
346
347
348

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26