From 701fc2cc84aa8fe5682e2d77473147bbf571027d Mon Sep 17 00:00:00 2001
From: Manuel Kaufmann <humitos@gmail.com>
Date: Tue, 16 Oct 2012 14:45:15 +0000
Subject: [PATCH ImageViewer] Scale the image through its center
Signed-off-by: Manuel Kaufmann <humitos@gmail.com>
---
ImageView.py | 33 ++++++++++++++++++++++-----------
ImageViewerActivity.py | 1 +
2 files changed, 23 insertions(+), 11 deletions(-)
diff --git a/ImageView.py b/ImageView.py
index 7ec9a41..30b19ac 100644
a
|
b
|
class ImageViewer(Gtk.DrawingArea): |
70 | 70 | self._angle_ori = 0.0 |
71 | 71 | self._fast = True |
72 | 72 | self._redraw_id = None |
| 73 | self._is_touching = False |
73 | 74 | |
74 | 75 | def do_get_property(self, pspec): |
75 | 76 | if pspec.name == 'zoom': |
… |
… |
class ImageViewer(Gtk.DrawingArea): |
131 | 132 | ctx.rotate(self.angle) |
132 | 133 | ctx.translate(-0.5 * w, -0.5 * h) |
133 | 134 | |
| 135 | scrolled_window = self.get_parent() |
| 136 | rect = scrolled_window.get_allocation() |
| 137 | x = y = 0 |
| 138 | if rect.width >= w: |
| 139 | x = int((rect.width - w) / 2) |
| 140 | elif self._is_touching: |
| 141 | hadj = int((w - rect.width) / 2) |
| 142 | hadjustment = scrolled_window.get_hadjustment() |
| 143 | hadjustment.set_value(hadj) |
| 144 | |
| 145 | if rect.height >= h: |
| 146 | y = int((rect.height - h) / 2) |
| 147 | elif self._is_touching: |
| 148 | vadj = int((h - rect.height) / 2) |
| 149 | vadjustment = scrolled_window.get_vadjustment() |
| 150 | vadjustment.set_value(vadj) |
| 151 | |
134 | 152 | if self.zoom != 1: |
135 | 153 | logging.error('Scaling: %s', self.zoom) |
| 154 | ctx.translate(x, y) |
136 | 155 | ctx.scale(self.zoom, self.zoom) |
137 | 156 | |
138 | | rect = self.get_allocation() |
139 | | x = rect.x |
140 | | y = rect.y |
141 | | |
142 | | rect = self.get_allocation() |
143 | | if rect.width > w: |
144 | | x = int((rect.width - w) / 2) |
145 | | if rect.height > h: |
146 | | y = int((rect.height - h) / 2) |
147 | | |
148 | | ctx.set_source_surface(self.surface, x, y) |
| 157 | ctx.set_source_surface(self.surface, 0, 0) |
149 | 158 | if self._fast: |
150 | 159 | ctx.get_source().set_filter(cairo.FILTER_NEAREST) |
151 | 160 | ctx.paint() |
… |
… |
class ImageViewer(Gtk.DrawingArea): |
157 | 166 | self._redraw_id = GObject.timeout_add(200, |
158 | 167 | self._redraw_high_quality) |
159 | 168 | |
| 169 | self._is_touching = False |
| 170 | |
160 | 171 | def _redraw_high_quality(self): |
161 | 172 | self._fast = False |
162 | 173 | self._redraw_id = None |
diff --git a/ImageViewerActivity.py b/ImageViewerActivity.py
index 2cfe88b..f17d4cf 100644
a
|
b
|
class ImageViewerActivity(activity.Activity): |
213 | 213 | self._last_scale = scale |
214 | 214 | logging.error('Scale changed %f', scale) |
215 | 215 | |
| 216 | self.view._is_touching = True |
216 | 217 | self.view.set_zoom_relative(scale) |
217 | 218 | |
218 | 219 | def handle_view_source(self): |