Hi all,
Now that I am working on my first python application, I find a lot of
things that are very common for every application, and I wonder if
effort couldn't be put in creating controls or wrappers for existing
controls that are data-aware?
How often do you need to;
- Restore the data from a db / file
- List it in a wx.ListCtrl / wx.ListBox / wx.Choice etc
- Edit the data
- Store it back
Those actions are so common, yet for every GUI you are stuck writing
the data sync methods to get the data inside the GUI controls and
back, not to mention every data sort routine needs to be
re-implemented, or if you want your columns to be customized, etc.
I am busy with wxPython for about a month now, and I do know it is a
language that lends itself perfectly for controls and classes that are
more elegant in use towards data management.
For example, a kind of provider class could get special accessor
methods (like a facade or an interface) that can be used with (for
example) a wx.data.listctrl (choice, combo, grid, listbox, etc). Which
is a wrapper for wx.ListCtrl but much more intelligent as most actions
can be 'automated', like:
- Create the wx.ListCtrl with a number of columns
- Fill the control with class data
- Implement native sorting on column headers
- Implement view manipulation like column ordering / removing / adding
of new columns
So for example, if one has a data class like:
# trivial person object
class Person(object):
def __int__(self):
self.mName = ''
self.mAge = 30
And a provider class like;
class PersonDataProvider(wx.data.AbstractDataProvider):
def __init__(self):
self.__mColumnCount = 2 # e.g name, age
# return amount of columns in the data view
def getColumnCount(self):
return self.__mColumnCount
# returns column name for every column
def getColumnName(self, colidx):
if colidx < self.__mColumnCount:
if colidx = 0: return 'Name'
elif colidx = 1: return 'Age'
# gets the amount of records
def getRecordCount(self):
return 1
# gets the value per cell
def getCellValue(self, recordidx, columnidx):
# get object in list
if columnidx == 0: return mPersonList[recordidx].mName
elif columnidx == 1: return mPersonList[recordidx].mAge
The above provider suggestion is not very 'pythonic' I still lack all
the knowledge to make it look slick, so it might look C+'ish .. anyway
the basic idea would be giving a wx.data.ListCtrl (as example for the
name) a *provider* class instance, which would act as an abstract
interface to initialize the columns inside the internal wxListCtrl,
let it fill itself by asking the records.
For example;
# instantiate a provider class, give it a pointer to my persons list
p = PersonDataProvider(self.__mMyPersons)
# create a list ctrl
lc = wx.data.listctrl(self, ...)
lc.provider = p
# show same options in a choice somewhere else
ch = wx.data.choice(self, ..)
ch.provider = p
And by passing the provider to the data aware list ctrl, the columns
are automatically created, records are filled in the list, and if
needed column ordering and sorting is restored from a previous
setting. For the choice, the items are dumped in a choice box.
By introducing data aware or data feeding controls, the same
dataprovider can be used to fill listboxes, listctrls, choices, grids
etc with data that is retrieved from the provider. By making a few
methods in the provider act like an interface to any data aware
component that wxPython could offer in the future, one is freed of the
ever repeating tasks of creating wx.ListCtrl objects, initializing
them, painstakingy filling them with the crappy interface that is
provided.
The basic goal could be is providing a a set of controls that are
max-data aware, and provide the following automated features:
- Auto filling with data in the preferred (or last used) sort order
- Column sorting by clicking on it
- Column re-ordering by dragging columns or even introducing new columns
- Filtering of data to be added
- Auto updating of data records by telling the provider class that
data is changed
- Auto signaling back that the user has selected an item inside the
control so that the application can react upon it
A lot of these things can be automated, if not more things. I work
with Delphi a lot and one it also uses data aware controls. A very
good example is DeveloperExpress. We use those components to fill the
tables with data and with little effort the controls allow us to move
around columns, sort, edit, even switch between column mode view, and
row mode view, all because of one class, a data provider.
I'm working on yet another app, and realized it would be a good
addition to wxPython. I just wanted to get the discussion going, maybe
one day I can contribute to this, as I am sure nobody likes to fight
with wx.ListCtrl and wx.Grid to show over and over again a list of
data, which would be trivial to implement in a provider class, but
once again a lot of work to manually add them to all controls in your
view.
Sorry it got so long. ...
- Jorgen