homescreenapp/hsutils/src/hspageindicator.cpp
changeset 36 cdae8c6c3876
parent 35 f9ce957a272c
child 39 4e8ebe173323
--- a/homescreenapp/hsutils/src/hspageindicator.cpp	Fri Mar 19 09:27:44 2010 +0200
+++ b/homescreenapp/hsutils/src/hspageindicator.cpp	Fri Apr 16 14:54:01 2010 +0300
@@ -11,240 +11,133 @@
 *
 * Contributors:
 *
-* Description:  Menu Event.
+* Description:
 *
 */
 
 #include <QGraphicsLinearLayout>
-#include <QGraphicsBlurEffect>
-#include <QPropertyAnimation>
-#include <QParallelAnimationGroup>
-#include <QPointer>
-
-#include <HbIconItem>
 
 #include "hspageindicator.h"
-
-const char *KPageIndicatorActiveItemImageName = "qtg_graf_hspage_highlight";
-const char *KPageIndicatorNonActiveItemImageName = "qtg_graf_hspage_normal";
-
-/*! 
-    \class HsPageIndicator
-    \ingroup group_hsutils
-    \brief Page indicator widget.
-*/
+#include "hspageindicatoritem.h"
 
 /*!
-    Constructor.
-
-    \a parent Owner. 
-*/
-HsPageIndicator::HsPageIndicator(QGraphicsItem* parent):
-    HbWidget(parent),
-    mItemCount(0),
-    mCurrentIndex(0),
-    mNonActiveIcon(0),
-    mActiveIcon(0),
-    mIconAnimationGroup(0)
-{
-    mItemSize = QSizeF(30, 15);
 
-    // perf improvement to load icons only once
-    mNonActiveIcon = new HbIcon(KPageIndicatorNonActiveItemImageName);
-    mActiveIcon = new HbIcon(KPageIndicatorActiveItemImageName);
-
-    QGraphicsLinearLayout *layout = new QGraphicsLinearLayout(Qt::Horizontal);
-    layout->addStretch();
+*/
+HsPageIndicator::HsPageIndicator(QGraphicsItem *parent)
+  : HbWidget(parent),
+    mActiveItemIndex(-1)
+{
+}
 
-    mPageIndicatorLayout = new QGraphicsLinearLayout(Qt::Horizontal);
-    layout->addItem(mPageIndicatorLayout);
+/*!
 
-    layout->addStretch();
-    
-    setLayout(layout);
+*/
+HsPageIndicator::~HsPageIndicator()
+{
+    qDeleteAll(mItems);
 }
 
 /*!
-    Destructor.
-*/
-HsPageIndicator::~HsPageIndicator()
-{
-}
-
-/*! 
-    \fn HsPageIndicator::currentItemChangeAnimationFinished()
-    Signaled when current item is changed and animation is finished.
-*/
 
-/*!
-   Add an item and animates if \a animated is true and sets the
-   new page as current if \a setAsCurrent is true
 */
-void HsPageIndicator::addItem(bool setAsCurrent,bool animated)
+void HsPageIndicator::initialize(int itemCount, int activeItemIndex)
 {
-    mItemCount++;
-    addItemInternal(setAsCurrent,mItemCount - 1,animated);
-}
+    qDeleteAll(mItems);
+    mItems.clear();
+    mActiveItemIndex = -1;
 
-/*!
-   Removes on item and animates if \a animated is true.
-*/
-void HsPageIndicator::removeItem(bool animated)
-{
-    if (mItemCount < 1) {
+    if (itemCount < 1 || activeItemIndex < 0 || 
+        itemCount <= activeItemIndex) {
         return;
     }
 
-    mItemCount--;
+    for (int i = 0; i < itemCount; ++i) {
+        mItems << new HsPageIndicatorItem(i == activeItemIndex);
+    }    
+    mActiveItemIndex = activeItemIndex;
+    layoutItems();
+}
+ 
+/*!
 
-    if(mCurrentIndex == mItemCount) {
-        setCurrentIndex(mItemCount-1,animated);
-    }
-    
-    QGraphicsLayoutItem *item = mPageIndicatorLayout->itemAt(mPageIndicatorLayout->count() - 1);
-    mPageIndicatorLayout->removeAt(mPageIndicatorLayout->count() - 1);
-
-    if (mItemCount < 2) {
-        hide();
-    }
-    
-    delete item;
-    layout()->invalidate();
+*/
+int HsPageIndicator::itemCount() const
+{
+    return mItems.count();
 }
 
 /*!
-    Set \a currentIndex as current item and animates the change if \a animated
-    is true.
+
 */
-void HsPageIndicator::setCurrentIndex(int currentIndex,bool animated)
+void HsPageIndicator::setActiveItemIndex(int activeItemIndex)
 {
-    if( currentIndex < mPageIndicatorLayout->count() && currentIndex>=0) {
-        if ( mIconAnimationGroup ) {
-            mIconAnimationGroup->disconnect(this);
-            resetEffect();        
-        }
-        
-        QGraphicsLayoutItem *previousItem = mPageIndicatorLayout->itemAt(mCurrentIndex);
-        HbIconItem *previousIconItem = static_cast<HbIconItem*>(previousItem);
-        previousIconItem->setIcon(*mNonActiveIcon);
-        mCurrentIndex = currentIndex;
+    if (activeItemIndex < 0 || itemCount() <= activeItemIndex) {
+        return;
+    }
 
-        if (animated) {
-            startItemAnimation();
-        }
+    mActiveItemIndex = activeItemIndex;
+    for (int i = 0; i < mItems.count(); ++i) {
+        mItems[i]->setActive(i == activeItemIndex);
     }
 }
+ 
+/*!
+
+*/
+int HsPageIndicator::activeItemIndex() const
+{
+    return mActiveItemIndex;
+}
 
 /*!
-    Sets the item count to \a itemCount. Ie. removes or adds items if 
-    necessary.
-*/
-void HsPageIndicator::setItemCount(int itemCount)
-{
-    if (mItemCount < itemCount) {
-        int count = itemCount - mItemCount;
-        for (int i = 0; i < count; ++i) {
-            addItem(false,false);
-        }
-    } else if (mItemCount > itemCount) {
-        int count = mItemCount - itemCount;
-        for (int i = 0; i < count; ++i) {
-            removeItem(false);
-        }
-    }
-}
 
-/*!
-    Returns the item count
 */
-int HsPageIndicator::itemCount()
+void HsPageIndicator::addItem(int activeItemIndex)
 {
-    return mItemCount;
-}
-
+    if (activeItemIndex < 0 || itemCount() < activeItemIndex) {
+        return;
+    }
 
-/*!
-    Returns current index.
-*/
-int HsPageIndicator::currentIndex()
-{
-    return mCurrentIndex;
-}
-
-/*!
-    Returns true if current item animation is ongoing.
-*/
-bool HsPageIndicator::isAnimatingCurrentItemChange()
-{
-    return mIconAnimationGroup;
+    mItems.append(new HsPageIndicatorItem);
+    layoutItems();
+    setActiveItemIndex(activeItemIndex);
 }
 
 /*!
-    \internal
+
 */
-void HsPageIndicator::resetEffect()
-{
-    QGraphicsLayoutItem *item = mPageIndicatorLayout->itemAt(mCurrentIndex);
-    HbIconItem *iconItem = static_cast<HbIconItem*>(item);
-    if ( iconItem ) {
-        QPointer<QGraphicsEffect> iconEffect = iconItem->graphicsEffect();
-        iconItem->setGraphicsEffect(0);
-        if (iconEffect) {
-            delete iconEffect;
-        }
+void HsPageIndicator::removeItem(int activeItemIndex)
+{   
+    if (activeItemIndex < 0 || itemCount() - 1 <= activeItemIndex) {
+        return;
     }
-    mIconAnimationGroup = 0;
-    emit currentItemChangeAnimationFinished();
+
+    delete mItems.last();
+    mItems.removeLast();
+    layoutItems();
+    setActiveItemIndex(activeItemIndex);
 }
 
 /*!
-    \internal    
+
 */
-void HsPageIndicator::addItemInternal(bool setAsCurrent,int itemIndex,bool animated)
-{    
-    HbIconItem *iconItem = new HbIconItem();
-    iconItem->setIcon(*mNonActiveIcon);
-    iconItem->setPreferredSize(mItemSize);
-    iconItem->setMinimumSize(mItemSize);
-    iconItem->setMaximumSize(mItemSize);
-    
-    mPageIndicatorLayout->addItem(iconItem);
-    if (setAsCurrent) {
-        setCurrentIndex(itemIndex,animated);
-    }
-    layout()->invalidate();
-
-    if (mItemCount < 2) {
-        hide();
-    } else {
-        show();
-    }    
+bool HsPageIndicator::isAnimationRunning() const
+{
+    return mItems.at(mActiveItemIndex)->isAnimationRunning();
 }
 
 /*!
-    \internal    
+
 */
-void HsPageIndicator::startItemAnimation()
+void HsPageIndicator::layoutItems()
 {
-    QGraphicsLayoutItem *item = mPageIndicatorLayout->itemAt(mCurrentIndex);
-    HbIconItem *iconItem = static_cast<HbIconItem*>(item);
-    iconItem->setIcon(*mActiveIcon);
-    
-    QGraphicsBlurEffect* iconBlurEffect = new QGraphicsBlurEffect();
-    iconBlurEffect->setBlurRadius(9);
-    iconItem->setGraphicsEffect(iconBlurEffect);
-    iconItem->setTransformOriginPoint(iconItem->rect().center());
-    
-    mIconAnimationGroup = new QParallelAnimationGroup();
-
-    QPropertyAnimation *animation = new QPropertyAnimation(iconItem, "scale");
-    animation->setDuration(1000);
-    animation->setKeyValueAt(0.5, 2);
-    animation->setEndValue(1.0);
-    mIconAnimationGroup->addAnimation(animation);
-
-    connect(mIconAnimationGroup, SIGNAL(finished()), SLOT(resetEffect()));
-
-    mIconAnimationGroup->start(QAbstractAnimation::DeleteWhenStopped);
-
+    QGraphicsLinearLayout *layout = new QGraphicsLinearLayout;
+    layout->setContentsMargins(0, 0, 0, 0);
+    layout->setSpacing(8);
+    layout->addStretch();
+    foreach (HsPageIndicatorItem *item, mItems) {
+        layout->addItem(item);
+    }
+    layout->addStretch();
+    setLayout(layout);
 }