1 |
#! /usr/bin/python |
# Copyright (c) 2001, 2002, 2003 by Intevation GmbH |
|
# Copyright (c) 2001, 2002 by Intevation GmbH |
|
2 |
# Authors: |
# Authors: |
3 |
# Jan-Oliver Wagner <[email protected]> |
# Jan-Oliver Wagner <[email protected]> |
4 |
# Bernhard Herzog <[email protected]> |
# Bernhard Herzog <[email protected]> |
10 |
|
|
11 |
from types import StringType, UnicodeType |
from types import StringType, UnicodeType |
12 |
|
|
13 |
from wxPython.wx import * |
import wx |
14 |
|
|
15 |
from Thuban import _ |
from Thuban import _ |
16 |
from Thuban.UI.common import * |
from Thuban.UI.common import Color2wxColour |
17 |
|
|
18 |
from Thuban.Model.color import Color |
from Thuban.Model.color import Color |
19 |
|
|
21 |
from Thuban.Model.layer import Layer |
from Thuban.Model.layer import Layer |
22 |
from Thuban.Model.map import Map |
from Thuban.Model.map import Map |
23 |
|
|
24 |
from dialogs import NonModalDialog |
from dialogs import NonModalNonParentDialog |
25 |
from messages import SESSION_REPLACED, SELECTED_LAYER |
from messages import SESSION_REPLACED, LAYER_SELECTED |
26 |
|
|
27 |
BMP_SIZE = 15 |
BMP_SIZE = 15 |
28 |
|
|
29 |
class SessionTreeCtrl(wxTreeCtrl): |
class SessionTreeCtrl(wx.TreeCtrl): |
30 |
|
|
31 |
"""Widget to display a tree view of the session. |
"""Widget to display a tree view of the session. |
32 |
|
|
48 |
value of TreeInfo. |
value of TreeInfo. |
49 |
""" |
""" |
50 |
|
|
51 |
def __init__(self, parent, ID, app): |
def __init__(self, parent, ID, mainwindow, app): |
52 |
|
|
53 |
# Use the WANTS_CHARS style so the panel doesn't eat the Return key. |
# Use the WANTS_CHARS style so the panel doesn't eat the Return key. |
54 |
wxTreeCtrl.__init__(self, parent, ID) |
wx.TreeCtrl.__init__(self, parent, ID) |
55 |
|
|
56 |
|
self.mainwindow = mainwindow |
57 |
self.app = app |
self.app = app |
58 |
# boolean to indicate that we manipulate the selection ourselves |
# boolean to indicate that we manipulate the selection ourselves |
59 |
# so that we can ignore the selection events generated |
# so that we can ignore the selection events generated |
63 |
self.layer_to_item = {} |
self.layer_to_item = {} |
64 |
|
|
65 |
self.app.Subscribe(SESSION_REPLACED, self.session_changed) |
self.app.Subscribe(SESSION_REPLACED, self.session_changed) |
66 |
self.app.interactor.Subscribe(SELECTED_LAYER, self.layer_selected) |
self.mainwindow.Subscribe(LAYER_SELECTED, self.layer_selected) |
67 |
|
|
68 |
# the session currently displayed in the tree |
# the session currently displayed in the tree |
69 |
self.session = None |
self.session = None |
72 |
# pretend the session has changed to build the initial tree |
# pretend the session has changed to build the initial tree |
73 |
self.session_changed() |
self.session_changed() |
74 |
|
|
75 |
EVT_TREE_SEL_CHANGED(self, self.GetId(), self.OnSelChanged) |
self.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnSelChanged, id=self.GetId()) |
76 |
|
|
77 |
def unsubscribe_all(self): |
def unsubscribe_all(self): |
78 |
if self.session is not None: |
if self.session is not None: |
79 |
self.session.Unsubscribe(CHANGED, self.update_tree) |
self.session.Unsubscribe(CHANGED, self.update_tree) |
80 |
self.session = None |
self.session = None |
81 |
self.app.Unsubscribe(SESSION_REPLACED, self.session_changed) |
self.app.Unsubscribe(SESSION_REPLACED, self.session_changed) |
82 |
self.app.interactor.Unsubscribe(SELECTED_LAYER, self.layer_selected) |
self.mainwindow.Unsubscribe(LAYER_SELECTED, self.layer_selected) |
83 |
|
|
84 |
def update_tree(self, *args): |
def update_tree(self, *args): |
85 |
"""Clear and rebuild the tree""" |
"""Clear and rebuild the tree""" |
86 |
self.DeleteAllItems() |
self.DeleteAllItems() |
87 |
self.layer_to_item.clear() |
self.layer_to_item.clear() |
88 |
self.image_list = wxImageList(BMP_SIZE, BMP_SIZE, False, 0) |
self.image_list = wx.ImageList(BMP_SIZE, BMP_SIZE, False, 0) |
89 |
|
|
90 |
bmp = wxEmptyBitmap(BMP_SIZE, BMP_SIZE) |
bmp = wx.EmptyBitmap(BMP_SIZE, BMP_SIZE) |
91 |
dc = wxMemoryDC() |
dc = wx.MemoryDC() |
92 |
dc.SelectObject(bmp) |
dc.SelectObject(bmp) |
93 |
dc.SetBrush(wxBLACK_BRUSH) |
dc.SetBrush(wx.BLACK_BRUSH) |
94 |
dc.Clear() |
dc.Clear() |
95 |
dc.SelectObject(wxNullBitmap) |
dc.SelectObject(wx.NullBitmap) |
96 |
|
|
97 |
self.emptyImageIndex = \ |
self.emptyImageIndex = \ |
98 |
self.image_list.AddWithColourMask(bmp, wxColour(0, 0, 0)) |
self.image_list.AddWithColourMask(bmp, wx.Colour(0, 0, 0)) |
99 |
|
|
100 |
self.AssignImageList(self.image_list) |
self.AssignImageList(self.image_list) |
101 |
|
|
106 |
self.add_items(root, info[1]) |
self.add_items(root, info[1]) |
107 |
self.Expand(root) |
self.Expand(root) |
108 |
# select the selected layer |
# select the selected layer |
109 |
selected_layer = self.app.interactor.selected_layer |
selected_layer = self.mainwindow.current_layer() |
110 |
if selected_layer is not None: |
if selected_layer is not None: |
111 |
# One would expect that the selected_layer's id is in |
# One would expect that the selected_layer's id is in |
112 |
# layer_to_item at this point as we've just rebuilt that |
# layer_to_item at this point as we've just rebuilt that |
113 |
# mapping completely. However, when a new session is loaded |
# mapping completely. However, when a new session is loaded |
114 |
# for instance, it can happen that the tree view is updated |
# for instance, it can happen that the tree view is updated |
115 |
# before the interactor in which case selected_layer may be |
# before the canvas's selection in which case selected_layer |
116 |
# a layer of the old session. |
# may be a layer of the old session. |
117 |
item = self.layer_to_item.get(id(selected_layer)) |
item = self.layer_to_item.get(id(selected_layer)) |
118 |
if item is not None: |
if item is not None: |
119 |
self.SelectItem(item) |
self.SelectItem(item) |
144 |
|
|
145 |
treeitem = self.AppendItem(parent, "(%s)" % item[0]) |
treeitem = self.AppendItem(parent, "(%s)" % item[0]) |
146 |
|
|
147 |
bmp = wxEmptyBitmap(BMP_SIZE, BMP_SIZE) |
bmp = wx.EmptyBitmap(BMP_SIZE, BMP_SIZE) |
148 |
brush = wxBrush(Color2wxColour(item[1]), wxSOLID) |
brush = wx.Brush(Color2wxColour(item[1]), wx.SOLID) |
149 |
dc = wxMemoryDC() |
dc = wx.MemoryDC() |
150 |
dc.SelectObject(bmp) |
dc.SelectObject(bmp) |
151 |
dc.SetBrush(brush) |
dc.SetBrush(brush) |
152 |
dc.Clear() |
dc.Clear() |
153 |
dc.DrawRoundedRectangle(0, 0, |
dc.DrawRoundedRectangle(0, 0, |
154 |
bmp.GetWidth(), bmp.GetHeight(), |
bmp.GetWidth(), bmp.GetHeight(), |
155 |
4) |
4) |
156 |
dc.SelectObject(wxNullBitmap) |
dc.SelectObject(wx.NullBitmap) |
157 |
|
|
158 |
i = self.image_list.Add(bmp) |
i = self.image_list.Add(bmp) |
159 |
self.SetItemImage(treeitem, i) |
self.SetItemImage(treeitem, i) |
208 |
return |
return |
209 |
self.normalize_selection() |
self.normalize_selection() |
210 |
# SelectedLayer returns None if no layer is selected. Since |
# SelectedLayer returns None if no layer is selected. Since |
211 |
# passing None to interactor.SelectLayer deselects the layer we |
# passing None to SelectLayer deselects the layer we can simply |
212 |
# can simply pass the result of SelectedLayer on in all cases |
# pass the result of SelectedLayer on in all cases |
213 |
self.app.interactor.SelectLayer(self.SelectedLayer()) |
self.mainwindow.SelectLayer(self.SelectedLayer()) |
214 |
|
|
215 |
def layer_selected(self, layer): |
def layer_selected(self, layer): |
216 |
item = self.layer_to_item.get(id(layer)) |
item = self.layer_to_item.get(id(layer)) |
218 |
self.SelectItem(item) |
self.SelectItem(item) |
219 |
|
|
220 |
|
|
221 |
class SessionTreeView(NonModalDialog): |
class SessionTreeView(NonModalNonParentDialog): |
222 |
|
|
223 |
"""Non modal dialog showing the session as a tree""" |
"""Non modal dialog showing the session as a tree""" |
224 |
|
|
225 |
def __init__(self, parent, app, name): |
def __init__(self, parent, app, name): |
226 |
NonModalDialog.__init__(self, parent, app.interactor, name, |
NonModalNonParentDialog.__init__(self, parent, name, _("Session")) |
227 |
_("Session")) |
self.tree = SessionTreeCtrl(self, -1, parent, app) |
|
self.tree = SessionTreeCtrl(self, -1, app) |
|
228 |
|
|
229 |
def OnClose(self, event): |
def OnClose(self, event): |
230 |
NonModalDialog.OnClose(self, event) |
NonModalNonParentDialog.OnClose(self, event) |
231 |
|
|
232 |
# if there were a way to get notified when the tree control |
# if there were a way to get notified when the tree control |
233 |
# itself is destroyed we could use that to unsubscribe instead |
# itself is destroyed we could use that to unsubscribe instead |