resource id addition
[apps/native/sample/ImageFeatureManager.git] / project / src / FeatureManagerForm.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 "FeatureManagerForm.h"
20 #include "MainForm.h"
21
22 using namespace Tizen::App;
23 using namespace Tizen::Ui;
24 using namespace Tizen::Ui::Controls;
25 using namespace Tizen::Ui::Scenes;
26 using namespace Tizen::Base;
27 using namespace Tizen::Graphics;
28
29 FeatureManagerForm::FeatureManagerForm(void)
30     : __pIconListView(null)
31     , __pPopup(0)
32 {
33 }
34
35 FeatureManagerForm::~FeatureManagerForm(void)
36 {
37 }
38
39 bool
40 FeatureManagerForm::Initialize(void)
41 {
42     Construct(FORM_STYLE_NORMAL| FORM_STYLE_HEADER| FORM_STYLE_INDICATOR | FORM_STYLE_FOOTER);
43
44     SetFormBackEventListener(this);
45
46     return true;
47 }
48
49 result
50 FeatureManagerForm::OnInitializing(void)
51 {
52     Header* pHeader = GetHeader();
53     pHeader->SetTitleText(L"Delete Features");
54     if (!CreateMainList())
55     {
56         return E_FAILURE;
57     }
58
59     Footer* pFooter = GetFooter();
60         pFooter->SetStyle(FOOTER_STYLE_BUTTON_TEXT);
61         pFooter->SetBackButton();
62
63         FooterItem pFooterItemDelete;
64         pFooterItemDelete.Construct(ID_FOOTER_ITEM_DELETE);
65         pFooterItemDelete.SetText(L"Delete");
66         pFooter->AddItem(pFooterItemDelete);
67         pFooter->SetItemEnabled(0, false);
68         pFooter->AddActionEventListener(*this);
69
70     return E_SUCCESS;
71 }
72
73 result
74 FeatureManagerForm::OnTerminating(void)
75 {
76     result r = E_SUCCESS;
77     return r;
78 }
79
80 bool
81 FeatureManagerForm::CreateMainList(void)
82 {
83         __pIconListView = new IconListView();
84         __pIconListView->Construct(Rectangle(0, 0, GetClientAreaBounds().width, GetClientAreaBounds().height),
85                         Dimension(GetClientAreaBounds().width - 100, 250), ICON_LIST_VIEW_STYLE_MARK, ICON_LIST_VIEW_SCROLL_DIRECTION_VERTICAL);
86         __pIconListView->SetItemProvider(*this);
87         __pIconListView->AddIconListViewItemEventListener(*this);
88     AddControl(*__pIconListView);
89
90     return true;
91 }
92
93
94 void
95 FeatureManagerForm::OnActionPerformed (const Tizen::Ui::Control &source, int actionId)
96 {
97     Tizen::Ui::Controls::Frame *pFrame = Tizen::App::Application::GetInstance()->GetAppFrame()->GetFrame();
98     MainForm* pForm = static_cast<MainForm*>(pFrame->GetControl(IDF_FORM, true));
99
100     SceneManager* pSceneManager = SceneManager::GetInstance();
101     AppAssert(pSceneManager);
102
103     switch (actionId)
104     {
105     case ID_FOOTER_ITEM_DELETE:
106     {
107         for (int i = this->GetItemCount() - 1; i >= 0; i--)
108                 {
109                         if (__pIconListView->IsItemChecked(i))
110                         {
111                                 pForm->GetFeatureManager()->DeleteFeature(i);
112                         }
113                 }
114
115         pSceneManager->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_ZOOM_OUT));
116     }
117     break;
118     default:
119         break;
120     }
121 }
122
123 void
124 FeatureManagerForm::OnFormBackRequested(Tizen::Ui::Controls::Form&  source)
125 {
126     SceneManager* pSceneManager = SceneManager::GetInstance();
127     AppAssert(pSceneManager);
128     pSceneManager->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_ZOOM_OUT));
129 }
130
131
132 int
133 FeatureManagerForm::GetItemCount(void)
134 {
135     Tizen::Ui::Controls::Frame *pFrame = Tizen::App::Application::GetInstance()->GetAppFrame()->GetFrame();
136     MainForm* pForm = static_cast<MainForm*>(pFrame->GetControl(IDF_FORM, true));
137
138     if (pForm)
139     {
140         if (pForm->GetFeatureManager())
141         {
142             return pForm->GetFeatureManager()->GetTotalNumberOfFeatures();
143         }
144     }
145
146     return 0;
147 }
148
149 bool
150 FeatureManagerForm::DeleteItem(int index, Tizen::Ui::Controls::IconListViewItem* pItem)
151 {
152     delete pItem;
153     pItem = null;
154     return true;
155 }
156
157 Tizen::Ui::Controls::IconListViewItem*
158 FeatureManagerForm::CreateItem(int index)
159 {
160         IconListViewItem* pIconListViewItem = new IconListViewItem();
161     Tizen::Ui::Controls::Frame *pFrame = Tizen::App::Application::GetInstance()->GetAppFrame()->GetFrame();
162     MainForm* pForm = static_cast<MainForm*>(pFrame->GetControl(IDF_FORM, true));
163     if (pForm->GetFeatureManager())
164     {
165         const Tizen::Uix::Vision::ImageFeatureInfo*  pFeature = pForm->GetFeatureManager()->GetImageFeatureInfo(index);
166                 if (pFeature == null)
167                 {
168                         return pIconListViewItem;
169                 }
170
171         ByteBuffer* pByteBuffer = null;
172         pByteBuffer = pFeature->GetThumbnailN();
173         if (pByteBuffer == null)
174         {
175             return null;
176         }
177         int __thumbnailWidth = pFeature->GetThumbnailWidth();
178         int __thumbnailHeight = pFeature->GetThumbnailHeight();
179         unsigned char*     ptr  = const_cast<unsigned char*>(pByteBuffer->GetPointer());
180         unsigned char*     end  = ptr + __thumbnailWidth * __thumbnailHeight;
181
182         ByteBuffer imageBuffer;
183         imageBuffer.Construct(__thumbnailWidth * __thumbnailHeight * 4);
184         unsigned char*     iptr  = const_cast<unsigned char*>(imageBuffer.GetPointer());
185
186         for (;ptr < end; ++ptr)
187         {
188             *(iptr++) = *ptr;
189             *(iptr++) = *ptr;
190             *(iptr++) = *ptr;
191             *(iptr++) = 255;
192         }
193
194         Bitmap* pBitmap = new Bitmap();
195         pBitmap->Construct(imageBuffer, Dimension(__thumbnailWidth, __thumbnailHeight), BITMAP_PIXEL_FORMAT_R8G8B8A8);
196
197         //before this we should get name of the item
198         Tizen::Base::String name;
199         name.Format(80, L"%i: %ls", index, pFeature->GetDescription().GetPointer());
200
201         pIconListViewItem->Construct(*pBitmap, &name);
202
203         if (pByteBuffer)
204         {
205             delete pByteBuffer;
206         }
207     }
208
209     return pIconListViewItem;
210 }
211
212 void
213 FeatureManagerForm::OnIconListViewItemStateChanged(Tizen::Ui::Controls::IconListView &view, int index, Tizen::Ui::Controls::IconListViewItemStatus status)
214 {
215         Footer* pFooter = GetFooter();
216         switch (status)
217         {
218         case ICON_LIST_VIEW_ITEM_CHECKED:
219         {
220                 pFooter->SetItemEnabled(0, true);
221                 pFooter->Draw();
222         }
223                 break;
224         case ICON_LIST_VIEW_ITEM_UNCHECKED:
225         {
226         pFooter->SetItemEnabled(0, false);
227         for (int i = this->GetItemCount() - 1; i >= 0; i--)
228                 {
229                         if (__pIconListView->IsItemChecked(i) && i != index)
230                         {
231                                 pFooter->SetItemEnabled(0, true);
232                         }
233                 }
234                 pFooter->Draw();
235         }
236                 break;
237         default:
238                 break;
239         }
240
241
242 }
243
244 void
245 FeatureManagerForm::OnSceneActivatedN(const Tizen::Ui::Scenes::SceneId& previousSceneId,
246         const Tizen::Ui::Scenes::SceneId& currentSceneId, Tizen::Base::Collection::IList* pArgs)
247 {
248     __pIconListView->ScrollToItem(0);
249     __pIconListView->UpdateList();
250
251     if (pArgs != null)
252     {
253         pArgs->RemoveAll();
254         delete pArgs;
255     }
256 }
257
258 void
259 FeatureManagerForm::OnSceneDeactivated(const Tizen::Ui::Scenes::SceneId& currentSceneId,
260         const Tizen::Ui::Scenes::SceneId& nextSceneId)
261 {
262 }