Add grid layout
authorKevin Krammer <kevin.krammer@kdab.com>
Thu, 18 Oct 2012 13:50:50 +0000 (15:50 +0200)
committerKevin Krammer <kevin.krammer@kdab.com>
Thu, 18 Oct 2012 13:50:50 +0000 (15:50 +0200)
declarativeobjects.cpp
declarativeobjects_p.h
declarativewidgetdocument.cpp
layouts.qml

index 4fabcf8..c78ca58 100644 (file)
@@ -207,7 +207,6 @@ Qt::Alignment DeclarativeBoxLayoutAttached::alignment() const
   return d->alignment;
 }
 
-// DeclarativeFormLayout
 class DeclarativeFormLayoutAttached::Private
 {
   public:
@@ -233,11 +232,104 @@ void DeclarativeFormLayoutAttached::setLabel(const QString &label)
   emit labelChanged(label);
 }
 
+class DeclarativeGridLayoutAttached::Private
+{
+  public:
+    Private() : row(0), column(0), rowSpan(1), columnSpan(1), alignment(0) {}
+
+    int row;
+    int column;
+    int rowSpan;
+    int columnSpan;
+    Qt::Alignment alignment;
+};
+
+DeclarativeGridLayoutAttached::DeclarativeGridLayoutAttached(QObject *parent)
+  : QObject(parent), d(new Private)
+{
+}
+
+DeclarativeGridLayoutAttached::~DeclarativeGridLayoutAttached()
+{
+  delete d;
+}
+
+void DeclarativeGridLayoutAttached::setRow(int row)
+{
+  if (row == d->row)
+    return;
+
+  d->row = row;
+  emit rowChanged(row);
+}
+
+int DeclarativeGridLayoutAttached::row() const
+{
+  return d->row;
+}
+
+void DeclarativeGridLayoutAttached::setColumn(int column)
+{
+  if (column == d->column)
+    return;
+
+  d->column = column;
+  emit columnChanged(column);
+}
+
+int DeclarativeGridLayoutAttached::column() const
+{
+  return d->column;
+}
+
+void DeclarativeGridLayoutAttached::setRowSpan(int rowSpan)
+{
+  if (rowSpan == d->rowSpan)
+    return;
+
+  d->rowSpan = rowSpan;
+  emit rowSpanChanged(rowSpan);
+}
+
+int DeclarativeGridLayoutAttached::rowSpan() const
+{
+  return d->rowSpan;
+}
+
+void DeclarativeGridLayoutAttached::setColumnSpan(int columnSpan)
+{
+  if (columnSpan == d->columnSpan)
+    return;
+
+  d->columnSpan = columnSpan;
+  emit columnSpanChanged(columnSpan);
+}
+
+int DeclarativeGridLayoutAttached::columnSpan() const
+{
+  return d->columnSpan;
+}
+
+void DeclarativeGridLayoutAttached::setAlignment(Qt::Alignment alignment)
+{
+  if (alignment == d->alignment)
+    return;
+
+  d->alignment = alignment;
+  emit alignmentChanged(alignment);
+}
+
+Qt::Alignment DeclarativeGridLayoutAttached::alignment() const
+{
+  return d->alignment;
+}
+
 QString DeclarativeFormLayoutAttached::label() const
 {
   return d->label;
 }
 
