Trying to set TreeNode icon

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Trying to set TreeNode icon

Gary Ruben-2
I have a TreeEditor whose nodes contain objects with some Bool traits
which I can toggle in an editor view. I'd like to set the icon on the
corresponding TreeNode to one of three images depending on the values of
the contained Bools.
A few months ago Jelle Feringa asked a related question:
http://article.gmane.org/gmane.comp.python.enthought.devel/30987/match=treeeditor+node

but got no response.

Since I have a checkbox event handler from which I can call other
functions I don't necessarily need a Handler if someone can tell me how
to just load an icon on demand into the TreeNode.

Thanks in advance,
Gary R
_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: Trying to set TreeNode icon

Gary Ruben-2
So a simpler question would be, is it actually possible to update an
icon after the view of the node object has been created? Maybe I need to
delete the node instance from the tree and add a new one, and use a
custom tree node whose constructor lets me choose the icon as it is added?

On 4/12/2012 5:50 PM, Gary Ruben wrote:

> I have a TreeEditor whose nodes contain objects with some Bool traits
> which I can toggle in an editor view. I'd like to set the icon on the
> corresponding TreeNode to one of three images depending on the values of
> the contained Bools.
> A few months ago Jelle Feringa asked a related question:
> http://article.gmane.org/gmane.comp.python.enthought.devel/30987/match=treeeditor+node
>
> but got no response.
>
> Since I have a checkbox event handler from which I can call other
> functions I don't necessarily need a Handler if someone can tell me how
> to just load an icon on demand into the TreeNode.
>
> Thanks in advance,
> Gary R
> _______________________________________________
> Enthought-Dev mailing list
> [hidden email]
> https://mail.enthought.com/mailman/listinfo/enthought-dev
>


--
Gary Ruben
Software Engineer
VeRSI - Victorian eResearch Strategic Initiative
e. [hidden email]
_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: Trying to set TreeNode icon

Brennan Williams
On 5/12/2012 1:37 p.m., Gary Ruben wrote:
> So a simpler question would be, is it actually possible to update an
> icon after the view of the node object has been created? Maybe I need to
> delete the node instance from the tree and add a new one, and use a
> custom tree node whose constructor lets me choose the icon as it is added?
I haven't done what you're looking for but I have created my own
MyTreeNode class which inherits TreeNode and then defined my own
get_view method. There are icon_group, icon_item and item_open
attributes for a TreeNode object and there is a get_icon method but no
set_icon method. Perhaps there needs to be a set_icon method which then
updates the displayed tree if the icon of a displayed object in the tree
is changed.

If you look at traitsui\tree_node.py you can see what happens if you
change the label. There's a set_label method and a when_label_changed so
it's probably not too difficult to do the same sort of thing for an icon
change.

Brennan

> On 4/12/2012 5:50 PM, Gary Ruben wrote:
>> I have a TreeEditor whose nodes contain objects with some Bool traits
>> which I can toggle in an editor view. I'd like to set the icon on the
>> corresponding TreeNode to one of three images depending on the values of
>> the contained Bools.
>> A few months ago Jelle Feringa asked a related question:
>> http://article.gmane.org/gmane.comp.python.enthought.devel/30987/match=treeeditor+node
>>
>> but got no response.
>>
>> Since I have a checkbox event handler from which I can call other
>> functions I don't necessarily need a Handler if someone can tell me how
>> to just load an icon on demand into the TreeNode.
>>
>> Thanks in advance,
>> Gary R
>> _______________________________________________
>> Enthought-Dev mailing list
>> [hidden email]
>> https://mail.enthought.com/mailman/listinfo/enthought-dev
>>
>

_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: Trying to set TreeNode icon

pankaj pandey

