controls/LibraryPanel.py
author Laurent Bessard
Fri, 06 Dec 2013 13:24:28 +0100
changeset 1383 45935812f930
parent 1235 1a30c70fa025
child 1501 d917c209529d
permissions -rw-r--r--
Added example of SFC simulating traffic lights
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     1
#!/usr/bin/env python
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     3
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     4
#This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     5
#based on the plcopen standard. 
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     6
#
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     7
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     8
#
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     9
#See COPYING file for copyrights details.
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    10
#
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    11
#This library is free software; you can redistribute it and/or
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    12
#modify it under the terms of the GNU General Public
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    13
#License as published by the Free Software Foundation; either
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    14
#version 2.1 of the License, or (at your option) any later version.
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    15
#
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    16
#This library is distributed in the hope that it will be useful,
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    17
#but WITHOUT ANY WARRANTY; without even the implied warranty of
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    18
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    19
#General Public License for more details.
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    20
#
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    21
#You should have received a copy of the GNU General Public
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    22
#License along with this library; if not, write to the Free Software
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    23
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    24
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    25
import wx
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    26
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    27
#-------------------------------------------------------------------------------
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    28
#                                 Helpers
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    29
#-------------------------------------------------------------------------------
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    30
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    31
[CATEGORY, BLOCK] = range(2)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    32
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    33
#-------------------------------------------------------------------------------
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    34
#                              Library Panel
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    35
#-------------------------------------------------------------------------------
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    36
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
    37