+// DeclarativeFormLayout
 DeclarativeFormLayout::DeclarativeFormLayout(QObject *parent) : DeclarativeLayoutProxy<QFormLayout>(parent)
 {
   connectAllSignals(m_proxiedObject, this);
@@ -281,6 +373,63 @@ void DeclarativeFormLayout::addLayout(QLayout *layout, AbstractDeclarativeObject
 
 CUSTOM_METAOBJECT(DeclarativeFormLayout, QFormLayout)
 
+// DeclarativeGridLayout
+DeclarativeGridLayout::DeclarativeGridLayout(QObject *parent) : DeclarativeLayoutProxy<QGridLayout>(parent)
+{
+  connectAllSignals(m_proxiedObject, this);
+}
+
+DeclarativeGridLayoutAttached *DeclarativeGridLayout::qmlAttachedProperties(QObject *parent)
+{
+  return new DeclarativeGridLayoutAttached(parent);
+}
+
+void DeclarativeGridLayout::addWidget(QWidget *widget, AbstractDeclarativeObject *declarativeObject)
+{
+  int row = 0;
+  int column = 0;
+  int rowSpan = 1;
+  int columnSpan = 1;
+  Qt::Alignment alignment = 0;
+
+  QObject *attachedProperties = qmlAttachedPropertiesObject<DeclarativeGridLayout>(declarativeObject, false);
+  DeclarativeGridLayoutAttached *properties = qobject_cast<DeclarativeGridLayoutAttached*>(attachedProperties);
+  if (properties) {
+    row = properties->row();
+    column = properties->column();
+    rowSpan = properties->rowSpan();
+    columnSpan = properties->columnSpan();
+    alignment = properties->alignment();
+  }
+
+  m_proxiedObject->addWidget(widget, row, column, rowSpan, columnSpan, alignment);
+  m_children.append(declarativeObject);
+}
+
+void DeclarativeGridLayout::addLayout(QLayout *layout, AbstractDeclarativeObject *declarativeObject)
+{
+  int row = 0;
+  int column = 0;
+  int rowSpan = 1;
+  int columnSpan = 1;
+  Qt::Alignment alignment = 0;
+
+  QObject *attachedProperties = qmlAttachedPropertiesObject<DeclarativeGridLayout>(declarativeObject, false);
+  DeclarativeGridLayoutAttached *properties = qobject_cast<DeclarativeGridLayoutAttached*>(attachedProperties);
+  if (properties) {
+    row = properties->row();
+    column = properties->column();
+    rowSpan = properties->rowSpan();
+    columnSpan = properties->columnSpan();
+    alignment = properties->alignment();
+  }
+
+  m_proxiedObject->addLayout(layout, row, column, rowSpan, columnSpan, alignment);
+  m_children.append(declarativeObject);
+}
+
+CUSTOM_METAOBJECT(DeclarativeGridLayout, QGridLayout)
+
 // DeclarativeHBoxLayout
 DeclarativeHBoxLayout::DeclarativeHBoxLayout(QObject *parent) : DeclarativeLayoutProxy<QHBoxLayout>(parent)
 {
index 74de13d..353f165 100644 (file)
@@ -195,11 +195,7 @@ class DeclarativeLayoutProxy : public DeclarativeObjectProxy<T>
       DeclarativeObjectProxy<T>::m_proxiedObject->addWidget(widget);
     }
 
-    virtual void addLayout(QLayout *layout, AbstractDeclarativeObject *declarativeObject)
-    {
-      DeclarativeObjectProxy<T>::m_children.append(declarativeObject);
-      DeclarativeObjectProxy<T>::m_proxiedObject->addItem(layout);
-    }
+    virtual void addLayout(QLayout *layout, AbstractDeclarativeObject *declarativeObject) = 0;
 };
 
 //// Objects ///
@@ -267,6 +263,47 @@ class DeclarativeFormLayoutAttached : public QObject
     Private *const d;
 };
 
