controls/LibraryPanel.py
author Laurent Bessard
Wed, 06 Jun 2012 16:57:58 +0200
changeset 704 aca0c83ed82e
parent 680 b693147fb2c3
child 714 131ea7f237b9
permissions -rw-r--r--
Replacing dialog asking for a name when drag'n drop function block to viewer by automatically generated name
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
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    27
[CATEGORY, BLOCK] = range(2)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    28
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    29
[ID_LIBRARYPANEL, ID_LIBRARYSEARCHCTRL, 
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    30
 ID_LIBRARYTREE, ID_LIBRARYCOMMENT, 
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    31
] = [wx.NewId() for _init_ctrls in range(4)]
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    32
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    33
class LibraryPanel(wx.Panel):
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    34
    
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    35
    def _init_coll_MainSizer_Items(self, parent):
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    36
        parent.AddWindow(self.SearchCtrl, 0, border=0, flag=wx.GROW)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    37
        parent.AddWindow(self.Tree, 0, border=0, flag=wx.GROW)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    38
        parent.AddWindow(self.Comment, 0, border=0, flag=wx.GROW)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    39
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    40
    def _init_coll_MainSizer_Growables(self, parent):
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    41
        parent.AddGrowableCol(0)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    42
        parent.AddGrowableRow(1)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    43
    
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    44
    def _init_sizers(self):
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    45
        self.MainSizer = wx.FlexGridSizer(cols=1, hgap=0, rows=3, vgap=0)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    46
        
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    47
        self._init_coll_MainSizer_Growables(self.MainSizer)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    48
        self._init_coll_MainSizer_Items(self.MainSizer)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    49
        
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    50
        self.SetSizer(self.MainSizer)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    51
    
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    52
    def _init_ctrls(self, prnt, enable_drag=False):
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    53
        wx.Panel.__init__(self, id=ID_LIBRARYPANEL,
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    54
                  name='LibraryPanel', parent=prnt,
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    55
                  pos=wx.Point(0, 0), size=wx.Size(0, 0), 
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    56
                  style=wx.TAB_TRAVERSAL)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    57
        
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    58
        self.SearchCtrl = wx.SearchCtrl(id=ID_LIBRARYSEARCHCTRL, 
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    59
                  name='SearchCtrl', parent=self,
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    60
                  pos=wx.Point(0, 0), size=wx.Size(0, 28), style=0)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    61
        self.SearchCtrl.ShowSearchButton(True)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    62
        self.Bind(wx.EVT_TEXT, self.OnSearchCtrlChanged,
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    63
              id=ID_LIBRARYSEARCHCTRL)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    64
        self.Bind(wx.EVT_SEARCHCTRL_SEARCH_BTN, self.OnSearchButtonClick,
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    65
              id=ID_LIBRARYSEARCHCTRL)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    66
        search_textctrl = self.SearchCtrl.GetChildren()[0]
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    67
        search_textctrl.Bind(wx.EVT_CHAR, self.OnKeyDown)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    68
        
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    69
        self.Tree = wx.TreeCtrl(id=ID_LIBRARYTREE,
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    70
                  name='Tree', parent=self, 
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    71
                  pos=wx.Point(0, 0), size=wx.Size(0, 0),
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    72
                  style=wx.TR_HAS_BUTTONS|wx.TR_SINGLE|wx.SUNKEN_BORDER|wx.TR_HIDE_ROOT|wx.TR_LINES_AT_ROOT)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    73
        self.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnTreeItemSelected,
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    74
              id=ID_LIBRARYTREE)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    75
        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
    76
        if enable_drag:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    77
            self.Bind(wx.EVT_TREE_BEGIN_DRAG, self.OnTreeBeginDrag,
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    78
                  id=ID_LIBRARYTREE)
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.Comment = wx.TextCtrl(id=ID_LIBRARYCOMMENT,
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    81
              name='Comment', parent=self, 
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    82
              pos=wx.Point(0, 0), size=wx.Size(0, 80), 
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    83
              style=wx.TE_READONLY|wx.TE_MULTILINE)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    84
        
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    85
        self._init_sizers()
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    86
    
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    87
    def __init__(self, parent, enable_drag=False):
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    88
        self._init_ctrls(parent, enable_drag)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    89
        
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    90
        self.Controler = None
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
        self.BlockList = None
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    93
    
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    94
    def __del__(self):
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    95
        self.Controler = None
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    96
    
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    97
    def SetControler(self, controler):
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    98
        self.Controler = controler
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 SetBlockList(self, blocklist):
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   101
        self.BlockList = blocklist
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   102
        self.RefreshTree()
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   103
    
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   104
    def SetFocus(self):
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   105
        self.SearchCtrl.SetFocus()
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   106
    
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   107
    def ResetTree(self):
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   108
        self.SearchCtrl.SetValue("")
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   109
        self.Tree.DeleteAllItems()
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   110
        self.Comment.SetValue("")
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   111
    
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   112
    def RefreshTree(self):
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   113
        if self.Controler is not None:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   114
            to_delete = []
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   115
            selected_name = None
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   116
            selected = self.Tree.GetSelection()
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   117
            if selected.IsOk():
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   118
                selected_pydata = self.Tree.GetPyData(selected)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   119
                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
   120
                    selected_name = self.Tree.GetItemText(selected)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   121
            if self.BlockList is not None:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   122
                blocktypes = self.BlockList
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
                blocktypes = self.Controler.GetBlockTypes()
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   125
            root = self.Tree.GetRootItem()
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   126
            if not root.IsOk():
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   127
                root = self.Tree.AddRoot("")
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   128
            if wx.VERSION >= (2, 6, 0):
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   129
                category_item, root_cookie = self.Tree.GetFirstChild(root)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   130
            else:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   131
                category_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
   132
            for category in blocktypes:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   133
                category_name = category["name"]
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   134
                if not category_item.IsOk():
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   135
                    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
   136
                    if wx.Platform != '__WXMSW__':
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   137
                        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
   138
                else:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   139
                    self.Tree.SetItemText(category_item, _(category_name))
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   140
                self.Tree.SetPyData(category_item, {"type" : CATEGORY})
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   141
                if wx.VERSION >= (2, 6, 0):
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   142
                    blocktype_item, category_cookie = self.Tree.GetFirstChild(category_item)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   143
                else:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   144
                    blocktype_item, category_cookie = self.Tree.GetFirstChild(category_item, 0)        
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   145
                for blocktype in category["list"]:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   146
                    if not blocktype_item.IsOk():
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   147
                        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
   148
                        if wx.Platform != '__WXMSW__':
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   149
                            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
   150
                    else:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   151
                        self.Tree.SetItemText(blocktype_item, blocktype["name"])
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   152
                    block_data = {"type" : BLOCK, 
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   153
                                  "block_type" : blocktype["type"], 
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   154
                                  "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
   155
                                  "extension" : None}
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   156
                    if blocktype["extensible"]:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   157
                        block_data["extension"] = len(blocktype["inputs"])
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   158
                    self.Tree.SetPyData(blocktype_item, block_data)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   159
                    if selected_name == blocktype["name"]:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   160
                        self.Tree.SelectItem(blocktype_item)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   161
                        comment = blocktype["comment"]
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   162
                        self.Comment.SetValue(_(comment) + blocktype.get("usage", ""))
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   163
                    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
   164
                while blocktype_item.IsOk():
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   165
                    to_delete.append(blocktype_item)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   166
                    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
   167
                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
   168
            while category_item.IsOk():
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   169
                to_delete.append(category_item)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   170
                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
   171
            for item in to_delete:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   172
                self.Tree.Delete(item)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   173
    
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   174
    def GetSelectedBlock(self):
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   175
        selected = self.Tree.GetSelection()
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   176
        if (selected.IsOk() and 
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   177
            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
   178
            selected != self.Tree.GetRootItem()):
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   179
            selected_data = self.Tree.GetPyData(selected)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   180
            return {"type": self.Tree.GetItemText(selected), 
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   181
                    "inputs": selected_data["inputs"]}
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   182
        return None
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   183
    
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   184
    def SelectTreeItem(self, name, inputs):
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   185
        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
   186
        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
   187
            self.Tree.SelectItem(item)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   188
            self.Tree.EnsureVisible(item)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   189
    
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   190
    def FindTreeItem(self, root, name, inputs = None):
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   191
        if root.IsOk():
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   192
            pydata = self.Tree.GetPyData(root)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   193
            if pydata is not None:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   194
                type_inputs = pydata.get("inputs", None)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   195
                type_extension = pydata.get("extension", None)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   196
                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
   197
                    if type_extension is not None:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   198
                        same_inputs = type_inputs == inputs[:type_extension]
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   199
                    else:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   200
                        same_inputs = type_inputs == inputs
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   201
                else:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   202
                    same_inputs = True
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   203
            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
   204
                return root
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   205
            else:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   206
                if wx.VERSION < (2, 6, 0):
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   207
                    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
   208
                else:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   209
                    item, root_cookie = self.Tree.GetFirstChild(root)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   210
                while item.IsOk():
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   211
                    result = self.FindTreeItem(item, name, inputs)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   212
                    if result:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   213
                        return result
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   214
                    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
   215
        return None
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   216
    
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   217
    def SearchInTree(self, value, mode="first"):
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   218
        root = self.Tree.GetRootItem()
