camerauis/cameraxui/cxui/src/cxuisettingradiobuttonlist.cpp
changeset 43 0e652f8f1fbd
parent 28 3075d9b614e6
child 45 24fd82631616
--- a/camerauis/cameraxui/cxui/src/cxuisettingradiobuttonlist.cpp	Thu May 13 21:30:19 2010 +0300
+++ b/camerauis/cameraxui/cxui/src/cxuisettingradiobuttonlist.cpp	Thu Jul 15 01:55:05 2010 +0300
@@ -1,5 +1,5 @@
 /*
-* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
+* Copyright (c) 2009-2010 Nokia Corporation and/or its subsidiary(-ies).
 * All rights reserved.
 * This component and the accompanying materials are made available
 * under the terms of "Eclipse Public License v1.0"
@@ -38,7 +38,9 @@
     connect(this, SIGNAL(itemSelected(int)), this, SLOT(handleItemSelected(int)));
 }
 
-
+/*!
+* Init contents of the listbox and select current setting value.
+*/
 void CxuiSettingRadioButtonList::init(CxUiSettings::RadioButtonListParams *data)
 {
     // first we reset the model and clear any previous data
@@ -67,29 +69,74 @@
 
         setSettingId(data->mSettingId);
         setListBoxType(data->mListboxType);
+
+        // Store the original setting value and focus matching item.
+        QString value;
+        mEngine->settings().get(mSettingId, value);
+        CX_DEBUG(("CxuiSettingRadioButtonList - original value: [%s]", qPrintable(value)));
+        setOriginalSelectedItemByValue(QVariant(value));
+    }
+}
+
+/*!
+ * Sets the original selection of list by value. Can be used to override value read from
+ * CxeSettings or used to remember previously selected value in case of setting that is
+ * not read from CxeSettings (e.g. selftimer)
+ */
+void CxuiSettingRadioButtonList::setOriginalSelectedItemByValue(const QVariant &value)
+{
+    CX_DEBUG_ENTER_FUNCTION();
+
+    // Find the index of given value among setting values.
+    // Default to first item, if given value is not found.
+    int index = mSettingValues.indexOf(QVariant(value));
+    if (index < 0) {
+        CX_DEBUG(("[WARNING] Value [%s] not found, selecting first item", qPrintable(value.toString())));
+        index = 0;
     }
 
+    // Store the original value.
+    mOriginalIndex = index;
+    // Select the index with current value item.
+    setSelected(index);
+    // Ensure that currently selected item is visible.
+    scrollTo(currentIndex());
+
+    CX_DEBUG_EXIT_FUNCTION();
 }
 
+/*!
+* Set list texts.
+* @param values List of the texts.
+*/
 void CxuiSettingRadioButtonList::setItems(const QStringList &values)
 {
     mListModel->setItems(values);
 }
 
+/*!
+* Set the type of this list.
+* @param type Type identifier, SingleLine or TwoLine.
+*/
 void CxuiSettingRadioButtonList::setListBoxType(int type)
 {
     mListModel->setListBoxType(type);
 }
 
-
+/*!
+* Set id of the setting this list represents.
+* @param id Id of the setting.
+*/
 void CxuiSettingRadioButtonList::setSettingId(const QString &id)
 {
     // Selected item is updated, when this list is shown.
     mSettingId = id;
 }
 
-
-
+/*!
+* Handle selecting an item.
+* @param index Index of the selected item in list.
+*/
 void CxuiSettingRadioButtonList::handleItemSelected(int index)
 {
     CX_DEBUG_ENTER_FUNCTION();
@@ -102,41 +149,6 @@
     CX_DEBUG_EXIT_FUNCTION();
 }
 
-void CxuiSettingRadioButtonList::showEvent(QShowEvent *event)
-{
-    CX_DEBUG_ENTER_FUNCTION();
-
-    initOriginalSelectedItem();
-    QGraphicsWidget::showEvent(event);
-
-    CX_DEBUG_EXIT_FUNCTION();
-}
-
-
-/*!
-*  Get the value currently active in settings.
-*/
-void CxuiSettingRadioButtonList::initOriginalSelectedItem()
-{
-    CX_DEBUG_ENTER_FUNCTION();
-
-    QString value;
-    int err = mEngine->settings().get(mSettingId, value);
-    CX_DEBUG(("CxuiSettingRadioButtonList - original value: [%s]", value.toAscii().data()));
-
-    int index = 0;
-
-    if (err == CxeError::None) {
-        index = mSettingValues.indexOf(QVariant(value));
-        CX_DEBUG(("CxuiSettingRadioButtonList - got original index of: %d", index));
-    }
-
-    mOriginalIndex = index;
-    setSelected(mOriginalIndex);
-
-    CX_DEBUG_EXIT_FUNCTION();
-}
-
 /*!
     This slot can be used to set the selection accepted.
 */
@@ -153,6 +165,11 @@
     CX_DEBUG_EXIT_FUNCTION();
 }
 
+/*!
+* Handle closing the listbox. If the current selection was accepted,
+* we commit the new value here. If current selection has been cancelled,
+* we commit the original value.
+*/
 void CxuiSettingRadioButtonList::handleClose()
 {
     CX_DEBUG_ENTER_FUNCTION();
@@ -173,18 +190,18 @@
 
 
 /*!
-  Commits value to the cenrep store.
+  Commits value to settings.
 */
 void CxuiSettingRadioButtonList::commit(int index)
 {
     CX_DEBUG_ENTER_FUNCTION();
 
-    CX_DEBUG(("id: %s", mSettingId.toAscii().data()));
+    CX_DEBUG(("CxuiSettingRadioButtonList - id: %s", qPrintable(mSettingId)));
 
     if (!mSettingId.isEmpty() && !mSettingValues.isEmpty()) {
         QVariant value = mSettingValues.at(index);
         if (value.type() == QVariant::Int) {
-            CX_DEBUG(("index:%d value:%d", index, value.toInt()));
+            CX_DEBUG(("CxuiSettingRadioButtonList - index:%d value:%d", index, value.toInt()));
 
             // Don't set the value again, if it is the current value.
             // For e.g. video quality it would result in re-preparation etc.
@@ -198,11 +215,11 @@
             emit valueSelected(value.toInt());
 
         } else if (value.type() == QVariant::String) {
-            CX_DEBUG(("index:%d value:[%s]", index, value.toString().toAscii().constData()));
+            CX_DEBUG(("CxuiSettingRadioButtonList - index:%d value:[%s]", index, qPrintable(value.toString())));
 
             QString current;
             CxeError::Id status(mEngine->settings().get(mSettingId, current));
-            CX_DEBUG(("settings model value:[%s]", current.toAscii().constData()));
+            CX_DEBUG(("CxuiSettingRadioButtonList - settings model value:[%s]", qPrintable(current)));
 
             if (status != CxeError::None || current != value.toString()) {
                 mEngine->settings().set(mSettingId, value.toString());