controls/LibraryPanel.py
author Reinhard Lee
Mon, 27 Aug 2012 17:33:41 +0200
changeset 748 18b38821201b
parent 741 330f578e228d
permissions -rw-r--r--
Update Korean translations
676
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
     1
#!/usr/bin/env python
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
     3
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
     4
#This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
     5
#based on the plcopen standard. 
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
     6
#
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
     7
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
     8
#
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
     9
#See COPYING file for copyrights details.
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    10
#
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    11
#This library is free software; you can redistribute it and/or
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    12
#modify it under the terms of the GNU General Public
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    13
#License as published by the Free Software Foundation; either
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    14
#version 2.1 of the License, or (at your option) any later version.
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    15
#
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    16
#This library is distributed in the hope that it will be useful,
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    17
#but WITHOUT ANY WARRANTY; without even the implied warranty of
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    18
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    19
#General Public License for more details.
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    20
#
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    21
#You should have received a copy of the GNU General Public
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    22
#License along with this library; if not, write to the Free Software
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    23
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    24
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    25
import wx
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    26
714
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 680
diff changeset
    27
#-------------------------------------------------------------------------------
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 680
diff changeset
    28
#                                 Helpers
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 680
diff changeset
    29
#-------------------------------------------------------------------------------
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 680
diff changeset
    30
676
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    31
[CATEGORY, BLOCK] = range(2)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    32
714
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 680
diff changeset
    33
#-------------------------------------------------------------------------------
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 680
diff changeset
    34
#                              Library Panel
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 680
diff changeset
    35
#-------------------------------------------------------------------------------
676
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    36
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    37
class LibraryPanel(wx.Panel):
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    38
    
714
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 680
diff changeset
    39
    def __init__(self, parent, enable_drag=False):
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 680
diff changeset
    40
        wx.Panel.__init__(self, parent, style=wx.TAB_TRAVERSAL)
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 680
diff changeset
    41
        
741
330f578e228d Fix bugs with Viewer objects dialogs
Laurent Bessard
parents: 714
diff changeset
    42
        main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=0)
714
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 680
diff changeset
    43
        main_sizer.AddGrowableCol(0)
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 680
diff changeset
    44
        main_sizer.AddGrowableRow(1)
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 680
diff changeset
    45
        
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 680
diff changeset
    46
        self.SearchCtrl = wx.SearchCtrl(self)
676
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    47
        self.SearchCtrl.ShowSearchButton(True)
714
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 680
diff changeset
    48
        self.Bind(wx.EVT_TEXT, self.OnSearchCtrlChanged, self.SearchCtrl)
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 680
diff changeset
    49
        self.Bind(wx.EVT_SEARCHCTRL_SEARCH_BTN, 
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 680
diff changeset
    50
              self.OnSearchButtonClick, self.SearchCtrl)
676
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    51
        search_textctrl = self.SearchCtrl.GetChildren()[0]
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    52
        search_textctrl.Bind(wx.EVT_CHAR, self.OnKeyDown)
714
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 680
diff changeset
    53
        main_sizer.AddWindow(self.SearchCtrl, flag=wx.GROW)
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 680
diff changeset
    54
        
741
330f578e228d Fix bugs with Viewer objects dialogs
Laurent Bessard
parents: 714
diff changeset
    55
        splitter_window = wx.SplitterWindow(self)
330f578e228d Fix bugs with Viewer objects dialogs
Laurent Bessard
parents: 714
diff changeset
    56
        splitter_window.SetSashGravity(1.0)
330f578e228d Fix bugs with Viewer objects dialogs
Laurent Bessard
parents: 714
diff changeset
    57
        main_sizer.AddWindow(splitter_window, flag=wx.GROW)
330f578e228d Fix bugs with Viewer objects dialogs
Laurent Bessard
parents: 714
diff changeset
    58
        
330f578e228d Fix bugs with Viewer objects dialogs
Laurent Bessard
parents: 714
diff changeset
    59
        self.Tree = wx.TreeCtrl(splitter_window,
330f578e228d Fix bugs with Viewer objects dialogs
Laurent Bessard
parents: 714
diff changeset
    60
              size=wx.Size(0, 0),  
714
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 680
diff changeset
    61
              style=wx.TR_HAS_BUTTONS|
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 680
diff changeset
    62
                    wx.TR_SINGLE|
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 680
diff changeset
    63
                    wx.SUNKEN_BORDER|
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 680
diff changeset
    64
                    wx.TR_HIDE_ROOT|
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 680
diff changeset
    65
                    wx.TR_LINES_AT_ROOT)
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 680
diff changeset
    66
        self.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnTreeItemSelected, self.Tree)
