1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
7 ** This file is part of the examples of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:BSD$
10 ** You may use this file under the terms of the BSD license as follows:
12 ** "Redistribution and use in source and binary forms, with or without
13 ** modification, are permitted provided that the following conditions are
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
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
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."
39 ****************************************************************************/
41 #include "gridlayout.h"
43 #include <QGraphicsWidget>
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)
53 void GridLayoutAttached::setAlignment(Qt::Alignment a)
55 if (m_alignment != a) {
57 emit alignmentChanged(reinterpret_cast<QGraphicsLayoutItem*>(parent()), m_alignment);
61 QHash<QGraphicsLayoutItem*, GridLayoutAttached*> GraphicsGridLayoutObject::attachedProperties;
63 GraphicsGridLayoutObject::GraphicsGridLayoutObject(QObject *parent)
68 GraphicsGridLayoutObject::~GraphicsGridLayoutObject()
72 void GraphicsGridLayoutObject::addWidget(QGraphicsWidget *widget)
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";
84 addItem(widget, row, column, rowSpan, columnSpan);
88 void GraphicsGridLayoutObject::addLayoutItem(QGraphicsLayoutItem *item)
90 //use attached properties
91 if (GridLayoutAttached *obj = attachedProperties.value(item)) {
93 int column = obj->column();
94 int rowSpan = obj->rowSpan();
95 int columnSpan = obj->columnSpan();
96 Qt::Alignment alignment = obj->alignment();
98 if (row == -1 || column == -1) {
99 qWarning() << "Must set row and column for an item in a grid layout";
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());
128 addItem(item, row, column, rowSpan, columnSpan);
131 setAlignment(item, alignment);
132 QObject::connect(obj, SIGNAL(alignmentChanged(QGraphicsLayoutItem*, Qt::Alignment)),
133 this, SLOT(updateAlignment(QGraphicsLayoutItem*, Qt::Alignment)));
137 void GraphicsGridLayoutObject::removeAt(int index)
139 QGraphicsLayoutItem *item = itemAt(index);
141 GridLayoutAttached *obj = attachedProperties.value(item);
142 obj->disconnect(this);
143 attachedProperties.remove(item);
145 QGraphicsGridLayout::removeAt(index);
148 void GraphicsGridLayoutObject::clearChildren()
150 // do not delete the removed items; they will be deleted by the QML engine
155 qreal GraphicsGridLayoutObject::spacing() const
157 if (verticalSpacing() == horizontalSpacing())
158 return verticalSpacing();
162 qreal GraphicsGridLayoutObject::contentsMargin() const
165 getContentsMargins(&a, &b, &c, &d);
166 if (a == b && a == c && a == d)
171 void GraphicsGridLayoutObject::setContentsMargin(qreal m)
173 setContentsMargins(m, m, m, m);
176 void GraphicsGridLayoutObject::updateAlignment(QGraphicsLayoutItem *item, Qt::Alignment alignment)
178 QGraphicsGridLayout::setAlignment(item, alignment);
181 GridLayoutAttached *GraphicsGridLayoutObject::qmlAttachedProperties(QObject *obj)
183 GridLayoutAttached *rv = new GridLayoutAttached(obj);
184 if (qobject_cast<QGraphicsLayoutItem*>(obj))
185 attachedProperties.insert(qobject_cast<QGraphicsLayoutItem*>(obj), rv);