96fe1462c3c44f414d9a3f323c3ba3625641b4ba
[apps/native/sample/ImageFeatureManager.git] / project / src / MainForm.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 <FApp.h>
19 #include "MainForm.h"
20 #include "FileManagerForm.h"
21 #include "GeneratePanel.h"
22 #include "UpdatePanel.h"
23
24 using namespace Tizen::Base;
25 using namespace Tizen::App;
26 using namespace Tizen::Graphics;
27 using namespace Tizen::Ui;
28 using namespace Tizen::Ui::Controls;
29 using namespace Tizen::Ui::Scenes;
30
31 static const String SAMPLE_TITLE = L"IMAGE FEATURE MANAGER";
32
33 MainForm::MainForm(void)
34     : __headerTitle(SAMPLE_TITLE)
35     , __pRadioGroupMode(null)
36     , __pFeatureManager(null)
37     , __pMessagePopup(null)
38     , __pMessageLabel(null)
39 {
40 }
41
42 MainForm::~MainForm(void)
43 {
44     if (__pFeatureManager)
45     {
46         delete __pFeatureManager;
47     }
48     delete __pMessagePopup;
49 }
50
51 bool
52 MainForm::Initialize(void)
53 {
54     Construct(L"IDF_FORM");
55     return true;
56 }
57
58 result
59 MainForm::OnInitializing(void)
60 {
61     result r = E_SUCCESS;
62
63     SetFormBackEventListener(this);
64
65     CheckButton* pCheckButton = null;
66
67     __pRadioGroupMode = new RadioGroup();
68     __pRadioGroupMode->Construct();
69
70     pCheckButton = static_cast<CheckButton*>(GetControl(L"IDC_UPDATE_MODE_BUTTON", true));
71     if (pCheckButton)
72     {
73         pCheckButton->AddActionEventListener(*this);
74         pCheckButton->SetActionId(ID_BUTTON_UPDATE_MODE, 0, 0);
75         __pRadioGroupMode->Add(*pCheckButton);
76     }
77
78     pCheckButton = static_cast<CheckButton*>(GetControl(L"IDC_GENERATION_MODE_BUTTON", true));
79
80     if (pCheckButton)
81     {
82         pCheckButton->SetSelected(true);
83         pCheckButton->AddActionEventListener(*this);
84         pCheckButton->SetActionId(ID_BUTTON_GENERATE_MODE, 0, 0);
85         __pRadioGroupMode->Add(*pCheckButton);
86     }
87
88     Footer* pFooter = GetFooter();
89     pFooter->SetStyle(FOOTER_STYLE_BUTTON_ICON_TEXT);
90     pFooter->SetBackButton();
91
92     FooterItem pFooterItemSend;
93     pFooterItemSend.Construct(ID_FOOTER_ITEM_GENERATE);
94     pFooterItemSend.SetText(L"Generate");
95     pFooter->AddItem(pFooterItemSend);
96     pFooter->AddActionEventListener(*this);
97
98     __dbGen.Construct();
99     __dbGen.SetListener(this);
100
101     __pFeatureManager = new Tizen::Uix::Vision::ImageFeatureManager();
102     __pFeatureManager->Construct();
103
104     __pMessagePopup = new Popup();
105     __pMessagePopup->Construct("IDC_MESSAGE_POPUP");
106     __pMessageLabel = static_cast<Label*>(__pMessagePopup->GetControl("IDC_LABEL_MASSEGE_TEXT", true));
107
108     __pContextMenu = new ContextMenu();
109     __pContextMenu->Construct(Point(50, 1200), CONTEXT_MENU_STYLE_LIST);
110
111     __pContextMenu->AddItem(L"Add Features", ID_CONTEXT_MENU_ADD);
112     __pContextMenu->AddItem(L"Delete Features", ID_CONTEXT_MENU_DELETE);
113     __pContextMenu->AddActionEventListener(*this);
114
115     Button* pButton = static_cast<Button*>(__pMessagePopup->GetControl("IDC_BUTTON_MESSAGE_CLOSE", true));
116     if (pButton)
117     {
118         pButton->AddActionEventListener(*this);
119         pButton->SetActionId(ID_BUTTON_CLOSE_RESULTS);
120     }
121
122     return r;
123 }
124
125 result
126 MainForm::OnTerminating(void)
127 {
128     result r = E_SUCCESS;
129     return r;
130 }
131
132 void
133 MainForm::OnActionPerformed(const Tizen::Ui::Control& source, int actionId)
134 {
135     SceneManager* pSceneManager = SceneManager::GetInstance();
136     AppAssert(pSceneManager);
137
138     switch (actionId)
139     {
140     case ID_BUTTON_GENERATE_MODE:
141     {
142
143         Footer* pFooter = GetFooter();
144         pFooter->RemoveAllItems();
145         pFooter->RemoveAllButtons();
146
147
148         FooterItem pFooterItemGenerate;
149         pFooterItemGenerate.Construct(ID_FOOTER_ITEM_GENERATE);
150         pFooterItemGenerate.SetText(L"Generate");
151         pFooter->AddItem(pFooterItemGenerate);
152
153         pFooter->SetBackButton();
154         pFooter->AddActionEventListener(*this);
155
156         __pFeatureManager->DeleteAllFeatures();
157         pSceneManager->GoForward(
158                 ForwardSceneTransition("GenerationModeScene",
159                         SCENE_TRANSITION_ANIMATION_TYPE_NONE,
160                         SCENE_HISTORY_OPTION_NO_HISTORY,
161                         SCENE_DESTROY_OPTION_KEEP), null);
162     }
163         break;
164     case ID_BUTTON_UPDATE_MODE:
165     {
166         Footer* pFooter = GetFooter();
167         pFooter->RemoveAllItems();
168         pFooter->RemoveAllButtons();
169
170         AppResource* pAppResource = Application::GetInstance()->GetAppResource();
171         Tizen::Graphics::Bitmap* pNormalIcon = pAppResource->GetBitmapN(L"00_more_icon_normal.png");
172         Tizen::Graphics::Bitmap* pPressedIcon = pAppResource->GetBitmapN(L"00_more_icon_pressed.png");
173         Tizen::Graphics::Bitmap* pDisabledIcon = pAppResource->GetBitmapN(L"00_more_icon_disabled.png");
174
175         ButtonItem  buttonItem;
176         buttonItem.Construct(BUTTON_ITEM_STYLE_ICON, ID_FOOTER_ITEM_SHOW_MENU);
177         buttonItem.SetIcon(BUTTON_ITEM_STATUS_NORMAL, pNormalIcon);
178         buttonItem.SetIcon(BUTTON_ITEM_STATUS_PRESSED, pPressedIcon);
179         buttonItem.SetIcon(BUTTON_ITEM_STATUS_DISABLED, pDisabledIcon);
180
181         pFooter->SetButton(BUTTON_POSITION_LEFT, buttonItem);
182
183         FooterItem pFooterItemUpdate;
184         pFooterItemUpdate.Construct(ID_FOOTER_ITEM_UPDATE);
185         pFooterItemUpdate.SetText(L"Update");
186         pFooter->AddItem(pFooterItemUpdate);
187
188         pFooter->SetBackButton();
189         pFooter->AddActionEventListener(*this);
190
191         __pFeatureManager->DeleteAllFeatures();
192         pSceneManager->GoForward(
193                 ForwardSceneTransition("UpdateModeScene",
194                         SCENE_TRANSITION_ANIMATION_TYPE_NONE,
195                         SCENE_HISTORY_OPTION_NO_HISTORY,
196                         SCENE_DESTROY_OPTION_KEEP), null);
197     }
198         break;
199     case ID_BUTTON_CLOSE_RESULTS:
200         __pMessagePopup->SetShowState(false);
201         break;
202
203     case ID_FOOTER_ITEM_GENERATE:
204     {
205         Panel* pPanel = static_cast<Panel*>(this->GetControl("IDC_GENERATION_MODE_PANEL", true));
206         if (pPanel)
207         {
208                 pPanel->SendUserEvent(GeneratePanel::REQUEST_GENERATE_FEATURE_SET, null);
209         }
210     }
211         break;
212     case ID_FOOTER_ITEM_UPDATE:
213     {
214         Panel* pPanel = static_cast<Panel*>(this->GetControl("IDC_UPDATE_MODE_PANEL", true));
215         if (pPanel)
216         {
217                 pPanel->SendUserEvent(UpdatePanel::REQUEST_UPDATE, null);
218         }
219     }
220         break;
221     case ID_FOOTER_ITEM_SHOW_MENU:
222     {
223                 __pContextMenu->SetShowState(true);
224                 __pContextMenu->Show();
225     }
226         break;
227     case ID_CONTEXT_MENU_ADD:
228     {
229         Panel* pPanel = static_cast<Panel*>(this->GetControl("IDC_UPDATE_MODE_PANEL", true));
230         if (pPanel)
231         {
232                 pPanel->SendUserEvent(UpdatePanel::REQUEST_ADD, null);
233         }
234     }
235         break;
236     case ID_CONTEXT_MENU_DELETE:
237     {
238         Panel* pPanel = static_cast<Panel*>(this->GetControl("IDC_UPDATE_MODE_PANEL", true));
239         if (pPanel)
240         {
241                 pPanel->SendUserEvent(UpdatePanel::REQUEST_DELETE, null);
242         }
243     }
244         break;
245     default:
246         break;
247     }
248 }
249
250 void
251 MainForm::AppendImageFolder(const Tizen::Base::String& imPath, IFeatureSetGeneratorListener* listener)
252 {
253     if (listener != null)
254     {
255         __dbGen.SetListener(listener);
256     }
257     else
258     {
259         __dbGen.SetListener(this);
260     }
261     __dbGen.SetImagePath(imPath);
262     __dbGen.SetFeatureManager(__pFeatureManager);
263     this->SetEnabled(false);
264     __dbGen.Start();
265 }
266
267 void
268 MainForm::OnFormBackRequested(Tizen::Ui::Controls::Form& source)
269 {
270     UiApp* pApp = UiApp::GetInstance();
271     AppAssert(pApp);
272     pApp->Terminate();
273 }
274
275 void
276 MainForm::OnSceneActivatedN(
277         const Tizen::Ui::Scenes::SceneId& previousSceneId,
278         const Tizen::Ui::Scenes::SceneId& currentSceneId,
279         Tizen::Base::Collection::IList* pArgs)
280 {
281 }
282
283 void
284 MainForm::OnSceneDeactivated(const Tizen::Ui::Scenes::SceneId& currentSceneId,
285         const Tizen::Ui::Scenes::SceneId& nextSceneId)
286 {
287 }
288
289 void
290 MainForm::OnImageProcessed(int imNo, int total)
291 {
292
293     SceneManager* pSceneManager = SceneManager::GetInstance();
294     AppAssert(pSceneManager);
295
296     if (pSceneManager->GetCurrentSceneId() == "GenerationModeScene")
297     {
298         __headerTitle = "Generating... ";
299     }
300     else if (pSceneManager->GetCurrentSceneId() == "UpdateModeScene")
301     {
302         __headerTitle = "Updating... ";
303     }
304
305     __headerTitle.Append(Integer(imNo).ToString());
306     __headerTitle.Append('/');
307     __headerTitle.Append(Integer(total).ToString());
308     RequestRedraw(true);
309 }
310
311 void
312 MainForm::OnFinish(void)
313 {
314     SendUserEvent(REQUEST_RESULT, 0);
315 }
316
317 result
318 MainForm::OnDraw(void)
319 {
320     Header* pHeader = GetHeader();
321     pHeader->SetTitleText(__headerTitle);
322     return E_SUCCESS;
323 }
324
325 void
326 MainForm::OnUserEventReceivedN(RequestId requestId, Tizen::Base::Collection::IList* pArgs)
327 {
328     switch(requestId)
329     {
330     case REQUEST_RESULT:
331     {
332         __dbGen.GetResults(__resInfo);
333         this->SetEnabled(true);
334         this->RequestRedraw(true);
335         __headerTitle = SAMPLE_TITLE;
336         if (__resInfo.appendTime > 0)
337         {
338             if (__resInfo.appendedObjectsNumber != 0)
339             {
340                 __pMessageLabel->SetText(
341                         "\nTotal time: " + Integer(__resInfo.appendTime).ToString() + "ms" +
342                         "\nAverage time: " + Integer(__resInfo.appendTime / __resInfo.appendedObjectsNumber).ToString() + " ms" +
343                         "\nAdded images: " + Integer(__resInfo.appendedObjectsNumber).ToString() +
344                         "\nRejected files: " + Integer(__resInfo.rejectedObjectsNumber).ToString() +
345                         "\nTotal files count: " + Integer(__resInfo.totalObjectsNumber).ToString()
346                 );
347             }
348             else
349             {
350                 __pMessageLabel->SetText(
351                         "\nTotal time: " + Integer(__resInfo.appendTime).ToString() + "ms" +
352                         "\nAdded images: " + Integer(__resInfo.appendedObjectsNumber).ToString() +
353                         "\nRejected files: " + Integer(__resInfo.rejectedObjectsNumber).ToString() +
354                         "\nTotal files count: " + Integer(__resInfo.totalObjectsNumber).ToString()
355                 );
356             }
357
358             __pMessagePopup->SetShowState(true);
359             __pMessagePopup->Show();
360         }
361         break;
362     }
363     default:
364         break;
365     }
366 }
367
368 Tizen::Uix::Vision::ImageFeatureManager*
369 MainForm::GetFeatureManager(void)
370 {
371     return __pFeatureManager;
372 }