3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.tizenopensource.org/license
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.
20 #include "BrowserForm.h"
21 #include "ImageViewerForm.h"
22 #include "ImageConverterForm.h"
24 #include "UnitTestFactory.h"
25 #include "AppConfig.h"
26 #include "PixelFormatMapper.h"
28 #define SECTION_NAME L"ImageViewer"
31 #define IV_FORM_LISTVIEW_H 250
32 #define IV_FORM_POPUP_W 400
34 using namespace Osp::Base;
35 using namespace Osp::Ui;
36 using namespace Osp::Ui::Controls;
37 using namespace Osp::Graphics;
38 using namespace Osp::Io;
39 using namespace Osp::App;
41 MULTI_FORM_REGISTER(ImageViewerForm, L"ImageViewerForm");
42 DECLARE_TC(L"Image", L"1.Image Viewer", ImageViewerForm::TestFunc, 0);
45 ImageViewerForm::TestFunc(void* pParam)
47 return MultiForm::ActivateForm(L"ImageViewerForm", pParam, MultiForm::GetCurrentForm(), null);
50 ImageViewerForm::ImageViewerForm(void)
53 __pContextMenu = null;
54 __pPopupPixFmt = null;
55 __pListViewPixFmt = null;
56 __pbtnPixFmtOK = null;
57 __currentListViewId = -1;
58 __pSettingItemArrayList = null;
67 __pixelFmt = BITMAP_PIXEL_FORMAT_RGB565;
71 ImageViewerForm::~ImageViewerForm(void)
73 if (__pBitmap != null)
79 if (__pContextMenu != null)
81 __pContextMenu->RemoveActionEventListener(*this);
82 delete __pContextMenu;
83 __pContextMenu = null;
86 if (__pPopupPixFmt != null)
88 delete __pPopupPixFmt;
89 __pPopupPixFmt = null;
92 if (__pSettingItemArrayList != null)
94 delete[] __pSettingItemArrayList;
95 __pSettingItemArrayList = null;
101 ImageViewerForm::Initialize()
103 Construct(L"IDF_IMAGE_VIEWER");
110 ImageViewerForm::OnInitializing(void)
112 result r = E_SUCCESS;
116 r = __img.Construct();
119 AppLogException("[%s] Failed to construct image instance.", GetErrorMessage(r));
123 SetHeaderText("Image Viewer");
124 SetFooterStyle(FOOTER_STYLE_BUTTON_TEXT, ID_BACK, this);
125 r = AddFooterItem(L"Settings", ID_SETTING);
128 AppLogException("Failed to add \"Settings\" button.");
132 __pixelFmt = BITMAP_PIXEL_FORMAT_RGB565;
134 AddFooterItem(L"Prev", ID_PREV);
135 AddFooterItem(L"Next", ID_NEXT);
136 SetStepInfo(0, 0, PREV_INDEX, NEXT_INDEX);
138 __pBmpCtrl = BitmapControl::ConstructN(GetControl(L"IDC_BUTTON_IMAGE"));
139 if (__pBmpCtrl == null)
142 AppLogException("BitmapControl::ConstructN failed:%s", GetErrorMessage(r));
145 AddControl(*__pBmpCtrl);
147 __pLog = LogControl::ConstructN(GetControl(L"IDC_EDITAREA_INFO"));
151 AppLogException("LogControl::ConstructN failed:%s", GetErrorMessage(GetLastResult()));
155 __pLog->SetEnabled(false);
157 FitControlBounds(*__pLog, __pBmpCtrl, GetFooter(), 2, 2, true);
159 r = ConstructContextMenu();
162 AppLogException("Context menu construction failed: %s", GetErrorMessage(r));
165 // Read files from directory. Arrange image files alphabetically.
166 dirName = __pConfig->GetString(SECTION_NAME, "loadPath");
167 fullDirName = App::GetInstance()->GetAppRootPath();
168 fullDirName.Append("data");
169 fullDirName.Append(dirName);
171 r = GetFilesList(fullDirName);
174 AppLogException("[%s] Could not get image files present in given directory %ls", GetErrorMessage(r), fullDirName.GetPointer());
182 ImageViewerForm::GetItemCount(void)
184 AppLog("Enter here ");
185 switch (__currentListViewId)
187 case ID_BTN_PIXELFORMAT:
189 if (__pSettingItemArrayList)
191 int count = __pSettingItemArrayList->GetCount();
192 AppLog("count = %d", count);
208 ImageViewerForm::AddListView(ListView** pListView, Popup* pPopup, int id)
210 result r = E_SUCCESS;
211 ListView* pCtrl = new (std::nothrow) ListView();
212 Rectangle popupRect = pPopup->GetClientAreaBounds();
218 //If listview height is lesser than popup area, use that height
219 if (popupRect.height > IV_FORM_LISTVIEW_H)
221 popupRect.height = IV_FORM_LISTVIEW_H;
223 r = pCtrl->Construct(Rectangle(0, 0, popupRect.width, popupRect.height), true, false);
226 AppLogException("ListView construct failed.");
230 __currentListViewId = id;
231 pCtrl->SetItemProvider(*this);
232 pCtrl->AddListViewItemEventListener(*this);
233 r = pPopup->AddControl(*pCtrl);
236 AppLogException("ListView AddControl failed.");
248 ImageViewerForm::OnListViewItemSwept(Osp::Ui::Controls::ListView& listView, int index, Osp::Ui::Controls::SweepDirection direction)
250 AppLog("Enter here");
253 ImageViewerForm::OnListViewContextItemStateChanged(Osp::Ui::Controls::ListView& listView, int index, int elementId, Osp::Ui::Controls::ListContextItemStatus state)
255 AppLog("Enter here");
258 Osp::Ui::Controls::ListItemBase*
259 ImageViewerForm::CreateItem(int index, int itemWidth)
261 AppLog("Enter here index = %d", index);
262 ListAnnexStyle style = LIST_ANNEX_STYLE_RADIO;
263 result r = E_SUCCESS;
264 CustomItem* pItem = new (std::nothrow) CustomItem();
270 r = pItem->Construct(Osp::Graphics::Dimension(itemWidth, LISTVIEW_ITEM_HEIGHT), style);
273 AppLogException("Failed to construct listview.");
276 switch (__currentListViewId)
278 case ID_BTN_PIXELFORMAT:
280 if (__pSettingItemArrayList)
282 SettingListData* pItemValue = (SettingListData*) __pSettingItemArrayList->GetAt(index);
285 r = pItem->AddElement(Rectangle(0, 0, itemWidth, LISTVIEW_ITEM_HEIGHT), pItemValue->elementId, *(pItemValue->pItemName), true);
288 AppLogException("Could not create listview.");
293 if (index == (__pSettingItemArrayList->GetCount() - 1))
295 __pSettingItemArrayList->RemoveAll(true);
296 delete __pSettingItemArrayList;
297 __pSettingItemArrayList = null;
304 AppLogException("default case matching = %d", __currentListViewId);
321 ImageViewerForm::DeleteItem(int index, Osp::Ui::Controls::ListItemBase* pItem, int itemWidth)
323 AppLog("Enter here");
330 ImageViewerForm::OnListViewItemStateChanged(Osp::Ui::Controls::ListView& source, int index, int elementId, Osp::Ui::Controls::ListItemStatus status)
332 AppLog("Enter here");
333 result r = E_SUCCESS;
334 if (&source == __pListViewPixFmt)
336 __pListViewPixFmt->Draw();
337 if (__pixFmtIndex == index)
339 r = __pListViewPixFmt->SetItemChecked(index, true);
342 AppLogException("Failed to check item in listview.");
347 r = __pListViewPixFmt->SetItemChecked(__pixFmtIndex, false);
350 AppLogException("SetItemChecked failed.");
353 __pixFmtIndex = index;
356 AppLog("ImageViewerForm PixelFormat = %d", __pixFmtIndex);
361 ImageViewerForm::ConstructContextMenu(void)
363 result r = E_SUCCESS;
365 if (__pContextMenu == null)
368 int x, y, width, height;
369 __pContextMenu = new (std::nothrow) ContextMenu();
371 if (__pContextMenu == null)
373 AppLogException("Failed to create \"Settings\" menu.");
374 return E_OUT_OF_MEMORY;
376 // Get reference coordinates of context menu from footer.
377 GetFooter()->GetBounds(x, y, width, height);
379 r = __pContextMenu->Construct(Point(0, y), CONTEXT_MENU_STYLE_LIST);
382 AppLogException("Failed to construct \"Settings\" menu.");
386 r = __pContextMenu->AddItem("PixelFormat", ID_MENU_PIXELFORMAT);
389 AppLogException("Failed to construct \"Settings\" menu.");
392 __pContextMenu->AddActionEventListener(*this);
398 ImageViewerForm::OnActivate(void* param)
400 result r = E_SUCCESS;
401 String* pFileName = null;
411 __pParam = (ImageViewerFormParam*) param;
412 pFileName = &__pParam->srcFile;
413 SetStepInfo(0, 0, PREV_INDEX, NEXT_INDEX);
417 count = fileList.GetCount();
418 pFileName = dynamic_cast< String* >(fileList.GetAt(0));
419 SetStepInfo(count, 0, PREV_INDEX, NEXT_INDEX);
422 AppLog("filename: %ls", pFileName->GetPointer());
423 r = Process(*pFileName);
427 AppLogException("Decode failed: %s", GetErrorMessage(r));
431 r = ConstructContextMenu();
434 AppLogException("Context menu construction failed: %s", GetErrorMessage(r));
442 ImageViewerForm::OnActionPerformed(const Osp::Ui::Control& source, int actionId)
444 result r = E_SUCCESS;
445 String* pFileName = null;
451 AppLog("Go back to Browser form\n");
453 RemoveControl(*__pBmpCtrl);
454 if (__pBitmap != null)
465 int index = PrevStep();
466 pFileName = dynamic_cast< String* >(fileList.GetAt(index));
467 AppLog("%d filename:%ls", index, pFileName->GetPointer());
468 r = Process(*pFileName);
475 int index = NextStep();
476 pFileName = dynamic_cast< String* >(fileList.GetAt(index));
477 AppLog("%d filename:%ls", index, pFileName->GetPointer());
478 r = Process(*pFileName);
485 __pContextMenu->SetShowState(true);
486 __pContextMenu->Show();
490 case ID_MENU_PIXELFORMAT:
496 case ID_BTN_PIXELFORMAT:
498 BitmapPixelFormat pixelformats[2] = {BITMAP_PIXEL_FORMAT_RGB565, BITMAP_PIXEL_FORMAT_ARGB8888};
499 HidePopUp(__pPopupPixFmt);
500 __pPopupPixFmt = null;
501 if (__pixelFmt != pixelformats[__pixFmtIndex])
503 __pixelFmt = pixelformats[__pixFmtIndex];
504 int index = CurrStep();
505 pFileName = dynamic_cast< String* >(fileList.GetAt(index));
506 AppLog("%d filename:%ls", index, pFileName->GetPointer());
507 r = Process(*pFileName);
519 ImageViewerForm::CreatePopUp(Popup** pPopupCtrl, String title, Dimension dimension, bool hasTitle)
521 result r = E_SUCCESS;
522 Popup* pCtrl = new (std::nothrow) Popup();
525 return E_OUT_OF_MEMORY;
528 r = pCtrl->Construct(hasTitle, dimension);
531 AppLogException("popup construct failed.");
537 r = pCtrl->SetTitleText(String(title));
540 AppLogException("popup SetTitleText failed.");
550 ImageViewerForm::DeletePopup(Popup* pPopup)
560 ImageViewerForm::AddButton(Button** pButtonCtrl, String text, int actionId, Popup* pPopup, Rectangle dimension)
562 result r = E_SUCCESS;
563 Button* pCtrl = new (std::nothrow) Button();
566 return E_OUT_OF_MEMORY;
569 r = pCtrl->Construct(dimension, text);
572 AppLogException("button construct failed.");
576 pCtrl->SetActionId(actionId);
577 pCtrl->AddActionEventListener(*this);
578 r = pPopup->AddControl(*pCtrl);
581 AppLogException("popup AddControl failed.");
585 *pButtonCtrl = pCtrl;
593 ImageViewerForm::ShowPopUp(Popup* pPopUp)
595 result r = E_SUCCESS;
600 r = pPopUp->SetShowState(true);
603 AppLogException("popup SetShowState failed.");
609 AppLogException("popup Show failed.");
620 ImageViewerForm::HidePopUp(Popup* pPopUp)
626 pPopUp->SetShowState(false);
632 ImageViewerForm::CreatePixFmtMenu()
634 result r = E_SUCCESS;
636 if (__pPopupPixFmt == null)
639 Dimension dim(400, 400);
640 Rectangle rect(120, 270, 120, 60);
641 r = CreatePopUp(&__pPopupPixFmt, L"Select Pixelformat", dim, true);
647 r = AddButton(&__pbtnPixFmtOK, L"OK", ID_BTN_PIXELFORMAT, __pPopupPixFmt, rect);
653 if (__pSettingItemArrayList != null)
655 delete[] __pSettingItemArrayList;
656 __pSettingItemArrayList = null;
659 __pSettingItemArrayList = new (std::nothrow) ArrayList;
661 String* pPixFmtStrValue = new (std::nothrow) String(L"RGB565");
662 SettingListData* pItemData = new (std::nothrow) SettingListData(pPixFmtStrValue, 0);
663 __pSettingItemArrayList->Add(*pItemData);
665 pPixFmtStrValue = new (std::nothrow) String(L"ARGB8888");
666 pItemData = new (std::nothrow) SettingListData(pPixFmtStrValue, 1);
667 __pSettingItemArrayList->Add(*pItemData);
669 r = AddListView(&__pListViewPixFmt, __pPopupPixFmt, ID_BTN_PIXELFORMAT);
676 ShowPopUp(__pPopupPixFmt);
678 __pListViewPixFmt->Draw();
679 __pListViewPixFmt->SetItemChecked(__pixFmtIndex, true);
683 SAFE_DELETE(__pPopupPixFmt);
684 AppLogException("%s", GetErrorMessage(r));
690 ImageViewerForm::Process(String filePath)
692 result r = E_SUCCESS;
698 AppLog("Reload: %ls", filePath.GetPointer());
699 ResetFileAtrributes();
700 __srcName = filePath;
702 if (__pBitmap != null)
708 r = Osp::Io::File::GetAttributes(filePath, attr);
711 AppLogException("file.GetAttributes failed : %s", GetErrorMessage(r));
715 __fileSize = attr.GetFileSize();
717 __pBitmap = __img.DecodeN(filePath, __pixelFmt);
719 if ((r = GetLastResult()) != E_SUCCESS)
721 AppLog("ImageViewerForm::Process __img.DecodeN FAILED");
722 filePath = App::GetInstance()->GetAppRootPath() + L"data/Image/decoding_failed.png";
723 __pBitmap = __img.DecodeN(filePath, BITMAP_PIXEL_FORMAT_ARGB8888);
726 __srcWidth = __pBitmap->GetWidth();
727 __srcHeight = __pBitmap->GetHeight();
728 __imgFormat = __img.GetImageFormat(filePath);
729 __hasAlpha = __img.HasAlphaChannels(filePath);
736 ImageViewerForm::ResetFileAtrributes(void)
741 __imgFormat = IMG_FORMAT_NONE;
747 ImageViewerForm::IsImageFile(String* pFileName)
749 ImageFormat format = IMG_FORMAT_NONE;
751 format = __img.GetImageFormat(*pFileName);
752 if (format != IMG_FORMAT_NONE)
760 ImageViewerForm::GetFilesList(String fullDirName)
762 Directory* pDir = null;
763 DirEnumerator* pDirEnum = null;
764 StringComparer strComparer;
765 result r = E_SUCCESS;
767 fileList.Construct();
769 pDir = new (std::nothrow) Directory();
771 r = pDir->Construct(fullDirName);
774 AppLogException("[%s] Failed to construct directory %S", GetErrorMessage(r), fullDirName.GetPointer());
779 pDirEnum = pDir->ReadN();
780 if (pDirEnum == null)
782 AppLogException("[%s] Failed to read entries from directory %S", GetErrorMessage(GetLastResult()), fullDirName.GetPointer());
787 while (pDirEnum->MoveNext() == E_SUCCESS)
789 DirEntry dirEntry = pDirEnum->GetCurrentDirEntry();
790 if ((dirEntry.IsDirectory() == false) && (dirEntry.IsHidden() == false))
792 String* fullFileName = new (std::nothrow) String;
793 fullFileName->Append(fullDirName);
794 fullFileName->Append('/');
795 String fileName(dirEntry.GetName());
796 fullFileName->Append(fileName);
798 r = IsImageFile(fullFileName);
801 fileList.Add(*fullFileName);
811 fileList.Sort(strComparer);
816 if (fileList.GetCount() >= 0)
827 ImageViewerForm::UpdateLog(result r)
830 __pLog->Log(__srcName);
831 __pLog->Log("\nFileSize:%dKB\nDimension:%dx%d\nFormat:%s\nPixelFormat:%s\nAlphChannel:%s\nResult:%s",
832 __fileSize / 1000, __srcWidth, __srcHeight,
833 PixelFormatMapper::GetImageFormatString(__imgFormat),
834 __pixelFmt == BITMAP_PIXEL_FORMAT_RGB565 ? "RGB565" : "ARGB8888",
835 __hasAlpha ? "true" : "false",
839 __pBmpCtrl->SetBitmap(*__pBitmap);
844 ImageViewerForm::OnFormBackRequested(Osp::Ui::Controls::Form& source)
846 AppLog("ImageViewerForm::OnFormBackRequested called");
847 if (__pBitmap != null)
852 if (__pContextMenu != null)
854 __pContextMenu->RemoveActionEventListener(*this);
855 delete __pContextMenu;
856 __pContextMenu = null;