Applied latest source code
[apps/native/preloaded/Settings.git] / src / StBrightnessForm.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.1 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://floralicense.org/license/
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an AS IS BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 /**
18  * @file                StBrightnessForm.cpp
19  * @brief               This is the implementation file for BrightnessForm class.
20  */
21
22 #include "StBrightnessForm.h"
23 #include "StResourceManager.h"
24 #include "StTypes.h"
25
26 using namespace Tizen::Base;
27 using namespace Tizen::Graphics;
28 using namespace Tizen::System;
29 using namespace Tizen::Ui;
30 using namespace Tizen::Ui::Controls;
31 using namespace Tizen::Ui::Scenes;
32
33 static const int ID_GROUP_BRIGHTNESS = 0;
34 static const int ID_GROUP_BRIGHTNESS_ITEM_COUNT = 2;
35 static const int ID_ITEM_BRIGHTNESS_AUTOMATIC = 0;
36 static const int ID_ITEM_BRIGHTNESS_SLIDE_BAR = 1;
37
38 static const int ID_GROUP_COUNT = 1;
39
40 static const int Y_GROUP_ITEM_SLIDER = -30;
41 static const int GROUP_ITEM_SLIDER_MIN_VALUE = 1;
42 static const int GROUP_ITEM_SLIDER_MAX_VALUE = 10;
43
44 static const int BRIGHTNESS_DEFAULT_VALUE = 1;
45
46 BrightnessForm::BrightnessForm(void)
47 {
48 }
49
50 BrightnessForm::~BrightnessForm(void)
51 {
52 }
53
54 void
55 BrightnessForm::CreateFooter(void)
56 {
57         Footer* pFooter = GetFooter();
58         AppAssert(pFooter);
59
60         pFooter->SetStyle(FOOTER_STYLE_BUTTON_TEXT);
61         pFooter->AddActionEventListener(*this);
62
63         SetFormBackEventListener(this);
64 }
65
66 result
67 BrightnessForm::OnInitializing(void)
68 {
69         CreateHeader(ResourceManager::GetString(L"IDS_ST_BODY_BRIGHTNESS"));
70         CreateTableView();
71
72         AppLogDebug("ENTER");
73
74         return E_SUCCESS;
75 }
76
77 result
78 BrightnessForm::OnTerminating(void)
79 {
80         __pTableView = null;
81
82         SetFormBackEventListener(null);
83         return E_SUCCESS;
84 }
85
86 void
87 BrightnessForm::OnSceneActivatedN(const Tizen::Ui::Scenes::SceneId& previousSceneId, const Tizen::Ui::Scenes::SceneId& currentSceneId, Tizen::Base::Collection::IList* pArgs)
88 {
89         __pTableView->UpdateTableView();
90 }
91
92 void
93 BrightnessForm::OnSceneDeactivated(const Tizen::Ui::Scenes::SceneId& currentSceneId, const Tizen::Ui::Scenes::SceneId& nextSceneId)
94 {
95 }
96
97 void
98 BrightnessForm::OnFormBackRequested(Tizen::Ui::Controls::Form& source)
99 {
100         SceneManager* pSceneManager = SceneManager::GetInstance();
101         AppAssert(pSceneManager);
102
103         pSceneManager->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_RIGHT), null);
104 }
105
106 int
107 BrightnessForm::GetGroupCount(void)
108 {
109         AppLogDebug("ENTER");
110         return ID_GROUP_COUNT;
111 }
112
113 int
114 BrightnessForm::GetItemCount(int groupIndex)
115 {
116         int itemCount = 0;
117
118         if (groupIndex == ID_GROUP_BRIGHTNESS)
119         {
120                 itemCount = ID_GROUP_BRIGHTNESS_ITEM_COUNT;
121         }
122
123         AppLogDebug("GetItemCount %d", itemCount);
124
125         return itemCount;
126 }
127
128 TableViewGroupItem*
129 BrightnessForm::CreateGroupItem(int groupIndex, int itemWidth)
130 {
131         AppLogDebug("ENTER");
132
133         int itemHeight = H_GROUP_INDEX_NO_TITLE_DEFAULT;
134         LabelTextStyle style = LABEL_TEXT_STYLE_BOLD;
135         String groupText;
136         Label* pLabel = null;
137
138         TableViewGroupItem* pItem = new (std::nothrow) TableViewGroupItem();
139
140         RelativeLayout relativeLayout;
141         relativeLayout.Construct();
142
143         pItem->Construct(relativeLayout, Dimension(itemWidth, H_GROUP_INDEX_NO_TITLE_DEFAULT));
144
145         pLabel = new (std::nothrow) Label();
146         pLabel->Construct(Rectangle(X_GROUP_INDEX_DEFAULT_LABEL, Y_GROUP_INDEX_DEFAULT_LABEL, itemWidth, itemHeight), groupText);
147         pLabel->SetTextHorizontalAlignment(ALIGNMENT_LEFT);
148         pLabel->SetTextConfig(FONT_SIZE_GROUP_TITLE_TEXT, style);
149         pLabel->SetTextColor(COLOR_GROUP_TITLE_TEXT);
150
151         pItem->AddControl(pLabel);
152         relativeLayout.SetMargin(*pLabel, 0, RELATIVE_LAYOUT_RIGHT_MARGIN, 0, 0);
153         relativeLayout.SetRelation(*pLabel, pItem, RECT_EDGE_RELATION_LEFT_TO_LEFT);
154         relativeLayout.SetRelation(*pLabel, pItem, RECT_EDGE_RELATION_RIGHT_TO_RIGHT);
155         pItem->SetBackgroundColor(COLOR_BG_GROUP_INDEX_DEFAULT);
156         pItem->SetEnabled(false);
157
158         return pItem;
159 }
160
161 TableViewItem*
162 BrightnessForm::CreateItem(int groupIndex, int itemIndex,int itemWidth)
163 {
164         AppLogDebug("CreateItem group[%d] index[%d]", groupIndex, itemIndex);
165
166         int brightness = 0;
167         Rectangle itemMainRectangle;
168         TableViewAnnexStyle style = TABLE_VIEW_ANNEX_STYLE_NORMAL;
169         Slider* pSlider = null;
170         String itemMainText;
171         Label* pLabel = null;
172         int fontSize = GetFontSize();
173
174         TableViewItem* pItem = new (std::nothrow) TableViewItem();
175         ItemTypeOneLine(itemMainRectangle);
176
177         switch (itemIndex)
178         {
179         case ID_ITEM_BRIGHTNESS_AUTOMATIC:
180                 {
181                         itemMainText = ResourceManager::GetString(L"IDS_ST_BODY_AUTOMATIC");
182                         style = TABLE_VIEW_ANNEX_STYLE_ONOFF_SLIDING;
183                         RelativeLayout relativeLayout;
184                         relativeLayout.Construct();
185
186                         pItem->Construct(relativeLayout, Dimension(W_GROUP_ITEM_DEFAULT, H_GROUP_ITEM_DEFAULT), style);
187                         pItem->SetBackgroundColor(COLOR_BG_GROUP_ITEM_DEFAULT);
188
189                         pLabel = new (std::nothrow) Label();
190                         pLabel->Construct(itemMainRectangle, itemMainText);
191                         pLabel->SetTextHorizontalAlignment(ALIGNMENT_LEFT);
192                         pLabel->SetTextConfig(fontSize, LABEL_TEXT_STYLE_NORMAL);
193                         pLabel->SetTextColor(COLOR_MAIN_TEXT);
194
195                         pItem->AddControl(pLabel);
196                         relativeLayout.SetMargin(*pLabel, 0, RELATIVE_LAYOUT_RIGHT_MARGIN, 0, 0);
197                         relativeLayout.SetRelation(*pLabel, pItem, RECT_EDGE_RELATION_LEFT_TO_LEFT);
198                         relativeLayout.SetRelation(*pLabel, pItem, RECT_EDGE_RELATION_RIGHT_TO_RIGHT);
199                 }
200                 break;
201
202         case ID_ITEM_BRIGHTNESS_SLIDE_BAR:
203                 {
204                         itemMainRectangle.width = W_GROUP_ITEM_DEFAULT_SLIDER;
205                         itemMainRectangle.height = H_GROUP_ITEM_DEFAULT_SLIDER;
206
207                         RelativeLayout relativeLayout;
208                         relativeLayout.Construct();
209
210                         pItem->Construct(relativeLayout, Dimension(W_GROUP_ITEM_DEFAULT, H_GROUP_ITEM_DEFAULT_SLIDER), style);
211                         pItem->SetBackgroundColor(COLOR_BG_GROUP_ITEM_DEFAULT);
212
213                         if ((brightness = PowerManager::GetScreenBrightness()) == 0)
214                         {
215                                 brightness = BRIGHTNESS_DEFAULT_VALUE;
216                         }
217
218                         pSlider = new (std::nothrow) Slider();
219                         pSlider->Construct(itemMainRectangle, BACKGROUND_STYLE_NONE, true, GROUP_ITEM_SLIDER_MIN_VALUE, GROUP_ITEM_SLIDER_MAX_VALUE);
220                         pSlider->SetValue(brightness);
221                         pSlider->SetPosition(itemMainRectangle.x, itemMainRectangle.y + Y_GROUP_ITEM_SLIDER);
222                         pSlider->AddAdjustmentEventListener(*this);
223                         pSlider->AddSliderEventListener(*this);
224
225                         pItem->AddControl(pSlider);
226                         relativeLayout.SetMargin(*pSlider, 0, RELATIVE_LAYOUT_RIGHT_MARGIN, 0, 0);
227                         relativeLayout.SetRelation(*pSlider, pItem, RECT_EDGE_RELATION_LEFT_TO_LEFT);
228                         relativeLayout.SetRelation(*pSlider, pItem, RECT_EDGE_RELATION_RIGHT_TO_RIGHT);
229                 }
230                 break;
231
232         default:
233                 break;
234         }
235
236         return pItem;
237 }
238
239 bool
240 BrightnessForm::DeleteGroupItem(int groupIndex, TableViewGroupItem* pItem)
241 {
242         AppLogDebug("ENTER");
243
244         delete pItem;
245         pItem = null;
246
247         return true;
248 }
249
250 bool
251 BrightnessForm::DeleteItem(int groupIndex, int itemIndex, TableViewItem* pItem)
252 {
253         AppLogDebug("ENTER");
254
255         delete pItem;
256         pItem = null;
257
258         return true;
259 }
260
261 void
262 BrightnessForm::OnGroupedTableViewItemStateChanged(Tizen::Ui::Controls::GroupedTableView& tableView, int groupIndex, int itemIndex, Tizen::Ui::Controls::TableViewItem* pItem, Tizen::Ui::Controls::TableViewItemStatus status)
263 {
264         AppLogDebug("OnTableViewItemStateChanged group[%d] index[%d]", groupIndex, itemIndex);
265
266         int localGroup = 0;
267         int localItem = 0;
268
269         if (itemIndex == ID_ITEM_BRIGHTNESS_AUTOMATIC)
270         {
271                 switch (status)
272                 {
273                 case TABLE_VIEW_ITEM_STATUS_CHECKED:
274                         {
275                                 localGroup = ID_GROUP_BRIGHTNESS;
276                                 localItem = ID_ITEM_BRIGHTNESS_SLIDE_BAR;
277
278                                 __pTableView->SetItemEnabled(localGroup, localItem, false);
279                         }
280                         break;
281
282                 case TABLE_VIEW_ITEM_STATUS_UNCHECKED:
283                         {
284                                 localGroup = ID_GROUP_BRIGHTNESS;
285                                 localItem = ID_ITEM_BRIGHTNESS_SLIDE_BAR;
286
287                                 __pTableView->SetItemEnabled(localGroup, localItem, true);
288                         }
289                         break;
290
291                 default:
292                         break;
293                 }
294         }
295 }
296
297 void
298 BrightnessForm::OnAdjustmentValueChanged(const Tizen::Ui::Control& source, int adjustment)
299 {
300         result r = E_SUCCESS;
301
302         if ((r = PowerManager::SetScreenBrightness(adjustment)) == E_SUCCESS)
303         {
304                 AppLogDebug("Brightness Set adjustment : %d Level", adjustment);
305         }
306         else
307         {
308                 AppLogDebug("Brightness Set allowed from 1 to 10 : adjustment Fail. %d", adjustment);
309         }
310 }
311
312 void
313 BrightnessForm::OnSliderBarMoved(Tizen::Ui::Controls::Slider& source, int value)
314 {
315         result r = E_SUCCESS;
316
317         if ((r = PowerManager::SetScreenBrightness(value)) == E_SUCCESS)
318         {
319                 AppLogDebug("Brightness Set value : %d Level ", value);
320         }
321         else
322         {
323                 AppLogDebug("Brightness Set allowed from 1 to 10 : Fail : %d", value);
324         }
325 }
326
327 int
328 BrightnessForm::GetDefaultGroupItemHeight(void)
329 {
330         return H_GROUP_INDEX_NO_TITLE_DEFAULT;
331 }
332
333 int
334 BrightnessForm::GetDefaultItemHeight(void)
335 {
336         return H_GROUP_ITEM_DEFAULT;
337 }
338
339 void
340 BrightnessForm::UpdateGroupItem(int groupIndex, Tizen::Ui::Controls::TableViewGroupItem* pItem)
341 {
342 }
343
344 void
345 BrightnessForm::UpdateItem(int groupIndex, int itemIndex, Tizen::Ui::Controls::TableViewItem* pItem)
346 {
347 }
348
349 void
350 BrightnessForm::OnGroupedTableViewContextItemActivationStateChanged(Tizen::Ui::Controls::GroupedTableView& tableView, int groupIndex, int itemIndex, Tizen::Ui::Controls::TableViewContextItem* pContextItem, bool activated)
351 {
352 }
353
354 void
355 BrightnessForm::OnGroupedTableViewGroupItemStateChanged(Tizen::Ui::Controls::GroupedTableView& tableView, int groupIndex, Tizen::Ui::Controls::TableViewGroupItem* pItem, Tizen::Ui::Controls::TableViewItemStatus status)
356 {
357 }