Some extensions
authorTobias Koenig <tobias.koenig@kdab.com>
Sun, 14 Oct 2012 10:52:45 +0000 (12:52 +0200)
committerTobias Koenig <tobias.koenig@kdab.com>
Sun, 14 Oct 2012 10:52:45 +0000 (12:52 +0200)
declarativeobjects.cpp
declarativeobjects_p.h
declarativewidgetdocument.cpp
test.qml

index ebdc0e0..0aed3a4 100644 (file)
@@ -156,6 +156,62 @@ void DeclarativeObject::data_clear(QDeclarativeListProperty<QObject> *property)
     qWarning("cast went wrong in data_clear");
 }
 
+// DeclarativeHBoxLayout
+DeclarativeHBoxLayout::DeclarativeHBoxLayout(QObject *parent)
+  : DeclarativeObject(parent)
+  , m_layout(new QHBoxLayout)
+{
+  connectAllSignals(m_layout, this);
+}
+
+DeclarativeHBoxLayout::~DeclarativeHBoxLayout()
+{
+  delete m_layout;
+}
+
+QObject* DeclarativeHBoxLayout::object()
+{
+  return m_layout;
+}
+
+void DeclarativeHBoxLayout::dataAppend(QObject *object)
+{
+  DeclarativeWidget *widget = dynamic_cast<DeclarativeWidget*>(object);
+  DeclarativeHBoxLayout *hboxLayout = dynamic_cast<DeclarativeHBoxLayout*>(object);
+  DeclarativeVBoxLayout *vboxLayout = dynamic_cast<DeclarativeVBoxLayout*>(object);
+
+  if (widget) {
+    m_children.append(object);
+    m_layout->addWidget(qobject_cast<QWidget*>(widget->object()));
+  } else if (hboxLayout) {
+    m_children.append(object);
+    m_layout->addLayout(qobject_cast<QLayout*>(hboxLayout->object()));
+  } else if (vboxLayout) {
+    m_children.append(object);
+    m_layout->addLayout(qobject_cast<QLayout*>(vboxLayout->object()));
+  } else {
+    // TODO: error unknown type
+  }
+}
+
+int DeclarativeHBoxLayout::dataCount()
+{
+  return m_children.count();
+}
+
+QObject* DeclarativeHBoxLayout::dataAt(int index)
+{
+  return m_children.at(index);
+}
+
+void DeclarativeHBoxLayout::dataClear()
+{
+  qDeleteAll(m_children);
+  m_children.clear();
+}
+
+CUSTOM_METAOBJECT(DeclarativeHBoxLayout, DeclarativeObject, QHBoxLayout, m_layout)
+
 // DeclarativeVBoxLayout
 DeclarativeVBoxLayout::DeclarativeVBoxLayout(QObject *parent)
   : DeclarativeObject(parent)
@@ -177,14 +233,18 @@ QObject* DeclarativeVBoxLayout::object()
 void DeclarativeVBoxLayout::dataAppend(QObject *object)
 {
   DeclarativeWidget *widget = dynamic_cast<DeclarativeWidget*>(object);
-  DeclarativeVBoxLayout *layout = dynamic_cast<DeclarativeVBoxLayout*>(object);
+  DeclarativeHBoxLayout *hboxLayout = dynamic_cast<DeclarativeHBoxLayout*>(object);
+  DeclarativeVBoxLayout *vboxLayout = dynamic_cast<DeclarativeVBoxLayout*>(object);
 
   if (widget) {
     m_children.append(object);
     m_layout->addWidget(qobject_cast<QWidget*>(widget->object()));
-  } else if (layout) {
+  } else if (hboxLayout) {
     m_children.append(object);
-    m_layout->addLayout(qobject_cast<QLayout*>(layout->object()));
+    m_layout->addLayout(qobject_cast<QLayout*>(hboxLayout->object()));
+  } else if (vboxLayout) {
+    m_children.append(object);
+    m_layout->addLayout(qobject_cast<QLayout*>(vboxLayout->object()));
   } else {
     // TODO: error unknown type
   }
@@ -229,17 +289,23 @@ QObject* DeclarativeWidget::object()
 void DeclarativeWidget::dataAppend(QObject *object)
 {
   DeclarativeWidget *widget = dynamic_cast<DeclarativeWidget*>(object);
-  DeclarativeVBoxLayout *layout = dynamic_cast<DeclarativeVBoxLayout*>(object);
+  DeclarativeHBoxLayout *hboxLayout = dynamic_cast<DeclarativeHBoxLayout*>(object);
+  DeclarativeVBoxLayout *vboxLayout = dynamic_cast<DeclarativeVBoxLayout*>(object);
 
   if (widget) {
     // TODO: error when layout is set
     m_children.append(object);
     qobject_cast<QWidget*>(widget->object())->setParent(m_widget);
-  } else if (layout) {
+  } else if (hboxLayout) {
     // TODO: error when widget is set
 
-    m_children.append(layout);
-    m_widget->setLayout(qobject_cast<QVBoxLayout*>(layout->object()));
+    m_children.append(object);
+    m_widget->setLayout(qobject_cast<QLayout*>(hboxLayout->object()));
+  } else if (vboxLayout) {
+    // TODO: error when widget is set
+
+    m_children.append(object);
+    m_widget->setLayout(qobject_cast<QLayout*>(vboxLayout->object()));
   } else {
     // TODO: error unknown type
   }
index 9a4b3eb..b6cd1d3 100644 (file)
@@ -4,6 +4,7 @@
 #include <QtCore/QObject>
 #include <QtCore/QPointer>
 #include <QtGui/QCheckBox>
+#include <QtGui/QHBoxLayout>
 #include <QtGui/QLabel>
 #include <QtGui/QPushButton>
 #include <QtGui/QVBoxLayout>
@@ -51,6 +52,26 @@ class DeclarativeObject : public QObject
     static void data_clear(QDeclarativeListProperty<QObject> *);
 };
 
+class DeclarativeHBoxLayout : public DeclarativeObject
+{
+  DECLARATIVE_OBJECT
+
+  public:
+    DeclarativeHBoxLayout(QObject *parent = 0);
+    ~DeclarativeHBoxLayout();
+
+    virtual QObject* object();
+
+    virtual void dataAppend(QObject *);
+    virtual int dataCount();
+    virtual QObject *dataAt(int);
+    virtual void dataClear();
+
+  private:
+    QPointer<QHBoxLayout> m_layout;
+    QVector<QObject*> m_children;
+};
+
 class DeclarativeVBoxLayout : public DeclarativeObject
 {
   DECLARATIVE_OBJECT
index 190ebf0..501df74 100644 (file)
@@ -28,11 +28,13 @@ DeclarativeWidgetDocument::DeclarativeWidgetDocument(const QUrl &url, QObject *p
   : QObject(parent)
   , d(new Private(this, url))
 {
-  qmlRegisterType<DeclarativeWidget>("qtgui.widgets", 1, 0, "Widget");
-  qmlRegisterType<DeclarativeLabel>("qtgui.widgets", 1, 0, "Label");
-  qmlRegisterType<DeclarativeVBoxLayout>("qtgui.widgets", 1, 0, "VBoxLayout");
-  qmlRegisterType<DeclarativeTabWidget>("qtgui.widgets", 1, 0, "TabWidget");
-  qmlRegisterType<DeclarativePushButton>("qtgui.widgets", 1, 0, "PushButton");
+  qmlRegisterType<DeclarativeWidget>("QtGui", 1, 0, "Widget");
+  qmlRegisterType<DeclarativeLabel>("QtGui", 1, 0, "Label");
+  qmlRegisterType<DeclarativeHBoxLayout>("QtGui", 1, 0, "HBoxLayout");
+  qmlRegisterType<DeclarativeVBoxLayout>("QtGui", 1, 0, "VBoxLayout");
+  qmlRegisterType<DeclarativeTabWidget>("QtGui", 1, 0, "TabWidget");
+  qmlRegisterType<DeclarativePushButton>("QtGui", 1, 0, "PushButton");
+  qmlRegisterType<DeclarativeCheckBox>("QtGui", 1, 0, "CheckBox");
 
   d->m_component->loadUrl(d->m_url);
   if (d->m_component->isError()) {
index 89c732a..b839f1d 100644 (file)
--- a/test.qml
+++ b/test.qml
@@ -1,5 +1,5 @@
 import QtQuick 1.0
-import qtgui.widgets 1.0
+import QtGui 1.0
 
 Widget {
   VBoxLayout {
@@ -24,5 +24,24 @@ Widget {
       text: "Click me"
       onClicked: secondLabel.text = "Changed"
     }
+    HBoxLayout {
+      CheckBox {
+        id: checkBox
+      }
+      Label {
+        text: checkBox.checked ? "Is checked" : "Is not checked"
+      }
+    }
+    TabWidget {
+      Label {
+        text: "Page 1"
+      }
+      Label {
+        text: "Page 2"
+      }
+      Label {
+        text: "Page 3"
+      }
+    }
   }
 }