camerauis/cameraxui/cxui/src/cxuistandby.cpp
changeset 37 64817133cd1d
parent 36 b12f3922a74f
child 44 2ce653e4920d
child 48 42ba2d16bf40
equal deleted inserted replaced
36:b12f3922a74f 37:64817133cd1d
     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:
       
    15 *
       
    16 */
       
    17 
       
    18 #include <QTimer>
       
    19 #include <QApplication>
       
    20 #include <QGraphicsSceneEvent>
       
    21 #include <hblabel.h>
       
    22 #include <hbdialog.h>
       
    23 #include <hbinstantfeedback.h>
       
    24 #include <QGraphicsRectItem>
       
    25 #include <QColor>
       
    26 
       
    27 #include "cxutils.h"
       
    28 #include "cxeengine.h"
       
    29 #include "cxuienums.h"
       
    30 #include "cxuistandby.h"
       
    31 #include "cxuidocumentloader.h"
       
    32 #include "cxeviewfindercontrol.h"
       
    33 #include "cxuicapturekeyhandler.h"
       
    34 #include "cxestillcapturecontrol.h"
       
    35 #include "cxevideocapturecontrol.h"
       
    36 
       
    37 
       
    38 
       
    39 /*!
       
    40  * Constructor
       
    41  */
       
    42 CxuiStandby::CxuiStandby(CxuiCaptureKeyHandler &keyHandler,CxuiDocumentLoader *documentLoader, CxeEngine *engine)
       
    43 : mKeyHandler(keyHandler),
       
    44   mDocumentLoader(documentLoader),
       
    45   mEngine(engine),
       
    46   mStandbyPopup(NULL),
       
    47   mStandbyDialogVisible(false),
       
    48   mAllowDismiss(true)
       
    49 {
       
    50     CX_DEBUG_ENTER_FUNCTION();
       
    51     CX_ASSERT_ALWAYS(engine);
       
    52 
       
    53     // initialize standby timer
       
    54     mStandbyTimer = new QTimer(this);
       
    55 
       
    56     // install event filter for application wide events
       
    57     QCoreApplication::instance()->installEventFilter(this);
       
    58 
       
    59     CX_ASSERT_ALWAYS(mStandbyTimer);
       
    60     connect(mStandbyTimer, SIGNAL(timeout()), this, SLOT(enterStandby()));
       
    61     mStandbyTimer->setSingleShot(true);
       
    62 
       
    63     CX_DEBUG_EXIT_FUNCTION();
       
    64 }
       
    65 
       
    66 
       
    67 
       
    68 /*!
       
    69  * Destructor
       
    70  */
       
    71 CxuiStandby::~CxuiStandby()
       
    72 {
       
    73     CX_DEBUG_IN_FUNCTION();
       
    74     // remove the event filter
       
    75     QCoreApplication::instance()->removeEventFilter(this);
       
    76     // stop standby timer
       
    77     stopTimer();
       
    78 }
       
    79 
       
    80 
       
    81 /*!
       
    82 * Allow dismissing standby mode with AF or capture key?
       
    83 * @param allow Is dismissing allowed.
       
    84 */
       
    85 void CxuiStandby::allowDismiss(bool allow)
       
    86 {
       
    87     CX_DEBUG_ENTER_FUNCTION();
       
    88     mAllowDismiss = allow;
       
    89     if (allow) {
       
    90         if (mStandbyDialogVisible) {
       
    91             // Reconnect the close signals if dialog is visible
       
    92             connect(&mKeyHandler, SIGNAL(autofocusKeyPressed()), mStandbyPopup, SLOT(close()), Qt::UniqueConnection);
       
    93             connect(&mKeyHandler, SIGNAL(captureKeyPressed()),   mStandbyPopup, SLOT(close()), Qt::UniqueConnection);
       
    94         }
       
    95     } else {
       
    96         disconnect(&mKeyHandler, SIGNAL(autofocusKeyPressed()), mStandbyPopup, SLOT(close()));
       
    97         disconnect(&mKeyHandler, SIGNAL(captureKeyPressed()),   mStandbyPopup, SLOT(close()));
       
    98     }
       
    99 
       
   100     CX_DEBUG_EXIT_FUNCTION();
       
   101 }
       
   102 
       
   103 
       
   104 /*!
       
   105 * Is standby mode active?
       
   106 * @return True, if standby mode is active, false if not.
       
   107 */
       
   108 bool CxuiStandby::isActive() const
       
   109 {
       
   110     return mStandbyDialogVisible;
       
   111 }
       
   112 
       
   113 /*!
       
   114  * stops standby timer
       
   115  */
       
   116 void CxuiStandby::stopTimer()
       
   117 {
       
   118     if(mStandbyTimer) {
       
   119         mStandbyTimer->stop();
       
   120     }
       
   121 }
       
   122 
       
   123 /*!
       
   124  * starts standby timer
       
   125  */
       
   126 void CxuiStandby::startTimer()
       
   127 {
       
   128     if(mStandbyTimer) {
       
   129         mStandbyTimer->start(CXUI_STANDBY_CAMERA_TIMEOUT);
       
   130     }
       
   131 }
       
   132 
       
   133 /*!
       
   134  * Handles mouse press events
       
   135  * returns if mouse key press is consumed.
       
   136  * \param event event to be handled
       
   137  * \return boolean to indicate whether the event was handled or not
       
   138  */
       
   139 bool CxuiStandby::handleMouseEvent(QEvent *event)
       
   140 {
       
   141     bool keyHandled = false;
       
   142 
       
   143     // close the dialog if it's visible
       
   144     if (mStandbyDialogVisible && mStandbyPopup) {
       
   145         HbInstantFeedback feedback(HbFeedback::BasicItem);
       
   146         switch (event->type()) {
       
   147             case QEvent::GraphicsSceneMousePress:
       
   148                 feedback.play();
       
   149                 break;
       
   150             case QEvent::GraphicsSceneMouseRelease:
       
   151                 if (mAllowDismiss) {
       
   152                     CX_DEBUG(( "closing the popup mStandbyDialogVisible = : %d", mStandbyDialogVisible ));
       
   153                     // todo: sound disabling doesn't work in orbit yet so don't do feedback on release
       
   154                     // needs to be enabled when orbit support is done
       
   155                     //feedback.setModalities(HbFeedback::Tactile);
       
   156                     //feedback.play();
       
   157                     exitStandby();
       
   158                 }
       
   159                 break;
       
   160             default:
       
   161                 break;
       
   162         }
       
   163         // eat all mouse events when standby is active
       
   164         keyHandled = true;
       
   165     } else if (mStandbyTimer && mStandbyTimer->isActive()) {
       
   166         // restart the timer only if it's running
       
   167         startTimer();
       
   168     }
       
   169 
       
   170     return keyHandled;
       
   171 }
       
   172 
       
   173 
       
   174 /*!
       
   175  * switching to standby.
       
   176  */
       
   177 void CxuiStandby::enterStandby()
       
   178 {
       
   179     CX_DEBUG_ENTER_FUNCTION();
       
   180 
       
   181     if (proceedToStandy()) {
       
   182 
       
   183         // signal for ui classes to prepare for standby
       
   184         emit aboutToEnterStandby();
       
   185 
       
   186         mStandbyDialogVisible = true;
       
   187 
       
   188         if (mStandbyPopup == NULL) {
       
   189             CX_DEBUG(("Loading standby DocML"));
       
   190             bool ok = false;
       
   191             // Use document loader to create popup
       
   192             mDocumentLoader->load(CxUiLayout::STANDBY_POPUP_XML, &ok);
       
   193             CX_DEBUG(("standby load ok=%d", ok));
       
   194             mStandbyPopup = qobject_cast<HbDialog*>(mDocumentLoader->findWidget(CxUiLayout::STANDBY_POPUP));
       
   195             CX_ASSERT_ALWAYS(mStandbyPopup);
       
   196             mStandbyPopup->setTimeout(HbDialog::NoTimeout);
       
   197             mStandbyPopup->setBackgroundFaded(false);
       
   198             mStandbyPopup->setPreferredPos(QPointF(0,0));
       
   199             // color of standby text is set in the code. It cannot be done in docml
       
   200             HbLabel* label = qobject_cast<HbLabel*>(mDocumentLoader->findWidget(CxUiLayout::STANDBY_TEXT_WIDGET));
       
   201             label->setTextColor(Qt::white);
       
   202 
       
   203             // connecting "abouttoclose" signal to dismissStandby
       
   204             connect(mStandbyPopup, SIGNAL(aboutToClose()), this, SLOT(dismissStandby()));
       
   205 
       
   206             // HbDialog's default background item is replaced with black rectangle
       
   207             QGraphicsRectItem *backgroundItem = new QGraphicsRectItem();
       
   208             QBrush blackBrush = QBrush(Qt::black);
       
   209             backgroundItem->setBrush(blackBrush);
       
   210             QGraphicsItem *origBgItem = mStandbyPopup->backgroundItem();
       
   211             backgroundItem->setRect(origBgItem->boundingRect());
       
   212             mStandbyPopup->setBackgroundItem(backgroundItem);
       
   213 
       
   214         }
       
   215 
       
   216         CX_ASSERT_ALWAYS(mStandbyPopup);
       
   217 
       
   218         mStandbyPopup->show();
       
   219         // connecting half press or full press key signal to dismiss standby
       
   220         connect(&mKeyHandler, SIGNAL(autofocusKeyPressed()), mStandbyPopup, SLOT(close()), Qt::UniqueConnection);
       
   221         connect(&mKeyHandler, SIGNAL(captureKeyPressed()),   mStandbyPopup, SLOT(close()), Qt::UniqueConnection);
       
   222     }
       
   223 
       
   224     CX_DEBUG_EXIT_FUNCTION();
       
   225 }
       
   226 
       
   227 /*!
       
   228 * Close the standby dialog.
       
   229 */
       
   230 void CxuiStandby::exitStandby()
       
   231 {
       
   232     CX_DEBUG_ENTER_FUNCTION();
       
   233 
       
   234     if (mAllowDismiss && mStandbyDialogVisible && mStandbyPopup) {
       
   235         mStandbyPopup->close();
       
   236     }
       
   237 
       
   238     CX_DEBUG_EXIT_FUNCTION();
       
   239 }
       
   240 
       
   241 
       
   242 /*!
       
   243  * dismisses standby
       
   244  */
       
   245 void CxuiStandby::dismissStandby()
       
   246 {
       
   247     CX_DEBUG_ENTER_FUNCTION();
       
   248 
       
   249     if(mStandbyDialogVisible) {
       
   250         // stop the standby timer and close the pop-up
       
   251         mStandbyDialogVisible = false;
       
   252         //restart timer
       
   253         startTimer();
       
   254         // signal for ui classes to prepare for standby exit
       
   255         emit aboutToExitStandby();
       
   256     }
       
   257 
       
   258     CX_DEBUG_EXIT_FUNCTION();
       
   259 }
       
   260 
       
   261 
       
   262 
       
   263 /*!
       
   264  * checks if we can switch to standby
       
   265  */
       
   266 bool CxuiStandby::proceedToStandy()
       
   267 {
       
   268     CX_DEBUG_ENTER_FUNCTION();
       
   269     CX_ASSERT_ALWAYS(mEngine);
       
   270 
       
   271     bool ok = false;
       
   272     if (!mStandbyDialogVisible) {
       
   273         CX_DEBUG(("show standby dialog"));
       
   274         ok = true;
       
   275     }
       
   276 
       
   277     CX_DEBUG(( "CxuiStandby::proceedToStandy proceedToStandy: %d", ok ));
       
   278 
       
   279     return ok;
       
   280 }
       
   281 
       
   282 
       
   283 
       
   284 /*!
       
   285  *  Event filter which filters application wide mouse events.
       
   286  */
       
   287 bool CxuiStandby::eventFilter(QObject *object, QEvent *event)
       
   288 {
       
   289     Q_UNUSED(object);
       
   290 
       
   291     bool eventWasConsumed = false;
       
   292     switch (event->type()) {
       
   293         case QEvent::GraphicsSceneMouseMove:
       
   294         case QEvent::GraphicsSceneMousePress:
       
   295         case QEvent::GraphicsSceneMouseRelease:
       
   296             eventWasConsumed = handleMouseEvent(event);
       
   297             break;
       
   298         default:
       
   299             break;
       
   300     }
       
   301     return eventWasConsumed;
       
   302 }
       
   303 
       
   304 // end of file