Browse Source

Refactored Viewer double tap actions.

master
Bogdan Cordier 3 years ago
parent
commit
923a09aecd
2 changed files with 69 additions and 50 deletions
  1. 22
    0
      hadaly/viewer.kv
  2. 47
    50
      hadaly/viewer.py

+ 22
- 0
hadaly/viewer.kv View File

@@ -15,6 +15,28 @@
15 15
             id: _carousel
16 16
             direction: 'right'
17 17
             loop: False
18
+    TouchActionArea:
19
+        app: app
20
+        Widget:
21
+            name: 'ltop'
22
+            size_hint: 0.4,0.3
23
+            pos_hint: {'top': 1}
24
+        Widget:
25
+            name: 'lbottom'
26
+            size_hint: 0.4,0.3
27
+            pos_hint: {'bottom': 1}
28
+        Widget:
29
+            name: 'rtop'
30
+            size_hint: 0.4,0.3
31
+            pos_hint: {'top': 1, 'right': 1}
32
+        Widget:
33
+            name: 'rbottom'
34
+            size_hint: 0.4,0.3
35
+            pos_hint: {'bottom': 1, 'right': 1}
36
+        Widget:
37
+            name: 'center'
38
+            size_hint: 0.5,0.4
39
+            pos_hint: {'center_x': 0.5, 'center_y': 0.5}
18 40
 
19 41
 <ImgFullZoom>:
20 42
     pos_hint: 0, 0

+ 47
- 50
hadaly/viewer.py View File

@@ -5,6 +5,7 @@ from kivy.uix.scatter import Matrix
5 5
 from kivy.uix.scatterlayout import ScatterLayout
6 6
 from kivy.uix.image import Image, AsyncImage
7 7
 from kivy.uix.boxlayout import BoxLayout
8
+from kivy.uix.floatlayout import FloatLayout
8 9
 from kivy.uix.stencilview import StencilView
9 10
 from kivy.uix.popup import Popup
10 11
 from kivy.uix.button import ButtonBehavior
@@ -21,49 +22,6 @@ class ViewerScreen(Screen):
21 22
     app = ObjectProperty(None)
22 23
     dialog = ObjectProperty(None)
23 24
 
24
-    def on_touch_down(self, touch, *args):
25
-        bottom_right_corner = [(dp(self.size[0]) - dp(200), dp(200)), (dp(self.size[0]), 0)]
26
-        top_left_corner = [(0, dp(self.size[1])), (dp(200), dp(self.size[1]) - dp(200))]
27
-        top_right_corner = [(dp(self.size[0]) - dp(200), dp(self.size[1])), (dp(self.size[0]), dp(self.size[1]) - dp(200))]
28
-        center = [(dp(200), dp(200)), (dp(self.size[0]) - dp(200), dp(self.size[1]) - dp(200))]
29
-        if self.collide_point(*touch.pos):
30
-
31
-            if Vector.in_bbox(touch.pos, bottom_right_corner[0], bottom_right_corner[1]) \
32
-                    and touch.is_double_tap and len(self.app.presentation['slides']) > 0:
33
-
34
-                self.dialog.to_switch = True
35
-                self.dialog.title = _('Switch to...')
36
-                self.dialog.open()
37
-                return False
38
-
39
-            elif Vector.in_bbox(touch.pos, top_left_corner[0], top_left_corner[1]) \
40
-                    and touch.is_double_tap:
41
-
42
-                self.app.root.current = 'editor'
43
-                return True
44
-
45
-            elif Vector.in_bbox(touch.pos, top_right_corner[0], top_right_corner[1]) \
46
-                    and touch.is_double_tap and len(self.app.presentation['slides']) > 0:
47
-
48
-                # TODO: Switch to toolbox mode.
49
-                return False
50
-
51
-            elif Vector.in_bbox(touch.pos, center[0], center[1]) \
52
-                    and touch.is_double_tap and len(self.app.presentation['slides']) > 0:
53
-
54
-                if len(self.box.children) < 2:
55
-                    Logger.info('Application: Switching to compare mode.')
56
-                    self.dialog.to_switch = False
57
-                    self.dialog.title = _('Compare to...')
58
-                    self.dialog.open()
59
-                else:
60
-                    self.app.compare_slide(action='rm')
61
-                    touch.ungrab(self)
62
-
63
-                return True
64
-
65
-        return super(ViewerScreen, self).on_touch_down(touch)
66
-
67 25
     def on_pre_enter(self, *args):
