controls/LibraryPanel.py
author Sergey Surkov <surkovsv93@gmail.com>
Tue, 18 Oct 2016 17:44:08 +0300
changeset 1544 2969c2123105
parent 1528 d551f2925a86
child 1571 486f94a8032c
permissions -rw-r--r--
Fix bug with two or more wires connected to one input. Now only one wire can be connected to one input, except BOOLean signals in LD and SFC. If user trying to connect wire with already connected input, wire highlight will become red.
Signed-off-by: Andrey Skvortsov <andrej.skvortzov@gmail.com>
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
1528
d551f2925a86 Fixed crash when starts on OS X with wxPython 3.0.x
alexander@macbook-pro-alexander.local
parents: 1501
diff changeset
    70
d551f2925a86 Fixed crash when starts on OS X with wxPython 3.0.x
alexander@macbook-pro-alexander.local
parents: 1501
diff changeset
    71
        # This protects from fail to start when no children[0] available (possible for wxPython 3.0)
d551f2925a86 Fixed crash when starts on OS X with wxPython 3.0.x
alexander@macbook-pro-alexander.local
parents: 1501
diff changeset
    72
        if self.SearchCtrl.GetChildren():
d551f2925a86 Fixed crash when starts on OS X with wxPython 3.0.x
alexander@macbook-pro-alexander.local
parents: 1501
diff changeset
    73
            search_textctrl = self.SearchCtrl.GetChildren()[0]
d551f2925a86 Fixed crash when starts on OS X with wxPython 3.0.x
alexander@macbook-pro-alexander.local
parents: 1501
diff changeset
    74
            search_textctrl.Bind(wx.EVT_CHAR, self.OnKeyDown)
