summaryrefslogtreecommitdiff
path: root/main/src/view/ivug-crop-view.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'main/src/view/ivug-crop-view.cpp')
-rwxr-xr-xmain/src/view/ivug-crop-view.cpp494
1 files changed, 494 insertions, 0 deletions
diff --git a/main/src/view/ivug-crop-view.cpp b/main/src/view/ivug-crop-view.cpp
new file mode 100755
index 0000000..49411d0
--- /dev/null
+++ b/main/src/view/ivug-crop-view.cpp
@@ -0,0 +1,494 @@
+/*
+ * Copyright 2012 Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.tizenopensource.org/license
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <Elementary.h>
+#include <fcntl.h>
+
+#include "ivug-crop-view.h"
+#include "EFLUtil.h"
+#include "ivug-debug.h"
+#include "ivug-image.h"
+#include "ivug-scissorbox.h"
+#include "ivug-context.h"
+#include "ivug-util.h"
+#include "ivug-selectioninfo.h"
+
+#undef LOG_LVL
+#define LOG_LVL DBG_MSG_LVL_MED
+
+#undef LOG_CAT
+#define LOG_CAT "IV-CROP"
+
+#define EDJ_PATH PREFIX"/res/edje/"PACKAGE
+
+static void _on_layout_resized(void *data, Evas *e, Evas_Object *obj, void *event_info)
+{
+ IvugCropView *pCropView = static_cast<IvugCropView *>(data);
+ ///////////////////////////////////////////////////////////////////
+ int org_x, org_y, org_w, org_h;
+
+ ivug_image_region_get(pCropView->photocam, &org_x, &org_y, &org_w, &org_h);
+
+ if(org_w == 0 || org_h == 0)
+ return;
+
+ MSG_MED("org_x=%d, org_y=%d, org_w=%d, org_h=%d", org_x, org_y, org_w, org_h);
+
+ int org_sx, org_sy, org_sw, org_sh;
+ ivug_scissorbox_region_get(pCropView->cropbox, &org_sx, &org_sy, &org_sw, &org_sh);
+
+ MSG_MED("org_sx=%d, org_sy=%d, org_sw=%d, org_sh=%d", org_sx, org_sy, org_sw, org_sh);
+
+ int dx = org_sx-org_x;
+ int dy = org_sy-org_y;
+
+ int lcd_x, lcd_y, lcd_w, lcd_h;
+
+ evas_object_geometry_get(obj, &lcd_x, &lcd_y, &lcd_w, &lcd_h);
+ MSG_MED("lcd_x=%d, lcd_y=%d, lcd_w=%d, lcd_h=%d", lcd_x, lcd_y, lcd_w, lcd_h);
+
+ int ph, pw;
+
+ int img_w, img_h;
+ int ext_w, ext_h;
+
+ img_w = org_w;
+ img_h = org_h;
+ ext_w = lcd_w;
+ ext_h = lcd_h;
+
+ ph = (img_h * ext_w) / img_w;
+
+ if (ph > ext_h)
+ {
+ pw = (img_w * ext_h) / img_h;
+ ph = ext_h;
+ }
+ else
+ {
+ pw = ext_w;
+ }
+
+ double zoom = 0.0;
+
+ if ( img_w > img_h)
+ {
+ zoom = (double)pw / img_w;
+ }
+ else
+ {
+ zoom = (double)ph / img_h;
+ }
+
+ int sx, sy, sw, sh;
+
+ sw = org_sw*zoom;
+ sh = org_sh*zoom;
+ dx *= zoom;
+ dy *= zoom;
+
+ sx = (ext_w-img_w*zoom)/2 + dx;
+ sy = (ext_h-img_h*zoom)/2 + dy;
+
+ ivug_scissorbox_boundary_set(pCropView->cropbox, (ext_w-img_w*zoom)/2, (ext_h-img_h*zoom)/2, img_w*zoom, img_h*zoom);
+
+ ///////////////////////////////////////////////////////////////////
+
+ MSG_MED("sx=%d, sy=%d, sw=%d, sh=%d", sx, sy, sw, sh);
+
+ ivug_scissorbox_region_set(pCropView->cropbox, sx, sy, sw, sh);
+}
+
+void _on_test(void *data, Evas_Object *obj, void *event_info)
+{
+ MSG_HIGH("Test signal received");
+}
+
+static Eina_Bool _on_crop_timer_expired(void *data)
+{
+ IvugCropView *pCropView = static_cast<IvugCropView *>(data);
+
+ pCropView->timer = NULL;
+
+ elm_object_disabled_set(pCropView->btn_ok, EINA_FALSE);
+
+ MSG_HIGH("Image preloaded");
+
+ int x, y, w, h;
+ ivug_image_region_get(pCropView->photocam, &x, &y, &w, &h);
+
+ // Check error!.
+
+ MSG_HIGH("Photcam image Geometry : %d,%d,%d,%d", x, y, w, h);
+
+ // Get Internal image and set that as scissorbox client
+ Evas_Object *subObj = ivug_image_internal_image_get(pCropView->photocam);
+
+ // Move crop box
+ ivug_scissorbox_attach(pCropView->cropbox, subObj);
+
+ int nW = 0;
+ int nH = 0;
+
+ if(pCropView->w > 0 && pCropView->h > 0)
+ {
+ nW = pCropView->w;
+ nH = pCropView->h;
+ }
+ else
+ {
+ nW = w / 2;
+ nH = h / 2;
+ }
+
+ ivug_scissorbox_region_set(pCropView->cropbox, x + (w - nW) / 2, y + (h - nH) / 2, nW, nH);
+
+ pCropView->notify = ivug_notify_create(pCropView->layout, IDS_CROP_NOTIFY);
+
+ return ECORE_CALLBACK_CANCEL;
+}
+
+void _on_preloaded(void *data, Evas_Object *obj, void *event_info)
+{
+ IV_ASSERT(data != NULL);
+
+ IvugCropView *pCropView = static_cast<IvugCropView *>(data);
+
+ MSG_HIGH("Image preloaded");
+
+ double zoom = ivug_image_zoom_get(pCropView->photocam);
+
+ MSG_HIGH("zoom = %g", zoom);
+
+ pCropView->timer = ecore_timer_add(0.5, _on_crop_timer_expired, data);
+
+ return;
+}
+
+static void
+_ivug_crop_view_response_cb( void *data, Evas_Object *obj, void *event_info )
+{
+ IV_ASSERT(obj != NULL);
+ IV_ASSERT(data != NULL);
+
+ MSG_HIGH( "response callback=%d",(int)event_info);
+ IvugCropView *pCropView = static_cast<IvugCropView *>(data);
+
+ evas_object_hide(obj); // popup
+ evas_object_del(obj);
+
+ // Remove view.
+ evas_object_smart_callback_call(pCropView->layout, "destroyed", NULL);
+
+ evas_object_smart_callback_call(pCropView->layout, "ok,clicked", pCropView->result_path);
+
+ ivug_crop_view_destroy(pCropView);
+}
+
+
+static Evas_Object*
+_show_exit_popup( Evas_Object *parent, const char *title, const char *desc, void *data)
+{
+ MSG_HIGH( "title: %s, desc %s", title, desc);
+
+ Evas_Object* style1_popup = elm_popup_add(parent);
+ evas_object_size_hint_weight_set(style1_popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
+ elm_popup_timeout_set(style1_popup, (double)2.0);
+ elm_object_text_set(style1_popup, desc);
+ elm_object_part_text_set(style1_popup, "title,text", title);
+
+ evas_object_smart_callback_add(style1_popup, "timeout", _ivug_crop_view_response_cb, data);
+
+ evas_object_show(style1_popup);
+ evas_object_layer_set(style1_popup, EVAS_LAYER_MAX);
+
+ return style1_popup;
+}
+
+static void _on_btn_save(void *data, Evas_Object *obj, void *event_info)
+{
+ IV_ASSERT(data != NULL);
+
+ IvugCropView *pCropView = static_cast<IvugCropView *>(data);
+ int rx, ry, rw, rh;
+ Evas_Object *ret_image = NULL;
+
+ char* save_file = ivug_generate_file_name(pCropView->file_path);
+ if(save_file == NULL)
+ {
+ MSG_SETAS_ERROR("Cannot generate file name");
+ goto SAVE_FAILED;
+ }
+
+ MSG_HIGH("Save clicked. %s", save_file);
+
+// Get region
+ ivug_scissorbox_region_get(pCropView->cropbox, &rx, &ry, &rw, &rh);
+
+// Save File
+ MSG_HIGH("SCissorbox Rect:(%d,%d,%d,%d)", rx, ry, rw, rh);
+
+ ret_image = ivug_image_region_image_get(pCropView->photocam, rx, ry, rw, rh);
+
+ if(ret_image == NULL)
+ {
+ MSG_ERROR("Region get faied.");
+ goto SAVE_FAILED;
+ }
+
+ if (evas_object_image_save(ret_image, save_file, NULL, "quality=100 compress=9") == EINA_FALSE)
+ {
+ MSG_ERROR("evas_object_image_save error. %s", save_file);
+ goto SAVE_FAILED;
+ }
+
+// Check again!
+ {
+ int fd = open(save_file, O_RDONLY);
+ if(fd < 0)
+ {
+ MSG_ERROR("%s open error[%d]", save_file, fd);
+ goto SAVE_FAILED;
+ }
+
+ int ret = close(fd);
+ if(ret < 0)
+ {
+ MSG_ERROR("%s open error[%d]", save_file, ret);
+ }
+ }
+
+ pCropView->result_path = strdup(save_file);
+
+ _show_exit_popup(pCropView->layout, IDS_CROP, IDS_SUCCESS, data);
+
+ evas_object_del(ret_image);
+ free(save_file);
+
+ return;
+
+SAVE_FAILED:
+ _show_exit_popup(pCropView->layout, IDS_CROP, IDS_FAILED, data);
+
+ if(ret_image)
+ evas_object_del(ret_image);
+ if(save_file)
+ free(save_file);
+
+ return;
+}
+
+static void _on_btn_cancel(void *data, Evas_Object *obj, void *event_info)
+{
+ IV_ASSERT(data != NULL);
+
+ IvugCropView *pCropView = static_cast<IvugCropView *>(data);
+
+ MSG_HIGH("Cancel clicked");
+
+ evas_object_smart_callback_call(pCropView->layout, "destroyed", NULL);
+
+ evas_object_smart_callback_call(pCropView->layout, "cancel,clicked", NULL);
+
+ ivug_crop_view_destroy(pCropView);
+}
+
+static Evas_Event_Flags _finger_tap_end(void *data , void *event_info)
+{
+ Elm_Gesture_Taps_Info *p = (Elm_Gesture_Taps_Info *) event_info;
+
+ MSG_HIGH("Finger tab end. evt=%d", p->timestamp);
+
+ if ( p->n != 1 ) return EVAS_EVENT_FLAG_NONE;
+
+ IvugCropView *pCropView = static_cast<IvugCropView *>(data);
+ IV_ASSERT(pCropView != NULL);
+
+ if(pCropView->bShowMenu)
+ {
+ MSG_HIGH("Hide");
+ elm_win_indicator_mode_set((Evas_Object*)ug_get_window(), ELM_WIN_INDICATOR_HIDE);
+ evas_object_hide(pCropView->notify);
+ edje_object_signal_emit(_EDJ(pCropView->layout), "elm,state,hide", "event");
+ }
+ else
+ {
+ MSG_HIGH("Show");
+ elm_win_indicator_mode_set((Evas_Object*)ug_get_window(), ELM_WIN_INDICATOR_SHOW);
+ evas_object_show(pCropView->notify);
+ edje_object_signal_emit(_EDJ(pCropView->layout), "elm,state,show", "event");
+ }
+
+ pCropView->bShowMenu = !pCropView->bShowMenu;
+
+ return EVAS_EVENT_FLAG_NONE;
+}
+
+void _add_gesture_layer(Evas_Object *parent, IvugCropView *pCropView)
+{
+ IV_ASSERT(pCropView != NULL);
+
+ Evas_Object *gesture = pCropView->gesture;
+
+ gesture = elm_gesture_layer_add(parent);
+ elm_gesture_layer_hold_events_set(gesture, EINA_TRUE);
+
+ elm_gesture_layer_cb_set(gesture, ELM_GESTURE_N_TAPS, ELM_GESTURE_STATE_END, _finger_tap_end, (void *)pCropView);
+
+ if ( elm_gesture_layer_attach(gesture, pCropView->photocam) == EINA_FALSE)
+ {
+ MSG_ERROR("Cannot attach event rect");
+ }
+
+ evas_object_name_set(gesture, "Crop:Getsture");
+}
+
+IvugCropView *ivug_crop_view_create(Evas_Object *parent)
+{
+ IvugCropView *pCropView = (IvugCropView *)calloc(1, sizeof(IvugCropView));
+
+ Evas_Object *layout;
+
+ layout = EFL::create_layout(parent, EDJ_PATH"/ivug-crop-view.edj", "crop_view");
+
+ if ( layout == NULL )
+ {
+ MSG_ERROR("Cannot create crop view");
+ delete pCropView;
+
+ return NULL;
+ }
+
+ pCropView->layout = layout;
+ evas_object_event_callback_add(layout, EVAS_CALLBACK_RESIZE, _on_layout_resized, pCropView );
+ evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND );
+
+ pCropView->photocam = ivug_image_create(layout);
+ ivug_image_animated_set(pCropView->photocam, EINA_FALSE);
+ ivug_image_hold_set(pCropView->photocam , EINA_TRUE); // Disable mouse events
+ evas_object_smart_callback_add(pCropView->photocam, "loaded", _on_preloaded, pCropView);
+
+ elm_object_part_content_set(layout, "photocam", pCropView->photocam );
+
+ pCropView->bShowMenu = true;
+ _add_gesture_layer(layout, pCropView);
+
+ pCropView->cropbox = ivug_scissorbox_add(layout);
+ ivug_scissorbox_type_set(pCropView->cropbox, SCISSOR_BOX_TYPE_2);
+
+ elm_object_part_content_set(layout, "scissorbox", pCropView->cropbox);
+
+// Create button
+ Evas_Object *btn = NULL;
+
+ btn = EFL::create_button(layout, "btn_style1", NULL, IDS_SAVE);
+ evas_object_smart_callback_add(btn, "clicked", _on_btn_save, (void *)pCropView);
+ elm_object_part_content_set(layout, "btn.save", btn);
+
+ pCropView->btn_ok = btn;
+
+ btn = EFL::create_button(layout, "btn_style1", NULL, IDS_CANCEL);
+ evas_object_smart_callback_add(btn, "clicked", _on_btn_cancel, (void *)pCropView);
+ elm_object_part_content_set(layout, "btn.cancel", btn);
+
+ MSG_HIGH("Create CropView");
+
+ evas_object_smart_callback_add(layout, "test", _on_test, NULL);
+
+ elm_object_disabled_set(pCropView->btn_ok, EINA_TRUE);
+
+ elm_win_resize_object_add(gGetCurrentWindow(), layout);
+
+ evas_object_show(layout);
+
+ pCropView->w = 0;
+ pCropView->h = 0;
+
+ return pCropView;
+
+}
+
+bool ivug_crop_view_box_size_set(IvugCropView *pCropView, int w, int h)
+{
+ IV_ASSERT(pCropView != NULL);
+
+ pCropView->w = w;
+ pCropView->h = h;
+
+ return true;
+}
+
+bool ivug_crop_view_box_ratio_fix(IvugCropView *pCropView, bool bFix)
+{
+ IV_ASSERT(pCropView != NULL);
+
+ ivug_scissorbox_ratio_fix(pCropView->cropbox, bFix);
+
+ return true;
+}
+
+bool ivug_crop_view_file_set(IvugCropView *pCropView, const char *file)
+{
+ IV_ASSERT(pCropView != NULL);
+
+ Evas_Load_Error err;
+
+ if ( pCropView->file_path )
+ {
+ free(pCropView->file_path);
+ }
+
+ pCropView->file_path = strdup(file);
+
+ err = ivug_image_file_set(pCropView->photocam, file, NULL);
+ evas_object_smart_callback_call(pCropView->layout, "test", NULL);
+
+ return true;
+}
+
+void ivug_crop_view_destroy(IvugCropView *pCropView)
+{
+ IV_ASSERT(pCropView != NULL);
+
+ MSG_HIGH("Destroy CropView");
+
+ if(pCropView->timer)
+ {
+ ecore_timer_del(pCropView->timer);
+ pCropView->timer = NULL;
+ }
+
+ if ( pCropView->file_path )
+ free(pCropView->file_path);
+
+ if ( pCropView->result_path)
+ free(pCropView->result_path);
+
+ if ( pCropView->layout )
+ evas_object_del(pCropView->layout);
+
+ delete pCropView;
+}
+
+Evas_Object *ivug_crop_view_get_object(IvugCropView *pCropView)
+{
+ IV_ASSERT(pCropView != NULL);
+
+ return pCropView->layout;
+}
+
+
+