Fixed Delete button disabling.
[apps/native/sample/ImageFeatureManager.git] / project / src / UpdatePanel.cpp
1 #include "UpdatePanel.h"
2 #include "FileManagerForm.h"
3 #include "MainForm.h"
4
5 using namespace Tizen::Base;
6 using namespace Tizen::Graphics;
7 using namespace Tizen::Ui;
8 using namespace Tizen::Ui::Controls;
9 using namespace Tizen::Ui::Scenes;
10
11
12 UpdatePanel::UpdatePanel(void)
13     : __currentFeaturePath("")
14     , __currentImagePath("")
15         , __prevFeaturesCount(0)
16 {
17 }
18
19 UpdatePanel::~UpdatePanel(void)
20 {
21 }
22
23 bool
24 UpdatePanel::Initialize()
25 {
26     Panel::Construct(L"IDC_UPDATE_MODE_PANEL");
27     return true;
28 }
29
30 result
31 UpdatePanel::OnInitializing(void)
32 {
33     result r = E_SUCCESS;
34
35     Button *pButton = null;
36     Tizen::Ui::Controls::Frame *pFrame = Tizen::App::Application::GetInstance()->GetAppFrame()->GetFrame();
37     MainForm* pForm = static_cast<MainForm*>(pFrame->GetControl(L"IDF_FORM", true));
38
39     Rectangle clienRect = pForm->GetClientAreaBounds();
40     this->SetBounds(clienRect);
41
42     pButton = static_cast<Button*>(GetControl(L"IDC_BUTTON_LOAD_FEATURE_SET", true));
43     if (pButton)
44     {
45         pButton->SetActionId(ID_BUTTON_LOAD_FEATURE_SET);
46         pButton->AddActionEventListener(*this);
47     }
48
49     return r;
50 }
51
52 result
53 UpdatePanel::OnTerminating(void)
54 {
55     result r = E_SUCCESS;
56     return r;
57 }
58
59 void
60 UpdatePanel::OnActionPerformed(const Tizen::Ui::Control& source, int actionId)
61 {
62     SceneManager* pSceneManager = SceneManager::GetInstance();
63     AppAssert(pSceneManager);
64
65     switch (actionId)
66     {
67     case ID_BUTTON_LOAD_FEATURE_SET:
68     {
69         pSceneManager->GoForward(
70                 ForwardSceneTransition("ChooseExistingFeatureSetScene",
71                         SCENE_TRANSITION_ANIMATION_TYPE_ZOOM_IN,
72                         SCENE_HISTORY_OPTION_ADD_HISTORY,
73                         SCENE_DESTROY_OPTION_KEEP), 0);
74     }
75     break;
76     }
77 }
78
79
80 void
81 UpdatePanel::OnSceneActivatedN(
82         const Tizen::Ui::Scenes::SceneId& previousSceneId,
83         const Tizen::Ui::Scenes::SceneId& currentSceneId,
84         Tizen::Base::Collection::IList* pArgs)
85 {
86
87     Tizen::Ui::Controls::Frame *pFrame = Tizen::App::Application::GetInstance()->GetAppFrame()->GetFrame();
88     MainForm* pForm = static_cast<MainForm*>(pFrame->GetControl(L"IDF_FORM", true));
89     Footer* pFooter = pForm->GetFooter();
90
91     if ((previousSceneId == "ChooseImageScene") && pArgs)
92     {
93         if (pArgs->GetCount() > 0)
94         {
95             __currentImagePath   = *(dynamic_cast<String*>(pArgs->GetAt(0)));
96             pForm->AppendImageFolder(__currentImagePath, this);
97
98             Footer* pFooter = pForm->GetFooter();
99                         pFooter->SetItemEnabled(0, true);
100         }
101     }
102     else if ((previousSceneId == "ChooseExistingFeatureSetScene") && pArgs)
103     {
104         if (pArgs->GetCount() > 0)
105         {
106             __currentFeaturePath   = *(dynamic_cast<String*>(pArgs->GetAt(0)));
107             if (E_SUCCESS != pForm->GetFeatureManager()->Load(__currentFeaturePath))
108             {
109                 __currentFeaturePath = "";
110             }
111             else
112             {
113                 __prevFeaturesCount = pForm->GetFeatureManager()->GetTotalNumberOfFeatures();
114                 pFooter->SetItemEnabled(0, false);
115             }
116         }
117     }
118     else if (previousSceneId == "GenerationModeScene")
119     {
120         pForm->GetFeatureManager()->Load(__currentFeaturePath);
121         __prevFeaturesCount = pForm->GetFeatureManager()->GetTotalNumberOfFeatures();
122         pFooter->SetItemEnabled(0, false);
123
124     }
125     else if (previousSceneId == "ManageScene")
126     {
127         if (pForm->GetFeatureManager()->GetTotalNumberOfFeatures() != __prevFeaturesCount)
128         {
129             Footer* pFooter = pForm->GetFooter();
130                         pFooter->SetItemEnabled(0, true);
131         }
132     }
133
134     if (pArgs)
135     {
136         pArgs->RemoveAll(true);
137         delete pArgs;
138     }
139
140
141     Label* pLabel = null;
142     pLabel = static_cast<Label*>(GetControl(L"IDC_FEATURE_SET_PATH_LABEL", true));
143     if (pLabel)
144     {
145         pLabel->SetText(__currentFeaturePath);
146     }
147
148
149     if (__currentFeaturePath.IsEmpty())
150     {
151         pFooter->SetItemEnabled(0, false);
152         pFooter->SetButtonEnabled(BUTTON_POSITION_LEFT, false);
153     }
154     else
155     {
156         pFooter->SetButtonEnabled(BUTTON_POSITION_LEFT, true);
157     }
158 }
159
160 void
161 UpdatePanel::OnSceneDeactivated(const Tizen::Ui::Scenes::SceneId& currentSceneId,
162         const Tizen::Ui::Scenes::SceneId& nextSceneId)
163 {
164 }
165
166 void
167 UpdatePanel::OnImageProcessed(int imNo, int total)
168 {
169     Tizen::Ui::Controls::Frame *pFrame = Tizen::App::Application::GetInstance()->GetAppFrame()->GetFrame();
170     MainForm* pForm = static_cast<MainForm*>(pFrame->GetControl(L"IDF_FORM", true));
171     pForm->OnImageProcessed(imNo, total);
172 }
173
174
175 void
176 UpdatePanel::OnFinish(void)
177 {
178     Tizen::Ui::Controls::Frame *pFrame = Tizen::App::Application::GetInstance()->GetAppFrame()->GetFrame();
179     MainForm* pForm = static_cast<MainForm*>(pFrame->GetControl(L"IDF_FORM", true));
180
181     __currentImagePath = "";
182     Button* pButtonPath = static_cast<Button*>(GetControl(L"IDC_BUTTON_UPDATE_IMAGE_FOLDER", true));
183     if (pButtonPath)
184     {
185         pButtonPath->SetText(__currentImagePath);
186     }
187
188     if (pForm->GetFeatureManager()->GetTotalNumberOfFeatures() != __prevFeaturesCount)
189     {
190         Footer* pFooter = pForm->GetFooter();
191         pFooter->SetItemEnabled(0, true);
192     }
193
194     pForm->OnFinish();
195 }
196
197 void
198 UpdatePanel::OnUserEventReceivedN(RequestId requestId, Tizen::Base::Collection::IList* pArgs)
199 {
200     SceneManager* pSceneManager = SceneManager::GetInstance();
201     AppAssert(pSceneManager);
202
203         switch (requestId)
204         {
205     case UpdatePanel::REQUEST_ADD:
206     {
207         pSceneManager->GoForward(
208                 ForwardSceneTransition("ChooseImageScene",
209                         SCENE_TRANSITION_ANIMATION_TYPE_ZOOM_IN,
210                         SCENE_HISTORY_OPTION_ADD_HISTORY,
211                         SCENE_DESTROY_OPTION_KEEP), /*pList*/null);
212     }
213     break;
214     case UpdatePanel::REQUEST_DELETE:
215         {
216         pSceneManager->GoForward(
217                 ForwardSceneTransition("ManageScene",
218                         SCENE_TRANSITION_ANIMATION_TYPE_ZOOM_IN,
219                         SCENE_HISTORY_OPTION_ADD_HISTORY,
220                         SCENE_DESTROY_OPTION_KEEP), null);
221         }
222         break;
223     case UpdatePanel::REQUEST_UPDATE:
224         {
225                 Tizen::Ui::Controls::Frame *pFrame = Tizen::App::Application::GetInstance()->GetAppFrame()->GetFrame();
226                 MainForm* pForm = static_cast<MainForm*>(pFrame->GetControl(L"IDF_FORM", true));
227
228                 //Disabling update button
229                 Footer* pFooter = pForm->GetFooter();
230                 pFooter->SetItemEnabled(0, false);
231
232                 if (E_SUCCESS == pForm->GetFeatureManager()->Flush(0))
233                 {
234                         pForm->GetFeatureManager()->Load(__currentFeaturePath);
235
236                         MessageBox msgBox;
237                         int res = 0;
238
239                         String message;
240                         int currentFeaturesCount = pForm->GetFeatureManager()->GetTotalNumberOfFeatures();
241
242                         if (__prevFeaturesCount > currentFeaturesCount)
243                         {
244                                 message.Format(80, L"Feature set successfully updated.\nDeleted %i features.", __prevFeaturesCount - currentFeaturesCount);
245                         }
246                         else
247                         {
248                                 message.Format(80, L"Feature set successfully updated.\nAdded %i features.", currentFeaturesCount - __prevFeaturesCount);
249                         }
250
251                         __prevFeaturesCount = currentFeaturesCount;
252
253                         msgBox.Construct("Update", message, MSGBOX_STYLE_OK, 0);
254                         msgBox.ShowAndWait(res);
255
256                 }
257                 else
258                 {
259                         MessageBox msgBox;
260                         int res = 0;
261
262                         msgBox.Construct("Update", "Feature set failed to update.", MSGBOX_STYLE_OK, 0);
263                         msgBox.ShowAndWait(res);
264
265                         pForm->GetFeatureManager()->Load(__currentFeaturePath);
266                 }
267         }
268         break;
269     default:
270         break;
271         }
272 }