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 unsigned int UNCROPPED_IMAGE_COLOR = Color32<139, 137, 137, 100>::Value;
45 static const int CROP_BOX_LINE_WIDTH = 5;
47 ImageCropForm::ImageCropForm(void)
50 , __isOrientationChanged(false)
52 , __pCurrentBitmap(null)
53 , __pRectangleBitmap(null)
54 , __pointLocation(OUTSIDE_CROPBOX)
55 , __pPresentationModel(null)
59 ImageCropForm::~ImageCropForm(void)
61 if (__pCurrentBitmap != null)
63 delete __pCurrentBitmap;
65 if (__pRectangleBitmap != null)
67 delete __pRectangleBitmap;
69 if (__pCanvas != null)
76 ImageCropForm::Initialize(void)
78 Form::Construct(IDL_FORM_IMAGE_CROP);
79 Form::SetFormStyle(FORM_STYLE_NORMAL | FORM_STYLE_INDICATOR);
81 __pPresentationModel = ImageViewerPresentationModel::GetInstance();
87 ImageCropForm::OnInitializing(void)
91 AppResource* pAppResource = null;
92 Bitmap* pSaveButtonBitmap = null;
93 Bitmap* pCancelButtonBitmap = null;
95 Button* pSaveButton = null;
96 Button* pCancelButton = null;
97 pAppResource = UiApp::App::GetInstance()->GetAppResource();
98 if (pAppResource != null)
100 pSaveButtonBitmap = pAppResource->GetBitmapN(IDB_IMAGE_CROP_FORM_SAVE_ICON);
101 pCancelButtonBitmap = pAppResource->GetBitmapN(IDB_IMAGE_CROP_FORM_CANCEL_ICON);
102 __pRectangleBitmap = pAppResource->GetBitmapN(IDB_IMAGE_CROP_RECTANGLE);
105 pPanel = static_cast<Panel*>(GetControl(L"IDC_FOOTER_PANEL"));
108 pSaveButton = static_cast<Button*>(pPanel->GetControl(L"IDC_SAVE_BUTTON"));
109 if (pSaveButton != null && pSaveButtonBitmap != null)
111 Point startPoint((pSaveButton->GetWidth() - pSaveButtonBitmap->GetWidth()) / 2,
112 (pSaveButton->GetHeight() - pSaveButtonBitmap->GetHeight()) / 2);
113 pSaveButton->SetNormalBitmap(startPoint, *pSaveButtonBitmap);
114 pSaveButton->AddActionEventListener(*this);
115 pSaveButton->SetActionId(IDA_BUTTON_SAVE);
116 delete pSaveButtonBitmap;
119 pCancelButton = static_cast<Button*>(pPanel->GetControl(L"IDC_CANCEL_BUTTON"));
120 if (pCancelButton != null && pCancelButtonBitmap != null)
122 Point startPoint((pCancelButton->GetWidth() - pCancelButtonBitmap->GetWidth()) / 2,
123 (pCancelButton->GetHeight() - pCancelButtonBitmap->GetHeight()) / 2);
124 pCancelButton->SetNormalBitmap(startPoint, *pCancelButtonBitmap);
125 pCancelButton->AddActionEventListener(*this);
126 pCancelButton->SetActionId(IDA_BUTTON_CANCEL);
127 delete pCancelButtonBitmap;
129 SetControlAlwaysOnTop(*pPanel, true);
130 Label* pLabel = static_cast<Label*>(pPanel->GetControl(L"IDC_PANEL_LABEL"));
133 SetControlAlwaysAtBottom(*pLabel, true);
136 AddTouchEventListener(*this);
137 AddOrientationEventListener(*this);
138 __pPresentationModel->AddFileUpdateListener(this);
144 ImageCropForm::OnSceneActivatedN(const SceneId& previousSceneId, const SceneId& currentSceneId, IList* pArgs)
146 AppLogDebug("ENTER pArgs(%x)", pArgs);
149 IEnumerator* pEnum = pArgs->GetEnumeratorN();
150 if (pEnum->MoveNext() != E_SUCCESS)
154 AppLogDebug("EXIT 1(%s)", GetErrorMessage(GetLastResult()));
158 __sourceFilePath = *(static_cast<String*>(pEnum->GetCurrent()));
159 if (__sourceFilePath.IsEmpty() == false)
161 result r = __imageBuffer.Construct(__sourceFilePath.GetPointer());
164 __imageWidth = __imageBuffer.GetWidth();
165 __imageHeight = __imageBuffer.GetHeight();
166 __pCurrentBitmap = __imageBuffer.GetBitmapN(BITMAP_PIXEL_FORMAT_RGB565, BUFFER_SCALING_AUTO);
169 __imageFormat = img.GetImageFormat(__sourceFilePath);
171 if (pEnum->MoveNext() != E_SUCCESS)
175 AppLogDebug("EXIT 2(%s)", GetErrorMessage(GetLastResult()));
179 __cropMode = *(static_cast<String*>(pEnum->GetCurrent()));
180 if (__cropMode.IsEmpty() == false)
182 __cropMode.ToLowerCase();
183 if (__cropMode == "auto")
190 else if (__cropMode == "fit-to-screen")
192 if (pEnum->MoveNext() != E_SUCCESS)
196 AppLogDebug("EXIT 3(%s)", GetErrorMessage(GetLastResult()));
200 Integer *pStatus = static_cast<Integer*>(pEnum->GetCurrent());
203 __statusValue = pStatus->ToInt();
205 if (__statusValue == SET_AT_TYPE_CALLER_IMAGE)
207 if (pEnum->MoveNext() != E_SUCCESS)
211 AppLogDebug("EXIT 4(%s)", GetErrorMessage(GetLastResult()));
216 LongLong* pContentId = static_cast<LongLong*>(pEnum->GetCurrent());
217 if (pContentId != null)
219 __contentId = pContentId->ToLongLong();
229 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
233 ImageCropForm::OnSceneDeactivated(const SceneId& currentSceneId, const SceneId& nextSceneId)
235 AppLogDebug("ENTER");
236 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
240 ImageCropForm::OnDraw(void)
242 result r = E_SUCCESS;
243 AppLogDebug("ENTER");
244 if (__pCanvas != null)
246 r = __pCanvas->SetLineWidth(CROP_BOX_LINE_WIDTH);
247 r = __pCanvas->DrawBitmap(Rectangle(__imageBox.x, __imageBox.y, __imageBox.width, __imageBox.height), *__pCurrentBitmap);
248 r = __pCanvas->DrawRectangle(Rectangle(__cropBox.x, __cropBox.y, __cropBox.width, __cropBox.height));
249 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
250 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
251 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
252 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
253 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
254 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
255 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
256 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
257 r = __pCanvas->FillRectangle(UNCROPPED_IMAGE_COLOR, Rectangle(__imageBox.x, __imageBox.y, __imageBox.width, (__cropBox.y - __imageBox.y)));
258 r = __pCanvas->FillRectangle(UNCROPPED_IMAGE_COLOR, Rectangle(__imageBox.x, (__cropBox.y + __cropBox.height), __imageBox.width, ((__imageBox.y + __imageBox.height) - (__cropBox.y + __cropBox.height))));
259 r = __pCanvas->FillRectangle(UNCROPPED_IMAGE_COLOR, Rectangle(__imageBox.x, __cropBox.y, (__cropBox.x - __imageBox.x), __cropBox.height));
260 r = __pCanvas->FillRectangle(UNCROPPED_IMAGE_COLOR, Rectangle((__cropBox.x + __cropBox.width), __cropBox.y, ((__imageBox.x + __imageBox.width) - (__cropBox.x + __cropBox.width)), __cropBox.height));
267 ImageCropForm::OnActionPerformed(const Tizen::Ui::Control& source, int actionId)
269 SceneManager* pSceneManager = SceneManager::GetInstance();
272 case IDA_BUTTON_CANCEL:
273 pSceneManager->GoBackward(BackwardSceneTransition());
275 case IDA_BUTTON_SAVE:
277 if (__cropMode == "auto")
279 ImageBuffer* pCropBuffer = null;
283 String stringToInsert = L"_";
284 String delimiter = L".";
285 if (__sourceFilePath.IsEmpty() == false)
287 __sourceFilePath.Reverse();
288 __sourceFilePath.IndexOf(delimiter, 0, index);
289 __sourceFilePath.Reverse();
290 __sourceFilePath.Insert(stringToInsert, __sourceFilePath.GetLength() - index - 1);
291 destFilePath.Append(__sourceFilePath);
292 destFilePath.Insert(count, destFilePath.GetLength() - index - 1);
293 while (File::IsFileExist(destFilePath) == true)
296 destFilePath.Clear();
297 destFilePath.Append(__sourceFilePath);
298 destFilePath.Insert(count, destFilePath.GetLength() - index - 1);
300 pCropBuffer = __imageBuffer.CropN(((__cropBox.x - __imageBox.x) * __imageWidth) / __imageBox.width, ((__cropBox.y - __imageBox.y) * __imageHeight) / __imageBox.height,
301 (__cropBox.width * __imageWidth) / __imageBox.width, (__cropBox.height * __imageHeight) / __imageBox.height);
302 pCropBuffer->EncodeToFile(destFilePath, __imageFormat, true, 100);
303 ContentManager::ScanFile(destFilePath);
306 ArrayList* pList = new (std::nothrow) ArrayList(SingleObjectDeleter);
307 pList->Add(new (std::nothrow) String(destFilePath));
308 pSceneManager->GoBackward(BackwardSceneTransition(), pList);
310 else if(__cropMode == "fit-to-screen")
312 ImageBuffer* pCropBuffer = null;
313 result r = E_SUCCESS;
314 pCropBuffer = __imageBuffer.CropN(((__cropBox.x - __imageBox.x) * __imageWidth) / __imageBox.width, ((__cropBox.y - __imageBox.y) * __imageHeight) / __imageBox.height,
315 (__cropBox.width * __imageWidth) / __imageBox.width, (__cropBox.height * __imageHeight) / __imageBox.height);
316 if (__statusValue == SET_AT_TYPE_HOME_SCREEN_WALLPAPER)
318 String destPath = App::GetInstance()->GetAppRootPath() + TEMP_FILE_PATH_HOME_SCREEN_WALLPAPER;
319 r = pCropBuffer->EncodeToFile(destPath, __imageFormat, true, 100);
322 r = SettingInfo::SetValue(SETTING_VALUE_HOME_SCREEN_WALLPAPER, destPath);
324 else if (r == E_OVERFLOW)
326 MessageBox messageBox;
327 messageBox.Construct(L"", L"File size is too big", MSGBOX_STYLE_NONE, 3000);
329 messageBox.ShowAndWait(modalResult);
332 else if (__statusValue == SET_AT_TYPE_LOCK_SCREEN_WALLPAPER)
334 String destPath = App::GetInstance()->GetAppRootPath() + TEMP_FILE_PATH_LOCK_SCREEN_WALLPAPER;
335 r = pCropBuffer->EncodeToFile(destPath, __imageFormat, true, 100);
338 r = SettingInfo::SetValue(SETTING_VALUE_LOCK_SCREEN_WALLPAPER, destPath);
340 else if (r == E_OVERFLOW)
342 MessageBox messageBox;
343 messageBox.Construct(L"", L"File size is too big", MSGBOX_STYLE_NONE, 3000);
345 messageBox.ShowAndWait(modalResult);
348 else if (__statusValue == SET_AT_TYPE_HOME_AND_LOCK_SCREEN_WALLPAPER)
350 String destPath = App::GetInstance()->GetAppRootPath() + TEMP_FILE_PATH_LOCK_SCREEN_WALLPAPER;
351 String destPathHome = App::GetInstance()->GetAppRootPath() + TEMP_FILE_PATH_HOME_SCREEN_WALLPAPER;
352 r = pCropBuffer->EncodeToFile(destPath, __imageFormat, true, 100);
353 r = pCropBuffer->EncodeToFile(destPathHome, __imageFormat, true, 100);
356 r = SettingInfo::SetValue(SETTING_VALUE_LOCK_SCREEN_WALLPAPER, destPath);
357 r = SettingInfo::SetValue(SETTING_VALUE_HOME_SCREEN_WALLPAPER, destPathHome);
359 else if (r == E_OVERFLOW)
361 MessageBox messageBox;
362 messageBox.Construct(L"", L"File size is too big", MSGBOX_STYLE_NONE, 3000);
364 messageBox.ShowAndWait(modalResult);
367 else if (__statusValue == SET_AT_TYPE_CALLER_IMAGE)
372 String stringToInsert = L"_";
373 String delimiter = L".";
374 if (__sourceFilePath.IsEmpty() == false)
376 __sourceFilePath.Reverse();
377 __sourceFilePath.IndexOf(delimiter, 0, index);
378 __sourceFilePath.Reverse();
379 __sourceFilePath.Insert(stringToInsert, __sourceFilePath.GetLength() - index - 1);
380 destFilePath.Append(__sourceFilePath);
381 destFilePath.Insert(count, destFilePath.GetLength() - index - 1);
382 while (File::IsFileExist(destFilePath) == true)
385 destFilePath.Clear();
386 destFilePath.Append(__sourceFilePath);
387 destFilePath.Insert(count, destFilePath.GetLength() - index - 1);
390 r = pCropBuffer->EncodeToFile(destFilePath, __imageFormat, true, 100);
391 ContentManager::ScanFile(destFilePath);
392 Contact* pContact = null;
393 AddressbookManager* pAddressbookManager = AddressbookManager::GetInstance();
394 Addressbook* pAddressbook = null;
396 pAddressbook = pAddressbookManager->GetAddressbookN(DEFAULT_ADDRESSBOOK_ID);
397 pContact = pAddressbook->GetContactN(__contentId);
398 pContact->SetThumbnail(destFilePath);
399 pAddressbook->UpdateContact(*pContact);
404 ArrayList* pList = new (std::nothrow) ArrayList(SingleObjectDeleter);
405 pList->Add(new (std::nothrow) String(destFilePath));
406 pSceneManager->GoBackward(BackwardSceneTransition(), pList);
411 pSceneManager->GoBackward(BackwardSceneTransition());
420 ImageCropForm::OnOrientationChanged(const Control &source, OrientationStatus orientationStatus)
422 AppLogDebug("ENTER");
423 __isOrientationChanged = true;
430 ImageCropForm::OnTouchPressed(const Tizen::Ui::Control& source, const Tizen::Graphics::Point& currentPosition, const Tizen::Ui::TouchEventInfo& touchInfo)
432 AppLogDebug("ENTER");
433 __touchStart = currentPosition;
434 CheckCurrentPosition(currentPosition);
439 ImageCropForm::OnTouchMoved(const Tizen::Ui::Control& source, const Tizen::Graphics::Point& currentPosition, const Tizen::Ui::TouchEventInfo& touchInfo)
441 AppLogDebug("ENTER");
442 int deltaX = currentPosition.x - __touchStart.x;
443 int deltaY = currentPosition.y - __touchStart.y;
444 if (__cropMode.Equals("auto", false))
446 switch(__pointLocation)
448 case INSIDE_TOP_RECTANGLE:
450 if ((__cropBox.y + deltaY) > __imageBox.y && deltaY < (__cropBox.height - (CROP_RECTANGLE_HEIGHT * 3)))
452 __cropBox.y += deltaY;
453 __cropBox.height -= deltaY;
458 case INSIDE_BOTTOM_RECTANGLE:
460 if ((__cropBox.y + __cropBox.height + deltaY) < (__imageBox.y + __imageBox.height)
461 && (__cropBox.height + deltaY) > (CROP_RECTANGLE_HEIGHT * 3))
463 __cropBox.height += deltaY;
468 case INSIDE_LEFT_RECTANGLE:
470 if ((__cropBox.x + deltaX) > __imageBox.x && deltaX < __cropBox.width - (CROP_RECTANGLE_HEIGHT * 3))
472 __cropBox.x += deltaX;
473 __cropBox.width -= deltaX;
478 case INSIDE_RIGHT_RECTANGLE:
480 if ((__cropBox.x + __cropBox.width + deltaX) < (__imageBox.x + __imageBox.width)
481 && __cropBox.width + deltaX > (CROP_RECTANGLE_HEIGHT * 3))
483 __cropBox.width += deltaX;
488 case INSIDE_TOP_LEFT_RECTANGLE:
490 if ((__cropBox.y + deltaY) > __imageBox.y && deltaY < (__cropBox.height - (CROP_RECTANGLE_HEIGHT * 3))
491 && (__cropBox.x + deltaX) > __imageBox.x && deltaX < __cropBox.width - (CROP_RECTANGLE_HEIGHT * 3))
493 __cropBox.y += deltaY;
494 __cropBox.height -= deltaY;
495 __cropBox.x += deltaX;
496 __cropBox.width -= deltaX;
501 case INSIDE_TOP_RIGHT_RECTANGLE:
503 if ((__cropBox.y + deltaY) > __imageBox.y && deltaY < (__cropBox.height - (CROP_RECTANGLE_HEIGHT * 3))
504 && (__cropBox.x + __cropBox.width + deltaX) < (__imageBox.x + __imageBox.width) && __cropBox.width + deltaX > (CROP_RECTANGLE_HEIGHT * 3))
506 __cropBox.width += deltaX;
507 __cropBox.y += deltaY;
508 __cropBox.height -= deltaY;
513 case INSIDE_BOTTOM_LEFT_RECTANGLE:
515 if ((__cropBox.y + __cropBox.height + deltaY) < (__imageBox.y + __imageBox.height) && (__cropBox.height + deltaY) > (CROP_RECTANGLE_HEIGHT * 3)
516 && (__cropBox.x + deltaX) > __imageBox.x && deltaX < __cropBox.width - (CROP_RECTANGLE_HEIGHT * 3))
518 __cropBox.x += deltaX;
519 __cropBox.width -= deltaX;
520 __cropBox.height += deltaY;
525 case INSIDE_BOTTOM_RIGHT_RECTANGLE:
527 if ((__cropBox.x + __cropBox.width + deltaX) < (__imageBox.x + __imageBox.width) && __cropBox.width + deltaX > (CROP_RECTANGLE_HEIGHT * 3)
528 && (__cropBox.y + __cropBox.height + deltaY) < (__imageBox.y + __imageBox.height) && (__cropBox.height + deltaY) > (CROP_RECTANGLE_HEIGHT * 3))
530 __cropBox.width += deltaX;
531 __cropBox.height += deltaY;
538 if ((__cropBox.x + deltaX) > __imageBox.x)
540 __cropBox.x += deltaX;
544 __cropBox.x = __imageBox.x;
546 if ((__cropBox.y + deltaY) > __imageBox.y)
548 __cropBox.y += deltaY;
552 __cropBox.y = __imageBox.y;
554 if ((__cropBox.x + __cropBox.width + deltaX) > (__imageBox.x + __imageBox.width))
556 __cropBox.x = __imageBox.x + __imageBox.width - __cropBox.width;
558 if ((__cropBox.y + __cropBox.height + deltaY) > (__imageBox.y + __imageBox.height))
560 __cropBox.y = __imageBox.y + __imageBox.height - __cropBox.height;
569 else if (__cropMode != "auto")
571 if (__pointLocation == INSIDE_LEFT_RECTANGLE || __pointLocation == INSIDE_BOTTOM_LEFT_RECTANGLE
572 || __pointLocation == INSIDE_TOP_LEFT_RECTANGLE)
574 int temp = __cropBox.height;
575 int height = (__formHeight * (__cropBox.width - (2 * deltaX))) /__formWidth;
576 temp = height - temp;
577 if ((__cropBox.width - (2 * deltaX) > (CROP_RECTANGLE_HEIGHT * 3)) && (!(height >= __imageBox.height || (__cropBox.width - (2 * deltaX)) >= __imageBox.width)))
579 if (__cropBox.x + deltaX >= __imageBox.x && __cropBox.x + __cropBox.width - deltaX <= __imageBox.x + __imageBox.width
580 && __cropBox.y - (temp / 2) >= __imageBox.y && __cropBox.y + __cropBox.height + (temp / 2) < __imageBox.y + __imageBox.height
581 && (__cropBox.width - (2 * deltaX) > (CROP_RECTANGLE_HEIGHT * 3)))
583 __cropBox.x += deltaX;
584 __cropBox.width = __cropBox.width - (2 * deltaX);
585 __cropBox.height = height;
586 __cropBox.y = __cropBox.y - (temp / 2);
588 else if ((__cropBox.y + __cropBox.height) <= (__imageBox.y + __imageBox.height)
589 && __cropBox.width < __imageBox.width
590 && __cropBox.x + deltaX >= __imageBox.x && __cropBox.y - temp >= __imageBox.y )
592 if (__cropBox.x + __cropBox.width - deltaX <= __imageBox.x + __imageBox.width
593 && __cropBox.x + deltaX >= __imageBox.x)
595 __cropBox.x += deltaX;
599 __cropBox.x = __cropBox.x + (2 * deltaX);
601 __cropBox.width = __cropBox.width - (2 * deltaX);
602 __cropBox.height = height;
603 __cropBox.y = __cropBox.y - temp;
605 else if (__cropBox.y - (temp / 2) <= __imageBox.y && __cropBox.width - (deltaX) <= __imageBox.width
606 && __cropBox.x + deltaX >= __imageBox.x && __cropBox.y + __cropBox.height + temp <= __imageBox.y + __imageBox.height)
608 if (__cropBox.x + __cropBox.width - deltaX <= __imageBox.x + __imageBox.width)
610 __cropBox.x += deltaX;
614 __cropBox.x = __cropBox.x + (2 * deltaX);
616 __cropBox.width = __cropBox.width - (2 * deltaX);
617 __cropBox.height = height;
621 else if (__pointLocation == INSIDE_RIGHT_RECTANGLE || __pointLocation == INSIDE_BOTTOM_RIGHT_RECTANGLE
622 || __pointLocation == INSIDE_TOP_RIGHT_RECTANGLE)
626 int temp = __cropBox.height;
627 int height = (__formHeight * (__cropBox.width - (2 * deltaX))) / __formWidth;
628 temp = height - temp;
629 if ((__cropBox.width - (2 * deltaX) > (CROP_RECTANGLE_HEIGHT * 3)) && (!(height >= __imageBox.height || (__cropBox.width - (2 * deltaX)) >= __imageBox.width)))
631 if (deltaX < __cropBox.width - (CROP_RECTANGLE_HEIGHT * 3))
633 if (__cropBox.x + deltaX > __imageBox.x && __cropBox.x + __cropBox.width - deltaX <= __imageBox.x + __imageBox.width
634 && __cropBox.y - (temp / 2) > __imageBox.y && __cropBox.y + __cropBox.height + (temp / 2) < __imageBox.y + __imageBox.height)
636 __cropBox.x += deltaX;
637 __cropBox.width = __cropBox.width - (2 * deltaX);;
638 __cropBox.height = height;
639 __cropBox.y = __cropBox.y - (temp / 2);
641 else if ((__cropBox.y + __cropBox.height + (temp / 2)) >= (__imageBox.y + __imageBox.height)
642 && __cropBox.x + __cropBox.width - (2 * deltaX) <= __imageBox.x + __imageBox.width
643 && __cropBox.x - deltaX >= __imageBox.x && __cropBox.y - temp >= __imageBox.y )
645 if (__cropBox.x + __cropBox.width - deltaX <= __imageBox.x + __imageBox.width
646 && __cropBox.x + deltaX >= __imageBox.x)
648 __cropBox.x += deltaX;
652 __cropBox.width = __cropBox.width - (2 * deltaX);
654 __cropBox.width = __cropBox.width - (2 * deltaX);
655 __cropBox.height = height;
656 __cropBox.y = __cropBox.y - temp;
658 else if (__cropBox.y - (temp / 2) <= __imageBox.y && __cropBox.width - (deltaX) <= __imageBox.width
659 && __cropBox.x + deltaX <= __imageBox.x && __cropBox.y + __cropBox.height + temp <= __imageBox.y + __imageBox.height)
661 if (__cropBox.x + deltaX >= __imageBox.x)
663 __cropBox.x += deltaX;
667 __cropBox.x = __imageBox.x;
669 __cropBox.width = __cropBox.width - (2 * deltaX);
670 __cropBox.height = height;
672 else if (__cropBox.y - (temp / 2) <= __imageBox.y && __cropBox.x + deltaX >= __imageBox.x
673 && __cropBox.x + __cropBox.width - deltaX <= __imageBox.x + __imageBox.width
674 && __cropBox.y + __cropBox.height + temp <= __imageBox.y + __imageBox.height)
676 __cropBox.x += deltaX;
677 __cropBox.width = __cropBox.width - (2 * deltaX);
678 __cropBox.height = height;
680 else if (__cropBox.x + deltaX <= __imageBox.x && __cropBox.y - (temp / 2) >= __imageBox.y
681 && __cropBox.x + __cropBox.width - deltaX <= __imageBox.x + __imageBox.width
682 && __cropBox.y + __cropBox.height + temp <= __imageBox.y + __imageBox.height)
684 __cropBox.width = __cropBox.width - (2 * deltaX);
685 __cropBox.y = __cropBox.y - (temp / 2);
686 __cropBox.height = height;
691 else if (__pointLocation == INSIDE_TOP_RECTANGLE)
693 int temp = __cropBox.width;
694 int width = (__formWidth * (__cropBox.height - (2 * deltaY))) / __formHeight;
696 if (__cropBox.height - (2 * deltaY) > (__formHeight * (CROP_RECTANGLE_HEIGHT * 3)) / __formWidth && (!(__cropBox.height - (2 * deltaY) >= __imageBox.height || width >= __imageBox.width)))
698 if (__cropBox.x - (temp /2 ) > __imageBox.x && __cropBox.x + __cropBox.width + temp < __imageBox.x + __imageBox.width
699 && __cropBox.y + (deltaY) > __imageBox.y && __cropBox.y + __cropBox.height - (2 * deltaY) < __imageBox.y + __imageBox.height)
701 __cropBox.y += deltaY;
702 __cropBox.height = __cropBox.height - (2 * deltaY);
703 __cropBox.width = width;
704 __cropBox.x = __cropBox.x - (temp / 2);
706 else if (__cropBox.x - (temp / 2) < __imageBox.x)
708 if (__cropBox.x + __cropBox.width + temp <= __imageBox.x + __imageBox.width)
710 __cropBox.width = width;
712 if (__cropBox.y + deltaY >= __imageBox.y)
714 __cropBox.y += deltaY;
716 if (__cropBox.y + __cropBox.height - (2 * deltaY) < __imageBox.y + __imageBox.height)
718 __cropBox.height = __cropBox.height - (2 * deltaY);
721 else if (__cropBox.y + __cropBox.height - (2 * deltaY) >= __imageBox.x + __imageBox.height
722 && __cropBox.y + (2 * deltaY) >= __imageBox.y)
724 if (__cropBox.x - temp >= __imageBox.x
725 && __cropBox.x + __cropBox.width + temp > __imageBox.x + __imageBox.width)
727 __cropBox.x = __cropBox.x - temp;
729 else if (__cropBox.x - temp >= __imageBox.x)
731 __cropBox.x = __cropBox.x - temp / 2;
733 if (__cropBox.x + __cropBox.width + temp <= __imageBox.x + __imageBox.width)
735 __cropBox.width = width;
737 if (__cropBox.y + (2 * deltaY) >= __imageBox.y)
739 __cropBox.y = __cropBox.y + (2 * deltaY);
740 __cropBox.height = __cropBox.height - (2 * deltaY);
743 else if (__cropBox.x + __cropBox.width + (temp / 2) > __imageBox.x + __imageBox.width
744 && __cropBox.x - temp >= __imageBox.x && __cropBox.y + deltaY >= __imageBox.y
745 && __cropBox.y + __cropBox.height - (2 * deltaY) <= __imageBox.y + __imageBox.height)
747 __cropBox.x = __cropBox.x - temp;
748 __cropBox.width = width;
749 __cropBox.y += deltaY;
750 __cropBox.height = __cropBox.height - (2 * deltaY);
754 else if (__pointLocation == INSIDE_BOTTOM_RECTANGLE)
756 deltaY = deltaY * (-1);
757 int temp = __cropBox.width;
758 int width = (__formWidth * (__cropBox.height - (2 * deltaY))) / __formHeight;
760 if (__cropBox.height - (2 * deltaY) > (__formHeight * (CROP_RECTANGLE_HEIGHT * 3)) / __formWidth && (!(__cropBox.height - (2 * deltaY) >= __imageBox.height || width >= __imageBox.width)))
762 if (__cropBox.x - (temp / 2) >= __imageBox.x && __cropBox.x + __cropBox.width + temp < __imageBox.x + __imageBox.width
763 && __cropBox.y + deltaY > __imageBox.y && __cropBox.y + __cropBox.height - (2 * deltaY) < __imageBox.y + __imageBox.height)
765 __cropBox.y += deltaY;
766 __cropBox.height = __cropBox.height - (2 * deltaY);;
767 __cropBox.width = width;
768 __cropBox.x = __cropBox.x - (temp / 2);
770 else if (__cropBox.y + deltaY < __imageBox.y && __cropBox.y + __cropBox.height - (2 * deltaY) < __imageBox.y + __imageBox.height
771 && __cropBox.x - (temp / 2) > __imageBox.x && __cropBox.x + __cropBox.width + temp < __imageBox.x + __imageBox.width)
773 __cropBox.x = __cropBox.x - (temp / 2);
774 __cropBox.width = width;
775 __cropBox.height = __cropBox.height - (2 * deltaY);
777 else if (__cropBox.x + __cropBox.width + temp > __imageBox.x + __imageBox.width
778 && __cropBox.y + deltaY >= __imageBox.y && __cropBox.y + __cropBox.height + temp <= __imageBox.y + __imageBox.height
779 && __cropBox.x - temp >= __imageBox.x)
781 __cropBox.x = __cropBox.x - temp;
782 __cropBox.y += deltaY;
783 __cropBox.width = width;
784 __cropBox.height = __cropBox.height - (2 * deltaY);
786 else if (__cropBox.x + __cropBox.width + temp >= __imageBox.x + __imageBox.width
787 && __cropBox.y + deltaY <= __imageBox.y && __cropBox.x - temp > __imageBox.x
788 && __cropBox.y + __cropBox.height + temp < __imageBox.y + __imageBox.height)
791 __cropBox.width = width;
792 __cropBox.height = __cropBox.height - (2 * deltaY);
794 else if (__cropBox.x - temp < __imageBox.x && __cropBox.x + __cropBox.width + temp <= __imageBox.x + __imageBox.width
795 && __cropBox.y + __cropBox.height - (2 * deltaY) <= __imageBox.y + __imageBox.height)
797 if (__cropBox.y + deltaY >= __imageBox.y)
799 __cropBox.y += deltaY;
801 __cropBox.width = width;
802 __cropBox.height = __cropBox.height - (2 * deltaY);
806 else if (__pointLocation == INSIDE_CROPBOX)
808 if ((__cropBox.x + deltaX) > __imageBox.x)
810 __cropBox.x += deltaX;
814 __cropBox.x = __imageBox.x;
816 if ((__cropBox.y + deltaY) > __imageBox.y)
818 __cropBox.y += deltaY;
822 __cropBox.y = __imageBox.y;
824 if ((__cropBox.x + __cropBox.width + deltaX) > (__imageBox.x + __imageBox.width))
826 __cropBox.x = __imageBox.x + __imageBox.width - __cropBox.width;
828 if ((__cropBox.y + __cropBox.height + deltaY) > (__imageBox.y + __imageBox.height))
830 __cropBox.y = __imageBox.y + __imageBox.height - __cropBox.height;
834 RequestRedraw(false);
835 __touchStart = currentPosition;
840 ImageCropForm::OnTouchReleased(const Tizen::Ui::Control& source, const Tizen::Graphics::Point& currentPosition, const Tizen::Ui::TouchEventInfo& touchInfo)
842 AppLogDebug("ENTER");
843 __pointLocation = OUTSIDE_CROPBOX;
848 ImageCropForm::CheckCurrentPosition(const Point currentPosition)
850 AppLogDebug("ENTER");
851 if (currentPosition.x > ((__cropBox.x + __cropBox.width / 2) - (CROP_RECTANGLE_HEIGHT / 2)) && currentPosition.x < ((__cropBox.x + __cropBox.width / 2) + (CROP_RECTANGLE_HEIGHT / 2))
852 && currentPosition.y > (__cropBox.y - (CROP_RECTANGLE_HEIGHT / 2)) && currentPosition.y < (__cropBox.y + (CROP_RECTANGLE_HEIGHT / 2)))
854 __pointLocation = INSIDE_TOP_RECTANGLE;
856 else if (currentPosition.x > (__cropBox.x - (CROP_RECTANGLE_HEIGHT / 2)) && currentPosition.x < (__cropBox.x + (CROP_RECTANGLE_HEIGHT / 2))
857 && currentPosition.y > ((__cropBox.y + __cropBox.height / 2) - (CROP_RECTANGLE_HEIGHT / 2)) && currentPosition.y < ((__cropBox.y + __cropBox.height / 2) + (CROP_RECTANGLE_HEIGHT / 2)))
859 __pointLocation = INSIDE_LEFT_RECTANGLE;
861 else if (currentPosition.x > (__cropBox.x + __cropBox.width - (CROP_RECTANGLE_HEIGHT / 2)) && currentPosition.x < (__cropBox.x + __cropBox.width + (CROP_RECTANGLE_HEIGHT / 2))
862 && currentPosition.y > ((__cropBox.y + __cropBox.height / 2) - (CROP_RECTANGLE_HEIGHT / 2)) && currentPosition.y < ((__cropBox.y + __cropBox.height / 2) + (CROP_RECTANGLE_HEIGHT / 2)))
864 __pointLocation = INSIDE_RIGHT_RECTANGLE;
866 else if (currentPosition.x > ((__cropBox.x + __cropBox.width / 2) - (CROP_RECTANGLE_HEIGHT / 2)) && currentPosition.x < ((__cropBox.x + __cropBox.width / 2) + (CROP_RECTANGLE_HEIGHT / 2))
867 && currentPosition.y > (__cropBox.y + __cropBox.height - (CROP_RECTANGLE_HEIGHT / 2)) && currentPosition.y < (__cropBox.y + __cropBox.height + (CROP_RECTANGLE_HEIGHT / 2)))
869 __pointLocation = INSIDE_BOTTOM_RECTANGLE;
871 else if (currentPosition.x > (__cropBox.x - (CROP_RECTANGLE_HEIGHT / 2)) && currentPosition.x < (__cropBox.x + (CROP_RECTANGLE_HEIGHT / 2))
872 && currentPosition.y > (__cropBox.y - (CROP_RECTANGLE_HEIGHT / 2)) && currentPosition.y < (__cropBox.y +(CROP_RECTANGLE_HEIGHT / 2)))
874 __pointLocation = INSIDE_TOP_LEFT_RECTANGLE;
876 else if (currentPosition.x > ((__cropBox.x + __cropBox.width / 2) - (CROP_RECTANGLE_HEIGHT / 2)) && currentPosition.x < (__cropBox.x + __cropBox.width + (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_RIGHT_RECTANGLE;
881 else if (currentPosition.x > (__cropBox.x + __cropBox.width - (CROP_RECTANGLE_HEIGHT / 2)) && currentPosition.x < (__cropBox.x + __cropBox.width + (CROP_RECTANGLE_HEIGHT / 2))
882 && currentPosition.y > (__cropBox.y + __cropBox.height - (CROP_RECTANGLE_HEIGHT / 2)) && currentPosition.y < (__cropBox.y + __cropBox.height + (CROP_RECTANGLE_HEIGHT / 2)))
884 __pointLocation = INSIDE_BOTTOM_RIGHT_RECTANGLE;
886 else if (currentPosition.x > (__cropBox.x - (CROP_RECTANGLE_HEIGHT / 2)) && currentPosition.x < (__cropBox.x + (CROP_RECTANGLE_HEIGHT / 2))
887 && currentPosition.y > (__cropBox.y + __cropBox.height - (CROP_RECTANGLE_HEIGHT / 2)) && currentPosition.y < (__cropBox.y + __cropBox.height + (CROP_RECTANGLE_HEIGHT / 2)))
889 __pointLocation = INSIDE_BOTTOM_LEFT_RECTANGLE;
891 else if (currentPosition.x > __cropBox.x && currentPosition.x < (__cropBox.x + __cropBox.width) && currentPosition.y > __cropBox.y && currentPosition.y < (__cropBox.y + __cropBox.height))
893 __pointLocation = INSIDE_CROPBOX;
897 __pointLocation = OUTSIDE_CROPBOX;
904 ImageCropForm::SetValue(void)
906 int prevHeight = __imageBox.height;
907 int prevWidth = __imageBox.width;
908 int prevImageboxX = __imageBox.x;
909 int prevImageBoxY = __imageBox.y;
910 if (GetOrientationStatus() == ORIENTATION_STATUS_PORTRAIT)
912 __formHeight = Form::GetBounds().height;
913 __formWidth = Form::GetBounds().width;
915 else if(GetOrientationStatus() == ORIENTATION_STATUS_LANDSCAPE || GetOrientationStatus() == ORIENTATION_STATUS_LANDSCAPE_REVERSE)
917 __formHeight = Form::GetBounds().width;
918 __formWidth = Form::GetBounds().height;
921 float clientAreaRatio = (GetClientAreaBoundsF().height - FOOTER_PANEL_HEIGHT) / GetClientAreaBoundsF().width;
922 if (__imageWidth >= GetClientAreaBounds().width || __imageHeight > (GetClientAreaBounds().height - FOOTER_PANEL_HEIGHT))
924 float imageRatio = __imageHeight / (float) __imageWidth;
925 if (imageRatio < clientAreaRatio)
927 __imageBox.width = GetClientAreaBounds().width;
928 __imageBox.height = (__imageHeight * __imageBox.width) / __imageWidth;
929 __imageBox.x = GetClientAreaBounds().x;
930 __imageBox.y = GetClientAreaBounds().y + ((GetClientAreaBounds().height - FOOTER_PANEL_HEIGHT) - __imageBox.height) / 2;
934 __imageBox.height = GetClientAreaBounds().height - FOOTER_PANEL_HEIGHT;
935 __imageBox.width = (__imageWidth * __imageBox.height) / __imageHeight;
936 __imageBox.x = (GetClientAreaBounds().width - __imageBox.width) / 2;
937 __imageBox.y = GetClientAreaBounds().y;
942 __imageBox.height = __imageHeight;
943 __imageBox.width = __imageWidth;
944 __imageBox.x = (GetClientAreaBounds().width - __imageBox.width) / 2;
945 __imageBox.y = (GetClientAreaBounds().height - __imageBox.height) / 2 ;
947 if (__isOrientationChanged == false)
949 if (__imageBox.height > __imageBox.width)
951 __cropBox.width = (2 * __imageBox.width) / 3;
952 __cropBox.height = (__formHeight * __cropBox.width / __formWidth);
953 if (__cropBox.height > __imageBox.height)
955 __cropBox.height = (2 * __imageBox.height) / 3;
956 __cropBox.width = (__cropBox.height * __formWidth) / __formHeight;
958 __cropBox.y = __imageBox.y + (__imageBox.height - __cropBox.height) / 2;
959 __cropBox.x = __imageBox.x + (__imageBox.width - __cropBox.width) / 2;
963 __cropBox.height = (2 *__imageBox.height) / 3;
964 __cropBox.width = (__formWidth * __cropBox.height / __formHeight);
965 if (__cropBox.width > __imageBox.width)
967 __cropBox.width = (2 * __imageBox.width) / 3;
968 __cropBox.height = (__formHeight * __cropBox.width) / __formWidth;
970 __cropBox.y = __imageBox.y + (__imageBox.height - __cropBox.height) / 2;
971 __cropBox.x = __imageBox.x + (__imageBox.width - __cropBox.width) / 2;
976 float temp = __imageBox.x + ((__cropBox.x - prevImageboxX) * __imageBox.width) / (float)prevWidth;
977 __cropBox.x = (temp + 0.5);
978 temp = __imageBox.y + ((__cropBox.y - prevImageBoxY) * __imageBox.height) / (float)prevHeight;
979 __cropBox.y = (temp + 0.5);
980 temp = (__cropBox.height * __imageBox.height) / (float)prevHeight;
981 __cropBox.height = (temp + 0.5);
982 temp = (__cropBox.width * __imageBox.width) / (float)prevWidth;
983 __cropBox.width = (temp + 0.5);
985 __pCanvas = GetCanvasN();
986 __pCanvas->SetForegroundColor(CROP_BOX_RECTANGLE_COLOR);
989 void ImageCropForm::OnFormFileEventOccuered(const int index, const unsigned long eventId)
991 AppLogDebug(" ENTER");
992 UiApp::GetInstance()->Terminate();
993 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));