controls/CustomGrid.py
author laurent
Tue, 27 Mar 2012 23:59:35 +0200
changeset 661 7891872e6fd7
parent 655 435e2d8ee580
child 662 3c4e0d4f7a61
permissions -rw-r--r--
Fix bug in debugger when transfer without having build before and and opening debug view before running PLC
#!/usr/bin/env python
# -*- coding: utf-8 -*-

#This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor
#based on the plcopen standard. 
#
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
#
#See COPYING file for copyrights details.
#
#This library is free software; you can redistribute it and/or
#modify it under the terms of the GNU General Public
#License as published by the Free Software Foundation; either
#version 2.1 of the License, or (at your option) any later version.
#
#This library is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#General Public License for more details.
#
#You should have received a copy of the GNU General Public
#License along with this library; if not, write to the Free Software
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

import wx
import wx.grid

class CustomGrid(wx.grid.Grid):
    
    if wx.VERSION < (2, 6, 0):
        def Bind(self, event, function, id = None):
            if id is not None:
                event(self, id, function)
            else:
                event(self, function)
    
    def __init__(self, *args, **kwargs):
        wx.grid.Grid.__init__(self, *args, **kwargs)
        
        self.Editable = True
        
        self.AddButton = None
        self.DeleteButton = None
        self.UpButton = None
        self.DownButton = None
        
        self.SetFont(wx.Font(12, 77, wx.NORMAL, wx.NORMAL, False, 'Sans'))
        self.SetLabelFont(wx.Font(10, 77, wx.NORMAL, wx.NORMAL, False, 'Sans'))
        self.SetSelectionBackground(wx.WHITE)
        self.SetSelectionForeground(wx.BLACK)
        self.DisableDragRowSize()
        
        if wx.VERSION >= (2, 6, 0):
            self.Bind(wx.grid.EVT_GRID_SELECT_CELL, self.OnSelectCell)
            self.Bind(wx.grid.EVT_GRID_EDITOR_HIDDEN, self.OnEditorHidden)
        else:
            wx.grid.EVT_GRID_SELECT_CELL(self, self.OnSelectCell)
        self.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
    
    def SetDefaultValue(self, default_value):
        self.DefaultValue = default_value
    
    def SetEditable(self, editable=True):
        self.Editable = editable
        self.RefreshButtons()
        
    def SetButtons(self, buttons):
        for name in ["Add", "Delete", "Up", "Down"]:
            button = buttons.get(name, None)
            setattr(self, "%sButton" % name, button)
            if button is not None:
                button.Bind(wx.EVT_BUTTON, getattr(self, "On%sButton" % name))
    
    def RefreshButtons(self):
        rows = self.Table.GetNumberRows()
        row = self.GetGridCursorRow()
        if self.AddButton is not None:
            self.AddButton.Enable(self.Editable)
        if self.DeleteButton is not None:
            self.DeleteButton.Enable(self.Editable and rows > 0)
        if self.UpButton is not None:
            self.UpButton.Enable(self.Editable and row > 0)
        if self.DownButton is not None:
            self.DownButton.Enable(self.Editable and 0 <= row < rows - 1)
    
    def CloseEditControl(self):
        self.SetGridCursor(self.GetGridCursorRow(), self.GetGridCursorCol())

    def AddRow(self):
        self.CloseEditControl()
        new_row = self.GetGridCursorRow() + 1
        col = max(self.GetGridCursorCol(), 0)
        if getattr(self, "_AddRow", None) is not None:
            new_row = self._AddRow(new_row)
        else:
            self.Table.InsertRow(new_row, self.DefaultValue.copy())
            self.Table.ResetView(self)
        self.SetGridCursor(new_row, col)
        self.MakeCellVisible(new_row, col)
        self.RefreshButtons()

    def DeleteRow(self):
        self.CloseEditControl()
        row = self.GetGridCursorRow()
        if row >= 0:
            col = self.GetGridCursorCol()
            if getattr(self, "_DeleteRow", None) is not None:
                self._DeleteRow(row)
            else:
                self.Table.RemoveRow(row)
                self.Table.ResetView(self)
            new_row = min(row, self.Table.GetNumberRows() - 1)
            self.SetGridCursor(new_row, col)
            self.MakeCellVisible(new_row, col)
            self.RefreshButtons()

    def MoveRow(self, row, move):
        self.CloseEditControl()
        col = self.GetGridCursorCol()
        if getattr(self, "_MoveRow", None) is not None:
            new_row = self._MoveRow(row, move)
        else:
            new_row = self.Table.MoveRow(row, move)
            if new_row != row:
                self.Table.ResetView(self)
        if new_row != row:
            self.SetGridCursor(new_row, col)
            self.MakeCellVisible(new_row, col)
            self.RefreshButtons()

    def OnAddButton(self, event):
        self.AddRow()
        self.SetFocus()
        event.Skip()
        
    def OnDeleteButton(self, event):
        self.DeleteRow()
        self.SetFocus()
        event.Skip()
        
    def OnUpButton(self, event):
        self.MoveRow(self.GetGridCursorRow(), -1)
        self.SetFocus()
        event.Skip()
        
    def OnDownButton(self, event):
        self.MoveRow(self.GetGridCursorRow(), 1)
        self.SetFocus()
        event.Skip()

    def OnSelectCell(self, event):
        wx.CallAfter(self.RefreshButtons)
        event.Skip()

    def OnEditorHidden(self, event):
        wx.CallAfter(self.SetFocus)
        event.Skip()

    def OnKeyDown(self, event):
        key_handled = False
        keycode = event.GetKeyCode()
        if keycode == wx.WXK_TAB:
            row = self.GetGridCursorRow()
            col = self.GetGridCursorCol()
            if event.ShiftDown():
                if row < 0 or col == 0:
                    self.Navigate(wx.NavigationKeyEvent.IsBackward)
                    key_handled = True
            elif row < 0 or col == self.Table.GetNumberCols() - 1:
                self.Navigate(wx.NavigationKeyEvent.IsForward)
                key_handled = True
        elif keycode in (wx.WXK_ADD, wx.WXK_NUMPAD_ADD) and self.Editable:
            self.AddRow()
            key_handled = True
        elif keycode in (wx.WXK_DELETE, wx.WXK_NUMPAD_DELETE) and self.Editable:
            self.DeleteRow()
            key_handled = True
        elif keycode == wx.WXK_UP and event.ShiftDown() and self.Editable:
            self.MoveRow(self.GetGridCursorRow(), -1)
            key_handled = True
        elif keycode == wx.WXK_DOWN and event.ShiftDown() and self.Editable:
            self.MoveRow(self.GetGridCursorRow(), 1)
            key_handled = True
        if not key_handled:
            event.Skip()