/[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 562 - (show annotations)
Wed Mar 26 11:06:22 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: 11224 byte(s)
Added some more buttons and made the LegendPanel a DockPanel.

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
14 from Thuban.Model.layer import Layer
15 from Thuban.Model.map import Map
16 from Thuban.Model.messages import *
17 from Thuban.Model.classification import ClassGroup
18
19 from Thuban.UI.classifier import ClassDataPreviewer
20 from Thuban.UI.dock import DockPanel
21
22 ID_LEGEND_MOVEUP = 4001
23 ID_LEGEND_MOVEDOWN = 4002
24 ID_LEGEND_TREE = 4003
25 ID_LEGEND_CLASSIFY = 4004
26 ID_LEGEND_SHOWLAYER = 4005
27 ID_LEGEND_HIDELAYER = 4006
28 ID_LEGEND_DOCK = 4007
29 ID_LEGEND_UNDOCK = 4008
30
31 BMP_SIZE_W = 30
32 BMP_SIZE_H = 15
33
34 #class LegendDialog(NonModalDialog):
35
36 #def __init__(self, parent, name, panel):
37 #NonModalDialog.__init__(self, parent, name,
38 #_("Legend: %s") % map.Title())
39
40 ##topBox = wxBoxSizer(wxVERTICAL)
41
42 #panel = LegendPanel(self, map)
43
44 #self.panel = panel
45
46 #topBox.Add(panel, 1, wxGROW, 4)
47
48 ##self.SetAutoLayout(True)
49 ##self.SetSizer(topBox)
50
51 #def GetLegendPanel(self):
52 #return self.panel
53
54 #def OnClose(self, event):
55 #NonModalDialog.OnClose(self, event)
56
57 #self.panel.SetMap(None)
58
59 class LegendPanel(DockPanel):
60
61 def __init__(self, parent, map, mainWindow):
62 DockPanel.__init__(self, parent, -1)
63
64 self.mainWindow = mainWindow
65 self.parent = parent
66
67 panelBox = wxBoxSizer(wxVERTICAL)
68
69 buttonBox = wxGridSizer(2, 3, 0, 0)
70
71 self.buttons = []
72
73 button = wxButton(self, ID_LEGEND_MOVEUP, _("Move Up"))
74 buttonBox.Add(button, 0, wxGROW | wxLEFT | wxRIGHT, 0)
75 self.buttons.append(button)
76
77 button = wxButton(self, ID_LEGEND_SHOWLAYER, _("Show Layer"))
78 buttonBox.Add(button, 0, wxGROW | wxLEFT | wxRIGHT, 0)
79 self.buttons.append(button)
80
81 button = wxButton(self, ID_LEGEND_CLASSIFY, _("Classify"))
82 buttonBox.Add(button, 0, wxGROW | wxLEFT | wxRIGHT, 0)
83 self.buttons.append(button)
84
85 button = wxButton(self, ID_LEGEND_MOVEDOWN, _("Move Down"))
86 buttonBox.Add(button, 0, wxGROW | wxLEFT | wxRIGHT, 0)
87 self.buttons.append(button)
88
89 button = wxButton(self, ID_LEGEND_HIDELAYER, _("Hide Layer"))
90 buttonBox.Add(button, 0, wxGROW | wxLEFT | wxRIGHT, 0)
91 self.buttons.append(button)
92
93 # button = wxButton(self, ID_LEGEND_DOCK, _("Dock"))
94 # buttonBox.Add(button, 0, wxGROW | wxLEFT | wxRIGHT, 0)
95 # self.buttons.append(button)
96
97 # button = wxButton(self, ID_LEGEND_UNDOCK, _("Undock"))
98 # buttonBox.Add(button, 0, wxGROW | wxLEFT | wxRIGHT, 0)
99 # self.buttons.append(button)
100
101
102 EVT_BUTTON(self, ID_LEGEND_MOVEUP, self._OnMoveUp)
103 EVT_BUTTON(self, ID_LEGEND_MOVEDOWN, self._OnMoveDown)
104 EVT_BUTTON(self, ID_LEGEND_CLASSIFY, self._OnClassify)
105 EVT_BUTTON(self, ID_LEGEND_SHOWLAYER, self._OnShowLayer)
106 EVT_BUTTON(self, ID_LEGEND_HIDELAYER, self._OnHideLayer)
107 #EVT_BUTTON(self, ID_LEGEND_DOCK, self._OnDock)
108 #EVT_BUTTON(self, ID_LEGEND_UNDOCK, self._OnUndock)
109
110 panelBox.Add(buttonBox, 0, 0, 4)
111
112 self.tree = LegendTree(self, ID_LEGEND_TREE, map, mainWindow)
113
114 panelBox.Add(self.tree, 1, wxGROW, 4)
115
116 self.SetAutoLayout(True)
117 self.SetSizer(panelBox)
118 panelBox.SetSizeHints(self)
119
120 #panelBox.SetSizeHints(self.parent)
121
122 self.panelBox = panelBox
123
124 def GetMap(self):
125 return self.tree.GetMap()
126
127 def SetMap(self, map):
128 self.tree.SetMap(map)
129
130 def DoOnSelChanged(self):
131 self.__EnableButtons(self.tree.GetSelection().IsOk())
132
133 def _OnClassify(self, event):
134 self.tree.DoOnClassify()
135
136 def _OnMoveUp(self, event):
137 self.tree.MoveCurrentItemUp()
138
139 def _OnMoveDown(self, event):
140 self.tree.MoveCurrentItemDown()
141
142 def _OnShowLayer(self, event):
143 self.tree.DoOnShowLayer()
144 pass
145
146 def _OnHideLayer(self, event):
147 self.tree.DoOnHideLayer()
148 pass
149
150 def _OnDock(self, event):
151 self.Dock()
152
153 def _OnUndock(self, event):
154 self.UnDock()
155
156 def __EnableButtons(self, on):
157 for b in self.buttons:
158 b.Enable(on)
159
160 class LegendTree(wxTreeCtrl):
161
162 def __init__(self, parent, id, map, mainWindow):
163 wxTreeCtrl.__init__(self, parent, id,
164 style = wxTR_DEFAULT_STYLE | wxTR_HIDE_ROOT,
165 size = (200, 200))
166
167 self.map = None
168 self.parent = parent
169 self.layer2id = None
170
171
172 self.image_list = None
173 self.emptyImageIndex = 0
174
175 self.previewer = ClassDataPreviewer()
176
177 EVT_TREE_ITEM_ACTIVATED(self, ID_LEGEND_TREE, self._OnItemActivated)
178 EVT_TREE_SEL_CHANGED(self, ID_LEGEND_TREE, self._OnSelChanged)
179
180 self.SetMap(map)
181
182 def GetMap(self):
183 return self.map
184
185 def SetMap(self, map):
186
187 sub_list = [(MAP_STACKING_CHANGED, self._OnMsgMapStackingChanged),
188 (MAP_LAYERS_ADDED, self._OnMsgMapLayersAddedRemoved),
189 (MAP_LAYERS_REMOVED, self._OnMsgMapLayersAddedRemoved)]
190
191 if self.map is not None:
192 for msg, func in sub_list: self.map.Unsubscribe(msg, func)
193 self.__DeleteAllItems()
194
195 self.map = map
196
197 if self.map is not None:
198 for msg, func in sub_list: self.map.Subscribe(msg, func)
199 self.__FillTree(self.map)
200
201
202 def MoveCurrentItemUp(self):
203 cur_id = self.GetSelection()
204 assert(cur_id.IsOk())
205
206 cur_data = self.GetPyData(cur_id)
207
208 #prev_id = self.GetPrevSibling(cur_id)
209
210 #
211 # Get out if there's nowhere to go
212 #
213 #if prev_id == INVALID_TREE_ID: return
214
215 if isinstance(cur_data, Layer):
216 self.map.RaiseLayer(cur_data)
217 elif isinstance(cur_data, ClassGroup):
218 pass
219 else:
220 assert(False, "Shouldn't be here.")
221 pass
222
223 def MoveCurrentItemDown(self):
224 cur_id = self.GetSelection()
225 assert(cur_id.IsOk())
226
227 cur_data = self.GetPyData(cur_id)
228
229 if isinstance(cur_data, Layer):
230 self.map.LowerLayer(cur_data)
231 elif isinstance(cur_data, ClassGroup):
232 pass
233 else:
234 assert(False, "Shouldn't be here.")
235 pass
236
237
238 def OnCompareItems(self, item1, item2):
239
240 data1 = self.GetPyData(item1)
241 data2 = self.GetPyData(item2)
242
243 if isinstance(data1, Layer):
244 layers = self.map.Layers()
245 return layers.index(data2) - layers.index(data1)
246 else:
247 return wxTreeCtrl.OnCompareItems(self, item1, item2)
248
249
250 def DoOnShowLayer(self):
251 self.__ShowHideLayer(True)
252
253 def DoOnHideLayer(self):
254 self.__ShowHideLayer(False)
255
256 def DoOnClassify(self):
257 id = self.GetSelection()
258 assert(id.IsOk())
259 assert(id.IsOk())
260
261 item = self.GetPyData(id)
262 if isinstance(item, ClassGroup):
263 id = self.GetItemParent(id)
264 assert(id.IsOk())
265 item = self.GetPyData(id)
266
267 # XXX: THIS IS SUCH AWFUL STYLE! YUCK!
268 self.parent.mainWindow.OpenClassifier(item)
269 #assert(False, "XXX: FIXME HERE")
270
271 def Sort(self):
272 self.SortChildren(self.GetRootItem())
273
274 def _OnSelChanged(self, event):
275 self.parent.DoOnSelChanged()
276
277 def _OnItemActivated(self, event):
278 self.DoOnClassify()
279
280 def _OnMsgLayerChanged(self, layer):
281 assert(isinstance(layer, Layer))
282
283 id = self.layer2id[layer]
284
285 self.__FillTreeLayer(id)
286
287 def _OnMsgMapStackingChanged(self, *args):
288 self.Sort()
289 id = self.GetSelection()
290
291 if id.IsOk():
292 self.EnsureVisible(id)
293
294 def _OnMsgMapLayersAddedRemoved(self, map):
295 assert(id(map) == id(self.map))
296
297 self.__FillTree(self.map)
298
299 def __FillTree(self, map):
300
301 assert(isinstance(map, Map))
302
303 self.Freeze()
304
305 self.__DeleteAllItems()
306
307 if map.HasLayers():
308
309 self.image_list = wxImageList(BMP_SIZE_W, BMP_SIZE_H, False, 0)
310
311 bmp = wxEmptyBitmap(BMP_SIZE_W, BMP_SIZE_H)
312 dc = wxMemoryDC()
313 dc.SelectObject(bmp)
314 dc.SetBrush(wxBLACK_BRUSH)
315 dc.Clear()
316 dc.SelectObject(wxNullBitmap)
317
318 self.emptyImageIndex = \
319 self.image_list.AddWithColourMask(bmp, wxColour(0, 0, 0))
320
321 self.AssignImageList(self.image_list)
322
323 root = self.AddRoot("")
324
325 for l in map.Layers():
326 id = self.PrependItem(root, l.Title())
327 l.Subscribe(LAYER_CHANGED, self._OnMsgLayerChanged)
328 self.SetPyData(id, l)
329 font = self.GetItemFont(id)
330 if not l.Visible():
331 font.SetStyle(wxITALIC)
332 self.SetItemFont(id, font)
333
334 self.layer2id[l] = id
335
336 self.__FillTreeLayer(id)
337 self.Expand(id)
338
339 self.Thaw()
340
341 def __FillTreeLayer(self, pid):
342 layer = self.GetPyData(pid)
343
344 self.Freeze()
345
346 self.DeleteChildren(pid)
347
348 clazz = layer.GetClassification()
349
350 shapeType = layer.ShapeType()
351
352 for g in clazz:
353 id = self.AppendItem(pid, g.GetDisplayText())
354 self.SetPyData(id, g)
355
356 bmp = self.__BuildGroupImage(g, shapeType)
357
358 if bmp is None:
359 self.SetItemImage(id, self.emptyImageIndex)
360 else:
361 i = self.image_list.Add(bmp)
362 self.SetItemImage(id, i)
363
364 #self.layer2id[g] = id
365
366 self.Thaw()
367
368 def __BuildGroupImage(self, group, shapeType):
369 assert(isinstance(group, ClassGroup))
370
371 bmp = wxEmptyBitmap(BMP_SIZE_W, BMP_SIZE_H)
372 #brush = wxBrush(Color2wxColour(item[1]), wxSOLID)
373 dc = wxMemoryDC()
374 dc.SelectObject(bmp)
375 dc.Clear()
376
377 self.previewer.Draw(dc, None, group.GetProperties(), shapeType)
378
379 return bmp
380
381 def __DeleteAllItems(self):
382 self.DeleteAllItems()
383 self.layer2id = {}
384
385
386 def __ShowHideLayer(self, show):
387 id = self.GetSelection()
388 assert(id.IsOk())
389
390 item = self.GetPyData(id)
391 if isinstance(item, ClassGroup):
392 id = self.GetItemParent(id)
393 assert(id.IsOk())
394 item = self.GetPyData(id)
395
396
397 if show != item.Visible():
398
399 item.SetVisible(show)
400
401 font = self.GetItemFont(id)
402 if show:
403 font.SetStyle(wxNORMAL)
404 self.SetItemFont(id, font)
405 else:
406 font.SetStyle(wxITALIC)
407 self.SetItemFont(id, font)
408
409
410

Properties

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26