|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 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 QtDeclarative module 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 "graphicslayouts_p.h" |
|
42 |
|
43 #include <QtGui/qgraphicswidget.h> |
|
44 #include <QtCore/qdebug.h> |
|
45 |
|
46 QT_BEGIN_NAMESPACE |
|
47 |
|
48 LinearLayoutAttached::LinearLayoutAttached(QObject *parent) |
|
49 : QObject(parent), _stretch(1), _alignment(Qt::AlignCenter), _spacing(0) |
|
50 { |
|
51 } |
|
52 |
|
53 void LinearLayoutAttached::setStretchFactor(int f) |
|
54 { |
|
55 if (_stretch == f) |
|
56 return; |
|
57 |
|
58 _stretch = f; |
|
59 emit stretchChanged(reinterpret_cast<QGraphicsLayoutItem*>(parent()), _stretch); |
|
60 } |
|
61 |
|
62 void LinearLayoutAttached::setSpacing(int s) |
|
63 { |
|
64 if (_spacing == s) |
|
65 return; |
|
66 |
|
67 _spacing = s; |
|
68 emit spacingChanged(reinterpret_cast<QGraphicsLayoutItem*>(parent()), _spacing); |
|
69 } |
|
70 |
|
71 void LinearLayoutAttached::setAlignment(Qt::Alignment a) |
|
72 { |
|
73 if (_alignment == a) |
|
74 return; |
|
75 |
|
76 _alignment = a; |
|
77 emit alignmentChanged(reinterpret_cast<QGraphicsLayoutItem*>(parent()), _alignment); |
|
78 } |
|
79 |
|
80 QGraphicsLinearLayoutStretchItemObject::QGraphicsLinearLayoutStretchItemObject(QObject *parent) |
|
81 : QObject(parent) |
|
82 { |
|
83 } |
|
84 |
|
85 QSizeF QGraphicsLinearLayoutStretchItemObject::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const |
|
86 { |
|
87 Q_UNUSED(which); |
|
88 Q_UNUSED(constraint); |
|
89 return QSizeF(); |
|
90 } |
|
91 |
|
92 |
|
93 QGraphicsLinearLayoutObject::QGraphicsLinearLayoutObject(QObject *parent) |
|
94 : QObject(parent) |
|
95 { |
|
96 } |
|
97 |
|
98 QGraphicsLinearLayoutObject::~QGraphicsLinearLayoutObject() |
|
99 { |
|
100 } |
|
101 |
|
102 void QGraphicsLinearLayoutObject::insertLayoutItem(int index, QGraphicsLayoutItem *item) |
|
103 { |
|
104 insertItem(index, item); |
|
105 |
|
106 //connect attached properties |
|
107 if (LinearLayoutAttached *obj = attachedProperties.value(item)) { |
|
108 setStretchFactor(item, obj->stretchFactor()); |
|
109 setAlignment(item, obj->alignment()); |
|
110 updateSpacing(item, obj->spacing()); |
|
111 QObject::connect(obj, SIGNAL(stretchChanged(QGraphicsLayoutItem*,int)), |
|
112 this, SLOT(updateStretch(QGraphicsLayoutItem*,int))); |
|
113 QObject::connect(obj, SIGNAL(alignmentChanged(QGraphicsLayoutItem*,Qt::Alignment)), |
|
114 this, SLOT(updateAlignment(QGraphicsLayoutItem*,Qt::Alignment))); |
|
115 QObject::connect(obj, SIGNAL(spacingChanged(QGraphicsLayoutItem*,int)), |
|
116 this, SLOT(updateSpacing(QGraphicsLayoutItem*,int))); |
|
117 //### need to disconnect when widget is removed? |
|
118 } |
|
119 } |
|
120 |
|
121 //### is there a better way to do this? |
|
122 void QGraphicsLinearLayoutObject::clearChildren() |
|
123 { |
|
124 for (int i = 0; i < count(); ++i) |
|
125 removeAt(i); |
|
126 } |
|
127 |
|
128 qreal QGraphicsLinearLayoutObject::contentsMargin() const |
|
129 { |
|
130 qreal a,b,c,d; |
|
131 getContentsMargins(&a, &b, &c, &d); |
|
132 if(a==b && a==c && a==d) |
|
133 return a; |
|
134 return -1; |
|
135 } |
|
136 |
|
137 void QGraphicsLinearLayoutObject::setContentsMargin(qreal m) |
|
138 { |
|
139 setContentsMargins(m,m,m,m); |
|
140 } |
|
141 |
|
142 void QGraphicsLinearLayoutObject::updateStretch(QGraphicsLayoutItem *item, int stretch) |
|
143 { |
|
144 QGraphicsLinearLayout::setStretchFactor(item, stretch); |
|
145 } |
|
146 |
|
147 void QGraphicsLinearLayoutObject::updateSpacing(QGraphicsLayoutItem* item, int spacing) |
|
148 { |
|
149 for(int i=0; i < count(); i++){ |
|
150 if(itemAt(i) == item){ //I do not see the reverse function, which is why we must loop over all items |
|
151 QGraphicsLinearLayout::setItemSpacing(i, spacing); |
|
152 return; |
|
153 } |
|
154 } |
|
155 } |
|
156 |
|
157 void QGraphicsLinearLayoutObject::updateAlignment(QGraphicsLayoutItem *item, Qt::Alignment alignment) |
|
158 { |
|
159 QGraphicsLinearLayout::setAlignment(item, alignment); |
|
160 } |
|
161 |
|
162 QHash<QGraphicsLayoutItem*, LinearLayoutAttached*> QGraphicsLinearLayoutObject::attachedProperties; |
|
163 LinearLayoutAttached *QGraphicsLinearLayoutObject::qmlAttachedProperties(QObject *obj) |
|
164 { |
|
165 // ### This is not allowed - you must attach to any object |
|
166 if (!qobject_cast<QGraphicsLayoutItem*>(obj)) |
|
167 return 0; |
|
168 LinearLayoutAttached *rv = new LinearLayoutAttached(obj); |
|
169 attachedProperties.insert(qobject_cast<QGraphicsLayoutItem*>(obj), rv); |
|
170 return rv; |
|
171 } |
|
172 |
|
173 ////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
174 // QGraphicsGridLayout-related classes |
|
175 ////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
176 GridLayoutAttached::GridLayoutAttached(QObject *parent) |
|
177 : QObject(parent), _row(-1), _column(-1), _rowspan(1), _colspan(1), _alignment(-1), _rowstretch(-1), |
|
178 _colstretch(-1), _rowspacing(-1), _colspacing(-1), _rowprefheight(-1), _rowmaxheight(-1), _rowminheight(-1), |
|
179 _rowfixheight(-1), _colprefwidth(-1), _colmaxwidth(-1), _colminwidth(-1), _colfixwidth(-1) |
|
180 { |
|
181 } |
|
182 |
|
183 void GridLayoutAttached::setRow(int r) |
|
184 { |
|
185 if (_row == r) |
|
186 return; |
|
187 |
|
188 _row = r; |
|
189 //emit rowChanged(reinterpret_cast<QGraphicsLayoutItem*>(parent()), _row); |
|
190 } |
|
191 |
|
192 void GridLayoutAttached::setColumn(int c) |
|
193 { |
|
194 if (_column == c) |
|
195 return; |
|
196 |
|
197 _column = c; |
|
198 //emit columnChanged(reinterpret_cast<QGraphicsLayoutItem*>(parent()), _column); |
|
199 } |
|
200 |
|
201 void GridLayoutAttached::setRowSpan(int rs) |
|
202 { |
|
203 if (_rowspan == rs) |
|
204 return; |
|
205 |
|
206 _rowspan = rs; |
|
207 //emit rowSpanChanged(reinterpret_cast<QGraphicsLayoutItem*>(parent()), _rowSpan); |
|
208 } |
|
209 |
|
210 void GridLayoutAttached::setColumnSpan(int cs) |
|
211 { |
|
212 if (_colspan == cs) |
|
213 return; |
|
214 |
|
215 _colspan = cs; |
|
216 //emit columnSpanChanged(reinterpret_cast<QGraphicsLayoutItem*>(parent()), _columnSpan); |
|
217 } |
|
218 |
|
219 void GridLayoutAttached::setAlignment(Qt::Alignment a) |
|
220 { |
|
221 if (_alignment == a) |
|
222 return; |
|
223 |
|
224 _alignment = a; |
|
225 emit alignmentChanged(reinterpret_cast<QGraphicsLayoutItem*>(parent()), _alignment); |
|
226 } |
|
227 |
|
228 void GridLayoutAttached::setRowStretchFactor(int f) |
|
229 { |
|
230 _rowstretch = f; |
|
231 } |
|
232 |
|
233 void GridLayoutAttached::setColumnStretchFactor(int f) |
|
234 { |
|
235 _colstretch = f; |
|
236 } |
|
237 |
|
238 void GridLayoutAttached::setRowSpacing(int s) |
|
239 { |
|
240 _rowspacing = s; |
|
241 } |
|
242 |
|
243 void GridLayoutAttached::setColumnSpacing(int s) |
|
244 { |
|
245 _colspacing = s; |
|
246 } |
|
247 |
|
248 |
|
249 QGraphicsGridLayoutObject::QGraphicsGridLayoutObject(QObject *parent) |
|
250 : QObject(parent) |
|
251 { |
|
252 } |
|
253 |
|
254 QGraphicsGridLayoutObject::~QGraphicsGridLayoutObject() |
|
255 { |
|
256 } |
|
257 |
|
258 void QGraphicsGridLayoutObject::addWidget(QGraphicsWidget *wid) |
|
259 { |
|
260 //use attached properties |
|
261 if (QObject *obj = attachedProperties.value(qobject_cast<QGraphicsLayoutItem*>(wid))) { |
|
262 int row = static_cast<GridLayoutAttached *>(obj)->row(); |
|
263 int column = static_cast<GridLayoutAttached *>(obj)->column(); |
|
264 int rowSpan = static_cast<GridLayoutAttached *>(obj)->rowSpan(); |
|
265 int columnSpan = static_cast<GridLayoutAttached *>(obj)->columnSpan(); |
|
266 if (row == -1 || column == -1) { |
|
267 qWarning() << "Must set row and column for an item in a grid layout"; |
|
268 return; |
|
269 } |
|
270 addItem(wid, row, column, rowSpan, columnSpan); |
|
271 } |
|
272 } |
|
273 |
|
274 void QGraphicsGridLayoutObject::addLayoutItem(QGraphicsLayoutItem *item) |
|
275 { |
|
276 //use attached properties |
|
277 if (GridLayoutAttached *obj = attachedProperties.value(item)) { |
|
278 int row = obj->row(); |
|
279 int column = obj->column(); |
|
280 int rowSpan = obj->rowSpan(); |
|
281 int columnSpan = obj->columnSpan(); |
|
282 Qt::Alignment alignment = obj->alignment(); |
|
283 if (row == -1 || column == -1) { |
|
284 qWarning() << "Must set row and column for an item in a grid layout"; |
|
285 return; |
|
286 } |
|
287 if(obj->rowSpacing() != -1) |
|
288 setRowSpacing(row, obj->rowSpacing()); |
|
289 if(obj->columnSpacing() != -1) |
|
290 setColumnSpacing(column, obj->columnSpacing()); |
|
291 if(obj->rowStretchFactor() != -1) |
|
292 setRowStretchFactor(row, obj->rowStretchFactor()); |
|
293 if(obj->columnStretchFactor() != -1) |
|
294 setColumnStretchFactor(column, obj->columnStretchFactor()); |
|
295 if(obj->rowPreferredHeight() != -1) |
|
296 setRowPreferredHeight(row, obj->rowPreferredHeight()); |
|
297 if(obj->rowMaximumHeight() != -1) |
|
298 setRowMaximumHeight(row, obj->rowMaximumHeight()); |
|
299 if(obj->rowMinimumHeight() != -1) |
|
300 setRowMinimumHeight(row, obj->rowMinimumHeight()); |
|
301 if(obj->rowFixedHeight() != -1) |
|
302 setRowFixedHeight(row, obj->rowFixedHeight()); |
|
303 if(obj->columnPreferredWidth() != -1) |
|
304 setColumnPreferredWidth(row, obj->columnPreferredWidth()); |
|
305 if(obj->columnMaximumWidth() != -1) |
|
306 setColumnMaximumWidth(row, obj->columnMaximumWidth()); |
|
307 if(obj->columnMinimumWidth() != -1) |
|
308 setColumnMinimumWidth(row, obj->columnMinimumWidth()); |
|
309 if(obj->columnFixedWidth() != -1) |
|
310 setColumnFixedWidth(row, obj->columnFixedWidth()); |
|
311 addItem(item, row, column, rowSpan, columnSpan); |
|
312 if (alignment != -1) |
|
313 setAlignment(item,alignment); |
|
314 QObject::connect(obj, SIGNAL(alignmentChanged(QGraphicsLayoutItem*,Qt::Alignment)), |
|
315 this, SLOT(updateAlignment(QGraphicsLayoutItem*,Qt::Alignment))); |
|
316 //### need to disconnect when widget is removed? |
|
317 } |
|
318 } |
|
319 |
|
320 //### is there a better way to do this? |
|
321 void QGraphicsGridLayoutObject::clearChildren() |
|
322 { |
|
323 for (int i = 0; i < count(); ++i) |
|
324 removeAt(i); |
|
325 } |
|
326 |
|
327 qreal QGraphicsGridLayoutObject::spacing() const |
|
328 { |
|
329 if (verticalSpacing() == horizontalSpacing()) |
|
330 return verticalSpacing(); |
|
331 return -1; //### |
|
332 } |
|
333 |
|
334 qreal QGraphicsGridLayoutObject::contentsMargin() const |
|
335 { |
|
336 qreal a,b,c,d; |
|
337 getContentsMargins(&a, &b, &c, &d); |
|
338 if(a==b && a==c && a==d) |
|
339 return a; |
|
340 return -1; |
|
341 } |
|
342 |
|
343 void QGraphicsGridLayoutObject::setContentsMargin(qreal m) |
|
344 { |
|
345 setContentsMargins(m,m,m,m); |
|
346 } |
|
347 |
|
348 |
|
349 void QGraphicsGridLayoutObject::updateAlignment(QGraphicsLayoutItem *item, Qt::Alignment alignment) |
|
350 { |
|
351 QGraphicsGridLayout::setAlignment(item, alignment); |
|
352 } |
|
353 |
|
354 QHash<QGraphicsLayoutItem*, GridLayoutAttached*> QGraphicsGridLayoutObject::attachedProperties; |
|
355 GridLayoutAttached *QGraphicsGridLayoutObject::qmlAttachedProperties(QObject *obj) |
|
356 { |
|
357 // ### This is not allowed - you must attach to any object |
|
358 if (!qobject_cast<QGraphicsLayoutItem*>(obj)) |
|
359 return 0; |
|
360 GridLayoutAttached *rv = new GridLayoutAttached(obj); |
|
361 attachedProperties.insert(qobject_cast<QGraphicsLayoutItem*>(obj), rv); |
|
362 return rv; |
|
363 } |
|
364 |
|
365 QT_END_NAMESPACE |