Repository: kivy/kivy

Issue:
TreeView: deselect_node does not properly deselect
<!--
The issue tracker is a tool to address bugs.
Please use the #kivy IRC channel on freenode or Stack Overflow for
support questions, more information at https://git.io/vM1yQ.

Before opening a new issue, make sure you do the following:
    * check that your issue isn't already filed: https://git.io/vM1iE
    * prepare a short, runnable example that reproduces the issue
    * reproduce the problem with the latest development version of Kivy
    * double-check that the issue is indeed a bug and not a support request
-->

### Versions

* Python: 3.6.5 64-bit
* OS: Windows
* Kivy: `1.10.0` and also nightly `v1.10.1.dev0, git-b19d755, 20180601`
* Kivy installation method: `pip install kivy` / `pip install <nightly>.whl`

### Description

When selecting a node in a TreeView and then deselecting it (e.g. by timer) by calling `deselect_node`, the node is only visually deselected. But clicking that node again will not again fire the `on_selected_node` event.

### Reproduce
* select e.g. the first node
* wait 2 seconds (timer will call `deselect_node()`) 
* node will be deselected (but only visually)
* select the same node again
* on_selected_node event is not fired (because the underlying node is still the previous)

### Code
```python

import kivy
kivy.require('1.10.0')

from kivy.app import App
from kivy.lang import Builder
from kivy.factory import Factory

from kivy.uix.treeview import TreeView

class OverrideTreeView(TreeView):
    def deselect_node(self, *args):
        if self._selected_node:
            self._selected_node.is_selected = False

Factory.register('OverrideTreeView', cls=OverrideTreeView)

root = Builder.load_string('''
#: import TreeViewLabel kivy.uix.treeview.TreeViewLabel
#:import Clock kivy.clock.Clock

BoxLayout:
    orientation: 'vertical'
    BoxLayout:
        orientation: 'horizontal'
        OverrideTreeView:
            id: tv
            on_selected_node:
                state.text += "on_selected_node " + str(tv.selected_node) + "\\n"
                Clock.schedule_once(lambda dt: tv.deselect_node(tv), 2)
            on_parent: 
                tv.add_node(TreeViewLabel(text = "Node"))
                tv.add_node(TreeViewLabel(text = "Node"))
                tv.add_node(TreeViewLabel(text = "Node"))

    Label:
        id: state
        text: "State: "
        halign: 'left'
        text_size: self.size
        color: 1,0,0,1
    Button:
        text: "Clear"
        on_press: state.text = ""

''')

class TestApp(App):
    def build(self):
        return root

if __name__ == '__main__':
    TestApp().run()
```

### Proposed fix

Thanks @tshirtman for the suggestion!

```python

def deselect_node(self, *args):
    if self._selected_node:
        self._selected_node.is_selected = False
        # Resetting the AliasProperty as well fixes the issue
        self._selected_node = None
```

If nobody thinks this may have some implications, I can do a pull request with the proposed fix later this week.


Discussion:

Resolving patch:
diff --git a/kivy/uix/treeview.py b/kivy/uix/treeview.py
--- a/kivy/uix/treeview.py
+++ b/kivy/uix/treeview.py
@@ -360,6 +360,7 @@ def deselect_node(self, *args):
         '''
         if self._selected_node:
             self._selected_node.is_selected = False
+            self._selected_node = None
 
     def toggle_node(self, node):
         '''Toggle the state of the node (open/collapsed).


Test patch: