From: Tobias Koenig Date: Sun, 14 Oct 2012 10:52:45 +0000 (+0200) Subject: Some extensions X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=b38117968e72ba16ec4700776b0ef0aae45311dc;p=web%2Fkonrad%2FDeclarativeWidgets.git Some extensions --- diff --git a/declarativeobjects.cpp b/declarativeobjects.cpp index ebdc0e0..0aed3a4 100644 --- a/declarativeobjects.cpp +++ b/declarativeobjects.cpp @@ -156,6 +156,62 @@ void DeclarativeObject::data_clear(QDeclarativeListProperty *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(object); + DeclarativeHBoxLayout *hboxLayout = dynamic_cast(object); + DeclarativeVBoxLayout *vboxLayout = dynamic_cast(object); + + if (widget) { + m_children.append(object); + m_layout->addWidget(qobject_cast(widget->object())); + } else if (hboxLayout) { + m_children.append(object); + m_layout->addLayout(qobject_cast(hboxLayout->object())); + } else if (vboxLayout) { + m_children.append(object); + m_layout->addLayout(qobject_cast(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(object); - DeclarativeVBoxLayout *layout = dynamic_cast(object); + DeclarativeHBoxLayout *hboxLayout = dynamic_cast(object); + DeclarativeVBoxLayout *vboxLayout = dynamic_cast(object); if (widget) { m_children.append(object); m_layout->addWidget(qobject_cast(widget->object())); - } else if (layout) { + } else if (hboxLayout) { m_children.append(object); - m_layout->addLayout(qobject_cast(layout->object())); + m_layout->addLayout(qobject_cast(hboxLayout->object())); + } else if (vboxLayout) { + m_children.append(object); + m_layout->addLayout(qobject_cast(vboxLayout->object())); } else { // TODO: error unknown type } @@ -229,17 +289,23 @@ QObject* DeclarativeWidget::object() void DeclarativeWidget::dataAppend(QObject *object) { DeclarativeWidget *widget = dynamic_cast(object); - DeclarativeVBoxLayout *layout = dynamic_cast(object); + DeclarativeHBoxLayout *hboxLayout = dynamic_cast(object); + DeclarativeVBoxLayout *vboxLayout = dynamic_cast(object); if (widget) { // TODO: error when layout is set m_children.append(object); qobject_cast(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(layout->object())); + m_children.append(object); + m_widget->setLayout(qobject_cast(hboxLayout->object())); + } else if (vboxLayout) { + // TODO: error when widget is set + + m_children.append(object); + m_widget->setLayout(qobject_cast(vboxLayout->object())); } else { // TODO: error unknown type } diff --git a/declarativeobjects_p.h b/declarativeobjects_p.h index 9a4b3eb..b6cd1d3 100644 --- a/declarativeobjects_p.h +++ b/declarativeobjects_p.h @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -51,6 +52,26 @@ class DeclarativeObject : public QObject static void data_clear(QDeclarativeListProperty *); }; +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 m_layout; + QVector m_children; +}; + class DeclarativeVBoxLayout : public DeclarativeObject { DECLARATIVE_OBJECT diff --git a/declarativewidgetdocument.cpp b/declarativewidgetdocument.cpp index 190ebf0..501df74 100644 --- a/declarativewidgetdocument.cpp +++ b/declarativewidgetdocument.cpp @@ -28,11 +28,13 @@ DeclarativeWidgetDocument::DeclarativeWidgetDocument(const QUrl &url, QObject *p : QObject(parent) , d(new Private(this, url)) { - qmlRegisterType("qtgui.widgets", 1, 0, "Widget"); - qmlRegisterType("qtgui.widgets", 1, 0, "Label"); - qmlRegisterType("qtgui.widgets", 1, 0, "VBoxLayout"); - qmlRegisterType("qtgui.widgets", 1, 0, "TabWidget"); - qmlRegisterType("qtgui.widgets", 1, 0, "PushButton"); + qmlRegisterType("QtGui", 1, 0, "Widget"); + qmlRegisterType("QtGui", 1, 0, "Label"); + qmlRegisterType("QtGui", 1, 0, "HBoxLayout"); + qmlRegisterType("QtGui", 1, 0, "VBoxLayout"); + qmlRegisterType("QtGui", 1, 0, "TabWidget"); + qmlRegisterType("QtGui", 1, 0, "PushButton"); + qmlRegisterType("QtGui", 1, 0, "CheckBox"); d->m_component->loadUrl(d->m_url); if (d->m_component->isError()) { diff --git a/test.qml b/test.qml index 89c732a..b839f1d 100644 --- 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" + } + } } }