Grid Cell Renderer

Is there a simple Grid Cell renderer that would allow me to simply
convert the cell's value to a string? Or is there another place to
convert the cell's data to a display string?

I'm using a GridTable and I know I could do the conversion in the
GetValue method, but that seems like adding a view-specific function to
the model. Also, I've written a Grid Cell editor that needs the raw
cell value, so I would like to have GetValue return that.

Could you please tell what kinds of data you want to convert to a string?

···

2008/1/2, Mark Erbaugh mark@microenh.com:

Is there a simple Grid Cell renderer that would allow me to simply
convert the cell’s value to a string? Or is there another place to
convert the cell’s data to a display string?

I’m using a GridTable and I know I could do the conversion in the

GetValue method, but that seems like adding a view-specific function to
the model. Also, I’ve written a Grid Cell editor that needs the raw
cell value, so I would like to have GetValue return that.


To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail:
wxPython-users-help@lists.wxwidgets.org

The data is an integer that corresponds to the string. I have a dict
that has the integer as the key and the string as the value.

wxPython has a GridCellEnumRenderer but I can't seem to get that working
and other than it's existence, I can't find any documentation on it.
It's not listed in the wxWidgets documentation, although it is there in
the C++ code.

Mark

···

On Wed, 2008-01-02 at 06:58 +0100, Raffaello Barella wrote:

Could you please tell what kinds of data you want to convert to a
string?

2008/1/2, Mark Erbaugh <mark@microenh.com>:
        Is there a simple Grid Cell renderer that would allow me to
        simply
        convert the cell's value to a string? Or is there another
        place to
        convert the cell's data to a display string?
        
        I'm using a GridTable and I know I could do the conversion in
        the
        GetValue method, but that seems like adding a view-specific
        function to
        the model. Also, I've written a Grid Cell editor that needs
        the raw
        cell value, so I would like to have GetValue return that.
        
        ---------------------------------------------------------------------

Mark,
I attach the code of my file rbchoicegrid.py, which contains an editor and a renderer returning a string from an underlying integer cell value. On my applications it works nicely. No copyright.

rbchoicegrid.py (7.57 KB)

···

2008/1/2, Mark Erbaugh mark@microenh.com:

On Wed, 2008-01-02 at 06:58 +0100, Raffaello Barella wrote:

Could you please tell what kinds of data you want to convert to a
string?

2008/1/2, Mark Erbaugh <
mark@microenh.com>:
Is there a simple Grid Cell renderer that would allow me to
simply
convert the cell’s value to a string? Or is there another
place to

    convert the cell's data to a display string?

    I'm using a GridTable and I know I could do the conversion in
    the
    GetValue method, but that seems like adding a view-specific
    function to
    the model. Also, I've written a Grid Cell editor that  needs
    the raw
    cell value, so I would like to have GetValue return that.

    ---------------------------------------------------------------------

The data is an integer that corresponds to the string. I have a dict
that has the integer as the key and the string as the value.

wxPython has a GridCellEnumRenderer but I can’t seem to get that working

and other than it’s existence, I can’t find any documentation on it.
It’s not listed in the wxWidgets documentation, although it is there in
the C++ code.

Mark


To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail:
wxPython-users-help@lists.wxwidgets.org

Here's the situation. There is a database table (CATEGORY) with a list
of categories. Each row in this table has a unique (SERIAL /
AUTO-INCREMENT) integer column a name column.

There is a second table (MATERIAL). Each row has an integer column (RSN)
that references a row in the first table. For each row in this table,
there needs to select a category from the CATEGORY table.

I want to display and edit the rows from the MATERIAL table in the grid.
I'd like to have a ComboBox from which the user can select the category
for the row. The integer RSN value doesn't mean anything to the user,
just the category name.

Are you suggesting that I put the code to translate from the integer to
the string in the GridTable? That should work, but wouldn't that be
putting some view logic in the model (assuming MVC)?

Mark

···

On Wed, 2008-01-02 at 12:07 -0800, Rich Shepard wrote:

   Just out of curiosity, what is the context for this need? I'm
trying without success to picture the situation. Is there a reason why
the string cannot be displayed in the grid cell, or used in your code?
You write that you have a dictionary of enumerates, but don't explain
why that's not sufficient.

Here's the situation. There is a database table (CATEGORY) with a list of
categories. Each row in this table has a unique (SERIAL/AUTO-INCREMENT)
integer column a name column.

There is a second table (MATERIAL). Each row has an integer column (RSN)
that references a row in the first table. For each row in this table,
there needs to select a category from the CATEGORY table.

Mark,

   I must be missing something because I don't see any problem. For example,
you have a combo box with CATEGORY names displayed. When your user selects a
name, your SELECT statement would match the MATERIAL foreign key to the
CATAGORY primary key (using your dictionary) and display the appropriate
MATERIAL string. Is this not what you want? And, each row in the grid has a
different CATEGORY?

Are you suggesting that I put the code to translate from the integer to
the string in the GridTable?

   No. In the method bound to the combo box selection.

That should work, but wouldn't that be putting some view logic in the
model (assuming MVC)?

   IIRC, wxPython implements the MVC concept imperfectly. And, there are
valid situations when de-normalizing relational database tables is also
justified for speed and ease of use. What works ought to be the determining
criterion.

Rich

···

On Wed, 2 Jan 2008, Mark Erbaugh wrote:

--
Richard B. Shepard, Ph.D. | Integrity Credibility
Applied Ecosystem Services, Inc. | Innovation
<http://www.appl-ecosys.com> Voice: 503-667-4517 Fax: 503-667-8863

   I must be missing something because I don't see any problem. For
