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