author | hgs |
Thu, 08 Jul 2010 12:44:18 +0300 | |
changeset 36 | ba22309243a1 |
parent 34 | bc10a61bd7d3 |
child 39 | ac7857bd5fdb |
permissions | -rw-r--r-- |
24 | 1 |
/* |
2 |
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 |
* All rights reserved. |
|
4 |
* This component and the accompanying materials are made available |
|
5 |
* under the terms of "Eclipse Public License v1.0" |
|
6 |
* which accompanies this distribution, and is available |
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 |
* |
|
9 |
* Initial Contributors: |
|
10 |
* Nokia Corporation - initial contribution. |
|
11 |
* |
|
12 |
* Contributors: |
|
13 |
* |
|
14 |
* Description: |
|
15 |
* |
|
16 |
*/ |
|
17 |
||
18 |
// System includes |
|
19 |
#include <QGraphicsSceneEvent> |
|
20 |
||
21 |
// User includes |
|
22 |
#include "radiostripbase.h" |
|
23 |
#include "radiologger.h" |
|
24 |
||
25 |
// Constants |
|
26 |
||
27 |
/*! |
|
28 |
* |
|
29 |
*/ |
|
30 |
RadioStripBase::RadioStripBase( QGraphicsItem* parent ) : |
|
31 |
HbScrollArea( parent ), |
|
32 |
mAutoScrollTime( 0 ), |
|
33 |
mStripContainer( new HbWidget( this ) ), |
|
34 |
mModel( 0 ), |
|
35 |
mIsCyclic( true ), |
|
36 |
mAutoCenter( false ), |
|
28
075425b8d9a4
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
24
diff
changeset
|
37 |
mOverlap( 0 ), |
24 | 38 |
mItemPoolParent( new QGraphicsWidget( NULL ) ), |
39 |
mCurrentIndex( 0 ), |
|
40 |
mPressedIndex( 0 ), |
|
41 |
mStripLength( 0 ), |
|
42 |
mContentsLength( 0 ) |
|
43 |
{ |
|
44 |
setClampingStyle( HbScrollArea::NoClamping ); |
|
45 |
setScrollDirections( Qt::Horizontal ); |
|
46 |
setFlag( QGraphicsItem::ItemClipsChildrenToShape, true ); |
|
47 |
setContentWidget( mStripContainer ); |
|
48 |
setFrictionEnabled( true ); |
|
49 |
setLongPressEnabled( false ); |
|
50 |
setHorizontalScrollBarPolicy( HbScrollArea::ScrollBarAlwaysOff ); |
|
51 |
setVerticalScrollBarPolicy( HbScrollArea::ScrollBarAlwaysOff ); |
|
52 |
||
53 |
// mItemParent is used to hold the unused QGraphicsItem's in the pool. It's visibility is set to false |
|
54 |
// so the visibility of the items doesn't need to be modified. |
|
55 |
mItemPoolParent->setVisible( false ); |
|
56 |
} |
|
57 |
||
58 |
/*! |
|
59 |
* |
|
60 |
*/ |
|
61 |
RadioStripBase::~RadioStripBase() |
|
62 |
{ |
|
63 |
} |
|
64 |
||
65 |
/*! |
|
66 |
* |
|
67 |
*/ |
|
68 |
void RadioStripBase::setAutoScrollTime( const int time ) |
|
69 |
{ |
|
70 |
mAutoScrollTime = time; |
|
71 |
} |
|
72 |
||
73 |
/*! |
|
74 |
* |
|
75 |
*/ |
|
76 |
int RadioStripBase::autoScrollTime() const |
|
77 |
{ |
|
78 |
return mAutoScrollTime; |
|
79 |
} |
|
80 |
||
81 |
/*! |
|
82 |
* |
|
83 |
*/ |
|
84 |
void RadioStripBase::setModel( QAbstractItemModel* model ) |
|
85 |
{ |
|
86 |
if ( mModel != model ) |
|
87 |
{ |
|
88 |
// if ( mModel ) |
|
89 |
// { |
|
90 |
// disconnectDataModel(); |
|
91 |
// } |
|
92 |
||
93 |
mModel = model; |
|
94 |
||
95 |
if ( mModel ) |
|
96 |
{ |
|
97 |
// connectDataModel(); |
|
98 |
||
99 |
mCurrentIndex = 0; |
|
100 |
populateAndLayout(); |
|
101 |
} |
|
102 |
} |
|
103 |
} |
|
104 |
||
105 |
/*! |
|
106 |
* |
|
107 |
*/ |
|
108 |
QAbstractItemModel* RadioStripBase::model() const |
|
109 |
{ |
|
110 |
return mModel; |
|
111 |
} |
|
112 |
||
113 |
/*! |
|
114 |
* |
|
115 |
*/ |
|
116 |
void RadioStripBase::setCyclic( bool isCyclic ) |
|
117 |
{ |
|
118 |
mIsCyclic = isCyclic; |
|
119 |
} |
|
120 |
||
121 |
/*! |
|
122 |
* |
|
123 |
*/ |
|
28
075425b8d9a4
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
24
diff
changeset
|
124 |
void RadioStripBase::setOverlap( qreal overlap ) |
24 | 125 |
{ |
28
075425b8d9a4
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
24
diff
changeset
|
126 |
mOverlap = overlap; |
24 | 127 |
} |
128 |
||
129 |
/*! |
|
130 |
* |
|
131 |
*/ |
|
132 |
void RadioStripBase::setAutoCenter( bool autoCenter ) |
|
133 |
{ |
|
134 |
mAutoCenter = autoCenter; |
|
135 |
} |
|
136 |
||
137 |
/*! |
|
138 |
* |
|
139 |
*/ |
|
140 |
void RadioStripBase::setItemSize( const QSizeF& size ) |
|
141 |
{ |
|
142 |
if ( mItemSize != size ) { |
|
143 |
mItemSize = size; |
|
144 |
||
145 |
prepareGeometryChange(); |
|
146 |
||
147 |
populateAndLayout(); |
|
148 |
||
149 |
update(); |
|
150 |
updateGeometry(); |
|
151 |
} |
|
152 |
} |
|
153 |
||
154 |
/*! |
|
155 |
* |
|
156 |
*/ |
|
157 |
void RadioStripBase::setIndex( int index, bool animateToCenter ) |
|
158 |
{ |
|
159 |
Q_UNUSED( animateToCenter ) |
|
160 |
// Sanity checks |
|
161 |
if ( !mModel || ( !mIsCyclic && ( index < 0 || index >= mModel->rowCount() ) ) ) { |
|
162 |
return; |
|
163 |
} |
|
164 |
||
165 |
const int oldIndex = mCurrentIndex; |
|
166 |
if ( mIsCyclic ) |
|
167 |
{ |
|
168 |
int numRows = mModel->rowCount(); |
|
169 |
index = (index + numRows) % numRows; |
|
170 |
} |
|
171 |
Q_ASSERT( index >= 0 ); |
|
172 |
||
173 |
mCurrentIndex = index; |
|
174 |
||
175 |
updateItemWithIndex( mCurrentIndex ); |
|
176 |
updateItemWithIndex( oldIndex ); |
|
177 |
} |
|
178 |
||
179 |
/*! |
|
180 |
* \reimp |
|
181 |
*/ |
|
182 |
void RadioStripBase::resizeEvent( QGraphicsSceneResizeEvent* event ) |
|
183 |
{ |
|
36 | 184 |
HbScrollArea::resizeEvent( event ); |
24 | 185 |
populateAndLayout(); |
186 |
} |
|
187 |
||
188 |
/*! |
|
189 |
* \reimp |
|
190 |
*/ |
|
191 |
void RadioStripBase::mousePressEvent( QGraphicsSceneMouseEvent* event ) |
|
192 |
{ |
|
193 |
if ( event->button() != Qt::LeftButton || !mModel || !mModel->rowCount() ) |
|
194 |
{ |
|
195 |
event->ignore(); |
|
196 |
return; |
|
197 |
} |
|
198 |
||
199 |
HbScrollArea::mousePressEvent( event ); |
|
200 |
} |
|
201 |
||
202 |
/*! |
|
203 |
* \reimp |
|
204 |
*/ |
|
205 |
void RadioStripBase::mouseReleaseEvent( QGraphicsSceneMouseEvent* event ) |
|
206 |
{ |
|
207 |
if ( event->button() != Qt::LeftButton ) |
|
208 |
{ |
|
209 |
event->ignore(); |
|
210 |
return; |
|
211 |
} |
|
212 |
||
213 |
HbScrollArea::mouseReleaseEvent( event ); |
|
214 |
} |
|
215 |
||
216 |
/*! |
|
217 |
* |
|
218 |
*/ |
|
219 |
void RadioStripBase::moveAllItemsToPool() |
|
220 |
{ |
|
221 |
// set parent of all items to pool |
|
222 |
foreach( QGraphicsItem* item, mItemAtSlot ) |
|
223 |
{ |
|
224 |
item->setParentItem( mItemPoolParent ); |
|
225 |
} |
|
226 |
||
227 |
// move all items to pool |
|
228 |
mItemPool += mItemAtSlot; |
|
229 |
mItemAtSlot.clear(); |
|
230 |
mIndexAtSlot.clear(); |
|
231 |
} |
|
232 |
||
233 |
/*! |
|
234 |
* |
|
235 |
*/ |
|
236 |
void RadioStripBase::populateAndLayout() |
|
237 |
{ |
|
238 |
moveAllItemsToPool(); |
|
239 |
||
240 |
if ( !mModel || mModel->rowCount() == 0 ) |
|
241 |
{ |
|
242 |
return; |
|
243 |
} |
|
244 |
||
245 |
mStripLength = boundingRect().width(); |
|
246 |
qreal itemSize = mItemSize.width(); |
|
28
075425b8d9a4
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
24
diff
changeset
|
247 |
mContentsLength = mModel->rowCount() * (itemSize - mOverlap); |
24 | 248 |
|
249 |
if ( mIsCyclic ) |
|
250 |
{ |
|
251 |
// if treating the items cyclically, double the content area so it can |
|
252 |
// be shifted back and forth as you scroll |
|
28
075425b8d9a4
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
24
diff
changeset
|
253 |
mContentsLength *= 2; |
24 | 254 |
} |
255 |
||
28
075425b8d9a4
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
24
diff
changeset
|
256 |
qreal currPos = -mOverlap; |
24 | 257 |
for ( int i = 0; i < mModel->rowCount(); ++i ) { |
258 |
if ( currPos > mStripLength ) |
|
259 |
{ |
|
260 |
break; |
|
261 |
} |
|
262 |
||
263 |
QGraphicsItem* item = constructItem( i, true ); |
|
264 |
if ( item ) |
|
265 |
{ |
|
28
075425b8d9a4
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
24
diff
changeset
|
266 |
item->setPos( QPointF( currPos, 0 ) ); |
075425b8d9a4
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
24
diff
changeset
|
267 |
currPos += itemSize - mOverlap; |
24 | 268 |
} |
269 |
} |
|
270 |
||
34 | 271 |
mStripContainer->setPreferredSize( mContentsLength, mItemSize.height() ); |
24 | 272 |
|
273 |
if ( mCurrentIndex >= 0 ) |
|
274 |
{ |
|
275 |
setIndex( mCurrentIndex, false ); |
|
276 |
} |
|
277 |
} |
|
278 |
||
279 |
/*! |
|
280 |
* |
|
281 |
*/ |
|
282 |
QGraphicsItem* RadioStripBase::constructItem( int index, bool append ) |
|
283 |
{ |
|
284 |
QGraphicsItem* item = getFromPool(); |
|
285 |
||
286 |
if ( mIsCyclic ) |
|
287 |
{ |
|
288 |
Q_ASSERT( index >= 0 && index < 2 * mModel->rowCount() ); |
|
289 |
||
290 |
updateItemPrimitive( item, index % mModel->rowCount() ); |
|
291 |
} |
|
292 |
else |
|
293 |
{ |
|
294 |
Q_ASSERT( index >= 0 && index < mModel->rowCount() ); |
|
295 |
||
296 |
updateItemPrimitive( item, index ); |
|
297 |
} |
|
298 |
||
299 |
item->setParentItem( mStripContainer ); |
|
300 |
||
301 |
if ( append ) |
|
302 |
{ |
|
303 |
mItemAtSlot.append( item ); |
|
304 |
mIndexAtSlot.append( index ); |
|
305 |
} |
|
306 |
else |
|
307 |
{ |
|
308 |
mItemAtSlot.prepend( item ); |
|
309 |
mIndexAtSlot.prepend( index ); |
|
310 |
} |
|
311 |
||
312 |
return item; |
|
313 |
} |
|
314 |
||
315 |
/*! |
|
316 |
* |
|
317 |
*/ |
|
318 |
QGraphicsItem* RadioStripBase::getFromPool() |
|
319 |
{ |
|
320 |
QGraphicsItem* item = 0; |
|
321 |
||
322 |
if ( mItemPool.isEmpty() ) |
|
323 |
{ |
|
324 |
item = createItemPrimitive( this ); |
|
325 |
} |
|
326 |
else |
|
327 |
{ |
|
328 |
item = mItemPool.takeFirst(); |
|
329 |
} |
|
330 |
||
331 |
return item; |
|
332 |
} |
|
333 |
||
334 |
/*! |
|
335 |
* |
|
336 |
*/ |
|
337 |
void RadioStripBase::returnToPool( QGraphicsItem* item ) |
|
338 |
{ |
|
339 |
// Unparent the item so it doesn't get deleted |
|
340 |
item->setParentItem( mItemPoolParent ); |
|
341 |
mItemPool.append( item ); |
|
342 |
} |
|
343 |
||
344 |
/*! |
|
345 |
* Returns starting coordinate of the item with the specified index |
|
346 |
*/ |
|
347 |
qreal RadioStripBase::indexToOffset( int index ) |
|
348 |
{ |
|
28
075425b8d9a4
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
24
diff
changeset
|
349 |
return index * ( mItemSize.width() - mOverlap ) - mOverlap; |
24 | 350 |
} |
351 |
||
352 |
/*! |
|
353 |
* Returns item index for specified offset amount into the content |
|
354 |
*/ |
|
355 |
int RadioStripBase::offsetToIndex( qreal offset ) |
|
356 |
{ |
|
357 |
const int rows = mModel->rowCount(); |
|
28
075425b8d9a4
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
24
diff
changeset
|
358 |
int index = (int)( offset / ( mItemSize.width() - mOverlap ) ); |
24 | 359 |
|
360 |
if ( mIsCyclic ) |
|
361 |
{ |
|
362 |
return qBound( 0, index, 2 * rows - 1 ); |
|
363 |
} |
|
364 |
||
365 |
return qBound( 0, index, rows - 1 ); |
|
366 |
} |
|
367 |
||
368 |
/*! |
|
369 |
* updates items with specified index value |
|
370 |
*/ |
|
371 |
void RadioStripBase::updateItemWithIndex( int index ) |
|
372 |
{ |
|
373 |
if( index >= 0 ) |
|
374 |
{ |
|
375 |
QList<QGraphicsItem *>::const_iterator item = mItemAtSlot.constBegin(); |
|
376 |
QList<QGraphicsItem *>::const_iterator itemsEnd = mItemAtSlot.constEnd(); |
|
377 |
QList<int>::const_iterator itemIndex = mIndexAtSlot.constBegin(); |
|
378 |
const int rowCount = mModel->rowCount(); |
|
379 |
||
380 |
// Find all items with this index (can be 2 in special cycling case) |
|
381 |
for( ; item != itemsEnd; ++item, ++itemIndex ) |
|
382 |
{ |
|
383 |
if( index == *itemIndex || index == *itemIndex - rowCount ) |
|
384 |
{ |
|
385 |
// update those items |
|
386 |
updateItemPrimitive( *item, index ); |
|
387 |
} |
|
388 |
} |
|
389 |
} |
|
390 |
} |
|
391 |
||
392 |
/*! |
|
393 |
* Updates items during scrolling: removing invisible items and adding items that became visible |
|
394 |
*/ |
|
395 |
void RadioStripBase::adjustItems() |
|
396 |
{ |
|
397 |
qreal contentPos = mStripContainer->pos().x(); |
|
398 |
||
399 |
if ( mIsCyclic ) |
|
400 |
{ |
|
401 |
if ( -contentPos < 0 ) |
|
402 |
{ |
|
403 |
// trying to display off the left end of the strip, so |
|
404 |
// shift the strip one length to the left |
|
405 |
contentPos -= mContentsLength * 0.5; |
|
406 |
mStripContainer->setPos( QPointF ( contentPos, mStripContainer->pos().y() ) ); |
|
407 |
} else if (-contentPos > mContentsLength * 0.5) { |
|
408 |
// trying to display off the right end of the strip, so |
|
409 |
// shift the strip one length to the right |
|
410 |
contentPos += mContentsLength * 0.5; |
|
411 |
mStripContainer->setPos( QPointF ( contentPos, mStripContainer->pos().y() ) ); |
|
412 |
} |
|
413 |
} |
|
414 |
||
415 |
// find the first and last indices of the visible items |
|
416 |
int firstVisibleIndex = offsetToIndex( -contentPos ); |
|
417 |
int lastVisibleIndex = offsetToIndex( -contentPos + mStripLength ); |
|
418 |
||
419 |
// remove items at the start that are no longer visible |
|
420 |
while ( !mIndexAtSlot.isEmpty() ) |
|
421 |
{ |
|
422 |
int firstSlotIndex = mIndexAtSlot.first(); |
|
423 |
if ( firstVisibleIndex <= firstSlotIndex ) |
|
424 |
{ |
|
425 |
break; |
|
426 |
} |
|
427 |
||
428 |
returnToPool( mItemAtSlot.first() ); |
|
429 |
mItemAtSlot.removeFirst(); |
|
430 |
mIndexAtSlot.removeFirst(); |
|
431 |
} |
|
432 |
||
433 |
// remove items at the end that are no longer visible |
|
434 |
while ( !mIndexAtSlot.isEmpty() ) |
|
435 |
{ |
|
436 |
int lastSlotIndex = mIndexAtSlot.last(); |
|
437 |
if ( lastVisibleIndex >= lastSlotIndex ) |
|
438 |
{ |
|
439 |
break; |
|
440 |
} |
|
441 |
||
442 |
returnToPool( mItemAtSlot.last() ); |
|
443 |
mItemAtSlot.removeLast(); |
|
444 |
mIndexAtSlot.removeLast(); |
|
445 |
} |
|
446 |
||
447 |
if ( mItemAtSlot.isEmpty() ) |
|
448 |
{ |
|
449 |
// fill area with all needed items |
|
450 |
for ( int i = firstVisibleIndex; i <= lastVisibleIndex; ++i ) |
|
451 |
{ |
|
452 |
QGraphicsItem* item = constructItem( i, true ); |
|
453 |
if ( item ) |
|
454 |
{ |
|
28
075425b8d9a4
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
24
diff
changeset
|
455 |
item->setPos( QPointF( indexToOffset( i ), 0 ) ); |
24 | 456 |
} |
457 |
} |
|
458 |
} |
|
459 |
else |
|
460 |
{ |
|
461 |
// add missing items at the front |
|
462 |
int firstItemToCreate = mIndexAtSlot.first()-1; |
|
463 |
for ( int i = firstItemToCreate; i >= firstVisibleIndex; --i ) |
|
464 |
{ |
|
465 |
QGraphicsItem* item = constructItem( i, false ); |
|
466 |
if ( item ) |
|
467 |
{ |
|
28
075425b8d9a4
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
24
diff
changeset
|
468 |
item->setPos( QPointF( indexToOffset( i ), 0 ) ); |
24 | 469 |
} |
470 |
} |
|
471 |
||
472 |
// add missing items at the end |
|
473 |
firstItemToCreate = mIndexAtSlot.last()+1; |
|
474 |
for ( int i = firstItemToCreate; i <= lastVisibleIndex; ++i ) |
|
475 |
{ |
|
476 |
QGraphicsItem* item = constructItem( i, true ); |
|
477 |
if ( item ) |
|
478 |
{ |
|
28
075425b8d9a4
Revision: 201021
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
24
diff
changeset
|
479 |
item->setPos( QPointF( indexToOffset( i ), 0 ) ); |
24 | 480 |
} |
481 |
} |
|
482 |
} |
|
483 |
} |
|
34 | 484 |
|
485 |
/*! |
|
486 |
* \reimp |
|
487 |
*/ |
|
488 |
bool RadioStripBase::scrollByAmount( const QPointF& delta ) |
|
489 |
{ |
|
490 |
bool ret = HbScrollArea::scrollByAmount( delta ); |
|
491 |
||
492 |
adjustItems(); |
|
493 |
scrollPosChanged(); |
|
494 |
||
495 |
return ret; |
|
496 |
} |