|
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: Menu Event. |
|
15 * |
|
16 */ |
|
17 |
|
18 #include <QGraphicsLinearLayout> |
|
19 #include <QGraphicsBlurEffect> |
|
20 #include <QPropertyAnimation> |
|
21 #include <QParallelAnimationGroup> |
|
22 #include <QPointer> |
|
23 |
|
24 #include <HbIconItem> |
|
25 |
|
26 #include "hspageindicator.h" |
|
27 |
|
28 const char *KPageIndicatorActiveItemImageName = "qtg_graf_hspage_highlight"; |
|
29 const char *KPageIndicatorNonActiveItemImageName = "qtg_graf_hspage_normal"; |
|
30 |
|
31 /*! |
|
32 \class HsPageIndicator |
|
33 \ingroup group_hsutils |
|
34 \brief Page indicator widget. |
|
35 */ |
|
36 |
|
37 /*! |
|
38 Constructor. |
|
39 |
|
40 \a parent Owner. |
|
41 */ |
|
42 HsPageIndicator::HsPageIndicator(QGraphicsItem* parent): |
|
43 HbWidget(parent), |
|
44 mItemCount(0), |
|
45 mCurrentIndex(0), |
|
46 mNonActiveIcon(0), |
|
47 mActiveIcon(0), |
|
48 mIconAnimationGroup(0) |
|
49 { |
|
50 mItemSize = QSizeF(30, 15); |
|
51 |
|
52 // perf improvement to load icons only once |
|
53 mNonActiveIcon = new HbIcon(KPageIndicatorNonActiveItemImageName); |
|
54 mActiveIcon = new HbIcon(KPageIndicatorActiveItemImageName); |
|
55 |
|
56 QGraphicsLinearLayout *layout = new QGraphicsLinearLayout(Qt::Horizontal); |
|
57 layout->addStretch(); |
|
58 |
|
59 mPageIndicatorLayout = new QGraphicsLinearLayout(Qt::Horizontal); |
|
60 layout->addItem(mPageIndicatorLayout); |
|
61 |
|
62 layout->addStretch(); |
|
63 |
|
64 setLayout(layout); |
|
65 } |
|
66 |
|
67 /*! |
|
68 Destructor. |
|
69 */ |
|
70 HsPageIndicator::~HsPageIndicator() |
|
71 { |
|
72 } |
|
73 |
|
74 /*! |
|
75 \fn HsPageIndicator::currentItemChangeAnimationFinished() |
|
76 Signaled when current item is changed and animation is finished. |
|
77 */ |
|
78 |
|
79 /*! |
|
80 Add an item and animates if \a animated is true and sets the |
|
81 new page as current if \a setAsCurrent is true |
|
82 */ |
|
83 void HsPageIndicator::addItem(bool setAsCurrent,bool animated) |
|
84 { |
|
85 mItemCount++; |
|
86 addItemInternal(setAsCurrent,mItemCount - 1,animated); |
|
87 } |
|
88 |
|
89 /*! |
|
90 Removes on item and animates if \a animated is true. |
|
91 */ |
|
92 void HsPageIndicator::removeItem(bool animated) |
|
93 { |
|
94 if (mItemCount < 1) { |
|
95 return; |
|
96 } |
|
97 |
|
98 mItemCount--; |
|
99 |
|
100 if(mCurrentIndex == mItemCount) { |
|
101 setCurrentIndex(mItemCount-1,animated); |
|
102 } |
|
103 |
|
104 QGraphicsLayoutItem *item = mPageIndicatorLayout->itemAt(mPageIndicatorLayout->count() - 1); |
|
105 mPageIndicatorLayout->removeAt(mPageIndicatorLayout->count() - 1); |
|
106 |
|
107 if (mItemCount < 2) { |
|
108 hide(); |
|
109 } |
|
110 |
|
111 delete item; |
|
112 layout()->invalidate(); |
|
113 } |
|
114 |
|
115 /*! |
|
116 Set \a currentIndex as current item and animates the change if \a animated |
|
117 is true. |
|
118 */ |
|
119 void HsPageIndicator::setCurrentIndex(int currentIndex,bool animated) |
|
120 { |
|
121 if( currentIndex < mPageIndicatorLayout->count() && currentIndex>=0) { |
|
122 if ( mIconAnimationGroup ) { |
|
123 mIconAnimationGroup->disconnect(this); |
|
124 resetEffect(); |
|
125 } |
|
126 |
|
127 QGraphicsLayoutItem *previousItem = mPageIndicatorLayout->itemAt(mCurrentIndex); |
|
128 HbIconItem *previousIconItem = static_cast<HbIconItem*>(previousItem); |
|
129 previousIconItem->setIcon(*mNonActiveIcon); |
|
130 mCurrentIndex = currentIndex; |
|
131 |
|
132 if (animated) { |
|
133 startItemAnimation(); |
|
134 } |
|
135 } |
|
136 } |
|
137 |
|
138 /*! |
|
139 Sets the item count to \a itemCount. Ie. removes or adds items if |
|
140 necessary. |
|
141 */ |
|
142 void HsPageIndicator::setItemCount(int itemCount) |
|
143 { |
|
144 if (mItemCount < itemCount) { |
|
145 int count = itemCount - mItemCount; |
|
146 for (int i = 0; i < count; ++i) { |
|
147 addItem(false,false); |
|
148 } |
|
149 } else if (mItemCount > itemCount) { |
|
150 int count = mItemCount - itemCount; |
|
151 for (int i = 0; i < count; ++i) { |
|
152 removeItem(false); |
|
153 } |
|
154 } |
|
155 } |
|
156 |
|
157 /*! |
|
158 Returns the item count |
|
159 */ |
|
160 int HsPageIndicator::itemCount() |
|
161 { |
|
162 return mItemCount; |
|
163 } |
|
164 |
|
165 |
|
166 /*! |
|
167 Returns current index. |
|
168 */ |
|
169 int HsPageIndicator::currentIndex() |
|
170 { |
|
171 return mCurrentIndex; |
|
172 } |
|
173 |
|
174 /*! |
|
175 Returns true if current item animation is ongoing. |
|
176 */ |
|
177 bool HsPageIndicator::isAnimatingCurrentItemChange() |
|
178 { |
|
179 return mIconAnimationGroup; |
|
180 } |
|
181 |
|
182 /*! |
|
183 \internal |
|
184 */ |
|
185 void HsPageIndicator::resetEffect() |
|
186 { |
|
187 QGraphicsLayoutItem *item = mPageIndicatorLayout->itemAt(mCurrentIndex); |
|
188 HbIconItem *iconItem = static_cast<HbIconItem*>(item); |
|
189 if ( iconItem ) { |
|
190 QPointer<QGraphicsEffect> iconEffect = iconItem->graphicsEffect(); |
|
191 iconItem->setGraphicsEffect(0); |
|
192 if (iconEffect) { |
|
193 delete iconEffect; |
|
194 } |
|
195 } |
|
196 mIconAnimationGroup = 0; |
|
197 emit currentItemChangeAnimationFinished(); |
|
198 } |
|
199 |
|
200 /*! |
|
201 \internal |
|
202 */ |
|
203 void HsPageIndicator::addItemInternal(bool setAsCurrent,int itemIndex,bool animated) |
|
204 { |
|
205 HbIconItem *iconItem = new HbIconItem(); |
|
206 iconItem->setIcon(*mNonActiveIcon); |
|
207 iconItem->setPreferredSize(mItemSize); |
|
208 iconItem->setMinimumSize(mItemSize); |
|
209 iconItem->setMaximumSize(mItemSize); |
|
210 |
|
211 mPageIndicatorLayout->addItem(iconItem); |
|
212 if (setAsCurrent) { |
|
213 setCurrentIndex(itemIndex,animated); |
|
214 } |
|
215 layout()->invalidate(); |
|
216 |
|
217 if (mItemCount < 2) { |
|
218 hide(); |
|
219 } else { |
|
220 show(); |
|
221 } |
|
222 } |
|
223 |
|
224 /*! |
|
225 \internal |
|
226 */ |
|
227 void HsPageIndicator::startItemAnimation() |
|
228 { |
|
229 QGraphicsLayoutItem *item = mPageIndicatorLayout->itemAt(mCurrentIndex); |
|
230 HbIconItem *iconItem = static_cast<HbIconItem*>(item); |
|
231 iconItem->setIcon(*mActiveIcon); |
|
232 |
|
233 QGraphicsBlurEffect* iconBlurEffect = new QGraphicsBlurEffect(); |
|
234 iconBlurEffect->setBlurRadius(9); |
|
235 iconItem->setGraphicsEffect(iconBlurEffect); |
|
236 iconItem->setTransformOriginPoint(iconItem->rect().center()); |
|
237 |
|
238 mIconAnimationGroup = new QParallelAnimationGroup(); |
|
239 |
|
240 QPropertyAnimation *animation = new QPropertyAnimation(iconItem, "scale"); |
|
241 animation->setDuration(1000); |
|
242 animation->setKeyValueAt(0.5, 2); |
|
243 animation->setEndValue(1.0); |
|
244 mIconAnimationGroup->addAnimation(animation); |
|
245 |
|
246 connect(mIconAnimationGroup, SIGNAL(finished()), SLOT(resetEffect())); |
|
247 |
|
248 mIconAnimationGroup->start(QAbstractAnimation::DeleteWhenStopped); |
|
249 |
|
250 } |