From df4efd43df35a6b740731630f9401de789190b19 Mon Sep 17 00:00:00 2001 From: Kevin Krammer Date: Fri, 28 Dec 2012 19:55:07 +0100 Subject: [PATCH] Add extension base classes --- lib/declarativelayoutextension.cpp | 36 ++++++++++++++++ lib/declarativelayoutextension.h | 30 +++++++++++++ lib/declarativeobjectextension.cpp | 81 ++++++++++++++++++++++++++++++++++++ lib/declarativeobjectextension.h | 38 +++++++++++++++++ lib/declarativewidgetextension.cpp | 63 ++++++++++++++++++++++++++++ lib/declarativewidgetextension.h | 32 ++++++++++++++ lib/declarativewidgetsdocument.cpp | 2 + lib/lib.pro | 7 +++- 8 files changed, 288 insertions(+), 1 deletions(-) create mode 100644 lib/declarativelayoutextension.cpp create mode 100644 lib/declarativelayoutextension.h create mode 100644 lib/declarativeobjectextension.cpp create mode 100644 lib/declarativeobjectextension.h create mode 100644 lib/declarativewidgetextension.cpp create mode 100644 lib/declarativewidgetextension.h diff --git a/lib/declarativelayoutextension.cpp b/lib/declarativelayoutextension.cpp new file mode 100644 index 0000000..8b3d70d --- /dev/null +++ b/lib/declarativelayoutextension.cpp @@ -0,0 +1,36 @@ +#include "declarativelayoutextension.h" + +#include +#include + +DeclarativeLayoutExtension::DeclarativeLayoutExtension(QObject *parent) + : DeclarativeObjectExtension(parent) +{ + parent->setParent(0); // otherwise first call to parentWidget() will complain about wrong parent class +} + +QLayout *DeclarativeLayoutExtension::extendedLayout() const +{ + QLayout *parentLayout = qobject_cast(parent()); + Q_ASSERT(parentLayout); + Q_UNUSED(parentLayout); + + return parentLayout; +} + +void DeclarativeLayoutExtension::dataAppend(QObject *object) +{ + DeclarativeObjectExtension::dataAppend(object); + + QWidget *widget = qobject_cast(object); + if (widget) { + addWidget(widget); + return; + } + + QLayout *layout = qobject_cast(object); + if (layout) { + addLayout(layout); + return; + } +} diff --git a/lib/declarativelayoutextension.h b/lib/declarativelayoutextension.h new file mode 100644 index 0000000..bcc28d6 --- /dev/null +++ b/lib/declarativelayoutextension.h @@ -0,0 +1,30 @@ +#ifndef DECLARATIVELAYOUTEXTENSION_H +#define DECLARATIVELAYOUTEXTENSION_H + +#include "declarativeobjectextension.h" + +class QLayout; +class QWidget; + +class DeclarativeLayoutExtension : public DeclarativeObjectExtension +{ + Q_OBJECT + + // repeat property declarations, qmlRegisterExtendedType doesn't see the ones from base class + Q_PROPERTY(QDeclarativeListProperty data READ data DESIGNABLE false) + + Q_CLASSINFO("DefaultProperty", "data") + + public: + DeclarativeLayoutExtension(QObject *parent); + + QLayout *extendedLayout() const; + + protected: + void dataAppend(QObject *object); + + virtual void addLayout(QLayout *layout) = 0; + virtual void addWidget(QWidget *widget) = 0; +}; + +#endif // DECLARATIVELAYOUTEXTENSION_H diff --git a/lib/declarativeobjectextension.cpp b/lib/declarativeobjectextension.cpp new file mode 100644 index 0000000..92922d1 --- /dev/null +++ b/lib/declarativeobjectextension.cpp @@ -0,0 +1,81 @@ +#include "declarativeobjectextension.h" + +DeclarativeObjectExtension::DeclarativeObjectExtension(QObject *parent) + : QObject(parent) +{ +} + +DeclarativeObjectExtension::~DeclarativeObjectExtension() +{ +} + +QDeclarativeListProperty DeclarativeObjectExtension::data() +{ + return QDeclarativeListProperty(this, 0, DeclarativeObjectExtension::data_append, + DeclarativeObjectExtension::data_count, + DeclarativeObjectExtension::data_at, + DeclarativeObjectExtension::data_clear); +} + +void DeclarativeObjectExtension::dataAppend(QObject *object) +{ + m_children.append(object); +} + +int DeclarativeObjectExtension::dataCount() const +{ + return m_children.count(); +} + +QObject* DeclarativeObjectExtension::dataAt(int index) const +{ + return m_children.at(index); +} + +void DeclarativeObjectExtension::dataClear() +{ + m_children.clear(); +} + +void DeclarativeObjectExtension::data_append(QDeclarativeListProperty *property, QObject *object) +{ + if (!object) + return; + + DeclarativeObjectExtension *that = qobject_cast(property->object); + if (that) + that->dataAppend(object); + else + qWarning("cast went wrong in data_append"); +} + +int DeclarativeObjectExtension::data_count(QDeclarativeListProperty *property) +{ + DeclarativeObjectExtension *that = qobject_cast(property->object); + if (that) + return that->dataCount(); + else { + qWarning("cast went wrong in data_count"); + return 0; + } +} + +QObject* DeclarativeObjectExtension::data_at(QDeclarativeListProperty *property, int index) +{ + DeclarativeObjectExtension *that = qobject_cast(property->object); + if (that) + return that->dataAt(index); + else { + qWarning("cast went wrong in data_at"); + return 0; + } +} + +void DeclarativeObjectExtension::data_clear(QDeclarativeListProperty *property) +{ + DeclarativeObjectExtension *that = qobject_cast(property->object); + if (that) + that->dataClear(); + else + qWarning("cast went wrong in data_clear"); +} diff --git a/lib/declarativeobjectextension.h b/lib/declarativeobjectextension.h new file mode 100644 index 0000000..d5e2908 --- /dev/null +++ b/lib/declarativeobjectextension.h @@ -0,0 +1,38 @@ +#ifndef DECLARATIVEOBJECTEXTENSION_H +#define DECLARATIVEOBJECTEXTENSION_H + +#include +#include + +class DeclarativeObjectExtension : public QObject +{ + Q_OBJECT + + Q_PROPERTY(QDeclarativeListProperty data READ data DESIGNABLE false) + + Q_CLASSINFO("DefaultProperty", "data") + + public: + explicit DeclarativeObjectExtension(QObject *parent = 0); + ~DeclarativeObjectExtension(); + + QObject *extendedObject() const { return parent(); } + + protected: + QObjectList m_children; + + virtual void dataAppend(QObject *object); + virtual int dataCount() const; + virtual QObject *dataAt(int index) const; + virtual void dataClear(); + + QDeclarativeListProperty data(); + + private: + static void data_append(QDeclarativeListProperty *, QObject *); + static int data_count(QDeclarativeListProperty *); + static QObject *data_at(QDeclarativeListProperty *, int); + static void data_clear(QDeclarativeListProperty *); +}; + +#endif // DECLARATIVEOBJECTEXTENSION_H diff --git a/lib/declarativewidgetextension.cpp b/lib/declarativewidgetextension.cpp new file mode 100644 index 0000000..f65b164 --- /dev/null +++ b/lib/declarativewidgetextension.cpp @@ -0,0 +1,63 @@ +#include "declarativewidgetextension.h" + +#include +#include +#include +#include + +DeclarativeWidgetExtension::DeclarativeWidgetExtension(QObject *parent) + : DeclarativeObjectExtension(parent) +{ +} + +QWidget *DeclarativeWidgetExtension::extendedWidget() const +{ + QWidget *parentWidget = qobject_cast(parent()); + Q_ASSERT(parentWidget); + Q_UNUSED(parentWidget); + + return parentWidget; +} + +void DeclarativeWidgetExtension::dataAppend(QObject *object) +{ + DeclarativeObjectExtension::dataAppend(object); + + QWidget *widget = qobject_cast(object); + if (widget) { + addWidget(widget); + return; + } + + QLayout *layout = qobject_cast(object); + if (layout) { + setLayout(layout); + return; + } + + QAction *action = qobject_cast(object); + if (action) { + addAction(action); + return; + } +} + +void DeclarativeWidgetExtension::addAction(QAction *action) +{ + extendedWidget()->addAction(action); +} + +void DeclarativeWidgetExtension::setLayout(QLayout *layout) +{ + if (extendedWidget()->layout()){ + qmlInfo(this) << "Cannot add a second layout"; + return; + } + + extendedWidget()->setLayout(layout); +} + +void DeclarativeWidgetExtension::addWidget(QWidget *widget) +{ + widget->setParent(extendedWidget()); +} diff --git a/lib/declarativewidgetextension.h b/lib/declarativewidgetextension.h new file mode 100644 index 0000000..d6e3872 --- /dev/null +++ b/lib/declarativewidgetextension.h @@ -0,0 +1,32 @@ +#ifndef DECLARATIVEWIDGETEXTENSION_H +#define DECLARATIVEWIDGETEXTENSION_H + +#include "declarativeobjectextension.h" + +class QAction; +class QLayout; +class QWidget; + +class DeclarativeWidgetExtension : public DeclarativeObjectExtension +{ + Q_OBJECT + + // repeat property declarations, qmlRegisterExtendedType doesn't see the ones from base class + Q_PROPERTY(QDeclarativeListProperty data READ data DESIGNABLE false) + + Q_CLASSINFO("DefaultProperty", "data") + + public: + DeclarativeWidgetExtension(QObject *parent); + + QWidget *extendedWidget() const; + + protected: + void dataAppend(QObject *object); + + virtual void addAction(QAction *action); + virtual void setLayout(QLayout *layout); + virtual void addWidget(QWidget *widget); +}; + +#endif // DECLARATIVEWIDGETEXTENSION_H diff --git a/lib/declarativewidgetsdocument.cpp b/lib/declarativewidgetsdocument.cpp index 7040877..6599557 100644 --- a/lib/declarativewidgetsdocument.cpp +++ b/lib/declarativewidgetsdocument.cpp @@ -220,6 +220,8 @@ QWidget* DeclarativeWidgetsDocument::createWidget() AbstractDeclarativeObject *declarativeObject = dynamic_cast(object); if (!declarativeObject) { + QWidget *widget = qobject_cast(object); + if (widget) return widget; qWarning("Root element is no AbstractDeclarativeObject subclass"); return 0; } diff --git a/lib/lib.pro b/lib/lib.pro index 7b21a44..4757812 100644 --- a/lib/lib.pro +++ b/lib/lib.pro @@ -32,6 +32,7 @@ HEADERS = \ declarativehboxlayout_p.h \ declarativeinputdialog_p.h \ declarativelabel_p.h \ + declarativelayoutextension.h \ declarativelayoutproxy_p.h \ declarativelcdnumber_p.h \ declarativelineedit_p.h \ @@ -40,6 +41,7 @@ HEADERS = \ declarativemenubar_p.h \ declarativemenu_p.h \ declarativemessagebox_p.h \ + declarativeobjectextension.h \ declarativeobjectproxy_p.h \ declarativeplaintextedit_p.h \ declarativeprogressbar_p.h \ @@ -63,6 +65,7 @@ HEADERS = \ declarativetreeview_p.h \ declarativevboxlayout_p.h \ declarativewebview_p.h \ + declarativewidgetextension.h \ declarativewidget_p.h \ declarativewidgetproxy_p.h \ declarativewidgetsdocument.h \ @@ -98,6 +101,7 @@ SOURCES = \ declarativehboxlayout.cpp \ declarativeinputdialog.cpp \ declarativelabel.cpp \ + declarativelayoutextension.cpp \ declarativelcdnumber.cpp \ declarativelineedit.cpp \ declarativelistview.cpp \ @@ -105,6 +109,7 @@ SOURCES = \ declarativemenubar.cpp \ declarativemenu.cpp \ declarativemessagebox.cpp \ + declarativeobjectextension.cpp \ declarativeplaintextedit.cpp \ declarativeprogressbar.cpp \ declarativepushbutton.cpp \ @@ -128,8 +133,8 @@ SOURCES = \ declarativevboxlayout.cpp \ declarativewebview.cpp \ declarativewidget.cpp \ + declarativewidgetextension.cpp \ declarativewidgetsdocument.cpp \ objectadaptors.cpp \ qmetaobjectbuilder.cpp \ staticdialogmethodattached.cpp - -- 1.7.2.5