676
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    67
        self.Tree.Bind(wx.EVT_CHAR, self.OnKeyDown)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    68
        if enable_drag:
714
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 680
diff changeset
    69
            self.Bind(wx.EVT_TREE_BEGIN_DRAG, self.OnTreeBeginDrag, self.Tree)
741
330f578e228d Fix bugs with Viewer objects dialogs
Laurent Bessard
parents: 714
diff changeset
    70
        
330f578e228d Fix bugs with Viewer objects dialogs
Laurent Bessard
parents: 714
diff changeset
    71
        self.Comment = wx.TextCtrl(splitter_window, size=wx.Size(0, 80), 
676
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    72
              style=wx.TE_READONLY|wx.TE_MULTILINE)
741
330f578e228d Fix bugs with Viewer objects dialogs
Laurent Bessard
parents: 714
diff changeset
    73
        
330f578e228d Fix bugs with Viewer objects dialogs
Laurent Bessard
parents: 714
diff changeset
    74
        splitter_window.SplitHorizontally(self.Tree, self.Comment, -80)
714
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 680
diff changeset
    75
        
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 680
diff changeset
    76
        self.SetSizer(main_sizer)
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 680
diff changeset
    77
            
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 680
diff changeset
    78
        self.Controller = None
676
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    79
    
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    80
        self.BlockList = None
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    81
    
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    82
    def __del__(self):
714
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 680
diff changeset
    83
        self.Controller = None
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 680
diff changeset
    84
    
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 680
diff changeset
    85
    def SetController(self, controller):
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 680
diff changeset
    86
        self.Controller = controller
676
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    87
    
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    88
    def SetBlockList(self, blocklist):
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    89
        self.BlockList = blocklist
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    90
        self.RefreshTree()
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    91
    
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    92
    def SetFocus(self):
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    93
        self.SearchCtrl.SetFocus()
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    94
    
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    95
    def ResetTree(self):
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    96
        self.SearchCtrl.SetValue("")
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    97
        self.Tree.DeleteAllItems()
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    98
        self.Comment.SetValue("")
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    99
    
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   100
    def RefreshTree(self):
714
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 680
diff changeset
   101
        if self.Controller is not None:
676
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   102
            to_delete = []
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   103
            selected_name = None
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   104
            selected = self.Tree.GetSelection()
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   105
            if selected.IsOk():
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   106
                selected_pydata = self.Tree.GetPyData(selected)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   107
                if selected_pydata is not None and selected_pydata["type"] != CATEGORY:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   108
                    selected_name = self.Tree.GetItemText(selected)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   109
            if self.BlockList is not None:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   110
                blocktypes = self.BlockList
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   111
            else:
714
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 680
diff changeset
   112
                blocktypes = self.Controller.GetBlockTypes()
676
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   113
            root = self.Tree.GetRootItem()
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   114
            if not root.IsOk():
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   115
                root = self.Tree.AddRoot("")
714
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 680
diff changeset
   116
            category_item, root_cookie = self.Tree.GetFirstChild(root)
676
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   117
            for category in blocktypes:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   118
                category_name = category["name"]
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   119
                if not category_item.IsOk():
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   120
                    category_item = self.Tree.AppendItem(root, _(category_name))
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   121
                    if wx.Platform != '__WXMSW__':
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   122
                        category_item, root_cookie = self.Tree.GetNextChild(root, root_cookie)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   123
                else:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   124
                    self.Tree.SetItemText(category_item, _(category_name))
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   125
                self.Tree.SetPyData(category_item, {"type" : CATEGORY})
714
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 680
diff changeset
   126
                blocktype_item, category_cookie = self.Tree.GetFirstChild(category_item)
