camerauis/cameraxui/cxui/src/cxuiselftimer.cpp
branchRCL_3
changeset 24 bac7acad7cb3
parent 23 61bc0f252b2b
child 25 2c87b2808fd7
equal deleted inserted replaced
23:61bc0f252b2b 24: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 <QGraphicsWidget>
       
    18 #include <hblabel.h>
       
    19 #include <hbpushbutton.h>
       
    20 
       
    21 #ifdef Q_OS_SYMBIAN
       
    22 #include <ProfileEngineSDKCRKeys.h>
       
    23 #endif
       
    24 
       
    25 #include "cxuiselftimer.h"
       
    26 #include "cxutils.h"
       
    27 #include "cxuienums.h"
       
    28 #include "cxuidocumentloader.h"
       
    29 #include "cxesettings.h"
       
    30 #include "cxenamespace.h" // CxeSettingIds
       
    31 #include "cxeerror.h"
       
    32 #include "cxeexception.h"
       
    33 
       
    34 using namespace CxUiLayout;
       
    35 
       
    36 /*!
       
    37     \class CxuiSelfTimer
       
    38     \brief Class for handling selftimer countdown.
       
    39 
       
    40     CxuiSelfTimer class handles the selftimer countdown and updates widgets
       
    41     accordingly.
       
    42 */
       
    43 
       
    44 // constants
       
    45 
       
    46 const int CONTINUOUS_POSTCAPTURE    = -1;
       
    47 const int UNKNOWN                   = -99;
       
    48 
       
    49 const static QString SELFTIMER_SOUND = "z:\\system\\sounds\\digital\\selftimer.wav";
       
    50 
       
    51 CxuiSelfTimer::CxuiSelfTimer(CxeSettings &settings)
       
    52 :  mDelay(-1),
       
    53    mCounter(0),
       
    54    mTimer(this),
       
    55    mOldPostCaptureTimeOut(UNKNOWN),
       
    56    mWidgetContainer(NULL),
       
    57    mTimerLabel(NULL),
       
    58    mCancelButton(NULL),
       
    59    mStartButton(NULL),
       
    60    mSettings(settings),
       
    61    mSound(SELFTIMER_SOUND),
       
    62    mUseSound(true)
       
    63 {
       
    64     CX_DEBUG_ENTER_FUNCTION();
       
    65 
       
    66     connect(&mTimer, SIGNAL(timeout()), this, SLOT(timeout()));
       
    67     mTimer.setSingleShot(false);
       
    68 
       
    69     // connect to capture sound signal in order to monitor
       
    70     // warning tone changes
       
    71     connect(&mSettings,
       
    72             SIGNAL(settingValueChanged(long int, unsigned long int, QVariant)),
       
    73             this, SLOT(enableSound(long int, unsigned long int, QVariant)));
       
    74 
       
    75     // get initial warning tone value from profile
       
    76     QVariant value(0);
       
    77 
       
    78 #ifdef Q_OS_SYMBIAN
       
    79     // get current profile setting for using camerasound
       
    80     // camera sound follows warning tone setting
       
    81     unsigned long int key = KProEngActiveWarningTones;
       
    82     long int uid = KCRUidProfileEngine.iUid;
       
    83     mSettings.get(uid, key, Cxe::Repository, value);
       
    84 #endif
       
    85 
       
    86     // possible values are:
       
    87     // 0 -> warning tones off
       
    88     // 1 -> warning tones on
       
    89     mUseSound = (value.toInt() == 1);
       
    90     CX_DEBUG(("Warning tones enabled [%d]", value.toInt()));
       
    91 
       
    92     CX_DEBUG_EXIT_FUNCTION();
       
    93 }
       
    94 
       
    95 CxuiSelfTimer::~CxuiSelfTimer()
       
    96 {
       
    97     CX_DEBUG_IN_FUNCTION();
       
    98 }
       
    99 
       
   100 /*!
       
   101     Get pointers to the UI widgets from the documentloader.
       
   102 
       
   103     \param documentLoader Pointer to a CxuiDocumentLoader instance that has
       
   104     loaded the docml containing selftimer widgets.
       
   105  */
       
   106 void CxuiSelfTimer::loadSelftimerWidgets(CxuiDocumentLoader *documentLoader) {
       
   107 
       
   108     if (documentLoader) {
       
   109 
       
   110         QGraphicsWidget *widget = NULL;
       
   111         
       
   112         widget = documentLoader->findWidget(STILL_PRE_CAPTURE_SELFTIMER_CONTAINER);
       
   113         mWidgetContainer = qobject_cast<HbWidget *>(widget);
       
   114         CX_DEBUG_ASSERT(mWidgetContainer);
       
   115 
       
   116         widget = documentLoader->findWidget(STILL_PRE_CAPTURE_SELFTIMER_COUNTER);
       
   117         mTimerLabel = qobject_cast<HbLabel *>(widget);
       
   118         CX_DEBUG_ASSERT(mTimerLabel);
       
   119 
       
   120         widget = documentLoader->findWidget(STILL_PRE_CAPTURE_SELFTIMER_CANCEL_BUTTON);
       
   121         mCancelButton = qobject_cast<HbPushButton *>(widget);
       
   122         CX_DEBUG_ASSERT(mCancelButton);
       
   123 
       
   124         widget = documentLoader->findWidget(STILL_PRE_CAPTURE_SELFTIMER_START_BUTTON);
       
   125         mStartButton = qobject_cast<HbPushButton *>(widget);
       
   126         CX_DEBUG_ASSERT(mStartButton);
       
   127 
       
   128         connect(mCancelButton, SIGNAL(released()), this, SLOT(cancel()));
       
   129         connect(mStartButton, SIGNAL(released()), this, SLOT(startTimer()));
       
   130 
       
   131         updateWidgets();
       
   132     }
       
   133 }
       
   134 
       
   135 /*!
       
   136     Method for checking if selftimer is enabled.
       
   137     Selftimer is enabled/disabled using slots cancel() and changeTimeout()
       
   138     \return True if selftimer is enabled
       
   139 
       
   140     \sa changeTimeOut(int seconds)
       
   141     \sa cancel()
       
   142  */
       
   143 bool CxuiSelfTimer::isEnabled()
       
   144 {
       
   145     return (mDelay >= 0);
       
   146 }
       
   147 
       
   148 /*!
       
   149     Method for checking if selftimer countdown is ongoing.
       
   150     Countdown is started with by calling startTimer().
       
   151     \return True if selftimer is active.
       
   152 
       
   153     \sa startTimer()
       
   154  */
       
   155 bool CxuiSelfTimer::isOngoing()
       
   156 {
       
   157     return mTimer.isActive();
       
   158 }
       
   159 
       
   160 /*!
       
   161    Returns current timeout value of selftimer.
       
   162  */
       
   163 int CxuiSelfTimer::getTimeout() const
       
   164 {
       
   165     return mDelay;
       
   166 }
       
   167 
       
   168 /*! Slot for canceling the selftimer.
       
   169     Disables selftimer, sets back the postcapturetimeout if it
       
   170     has been changed by selftimer and emits signal to notify interested
       
   171     parties that cancel has been called.
       
   172 
       
   173     \sa startTimer()
       
   174  */
       
   175 void CxuiSelfTimer::cancel()
       
   176 {
       
   177     CX_DEBUG_ENTER_FUNCTION();
       
   178 
       
   179     // show postcapture setting is changed when selftimer is started
       
   180     // if it has been changed, then change it back
       
   181     if (mOldPostCaptureTimeOut != UNKNOWN) {
       
   182         mSettings.set(CxeSettingIds::STILL_SHOWCAPTURED, mOldPostCaptureTimeOut);
       
   183         mOldPostCaptureTimeOut = UNKNOWN;
       
   184     }
       
   185 
       
   186     // Reset counter and stop any ongoing action.
       
   187     // Start button back to enabled.
       
   188     reset(false);
       
   189     // Disable self timer
       
   190     mDelay = -1;
       
   191 
       
   192     // Update UI components.
       
   193     updateWidgets();
       
   194 
       
   195     // Signal others about cancellation.
       
   196     emit cancelled();
       
   197 
       
   198     CX_DEBUG_EXIT_FUNCTION();
       
   199 }
       
   200 
       
   201 /*!
       
   202     Slot for handling timer timouts. Counter will be updated and when
       
   203     selftimer countdown is finished, timerFinished() signal is emitted.
       
   204  */
       
   205 void CxuiSelfTimer::timeout()
       
   206 {
       
   207     CX_DEBUG_ENTER_FUNCTION();
       
   208 
       
   209     // Update seconds passed.
       
   210     ++mCounter;
       
   211 
       
   212     // Update now to have better looking user experience,
       
   213     // especially when counter elapses: "0" is updated before emitting event,
       
   214     // so the UI seems to update smoother.
       
   215     updateWidgets();
       
   216 
       
   217     playSound();
       
   218 
       
   219     // Check if timer ran out
       
   220     if (mCounter >= mDelay) {
       
   221         mTimer.stop();
       
   222         mSound.stop();
       
   223         hideWidgets();
       
   224         emit timerFinished();
       
   225     }
       
   226 
       
   227     CX_DEBUG_EXIT_FUNCTION();
       
   228 }
       
   229 
       
   230 /*!
       
   231  * Play selftimer sound.
       
   232  */
       
   233 void CxuiSelfTimer::playSound()
       
   234 {
       
   235     CX_DEBUG_ENTER_FUNCTION();
       
   236 
       
   237     // play sounds only if warning tones are enabled
       
   238     if (mUseSound) {
       
   239         CX_DEBUG(("play"));
       
   240 
       
   241         int timeLeft = mDelay - mCounter;
       
   242 
       
   243         if (timeLeft <= 3) {
       
   244             // play as fast as we can
       
   245             if (mSound.isFinished()) {
       
   246                 mSound.setLoops(-1);
       
   247                 mSound.play();
       
   248             }
       
   249         } else if (timeLeft <= 10) {
       
   250             // play every second
       
   251             mSound.setLoops(1);
       
   252             mSound.play();
       
   253         } else {
       
   254             // play once every two seconds
       
   255             if (mCounter%2) {
       
   256                 mSound.setLoops(1);
       
   257                 mSound.play();
       
   258             }
       
   259         }
       
   260     }
       
   261 
       
   262     CX_DEBUG_EXIT_FUNCTION();
       
   263 }
       
   264 
       
   265 /*!
       
   266      Slot for starting the countdown. Show postcapture setting is set to
       
   267      continuous and previous value stored. Timer is started.
       
   268 
       
   269      \sa cancel()
       
   270  */
       
   271 void CxuiSelfTimer::startTimer()
       
   272 {
       
   273     CX_DEBUG_ENTER_FUNCTION();
       
   274 
       
   275     // get the current postcapture timeout
       
   276     try {
       
   277         mOldPostCaptureTimeOut = mSettings.get<int>(CxeSettingIds::STILL_SHOWCAPTURED);
       
   278         // set continuous postcapture (view is visible until dismissed)
       
   279         mSettings.set(CxeSettingIds::STILL_SHOWCAPTURED, CONTINUOUS_POSTCAPTURE);
       
   280     } catch (CxeException &e) {
       
   281         // if there was an error, don't modify the postcapture setting
       
   282         mOldPostCaptureTimeOut = UNKNOWN;
       
   283     }
       
   284 
       
   285     if (mStartButton) {
       
   286         mStartButton->setEnabled(false);
       
   287     }
       
   288 
       
   289     // start countdown
       
   290     mCounter = 0;
       
   291     playSound();
       
   292     mTimer.start(1000); // 1000 milliseconds == 1 second
       
   293 
       
   294     CX_DEBUG_EXIT_FUNCTION();
       
   295 }
       
   296 
       
   297 /*!
       
   298     Slot for receiving changes to the selftimer countdown timeout.
       
   299 
       
   300     \param seconds Amount of seconds for the selftimer countdown. If the value is
       
   301     0 then selftimer is disabled.
       
   302  */
       
   303 void CxuiSelfTimer::changeTimeOut(int seconds)
       
   304 {
       
   305     CX_DEBUG_ENTER_FUNCTION();
       
   306 
       
   307     // make sure that timer is not running (should not be)
       
   308     reset(false);
       
   309 
       
   310     // set the new timer value
       
   311     // if incoming value is 0, it means that the selftimer is disabled
       
   312     if (seconds > 0) {
       
   313         mDelay = seconds;
       
   314     } else {
       
   315         //value == 0
       
   316         mDelay = -1;
       
   317     }
       
   318 
       
   319     updateWidgets();
       
   320 
       
   321     CX_DEBUG_EXIT_FUNCTION();
       
   322 }
       
   323 
       
   324 /*! Resets the selftimer countdown.
       
   325     Countdown is stopped, and set back to starting value. Start button is set enabled.
       
   326     @param update Update the UI widgets if true, skip update if false.
       
   327     Can be used to by-pass the UI update if planned to do shortly again.
       
   328  */
       
   329 void CxuiSelfTimer::reset(bool update)
       
   330 {
       
   331     // Stop timer and reset counter.
       
   332     mTimer.stop();
       
   333     mSound.stop();
       
   334     mCounter = 0;
       
   335 
       
   336     // Set start buttonback to enabled.
       
   337     if (mStartButton) {
       
   338         mStartButton->setEnabled(true);
       
   339     }
       
   340 
       
   341     // Update UI if requested.
       
   342     if(update) {
       
   343         updateWidgets();
       
   344     }
       
   345 }
       
   346 
       
   347 /*! Updates the selftimer widgets
       
   348     Changes the visibility of the selftimer widgets (counter and cancel button)
       
   349     based on the timeoutValue. Updates the counter value.
       
   350  */
       
   351 void CxuiSelfTimer::updateWidgets()
       
   352 {
       
   353     CX_DEBUG_ENTER_FUNCTION();
       
   354 
       
   355     int remaining(mDelay - mCounter);
       
   356     CX_DEBUG(("remaining time %d", remaining));
       
   357 
       
   358     if (mTimerLabel) {
       
   359         mTimerLabel->setPlainText(QString::number(remaining));
       
   360     }
       
   361 
       
   362     if (isEnabled()) {
       
   363         showWidgets();
       
   364     } else {
       
   365         hideWidgets();
       
   366     }
       
   367 
       
   368     CX_DEBUG_EXIT_FUNCTION();
       
   369 }
       
   370 
       
   371 /*!
       
   372     Shows the selftimer widgets.
       
   373  */
       
   374 void CxuiSelfTimer::showWidgets()
       
   375 {
       
   376     if (mWidgetContainer){
       
   377         mWidgetContainer->show();
       
   378     }
       
   379 
       
   380 
       
   381 }
       
   382 
       
   383 /*!
       
   384     Hides the selftimer widgets.
       
   385  */
       
   386 void CxuiSelfTimer::hideWidgets()
       
   387 {
       
   388     if (mWidgetContainer){
       
   389         mWidgetContainer->hide();
       
   390     }
       
   391 
       
   392 }
       
   393 
       
   394 /*!
       
   395  * Enables or disables the selftimer sound.
       
   396  * \param uid UID of the changed setting
       
   397  * \param key Key of the changed setting
       
   398  * \param value New setting value
       
   399  */
       
   400 void CxuiSelfTimer::enableSound(long int uid, unsigned long int key, QVariant value)
       
   401 {
       
   402 #ifdef Q_OS_SYMBIAN
       
   403     // selftimer is only interested in warning tones
       
   404     if (uid == KCRUidProfileEngine.iUid && key == KProEngActiveWarningTones) {
       
   405         CX_DEBUG_IN_FUNCTION();
       
   406         // possible values are:
       
   407         // 0 -> warning tones off
       
   408         // 1 -> warning tones on
       
   409         mUseSound = (value.toInt() == 1);
       
   410     }
       
   411 #else
       
   412     Q_UNUSED(uid);
       
   413     Q_UNUSED(key);
       
   414     Q_UNUSED(value);
       
   415 #endif
       
   416 }
       
   417