[Kde-bindings] KDE/kdebindings/ruby/qtruby

Richard Dale Richard_Dale at tipitina.demon.co.uk
Mon Aug 11 08:18:34 UTC 2008


SVN commit 845073 by rdale:

* Added a qobject_cast() method that uses qt_metacast() to cast one 
  instance of QObject to another. This allow the KTextEditor:: 
  interface classes to be used. Fixes a problem reported by Stefano
  Crocco. Example usage:

	doc = KTextEditor::EditorChooser.editor('katepart').create_document( nil)
	annotator = doc.qobject_cast(KTextEditor::AnnotationInterface)

CCMAIL: kde-bindings at kde.org



 M  +10 -0     ChangeLog  
 M  +0 -22     src/Qt.cpp  
 M  +60 -0     src/qtruby.cpp  
 M  +0 -1      src/qtruby.h  


--- trunk/KDE/kdebindings/ruby/qtruby/ChangeLog #845072:845073
@@ -1,3 +1,13 @@
+2008-08-11  Richard Dale  <richard.j.dale at gmail.com>
+
+	* Added a qobject_cast() method that uses qt_metacast() to cast one 
+	  instance of QObject to another. This allow the KTextEditor:: 
+	  interface classes to be used. Fixes a problem reported by Stefano
+	  Crocco. Example usage:
+
+		doc = KTextEditor::EditorChooser.editor('katepart').create_document( nil)
+		annotator = doc.qobject_cast(KTextEditor::AnnotationInterface)
+
 2008-08-10  Richard Dale  <richard.j.dale at gmail.com>
 
 	* Fix a bug reported by Stefano Crocco where primitives types and QStrings
--- trunk/KDE/kdebindings/ruby/qtruby/src/Qt.cpp #845072:845073
@@ -1157,28 +1157,6 @@
     return obj;
 }
 
-VALUE
-cast_object_to(VALUE /*self*/, VALUE object, VALUE new_klass)
-{
-    smokeruby_object *o = value_obj_info(object);
-
-	VALUE new_klassname = rb_funcall(new_klass, rb_intern("name"), 0);
-
-    Smoke::ModuleIndex * cast_to_id = classcache.value(StringValuePtr(new_klassname));
-	if (cast_to_id == 0) {
-		rb_raise(rb_eArgError, "unable to find class \"%s\" to cast to\n", StringValuePtr(new_klassname));
-	}
-
-	smokeruby_object * o_cast = alloc_smokeruby_object(	o->allocated, 
-														cast_to_id->smoke, 
-														(int) cast_to_id->index, 
-														o->smoke->cast(o->ptr, o->classId, (int) cast_to_id->index) );
-
-    VALUE obj = Data_Wrap_Struct(new_klass, smokeruby_mark, smokeruby_free, (void *) o_cast);
-    mapPointer(obj, o_cast, o_cast->classId, 0);
-    return obj;
-}
-
 VALUE 
 kross2smoke(VALUE /*self*/, VALUE krobject, VALUE new_klass)
 {
--- trunk/KDE/kdebindings/ruby/qtruby/src/qtruby.cpp #845072:845073
@@ -827,6 +827,65 @@
 }
 
 static VALUE
+cast_object_to(VALUE /*self*/, VALUE object, VALUE new_klass)
+{
+    smokeruby_object *o = value_obj_info(object);
+
+	VALUE new_klassname = rb_funcall(new_klass, rb_intern("name"), 0);
+
+    Smoke::ModuleIndex * cast_to_id = classcache.value(StringValuePtr(new_klassname));
+	if (cast_to_id == 0) {
+		rb_raise(rb_eArgError, "unable to find class \"%s\" to cast to\n", StringValuePtr(new_klassname));
+	}
+
+	smokeruby_object * o_cast = alloc_smokeruby_object(	o->allocated, 
+														cast_to_id->smoke, 
+														(int) cast_to_id->index, 
+														o->smoke->cast(o->ptr, o->classId, (int) cast_to_id->index) );
+
+    VALUE obj = Data_Wrap_Struct(new_klass, smokeruby_mark, smokeruby_free, (void *) o_cast);
+    mapPointer(obj, o_cast, o_cast->classId, 0);
+    return obj;
+}
+
+static VALUE
+qobject_qt_metacast(VALUE self, VALUE klass)
+{
+    smokeruby_object *o = value_obj_info(self);
+	if (o == 0 || o->ptr == 0) {
+		return Qnil;
+	}
+
+	const char * classname = rb_class2name(klass);
+	Smoke::ModuleIndex * mi = classcache.value(classname);
+	if (mi == 0) {
+		return Qnil;
+	}
+
+	o->classId = (int) mi->index;
+
+	QObject* qobj = (QObject*) o->smoke->cast(o->ptr, o->classId, o->smoke->idClass("QObject").index);
+	if (qobj == 0) {
+		return Qnil;
+	}
+
+	void* ret = qobj->qt_metacast(mi->smoke->classes[mi->index].className);
+
+	if (ret == 0) {
+		return Qnil;
+	}
+
+	smokeruby_object * o_cast = alloc_smokeruby_object(	o->allocated, 
+														mi->smoke, 
+														(int) mi->index, 
+														ret );
+
+    VALUE obj = Data_Wrap_Struct(klass, smokeruby_mark, smokeruby_free, (void *) o_cast);
+    mapPointer(obj, o_cast, o_cast->classId, 0);
+    return obj;
+}
+
+static VALUE
 qvariant_value(VALUE /*self*/, VALUE variant_value_klass, VALUE variant_value)
 {
 	void * value_ptr = 0;
@@ -1962,6 +2021,7 @@
 	}
 	
 
+	rb_define_method(klass, "qobject_cast", (VALUE (*) (...)) qobject_qt_metacast, 1);
 	rb_define_method(klass, "inspect", (VALUE (*) (...)) inspect_qobject, 0);
 	rb_define_method(klass, "pretty_print", (VALUE (*) (...)) pretty_print_qobject, 1);
 	rb_define_method(klass, "className", (VALUE (*) (...)) class_name, 0);
--- trunk/KDE/kdebindings/ruby/qtruby/src/qtruby.h #845072:845073
@@ -153,7 +153,6 @@
 extern Q_DECL_EXPORT VALUE mapObject(VALUE self, VALUE obj);
 extern Q_DECL_EXPORT VALUE qobject_metaobject(VALUE self);
 extern Q_DECL_EXPORT VALUE set_obj_info(const char * className, smokeruby_object * o);
-extern Q_DECL_EXPORT VALUE cast_object_to(VALUE self, VALUE object, VALUE new_klass);
 extern Q_DECL_EXPORT VALUE kross2smoke(VALUE self, VALUE krobject, VALUE new_klass);
 extern Q_DECL_EXPORT const char* get_VALUEtype(VALUE ruby_value);
 extern Q_DECL_EXPORT VALUE prettyPrintMethod(Smoke::Index id);



More information about the Kde-bindings mailing list