From b0e066894ab52d4dc7a8e7273b04d885953b870b Mon Sep 17 00:00:00 2001 From: Manuel QuiƱones Date: Wed, 15 May 2013 04:32:36 +0000 Subject: Basic pinch to zoom --- diff --git a/ImageView.py b/ImageView.py index e74f3e9..7bc9095 100644 --- a/ImageView.py +++ b/ImageView.py @@ -15,6 +15,7 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +import logging import cairo from gi.repository import Gtk @@ -47,6 +48,9 @@ class ImageViewer(Gtk.DrawingArea): self._surface = None self._zoom = None + self._in_zoomtouch = False + self._zoomtouch_scale = 1 + self.connect('draw', self.__draw_cb) def set_file_location(self, file_location): @@ -102,6 +106,27 @@ class ImageViewer(Gtk.DrawingArea): self._zoom = 1 self.queue_draw() + def start_zoomtouch(self, center): + logging.debug("start_zoomtouch %r", center) + self._in_zoomtouch = True + self._zoomtouch_scale = 1 + self.queue_draw() + + def update_zoomtouch(self, center, scale): + logging.debug("update_zoomtouch %r", (center, scale)) + self._zoomtouch_scale = scale + self.queue_draw() + + def finish_zoomtouch(self): + logging.debug("finish_zoomtouch") + + # apply zoom + self._zoom = self._zoom * self._zoomtouch_scale + + self._in_zoomtouch = False + self._zoomtouch_scale = 1 + self.queue_draw() + def __draw_cb(self, widget, ctx): # If the image surface is not set, it reads it from the file @@ -120,15 +145,18 @@ class ImageViewer(Gtk.DrawingArea): # Scale and center the image according to the current zoom. - scaled_width = int(self._surface.get_width() * self._zoom) - scaled_height = int(self._surface.get_height() * self._zoom) + zoom_absolute = self._zoom * self._zoomtouch_scale + + scaled_width = int(self._surface.get_width() * zoom_absolute) + scaled_height = int(self._surface.get_height() * zoom_absolute) + alloc = self.get_allocation() x_offset = (alloc.width * 1.0 - scaled_width) / 2 y_offset = (alloc.height * 1.0 - scaled_height) / 2 ctx.translate(x_offset, y_offset) - ctx.scale(self._zoom, self._zoom) + ctx.scale(zoom_absolute, zoom_absolute) ctx.set_source_surface(self._surface, 0, 0) # FIXME investigate diff --git a/ImageViewerActivity.py b/ImageViewerActivity.py index 4e5c6d1..ab1424c 100644 --- a/ImageViewerActivity.py +++ b/ImageViewerActivity.py @@ -116,13 +116,14 @@ class ImageViewerActivity(activity.Activity): if GESTURES_AVAILABLE: zoom_controller = SugarGestures.ZoomController() - zoom_controller.connect('scale-changed', - self.__scale_changed_cb) - zoom_controller.connect('began', - self.__scale_began_cb) zoom_controller.attach(self, SugarGestures.EventControllerFlags.NONE) + zoom_controller.connect('began', self.__zoomtouch_began_cb) + zoom_controller.connect('scale-changed', + self.__zoomtouch_changed_cb) + zoom_controller.connect('ended', self.__zoomtouch_ended_cb) + self.progressdialog = None toolbar_box = ToolbarBox() @@ -130,9 +131,6 @@ class ImageViewerActivity(activity.Activity): self.set_toolbar_box(toolbar_box) toolbar_box.show() - self._last_angle = 0.0 - self._last_scale = 1.0 - if self._object_id is None: empty_widgets = Gtk.EventBox() empty_widgets.modify_bg(Gtk.StateType.NORMAL, @@ -195,16 +193,14 @@ class ImageViewerActivity(activity.Activity): # Wait for a successful join before trying to get the document self.connect("joined", self._joined_cb) - def __scale_began_cb(self, controller): - self.view._zoom_ori = self.view.zoom + def __zoomtouch_began_cb(self, controller): + self.view.start_zoomtouch(controller.get_center()) - def __scale_changed_cb(self, controller, scale): - if scale != self._last_scale: - self._last_scale = scale + def __zoomtouch_changed_cb(self, controller, scale): + self.view.update_zoomtouch(controller.get_center(), scale) - self.view._is_touching = True - self.view._touch_center = controller.get_center() - self.view.set_zoom_relative(scale) + def __zoomtouch_ended_cb(self, controller): + self.view.finish_zoomtouch() def _add_toolbar_buttons(self, toolbar_box): activity_button = ActivityToolbarButton(self) -- cgit v0.9.1