2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 // Licensed under the Flora License, Version 1.1 (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 GlImageCrop.cpp
19 * @brief This is the implementation file for Image Crop.
26 #include "GlGalleryApp.h"
27 #include "GlImageCropForm.h"
28 #include "GlResourceManager.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::Text;
40 using namespace Tizen::Ui;
41 using namespace Tizen::Ui::Controls;
42 using namespace Tizen::Ui::Scenes;
44 static const unsigned int COLOR_GALLERY_BG = Color32<31,31,31>::Value;
45 static const unsigned int CROP_BOX_RECTANGLE_COLOR = Color32<199, 110, 6>::Value;
46 static const int CROP_BOX_LINE_WIDTH = 5;
47 static const int H_CROP_RECTANGLE = 20;
48 static const wchar_t* PATH_DATA_DIRECTORY_IN_CROP = L"data/";
49 static const wchar_t* FILE_NAME_SEPARATOR = L"_";
50 static const int MAX_FILE_LENGHT = 255;
51 static const int CROP_BOX_OFFSET = 20;
53 ImageCropForm::ImageCropForm(void)
54 : __sourceFilePath(L"")
55 , __cropMode(APPCONTROL_DATA_CROP_TYPE_FIT_TO_SCREEN)
57 , __pCurrentBitmap(null)
58 , __pRectangleBitmap(null)
59 , __imageFormat(IMG_FORMAT_NONE)
61 , __pointLocation(OUTSIDE_CROPBOX)
62 , __isOrientationChanged(false)
73 ImageCropForm::~ImageCropForm(void)
75 if (__pCurrentBitmap != null)
77 delete __pCurrentBitmap;
79 if (__pRectangleBitmap != null)
81 delete __pRectangleBitmap;
83 if (__pCanvas != null)
90 ImageCropForm::Initialize(void)
92 Form::Construct(IDL_FORM_IMAGE_CROP);
98 ImageCropForm::OnInitializing(void)
100 AppLogDebug("OnInitializing");
102 AppResource* pAppResource = null;
103 pAppResource = UiApp::App::GetInstance()->GetAppResource();
105 if (pAppResource != null)
107 __pRectangleBitmap = pAppResource->GetBitmapN(IDB_IMAGE_CROP_RECTANGLE);
110 Footer* pFooter = GetFooter();
113 pFooter->AddActionEventListener(*this);
114 pFooter->RemoveAllItems();
115 pFooter->SetStyle(FOOTER_STYLE_BUTTON_TEXT);
117 FooterItem footerItem1;
118 footerItem1.Construct(IDA_BUTTON_SAVE);
119 footerItem1.SetText(ResourceManager::GetString(L"IDS_COM_OPT_SAVE"));
121 pFooter->AddItem(footerItem1);
124 __pPresentationModel = FileListPresentationModel::GetInstance();
126 AddTouchEventListener(*this);
127 AddOrientationEventListener(*this);
128 SetFormBackEventListener(this);
134 ImageCropForm::OnDraw(void)
136 AppLogDebug("OnDraw");
137 result r = E_SUCCESS;
139 if (__pCanvas != null)
142 r = __pCanvas->SetLineWidth(CROP_BOX_LINE_WIDTH);
143 r = __pCanvas->DrawBitmap(Rectangle(__imageBox.x, __imageBox.y, __imageBox.width, __imageBox.height), *__pCurrentBitmap); // Image to be cropped
144 r = __pCanvas->DrawRectangle(Rectangle(__cropBox.x, __cropBox.y, __cropBox.width, __cropBox.height)); // Cropbox rectangle
145 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
146 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
147 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
148 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
149 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
150 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
151 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
152 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
153 __pCanvas->SetBackgroundColor(COLOR_GALLERY_BG);
160 ImageCropForm::OnFormBackRequested(Form& source)
162 AppLogDebug("OnFormBackRequested");
164 SceneManager::GetInstance()->GoBackward(BackwardSceneTransition());
168 ImageCropForm::OnActionPerformed(const Tizen::Ui::Control& source, int actionId)
170 AppLogDebug("OnActionPerformed");
171 result r = E_SUCCESS;
175 case IDA_BUTTON_SAVE:
177 GalleryApp* pApp = dynamic_cast<GalleryApp*>(UiApp::GetInstance());
179 if (pApp != null && pApp->GetAppControlOperationId() == APPCONTROL_OPERATION_ID_PICK)
181 ImageBuffer* pCropBuffer = null;
186 destFilePath.Append(App::GetInstance()->GetAppSharedPath());
187 destFilePath.Append(PATH_DATA_DIRECTORY_IN_CROP);
188 destFilePath.Append(GetFileName(__sourceFilePath));
189 destFilePath.Reverse();
190 r = destFilePath.IndexOf(FILE_EXT_SEPARATOR, 0, index);
191 destFilePath.Reverse();
195 destFilePath.Append(FILE_EXT_SEPARATOR);
197 destFilePath.Insert(FILE_NAME_SEPARATOR, destFilePath.GetLength() - index - 1);
198 destFilePath.Insert(count, destFilePath.GetLength() - index - 1);
200 if (__imageFormat != IMG_FORMAT_JPG || __imageFormat != IMG_FORMAT_PNG || __imageFormat != IMG_FORMAT_BMP)
202 destFilePath.Remove(destFilePath.GetLength() - index, index);
203 destFilePath.Append(CONTENT_EXT_JPG);
206 while (File::IsFileExist(destFilePath) == true)
209 destFilePath.Clear();
210 destFilePath.Append(App::GetInstance()->GetAppSharedPath());
211 destFilePath.Append(PATH_DATA_DIRECTORY_IN_CROP);
212 destFilePath.Append(GetFileName(__sourceFilePath));
213 destFilePath.Insert(FILE_NAME_SEPARATOR, destFilePath.GetLength() - index - 1);
214 destFilePath.Insert(count, destFilePath.GetLength() - index - 1);
215 if (__imageFormat != IMG_FORMAT_JPG || __imageFormat != IMG_FORMAT_PNG || __imageFormat != IMG_FORMAT_BMP)
217 destFilePath.Remove(destFilePath.GetLength() - index, index);
218 destFilePath.Append(CONTENT_EXT_JPG);
222 AppCtrlResult appControlResult = APP_CTRL_RESULT_FAILED;
223 HashMap* pMap = null;
226 r = utf8.GetByteCount(GetFileName(destFilePath), inputLength);
228 while (inputLength > MAX_FILE_LENGHT)
230 destFilePath.IndexOf(FILE_NAME_SEPARATOR, 0, index);
231 destFilePath.Remove(index - 1, 1);
232 r = utf8.GetByteCount(GetFileName(destFilePath), inputLength);
235 pCropBuffer = __imageBuffer->CropN(((__cropBox.x - __imageBox.x) * __imageWidth) / __imageBox.width, ((__cropBox.y - __imageBox.y) * __imageHeight) / __imageBox.height,
236 (__cropBox.width * __imageWidth) / __imageBox.width, (__cropBox.height * __imageHeight) / __imageBox.height);
238 if (pCropBuffer != null)
240 if (__imageFormat == IMG_FORMAT_JPG || __imageFormat == IMG_FORMAT_PNG || __imageFormat == IMG_FORMAT_BMP)
242 r = pCropBuffer->EncodeToFile(destFilePath, __imageFormat, true, 100);
246 r = pCropBuffer->EncodeToFile(destFilePath, IMG_FORMAT_JPG, true, 100);
252 ArrayList* pList = new (std::nothrow) ArrayList(SingleObjectDeleter);
253 pList->Add(new (std::nothrow) String(destFilePath));
255 pMap = new (std::nothrow) HashMap(SingleObjectDeleter);
257 pMap->Add(new (std::nothrow) String(APPCONTROL_KEY_DATA_SELECTED), pList);
259 appControlResult = APP_CTRL_RESULT_SUCCEEDED;
263 appControlResult = APP_CTRL_RESULT_FAILED;
268 appControlResult = APP_CTRL_RESULT_FAILED;
271 pApp->SendAppControlResult(appControlResult, pMap);
278 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
283 ImageCropForm::OnOrientationChanged(const Control &source, OrientationStatus orientationStatus)
285 AppLogDebug("OnOrientationChanged");
287 __isOrientationChanged = true;
290 if (__pCanvas != null)
294 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
298 ImageCropForm::OnTouchPressed(const Tizen::Ui::Control& source, const Tizen::Graphics::Point& currentPosition, const Tizen::Ui::TouchEventInfo& touchInfo)
300 AppLogDebug("OnTouchPressed");
302 __touchStart = currentPosition;
303 CheckCurrentPosition(currentPosition);
305 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
309 ImageCropForm::OnTouchMoved(const Tizen::Ui::Control& source, const Tizen::Graphics::Point& currentPosition, const Tizen::Ui::TouchEventInfo& touchInfo)
311 if (++__skipMoveEvent % 5 != 0)
320 int deltaX = currentPosition.x - __touchStart.x;
321 int deltaY = currentPosition.y - __touchStart.y;
322 if (__cropMode.Equals(APPCONTROL_DATA_CROP_TYPE_AUTO, false))
324 switch(__pointLocation)
326 case INSIDE_TOP_RECTANGLE:
328 if ((__cropBox.y + deltaY) > __imageBox.y && deltaY < (__cropBox.height - (H_CROP_RECTANGLE * 3)))
330 __cropBox.y += deltaY;
331 __cropBox.height -= deltaY;
336 case INSIDE_BOTTOM_RECTANGLE:
338 if ((__cropBox.y + __cropBox.height + deltaY) < (__imageBox.y + __imageBox.height)
339 && (__cropBox.height + deltaY) > (H_CROP_RECTANGLE * 3))
341 __cropBox.height += deltaY;
346 case INSIDE_LEFT_RECTANGLE:
348 if ((__cropBox.x + deltaX) > __imageBox.x && deltaX < __cropBox.width - (H_CROP_RECTANGLE * 3))
350 __cropBox.x += deltaX;
351 __cropBox.width -= deltaX;
356 case INSIDE_RIGHT_RECTANGLE:
358 if ((__cropBox.x + __cropBox.width + deltaX) < (__imageBox.x + __imageBox.width)
359 && __cropBox.width + deltaX > (H_CROP_RECTANGLE * 3))
361 __cropBox.width += deltaX;
366 case INSIDE_TOP_LEFT_RECTANGLE:
368 if ((__cropBox.y + deltaY) > __imageBox.y && deltaY < (__cropBox.height - (H_CROP_RECTANGLE * 3))
369 && (__cropBox.x + deltaX) > __imageBox.x && deltaX < __cropBox.width - (H_CROP_RECTANGLE * 3))
371 __cropBox.y += deltaY;
372 __cropBox.height -= deltaY;
373 __cropBox.x += deltaX;
374 __cropBox.width -= deltaX;
379 case INSIDE_TOP_RIGHT_RECTANGLE:
381 if ((__cropBox.y + deltaY) > __imageBox.y && deltaY < (__cropBox.height - (H_CROP_RECTANGLE * 3))
382 && (__cropBox.x + __cropBox.width + deltaX) < (__imageBox.x + __imageBox.width) && __cropBox.width + deltaX > (H_CROP_RECTANGLE * 3))
384 __cropBox.width += deltaX;
385 __cropBox.y += deltaY;
386 __cropBox.height -= deltaY;
391 case INSIDE_BOTTOM_LEFT_RECTANGLE:
393 if ((__cropBox.y + __cropBox.height + deltaY) < (__imageBox.y + __imageBox.height) && (__cropBox.height + deltaY) > (H_CROP_RECTANGLE * 3)
394 && (__cropBox.x + deltaX) > __imageBox.x && deltaX < __cropBox.width - (H_CROP_RECTANGLE * 3))
396 __cropBox.x += deltaX;
397 __cropBox.width -= deltaX;
398 __cropBox.height += deltaY;
403 case INSIDE_BOTTOM_RIGHT_RECTANGLE:
405 if ((__cropBox.x + __cropBox.width + deltaX) < (__imageBox.x + __imageBox.width) && __cropBox.width + deltaX > (H_CROP_RECTANGLE * 3)
406 && (__cropBox.y + __cropBox.height + deltaY) < (__imageBox.y + __imageBox.height) && (__cropBox.height + deltaY) > (H_CROP_RECTANGLE * 3))
408 __cropBox.width += deltaX;
409 __cropBox.height += deltaY;
416 if ((__cropBox.x + deltaX) > __imageBox.x)
418 __cropBox.x += deltaX;
422 __cropBox.x = __imageBox.x;
424 if ((__cropBox.y + deltaY) > __imageBox.y)
426 __cropBox.y += deltaY;
430 __cropBox.y = __imageBox.y;
432 if ((__cropBox.x + __cropBox.width + deltaX) > (__imageBox.x + __imageBox.width))
434 __cropBox.x = __imageBox.x + __imageBox.width - __cropBox.width;
436 if ((__cropBox.y + __cropBox.height + deltaY) > (__imageBox.y + __imageBox.height))
438 __cropBox.y = __imageBox.y + __imageBox.height - __cropBox.height;
447 else if (__cropMode.Equals(APPCONTROL_DATA_CROP_TYPE_FIT_TO_SCREEN, false))
449 if (__pointLocation == INSIDE_LEFT_RECTANGLE || __pointLocation == INSIDE_BOTTOM_LEFT_RECTANGLE
450 || __pointLocation == INSIDE_TOP_LEFT_RECTANGLE)
452 int temp = __cropBox.height;
453 int height = (__formHeight * (__cropBox.width - (2 * deltaX))) /__formWidth;
454 temp = height - temp;
455 if ((__cropBox.width - (2 * deltaX) > (H_CROP_RECTANGLE * 3)) && (!(height >= __imageBox.height || (__cropBox.width - (2 * deltaX)) >= __imageBox.width)))
457 if (__cropBox.x + deltaX >= __imageBox.x && __cropBox.x + __cropBox.width - deltaX <= __imageBox.x + __imageBox.width
458 && __cropBox.y - (temp / 2) >= __imageBox.y && __cropBox.y + __cropBox.height + (temp / 2) < __imageBox.y + __imageBox.height
459 && (__cropBox.width - (2 * deltaX) > (H_CROP_RECTANGLE * 3)))
461 __cropBox.x += deltaX;
462 __cropBox.width = __cropBox.width - (2 * deltaX);
463 __cropBox.height = height;
464 __cropBox.y = __cropBox.y - (temp / 2);
466 else if ((__cropBox.y + __cropBox.height) <= (__imageBox.y + __imageBox.height)
467 && __cropBox.width < __imageBox.width
468 && __cropBox.x + deltaX >= __imageBox.x && __cropBox.y - temp >= __imageBox.y )
470 if (__cropBox.x + __cropBox.width - deltaX <= __imageBox.x + __imageBox.width
471 && __cropBox.x + deltaX >= __imageBox.x)
473 __cropBox.x += deltaX;
477 __cropBox.x = __cropBox.x + (2 * deltaX);
479 __cropBox.width = __cropBox.width - (2 * deltaX);
480 __cropBox.height = height;
481 __cropBox.y = __cropBox.y - temp;
483 else if (__cropBox.y - (temp / 2) <= __imageBox.y && __cropBox.width - (deltaX) <= __imageBox.width
484 && __cropBox.x + deltaX >= __imageBox.x && __cropBox.y + __cropBox.height + temp <= __imageBox.y + __imageBox.height)
486 if (__cropBox.x + __cropBox.width - deltaX <= __imageBox.x + __imageBox.width)
488 __cropBox.x += deltaX;
492 __cropBox.x = __cropBox.x + (2 * deltaX);
494 __cropBox.width = __cropBox.width - (2 * deltaX);
495 __cropBox.height = height;
499 else if (__pointLocation == INSIDE_RIGHT_RECTANGLE || __pointLocation == INSIDE_BOTTOM_RIGHT_RECTANGLE
500 || __pointLocation == INSIDE_TOP_RIGHT_RECTANGLE)
503 int temp = __cropBox.height;
504 int height = (__formHeight * (__cropBox.width - (2 * deltaX))) / __formWidth;
505 temp = height - temp;
506 if ((__cropBox.width - (2 * deltaX) > (H_CROP_RECTANGLE * 3)) && (!(height >= __imageBox.height || (__cropBox.width - (2 * deltaX)) >= __imageBox.width)))
508 if (deltaX < __cropBox.width - (H_CROP_RECTANGLE * 3))
510 if (__cropBox.x + deltaX > __imageBox.x && __cropBox.x + __cropBox.width - deltaX <= __imageBox.x + __imageBox.width
511 && __cropBox.y - (temp / 2) > __imageBox.y && __cropBox.y + __cropBox.height + (temp / 2) < __imageBox.y + __imageBox.height)
513 __cropBox.x += deltaX;
514 __cropBox.width = __cropBox.width - (2 * deltaX);;
515 __cropBox.height = height;
516 __cropBox.y = __cropBox.y - (temp / 2);
518 else if ((__cropBox.y + __cropBox.height + (temp / 2)) >= (__imageBox.y + __imageBox.height)
519 && __cropBox.x + __cropBox.width - (2 * deltaX) <= __imageBox.x + __imageBox.width
520 && __cropBox.x - deltaX >= __imageBox.x && __cropBox.y - temp >= __imageBox.y )
522 if (__cropBox.x + __cropBox.width - deltaX <= __imageBox.x + __imageBox.width
523 && __cropBox.x + deltaX >= __imageBox.x)
525 __cropBox.x += deltaX;
527 __cropBox.width = __cropBox.width - (2 * deltaX);
528 __cropBox.height = height;
529 __cropBox.y = __cropBox.y - temp;
531 else if (__cropBox.y - (temp / 2) <= __imageBox.y && __cropBox.width - (deltaX) <= __imageBox.width
532 && __cropBox.x + deltaX <= __imageBox.x && __cropBox.y + __cropBox.height + temp <= __imageBox.y + __imageBox.height)
534 if (__cropBox.x + deltaX >= __imageBox.x)
536 __cropBox.x += deltaX;
540 __cropBox.x = __imageBox.x;
542 __cropBox.width = __cropBox.width - (2 * deltaX);
543 __cropBox.height = height;
545 else if (__cropBox.y - (temp / 2) <= __imageBox.y && __cropBox.x + deltaX >= __imageBox.x
546 && __cropBox.x + __cropBox.width - deltaX <= __imageBox.x + __imageBox.width
547 && __cropBox.y + __cropBox.height + temp <= __imageBox.y + __imageBox.height)
549 __cropBox.x += deltaX;
550 __cropBox.width = __cropBox.width - (2 * deltaX);
551 __cropBox.height = height;
553 else if (__cropBox.x + deltaX <= __imageBox.x && __cropBox.y - (temp / 2) >= __imageBox.y
554 && __cropBox.x + __cropBox.width - (2 * deltaX) <= __imageBox.x + __imageBox.width
555 && __cropBox.y + __cropBox.height + temp <= __imageBox.y + __imageBox.height)
557 __cropBox.width = __cropBox.width - (2 * deltaX);
558 __cropBox.y = __cropBox.y - (temp / 2);
559 __cropBox.height = height;
564 else if (__pointLocation == INSIDE_TOP_RECTANGLE)
566 int temp = __cropBox.width;
567 int width = (__formWidth * (__cropBox.height - (2 * deltaY))) / __formHeight;
569 if (__cropBox.height - (2 * deltaY) > (__formHeight * (H_CROP_RECTANGLE * 3)) / __formWidth && (!(__cropBox.height - (2 * deltaY) >= __imageBox.height || width >= __imageBox.width)))
571 if (__cropBox.x - (temp /2 ) > __imageBox.x && __cropBox.x + __cropBox.width + temp < __imageBox.x + __imageBox.width
572 && __cropBox.y + (deltaY) > __imageBox.y && __cropBox.y + __cropBox.height - (2 * deltaY) < __imageBox.y + __imageBox.height)
574 __cropBox.y += deltaY;
575 __cropBox.height = __cropBox.height - (2 * deltaY);
576 __cropBox.width = width;
577 __cropBox.x = __cropBox.x - (temp / 2);
579 else if (__cropBox.x - (temp / 2) < __imageBox.x)
581 if (__cropBox.x + __cropBox.width + temp <= __imageBox.x + __imageBox.width)
583 __cropBox.width = width;
585 if (__cropBox.y + deltaY >= __imageBox.y)
587 __cropBox.y += deltaY;
589 if (__cropBox.y + __cropBox.height - (2 * deltaY) < __imageBox.y + __imageBox.height)
591 __cropBox.height = __cropBox.height - (2 * deltaY);
594 else if (__cropBox.y + __cropBox.height - (2 * deltaY) >= __imageBox.x + __imageBox.height
595 && __cropBox.y + (2 * deltaY) >= __imageBox.y)
597 if (__cropBox.x - temp >= __imageBox.x
598 && __cropBox.x + __cropBox.width + temp > __imageBox.x + __imageBox.width)
600 __cropBox.x = __cropBox.x - temp;
602 else if (__cropBox.x - temp >= __imageBox.x)
604 __cropBox.x = __cropBox.x - temp / 2;
606 if (__cropBox.x + __cropBox.width + temp <= __imageBox.x + __imageBox.width)
608 __cropBox.width = width;
610 if (__cropBox.y + (2 * deltaY) >= __imageBox.y)
612 __cropBox.y = __cropBox.y + (2 * deltaY);
613 __cropBox.height = __cropBox.height - (2 * deltaY);
616 else if (__cropBox.x + __cropBox.width + (temp / 2) > __imageBox.x + __imageBox.width
617 && __cropBox.x - temp >= __imageBox.x && __cropBox.y + deltaY >= __imageBox.y
618 && __cropBox.y + __cropBox.height - (2 * deltaY) <= __imageBox.y + __imageBox.height)
620 __cropBox.x = __cropBox.x - temp;
621 __cropBox.width = width;
622 __cropBox.y += deltaY;
623 __cropBox.height = __cropBox.height - (2 * deltaY);
627 else if (__pointLocation == INSIDE_BOTTOM_RECTANGLE)
629 deltaY = deltaY * (-1);
630 int temp = __cropBox.width;
631 int width = (__formWidth * (__cropBox.height - (2 * deltaY))) / __formHeight;
633 if (__cropBox.height - (2 * deltaY) > (__formHeight * (H_CROP_RECTANGLE * 3)) / __formWidth && (!(__cropBox.height - (2 * deltaY) >= __imageBox.height || width >= __imageBox.width)))
635 if (__cropBox.x - (temp / 2) >= __imageBox.x && __cropBox.x + __cropBox.width + temp < __imageBox.x + __imageBox.width
636 && __cropBox.y + deltaY > __imageBox.y && __cropBox.y + __cropBox.height - (2 * deltaY) < __imageBox.y + __imageBox.height)
638 __cropBox.y += deltaY;
639 __cropBox.height = __cropBox.height - (2 * deltaY);;
640 __cropBox.width = width;
641 __cropBox.x = __cropBox.x - (temp / 2);
643 else if (__cropBox.y + deltaY < __imageBox.y && __cropBox.y + __cropBox.height - (2 * deltaY) < __imageBox.y + __imageBox.height
644 && __cropBox.x - (temp / 2) > __imageBox.x && __cropBox.x + __cropBox.width + temp < __imageBox.x + __imageBox.width)
646 __cropBox.x = __cropBox.x - (temp / 2);
647 __cropBox.width = width;
648 __cropBox.height = __cropBox.height - (2 * deltaY);
650 else if (__cropBox.x + __cropBox.width + temp > __imageBox.x + __imageBox.width
651 && __cropBox.y + deltaY >= __imageBox.y && __cropBox.y + __cropBox.height + temp <= __imageBox.y + __imageBox.height
652 && __cropBox.x - temp >= __imageBox.x)
654 __cropBox.x = __cropBox.x - temp;
655 __cropBox.y += deltaY;
656 __cropBox.width = width;
657 __cropBox.height = __cropBox.height - (2 * deltaY);
659 else if (__cropBox.x + __cropBox.width + temp >= __imageBox.x + __imageBox.width
660 && __cropBox.y + deltaY <= __imageBox.y && __cropBox.x - temp > __imageBox.x
661 && __cropBox.y + __cropBox.height + temp < __imageBox.y + __imageBox.height)
664 __cropBox.width = width;
665 __cropBox.height = __cropBox.height - (2 * deltaY);
667 else if (__cropBox.x - temp < __imageBox.x && __cropBox.x + __cropBox.width + temp <= __imageBox.x + __imageBox.width
668 && __cropBox.y + __cropBox.height - (2 * deltaY) <= __imageBox.y + __imageBox.height)
670 if (__cropBox.y + deltaY >= __imageBox.y)
672 __cropBox.y += deltaY;
674 __cropBox.width = width;
675 __cropBox.height = __cropBox.height - (2 * deltaY);
679 else if (__pointLocation == INSIDE_CROPBOX)
681 if ((__cropBox.x + deltaX) > __imageBox.x)
683 __cropBox.x += deltaX;
687 __cropBox.x = __imageBox.x;
689 if ((__cropBox.y + deltaY) > __imageBox.y)
691 __cropBox.y += deltaY;
695 __cropBox.y = __imageBox.y;
697 if ((__cropBox.x + __cropBox.width + deltaX) > (__imageBox.x + __imageBox.width))
699 __cropBox.x = __imageBox.x + __imageBox.width - __cropBox.width;
701 if ((__cropBox.y + __cropBox.height + deltaY) > (__imageBox.y + __imageBox.height))
703 __cropBox.y = __imageBox.y + __imageBox.height - __cropBox.height;
707 RequestRedraw(false);
708 __touchStart = currentPosition;
712 ImageCropForm::OnTouchReleased(const Tizen::Ui::Control& source, const Tizen::Graphics::Point& currentPosition, const Tizen::Ui::TouchEventInfo& touchInfo)
714 AppLogDebug("OnTouchReleased");
716 __pointLocation = OUTSIDE_CROPBOX;
720 ImageCropForm::OnSceneActivatedN(const SceneId& previousSceneId, const SceneId& currentSceneId, IList* pArgs)
722 AppLogDebug("OnSceneActivatedN");
724 __pPresentationModel->AddContentEventListener(this);
728 __sourceFilePath = *(static_cast<String*>(pArgs->GetAt(0)));
729 pArgs->RemoveAll(true);
732 if (__sourceFilePath.IsEmpty() == false)
734 if ( __imageBuffer != null)
736 delete __imageBuffer;
737 __imageBuffer = null;
740 __imageBuffer = new (std::nothrow) ImageBuffer();
742 result r = __imageBuffer->Construct(__sourceFilePath.GetPointer());
745 __imageWidth = __imageBuffer->GetWidth();
746 __imageHeight = __imageBuffer->GetHeight();
748 int physicalHeight = CoordinateSystem::ConvertToPhysicalY(__imageHeight);
749 int physicalWidth = CoordinateSystem::ConvertToPhysicalX(__imageWidth);
750 __pCurrentBitmap = __imageBuffer->GetBitmapN(BITMAP_PIXEL_FORMAT_ARGB8888, physicalWidth, physicalHeight);
757 __imageFormat = img.GetImageFormat(__sourceFilePath);
762 GalleryApp* pApp = dynamic_cast<GalleryApp*>(UiApp::GetInstance());
763 if (pApp != null && pApp->GetAppControlOperationId() == APPCONTROL_OPERATION_ID_PICK)
765 pApp->SendAppControlResult(APP_CTRL_RESULT_FAILED, null);
771 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
775 ImageCropForm::OnSceneDeactivated(const SceneId& currentSceneId, const SceneId& nextSceneId)
777 AppLogDebug("OnSceneDeactivated");
778 __pPresentationModel->RemoveContentEventListener(this);
779 RemoveOrientationEventListener(*this);
783 ImageCropForm::CheckCurrentPosition(const Point currentPosition)
785 AppLogDebug("CheckCurrentPosition");
787 if (currentPosition.x > ((__cropBox.x + __cropBox.width / 2) - (H_CROP_RECTANGLE / 2) - CROP_BOX_OFFSET) && currentPosition.x < ((__cropBox.x + __cropBox.width / 2) + (H_CROP_RECTANGLE / 2) + CROP_BOX_OFFSET)
788 && currentPosition.y > (__cropBox.y - (H_CROP_RECTANGLE / 2) - CROP_BOX_OFFSET) && currentPosition.y < (__cropBox.y + (H_CROP_RECTANGLE / 2) + CROP_BOX_OFFSET))
790 __pointLocation = INSIDE_TOP_RECTANGLE;
792 else if (currentPosition.x > (__cropBox.x - (H_CROP_RECTANGLE / 2) - CROP_BOX_OFFSET) && currentPosition.x < (__cropBox.x + (H_CROP_RECTANGLE / 2) + CROP_BOX_OFFSET)
793 && currentPosition.y > ((__cropBox.y + __cropBox.height / 2) - (H_CROP_RECTANGLE / 2) - CROP_BOX_OFFSET) && currentPosition.y < ((__cropBox.y + __cropBox.height / 2) + (H_CROP_RECTANGLE / 2) + CROP_BOX_OFFSET))
795 __pointLocation = INSIDE_LEFT_RECTANGLE;
797 else if (currentPosition.x > (__cropBox.x + __cropBox.width - (H_CROP_RECTANGLE / 2) - CROP_BOX_OFFSET) && currentPosition.x < (__cropBox.x + __cropBox.width + (H_CROP_RECTANGLE / 2) + CROP_BOX_OFFSET)
798 && currentPosition.y > ((__cropBox.y + __cropBox.height / 2) - (H_CROP_RECTANGLE / 2) - CROP_BOX_OFFSET) && currentPosition.y < ((__cropBox.y + __cropBox.height / 2) + (H_CROP_RECTANGLE / 2) + CROP_BOX_OFFSET))
800 __pointLocation = INSIDE_RIGHT_RECTANGLE;
802 else if (currentPosition.x > ((__cropBox.x + __cropBox.width / 2) - (H_CROP_RECTANGLE / 2) - CROP_BOX_OFFSET) && currentPosition.x < ((__cropBox.x + __cropBox.width / 2) + (H_CROP_RECTANGLE / 2) + CROP_BOX_OFFSET)
803 && currentPosition.y > (__cropBox.y + __cropBox.height - (H_CROP_RECTANGLE / 2) - CROP_BOX_OFFSET) && currentPosition.y < (__cropBox.y + __cropBox.height + (H_CROP_RECTANGLE / 2) + CROP_BOX_OFFSET))
805 __pointLocation = INSIDE_BOTTOM_RECTANGLE;
807 else if (currentPosition.x > (__cropBox.x - (H_CROP_RECTANGLE / 2) - CROP_BOX_OFFSET) && currentPosition.x < (__cropBox.x + (H_CROP_RECTANGLE / 2) + CROP_BOX_OFFSET)
808 && currentPosition.y > (__cropBox.y - (H_CROP_RECTANGLE / 2) - CROP_BOX_OFFSET) && currentPosition.y < (__cropBox.y +(H_CROP_RECTANGLE / 2) + CROP_BOX_OFFSET))
810 __pointLocation = INSIDE_TOP_LEFT_RECTANGLE;
812 else if (currentPosition.x > ((__cropBox.x + __cropBox.width / 2) - (H_CROP_RECTANGLE / 2) - CROP_BOX_OFFSET) && currentPosition.x < (__cropBox.x + __cropBox.width + (H_CROP_RECTANGLE / 2) + CROP_BOX_OFFSET)
813 && currentPosition.y > (__cropBox.y - (H_CROP_RECTANGLE / 2) - CROP_BOX_OFFSET) && currentPosition.y < (__cropBox.y +(H_CROP_RECTANGLE / 2) + CROP_BOX_OFFSET))
815 __pointLocation = INSIDE_TOP_RIGHT_RECTANGLE;
817 else if (currentPosition.x > (__cropBox.x + __cropBox.width - (H_CROP_RECTANGLE / 2) - CROP_BOX_OFFSET) && currentPosition.x < (__cropBox.x + __cropBox.width + (H_CROP_RECTANGLE / 2) + CROP_BOX_OFFSET)
818 && currentPosition.y > (__cropBox.y + __cropBox.height - (H_CROP_RECTANGLE / 2) - CROP_BOX_OFFSET) && currentPosition.y < (__cropBox.y + __cropBox.height + (H_CROP_RECTANGLE / 2) + CROP_BOX_OFFSET))
820 __pointLocation = INSIDE_BOTTOM_RIGHT_RECTANGLE;
822 else if (currentPosition.x > (__cropBox.x - (H_CROP_RECTANGLE / 2) - CROP_BOX_OFFSET) && currentPosition.x < (__cropBox.x + (H_CROP_RECTANGLE / 2) + CROP_BOX_OFFSET)
823 && currentPosition.y > (__cropBox.y + __cropBox.height - (H_CROP_RECTANGLE / 2) - CROP_BOX_OFFSET) && currentPosition.y < (__cropBox.y + __cropBox.height + (H_CROP_RECTANGLE / 2) + CROP_BOX_OFFSET))
825 __pointLocation = INSIDE_BOTTOM_LEFT_RECTANGLE;
827 else if (currentPosition.x > __cropBox.x && currentPosition.x < (__cropBox.x + __cropBox.width) && currentPosition.y > __cropBox.y && currentPosition.y < (__cropBox.y + __cropBox.height))
829 __pointLocation = INSIDE_CROPBOX;
833 __pointLocation = OUTSIDE_CROPBOX;
839 ImageCropForm::SetValue(void)
841 int prevHeight = __imageBox.height;
842 int prevWidth = __imageBox.width;
843 int prevImageboxX = __imageBox.x;
844 int prevImageBoxY = __imageBox.y;
846 if (GetOrientationStatus() == ORIENTATION_STATUS_PORTRAIT)
848 __formHeight = Form::GetBounds().height;
849 __formWidth = Form::GetBounds().width;
851 else if (GetOrientationStatus() == ORIENTATION_STATUS_LANDSCAPE || GetOrientationStatus() == ORIENTATION_STATUS_LANDSCAPE_REVERSE)
853 __formHeight = Form::GetBounds().width;
854 __formWidth = Form::GetBounds().height;
857 float clientAreaRatio = (GetClientAreaBoundsF().height) / GetClientAreaBoundsF().width;
859 if (__imageWidth >= GetClientAreaBounds().width || __imageHeight > GetClientAreaBounds().height)
861 float imageRatio = __imageHeight / (float) __imageWidth;
862 if (imageRatio < clientAreaRatio)
864 __imageBox.width = GetClientAreaBounds().width;
865 __imageBox.height = (__imageHeight * __imageBox.width) / __imageWidth;
866 __imageBox.x = GetClientAreaBounds().x;
867 __imageBox.y = GetClientAreaBounds().y + (GetClientAreaBounds().height - __imageBox.height) / 2;
871 __imageBox.height = GetClientAreaBounds().height;
872 __imageBox.width = (__imageWidth * __imageBox.height) / __imageHeight;
873 __imageBox.x = (GetClientAreaBounds().width - __imageBox.width) / 2;
874 __imageBox.y = GetClientAreaBounds().y;
879 __imageBox.height = __imageHeight;
880 __imageBox.width = __imageWidth;
881 __imageBox.x = (GetClientAreaBounds().width - __imageBox.width) / 2;
882 __imageBox.y = GetClientAreaBounds().y + (GetClientAreaBounds().height - __imageBox.height) / 2 ;
884 if (__isOrientationChanged == false)
886 if (__imageBox.height > __imageBox.width)
888 __cropBox.width = (2 * __imageBox.width) / 3;
889 __cropBox.height = (__formHeight * __cropBox.width / __formWidth);
890 if (__cropBox.height > __imageBox.height)
892 __cropBox.height = (2 * __imageBox.height) / 3;
893 __cropBox.width = (__cropBox.height * __formWidth) / __formHeight;
895 __cropBox.y = __imageBox.y + (__imageBox.height - __cropBox.height) / 2;
896 __cropBox.x = __imageBox.x + (__imageBox.width - __cropBox.width) / 2;
900 __cropBox.height = (2 *__imageBox.height) / 3;
901 __cropBox.width = (__formWidth * __cropBox.height / __formHeight);
902 if (__cropBox.width > __imageBox.width)
904 __cropBox.width = (2 * __imageBox.width) / 3;
905 __cropBox.height = (__formHeight * __cropBox.width) / __formWidth;
907 __cropBox.y = __imageBox.y + (__imageBox.height - __cropBox.height) / 2;
908 __cropBox.x = __imageBox.x + (__imageBox.width - __cropBox.width) / 2;
913 float temp = __imageBox.x + ((__cropBox.x - prevImageboxX) * __imageBox.width) / (float) prevWidth;
914 __cropBox.x = (temp + 0.5);
915 temp = __imageBox.y + ((__cropBox.y - prevImageBoxY) * __imageBox.height) / (float) prevHeight;
916 __cropBox.y = (temp + 0.5);
917 temp = (__cropBox.height * __imageBox.height) / (float) prevHeight;
918 __cropBox.height = (temp + 0.5);
919 temp = (__cropBox.width * __imageBox.width) / (float) prevWidth;
920 __cropBox.width = (temp + 0.5);
922 __pCanvas = GetCanvasN(0.0f, 0.0f, GetClientAreaBoundsF().width, GetClientAreaBoundsF().height);
923 if (__pCanvas != null)
925 __pCanvas->SetBackgroundColor(COLOR_GALLERY_BG);
926 __pCanvas->SetForegroundColor(CROP_BOX_RECTANGLE_COLOR);
931 ImageCropForm::GetFileName(const String& filePath, bool isCheckExe) const
936 filePath.LastIndexOf(DIRECTORY_SEPARATOR, filePath.GetLength() - 1, lastIndex);
937 filePath.SubString(lastIndex+1, strResult);
939 if (isCheckExe == true)
941 String strTemp = strResult;
944 strTemp.LastIndexOf(FILE_EXT_SEPARATOR, strTemp.GetLength() - 1, lastIndex);
945 strTemp.SubString(0, lastIndex, strResult);
952 ImageCropForm::OnContentUpdated()
954 AppLogDebug("ENTER");
956 SceneManager* pSceneManager = SceneManager::GetInstance();
957 pSceneManager->GoForward(ForwardSceneTransition(IDSCN_ALBUM_LIST));
959 AppLogDebug("EXIT(%s)");