|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2008-2010 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (developer.feedback@nokia.com) |
|
6 ** |
|
7 ** This file is part of the HbWidgets module of the UI Extensions for Mobile. |
|
8 ** |
|
9 ** GNU Lesser General Public License Usage |
|
10 ** This file may be used under the terms of the GNU Lesser General Public |
|
11 ** License version 2.1 as published by the Free Software Foundation and |
|
12 ** appearing in the file LICENSE.LGPL included in the packaging of this file. |
|
13 ** Please review the following information to ensure the GNU Lesser General |
|
14 ** Public License version 2.1 requirements will be met: |
|
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
16 ** |
|
17 ** In addition, as a special exception, Nokia gives you certain additional |
|
18 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
20 ** |
|
21 ** If you have questions regarding the use of this file, please contact |
|
22 ** Nokia at developer.feedback@nokia.com. |
|
23 ** |
|
24 ****************************************************************************/ |
|
25 |
|
26 #include <hbgridlayout_p.h> |
|
27 #include <hbgridlayout_p_p.h> |
|
28 |
|
29 #include <QGraphicsWidget> |
|
30 |
|
31 /*! |
|
32 \class HbGridLayout |
|
33 \brief HbGridLayout manages geometries of grid view contents. |
|
34 @internal |
|
35 */ |
|
36 |
|
37 /*! |
|
38 Constructor. |
|
39 \param parent parent layout item. |
|
40 */ |
|
41 HbGridLayout::HbGridLayout(QGraphicsLayoutItem *parent) |
|
42 : QGraphicsLayout(parent), d_ptr(new HbGridLayoutPrivate()) |
|
43 { |
|
44 Q_D(HbGridLayout); |
|
45 d->q_ptr = this; |
|
46 d->init(); |
|
47 } |
|
48 |
|
49 HbGridLayout::HbGridLayout(HbGridLayoutPrivate &dd, QGraphicsLayoutItem *parent) |
|
50 : QGraphicsLayout(parent),d_ptr(&dd) |
|
51 { |
|
52 Q_D(HbGridLayout); |
|
53 d->q_ptr = this; |
|
54 d->init(); |
|
55 } |
|
56 |
|
57 /*! |
|
58 Destructor. |
|
59 */ |
|
60 HbGridLayout::~HbGridLayout() |
|
61 { |
|
62 if (d_ptr) { |
|
63 for (int i = count() - 1; i >= 0; --i) { |
|
64 QGraphicsLayoutItem *item = itemAt(i); |
|
65 // The following lines can be removed, but this removes the item |
|
66 // from the layout more efficiently than the implementation of |
|
67 // ~QGraphicsLayoutItem. |
|
68 d_ptr->removeItem(item, i, false); |
|
69 if (item) { |
|
70 item->setParentLayoutItem(0); |
|
71 if (item->ownedByLayout()) { |
|
72 delete item; |
|
73 } |
|
74 } |
|
75 } |
|
76 } |
|
77 |
|
78 delete d_ptr; |
|
79 } |
|
80 |
|
81 /*! |
|
82 adds \a item to gridlayout. |
|
83 NOTE::there is no support for adding items at a specific row and column yet. |
|
84 */ |
|
85 void HbGridLayout::addItem(QGraphicsLayoutItem *item) |
|
86 { |
|
87 insertItem(-1, item, false); |
|
88 } |
|
89 /*! |
|
90 Sets size of gridlayout to \a size. |
|
91 \a targetCount will be updated with the maximum no of items it can show |
|
92 at a time. |
|
93 */ |
|
94 void HbGridLayout::setSize(const QSizeF &size,int &targetCount) |
|
95 { |
|
96 Q_D(HbGridLayout); |
|
97 d->setSize(size,targetCount); |
|
98 } |
|
99 |
|
100 /*! |
|
101 Returns position of layout \a item in the grid layout or -1 if not found. |
|
102 */ |
|
103 int HbGridLayout::indexOf(QGraphicsLayoutItem *item) const |
|
104 { |
|
105 const int totalItems = count(); |
|
106 for(int i = 0; i < totalItems; ++i) { |
|
107 if (itemAt(i) == item) { |
|
108 return i; |
|
109 } |
|
110 } |
|
111 return -1; |
|
112 } |
|
113 |
|
114 /*! |
|
115 Removes \a item from the grid layout. |
|
116 Equivalent of calling removeAt(indexOf(\aitem)). |
|
117 */ |
|
118 void HbGridLayout::removeItem(QGraphicsLayoutItem *item, bool animate) |
|
119 { |
|
120 Q_D(HbGridLayout); |
|
121 d->removeItem(item, indexOf(item), animate); |
|
122 } |
|
123 |
|
124 /*! |
|
125 Sets spacing to \a spacing. |
|
126 This sets uniform horizontal and vertical spacing between each item in the layout. |
|
127 */ |
|
128 void HbGridLayout::setSpacing(qreal spacing) |
|
129 { |
|
130 Q_D(HbGridLayout); |
|
131 if( spacing != d->mSpacing){ |
|
132 //TODO RECALCULATE ITEM SIZE |
|
133 d->mSpacing = spacing; |
|
134 if(count()>0){ |
|
135 d->reset(); |
|
136 } |
|
137 } |
|
138 } |
|
139 |
|
140 void HbGridLayout::setMargins(qreal topMargin, qreal bottomMargin, qreal leftMargin, qreal rightMargin) |
|
141 { |
|
142 Q_D(HbGridLayout); |
|
143 //TODO CALCULATE ITEM SIZE |
|
144 d->setMargins(topMargin, bottomMargin, leftMargin, rightMargin); |
|
145 d->reset(); |
|
146 |
|
147 } |
|
148 |
|
149 /*! |
|
150 Returns spacing between items.This is uniform spacing between items |
|
151 in both horizontally and vertically directions |
|
152 */ |
|
153 qreal HbGridLayout::spacing() const |
|
154 { |
|
155 Q_D(const HbGridLayout); |
|
156 return d->mSpacing; |
|
157 } |
|
158 /*! |
|
159 Inserts a layout at \a item at given position \a index to the grid layout. |
|
160 If index is out of range, the item is appended to the end of the list. |
|
161 */ |
|
162 void HbGridLayout::insertItem(int index, QGraphicsLayoutItem *item, bool animate) |
|
163 { |
|
164 if (item) { |
|
165 Q_D(HbGridLayout); |
|
166 d->insertItem(item, index, animate, true); |
|
167 } |
|
168 } |
|
169 |
|
170 /*! |
|
171 \reimp |
|
172 */ |
|
173 void HbGridLayout::setRowCount(int rowCount) |
|
174 { |
|
175 Q_D(HbGridLayout); |
|
176 if (d->mMaxVisRows!= rowCount){ |
|
177 //TODO RECALCULATE ITEM SIZE |
|
178 d->mMaxVisRows = rowCount; |
|
179 d->reset(); |
|
180 } |
|
181 } |
|
182 |
|
183 /*! |
|
184 \reimp |
|
185 */ |
|
186 void HbGridLayout::setColumnCount(int columnCount) |
|
187 { |
|
188 Q_D(HbGridLayout); |
|
189 if (d->mMaxVisColumns != columnCount){ |
|
190 //TODO RECALCULATE ITEM SIZE |
|
191 d->mMaxVisColumns = columnCount; |
|
192 d->reset(); |
|
193 } |
|
194 } |
|
195 |
|
196 /*! |
|
197 \reimp |
|
198 */ |
|
199 int HbGridLayout::count() const |
|
200 { |
|
201 Q_D(const HbGridLayout); |
|
202 return d->mItems.count(); |
|
203 } |
|
204 |
|
205 /*! |
|
206 \reimp |
|
207 */ |
|
208 QGraphicsLayoutItem *HbGridLayout::itemAt(int i) const |
|
209 { |
|
210 Q_D(const HbGridLayout); |
|
211 return d->mItems.value(i); |
|
212 } |
|
213 |
|
214 /*! |
|
215 \reimp |
|
216 */ |
|
217 int HbGridLayout::rowCount() const |
|
218 { |
|
219 Q_D(const HbGridLayout); |
|
220 return d->mMaxVisRows; |
|
221 } |
|
222 |
|
223 /*! |
|
224 \reimp |
|
225 */ |
|
226 int HbGridLayout::columnCount() const |
|
227 { |
|
228 Q_D(const HbGridLayout); |
|
229 return d->mMaxVisColumns; |
|
230 } |
|
231 |
|
232 /*! |
|
233 \reimp |
|
234 */ |
|
235 void HbGridLayout::removeAt(int index) |
|
236 { |
|
237 Q_D(HbGridLayout); |
|
238 |
|
239 QGraphicsLayoutItem *item = itemAt(index); |
|
240 if (item){ |
|
241 d->removeItem(item, index, false); |
|
242 } |
|
243 } |
|
244 |
|
245 /*! |
|
246 \reimp |
|
247 */ |
|
248 void HbGridLayout::setGeometry(const QRectF &rect) |
|
249 { |
|
250 Q_D( HbGridLayout ); |
|
251 if(!d->mClearGeometry) { |
|
252 return; |
|
253 } |
|
254 d->setGeometry(rect); |
|
255 QRectF newGeom = rect; |
|
256 newGeom.setSize(rect.size().expandedTo(d->mItemSize) |
|
257 .boundedTo(d->mMaxSizeHint)); |
|
258 if (newGeom == d->mGeometry) |
|
259 return; |
|
260 d->mGeometry = rect; |
|
261 QGraphicsLayout::setGeometry(d->mGeometry); |
|
262 } |
|
263 |
|
264 |
|
265 /*! |
|
266 \reimp |
|
267 */ |
|
268 QSizeF HbGridLayout::sizeHint( Qt::SizeHint which, const QSizeF &constraint ) const |
|
269 { |
|
270 Q_D(const HbGridLayout); |
|
271 return d->sizeHint(which,constraint); |
|
272 } |
|
273 |
|
274 /*! |
|
275 sets the scrollDirection of the layout to \a scrollDirection. |
|
276 This determines how items are drawni.e horizontally or vertically. |
|
277 */ |
|
278 void HbGridLayout::setScrollDirection(Qt::Orientations scrollDirection) |
|
279 { |
|
280 Q_D(HbGridLayout); |
|
281 if(d->mScrollDirection != scrollDirection){ |
|
282 d->mScrollDirection = scrollDirection; |
|
283 d->reset(); |
|
284 } |
|
285 } |
|
286 |
|
287 /*! |
|
288 Returns current scrollDirection. |
|
289 */ |
|
290 Qt::Orientations HbGridLayout::scrollDirection() const |
|
291 { |
|
292 Q_D(const HbGridLayout); |
|
293 return d->mScrollDirection; |
|
294 } |
|
295 |
|
296 /*! |
|
297 moves \a item to \a targetIndex and sets clearGeometry Flag to true. |
|
298 Need to call updateGeometry for updating layout. |
|
299 */ |
|
300 void HbGridLayout::moveItem(QGraphicsLayoutItem *item, int targetIndex, bool animate) |
|
301 { |
|
302 if (item) { |
|
303 Q_D(HbGridLayout); |
|
304 |
|
305 d->removeItem(item, indexOf(item), animate); |
|
306 d->insertItem(item, targetIndex, animate, false); |
|
307 } |
|
308 } |
|
309 |
|
310 /*! |
|
311 \reimp |
|
312 */ |
|
313 void HbGridLayout::invalidate() |
|
314 { |
|
315 Q_D(HbGridLayout); |
|
316 if(!d->mRecycling) |
|
317 d->mClearCacheHint = true; |
|
318 d->mClearGeometry = true; |
|
319 QGraphicsLayout::invalidate(); |
|
320 } |
|
321 |
|
322 void HbGridLayout::setRecycling(bool enableRecycling) |
|
323 { |
|
324 Q_D(HbGridLayout); |
|
325 if(enableRecycling != d->mRecycling){ |
|
326 invalidate(); |
|
327 d->mRecycling = enableRecycling; |
|
328 } |
|
329 } |
|
330 |
|
331 bool HbGridLayout::isRecyclingEnabled() |
|
332 { |
|
333 Q_D(HbGridLayout); |
|
334 return d->mRecycling; |
|
335 } |
|
336 |
|
337 void HbGridLayout::recycleItems(QGraphicsLayoutItem *sourceItem, QGraphicsLayoutItem *targetItem) |
|
338 { |
|
339 Q_D(HbGridLayout); |
|
340 |
|
341 HbGridLayoutPrivate::AnimationData *sourceData = d->animationData(sourceItem); |
|
342 HbGridLayoutPrivate::AnimationData *targetData = d->animationData(targetItem); |
|
343 |
|
344 d->stopAnimation(sourceData); |
|
345 |
|
346 d->stopAnimation(targetData); |
|
347 } |
|
348 |
|
349 bool HbGridLayout::isAnimated(QGraphicsLayoutItem *item) const |
|
350 { |
|
351 Q_D(const HbGridLayout); |
|
352 if (d->animationData(item) != 0) { |
|
353 return true; |
|
354 } else { |
|
355 return false; |
|
356 } |
|
357 } |
|
358 |
|
359 void HbGridLayout::effectFinished(const HbEffect::EffectStatus &status) |
|
360 { |
|
361 Q_D(HbGridLayout); |
|
362 |
|
363 QGraphicsLayoutItem *item = static_cast<QGraphicsWidget *>(status.item); |
|
364 d->stopAnimation(d->animationData(item)); |
|
365 } |
|
366 |
|
367 #include "moc_hbgridlayout_p.cpp" |
|
368 |