> > On 4/12/2012 5:50 PM, Gary Ruben wrote:
> >> I have a TreeEditor whose nodes contain objects with some Bool traits
> >> which I can toggle in an editor view. I'd like to set the icon on the
> >> corresponding TreeNode to one of three images depending on the values of
> >> the contained Bools.
> >> A few months ago Jelle Feringa asked a related question:
> >>
http://article.gmane.org/gmane.comp.python.enthought.devel/30987/match=treeeditor+node
> >>
> >> but got no response.
> >>
> >> Since I have a checkbox event handler from which I can call other
> >> functions I don't necessarily need a Handler if someone can tell me how
> >> to just load an icon on demand into the TreeNode.
> >>
> >> Thanks in advance,
> >> Gary R

Here's a small gist which demonstrates how you can change the icon of a Traits
TreeEditor https://gist.github.com/4213200

Select any node and click the button at the bottom repeatedly to toggle the icon
(It'll be good if you have an img.png file in the directory where you save the
gist).

Basically, the tree editor implementation listens to the 'icon' trait on the
TreeNode, so you change fire changes on it to make it fetch the node icon again.

HTH,
Pankaj


_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: Trying to set TreeNode icon

Gary Ruben-2
Thanks Brennan and Pankaj,
I'll look at this properly tomorrow.

Brennan, I had done something along the lines of what you say you had
tried, but hadn't gone the extra step along the lines you suggested of
implementing something like the set_label method and a when_label_changed.

Pankaj, thanks in particular for your time putting together the code
snippet which looks like it will work for my case. My initial play with
it works well as a standalone example.

Gary

On 5/12/2012 6:07 PM, Pankaj Pandey wrote:

>
>>> On 4/12/2012 5:50 PM, Gary Ruben wrote:
>>>> I have a TreeEditor whose nodes contain objects with some Bool traits
>>>> which I can toggle in an editor view. I'd like to set the icon on the
>>>> corresponding TreeNode to one of three images depending on the values of
>>>> the contained Bools.
>>>> A few months ago Jelle Feringa asked a related question:
>>>>
> http://article.gmane.org/gmane.comp.python.enthought.devel/30987/match=treeeditor+node
>>>>
>>>> but got no response.
>>>>
>>>> Since I have a checkbox event handler from which I can call other
>>>> functions I don't necessarily need a Handler if someone can tell me how
>>>> to just load an icon on demand into the TreeNode.
>>>>
>>>> Thanks in advance,
>>>> Gary R
>
> Here's a small gist which demonstrates how you can change the icon of a Traits
> TreeEditor https://gist.github.com/4213200
>
> Select any node and click the button at the bottom repeatedly to toggle the icon
> (It'll be good if you have an img.png file in the directory where you save the
> gist).
>
> Basically, the tree editor implementation listens to the 'icon' trait on the
> TreeNode, so you change fire changes on it to make it fetch the node icon again.
>
> HTH,
> Pankaj
>
>
> _______________________________________________
> Enthought-Dev mailing list
> [hidden email]
> https://mail.enthought.com/mailman/listinfo/enthought-dev
>


--
Gary Ruben
Software Engineer
VeRSI - Victorian eResearch Strategic Initiative
e. [hidden email]
_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: Trying to set TreeNode icon

Gary Ruben-2
Hi Pankaj,

I've spent the day trying to get the icon to update in my code and still
haven't managed it.
I can't use your code example directly because:
my tree editor has nodes that are instances of TreeNode rather than
TreeNodeObject and the editor defines nodes as instances of TreeNode
instead of ObjectTreeNode.
If you want to see, the currently working code (without updating icons)
is here:
https://github.com/AustralianSynchrotron/sinspect/blob/master/app.py
and my failing attempt to get updating icons is here:
https://gist.github.com/4222301
In your example you have a comment: # Special named trait which is
listened by the Tree for icon changes.
Do you mean that the TreeEditor somewhere sets up an event listener if
it sees a Trait with the name icon in the node object? I can't find any
evidence in the TraitsUI code that this is the case, but I can't see how
the tree knows to call the tno_get_icon() method on the node in response
to icon changing.
If so, in my case, since my nodes are TreeNode objects, does this still
occur, or will it try to call a method in the object contained in the
node instead (i.e. in my code, the SPECSRegion object in the node)? I
think I need some guidance on how this works to know whether Brennan's
suggestion is worth pursuing, which seemed to me to be the harder route
to getting this working.

