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 "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->mMinorStep != 0) { |
|
55 mSliderScaleValue = step / data->mMinorStep; |
|
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::MinorTickSnapping); |
|
66 |
|
67 setMajorTickLabels(data->mSettingStrings); |
|
68 setMajorTickInterval(mSliderScaleValue * data->mMajorStep); |
|
69 setMinorTickInterval(mSliderScaleValue * data->mMinorStep); |
|
70 setTickPosition(Hb::SliderTicksAbove); |
|
71 |
|
72 setSettingId(data->mSettingId); |
|
73 |
|
74 } |
|
75 CX_DEBUG_EXIT_FUNCTION(); |
|
76 } |
|
77 |
|
78 /*! |
|
79 Returns settingId |
|
80 */ |
|
81 QString CxuiSettingSlider::settingId() const |
|
82 { |
|
83 return mSettingId; |
|
84 } |
|
85 |
|
86 /*! |
|
87 Sets the setting identification string. Setting id corresponds to specific setting |
|
88 defined in cxenamespace.h. A value that corresponds the id is got from engine and |
|
89 stored as original value. Also slider value is updated. |
|
90 */ |
|
91 void CxuiSettingSlider::setSettingId(const QString &id) |
|
92 { |
|
93 CX_DEBUG_ENTER_FUNCTION(); |
|
94 |
|
95 mSettingId = id; |
|
96 |
|
97 // once the settingid is set, we can get the current value for the slider from the engine |
|
98 qreal value = mSettings->get<qreal>(mSettingId, 0); |
|
99 |
|
100 // engine value has to be scaled when set to slider |
|
101 setValue(value * mSliderScaleValue); |
|
102 mOriginalValue = value; |
|
103 |
|
104 CX_DEBUG_EXIT_FUNCTION(); |
|
105 } |
|
106 |
|
107 /*! |
|
108 This slot is called when slider's value is changed. |
|
109 Value is stored to engine. |
|
110 */ |
|
111 void CxuiSettingSlider::handleValueChanged(int value) |
|
112 { |
|
113 CX_DEBUG_ENTER_FUNCTION(); |
|
114 |
|
115 qreal realValue = 0.0; |
|
116 if (mSliderScaleValue != 0) { |
|
117 realValue = value / mSliderScaleValue; |
|
118 } |
|
119 |
|
120 CX_DEBUG(("id: %s", mSettingId.toAscii().data())); |
|
121 CX_DEBUG(("changing value to: %f ", realValue)); |
|
122 |
|
123 // check added because when the slider is created (or ui appearance changed) |
|
124 // handlevaluechanged function is called before the mSettingId is set |
|
125 if (!mSettingId.isEmpty()){ |
|
126 mSettings->set(mSettingId, realValue); |
|
127 } |
|
128 CX_DEBUG_EXIT_FUNCTION(); |
|
129 } |
|
130 |
|
131 /*! |
|
132 This slot should be called when slider is closed. Original value will always be given |
|
133 to engine. Original value is the value that slider had when it was initialised unless |
|
134 slider value is changed and handleSelectionAccepted() function is called before coming here |
|
135 */ |
|
136 void CxuiSettingSlider::handleClose() |
|
137 { |
|
138 CX_DEBUG_ENTER_FUNCTION(); |
|
139 |
|
140 setValue(mOriginalValue * mSliderScaleValue); |
|
141 // value selected from slider not accepted, revert back |
|
142 // the value to original value. |
|
143 CX_DEBUG(("id: %s", mSettingId.toAscii().data())); |
|
144 CX_DEBUG(("changing value back to original value: %f ", mOriginalValue)); |
|
145 |
|
146 // when the slider is created, handlevaluechanged function is |
|
147 // called before the mSettingId is set |
|
148 if (mSettings && !mSettingId.isEmpty()){ |
|
149 mSettings->set(mSettingId, mOriginalValue); |
|
150 } |
|
151 |
|
152 // clear settings id so that setting value doesn't get updated by accident |
|
153 // when updating slider's ui appearance |
|
154 mSettingId.clear(); |
|
155 |
|
156 CX_DEBUG_EXIT_FUNCTION(); |
|
157 } |
|
158 |
|
159 /*! |
|
160 Accepts the current selection. |
|
161 */ |
|
162 void CxuiSettingSlider::handleSelectionAccepted() |
|
163 { |
|
164 CX_DEBUG_ENTER_FUNCTION(); |
|
165 |
|
166 // sets the curren slider value to be original value. mOriginal value |
|
167 // is given to engine whenever handleClose() function is called |
|
168 if (mSliderScaleValue != 0) { |
|
169 mOriginalValue = value() / mSliderScaleValue; |
|
170 } |
|
171 |
|
172 emit selectionCommitted(); |
|
173 |
|
174 CX_DEBUG_EXIT_FUNCTION(); |
|
175 } |
|