camerauis/cameraxui/cxui/src/cxuiviewmanager.cpp
branchRCL_3
changeset 54 bac7acad7cb3
parent 53 61bc0f252b2b
child 57 2c87b2808fd7
equal deleted inserted replaced
53:61bc0f252b2b 54:bac7acad7cb3
     1 /*
       
     2 * Copyright (c) 2009-2010 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 #include <QDebug>
       
    18 #include <QTimer>
       
    19 #include <hbmainwindow.h>
       
    20 #include <QGraphicsSceneEvent>
       
    21 #include <hbstyleloader.h>
       
    22 #include <hbactivitymanager.h>
       
    23 #include <hbaction.h>
       
    24 #include <xqserviceutil.h>
       
    25 
       
    26 #include "cxuiapplication.h"
       
    27 #include "cxuiapplicationstate.h"
       
    28 #include "cxuiviewmanager.h"
       
    29 #include "cxuivideoprecaptureview.h"
       
    30 #include "cxuistillprecaptureview.h"
       
    31 #include "cxuipostcaptureview.h"
       
    32 #include "cxuicapturekeyhandler.h"
       
    33 #include "cxeengine.h"
       
    34 #include "cxecameradevicecontrol.h"
       
    35 #include "cxefeaturemanager.h" //  mEngine->featureManager()
       
    36 #include "cxeviewfindercontrol.h"
       
    37 #include "cxenamespace.h"
       
    38 #include "cxuienums.h"
       
    39 #include "cxutils.h"
       
    40 #include "cxuidocumentloader.h"
       
    41 #include "cxesettings.h"
       
    42 #include "cxememorymonitor.h"
       
    43 #include "cxuiserviceprovider.h"
       
    44 #include "cxuiscenemodeview.h"
       
    45 
       
    46 
       
    47 #ifdef Q_OS_SYMBIAN
       
    48 #include "OstTraceDefinitions.h"
       
    49 
       
    50 #ifdef OST_TRACE_COMPILER_IN_USE
       
    51 #include "cxuiviewmanagerTraces.h"
       
    52 #endif
       
    53 
       
    54 #endif //Q_OS_SYMBIAN
       
    55 
       
    56 
       
    57 using namespace Cxe;
       
    58 using namespace CxUiLayout;
       
    59 
       
    60 namespace
       
    61 {
       
    62     static const int CXUI_STANDBY_CAMERA_TIMEOUT = 60000; // 60 seconds
       
    63 }
       
    64 
       
    65 
       
    66 /*!
       
    67 * Constructor.
       
    68 */
       
    69 CxuiViewManager::CxuiViewManager(CxuiApplication &application, HbMainWindow &mainWindow, CxeEngine &engine) :
       
    70     mApplication(application),
       
    71     mMainWindow(mainWindow),
       
    72     mEngine(engine),
       
    73     mKeyHandler(NULL),
       
    74     mCameraDocumentLoader(NULL),
       
    75     mApplicationState(NULL),
       
    76     mErrorManager(NULL)
       
    77 {
       
    78     CX_DEBUG_ENTER_FUNCTION();
       
    79 
       
    80     // create mappings to map view name to correct docml file
       
    81     mDocmlFilesByView.insert(STILL_PRE_CAPTURE_VIEW, STILL_1ST_XML);
       
    82     mDocmlFilesByView.insert(VIDEO_PRE_CAPTURE_VIEW, VIDEO_1ST_XML);
       
    83     mDocmlFilesByView.insert(POSTCAPTURE_VIEW, POSTCAPTURE_XML);
       
    84     mDocmlFilesByView.insert(SCENE_MODE_VIEW, SCENEMODE_SETTING_XML);
       
    85 
       
    86     // Key handler
       
    87     mKeyHandler = new CxuiCaptureKeyHandler(mEngine);
       
    88 
       
    89     // Document loader
       
    90     OstTrace0(camerax_performance, CXUIVIEWMANAGER_CXUIVIEWMANAGER_1, "msg: e_CX_VIEWMANAGER_CREATE_DOCLOADER 1");
       
    91     mCameraDocumentLoader = new CxuiDocumentLoader(&engine);
       
    92     OstTrace0(camerax_performance, CXUIVIEWMANAGER_CXUIVIEWMANAGER_2, "msg: e_CX_VIEWMANAGER_CREATE_DOCLOADER 0");
       
    93 
       
    94     // Application state
       
    95     mApplicationState = new CxuiApplicationState(mApplication, mEngine.settings(), mCameraDocumentLoader);
       
    96     CX_ASSERT_ALWAYS(mApplicationState);
       
    97     bool ok = connect(mApplicationState, SIGNAL(stateChanged(CxuiApplicationState::State, CxuiApplicationState::State)),
       
    98                       this, SLOT(handleApplicationStateChanged(CxuiApplicationState::State, CxuiApplicationState::State)));
       
    99     Q_ASSERT_X(ok, "CxuiViewManager", "Application state change signal connect failed.");
       
   100     connect(this, SIGNAL(standbyExitRequested()), mApplicationState, SLOT(exitStandby()), Qt::QueuedConnection);
       
   101 
       
   102     // Standby timer
       
   103     mStandbyTimer.setInterval(CXUI_STANDBY_CAMERA_TIMEOUT);
       
   104     mStandbyTimer.setSingleShot(true);
       
   105     connect(&mStandbyTimer, SIGNAL(timeout()), mApplicationState, SLOT(enterStandby()));
       
   106 
       
   107     // Filter capture and AF keys and follow user activity from mouse events.
       
   108     QCoreApplication::instance()->installEventFilter(this);
       
   109 
       
   110     // Monitor memory on normal state, not on standby, error or background.
       
   111     connect(this, SIGNAL(normalStateEntered()), &mEngine.memoryMonitor(), SLOT(startMonitoring()));
       
   112     connect(this, SIGNAL(normalStateExited()), &mEngine.memoryMonitor(), SLOT(stopMonitoring()));
       
   113 
       
   114     //connecting initmode signals
       
   115     connect(&mEngine.cameraDeviceControl(), SIGNAL(initModeComplete(CxeError::Id)),
       
   116             mApplicationState, SLOT(handleApplicationError(CxeError::Id)));
       
   117 
       
   118     connect(&mEngine.stillCaptureControl(), SIGNAL(imageCaptured(CxeError::Id, int)),
       
   119             mApplicationState, SLOT(handleApplicationError(CxeError::Id)));
       
   120 
       
   121     // Register stylesheet. It will be automatically destroyed on application
       
   122     // exit.
       
   123     HbStyleLoader::registerFilePath(":/camerax/cxui.css");
       
   124 
       
   125     // Create the view we are starting in, or connect signals so it
       
   126     // will be created once we know the mode we are starting to.
       
   127     initStartupView();
       
   128 
       
   129     // For UI startup testing, we need to emit applicationReady signal once UI is ready to be used.
       
   130     connect(mMainWindow.currentView(), SIGNAL(viewReady()), &mApplication, SIGNAL(applicationReady()));
       
   131 
       
   132     CX_DEBUG_EXIT_FUNCTION();
       
   133 }
       
   134 
       
   135 
       
   136 /*!
       
   137 * Destructor.
       
   138 */
       
   139 CxuiViewManager::~CxuiViewManager()
       
   140 {
       
   141     CX_DEBUG_ENTER_FUNCTION();
       
   142     QCoreApplication::instance()->removeEventFilter(this);
       
   143 
       
   144     if (!CxuiServiceProvider::isCameraEmbedded()) {
       
   145         currentView()->saveActivity();
       
   146     }
       
   147 
       
   148     delete mApplicationState;
       
   149     delete mCameraDocumentLoader;
       
   150     delete mKeyHandler;
       
   151 
       
   152     CX_DEBUG_EXIT_FUNCTION();
       
   153 }
       
   154 
       
   155 
       
   156 
       
   157 /*!
       
   158 * Handle change in overall state of application.
       
   159 * @param newState The new application state.
       
   160 */
       
   161 void CxuiViewManager::handleApplicationStateChanged(CxuiApplicationState::State newState,
       
   162                                                     CxuiApplicationState::State oldState)
       
   163 {
       
   164     Q_UNUSED(oldState);
       
   165     CX_DEBUG_ENTER_FUNCTION();
       
   166 
       
   167     CxuiView *view = currentView();
       
   168     CX_DEBUG(("CxuiViewManager - current view %d", view));
       
   169 
       
   170     switch (newState) {
       
   171     case CxuiApplicationState::Normal:
       
   172         // If camera is in embedded mode, we need to start
       
   173         // listening to key events again
       
   174         if (CxuiServiceProvider::isCameraEmbedded()){
       
   175             mKeyHandler->startListeningKeys();
       
   176         } else {
       
   177             // If in standalone mode, we disable raising to
       
   178             // foreground with capture key.
       
   179             disconnect(mKeyHandler, SIGNAL(captureKeyPressed()),
       
   180                        this, SLOT(toForeground()));
       
   181         }
       
   182         connectSignals(view);
       
   183 
       
   184         CX_DEBUG(("CxuiViewManager - emitting normalStateEntered"));
       
   185         emit normalStateEntered();
       
   186         break;
       
   187     case CxuiApplicationState::Standby:
       
   188          if (view && !view->isStandbyModeSupported()) {
       
   189             // If we move to Stanby, and current view does not
       
   190             // support standby mode, move to Normal mode right away.
       
   191             CX_DEBUG(("CxuiViewManager - view does not support stanbdy, exiting standby right away.."));
       
   192             mApplicationState->exitStandby();
       
   193         } else {
       
   194             handleExitingNormalState();
       
   195         }
       
   196         break;
       
   197     case CxuiApplicationState::Error:
       
   198     case CxuiApplicationState::Background:
       
   199         handleExitingNormalState();
       
   200 
       
   201         if (newState == CxuiApplicationState::Background) {
       
   202             // Moved to background.
       
   203             // If we're in embedded mode, we should stop listening to camera key
       
   204             // events
       
   205             if (CxuiServiceProvider::isCameraEmbedded()){
       
   206                 mKeyHandler->stopListeningKeys();
       
   207             } else {
       
   208                 // If not in embedded mode, then we bring application back to
       
   209                 // foreground by capture key press
       
   210                 connect(mKeyHandler, SIGNAL(captureKeyPressed()),
       
   211                         this, SLOT(toForeground()));
       
   212             }
       
   213         }
       
   214         break;
       
   215     }
       
   216 
       
   217     CX_DEBUG_EXIT_FUNCTION();
       
   218 }
       
   219 
       
   220 /*!
       
   221 * Helper method to handle (possible) exiting from normal state.
       
   222 * @param oldState The previous state.
       
   223 */
       
   224 void CxuiViewManager::handleExitingNormalState()
       
   225 {
       
   226     CX_DEBUG_ENTER_FUNCTION();
       
   227     // Store view that is active now.
       
   228     CxuiView *view = currentView();
       
   229     CX_DEBUG(("CxuiViewManager - current view %d", view));
       
   230 
       
   231     // Emit signal so current view can enter standby.
       
   232     emit normalStateExited();
       
   233     // Disconnect signals so we don't send key event's etc.
       
   234     // Using the view before normalStateExited() signal was emitted,
       
   235     // just in case it caused view switch.
       
   236     disconnectSignals(view);
       
   237     // Make sure standby timer is not running.
       
   238     stopStandbyTimer();
       
   239 
       
   240     CX_DEBUG_EXIT_FUNCTION();
       
   241 }
       
   242 
       
   243 /*!
       
   244 * Slot for starting standby timer.
       
   245 */
       
   246 void CxuiViewManager::startStandbyTimer()
       
   247 {
       
   248     CX_DEBUG_ENTER_FUNCTION();
       
   249     mStandbyTimer.start();
       
   250     CX_DEBUG_EXIT_FUNCTION();
       
   251 }
       
   252 
       
   253 /*!
       
   254 * Slot for stopping standby timer.
       
   255 */
       
   256 void CxuiViewManager::stopStandbyTimer()
       
   257 {
       
   258     CX_DEBUG_ENTER_FUNCTION();
       
   259     mStandbyTimer.stop();
       
   260     CX_DEBUG_EXIT_FUNCTION();
       
   261 }
       
   262 
       
   263 /*!
       
   264  * Helper function to return current view cast to CxuiView.
       
   265  */
       
   266 CxuiView* CxuiViewManager::currentView() const
       
   267 {
       
   268     CxuiView *view = qobject_cast<CxuiView*> (mMainWindow.currentView());
       
   269     return view;
       
   270 }
       
   271 
       
   272 /*!
       
   273 * Select and initialize the view we need to start into.
       
   274 */
       
   275 void CxuiViewManager::initStartupView()
       
   276 {
       
   277     CX_DEBUG_ENTER_FUNCTION();
       
   278 
       
   279     if (mApplication.activateReason() == Hb::ActivationReasonService ||
       
   280         // @todo: There's a bug in orbit and we never get Hb::ActivationReasonService as
       
   281         // activation reason. Use XQServiceUtil to determine if starting service as
       
   282         // a workaround for now
       
   283         XQServiceUtil::isService()) {
       
   284 
       
   285         // For embedded mode: don't create view yet, create when engine inits to correct mode.
       
   286         // Connect signals to set up the view after image/video prepare
       
   287         connect(&mEngine.stillCaptureControl(), SIGNAL(imagePrepareComplete(CxeError::Id)),
       
   288                 this, SLOT(changeToPrecaptureView()));
       
   289         connect(&mEngine.videoCaptureControl(), SIGNAL(videoPrepareComplete(CxeError::Id)),
       
   290                 this, SLOT(changeToPrecaptureView()));
       
   291 
       
   292     } else if (mApplication.activateReason() == Hb::ActivationReasonActivity) {
       
   293         // restoring activity, read startup view from stored activity
       
   294 
       
   295         // view to start in
       
   296         QString viewName = STILL_PRE_CAPTURE_VIEW;
       
   297 
       
   298         bool preCapture = true;
       
   299         QString activityId = mApplication.activateId();
       
   300         if (activityId == CxuiActivityIds::STILL_PRECAPTURE_ACTIVITY) {
       
   301             viewName = STILL_PRE_CAPTURE_VIEW;
       
   302         } else if (activityId == CxuiActivityIds::STILL_POSTCAPTURE_ACTIVITY) {
       
   303             viewName = POSTCAPTURE_VIEW;
       
   304             preCapture = false;
       
   305         } else if (activityId == CxuiActivityIds::VIDEO_PRECAPTURE_ACTIVITY) {
       
   306             viewName = VIDEO_PRE_CAPTURE_VIEW;
       
   307         } else if (activityId == CxuiActivityIds::VIDEO_POSTCAPTURE_ACTIVITY) {
       
   308             viewName = POSTCAPTURE_VIEW;
       
   309             preCapture = false;
       
   310         }
       
   311 
       
   312         CxuiView *view = createView(viewName);
       
   313         if (preCapture) {
       
   314             connectPreCaptureSignals(static_cast<CxuiPrecaptureView *>(view));
       
   315         } else {
       
   316             connectPostCaptureSignals();
       
   317         }
       
   318         mMainWindow.setCurrentView(view, false);
       
   319 
       
   320         // restore view from activity
       
   321         bool ok = mApplication.activityManager()->waitActivity();
       
   322 
       
   323         view->restoreActivity(activityId,
       
   324                               mApplication.activityManager()->activityData(mApplication.activateId()));
       
   325 
       
   326         clearAllActivities();
       
   327     } else {
       
   328         // normal start
       
   329         // no activity id, get default view from engine state
       
   330         CxuiView *view = NULL;
       
   331         if (mEngine.mode() == Cxe::VideoMode) {
       
   332             view = createView(VIDEO_PRE_CAPTURE_VIEW);
       
   333         } else {
       
   334             view = createView(STILL_PRE_CAPTURE_VIEW);
       
   335         }
       
   336         connectPreCaptureSignals(static_cast<CxuiPrecaptureView *>(view));
       
   337         mMainWindow.setCurrentView(view, false);
       
   338 
       
   339         clearAllActivities();
       
   340     }
       
   341 
       
   342     // Check the current application state, signalled to handleApplicationStateChanged.
       
   343     mApplicationState->startMonitoring();
       
   344 
       
   345     CX_DEBUG_EXIT_FUNCTION();
       
   346 }
       
   347 
       
   348 /*!
       
   349 * Create view and add it to main window.
       
   350 */
       
   351 CxuiView* CxuiViewManager::createView(const QString &viewName)
       
   352 {
       
   353     CX_DEBUG_ENTER_FUNCTION();
       
   354 
       
   355     OstTrace0(camerax_performance, CXUIVIEWMANAGER_CREATEVIEW_IN, "msg: e_CX_CREATE_VIEW 1");
       
   356 
       
   357     CX_DEBUG(("View name: %s", viewName.toAscii().data()));
       
   358 
       
   359     CxuiView* view = mViews[viewName];
       
   360     if (view == NULL) {
       
   361         CX_DEBUG(("View not loaded yet, loading now"));
       
   362         bool ok = false;
       
   363         // Use document loader to create widgets and layouts
       
   364         // (non-sectioned parts are parsed and loaded)
       
   365         QString docmlFile = mDocmlFilesByView[viewName];
       
   366         CX_ASSERT_ALWAYS(mCameraDocumentLoader);
       
   367         CX_ASSERT_ALWAYS(!docmlFile.isNull());
       
   368 
       
   369         OstTrace0(camerax_performance, CXUIVIEWMANAGER_CREATEVIEW_2, "msg: e_CX_DOCUMENTLOADER_LOAD 1");
       
   370         mCameraDocumentLoader->load(docmlFile, &ok);
       
   371         OstTrace0(camerax_performance, CXUIVIEWMANAGER_CREATEVIEW_4, "msg: e_CX_DOCUMENTLOADER_LOAD 0");
       
   372         Q_ASSERT_X(ok, "createView", "error in xml file parsing");
       
   373 
       
   374         OstTrace0(camerax_performance, CXUIVIEWMANAGER_CREATEVIEW_6, "msg: e_CX_DOCUMENTLOADER_FINDWIDGET 1");
       
   375         QGraphicsWidget *widget = NULL;
       
   376         // ask for the view widget pointer
       
   377         widget = mCameraDocumentLoader->findWidget(viewName);
       
   378         view = qobject_cast<CxuiView *> (widget);
       
   379         CX_ASSERT_ALWAYS(view);
       
   380         OstTrace0(camerax_performance, CXUIVIEWMANAGER_CREATEVIEW_8, "msg: e_CX_DOCUMENTLOADER_FINDWIDGET 1");
       
   381 
       
   382         // call for needed consturction methods
       
   383         view->construct(&mMainWindow, &mEngine, mCameraDocumentLoader, mKeyHandler, mApplication.activityManager());
       
   384         // .. and add to main window (which also takes ownership)
       
   385         OstTrace0(camerax_performance, CXUIVIEWMANAGER_CREATEVIEW_10, "msg: e_CX_MAINWINDOW_ADDVIEW 1");
       
   386         mMainWindow.addView(view);
       
   387         mViews.insert(viewName, view);
       
   388         OstTrace0(camerax_performance, CXUIVIEWMANAGER_CREATEVIEW_12, "msg: e_CX_MAINWINDOW_ADDVIEW 0");
       
   389     }
       
   390 
       
   391     OstTrace0(camerax_performance, CXUIVIEWMANAGER_CREATEVIEW_OUT, "msg: e_CX_CREATE_VIEW 0");
       
   392 
       
   393     CX_DEBUG_EXIT_FUNCTION();
       
   394     return view;
       
   395 }
       
   396 
       
   397 /*!
       
   398 * CxuiViewManager::showScenesView
       
   399 * A private method for displaying Scene Modes selection view
       
   400 */
       
   401 void CxuiViewManager::showScenesView()
       
   402 {
       
   403     CX_DEBUG_ENTER_FUNCTION();
       
   404     // Disconnect signals from old view.
       
   405     disconnectSignals();
       
   406 
       
   407     CxuiSceneModeView *view = qobject_cast<CxuiSceneModeView*>(createView(SCENE_MODE_VIEW));
       
   408     CX_ASSERT_ALWAYS(view);
       
   409     view->loadBackgroundImages();
       
   410 
       
   411     stopStandbyTimer();
       
   412     connectSceneModeSignals();
       
   413     mMainWindow.setCurrentView(view, false);
       
   414 
       
   415     CX_DEBUG_EXIT_FUNCTION();
       
   416 }
       
   417 
       
   418 /*!
       
   419 * Get the precapture view appropriate for given camera mode and index.
       
   420 * If the view does not exist, it is created.
       
   421 * @param mode Which camera mode the view should match.
       
   422 * @param index Which camera index the view should match.
       
   423 */
       
   424 CxuiPrecaptureView*
       
   425 CxuiViewManager::getPrecaptureView(Cxe::CameraMode mode, Cxe::CameraIndex camera)
       
   426 {
       
   427     CX_DEBUG_ENTER_FUNCTION();
       
   428     CX_DEBUG(("CxuiViewManager::getPrecaptureView - mode=%d, camera index=%d", mode, camera));
       
   429 
       
   430     CX_DEBUG_ASSERT(camera == Cxe::PrimaryCameraIndex);
       
   431 
       
   432     if (mode == ImageMode) {
       
   433         CX_DEBUG(("CxuiViewManager::getPrecaptureView - image mode"));
       
   434         return qobject_cast<CxuiPrecaptureView*>(createView(STILL_PRE_CAPTURE_VIEW));
       
   435 
       
   436     } else {
       
   437         CX_DEBUG(("CxuiViewManager::getPrecaptureView - video mode"));
       
   438         return qobject_cast<CxuiPrecaptureView*>(createView(VIDEO_PRE_CAPTURE_VIEW));
       
   439     }
       
   440 }
       
   441 
       
   442 /*!
       
   443 * Move to post-capture view.
       
   444 */
       
   445 void CxuiViewManager::changeToPostcaptureView()
       
   446 {
       
   447     CX_DEBUG_ENTER_FUNCTION();
       
   448     // Disconnect signals from old view.
       
   449     disconnectSignals();
       
   450 
       
   451     CxuiView *postCaptureView = createView(POSTCAPTURE_VIEW);
       
   452 
       
   453     // Connecting all necessary signals for postcapture view.
       
   454     // Not connected yet if not in normal state. We connect the signals
       
   455     // once we enter normal state again.
       
   456     if (mApplicationState->currentState() == CxuiApplicationState::Normal) {
       
   457         connectPostCaptureSignals();
       
   458     }
       
   459 
       
   460     // Connect signals before we set the post-capture view as current view.
       
   461     // We need to have signals connected if post-capture view for example
       
   462     // needs to move back to pre-capture view already in showEvent.
       
   463     mMainWindow.setCurrentView(postCaptureView, false);
       
   464 
       
   465     CX_DEBUG_EXIT_FUNCTION();
       
   466 }
       
   467 
       
   468 /*!
       
   469 * Move to pre-capture view.
       
   470 */
       
   471 void CxuiViewManager::changeToPrecaptureView()
       
   472 {
       
   473     CX_DEBUG_ENTER_FUNCTION();
       
   474 
       
   475     if (CxuiServiceProvider::isCameraEmbedded()) {
       
   476         // disconnect signals, we only want to call this once after the initial prepare
       
   477         disconnect(&mEngine.stillCaptureControl(), SIGNAL(imagePrepareComplete(CxeError::Id)),
       
   478                 this, SLOT(changeToPrecaptureView()));
       
   479         disconnect(&mEngine.videoCaptureControl(), SIGNAL(videoPrepareComplete(CxeError::Id)),
       
   480                 this, SLOT(changeToPrecaptureView()));
       
   481     }
       
   482 
       
   483     // If normal mode is not active, don't switch to precapture view and reserve camera now.
       
   484     if (mApplicationState->currentState() != CxuiApplicationState::Normal) {
       
   485         CX_DEBUG(("CxuiViewManager - Change to precapture blocked as not normal mode (error, standby, background)."));
       
   486     } else {
       
   487 
       
   488         // Disconnect signals from old view.
       
   489         disconnectSignals();
       
   490 
       
   491         CxuiPrecaptureView *view =
       
   492             getPrecaptureView(mEngine.mode(), mEngine.cameraDeviceControl().cameraIndex());
       
   493 
       
   494         // Connect necessary pre-capture view signals.
       
   495         connectPreCaptureSignals(view);
       
   496 
       
   497         mMainWindow.setCurrentView(view, false);
       
   498 
       
   499         // Release resources needed by scene view.
       
   500         HbView *sceneView = mViews.take(SCENE_MODE_VIEW);
       
   501         if (sceneView) {
       
   502             // This will not delete the view.
       
   503             mMainWindow.removeView(sceneView);
       
   504             // We can get to this slot from scene view, so don't delete the object too early.
       
   505             sceneView->deleteLater();
       
   506             sceneView = NULL;
       
   507         }
       
   508 
       
   509         // Make sure engine prepares for new image/video if necessary
       
   510         mEngine.initMode(mEngine.mode());
       
   511 
       
   512         startStandbyTimer();
       
   513 
       
   514     }
       
   515     CX_DEBUG_EXIT_FUNCTION();
       
   516 }
       
   517 
       
   518 /*!
       
   519 * Switch between cameras.
       
   520 */
       
   521 void CxuiViewManager::switchCamera()
       
   522 {
       
   523     CX_DEBUG_ENTER_FUNCTION();
       
   524 
       
   525     Cxe::CameraIndex nextCamera;
       
   526     Qt::Orientation nextViewOrientation;
       
   527 
       
   528     if (mEngine.cameraDeviceControl().cameraIndex() == Cxe::PrimaryCameraIndex) {
       
   529         nextCamera = Cxe::SecondaryCameraIndex;
       
   530         nextViewOrientation = Qt::Vertical;
       
   531     } else {
       
   532         nextCamera = Cxe::PrimaryCameraIndex;
       
   533         nextViewOrientation = Qt::Horizontal;
       
   534     }
       
   535 
       
   536     CxuiPrecaptureView* view = getPrecaptureView(mEngine.mode(), nextCamera);
       
   537     connectPreCaptureSignals(view);
       
   538     mMainWindow.setCurrentView(view, false);
       
   539     view->updateOrientation(nextViewOrientation);
       
   540 
       
   541     mEngine.cameraDeviceControl().switchCamera(nextCamera);
       
   542 
       
   543     CX_DEBUG_EXIT_FUNCTION();
       
   544 }
       
   545 
       
   546 /*!
       
   547 * Event filter function.
       
   548 * Used to get mouse and key events for standby and capture key handling.
       
   549 * @param object Target object.
       
   550 * @param event Event to be checked.
       
   551 */
       
   552 bool CxuiViewManager::eventFilter(QObject *object, QEvent *event)
       
   553 {
       
   554     Q_UNUSED(object)
       
   555 
       
   556     bool eventWasConsumed = false;
       
   557     bool userActivity = false;
       
   558 
       
   559     switch (event->type()) {
       
   560     case QEvent::KeyPress:
       
   561     case QEvent::KeyRelease:
       
   562         userActivity = true;
       
   563         eventWasConsumed = mKeyHandler->handleKeyEvent(event);
       
   564         if (eventWasConsumed && mApplicationState->currentState() == CxuiApplicationState::Standby) {
       
   565             // Queued exit from standby.
       
   566             emit standbyExitRequested();
       
   567         }
       
   568         break;
       
   569 
       
   570     case QEvent::GraphicsSceneMouseMove:
       
   571     case QEvent::GraphicsSceneMousePress:
       
   572         userActivity = true;
       
   573         break;
       
   574     case QEvent::GraphicsSceneMouseRelease:
       
   575         userActivity = true;
       
   576         if (mApplicationState->currentState() == CxuiApplicationState::Standby) {
       
   577             // Queued exit from standby.
       
   578             // Standby popup in view can receive mouse event before it is dismissed.
       
   579             emit standbyExitRequested();
       
   580         }
       
   581         break;
       
   582     }
       
   583 
       
   584     // Restart standby timer if there is user activity.
       
   585     // Only restart the timer if it is running, do not set it running here.
       
   586     if (userActivity && mStandbyTimer.isActive()) {
       
   587         startStandbyTimer();
       
   588     }
       
   589 
       
   590     // No need to call base class implementation, because we derive from QObject directly.
       
   591     // QObject::eventFilter() implementation always returns false.
       
   592     return eventWasConsumed;
       
   593 }
       
   594 
       
   595 /*!
       
   596 * Connect signals specific to given view.
       
   597 */
       
   598 void CxuiViewManager::connectSignals(CxuiView *view)
       
   599 {
       
   600     CX_DEBUG_ENTER_FUNCTION();
       
   601     OstTrace0(camerax_performance, CXUIVIEWMANAGER_CONNECTSIGNALS_1, "msg: e_CX_VIEWMANAGER_CONNECT_SIGNALS 1");
       
   602 
       
   603     if (view) {
       
   604         if (view == mViews[POSTCAPTURE_VIEW]) {
       
   605             connectPostCaptureSignals();
       
   606         } else if (view == mViews[SCENE_MODE_VIEW]) {
       
   607             connectSceneModeSignals();
       
   608         } else {
       
   609             connectPreCaptureSignals(static_cast<CxuiPrecaptureView *>(view));
       
   610         }
       
   611     }
       
   612 
       
   613     OstTrace0(camerax_performance, CXUIVIEWMANAGER_CONNECTSIGNALS_2, "msg: e_CX_VIEWMANAGER_CONNECT_SIGNALS 0");
       
   614     CX_DEBUG_EXIT_FUNCTION();
       
   615 }
       
   616 
       
   617 /*!
       
   618 * Disconnect signals.
       
   619 * We don't want to send or receive signals with inactive views, so this is done every time changing a view.
       
   620 * @param view View object from which signals are disconnected. If NULL is given, current view is used.
       
   621 */
       
   622 void CxuiViewManager::disconnectSignals(CxuiView *view)
       
   623 {
       
   624     CX_DEBUG_ENTER_FUNCTION();
       
   625     OstTrace0(camerax_performance, CXUIVIEWMANAGER_DISCONNECT_1, "msg: e_CX_VIEWMANAGER_DISCONNECT_SIGNALS 1");
       
   626 
       
   627     // Disconnect all existing capture key signals
       
   628     mKeyHandler->disconnect();
       
   629 
       
   630     if (!view) {
       
   631         // If view is not given, take current view.
       
   632         view = currentView();
       
   633     }
       
   634 
       
   635     CX_DEBUG(("CxuiViewManager - disconnecting from view %d", view));
       
   636     if (view) {
       
   637         // Disconnect all signals from current view to us.
       
   638         disconnect(view, 0, this, 0);
       
   639         // Disconnect all signals from us to current view.
       
   640         disconnect(this, 0, view, 0);
       
   641     }
       
   642 
       
   643     OstTrace0(camerax_performance, CXUIVIEWMANAGER_DISCONNECT_2, "msg: e_CX_VIEWMANAGER_DISCONNECT_SIGNALS 0");
       
   644     CX_DEBUG_EXIT_FUNCTION();
       
   645 }
       
   646 
       
   647 /*!
       
   648 * Connect signals to pre-capture view.
       
   649 */
       
   650 void CxuiViewManager::connectPreCaptureSignals(CxuiPrecaptureView *view)
       
   651 {
       
   652     CX_DEBUG_ENTER_FUNCTION();
       
   653 
       
   654     // Disconnect from the current, "old" view
       
   655     disconnectSignals();
       
   656 
       
   657     // connecting pre-capture view signals to standby timer.
       
   658     connect(view, SIGNAL(startStandbyTimer()),       this, SLOT(startStandbyTimer()), Qt::UniqueConnection);
       
   659     connect(view, SIGNAL(stopStandbyTimer()),        this, SLOT(stopStandbyTimer()),  Qt::UniqueConnection);
       
   660     connect(view, SIGNAL(changeToPrecaptureView()),  this, SLOT(startStandbyTimer()), Qt::UniqueConnection);
       
   661     connect(view, SIGNAL(changeToPostcaptureView()), this, SLOT(stopStandbyTimer()),  Qt::UniqueConnection);
       
   662 
       
   663     // connecting pre-capture view signals to viewmanager slots
       
   664     connect(view, SIGNAL(changeToPostcaptureView()), this, SLOT(changeToPostcaptureView()), Qt::UniqueConnection);
       
   665     connect(view, SIGNAL(changeToPrecaptureView()),  this, SLOT(changeToPrecaptureView()),  Qt::UniqueConnection);
       
   666 
       
   667     //connecting scene modes signal
       
   668     connect(view, SIGNAL(showScenesView()), this, SLOT(showScenesView()), Qt::UniqueConnection);
       
   669 
       
   670     connect(view, SIGNAL(switchCamera()), this, SLOT(switchCamera()), Qt::UniqueConnection);
       
   671 
       
   672     // connecting error signals from precapture view to application state.
       
   673     connect(view, SIGNAL(errorEncountered(CxeError::Id)),
       
   674             mApplicationState, SLOT(handleApplicationError(CxeError::Id)),
       
   675             Qt::UniqueConnection);
       
   676 
       
   677     // Standby signals
       
   678     connect(this, SIGNAL(normalStateEntered()), view, SLOT(exitStandby()), Qt::UniqueConnection);
       
   679     connect(this, SIGNAL(normalStateExited()), view, SLOT(enterStandby()), Qt::UniqueConnection);
       
   680 
       
   681     // connects all capture key signals.
       
   682     connectCaptureKeySignals(view);
       
   683 
       
   684     CX_DEBUG_EXIT_FUNCTION();
       
   685 }
       
   686 
       
   687 /*!
       
   688 * Connect signals to post-capture view.
       
   689 */
       
   690 void CxuiViewManager::connectPostCaptureSignals()
       
   691 {
       
   692     CX_DEBUG_ENTER_FUNCTION();
       
   693 
       
   694     // Disconnect from the current, "old" view
       
   695     disconnectSignals();
       
   696 
       
   697     CxuiView *view = mViews[POSTCAPTURE_VIEW];
       
   698     if (view) {
       
   699         connect(view, SIGNAL(changeToPrecaptureView()), this, SLOT(changeToPrecaptureView()), Qt::UniqueConnection);
       
   700 
       
   701         // Standby signals
       
   702         connect(this, SIGNAL(normalStateEntered()), view, SLOT(exitStandby()), Qt::UniqueConnection);
       
   703         connect(this, SIGNAL(normalStateExited()), view, SLOT(enterStandby()), Qt::UniqueConnection);
       
   704 
       
   705         // connect necessary capturekey signals
       
   706         connectCaptureKeySignals(view);
       
   707     }
       
   708     CX_DEBUG_EXIT_FUNCTION();
       
   709 }
       
   710 
       
   711 /*!
       
   712 * Connect signals to scene mode view.
       
   713 */
       
   714 void CxuiViewManager::connectSceneModeSignals()
       
   715 {
       
   716     CX_DEBUG_ENTER_FUNCTION();
       
   717 
       
   718     // Disconnect from the current, "old" view
       
   719     disconnectSignals();
       
   720 
       
   721     CxuiView *view = mViews[SCENE_MODE_VIEW];
       
   722     if (view) {
       
   723         // Standby signals for releasing camera
       
   724         connect(this, SIGNAL(normalStateEntered()), view, SLOT(exitStandby()));
       
   725         connect(this, SIGNAL(normalStateExited()), view, SLOT(enterStandby()));
       
   726 
       
   727         // Moving back to pre-capture view
       
   728         connect(view, SIGNAL(viewCloseEvent()), this, SLOT(changeToPrecaptureView()));
       
   729 
       
   730         connectCaptureKeySignals(view);
       
   731     }
       
   732     CX_DEBUG_EXIT_FUNCTION();
       
   733 }
       
   734 
       
   735 /*!
       
   736 * Connect key handler capture key signals.
       
   737 */
       
   738 void CxuiViewManager::connectCaptureKeySignals(CxuiView *view)
       
   739 {
       
   740     CX_DEBUG_ENTER_FUNCTION();
       
   741 
       
   742     // Disconnect all existing capture key signals
       
   743     mKeyHandler->disconnect();
       
   744 
       
   745     if (view) {
       
   746         // If the view class does not implement the named slot, the connect will fail
       
   747         // and output some warnings as debug prints. This is by design.
       
   748         connect(mKeyHandler, SIGNAL(autofocusKeyPressed()),  view, SLOT(handleAutofocusKeyPressed()));
       
   749         connect(mKeyHandler, SIGNAL(autofocusKeyReleased()), view, SLOT(handleAutofocusKeyReleased()));
       
   750         connect(mKeyHandler, SIGNAL(captureKeyPressed()),    view, SLOT(handleCaptureKeyPressed()));
       
   751         connect(mKeyHandler, SIGNAL(captureKeyReleased()),   view, SLOT(handleCaptureKeyReleased()));
       
   752     }
       
   753 
       
   754     CX_DEBUG_EXIT_FUNCTION();
       
   755 }
       
   756 
       
   757 /*!
       
   758  * Clears all camera activities from activity manager.
       
   759  */
       
   760 void CxuiViewManager::clearAllActivities()
       
   761 {
       
   762     HbActivityManager *activityManager = mApplication.activityManager();
       
   763     activityManager->removeActivity(CxuiActivityIds::STILL_PRECAPTURE_ACTIVITY);
       
   764     activityManager->removeActivity(CxuiActivityIds::STILL_POSTCAPTURE_ACTIVITY);
       
   765     activityManager->removeActivity(CxuiActivityIds::VIDEO_PRECAPTURE_ACTIVITY);
       
   766     activityManager->removeActivity(CxuiActivityIds::VIDEO_POSTCAPTURE_ACTIVITY);
       
   767 }
       
   768 
       
   769 /*!
       
   770 * Raise application to foreground if it's currently (fully) in background.
       
   771 */
       
   772 void CxuiViewManager::toForeground()
       
   773 {
       
   774     CX_DEBUG_ENTER_FUNCTION();
       
   775 
       
   776     if (mApplicationState->currentState() == CxuiApplicationState::Background) {
       
   777         // Bring to foreground and gain focus.
       
   778         CX_DEBUG(("CxuiViewManager - in background, bringing to foreground now."));
       
   779         mMainWindow.raise();
       
   780         mMainWindow.activateWindow();
       
   781     }
       
   782 
       
   783     CX_DEBUG_EXIT_FUNCTION();
       
   784 }
       
   785 
       
   786 // end of file