summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKai Huuhko <kai.huuhko@gmail.com>2015-03-23 22:05:25 +0200
committerKai Huuhko <kai.huuhko@gmail.com>2015-03-23 22:05:25 +0200
commit1ae2d73829d3cc1953ffb27199514ca131662bef (patch)
tree6808a960015823ea5df201555d7e1622749b602f
parent6993bfd5dd94659d713f0609d2292ac45713f218 (diff)
Add zoom menu to toolbar, fix zoom issues
Scroller still doesn't keep the position when zooming :(
-rw-r--r--README2
-rw-r--r--lekha/app.py74
2 files changed, 58 insertions, 18 deletions
diff --git a/README b/README
index efc7076..1953d14 100644
--- a/README
+++ b/README
@@ -1,3 +1,3 @@
1Test run from top directory: 1Test run from top directory:
2 2
3 python -m lekha.app pdf [pdf ...] 3 python -m lekha.app [pdf ...]
diff --git a/lekha/app.py b/lekha/app.py
index 14efc12..ada22a6 100644
--- a/lekha/app.py
+++ b/lekha/app.py
@@ -73,12 +73,29 @@ class AppWindow(StandardWindow):
73 self.resize_object_add(main_box) 73 self.resize_object_add(main_box)
74 74
75 tb = Toolbar( 75 tb = Toolbar(
76 main_box, 76 self, # For some reason item menus break when parent is main_box
77 size_hint_weight=EXPAND_HORIZ, size_hint_align=FILL_HORIZ, 77 size_hint_weight=EXPAND_HORIZ, size_hint_align=FILL_HORIZ,
78 select_mode=ELM_OBJECT_SELECT_MODE_NONE, icon_size=24) 78 select_mode=ELM_OBJECT_SELECT_MODE_NONE, icon_size=24)
79 tb.item_append( 79 tb.item_append(
80 "document-open", "Open", lambda x, y: Fs(self.document_open)) 80 "document-open", "Open", lambda x, y: Fs(self.document_open))
81 81
82 it = tb.item_append("zoom-in", "Zoom")
83 it.menu = True
84 tb.menu_parent = self
85 menu = it.menu
86 menu.item_add(
87 None, "Zoom In", "zoom-in",
88 lambda x, y: tabs.currentContent.zoom_in())
89 menu.item_add(
90 None, "Zoom Out", "zoom-out",
91 lambda x, y: tabs.currentContent.zoom_out())
92 menu.item_add(
93 None, "Zoom 1:1", "zoom-original",
94 lambda x, y: tabs.currentContent.zoom_orig())
95 menu.item_add(
96 None, "Zoom Fit", "zoom-fit-best",
97 lambda x, y: tabs.currentContent.zoom_fit())
98
82 tabs = self.tabs = Tabs(main_box, size_hint_weight=EXPAND_BOTH, size_hint_align=FILL_BOTH) 99 tabs = self.tabs = Tabs(main_box, size_hint_weight=EXPAND_BOTH, size_hint_align=FILL_BOTH)
83 100
84 # tabs.callback_add("tab,added", lambda x, y: print("added", y)) 101 # tabs.callback_add("tab,added", lambda x, y: print("added", y))
@@ -134,8 +151,6 @@ class AppWindow(StandardWindow):
134 151
135class Document(Box): 152class Document(Box):
136 153
137 SIZE_MIN = 50
138
139 def __init__(self, parent, doc, doc_path, doc_zoom=1.0, doc_pos=None): 154 def __init__(self, parent, doc, doc_path, doc_zoom=1.0, doc_pos=None):
140 self.doc_path = doc_path 155 self.doc_path = doc_path
141 self._zoom = doc_zoom 156 self._zoom = doc_zoom
@@ -164,6 +179,10 @@ class Document(Box):
164 btn.callback_clicked_add(self._show_page_cb, spn) 179 btn.callback_clicked_add(self._show_page_cb, spn)
165 toolbox.pack_end(btn) 180 toolbox.pack_end(btn)
166 181
182 zlbl = self.zlbl = Label(
183 toolbox, text="%1.0f %%" % (self.zoom * 100.0))
184 toolbox.pack_end(zlbl)
185
167 for c in toolbox: 186 for c in toolbox:
168 c.show() 187 c.show()
169 188
@@ -223,30 +242,36 @@ class Document(Box):
223 242
224 @zoom.setter 243 @zoom.setter
225 def zoom(self, value): 244 def zoom(self, value):
226 self._zoom = value
227 for c in self.page_box: 245 for c in self.page_box:
228 old_size = c.size_hint_min 246 c.zoom_set(value)
229 new_size = [i * value for i in old_size] 247 self._zoom = value
230 if ( 248 self.zlbl.text = "%1.0f %%" % (value * 100.0)
231 (old_size[0] >= self.SIZE_MIN or
232 old_size[1] >= self.SIZE_MIN) and
233 (new_size[0] < self.SIZE_MIN or
234 new_size[1] < self.SIZE_MIN)):
235 return
236 c.size_hint_min = new_size
237 249
238 def zoom_in(self, value): 250 def zoom_in(self, value=0.2):
239 self.zoom += value 251 self.zoom += value
240 252
241 def zoom_out(self, value): 253 def zoom_out(self, value=0.2):
242 self.zoom -= value 254 self.zoom -= value
243 255
256 def zoom_orig(self):
257 self.zoom = 1.0
258
259 def zoom_fit(self):
260 widest = 0
261 for c in self.page_box:
262 pw = c.size[0]
263 if pw > widest:
264 widest = pw
265
266 viewport_width = self.scr.region[2]
267 self.zoom *= viewport_width/widest
268
244 def _event_handler(self, obj, src, tp, ev): 269 def _event_handler(self, obj, src, tp, ev):
245 if tp == EVAS_CALLBACK_KEY_UP: 270 if tp == EVAS_CALLBACK_KEY_UP:
246 if ev.key == "plus": 271 if ev.key == "plus":
247 self.zoom *= 1.2 272 self.zoom_in()
248 elif ev.key == "minus": 273 elif ev.key == "minus":
249 self.zoom *= 0.8 274 self.zoom_out()
250 275
251 @staticmethod 276 @staticmethod
252 def _viewport_in(obj, ei, n): 277 def _viewport_in(obj, ei, n):
@@ -345,6 +370,7 @@ class PageSmart(Smart):
345class Page(SmartObject): 370class Page(SmartObject):
346 371
347 SMART = PageSmart() 372 SMART = PageSmart()
373 SIZE_MIN = 50
348 374
349 def __init__(self, parent, doc_path, page_num, w, h, zoom=1.0): 375 def __init__(self, parent, doc_path, page_num, w, h, zoom=1.0):
350 self.doc_path = doc_path 376 self.doc_path = doc_path
@@ -370,6 +396,9 @@ class Page(SmartObject):
370 396
371 self.hq_img.on_image_preloaded_add(self.hq_preloaded, self.pv_img) 397 self.hq_img.on_image_preloaded_add(self.hq_preloaded, self.pv_img)
372 398
399 self.orig_w = float(w)
400 self.orig_h = float(h)
401
373 w = float(w) * zoom 402 w = float(w) * zoom
374 h = float(h) * zoom 403 h = float(h) * zoom
375 404
@@ -377,6 +406,17 @@ class Page(SmartObject):
377 406
378 self.pass_events = True 407 self.pass_events = True
379 408
409 def zoom_set(self, value):
410 old_size = self.size_hint_min
411 new_size = [i * value for i in (self.orig_w, self.orig_h)]
412 if (
413 (old_size[0] >= self.SIZE_MIN or
414 old_size[1] >= self.SIZE_MIN) and
415 (new_size[0] < self.SIZE_MIN or
416 new_size[1] < self.SIZE_MIN)):
417 return
418 self.size_hint_min = new_size
419
380 def hq_preloaded(self, hq_img, pv_img): 420 def hq_preloaded(self, hq_img, pv_img):
381 log.debug("preloaded hq %d", self.page_num) 421 log.debug("preloaded hq %d", self.page_num)
382 pv_img.hide() 422 pv_img.hide()