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 |
|
18 #include "cxutils.h" |
|
19 #include "cxeengine.h" |
|
20 #include "cxuisettingsinfo.h" |
|
21 #include "cxesettings.h" |
|
22 #include "cxuisettingradiobuttonlist.h" |
|
23 #include "cxuisettingradiobuttonlistmodel.h" |
|
24 |
|
25 CxuiSettingRadioButtonList::CxuiSettingRadioButtonList(QGraphicsItem *parent, CxeEngine *engine) |
|
26 : HbRadioButtonList(parent), |
|
27 mEngine(engine), |
|
28 mSettingId(), |
|
29 mSettingValues(), |
|
30 mOriginalIndex(0) |
|
31 { |
|
32 CX_ASSERT_ALWAYS(mEngine); |
|
33 |
|
34 mListModel = new CxuiSettingRadioButtonListModel(); |
|
35 setModel(mListModel); |
|
36 |
|
37 // connect the signals again |
|
38 connect(this, SIGNAL(itemSelected(int)), this, SLOT(handleItemSelected(int))); |
|
39 } |
|
40 |
|
41 /*! |
|
42 * Init contents of the listbox and select current setting value. |
|
43 */ |
|
44 void CxuiSettingRadioButtonList::init(CxUiSettings::RadioButtonListParams *data) |
|
45 { |
|
46 // first we reset the model and clear any previous data |
|
47 mSettingValues.clear(); |
|
48 mListModel->resetModel(); |
|
49 |
|
50 disconnect(SIGNAL(valueSelected(int))); |
|
51 |
|
52 if (data) { |
|
53 QStringList settingStrings; |
|
54 mSettingValues.clear(); |
|
55 |
|
56 CxUiSettings::SettingItem setting; |
|
57 foreach (setting, data->mSettingPairList) { |
|
58 CX_DEBUG(("CxuiSettingRadioButtonList - appending setting value: %s", setting.mValue.toString().toAscii().data())); |
|
59 settingStrings.append(setting.mItem); // setting string |
|
60 mSettingValues.append(setting.mValue); // engine value for setting |
|
61 } |
|
62 |
|
63 // Set the setting strings to the model. |
|
64 setItems(settingStrings); |
|
65 // Set the preview mode. |
|
66 // Note: We implement preview ourselves, not with HbRadioButtonList preview mode. |
|
67 setPreviewMode(HbRadioButtonList::NoPreview); |
|
68 mPreview = data->mPreview; |
|
69 |
|
70 setSettingId(data->mSettingId); |
|
71 setListBoxType(data->mListboxType); |
|
72 |
|
73 // Store the original setting value and focus matching item. |
|
74 QString value = mEngine->settings().get<QString>(mSettingId, ""); |
|
75 CX_DEBUG(("CxuiSettingRadioButtonList - original value: [%s]", qPrintable(value))); |
|
76 setOriginalSelectedItemByValue(QVariant(value)); |
|
77 } |
|
78 } |
|
79 |
|
80 /*! |
|
81 * Sets the original selection of list by value. Can be used to override value read from |
|
82 * CxeSettings or used to remember previously selected value in case of setting that is |
|
83 * not read from CxeSettings (e.g. selftimer) |
|
84 */ |
|
85 void CxuiSettingRadioButtonList::setOriginalSelectedItemByValue(const QVariant &value) |
|
86 { |
|
87 CX_DEBUG_ENTER_FUNCTION(); |
|
88 |
|
89 // Find the index of given value among setting values. |
|
90 // Default to first item, if given value is not found. |
|
91 int index = mSettingValues.indexOf(QVariant(value)); |
|
92 if (index < 0) { |
|
93 CX_DEBUG(("[WARNING] Value [%s] not found, selecting first item", qPrintable(value.toString()))); |
|
94 index = 0; |
|
95 } |
|
96 |
|
97 // Store the original value. |
|
98 mOriginalIndex = index; |
|
99 // Select the index with current value item. |
|
100 setSelected(index); |
|
101 // Ensure that currently selected item is visible. |
|
102 scrollTo(currentIndex()); |
|
103 |
|
104 CX_DEBUG_EXIT_FUNCTION(); |
|
105 } |
|
106 |
|
107 /*! |
|
108 * Set list texts. |
|
109 * @param values List of the texts. |
|
110 */ |
|
111 void CxuiSettingRadioButtonList::setItems(const QStringList &values) |
|
112 { |
|
113 mListModel->setItems(values); |
|
114 } |
|
115 |
|
116 /*! |
|
117 * Set the type of this list. |
|
118 * @param type Type identifier, SingleLine or TwoLine. |
|
119 */ |
|
120 void CxuiSettingRadioButtonList::setListBoxType(int type) |
|
121 { |
|
122 mListModel->setListBoxType(type); |
|
123 } |
|
124 |
|
125 /*! |
|
126 * Set id of the setting this list represents. |
|
127 * @param id Id of the setting. |
|
128 */ |
|
129 void CxuiSettingRadioButtonList::setSettingId(const QString &id) |
|
130 { |
|
131 // Selected item is updated, when this list is shown. |
|
132 mSettingId = id; |
|
133 } |
|
134 |
|
135 /*! |
|
136 * Handle selecting an item. |
|
137 * @param index Index of the selected item in list. |
|
138 */ |
|
139 void CxuiSettingRadioButtonList::handleItemSelected(int index) |
|
140 { |
|
141 CX_DEBUG_ENTER_FUNCTION(); |
|
142 |
|
143 if (mPreview) { |
|
144 commit(index); |
|
145 } else { |
|
146 // no action needed |
|
147 } |
|
148 CX_DEBUG_EXIT_FUNCTION(); |
|
149 } |
|
150 |
|
151 /*! |
|
152 This slot can be used to set the selection accepted. |
|
153 */ |
|
154 void CxuiSettingRadioButtonList::handleSelectionAccepted() |
|
155 { |
|
156 CX_DEBUG_ENTER_FUNCTION(); |
|
157 |
|
158 // Ok button pressed. Update originally selected item, |
|
159 // which is the one that always get's set when closing. |
|
160 mOriginalIndex = selected(); |
|
161 |
|
162 emit selectionCommitted(); |
|
163 |
|
164 CX_DEBUG_EXIT_FUNCTION(); |
|
165 } |
|
166 |
|
167 /*! |
|
168 * Handle closing the listbox. If the current selection was accepted, |
|
169 * we commit the new value here. If current selection has been cancelled, |
|
170 * we commit the original value. |
|
171 */ |
|
172 void CxuiSettingRadioButtonList::handleClose() |
|
173 { |
|
174 CX_DEBUG_ENTER_FUNCTION(); |
|
175 |
|
176 if (!mSettingId.isEmpty()) { |
|
177 // If handleSelectionAccepted was called, this now contains the new index, |
|
178 // otherwise the original one is selected. |
|
179 commit(mOriginalIndex); |
|
180 } |
|
181 |
|
182 // clear settings id so setting value doesn't get updated by |
|
183 // accident when updating ui appearance |
|
184 mSettingId.clear(); |
|
185 |
|
186 CX_DEBUG_EXIT_FUNCTION(); |
|
187 } |
|
188 |
|
189 |
|
190 |
|
191 /*! |
|
192 Commits value to settings. |
|
193 */ |
|
194 void CxuiSettingRadioButtonList::commit(int index) |
|
195 { |
|
196 CX_DEBUG_ENTER_FUNCTION(); |
|
197 |
|
198 CX_DEBUG(("CxuiSettingRadioButtonList - id: %s", qPrintable(mSettingId))); |
|
199 |
|
200 if (!mSettingId.isEmpty() && !mSettingValues.isEmpty()) { |
|
201 QVariant value = mSettingValues.at(index); |
|
202 if (value.type() == QVariant::Int) { |
|
203 CX_DEBUG(("CxuiSettingRadioButtonList - index:%d value:%d", index, value.toInt())); |
|
204 |
|
205 // Don't set the value again, if it is the current value. |
|
206 // For e.g. video quality it would result in re-preparation etc. |
|
207 try { |
|
208 int current = mEngine->settings().get<int>(mSettingId); |
|
209 if (current != value.toInt()) { |
|
210 mEngine->settings().set(mSettingId, value.toInt()); |
|
211 } |
|
212 } catch (CxeException &e) { |
|
213 // ignore error |
|
214 } |
|
215 // inform interested clients about value changed event |
|
216 emit valueSelected(value.toInt()); |
|
217 |
|
218 } else if (value.type() == QVariant::String) { |
|
219 CX_DEBUG(("CxuiSettingRadioButtonList - index:%d value:[%s]", index, qPrintable(value.toString()))); |
|
220 |
|
221 try { |
|
222 QString current = mEngine->settings().get<QString>(mSettingId); |
|
223 CX_DEBUG(("CxuiSettingRadioButtonList - settings model value:[%s]", qPrintable(current))); |
|
224 // If the changed setting is a scene mode, we need to reset it |
|
225 // because a scene mode related setting might have changed |
|
226 // even though the scene mode itself hadn't |
|
227 if (current != value.toString() || |
|
228 mSettingId == CxeSettingIds::IMAGE_SCENE || |
|
229 mSettingId == CxeSettingIds::VIDEO_SCENE) { |
|
230 mEngine->settings().set(mSettingId, value.toString()); |
|
231 } |
|
232 } catch (CxeException &e) { |
|
233 // ignore error |
|
234 } |
|
235 } |
|
236 } |
|
237 |
|
238 CX_DEBUG_EXIT_FUNCTION(); |
|
239 } |
|
240 |
|
241 // end of file |
|