Fixed and rewrite LibraryPanel
authorLaurent Bessard
Thu, 06 Jun 2013 14:24:22 +0200
changeset 1230 5a4c5724788e
parent 1229 137fd7e7b102
child 1231 0b5d608ed2be
Fixed and rewrite LibraryPanel
controls/LibraryPanel.py
dialogs/FBDBlockDialog.py
--- 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 @@
 #                              Library Panel
 #-------------------------------------------------------------------------------
 
+"""
+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):
+        """
+        Constructor
+        @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
+        # window
         self.Tree = wx.TreeCtrl(splitter_window,
               size=wx.Size(0, 0),  
               style=wx.TR_HAS_BUTTONS|
@@ -65,228 +87,426 @@
                     wx.TR_LINES_AT_ROOT)
         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
         if enable_drag:
             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
         self.Controller = None
-    
+        
+        # Variable storing functions and function blocks library to display
         self.BlockList = None
     
     def __del__(self):
+        """
+        Destructor
+        """
+        # Remove reference to project controller
         self.Controller = None
     
     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
         self.RefreshTree()
     
     def SetFocus(self):
+        """
+        Called to give focus to LibraryPanel
+        Override wx.Window SetFocus method 
+        """
+        # Give focus to SearchCtrl
         self.SearchCtrl.SetFocus()
     
     def ResetTree(self):
+        """
+        Reset LibraryPanel values displayed in controls
+        """
+        # Clear SearchCtrl, TreeCtrl and TextCtrl
         self.SearchCtrl.SetValue("")
         self.Tree.DeleteAllItems()
         self.Comment.SetValue("")
     
     def RefreshTree(self):
-        if self.Controller is not None:
-            to_delete = []
-            selected_name = None
-            selected = self.Tree.GetSelection()
-            if selected.IsOk():
-                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
-            else:
-                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
+            # will be refreshed
+            items_to_delete = []
+            
+            # 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()
+                               else None)
+            # 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
+                             else (None, None))
+            
+            # Get TreeCtrl root item (hidden)
             root = self.Tree.GetRootItem()
             if not root.IsOk():
+                # 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"
+                # to be translated
                 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
+                else:
                     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)
-                else:
-                    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 
+                # category)
                 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 
+                # them
+                
+                # 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
+                    else:
+                        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)
-                    else:
-                        self.Tree.SetItemText(blocktype_item, blocktype["name"])
+                            blocktype_item, category_cookie = \
+                                self.Tree.GetNextChild(category_item, 
+                                                       category_cookie)
+                    
+                    # 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"]]), 
-                                  "extension" : None}
-                    if blocktype["extensible"]:
-                        block_data["extension"] = len(blocktype["inputs"])
+                                  "inputs" : tuple([type 
+                                                    for name, type, modifier
+                                                    in blocktype["inputs"]]), 
+                                  "extension" : (len(blocktype["inputs"])
+                                                 if blocktype["extensible"]
+                                                 else None),
+                                  "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"], 
+                                          blocktype["inputs"]):
                         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)
-            for item in to_delete:
+                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:
                 self.Tree.Delete(item)
     
     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"]}
-        return None
+        """
+        Get selected block informations
+        @return: {"type": block_type_name, "inputs": [input_type,...]} or None
+        if no block selected
+        """
+        # 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()
+                           else None)
+        
+        # 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
+                else None)
     
     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):
-        if root.IsOk():
-            pydata = self.Tree.GetPyData(root)
-            if pydata is not None:
-                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]
-                    else:
-                        same_inputs = type_inputs == inputs
-                else:
-                    same_inputs = True
-            if pydata is not None and self.Tree.GetItemText(root) == name and same_inputs:
-                return root
+    def FindTreeItem(self, item, name, inputs = None):
+        """
+        Find Tree item corresponding to block informations given
+        Function is recursive
+        @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
+        if not item.IsOk():
+            return None
+        
+        # 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:
+                same_inputs = reduce(
+                    lambda x, y: x and y,
+                    map(
+                        lambda x: x[0]==x[1] or x[0]=='ANY' or x[1]=='ANY',
+                        zip(type_inputs,
+                            (inputs[:type_extension]
+                             if type_extension is not None
+                             else inputs))),
+                    True)
             else:
-                item, root_cookie = self.Tree.GetFirstChild(root)
-                while item.IsOk():
-                    result = self.FindTreeItem(item, name, inputs)
-                    if result:
-                        return result
-                    item, root_cookie = self.Tree.GetNextChild(root, root_cookie)
+                same_inputs = True
+            
+            # Return item if  block data corresponds to informations given
+            if self.Tree.GetItemText(item) == name and same_inputs:
+                return item
+        
+        # Test item children if item doesn't correspond
+        child, child_cookie = self.Tree.GetFirstChild(item)
+        while child.IsOk():
+            result = self.FindTreeItem(child, name, inputs)
+            if result:
+                return result
+            child, child_cookie = self.Tree.GetNextChild(item, child_cookie)
+        
         return None
     
     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')
+        (default: 'first')
+        @return: True if an item was found
+        """
+        # Return immediately if root isn't valid
         root = self.Tree.GetRootItem()
         if not root.IsOk():
             return False
         
