Initial import from the monolithic Qt.
[profile/ivi/qtdeclarative.git] / examples / declarative / cppextensions / qgraphicslayouts / qgraphicsgridlayout / gridlayout.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the examples of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:BSD$
10 ** You may use this file under the terms of the BSD license as follows:
11 **
12 ** "Redistribution and use in source and binary forms, with or without
13 ** modification, are permitted provided that the following conditions are
14 ** met:
15 **   * Redistributions of source code must retain the above copyright
16 **     notice, this list of conditions and the following disclaimer.
17 **   * Redistributions in binary form must reproduce the above copyright
18 **     notice, this list of conditions and the following disclaimer in
19 **     the documentation and/or other materials provided with the
20 **     distribution.
21 **   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
22 **     the names of its contributors may be used to endorse or promote
23 **     products derived from this software without specific prior written
24 **     permission.
25 **
26 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40
41 #include "gridlayout.h"
42
43 #include <QGraphicsWidget>
44 #include <QDebug>
45
46 GridLayoutAttached::GridLayoutAttached(QObject *parent)
47 : QObject(parent), m_row(-1), m_column(-1), m_rowspan(1), m_colspan(1), m_alignment(-1), m_rowStretch(-1),
48     m_colStretch(-1), m_rowSpacing(-1), m_colSpacing(-1), m_rowPrefHeight(-1), m_rowMaxHeight(-1), m_rowMinHeight(-1),
49     m_rowFixHeight(-1), m_colPrefwidth(-1), m_colMaxwidth(-1), m_colMinwidth(-1), m_colFixwidth(-1)
50 {
51 }
52
53 void GridLayoutAttached::setAlignment(Qt::Alignment a)
54 {
55     if (m_alignment != a) {
56         m_alignment = a;
57         emit alignmentChanged(reinterpret_cast<QGraphicsLayoutItem*>(parent()), m_alignment);
58     }
59 }
60
61 QHash<QGraphicsLayoutItem*, GridLayoutAttached*> GraphicsGridLayoutObject::attachedProperties;
62
63 GraphicsGridLayoutObject::GraphicsGridLayoutObject(QObject *parent)
64 : QObject(parent)
65 {
66 }
67
68 GraphicsGridLayoutObject::~GraphicsGridLayoutObject()
69 {
70 }
71
72 void GraphicsGridLayoutObject::addWidget(QGraphicsWidget *widget)
73 {
74     //use attached properties
75     if (QObject *obj = attachedProperties.value(qobject_cast<QGraphicsLayoutItem*>(widget))) {
76         int row = static_cast<GridLayoutAttached *>(obj)->row();
77         int column = static_cast<GridLayoutAttached *>(obj)->column();
78         int rowSpan = static_cast<GridLayoutAttached *>(obj)->rowSpan();
79         int columnSpan = static_cast<GridLayoutAttached *>(obj)->columnSpan();
80         if (row == -1 || column == -1) {
81             qWarning() << "Must set row and column for an item in a grid layout";
82             return;
83         }
84         addItem(widget, row, column, rowSpan, columnSpan);
85     }
86 }
87
88 void GraphicsGridLayoutObject::addLayoutItem(QGraphicsLayoutItem *item)
89 {
90     //use attached properties
91     if (GridLayoutAttached *obj = attachedProperties.value(item)) {
92         int row = obj->row();
93         int column = obj->column();
94         int rowSpan = obj->rowSpan();
95         int columnSpan = obj->columnSpan();
96         Qt::Alignment alignment = obj->alignment();
97
98         if (row == -1 || column == -1) {
99             qWarning() << "Must set row and column for an item in a grid layout";
100             return;
101         }
102
103         if (obj->rowSpacing() != -1)
104             setRowSpacing(row, obj->rowSpacing());
105         if (obj->columnSpacing() != -1)
106             setColumnSpacing(column, obj->columnSpacing());
107         if (obj->rowStretchFactor() != -1)
108             setRowStretchFactor(row, obj->rowStretchFactor());
109         if (obj->columnStretchFactor() != -1)
110             setColumnStretchFactor(column, obj->columnStretchFactor());
111         if (obj->rowPreferredHeight() != -1)
112             setRowPreferredHeight(row, obj->rowPreferredHeight());
113         if (obj->rowMaximumHeight() != -1)
114             setRowMaximumHeight(row, obj->rowMaximumHeight());
115         if (obj->rowMinimumHeight() != -1)
116             setRowMinimumHeight(row, obj->rowMinimumHeight());
117         if (obj->rowFixedHeight() != -1)
118             setRowFixedHeight(row, obj->rowFixedHeight());
119         if (obj->columnPreferredWidth() != -1)
120             setColumnPreferredWidth(row, obj->columnPreferredWidth());
121         if (obj->columnMaximumWidth() != -1)
122             setColumnMaximumWidth(row, obj->columnMaximumWidth());
123         if (obj->columnMinimumWidth() != -1)
124             setColumnMinimumWidth(row, obj->columnMinimumWidth());
125         if (obj->columnFixedWidth() != -1)
126             setColumnFixedWidth(row, obj->columnFixedWidth());
127
128         addItem(item, row, column, rowSpan, columnSpan);
129
130         if (alignment != -1)
131             setAlignment(item, alignment);
132         QObject::connect(obj, SIGNAL(alignmentChanged(QGraphicsLayoutItem*, Qt::Alignment)),
133                          this, SLOT(updateAlignment(QGraphicsLayoutItem*, Qt::Alignment)));
134     }
135 }
136
137 void GraphicsGridLayoutObject::removeAt(int index)
138 {
139     QGraphicsLayoutItem *item = itemAt(index);
140     if (item) {
141         GridLayoutAttached *obj = attachedProperties.value(item);
142         obj->disconnect(this);
143         attachedProperties.remove(item);
144     }
145     QGraphicsGridLayout::removeAt(index);
146 }
147
148 void GraphicsGridLayoutObject::clearChildren()
149 {
150     // do not delete the removed items; they will be deleted by the QML engine
151     while (count() > 0)
152         removeAt(count()-1);
153 }
154
155 qreal GraphicsGridLayoutObject::spacing() const
156 {
157     if (verticalSpacing() == horizontalSpacing())
158         return verticalSpacing();
159     return -1; 
160 }
161
162 qreal GraphicsGridLayoutObject::contentsMargin() const
163 {
164     qreal a, b, c, d;
165     getContentsMargins(&a, &b, &c, &d);
166     if (a == b && a == c && a == d)
167         return a;
168     return -1;
169 }
170
171 void GraphicsGridLayoutObject::setContentsMargin(qreal m)
172 {
173     setContentsMargins(m, m, m, m);
174 }
175
176 void GraphicsGridLayoutObject::updateAlignment(QGraphicsLayoutItem *item, Qt::Alignment alignment)
177 {
178     QGraphicsGridLayout::setAlignment(item, alignment);
179 }
180
181 GridLayoutAttached *GraphicsGridLayoutObject::qmlAttachedProperties(QObject *obj)
182 {
183     GridLayoutAttached *rv = new GridLayoutAttached(obj);
184     if (qobject_cast<QGraphicsLayoutItem*>(obj))
185         attachedProperties.insert(qobject_cast<QGraphicsLayoutItem*>(obj), rv);
186     return rv;
187 }
188