controls/LocationCellEditor.py
author Laurent Bessard
Tue, 17 Jul 2012 21:23:21 +0200
changeset 727 46ae58e6469f
parent 723 5df934c273e1
permissions -rw-r--r--
Fixing bug with filesystem encoding on Windows
586
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
     1
#!/usr/bin/env python
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
     3
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
     4
#This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
     5
#based on the plcopen standard. 
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
     6
#
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
     7
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
     8
#
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
     9
#See COPYING file for copyrights details.
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
    10
#
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
    11
#This library is free software; you can redistribute it and/or
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
    12
#modify it under the terms of the GNU General Public
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
    13
#License as published by the Free Software Foundation; either
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
    14
#version 2.1 of the License, or (at your option) any later version.
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
    15
#
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
    16
#This library is distributed in the hope that it will be useful,
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
    17
#but WITHOUT ANY WARRANTY; without even the implied warranty of
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
    18
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
    19
#General Public License for more details.
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
    20
#
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
    21
#You should have received a copy of the GNU General Public
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
    22
#License along with this library; if not, write to the Free Software
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
    23
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
    24
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
    25
import wx
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
    26
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
    27
from dialogs.BrowseLocationsDialog import BrowseLocationsDialog
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
    28
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
    29
class LocationCellControl(wx.PyControl):
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
    30
    
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
    31
    '''
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
    32
    Custom cell editor control with a text box and a button that launches
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
    33
    the BrowseLocationsDialog.
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
    34
    '''
657
b286a16162fc Fix issue losing focus on Windows when closing custom grid cell editors
laurent
parents: 655
diff changeset
    35
    def __init__(self, parent):
714
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 657
diff changeset
    36
        wx.Control.__init__(self, parent)
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 657
diff changeset
    37
        
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 657
diff changeset
    38
        main_sizer = wx.FlexGridSizer(cols=2, hgap=0, rows=1, vgap=0)
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 657
diff changeset
    39
        main_sizer.AddGrowableCol(0)
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 657
diff changeset
    40
        main_sizer.AddGrowableRow(0)
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 657
diff changeset
    41
        
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 657
diff changeset
    42
        # create location text control
723
5df934c273e1 Fix bug browse button in special cell editors on Windows
Laurent Bessard
parents: 714
diff changeset
    43
        self.Location = wx.TextCtrl(self, size=wx.Size(0, -1), 
5df934c273e1 Fix bug browse button in special cell editors on Windows
Laurent Bessard
parents: 714
diff changeset
    44
              style=wx.TE_PROCESS_ENTER)
714
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 657
diff changeset
    45
        self.Location.Bind(wx.EVT_KEY_DOWN, self.OnLocationChar)
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 657
diff changeset
    46
        main_sizer.AddWindow(self.Location, flag=wx.GROW)
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 657
diff changeset
    47
        
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 657
diff changeset
    48
        # create browse button
723
5df934c273e1 Fix bug browse button in special cell editors on Windows
Laurent Bessard
parents: 714
diff changeset
    49
        self.BrowseButton = wx.Button(self, label='...', size=wx.Size(30, -1))
714
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 657
diff changeset
    50
        self.BrowseButton.Bind(wx.EVT_BUTTON, self.OnBrowseButtonClick)
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 657
diff changeset
    51
        main_sizer.AddWindow(self.BrowseButton, flag=wx.GROW)
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 657
diff changeset
    52
        
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 657
diff changeset
    53
        self.Bind(wx.EVT_SIZE, self.OnSize)
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 657
diff changeset
    54
        
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 657
diff changeset
    55
        self.SetSizer(main_sizer)
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 657
diff changeset
    56
657
b286a16162fc Fix issue losing focus on Windows when closing custom grid cell editors
laurent
parents: 655
diff changeset
    57
        self.Locations = None
586
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
    58
        self.VarType = None
655
435e2d8ee580 Fixing issues regarding control navigation in EditorPanel and CustomGrid
laurent
parents: 615
diff changeset
    59
        self.Default = False
586
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
    60
657
b286a16162fc Fix issue losing focus on Windows when closing custom grid cell editors
laurent
parents: 655
diff changeset
    61
    def SetLocations(self, locations):
b286a16162fc Fix issue losing focus on Windows when closing custom grid cell editors
laurent
parents: 655
diff changeset
    62
        self.Locations = locations
b286a16162fc Fix issue losing focus on Windows when closing custom grid cell editors
laurent
parents: 655
diff changeset
    63
586
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
    64
    def SetVarType(self, vartype):
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
    65
        self.VarType = vartype
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
    66
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
    67
    def SetValue(self, value):
655
435e2d8ee580 Fixing issues regarding control navigation in EditorPanel and CustomGrid
laurent
parents: 615
diff changeset
    68
        self.Default = value