-        if mode == "first":
+        # Set function to navigate in Tree item sibling according to search
+        # mode defined 
+        sibling_function = (self.Tree.GetPrevSibling
+                            if mode == "previous"
+                            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)
             selected = None
         else:
-            item = self.Tree.GetSelection()
             selected = item
-            if not item.IsOk():
-                item, item_cookie = self.Tree.GetFirstChild(root)
+        
+        # Navigate through tree items until one matching found or reach tree
+        # starting or ending
         while item.IsOk():
+            
+            # Get item data to get item type
             item_pydata = self.Tree.GetPyData(item)
+            
+            # Item is a block category
             if item_pydata["type"] == CATEGORY:
-                if mode == "previous":
-                    child = self.Tree.GetLastChild(item)
-                else:
-                    child, child_cookie = self.Tree.GetFirstChild(item)
-                if child.IsOk():
-                    item = child
-                elif mode == "previous":
-                    item = self.Tree.GetPrevSibling(item)
-                else:
-                    item = self.Tree.GetNextSibling(item)
+                
+                # Get category first or last child according to search mode 
+                # defined
+                child = (self.Tree.GetLastChild(item)
+                         if mode == "previous"
+                         else self.Tree.GetFirstChild(item)[0])
+                
+                # If category has no child, go to sibling category
+                item = (child if child.IsOk() else sibling_function(item))
+            
+            # Item is a block
             else:
+                
+                # 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)
-                    while child.IsOk():
-                        self.Tree.CollapseAllChildren(child)
-                        child, child_cookie = self.Tree.GetNextChild(root, child_cookie)
+                    # Select block and collapse all categories other than block
+                    # category
+                    self.Tree.CollapseAllChildren(root)
                     self.Tree.SelectItem(item)
                     self.Tree.EnsureVisible(item)
                     return True
                 
-                elif mode == "previous":
-                    previous = self.Tree.GetPrevSibling(item)
-                    if previous.IsOk():
-                        item = previous
-                    else:
-                        parent = self.Tree.GetItemParent(item)
-                        item = self.Tree.GetPrevSibling(parent)
-                
-                else:
-                    next = self.Tree.GetNextSibling(item)
-                    if next.IsOk():
-                        item = next
-                    else:
-                        parent = self.Tree.GetItemParent(item)
-                        item = self.Tree.GetNextSibling(parent)
+                # Go to next item sibling if block not found
+                next = sibling_function(item)
+                
+                # If category has no other child, go to next category sibling
+                item = (next
+                        if next.IsOk()
+                        else sibling_function(self.Tree.GetItemParent(item)))
+        
         return False
     
     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())
         event.Skip()
     
     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")
         event.Skip()
     
     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"])
-            if blocktype:
-                comment = blocktype["comment"]
-                self.Comment.SetValue(_(comment) + blocktype.get("usage", ""))
-            else:
-                self.Comment.SetValue("")
-        else:
-            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())
+        self.Comment.SetValue(
+            item_pydata["comment"]
+            if item_pydata is not None and item_pydata["type"] == BLOCK
+            else "")
+        
+        # Call extra function defined when tree item is selected
         if getattr(self, "_OnTreeItemSelected", None) is not None:
             self._OnTreeItemSelected(event)
+        
         event.Skip()
     
     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:
+            # Start a drag'n drop
+            data = wx.TextDataObject(str(
+                (self.Tree.GetItemText(selected_item), 
+                 item_pydata["block_type"], 
+                 "", 
+                 item_pydata["inputs"])))
             dragSource = wx.DropSource(self.Tree)
             dragSource.SetData(data)
             dragSource.DoDragDrop()
     
     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
+        # 'previous' mode 
         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
+        # 'next' mode 
         elif keycode == wx.WXK_DOWN and search_value != "":
             self.SearchInTree(search_value, "next")
+        
+        # Handle key normally
         else:
             event.Skip()
--- a/dialogs/FBDBlockDialog.py	Thu Jun 06 00:22:22 2013 +0200
+++ b/dialogs/FBDBlockDialog.py	Thu Jun 06 14:24:22 2013 +0200
@@ -51,7 +51,6 @@
               flag=wx.GROW|wx.RIGHT)
         
         self.LibraryPanel = LibraryPanel(self)
-        self.LibraryPanel.SetController(controller)
         setattr(self.LibraryPanel, "_OnTreeItemSelected", 
               self.OnLibraryTreeItemSelected)
         left_staticboxsizer.AddWindow(self.LibraryPanel, 1, border=5,