--- a/controls/LibraryPanel.py Thu Jun 06 00:22:22 2013 +0200
+++ b/controls/LibraryPanel.py Thu Jun 06 14:24:22 2013 +0200
@@ -34,28 +34,50 @@
#-------------------------------------------------------------------------------
+Class that implements a panel displaying a tree containing an hierarchical list +of functions and function blocks available in project an a search control for +quickly find one functions or function blocks in this list and a text control +displaying informations about selected functions or function blocks class LibraryPanel(wx.Panel):
def __init__(self, parent, enable_drag=False):
+ @param parent: Parent wx.Window of LibraryPanel + @param enable_drag: Flag indicating that function or function block can + be drag'n drop from LibraryPanel (default: False) wx.Panel.__init__(self, parent, style=wx.TAB_TRAVERSAL)
+ # Define LibraryPanel main sizer main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=0)
main_sizer.AddGrowableCol(0)
main_sizer.AddGrowableRow(1)
+ # Add SearchCtrl to main sizer self.SearchCtrl = wx.SearchCtrl(self)
+ # Add a button with a magnifying glass, essentially to show that this + # control is for searching in tree self.SearchCtrl.ShowSearchButton(True)
self.Bind(wx.EVT_TEXT, self.OnSearchCtrlChanged, self.SearchCtrl)
self.Bind(wx.EVT_SEARCHCTRL_SEARCH_BTN,
- self.OnSearchButtonClick, self.SearchCtrl)
+ self.OnSearchButtonClick, self.SearchCtrl) + # Bind keyboard event on SearchCtrl text control to catch UP and DOWN + # for search previous and next occurrence search_textctrl = self.SearchCtrl.GetChildren()[0]
search_textctrl.Bind(wx.EVT_CHAR, self.OnKeyDown)
main_sizer.AddWindow(self.SearchCtrl, flag=wx.GROW)
+ # Add Splitter window for tree and block comment to main sizer splitter_window = wx.SplitterWindow(self)
splitter_window.SetSashGravity(1.0)
main_sizer.AddWindow(splitter_window, flag=wx.GROW)
+ # Add TreeCtrl for functions and function blocks library in splitter self.Tree = wx.TreeCtrl(splitter_window,
@@ -65,228 +87,426 @@
self.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnTreeItemSelected, self.Tree)
self.Tree.Bind(wx.EVT_CHAR, self.OnKeyDown)
+ # If drag'n drop is enabled, bind event generated when a drag begins on + # tree to start a drag'n drop self.Bind(wx.EVT_TREE_BEGIN_DRAG, self.OnTreeBeginDrag, self.Tree)
+ # Add TextCtrl for function and function block informations self.Comment = wx.TextCtrl(splitter_window, size=wx.Size(0, 80),
style=wx.TE_READONLY|wx.TE_MULTILINE)
splitter_window.SplitHorizontally(self.Tree, self.Comment, -80)
self.SetSizer(main_sizer)
+ # Reference to the project controller
+ # Variable storing functions and function blocks library to display + # Remove reference to project controller def SetController(self, controller):
+ Set reference to project controller + @param controller: Reference to project controller self.Controller = controller
def SetBlockList(self, blocklist):
+ Set function and function block library to display in TreeCtrl + @param blocklist: Function and function block library + # Save functions and function blocks library self.BlockList = blocklist
+ # Refresh TreeCtrl values + Called to give focus to LibraryPanel + Override wx.Window SetFocus method + # Give focus to SearchCtrl self.SearchCtrl.SetFocus()
+ Reset LibraryPanel values displayed in controls + # Clear SearchCtrl, TreeCtrl and TextCtrl self.SearchCtrl.SetValue("")
self.Tree.DeleteAllItems()
self.Comment.SetValue("")
- if self.Controller is not None:
- selected = self.Tree.GetSelection()
- selected_pydata = self.Tree.GetPyData(selected)
- if selected_pydata is not None and selected_pydata["type"] != CATEGORY:
- selected_name = self.Tree.GetItemText(selected)
- if self.BlockList is not None:
- blocktypes = self.BlockList
- blocktypes = self.Controller.GetBlockTypes()
+ Refresh LibraryPanel values displayed in controls + # Get function and function blocks library + blocktypes = self.BlockList + if blocktypes is None and self.Controller is not None: + # Get library from project controller if not defined + blocktypes = self.Controller.GetBlockTypes() + # Refresh TreeCtrl values if a library is defined + if blocktypes is not None: + # List that will contain tree items to be deleted when TreeCtrl + # Get current selected item for selected it when values refreshed + selected_item = self.Tree.GetSelection() + selected_pydata = (self.Tree.GetPyData(selected_item) + if selected_item.IsOk() and + selected_item != self.Tree.GetRootItem() + # Don't save selected item if it is a category + selected_infos = ((self.Tree.GetItemText(selected_item), + selected_pydata["inputs"]) + if selected_pydata is not None and + selected_pydata["type"] == BLOCK + # Get TreeCtrl root item (hidden) root = self.Tree.GetRootItem()
+ # Create root if not present root = self.Tree.AddRoot("")
+ # Iterate over functions and function blocks library categories and + # add a tree item to root item for each of them + # Get first child under root item category_item, root_cookie = self.Tree.GetFirstChild(root)
for category in blocktypes:
+ # Store category name in a local variable to prevent script + # extracting translated strings for gettext to consider "name" category_name = category["name"]
- if not category_item.IsOk():
+ # Tree item already exists, set item label + if category_item.IsOk(): + self.Tree.SetItemText(category_item, _(category_name)) + # Tree item doesn't exist, add new one to root category_item = self.Tree.AppendItem(root, _(category_name))
+ # On Windows, needs to get next child of root to have a + # reference to the newly added tree item if wx.Platform != '__WXMSW__':
- category_item, root_cookie = self.Tree.GetNextChild(root, root_cookie)
- self.Tree.SetItemText(category_item, _(category_name))
+ category_item, root_cookie = \ + self.Tree.GetNextChild(root, root_cookie) + # Set data associated to tree item (only save that item is a self.Tree.SetPyData(category_item, {"type" : CATEGORY})
- blocktype_item, category_cookie = self.Tree.GetFirstChild(category_item)
+ # Iterate over functions and function blocks defined in library + # category add a tree item to category tree item for each of + # Get first child under category tree item + blocktype_item, category_cookie = \ + self.Tree.GetFirstChild(category_item) for blocktype in category["list"]:
- if not blocktype_item.IsOk():
- blocktype_item = self.Tree.AppendItem(category_item, blocktype["name"])
+ # Tree item already exists, set item label + if blocktype_item.IsOk(): + self.Tree.SetItemText(blocktype_item, blocktype["name"]) + # Tree item doesn't exist, add new one to category item + blocktype_item = self.Tree.AppendItem( + category_item, blocktype["name"]) + # See comment when adding category if wx.Platform != '__WXMSW__':
- blocktype_item, category_cookie = self.Tree.GetNextChild(category_item, category_cookie)
- self.Tree.SetItemText(blocktype_item, blocktype["name"])
+ blocktype_item, category_cookie = \ + self.Tree.GetNextChild(category_item, + # Define data to associate to block tree item + comment = blocktype["comment"] block_data = {"type" : BLOCK,
"block_type" : blocktype["type"],
- "inputs" : tuple([type for name, type, modifier in blocktype["inputs"]]),
- if blocktype["extensible"]:
- block_data["extension"] = len(blocktype["inputs"])
+ for name, type, modifier + in blocktype["inputs"]]), + "extension" : (len(blocktype["inputs"]) + if blocktype["extensible"] + "comment": _(comment) + + blocktype.get("usage", "")} self.Tree.SetPyData(blocktype_item, block_data)
- if selected_name == blocktype["name"]:
+ # Select block tree item in tree if it corresponds to + # previously selected one + if selected_infos == (blocktype["name"], self.Tree.SelectItem(blocktype_item)
- comment = blocktype["comment"]
- self.Comment.SetValue(_(comment) + blocktype.get("usage", ""))
- blocktype_item, category_cookie = self.Tree.GetNextChild(category_item, category_cookie)
+ # Update TextCtrl value + self.Comment.SetValue(block_data["comment"]) + # Get next block tree item under category tree item + blocktype_item, category_cookie = \ + self.Tree.GetNextChild(category_item, category_cookie) + # Add every remaining tree item under category tree item after + # updating all block items to the list of items to delete while blocktype_item.IsOk():
- to_delete.append(blocktype_item)
- blocktype_item, category_cookie = self.Tree.GetNextChild(category_item, category_cookie)
- category_item, root_cookie = self.Tree.GetNextChild(root, root_cookie)
+ items_to_delete.append(blocktype_item) + blocktype_item, category_cookie = \ + self.Tree.GetNextChild(category_item, category_cookie) + # Get next category tree item under root item + category_item, root_cookie = \ + self.Tree.GetNextChild(root, root_cookie) + # Add every remaining tree item under root item after updating all + # category items to the list of items to delete while category_item.IsOk():
- to_delete.append(category_item)
- category_item, root_cookie = self.Tree.GetNextChild(root, root_cookie)
+ items_to_delete.append(category_item) + category_item, root_cookie = \ + self.Tree.GetNextChild(root, root_cookie) + # Remove all items in list of items to delete from TreeCtrl + for item in items_to_delete: def GetSelectedBlock(self):
- selected = self.Tree.GetSelection()
- if (selected.IsOk() and
- self.Tree.GetItemParent(selected) != self.Tree.GetRootItem() and
- selected != self.Tree.GetRootItem()):
- selected_data = self.Tree.GetPyData(selected)
- return {"type": self.Tree.GetItemText(selected),
- "inputs": selected_data["inputs"]}
+ Get selected block informations + @return: {"type": block_type_name, "inputs": [input_type,...]} or None + # Get selected item associated data in tree + selected_item = self.Tree.GetSelection() + selected_pydata = (self.Tree.GetPyData(selected_item) + if selected_item.IsOk() and + selected_item != self.Tree.GetRootItem() + # Return value is None if selected tree item is root or a category + return ({"type": self.Tree.GetItemText(selected_item), + "inputs": selected_pydata["inputs"]} + if selected_pydata is not None and + selected_pydata["type"] == BLOCK def SelectTreeItem(self, name, inputs):
+ Select Tree item corresponding to block informations given + @param name: Block type name + @param inputs: List of block inputs type [input_type,...] + # Find tree item corresponding to block informations item = self.FindTreeItem(self.Tree.GetRootItem(), name, inputs)
if item is not None and item.IsOk():
+ # Select tree item found self.Tree.SelectItem(item)
self.Tree.EnsureVisible(item)
- def FindTreeItem(self, root, name, inputs = None):
- pydata = self.Tree.GetPyData(root)
- type_inputs = pydata.get("inputs", None)
- type_extension = pydata.get("extension", None)
- if inputs is not None and type_inputs is not None:
- if type_extension is not None:
- same_inputs = type_inputs == inputs[:type_extension]
- same_inputs = type_inputs == inputs
- if pydata is not None and self.Tree.GetItemText(root) == name and same_inputs:
+ def FindTreeItem(self, item, name, inputs = None): + Find Tree item corresponding to block informations given + @param item: Item to test + @param name: Block type name + @param inputs: List of block inputs type [input_type,...] + # Return immediately if item isn't valid + # Get data associated to item to test + item_pydata = self.Tree.GetPyData(item) + if item_pydata is not None and item_pydata["type"] == BLOCK: + # Only test item corresponding to block + # Test if block inputs type are the same than those given + type_inputs = item_pydata.get("inputs", None) + type_extension = item_pydata.get("extension", None) + if inputs is not None and type_inputs is not None: + lambda x: x[0]==x[1] or x[0]=='ANY' or x[1]=='ANY', + (inputs[:type_extension] + if type_extension is not None - item, root_cookie = self.Tree.GetFirstChild(root)
- result = self.FindTreeItem(item, name, inputs)
- item, root_cookie = self.Tree.GetNextChild(root, root_cookie)
+ # Return item if block data corresponds to informations given + if self.Tree.GetItemText(item) == name and same_inputs: + # Test item children if item doesn't correspond + child, child_cookie = self.Tree.GetFirstChild(item) + result = self.FindTreeItem(child, name, inputs) + child, child_cookie = self.Tree.GetNextChild(item, child_cookie) def SearchInTree(self, value, mode="first"):
+ Search in Tree and select item that name contains string given + @param value: String contained in block name to find + @param mode: Search mode ('first', 'previous' or 'next') + @return: True if an item was found + # Return immediately if root isn't valid root = self.Tree.GetRootItem()
+ # Set function to navigate in Tree item sibling according to search + sibling_function = (self.Tree.GetPrevSibling + else self.Tree.GetNextSibling) + # Get current selected item (for next and previous mode) + item = self.Tree.GetSelection() + if not item.IsOk() or mode == "first": item, item_cookie = self.Tree.GetFirstChild(root)
- item = self.Tree.GetSelection()
- item, item_cookie = self.Tree.GetFirstChild(root)
+ # Navigate through tree items until one matching found or reach tree + # Get item data to get item type item_pydata = self.Tree.GetPyData(item)
+ # Item is a block category if item_pydata["type"] == CATEGORY:
- child = self.Tree.GetLastChild(item)
- child, child_cookie = self.Tree.GetFirstChild(item)
- elif mode == "previous":
- item = self.Tree.GetPrevSibling(item)
- item = self.Tree.GetNextSibling(item)
+ # Get category first or last child according to search mode + child = (self.Tree.GetLastChild(item) + else self.Tree.GetFirstChild(item)[0]) + # If category has no child, go to sibling category + item = (child if child.IsOk() else sibling_function(item)) + # Extract item block name name = self.Tree.GetItemText(item)
+ # Test if block name contains string given if name.upper().find(value.upper()) != -1 and item != selected:
- child, child_cookie = self.Tree.GetFirstChild(root)
- self.Tree.CollapseAllChildren(child)
- child, child_cookie = self.Tree.GetNextChild(root, child_cookie)
+ # Select block and collapse all categories other than block + self.Tree.CollapseAllChildren(root) self.Tree.SelectItem(item)
self.Tree.EnsureVisible(item)
- elif mode == "previous":
- previous = self.Tree.GetPrevSibling(item)
- parent = self.Tree.GetItemParent(item)
- item = self.Tree.GetPrevSibling(parent)
+ # Go to next item sibling if block not found + next = sibling_function(item)
- next = self.Tree.GetNextSibling(item)
- parent = self.Tree.GetItemParent(item)
- item = self.Tree.GetNextSibling(parent)
+ # If category has no other child, go to next category sibling + else sibling_function(self.Tree.GetItemParent(item))) def OnSearchCtrlChanged(self, event):
+ Called when SearchCtrl text control value changed + @param event: TextCtrl change event + # Search for block containing SearchCtrl value in 'first' mode self.SearchInTree(self.SearchCtrl.GetValue())
def OnSearchButtonClick(self, event):
+ Called when SearchCtrl search button was clicked + @param event: Button clicked event + # Search for block containing SearchCtrl value in 'next' mode self.SearchInTree(self.SearchCtrl.GetValue(), "next")
def OnTreeItemSelected(self, event):
- selected = event.GetItem()
- pydata = self.Tree.GetPyData(selected)
- if pydata is not None and pydata["type"] != CATEGORY:
- blocktype = self.Controller.GetBlockType(self.Tree.GetItemText(selected), pydata["inputs"])
- comment = blocktype["comment"]
- self.Comment.SetValue(_(comment) + blocktype.get("usage", ""))
- self.Comment.SetValue("")
- self.Comment.SetValue("")
+ Called when tree item is selected + @param event: wx.TreeEvent + # Update TextCtrl value with block selected usage + item_pydata = self.Tree.GetPyData(event.GetItem()) + if item_pydata is not None and item_pydata["type"] == BLOCK + # Call extra function defined when tree item is selected if getattr(self, "_OnTreeItemSelected", None) is not None:
self._OnTreeItemSelected(event)
def OnTreeBeginDrag(self, event):
- selected = event.GetItem()
- pydata = self.Tree.GetPyData(selected)
- if pydata is not None and pydata["type"] == BLOCK:
- data = wx.TextDataObject(str((self.Tree.GetItemText(selected),
- pydata["block_type"], "", pydata["inputs"])))
+ Called when a drag is started in tree + @param event: wx.TreeEvent + selected_item = event.GetItem() + item_pydata = self.Tree.GetPyData(selected_item) + # Item dragged is a block + if item_pydata is not None and item_pydata["type"] == BLOCK: + data = wx.TextDataObject(str( + (self.Tree.GetItemText(selected_item), + item_pydata["block_type"], + item_pydata["inputs"]))) dragSource = wx.DropSource(self.Tree)
def OnKeyDown(self, event):
+ Called when key is pressed in SearchCtrl text control + @param event: wx.KeyEvent + # Get event keycode and value in SearchCtrl keycode = event.GetKeyCode()
search_value = self.SearchCtrl.GetValue()
+ # Up key was pressed and SearchCtrl isn't empty, search for block in if keycode == wx.WXK_UP and search_value != "":
self.SearchInTree(search_value, "previous")
+ # Down key was pressed and SearchCtrl isn't empty, search for block in elif keycode == wx.WXK_DOWN and search_value != "":
self.SearchInTree(search_value, "next")