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