By the way, in my tree editor I have selection_mode='extended', which
enables me to apply settings to all the objects contained in a selected
subset of the tree. This means that in order to update the icon for all
the nodes in the selected portion of the tree such that they reflect the
settings in the objects contained by the nodes I need to trigger events
on all nodes in the selection. I modified your example to support this
by changing TView() to

class TView(HasTraits):
     root = Instance(TNode)
     sel = Any()             # changed from sel = Instance(TNode)
     button = Event

     traits_view = View(Group(
           Item('root',
                 editor=TreeEditor(
                     nodes=[ObjectTreeNode(node_for = [ TNode ],
                            children='something',
                     )],
                     selected = 'sel',
                     selection_mode = 'extended',    # added this
                     hide_root=True,
                     auto_open=2,
                     editable=False,
                 ),
              ),
         Item('button', editor=ButtonEditor(label='Click to change icon')),
         show_labels=False,),
         resizable=True,)

     def _button_fired(self):
         for s in self.sel:      # changed from self.sel.icon += 1
             s.icon += 1

and this seems to work.
In case it is poor etiquette, I apologise for not providing a minimal
example, but it would probably take me hours or days to make one. I
tried modifying your example to use TreeNode instead and couldn't
immediately get it to work,

thanks,
Gary


On 5/12/2012 6:28 PM, Gary Ruben wrote:

> Thanks Brennan and Pankaj,
> I'll look at this properly tomorrow.
>
> Brennan, I had done something along the lines of what you say you had
> tried, but hadn't gone the extra step along the lines you suggested of
> implementing something like the set_label method and a when_label_changed.
>
> Pankaj, thanks in particular for your time putting together the code
> snippet which looks like it will work for my case. My initial play with
> it works well as a standalone example.
>
> Gary
>
> On 5/12/2012 6:07 PM, Pankaj Pandey wrote:
>>
>>>> On 4/12/2012 5:50 PM, Gary Ruben wrote:
>>>>> I have a TreeEditor whose nodes contain objects with some Bool traits
>>>>> which I can toggle in an editor view. I'd like to set the icon on the
>>>>> corresponding TreeNode to one of three images depending on the values of
>>>>> the contained Bools.
>>>>> A few months ago Jelle Feringa asked a related question:
>>>>>
>> http://article.gmane.org/gmane.comp.python.enthought.devel/30987/match=treeeditor+node
>>>>>
>>>>> but got no response.
>>>>>
>>>>> Since I have a checkbox event handler from which I can call other
>>>>> functions I don't necessarily need a Handler if someone can tell me how
>>>>> to just load an icon on demand into the TreeNode.
>>>>>
>>>>> Thanks in advance,
>>>>> Gary R
>>
>> Here's a small gist which demonstrates how you can change the icon of a Traits
>> TreeEditor https://gist.github.com/4213200
>>
>> Select any node and click the button at the bottom repeatedly to toggle the icon
>> (It'll be good if you have an img.png file in the directory where you save the
>> gist).
>>
>> Basically, the tree editor implementation listens to the 'icon' trait on the
>> TreeNode, so you change fire changes on it to make it fetch the node icon again.
>>
>> HTH,
>> Pankaj
>>
>>
>> _______________________________________________
>> Enthought-Dev mailing list
>> [hidden email]
>> https://mail.enthought.com/mailman/listinfo/enthought-dev
>>
>
>


--
Gary Ruben
Software Engineer
VeRSI - Victorian eResearch Strategic Initiative
e. [hidden email]
_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev