Henning Schröder avatar Henning Schröder committed ce39841

new_instance now looks at each constructor meta-method

Comments (0)

Files changed (1)

experimental/test.py

 
 def cfunc(func, *args, **kwargs):
 	ptr = func(*args)
-	result_type = kwargs.pop("type", QObject)
+	result_type = kwargs.pop("type", None)
 	if kwargs:
 		raise TypeError("Unknown keyword argument(s): %r" % kwargs)
-	return sip.wrapinstance(int(ptr), result_type)
+	if result_type is not None:
+		return sip.wrapinstance(int(ptr), result_type)
 
 
-def new(pyqtwrapper, class_name, *args, **properties):
-	cls = cfunc(pyqtwrapper.metaObjectByName, class_name, type=QMetaObject)
-	obj = cls.newInstance(*args)
-	obj.pyqtConfigure(**properties)
+def new_instance(mo, *args, **properties):
+	obj = None
+	for mi in range(mo.constructorCount()):
+		mm = mo.constructor(mi)
+		names = [str(n) for n in mm.parameterNames()]
+		types = [str(t).rstrip("&*") for t in mm.parameterTypes()]
+		param_types = zip(names, types)
+		if len(param_types) == len(args):
+			arg_types = []
+			for i, (pn, pt) in enumerate(param_types):
+				a = args[i]
+				classes = [type(a)]
+				at = None
+				for cls in classes:
+					if cls.__name__ == pt:
+						at = cls
+						break
+					for base in cls.__bases__:
+						if base not in classes:
+							classes.append(base)
+				if not at:
+					break
+				arg_types.append(at)
+			if len(arg_types) == len(args):
+				obj = mo.newInstance(*[Q_ARG(at, an) for (at,an) in zip(arg_types, args)])
+				obj.pyqtConfigure(**properties)
+				if obj is not None:
+					break
+
+	if not obj:
+		raise TypeError("Could not find matching constructor for %s" % class_name)
 	return obj
 
 
 
+
+
 class QtLibWrapper(object):
-	
+
+
 	def __init__(self, filename):
 		self._lib = ctypes.cdll.LoadLibrary(filename)
-		self._instance = cfunc(self._lib.init)
+		#self._qlib = QLibrary(filename)
+		#print "*", self._qlib.resolve("init")
+		self._instance = cfunc(self._lib.init, type=QObject)
+
 
 	
 	def __getattr__(self, name):
-		cls = lambda *args, **properties: new(self._instance, name, **properties)
-		return cls
+		mo = cfunc(self._instance.metaObjectByName, name, type=QMetaObject)
+		if mo is not None:
+			cls = lambda *args, **properties: new_instance(mo, *args, **properties)
+			setattr(self, name, cls)
+			return cls
+		else:
+			obj = cfunc(self._instance.classByName, name, type=QObject)
+			if obj is not None:
+				setattr(self, name, obj)
+				return obj
+		raise AttributeError(name)
 
 
 
 qtlib = QtLibWrapper("./libhelloworld.so.1.0.0")
 Hello = qtlib.Hello
 
-hello = Hello(text="foo, bar")
-hello.show()
+parent = QDialog(None)
+hello = Hello(parent, text="foo, bar")
+parent.show()
 app.exec_()
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.