|
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 "cxuisettingslider.h" |
|
19 #include "cxeengine.h" |
|
20 #include "cxesettings.h" |
|
21 #include "cxutils.h" |
|
22 |
|
23 CxuiSettingSlider::CxuiSettingSlider(QGraphicsItem *parent, CxeEngine *engine) : |
|
24 HbSlider(parent), |
|
25 mSettingId(), |
|
26 mEngine(engine), |
|
27 mSettings(NULL), |
|
28 mOriginalValue(0), |
|
29 mSliderScaleValue(1) |
|
30 { |
|
31 connect(this, SIGNAL(valueChanged(int)), this, SLOT(handleValueChanged(int))); |
|
32 } |
|
33 |
|
34 /*! |
|
35 Initialises slider according to SliderParams data |
|
36 */ |
|
37 void CxuiSettingSlider::init(CxUiSettings::SliderParams *data) |
|
38 { |
|
39 CX_DEBUG_ENTER_FUNCTION(); |
|
40 |
|
41 // initialise engine side settings |
|
42 if (mEngine) { |
|
43 mSettings = &mEngine->settings(); |
|
44 CX_ASSERT_ALWAYS(mSettings); |
|
45 } |
|
46 |
|
47 // initialise data |
|
48 if (data) { |
|
49 |
|
50 // scale value is needed because slider doesn't show all ticks and |
|
51 // labels correctly if step is something else than 1 |
|
52 int step = 1; |
|
53 mSliderScaleValue = 1; |
|
54 if (data->mStep != 0) { |
|
55 mSliderScaleValue = step / data->mStep; |
|
56 } |
|
57 |
|
58 // scale min and max values as step has been set to 1 |
|
59 int minvalue = data->mRange.first * mSliderScaleValue; |
|
60 int maxvalue = data->mRange.second * mSliderScaleValue; |
|
61 CX_DEBUG(("Setting slider range [%d..%d]", minvalue, maxvalue)); |
|
62 setRange(minvalue,maxvalue); |
|
63 setSingleStep(step); |
|
64 |
|
65 setSnappingMode(HbSlider::MajorTickSnapping); |
|
66 |
|
67 // don't show labels and tickmarks if there are no strings to be shown |
|
68 if (!data->mSettingStrings.isEmpty()) { |
|
69 setMajorTickLabels(data->mSettingStrings); |
|
70 setMajorTickInterval(step); |
|
71 setTickPosition(Hb::SliderTicksLeft); |
|
72 } else { |
|
73 setTickPosition(Hb::NoSliderTicks); |
|
74 } |
|
75 |
|
76 setSettingId(data->mSettingId); |
|
77 |
|
78 } |
|
79 CX_DEBUG_EXIT_FUNCTION(); |
|
80 } |
|
81 |
|
82 /*! |
|
83 Returns settingId |
|
84 */ |
|
85 QString CxuiSettingSlider::settingId() const |
|
86 { |
|
87 return mSettingId; |
|
88 } |
|
89 |
|
90 /*! |
|
91 Sets the setting identification string. Setting id corresponds to specific setting |
|
92 defined in cxenamespace.h. A value that corresponds the id is got from engine and |
|
93 stored as original value. Also slider value is updated. |
|
94 */ |
|
95 void CxuiSettingSlider::setSettingId(const QString &id) |
|
96 { |
|
97 CX_DEBUG_ENTER_FUNCTION(); |
|
98 |
|
99 mSettingId = id; |
|
100 |
|
101 // once the settingid is set, we can get the current value for the slider from the engine |
|
102 qreal value = 0; |
|
103 |
|
104 int err = mSettings->get(mSettingId, value); |
|
105 CX_DEBUG((("err: %d value: %d"),err,value)); |
|
106 |
|
107 // engine value has to be scaled when set to slider |
|
108 setValue(value * mSliderScaleValue); |
|
109 mOriginalValue = value; |
|
110 |
|
111 CX_DEBUG_EXIT_FUNCTION(); |
|
112 } |
|
113 |
|
114 /*! |
|
115 This slot is called when slider's value is changed. |
|
116 Value is stored to engine. |
|
117 */ |
|
118 void CxuiSettingSlider::handleValueChanged(int value) |
|
119 { |
|
120 CX_DEBUG_ENTER_FUNCTION(); |
|
121 |
|
122 qreal realValue = 0.0; |
|
123 if (mSliderScaleValue != 0) { |
|
124 realValue = value / mSliderScaleValue; |
|
125 } |
|
126 |
|
127 CX_DEBUG(("id: %s", mSettingId.toAscii().data())); |
|
128 CX_DEBUG(("changing value to: %f ", realValue)); |
|
129 |
|
130 // check added because when the slider is created (or ui appearance changed) |
|
131 // handlevaluechanged function is called before the mSettingId is set |
|
132 if (!mSettingId.isEmpty()){ |
|
133 mSettings->set(mSettingId, realValue); |
|
134 } |
|
135 CX_DEBUG_EXIT_FUNCTION(); |
|
136 } |
|
137 |
|
138 /*! |
|
139 This slot should be called when slider is closed. Original value will always be given |
|
140 to engine. Original value is the value that slider had when it was initialised unless |
|
141 slider value is changed and handleSelectionAccepted() function is called before coming here |
|
142 */ |
|
143 void CxuiSettingSlider::handleClose() |
|
144 { |
|
145 CX_DEBUG_ENTER_FUNCTION(); |
|
146 |
|
147 setValue(mOriginalValue * mSliderScaleValue); |
|
148 // value selected from slider not accepted, revert back |
|
149 // the value to original value. |
|
150 CX_DEBUG(("id: %s", mSettingId.toAscii().data())); |
|
151 CX_DEBUG(("changing value back to original value: %f ", mOriginalValue)); |
|
152 |
|
153 // when the slider is created, handlevaluechanged function is |
|
154 // called before the mSettingId is set |
|
155 if (mSettings && !mSettingId.isEmpty()){ |
|
156 mSettings->set(mSettingId, mOriginalValue); |
|
157 } |
|
158 |
|
159 // clear settings id so that setting value doesn't get updated by accident |
|
160 // when updating slider's ui appearance |
|
161 mSettingId.clear(); |
|
162 |
|
163 CX_DEBUG_EXIT_FUNCTION(); |
|
164 } |
|
165 |
|
166 /*! |
|
167 Accepts the current selection. |
|
168 */ |
|
169 void CxuiSettingSlider::handleSelectionAccepted() |
|
170 { |
|
171 CX_DEBUG_ENTER_FUNCTION(); |
|
172 |
|
173 // sets the curren slider value to be original value. mOriginal value |
|
174 // is given to engine whenever handleClose() function is called |
|
175 if (mSliderScaleValue != 0) { |
|
176 mOriginalValue = value() / mSliderScaleValue; |
|
177 } |
|
178 |
|
179 emit selectionCommitted(); |
|
180 |
|
181 CX_DEBUG_EXIT_FUNCTION(); |
|
182 } |