+class DeclarativeGridLayoutAttached : public QObject
+{
+  Q_OBJECT
+
+  Q_PROPERTY(int row READ row WRITE setRow NOTIFY rowChanged)
+  Q_PROPERTY(int column READ column WRITE setColumn NOTIFY columnChanged)
+  Q_PROPERTY(int rowSpan READ rowSpan WRITE setRowSpan NOTIFY rowSpanChanged)
+  Q_PROPERTY(int columnSpan READ columnSpan WRITE setColumnSpan NOTIFY columnSpanChanged)
+  Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment NOTIFY alignmentChanged)
+
+  public:
+    DeclarativeGridLayoutAttached(QObject *parent = 0);
+    ~DeclarativeGridLayoutAttached();
+
+    void setRow(int row);
+    int row() const;
+
+    void setColumn(int column);
+    int column() const;
+
+    void setRowSpan(int rowSpan);
+    int rowSpan() const;
+
+    void setColumnSpan(int columnSpan);
+    int columnSpan() const;
+
+    void setAlignment(Qt::Alignment alignment);
+    Qt::Alignment alignment() const;
+
+  Q_SIGNALS:
+    void rowChanged(int row);
+    void columnChanged(int column);
+    void rowSpanChanged(int rowSpan);
+    void columnSpanChanged(int columnSpan);
+    void alignmentChanged(Qt::Alignment alignment);
+
+  private:
+    class Private;
+    Private *const d;
+};
+
 class DeclarativeFormLayout : public DeclarativeLayoutProxy<QFormLayout>
 {
   DECLARATIVE_OBJECT
@@ -283,6 +320,22 @@ class DeclarativeFormLayout : public DeclarativeLayoutProxy<QFormLayout>
 
 QML_DECLARE_TYPEINFO(DeclarativeFormLayout, QML_HAS_ATTACHED_PROPERTIES)
 
+class DeclarativeGridLayout : public DeclarativeLayoutProxy<QGridLayout>
+{
+  DECLARATIVE_OBJECT
+
+  public:
+    DeclarativeGridLayout(QObject *parent = 0);
+
+    static DeclarativeGridLayoutAttached *qmlAttachedProperties(QObject *parent);
+
+  protected:
+    void addWidget(QWidget *widget, AbstractDeclarativeObject *declarativeObject);
+    void addLayout(QLayout *layout, AbstractDeclarativeObject *declarativeObject);
+};
+
+QML_DECLARE_TYPEINFO(DeclarativeGridLayout, QML_HAS_ATTACHED_PROPERTIES)
+
 class DeclarativeHBoxLayout : public DeclarativeLayoutProxy<QHBoxLayout>
 {
   DECLARATIVE_OBJECT
index 3d88ece..b2f3e94 100644 (file)
@@ -37,7 +37,9 @@ DeclarativeWidgetDocument::DeclarativeWidgetDocument(const QUrl &url, QObject *p
   // layouts
   qmlRegisterType<DeclarativeBoxLayoutAttached>();
   qmlRegisterType<DeclarativeFormLayoutAttached>();
+  qmlRegisterType<DeclarativeGridLayoutAttached>();
   qmlRegisterType<DeclarativeFormLayout>("QtGui", 1, 0, "FormLayout");
+  qmlRegisterType<DeclarativeGridLayout>("QtGui", 1, 0, "GridLayout");
   qmlRegisterType<DeclarativeHBoxLayout>("QtGui", 1, 0, "HBoxLayout");
   qmlRegisterType<DeclarativeVBoxLayout>("QtGui", 1, 0, "VBoxLayout");
 
index 0417c03..259d537 100644 (file)
@@ -100,4 +100,55 @@ TabWidget {
       }
     }
   }
+  Widget {
+    TabWidget.label: "Grid Layout"
+
+    GridLayout {
+      PushButton {
+        GridLayout.row: 0
+        GridLayout.column: 0
+
+        text: "0/0"
+      }
+      PushButton {
+        GridLayout.row: 0
+        GridLayout.column: 2
+
+        text: "0/2"
+      }
+      PushButton {
+        GridLayout.row: 1
+        GridLayout.column: 0
+
+        text: "1/0"
+      }
+      PushButton {
+        GridLayout.row: 1
+        GridLayout.column: 1
+        GridLayout.columnSpan: 2
+
+        text: "1/1, 1/2"
+      }
+      TextEdit {
+        GridLayout.row: 2
+        GridLayout.column: 0
+        GridLayout.columnSpan: 2
+        GridLayout.rowSpan: 2
+
+        plainText: "2/0, 2/2"
+      }
+      PushButton {
+        GridLayout.row: 2
+        GridLayout.column: 2
+
+        text: "2/2"
+      }
+      PushButton {
+        GridLayout.row: 3
+        GridLayout.column: 2
+
+        text: "3/2"
+      }
+    }
+  }
 }