File: qtcompat.py

package info (click to toggle)
git-cola 4.13.0-1
  • links: PTS
  • area: main
  • in suites: sid
  • size: 6,480 kB
  • sloc: python: 36,938; sh: 304; makefile: 223; xml: 100; tcl: 62
file content (69 lines) | stat: -rw-r--r-- 1,659 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from qtpy import QtCore
from qtpy import QtGui
from qtpy import QtWidgets
from qtpy.QtCore import Qt

try:
    from qtpy import PYQT4
except ImportError:
    PYQT4 = False

from . import hotkeys


def patch(obj, attr, value):
    if not hasattr(obj, attr):
        setattr(obj, attr, value)


def install():
    patch(QtWidgets.QGraphicsItem, 'mapRectToScene', _map_rect_to_scene)
    patch(QtGui.QKeySequence, 'Preferences', hotkeys.PREFERENCES)


def add_search_path(prefix, path):
    if hasattr(QtCore.QDir, 'addSearchPath'):
        QtCore.QDir.addSearchPath(prefix, path)


def set_common_dock_options(window):
    if not hasattr(window, 'setDockOptions'):
        return
    nested = QtWidgets.QMainWindow.AllowNestedDocks
    tabbed = QtWidgets.QMainWindow.AllowTabbedDocks
    animated = QtWidgets.QMainWindow.AnimatedDocks
    window.setDockOptions(nested | tabbed | animated)


def _map_rect_to_scene(self, rect):
    """Only available in newer PyQt4 versions"""
    return self.sceneTransform().mapRect(rect)


def wheel_translation(event):
    """Return the (Tx, Ty) translation delta for a pan"""
    if PYQT4:
        tx = event.delta()
        ty = 0.0
        if event.orientation() == Qt.Vertical:
            (tx, ty) = (ty, tx)
    else:
        angle = event.angleDelta()
        tx = angle.x()
        ty = angle.y()
    return (tx, ty)


def wheel_delta(event):
    """Return a single wheel delta"""
    if PYQT4:
        delta = event.delta()
    else:
        angle = event.angleDelta()
        x = angle.x()
        y = angle.y()
        if abs(x) > abs(y):
            delta = x
        else:
            delta = y
    return delta