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 "IvImageViewerApp.h"
28 #include "IvImageViewerPresentationModel.h"
29 #include "IvResourceManager.h"
32 using namespace Tizen::App;
33 using namespace Tizen::Base;
34 using namespace Tizen::Base::Collection;
35 using namespace Tizen::Content;
36 using namespace Tizen::Graphics;
37 using namespace Tizen::Io;
38 using namespace Tizen::Media;
39 using namespace Tizen::System;
40 using namespace Tizen::Social;
41 using namespace Tizen::Ui::Controls;
42 using namespace Tizen::Ui::Scenes;
44 static const unsigned int CROP_BOX_RECTANGLE_COLOR = Color32<199, 110, 6>::Value;
45 static const int CROP_BOX_LINE_WIDTH = 5;
47 ImageCropForm::ImageCropForm(void)
48 : __cropMode(APPCONTROL_DATA_AUTO)
51 , __isOrientationChanged(false)
53 , __pCurrentBitmap(null)
54 , __pRectangleBitmap(null)
55 , __pointLocation(OUTSIDE_CROPBOX)
56 , __pPresentationModel(null)
63 ImageCropForm::~ImageCropForm(void)
65 if (__pCurrentBitmap != null)
67 delete __pCurrentBitmap;
69 if (__pRectangleBitmap != null)
71 delete __pRectangleBitmap;
73 if (__pCanvas != null)
80 ImageCropForm::Initialize(void)
82 Form::Construct(IDL_FORM_IMAGE_CROP);
83 Form::SetFormStyle(FORM_STYLE_NORMAL | FORM_STYLE_INDICATOR);
85 __pPresentationModel = ImageViewerPresentationModel::GetInstance();
91 ImageCropForm::OnInitializing(void)
93 AppResource* pAppResource = null;
94 Bitmap* pSaveButtonBitmap = null;
95 Bitmap* pCancelButtonBitmap = null;
97 Button* pSaveButton = null;
98 Button* pCancelButton = null;
99 pAppResource = UiApp::App::GetInstance()->GetAppResource();
100 if (pAppResource != null)
102 pSaveButtonBitmap = pAppResource->GetBitmapN(IDB_IMAGE_CROP_FORM_SAVE_ICON);
103 pCancelButtonBitmap = pAppResource->GetBitmapN(IDB_IMAGE_CROP_FORM_CANCEL_ICON);
104 __pRectangleBitmap = pAppResource->GetBitmapN(IDB_IMAGE_CROP_RECTANGLE);
107 pPanel = static_cast<Panel*>(GetControl(L"IDC_FOOTER_PANEL"));
110 pSaveButton = static_cast<Button*>(pPanel->GetControl(L"IDC_SAVE_BUTTON"));
111 if (pSaveButton != null && pSaveButtonBitmap != null)
113 Point startPoint((pSaveButton->GetWidth() - pSaveButtonBitmap->GetWidth()) / 2,
114 (pSaveButton->GetHeight() - pSaveButtonBitmap->GetHeight()) / 2);
115 pSaveButton->SetNormalBitmap(startPoint, *pSaveButtonBitmap);
116 pSaveButton->AddActionEventListener(*this);
117 pSaveButton->SetActionId(IDA_BUTTON_SAVE);
118 delete pSaveButtonBitmap;
121 pCancelButton = static_cast<Button*>(pPanel->GetControl(L"IDC_CANCEL_BUTTON"));
122 if (pCancelButton != null && pCancelButtonBitmap != null)
124 Point startPoint((pCancelButton->GetWidth() - pCancelButtonBitmap->GetWidth()) / 2,
125 (pCancelButton->GetHeight() - pCancelButtonBitmap->GetHeight()) / 2);
126 pCancelButton->SetNormalBitmap(startPoint, *pCancelButtonBitmap);
127 pCancelButton->AddActionEventListener(*this);
128 pCancelButton->SetActionId(IDA_BUTTON_CANCEL);
129 delete pCancelButtonBitmap;
131 SetControlAlwaysOnTop(*pPanel, true);
132 Label* pLabel = static_cast<Label*>(pPanel->GetControl(L"IDC_PANEL_LABEL"));
135 SetControlAlwaysAtBottom(*pLabel, true);
138 AddTouchEventListener(*this);
139 AddOrientationEventListener(*this);
140 __pPresentationModel->AddFileUpdateListener(this);
146 ImageCropForm::OnSceneActivatedN(const SceneId& previousSceneId, const SceneId& currentSceneId, IList* pArgs)
148 AppLogDebug("ENTER pArgs(%x)", pArgs);
151 __sourceFilePath = *(static_cast<String*>(pArgs->GetAt(0)));
152 __cropMode = *(static_cast<String*>(pArgs->GetAt(1)));
156 __sourceFilePath = __pPresentationModel->GetFilePathAt(0);
158 ImageViewerApp* pImageViewerApp = static_cast<ImageViewerApp*>(ImageViewerApp::GetInstance());
159 const IMap* pIMap = pImageViewerApp->GetAppControlArguments();
163 const String* pCropMode = static_cast<const String*>(pIMap->GetValue(String(APPCONTROL_KEY_IMAGE_CROP_MODE)));
165 if (pCropMode != null)
167 __cropMode = *pCropMode;
172 if (__sourceFilePath.IsEmpty() == false)
174 result r = __imageBuffer.Construct(__sourceFilePath.GetPointer());
177 __imageWidth = __imageBuffer.GetWidth();
178 __imageHeight = __imageBuffer.GetHeight();
179 __pCurrentBitmap = __imageBuffer.GetBitmapN(BITMAP_PIXEL_FORMAT_RGB565, BUFFER_SCALING_AUTO);
182 __imageFormat = img.GetImageFormat(__sourceFilePath);
185 if (__cropMode == APPCONTROL_DATA_AUTO)
190 else if (__cropMode == APPCONTROL_DATA_FIT_TO_SCREEN)
192 Integer *pStatus = static_cast<Integer*>(pArgs->GetAt(2));
195 __statusValue = pStatus->ToInt();
197 if (__statusValue == SET_AT_TYPE_CALLER_IMAGE)
199 LongLong* pContentId = static_cast<LongLong*>(pArgs->GetAt(3));
200 if (pContentId != null)
202 __contentId = pContentId->ToLongLong();
207 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
211 ImageCropForm::OnSceneDeactivated(const SceneId& currentSceneId, const SceneId& nextSceneId)
213 AppLogDebug("ENTER");
214 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
218 ImageCropForm::OnDraw(void)
220 result r = E_SUCCESS;
221 AppLogDebug("ENTER");
222 if (__pCanvas != null)
224 r = __pCanvas->SetLineWidth(CROP_BOX_LINE_WIDTH);
225 r = __pCanvas->DrawBitmap(Rectangle(__imageBox.x, __imageBox.y, __imageBox.width, __imageBox.height), *__pCurrentBitmap);
226 r = __pCanvas->DrawRectangle(Rectangle(__cropBox.x, __cropBox.y, __cropBox.width, __cropBox.height));
227 r = __pCanvas->DrawBitmap(Rectangle((__cropBox.x + __cropBox.width / 2) - (H_CROP_RECTANGLE / 2), __cropBox.y - (H_CROP_RECTANGLE / 2), H_CROP_RECTANGLE, H_CROP_RECTANGLE), *__pRectangleBitmap); // Top Rectangle
228 r = __pCanvas->DrawBitmap(Rectangle((__cropBox.x + __cropBox.width / 2) - (H_CROP_RECTANGLE / 2), (__cropBox.y + __cropBox.height - (H_CROP_RECTANGLE / 2)), H_CROP_RECTANGLE, H_CROP_RECTANGLE), *__pRectangleBitmap); // Bottom Rectangle
229 r = __pCanvas->DrawBitmap(Rectangle(__cropBox.x - (H_CROP_RECTANGLE / 2), (__cropBox.y + __cropBox.height / 2) - (H_CROP_RECTANGLE / 2), H_CROP_RECTANGLE, H_CROP_RECTANGLE), *__pRectangleBitmap); //Left Rectangle
230 r = __pCanvas->DrawBitmap(Rectangle(__cropBox.x + __cropBox.width - (H_CROP_RECTANGLE / 2), (__cropBox.y + __cropBox.height / 2) - (H_CROP_RECTANGLE / 2), H_CROP_RECTANGLE, H_CROP_RECTANGLE), *__pRectangleBitmap); //Right Rectangle
231 r = __pCanvas->DrawBitmap(Rectangle(__cropBox.x - (H_CROP_RECTANGLE / 2), __cropBox.y - (H_CROP_RECTANGLE / 2), H_CROP_RECTANGLE, H_CROP_RECTANGLE), *__pRectangleBitmap); // Top Left Rectangle
232 r = __pCanvas->DrawBitmap(Rectangle(__cropBox.x + __cropBox.width - (H_CROP_RECTANGLE / 2), __cropBox.y - (H_CROP_RECTANGLE / 2), H_CROP_RECTANGLE, H_CROP_RECTANGLE), *__pRectangleBitmap); // Top Right Rectangle
233 r = __pCanvas->DrawBitmap(Rectangle(__cropBox.x + __cropBox.width - (H_CROP_RECTANGLE / 2), (__cropBox.y + __cropBox.height - (H_CROP_RECTANGLE / 2)), H_CROP_RECTANGLE, H_CROP_RECTANGLE), *__pRectangleBitmap); // Bottom Right Rectangle
234 r = __pCanvas->DrawBitmap(Rectangle(__cropBox.x - (H_CROP_RECTANGLE / 2), (__cropBox.y + __cropBox.height - (H_CROP_RECTANGLE / 2)), H_CROP_RECTANGLE, H_CROP_RECTANGLE), *__pRectangleBitmap); // Bottom Left Rectangle
236 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
241 ImageCropForm::OnActionPerformed(const Tizen::Ui::Control& source, int actionId)
243 SceneManager* pSceneManager = SceneManager::GetInstance();
246 case IDA_BUTTON_CANCEL:
248 ImageViewerApp* pApp = dynamic_cast<ImageViewerApp*>(UiApp::GetInstance());
250 if (pApp->GetAppControlOperationId() == APPCONTROL_OPERATION_ID_IMAGE_CROP)
254 pApp->SendAppControlResult(APP_CTRL_RESULT_CANCELED, null);
260 pSceneManager->GoBackward(BackwardSceneTransition());
265 case IDA_BUTTON_SAVE:
267 if (__cropMode == APPCONTROL_DATA_AUTO)
269 ImageBuffer* pCropBuffer = null;
273 if (__sourceFilePath.IsEmpty() == false)
275 __sourceFilePath.Reverse();
276 __sourceFilePath.IndexOf(FILE_EXT_SEPARATOR, 0, index);
277 __sourceFilePath.Reverse();
278 __sourceFilePath.Insert(FILE_NAME_SEPARATOR, __sourceFilePath.GetLength() - index - 1);
279 destFilePath.Append(__sourceFilePath);
280 destFilePath.Insert(count, destFilePath.GetLength() - index - 1);
281 while (File::IsFileExist(destFilePath) == true)
284 destFilePath.Clear();
285 destFilePath.Append(__sourceFilePath);
286 destFilePath.Insert(count, destFilePath.GetLength() - index - 1);
288 pCropBuffer = __imageBuffer.CropN(((__cropBox.x - __imageBox.x) * __imageWidth) / __imageBox.width, ((__cropBox.y - __imageBox.y) * __imageHeight) / __imageBox.height,
289 (__cropBox.width * __imageWidth) / __imageBox.width, (__cropBox.height * __imageHeight) / __imageBox.height);
290 pCropBuffer->EncodeToFile(destFilePath, __imageFormat, true, 100);
291 ContentManager::ScanFile(destFilePath);
294 ArrayList* pList = new (std::nothrow) ArrayList(SingleObjectDeleter);
295 pList->Add(new (std::nothrow) String(destFilePath));
296 ImageViewerApp* pApp = dynamic_cast<ImageViewerApp*>(UiApp::GetInstance());
298 if (pApp->GetAppControlOperationId() == APPCONTROL_OPERATION_ID_IMAGE_CROP)
300 HashMap* pMap = new (std::nothrow) HashMap(SingleObjectDeleter);
302 pMap->Add(new (std::nothrow) String(APPCONTROL_KEY_SELECTED), pList);
306 pApp->SendAppControlResult(APP_CTRL_RESULT_SUCCEEDED, pMap);
312 pSceneManager->GoBackward(BackwardSceneTransition(), pList);
315 else if (__cropMode == APPCONTROL_DATA_FIT_TO_SCREEN)
317 ImageBuffer* pCropBuffer = null;
318 result r = E_SUCCESS;
319 pCropBuffer = __imageBuffer.CropN(((__cropBox.x - __imageBox.x) * __imageWidth) / __imageBox.width, ((__cropBox.y - __imageBox.y) * __imageHeight) / __imageBox.height,
320 (__cropBox.width * __imageWidth) / __imageBox.width, (__cropBox.height * __imageHeight) / __imageBox.height);
322 if (__statusValue == SET_AT_TYPE_HOME_SCREEN_WALLPAPER)
324 String destPath = App::GetInstance()->GetAppDataPath() + TEMP_FILE_PATH_HOME_SCREEN_WALLPAPER;
325 r = pCropBuffer->EncodeToFile(destPath, __imageFormat, true, 100);
328 r = SettingInfo::SetValue(SETTING_VALUE_HOME_SCREEN_WALLPAPER, destPath);
331 else if (__statusValue == SET_AT_TYPE_LOCK_SCREEN_WALLPAPER)
333 String destPath = App::GetInstance()->GetAppDataPath() + TEMP_FILE_PATH_LOCK_SCREEN_WALLPAPER;
334 r = pCropBuffer->EncodeToFile(destPath, __imageFormat, true, 100);
337 r = SettingInfo::SetValue(SETTING_VALUE_LOCK_SCREEN_WALLPAPER, destPath);
340 else if (__statusValue == SET_AT_TYPE_HOME_AND_LOCK_SCREEN_WALLPAPER)
342 String destPath = App::GetInstance()->GetAppDataPath() + TEMP_FILE_PATH_LOCK_SCREEN_WALLPAPER;
343 String destPathHome = App::GetInstance()->GetAppDataPath() + TEMP_FILE_PATH_HOME_SCREEN_WALLPAPER;
344 r = pCropBuffer->EncodeToFile(destPath, __imageFormat, true, 100);
345 r = pCropBuffer->EncodeToFile(destPathHome, __imageFormat, true, 100);
348 r = SettingInfo::SetValue(SETTING_VALUE_LOCK_SCREEN_WALLPAPER, destPath);
349 r = SettingInfo::SetValue(SETTING_VALUE_HOME_SCREEN_WALLPAPER, destPathHome);
351 else if (r == E_OVERFLOW)
353 MessageBox messageBox;
354 messageBox.Construct(L"", L"File size is too big", MSGBOX_STYLE_NONE, 3000);
356 messageBox.ShowAndWait(modalResult);
359 else if (__statusValue == SET_AT_TYPE_CALLER_IMAGE)
365 if (__sourceFilePath.IsEmpty() == false)
367 __sourceFilePath.Reverse();
368 __sourceFilePath.IndexOf(FILE_EXT_SEPARATOR, 0, index);
369 __sourceFilePath.Reverse();
370 __sourceFilePath.Insert(FILE_NAME_SEPARATOR, __sourceFilePath.GetLength() - index - 1);
371 destFilePath.Append(__sourceFilePath);
372 destFilePath.Insert(count, destFilePath.GetLength() - index - 1);
373 while (File::IsFileExist(destFilePath) == true)
376 destFilePath.Clear();
377 destFilePath.Append(__sourceFilePath);
378 destFilePath.Insert(count, destFilePath.GetLength() - index - 1);
381 r = pCropBuffer->EncodeToFile(destFilePath, __imageFormat, true, 100);
382 ContentManager::ScanFile(destFilePath);
383 Contact* pContact = null;
384 AddressbookManager* pAddressbookManager = AddressbookManager::GetInstance();
385 Addressbook* pAddressbook = null;
387 pAddressbook = pAddressbookManager->GetAddressbookN(DEFAULT_ADDRESSBOOK_ID);
388 pContact = pAddressbook->GetContactN(__contentId);
389 pContact->SetThumbnail(destFilePath);
390 pAddressbook->UpdateContact(*pContact);
396 ArrayList* pList = new (std::nothrow) ArrayList(SingleObjectDeleter);
397 pList->Add(new (std::nothrow) String(destFilePath));
398 pSceneManager->GoBackward(BackwardSceneTransition(), pList);
400 AppLogDebug("EXIT(%s)1", GetErrorMessage(GetLastResult()));
404 pSceneManager->GoBackward(BackwardSceneTransition());
409 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
413 ImageCropForm::OnOrientationChanged(const Control &source, OrientationStatus orientationStatus)
415 AppLogDebug("ENTER");
416 __isOrientationChanged = true;
419 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
423 ImageCropForm::OnTouchPressed(const Tizen::Ui::Control& source, const Tizen::Graphics::Point& currentPosition, const Tizen::Ui::TouchEventInfo& touchInfo)
425 AppLogDebug("ENTER");
426 __touchStart = currentPosition;
427 CheckCurrentPosition(currentPosition);
428 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
432 ImageCropForm::OnTouchMoved(const Tizen::Ui::Control& source, const Tizen::Graphics::Point& currentPosition, const Tizen::Ui::TouchEventInfo& touchInfo)
434 AppLogDebug("ENTER");
435 int deltaX = currentPosition.x - __touchStart.x;
436 int deltaY = currentPosition.y - __touchStart.y;
437 if (__cropMode.Equals(APPCONTROL_DATA_AUTO, false))
439 switch(__pointLocation)
441 case INSIDE_TOP_RECTANGLE:
443 if ((__cropBox.y + deltaY) > __imageBox.y && deltaY < (__cropBox.height - (H_CROP_RECTANGLE * 3)))
445 __cropBox.y += deltaY;
446 __cropBox.height -= deltaY;
451 case INSIDE_BOTTOM_RECTANGLE:
453 if ((__cropBox.y + __cropBox.height + deltaY) < (__imageBox.y + __imageBox.height)
454 && (__cropBox.height + deltaY) > (H_CROP_RECTANGLE * 3))
456 __cropBox.height += deltaY;
461 case INSIDE_LEFT_RECTANGLE:
463 if ((__cropBox.x + deltaX) > __imageBox.x && deltaX < __cropBox.width - (H_CROP_RECTANGLE * 3))
465 __cropBox.x += deltaX;
466 __cropBox.width -= deltaX;
471 case INSIDE_RIGHT_RECTANGLE:
473 if ((__cropBox.x + __cropBox.width + deltaX) < (__imageBox.x + __imageBox.width)
474 && __cropBox.width + deltaX > (H_CROP_RECTANGLE * 3))
476 __cropBox.width += deltaX;
481 case INSIDE_TOP_LEFT_RECTANGLE:
483 if ((__cropBox.y + deltaY) > __imageBox.y && deltaY < (__cropBox.height - (H_CROP_RECTANGLE * 3))
484 && (__cropBox.x + deltaX) > __imageBox.x && deltaX < __cropBox.width - (H_CROP_RECTANGLE * 3))
486 __cropBox.y += deltaY;
487 __cropBox.height -= deltaY;
488 __cropBox.x += deltaX;
489 __cropBox.width -= deltaX;
494 case INSIDE_TOP_RIGHT_RECTANGLE:
496 if ((__cropBox.y + deltaY) > __imageBox.y && deltaY < (__cropBox.height - (H_CROP_RECTANGLE * 3))
497 && (__cropBox.x + __cropBox.width + deltaX) < (__imageBox.x + __imageBox.width) && __cropBox.width + deltaX > (H_CROP_RECTANGLE * 3))
499 __cropBox.width += deltaX;
500 __cropBox.y += deltaY;
501 __cropBox.height -= deltaY;
506 case INSIDE_BOTTOM_LEFT_RECTANGLE:
508 if ((__cropBox.y + __cropBox.height + deltaY) < (__imageBox.y + __imageBox.height) && (__cropBox.height + deltaY) > (H_CROP_RECTANGLE * 3)
509 && (__cropBox.x + deltaX) > __imageBox.x && deltaX < __cropBox.width - (H_CROP_RECTANGLE * 3))
511 __cropBox.x += deltaX;
512 __cropBox.width -= deltaX;
513 __cropBox.height += deltaY;
518 case INSIDE_BOTTOM_RIGHT_RECTANGLE:
520 if ((__cropBox.x + __cropBox.width + deltaX) < (__imageBox.x + __imageBox.width) && __cropBox.width + deltaX > (H_CROP_RECTANGLE * 3)
521 && (__cropBox.y + __cropBox.height + deltaY) < (__imageBox.y + __imageBox.height) && (__cropBox.height + deltaY) > (H_CROP_RECTANGLE * 3))
523 __cropBox.width += deltaX;
524 __cropBox.height += deltaY;
531 if ((__cropBox.x + deltaX) > __imageBox.x)
533 __cropBox.x += deltaX;
537 __cropBox.x = __imageBox.x;
539 if ((__cropBox.y + deltaY) > __imageBox.y)
541 __cropBox.y += deltaY;
545 __cropBox.y = __imageBox.y;
547 if ((__cropBox.x + __cropBox.width + deltaX) > (__imageBox.x + __imageBox.width))
549 __cropBox.x = __imageBox.x + __imageBox.width - __cropBox.width;
551 if ((__cropBox.y + __cropBox.height + deltaY) > (__imageBox.y + __imageBox.height))
553 __cropBox.y = __imageBox.y + __imageBox.height - __cropBox.height;
562 else if (__cropMode != APPCONTROL_DATA_AUTO)
564 if (__pointLocation == INSIDE_LEFT_RECTANGLE || __pointLocation == INSIDE_BOTTOM_LEFT_RECTANGLE
565 || __pointLocation == INSIDE_TOP_LEFT_RECTANGLE)
567 int temp = __cropBox.height;
568 int height = (__formHeight * (__cropBox.width - (2 * deltaX))) /__formWidth;
569 temp = height - temp;
570 if ((__cropBox.width - (2 * deltaX) > (H_CROP_RECTANGLE * 3)) && (!(height >= __imageBox.height || (__cropBox.width - (2 * deltaX)) >= __imageBox.width)))
572 if (__cropBox.x + deltaX >= __imageBox.x && __cropBox.x + __cropBox.width - deltaX <= __imageBox.x + __imageBox.width
573 && __cropBox.y - (temp / 2) >= __imageBox.y && __cropBox.y + __cropBox.height + (temp / 2) < __imageBox.y + __imageBox.height
574 && (__cropBox.width - (2 * deltaX) > (H_CROP_RECTANGLE * 3)))
576 __cropBox.x += deltaX;
577 __cropBox.width = __cropBox.width - (2 * deltaX);
578 __cropBox.height = height;
579 __cropBox.y = __cropBox.y - (temp / 2);
581 else if ((__cropBox.y + __cropBox.height) <= (__imageBox.y + __imageBox.height)
582 && __cropBox.width < __imageBox.width
583 && __cropBox.x + deltaX >= __imageBox.x && __cropBox.y - temp >= __imageBox.y )
585 if (__cropBox.x + __cropBox.width - deltaX <= __imageBox.x + __imageBox.width
586 && __cropBox.x + deltaX >= __imageBox.x)
588 __cropBox.x += deltaX;
592 __cropBox.x = __cropBox.x + (2 * deltaX);
594 __cropBox.width = __cropBox.width - (2 * deltaX);
595 __cropBox.height = height;
596 __cropBox.y = __cropBox.y - temp;
598 else if (__cropBox.y - (temp / 2) <= __imageBox.y && __cropBox.width - (deltaX) <= __imageBox.width
599 && __cropBox.x + deltaX >= __imageBox.x && __cropBox.y + __cropBox.height + temp <= __imageBox.y + __imageBox.height)
601 if (__cropBox.x + __cropBox.width - deltaX <= __imageBox.x + __imageBox.width)
603 __cropBox.x += deltaX;
607 __cropBox.x = __cropBox.x + (2 * deltaX);
609 __cropBox.width = __cropBox.width - (2 * deltaX);
610 __cropBox.height = height;
614 else if (__pointLocation == INSIDE_RIGHT_RECTANGLE || __pointLocation == INSIDE_BOTTOM_RIGHT_RECTANGLE
615 || __pointLocation == INSIDE_TOP_RIGHT_RECTANGLE)
619 int temp = __cropBox.height;
620 int height = (__formHeight * (__cropBox.width - (2 * deltaX))) / __formWidth;
621 temp = height - temp;
622 if ((__cropBox.width - (2 * deltaX) > (H_CROP_RECTANGLE * 3)) && (!(height >= __imageBox.height || (__cropBox.width - (2 * deltaX)) >= __imageBox.width)))
624 if (deltaX < __cropBox.width - (H_CROP_RECTANGLE * 3))
626 if (__cropBox.x + deltaX > __imageBox.x && __cropBox.x + __cropBox.width - deltaX <= __imageBox.x + __imageBox.width
627 && __cropBox.y - (temp / 2) > __imageBox.y && __cropBox.y + __cropBox.height + (temp / 2) < __imageBox.y + __imageBox.height)
629 __cropBox.x += deltaX;
630 __cropBox.width = __cropBox.width - (2 * deltaX);;
631 __cropBox.height = height;
632 __cropBox.y = __cropBox.y - (temp / 2);
634 else if ((__cropBox.y + __cropBox.height + (temp / 2)) >= (__imageBox.y + __imageBox.height)
635 && __cropBox.x + __cropBox.width - (2 * deltaX) <= __imageBox.x + __imageBox.width
636 && __cropBox.x - deltaX >= __imageBox.x && __cropBox.y - temp >= __imageBox.y )
638 if (__cropBox.x + __cropBox.width - deltaX <= __imageBox.x + __imageBox.width
639 && __cropBox.x + deltaX >= __imageBox.x)
641 __cropBox.x += deltaX;
645 __cropBox.width = __cropBox.width - (2 * deltaX);
647 __cropBox.width = __cropBox.width - (2 * deltaX);
648 __cropBox.height = height;
649 __cropBox.y = __cropBox.y - temp;
651 else if (__cropBox.y - (temp / 2) <= __imageBox.y && __cropBox.width - (deltaX) <= __imageBox.width
652 && __cropBox.x + deltaX <= __imageBox.x && __cropBox.y + __cropBox.height + temp <= __imageBox.y + __imageBox.height)
654 if (__cropBox.x + deltaX >= __imageBox.x)
656 __cropBox.x += deltaX;
660 __cropBox.x = __imageBox.x;
662 __cropBox.width = __cropBox.width - (2 * deltaX);
663 __cropBox.height = height;
665 else if (__cropBox.y - (temp / 2) <= __imageBox.y && __cropBox.x + deltaX >= __imageBox.x
666 && __cropBox.x + __cropBox.width - deltaX <= __imageBox.x + __imageBox.width
667 && __cropBox.y + __cropBox.height + temp <= __imageBox.y + __imageBox.height)
669 __cropBox.x += deltaX;
670 __cropBox.width = __cropBox.width - (2 * deltaX);
671 __cropBox.height = height;
673 else if (__cropBox.x + deltaX <= __imageBox.x && __cropBox.y - (temp / 2) >= __imageBox.y
674 && __cropBox.x + __cropBox.width - deltaX <= __imageBox.x + __imageBox.width
675 && __cropBox.y + __cropBox.height + temp <= __imageBox.y + __imageBox.height)
677 __cropBox.width = __cropBox.width - (2 * deltaX);
678 __cropBox.y = __cropBox.y - (temp / 2);
679 __cropBox.height = height;
684 else if (__pointLocation == INSIDE_TOP_RECTANGLE)
686 int temp = __cropBox.width;
687 int width = (__formWidth * (__cropBox.height - (2 * deltaY))) / __formHeight;
689 if (__cropBox.height - (2 * deltaY) > (__formHeight * (H_CROP_RECTANGLE * 3)) / __formWidth && (!(__cropBox.height - (2 * deltaY) >= __imageBox.height || width >= __imageBox.width)))
691 if (__cropBox.x - (temp /2 ) > __imageBox.x && __cropBox.x + __cropBox.width + temp < __imageBox.x + __imageBox.width
692 && __cropBox.y + (deltaY) > __imageBox.y && __cropBox.y + __cropBox.height - (2 * deltaY) < __imageBox.y + __imageBox.height)
694 __cropBox.y += deltaY;
695 __cropBox.height = __cropBox.height - (2 * deltaY);
696 __cropBox.width = width;
697 __cropBox.x = __cropBox.x - (temp / 2);
699 else if (__cropBox.x - (temp / 2) < __imageBox.x)
701 if (__cropBox.x + __cropBox.width + temp <= __imageBox.x + __imageBox.width)
703 __cropBox.width = width;
705 if (__cropBox.y + deltaY >= __imageBox.y)
707 __cropBox.y += deltaY;
709 if (__cropBox.y + __cropBox.height - (2 * deltaY) < __imageBox.y + __imageBox.height)
711 __cropBox.height = __cropBox.height - (2 * deltaY);
714 else if (__cropBox.y + __cropBox.height - (2 * deltaY) >= __imageBox.x + __imageBox.height
715 && __cropBox.y + (2 * deltaY) >= __imageBox.y)
717 if (__cropBox.x - temp >= __imageBox.x
718 && __cropBox.x + __cropBox.width + temp > __imageBox.x + __imageBox.width)
720 __cropBox.x = __cropBox.x - temp;
722 else if (__cropBox.x - temp >= __imageBox.x)
724 __cropBox.x = __cropBox.x - temp / 2;
726 if (__cropBox.x + __cropBox.width + temp <= __imageBox.x + __imageBox.width)
728 __cropBox.width = width;
730 if (__cropBox.y + (2 * deltaY) >= __imageBox.y)
732 __cropBox.y = __cropBox.y + (2 * deltaY);
733 __cropBox.height = __cropBox.height - (2 * deltaY);
736 else if (__cropBox.x + __cropBox.width + (temp / 2) > __imageBox.x + __imageBox.width
737 && __cropBox.x - temp >= __imageBox.x && __cropBox.y + deltaY >= __imageBox.y
738 && __cropBox.y + __cropBox.height - (2 * deltaY) <= __imageBox.y + __imageBox.height)
740 __cropBox.x = __cropBox.x - temp;
741 __cropBox.width = width;
742 __cropBox.y += deltaY;
743 __cropBox.height = __cropBox.height - (2 * deltaY);
747 else if (__pointLocation == INSIDE_BOTTOM_RECTANGLE)
749 deltaY = deltaY * (-1);
750 int temp = __cropBox.width;
751 int width = (__formWidth * (__cropBox.height - (2 * deltaY))) / __formHeight;
753 if (__cropBox.height - (2 * deltaY) > (__formHeight * (H_CROP_RECTANGLE * 3)) / __formWidth && (!(__cropBox.height - (2 * deltaY) >= __imageBox.height || width >= __imageBox.width)))
755 if (__cropBox.x - (temp / 2) >= __imageBox.x && __cropBox.x + __cropBox.width + temp < __imageBox.x + __imageBox.width
756 && __cropBox.y + deltaY > __imageBox.y && __cropBox.y + __cropBox.height - (2 * deltaY) < __imageBox.y + __imageBox.height)
758 __cropBox.y += deltaY;
759 __cropBox.height = __cropBox.height - (2 * deltaY);;
760 __cropBox.width = width;
761 __cropBox.x = __cropBox.x - (temp / 2);
763 else if (__cropBox.y + deltaY < __imageBox.y && __cropBox.y + __cropBox.height - (2 * deltaY) < __imageBox.y + __imageBox.height
764 && __cropBox.x - (temp / 2) > __imageBox.x && __cropBox.x + __cropBox.width + temp < __imageBox.x + __imageBox.width)
766 __cropBox.x = __cropBox.x - (temp / 2);
767 __cropBox.width = width;
768 __cropBox.height = __cropBox.height - (2 * deltaY);
770 else if (__cropBox.x + __cropBox.width + temp > __imageBox.x + __imageBox.width
771 && __cropBox.y + deltaY >= __imageBox.y && __cropBox.y + __cropBox.height + temp <= __imageBox.y + __imageBox.height
772 && __cropBox.x - temp >= __imageBox.x)
774 __cropBox.x = __cropBox.x - temp;
775 __cropBox.y += deltaY;
776 __cropBox.width = width;
777 __cropBox.height = __cropBox.height - (2 * deltaY);
779 else if (__cropBox.x + __cropBox.width + temp >= __imageBox.x + __imageBox.width
780 && __cropBox.y + deltaY <= __imageBox.y && __cropBox.x - temp > __imageBox.x
781 && __cropBox.y + __cropBox.height + temp < __imageBox.y + __imageBox.height)
784 __cropBox.width = width;
785 __cropBox.height = __cropBox.height - (2 * deltaY);
787 else if (__cropBox.x - temp < __imageBox.x && __cropBox.x + __cropBox.width + temp <= __imageBox.x + __imageBox.width
788 && __cropBox.y + __cropBox.height - (2 * deltaY) <= __imageBox.y + __imageBox.height)
790 if (__cropBox.y + deltaY >= __imageBox.y)
792 __cropBox.y += deltaY;
794 __cropBox.width = width;
795 __cropBox.height = __cropBox.height - (2 * deltaY);
799 else if (__pointLocation == INSIDE_CROPBOX)
801 if ((__cropBox.x + deltaX) > __imageBox.x)
803 __cropBox.x += deltaX;
807 __cropBox.x = __imageBox.x;
809 if ((__cropBox.y + deltaY) > __imageBox.y)
811 __cropBox.y += deltaY;
815 __cropBox.y = __imageBox.y;
817 if ((__cropBox.x + __cropBox.width + deltaX) > (__imageBox.x + __imageBox.width))
819 __cropBox.x = __imageBox.x + __imageBox.width - __cropBox.width;
821 if ((__cropBox.y + __cropBox.height + deltaY) > (__imageBox.y + __imageBox.height))
823 __cropBox.y = __imageBox.y + __imageBox.height - __cropBox.height;
827 RequestRedraw(false);
828 __touchStart = currentPosition;
829 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
833 ImageCropForm::OnTouchReleased(const Tizen::Ui::Control& source, const Tizen::Graphics::Point& currentPosition, const Tizen::Ui::TouchEventInfo& touchInfo)
835 AppLogDebug("ENTER");
836 __pointLocation = OUTSIDE_CROPBOX;
837 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
841 ImageCropForm::CheckCurrentPosition(const Point currentPosition)
843 AppLogDebug("ENTER");
844 if (currentPosition.x > ((__cropBox.x + __cropBox.width / 2) - (H_CROP_RECTANGLE / 2)) && currentPosition.x < ((__cropBox.x + __cropBox.width / 2) + (H_CROP_RECTANGLE / 2))
845 && currentPosition.y > (__cropBox.y - (H_CROP_RECTANGLE / 2)) && currentPosition.y < (__cropBox.y + (H_CROP_RECTANGLE / 2)))
847 __pointLocation = INSIDE_TOP_RECTANGLE;
849 else if (currentPosition.x > (__cropBox.x - (H_CROP_RECTANGLE / 2)) && currentPosition.x < (__cropBox.x + (H_CROP_RECTANGLE / 2))
850 && currentPosition.y > ((__cropBox.y + __cropBox.height / 2) - (H_CROP_RECTANGLE / 2)) && currentPosition.y < ((__cropBox.y + __cropBox.height / 2) + (H_CROP_RECTANGLE / 2)))
852 __pointLocation = INSIDE_LEFT_RECTANGLE;
854 else if (currentPosition.x > (__cropBox.x + __cropBox.width - (H_CROP_RECTANGLE / 2)) && currentPosition.x < (__cropBox.x + __cropBox.width + (H_CROP_RECTANGLE / 2))
855 && currentPosition.y > ((__cropBox.y + __cropBox.height / 2) - (H_CROP_RECTANGLE / 2)) && currentPosition.y < ((__cropBox.y + __cropBox.height / 2) + (H_CROP_RECTANGLE / 2)))
857 __pointLocation = INSIDE_RIGHT_RECTANGLE;
859 else if (currentPosition.x > ((__cropBox.x + __cropBox.width / 2) - (H_CROP_RECTANGLE / 2)) && currentPosition.x < ((__cropBox.x + __cropBox.width / 2) + (H_CROP_RECTANGLE / 2))
860 && currentPosition.y > (__cropBox.y + __cropBox.height - (H_CROP_RECTANGLE / 2)) && currentPosition.y < (__cropBox.y + __cropBox.height + (H_CROP_RECTANGLE / 2)))
862 __pointLocation = INSIDE_BOTTOM_RECTANGLE;
864 else if (currentPosition.x > (__cropBox.x - (H_CROP_RECTANGLE / 2)) && currentPosition.x < (__cropBox.x + (H_CROP_RECTANGLE / 2))
865 && currentPosition.y > (__cropBox.y - (H_CROP_RECTANGLE / 2)) && currentPosition.y < (__cropBox.y +(H_CROP_RECTANGLE / 2)))
867 __pointLocation = INSIDE_TOP_LEFT_RECTANGLE;
869 else if (currentPosition.x > ((__cropBox.x + __cropBox.width / 2) - (H_CROP_RECTANGLE / 2)) && currentPosition.x < (__cropBox.x + __cropBox.width + (H_CROP_RECTANGLE / 2))
870 && currentPosition.y > (__cropBox.y - (H_CROP_RECTANGLE / 2)) && currentPosition.y < (__cropBox.y +(H_CROP_RECTANGLE / 2)))
872 __pointLocation = INSIDE_TOP_RIGHT_RECTANGLE;
874 else if (currentPosition.x > (__cropBox.x + __cropBox.width - (H_CROP_RECTANGLE / 2)) && currentPosition.x < (__cropBox.x + __cropBox.width + (H_CROP_RECTANGLE / 2))
875 && currentPosition.y > (__cropBox.y + __cropBox.height - (H_CROP_RECTANGLE / 2)) && currentPosition.y < (__cropBox.y + __cropBox.height + (H_CROP_RECTANGLE / 2)))
877 __pointLocation = INSIDE_BOTTOM_RIGHT_RECTANGLE;
879 else if (currentPosition.x > (__cropBox.x - (H_CROP_RECTANGLE / 2)) && currentPosition.x < (__cropBox.x + (H_CROP_RECTANGLE / 2))
880 && currentPosition.y > (__cropBox.y + __cropBox.height - (H_CROP_RECTANGLE / 2)) && currentPosition.y < (__cropBox.y + __cropBox.height + (H_CROP_RECTANGLE / 2)))
882 __pointLocation = INSIDE_BOTTOM_LEFT_RECTANGLE;
884 else if (currentPosition.x > __cropBox.x && currentPosition.x < (__cropBox.x + __cropBox.width) && currentPosition.y > __cropBox.y && currentPosition.y < (__cropBox.y + __cropBox.height))
886 __pointLocation = INSIDE_CROPBOX;
890 __pointLocation = OUTSIDE_CROPBOX;
892 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
897 ImageCropForm::SetValue(void)
899 int prevHeight = __imageBox.height;
900 int prevWidth = __imageBox.width;
901 int prevImageboxX = __imageBox.x;
902 int prevImageBoxY = __imageBox.y;
903 if (GetOrientationStatus() == ORIENTATION_STATUS_PORTRAIT)
905 __formHeight = Form::GetBounds().height;
906 __formWidth = Form::GetBounds().width;
908 else if (GetOrientationStatus() == ORIENTATION_STATUS_LANDSCAPE || GetOrientationStatus() == ORIENTATION_STATUS_LANDSCAPE_REVERSE)
910 __formHeight = Form::GetBounds().width;
911 __formWidth = Form::GetBounds().height;
914 float clientAreaRatio = (GetClientAreaBoundsF().height - H_FOOTER_PANEL) / GetClientAreaBoundsF().width;
915 if (__imageWidth >= GetClientAreaBounds().width || __imageHeight > (GetClientAreaBounds().height - H_FOOTER_PANEL))
917 float imageRatio = __imageHeight / (float) __imageWidth;
918 if (imageRatio < clientAreaRatio)
920 __imageBox.width = GetClientAreaBounds().width;
921 __imageBox.height = (__imageHeight * __imageBox.width) / __imageWidth;
922 __imageBox.x = GetClientAreaBounds().x;
923 __imageBox.y = GetClientAreaBounds().y + ((GetClientAreaBounds().height - H_FOOTER_PANEL) - __imageBox.height) / 2;
927 __imageBox.height = GetClientAreaBounds().height - H_FOOTER_PANEL;
928 __imageBox.width = (__imageWidth * __imageBox.height) / __imageHeight;
929 __imageBox.x = (GetClientAreaBounds().width - __imageBox.width) / 2;
930 __imageBox.y = GetClientAreaBounds().y;
935 __imageBox.height = __imageHeight;
936 __imageBox.width = __imageWidth;
937 __imageBox.x = (GetClientAreaBounds().width - __imageBox.width) / 2;
938 __imageBox.y = (GetClientAreaBounds().height - __imageBox.height) / 2 ;
940 if (__isOrientationChanged == false)
942 if (__imageBox.height > __imageBox.width)
944 __cropBox.width = (2 * __imageBox.width) / 3;
945 __cropBox.height = (__formHeight * __cropBox.width / __formWidth);
946 if (__cropBox.height > __imageBox.height)
948 __cropBox.height = (2 * __imageBox.height) / 3;
949 __cropBox.width = (__cropBox.height * __formWidth) / __formHeight;
951 __cropBox.y = __imageBox.y + (__imageBox.height - __cropBox.height) / 2;
952 __cropBox.x = __imageBox.x + (__imageBox.width - __cropBox.width) / 2;
956 __cropBox.height = (2 *__imageBox.height) / 3;
957 __cropBox.width = (__formWidth * __cropBox.height / __formHeight);
958 if (__cropBox.width > __imageBox.width)
960 __cropBox.width = (2 * __imageBox.width) / 3;
961 __cropBox.height = (__formHeight * __cropBox.width) / __formWidth;
963 __cropBox.y = __imageBox.y + (__imageBox.height - __cropBox.height) / 2;
964 __cropBox.x = __imageBox.x + (__imageBox.width - __cropBox.width) / 2;
969 float temp = __imageBox.x + ((__cropBox.x - prevImageboxX) * __imageBox.width) / (float)prevWidth;
970 __cropBox.x = (temp + 0.5);
971 temp = __imageBox.y + ((__cropBox.y - prevImageBoxY) * __imageBox.height) / (float)prevHeight;
972 __cropBox.y = (temp + 0.5);
973 temp = (__cropBox.height * __imageBox.height) / (float)prevHeight;
974 __cropBox.height = (temp + 0.5);
975 temp = (__cropBox.width * __imageBox.width) / (float)prevWidth;
976 __cropBox.width = (temp + 0.5);
978 __pCanvas = GetCanvasN();
979 __pCanvas->SetForegroundColor(CROP_BOX_RECTANGLE_COLOR);
982 void ImageCropForm::OnFormFileEventOccuered(const int index, const unsigned long eventId)
984 AppLogDebug(" ENTER");
985 UiApp::GetInstance()->Terminate();
986 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));