9642de3cca16692f04270c9fdafb9dd67c6523ed
[apps/native/sample/ImageFeatureManager.git] / project / src / UpdatePanel.cpp
1 //
2 // Tizen C++ SDK
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.tizenopensource.org/license
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 #include "UpdatePanel.h"
19 #include "FileManagerForm.h"
20 #include "MainForm.h"
21
22 using namespace Tizen::Base;
23 using namespace Tizen::Graphics;
24 using namespace Tizen::Ui;
25 using namespace Tizen::Ui::Controls;
26 using namespace Tizen::Ui::Scenes;
27
28
29 UpdatePanel::UpdatePanel(void)
30     : __currentFeaturePath("")
31     , __currentImagePath("")
32         , __prevFeaturesCount(0)
33 {
34 }
35
36 UpdatePanel::~UpdatePanel(void)
37 {
38 }
39
40 bool
41 UpdatePanel::Initialize()
42 {
43     Panel::Construct(L"IDC_UPDATE_MODE_PANEL");
44     return true;
45 }
46
47 result
48 UpdatePanel::OnInitializing(void)
49 {
50     result r = E_SUCCESS;
51
52     Button *pButton = null;
53     Tizen::Ui::Controls::Frame *pFrame = Tizen::App::Application::GetInstance()->GetAppFrame()->GetFrame();
54     MainForm* pForm = static_cast<MainForm*>(pFrame->GetControl(L"IDF_FORM", true));
55
56     Rectangle clienRect = pForm->GetClientAreaBounds();
57     this->SetBounds(clienRect);
58
59     pButton = static_cast<Button*>(GetControl(L"IDC_BUTTON_LOAD_FEATURE_SET", true));
60     if (pButton)
61     {
62         pButton->SetActionId(ID_BUTTON_LOAD_FEATURE_SET);
63         pButton->AddActionEventListener(*this);
64     }
65
66     return r;
67 }
68
69 result
70 UpdatePanel::OnTerminating(void)
71 {
72     result r = E_SUCCESS;
73     return r;
74 }
75
76 void
77 UpdatePanel::OnActionPerformed(const Tizen::Ui::Control& source, int actionId)
78 {
79     SceneManager* pSceneManager = SceneManager::GetInstance();
80     AppAssert(pSceneManager);
81
82     switch (actionId)
83     {
84     case ID_BUTTON_LOAD_FEATURE_SET:
85     {
86         pSceneManager->GoForward(
87                 ForwardSceneTransition("ChooseExistingFeatureSetScene",
88                         SCENE_TRANSITION_ANIMATION_TYPE_ZOOM_IN,
89                         SCENE_HISTORY_OPTION_ADD_HISTORY,
90                         SCENE_DESTROY_OPTION_KEEP), 0);
91     }
92     break;
93     }
94 }
95
96
97 void
98 UpdatePanel::OnSceneActivatedN(
99         const Tizen::Ui::Scenes::SceneId& previousSceneId,
100         const Tizen::Ui::Scenes::SceneId& currentSceneId,
101         Tizen::Base::Collection::IList* pArgs)
102 {
103
104     Tizen::Ui::Controls::Frame *pFrame = Tizen::App::Application::GetInstance()->GetAppFrame()->GetFrame();
105     MainForm* pForm = static_cast<MainForm*>(pFrame->GetControl(L"IDF_FORM", true));
106     Footer* pFooter = pForm->GetFooter();
107
108     if ((previousSceneId == "ChooseImageScene") && pArgs)
109     {
110         if (pArgs->GetCount() > 0)
111         {
112             __currentImagePath   = *(dynamic_cast<String*>(pArgs->GetAt(0)));
113             pForm->AppendImageFolder(__currentImagePath, this);
114
115                 if (pForm->GetFeatureManager()->GetTotalNumberOfFeatures() != __prevFeaturesCount)
116                 {
117                                 Footer* pFooter = pForm->GetFooter();
118                                 pFooter->SetItemEnabled(0, true);
119                 }
120         }
121     }
122     else if ((previousSceneId == "ChooseExistingFeatureSetScene") && pArgs)
123     {
124         if (pArgs->GetCount() > 0)
125         {
126             __currentFeaturePath   = *(dynamic_cast<String*>(pArgs->GetAt(0)));
127
128             result res = pForm->GetFeatureManager()->Load(__currentFeaturePath);
129             AppLog("Result:: %s %ls", GetErrorMessage(res), __currentFeaturePath.GetPointer());
130
131             if (IsFailed(res))
132             {
133                 __currentFeaturePath = "";
134                 SendUserEvent(REQUEST_MESSAGE_LOAD_FAILED, null);
135             }
136             else
137             {
138                 __prevFeaturesCount = pForm->GetFeatureManager()->GetTotalNumberOfFeatures();
139                 pFooter->SetItemEnabled(0, false);
140             }
141         }
142     }
143     else if (previousSceneId == "GenerationModeScene")
144     {
145         result res = pForm->GetFeatureManager()->Load(__currentFeaturePath);
146         AppLog("Result:: %s %ls", GetErrorMessage(res), __currentFeaturePath.GetPointer());
147         __prevFeaturesCount = pForm->GetFeatureManager()->GetTotalNumberOfFeatures();
148         pFooter->SetItemEnabled(0, false);
149
150     }
151     else if (previousSceneId == "ManageScene")
152     {
153         if (pForm->GetFeatureManager()->GetTotalNumberOfFeatures() != __prevFeaturesCount)
154         {
155             Footer* pFooter = pForm->GetFooter();
156                         pFooter->SetItemEnabled(0, true);
157                 SendUserEvent(REQUEST_MESSAGE_AFTER_DELETE, null);
158         }
159     }
160
161     if (pArgs)
162     {
163         pArgs->RemoveAll(true);
164         delete pArgs;
165     }
166
167
168     Label* pLabel = null;
169     pLabel = static_cast<Label*>(GetControl(L"IDC_FEATURE_SET_PATH_LABEL", true));
170     if (pLabel)
171     {
172         pLabel->SetText(__currentFeaturePath);
173     }
174
175
176     if (__currentFeaturePath.IsEmpty())
177     {
178         pFooter->SetItemEnabled(0, false);
179         pFooter->SetButtonEnabled(BUTTON_POSITION_LEFT, false);
180     }
181     else
182     {
183         pFooter->SetButtonEnabled(BUTTON_POSITION_LEFT, true);
184     }
185 }
186
187 void
188 UpdatePanel::OnSceneDeactivated(const Tizen::Ui::Scenes::SceneId& currentSceneId,
189         const Tizen::Ui::Scenes::SceneId& nextSceneId)
190 {
191 }
192
193 void
194 UpdatePanel::OnImageProcessed(int imNo, int total)
195 {
196     Tizen::Ui::Controls::Frame *pFrame = Tizen::App::Application::GetInstance()->GetAppFrame()->GetFrame();
197     MainForm* pForm = static_cast<MainForm*>(pFrame->GetControl(L"IDF_FORM", true));
198     pForm->OnImageProcessed(imNo, total);
199 }
200
201
202 void
203 UpdatePanel::OnFinish(void)
204 {
205     Tizen::Ui::Controls::Frame *pFrame = Tizen::App::Application::GetInstance()->GetAppFrame()->GetFrame();
206     MainForm* pForm = static_cast<MainForm*>(pFrame->GetControl(L"IDF_FORM", true));
207
208     __currentImagePath = "";
209     Button* pButtonPath = static_cast<Button*>(GetControl(L"IDC_BUTTON_UPDATE_IMAGE_FOLDER", true));
210     if (pButtonPath)
211     {
212         pButtonPath->SetText(__currentImagePath);
213     }
214
215     if (pForm->GetFeatureManager()->GetTotalNumberOfFeatures() != __prevFeaturesCount)
216     {
217         Footer* pFooter = pForm->GetFooter();
218         pFooter->SetItemEnabled(0, true);
219         __prevFeaturesCount = pForm->GetFeatureManager()->GetTotalNumberOfFeatures();
220     }
221
222     pForm->OnFinish();
223 }
224
225 void
226 UpdatePanel::OnUserEventReceivedN(RequestId requestId, Tizen::Base::Collection::IList* pArgs)
227 {
228     SceneManager* pSceneManager = SceneManager::GetInstance();
229     AppAssert(pSceneManager);
230
231         switch (requestId)
232         {
233
234         case UpdatePanel::REQUEST_MESSAGE_LOAD_FAILED:
235         {
236         MessageBox msgBox;
237         int res = 0;
238
239         msgBox.Construct("Error", "Can't load feature set.", MSGBOX_STYLE_OK, 0);
240         msgBox.ShowAndWait(res);
241         }
242         break;
243         case UpdatePanel::REQUEST_MESSAGE_AFTER_DELETE:
244         {
245                 Tizen::Ui::Controls::Frame *pFrame = Tizen::App::Application::GetInstance()->GetAppFrame()->GetFrame();
246                 MainForm* pForm = static_cast<MainForm*>(pFrame->GetControl(L"IDF_FORM", true));
247
248                 Tizen::Base::String message;
249                 message.Format(80, L"Deleted %i features.", __prevFeaturesCount - pForm->GetFeatureManager()->GetTotalNumberOfFeatures());
250
251         MessageBox msgBox;
252         int res = 0;
253
254         msgBox.Construct("Update", message, MSGBOX_STYLE_OK, 0);
255         msgBox.ShowAndWait(res);
256
257         __prevFeaturesCount = pForm->GetFeatureManager()->GetTotalNumberOfFeatures();
258         }
259         break;
260     case UpdatePanel::REQUEST_ADD:
261     {
262         pSceneManager->GoForward(
263                 ForwardSceneTransition("ChooseImageScene",
264                         SCENE_TRANSITION_ANIMATION_TYPE_ZOOM_IN,
265                         SCENE_HISTORY_OPTION_ADD_HISTORY,
266                         SCENE_DESTROY_OPTION_KEEP), /*pList*/null);
267     }
268     break;
269     case UpdatePanel::REQUEST_DELETE:
270         {
271         pSceneManager->GoForward(
272                 ForwardSceneTransition("ManageScene",
273                         SCENE_TRANSITION_ANIMATION_TYPE_ZOOM_IN,
274                         SCENE_HISTORY_OPTION_ADD_HISTORY,
275                         SCENE_DESTROY_OPTION_KEEP), null);
276         }
277         break;
278     case UpdatePanel::REQUEST_UPDATE:
279         {
280                 Tizen::Ui::Controls::Frame *pFrame = Tizen::App::Application::GetInstance()->GetAppFrame()->GetFrame();
281                 MainForm* pForm = static_cast<MainForm*>(pFrame->GetControl(L"IDF_FORM", true));
282
283                 //Disabling update button
284                 Footer* pFooter = pForm->GetFooter();
285                 pFooter->SetItemEnabled(0, false);
286
287         result res = pForm->GetFeatureManager()->Flush(0);
288         AppLog("Result:: on Flush %s", GetErrorMessage(res));
289
290                 if (!IsFailed(res))
291                 {
292                         pForm->GetFeatureManager()->Load(__currentFeaturePath);
293
294                         MessageBox msgBox;
295                         int res = 0;
296
297                         String message;
298                         int currentFeaturesCount = pForm->GetFeatureManager()->GetTotalNumberOfFeatures();
299
300                         message.Format(80, L"Feature set successfully updated.\nTotal features count: %i", currentFeaturesCount);
301
302                         __prevFeaturesCount = currentFeaturesCount;
303
304                         msgBox.Construct("Update", message, MSGBOX_STYLE_OK, 0);
305                         msgBox.ShowAndWait(res);
306
307                 }
308                 else
309                 {
310                         MessageBox msgBox;
311                         int res = 0;
312
313                         msgBox.Construct("Update", "Feature set failed to update.", MSGBOX_STYLE_OK, 0);
314                         msgBox.ShowAndWait(res);
315
316                         pForm->GetFeatureManager()->Load(__currentFeaturePath);
317                 }
318         }
319         break;
320     default:
321         break;
322         }
323 }