homescreenapp/stateplugins/hshomescreenstateplugin/src/hsidlestate.cpp
changeset 77 4b195f3bea29
parent 61 2b1b11a301d2
child 85 35368b604b28
--- a/homescreenapp/stateplugins/hshomescreenstateplugin/src/hsidlestate.cpp	Tue Jul 06 14:06:53 2010 +0300
+++ b/homescreenapp/stateplugins/hshomescreenstateplugin/src/hsidlestate.cpp	Wed Aug 18 09:40:07 2010 +0300
@@ -42,7 +42,9 @@
 #include "hsdomainmodeldatastructures.h"
 #include "hsscene.h"
 #include "hspage.h"
+#include "hspagevisual.h"
 #include "hswidgethost.h"
+#include "hswidgethostvisual.h"
 #include "hswallpaper.h"
 #include "hswallpaperselectionstate.h"
 #include "hstrashbinwidget.h"
@@ -51,19 +53,15 @@
 #include "hshomescreenstatecommon.h"
 #include "hstitleresolver.h"
 #include "hsmenuservice.h"
+#include "hsconfiguration.h"
+#include "hsmessageboxwrapper.h"
+#include "hspropertyanimationwrapper.h"
 #include "hsgui.h"
-#include "hsconfiguration.h"
 
-// Helper macros for connecting state entry and exit actions.
-#define ENTRY_ACTION(state, action) \
-    connect(state, SIGNAL(entered()), SLOT(action()));
-#define EXIT_ACTION(state, action) \
-    connect(state, SIGNAL(exited()), SLOT(action()));
 
 namespace
 {
-    const char gApplicationLibraryIconName[] = "qtg_mono_applications_all";
-
+  
     //User adds a new page to home screen
     const char hsLocTextId_OptionsMenu_AddPage[] = "txt_homescreen_opt_add_page";
 
@@ -90,13 +88,15 @@
 
     //Text in confirmation dialog while removing page with content
     const char hsLocTextId_Confirmation_RemovePage[] = "txt_homescreen_info_page_and_content_will_be_remov";
-
-    //Button in confirmation dialog while removing page with content
-    const char hsLocTextId_ConfirmationButton_Ok[] = "txt_homescreen_button_ok";
+}
 
-    //Button in confirmation dialog while removing page with content
-    const char hsLocTextId_ConfirmationButton_Cancel[] = "txt_homescreen_button_cancel";
-}
+// Helper macros for connecting state entry and exit actions.
+#define ENTRY_ACTION(state, action) \
+    connect(state, SIGNAL(entered()), SLOT(action()));
+#define EXIT_ACTION(state, action) \
+    connect(state, SIGNAL(exited()), SLOT(action()));
+
+
 
 /*!
     \class HsIdleState
@@ -114,23 +114,22 @@
 */
 HsIdleState::HsIdleState(QState *parent)
   : QState(parent),
-    mNavigationAction(0), mUiWidget(0),
+    mNavigationAction(0), 
     mTitleResolver(0),
-    mZoneAnimation(0),
     mAllowZoneAnimation(false),
-    mPageChangeAnimation(0),
     mContinuousFeedback(0),
     mTrashBinFeedbackAlreadyPlayed(false),
     mDeltaX(0),
-    mSceneMenu(0),
-    mSnapBorderGap(0.0)
+    mSceneMenu(0)
 #ifdef Q_OS_SYMBIAN
     ,mSettingsMgr(0)
 #endif
+    ,mMessageBoxWrapper(0)
+    
 {
     setupStates();
     mTitleResolver = new HsTitleResolver(this);
-
+     
     // TODO: Uncomment when updated API available
     //mContinuousFeedback = new HbContinuousFeedback;
     //mContinuousFeedback->setContinuousEffect(HbFeedback::ContinuousSmooth);
@@ -145,7 +144,6 @@
 */
 HsIdleState::~HsIdleState()
 {
-    delete mZoneAnimation;
     // TODO: Uncomment when updated API available
     //delete mContinuousFeedback;
 }
@@ -231,6 +229,7 @@
     HsWallpaperSelectionState *state_wallpaperSelectionState =
         new HsWallpaperSelectionState(this);
     QState *state_addPage = new QState(this);
+    QState *state_preRemovePage = new QState(this);
     QState *state_removePage = new QState(this);
     QState *state_toggleConnection = new QState(this);
 
@@ -239,7 +238,7 @@
     state_waitInput->addTransition(
         this, SIGNAL(event_addPage()), state_addPage);
     state_waitInput->addTransition(
-        this, SIGNAL(event_removePage()), state_removePage);
+        this, SIGNAL(event_preRemovePage()), state_preRemovePage);
     state_waitInput->addTransition(
         this, SIGNAL(event_toggleConnection()), state_toggleConnection);
     state_waitInput->addTransition(
@@ -259,8 +258,15 @@
         state_wallpaperSelectionState, SIGNAL(event_waitInput()), state_waitInput);
 
     state_addPage->addTransition(state_waitInput);
+    
+    state_preRemovePage->addTransition(
+        this,SIGNAL(event_removePage()),state_removePage);
+    
+    state_preRemovePage->addTransition(
+        this,SIGNAL(event_waitInput()),state_waitInput);
 
-    state_removePage->addTransition(state_waitInput);
+    state_removePage->addTransition(
+        this,SIGNAL(event_waitInput()),state_waitInput);
 
     state_toggleConnection->addTransition(state_waitInput);
 
