source: orange/Orange/OrangeCanvas/gui/utils.py @ 11170:dcc12da18030

Revision 11170:dcc12da18030, 3.8 KB checked in by Ales Erjavec <ales.erjavec@…>, 18 months ago (diff)

Added fallback using 'setMask' to FramelessWindow.

Line 
1"""
2Helper utilities
3
4"""
5import os
6import sys
7
8from contextlib import contextmanager
9
10from PyQt4.QtGui import (
11    QWidget, QGradient, QLinearGradient, QRadialGradient, QBrush, QPainter,
12    QStyleOption, QStyle
13)
14
15import sip
16
17
18@contextmanager
19def updates_disabled(widget):
20    """Disable QWidget updates (using QWidget.setUpdatesEnabled)
21    """
22    old_state = widget.updatesEnabled()
23    widget.setUpdatesEnabled(False)
24    try:
25        yield
26    finally:
27        widget.setUpdatesEnabled(old_state)
28
29
30@contextmanager
31def signals_disabled(qobject):
32    """Disables signals on an instance of QObject.
33    """
34    old_state = qobject.signalsBlocked()
35    qobject.blockSignals(True)
36    try:
37        yield
38    finally:
39        qobject.blockSignals(old_state)
40
41
42def StyledWidget_paintEvent(self, event):
43    """A default styled QWidget subclass  paintEvent function.
44    """
45    opt = QStyleOption()
46    opt.init(self)
47    painter = QPainter(self)
48    self.style().drawPrimitive(QStyle.PE_Widget, opt, painter, self)
49
50
51class StyledWidget(QWidget):
52    """
53    """
54    paintEvent = StyledWidget_paintEvent
55
56
57def is_transparency_supported():
58    """Is window transparency supported by the current windowing system.
59
60    """
61    if sys.platform == "win32":
62        return is_dwm_compositing_enabled()
63    elif sys.platform == "cygwin":
64        return False
65    elif sys.platform == "darwin":
66        try:
67            # Test if Qt was build against X11.
68            from PyQt4.QtGui import QX11Info
69            return QX11Info.isCompositingManagerRunning()
70        except ImportError:
71            # Assuming Quartz compositor is running.
72            return True
73    elif sys.platform.startswith("linux"):
74        # TODO: wayland??
75        return is_x11_compositing_enabled()
76    elif sys.platform.startswith("freebsd"):
77        return is_x11_compositing_enabled()
78    elif os.name == "":
79        # Any other system (Win, OSX) is assumed to support it
80        return True
81
82
83def is_x11_compositing_enabled():
84    """Is X11 compositing manager running.
85    """
86    try:
87        from PyQt4.QtGui import QX11Info
88    except ImportError:
89        return False
90
91    return QX11Info.isCompositingManagerRunning()
92
93
94def is_dwm_compositing_enabled():
95    """Is Desktop Window Manager compositing (Aero) enabled.
96    """
97    import ctypes
98
99    enabled = ctypes.c_bool()
100    try:
101        DwmIsCompositionEnabled = ctypes.windll.dwmapi.DwmIsCompositionEnabled
102    except AttributeError:
103        # dwmapi or DwmIsCompositionEnabled is not present
104        return False
105
106    rval = DwmIsCompositionEnabled(ctypes.byref(enabled))
107
108    return rval == 0 and enabled.value
109
110
111def gradient_darker(grad, factor):
112    """Return a copy of the QGradient darkened by factor.
113
114    .. note:: Only QLinearGradeint and QRadialGradient are supported.
115
116    """
117    if type(grad) is QGradient:
118        if grad.type() == QGradient.LinearGradient:
119            grad = sip.cast(grad, QLinearGradient)
120        elif grad.type() == QGradient.RadialGradient:
121            grad = sip.cast(grad, QRadialGradient)
122
123    if isinstance(grad, QLinearGradient):
124        new_grad = QLinearGradient(grad.start(), grad.finalStop())
125    elif isinstance(grad, QRadialGradient):
126        new_grad = QRadialGradient(grad.center(), grad.radius(),
127                                   grad.focalPoint())
128    else:
129        raise TypeError
130
131    new_grad.setCoordinateMode(grad.coordinateMode())
132
133    for pos, color in grad.stops():
134        new_grad.setColorAt(pos, color.darker(factor))
135
136    return new_grad
137
138
139def brush_darker(brush, factor):
140    """Return a copy of the brush darkened by factor.
141    """
142    grad = brush.gradient()
143    if grad:
144        return QBrush(gradient_darker(grad, factor))
145    else:
146        brush = QBrush(brush)
147        brush.setColor(brush.color().darker(factor))
148        return brush
Note: See TracBrowser for help on using the repository browser.