9 |
|
|
10 |
__version__ = "$Revision$" |
__version__ = "$Revision$" |
11 |
|
|
12 |
from types import StringType |
from types import StringType, UnicodeType |
13 |
|
|
14 |
from wxPython.wx import * |
from wxPython.wx import * |
15 |
|
|
16 |
|
from Thuban import _ |
17 |
|
from Thuban.UI.common import * |
18 |
|
|
19 |
|
from Thuban.Model.color import Color |
20 |
|
|
21 |
from Thuban.Model.messages import CHANGED |
from Thuban.Model.messages import CHANGED |
22 |
from Thuban.Model.layer import Layer |
from Thuban.Model.layer import Layer |
23 |
from Thuban.Model.map import Map |
from Thuban.Model.map import Map |
24 |
|
|
25 |
from dialogs import NonModalDialog |
from dialogs import NonModalDialog |
26 |
from messages import SESSION_CHANGED, SELECTED_LAYER |
from messages import SESSION_REPLACED, SELECTED_LAYER |
27 |
|
|
28 |
|
BMP_SIZE = 15 |
29 |
|
|
30 |
class SessionTreeCtrl(wxTreeCtrl): |
class SessionTreeCtrl(wxTreeCtrl): |
31 |
|
|
50 |
""" |
""" |
51 |
|
|
52 |
def __init__(self, parent, ID, app): |
def __init__(self, parent, ID, app): |
53 |
|
|
54 |
# 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. |
55 |
wxTreeCtrl.__init__(self, parent, ID) |
wxTreeCtrl.__init__(self, parent, ID) |
56 |
|
|
62 |
# Dictionary mapping layer id's to tree items |
# Dictionary mapping layer id's to tree items |
63 |
self.layer_to_item = {} |
self.layer_to_item = {} |
64 |
|
|
65 |
self.app.Subscribe(SESSION_CHANGED, self.session_changed) |
self.app.Subscribe(SESSION_REPLACED, self.session_changed) |
66 |
self.app.interactor.Subscribe(SELECTED_LAYER, self.layer_selected) |
self.app.interactor.Subscribe(SELECTED_LAYER, 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 |
70 |
|
|
71 |
|
|
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 |
|
|
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_CHANGED, self.session_changed) |
self.app.Unsubscribe(SESSION_REPLACED, self.session_changed) |
82 |
self.app.interactor.Unsubscribe(SELECTED_LAYER, self.layer_selected) |
self.app.interactor.Unsubscribe(SELECTED_LAYER, 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) |
89 |
|
|
90 |
|
bmp = wxEmptyBitmap(BMP_SIZE, BMP_SIZE) |
91 |
|
dc = wxMemoryDC() |
92 |
|
dc.SelectObject(bmp) |
93 |
|
dc.SetBrush(wxBLACK_BRUSH) |
94 |
|
dc.Clear() |
95 |
|
dc.SelectObject(wxNullBitmap) |
96 |
|
|
97 |
|
self.emptyImageIndex = \ |
98 |
|
self.image_list.AddWithColourMask(bmp, wxColour(0, 0, 0)) |
99 |
|
|
100 |
|
self.AssignImageList(self.image_list) |
101 |
|
|
102 |
session = self.app.session |
session = self.app.session |
103 |
info = session.TreeInfo() |
info = session.TreeInfo() |
104 |
root = self.AddRoot(info[0]) |
root = self.AddRoot(info[0], -1, -1, None) |
105 |
|
self.SetItemImage(root, self.emptyImageIndex) |
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 |
119 |
self.SelectItem(item) |
self.SelectItem(item) |
120 |
|
|
121 |
def add_items(self, parent, items): |
def add_items(self, parent, items): |
122 |
|
|
123 |
|
if items is None: return |
124 |
|
|
125 |
for item in items: |
for item in items: |
126 |
if hasattr(item, "TreeInfo"): |
if hasattr(item, "TreeInfo"): |
127 |
# Supports the TreeInfo protocol |
# Supports the TreeInfo protocol |
128 |
info = item.TreeInfo() |
info = item.TreeInfo() |
129 |
treeitem = self.AppendItem(parent, info[0]) |
treeitem = self.AppendItem(parent, info[0], -1, -1, None) |
130 |
|
self.SetItemImage(treeitem, self.emptyImageIndex) |
131 |
self.SetPyData(treeitem, item) |
self.SetPyData(treeitem, item) |
132 |
self.add_items(treeitem, info[1]) |
self.add_items(treeitem, info[1]) |
133 |
self.Expand(treeitem) |
self.Expand(treeitem) |
134 |
if isinstance(item, Layer): |
if isinstance(item, Layer): |
135 |
self.layer_to_item[id(item)] = treeitem |
self.layer_to_item[id(item)] = treeitem |
136 |
elif isinstance(item, StringType): |
elif isinstance(item, StringType) or \ |
137 |
|
isinstance(item, UnicodeType): |
138 |
# it's a string |
# it's a string |
139 |
# FIXME: What to do about UNICODE |
treeitem = self.AppendItem(parent, item, -1, -1, None) |
140 |
self.AppendItem(parent, item) |
self.SetItemImage(treeitem, self.emptyImageIndex) |
141 |
else: |
else: |
142 |
# assume its a sequence (title, items) |
# assume its a sequence (title, items) |
143 |
treeitem = self.AppendItem(parent, item[0]) |
if isinstance(item[1], Color): |
144 |
self.add_items(treeitem, item[1]) |
|
145 |
|
treeitem = self.AppendItem(parent, "(%s)" % item[0]) |
146 |
|
|
147 |
|
bmp = wxEmptyBitmap(BMP_SIZE, BMP_SIZE) |
148 |
|
brush = wxBrush(Color2wxColour(item[1]), wxSOLID) |
149 |
|
dc = wxMemoryDC() |
150 |
|
dc.SelectObject(bmp) |
151 |
|
dc.SetBrush(brush) |
152 |
|
dc.Clear() |
153 |
|
dc.DrawRoundedRectangle(0, 0, |
154 |
|
bmp.GetWidth(), bmp.GetHeight(), |
155 |
|
4) |
156 |
|
dc.SelectObject(wxNullBitmap) |
157 |
|
|
158 |
|
i = self.image_list.Add(bmp) |
159 |
|
self.SetItemImage(treeitem, i) |
160 |
|
else: |
161 |
|
treeitem = self.AppendItem(parent, item[0], -1, -1, None) |
162 |
|
self.SetItemImage(treeitem, self.emptyImageIndex) |
163 |
|
self.add_items(treeitem, item[1]) |
164 |
self.Expand(treeitem) |
self.Expand(treeitem) |
165 |
|
|
166 |
def session_changed(self, *args): |
def session_changed(self, *args): |
182 |
if isinstance(object, Layer) or isinstance(object, Map): |
if isinstance(object, Layer) or isinstance(object, Map): |
183 |
break |
break |
184 |
item = self.GetItemParent(item) |
item = self.GetItemParent(item) |
185 |
|
else: |
186 |
|
# No layer or map was found in the chain of parents, so |
187 |
|
# there's nothing we can do. |
188 |
|
return |
189 |
|
|
190 |
self.changing_selection = 1 |
self.changing_selection = 1 |
191 |
try: |
try: |
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, "Session") |
NonModalDialog.__init__(self, parent, app.interactor, name, |
227 |
|
_("Session")) |
228 |
self.tree = SessionTreeCtrl(self, -1, app) |
self.tree = SessionTreeCtrl(self, -1, app) |
229 |
|
|
230 |
def OnClose(self, event): |
def OnClose(self, event): |
|
#self.interactor.Unsubscribe(SELECTED_SHAPE, self.select_shape) |
|
231 |
NonModalDialog.OnClose(self, event) |
NonModalDialog.OnClose(self, event) |
232 |
|
|
233 |
# if there were a way to get notified when the tree control |
# if there were a way to get notified when the tree control |
235 |
# of doing it here. (EVT_WINDOW_DESTROY doesn't seem to sent at |
# of doing it here. (EVT_WINDOW_DESTROY doesn't seem to sent at |
236 |
# all) |
# all) |
237 |
self.tree.unsubscribe_all() |
self.tree.unsubscribe_all() |
238 |
|
|