@@ -299,8 +305,12 @@
     EXIT_ACTION(state_moveScene, action_moveScene_disconnectGestureHandlers)
     
     ENTRY_ACTION(state_addPage, action_addPage_addPage)
+    
+    ENTRY_ACTION(state_preRemovePage, action_preRemovePage_showQuery)
+    EXIT_ACTION(state_preRemovePage, action_preRemovePage_exit);
 
-    ENTRY_ACTION(state_removePage, action_removePage_removePage)
+    ENTRY_ACTION(state_removePage, action_removePage_startRemovePageAnimation)
+    EXIT_ACTION(state_removePage, action_removePage_removePage)
 
     ENTRY_ACTION(state_toggleConnection, action_toggleConnection_toggleConnection)
 }
@@ -310,7 +320,7 @@
 */
 qreal HsIdleState::pageLayerXPos(int pageIndex) const
 {
-    return -pageIndex * HsScene::mainWindow()->layoutRect().width();
+    return -pageIndex * HsGui::instance()->layoutRect().width();
 }
 
 /*!
@@ -319,25 +329,31 @@
 */
 void HsIdleState::startPageChangeAnimation(int targetPageIndex, int duration)
 {
-    if (!mPageChangeAnimation) {
-        mPageChangeAnimation = new QPropertyAnimation(mUiWidget, "sceneX");        
+    HsPropertyAnimationWrapper *animation = HsGui::instance()->pageChangeAnimation();
+    if (animation->isRunning()) {
+        animation->stop();
+    }        
+    animation->disconnect(this);
+    animation->setEndValue(pageLayerXPos(targetPageIndex));
+    animation->setDuration(duration);
+    
+    if (abs(mDeltaX) < HSCONFIGURATION_GET(pageChangePanDistanceInPixels)) {
+        animation->setEasingCurve(HSCONFIGURATION_GET(bounceAnimationEasingCurve)); 
     }
-    else if (mPageChangeAnimation->state() != QAbstractAnimation::Stopped) {
-        mPageChangeAnimation->stop();
-    }        
-    
-    mPageChangeAnimation->setEndValue(pageLayerXPos(targetPageIndex));
-    mPageChangeAnimation->setDuration(duration);
-    connect(mPageChangeAnimation,
+    else {
+        animation->setEasingCurve(HSCONFIGURATION_GET(pageChangeAnimationEasingCurve));
+    }
+
+    connect(animation,
             SIGNAL(finished()),
             SLOT(pageChangeAnimationFinished()), 
             Qt::UniqueConnection);
-    mPageChangeAnimation->start();
+    animation->start();
     
     HbInstantFeedback::play(HSCONFIGURATION_GET(pageChangeFeedbackEffect));
     
-    mUiWidget->showPageIndicator();
-    mUiWidget->setActivePage(targetPageIndex);
+    HsGui::instance()->idleWidget()->showPageIndicator();
+    HsGui::instance()->idleWidget()->setActivePage(targetPageIndex);
 }
 
 /*!
@@ -358,25 +374,25 @@
         return;
     }
 
-    if (!mZoneAnimation) {
-        mZoneAnimation = new QPropertyAnimation(mUiWidget, "sceneX");
-    }
-
     int bounceEffect = HSCONFIGURATION_GET(bounceEffect);
+    
+    HsPropertyAnimationWrapper *pageCrawlingAnimation =
+        HsGui::instance()->pageCrawlingAnimation();
 
     if (isInLeftPageChangeZone()) {
-        mZoneAnimation->setEndValue(pageLayerXPos(scene->activePageIndex()) + bounceEffect);
+        pageCrawlingAnimation->setEndValue(pageLayerXPos(scene->activePageIndex()) + bounceEffect);
     } else {
-        mZoneAnimation->setEndValue(pageLayerXPos(scene->activePageIndex()) - bounceEffect);
+        pageCrawlingAnimation->setEndValue(pageLayerXPos(scene->activePageIndex()) - bounceEffect);
     }
-    mZoneAnimation->setDuration(duration);
-    mZoneAnimation->setDirection(QAbstractAnimation::Forward);
+    pageCrawlingAnimation->setDuration(duration);
+    pageCrawlingAnimation->setForward();
 
-    connect(mZoneAnimation,
+    connect(pageCrawlingAnimation,
             SIGNAL(finished()),
-            SLOT(zoneAnimationFinished()));
+            SLOT(zoneAnimationFinished()),
+            Qt::UniqueConnection);
     
-    mZoneAnimation->start();
+    pageCrawlingAnimation->start();
 }
 
 /*!
@@ -404,7 +420,7 @@
 */
 bool HsIdleState::isInRightPageChangeZone()
 {
-    qreal pageWidth = HsScene::mainWindow()->layoutRect().width();
+    qreal pageWidth = HsGui::instance()->layoutRect().width();
     return mWidgetHotSpot.x() > pageWidth - HSCONFIGURATION_GET(pageChangeZoneWidth);
 }
 
@@ -418,7 +434,7 @@
     HsPage *page = HsPage::createInstance(data);
     page->load();
     HsScene::instance()->addPage(page);
-    mUiWidget->insertPage(pageIndex, page);
+    HsGui::instance()->idleWidget()->insertPage(pageIndex, page);
 }
 
 /*!
@@ -426,26 +442,25 @@
 */
 void HsIdleState::updateZoneAnimation()
 {
-    if (isInPageChangeZone() && mAllowZoneAnimation) {
-        if (!mZoneAnimation && (!mPageChangeAnimation
-            || mPageChangeAnimation->state() == QAbstractAnimation::Stopped )) {
+    HsPropertyAnimationWrapper *pageCrawlingAnimation = 
+        HsGui::instance()->pageCrawlingAnimation();
+
+    if (!mAllowZoneAnimation) {
+        pageCrawlingAnimation->stop();
+    } else if (isInPageChangeZone()) {
+        // should we start it
+        if (!pageCrawlingAnimation->isRunning() &&
+            !HsGui::instance()->pageChangeAnimation()->isRunning()) {
             startPageChangeZoneAnimation(HSCONFIGURATION_GET(pageChangeZoneAnimationDuration));
-        }
-    } else if (mZoneAnimation && !isInPageChangeZone()) {
-        if (mZoneAnimation->state() == QAbstractAnimation::Running) {
-            if (mZoneAnimation->direction() == QAbstractAnimation::Forward) {
-                mZoneAnimation->setDuration(HSCONFIGURATION_GET(pageChangeZoneReverseAnimationDuration));
-                mZoneAnimation->setDirection(QAbstractAnimation::Backward);
-            }
-        } else {
-            // Out of the page change zone. Delete animation.
-            deleteZoneAnimation();
-        }
-    } else if (!isInPageChangeZone()) {
-        if (mZoneAnimation) {
-            mZoneAnimation->stop();
-        }
-    }
+        } 
+    } else if (pageCrawlingAnimation->isRunning()) {
+        // Not in zone, but still running     
+        if (pageCrawlingAnimation->isForward()){
+            // reverse
+            pageCrawlingAnimation->setDuration(HSCONFIGURATION_GET(pageChangeZoneReverseAnimationDuration));
+            pageCrawlingAnimation->setBackward();
+        } 
+    } 
 }
 
 /*!
@@ -453,87 +468,32 @@
 */
 void HsIdleState::showTrashBin()
 {
-    if (mUiWidget->trashBin()->isUnderMouse()) {
+    if (HsGui::instance()->idleWidget()->trashBin()->isUnderMouse()) {
         if (!mTrashBinFeedbackAlreadyPlayed) {
             HbInstantFeedback::play(HSCONFIGURATION_GET(widgetOverTrashbinFeedbackEffect));
             mTrashBinFeedbackAlreadyPlayed = true;
         }
-        mUiWidget->trashBin()->activate();
+        HsGui::instance()->idleWidget()->trashBin()->activate();
     } else {
-        mUiWidget->trashBin()->deactivate();
+        HsGui::instance()->idleWidget()->trashBin()->deactivate();
         mTrashBinFeedbackAlreadyPlayed = false;
     }
 
-    if (!mUiWidget->pageIndicator()->isAnimationRunning()) {
-        mUiWidget->showTrashBin();
+    if (!HsGui::instance()->idleWidget()->pageIndicator()->isAnimationRunning()) {
+        HsGui::instance()->idleWidget()->showTrashBin();
     }
 }
 
 /*!
-    Removes currently active page.
-*/
-void HsIdleState::removeActivePage()
-{
-    HsScene *scene = HsScene::instance();
-    HsPage *page = scene->activePage();
-
-    int pageIndex = scene->activePageIndex();
-
-    mUiWidget->removePage(pageIndex);
-    scene->removePage(page);
-    
-    if (page->wallpaper()) {
-        page->wallpaper()->remove();        
-    }
-    delete page;
-
-    pageIndex = pageIndex == 0 ? 0 : pageIndex - 1;
-    scene->setActivePageIndex(pageIndex);
-
-    startPageChangeAnimation(pageIndex, HSCONFIGURATION_GET(pageRemovedAnimationDuration));
-
-    mUiWidget->pageIndicator()->removeItem(pageIndex);
-    mUiWidget->setActivePage(pageIndex);
-    mUiWidget->showPageIndicator();
-}
-
-/*!
-    Deletes page change zone animation.
-*/
-void HsIdleState::deleteZoneAnimation()
-{
-    delete mZoneAnimation;
-    mZoneAnimation = NULL;
-}
-
-/*!
     If called for the first time, setups the idle view.
     Updates the soft key action and sets the idle view
     as the current view to the main window.
 */
 void HsIdleState::action_idle_setupView()
 {
-    HbView *idleView = HsGui::idleView();
-    if (!idleView) {
-        mUiWidget = new HsIdleWidget;
-        idleView = HsScene::mainWindow()->addView(mUiWidget);
-        idleView->setContentFullScreen();
+    HsGui::instance()->setupIdleUi();
+    connect(HsGui::instance(),SIGNAL(navigateToApplibrary()),SIGNAL(event_applicationLibrary()),Qt::UniqueConnection);
 
-        mNavigationAction = new HbAction(this);
-        mNavigationAction->setIcon(HbIcon(gApplicationLibraryIconName));
-        mNavigationAction->setObjectName("applib_navigation_action");
-        connect(mNavigationAction, SIGNAL(triggered()), SIGNAL(event_applicationLibrary()));
-        idleView->setNavigationAction(mNavigationAction);
-
-        HsGui::setIdleView(idleView);
-
-        if (mPageChangeAnimation) {
-            delete mPageChangeAnimation;
-            mPageChangeAnimation = NULL;
-        }
-    }
-
-    HsScene::mainWindow()->setCurrentView(idleView);
 }
 
 /*!
@@ -553,7 +513,7 @@
 void HsIdleState::onTitleChanged(QString title)
 {
     qDebug() << "HsIdleState::onTitleChanged() to title: " << title;
-    HsGui::idleView()->setTitle(title);
+    HsGui::instance()->idleView()->setTitle(title);
 }
 
 /*!
@@ -587,9 +547,6 @@
     mTitleResolver->disconnect(this);
 }
 
-#ifdef COVERAGE_MEASUREMENT
-#pragma CTC SKIP
-#endif //COVERAGE_MEASUREMENT
 /*!
 
 */
@@ -617,9 +574,6 @@
     mSceneMenu->setPreferredPos(mPageHotSpot);
     mSceneMenu->open();
 }
-#ifdef COVERAGE_MEASUREMENT
-#pragma CTC ENDSKIP
-#endif //COVERAGE_MEASUREMENT
 
 void HsIdleState::onPagePanStarted(QGestureEvent *event)
 {
@@ -636,11 +590,11 @@
 
     HsScene *scene = HsScene::instance();
     int bounceEffect = HSCONFIGURATION_GET(bounceEffect);
-    qreal x = qBound(pageLayerXPos(scene->pages().count() - 1) - bounceEffect / 2 / mUiWidget->parallaxFactor(),
+    qreal x = qBound(pageLayerXPos(scene->pages().count() - 1) - bounceEffect / 2 / HsGui::instance()->idleWidget()->parallaxFactor(),
                      pageLayerXPos(scene->activePageIndex()) + mDeltaX,
-                     pageLayerXPos(0) + (bounceEffect / 2 / mUiWidget->parallaxFactor()));
+                     pageLayerXPos(0) + (bounceEffect / 2 / HsGui::instance()->idleWidget()->parallaxFactor()));
 
-    mUiWidget->setSceneX(x);
+    HsGui::instance()->idleWidget()->setSceneX(x);
 }
  
 void HsIdleState::onPagePanFinished(QGestureEvent *event)
@@ -666,41 +620,42 @@
     
     mWidgetHotSpot = qobject_cast<HbTapAndHoldGesture *>(
         event->gesture(Qt::TapAndHoldGesture))->scenePosition();
-    mWidgetHotSpotOffset = mWidgetHotSpot - widget->pos();
+    mWidgetHotSpotOffset = mWidgetHotSpot - widget->visual()->pos();
     
     emit event_moveWidget();
 }
  
 void HsIdleState::onWidgetMoveUpdated(const QPointF &scenePos, HsWidgetHost *widget)
 {
-    HsScene *scene = HsScene::instance();
-    QRectF widgetRect = widget->geometry();
+    QRectF widgetRect = widget->visual()->geometry();
 
-    // Move widget to new position.
+    // Move widget to updated position.
     mWidgetHotSpot = scenePos;
-       
     widgetRect.moveTopLeft(mWidgetHotSpot - mWidgetHotSpotOffset);
 
-    // Widget can be moved over the pages left border.
+    // When moving widget can go over the pages left border.
     qreal lowerBoundX = -widgetRect.width();
-    HsPage *page = scene->activePage();
-    QRectF pageRect = HsGui::idleView()->rect();
-    // Widget can be moved over the pages right border.
+
+    // When moving widget can go over the pages right border.
+    QRectF pageRect = HsGui::instance()->idleView()->rect();
     qreal upperBoundX = pageRect.width();
 
+    // When moving widget can go under the chrome at the pages upper border.
     // Notice that chrome height is 64 pixels.
     qreal lowerBoundY = qreal(64) - widgetRect.height();
-    // Widget can be moved over the pages down border.
+
+    // When moving widget can go over the pages down border.
     qreal upperBoundY = pageRect.height();
 
     qreal widgetX = qBound(lowerBoundX, widgetRect.x(), upperBoundX);
     qreal widgetY = qBound(lowerBoundY, widgetRect.y(), upperBoundY);
+
     // If using ItemClipsChildrenToShape-flag in widgethost then
     // setPos does not update position here, however setGeometry does it, QT bug?
-    widget->setGeometry(widgetX, widgetY, widgetRect.width(), widgetRect.height());
+    widget->visual()->setGeometry(widgetX, widgetY, widgetRect.width(), widgetRect.height());
     
     if (HSCONFIGURATION_GET(isSnapEnabled)) {
-        mSnapResult = HsWidgetPositioningOnWidgetMove::instance()->run(widget->sceneBoundingRect());
+        mSnapResult = HsWidgetPositioningOnWidgetMove::instance()->run(widget->visual()->sceneBoundingRect());
 
         if (HSCONFIGURATION_GET(isSnapEffectsEnabled)) {
             if (mSnapResult.hasHorizontalSnap) {
@@ -720,11 +675,12 @@
         mPreviousSnapResult = mSnapResult;
     }
 
-
-    int bounceFeedbackEffectDistance = HSCONFIGURATION_GET(bounceFeedbackEffectDistance);
     // Handle effects:
     // User is indicated by a tactile feedback if he/she is trying to move
     // widget over the first or the last page.
+    HsScene *scene = HsScene::instance();
+    HsPage *page = scene->activePage();
+    int bounceFeedbackEffectDistance = HSCONFIGURATION_GET(bounceFeedbackEffectDistance);
     if( (page == scene->pages().first() && mWidgetHotSpot.x() < bounceFeedbackEffectDistance ) ||
         (page == scene->pages().last() && scene->pages().count() == HSCONFIGURATION_GET(maximumPageCount)
          && mWidgetHotSpot.x() > pageRect.width() - bounceFeedbackEffectDistance)) {
@@ -733,8 +689,7 @@
             //if (!mContinuousFeedback->isPlaying()) {
             //    mContinuousFeedback->play();
             //}
-        }
-        else /*if (mContinuousFeedback->isPlaying())*/ {
+        } else /*if (mContinuousFeedback->isPlaying())*/ {
             //mContinuousFeedback->stop();
         }
 
@@ -783,7 +738,7 @@
     QApplication::instance()->installEventFilter(scene);
     scene->activePage()->showWidgets();
     qreal x = pageLayerXPos(scene->activePageIndex());
-    mUiWidget->setSceneX(x);
+    HsGui::instance()->idleWidget()->setSceneX(x);
 }
 
 /*!
@@ -791,7 +746,7 @@
 */
 void HsIdleState::action_idle_connectOrientationChangeEventHandler()
 {
-    connect(HsScene::mainWindow(),
+    connect(HsGui::instance(),
         SIGNAL(orientationChanged(Qt::Orientation)),
         SLOT(action_idle_orientationChanged()));
 }
@@ -838,9 +793,10 @@
     HsScene *scene = HsScene::instance();
     QApplication::instance()->removeEventFilter(scene);
     scene->activePage()->hideWidgets();
-
+    
+    HsGui::instance()->disconnect(this,SIGNAL(event_applicationLibrary()));
     //Close options menu on view change
-    HsGui::idleView()->setMenu(NULL);
+    HsGui::instance()->idleView()->setMenu(NULL);
 
     //Close context menu on view change
     if (mSceneMenu) {
@@ -853,7 +809,7 @@
 */
 void HsIdleState::action_idle_disconnectOrientationChangeEventHandler()
 {
-    disconnect(HsScene::mainWindow(),
+    disconnect(HsGui::instance(),
         SIGNAL(orientationChanged(Qt::Orientation)),
         this, SLOT(action_idle_orientationChanged()));
 }
@@ -876,44 +832,39 @@
     HbMenu *menu = new HbMenu();
     menu->setObjectName("hs_menu");
     // Task switcher
-    HbAction *action = menu->addAction(hbTrId(hsLocTextId_OptionsMenu_TaskSwitcher),
+    menu->addAction(hbTrId(hsLocTextId_OptionsMenu_TaskSwitcher),
         this, SLOT(openTaskSwitcher()));
-    action->setObjectName("action_open_task_switcher");
+    
     // Add content
-    action = menu->addAction(hbTrId(hsLocTextId_ContextMenu_AddContent),
+    menu->addAction(hbTrId(hsLocTextId_ContextMenu_AddContent),
         this, SLOT(onAddContentFromOptionsMenuActionTriggered()));
-    action->setObjectName("action_add_content");
     // Add page
     if (scene->pages().count() < HSCONFIGURATION_GET(maximumPageCount)) {
-        action = menu->addAction(hbTrId(hsLocTextId_OptionsMenu_AddPage),
+        menu->addAction(hbTrId(hsLocTextId_OptionsMenu_AddPage),
             this, SIGNAL(event_addPage()));
-        action->setObjectName("action_add_page");
-
+    
     }
 
     // Change wallpaper
-    action = menu->addAction(hbTrId(hsLocTextId_ContextMenu_ChangeWallpaper),
+    menu->addAction(hbTrId(hsLocTextId_ContextMenu_ChangeWallpaper),
         this, SIGNAL(event_selectWallpaper()));
-    action->setObjectName("action_change_wallpaper");
+    
     // Remove page
     if (scene->activePage()->isRemovable()) {
-        action = menu->addAction(hbTrId(hsLocTextId_OptionsMenu_RemovePage),
-            this, SIGNAL(event_removePage()));
-        action->setObjectName("action_remove_page"); 
+        menu->addAction(hbTrId(hsLocTextId_OptionsMenu_RemovePage),
+            this, SIGNAL(event_preRemovePage()));
     }
 
     // Online / Offline
     if (scene->isOnline()) {
-        action = menu->addAction(hbTrId(hsLocTextId_OptionsMenu_HsToOffline),
+        menu->addAction(hbTrId(hsLocTextId_OptionsMenu_HsToOffline),
             this, SIGNAL(event_toggleConnection()));
-        action->setObjectName("action_to_offline");
     } else {
-        action = menu->addAction(hbTrId(hsLocTextId_OptionsMenu_HsToOnline),
+        menu->addAction(hbTrId(hsLocTextId_OptionsMenu_HsToOnline),
             this, SIGNAL(event_toggleConnection()));
-        action->setObjectName("action_to_online");
     }
 
-    HsGui::idleView()->setMenu(menu);
+    HsGui::instance()->idleView()->setMenu(menu);
 }
 
 void HsIdleState::action_waitInput_connectGestureHandlers()
@@ -962,9 +913,9 @@
 {
     HsWidgetHost *widget = HsScene::instance()->activeWidget();
     Q_ASSERT(widget);
-    widget->setParentItem(mUiWidget->controlLayer());
+    widget->visual()->setParentItem(HsGui::instance()->idleWidget()->controlLayer());
 
-    mUiWidget->showTrashBin();
+    HsGui::instance()->idleWidget()->showTrashBin();
 }
 
 /*!
@@ -1001,16 +952,13 @@
 void HsIdleState::action_moveWidget_setWidgetSnap()
 {
     if (HSCONFIGURATION_GET(isSnapEnabled)) {
-        mSnapBorderGap = HSCONFIGURATION_GET(snapBorderGap);
 
         QVariantHash snapConfiguration;
         snapConfiguration[SNAPENABLED] = QString::number(HSCONFIGURATION_GET(isSnapEnabled));
         snapConfiguration[SNAPFORCE] = QString::number(HSCONFIGURATION_GET(snapForce));
         snapConfiguration[SNAPGAP] = QString::number(HSCONFIGURATION_GET(snapGap));
-        snapConfiguration[SNAPBORDERGAP] = mSnapBorderGap;
         HsWidgetPositioningOnWidgetMove::instance()->setConfiguration(snapConfiguration);
 
-
         updatePagePresentationToWidgetSnap();
 
         connect(HsScene::instance(), SIGNAL(activePageChanged()),
@@ -1045,7 +993,7 @@
         if (widget == activeWidget) {
            continue;
         }
-        QRectF widgetRect = widget->geometry();
+        QRectF widgetRect = widget->visual()->geometry();
         incativeWidgetRects.append(widgetRect);
     }
     return incativeWidgetRects;
@@ -1056,18 +1004,20 @@
 */
 void HsIdleState::action_moveWidget_reparentToPage()
 {
-    if (mZoneAnimation
-        && mZoneAnimation->state() == QAbstractAnimation::Running
-        && mZoneAnimation->direction() == QAbstractAnimation::Forward) {
-        mZoneAnimation->setDuration(HSCONFIGURATION_GET(pageChangeZoneReverseAnimationDuration));
-        mZoneAnimation->setDirection(QAbstractAnimation::Backward);
+    HsPropertyAnimationWrapper *pageCrawlingAnimation =
+        HsGui::instance()->pageCrawlingAnimation();
+    if (pageCrawlingAnimation->isRunning()&& 
+        pageCrawlingAnimation->isForward()) {
+
+        pageCrawlingAnimation->setDuration(HSCONFIGURATION_GET(pageChangeZoneReverseAnimationDuration));
+        pageCrawlingAnimation->setBackward();
     }
 
     HsScene *scene = HsScene::instance();
     HsPage *page = scene->activePage();
     HsWidgetHost *widget = scene->activeWidget();
 
-    if (mUiWidget->trashBin()->isUnderMouse()) {
+    if (HsGui::instance()->idleWidget()->trashBin()->isUnderMouse()) {
         HbInstantFeedback::play(HSCONFIGURATION_GET(widgetDropToTrashbinFeedbackEffect));
         widget->page()->removeWidget(widget);
         widget->remove();
@@ -1076,17 +1026,15 @@
         if (widget->page() != page) {
             widget->page()->removeWidget(widget);
             page->addExistingWidget(widget);
-            if (HsScene::orientation() == Qt::Horizontal) {
+            if (HsGui::instance()->orientation() == Qt::Horizontal) {
                 widget->removePresentation(Qt::Vertical);
             } else {
                 widget->removePresentation(Qt::Horizontal);
             }
         }
 
-        QRectF widgetRect = widget->geometry();
-        QRectF pageRect = page->rect();
-
         //Set the snap position of the widget and save the position
+        QRectF widgetRect = widget->visual()->geometry();
         if (mSnapResult.hasHorizontalSnap) {
             widgetRect.moveLeft(mSnapResult.horizontalSnapPosition);
         }
@@ -1094,23 +1042,16 @@
             widgetRect.moveTop(mSnapResult.verticalSnapPosition);
         }
 
-        //if snapBorderGap is defined, the widget is bounded in the rectangle which is smaller by snapBorderGap on all sides
-        qreal widgetX = qBound(qreal(0) + mSnapBorderGap, widgetRect.x(), (pageRect.width() - mSnapBorderGap) - widgetRect.width());
-        qreal widgetY = qBound(qreal(64) + mSnapBorderGap, widgetRect.y(), (pageRect.height() - mSnapBorderGap) - widgetRect.height());
-
-        // play feedback effect if user drops widget between pages and it needs to be repositioned back to original page
-        if( widgetX != widgetRect.x() || widgetY != widgetRect.y()) {
-            HbInstantFeedback::play(HSCONFIGURATION_GET(widgetRepositionFeedbackEffect));
-        }
-        widget->setPos(widgetX, widgetY);
+        QPointF adjustedWidgetPosition = page->adjustedWidgetPosition(widgetRect);
+        widget->visual()->setPos(adjustedWidgetPosition);
 
         widget->savePresentation();
         page->updateZValues();
     }
 
-    widget->setParentItem(HsScene::instance()->activePage());
+    widget->visual()->setParentItem(HsScene::instance()->activePage()->visual());
 
-    mUiWidget->showPageIndicator();
+    HsGui::instance()->idleWidget()->showPageIndicator();
 }
 
 /*!
@@ -1181,7 +1122,7 @@
 void HsIdleState::action_moveScene_moveToNearestPage()
 {
     QList<HsPage *> pages = HsScene::instance()->pages();
-    QSizeF pageSize = pages.first()->size();
+    QSizeF pageSize = pages.first()->visual()->size();
 
     int pageIndex = HsScene::instance()->activePageIndex();
 
@@ -1193,6 +1134,17 @@
 
     HsScene::instance()->setActivePageIndex(pageIndex);
 
+    HsPage *page = HsScene::instance()->activePage();
+    if (page) {
+        QList<HsWidgetHost *> widgets = page->newWidgets();
+        if (!widgets.isEmpty()) {
+            foreach (HsWidgetHost *widget, widgets) {
+                widget->startWidget();
+            }
+        }
+    page->layoutNewWidgets();
+    }
+
     startPageChangeAnimation(pageIndex, HSCONFIGURATION_GET(pageChangeAnimationDuration));
 }
 
@@ -1212,8 +1164,74 @@
     addPageToScene(pageIndex);
     scene->setActivePageIndex(pageIndex);
     startPageChangeAnimation(pageIndex, HSCONFIGURATION_GET(newPageAddedAnimationDuration));
-    mUiWidget->pageIndicator()->addItem(pageIndex);
-    mUiWidget->showPageIndicator();
+    HsGui::instance()->idleWidget()->pageIndicator()->addItem(pageIndex);
+    HsGui::instance()->idleWidget()->showPageIndicator();
+}
+
+/*!
+    Displays a confirmation query before page is removed if there are widgets on page 
+*/
+void HsIdleState::action_preRemovePage_showQuery()
+{
+    HsScene *scene = HsScene::instance();
+    HsPage *page = scene->activePage();
+
+    if (!page->widgets().isEmpty()) {
+        if (!mMessageBoxWrapper) {
+            mMessageBoxWrapper = new HsMessageBoxWrapper(this);
+            mMessageBoxWrapper->setHeader(hbTrId(hsLocTextId_Title_RemovePage));
+            mMessageBoxWrapper->setQueryText(hbTrId(hsLocTextId_Confirmation_RemovePage));
+            connect(mMessageBoxWrapper,SIGNAL(accepted()),SIGNAL(event_removePage())); 
+            connect(mMessageBoxWrapper,SIGNAL(rejected()),SIGNAL(event_waitInput())); 
+        }
+        mMessageBoxWrapper->show();
+    } else {
+        // Empty page can be removed without confirmation
+        emit event_removePage();
+    }
+}
+
+/*!
+    Make sure confirmation query gets closed if user exits preRemovePage state e.g. by pressing application key
+*/
+void HsIdleState::action_preRemovePage_exit()
+{
+    if (mMessageBoxWrapper) {
+        mMessageBoxWrapper->close();
+    }
+}
+
+/*!
+    Start remove page animation.
+*/
+void HsIdleState::action_removePage_startRemovePageAnimation()
+{
+    HsScene *scene = HsScene::instance();
+   
+    int pageToRemoveIndex = scene->activePageIndex();
+    bool isLastPage(scene->activePage() == scene->pages().last());
+    
+    int nextPageIndex(pageToRemoveIndex);
+    nextPageIndex++;
+    if (isLastPage) {
+        nextPageIndex--; 
+    }
+    HsPropertyAnimationWrapper *animation = HsGui::instance()->pageChangeAnimation();
+    if (animation->isRunning()) {
+        animation->stop();
+    }
+    animation->disconnect(this);
+    connect(animation,
+            SIGNAL(finished()),
+            SIGNAL(event_waitInput()), 
+            Qt::UniqueConnection);
+    animation->setEndValue(pageLayerXPos(nextPageIndex));
+    animation->setDuration(HSCONFIGURATION_GET(pageRemovedAnimationDuration));
+   
+    animation->start();
+    
+    HbInstantFeedback::play(HSCONFIGURATION_GET(pageChangeFeedbackEffect));
+    
 }
 
 /*!
@@ -1221,30 +1239,26 @@
 */
 void HsIdleState::action_removePage_removePage()
 {
+    HsIdleWidget *idleWidget = HsGui::instance()->idleWidget();
+    HsPropertyAnimationWrapper *animation = HsGui::instance()->pageChangeAnimation();
     HsScene *scene = HsScene::instance();
-    HsPage *page = scene->activePage();
-
-    if (!page->widgets().isEmpty()) {
-#ifndef HOMESCREEN_TEST //We don't want to test message box.
-        //Confirm removal of page having content
-        HbMessageBox *box = new HbMessageBox(HbMessageBox::MessageTypeQuestion);
-        box->setAttribute(Qt::WA_DeleteOnClose);
-        box->setHeadingWidget(new HbLabel
-                                (hbTrId(hsLocTextId_Title_RemovePage)));
-        box->setText(hbTrId(hsLocTextId_Confirmation_RemovePage));
+    HsPage *pageToRemove = scene->activePage();
 
-        QAction *buttonOk = box->actions().at(0);
-        //We are keen only from OK button. Cancel is not connected to any slot.
-        connect(buttonOk, SIGNAL(triggered()), SLOT(onRemovePageConfirmationOk()));
-       
-        box->open();
-#endif // HOMESCREEN_TEST
-    } else {
-        // Empty page can be removed without confirmation
-        removeActivePage();
+    if (animation->isRunning()) {
+        animation->stop();
     }
+    // remove from ui
+    idleWidget->removePage(pageToRemove->pageIndex());
+    idleWidget->showPageIndicator();
+    // update data model
+    scene->removePage(pageToRemove);
+    // Take new active page (previous was removed) and move scene to right position
+    qreal x = pageLayerXPos(scene->activePageIndex());
+    idleWidget->setSceneX(x);
+    // delete it   
+    pageToRemove->deleteLater();
+   
 }
-
 /*!
     Toggles the homescreen online/offline state.
 */
@@ -1277,8 +1291,8 @@
 {
     HsScene *scene = HsScene::instance();
     int pageIndex = scene->activePageIndex();
-
-    if (mZoneAnimation->direction() == QAbstractAnimation::Forward) {
+    HsGui *gui(HsGui::instance());
+    if (gui->pageCrawlingAnimation()->isForward()) {
         if (isInLeftPageChangeZone() &&
             0 < pageIndex) {
             --pageIndex;
@@ -1289,18 +1303,16 @@
         if (pageIndex == scene->pages().count()) {
             if (pageIndex < HSCONFIGURATION_GET(maximumPageCount)) {
                 addPageToScene(pageIndex);
-                mUiWidget->showPageIndicator();
-                mUiWidget->pageIndicator()->addItem(pageIndex);
+                gui->idleWidget()->showPageIndicator();
+                gui->idleWidget()->pageIndicator()->addItem(pageIndex);
             }
         }
         scene->setActivePageIndex(pageIndex);
         startPageChangeAnimation(pageIndex, HSCONFIGURATION_GET(pageChangeAnimationDuration));
     } else {
         scene->setActivePageIndex(pageIndex);
-        mUiWidget->setActivePage(pageIndex);
+        gui->idleWidget()->setActivePage(pageIndex);
     }
-
-    deleteZoneAnimation();
 }
 
 /*!
@@ -1311,23 +1323,16 @@
     updateZoneAnimation();
 }
 
-/*!
-    Handles the close of remove page confirmation dialog for page having content.
-*/
-void HsIdleState::onRemovePageConfirmationOk()
-{
-    removeActivePage();
-}
 
 /*!
     Provides the page presentation to the Widget Snap algorithm
 */
 void HsIdleState::updatePagePresentationToWidgetSnap()
 {
-    QRectF containerRect = HsScene::instance()->activePage()->rect();
-    containerRect.setTop(qreal(64));
+    QRectF containerRect = HsScene::instance()->activePage()->contentGeometry();
     HsWidgetHost *activeWidget = HsScene::instance()->activeWidget();
-    HsWidgetPositioningOnWidgetMove::instance()->setPagePresentation(containerRect, createInactiveWidgetRects(), activeWidget->geometry());
+    HsWidgetPositioningOnWidgetMove::instance()->setPagePresentation(
+        containerRect, createInactiveWidgetRects(), activeWidget->visual()->geometry());
 }
 
 /*!
@@ -1356,7 +1361,7 @@
         //the line will be shown when the timer expires.
         //If timer has already expired, just show the line, which is redrawn to new geometry.
         if (!mVerticalSnapLineTimer.isActive()) {
-            mUiWidget->showVerticalSnapLine(mSnapResult.verticalSnapLine);
+            HsGui::instance()->idleWidget()->showVerticalSnapLine(mSnapResult.verticalSnapLine);
         }
     }
 }
@@ -1366,7 +1371,7 @@
 */
 void HsIdleState::onVerticalSnapLineTimerTimeout()
 {
-    mUiWidget->showVerticalSnapLine(mSnapResult.verticalSnapLine);
+    HsGui::instance()->idleWidget()->showVerticalSnapLine(mSnapResult.verticalSnapLine);
 }
 
 /*!
@@ -1374,7 +1379,7 @@
 */
 void HsIdleState::hideVerticalLine()
 {
-    mUiWidget->hideVerticalSnapLine();
+    HsGui::instance()->idleWidget()->hideVerticalSnapLine();
     mVerticalSnapLineTimer.stop();
 }
 
@@ -1389,7 +1394,7 @@
     }
     else {
         if (!mHorizontalSnapLineTimer.isActive()) {
-            mUiWidget->showHorizontalSnapLine(mSnapResult.horizontalSnapLine);
+            HsGui::instance()->idleWidget()->showHorizontalSnapLine(mSnapResult.horizontalSnapLine);
         }
     }
 }
@@ -1399,7 +1404,7 @@
 */
 void HsIdleState::onHorizontalSnapLineTimerTimeout()
 {
-    mUiWidget->showHorizontalSnapLine(mSnapResult.horizontalSnapLine);
+    HsGui::instance()->idleWidget()->showHorizontalSnapLine(mSnapResult.horizontalSnapLine);
 }
 
 /*!
@@ -1407,7 +1412,7 @@
 */
 void HsIdleState::hideHorizontalLine()
 {
-    mUiWidget->hideHorizontalSnapLine();
+    HsGui::instance()->idleWidget()->hideHorizontalSnapLine();
     mHorizontalSnapLineTimer.stop();
 }
 
@@ -1419,3 +1424,5 @@
     updatePagePresentationToWidgetSnap();
     resetSnapPosition();
 }
+
+