e01f03f78638d5a6e3d3f0deb2389e1b73fb82ee
[apps/native/sample/ImageFeatureManager.git] / project / src / FeatureManagerForm.cpp
1 //
2 // Tizen Native SDK
3 // Open Service Platform
4 // Copyright (c) 2013 Samsung Electronics Co., Ltd.
5 // All rights reserved.
6 //
7 // This software contains confidential and proprietary information
8 // of Samsung Electronics Co., Ltd.
9 // The user of this software agrees not to disclose, disseminate or copy such
10 // Confidential Information and shall use the software only in accordance with
11 // the terms of the license agreement the user entered into with Samsung.
12 //
13
14 #include "FeatureManagerForm.h"
15 #include "MainForm.h"
16
17 using namespace Tizen::App;
18 using namespace Tizen::Ui;
19 using namespace Tizen::Ui::Controls;
20 using namespace Tizen::Ui::Scenes;
21 using namespace Tizen::Base;
22 using namespace Tizen::Graphics;
23
24 FeatureManagerForm::FeatureManagerForm(void)
25     : __pIconListView(null)
26     , __pPopup(0)
27 {
28 }
29
30 FeatureManagerForm::~FeatureManagerForm(void)
31 {
32 }
33
34 bool
35 FeatureManagerForm::Initialize(void)
36 {
37     Construct(FORM_STYLE_NORMAL| FORM_STYLE_HEADER| FORM_STYLE_INDICATOR | FORM_STYLE_FOOTER);
38
39     SetFormBackEventListener(this);
40
41     return true;
42 }
43
44 result
45 FeatureManagerForm::OnInitializing(void)
46 {
47     Header* pHeader = GetHeader();
48     pHeader->SetTitleText(L"Delete Features");
49     if (!CreateMainList())
50     {
51         return E_FAILURE;
52     }
53
54     Footer* pFooter = GetFooter();
55         pFooter->SetStyle(FOOTER_STYLE_BUTTON_TEXT);
56         pFooter->SetBackButton();
57
58         FooterItem pFooterItemSend;
59         pFooterItemSend.Construct(ID_FOOTER_ITEM_DELETE);
60         pFooterItemSend.SetText(L"Delete");
61         pFooter->AddItem(pFooterItemSend);
62         pFooter->AddActionEventListener(*this);
63
64     return E_SUCCESS;
65 }
66
67 result
68 FeatureManagerForm::OnTerminating(void)
69 {
70     result r = E_SUCCESS;
71     return r;
72 }
73
74 bool
75 FeatureManagerForm::CreateMainList(void)
76 {
77         __pIconListView = new IconListView();
78         __pIconListView->Construct(Rectangle(0, 0, GetClientAreaBounds().width, GetClientAreaBounds().height),
79                         Dimension(GetClientAreaBounds().width - 100, 250), ICON_LIST_VIEW_STYLE_MARK, ICON_LIST_VIEW_SCROLL_DIRECTION_VERTICAL);
80         __pIconListView->SetItemProvider(*this);
81         __pIconListView->AddIconListViewItemEventListener(*this);
82     AddControl(*__pIconListView);
83
84     return true;
85 }
86
87
88 void
89 FeatureManagerForm::OnActionPerformed (const Tizen::Ui::Control &source, int actionId)
90 {
91     Tizen::Ui::Controls::Frame *pFrame = Tizen::App::Application::GetInstance()->GetAppFrame()->GetFrame();
92     MainForm* pForm = static_cast<MainForm*>(pFrame->GetControl(L"IDF_FORM", true));
93
94     SceneManager* pSceneManager = SceneManager::GetInstance();
95     AppAssert(pSceneManager);
96
97     switch(actionId)
98     {
99     case ID_FOOTER_ITEM_DELETE:
100     {
101         for (int i = this->GetItemCount(); i >= 0; i--)
102                 {
103                         if (__pIconListView->IsItemChecked(i))
104                         {
105                                 AppLog("MainForm:: deleting item %i", i);
106                                 pForm->GetFeatureManager()->DeleteFeature(i);
107                         }
108                 }
109
110         pSceneManager->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_ZOOM_OUT));
111     }
112     break;
113     default:
114         break;
115     }
116 }
117
118 void
119 FeatureManagerForm::OnFormBackRequested(Tizen::Ui::Controls::Form&  source)
120 {
121     SceneManager* pSceneManager = SceneManager::GetInstance();
122     AppAssert(pSceneManager);
123     pSceneManager->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_ZOOM_OUT));
124 }
125
126
127 int
128 FeatureManagerForm::GetItemCount(void)
129 {
130     Tizen::Ui::Controls::Frame *pFrame = Tizen::App::Application::GetInstance()->GetAppFrame()->GetFrame();
131     MainForm* pForm = static_cast<MainForm*>(pFrame->GetControl(L"IDF_FORM", true));
132
133     if (pForm)
134     {
135         if (pForm->GetFeatureManager())
136         {
137             return pForm->GetFeatureManager()->GetTotalNumberOfFeatures();
138         }
139     }
140
141     return 0;
142 }
143
144 bool
145 FeatureManagerForm::DeleteItem(int index, Tizen::Ui::Controls::IconListViewItem* pItem)
146 {
147     delete pItem;
148     pItem = null;
149     return true;
150 }
151
152 Tizen::Ui::Controls::IconListViewItem*
153 FeatureManagerForm::CreateItem(int index)
154 {
155         IconListViewItem* pIconListViewItem = new IconListViewItem();
156     Tizen::Ui::Controls::Frame *pFrame = Tizen::App::Application::GetInstance()->GetAppFrame()->GetFrame();
157     MainForm* pForm = static_cast<MainForm*>(pFrame->GetControl(L"IDF_FORM", true));
158     if (pForm->GetFeatureManager())
159     {
160         const Tizen::Uix::Vision::ImageFeatureInfo*  pFeature = pForm->GetFeatureManager()->GetImageFeatureInfo(index);
161                 if(pFeature == null)
162                 {
163                         return pIconListViewItem;
164                 }
165
166         ByteBuffer* pByteBuffer = null;
167         pByteBuffer = pFeature->GetThumbnailN();
168         if (pByteBuffer == null)
169         {
170             return null;
171         }
172         int __thumbnailWidth = pFeature->GetThumbnailWidth();
173         int __thumbnailHeight = pFeature->GetThumbnailHeight();
174         unsigned char*     ptr  = const_cast<unsigned char*>(pByteBuffer->GetPointer());
175         unsigned char*     end  = ptr + __thumbnailWidth * __thumbnailHeight;
176
177         ByteBuffer imageBuffer;
178         imageBuffer.Construct(__thumbnailWidth * __thumbnailHeight * 4);
179         unsigned char*     iptr  = const_cast<unsigned char*>(imageBuffer.GetPointer());
180
181         for (;ptr < end; ++ptr)
182         {
183             *(iptr++) = *ptr;
184             *(iptr++) = *ptr;
185             *(iptr++) = *ptr;
186             *(iptr++) = 255;
187         }
188
189         Bitmap* pBitmap = new Bitmap();
190         pBitmap->Construct(imageBuffer, Dimension(__thumbnailWidth, __thumbnailHeight), BITMAP_PIXEL_FORMAT_R8G8B8A8);
191
192         //before this we should get name of the item
193         Tizen::Base::String name;
194         name.Format(80, L"%i: %ls", index, pFeature->GetDescription().GetPointer());
195
196         pIconListViewItem->Construct(*pBitmap, &name);
197
198         if(pByteBuffer)
199         {
200             delete pByteBuffer;
201         }
202     }
203
204     return pIconListViewItem;
205 }
206
207 void
208 FeatureManagerForm::OnIconListViewItemStateChanged(Tizen::Ui::Controls::IconListView &view, int index, Tizen::Ui::Controls::IconListViewItemStatus status)
209 {
210 }
211
212 void
213 FeatureManagerForm::OnSceneActivatedN(const Tizen::Ui::Scenes::SceneId& previousSceneId,
214         const Tizen::Ui::Scenes::SceneId& currentSceneId, Tizen::Base::Collection::IList* pArgs)
215 {
216     __pIconListView->ScrollToItem(0);
217     __pIconListView->UpdateList();
218
219     if(pArgs != null)
220     {
221         pArgs->RemoveAll();
222         delete pArgs;
223     }
224 }
225
226 void
227 FeatureManagerForm::OnSceneDeactivated(const Tizen::Ui::Scenes::SceneId& currentSceneId,
228         const Tizen::Ui::Scenes::SceneId& nextSceneId)
229 {
230 }