676
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   127
                for blocktype in category["list"]:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   128
                    if not blocktype_item.IsOk():
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   129
                        blocktype_item = self.Tree.AppendItem(category_item, blocktype["name"])
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   130
                        if wx.Platform != '__WXMSW__':
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   131
                            blocktype_item, category_cookie = self.Tree.GetNextChild(category_item, category_cookie)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   132
                    else:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   133
                        self.Tree.SetItemText(blocktype_item, blocktype["name"])
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   134
                    block_data = {"type" : BLOCK, 
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   135
                                  "block_type" : blocktype["type"], 
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   136
                                  "inputs" : tuple([type for name, type, modifier in blocktype["inputs"]]), 
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   137
                                  "extension" : None}
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   138
                    if blocktype["extensible"]:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   139
                        block_data["extension"] = len(blocktype["inputs"])
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   140
                    self.Tree.SetPyData(blocktype_item, block_data)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   141
                    if selected_name == blocktype["name"]:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   142
                        self.Tree.SelectItem(blocktype_item)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   143
                        comment = blocktype["comment"]
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   144
                        self.Comment.SetValue(_(comment) + blocktype.get("usage", ""))
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   145
                    blocktype_item, category_cookie = self.Tree.GetNextChild(category_item, category_cookie)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   146
                while blocktype_item.IsOk():
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   147
                    to_delete.append(blocktype_item)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   148
                    blocktype_item, category_cookie = self.Tree.GetNextChild(category_item, category_cookie)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   149
                category_item, root_cookie = self.Tree.GetNextChild(root, root_cookie)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   150
            while category_item.IsOk():
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   151
                to_delete.append(category_item)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   152
                category_item, root_cookie = self.Tree.GetNextChild(root, root_cookie)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   153
            for item in to_delete:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   154
                self.Tree.Delete(item)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   155
    
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   156
    def GetSelectedBlock(self):
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   157
        selected = self.Tree.GetSelection()
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   158
        if (selected.IsOk() and 
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   159
            self.Tree.GetItemParent(selected) != self.Tree.GetRootItem() and 
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   160
            selected != self.Tree.GetRootItem()):
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   161
            selected_data = self.Tree.GetPyData(selected)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   162
            return {"type": self.Tree.GetItemText(selected), 
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   163
                    "inputs": selected_data["inputs"]}
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   164
        return None
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   165
    
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   166
    def SelectTreeItem(self, name, inputs):
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   167
        item = self.FindTreeItem(self.Tree.GetRootItem(), name, inputs)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   168
        if item is not None and item.IsOk():
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   169
            self.Tree.SelectItem(item)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   170
            self.Tree.EnsureVisible(item)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   171
    
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   172
    def FindTreeItem(self, root, name, inputs = None):
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   173
        if root.IsOk():
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   174
            pydata = self.Tree.GetPyData(root)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   175
            if pydata is not None:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   176
                type_inputs = pydata.get("inputs", None)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   177
                type_extension = pydata.get("extension", None)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   178
                if inputs is not None and type_inputs is not None:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   179
                    if type_extension is not None:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   180
                        same_inputs = type_inputs == inputs[:type_extension]
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   181
                    else:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   182
                        same_inputs = type_inputs == inputs
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   183
                else:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   184
                    same_inputs = True
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   185
            if pydata is not None and self.Tree.GetItemText(root) == name and same_inputs:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   186
                return root
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   187
            else:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   188
                if wx.VERSION < (2, 6, 0):
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   189
                    item, root_cookie = self.Tree.GetFirstChild(root, 0)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   190
                else:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   191
                    item, root_cookie = self.Tree.GetFirstChild(root)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   192
                while item.IsOk():
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   193
                    result = self.FindTreeItem(item, name, inputs)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   194
                    if result:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   195
                        return result
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   196
                    item, root_cookie = self.Tree.GetNextChild(root, root_cookie)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   197
        return None
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   198
    
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   199
    def SearchInTree(self, value, mode="first"):
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   200
        root = self.Tree.GetRootItem()
680
b693147fb2c3 Fixing bugs with latest changesets on Windows
laurent
parents: 676
diff changeset
   201
        if not root.IsOk():
b693147fb2c3 Fixing bugs with latest changesets on Windows
laurent
parents: 676
diff changeset
   202
            return False
b693147fb2c3 Fixing bugs with latest changesets on Windows
laurent
parents: 676
diff changeset
   203
        