d551f2925a86 Fixed crash when starts on OS X with wxPython 3.0.x
alexander@macbook-pro-alexander.local
parents: 1501
diff changeset
    75
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    76
        main_sizer.AddWindow(self.SearchCtrl, flag=wx.GROW)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    77
        
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
    78
        # Add Splitter window for tree and block comment to main sizer
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    79
        splitter_window = wx.SplitterWindow(self)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    80
        splitter_window.SetSashGravity(1.0)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    81
        main_sizer.AddWindow(splitter_window, flag=wx.GROW)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    82
        
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
    83
        # Add TreeCtrl for functions and function blocks library in splitter
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
    84
        # window
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    85
        self.Tree = wx.TreeCtrl(splitter_window,
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    86
              size=wx.Size(0, 0),  
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    87
              style=wx.TR_HAS_BUTTONS|
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    88
                    wx.TR_SINGLE|
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    89
                    wx.SUNKEN_BORDER|
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    90
                    wx.TR_HIDE_ROOT|
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    91
                    wx.TR_LINES_AT_ROOT)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    92
        self.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnTreeItemSelected, self.Tree)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    93
        self.Tree.Bind(wx.EVT_CHAR, self.OnKeyDown)
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
    94
        # 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
    95
        # tree to start a drag'n drop
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    96
        if enable_drag:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    97
            self.Bind(wx.EVT_TREE_BEGIN_DRAG, self.OnTreeBeginDrag, self.Tree)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    98
        
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
    99
        # Add TextCtrl for function and function block informations
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   100
        self.Comment = wx.TextCtrl(splitter_window, size=wx.Size(0, 80), 
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   101
              style=wx.TE_READONLY|wx.TE_MULTILINE)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   102
        
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   103
        splitter_window.SplitHorizontally(self.Tree, self.Comment, -80)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   104
        
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   105
        self.SetSizer(main_sizer)
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   106
        
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   107
        # Reference to the project controller
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   108
        self.Controller = None
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   109
        
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   110
        # Variable storing functions and function blocks library to display
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   111
        self.BlockList = None
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   112
    
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   113
    def __del__(self):
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   114
        """
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   115
        Destructor
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   116
        """
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   117
        # Remove reference to project controller
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   118
        self.Controller = None
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   119
    
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   120
    def SetController(self, controller):
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   121
        """
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   122
        Set reference to project controller
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   123
        @param controller: Reference to project controller
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   124
        """
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   125
        self.Controller = controller
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   126
    
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   127
    def SetBlockList(self, blocklist):
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   128
        """
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   129
        Set function and function block library to display in TreeCtrl
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   130
        @param blocklist: Function and function block library
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   131
        """
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   132
        # Save functions and function blocks library
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   133
        self.BlockList = blocklist
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   134
        # Refresh TreeCtrl values
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   135
        self.RefreshTree()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   136
    
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   137
    def SetFocus(self):
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   138
        """
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   139
        Called to give focus to LibraryPanel
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   140
        Override wx.Window SetFocus method 
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   141
        """
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   142
        # Give focus to SearchCtrl
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   143
        self.SearchCtrl.SetFocus()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   144
    
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   145
    def ResetTree(self):
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   146
        """
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   147
        Reset LibraryPanel values displayed in controls
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   148
        """
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   149
        # Clear SearchCtrl, TreeCtrl and TextCtrl
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   150
        self.SearchCtrl.SetValue("")
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   151
        self.Tree.DeleteAllItems()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   152
        self.Comment.SetValue("")
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   153
    
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   154
    def RefreshTree(self):
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   155
        """
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   156
        Refresh LibraryPanel values displayed in controls
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   157
        """
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   158
        # Get function and function blocks library
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   159
        blocktypes = self.BlockList
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   160
        if blocktypes is None and self.Controller is not None:
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   161
            # Get library from project controller if not defined
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   162
            blocktypes = self.Controller.GetBlockTypes()
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   163
        
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   164
        # Refresh TreeCtrl values if a library is defined
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   165
        if blocktypes is not None:
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   166
            # List that will contain tree items to be deleted when TreeCtrl
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   167
            # will be refreshed
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   168
            items_to_delete = []
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   169
            
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   170
            # Get current selected item for selected it when values refreshed
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   171
            selected_item = self.Tree.GetSelection()
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   172
            selected_pydata = (self.Tree.GetPyData(selected_item)
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   173
                               if selected_item.IsOk() and
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   174
                                  selected_item != self.Tree.GetRootItem()
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   175
                               else None)
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   176
            # Don't save selected item if it is a category
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   177
            selected_infos = ((self.Tree.GetItemText(selected_item),
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   178
                               selected_pydata["inputs"])
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   179
                             if selected_pydata is not None and
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   180
                                selected_pydata["type"] == BLOCK
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   181
                             else (None, None))
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   182
            
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   183
            # Get TreeCtrl root item (hidden)
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   184
            root = self.Tree.GetRootItem()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   185
            if not root.IsOk():
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   186
                # Create root if not present
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   187
                root = self.Tree.AddRoot("")
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   188
            
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   189
            # Iterate over functions and function blocks library categories and
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   190
            # add a tree item to root item for each of them
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   191
            
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   192
            # Get first child under root item
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   193
            category_item, root_cookie = self.Tree.GetFirstChild(root)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   194
            for category in blocktypes:
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   195
                # Store category name in a local variable to prevent script
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   196
                # extracting translated strings for gettext to consider "name"
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   197
                # to be translated
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   198
                category_name = category["name"]
1230
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 already exists, set item label
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   201
                if category_item.IsOk():
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   202
                    self.Tree.SetItemText(category_item, _(category_name))
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   203
                
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   204
                # Tree item doesn't exist, add new one to root
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   205
                else:
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   206
                    category_item = self.Tree.AppendItem(root, _(category_name))
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   207
                    # On Windows, needs to get next child of root to have a
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   208
                    # reference to the newly added tree item
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   209
                    if wx.Platform != '__WXMSW__':
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   210
                        category_item, root_cookie = \
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   211
                            self.Tree.GetNextChild(root, root_cookie)
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   212
                
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   213
                # Set data associated to tree item (only save that item is a 
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   214
                # category)
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   215
                self.Tree.SetPyData(category_item, {"type" : CATEGORY})
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   216
                
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   217
                # Iterate over functions and function blocks defined in library
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   218
                # category add a tree item to category tree item for each of 
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   219
                # them
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   220
                
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   221
                # Get first child under category tree item
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   222
                blocktype_item, category_cookie = \
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   223
                    self.Tree.GetFirstChild(category_item)
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   224
                for blocktype in category["list"]:
