screensaverapp/stateproviders/snsrdefaultstateprovider/src/snsrrootstate.cpp
changeset 39 4e8ebe173323
parent 36 cdae8c6c3876
child 42 517f4fb5ec74
child 46 23b5d6a29cce
equal deleted inserted replaced
36:cdae8c6c3876 39:4e8ebe173323
     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:  Root Screensaver state.
       
    15 *
       
    16 */
       
    17 
       
    18 #include "snsrrootstate.h"
       
    19 
       
    20 #include <QDebug>
       
    21 #include <QEventTransition>
       
    22 
       
    23 #include <hbinstance.h>
       
    24 #include <hbmainwindow.h>
       
    25 #include <hbview.h>
       
    26 
       
    27 #include <screensaverfactory.h>
       
    28 
       
    29 #include "snsrtest_global.h"
       
    30 #include "snsruseractivityservice.h"
       
    31 
       
    32 /*!
       
    33     \class SnsrRootState
       
    34     \ingroup group_snsrdefaultstateprovider
       
    35     \brief Root Screensaver state.
       
    36  */
       
    37 
       
    38 /*!
       
    39     Constructs a new SnsrRootState with parent.
       
    40 */
       
    41 SnsrRootState::SnsrRootState(QState *parent) : 
       
    42     QState(parent), mScreensaver(0), mCurrentView(0), mMainWindow(0)
       
    43 {
       
    44     SCREENSAVER_TEST_FUNC_ENTRY("SnsrRootState::SnsrRootState")
       
    45     
       
    46     mMainWindow = HbInstance::instance()->allMainWindows().at(0);
       
    47     // TODO: consider if this should be Screensaver's responsibility
       
    48     mMainWindow->hideItems(Hb::AllItems);
       
    49     // load screensaver plugin
       
    50     mUserActivity = new SnsrUserActivityService();
       
    51     mUserActivity->setInactivityPeriod(5); // 5 seconds inactivity period
       
    52 
       
    53     ScreensaverFactory factory(QString("snsrresources/plugins/screensaverproviders"));
       
    54     ScreensaverToken bigClockToken;
       
    55     bigClockToken.mLibrary = QString("snsrbigclockscreensaverprovider.dll");
       
    56     bigClockToken.mUri = QString("screensaver.nokia.com/screensaver/bigclock");
       
    57     mScreensaver = factory.createScreensaver(bigClockToken);
       
    58     if (mScreensaver) {
       
    59         mScreensaver->setParent(this);
       
    60     } else {
       
    61         // TODO: error state
       
    62     }
       
    63     connect(mScreensaver, SIGNAL(viewChanged(QGraphicsWidget*)), SLOT(changeView(QGraphicsWidget*)));
       
    64     connect(mScreensaver, SIGNAL(faulted()), SLOT(screensaverFaulted()));
       
    65 
       
    66     // foreground state
       
    67     QState *foregroundState = new QState(this);
       
    68     connect(foregroundState, SIGNAL(entered()), mScreensaver, SLOT(foreground()));
       
    69 
       
    70     // background state
       
    71     QState *backgroundState = new QState(this);
       
    72     connect(backgroundState, SIGNAL(entered()), mScreensaver, SLOT(background()));
       
    73 
       
    74     // partial foreground state
       
    75     QState *partialForegroundState = new QState(this);
       
    76     connect(partialForegroundState, SIGNAL(entered()), mScreensaver, SLOT(partialForeground()));
       
    77 
       
    78     //setup user inactivity behaviour
       
    79     partialForegroundState->addTransition(mUserActivity, SIGNAL(active()), foregroundState);
       
    80     foregroundState->addTransition(mUserActivity, SIGNAL(notActive()), partialForegroundState);
       
    81 
       
    82     // power save state
       
    83 
       
    84     // set up transitions
       
    85     QEventTransition *toPartialForegroundTransition =
       
    86         new QEventTransition(qApp, QEvent::ApplicationActivate);
       
    87     toPartialForegroundTransition->setTargetState(partialForegroundState);
       
    88     backgroundState->addTransition(toPartialForegroundTransition);
       
    89 
       
    90     QEventTransition *toBackgroundTransition = 
       
    91         new QEventTransition(qApp, QEvent::ApplicationDeactivate);
       
    92     toBackgroundTransition->setTargetState(backgroundState);
       
    93     partialForegroundState->addTransition(toBackgroundTransition);
       
    94 
       
    95     QEventTransition *toBackgroundTransition2 =
       
    96         new QEventTransition(qApp, QEvent::ApplicationDeactivate);
       
    97     toBackgroundTransition2->setTargetState(backgroundState);
       
    98     foregroundState->addTransition(toBackgroundTransition2);
       
    99 
       
   100     setInitialState(partialForegroundState);
       
   101     
       
   102     SCREENSAVER_TEST_FUNC_EXIT("SnsrRootState::SnsrRootState")
       
   103 }
       
   104 
       
   105 /*!
       
   106     Destructs the class.
       
   107 */
       
   108 SnsrRootState::~SnsrRootState()
       
   109 {
       
   110     // mScreensaver deleted by parent
       
   111 }
       
   112 
       
   113 /*!
       
   114     This function is called when the state is entered.
       
   115 */
       
   116 void SnsrRootState::onEntry(QEvent *event)
       
   117 {
       
   118     QState::onEntry(event);
       
   119     qDebug() << objectName() << "- onEntry()";
       
   120 
       
   121     mScreensaver->initialize();
       
   122 }
       
   123 
       
   124 /*!
       
   125     This function is called when the state is exited.
       
   126 */
       
   127 void SnsrRootState::onExit(QEvent *event)
       
   128 {
       
   129     if (mCurrentView) {
       
   130         mMainWindow->removeView(mCurrentView);
       
   131     }
       
   132 
       
   133     mScreensaver->close();
       
   134 
       
   135     qDebug() << objectName() << "- onExit()";
       
   136     QState::onExit(event);
       
   137 }
       
   138 
       
   139 /*!
       
   140     Changes view in main window on widget.
       
   141     \param widget Widget with new view.
       
   142 */
       
   143 void SnsrRootState::changeView(QGraphicsWidget *widget)
       
   144 {
       
   145     SCREENSAVER_TEST_FUNC_ENTRY("SnsrRootState::changeView")
       
   146     
       
   147     qDebug("SnsrRootState::changeView() - widget: 0x%X", (int)widget);
       
   148 
       
   149     if (mCurrentView) {
       
   150         mCurrentView->hide();
       
   151         mMainWindow->removeView(mCurrentView);
       
   152     }
       
   153     mCurrentView = widget;
       
   154     if (mCurrentView) {
       
   155         mCurrentView->show();
       
   156         mMainWindow->addView(mCurrentView);
       
   157         mMainWindow->currentView()->setContentFullScreen(true);
       
   158     }
       
   159     
       
   160     SCREENSAVER_TEST_FUNC_EXIT("SnsrRootState::changeView")
       
   161 }
       
   162 
       
   163 /*!
       
   164     Invoked when screensaver faulted.
       
   165 */
       
   166 void SnsrRootState::screensaverFaulted()
       
   167 {
       
   168     qWarning() << "SnsrRootState::screensaverFaulted()";
       
   169     // TODO: some error handling
       
   170 }