2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 // Licensed under the Flora License, Version 1.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://floralicense.org/license/
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an AS IS BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
18 * @file IvImageCrop.cpp
19 * @brief This is the implementation file for Image Crop.
26 #include "IvImageCropForm.h"
27 #include "IvImageViewerPresentationModel.h"
28 #include "IvResourceManager.h"
31 using namespace Tizen::App;
32 using namespace Tizen::Base;
33 using namespace Tizen::Base::Collection;
34 using namespace Tizen::Content;
35 using namespace Tizen::Graphics;
36 using namespace Tizen::Io;
37 using namespace Tizen::Media;
38 using namespace Tizen::System;
39 using namespace Tizen::Social;
40 using namespace Tizen::Ui::Controls;
41 using namespace Tizen::Ui::Scenes;
43 static const unsigned int CROP_BOX_RECTANGLE_COLOR = Color32<199, 110, 6>::Value;
44 static const int CROP_BOX_LINE_WIDTH = 5;
46 ImageCropForm::ImageCropForm(void)
49 , __isOrientationChanged(false)
51 , __pCurrentBitmap(null)
52 , __pRectangleBitmap(null)
53 , __pointLocation(OUTSIDE_CROPBOX)
54 , __pPresentationModel(null)
58 ImageCropForm::~ImageCropForm(void)
60 if (__pCurrentBitmap != null)
62 delete __pCurrentBitmap;
64 if (__pRectangleBitmap != null)
66 delete __pRectangleBitmap;
68 if (__pCanvas != null)
75 ImageCropForm::Initialize(void)
77 Form::Construct(IDL_FORM_IMAGE_CROP);
78 Form::SetFormStyle(FORM_STYLE_NORMAL | FORM_STYLE_INDICATOR);
80 __pPresentationModel = ImageViewerPresentationModel::GetInstance();
86 ImageCropForm::OnInitializing(void)
90 AppResource* pAppResource = null;
91 Bitmap* pSaveButtonBitmap = null;
92 Bitmap* pCancelButtonBitmap = null;
93 Bitmap* pCWRotationButtonBitmap = null;
94 Bitmap* pCCWRotationButtonBitmap = null;
96 Button* pSaveButton = null;
97 Button* pCancelButton = null;
98 pAppResource = UiApp::App::GetInstance()->GetAppResource();
99 if (pAppResource != null)
101 pSaveButtonBitmap = pAppResource->GetBitmapN(IDB_IMAGE_CROP_FORM_SAVE_ICON);
102 pCancelButtonBitmap = pAppResource->GetBitmapN(IDB_IMAGE_CROP_FORM_CANCEL_ICON);
103 pCWRotationButtonBitmap = pAppResource->GetBitmapN(IDB_IMAGE_CROP_FORM_CW_ROTATION_ICON);
104 pCCWRotationButtonBitmap = pAppResource->GetBitmapN(IDB_IMAGE_CROP_FORM_CCW_ROTATION_ICON);
105 __pRectangleBitmap = pAppResource->GetBitmapN(IDB_IMAGE_CROP_RECTANGLE);
108 pPanel = static_cast<Panel*>(GetControl(L"IDC_FOOTER_PANEL"));
111 pSaveButton = static_cast<Button*>(pPanel->GetControl(L"IDC_SAVE_BUTTON"));
112 if (pSaveButton != null && pSaveButtonBitmap != null)
114 Point startPoint((pSaveButton->GetWidth() - pSaveButtonBitmap->GetWidth()) / 2,
115 (pSaveButton->GetHeight() - pSaveButtonBitmap->GetHeight()) / 2);
116 pSaveButton->SetNormalBitmap(startPoint, *pSaveButtonBitmap);
117 pSaveButton->AddActionEventListener(*this);
118 pSaveButton->SetActionId(IDA_BUTTON_SAVE);
119 delete pSaveButtonBitmap;
122 pCancelButton = static_cast<Button*>(pPanel->GetControl(L"IDC_CANCEL_BUTTON"));
123 if (pCancelButton != null && pCancelButtonBitmap != null)
125 Point startPoint((pCancelButton->GetWidth() - pCancelButtonBitmap->GetWidth()) / 2,
126 (pCancelButton->GetHeight() - pCancelButtonBitmap->GetHeight()) / 2);
127 pCancelButton->SetNormalBitmap(startPoint, *pCancelButtonBitmap);
128 pCancelButton->AddActionEventListener(*this);
129 pCancelButton->SetActionId(IDA_BUTTON_CANCEL);
130 delete pCancelButtonBitmap;
132 SetControlAlwaysOnTop(*pPanel, true);
133 Label* pLabel = static_cast<Label*>(pPanel->GetControl(L"IDC_PANEL_LABEL"));
136 SetControlAlwaysAtBottom(*pLabel, true);
139 AddTouchEventListener(*this);
140 AddOrientationEventListener(*this);
141 __pPresentationModel->AddFileUpdateListener(this);
143 delete pCWRotationButtonBitmap;
144 delete pCCWRotationButtonBitmap;
149 ImageCropForm::OnSceneActivatedN(const SceneId& previousSceneId, const SceneId& currentSceneId, IList* pArgs)
151 AppLogDebug("ENTER pArgs(%x)", pArgs);
154 IEnumerator* pEnum = pArgs->GetEnumeratorN();
155 if (pEnum->MoveNext() != E_SUCCESS)
159 AppLogDebug("EXIT 1(%s)", GetErrorMessage(GetLastResult()));
163 __sourceFilePath = *(static_cast<String*>(pEnum->GetCurrent()));
164 if (__sourceFilePath.IsEmpty() == false)
166 result r = __imageBuffer.Construct(__sourceFilePath.GetPointer());
169 __imageWidth = __imageBuffer.GetWidth();
170 __imageHeight = __imageBuffer.GetHeight();
171 __pCurrentBitmap = __imageBuffer.GetBitmapN(BITMAP_PIXEL_FORMAT_RGB565, BUFFER_SCALING_AUTO);
174 __imageFormat = img.GetImageFormat(__sourceFilePath);
176 if (pEnum->MoveNext() != E_SUCCESS)
180 AppLogDebug("EXIT 2(%s)", GetErrorMessage(GetLastResult()));
184 __cropMode = *(static_cast<String*>(pEnum->GetCurrent()));
185 if (__cropMode.IsEmpty() == false)
187 __cropMode.ToLowerCase();
188 if (__cropMode == "auto")
195 else if (__cropMode == "fit-to-screen")
197 if (pEnum->MoveNext() != E_SUCCESS)
201 AppLogDebug("EXIT 3(%s)", GetErrorMessage(GetLastResult()));
205 Integer *pStatus = static_cast<Integer*>(pEnum->GetCurrent());
208 __statusValue = pStatus->ToInt();
210 if (__statusValue == SET_AT_TYPE_CALLER_IMAGE)
212 if (pEnum->MoveNext() != E_SUCCESS)
216 AppLogDebug("EXIT 4(%s)", GetErrorMessage(GetLastResult()));
221 LongLong* pContentId = static_cast<LongLong*>(pEnum->GetCurrent());
222 if (pContentId != null)
224 __contentId = pContentId->ToLongLong();
234 // if (pArgs != null)
236 // __sourceFilePath = *(static_cast<String*>(pArgs->GetAt(0)));
237 // __cropMode = *(static_cast<String*>(pArgs->GetAt(1)));
242 // __sourceFilePath = __pPresentationModel->GetFilePathAt(0);
244 /* if (__sourceFilePath.IsEmpty() == false && __cropMode.IsEmpty() == false)
246 __cropMode.ToLowerCase();
247 result r = __imageBuffer.Construct(__sourceFilePath.GetPointer());
250 __imageWidth = __imageBuffer.GetWidth();
251 __imageHeight = __imageBuffer.GetHeight();
252 __pCurrentBitmap = __imageBuffer.GetBitmapN(BITMAP_PIXEL_FORMAT_RGB565, BUFFER_SCALING_AUTO);
255 __imageFormat = img.GetImageFormat(__sourceFilePath);
258 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
262 ImageCropForm::OnSceneDeactivated(const SceneId& currentSceneId, const SceneId& nextSceneId)
264 AppLogDebug("ENTER");
265 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
269 ImageCropForm::OnDraw(void)
271 result r = E_SUCCESS;
272 AppLogDebug("ENTER");
273 if (__pCanvas != null)
275 r = __pCanvas->SetLineWidth(CROP_BOX_LINE_WIDTH);
276 r = __pCanvas->DrawBitmap(Rectangle(__imageBox.x, __imageBox.y, __imageBox.width, __imageBox.height), *__pCurrentBitmap);
277 r = __pCanvas->DrawRectangle(Rectangle(__cropBox.x, __cropBox.y, __cropBox.width, __cropBox.height));
278 r = __pCanvas->DrawBitmap(Rectangle((__cropBox.x + __cropBox.width / 2) - (CROP_RECTANGLE_HEIGHT / 2), __cropBox.y - (CROP_RECTANGLE_HEIGHT / 2), CROP_RECTANGLE_HEIGHT, CROP_RECTANGLE_HEIGHT), *__pRectangleBitmap); // Top Rectangle
279 r = __pCanvas->DrawBitmap(Rectangle((__cropBox.x + __cropBox.width / 2) - (CROP_RECTANGLE_HEIGHT / 2), (__cropBox.y + __cropBox.height - (CROP_RECTANGLE_HEIGHT / 2)), CROP_RECTANGLE_HEIGHT, CROP_RECTANGLE_HEIGHT), *__pRectangleBitmap); // Bottom Rectangle
280 r = __pCanvas->DrawBitmap(Rectangle(__cropBox.x - (CROP_RECTANGLE_HEIGHT / 2), (__cropBox.y + __cropBox.height / 2) - (CROP_RECTANGLE_HEIGHT / 2), CROP_RECTANGLE_HEIGHT, CROP_RECTANGLE_HEIGHT), *__pRectangleBitmap); //Left Rectangle
281 r = __pCanvas->DrawBitmap(Rectangle(__cropBox.x + __cropBox.width - (CROP_RECTANGLE_HEIGHT / 2), (__cropBox.y + __cropBox.height / 2) - (CROP_RECTANGLE_HEIGHT / 2), CROP_RECTANGLE_HEIGHT, CROP_RECTANGLE_HEIGHT), *__pRectangleBitmap); //Right Rectangle
282 r = __pCanvas->DrawBitmap(Rectangle(__cropBox.x - (CROP_RECTANGLE_HEIGHT / 2), __cropBox.y - (CROP_RECTANGLE_HEIGHT / 2), CROP_RECTANGLE_HEIGHT, CROP_RECTANGLE_HEIGHT), *__pRectangleBitmap); // Top Left Rectangle
283 r = __pCanvas->DrawBitmap(Rectangle(__cropBox.x + __cropBox.width - (CROP_RECTANGLE_HEIGHT / 2), __cropBox.y - (CROP_RECTANGLE_HEIGHT / 2), CROP_RECTANGLE_HEIGHT, CROP_RECTANGLE_HEIGHT), *__pRectangleBitmap); // Top Right Rectangle
284 r = __pCanvas->DrawBitmap(Rectangle(__cropBox.x + __cropBox.width - (CROP_RECTANGLE_HEIGHT / 2), (__cropBox.y + __cropBox.height - (CROP_RECTANGLE_HEIGHT / 2)), CROP_RECTANGLE_HEIGHT, CROP_RECTANGLE_HEIGHT), *__pRectangleBitmap); // Bottom Right Rectangle
285 r = __pCanvas->DrawBitmap(Rectangle(__cropBox.x - (CROP_RECTANGLE_HEIGHT / 2), (__cropBox.y + __cropBox.height - (CROP_RECTANGLE_HEIGHT / 2)), CROP_RECTANGLE_HEIGHT, CROP_RECTANGLE_HEIGHT), *__pRectangleBitmap); // Bottom Left Rectangle
292 ImageCropForm::OnActionPerformed(const Tizen::Ui::Control& source, int actionId)
294 SceneManager* pSceneManager = SceneManager::GetInstance();
297 case IDA_BUTTON_CANCEL:
298 pSceneManager->GoBackward(BackwardSceneTransition());
300 case IDA_BUTTON_SAVE:
302 if (__cropMode == "auto")
304 ImageBuffer* pCropBuffer = null;
308 String stringToInsert = L"_";
309 String delimiter = L".";
310 if (__sourceFilePath.IsEmpty() == false)
312 __sourceFilePath.Reverse();
313 __sourceFilePath.IndexOf(delimiter, 0, index);
314 __sourceFilePath.Reverse();
315 __sourceFilePath.Insert(stringToInsert, __sourceFilePath.GetLength() - index - 1);
316 destFilePath.Append(__sourceFilePath);
317 destFilePath.Insert(count, destFilePath.GetLength() - index - 1);
318 while (File::IsFileExist(destFilePath) == true)
321 destFilePath.Clear();
322 destFilePath.Append(__sourceFilePath);
323 destFilePath.Insert(count, destFilePath.GetLength() - index - 1);
325 pCropBuffer = __imageBuffer.CropN(((__cropBox.x - __imageBox.x) * __imageWidth) / __imageBox.width, ((__cropBox.y - __imageBox.y) * __imageHeight) / __imageBox.height,
326 (__cropBox.width * __imageWidth) / __imageBox.width, (__cropBox.height * __imageHeight) / __imageBox.height);
327 pCropBuffer->EncodeToFile(destFilePath, __imageFormat, true, 100);
328 ContentManager::ScanFile(destFilePath);
331 ArrayList* pList = new (std::nothrow) ArrayList(SingleObjectDeleter);
332 pList->Add(new (std::nothrow) String(destFilePath));
333 pSceneManager->GoBackward(BackwardSceneTransition(), pList);
335 else if(__cropMode == "fit-to-screen")
337 ImageBuffer* pCropBuffer = null;
338 result r = E_SUCCESS;
339 pCropBuffer = __imageBuffer.CropN(((__cropBox.x - __imageBox.x) * __imageWidth) / __imageBox.width, ((__cropBox.y - __imageBox.y) * __imageHeight) / __imageBox.height,
340 (__cropBox.width * __imageWidth) / __imageBox.width, (__cropBox.height * __imageHeight) / __imageBox.height);
341 if (__statusValue == SET_AT_TYPE_HOME_SCREEN_WALLPAPER)
343 String destPath = App::GetInstance()->GetAppRootPath() + TEMP_FILE_PATH_HOME_SCREEN_WALLPAPER;
344 r = pCropBuffer->EncodeToFile(destPath, __imageFormat, true, 100);
347 r = SettingInfo::SetValue(SETTING_VALUE_HOME_SCREEN_WALLPAPER, destPath);
349 else if (r == E_OVERFLOW)
351 MessageBox messageBox;
352 messageBox.Construct(L"", L"File size is too big", MSGBOX_STYLE_NONE, 3000);
354 messageBox.ShowAndWait(modalResult);
357 else if (__statusValue == SET_AT_TYPE_LOCK_SCREEN_WALLPAPER)
359 String destPath = App::GetInstance()->GetAppRootPath() + TEMP_FILE_PATH_LOCK_SCREEN_WALLPAPER;
360 r = pCropBuffer->EncodeToFile(destPath, __imageFormat, true, 100);
363 r = SettingInfo::SetValue(SETTING_VALUE_LOCK_SCREEN_WALLPAPER, destPath);
365 else if (r == E_OVERFLOW)
367 MessageBox messageBox;
368 messageBox.Construct(L"", L"File size is too big", MSGBOX_STYLE_NONE, 3000);
370 messageBox.ShowAndWait(modalResult);
373 else if (__statusValue == SET_AT_TYPE_HOME_AND_LOCK_SCREEN_WALLPAPER)
375 String destPath = App::GetInstance()->GetAppRootPath() + TEMP_FILE_PATH_LOCK_SCREEN_WALLPAPER;
376 String destPathHome = App::GetInstance()->GetAppRootPath() + TEMP_FILE_PATH_HOME_SCREEN_WALLPAPER;
377 r = pCropBuffer->EncodeToFile(destPath, __imageFormat, true, 100);
378 r = pCropBuffer->EncodeToFile(destPathHome, __imageFormat, true, 100);
381 r = SettingInfo::SetValue(SETTING_VALUE_LOCK_SCREEN_WALLPAPER, destPath);
382 r = SettingInfo::SetValue(SETTING_VALUE_HOME_SCREEN_WALLPAPER, destPathHome);
384 else if (r == E_OVERFLOW)
386 MessageBox messageBox;
387 messageBox.Construct(L"", L"File size is too big", MSGBOX_STYLE_NONE, 3000);
389 messageBox.ShowAndWait(modalResult);
392 else if (__statusValue == SET_AT_TYPE_CALLER_IMAGE)
397 String stringToInsert = L"_";
398 String delimiter = L".";
399 if (__sourceFilePath.IsEmpty() == false)
401 __sourceFilePath.Reverse();
402 __sourceFilePath.IndexOf(delimiter, 0, index);
403 __sourceFilePath.Reverse();
404 __sourceFilePath.Insert(stringToInsert, __sourceFilePath.GetLength() - index - 1);
405 destFilePath.Append(__sourceFilePath);
406 destFilePath.Insert(count, destFilePath.GetLength() - index - 1);
407 while (File::IsFileExist(destFilePath) == true)
410 destFilePath.Clear();
411 destFilePath.Append(__sourceFilePath);
412 destFilePath.Insert(count, destFilePath.GetLength() - index - 1);
415 r = pCropBuffer->EncodeToFile(destFilePath, __imageFormat, true, 100);
416 ContentManager::ScanFile(destFilePath);
417 Contact* pContact = null;
418 AddressbookManager* pAddressbookManager = AddressbookManager::GetInstance();
419 Addressbook* pAddressbook = null;
421 pAddressbook = pAddressbookManager->GetAddressbookN(DEFAULT_ADDRESSBOOK_ID);
422 pContact = pAddressbook->GetContactN(__contentId);
423 pContact->SetThumbnail(destFilePath);
424 pAddressbook->UpdateContact(*pContact);
429 ArrayList* pList = new (std::nothrow) ArrayList(SingleObjectDeleter);
430 pList->Add(new (std::nothrow) String(destFilePath));
431 pSceneManager->GoBackward(BackwardSceneTransition(), pList);
436 pSceneManager->GoBackward(BackwardSceneTransition());
445 ImageCropForm::OnOrientationChanged(const Control &source, OrientationStatus orientationStatus)
447 AppLogDebug("ENTER");
448 __isOrientationChanged = true;
455 ImageCropForm::OnTouchPressed(const Tizen::Ui::Control& source, const Tizen::Graphics::Point& currentPosition, const Tizen::Ui::TouchEventInfo& touchInfo)
457 AppLogDebug("ENTER");
458 __touchStart = currentPosition;
459 CheckCurrentPosition(currentPosition);
464 ImageCropForm::OnTouchMoved(const Tizen::Ui::Control& source, const Tizen::Graphics::Point& currentPosition, const Tizen::Ui::TouchEventInfo& touchInfo)
466 AppLogDebug("ENTER");
467 int deltaX = currentPosition.x - __touchStart.x;
468 int deltaY = currentPosition.y - __touchStart.y;
469 if (__cropMode.Equals("auto", false))
471 switch(__pointLocation)
473 case INSIDE_TOP_RECTANGLE:
475 if ((__cropBox.y + deltaY) > __imageBox.y && deltaY < (__cropBox.height - (CROP_RECTANGLE_HEIGHT * 3)))
477 __cropBox.y += deltaY;
478 __cropBox.height -= deltaY;
483 case INSIDE_BOTTOM_RECTANGLE:
485 if ((__cropBox.y + __cropBox.height + deltaY) < (__imageBox.y + __imageBox.height)
486 && (__cropBox.height + deltaY) > (CROP_RECTANGLE_HEIGHT * 3))
488 __cropBox.height += deltaY;
493 case INSIDE_LEFT_RECTANGLE:
495 if ((__cropBox.x + deltaX) > __imageBox.x && deltaX < __cropBox.width - (CROP_RECTANGLE_HEIGHT * 3))
497 __cropBox.x += deltaX;
498 __cropBox.width -= deltaX;
503 case INSIDE_RIGHT_RECTANGLE:
505 if ((__cropBox.x + __cropBox.width + deltaX) < (__imageBox.x + __imageBox.width)
506 && __cropBox.width + deltaX > (CROP_RECTANGLE_HEIGHT * 3))
508 __cropBox.width += deltaX;
513 case INSIDE_TOP_LEFT_RECTANGLE:
515 if ((__cropBox.y + deltaY) > __imageBox.y && deltaY < (__cropBox.height - (CROP_RECTANGLE_HEIGHT * 3))
516 && (__cropBox.x + deltaX) > __imageBox.x && deltaX < __cropBox.width - (CROP_RECTANGLE_HEIGHT * 3))
518 __cropBox.y += deltaY;
519 __cropBox.height -= deltaY;
520 __cropBox.x += deltaX;
521 __cropBox.width -= deltaX;
526 case INSIDE_TOP_RIGHT_RECTANGLE:
528 if ((__cropBox.y + deltaY) > __imageBox.y && deltaY < (__cropBox.height - (CROP_RECTANGLE_HEIGHT * 3))
529 && (__cropBox.x + __cropBox.width + deltaX) < (__imageBox.x + __imageBox.width) && __cropBox.width + deltaX > (CROP_RECTANGLE_HEIGHT * 3))
531 __cropBox.width += deltaX;
532 __cropBox.y += deltaY;
533 __cropBox.height -= deltaY;
538 case INSIDE_BOTTOM_LEFT_RECTANGLE:
540 if ((__cropBox.y + __cropBox.height + deltaY) < (__imageBox.y + __imageBox.height) && (__cropBox.height + deltaY) > (CROP_RECTANGLE_HEIGHT * 3)
541 && (__cropBox.x + deltaX) > __imageBox.x && deltaX < __cropBox.width - (CROP_RECTANGLE_HEIGHT * 3))
543 __cropBox.x += deltaX;
544 __cropBox.width -= deltaX;
545 __cropBox.height += deltaY;
550 case INSIDE_BOTTOM_RIGHT_RECTANGLE:
552 if ((__cropBox.x + __cropBox.width + deltaX) < (__imageBox.x + __imageBox.width) && __cropBox.width + deltaX > (CROP_RECTANGLE_HEIGHT * 3)
553 && (__cropBox.y + __cropBox.height + deltaY) < (__imageBox.y + __imageBox.height) && (__cropBox.height + deltaY) > (CROP_RECTANGLE_HEIGHT * 3))
555 __cropBox.width += deltaX;
556 __cropBox.height += deltaY;
563 if ((__cropBox.x + deltaX) > __imageBox.x)
565 __cropBox.x += deltaX;
569 __cropBox.x = __imageBox.x;
571 if ((__cropBox.y + deltaY) > __imageBox.y)
573 __cropBox.y += deltaY;
577 __cropBox.y = __imageBox.y;
579 if ((__cropBox.x + __cropBox.width + deltaX) > (__imageBox.x + __imageBox.width))
581 __cropBox.x = __imageBox.x + __imageBox.width - __cropBox.width;
583 if ((__cropBox.y + __cropBox.height + deltaY) > (__imageBox.y + __imageBox.height))
585 __cropBox.y = __imageBox.y + __imageBox.height - __cropBox.height;
594 else if (__cropMode != "auto")
596 if (__pointLocation == INSIDE_LEFT_RECTANGLE || __pointLocation == INSIDE_BOTTOM_LEFT_RECTANGLE
597 || __pointLocation == INSIDE_TOP_LEFT_RECTANGLE)
599 int temp = __cropBox.height;
600 int height = (__formHeight * (__cropBox.width - (2 * deltaX))) /__formWidth;
601 temp = height - temp;
602 if ((__cropBox.width - (2 * deltaX) > (CROP_RECTANGLE_HEIGHT * 3)) && (!(height >= __imageBox.height || (__cropBox.width - (2 * deltaX)) >= __imageBox.width)))
604 if (__cropBox.x + deltaX >= __imageBox.x && __cropBox.x + __cropBox.width - deltaX <= __imageBox.x + __imageBox.width
605 && __cropBox.y - (temp / 2) >= __imageBox.y && __cropBox.y + __cropBox.height + (temp / 2) < __imageBox.y + __imageBox.height
606 && (__cropBox.width - (2 * deltaX) > (CROP_RECTANGLE_HEIGHT * 3)))
608 __cropBox.x += deltaX;
609 __cropBox.width = __cropBox.width - (2 * deltaX);
610 __cropBox.height = height;
611 __cropBox.y = __cropBox.y - (temp / 2);
613 else if ((__cropBox.y + __cropBox.height) <= (__imageBox.y + __imageBox.height)
614 && __cropBox.width < __imageBox.width
615 && __cropBox.x + deltaX >= __imageBox.x && __cropBox.y - temp >= __imageBox.y )
617 if (__cropBox.x + __cropBox.width - deltaX <= __imageBox.x + __imageBox.width
618 && __cropBox.x + deltaX >= __imageBox.x)
620 __cropBox.x += deltaX;
624 __cropBox.x = __cropBox.x + (2 * deltaX);
626 __cropBox.width = __cropBox.width - (2 * deltaX);
627 __cropBox.height = height;
628 __cropBox.y = __cropBox.y - temp;
630 else if (__cropBox.y - (temp / 2) <= __imageBox.y && __cropBox.width - (deltaX) <= __imageBox.width
631 && __cropBox.x + deltaX >= __imageBox.x && __cropBox.y + __cropBox.height + temp <= __imageBox.y + __imageBox.height)
633 if (__cropBox.x + __cropBox.width - deltaX <= __imageBox.x + __imageBox.width)
635 __cropBox.x += deltaX;
639 __cropBox.x = __cropBox.x + (2 * deltaX);
641 __cropBox.width = __cropBox.width - (2 * deltaX);
642 __cropBox.height = height;
646 else if (__pointLocation == INSIDE_RIGHT_RECTANGLE || __pointLocation == INSIDE_BOTTOM_RIGHT_RECTANGLE
647 || __pointLocation == INSIDE_TOP_RIGHT_RECTANGLE)
651 int temp = __cropBox.height;
652 int height = (__formHeight * (__cropBox.width - (2 * deltaX))) / __formWidth;
653 temp = height - temp;
654 if ((__cropBox.width - (2 * deltaX) > (CROP_RECTANGLE_HEIGHT * 3)) && (!(height >= __imageBox.height || (__cropBox.width - (2 * deltaX)) >= __imageBox.width)))
656 if (deltaX < __cropBox.width - (CROP_RECTANGLE_HEIGHT * 3))
658 if (__cropBox.x + deltaX > __imageBox.x && __cropBox.x + __cropBox.width - deltaX <= __imageBox.x + __imageBox.width
659 && __cropBox.y - (temp / 2) > __imageBox.y && __cropBox.y + __cropBox.height + (temp / 2) < __imageBox.y + __imageBox.height)
661 __cropBox.x += deltaX;
662 __cropBox.width = __cropBox.width - (2 * deltaX);;
663 __cropBox.height = height;
664 __cropBox.y = __cropBox.y - (temp / 2);
666 else if ((__cropBox.y + __cropBox.height + (temp / 2)) >= (__imageBox.y + __imageBox.height)
667 && __cropBox.x + __cropBox.width - (2 * deltaX) <= __imageBox.x + __imageBox.width
668 && __cropBox.x - deltaX >= __imageBox.x && __cropBox.y - temp >= __imageBox.y )
670 if (__cropBox.x + __cropBox.width - deltaX <= __imageBox.x + __imageBox.width
671 && __cropBox.x + deltaX >= __imageBox.x)
673 __cropBox.x += deltaX;
677 __cropBox.width = __cropBox.width - (2 * deltaX);
679 __cropBox.width = __cropBox.width - (2 * deltaX);
680 __cropBox.height = height;
681 __cropBox.y = __cropBox.y - temp;
683 else if (__cropBox.y - (temp / 2) <= __imageBox.y && __cropBox.width - (deltaX) <= __imageBox.width
684 && __cropBox.x + deltaX <= __imageBox.x && __cropBox.y + __cropBox.height + temp <= __imageBox.y + __imageBox.height)
686 if (__cropBox.x + deltaX >= __imageBox.x)
688 __cropBox.x += deltaX;
692 __cropBox.x = __imageBox.x;
694 __cropBox.width = __cropBox.width - (2 * deltaX);
695 __cropBox.height = height;
697 else if (__cropBox.y - (temp / 2) <= __imageBox.y && __cropBox.x + deltaX >= __imageBox.x
698 && __cropBox.x + __cropBox.width - deltaX <= __imageBox.x + __imageBox.width
699 && __cropBox.y + __cropBox.height + temp <= __imageBox.y + __imageBox.height)
701 __cropBox.x += deltaX;
702 __cropBox.width = __cropBox.width - (2 * deltaX);
703 __cropBox.height = height;
705 else if (__cropBox.x + deltaX <= __imageBox.x && __cropBox.y - (temp / 2) >= __imageBox.y
706 && __cropBox.x + __cropBox.width - deltaX <= __imageBox.x + __imageBox.width
707 && __cropBox.y + __cropBox.height + temp <= __imageBox.y + __imageBox.height)
709 __cropBox.width = __cropBox.width - (2 * deltaX);
710 __cropBox.y = __cropBox.y - (temp / 2);
711 __cropBox.height = height;
716 else if (__pointLocation == INSIDE_TOP_RECTANGLE)
718 int temp = __cropBox.width;
719 int width = (__formWidth * (__cropBox.height - (2 * deltaY))) / __formHeight;
721 if (__cropBox.height - (2 * deltaY) > (__formHeight * (CROP_RECTANGLE_HEIGHT * 3)) / __formWidth && (!(__cropBox.height - (2 * deltaY) >= __imageBox.height || width >= __imageBox.width)))
723 if (__cropBox.x - (temp /2 ) > __imageBox.x && __cropBox.x + __cropBox.width + temp < __imageBox.x + __imageBox.width
724 && __cropBox.y + (deltaY) > __imageBox.y && __cropBox.y + __cropBox.height - (2 * deltaY) < __imageBox.y + __imageBox.height)
726 __cropBox.y += deltaY;
727 __cropBox.height = __cropBox.height - (2 * deltaY);
728 __cropBox.width = width;
729 __cropBox.x = __cropBox.x - (temp / 2);
731 else if (__cropBox.x - (temp / 2) < __imageBox.x)
733 if (__cropBox.x + __cropBox.width + temp <= __imageBox.x + __imageBox.width)
735 __cropBox.width = width;
737 if (__cropBox.y + deltaY >= __imageBox.y)
739 __cropBox.y += deltaY;
741 if (__cropBox.y + __cropBox.height - (2 * deltaY) < __imageBox.y + __imageBox.height)
743 __cropBox.height = __cropBox.height - (2 * deltaY);
746 else if (__cropBox.y + __cropBox.height - (2 * deltaY) >= __imageBox.x + __imageBox.height
747 && __cropBox.y + (2 * deltaY) >= __imageBox.y)
749 if (__cropBox.x - temp >= __imageBox.x
750 && __cropBox.x + __cropBox.width + temp > __imageBox.x + __imageBox.width)
752 __cropBox.x = __cropBox.x - temp;
754 else if (__cropBox.x - temp >= __imageBox.x)
756 __cropBox.x = __cropBox.x - temp / 2;
758 if (__cropBox.x + __cropBox.width + temp <= __imageBox.x + __imageBox.width)
760 __cropBox.width = width;
762 if (__cropBox.y + (2 * deltaY) >= __imageBox.y)
764 __cropBox.y = __cropBox.y + (2 * deltaY);
765 __cropBox.height = __cropBox.height - (2 * deltaY);
768 else if (__cropBox.x + __cropBox.width + (temp / 2) > __imageBox.x + __imageBox.width
769 && __cropBox.x - temp >= __imageBox.x && __cropBox.y + deltaY >= __imageBox.y
770 && __cropBox.y + __cropBox.height - (2 * deltaY) <= __imageBox.y + __imageBox.height)
772 __cropBox.x = __cropBox.x - temp;
773 __cropBox.width = width;
774 __cropBox.y += deltaY;
775 __cropBox.height = __cropBox.height - (2 * deltaY);
779 else if (__pointLocation == INSIDE_BOTTOM_RECTANGLE)
781 deltaY = deltaY * (-1);
782 int temp = __cropBox.width;
783 int width = (__formWidth * (__cropBox.height - (2 * deltaY))) / __formHeight;
785 if (__cropBox.height - (2 * deltaY) > (__formHeight * (CROP_RECTANGLE_HEIGHT * 3)) / __formWidth && (!(__cropBox.height - (2 * deltaY) >= __imageBox.height || width >= __imageBox.width)))
787 if (__cropBox.x - (temp / 2) >= __imageBox.x && __cropBox.x + __cropBox.width + temp < __imageBox.x + __imageBox.width
788 && __cropBox.y + deltaY > __imageBox.y && __cropBox.y + __cropBox.height - (2 * deltaY) < __imageBox.y + __imageBox.height)
790 __cropBox.y += deltaY;
791 __cropBox.height = __cropBox.height - (2 * deltaY);;
792 __cropBox.width = width;
793 __cropBox.x = __cropBox.x - (temp / 2);
795 else if (__cropBox.y + deltaY < __imageBox.y && __cropBox.y + __cropBox.height - (2 * deltaY) < __imageBox.y + __imageBox.height
796 && __cropBox.x - (temp / 2) > __imageBox.x && __cropBox.x + __cropBox.width + temp < __imageBox.x + __imageBox.width)
798 __cropBox.x = __cropBox.x - (temp / 2);
799 __cropBox.width = width;
800 __cropBox.height = __cropBox.height - (2 * deltaY);
802 else if (__cropBox.x + __cropBox.width + temp > __imageBox.x + __imageBox.width
803 && __cropBox.y + deltaY >= __imageBox.y && __cropBox.y + __cropBox.height + temp <= __imageBox.y + __imageBox.height
804 && __cropBox.x - temp >= __imageBox.x)
806 __cropBox.x = __cropBox.x - temp;
807 __cropBox.y += deltaY;
808 __cropBox.width = width;
809 __cropBox.height = __cropBox.height - (2 * deltaY);
811 else if (__cropBox.x + __cropBox.width + temp >= __imageBox.x + __imageBox.width
812 && __cropBox.y + deltaY <= __imageBox.y && __cropBox.x - temp > __imageBox.x
813 && __cropBox.y + __cropBox.height + temp < __imageBox.y + __imageBox.height)
816 __cropBox.width = width;
817 __cropBox.height = __cropBox.height - (2 * deltaY);
819 else if (__cropBox.x - temp < __imageBox.x && __cropBox.x + __cropBox.width + temp <= __imageBox.x + __imageBox.width
820 && __cropBox.y + __cropBox.height - (2 * deltaY) <= __imageBox.y + __imageBox.height)
822 if (__cropBox.y + deltaY >= __imageBox.y)
824 __cropBox.y += deltaY;
826 __cropBox.width = width;
827 __cropBox.height = __cropBox.height - (2 * deltaY);
831 else if (__pointLocation == INSIDE_CROPBOX)
833 if ((__cropBox.x + deltaX) > __imageBox.x)
835 __cropBox.x += deltaX;
839 __cropBox.x = __imageBox.x;
841 if ((__cropBox.y + deltaY) > __imageBox.y)
843 __cropBox.y += deltaY;
847 __cropBox.y = __imageBox.y;
849 if ((__cropBox.x + __cropBox.width + deltaX) > (__imageBox.x + __imageBox.width))
851 __cropBox.x = __imageBox.x + __imageBox.width - __cropBox.width;
853 if ((__cropBox.y + __cropBox.height + deltaY) > (__imageBox.y + __imageBox.height))
855 __cropBox.y = __imageBox.y + __imageBox.height - __cropBox.height;
859 RequestRedraw(false);
860 __touchStart = currentPosition;
865 ImageCropForm::OnTouchReleased(const Tizen::Ui::Control& source, const Tizen::Graphics::Point& currentPosition, const Tizen::Ui::TouchEventInfo& touchInfo)
867 AppLogDebug("ENTER");
868 __pointLocation = OUTSIDE_CROPBOX;
873 ImageCropForm::CheckCurrentPosition(const Point currentPosition)
875 AppLogDebug("ENTER");
876 if (currentPosition.x > ((__cropBox.x + __cropBox.width / 2) - (CROP_RECTANGLE_HEIGHT / 2)) && currentPosition.x < ((__cropBox.x + __cropBox.width / 2) + (CROP_RECTANGLE_HEIGHT / 2))
877 && currentPosition.y > (__cropBox.y - (CROP_RECTANGLE_HEIGHT / 2)) && currentPosition.y < (__cropBox.y + (CROP_RECTANGLE_HEIGHT / 2)))
879 __pointLocation = INSIDE_TOP_RECTANGLE;
881 else if (currentPosition.x > (__cropBox.x - (CROP_RECTANGLE_HEIGHT / 2)) && currentPosition.x < (__cropBox.x + (CROP_RECTANGLE_HEIGHT / 2))
882 && currentPosition.y > ((__cropBox.y + __cropBox.height / 2) - (CROP_RECTANGLE_HEIGHT / 2)) && currentPosition.y < ((__cropBox.y + __cropBox.height / 2) + (CROP_RECTANGLE_HEIGHT / 2)))
884 __pointLocation = INSIDE_LEFT_RECTANGLE;
886 else if (currentPosition.x > (__cropBox.x + __cropBox.width - (CROP_RECTANGLE_HEIGHT / 2)) && currentPosition.x < (__cropBox.x + __cropBox.width + (CROP_RECTANGLE_HEIGHT / 2))
887 && currentPosition.y > ((__cropBox.y + __cropBox.height / 2) - (CROP_RECTANGLE_HEIGHT / 2)) && currentPosition.y < ((__cropBox.y + __cropBox.height / 2) + (CROP_RECTANGLE_HEIGHT / 2)))
889 __pointLocation = INSIDE_RIGHT_RECTANGLE;
891 else if (currentPosition.x > ((__cropBox.x + __cropBox.width / 2) - (CROP_RECTANGLE_HEIGHT / 2)) && currentPosition.x < ((__cropBox.x + __cropBox.width / 2) + (CROP_RECTANGLE_HEIGHT / 2))
892 && currentPosition.y > (__cropBox.y + __cropBox.height - (CROP_RECTANGLE_HEIGHT / 2)) && currentPosition.y < (__cropBox.y + __cropBox.height + (CROP_RECTANGLE_HEIGHT / 2)))
894 __pointLocation = INSIDE_BOTTOM_RECTANGLE;
896 else if (currentPosition.x > (__cropBox.x - (CROP_RECTANGLE_HEIGHT / 2)) && currentPosition.x < (__cropBox.x + (CROP_RECTANGLE_HEIGHT / 2))
897 && currentPosition.y > (__cropBox.y - (CROP_RECTANGLE_HEIGHT / 2)) && currentPosition.y < (__cropBox.y +(CROP_RECTANGLE_HEIGHT / 2)))
899 __pointLocation = INSIDE_TOP_LEFT_RECTANGLE;
901 else if (currentPosition.x > ((__cropBox.x + __cropBox.width / 2) - (CROP_RECTANGLE_HEIGHT / 2)) && currentPosition.x < (__cropBox.x + __cropBox.width + (CROP_RECTANGLE_HEIGHT / 2))
902 && currentPosition.y > (__cropBox.y - (CROP_RECTANGLE_HEIGHT / 2)) && currentPosition.y < (__cropBox.y +(CROP_RECTANGLE_HEIGHT / 2)))
904 __pointLocation = INSIDE_TOP_RIGHT_RECTANGLE;
906 else if (currentPosition.x > (__cropBox.x + __cropBox.width - (CROP_RECTANGLE_HEIGHT / 2)) && currentPosition.x < (__cropBox.x + __cropBox.width + (CROP_RECTANGLE_HEIGHT / 2))
907 && currentPosition.y > (__cropBox.y + __cropBox.height - (CROP_RECTANGLE_HEIGHT / 2)) && currentPosition.y < (__cropBox.y + __cropBox.height + (CROP_RECTANGLE_HEIGHT / 2)))
909 __pointLocation = INSIDE_BOTTOM_RIGHT_RECTANGLE;
911 else if (currentPosition.x > (__cropBox.x - (CROP_RECTANGLE_HEIGHT / 2)) && currentPosition.x < (__cropBox.x + (CROP_RECTANGLE_HEIGHT / 2))
912 && currentPosition.y > (__cropBox.y + __cropBox.height - (CROP_RECTANGLE_HEIGHT / 2)) && currentPosition.y < (__cropBox.y + __cropBox.height + (CROP_RECTANGLE_HEIGHT / 2)))
914 __pointLocation = INSIDE_BOTTOM_LEFT_RECTANGLE;
916 else if (currentPosition.x > __cropBox.x && currentPosition.x < (__cropBox.x + __cropBox.width) && currentPosition.y > __cropBox.y && currentPosition.y < (__cropBox.y + __cropBox.height))
918 __pointLocation = INSIDE_CROPBOX;
922 __pointLocation = OUTSIDE_CROPBOX;
929 ImageCropForm::SetValue(void)
931 int prevHeight = __imageBox.height;
932 int prevWidth = __imageBox.width;
933 int prevImageboxX = __imageBox.x;
934 int prevImageBoxY = __imageBox.y;
935 if (GetOrientationStatus() == ORIENTATION_STATUS_PORTRAIT)
937 __formHeight = Form::GetBounds().height;
938 __formWidth = Form::GetBounds().width;
940 else if(GetOrientationStatus() == ORIENTATION_STATUS_LANDSCAPE || GetOrientationStatus() == ORIENTATION_STATUS_LANDSCAPE_REVERSE)
942 __formHeight = Form::GetBounds().width;
943 __formWidth = Form::GetBounds().height;
946 float clientAreaRatio = (GetClientAreaBoundsF().height - FOOTER_PANEL_HEIGHT) / GetClientAreaBoundsF().width;
947 if (__imageWidth >= GetClientAreaBounds().width || __imageHeight > (GetClientAreaBounds().height - FOOTER_PANEL_HEIGHT))
949 float imageRatio = __imageHeight / (float) __imageWidth;
950 if (imageRatio < clientAreaRatio)
952 __imageBox.width = GetClientAreaBounds().width;
953 __imageBox.height = (__imageHeight * __imageBox.width) / __imageWidth;
954 __imageBox.x = GetClientAreaBounds().x;
955 __imageBox.y = GetClientAreaBounds().y + ((GetClientAreaBounds().height - FOOTER_PANEL_HEIGHT) - __imageBox.height) / 2;
959 __imageBox.height = GetClientAreaBounds().height - FOOTER_PANEL_HEIGHT;
960 __imageBox.width = (__imageWidth * __imageBox.height) / __imageHeight;
961 __imageBox.x = (GetClientAreaBounds().width - __imageBox.width) / 2;
962 __imageBox.y = GetClientAreaBounds().y;
967 __imageBox.height = __imageHeight;
968 __imageBox.width = __imageWidth;
969 __imageBox.x = (GetClientAreaBounds().width - __imageBox.width) / 2;
970 __imageBox.y = (GetClientAreaBounds().height - __imageBox.height) / 2 ;
972 if (__isOrientationChanged == false)
974 if (__imageBox.height > __imageBox.width)
976 __cropBox.width = (2 * __imageBox.width) / 3;
977 __cropBox.height = (__formHeight * __cropBox.width / __formWidth);
978 if (__cropBox.height > __imageBox.height)
980 __cropBox.height = (2 * __imageBox.height) / 3;
981 __cropBox.width = (__cropBox.height * __formWidth) / __formHeight;
983 __cropBox.y = __imageBox.y + (__imageBox.height - __cropBox.height) / 2;
984 __cropBox.x = __imageBox.x + (__imageBox.width - __cropBox.width) / 2;
988 __cropBox.height = (2 *__imageBox.height) / 3;
989 __cropBox.width = (__formWidth * __cropBox.height / __formHeight);
990 if (__cropBox.width > __imageBox.width)
992 __cropBox.width = (2 * __imageBox.width) / 3;
993 __cropBox.height = (__formHeight * __cropBox.width) / __formWidth;
995 __cropBox.y = __imageBox.y + (__imageBox.height - __cropBox.height) / 2;
996 __cropBox.x = __imageBox.x + (__imageBox.width - __cropBox.width) / 2;
1001 float temp = __imageBox.x + ((__cropBox.x - prevImageboxX) * __imageBox.width) / (float)prevWidth;
1002 __cropBox.x = (temp + 0.5);
1003 temp = __imageBox.y + ((__cropBox.y - prevImageBoxY) * __imageBox.height) / (float)prevHeight;
1004 __cropBox.y = (temp + 0.5);
1005 temp = (__cropBox.height * __imageBox.height) / (float)prevHeight;
1006 __cropBox.height = (temp + 0.5);
1007 temp = (__cropBox.width * __imageBox.width) / (float)prevWidth;
1008 __cropBox.width = (temp + 0.5);
1010 __pCanvas = GetCanvasN();
1011 __pCanvas->SetForegroundColor(CROP_BOX_RECTANGLE_COLOR);
1014 void ImageCropForm::OnFormFileEventOccuered(const int index, const unsigned long eventId)
1016 AppLogDebug(" ENTER");
1017 UiApp::GetInstance()->Terminate();
1018 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));