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