[Kde-bindings] KDE/kdebindings/qtruby

Richard Dale Richard_Dale at tipitina.demon.co.uk
Sat Feb 17 09:52:32 UTC 2007


SVN commit 634420 by rdale:

* If a new Ruby class is created for a custom C++ QObject derived class,
  then create Ruby constants in the class for any enums defined via 
  Q_ENUMS which are in the same scope as the new class. For instance:

	class TestObject : public QObject
	{
		Q_OBJECT
		Q_ENUMS(Priority)
		public:
			enum Priority { High, Low, VeryHigh, VeryLow };

	...

	irb(main):001:0> app = Qt::Application.new(ARGV)
	=> #<Qt::Application:0xb6aaaf58 objectName="irb">
	irb(main):002:0> require 'testqobject'
	=> true
	irb(main):003:0> test = app.findChild(Qt::Object, "QtRuby TestObject")
	=> #<TestObject:0xb6aa5184 objectName="QtRuby TestObject">
	irb(main):004:0> TestObject::High
	=> 0
	irb(main):005:0> TestObject::Low
	=> 1
	irb(main):006:0> TestObject::VeryHigh
	=> 2

CCMAIL: kde-bindings at kde.org



 M  +28 -0     ChangeLog  
 M  +41 -28    rubylib/qtruby/Qt.cpp  


--- trunk/KDE/kdebindings/qtruby/ChangeLog #634419:634420
@@ -1,3 +1,31 @@
+2007-02-17  Richard Dale  <rdale at foton.es>
+
+	* If a new Ruby class is created for a custom C++ QObject derived class,
+	  then create Ruby constants in the class for any enums defined via 
+	  Q_ENUMS which are in the same scope as the new class. For instance:
+
+		class TestObject : public QObject
+		{
+			Q_OBJECT
+			Q_ENUMS(Priority)
+			public:
+				enum Priority { High, Low, VeryHigh, VeryLow };
+
+		...
+
+		irb(main):001:0> app = Qt::Application.new(ARGV)
+		=> #<Qt::Application:0xb6aaaf58 objectName="irb">
+		irb(main):002:0> require 'testqobject'
+		=> true
+		irb(main):003:0> test = app.findChild(Qt::Object, "QtRuby TestObject")
+		=> #<TestObject:0xb6aa5184 objectName="QtRuby TestObject">
+		irb(main):004:0> TestObject::High
+		=> 0
+		irb(main):005:0> TestObject::Low
+		=> 1
+		irb(main):006:0> TestObject::VeryHigh
+		=> 2
+
 2007-02-16  Richard Dale  <rdale at foton.es>
 
 	* Thomas Moenicke fixed the qtruby cmake build so it works on
--- trunk/KDE/kdebindings/qtruby/rubylib/qtruby/Qt.cpp #634419:634420
@@ -590,6 +590,18 @@
 
 			if (new_klass != Qnil) {
 				klass = new_klass;
+
+				for (int id = meta->enumeratorOffset(); id < meta->enumeratorCount(); id++) {
+					// If there are any enum keys with the same scope as the new class then
+					// add them
+					if (qstrcmp(meta->className(), meta->enumerator(id).scope()) == 0) {
+						for (int i = 0; i < meta->enumerator(id).keyCount(); i++) {
+							rb_define_const(	klass, 
+												meta->enumerator(id).key(i), 
+												INT2NUM(meta->enumerator(id).value(i)) );
+						}
+					}
+				}
 			}
 
 			// Add a Qt::Object.metaObject method which will do dynamic despatch on the
@@ -1789,42 +1801,43 @@
 								VALUE qvariant = rb_funcall(self, rb_intern("property"), 1, rb_str_new2(*name));
 								return rb_funcall(qvariant, rb_intern("value"), 0);
 							}
-						} else if (argc == 2 && name->endsWith("=")) {
+						}
+
+						if (argc == 2 && name->endsWith("=")) {
 							name->replace("=", "");
 							if (meta->indexOfProperty(*name) != -1) {
 								VALUE qvariant = rb_funcall(self, rb_intern("qVariantFromValue"), 1, argv[1]);
 								return rb_funcall(self, rb_intern("setProperty"), 2, rb_str_new2(*name), qvariant);
 							}
-						} else {
-							int classId = o->smoke->idClass(meta->className());
-							// The class isn't in the Smoke lib..
-							while (classId == 0) {
-								// Assume the QObject has slots which aren't in the Smoke library, so try
-								// and call the slot directly
-								for (int id = meta->methodOffset(); id < meta->methodCount(); id++) {
-									if (meta->method(id).methodType() == QMetaMethod::Slot) {
-										QByteArray signature(meta->method(id).signature());
-										QByteArray methodName = signature.mid(0, signature.indexOf('('));
-	
-										// Don't check that the types of the ruby args match the c++ ones for now,
-										// only that the name and arg count is the same.
-										if (*name == methodName && signature.count(',') == (argc - 2)) {
-											VALUE args = rb_funcall(	qt_internal_module, 
-																		rb_intern("getMocArguments"), 
-																		2, 
-																		rb_str_new2(meta->method(id).typeName()), 
-																		rb_str_new2(meta->method(id).signature()) );
-										
-											VALUE result = Qnil;
-											InvokeNativeSlot slot(qobject, id, argc - 1, args, argv + 1, &result);
-											slot.next();
-											return result;
-										}
+						}
+
+						int classId = o->smoke->idClass(meta->className());
+						// The class isn't in the Smoke lib..
+						while (classId == 0) {
+							// Assume the QObject has slots which aren't in the Smoke library, so try
+							// and call the slot directly
+							for (int id = meta->methodOffset(); id < meta->methodCount(); id++) {
+								if (meta->method(id).methodType() == QMetaMethod::Slot) {
+									QByteArray signature(meta->method(id).signature());
+									QByteArray methodName = signature.mid(0, signature.indexOf('('));
+
+									// Don't check that the types of the ruby args match the c++ ones for now,
+									// only that the name and arg count is the same.
+									if (*name == methodName && signature.count(',') == (argc - 2)) {
+										VALUE args = rb_funcall(	qt_internal_module, 
+																	rb_intern("getMocArguments"), 
+																	2, 
+																	rb_str_new2(meta->method(id).typeName()), 
+																	rb_str_new2(meta->method(id).signature()) );									
+										VALUE result = Qnil;
+										InvokeNativeSlot slot(qobject, id, argc - 1, args, argv + 1, &result);
+										slot.next();
+										return result;
 									}
 								}
-								meta = meta->superClass();
-								classId = o->smoke->idClass(meta->className());
 							}
+							meta = meta->superClass();
+							classId = o->smoke->idClass(meta->className());
 						}
 					}
 					



More information about the Kde-bindings mailing list