586
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
    69
        self.Location.SetValue(value)
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
    70
    
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
    71
    def GetValue(self):
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
    72
        return self.Location.GetValue()
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
    73
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
    74
    def OnSize(self, event):
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
    75
        self.Layout()
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
    76
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
    77
    def OnBrowseButtonClick(self, event):
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
    78
        # pop up the location browser dialog
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
    79
        dialog = BrowseLocationsDialog(self, self.VarType, self.Locations)
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
    80
        if dialog.ShowModal() == wx.ID_OK:
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
    81
            infos = dialog.GetValues()
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
    82
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
    83
            # set the location
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
    84
            self.Location.SetValue(infos["location"])
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
    85
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
    86
        dialog.Destroy()
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
    87
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
    88
        self.Location.SetFocus()
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
    89
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
    90
    def OnLocationChar(self, event):
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
    91
        keycode = event.GetKeyCode()
655
435e2d8ee580 Fixing issues regarding control navigation in EditorPanel and CustomGrid
laurent
parents: 615
diff changeset
    92
        if keycode in [wx.WXK_RETURN, wx.WXK_TAB]:
586
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
    93
            self.Parent.Parent.ProcessEvent(event)
655
435e2d8ee580 Fixing issues regarding control navigation in EditorPanel and CustomGrid
laurent
parents: 615
diff changeset
    94
        elif keycode == wx.WXK_ESCAPE:
435e2d8ee580 Fixing issues regarding control navigation in EditorPanel and CustomGrid
laurent
parents: 615
diff changeset
    95
            self.Location.SetValue(self.Default)
435e2d8ee580 Fixing issues regarding control navigation in EditorPanel and CustomGrid
laurent
parents: 615
diff changeset
    96
            self.Parent.Parent.CloseEditControl()
586
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
    97
        else:
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
    98
            event.Skip()
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
    99
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
   100
    def SetInsertionPoint(self, i):
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
   101
        self.Location.SetInsertionPoint(i)
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
   102
    
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
   103
    def SetFocus(self):
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
   104
        self.Location.SetFocus()
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
   105
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
   106
class LocationCellEditor(wx.grid.PyGridCellEditor):
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
   107
    '''
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
   108
    Grid cell editor that uses LocationCellControl to display a browse button.
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
   109
    '''
714
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 657
diff changeset
   110
    def __init__(self, table, controller):
586
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
   111
        wx.grid.PyGridCellEditor.__init__(self)
714
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 657
diff changeset
   112
        
586
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
   113
        self.Table = table
714
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 657
diff changeset
   114
        self.Controller = controller
586
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
   115
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
   116
    def __del__(self):
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
   117
        self.CellControl = None
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
   118
    
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
   119
    def Create(self, parent, id, evt_handler):
657
b286a16162fc Fix issue losing focus on Windows when closing custom grid cell editors
laurent
parents: 655
diff changeset
   120
        self.CellControl = LocationCellControl(parent)
586
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
   121
        self.SetControl(self.CellControl)
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
   122
        if evt_handler:
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
   123
            self.CellControl.PushEventHandler(evt_handler)
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
   124
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
   125
    def BeginEdit(self, row, col, grid):
657
b286a16162fc Fix issue losing focus on Windows when closing custom grid cell editors
laurent
parents: 655
diff changeset
   126
        self.CellControl.Enable()
714
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 657
diff changeset
   127
        self.CellControl.SetLocations(self.Controller.GetVariableLocationTree())
586
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
   128
        self.CellControl.SetValue(self.Table.GetValueByName(row, 'Location'))
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
   129
        if isinstance(self.CellControl, LocationCellControl):
714
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 657
diff changeset
   130
            self.CellControl.SetVarType(self.Controller.GetBaseType(self.Table.GetValueByName(row, 'Type')))
586
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
   131
        self.CellControl.SetFocus()
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
   132
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
   133
    def EndEdit(self, row, col, grid):
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
   134
        loc = self.CellControl.GetValue()
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
   135
        old_loc = self.Table.GetValueByName(row, 'Location')
657
b286a16162fc Fix issue losing focus on Windows when closing custom grid cell editors
laurent
parents: 655
diff changeset
   136
        changed = loc != old_loc
b286a16162fc Fix issue losing focus on Windows when closing custom grid cell editors
laurent
parents: 655
diff changeset
   137
        if changed:
586
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
   138
            self.Table.SetValueByName(row, 'Location', loc)
657
b286a16162fc Fix issue losing focus on Windows when closing custom grid cell editors
laurent
parents: 655
diff changeset
   139
        self.CellControl.Disable()
b286a16162fc Fix issue losing focus on Windows when closing custom grid cell editors
laurent
parents: 655
diff changeset
   140
        return changed
655
435e2d8ee580 Fixing issues regarding control navigation in EditorPanel and CustomGrid
laurent
parents: 615
diff changeset
   141
    
586
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
   142
    def SetSize(self, rect):
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
   143
        self.CellControl.SetDimensions(rect.x + 1, rect.y,
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
   144
                                        rect.width, rect.height,
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
   145
                                        wx.SIZE_ALLOW_MINUS_ONE)
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
   146
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
   147
    def Clone(self):
714
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 657
diff changeset
   148
        return LocationCellEditor(self.Table, self.Controller)
586
9aa96a36cf33 Moving variable panel from bottom notebook panel to POU editor panel
laurent
parents:
diff changeset
   149