example, you have a combo box with CATEGORY names displayed. When your
user selects a name, your SELECT statement would match the MATERIAL
foreign key to the CATAGORY primary key (using your dictionary) and
display the appropriate MATERIAL string. Is this not what you want?
And, each row in the grid has a different CATEGORY?

That's not quite what I had in mind.

Here are a few sample CATEGORY records:

rsn | Name
001 | Steel
002 | Aluminum
003 | Electical

And some matching MATERIAL records:

rsn | cat | Name
001 | 001 | 4' by 8' by 0.25"
002 | 001 | 4' by 10' by 0.25"
003 | 002 | 4' by 8' by 0.25"
004 | 002 | 4' by 10' by 0.25"
005 | 003 | 20A panel
006 | 004 | duplex outlet

The grid in question will show all MATERIAL records (there will be only
a couple hundred). They will be retrieved from the database with a
single query: 'select rsn, cat, Name from MATERIAL'

However, instead of displaying the integer values for the category
column, I want to display the name

rsn | category | Name
001 | Steel | 4' by 8' by 0.25"
002 | Steel | 4' by 10' by 0.25"
003 | Aluminum | 4' by 8' by 0.25"
004 | Aluminum | 4' by 10' by 0.25"
005 | Electrical | 20A panel
006 | Electrical | duplex outlet

When the user edits the category column, they do so by a drop down list.

An alternative would be to select the rows with a join to get category
names: select m.rsn, c.name, m.name from MATERIAL m join CATEGORY c
on ...

Then use just user the drop down list to force the user to select a
valid category and then translate from the category string to it's RSN
in the process that writes the rows to the database.

   IIRC, wxPython implements the MVC concept imperfectly. And, there
are valid situations when de-normalizing relational database tables is
also justified for speed and ease of use. What works ought to be the
determining criterion.

I agree. After composing this reply, I'm considering storing the
category string directly in the MATERIAL table. In this application,
the only advantage of normalizing the category name is to allow the
category name to change and that's unlikely enough that it could be
handled with a mass update: update material set c_name=<NEW CATEGORY>
where c_name = <OLD CATEGORY>

Mark

···

On Wed, 2008-01-02 at 15:11 -0800, Rich Shepard wrote:

Mark,

   That looks like the ideal solution. I know that there's a lot of
unresolved debate in the database world about surrogate keys, and I tend to
be in the group that avoids them.

   The primary key should be meaningful, and using a serial number just to
have a numeric primary key adds no value to the table. Storing strings when
they carry meaning is a good idea.

Glad you have a solution path,

Rich

···

On Wed, 2 Jan 2008, Mark Erbaugh wrote:

I agree. After composing this reply, I'm considering storing the category
string directly in the MATERIAL table. In this application, the only
advantage of normalizing the category name is to allow the category name
to change and that's unlikely enough that it could be handled with a mass
update: update material set c_name=<NEW CATEGORY> where c_name = <OLD
>

--
Richard B. Shepard, Ph.D. | Integrity Credibility
Applied Ecosystem Services, Inc. | Innovation
<http://www.appl-ecosys.com> Voice: 503-667-4517 Fax: 503-667-8863

Mark Erbaugh wrote:

Is there a simple Grid Cell renderer that would allow me to simply
convert the cell's value to a string? Or is there another place to
convert the cell's data to a display string?

I'm using a GridTable and I know I could do the conversion in the
GetValue method, but that seems like adding a view-specific function to
the model. Also, I've written a Grid Cell editor that needs the raw
cell value, so I would like to have GetValue return that.

The table's GetValue is supposed to always deal with strings in order to comply with the API. There is some limited support to query the table and find out if it can provide the value as other types, but so far it can only deal with longs, doubles and bools. In the past I've thought that it would be nice for the table to be able to provide the data as a PyObject* (ie, any Python object) but I haven't tried implementing that yet.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

I'm having no luck implementing GetValueAsLong. I added CanGetValueAs
and GetValueAsLong methods to my grid table. I placed a print
statement in it so I can see that it is being called and it returns a
Python True. However, the table's GetValueAsLong is not being called.
Instead, the table's GetValue is called. I've looked at the C++ code:

    wxGridTableBase *table = grid->GetTable();

    if ( table->CanGetValueAs(row, col, wxGRID_VALUE_NUMBER) )
    {
        m_startint = table->GetValueAsLong(row, col);
    }
    else
    {
        wxString startValue = table->GetValue(row, col);

And it sure looks like it should work. Here are the relevant methods
from the table:

    def CanGetValueAs(self, row, col, typeName):
        col_name = self.col_fields[col]
        if (col_name == 'category_rsn') and (typeName ==
wx.grid.GRID_VALUE_NUMBER):
            print 'can get value as long'
            return True
        else:
            return False

    def GetValueAsLong(self, row, col):
        print 'get value as long'
        col_name = self.col_fields[col]
        r = self.data[row][col_name]
        return r

Suggestions?

Mark

···

On Wed, 2008-01-02 at 17:00 -0800, Robin Dunn wrote:

Mark Erbaugh wrote:
> On Wed, 2008-01-02 at 06:58 +0100, Raffaello Barella wrote:
>> Could you please tell what kinds of data you want to convert to a
>> string?
>>

>
> The data is an integer that corresponds to the string. I have a dict
> that has the integer as the key and the string as the value.
>
> wxPython has a GridCellEnumRenderer but I can't seem to get that working
> and other than it's existence, I can't find any documentation on it.
> It's not listed in the wxWidgets documentation, although it is there in
> the C++ code.

If you really want to keep the integer values all the way through then
try implementing GetValueAsLong(self, row, col) in the table, and also
CanGetValueAs(self, row, col, typeName) and return true for that cell
for typeName == "long". Otherwise the grid, editors, and renderers
should translate the value to/from strings as needed.