camerauis/cameraxui/cxui/src/cxuiselftimer.cpp
changeset 19 d9aefe59d544
child 21 fa6d9f75d6a6
equal deleted inserted replaced
3:8b2d6d0384b0 19:d9aefe59d544
       
     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 #include <QGraphicsWidget>
       
    18 #include <hblabel.h>
       
    19 #include <hbpushbutton.h>
       
    20 
       
    21 #include "cxuiselftimer.h"
       
    22 #include "cxutils.h"
       
    23 #include "cxuienums.h"
       
    24 #include "cxuidocumentloader.h"
       
    25 #include "cxesettings.h"
       
    26 #include "cxenamespace.h" // CxeSettingIds
       
    27 #include "cxeerror.h"
       
    28 
       
    29 using namespace CxUiLayout;
       
    30 
       
    31 /*!
       
    32     \class CxuiSelfTimer
       
    33     \brief Class for handling selftimer countdown.
       
    34 
       
    35     CxuiSelfTimer class handles the selftimer countdown and updates widgets
       
    36     accordingly.
       
    37 */
       
    38 
       
    39 // constants
       
    40 const int CONTINUOUS_POSTCAPTURE    = -1;
       
    41 const int UNKNOWN                   = -99;
       
    42 
       
    43 CxuiSelfTimer::CxuiSelfTimer(CxeSettings &settings)
       
    44 :  mDelay(-1),
       
    45    mCounter(0),
       
    46    mTimer(this),
       
    47    mOldPostCaptureTimeOut(UNKNOWN),
       
    48    mIndicatorContainer(NULL),
       
    49    mButtonContainer(NULL),
       
    50    mTimerLabel(NULL),
       
    51    mCancelButton(NULL),
       
    52    mStartButton(NULL),
       
    53    mSettings(settings)
       
    54 {
       
    55     CX_DEBUG_ENTER_FUNCTION();
       
    56 
       
    57     connect(&mTimer, SIGNAL(timeout()), this, SLOT(timeout()));
       
    58     mTimer.setSingleShot(false);
       
    59 
       
    60     CX_DEBUG_EXIT_FUNCTION();
       
    61 }
       
    62 
       
    63 CxuiSelfTimer::~CxuiSelfTimer()
       
    64 {
       
    65     CX_DEBUG_IN_FUNCTION();
       
    66 }
       
    67 
       
    68 /*!
       
    69     Get pointers to the UI widgets from the documentloader.
       
    70 
       
    71     \param documentLoader Pointer to a CxuiDocumentLoader instance that has
       
    72     loaded the docml containing selftimer widgets.
       
    73  */
       
    74 void CxuiSelfTimer::loadSelftimerWidgets(CxuiDocumentLoader *documentLoader) {
       
    75 
       
    76     if (documentLoader) {
       
    77 
       
    78         QGraphicsWidget *widget = NULL;
       
    79         
       
    80         widget = documentLoader->findWidget(STILL_PRE_CAPTURE_SELFTIMER_CONTAINER);
       
    81         mIndicatorContainer = qobject_cast<HbWidget *>(widget);
       
    82         CX_DEBUG_ASSERT(mIndicatorContainer);
       
    83 
       
    84         widget = documentLoader->findWidget(STILL_PRE_CAPTURE_SELFTIMER_BUTTON_CONTAINER);
       
    85         mButtonContainer = qobject_cast<HbWidget *>(widget);
       
    86         CX_DEBUG_ASSERT(mButtonContainer);
       
    87 
       
    88         widget = documentLoader->findWidget(STILL_PRE_CAPTURE_SELFTIMER_COUNTER);
       
    89         mTimerLabel = qobject_cast<HbLabel *>(widget);
       
    90         CX_DEBUG_ASSERT(mTimerLabel);
       
    91 
       
    92         widget = documentLoader->findWidget(STILL_PRE_CAPTURE_SELFTIMER_CANCEL_BUTTON);
       
    93         mCancelButton = qobject_cast<HbPushButton *>(widget);
       
    94         CX_DEBUG_ASSERT(mCancelButton);
       
    95 
       
    96         widget = documentLoader->findWidget(STILL_PRE_CAPTURE_SELFTIMER_START_BUTTON);
       
    97         mStartButton = qobject_cast<HbPushButton *>(widget);
       
    98         CX_DEBUG_ASSERT(mStartButton);
       
    99 
       
   100         connect(mCancelButton, SIGNAL(released()), this, SLOT(cancel()));
       
   101         connect(mStartButton, SIGNAL(released()), this, SLOT(startTimer()));
       
   102 
       
   103         updateWidgets();
       
   104     }
       
   105 }
       
   106 
       
   107 /*!
       
   108     Method for checking if selftimer is enabled.
       
   109     Selftimer is enabled/disabled using slots cancel() and changeTimeout()
       
   110     \return True if selftimer is enabled
       
   111 
       
   112     \sa changeTimeOut(int seconds)
       
   113     \sa cancel()
       
   114  */
       
   115 bool CxuiSelfTimer::isEnabled()
       
   116 {
       
   117     return (mDelay >= 0);
       
   118 }
       
   119 
       
   120 /*!
       
   121     Method for checking if selftimer countdown is ongoing.
       
   122     Countdown is started with by calling startTimer().
       
   123     \return True if selftimer is active.
       
   124 
       
   125     \sa startTimer()
       
   126  */
       
   127 bool CxuiSelfTimer::isOngoing()
       
   128 {
       
   129     return mTimer.isActive();
       
   130 }
       
   131 
       
   132 /*! Slot for canceling the selftimer.
       
   133     Disables selftimer, sets back the postcapturetimeout if it
       
   134     has been changed by selftimer and emits signal to notify interested
       
   135     parties that cancel has been called.
       
   136 
       
   137     \sa startTimer()
       
   138  */
       
   139 void CxuiSelfTimer::cancel()
       
   140 {
       
   141     CX_DEBUG_ENTER_FUNCTION();
       
   142 
       
   143     // show postcapture setting is changed when selftimer is started
       
   144     // if it has been changed, then change it back
       
   145     if (mOldPostCaptureTimeOut != UNKNOWN) {
       
   146         mSettings.set(CxeSettingIds::STILL_SHOWCAPTURED, mOldPostCaptureTimeOut);
       
   147         mOldPostCaptureTimeOut = UNKNOWN;
       
   148     }
       
   149 
       
   150     // Reset counter and stop any ongoing action.
       
   151     // Start button back to enabled.
       
   152     reset(false);
       
   153     // Disable self timer
       
   154     mDelay = -1;
       
   155 
       
   156     // Update UI components.
       
   157     updateWidgets();
       
   158 
       
   159     // Signal others about cancellation.
       
   160     emit cancelled();
       
   161 
       
   162     CX_DEBUG_EXIT_FUNCTION();
       
   163 }
       
   164 
       
   165 /*!
       
   166     Slot for handling timer timouts. Counter will be updated and when
       
   167     selftimer countdown is finished, timerFinished() signal is emitted.
       
   168  */
       
   169 void CxuiSelfTimer::timeout()
       
   170 {
       
   171     CX_DEBUG_ENTER_FUNCTION();
       
   172 
       
   173     // Update seconds passed.
       
   174     ++mCounter;
       
   175 
       
   176     // Update now to have better looking user experience,
       
   177     // especially when counter elapses: "0" is updated before emitting event,
       
   178     // so the UI seems to update smoother.
       
   179     updateWidgets();
       
   180 
       
   181     // Check if timer ran out
       
   182     if (mCounter >= mDelay) {
       
   183         mTimer.stop();
       
   184         emit timerFinished();
       
   185     }
       
   186 
       
   187     CX_DEBUG_EXIT_FUNCTION();
       
   188 }
       
   189 
       
   190 /*!
       
   191     Slot for resetting the selftimer countdown. Countdown is stopped,
       
   192     and set back to starting value. Start button is set enabled.
       
   193 
       
   194  */
       
   195 void CxuiSelfTimer::reset()
       
   196 {
       
   197     CX_DEBUG_ENTER_FUNCTION();
       
   198     reset(true);
       
   199     CX_DEBUG_EXIT_FUNCTION();
       
   200 }
       
   201 
       
   202 /*!
       
   203      Slot for starting the countdown. Show postcapture setting is set to
       
   204      continuous and previous value stored. Timer is started.
       
   205 
       
   206      \sa cancel()
       
   207  */
       
   208 void CxuiSelfTimer::startTimer()
       
   209 {
       
   210     CX_DEBUG_ENTER_FUNCTION();
       
   211 
       
   212     // get the current postcapture timeout
       
   213     CxeError::Id error = mSettings.get(CxeSettingIds::STILL_SHOWCAPTURED, mOldPostCaptureTimeOut);
       
   214 
       
   215     if (error == CxeError::None) {
       
   216         // set continuous postcapture (view is visible until dismissed)
       
   217         mSettings.set(CxeSettingIds::STILL_SHOWCAPTURED, CONTINUOUS_POSTCAPTURE);
       
   218     } else {
       
   219         // if there was an error, don't modify the postcapture setting
       
   220         mOldPostCaptureTimeOut = UNKNOWN;
       
   221     }
       
   222 
       
   223     if (mStartButton) {
       
   224         mStartButton->setEnabled(false);
       
   225     }
       
   226 
       
   227     // start countdown
       
   228     mCounter = 0;
       
   229     mTimer.start(1000); // 1000 milliseconds == 1 second
       
   230 
       
   231     CX_DEBUG_EXIT_FUNCTION();
       
   232 }
       
   233 
       
   234 /*!
       
   235     Slot for receiving changes to the selftimer countdown timeout.
       
   236 
       
   237     \param seconds Amount of seconds for the selftimer countdown. If the value is
       
   238     0 then selftimer is disabled.
       
   239  */
       
   240 void CxuiSelfTimer::changeTimeOut(int seconds)
       
   241 {
       
   242     CX_DEBUG_ENTER_FUNCTION();
       
   243 
       
   244     // make sure that timer is not running (should not be)
       
   245     reset(false);
       
   246 
       
   247     // set the new timer value
       
   248     // if incoming value is 0, it means that the selftimer is disabled
       
   249     if (seconds > 0) {
       
   250         mDelay = seconds;
       
   251     } else {
       
   252         //value == 0
       
   253         mDelay = -1;
       
   254     }
       
   255 
       
   256     updateWidgets();
       
   257 
       
   258     CX_DEBUG_EXIT_FUNCTION();
       
   259 }
       
   260 
       
   261 /*! Resets the selftimer countdown.
       
   262     Countdown is stopped, and set back to starting value. Start button is set enabled.
       
   263     @param update Update the UI widgets if true, skip update if false.
       
   264     Can be used to by-pass the UI update if planned to do shortly again.
       
   265  */
       
   266 void CxuiSelfTimer::reset(bool update)
       
   267 {
       
   268     // Stop timer and reset counter.
       
   269     mTimer.stop();
       
   270     mCounter = 0;
       
   271 
       
   272     // Set start buttonback to enabled.
       
   273     if (mStartButton) {
       
   274         mStartButton->setEnabled(true);
       
   275     }
       
   276 
       
   277     // Update UI if requested.
       
   278     if(update) {
       
   279         updateWidgets();
       
   280     }
       
   281 }
       
   282 
       
   283 /*! Updates the selftimer widgets
       
   284     Changes the visibility of the selftimer widgets (counter and cancel button)
       
   285     based on the timeoutValue. Updates the counter value.
       
   286  */
       
   287 void CxuiSelfTimer::updateWidgets()
       
   288 {
       
   289     CX_DEBUG_ENTER_FUNCTION();
       
   290 
       
   291     int remaining(mDelay - mCounter);
       
   292     CX_DEBUG(("remaining time %d", remaining));
       
   293 
       
   294     if (mTimerLabel) {
       
   295         mTimerLabel->setPlainText(QString::number(remaining));
       
   296     }
       
   297 
       
   298     if (isEnabled()) {
       
   299         showWidgets();
       
   300     } else {
       
   301         hideWidgets();
       
   302     }
       
   303 
       
   304     CX_DEBUG_EXIT_FUNCTION();
       
   305 }
       
   306 
       
   307 /*!
       
   308     Shows the selftimer widgets.
       
   309  */
       
   310 void CxuiSelfTimer::showWidgets()
       
   311 {
       
   312     if (mIndicatorContainer){
       
   313         mIndicatorContainer->show();
       
   314     }
       
   315 
       
   316     if (mButtonContainer){
       
   317         mButtonContainer->show();
       
   318     }
       
   319 
       
   320 }
       
   321 
       
   322 /*!
       
   323     Hides the selftimer widgets.
       
   324  */
       
   325 void CxuiSelfTimer::hideWidgets()
       
   326 {
       
   327     if (mIndicatorContainer){
       
   328         mIndicatorContainer->hide();
       
   329     }
       
   330 
       
   331     if (mButtonContainer){
       
   332         mButtonContainer->hide();
       
   333     }
       
   334 
       
   335 }