1230
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 already exists, set item label
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   227
                    if blocktype_item.IsOk():
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   228
                        self.Tree.SetItemText(blocktype_item, blocktype["name"])
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   229
                    
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   230
                    # Tree item doesn't exist, add new one to category item
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   231
                    else:
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   232
                        blocktype_item = self.Tree.AppendItem(
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   233
                                            category_item, blocktype["name"])
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   234
                        # See comment when adding category
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   235
                        if wx.Platform != '__WXMSW__':
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   236
                            blocktype_item, category_cookie = \
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   237
                                self.Tree.GetNextChild(category_item, 
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   238
                                                       category_cookie)
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   239
                    
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   240
                    # Define data to associate to block tree item
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   241
                    comment = blocktype["comment"]
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   242
                    block_data = {"type" : BLOCK, 
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   243
                                  "block_type" : blocktype["type"], 
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   244
                                  "inputs" : tuple([type 
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   245
                                                    for name, type, modifier
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   246
                                                    in blocktype["inputs"]]), 
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   247
                                  "extension" : (len(blocktype["inputs"])
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   248
                                                 if blocktype["extensible"]
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   249
                                                 else None),
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   250
                                  "comment": _(comment) + 
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   251
                                             blocktype.get("usage", "")}
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   252
                    self.Tree.SetPyData(blocktype_item, block_data)
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   253
                    
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   254
                    # Select block tree item in tree if it corresponds to
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   255
                    # previously selected one
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   256
                    if selected_infos == (blocktype["name"], 
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   257
                                          blocktype["inputs"]):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   258
                        self.Tree.SelectItem(blocktype_item)
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   259
                        
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   260
                        # Update TextCtrl value
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   261
                        self.Comment.SetValue(block_data["comment"])
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   262
                
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   263
                    # Get next block tree item under category tree item
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   264
                    blocktype_item, category_cookie = \
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   265
                        self.Tree.GetNextChild(category_item, category_cookie)
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   266
                
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   267
                # Add every remaining tree item under category tree item after
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   268
                # updating all block items to the list of items to delete
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   269
                while blocktype_item.IsOk():
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   270
                    items_to_delete.append(blocktype_item)
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   271
                    blocktype_item, category_cookie = \
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   272
                        self.Tree.GetNextChild(category_item, category_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
                # Get next category tree item under root item
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   275
                category_item, root_cookie = \
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   276
                    self.Tree.GetNextChild(root, root_cookie)
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   277
            
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   278
            # Add every remaining tree item under root item after updating all 
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   279
            # category items to the list of items to delete
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   280
            while category_item.IsOk():
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   281
                items_to_delete.append(category_item)
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   282
                category_item, root_cookie = \
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   283
                    self.Tree.GetNextChild(root, root_cookie)
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   284
            
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   285
            # Remove all items in list of items to delete from TreeCtrl
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   286
            for item in items_to_delete:
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   287
                self.Tree.Delete(item)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   288
    
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   289
    def GetSelectedBlock(self):
1230
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 block informations
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   292
        @return: {"type": block_type_name, "inputs": [input_type,...]} or None
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   293
        if no block selected
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   294
        """
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   295
        # Get selected item associated data in tree
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   296
        selected_item = self.Tree.GetSelection()
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   297
        selected_pydata = (self.Tree.GetPyData(selected_item)
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   298
                           if selected_item.IsOk() and
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   299
                              selected_item != self.Tree.GetRootItem()
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   300
                           else None)
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   301
        
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   302
        # Return value is None if selected tree item is root or a category
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   303
        return ({"type": self.Tree.GetItemText(selected_item), 
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   304
                 "inputs": selected_pydata["inputs"]}
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   305
                if selected_pydata is not None and 
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   306
                   selected_pydata["type"] == BLOCK
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   307
                else None)
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   308
    
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   309
    def SelectTreeItem(self, name, inputs):
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   310
        """
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   311
        Select Tree item corresponding to block informations given 
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   312
        @param name: Block type name
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   313
        @param inputs: List of block inputs type [input_type,...]
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   314
        """
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   315
        # Find tree item corresponding to block informations
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   316
        item = self.FindTreeItem(self.Tree.GetRootItem(), name, inputs)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   317
        if item is not None and item.IsOk():
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   318
            # Select tree item found
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   319
            self.Tree.SelectItem(item)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   320
            self.Tree.EnsureVisible(item)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   321
    
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   322
    def FindTreeItem(self, item, name, inputs = None):
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   323
        """
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   324
        Find Tree item corresponding to block informations given
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   325
        Function is recursive
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   326
        @param item: Item to test
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   327
        @param name: Block type name
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   328
        @param inputs: List of block inputs type [input_type,...]
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   329
        """
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   330
        # Return immediately if item isn't valid
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   331
        if not item.IsOk():
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   332
            return None
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   333
        
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   334
        # Get data associated to item to test
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   335
        item_pydata = self.Tree.GetPyData(item)
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   336
        if item_pydata is not None and item_pydata["type"] == BLOCK:
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   337
            # Only test item corresponding to block
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   338
            
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   339
            # Test if block inputs type are the same than those given
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   340
            type_inputs = item_pydata.get("inputs", None)
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   341
            type_extension = item_pydata.get("extension", None)
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   342
            if inputs is not None and type_inputs is not None:
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   343
                same_inputs = reduce(
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   344
                    lambda x, y: x and y,
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   345
                    map(
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   346
                        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
   347
                        zip(type_inputs,
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   348
                            (inputs[:type_extension]
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   349
                             if type_extension is not None
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   350
                             else inputs))),
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   351
                    True)
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   352
            else:
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   353
                same_inputs = True
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   354
            
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   355
            # Return item if  block data corresponds to informations given
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   356
            if self.Tree.GetItemText(item) == name and same_inputs:
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   357
                return item
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   358
        
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   359
        # Test item children if item doesn't correspond
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   360
        child, child_cookie = self.Tree.GetFirstChild(item)
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   361
        while child.IsOk():
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   362
            result = self.FindTreeItem(child, name, inputs)
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   363
            if result:
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   364
                return result
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   365
            child, child_cookie = self.Tree.GetNextChild(item, child_cookie)
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   366
        
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   367
        return None
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   368
    
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   369
    def SearchInTree(self, value, mode="first"):
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   370
        """
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   371
        Search in Tree and select item that name contains string given
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   372
        @param value: String contained in block name to find
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   373
        @param mode: Search mode ('first', 'previous' or 'next')
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   374
        (default: 'first')
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   375
        @return: True if an item was found
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   376
        """
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   377
        # Return immediately if root isn't valid
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   378
        root = self.Tree.GetRootItem()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   379
        if not root.IsOk():
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   380
            return False
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   381
        
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   382
        # Set function to navigate in Tree item sibling according to search
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   383
        # mode defined 
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   384
        sibling_function = (self.Tree.GetPrevSibling
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   385
                            if mode == "previous"
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   386
                            else self.Tree.GetNextSibling)
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   387
        
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   388
        # Get current selected item (for next and previous mode)
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   389
        item = self.Tree.GetSelection()
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   390
        if not item.IsOk() or mode == "first":
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   391
            item, item_cookie = self.Tree.GetFirstChild(root)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   392
            selected = None
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   393
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   394
            selected = item
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
        # Navigate through tree items until one matching found or reach tree
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   397
        # starting or ending
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   398
        while item.IsOk():
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   399
            
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   400
            # Get item data to get item type
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   401
            item_pydata = self.Tree.GetPyData(item)
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   402
            
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   403
            # Item is a block category
1501
d917c209529d fix Traceback if search icon on library panel is clicked, when no
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1235
diff changeset
   404
            if (item == root) or item_pydata["type"] == CATEGORY:
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   405
                
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   406
                # Get category first or last child according to search mode 
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   407
                # defined
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   408
                child = (self.Tree.GetLastChild(item)
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   409
                         if mode == "previous"
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   410
                         else self.Tree.GetFirstChild(item)[0])
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   411
                
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   412
                # If category has no child, go to sibling category
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   413
                item = (child if child.IsOk() else sibling_function(item))
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   414
            
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   415
            # Item is a block
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   416
            else:
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   417
                
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   418
                # Extract item block name
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   419
                name = self.Tree.GetItemText(item)
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   420
                # 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
   421
                if name.upper().find(value.upper()) != -1 and item != selected:
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   422
                    # Select block and collapse all categories other than block
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   423
                    # category
1235
1a30c70fa025 Fixed bug when searching in LibraryPanel on Windows
Laurent Bessard
parents: 1230
diff changeset
   424
                    child, child_cookie = self.Tree.GetFirstChild(root)
1a30c70fa025 Fixed bug when searching in LibraryPanel on Windows
Laurent Bessard
parents: 1230
diff changeset
   425
                    while child.IsOk():
1a30c70fa025 Fixed bug when searching in LibraryPanel on Windows
Laurent Bessard
parents: 1230
diff changeset
   426
                        self.Tree.CollapseAllChildren(child)
1a30c70fa025 Fixed bug when searching in LibraryPanel on Windows
Laurent Bessard
parents: 1230
diff changeset
   427
                        child, child_cookie = self.Tree.GetNextChild(root, child_cookie)
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   428
                    self.Tree.SelectItem(item)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   429
                    self.Tree.EnsureVisible(item)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   430
                    return True
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   431
                
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   432
                # Go to next item sibling if block not found
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   433
                next = sibling_function(item)
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   434
                
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   435
                # If category has no other child, go to next category sibling
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   436
                item = (next
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   437
                        if next.IsOk()
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   438
                        else sibling_function(self.Tree.GetItemParent(item)))
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   439
        
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   440
        return False
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   441
    
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   442
    def OnSearchCtrlChanged(self, event):
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   443
        """
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   444
        Called when SearchCtrl text control value changed
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   445
        @param event: TextCtrl change event
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   446
        """
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   447
        # Search for block containing SearchCtrl value in 'first' mode
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   448
        self.SearchInTree(self.SearchCtrl.GetValue())
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   449
        event.Skip()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   450
    
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   451
    def OnSearchButtonClick(self, event):
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   452
        """
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   453
        Called when SearchCtrl search button was clicked
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   454
        @param event: Button clicked event
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   455
        """
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   456
        # Search for block containing SearchCtrl value in 'next' mode
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   457
        self.SearchInTree(self.SearchCtrl.GetValue(), "next")
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   458
        event.Skip()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   459
    
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   460
    def OnTreeItemSelected(self, event):
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   461
        """
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   462
        Called when tree item is selected
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   463
        @param event: wx.TreeEvent
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   464
        """
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   465
        # Update TextCtrl value with block selected usage
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   466
        item_pydata = self.Tree.GetPyData(event.GetItem())
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   467
        self.Comment.SetValue(
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   468
            item_pydata["comment"]
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   469
            if item_pydata is not None and item_pydata["type"] == BLOCK
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   470
            else "")
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   471
        
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   472
        # Call extra function defined when tree item is selected
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   473
        if getattr(self, "_OnTreeItemSelected", None) is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   474
            self._OnTreeItemSelected(event)
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   475
        
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   476
        event.Skip()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   477
    
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   478
    def OnTreeBeginDrag(self, event):
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   479
        """
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   480
        Called when a drag is started in tree
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   481
        @param event: wx.TreeEvent
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   482
        """
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   483
        selected_item = event.GetItem()
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   484
        item_pydata = self.Tree.GetPyData(selected_item)
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   485
        
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   486
        # Item dragged is a block
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   487
        if item_pydata is not None and item_pydata["type"] == BLOCK:
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   488
            # Start a drag'n drop
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   489
            data = wx.TextDataObject(str(
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   490
                (self.Tree.GetItemText(selected_item), 
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   491
                 item_pydata["block_type"], 
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   492
                 "", 
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   493
                 item_pydata["inputs"])))
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   494
            dragSource = wx.DropSource(self.Tree)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   495
            dragSource.SetData(data)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   496
            dragSource.DoDragDrop()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   497
    
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   498
    def OnKeyDown(self, event):
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   499
        """
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   500
        Called when key is pressed in SearchCtrl text control
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   501
        @param event: wx.KeyEvent
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   502
        """
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   503
        # Get event keycode and value in SearchCtrl
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   504
        keycode = event.GetKeyCode()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   505
        search_value = self.SearchCtrl.GetValue()
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   506
        
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   507
        # Up key was pressed and SearchCtrl isn't empty, search for block in
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   508
        # 'previous' mode 
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   509
        if keycode == wx.WXK_UP and search_value != "":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   510
            self.SearchInTree(search_value, "previous")
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   511
        
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   512
        # Down key was pressed and SearchCtrl isn't empty, search for block in
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   513
        # 'next' mode 
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   514
        elif keycode == wx.WXK_DOWN and search_value != "":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   515
            self.SearchInTree(search_value, "next")
1230
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   516
        
5a4c5724788e Fixed and rewrite LibraryPanel
Laurent Bessard
parents: 1223
diff changeset
   517
        # Handle key normally
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   518
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   519
            event.Skip()