author | Andrey Skvortsov <andrej.skvortzov@gmail.com> |
Thu, 24 Nov 2016 15:46:17 +0300 | |
changeset 1577 | 6a3c29fdcfea |
parent 1571 | 486f94a8032c |
child 1730 | 64d8f52bc8c8 |
permissions | -rw-r--r-- |
814 | 1 |
#!/usr/bin/env python |
2 |
# -*- coding: utf-8 -*- |
|
3 |
||
1571
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1528
diff
changeset
|
4 |
# This file is part of Beremiz, a Integrated Development Environment for |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1528
diff
changeset
|
5 |
# programming IEC 61131-3 automates supporting plcopen standard and CanFestival. |
814 | 6 |
# |
1571
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1528
diff
changeset
|
7 |
# Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD |
814 | 8 |
# |
1571
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1528
diff
changeset
|
9 |
# See COPYING file for copyrights details. |
814 | 10 |
# |
1571
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1528
diff
changeset
|
11 |
# This program is free software; you can redistribute it and/or |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1528
diff
changeset
|
12 |
# modify it under the terms of the GNU General Public License |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1528
diff
changeset
|
13 |
# as published by the Free Software Foundation; either version 2 |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1528
diff
changeset
|
14 |
# of the License, or (at your option) any later version. |
814 | 15 |
# |
1571
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1528
diff
changeset
|
16 |
# This program is distributed in the hope that it will be useful, |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1528
diff
changeset
|
17 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1528
diff
changeset
|
18 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1528
diff
changeset
|
19 |
# GNU General Public License for more details. |
814 | 20 |
# |
1571
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1528
diff
changeset
|
21 |
# You should have received a copy of the GNU General Public License |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1528
diff
changeset
|
22 |
# along with this program; if not, write to the Free Software |
486f94a8032c
fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1528
diff
changeset
|
23 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
814 | 24 |
|
25 |
import wx |
|
26 |
||
27 |
#------------------------------------------------------------------------------- |
|
28 |
# Helpers |
|
29 |
#------------------------------------------------------------------------------- |
|
30 |
||
31 |
[CATEGORY, BLOCK] = range(2) |
|
32 |
||
33 |
#------------------------------------------------------------------------------- |
|
34 |
# Library Panel |
|
35 |
#------------------------------------------------------------------------------- |
|
36 |
||
1230 | 37 |
""" |
38 |
Class that implements a panel displaying a tree containing an hierarchical list |
|
39 |
of functions and function blocks available in project an a search control for |
|
40 |
quickly find one functions or function blocks in this list and a text control |
|
41 |
displaying informations about selected functions or function blocks |
|
42 |
""" |
|
43 |
||
814 | 44 |
class LibraryPanel(wx.Panel): |
45 |
||
46 |
def __init__(self, parent, enable_drag=False): |
|
1230 | 47 |
""" |
48 |
Constructor |
|
49 |
@param parent: Parent wx.Window of LibraryPanel |
|
50 |
@param enable_drag: Flag indicating that function or function block can |
|
51 |
be drag'n drop from LibraryPanel (default: False) |
|
52 |
""" |
|
814 | 53 |
wx.Panel.__init__(self, parent, style=wx.TAB_TRAVERSAL) |
54 |
||
1230 | 55 |
# Define LibraryPanel main sizer |
814 | 56 |
main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=0) |
57 |
main_sizer.AddGrowableCol(0) |
|
58 |
main_sizer.AddGrowableRow(1) |
|
59 |
||
1230 | 60 |
# Add SearchCtrl to main sizer |
814 | 61 |
self.SearchCtrl = wx.SearchCtrl(self) |
1230 | 62 |
# Add a button with a magnifying glass, essentially to show that this |
63 |
# control is for searching in tree |
|
814 | 64 |
self.SearchCtrl.ShowSearchButton(True) |
65 |
self.Bind(wx.EVT_TEXT, self.OnSearchCtrlChanged, self.SearchCtrl) |
|
66 |
self.Bind(wx.EVT_SEARCHCTRL_SEARCH_BTN, |
|
1230 | 67 |
self.OnSearchButtonClick, self.SearchCtrl) |
68 |
# Bind keyboard event on SearchCtrl text control to catch UP and DOWN |
|
69 |
# for search previous and next occurrence |
|
1528
d551f2925a86
Fixed crash when starts on OS X with wxPython 3.0.x
alexander@macbook-pro-alexander.local
parents:
1501
diff
changeset
|
70 |
|
d551f2925a86
Fixed crash when starts on OS X with wxPython 3.0.x
alexander@macbook-pro-alexander.local
parents:
1501
diff
changeset
|
71 |
# This protects from fail to start when no children[0] available (possible for wxPython 3.0) |
d551f2925a86
Fixed crash when starts on OS X with wxPython 3.0.x
alexander@macbook-pro-alexander.local
parents:
1501
diff
changeset
|
72 |
if self.SearchCtrl.GetChildren(): |
d551f2925a86
Fixed crash when starts on OS X with wxPython 3.0.x
alexander@macbook-pro-alexander.local
parents:
1501
diff
changeset
|
73 |
search_textctrl = self.SearchCtrl.GetChildren()[0] |
d551f2925a86
Fixed crash when starts on OS X with wxPython 3.0.x
alexander@macbook-pro-alexander.local
parents:
1501
diff
changeset
|
74 |
search_textctrl.Bind(wx.EVT_CHAR, self.OnKeyDown) |
d551f2925a86
Fixed crash when starts on OS X with wxPython 3.0.x
alexander@macbook-pro-alexander.local
parents:
1501
diff
changeset
|
75 |
|
814 | 76 |
main_sizer.AddWindow(self.SearchCtrl, flag=wx.GROW) |
77 |
||
1230 | 78 |
# Add Splitter window for tree and block comment to main sizer |
814 | 79 |
splitter_window = wx.SplitterWindow(self) |
80 |
splitter_window.SetSashGravity(1.0) |
|
81 |
main_sizer.AddWindow(splitter_window, flag=wx.GROW) |
|
82 |
||
1230 | 83 |
# Add TreeCtrl for functions and function blocks library in splitter |
84 |
# window |
|
814 | 85 |
self.Tree = wx.TreeCtrl(splitter_window, |
86 |
size=wx.Size(0, 0), |
|
87 |
style=wx.TR_HAS_BUTTONS| |
|
88 |
wx.TR_SINGLE| |
|
89 |
wx.SUNKEN_BORDER| |
|
90 |
wx.TR_HIDE_ROOT| |
|
91 |
wx.TR_LINES_AT_ROOT) |
|
92 |
self.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnTreeItemSelected, self.Tree) |
|
93 |
self.Tree.Bind(wx.EVT_CHAR, self.OnKeyDown) |
|
1230 | 94 |
# If drag'n drop is enabled, bind event generated when a drag begins on |
95 |
# tree to start a drag'n drop |
|
814 | 96 |
if enable_drag: |
97 |
self.Bind(wx.EVT_TREE_BEGIN_DRAG, self.OnTreeBeginDrag, self.Tree) |
|
98 |
||
1230 | 99 |
# Add TextCtrl for function and function block informations |
814 | 100 |
self.Comment = wx.TextCtrl(splitter_window, size=wx.Size(0, 80), |
101 |
style=wx.TE_READONLY|wx.TE_MULTILINE) |
|
102 |
||
103 |
splitter_window.SplitHorizontally(self.Tree, self.Comment, -80) |
|
104 |
||
105 |
self.SetSizer(main_sizer) |
|
1230 | 106 |
|
107 |
# Reference to the project controller |
|
814 | 108 |
self.Controller = None |
1230 | 109 |
|
110 |
# Variable storing functions and function blocks library to display |
|
814 | 111 |
self.BlockList = None |
112 |
||
113 |
def __del__(self): |
|
1230 | 114 |
""" |
115 |
Destructor |
|
116 |
""" |
|
117 |
# Remove reference to project controller |
|
814 | 118 |
self.Controller = None |
119 |
||
120 |
def SetController(self, controller): |
|
1230 | 121 |
""" |
122 |
Set reference to project controller |
|
123 |
@param controller: Reference to project controller |
|
124 |
""" |
|
814 | 125 |
self.Controller = controller |
126 |
||
127 |
def SetBlockList(self, blocklist): |
|
1230 | 128 |
""" |
129 |
Set function and function block library to display in TreeCtrl |
|
130 |
@param blocklist: Function and function block library |
|
131 |
""" |
|
132 |
# Save functions and function blocks library |
|
814 | 133 |
self.BlockList = blocklist |
1230 | 134 |
# Refresh TreeCtrl values |
814 | 135 |
self.RefreshTree() |
136 |
||
137 |
def SetFocus(self): |
|
1230 | 138 |
""" |
139 |
Called to give focus to LibraryPanel |
|
140 |
Override wx.Window SetFocus method |
|
141 |
""" |
|
142 |
# Give focus to SearchCtrl |
|
814 | 143 |
self.SearchCtrl.SetFocus() |
144 |
||
145 |
def ResetTree(self): |
|
1230 | 146 |
""" |
147 |
Reset LibraryPanel values displayed in controls |
|
148 |
""" |
|
149 |
# Clear SearchCtrl, TreeCtrl and TextCtrl |
|
814 | 150 |
self.SearchCtrl.SetValue("") |
151 |
self.Tree.DeleteAllItems() |
|
152 |
self.Comment.SetValue("") |
|
153 |
||
154 |
def RefreshTree(self): |
|
1230 | 155 |
""" |
156 |
Refresh LibraryPanel values displayed in controls |
|
157 |
""" |
|
158 |
# Get function and function blocks library |
|
159 |
blocktypes = self.BlockList |
|
160 |
if blocktypes is None and self.Controller is not None: |
|
161 |
# Get library from project controller if not defined |
|
162 |
blocktypes = self.Controller.GetBlockTypes() |
|
163 |
||
164 |
# Refresh TreeCtrl values if a library is defined |
|
165 |
if blocktypes is not None: |
|
166 |
# List that will contain tree items to be deleted when TreeCtrl |
|
167 |
# will be refreshed |
|
168 |
items_to_delete = [] |
|
169 |
||
170 |
# Get current selected item for selected it when values refreshed |
|
171 |
selected_item = self.Tree.GetSelection() |
|
172 |
selected_pydata = (self.Tree.GetPyData(selected_item) |
|
173 |
if selected_item.IsOk() and |
|
174 |
selected_item != self.Tree.GetRootItem() |
|
175 |
else None) |
|
176 |
# Don't save selected item if it is a category |
|
177 |
selected_infos = ((self.Tree.GetItemText(selected_item), |
|
178 |
selected_pydata["inputs"]) |
|
179 |
if selected_pydata is not None and |
|
180 |
selected_pydata["type"] == BLOCK |
|
181 |
else (None, None)) |
|
182 |
||
183 |
# Get TreeCtrl root item (hidden) |
|
814 | 184 |
root = self.Tree.GetRootItem() |
185 |
if not root.IsOk(): |
|
1230 | 186 |
# Create root if not present |
814 | 187 |
root = self.Tree.AddRoot("") |
1230 | 188 |
|
189 |
# Iterate over functions and function blocks library categories and |
|
190 |
# add a tree item to root item for each of them |
|
191 |
||
192 |
# Get first child under root item |
|
814 | 193 |
category_item, root_cookie = self.Tree.GetFirstChild(root) |
194 |
for category in blocktypes: |
|
1230 | 195 |
# Store category name in a local variable to prevent script |
196 |
# extracting translated strings for gettext to consider "name" |
|
197 |
# to be translated |
|
814 | 198 |
category_name = category["name"] |
1230 | 199 |
|
200 |
# Tree item already exists, set item label |
|
201 |
if category_item.IsOk(): |
|
202 |
self.Tree.SetItemText(category_item, _(category_name)) |
|
203 |
||
204 |
# Tree item doesn't exist, add new one to root |
|
205 |
else: |
|
814 | 206 |
category_item = self.Tree.AppendItem(root, _(category_name)) |
1230 | 207 |
# On Windows, needs to get next child of root to have a |
208 |
# reference to the newly added tree item |
|
814 | 209 |
if wx.Platform != '__WXMSW__': |
1230 | 210 |
category_item, root_cookie = \ |
211 |
self.Tree.GetNextChild(root, root_cookie) |
|
212 |
||
213 |
# Set data associated to tree item (only save that item is a |
|
214 |
# category) |
|
814 | 215 |
self.Tree.SetPyData(category_item, {"type" : CATEGORY}) |
1230 | 216 |
|
217 |
# Iterate over functions and function blocks defined in library |
|
218 |
# category add a tree item to category tree item for each of |
|
219 |
# them |
|
220 |
||
221 |
# Get first child under category tree item |
|
222 |
blocktype_item, category_cookie = \ |
|
223 |
self.Tree.GetFirstChild(category_item) |
|
814 | 224 |
for blocktype in category["list"]: |
1230 | 225 |
|
226 |
# Tree item already exists, set item label |
|
227 |
if blocktype_item.IsOk(): |
|
228 |
self.Tree.SetItemText(blocktype_item, blocktype["name"]) |
|
229 |
||
230 |
# Tree item doesn't exist, add new one to category item |
|
231 |
else: |
|
232 |
blocktype_item = self.Tree.AppendItem( |
|
233 |
category_item, blocktype["name"]) |
|
234 |
# See comment when adding category |
|
814 | 235 |
if wx.Platform != '__WXMSW__': |
1230 | 236 |
blocktype_item, category_cookie = \ |
237 |
self.Tree.GetNextChild(category_item, |
|
238 |
category_cookie) |
|
239 |
||
240 |
# Define data to associate to block tree item |
|
241 |
comment = blocktype["comment"] |
|
814 | 242 |
block_data = {"type" : BLOCK, |
243 |
"block_type" : blocktype["type"], |
|
1230 | 244 |
"inputs" : tuple([type |
245 |
for name, type, modifier |
|
246 |
in blocktype["inputs"]]), |
|
247 |
"extension" : (len(blocktype["inputs"]) |
|
248 |
if blocktype["extensible"] |
|
249 |
else None), |
|
250 |
"comment": _(comment) + |
|
251 |
blocktype.get("usage", "")} |
|
814 | 252 |
self.Tree.SetPyData(blocktype_item, block_data) |
1230 | 253 |
|
254 |
# Select block tree item in tree if it corresponds to |
|
255 |
# previously selected one |
|
256 |
if selected_infos == (blocktype["name"], |
|
257 |
blocktype["inputs"]): |
|
814 | 258 |
self.Tree.SelectItem(blocktype_item) |
1230 | 259 |
|
260 |
# Update TextCtrl value |
|
261 |
self.Comment.SetValue(block_data["comment"]) |
|
262 |
||
263 |
# Get next block tree item under category tree item |
|
264 |
blocktype_item, category_cookie = \ |
|
265 |
self.Tree.GetNextChild(category_item, category_cookie) |
|
266 |
||
267 |
# Add every remaining tree item under category tree item after |
|
268 |
# updating all block items to the list of items to delete |
|
814 | 269 |
while blocktype_item.IsOk(): |
1230 | 270 |
items_to_delete.append(blocktype_item) |
271 |
blocktype_item, category_cookie = \ |
|
272 |
self.Tree.GetNextChild(category_item, category_cookie) |
|
273 |
||
274 |
# Get next category tree item under root item |
|
275 |
category_item, root_cookie = \ |
|
276 |
self.Tree.GetNextChild(root, root_cookie) |
|
277 |
||
278 |
# Add every remaining tree item under root item after updating all |
|
279 |
# category items to the list of items to delete |
|
814 | 280 |
while category_item.IsOk(): |
1230 | 281 |
items_to_delete.append(category_item) |
282 |
category_item, root_cookie = \ |
|
283 |
self.Tree.GetNextChild(root, root_cookie) |
|
284 |
||
285 |
# Remove all items in list of items to delete from TreeCtrl |
|
286 |
for item in items_to_delete: |
|
814 | 287 |
self.Tree.Delete(item) |
288 |
||
289 |
def GetSelectedBlock(self): |
|
1230 | 290 |
""" |
291 |
Get selected block informations |
|
292 |
@return: {"type": block_type_name, "inputs": [input_type,...]} or None |
|
293 |
if no block selected |
|
294 |
""" |
|
295 |
# Get selected item associated data in tree |
|
296 |
selected_item = self.Tree.GetSelection() |
|
297 |
selected_pydata = (self.Tree.GetPyData(selected_item) |
|
298 |
if selected_item.IsOk() and |
|
299 |
selected_item != self.Tree.GetRootItem() |
|
300 |
else None) |
|
301 |
||
302 |
# Return value is None if selected tree item is root or a category |
|
303 |
return ({"type": self.Tree.GetItemText(selected_item), |
|
304 |
"inputs": selected_pydata["inputs"]} |
|
305 |
if selected_pydata is not None and |
|
306 |
selected_pydata["type"] == BLOCK |
|
307 |
else None) |
|
814 | 308 |
|
309 |
def SelectTreeItem(self, name, inputs): |
|
1230 | 310 |
""" |
311 |
Select Tree item corresponding to block informations given |
|
312 |
@param name: Block type name |
|
313 |
@param inputs: List of block inputs type [input_type,...] |
|
314 |
""" |
|
315 |
# Find tree item corresponding to block informations |
|
814 | 316 |
item = self.FindTreeItem(self.Tree.GetRootItem(), name, inputs) |
317 |
if item is not None and item.IsOk(): |
|
1230 | 318 |
# Select tree item found |
814 | 319 |
self.Tree.SelectItem(item) |
320 |
self.Tree.EnsureVisible(item) |
|
321 |
||
1230 | 322 |
def FindTreeItem(self, item, name, inputs = None): |
323 |
""" |
|
324 |
Find Tree item corresponding to block informations given |
|
325 |
Function is recursive |
|
326 |
@param item: Item to test |
|
327 |
@param name: Block type name |
|
328 |
@param inputs: List of block inputs type [input_type,...] |
|
329 |
""" |
|
330 |
# Return immediately if item isn't valid |
|
331 |
if not item.IsOk(): |
|
332 |
return None |
|
333 |
||
334 |
# Get data associated to item to test |
|
335 |
item_pydata = self.Tree.GetPyData(item) |
|
336 |
if item_pydata is not None and item_pydata["type"] == BLOCK: |
|
337 |
# Only test item corresponding to block |
|
338 |
||
339 |
# Test if block inputs type are the same than those given |
|
340 |
type_inputs = item_pydata.get("inputs", None) |
|
341 |
type_extension = item_pydata.get("extension", None) |
|
342 |
if inputs is not None and type_inputs is not None: |
|
343 |
same_inputs = reduce( |
|
344 |
lambda x, y: x and y, |
|
345 |
map( |
|
346 |
lambda x: x[0]==x[1] or x[0]=='ANY' or x[1]=='ANY', |
|
347 |
zip(type_inputs, |
|
348 |
(inputs[:type_extension] |
|
349 |
if type_extension is not None |
|
350 |
else inputs))), |
|
351 |
True) |
|
814 | 352 |
else: |
1230 | 353 |
same_inputs = True |
354 |
||
355 |
# Return item if block data corresponds to informations given |
|
356 |
if self.Tree.GetItemText(item) == name and same_inputs: |
|
357 |
return item |
|
358 |
||
359 |
# Test item children if item doesn't correspond |
|
360 |
child, child_cookie = self.Tree.GetFirstChild(item) |
|
361 |
while child.IsOk(): |
|
362 |
result = self.FindTreeItem(child, name, inputs) |
|
363 |
if result: |
|
364 |
return result |
|
365 |
child, child_cookie = self.Tree.GetNextChild(item, child_cookie) |
|
366 |
||
814 | 367 |
return None |
368 |
||
369 |
def SearchInTree(self, value, mode="first"): |
|
1230 | 370 |
""" |
371 |
Search in Tree and select item that name contains string given |
|
372 |
@param value: String contained in block name to find |
|
373 |
@param mode: Search mode ('first', 'previous' or 'next') |
|
374 |
(default: 'first') |
|
375 |
@return: True if an item was found |
|
376 |
""" |
|
377 |
# Return immediately if root isn't valid |
|
814 | 378 |
root = self.Tree.GetRootItem() |
379 |
if not root.IsOk(): |
|
380 |
return False |
|
381 |
||
1230 | 382 |
# Set function to navigate in Tree item sibling according to search |
383 |
# mode defined |
|
384 |
sibling_function = (self.Tree.GetPrevSibling |
|
385 |
if mode == "previous" |
|
386 |
else self.Tree.GetNextSibling) |
|
387 |
||
388 |
# Get current selected item (for next and previous mode) |
|
389 |
item = self.Tree.GetSelection() |
|
390 |
if not item.IsOk() or mode == "first": |
|
814 | 391 |
item, item_cookie = self.Tree.GetFirstChild(root) |
392 |
selected = None |
|
393 |
else: |
|
394 |
selected = item |
|
1230 | 395 |
|
396 |
# Navigate through tree items until one matching found or reach tree |
|
397 |
# starting or ending |
|
814 | 398 |
while item.IsOk(): |
1230 | 399 |
|
400 |
# Get item data to get item type |
|
814 | 401 |
item_pydata = self.Tree.GetPyData(item) |
1230 | 402 |
|
403 |
# Item is a block category |
|
1501
d917c209529d
fix Traceback if search icon on library panel is clicked, when no
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1235
diff
changeset
|
404 |
if (item == root) or item_pydata["type"] == CATEGORY: |
1230 | 405 |
|
406 |
# Get category first or last child according to search mode |
|
407 |
# defined |
|
408 |
child = (self.Tree.GetLastChild(item) |
|
409 |
if mode == "previous" |
|
410 |
else self.Tree.GetFirstChild(item)[0]) |
|
411 |
||
412 |
# If category has no child, go to sibling category |
|
413 |
item = (child if child.IsOk() else sibling_function(item)) |
|
414 |
||
415 |
# Item is a block |
|
814 | 416 |
else: |
1230 | 417 |
|
418 |
# Extract item block name |
|
814 | 419 |
name = self.Tree.GetItemText(item) |
1230 | 420 |
# Test if block name contains string given |
1068
ef088254ba4b
Modify search algorithm in LibraryPanel to search match in whole block name, not only at beginning
Laurent Bessard
parents:
814
diff
changeset
|
421 |
if name.upper().find(value.upper()) != -1 and item != selected: |
1230 | 422 |
# Select block and collapse all categories other than block |
423 |
# category |
|
1235
1a30c70fa025
Fixed bug when searching in LibraryPanel on Windows
Laurent Bessard
parents:
1230
diff
changeset
|
424 |
child, child_cookie = self.Tree.GetFirstChild(root) |
1a30c70fa025
Fixed bug when searching in LibraryPanel on Windows
Laurent Bessard
parents:
1230
diff
changeset
|
425 |
while child.IsOk(): |
1a30c70fa025
Fixed bug when searching in LibraryPanel on Windows
Laurent Bessard
parents:
1230
diff
changeset
|
426 |
self.Tree.CollapseAllChildren(child) |
1a30c70fa025
Fixed bug when searching in LibraryPanel on Windows
Laurent Bessard
parents:
1230
diff
changeset
|
427 |
child, child_cookie = self.Tree.GetNextChild(root, child_cookie) |
814 | 428 |
self.Tree.SelectItem(item) |
429 |
self.Tree.EnsureVisible(item) |
|
430 |
return True |
|
431 |
||
1230 | 432 |
# Go to next item sibling if block not found |
433 |
next = sibling_function(item) |
|
434 |
||
435 |
# If category has no other child, go to next category sibling |
|
436 |
item = (next |
|
437 |
if next.IsOk() |
|
438 |
else sibling_function(self.Tree.GetItemParent(item))) |
|
439 |
||
814 | 440 |
return False |
441 |
||
442 |
def OnSearchCtrlChanged(self, event): |
|
1230 | 443 |
""" |
444 |
Called when SearchCtrl text control value changed |
|
445 |
@param event: TextCtrl change event |
|
446 |
""" |
|
447 |
# Search for block containing SearchCtrl value in 'first' mode |
|
814 | 448 |
self.SearchInTree(self.SearchCtrl.GetValue()) |
449 |
event.Skip() |
|
450 |
||
451 |
def OnSearchButtonClick(self, event): |
|
1230 | 452 |
""" |
453 |
Called when SearchCtrl search button was clicked |
|
454 |
@param event: Button clicked event |
|
455 |
""" |
|
456 |
# Search for block containing SearchCtrl value in 'next' mode |
|
814 | 457 |
self.SearchInTree(self.SearchCtrl.GetValue(), "next") |
458 |
event.Skip() |
|
459 |
||
460 |
def OnTreeItemSelected(self, event): |
|
1230 | 461 |
""" |
462 |
Called when tree item is selected |
|
463 |
@param event: wx.TreeEvent |
|
464 |
""" |
|
465 |
# Update TextCtrl value with block selected usage |
|
466 |
item_pydata = self.Tree.GetPyData(event.GetItem()) |
|
467 |
self.Comment.SetValue( |
|
468 |
item_pydata["comment"] |
|
469 |
if item_pydata is not None and item_pydata["type"] == BLOCK |
|
470 |
else "") |
|
471 |
||
472 |
# Call extra function defined when tree item is selected |
|
814 | 473 |
if getattr(self, "_OnTreeItemSelected", None) is not None: |
474 |
self._OnTreeItemSelected(event) |
|
1230 | 475 |
|
814 | 476 |
event.Skip() |
477 |
||
478 |
def OnTreeBeginDrag(self, event): |
|
1230 | 479 |
""" |
480 |
Called when a drag is started in tree |
|
481 |
@param event: wx.TreeEvent |
|
482 |
""" |
|
483 |
selected_item = event.GetItem() |
|
484 |
item_pydata = self.Tree.GetPyData(selected_item) |
|
485 |
||
486 |
# Item dragged is a block |
|
487 |
if item_pydata is not None and item_pydata["type"] == BLOCK: |
|
488 |
# Start a drag'n drop |
|
489 |
data = wx.TextDataObject(str( |
|
490 |
(self.Tree.GetItemText(selected_item), |
|
491 |
item_pydata["block_type"], |
|
492 |
"", |
|
493 |
item_pydata["inputs"]))) |
|
814 | 494 |
dragSource = wx.DropSource(self.Tree) |
495 |
dragSource.SetData(data) |
|
496 |
dragSource.DoDragDrop() |
|
497 |
||
498 |
def OnKeyDown(self, event): |
|
1230 | 499 |
""" |
500 |
Called when key is pressed in SearchCtrl text control |
|
501 |
@param event: wx.KeyEvent |
|
502 |
""" |
|
503 |
# Get event keycode and value in SearchCtrl |
|
814 | 504 |
keycode = event.GetKeyCode() |
505 |
search_value = self.SearchCtrl.GetValue() |
|
1230 | 506 |
|
507 |
# Up key was pressed and SearchCtrl isn't empty, search for block in |
|
508 |
# 'previous' mode |
|
814 | 509 |
if keycode == wx.WXK_UP and search_value != "": |
510 |
self.SearchInTree(search_value, "previous") |
|
1230 | 511 |
|
512 |
# Down key was pressed and SearchCtrl isn't empty, search for block in |
|
513 |
# 'next' mode |
|
814 | 514 |
elif keycode == wx.WXK_DOWN and search_value != "": |
515 |
self.SearchInTree(search_value, "next") |
|
1230 | 516 |
|
517 |
# Handle key normally |
|
814 | 518 |
else: |
519 |
event.Skip() |