68 26
         if not self.dialog:
69 27
             self.dialog = Factory.SlidesDialog()
@@ -94,6 +52,41 @@ class ViewerScreen(Screen):
94 52
             image = SlideBox(slide=slide)
95 53
             self.carousel.add_widget(image)
96 54
 
55
+class TouchActionArea(FloatLayout):
56
+
57
+    def on_touch_down(self, touch):
58
+
59
+        if self.collide_point(*touch.pos) and touch.is_double_tap and len(self.app.presentation['slides']) > 0:
60
+            try:
61
+                child = [child for child in self.children if child.collide_point(*touch.pos)][0]
62
+            except IndexError:
63
+                Logger.debug('Viewer: No TouchActionArea child touched.')
64
+                return
65
+            if child.name == 'center':
66
+                if len(self.app.root.current_screen.box.children) < 2:
67
+                        Logger.info('Application: Switching to compare mode.')
68
+                        self.parent.dialog.to_switch = False
69
+                        self.parent.dialog.title = _('Compare to...')
70
+                        self.parent.dialog.open()
71
+                else:
72
+                    self.app.compare_slide(action='rm')
73
+                    touch.ungrab(self)
74
+                return True
75
+
76
+            elif child.name == 'ltop':
77
+                self.app.root.current = 'editor'
78
+                return True
79
+            elif child.name == 'rtop':
80
+                pass
81
+            elif child.name == 'lbottom':
82
+                pass
83
+            elif child.name == 'rbottom':
84
+                self.parent.dialog.to_switch = True
85
+                self.parent.dialog.title = _('Switch to...')
86
+                self.parent.dialog.open()
87
+                return False
88
+
89
+        return super(TouchActionArea, self).on_touch_down(touch)
97 90
 
98 91
 class SlideBox(BoxLayout, StencilView):
99 92
     slide = DictProperty(None)
@@ -114,10 +107,12 @@ class SlideBox(BoxLayout, StencilView):
114 107
 
115 108
     def on_size(self, *args):
116 109
 
117
-        for child in self.float_layout.children:
118
-            if child.id == 'img_zoom':
119
-                child.size = (self.size[0] / 6, (self.size[0] / 6) / child.image_ratio)
120
-                child.pos = [self.pos[0], 0.05 * dp(self.size[1])]
110
+        try:
111
+            img_zoom = [child for child in self.float_layout.children if child.id == 'img_zoom'][0]
112
+            img_zoom.size = (self.size[0] / 6, (self.size[0] / 6) / img_zoom.image_ratio)
113
+            img_zoom.pos = [self.pos[0], 0.05 * dp(self.size[1])]
114
+        except IndexError:
115
+            pass
121 116
 
122 117
     def get_caption(self):
123 118
 
@@ -184,9 +179,11 @@ class SlideViewer(ScatterLayout):
184 179
                     self.parent.add_widget(thumb)
185 180
 
186 181
             elif min(self.bbox[0]) > 0:
187
-                for child in self.parent.children:
188
-                    if child.id == 'img_zoom':
189
-                        self.parent.remove_widget(child)
182
+                img_zoom = [child for child in self.parent.children if child.id == 'img_zoom']
183
+                try:
184
+                    self.parent.remove_widget(img_zoom[0])
185
+                except IndexError:
186
+                    pass
190 187
 
191 188
     def lock(self):
192 189
         if not self.locked:

Loading…
Cancel
Save