[master/rhel7 1/2] Add a wrapper function for GtkTreeViewColumn.set_cell_data_func

Vratislav Podzimek vpodzime at redhat.com
Thu Jul 31 08:59:49 UTC 2014


On Wed, 2014-07-30 at 13:46 -0400, David Shea wrote:
> This method makes it easier to override more than one property on a
> particular column, or to add properties to a column that has already
> been modified by a different method.
> 
> Related: rhbz#1072355
> ---
>  pyanaconda/ui/gui/spokes/datetime_spoke.py         | 12 ++++----
>  pyanaconda/ui/gui/spokes/keyboard.py               | 15 +++++-----
>  pyanaconda/ui/gui/spokes/langsupport.py            | 16 +++++-----
>  .../ui/gui/spokes/lib/lang_locale_handler.py       | 10 +++----
>  pyanaconda/ui/gui/utils.py                         | 34 ++++++++++++++++++++++
>  5 files changed, 61 insertions(+), 26 deletions(-)
> 
> diff --git a/pyanaconda/ui/gui/spokes/datetime_spoke.py b/pyanaconda/ui/gui/spokes/datetime_spoke.py
> index 103338c..a74175f 100644
> --- a/pyanaconda/ui/gui/spokes/datetime_spoke.py
> +++ b/pyanaconda/ui/gui/spokes/datetime_spoke.py
> @@ -30,7 +30,8 @@ from pyanaconda.ui.common import FirstbootSpokeMixIn
>  from pyanaconda.ui.gui import GUIObject
>  from pyanaconda.ui.gui.spokes import NormalSpoke
>  from pyanaconda.ui.gui.categories.localization import LocalizationCategory
> -from pyanaconda.ui.gui.utils import enlightbox, gtk_action_nowait, gtk_action_wait, gtk_call_once
> +from pyanaconda.ui.gui.utils import enlightbox, gtk_action_nowait, gtk_action_wait, gtk_call_once,\
> +        override_cell_property
You might have different preferences, but I like two 'from something
import ...' lines more than a single line ending with a backslash.

> +
> +# This will be populated by override_cell_property. Keys are tuples of (column, renderer).
> +# Values are a dict of the form {property-name: (property-func, property-data)}.
> +_override_cell_property_map = {}
Add a blank line here, please. Otherwise the variable gets lost in
between the comment and the function.

> +def override_cell_property(tree_column, cell_renderer, propname, property_func, data=None):
> +    """
> +    Override a single property of a cell renderer.
> +
> +    property_func takes the same arguments as GtkTreeCellDataFunc:
> +    (TreeViewColumn, CellRenderer, TreeModel, TreeIter, data). Instead of being
> +    expected to manipulate the CellRenderer itself, this method should instead
> +    return the value to which the property should be set.
> +
> +    This method calls set_cell_data_func on the column and renderer.
> +
> +    :param GtkTreeViewColumn column: the column to override
> +    :param GtkCellRenderer cell_renderer: the cell renderer to override
> +    :param str propname: the property to set on the renderer
> +    :param function property_func: a function that returns the value of the property to set
> +    :param data: Optional data to pass to property_func
> +    """
> +
> +    def _cell_data_func(tree_column, cell_renderer, tree_model, tree_iter, _data=None):
> +        overrides = _override_cell_property_map[(tree_column, cell_renderer)]
> +        for property_name in overrides:
> +            property_func, property_func_data = overrides[property_name]
> +            cell_renderer.set_property(property_name,
> +                    property_func(tree_column, cell_renderer, tree_model, tree_iter, property_func_data))
I'd really like to see the return value of property_func assigned to a
variable and then used in the set_property() call. It would be better
readable, better "stepable" in pdb and clearer in case an exception is
raised from it. 

> +
> +    if (tree_column, cell_renderer) not in _override_cell_property_map:
> +        _override_cell_property_map[(tree_column, cell_renderer)] = {}
> +        tree_column.set_cell_data_func(cell_renderer, _cell_data_func)
> +
> +    _override_cell_property_map[(tree_column, cell_renderer)][propname] = (property_func, data)
Other than that I like this change. I must say the mechanism is quite
complicated, but I doubt there could be anything easier due to the way
how the Gtk side works here.

-- 
Vratislav Podzimek

Anaconda Rider | RHCE | Red Hat, Inc. | Brno - Czech Republic




More information about the anaconda-patches mailing list