~ubuntu-branches/ubuntu/trusty/onboard/trusty-proposed

« back to all changes in this revision

Viewing changes to Onboard/TouchHandles.py

  • Committer: Package Import Robot
  • Author(s): Francesco Fumanti
  • Date: 2012-12-12 21:33:43 UTC
  • mfrom: (1.2.7)
  • Revision ID: package-import@ubuntu.com-20121212213343-df85jzabyw4c886a
Tags: 0.99.0~alpha1~tr1190-0ubuntu1
* New upstream alpha release. (LP: #1089396)
  + Fix Onboard becoming empty when system font dpi changes

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
    corner_radius = 0     # radius of the outer corners (window edges)
25
25
 
26
26
    _size = (40, 40)
 
27
    _fallback_size = (40, 40)
 
28
    _hit_proximity_factor = 1.5
27
29
    _rect = None
28
30
    _scale = 1.0   # scale of handle relative to resize handles
29
31
    _handle_alpha = 0.45
30
32
    _shadow_alpha = 0.04
31
33
    _shadow_size = 8
32
34
    _shadow_offset = (0.0, 2.0)
 
35
    _screen_dpi = 96
33
36
 
34
37
    _handle_angles = {}  # dictionary at class scope!
35
38
 
 
39
    lock_x_axis = False
 
40
    lock_y_axis = False
 
41
 
36
42
    def __init__(self, id):
37
43
        self.id = id
38
44
 
109
115
        self._rect = Rect(x, y, w, h)
110
116
 
111
117
    def hit_test(self, point):
112
 
        rect = self.get_rect()
 
118
        rect   = self.get_rect().grow(self._hit_proximity_factor)
 
119
        radius = self.get_radius() * self._hit_proximity_factor
 
120
 
113
121
        if rect and rect.is_point_within(point):
114
122
            _win = self._window.get_window()
115
123
            if _win:
116
124
                context = _win.cairo_create()
117
 
                self._build_handle_path(context)
 
125
                self._build_handle_path(context, rect, radius)
118
126
                return context.in_fill(*point)
119
127
        return False
120
128
 
121
 
        radius = self.get_radius()
122
129
        xc, yc = rect.get_center()
123
130
        dx = xc - point[0]
124
131
        dy = yc - point[1]
198
205
        radius = self.get_radius()
199
206
        xc, yc = self.get_rect().get_center()
200
207
        scale = radius / 2.0 / self._scale * 1.2
201
 
        num_arrows = 4 if self.id == Handle.MOVE else 2
 
208
 
202
209
        angle = self.get_arrow_angle()
 
210
        if self.id == Handle.MOVE:
 
211
            num_arrows = 4
 
212
            if self.lock_x_axis:
 
213
                num_arrows -= 2
 
214
                angle += pi * 0.5
 
215
            if self.lock_y_axis:
 
216
                num_arrows -= 2
 
217
        else:
 
218
            num_arrows = 2
203
219
        angle_step = 2.0 * pi / num_arrows
204
220
 
205
 
        context.save()
206
 
 
207
221
        for i in range(num_arrows):
208
 
            m = cairo.Matrix()
209
 
            m.translate(xc, yc)
210
 
            m.rotate(angle + i * angle_step)
211
 
            m.scale(scale, scale)
 
222
            context.save()
 
223
 
 
224
            context.translate(xc, yc)
 
225
            context.rotate(angle + i * angle_step)
 
226
            context.scale(scale, scale)
212
227
 
213
228
            # arrow distance from center
214
229
            if self.id is Handle.MOVE:
215
 
                m.translate(0.9, 0)
 
230
                context.translate(0.9, 0)
216
231
            else:
217
 
                m.translate(0.30, 0)
 
232
                context.translate(0.30, 0)
218
233
 
219
 
            context.set_matrix(m)
220
234
            self._draw_arrow(context)
221
235
 
222
 
        context.restore()
 
236
            context.restore()
223
237
 
224
238
    def _draw_arrow(self, context):
225
239
        context.move_to( 0.0, -0.5)
234
248
        context.set_line_width(0)
235
249
        context.stroke()
236
250
 
237
 
    def _build_handle_path(self, context):
238
 
        rect = self.get_rect()
 
251
    def _build_handle_path(self, context, rect = None, radius = None):
 
252
        if rect is None:
 
253
            rect = self.get_rect()
 
254
        if radius is None:
 
255
            radius = self.get_radius()
239
256
        xc, yc = rect.get_center()
240
 
        radius = self.get_radius()
241
257
        corner_radius = self.corner_radius
242
258
 
243
259
        angle = self.get_arrow_angle()
291
307
    rect = None
292
308
 
293
309
    def __init__(self):
294
 
        self.handles = [TouchHandle(Handle.MOVE),
295
 
                        TouchHandle(Handle.NORTH_WEST),
296
 
                        TouchHandle(Handle.NORTH),
297
 
                        TouchHandle(Handle.NORTH_EAST),
298
 
                        TouchHandle(Handle.EAST),
299
 
                        TouchHandle(Handle.SOUTH_EAST),
300
 
                        TouchHandle(Handle.SOUTH),
301
 
                        TouchHandle(Handle.SOUTH_WEST),
302
 
                        TouchHandle(Handle.WEST)]
 
310
        self.handles = []
 
311
        self._handle_pool = [TouchHandle(Handle.MOVE),
 
312
                             TouchHandle(Handle.NORTH_WEST),
 
313
                             TouchHandle(Handle.NORTH),
 
314
                             TouchHandle(Handle.NORTH_EAST),
 
315
                             TouchHandle(Handle.EAST),
 
316
                             TouchHandle(Handle.SOUTH_EAST),
 
317
                             TouchHandle(Handle.SOUTH),
 
318
                             TouchHandle(Handle.SOUTH_WEST),
 
319
                             TouchHandle(Handle.WEST)]
 
320
 
 
321
    def set_active_handles(self, handle_ids):
 
322
        self.handles = []
 
323
        for handle in self._handle_pool:
 
324
            if handle.id in handle_ids:
 
325
                self.handles.append(handle)
303
326
 
304
327
    def set_window(self, window):
305
 
        for handle in self.handles:
 
328
        for handle in self._handle_pool:
306
329
            handle._window = window
307
330
 
308
331
    def update_positions(self, canvas_rect):
356
379
        for handle in self.handles:
357
380
            handle.corner_radius = corner_radius
358
381
 
 
382
    def set_monitor_dimensions(self, size_px, size_mm):
 
383
        min_monitor_mm = 50
 
384
        target_size_mm = (5, 5)
 
385
        min_size = TouchHandle._fallback_size
 
386
        
 
387
        if size_mm[0] < min_monitor_mm or \
 
388
           size_mm[1] < min_monitor_mm:
 
389
            w = 0
 
390
            h = 0
 
391
        else:
 
392
            w = size_px[0] / size_mm[0] * target_size_mm[0]
 
393
            h = size_px[0] / size_mm[0] * target_size_mm[0]
 
394
        size = max(w, min_size[0]), max(h, min_size[1])
 
395
        TouchHandle._size = size
 
396
 
 
397
    def lock_x_axis(self, lock):
 
398
        """ Set to False to constraint movement in x. """
 
399
        for handle in self.handles:
 
400
            handle.lock_x_axis = lock
 
401
 
 
402
    def lock_y_axis(self, lock):
 
403
        """ Set to True to constraint movement in y. """
 
404
        for handle in self.handles:
 
405
            handle.lock_y_axis = lock
 
406
 
 
407