Source code for tagit.view.wx_adapter
"""Extensions to *Kivy* classes.
Part of the tagit module.
A copy of the license is provided with the project.
Author: Matthias Baumgartner, 2016
"""
# IMPORTS
from kivy.uix.widget import Widget
import warnings
# EXPORTS
__all__ = ('decorate_widget', )
## CODE ##
[docs]def on_parent(obj, self, parent):
"""Default behaviour if the parent of a widget is changed.
Calls *on_parent* of all childs, even though the direct parent
has not changed. This behaviour is usefull to pass down widget
tree changes to lower widgets.
"""
for child in self.children:
child.on_parent(child, self)
[docs]def get_root(self):
"""Traverse the widget tree upwards until the root is found.
Note that this is not the same as basics.get_root (or app.root from a
debug shell) unless the calling widget is attached to the main widget tree.
Specifically, if a widget or any of its parents is created but not yet
attached.
"""
obj = self
while obj.parent is not None and obj.parent != obj.parent.parent:
obj = obj.parent
return obj
[docs]def get_controller(self, cls=None, wx=None, **kwargs):
"""Search and return the nearest controller.
Create and return an instance of type *cls*, unless *cls* is None.
Returns None if no controller was found.
"""
obj = self
while obj.parent is not None and obj != obj.parent:
if hasattr(obj, 'controller') and obj.controller is not None:
if cls is not None:
if wx is None: wx = self
return obj.controller.add_child(cls=cls, wx=wx, **kwargs)
else:
return obj.controller
obj = obj.parent
return None
def decorate_widget():
"""Add some functions to the *Widget* class.
"""
if not hasattr(Widget, 'on_parent'):
Widget.on_parent = on_parent
else:
warnings.warn("Cannot decorate Widget because target is present")
if not hasattr(Widget, 'get_root'):
Widget.get_root = get_root
else:
warnings.warn("Cannot decorate Widget because target is present")
if not hasattr(Widget, 'get_controller'):
Widget.get_controller = get_controller
else:
warnings.warn("Cannot decorate Widget because target is present")
## EOF ##