"""
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
    38
Class that implements a panel displaying a tree containing an hierarchical list
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
    39
of functions and function blocks available in project an a search control for
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
    40
quickly find one functions or function blocks in this list and a text control
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
    41
displaying informations about selected functions or function blocks
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
    42
"""
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
    43
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    44
class LibraryPanel(wx.Panel):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    45
    
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    46
    def __init__(self, parent, enable_drag=False):
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
    47
        """
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
    48
        Constructor
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
    49
        @param parent: Parent wx.Window of LibraryPanel
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
    50
        @param enable_drag: Flag indicating that function or function block can
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
    51
        be drag'n drop from LibraryPanel (default: False)
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
    52
        """
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    53
        wx.Panel.__init__(self, parent, style=wx.TAB_TRAVERSAL)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    54
        
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
    55
        # Define LibraryPanel main sizer
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    56
        main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=0)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    57
        main_sizer.AddGrowableCol(0)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    58
        main_sizer.AddGrowableRow(1)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    59
        
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
    60
        # Add SearchCtrl to main sizer
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    61
        self.SearchCtrl = wx.SearchCtrl(self)
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
    62
        # Add a button with a magnifying glass, essentially to show that this
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
    63
        # control is for searching in tree
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    64
        self.SearchCtrl.ShowSearchButton(True)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    65
        self.Bind(wx.EVT_TEXT, self.OnSearchCtrlChanged, self.SearchCtrl)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    66
        self.Bind(wx.EVT_SEARCHCTRL_SEARCH_BTN, 
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
    67
                  self.OnSearchButtonClick, self.SearchCtrl)
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
    68
        # Bind keyboard event on SearchCtrl text control to catch UP and DOWN
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
    69
        # for search previous and next occurrence
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    70
        search_textctrl = self.SearchCtrl.GetChildren()[0]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    71
        search_textctrl.Bind(wx.EVT_CHAR, self.OnKeyDown)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    72
        main_sizer.AddWindow(self.SearchCtrl, flag=wx.GROW)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    73
        
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
    74
        # Add Splitter window for tree and block comment to main sizer
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    75
        splitter_window = wx.SplitterWindow(self)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    76
        splitter_window.SetSashGravity(1.0)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    77
        main_sizer.AddWindow(splitter_window, flag=wx.GROW)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    78
        
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
    79
        # Add TreeCtrl for functions and function blocks library in splitter
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
    80
        # window
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    81
        self.Tree = wx.TreeCtrl(splitter_window,
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    82
              size=wx.Size(0, 0),  
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    83
              style=wx.TR_HAS_BUTTONS|
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    84
                    wx.TR_SINGLE|
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    85
                    wx.SUNKEN_BORDER|
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    86
                    wx.TR_HIDE_ROOT|
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    87
                    wx.TR_LINES_AT_ROOT)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    88
        self.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnTreeItemSelected, self.Tree)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    89
        self.Tree.Bind(wx.EVT_CHAR, self.OnKeyDown)
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
    90
        # If drag'n drop is enabled, bind event generated when a drag begins on
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
    91
        # tree to start a drag'n drop
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    92
        if enable_drag:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    93
            self.Bind(wx.EVT_TREE_BEGIN_DRAG, self.OnTreeBeginDrag, self.Tree)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    94
        
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
    95
        # Add TextCtrl for function and function block informations
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    96
        self.Comment = wx.TextCtrl(splitter_window, size=wx.Size(0, 80), 
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    97
              style=wx.TE_READONLY|wx.TE_MULTILINE)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    98
        
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    99
        splitter_window.SplitHorizontally(self.Tree, self.Comment, -80)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   100
        
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   101
        self.SetSizer(main_sizer)
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   102
        
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   103
        # Reference to the project controller
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   104
        self.Controller = None
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   105
        
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   106
        # Variable storing functions and function blocks library to display
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   107
        self.BlockList = None
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   108
    
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   109
    def __del__(self):
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   110
        """
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   111
        Destructor
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   112
        """
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   113
        # Remove reference to project controller
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   114
        self.Controller = None
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   115
    
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   116
    def SetController(self, controller):
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   117
        """
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   118
        Set reference to project controller
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   119
        @param controller: Reference to project controller
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   120
        """
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   121
        self.Controller = controller
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   122
    
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   123
    def SetBlockList(self, blocklist):
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   124
        """
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   125
        Set function and function block library to display in TreeCtrl
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   126
        @param blocklist: Function and function block library
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   127
        """
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   128
        # Save functions and function blocks library
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   129
        self.BlockList = blocklist
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   130
        # Refresh TreeCtrl values
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   131
        self.RefreshTree()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   132
    
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   133
    def SetFocus(self):
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   134
        """
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   135
        Called to give focus to LibraryPanel
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   136
        Override wx.Window SetFocus method 
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   137
        """
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   138
        # Give focus to SearchCtrl
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   139
        self.SearchCtrl.SetFocus()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   140
    
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   141
    def ResetTree(self):
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   142
        """
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   143
        Reset LibraryPanel values displayed in controls
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   144
        """
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   145
        # Clear SearchCtrl, TreeCtrl and TextCtrl
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   146
        self.SearchCtrl.SetValue("")
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   147
        self.Tree.DeleteAllItems()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   148
        self.Comment.SetValue("")
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   149
    
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   150
    def RefreshTree(self):
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   151
        """
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   152
        Refresh LibraryPanel values displayed in controls
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   153
        """
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   154
        # Get function and function blocks library
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   155
        blocktypes = self.BlockList
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   156
        if blocktypes is None and self.Controller is not None:
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   157
            # Get library from project controller if not defined
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   158
            blocktypes = self.Controller.GetBlockTypes()
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   159
        
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   160
        # Refresh TreeCtrl values if a library is defined
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   161
        if blocktypes is not None:
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   162
            # List that will contain tree items to be deleted when TreeCtrl
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   163
            # will be refreshed
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   164
            items_to_delete = []
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   165
            
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   166
            # Get current selected item for selected it when values refreshed
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   167
            selected_item = self.Tree.GetSelection()
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   168
            selected_pydata = (self.Tree.GetPyData(selected_item)
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   169
                               if selected_item.IsOk() and
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   170
                                  selected_item != self.Tree.GetRootItem()
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   171
                               else None)
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   172
            # Don't save selected item if it is a category
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   173
            selected_infos = ((self.Tree.GetItemText(selected_item),
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   174
                               selected_pydata["inputs"])
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   175
                             if selected_pydata is not None and
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   176
                                selected_pydata["type"] == BLOCK
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   177
                             else (None, None))
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   178
            
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   179
            # Get TreeCtrl root item (hidden)
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   180
            root = self.Tree.GetRootItem()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   181
            if not root.IsOk():
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   182
                # Create root if not present
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   183
                root = self.Tree.AddRoot("")
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   184
            
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   185
            # Iterate over functions and function blocks library categories and
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   186
            # add a tree item to root item for each of them
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   187
            
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   188
            # Get first child under root item
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   189
            category_item, root_cookie = self.Tree.GetFirstChild(root)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   190
            for category in blocktypes:
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   191
                # Store category name in a local variable to prevent script
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   192
                # extracting translated strings for gettext to consider "name"
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   193
                # to be translated
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   194
                category_name = category["name"]
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   195
                
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   196
                # Tree item already exists, set item label
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   197
                if category_item.IsOk():
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   198
                    self.Tree.SetItemText(category_item, _(category_name))
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   199
                
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   200
                # Tree item doesn't exist, add new one to root
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   201
                else:
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   202
                    category_item = self.Tree.AppendItem(root, _(category_name))
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   203
                    # On Windows, needs to get next child of root to have a
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   204
                    # reference to the newly added tree item
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   205
                    if wx.Platform != '__WXMSW__':
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   206
                        category_item, root_cookie = \
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   207
                            self.Tree.GetNextChild(root, root_cookie)
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   208
                
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   209
                # Set data associated to tree item (only save that item is a 
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   210
                # category)
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   211
                self.Tree.SetPyData(category_item, {"type" : CATEGORY})
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   212
                
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   213
                # Iterate over functions and function blocks defined in library
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   214
                # category add a tree item to category tree item for each of 
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   215
                # them
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   216
                
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   217
                # Get first child under category tree item
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   218
                blocktype_item, category_cookie = \
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   219
                    self.Tree.GetFirstChild(category_item)
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   220
                for blocktype in category["list"]:
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   221
                    
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   222
                    # Tree item already exists, set item label
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   223
                    if blocktype_item.IsOk():
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   224
                        self.Tree.SetItemText(blocktype_item, blocktype["name"])
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   225
                    
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   226
                    # Tree item doesn't exist, add new one to category item
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   227
                    else:
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   228
                        blocktype_item = self.Tree.AppendItem(
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   229
                                            category_item, blocktype["name"])
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   230
                        # See comment when adding category
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   231
                        if wx.Platform != '__WXMSW__':
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   232
                            blocktype_item, category_cookie = \
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   233
                                self.Tree.GetNextChild(category_item, 
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   234
                                                       category_cookie)
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   235
                    
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   236
                    # Define data to associate to block tree item
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   237
                    comment = blocktype["comment"]
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   238
                    block_data = {"type" : BLOCK, 
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   239
                                  "block_type" : blocktype["type"], 
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   240
                                  "inputs" : tuple([type 
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   241
                                                    for name, type, modifier
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   242
                                                    in blocktype["inputs"]]), 
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   243
                                  "extension" : (len(blocktype["inputs"])
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   244
                                                 if blocktype["extensible"]
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   245
                                                 else None),
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   246
                                  "comment": _(comment) + 
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   247
                                             blocktype.get("usage", "")}
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   248
                    self.Tree.SetPyData(blocktype_item, block_data)
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   249
                    
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   250
                    # Select block tree item in tree if it corresponds to
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   251
                    # previously selected one
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   252
                    if selected_infos == (blocktype["name"], 
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   253
                                          blocktype["inputs"]):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   254
                        self.Tree.SelectItem(blocktype_item)
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   255
                        
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   256
                        # Update TextCtrl value
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   257
                        self.Comment.SetValue(block_data["comment"])
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   258
                
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   259
                    # Get next block tree item under category tree item
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   260
                    blocktype_item, category_cookie = \
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   261
                        self.Tree.GetNextChild(category_item, category_cookie)
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   262
                
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   263
                # Add every remaining tree item under category tree item after
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   264
                # updating all block items to the list of items to delete
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   265
                while blocktype_item.IsOk():
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   266
                    items_to_delete.append(blocktype_item)
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   267
                    blocktype_item, category_cookie = \
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   268
                        self.Tree.GetNextChild(category_item, category_cookie)
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   269
                
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   270
                # Get next category tree item under root item
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   271
                category_item, root_cookie = \
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   272
                    self.Tree.GetNextChild(root, root_cookie)
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   273
            
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   274
            # Add every remaining tree item under root item after updating all 
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   275
            # category items to the list of items to delete
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   276
            while category_item.IsOk():
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   277
                items_to_delete.append(category_item)
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   278
                category_item, root_cookie = \
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   279
                    self.Tree.GetNextChild(root, root_cookie)
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   280
            
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   281
            # Remove all items in list of items to delete from TreeCtrl
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   282
            for item in items_to_delete:
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   283
                self.Tree.Delete(item)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   284
    
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   285
    def GetSelectedBlock(self):
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   286
        """
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   287
        Get selected block informations
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   288
        @return: {"type": block_type_name, "inputs": [input_type,...]} or None
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   289
        if no block selected
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   290
        """
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   291
        # Get selected item associated data in tree
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   292
        selected_item = self.Tree.GetSelection()
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   293
        selected_pydata = (self.Tree.GetPyData(selected_item)
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   294
                           if selected_item.IsOk() and
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   295
                              selected_item != self.Tree.GetRootItem()
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   296
                           else None)
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   297
        
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   298
        # Return value is None if selected tree item is root or a category
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   299
        return ({"type": self.Tree.GetItemText(selected_item), 
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   300
                 "inputs": selected_pydata["inputs"]}
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   301
                if selected_pydata is not None and 
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   302
                   selected_pydata["type"] == BLOCK
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   303
                else None)
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   304
    
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   305
    def SelectTreeItem(self, name, inputs):
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   306
        """
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   307
        Select Tree item corresponding to block informations given 
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   308
        @param name: Block type name
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   309
        @param inputs: List of block inputs type [input_type,...]
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   310
        """
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   311
        # Find tree item corresponding to block informations
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   312
        item = self.FindTreeItem(self.Tree.GetRootItem(), name, inputs)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   313
        if item is not None and item.IsOk():
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   314
            # Select tree item found
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   315
            self.Tree.SelectItem(item)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   316
            self.Tree.EnsureVisible(item)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   317
    
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   318
    def FindTreeItem(self, item, name, inputs = None):
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   319
        """
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   320
        Find Tree item corresponding to block informations given
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   321
        Function is recursive
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   322
        @param item: Item to test
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   323
        @param name: Block type name
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   324
        @param inputs: List of block inputs type [input_type,...]
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   325
        """
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   326
        # Return immediately if item isn't valid
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   327
        if not item.IsOk():
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   328
            return None
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   329
        
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   330
        # Get data associated to item to test
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   331
        item_pydata = self.Tree.GetPyData(item)
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   332
        if item_pydata is not None and item_pydata["type"] == BLOCK:
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   333
            # Only test item corresponding to block
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   334
            
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   335
            # Test if block inputs type are the same than those given
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   336
            type_inputs = item_pydata.get("inputs", None)
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   337
            type_extension = item_pydata.get("extension", None)
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   338
            if inputs is not None and type_inputs is not None:
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   339
                same_inputs = reduce(
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   340
                    lambda x, y: x and y,
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   341
                    map(
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   342
                        lambda x: x[0]==x[1] or x[0]=='ANY' or x[1]=='ANY',
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   343
                        zip(type_inputs,
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   344
                            (inputs[:type_extension]
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   345
                             if type_extension is not None
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   346
                             else inputs))),
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   347
                    True)
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   348
            else:
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   349
                same_inputs = True
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   350
            
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   351
            # Return item if  block data corresponds to informations given
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   352
            if self.Tree.GetItemText(item) == name and same_inputs:
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   353
                return item
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   354
        
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   355
        # Test item children if item doesn't correspond
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   356
        child, child_cookie = self.Tree.GetFirstChild(item)
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   357
        while child.IsOk():
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   358
            result = self.FindTreeItem(child, name, inputs)
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   359
            if result:
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   360
                return result
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   361
            child, child_cookie = self.Tree.GetNextChild(item, child_cookie)
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   362
        
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   363
        return None
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   364
    
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   365
    def SearchInTree(self, value, mode="first"):
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   366
        """
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   367
        Search in Tree and select item that name contains string given
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   368
        @param value: String contained in block name to find
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   369
        @param mode: Search mode ('first', 'previous' or 'next')
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   370
        (default: 'first')
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   371
        @return: True if an item was found
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   372
        """
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   373
        # Return immediately if root isn't valid
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   374
        root = self.Tree.GetRootItem()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   375
        if not root.IsOk():
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   376
            return False
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   377
        
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   378
        # Set function to navigate in Tree item sibling according to search
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   379
        # mode defined 
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   380
        sibling_function = (self.Tree.GetPrevSibling
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   381
                            if mode == "previous"
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   382
                            else self.Tree.GetNextSibling)
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   383
        
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   384
        # Get current selected item (for next and previous mode)
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   385
        item = self.Tree.GetSelection()
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   386
        if not item.IsOk() or mode == "first":
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   387
            item, item_cookie = self.Tree.GetFirstChild(root)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   388
            selected = None
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   389
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   390
            selected = item
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   391
        
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   392
        # Navigate through tree items until one matching found or reach tree
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   393
        # starting or ending
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   394
        while item.IsOk():
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   395
            
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   396
            # Get item data to get item type
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   397
            item_pydata = self.Tree.GetPyData(item)
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   398
            
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   399
            # Item is a block category
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   400
            if item_pydata["type"] == CATEGORY:
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   401
                
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   402
                # Get category first or last child according to search mode 
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   403
                # defined
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   404
                child = (self.Tree.GetLastChild(item)
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   405
                         if mode == "previous"
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   406
                         else self.Tree.GetFirstChild(item)[0])
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   407
                
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   408
                # If category has no child, go to sibling category
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   409
                item = (child if child.IsOk() else sibling_function(item))
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   410
            
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   411
            # Item is a block
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   412
            else:
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   413
                
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   414
                # Extract item block name
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   415
                name = self.Tree.GetItemText(item)
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   416
                # Test if block name contains string given
