1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the examples of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
10 **
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
14 **   * Redistributions of source code must retain the above copyright
15 **     notice, this list of conditions and the following disclaimer.
16 **   * Redistributions in binary form must reproduce the above copyright
17 **     notice, this list of conditions and the following disclaimer in
18 **     the documentation and/or other materials provided with the
19 **     distribution.
20 **   * Neither the name of The Qt Company Ltd nor the names of its
21 **     contributors may be used to endorse or promote products derived
22 **     from this software without specific prior written permission.
23 **
24 **
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40 
41 #ifndef GRIDLAYOUT_H
42 #define GRIDLAYOUT_H
43 
44 #include <qdeclarative.h>
45 
46 #include <QGraphicsGridLayout>
47 #include <QGraphicsLayoutItem>
48 
49 class GridLayoutAttached;
50 class GraphicsGridLayoutObject : public QObject, public QGraphicsGridLayout
51 {
52     Q_OBJECT
53     Q_INTERFACES(QGraphicsLayout QGraphicsLayoutItem)
54 
55     Q_PROPERTY(QDeclarativeListProperty<QGraphicsLayoutItem> children READ children)
56     Q_PROPERTY(qreal spacing READ spacing WRITE setSpacing)
57     Q_PROPERTY(qreal contentsMargin READ contentsMargin WRITE setContentsMargin)
58     Q_PROPERTY(qreal verticalSpacing READ verticalSpacing WRITE setVerticalSpacing)
59     Q_PROPERTY(qreal horizontalSpacing READ horizontalSpacing WRITE setHorizontalSpacing)
60     Q_CLASSINFO("DefaultProperty", "children")
61 
62 public:
63     GraphicsGridLayoutObject(QObject * = 0);
64     ~GraphicsGridLayoutObject();
65 
children()66     QDeclarativeListProperty<QGraphicsLayoutItem> children() { return QDeclarativeListProperty<QGraphicsLayoutItem>(this, 0, children_append, children_count, children_at, children_clear); }
67 
68     qreal spacing() const;
69     qreal contentsMargin() const;
70     void setContentsMargin(qreal);
71 
72     void removeAt(int index);
73 
74     static GridLayoutAttached *qmlAttachedProperties(QObject *);
75 
76 private slots:
77     void updateAlignment(QGraphicsLayoutItem*,Qt::Alignment);
78 
79 private:
80     friend class GraphicsLayoutAttached;
81 
82     void addWidget(QGraphicsWidget *);
83     void clearChildren();
84     void addLayoutItem(QGraphicsLayoutItem *);
85 
children_append(QDeclarativeListProperty<QGraphicsLayoutItem> * prop,QGraphicsLayoutItem * item)86     static void children_append(QDeclarativeListProperty<QGraphicsLayoutItem> *prop, QGraphicsLayoutItem *item) {
87         static_cast<GraphicsGridLayoutObject*>(prop->object)->addLayoutItem(item);
88     }
89 
children_clear(QDeclarativeListProperty<QGraphicsLayoutItem> * prop)90     static void children_clear(QDeclarativeListProperty<QGraphicsLayoutItem> *prop) {
91         static_cast<GraphicsGridLayoutObject*>(prop->object)->clearChildren();
92     }
93 
children_count(QDeclarativeListProperty<QGraphicsLayoutItem> * prop)94     static int children_count(QDeclarativeListProperty<QGraphicsLayoutItem> *prop) {
95         return static_cast<GraphicsGridLayoutObject*>(prop->object)->count();
96     }
97 
children_at(QDeclarativeListProperty<QGraphicsLayoutItem> * prop,int index)98     static QGraphicsLayoutItem *children_at(QDeclarativeListProperty<QGraphicsLayoutItem> *prop, int index) {
99         return static_cast<GraphicsGridLayoutObject*>(prop->object)->itemAt(index);
100     }
101 
102     static QHash<QGraphicsLayoutItem*, GridLayoutAttached*> attachedProperties;
103 };
104 
105 
106 class GridLayoutAttached : public QObject
107 {
108     Q_OBJECT
109 
110     Q_PROPERTY(int row READ row WRITE setRow)
111     Q_PROPERTY(int column READ column WRITE setColumn)
112 
113     Q_PROPERTY(int rowSpan READ rowSpan WRITE setRowSpan)
114     Q_PROPERTY(int columnSpan READ columnSpan WRITE setColumnSpan)
115     Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment)
116 
117     Q_PROPERTY(int rowStretchFactor READ rowStretchFactor WRITE setRowStretchFactor)
118     Q_PROPERTY(int columnStretchFactor READ columnStretchFactor WRITE setColumnStretchFactor)
119     Q_PROPERTY(int rowSpacing READ rowSpacing WRITE setRowSpacing)
120     Q_PROPERTY(int columnSpacing READ columnSpacing WRITE setColumnSpacing)
121 
122     Q_PROPERTY(int rowPreferredHeight READ rowPreferredHeight WRITE setRowPreferredHeight)
123     Q_PROPERTY(int rowMinimumHeight READ rowMinimumHeight WRITE setRowMinimumHeight)
124     Q_PROPERTY(int rowMaximumHeight READ rowMaximumHeight WRITE setRowMaximumHeight)
125     Q_PROPERTY(int rowFixedHeight READ rowFixedHeight WRITE setRowFixedHeight)
126 
127     Q_PROPERTY(int columnPreferredWidth READ columnPreferredWidth WRITE setColumnPreferredWidth)
128     Q_PROPERTY(int columnMaximumWidth READ columnMaximumWidth WRITE setColumnMaximumWidth)
129     Q_PROPERTY(int columnMinimumWidth READ columnMinimumWidth WRITE setColumnMinimumWidth)
130     Q_PROPERTY(int columnFixedWidth READ columnFixedWidth WRITE setColumnFixedWidth)
131 
132 public:
133     GridLayoutAttached(QObject *parent);
134 
row()135     int row() const { return m_row; }
setRow(int r)136     void setRow(int r) { m_row = r; }
137 
column()138     int column() const { return m_column; }
setColumn(int c)139     void setColumn(int c) { m_column = c; }
140 
rowSpan()141     int rowSpan() const { return m_rowspan; }
setRowSpan(int rs)142     void setRowSpan(int rs) { m_rowspan = rs; }
143 
columnSpan()144     int columnSpan() const { return m_colspan; }
setColumnSpan(int cs)145     void setColumnSpan(int cs) { m_colspan = cs; }
146 
alignment()147     Qt::Alignment alignment() const { return m_alignment; }
148     void setAlignment(Qt::Alignment a);
149 
rowStretchFactor()150     int rowStretchFactor() const { return m_rowStretch; }
setRowStretchFactor(int f)151     void setRowStretchFactor(int f) { m_rowStretch = f; }
columnStretchFactor()152     int columnStretchFactor() const { return m_colStretch; }
setColumnStretchFactor(int f)153     void setColumnStretchFactor(int f) { m_colStretch = f; }
154 
rowSpacing()155     int rowSpacing() const { return m_rowSpacing; }
setRowSpacing(int s)156     void setRowSpacing(int s) { m_rowSpacing = s; }
columnSpacing()157     int columnSpacing() const { return m_colSpacing; }
setColumnSpacing(int s)158     void setColumnSpacing(int s) { m_colSpacing = s; }
159 
rowPreferredHeight()160     int rowPreferredHeight() const { return m_rowPrefHeight; }
setRowPreferredHeight(int s)161     void setRowPreferredHeight(int s) { m_rowPrefHeight = s; }
162 
rowMaximumHeight()163     int rowMaximumHeight() const { return m_rowMaxHeight; }
setRowMaximumHeight(int s)164     void setRowMaximumHeight(int s) { m_rowMaxHeight = s; }
165 
rowMinimumHeight()166     int rowMinimumHeight() const { return m_rowMinHeight; }
setRowMinimumHeight(int s)167     void setRowMinimumHeight(int s) { m_rowMinHeight = s; }
168 
rowFixedHeight()169     int rowFixedHeight() const { return m_rowFixHeight; }
setRowFixedHeight(int s)170     void setRowFixedHeight(int s) { m_rowFixHeight = s; }
171 
columnPreferredWidth()172     int columnPreferredWidth() const { return m_colPrefwidth; }
setColumnPreferredWidth(int s)173     void setColumnPreferredWidth(int s) { m_colPrefwidth = s; }
174 
columnMaximumWidth()175     int columnMaximumWidth() const { return m_colMaxwidth; }
setColumnMaximumWidth(int s)176     void setColumnMaximumWidth(int s) { m_colMaxwidth = s; }
177 
columnMinimumWidth()178     int columnMinimumWidth() const { return m_colMinwidth; }
setColumnMinimumWidth(int s)179     void setColumnMinimumWidth(int s) { m_colMinwidth = s; }
180 
columnFixedWidth()181     int columnFixedWidth() const { return m_colFixwidth; }
setColumnFixedWidth(int s)182     void setColumnFixedWidth(int s) { m_colFixwidth = s; }
183 
184 signals:
185     void alignmentChanged(QGraphicsLayoutItem*, Qt::Alignment);
186 
187 private:
188     int m_row;
189     int m_column;
190 
191     int m_rowspan;
192     int m_colspan;
193     Qt::Alignment m_alignment;
194 
195     int m_rowStretch;
196     int m_colStretch;
197     int m_rowSpacing;
198     int m_colSpacing;
199 
200     int m_rowPrefHeight;
201     int m_rowMaxHeight;
202     int m_rowMinHeight;
203     int m_rowFixHeight;
204 
205     int m_colPrefwidth;
206     int m_colMaxwidth;
207     int m_colMinwidth;
208     int m_colFixwidth;
209 };
210 
211 QML_DECLARE_INTERFACE(QGraphicsLayoutItem)
212 QML_DECLARE_INTERFACE(QGraphicsLayout)
213 QML_DECLARE_TYPE(GraphicsGridLayoutObject)
214 QML_DECLARE_TYPEINFO(GraphicsGridLayoutObject, QML_HAS_ATTACHED_PROPERTIES)
215 
216 #endif
217 
218