controls/DurationCellEditor.py
changeset 586 9aa96a36cf33
child 615 8baeb9dff775
equal deleted inserted replaced
585:bd8c7a033b17 586:9aa96a36cf33
       
     1 #!/usr/bin/env python
       
     2 # -*- coding: utf-8 -*-
       
     3 
       
     4 #This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor
       
     5 #based on the plcopen standard. 
       
     6 #
       
     7 #Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
       
     8 #
       
     9 #See COPYING file for copyrights details.
       
    10 #
       
    11 #This library is free software; you can redistribute it and/or
       
    12 #modify it under the terms of the GNU General Public
       
    13 #License as published by the Free Software Foundation; either
       
    14 #version 2.1 of the License, or (at your option) any later version.
       
    15 #
       
    16 #This library is distributed in the hope that it will be useful,
       
    17 #but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    18 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    19 #General Public License for more details.
       
    20 #
       
    21 #You should have received a copy of the GNU General Public
       
    22 #License along with this library; if not, write to the Free Software
       
    23 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
       
    24 
       
    25 import wx
       
    26 
       
    27 from dialogs.DurationEditorDialog import DurationEditorDialog
       
    28 
       
    29 class DurationCellControl(wx.PyControl):
       
    30     
       
    31     def _init_coll_MainSizer_Items(self, parent):
       
    32         parent.AddWindow(self.Duration, 0, border=0, flag=wx.GROW)
       
    33         parent.AddWindow(self.EditButton, 0, border=0, flag=wx.GROW)
       
    34         
       
    35     def _init_coll_MainSizer_Growables(self, parent):
       
    36         parent.AddGrowableCol(0)
       
    37         parent.AddGrowableRow(0)
       
    38     
       
    39     def _init_sizers(self):
       
    40         self.MainSizer = wx.FlexGridSizer(cols=2, hgap=0, rows=1, vgap=0)
       
    41         
       
    42         self._init_coll_MainSizer_Items(self.MainSizer)
       
    43         self._init_coll_MainSizer_Growables(self.MainSizer)
       
    44         
       
    45         self.SetSizer(self.MainSizer)
       
    46     
       
    47     def _init_ctrls(self, prnt):
       
    48         wx.Control.__init__(self, id=-1, 
       
    49               name='DurationCellControl', parent=prnt,  
       
    50               size=wx.DefaultSize, style=0)
       
    51         
       
    52         # create location text control
       
    53         self.Duration = wx.TextCtrl(id=-1, name='Duration', parent=self,
       
    54               pos=wx.Point(0, 0), size=wx.Size(0, 0), style=wx.TE_PROCESS_ENTER)
       
    55         self.Duration.Bind(wx.EVT_KEY_DOWN, self.OnDurationChar)
       
    56         
       
    57         # create browse button
       
    58         self.EditButton = wx.Button(id=-1, label='...', 
       
    59               name='EditButton', parent=self, pos=wx.Point(0, 0), 
       
    60               size=wx.Size(30, 0), style=0)
       
    61         self.EditButton.Bind(wx.EVT_BUTTON, self.OnEditButtonClick)
       
    62 
       
    63         self.Bind(wx.EVT_SIZE, self.OnSize)
       
    64         
       
    65         self._init_sizers()
       
    66 
       
    67     '''
       
    68     Custom cell editor control with a text box and a button that launches
       
    69     the DurationEditorDialog.
       
    70     '''
       
    71     def __init__(self, parent):
       
    72         self._init_ctrls(parent)
       
    73         
       
    74     def SetValue(self, value):
       
    75         self.Duration.SetValue(value)
       
    76     
       
    77     def GetValue(self):
       
    78         return self.Duration.GetValue()
       
    79 
       
    80     def OnSize(self, event):
       
    81         self.Layout()
       
    82 
       
    83     def OnEditButtonClick(self, event):
       
    84         # pop up the Duration Editor dialog
       
    85         dialog = DurationEditorDialog(self)
       
    86         dialog.SetDuration(self.Duration.GetValue())
       
    87         if dialog.ShowModal() == wx.ID_OK:
       
    88             # set the duration
       
    89             self.Duration.SetValue(dialog.GetDuration())
       
    90 
       
    91         dialog.Destroy()
       
    92 
       
    93         self.Duration.SetFocus()
       
    94 
       
    95     def OnDurationChar(self, event):
       
    96         keycode = event.GetKeyCode()
       
    97         if keycode == wx.WXK_RETURN or keycode == wx.WXK_TAB:
       
    98             self.Parent.Parent.ProcessEvent(event)
       
    99             self.Parent.Parent.SetFocus()
       
   100         else:
       
   101             event.Skip()
       
   102 
       
   103     def SetInsertionPoint(self, i):
       
   104         self.Duration.SetInsertionPoint(i)
       
   105     
       
   106     def SetFocus(self):
       
   107         self.Duration.SetFocus()
       
   108 
       
   109 class DurationCellEditor(wx.grid.PyGridCellEditor):
       
   110     '''
       
   111     Grid cell editor that uses DurationCellControl to display an edit button.
       
   112     '''
       
   113     def __init__(self, table):
       
   114         wx.grid.PyGridCellEditor.__init__(self)
       
   115         self.Table = table
       
   116     
       
   117     def __del__(self):
       
   118         self.CellControl = None
       
   119     
       
   120     def Create(self, parent, id, evt_handler):
       
   121         self.CellControl = DurationCellControl(parent)
       
   122         self.SetControl(self.CellControl)
       
   123         if evt_handler:
       
   124             self.CellControl.PushEventHandler(evt_handler)
       
   125 
       
   126     def BeginEdit(self, row, col, grid):
       
   127         self.CellControl.SetValue(self.Table.GetValueByName(row, 'Interval'))
       
   128         self.CellControl.SetFocus()
       
   129 
       
   130     def EndEdit(self, row, col, grid):
       
   131         duration = self.CellControl.GetValue()
       
   132         old_duration = self.Table.GetValueByName(row, 'Interval')
       
   133         if duration != old_duration:
       
   134             self.Table.SetValueByName(row, 'Interval', duration)
       
   135             return True
       
   136         return False
       
   137 
       
   138     def SetSize(self, rect):
       
   139         self.CellControl.SetDimensions(rect.x + 1, rect.y,
       
   140                                         rect.width, rect.height,
       
   141                                         wx.SIZE_ALLOW_MINUS_ONE)
       
   142 
       
   143     def Clone(self):
       
   144         return DurationCellEditor(self.Table)