680
b693147fb2c3 Fixing bugs with latest changesets on Windows
laurent
parents: 676
diff changeset
   219
        if not root.IsOk():
b693147fb2c3 Fixing bugs with latest changesets on Windows
laurent
parents: 676
diff changeset
   220
            return False
b693147fb2c3 Fixing bugs with latest changesets on Windows
laurent
parents: 676
diff changeset
   221
        
676
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   222
        if mode == "first":
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   223
            item, item_cookie = self.Tree.GetFirstChild(root)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   224
            selected = None
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
            item = self.Tree.GetSelection()
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   227
            selected = item
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   228
            if not item.IsOk():
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   229
                item, item_cookie = self.Tree.GetFirstChild(root)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   230
        while item.IsOk():
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   231
            item_pydata = self.Tree.GetPyData(item)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   232
            if item_pydata["type"] == CATEGORY:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   233
                if mode == "previous":
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   234
                    child = self.Tree.GetLastChild(item)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   235
                else:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   236
                    child, child_cookie = self.Tree.GetFirstChild(item)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   237
                if child.IsOk():
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   238
                    item = child
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   239
                elif mode == "previous":
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   240
                    item = self.Tree.GetPrevSibling(item)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   241
                else:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   242
                    item = self.Tree.GetNextSibling(item)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   243
            else:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   244
                name = self.Tree.GetItemText(item)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   245
                if name.upper().startswith(value.upper()) and item != selected:
680
b693147fb2c3 Fixing bugs with latest changesets on Windows
laurent
parents: 676
diff changeset
   246
                    child, child_cookie = self.Tree.GetFirstChild(root)
b693147fb2c3 Fixing bugs with latest changesets on Windows
laurent
parents: 676
diff changeset
   247
                    while child.IsOk():
b693147fb2c3 Fixing bugs with latest changesets on Windows
laurent
parents: 676
diff changeset
   248
                        self.Tree.CollapseAllChildren(child)
b693147fb2c3 Fixing bugs with latest changesets on Windows
laurent
parents: 676
diff changeset
   249
                        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
   250
                    self.Tree.SelectItem(item)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   251
                    self.Tree.EnsureVisible(item)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   252
                    return True
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   253
                
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   254
                elif mode == "previous":
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   255
                    previous = self.Tree.GetPrevSibling(item)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   256
                    if previous.IsOk():
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   257
                        item = previous
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   258
                    else:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   259
                        parent = self.Tree.GetItemParent(item)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   260
                        item = self.Tree.GetPrevSibling(parent)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   261
                
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   262
                else:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   263
                    next = self.Tree.GetNextSibling(item)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   264
                    if next.IsOk():
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   265
                        item = next
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   266
                    else:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   267
                        parent = self.Tree.GetItemParent(item)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   268
                        item = self.Tree.GetNextSibling(parent)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   269
        return False
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   270
    
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   271
    def OnSearchCtrlChanged(self, event):
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   272
        self.SearchInTree(self.SearchCtrl.GetValue())
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   273
        event.Skip()
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   274
    
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   275
    def OnSearchButtonClick(self, event):
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   276
        self.SearchInTree(self.SearchCtrl.GetValue(), "next")
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   277
        event.Skip()
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   278
    
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   279
    def OnTreeItemSelected(self, event):
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   280
        selected = event.GetItem()
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   281
        pydata = self.Tree.GetPyData(selected)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   282
        if pydata is not None and pydata["type"] != CATEGORY:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   283
            blocktype = self.Controler.GetBlockType(self.Tree.GetItemText(selected), pydata["inputs"])
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   284
            if blocktype:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   285
                comment = blocktype["comment"]
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   286
                self.Comment.SetValue(_(comment) + blocktype.get("usage", ""))
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   287
            else:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   288
                self.Comment.SetValue("")
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   289
        else:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   290
            self.Comment.SetValue("")
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   291
        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
   292
            self._OnTreeItemSelected(event)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   293
        event.Skip()
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   294
    
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   295
    def OnTreeBeginDrag(self, event):
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   296
        selected = event.GetItem()
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   297
        pydata = self.Tree.GetPyData(selected)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   298
        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
   299
            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
   300
                pydata["block_type"], "", pydata["inputs"])))
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   301
            dragSource = wx.DropSource(self.Tree)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   302
            dragSource.SetData(data)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   303
            dragSource.DoDragDrop()
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   304
    
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   305
    def OnKeyDown(self, event):
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   306
        keycode = event.GetKeyCode()
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   307
        search_value = self.SearchCtrl.GetValue()
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   308
        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
   309
            self.SearchInTree(search_value, "previous")
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   310
        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
   311
            self.SearchInTree(search_value, "next")
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   312
        else:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   313
            event.Skip()