676
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   204
        if mode == "first":
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   205
            item, item_cookie = self.Tree.GetFirstChild(root)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   206
            selected = None
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   207
        else:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   208
            item = self.Tree.GetSelection()
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   209
            selected = item
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   210
            if not item.IsOk():
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   211
                item, item_cookie = self.Tree.GetFirstChild(root)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   212
        while item.IsOk():
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   213
            item_pydata = self.Tree.GetPyData(item)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   214
            if item_pydata["type"] == CATEGORY:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   215
                if mode == "previous":
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   216
                    child = self.Tree.GetLastChild(item)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   217
                else:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   218
                    child, child_cookie = self.Tree.GetFirstChild(item)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   219
                if child.IsOk():
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   220
                    item = child
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   221
                elif mode == "previous":
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   222
                    item = self.Tree.GetPrevSibling(item)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   223
                else:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   224
                    item = self.Tree.GetNextSibling(item)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   225
            else:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   226
                name = self.Tree.GetItemText(item)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   227
                if name.upper().startswith(value.upper()) and item != selected:
680
b693147fb2c3 Fixing bugs with latest changesets on Windows
laurent
parents: 676
diff changeset
   228
                    child, child_cookie = self.Tree.GetFirstChild(root)
b693147fb2c3 Fixing bugs with latest changesets on Windows
laurent
parents: 676
diff changeset
   229
                    while child.IsOk():
b693147fb2c3 Fixing bugs with latest changesets on Windows
laurent
parents: 676
diff changeset
   230
                        self.Tree.CollapseAllChildren(child)
b693147fb2c3 Fixing bugs with latest changesets on Windows
laurent
parents: 676
diff changeset
   231
                        child, child_cookie = self.Tree.GetNextChild(root, child_cookie)
676
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   232
                    self.Tree.SelectItem(item)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   233
                    self.Tree.EnsureVisible(item)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   234
                    return True
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   235
                
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   236
                elif mode == "previous":
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   237
                    previous = self.Tree.GetPrevSibling(item)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   238
                    if previous.IsOk():
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   239
                        item = previous
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   240
                    else:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   241
                        parent = self.Tree.GetItemParent(item)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   242
                        item = self.Tree.GetPrevSibling(parent)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   243
                
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   244
                else:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   245
                    next = self.Tree.GetNextSibling(item)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   246
                    if next.IsOk():
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   247
                        item = next
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   248
                    else:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   249
                        parent = self.Tree.GetItemParent(item)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   250
                        item = self.Tree.GetNextSibling(parent)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   251
        return False
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   252
    
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   253
    def OnSearchCtrlChanged(self, event):
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   254
        self.SearchInTree(self.SearchCtrl.GetValue())
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   255
        event.Skip()
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   256
    
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   257
    def OnSearchButtonClick(self, event):
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   258
        self.SearchInTree(self.SearchCtrl.GetValue(), "next")
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   259
        event.Skip()
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   260
    
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   261
    def OnTreeItemSelected(self, event):
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   262
        selected = event.GetItem()
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   263
        pydata = self.Tree.GetPyData(selected)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   264
        if pydata is not None and pydata["type"] != CATEGORY:
714
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 680
diff changeset
   265
            blocktype = self.Controller.GetBlockType(self.Tree.GetItemText(selected), pydata["inputs"])
676
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   266
            if blocktype:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   267
                comment = blocktype["comment"]
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   268
                self.Comment.SetValue(_(comment) + blocktype.get("usage", ""))
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   269
            else:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   270
                self.Comment.SetValue("")
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   271
        else:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   272
            self.Comment.SetValue("")
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   273
        if getattr(self, "_OnTreeItemSelected", None) is not None:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   274
            self._OnTreeItemSelected(event)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   275
        event.Skip()
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   276
    
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   277
    def OnTreeBeginDrag(self, event):
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   278
        selected = event.GetItem()
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   279
        pydata = self.Tree.GetPyData(selected)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   280
        if pydata is not None and pydata["type"] == BLOCK:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   281
            data = wx.TextDataObject(str((self.Tree.GetItemText(selected), 
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   282
                pydata["block_type"], "", pydata["inputs"])))
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   283
            dragSource = wx.DropSource(self.Tree)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   284
            dragSource.SetData(data)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   285
            dragSource.DoDragDrop()
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   286
    
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   287
    def OnKeyDown(self, event):
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   288
        keycode = event.GetKeyCode()
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   289
        search_value = self.SearchCtrl.GetValue()
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   290
        if keycode == wx.WXK_UP and search_value != "":
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   291
            self.SearchInTree(search_value, "previous")
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   292
        elif keycode == wx.WXK_DOWN and search_value != "":
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   293
            self.SearchInTree(search_value, "next")
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   294
        else:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   295
            event.Skip()