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