widgets: move PlainTextLabel and RichTextLabel to the text module
[git-cola.git] / cola / qtcompat.py
blob4ac8520c035c833051571cdcf8ecaf1701865ede
1 from qtpy import QtCore
2 from qtpy import QtGui
3 from qtpy import QtWidgets
4 from qtpy.QtCore import Qt
6 try:
7 from qtpy import PYQT4
8 except ImportError:
9 PYQT4 = False
11 from . import hotkeys
14 def patch(obj, attr, value):
15 if not hasattr(obj, attr):
16 setattr(obj, attr, value)
19 def install():
20 patch(QtWidgets.QGraphicsItem, 'mapRectToScene', _map_rect_to_scene)
21 patch(QtGui.QKeySequence, 'Preferences', hotkeys.PREFERENCES)
24 def add_search_path(prefix, path):
25 if hasattr(QtCore.QDir, 'addSearchPath'):
26 QtCore.QDir.addSearchPath(prefix, path)
29 def set_common_dock_options(window):
30 if not hasattr(window, 'setDockOptions'):
31 return
32 nested = QtWidgets.QMainWindow.AllowNestedDocks
33 tabbed = QtWidgets.QMainWindow.AllowTabbedDocks
34 animated = QtWidgets.QMainWindow.AnimatedDocks
35 window.setDockOptions(nested | tabbed | animated)
38 def _map_rect_to_scene(self, rect):
39 """Only available in newer PyQt4 versions"""
40 return self.sceneTransform().mapRect(rect)
43 def wheel_translation(event):
44 """Return the Tx Ty translation delta for a pan"""
45 if PYQT4:
46 tx = event.delta()
47 ty = 0.0
48 if event.orientation() == Qt.Vertical:
49 (tx, ty) = (ty, tx)
50 else:
51 angle = event.angleDelta()
52 tx = angle.x()
53 ty = angle.y()
54 return (tx, ty)
57 def wheel_delta(event):
58 """Return a single wheel delta"""
59 if PYQT4:
60 delta = event.delta()
61 else:
62 angle = event.angleDelta()
63 x = angle.x()
64 y = angle.y()
65 if abs(x) > abs(y):
66 delta = x
67 else:
68 delta = y
69 return delta