controls/LibraryPanel.py
author Laurent Bessard
Thu, 28 Jun 2012 12:00:45 +0200
changeset 716 2681a6da58d6
parent 714 131ea7f237b9
child 741 330f578e228d
permissions -rw-r--r--
Adding support for drag'n dropping constant values
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
        
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 680
diff changeset
    42
        main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=3, vgap=0)
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
        
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 680
diff changeset
    55
        self.Tree = wx.TreeCtrl(self, 
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 680
diff changeset
    56
              style=wx.TR_HAS_BUTTONS|
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 680
diff changeset
    57
                    wx.TR_SINGLE|
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 680
diff changeset
    58
                    wx.SUNKEN_BORDER|
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 680
diff changeset
    59
                    wx.TR_HIDE_ROOT|
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 680
diff changeset
    60
                    wx.TR_LINES_AT_ROOT)
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 680
diff changeset
    61
        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
    62
        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
    63
        if enable_drag:
714
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 680
diff changeset
    64
            self.Bind(wx.EVT_TREE_BEGIN_DRAG, self.OnTreeBeginDrag, self.Tree)
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 680
diff changeset
    65
        main_sizer.AddWindow(self.Tree, flag=wx.GROW)
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 680
diff changeset
    66
        
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 680
diff changeset
    67
        self.Comment = wx.TextCtrl(self, size=wx.Size(0, 80), 
676
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    68
              style=wx.TE_READONLY|wx.TE_MULTILINE)
714
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 680
diff changeset
    69
        main_sizer.AddWindow(self.Comment, flag=wx.GROW)
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 680
diff changeset
    70
        
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 680
diff changeset
    71
        self.SetSizer(main_sizer)
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 680
diff changeset
    72
            
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 680
diff changeset
    73
        self.Controller = None
676
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    74
    
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    75
        self.BlockList = None
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    76
    
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    77
    def __del__(self):
714
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 680
diff changeset
    78
        self.Controller = None
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 680
diff changeset
    79
    
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 680
diff changeset
    80
    def SetController(self, controller):
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 680
diff changeset
    81
        self.Controller = controller
676
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    82
    
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    83
    def SetBlockList(self, blocklist):
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    84
        self.BlockList = blocklist
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    85
        self.RefreshTree()
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 SetFocus(self):
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    88
        self.SearchCtrl.SetFocus()
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
    def ResetTree(self):
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    91
        self.SearchCtrl.SetValue("")
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    92
        self.Tree.DeleteAllItems()
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    93
        self.Comment.SetValue("")
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 RefreshTree(self):
714
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 680
diff changeset
    96
        if self.Controller is not None:
676
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    97
            to_delete = []
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    98
            selected_name = None
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
    99
            selected = self.Tree.GetSelection()
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   100
            if selected.IsOk():
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   101
                selected_pydata = self.Tree.GetPyData(selected)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   102
                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
   103
                    selected_name = self.Tree.GetItemText(selected)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   104
            if self.BlockList is not None:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   105
                blocktypes = self.BlockList
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   106
            else:
714
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 680
diff changeset
   107
                blocktypes = self.Controller.GetBlockTypes()
676
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   108
            root = self.Tree.GetRootItem()
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   109
            if not root.IsOk():
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   110
                root = self.Tree.AddRoot("")
714
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 680
diff changeset
   111
            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
   112
            for category in blocktypes:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   113
                category_name = category["name"]
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   114
                if not category_item.IsOk():
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   115
                    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
   116
                    if wx.Platform != '__WXMSW__':
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   117
                        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
   118
                else:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   119
                    self.Tree.SetItemText(category_item, _(category_name))
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   120
                self.Tree.SetPyData(category_item, {"type" : CATEGORY})
714
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 680
diff changeset
   121
                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
   122
                for blocktype in category["list"]:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   123
                    if not blocktype_item.IsOk():
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   124
                        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
   125
                        if wx.Platform != '__WXMSW__':
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   126
                            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
   127
                    else:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   128
                        self.Tree.SetItemText(blocktype_item, blocktype["name"])
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   129
                    block_data = {"type" : BLOCK, 
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   130
                                  "block_type" : blocktype["type"], 
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   131
                                  "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
   132
                                  "extension" : None}
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   133
                    if blocktype["extensible"]:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   134
                        block_data["extension"] = len(blocktype["inputs"])
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   135
                    self.Tree.SetPyData(blocktype_item, block_data)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   136
                    if selected_name == blocktype["name"]:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   137
                        self.Tree.SelectItem(blocktype_item)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   138
                        comment = blocktype["comment"]
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   139
                        self.Comment.SetValue(_(comment) + blocktype.get("usage", ""))
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   140
                    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
   141
                while blocktype_item.IsOk():
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   142
                    to_delete.append(blocktype_item)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   143
                    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
   144
                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
   145
            while category_item.IsOk():
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   146
                to_delete.append(category_item)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   147
                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
   148
            for item in to_delete:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   149
                self.Tree.Delete(item)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   150
    
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   151
    def GetSelectedBlock(self):
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   152
        selected = self.Tree.GetSelection()
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   153
        if (selected.IsOk() and 
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   154
            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
   155
            selected != self.Tree.GetRootItem()):
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   156
            selected_data = self.Tree.GetPyData(selected)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   157
            return {"type": self.Tree.GetItemText(selected), 
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   158
                    "inputs": selected_data["inputs"]}
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   159
        return None
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   160
    
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   161
    def SelectTreeItem(self, name, inputs):
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   162
        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
   163
        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
   164
            self.Tree.SelectItem(item)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   165
            self.Tree.EnsureVisible(item)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   166
    
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   167
    def FindTreeItem(self, root, name, inputs = None):
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   168
        if root.IsOk():
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   169
            pydata = self.Tree.GetPyData(root)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   170
            if pydata is not None:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   171
                type_inputs = pydata.get("inputs", None)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   172
                type_extension = pydata.get("extension", None)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   173
                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
   174
                    if type_extension is not None:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   175
                        same_inputs = type_inputs == inputs[:type_extension]
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   176
                    else:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   177
                        same_inputs = type_inputs == inputs
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   178
                else:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   179
                    same_inputs = True
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   180
            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
   181
                return root
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   182
            else:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   183
                if wx.VERSION < (2, 6, 0):
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   184
                    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
   185
                else:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   186
                    item, root_cookie = self.Tree.GetFirstChild(root)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   187
                while item.IsOk():
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   188
                    result = self.FindTreeItem(item, name, inputs)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   189
                    if result:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   190
                        return result
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   191
                    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
   192
        return None
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   193
    
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   194
    def SearchInTree(self, value, mode="first"):
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   195
        root = self.Tree.GetRootItem()
680
b693147fb2c3 Fixing bugs with latest changesets on Windows
laurent
parents: 676
diff changeset
   196
        if not root.IsOk():
b693147fb2c3 Fixing bugs with latest changesets on Windows
laurent
parents: 676
diff changeset
   197
            return False
b693147fb2c3 Fixing bugs with latest changesets on Windows
laurent
parents: 676
diff changeset
   198
        
676
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   199
        if mode == "first":
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   200
            item, item_cookie = self.Tree.GetFirstChild(root)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   201
            selected = None
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   202
        else:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   203
            item = self.Tree.GetSelection()
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   204
            selected = item
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   205
            if not item.IsOk():
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   206
                item, item_cookie = self.Tree.GetFirstChild(root)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   207
        while item.IsOk():
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   208
            item_pydata = self.Tree.GetPyData(item)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   209
            if item_pydata["type"] == CATEGORY:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   210
                if mode == "previous":
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   211
                    child = self.Tree.GetLastChild(item)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   212
                else:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   213
                    child, child_cookie = self.Tree.GetFirstChild(item)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   214
                if child.IsOk():
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   215
                    item = child
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   216
                elif mode == "previous":
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   217
                    item = self.Tree.GetPrevSibling(item)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   218
                else:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   219
                    item = self.Tree.GetNextSibling(item)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   220
            else:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   221
                name = self.Tree.GetItemText(item)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   222
                if name.upper().startswith(value.upper()) and item != selected:
680
b693147fb2c3 Fixing bugs with latest changesets on Windows
laurent
parents: 676
diff changeset
   223
                    child, child_cookie = self.Tree.GetFirstChild(root)
b693147fb2c3 Fixing bugs with latest changesets on Windows
laurent
parents: 676
diff changeset
   224
                    while child.IsOk():
b693147fb2c3 Fixing bugs with latest changesets on Windows
laurent
parents: 676
diff changeset
   225
                        self.Tree.CollapseAllChildren(child)
b693147fb2c3 Fixing bugs with latest changesets on Windows
laurent
parents: 676
diff changeset
   226
                        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
   227
                    self.Tree.SelectItem(item)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   228
                    self.Tree.EnsureVisible(item)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   229
                    return True
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   230
                
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   231
                elif mode == "previous":
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   232
                    previous = self.Tree.GetPrevSibling(item)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   233
                    if previous.IsOk():
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   234
                        item = previous
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
                        parent = self.Tree.GetItemParent(item)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   237
                        item = self.Tree.GetPrevSibling(parent)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   238
                
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   239
                else:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   240
                    next = self.Tree.GetNextSibling(item)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   241
                    if next.IsOk():
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   242
                        item = next
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
                        parent = self.Tree.GetItemParent(item)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   245
                        item = self.Tree.GetNextSibling(parent)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   246
        return False
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   247
    
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   248
    def OnSearchCtrlChanged(self, event):
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   249
        self.SearchInTree(self.SearchCtrl.GetValue())
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   250
        event.Skip()
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   251
    
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   252
    def OnSearchButtonClick(self, event):
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   253
        self.SearchInTree(self.SearchCtrl.GetValue(), "next")
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   254
        event.Skip()
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   255
    
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   256
    def OnTreeItemSelected(self, event):
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   257
        selected = event.GetItem()
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   258
        pydata = self.Tree.GetPyData(selected)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   259
        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
   260
            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
   261
            if blocktype:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   262
                comment = blocktype["comment"]
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   263
                self.Comment.SetValue(_(comment) + blocktype.get("usage", ""))
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   264
            else:
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   265
                self.Comment.SetValue("")
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
            self.Comment.SetValue("")
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   268
        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
   269
            self._OnTreeItemSelected(event)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   270
        event.Skip()
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   271
    
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   272
    def OnTreeBeginDrag(self, event):
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   273
        selected = event.GetItem()
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   274
        pydata = self.Tree.GetPyData(selected)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   275
        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
   276
            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
   277
                pydata["block_type"], "", pydata["inputs"])))
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   278
            dragSource = wx.DropSource(self.Tree)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   279
            dragSource.SetData(data)
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   280
            dragSource.DoDragDrop()
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   281
    
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   282
    def OnKeyDown(self, event):
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   283
        keycode = event.GetKeyCode()
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   284
        search_value = self.SearchCtrl.GetValue()
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   285
        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
   286
            self.SearchInTree(search_value, "previous")
0f10f5091245 Adding search field for finding function or function block in library tree
laurent
parents:
diff changeset
   287
        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
   288
            self.SearchInTree(search_value, "next")
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
            event.Skip()