1068
ef088254ba4b Modify search algorithm in LibraryPanel to search match in whole block name, not only at beginning
Laurent Bessard
parents: 814
diff changeset
   417
                if name.upper().find(value.upper()) != -1 and item != selected:
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   418
                    # Select block and collapse all categories other than block
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   419
                    # category
1235
1a30c70fa025 Fixed bug when searching in LibraryPanel on Windows
Laurent Bessard
parents: 1230
diff changeset
   420
                    child, child_cookie = self.Tree.GetFirstChild(root)
1a30c70fa025 Fixed bug when searching in LibraryPanel on Windows
Laurent Bessard
parents: 1230
diff changeset
   421
                    while child.IsOk():
1a30c70fa025 Fixed bug when searching in LibraryPanel on Windows
Laurent Bessard
parents: 1230
diff changeset
   422
                        self.Tree.CollapseAllChildren(child)
1a30c70fa025 Fixed bug when searching in LibraryPanel on Windows
Laurent Bessard
parents: 1230
diff changeset
   423
                        child, child_cookie = self.Tree.GetNextChild(root, child_cookie)
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   424
                    self.Tree.SelectItem(item)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   425
                    self.Tree.EnsureVisible(item)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   426
                    return True
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   427
                
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   428
                # Go to next item sibling if block not found
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   429
                next = sibling_function(item)
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   430
                
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   431
                # If category has no other child, go to next category sibling
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   432
                item = (next
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   433
                        if next.IsOk()
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   434
                        else sibling_function(self.Tree.GetItemParent(item)))
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   435
        
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   436
        return False
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   437
    
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   438
    def OnSearchCtrlChanged(self, event):
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   439
        """
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   440
        Called when SearchCtrl text control value changed
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   441
        @param event: TextCtrl change event
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   442
        """
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   443
        # Search for block containing SearchCtrl value in 'first' mode
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   444
        self.SearchInTree(self.SearchCtrl.GetValue())
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   445
        event.Skip()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   446
    
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   447
    def OnSearchButtonClick(self, event):
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   448
        """
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   449
        Called when SearchCtrl search button was clicked
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   450
        @param event: Button clicked event
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   451
        """
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   452
        # Search for block containing SearchCtrl value in 'next' mode
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   453
        self.SearchInTree(self.SearchCtrl.GetValue(), "next")
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   454
        event.Skip()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   455
    
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   456
    def OnTreeItemSelected(self, event):
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   457
        """
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   458
        Called when tree item is selected
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   459
        @param event: wx.TreeEvent
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   460
        """
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   461
        # Update TextCtrl value with block selected usage
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   462
        item_pydata = self.Tree.GetPyData(event.GetItem())
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   463
        self.Comment.SetValue(
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   464
            item_pydata["comment"]
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   465
            if item_pydata is not None and item_pydata["type"] == BLOCK
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   466
            else "")
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   467
        
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   468
        # Call extra function defined when tree item is selected
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   469
        if getattr(self, "_OnTreeItemSelected", None) is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   470
            self._OnTreeItemSelected(event)
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   471
        
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   472
        event.Skip()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   473
    
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   474
    def OnTreeBeginDrag(self, event):
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   475
        """
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   476
        Called when a drag is started in tree
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   477
        @param event: wx.TreeEvent
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   478
        """
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   479
        selected_item = event.GetItem()
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   480
        item_pydata = self.Tree.GetPyData(selected_item)
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   481
        
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   482
        # Item dragged is a block
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   483
        if item_pydata is not None and item_pydata["type"] == BLOCK:
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   484
            # Start a drag'n drop
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   485
            data = wx.TextDataObject(str(
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   486
                (self.Tree.GetItemText(selected_item), 
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   487
                 item_pydata["block_type"], 
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   488
                 "", 
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   489
                 item_pydata["inputs"])))
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   490
            dragSource = wx.DropSource(self.Tree)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   491
            dragSource.SetData(data)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   492
            dragSource.DoDragDrop()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   493
    
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   494
    def OnKeyDown(self, event):
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   495
        """
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   496
        Called when key is pressed in SearchCtrl text control
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   497
        @param event: wx.KeyEvent
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   498
        """
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   499
        # Get event keycode and value in SearchCtrl
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   500
        keycode = event.GetKeyCode()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   501
        search_value = self.SearchCtrl.GetValue()
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   502
        
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   503
        # Up key was pressed and SearchCtrl isn't empty, search for block in
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   504
        # 'previous' mode 
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   505
        if keycode == wx.WXK_UP and search_value != "":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   506
            self.SearchInTree(search_value, "previous")
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   507
        
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   508
        # Down key was pressed and SearchCtrl isn't empty, search for block in
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   509
        # 'next' mode 
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   510
        elif keycode == wx.WXK_DOWN and search_value != "":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   511
            self.SearchInTree(search_value, "next")
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   512
        
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   513
        # Handle key normally
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   514
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   515
            event.Skip()