DatePickerEditor control

4 views
Skip to first unread message

dklon

unread,
Oct 21, 2009, 4:14:50 PM10/21/09
to wxPython-dev
Hello!

I've been developing with wxPython for about a year or so now, and
I've gained enough of a grip on the docs and such to develop some
custom controls that I've needed.

Recently I came up with a DatePickerEditor class which is a
GridCellEditor. If anyone is interested in using it, I posted the
source for it on my blog:

http://www.moviepartners.com/blog/2009/10/21/datepickereditor-a-useful-control-for-wxpython/

If it's useful enough, should I contribute a demo module?

dklon

unread,
Oct 25, 2009, 7:37:05 PM10/25/09
to wxPython-dev
Sorry to reply to my own post here, but I found some bugs and fixed
them in the editor.

Here's the new source:

# Custom Grid Cell Editor that performs date picking

import string
import wx
import wx.grid as Grid

class DatePickerEditor(Grid.PyGridCellEditor):
"""
This GridCellEditor allows you to date pick from a calendar inside
the
cell of a grid.
"""
def __init__(self):
# Constructor
Grid.PyGridCellEditor.__init__(self)
# end __init__

def Create(self, parent, id, evtHandler):
"""
Called to create the control, which must derive from
wx.Control.
"""
self._picker = wx.DatePickerCtrl(parent, id,
style=wx.DP_DROPDOWN)
self.startingDate = None
self.SetControl(self._picker)

#if evtHandler:
# self._picker.PushEventHandler(evtHandler)
# end Create

def SetSize(self, rect):
"""
Called to position/size the edit control within the cell
rectangle.
If you don't fill the cell (the rect) then be sure to override
PaintBackground and do something meaningful there.
"""
self._picker.SetDimensions(rect.x, rect.y, rect.width+2,
rect.height+2,
wx.SIZE_ALLOW_MINUS_ONE)
# end SetSize

def Show(self, show, attr):
"""
Show or hide the edit control. You can use the attr (if not
None)
to set colours or fonts for the control.
"""
super(DatePickerEditor, self).Show(show, attr)
# end Show

def PaintBackground(self, rect, attr):
"""
Draws the part of the cell not occupied by the edit control.
The
base class version just fills it with background colour from
the
attribute. In this class the edit control fills the whole
cell so
don't do anything at all in order to reduce flicker.
"""
pass
# end PaintBackground

def BeginEdit(self, row, col, grid):
"""
Fetch the value from the table and prepare the edit control
to begin editing. Set the focus to the edit control.
*Must Override*
"""
self.startValue = str(grid.GetTable().GetValue(row, col)).strip
()

if not self.startValue == '':
# Split the string up and then insert it in there
tmpDate = wx.DateTime()
tmpDate.ParseDate(self.startValue)
self._picker.SetValue(tmpDate)
self.startingDate = tmpDate

self._picker.SetFocus()

# end BeginEdit

def EndEdit(self, row, col, grid):
"""
Complete the editing of the current cell. Returns True if the
value
has changed. If necessary, the control may be destroyed.
*Must Override*
"""
changed = False

val = self._picker.GetValue().GetDateOnly()

if val.Format("%m/%d/%Y") != self.startValue:
changed = True
print "Setting Cell Value to %s" % val.Format("%m/%d/%Y")
grid.SetCellValue(row, col, str(val.Format("%m/%d/%Y"))) #
update the table
self.startValue = val.Format("%m/%d/%Y")

return changed
# end EndEdit

def Reset(self):
"""
Reset the value in the control back to its starting value.
*Must Override*
"""
if self.startingDate is not None:
self._picker.SetValue(self.startingDate)
# end Reset

def IsAcceptedKey(self, evt):
"""
Return True to allow the given key to start editing: the base
class
version only checks that the event has no modifiers. F2 is
special
and will always start the editor.
"""

# or do it ourselves
return (not (evt.ControlDown() or evt.AltDown()) and
evt.GetKeyCode() != wx.WXK_SHIFT)
# end IsAcceptedKey

def StartingKey(self, evt):
"""
If the editor is enabled by pressing keys on the grid, this
will be
called to let the editor do something about that first key if
desired.
"""
key = evt.GetKeyCode()
ch = None
if key in [ wx.WXK_NUMPAD0, wx.WXK_NUMPAD1, wx.WXK_NUMPAD2,
wx.WXK_NUMPAD3,
wx.WXK_NUMPAD4, wx.WXK_NUMPAD5, wx.WXK_NUMPAD6,
wx.WXK_NUMPAD7,
wx.WXK_NUMPAD8, wx.WXK_NUMPAD9
]:

ch = ch = chr(ord('0') + key - wx.WXK_NUMPAD0)

elif key < 256 and key >= 0 and chr(key) in string.printable:
ch = chr(key)

evt.Skip()
# end StartingKey

def StartingClick(self):
"""
If the editor is enabled by clicking on the cell, this method
will be
called to allow the editor to simulate the click on the
control if
needed.
"""

pass
# end StartingClick

def Destroy(self):
"""final cleanup"""
super(DatePickerEditor, self).Destroy()
# end Destroy

def Clone(self):
"""
Create a new object which is the copy of this one
*Must Override*
"""
return DatePickerEditor()
# end Clone



On Oct 21, 3:14 pm, dklon <dmwat...@gmail.com> wrote:
> Hello!
>
> I've been developing with wxPython for about a year or so now, and
> I've gained enough of a grip on the docs and such to develop some
> custom controls that I've needed.
>
> Recently I came up with a DatePickerEditor class which is a
> GridCellEditor.  If anyone is interested in using it, I posted the
> source for it on my blog:
>
> http://www.moviepartners.com/blog/2009/10/21/datepickereditor-a-usefu...
Reply all
Reply to author
Forward
0 new messages