Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lang: set id name as attribute of root widget #812

Merged
merged 2 commits into from Mar 31, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions kivy/lang.py
Expand Up @@ -464,6 +464,7 @@
from kivy import kivy_data_dir, require
from kivy.lib.debug import make_traceback
import kivy.metrics as metrics
from weakref import ref


trace = Logger.trace
Expand Down Expand Up @@ -1264,6 +1265,17 @@ def _apply_rule(self, widget, rule, rootrule, template_ctx=None):
# if we got an id, put it in the root rule for a later global usage
if rule.id:
rctx['ids'][rule.id] = widget
# set id name as a attribute for root widget so one can in python
# code simply access root_widget.id_name
_ids = dict(rctx['ids'])
_root = _ids.pop('root')
_new_ids = _root.ids
for _key in _ids.keys():
if _ids[_key] == _root:
# skip on self
continue
_new_ids[_key] = ref(_ids[_key])
_root.ids = _new_ids

# first, ensure that the widget have all the properties used in
# the rule if not, they will be created as ObjectProperty.
Expand Down
34 changes: 31 additions & 3 deletions kivy/properties.pyx
Expand Up @@ -234,9 +234,11 @@ cdef class Property:

:Parameters:
`errorhandler`: callable
If set, must take a single argument and return a valid substitute value
If set, must take a single argument and return a valid substitute
value
`errorvalue`: object
If set, will replace an invalid property value (overrides errorhandler)
If set, will replace an invalid property value (overrides
errorhandler)

.. versionchanged:: 1.4.2
Parameters errorhandler and errorvalue added
Expand Down Expand Up @@ -601,8 +603,34 @@ class ObservableDict(dict):
self.obj = largs[1]
super(ObservableDict, self).__init__(*largs[2:])

def _weak_return(self, item):
if isinstance(item, ref):
return item()
return item

def __getattr__(self, attr):
try:
return self._weak_return(self.__getitem__(attr))
except KeyError:
try:
return self._weak_return(
super(ObservableDict, self).__getattr__(attr))
except AttributeError:
raise KeyError(attr)

def __setattr__(self, attr, value):
if attr in ('prop', 'obj'):
super(ObservableDict, self).__setattr__(attr, value)
return
self.__setitem__(attr, value)

def __setitem__(self, key, value):
dict.__setitem__(self, key, value)
if value is None:
# remove attribute if value is None
# is this really needed?
self.__delitem__(key)
else:
dict.__setitem__(self, key, value)
observable_dict_dispatch(self)

def __delitem__(self, key):
Expand Down
12 changes: 11 additions & 1 deletion kivy/uix/widget.py
Expand Up @@ -82,7 +82,7 @@ def callback_pos(instance, value):
from kivy.factory import Factory
from kivy.properties import NumericProperty, StringProperty, \
AliasProperty, ReferenceListProperty, ObjectProperty, \
ListProperty
ListProperty, DictProperty
from kivy.graphics import Canvas
from kivy.base import EventLoop
from kivy.lang import Builder
Expand Down Expand Up @@ -564,6 +564,16 @@ def set_center_y(self, value):
dict.
'''

ids = DictProperty({})
'''This is a Dictionary of id's defined in your kv language. This will only
be populated if you use id's in your kv language code.

.. versionadded:: 1.6.0

:data:`ids` is a :class:`~kivy.properties.DictProperty`, defaults to a empty
dict {}.
'''

opacity = NumericProperty(1.0)
'''Opacity of the widget and all the children.

Expand Down