Add extension base classes
authorKevin Krammer <kevin.krammer@kdab.com>
Fri, 28 Dec 2012 18:55:07 +0000 (19:55 +0100)
committerKevin Krammer <kevin.krammer@kdab.com>
Wed, 9 Jan 2013 11:29:16 +0000 (12:29 +0100)
lib/declarativelayoutextension.cpp [new file with mode: 0644]
lib/declarativelayoutextension.h [new file with mode: 0644]
lib/declarativeobjectextension.cpp [new file with mode: 0644]
lib/declarativeobjectextension.h [new file with mode: 0644]
lib/declarativewidgetextension.cpp [new file with mode: 0644]
lib/declarativewidgetextension.h [new file with mode: 0644]
lib/declarativewidgetsdocument.cpp
lib/lib.pro

diff --git a/lib/declarativelayoutextension.cpp b/lib/declarativelayoutextension.cpp
new file mode 100644 (file)
index 0000000..8b3d70d
--- /dev/null
@@ -0,0 +1,36 @@
+#include "declarativelayoutextension.h"
+
+#include <QLayout>
+#include <QWidget>
+
+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<QLayout*>(parent());
+  Q_ASSERT(parentLayout);
+  Q_UNUSED(parentLayout);
+
+  return parentLayout;
+}
+
+void DeclarativeLayoutExtension::dataAppend(QObject *object)
+{
+  DeclarativeObjectExtension::dataAppend(object);
+
+  QWidget *widget = qobject_cast<QWidget*>(object);
+  if (widget) {
+    addWidget(widget);
+    return;
+  }
+
+  QLayout *layout = qobject_cast<QLayout*>(object);
+  if (layout) {
+    addLayout(layout);
+    return;
+  }
+}
diff --git a/lib/declarativelayoutextension.h b/lib/declarativelayoutextension.h
new file mode 100644 (file)
index 0000000..bcc28d6
--- /dev/null
@@ -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<QObject> 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 (file)
index 0000000..92922d1
--- /dev/null
@@ -0,0 +1,81 @@
+#include "declarativeobjectextension.h"
+
+DeclarativeObjectExtension::DeclarativeObjectExtension(QObject *parent)
+  : QObject(parent)
+{
+}
+
+DeclarativeObjectExtension::~DeclarativeObjectExtension()
+{
+}
+
+QDeclarativeListProperty<QObject> DeclarativeObjectExtension::data()
+{
+  return QDeclarativeListProperty<QObject>(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<QObject> *property, QObject *object)
+{
+  if (!object)
+    return;
+
+  DeclarativeObjectExtension *that = qobject_cast<DeclarativeObjectExtension*>(property->object);
+  if (that)
+    that->dataAppend(object);
+  else
+    qWarning("cast went wrong in data_append");
+}
+
+int DeclarativeObjectExtension::data_count(QDeclarativeListProperty<QObject> *property)
+{
+  DeclarativeObjectExtension *that = qobject_cast<DeclarativeObjectExtension*>(property->object);
+  if (that)
+    return that->dataCount();
+  else {
+    qWarning("cast went wrong in data_count");
+    return 0;
+  }
+}
+
+QObject* DeclarativeObjectExtension::data_at(QDeclarativeListProperty<QObject> *property, int index)
+{
+  DeclarativeObjectExtension *that = qobject_cast<DeclarativeObjectExtension*>(property->object);
+  if (that)
+    return that->dataAt(index);
+  else {
+    qWarning("cast went wrong in data_at");
+    return 0;
+  }
+}
+
+void DeclarativeObjectExtension::data_clear(QDeclarativeListProperty<QObject> *property)
+{
+  DeclarativeObjectExtension *that = qobject_cast<DeclarativeObjectExtension*>(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 (file)
index 0000000..d5e2908
--- /dev/null
@@ -0,0 +1,38 @@
+#ifndef DECLARATIVEOBJECTEXTENSION_H
+#define DECLARATIVEOBJECTEXTENSION_H
+
+#include <QDeclarativeListProperty>
+#include <QObject>
+
+class DeclarativeObjectExtension : public QObject
+{
+  Q_OBJECT
+
+  Q_PROPERTY(QDeclarativeListProperty<QObject> 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<QObject> data();
+
+  private:
+    static void data_append(QDeclarativeListProperty<QObject> *, QObject *);
+    static int data_count(QDeclarativeListProperty<QObject> *);
+    static QObject *data_at(QDeclarativeListProperty<QObject> *, int);
+    static void data_clear(QDeclarativeListProperty<QObject> *);
+};
+
+#endif // DECLARATIVEOBJECTEXTENSION_H
diff --git a/lib/declarativewidgetextension.cpp b/lib/declarativewidgetextension.cpp
new file mode 100644 (file)
index 0000000..f65b164
--- /dev/null
@@ -0,0 +1,63 @@
+#include "declarativewidgetextension.h"
+
+#include <QAction>
+#include <QDeclarativeInfo>
+#include <QLayout>
+#include <QWidget>
+
+DeclarativeWidgetExtension::DeclarativeWidgetExtension(QObject *parent)
+  : DeclarativeObjectExtension(parent)
+{
+}
+
+QWidget *DeclarativeWidgetExtension::extendedWidget() const
+{
+  QWidget *parentWidget = qobject_cast<QWidget*>(parent());
+  Q_ASSERT(parentWidget);
+  Q_UNUSED(parentWidget);
+
+  return parentWidget;
+}
+
+void DeclarativeWidgetExtension::dataAppend(QObject *object)
+{
+  DeclarativeObjectExtension::dataAppend(object);
+
+  QWidget *widget = qobject_cast<QWidget*>(object);
+  if (widget) {
+    addWidget(widget);
+    return;
+  }
+
+  QLayout *layout = qobject_cast<QLayout*>(object);
+  if (layout) {
+    setLayout(layout);
+    return;
+  }
+
+  QAction *action = qobject_cast<QAction*>(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 (file)
index 0000000..d6e3872
--- /dev/null
@@ -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<QObject> 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
index 7040877..6599557 100644 (file)
@@ -220,6 +220,8 @@ QWidget* DeclarativeWidgetsDocument::createWidget()
   AbstractDeclarativeObject *declarativeObject = dynamic_cast<AbstractDeclarativeObject*>(object);
 
   if (!declarativeObject) {
+    QWidget *widget = qobject_cast<QWidget*>(object);
+    if (widget) return widget;
     qWarning("Root element is no AbstractDeclarativeObject subclass");
     return 0;
   }
index 7b21a44..4757812 100644 (file)
@@ -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
-