Fixed N_SE-38563, N_SE-38552
[apps/osp/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 static const int ID_GROUP_MAX_ITEM_COUNT = ID_GROUP_BRIGHTNESS_ITEM_COUNT;
40
41 static const int Y_GROUP_ITEM_SLIDER = -30;
42 static const int GROUP_ITEM_SLIDER_MIN_VALUE = 1;
43 static const int GROUP_ITEM_SLIDER_MAX_VALUE = 10;
44
45 static const int BRIGHTNESS_DEFAULT_VALUE = 1;
46
47 BrightnessForm::BrightnessForm(void)
48 {
49 }
50
51 BrightnessForm::~BrightnessForm(void)
52 {
53 }
54
55 void
56 BrightnessForm::CreateFooter(void)
57 {
58         Footer* pFooter = GetFooter();
59         AppAssert(pFooter);
60
61         pFooter->SetStyle(FOOTER_STYLE_BUTTON_TEXT);
62         pFooter->SetBackButton();
63         pFooter->AddActionEventListener(*this);
64
65         SetFormBackEventListener(this);
66 }
67
68 result
69 BrightnessForm::OnInitializing(void)
70 {
71         CreateHeader(ResourceManager::GetString(L"IDS_ST_BODY_BRIGHTNESS"));
72         CreateFooter();
73         CreateTableView();
74
75         AppLogDebug("ENTER");
76
77         return E_SUCCESS;
78 }
79
80 result
81 BrightnessForm::OnTerminating(void)
82 {
83         __pTableView = null;
84
85         SetFormBackEventListener(null);
86         return E_SUCCESS;
87 }
88
89 void
90 BrightnessForm::OnSceneActivatedN(const Tizen::Ui::Scenes::SceneId& previousSceneId, const Tizen::Ui::Scenes::SceneId& currentSceneId, Tizen::Base::Collection::IList* pArgs)
91 {
92         __pTableView->UpdateTableView();
93 }
94
95 void
96 BrightnessForm::OnSceneDeactivated(const Tizen::Ui::Scenes::SceneId& currentSceneId, const Tizen::Ui::Scenes::SceneId& nextSceneId)
97 {
98 }
99
100 void
101 BrightnessForm::OnFormBackRequested(Tizen::Ui::Controls::Form& source)
102 {
103         SceneManager* pSceneManager = SceneManager::GetInstance();
104         AppAssert(pSceneManager);
105
106         pSceneManager->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_RIGHT), null);
107 }
108
109 int
110 BrightnessForm::GetGroupCount(void)
111 {
112         AppLogDebug("ENTER");
113         return ID_GROUP_COUNT;
114 }
115
116 int
117 BrightnessForm::GetItemCount(int groupIndex)
118 {
119         int itemCount = 0;
120
121         if (groupIndex == ID_GROUP_BRIGHTNESS)
122         {
123                 itemCount = ID_GROUP_BRIGHTNESS_ITEM_COUNT;
124         }
125
126         AppLogDebug("GetItemCount %d", itemCount);
127
128         return itemCount;
129 }
130
131 TableViewGroupItem*
132 BrightnessForm::CreateGroupItem(int groupIndex, int itemWidth)
133 {
134         AppLogDebug("ENTER");
135
136         int itemHeight = H_GROUP_INDEX_NO_TITLE_DEFAULT;
137         LabelTextStyle style = LABEL_TEXT_STYLE_BOLD;
138         String groupText;
139         Label* pLabel = null;
140
141         TableViewGroupItem* pItem = new (std::nothrow) TableViewGroupItem();
142
143         RelativeLayout relativeLayout;
144         relativeLayout.Construct();
145
146         pItem->Construct(relativeLayout, Dimension(itemWidth, H_GROUP_INDEX_NO_TITLE_DEFAULT));
147
148         pLabel = new (std::nothrow) Label();
149         pLabel->Construct(Rectangle(X_GROUP_INDEX_DEFAULT_LABEL, Y_GROUP_INDEX_DEFAULT_LABEL, itemWidth, itemHeight), groupText);
150         pLabel->SetTextHorizontalAlignment(ALIGNMENT_LEFT);
151         pLabel->SetTextConfig(FONT_SIZE_GROUP_TITLE_TEXT, style);
152         pLabel->SetTextColor(COLOR_GROUP_TITLE_TEXT);
153
154         pItem->AddControl(pLabel);
155         relativeLayout.SetMargin(*pLabel, 0, RELATIVE_LAYOUT_RIGHT_MARGIN, 0, 0);
156         relativeLayout.SetRelation(*pLabel, pItem, RECT_EDGE_RELATION_LEFT_TO_LEFT);
157         relativeLayout.SetRelation(*pLabel, pItem, RECT_EDGE_RELATION_RIGHT_TO_RIGHT);
158         pItem->SetBackgroundColor(COLOR_BG_GROUP_INDEX_DEFAULT);
159         pItem->SetEnabled(false);
160
161         return pItem;
162 }
163
164 TableViewItem*
165 BrightnessForm::CreateItem(int groupIndex, int itemIndex,int itemWidth)
166 {
167         AppLogDebug("CreateItem group[%d] index[%d]", groupIndex, itemIndex);
168
169         int brightness = 0;
170         Rectangle itemMainRectangle;
171         TableViewAnnexStyle style = TABLE_VIEW_ANNEX_STYLE_NORMAL;
172         Slider* pSlider = null;
173         String itemMainText;
174         Label* pLabel = null;
175         int fontSize = GetFontSize();
176
177         TableViewItem* pItem = new (std::nothrow) TableViewItem();
178         ItemTypeOneLine(itemMainRectangle);
179
180         switch (itemIndex)
181         {
182         case ID_ITEM_BRIGHTNESS_AUTOMATIC:
183                 {
184                         itemMainText = ResourceManager::GetString(L"IDS_ST_BODY_AUTOMATIC");
185                         style = TABLE_VIEW_ANNEX_STYLE_ONOFF_SLIDING;
186                         RelativeLayout relativeLayout;
187                         relativeLayout.Construct();
188
189                         pItem->Construct(relativeLayout, Dimension(W_GROUP_ITEM_DEFAULT, H_GROUP_ITEM_DEFAULT), style);
190                         pItem->SetBackgroundColor(COLOR_BG_GROUP_ITEM_DEFAULT);
191
192                         pLabel = new (std::nothrow) Label();
193                         pLabel->Construct(itemMainRectangle, itemMainText);
194                         pLabel->SetTextHorizontalAlignment(ALIGNMENT_LEFT);
195                         pLabel->SetTextConfig(fontSize, LABEL_TEXT_STYLE_NORMAL);
196                         pLabel->SetTextColor(COLOR_MAIN_TEXT);
197
198                         pItem->AddControl(pLabel);
199                         relativeLayout.SetMargin(*pLabel, 0, RELATIVE_LAYOUT_RIGHT_MARGIN, 0, 0);
200                         relativeLayout.SetRelation(*pLabel, pItem, RECT_EDGE_RELATION_LEFT_TO_LEFT);
201                         relativeLayout.SetRelation(*pLabel, pItem, RECT_EDGE_RELATION_RIGHT_TO_RIGHT);
202                 }
203                 break;
204
205         case ID_ITEM_BRIGHTNESS_SLIDE_BAR:
206                 {
207                         itemMainRectangle.width = W_GROUP_ITEM_DEFAULT_SLIDER;
208                         itemMainRectangle.height = H_GROUP_ITEM_DEFAULT_SLIDER;
209
210                         RelativeLayout relativeLayout;
211                         relativeLayout.Construct();
212
213                         pItem->Construct(relativeLayout, Dimension(W_GROUP_ITEM_DEFAULT, H_GROUP_ITEM_DEFAULT_SLIDER), style);
214                         pItem->SetBackgroundColor(COLOR_BG_GROUP_ITEM_DEFAULT);
215
216                         if ((brightness = PowerManager::GetScreenBrightness()) == 0)
217                         {
218                                 brightness = BRIGHTNESS_DEFAULT_VALUE;
219                         }
220
221                         pSlider = new (std::nothrow) Slider();
222                         pSlider->Construct(itemMainRectangle, BACKGROUND_STYLE_NONE, true, GROUP_ITEM_SLIDER_MIN_VALUE, GROUP_ITEM_SLIDER_MAX_VALUE);
223                         pSlider->SetValue(brightness);
224                         pSlider->SetPosition(itemMainRectangle.x, itemMainRectangle.y + Y_GROUP_ITEM_SLIDER);
225                         pSlider->AddAdjustmentEventListener(*this);
226                         pSlider->AddSliderEventListener(*this);
227
228                         pItem->AddControl(pSlider);
229                         relativeLayout.SetMargin(*pSlider, 0, RELATIVE_LAYOUT_RIGHT_MARGIN, 0, 0);
230                         relativeLayout.SetRelation(*pSlider, pItem, RECT_EDGE_RELATION_LEFT_TO_LEFT);
231                         relativeLayout.SetRelation(*pSlider, pItem, RECT_EDGE_RELATION_RIGHT_TO_RIGHT);
232                 }
233                 break;
234
235         default:
236                 break;
237         }
238
239         return pItem;
240 }
241
242 bool
243 BrightnessForm::DeleteGroupItem(int groupIndex, TableViewGroupItem* pItem)
244 {
245         AppLogDebug("ENTER");
246
247         delete pItem;
248         pItem = null;
249
250         return true;
251 }
252
253 bool
254 BrightnessForm::DeleteItem(int groupIndex, int itemIndex, TableViewItem* pItem)
255 {
256         AppLogDebug("ENTER");
257
258         delete pItem;
259         pItem = null;
260
261         return true;
262 }
263
264 void
265 BrightnessForm::OnGroupedTableViewItemStateChanged(Tizen::Ui::Controls::GroupedTableView& tableView, int groupIndex, int itemIndex, Tizen::Ui::Controls::TableViewItem* pItem, Tizen::Ui::Controls::TableViewItemStatus status)
266 {
267         AppLogDebug("OnTableViewItemStateChanged group[%d] index[%d]", groupIndex, itemIndex);
268
269         int localGroup = 0;
270         int localItem = 0;
271
272         if (itemIndex == ID_ITEM_BRIGHTNESS_AUTOMATIC)
273         {
274                 switch (status)
275                 {
276                 case TABLE_VIEW_ITEM_STATUS_CHECKED:
277                         {
278                                 localGroup = ID_GROUP_BRIGHTNESS;
279                                 localItem = ID_ITEM_BRIGHTNESS_SLIDE_BAR;
280
281                                 __pTableView->SetItemEnabled(localGroup, localItem, false);
282                         }
283                         break;
284
285                 case TABLE_VIEW_ITEM_STATUS_UNCHECKED:
286                         {
287                                 localGroup = ID_GROUP_BRIGHTNESS;
288                                 localItem = ID_ITEM_BRIGHTNESS_SLIDE_BAR;
289
290                                 __pTableView->SetItemEnabled(localGroup, localItem, true);
291                         }
292                         break;
293
294                 default:
295                         break;
296                 }
297         }
298 }
299
300 void
301 BrightnessForm::OnAdjustmentValueChanged(const Tizen::Ui::Control& source, int adjustment)
302 {
303         result r = E_SUCCESS;
304
305         if ((r = PowerManager::SetScreenBrightness(adjustment)) == E_SUCCESS)
306         {
307                 AppLogDebug("Brightness Set adjustment : %d Level", adjustment);
308         }
309         else
310         {
311                 AppLogDebug("Brightness Set allowed from 1 to 10 : adjustment Fail. %d", adjustment);
312         }
313 }
314
315 void
316 BrightnessForm::OnSliderBarMoved(Tizen::Ui::Controls::Slider& source, int value)
317 {
318         result r = E_SUCCESS;
319
320         if ((r = PowerManager::SetScreenBrightness(value)) == E_SUCCESS)
321         {
322                 AppLogDebug("Brightness Set value : %d Level ", value);
323         }
324         else
325         {
326                 AppLogDebug("Brightness Set allowed from 1 to 10 : Fail : %d", value);
327         }
328 }
329
330 int
331 BrightnessForm::GetDefaultGroupItemHeight(void)
332 {
333         return H_GROUP_INDEX_NO_TITLE_DEFAULT;
334 }
335
336 int
337 BrightnessForm::GetDefaultItemHeight(void)
338 {
339         return H_GROUP_ITEM_DEFAULT;
340 }
341
342 void
343 BrightnessForm::UpdateGroupItem(int groupIndex, Tizen::Ui::Controls::TableViewGroupItem* pItem)
344 {
345 }
346
347 void
348 BrightnessForm::UpdateItem(int groupIndex, int itemIndex, Tizen::Ui::Controls::TableViewItem* pItem)
349 {
350 }
351
352 void
353 BrightnessForm::OnGroupedTableViewContextItemActivationStateChanged(Tizen::Ui::Controls::GroupedTableView& tableView, int groupIndex, int itemIndex, Tizen::Ui::Controls::TableViewContextItem* pContextItem, bool activated)
354 {
355 }
356
357 void
358 BrightnessForm::OnGroupedTableViewGroupItemStateChanged(Tizen::Ui::Controls::GroupedTableView& tableView, int groupIndex, Tizen::Ui::Controls::TableViewGroupItem* pItem, Tizen::Ui::Controls::TableViewItemStatus status)
359 {
360 }