1. Henning Schröder
  2. ttgui

Commits

Henning Schröder  committed 95f592b

ObjectName property and connectSlotsByName

  • Participants
  • Parent commits 4b218a1
  • Branches default

Comments (0)

Files changed (1)

File tcore/__init__.py

View file
         self.setter(obj, value)
 
 
-class ObjectSignal(object):
+class BoundSignal(object):
 
     def __init__(self, sender):
         self.sender = sender
 
     def connect(self, slot):
         self.slots.add(slot)
+        return self
+
+    __iadd__ = connect
 
     def emit(self, *args, **kwargs):
         direct = kwargs.pop("direct", False)
         if kwargs:
-            raise TypeError("Unknown keyword arguments %r" % kwargs)
+            raise TypeError("Unknown keyword argument(s) %r" % kwargs)
         if self.sender.signalsBlocked():
             return
-        TObject._sender = self.sender
+        TObject._sender = self.sender # TODO: consider thread-locals
         for ref in self.slots:
             slot = ref()
             if slot:
                 if direct:
-                    slot(*args)
+                    try:
+                        slot(*args)
+                    except Exception, _exc:
+                        import traceback
+                        sys.stderr.write(traceback.format_exc())
                 else:
                     # queued
                     self.sender.postEvent(TInvokeEvent(slot, args))
-
         # Object.sender = None
 
-    def __call__(self, *args, **kwargs):
-        #return self.emit(*args, **kwargs)
-        raise RuntimeError("You should call emit on signal")
+    __call__ = emit
+
 
 class Signal(object):
 
         signals = obj.__dict__.setdefault("__signals__", {})
         objsig = signals.get(self)
         if not objsig:
-            objsig = ObjectSignal(obj)
+            objsig = BoundSignal(obj)
             signals[self] = objsig
         return objsig
 
 
+
 class TObject(object):
     _object_instances = WeakSet()
-
+    _object_id_counter = 0
     _sender = None
 
     destroyed = Signal()
         self._parent = None
         self._event_filters = set()
         self._signals_blocked = False
+        TObject._object_id_counter += 1
+        self._obj_name = "%s%s" % (self.__class__.__name__, TObject._object_id_counter)
 
         if parent:
             self.setParent(parent)
             else:
                 raise TypeError("Invalid keyword argument %r" % key)
 
+    def objectName(self):
+        return self._obj_name
+
+    def setObjectName(self, name):
+        self._obj_name = name
+
+    ObjectName = Property(objectName, setObjectName)
+
+
     @classmethod
     def sender(cls):
         return cls._sender
     def blockSignals(self, flag):
         self._signals_blcoked = flag
 
+    def connectSlotsByName(self):
+        for name in dir(self):
+            if not name.startswith("on_") or name.count("_") < 2:
+                continue
+            slot = getattr(self, name)
+            _on, child, signal = name.split("_", 3)
+            obj = getattr(self, child)
+            obj.connect(slot)
+
     def delete(self):
         self.setParent(None)
         for child in self.children():