Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from kivy.properties cimport Property, PropertyStorage
- from kivy._event cimport EventDispatcher
- cdef inline void observable_object_dispatch(object self, str name):
- cdef Property prop = self.prop
- prop.dispatch(self.obj, name)
- class ObservableObject(object):
- # Internal class to observe changes inside a native python object.
- def __init__(self, *largs):
- self.prop = largs[0]
- self.obj = largs[1]
- super(ObservableObject, self).__init__()
- def __setattr__(self, name, value):
- object.__setattr__(self, name, value)
- observable_object_dispatch(self, name)
- cdef class DocProperty(Property):
- def __init__(self, defaultvalue=None, rebind=False, **kw):
- self.baseclass = kw.get('baseclass', object)
- super(DocProperty, self).__init__(defaultvalue, **kw)
- self.rebind = rebind
- cpdef link(self, EventDispatcher obj, str name):
- Property.link(self, obj, name)
- cdef PropertyStorage ps = obj.__storage[self._name]
- ps.value = ObservableObject(self, obj, ps.value)
- cdef check(self, EventDispatcher obj, value):
- if Property.check(self, obj, value):
- return True
- if not isinstance(value, object):
- raise ValueError('{}.{} accept only object based on {}'.format(
- obj.__class__.__name__,
- self.name,
- self.baseclass.__name__))
- cpdef dispatch(self, EventDispatcher obj, str name):
- '''Dispatch the value change to all observers.
- .. versionchanged:: 1.1.0
- The method is now accessible from Python.
- This can be used to force the dispatch of the property, even if the
- value didn't change::
- button = Button()
- # get the Property class instance
- prop = button.property('text')
- # dispatch this property on the button instance
- prop.dispatch(button)
- '''
- cdef PropertyStorage ps = obj.__storage[self._name]
- ps.observers.dispatch(obj, ps.value, (name,), None, 0)
- from kivy.properties cimport Property, PropertyStorage
- from kivy._event cimport EventDispatcher
- cdef class DocProperty(Property):
- cdef object baseclass
- cdef public int rebind
- cpdef dispatch(self, EventDispatcher obj, str name)
- # -*- coding: utf-8 -*-
- from kivy.event import EventDispatcher
- import pyximport
- pyximport.install()
- from properties import DocProperty
- if __name__ == '__main__':
- class ED(EventDispatcher):
- doc = DocProperty()
- def on_doc(self, obj, value):
- print 'printing doc', self.doc
- class DumbObj(object):
- def __init__(self, num):
- self._num = num
- @property
- def num(self):
- return 5
- @num.setter
- def num(self, value):
- self._num = value
- ed = ED()
- ed.doc = DumbObj(3)
- ed.doc.num = 4
Add Comment
Please, Sign In to add comment