source: orange/Orange/OrangeCanvas/gui/utils.py @ 11100:cf6f6744dd9b

Revision 11100:cf6f6744dd9b, 2.5 KB checked in by Ales Erjavec <ales.erjavec@…>, 19 months ago (diff)

Added gui widget toolkit.

Line 
1"""
2Helper utilities
3
4"""
5from contextlib import contextmanager
6
7from PyQt4.QtGui import (
8    QWidget, QGradient, QLinearGradient, QRadialGradient, QBrush, QPainter,
9    QStyleOption, QStyle
10)
11
12import sip
13
14
15@contextmanager
16def updates_disabled(widget):
17    """Disable QWidget updates (using QWidget.setUpdatesEnabled)
18    """
19    old_state = widget.updatesEnabled()
20    widget.setUpdatesEnabled(False)
21    try:
22        yield
23    finally:
24        widget.setUpdatesEnabled(old_state)
25
26
27@contextmanager
28def signals_disabled(qobject):
29    """Disables signals on an instance of QObject.
30    """
31    old_state = qobject.signalsBlocked()
32    qobject.blockSignals(True)
33    try:
34        yield
35    finally:
36        qobject.blockSignals(old_state)
37
38
39def StyledWidget_paintEvent(self, event):
40    """A default styled QWidget subclass  paintEvent function.
41    """
42    opt = QStyleOption()
43    opt.init(self)
44    painter = QPainter(self)
45    self.style().drawPrimitive(QStyle.PE_Widget, opt, painter, self)
46
47
48class StyledWidget(QWidget):
49    """
50    """
51    paintEvent = StyledWidget_paintEvent
52
53
54def is_transparency_supported():
55    """Is window transparency supported by the current windowing system.
56
57    """
58    try:
59        from PyQt4.QtGui import QX11Info
60        return QX11Info.isCompositingManagerRunning()
61    except ImportError:
62        # Any other system (Win, OSX) is assumed to support it
63        return True
64
65
66def gradient_darker(grad, factor):
67    """Return a copy of the QGradient darkened by factor.
68
69    .. note:: Only QLinearGradeint and QRadialGradient are supported.
70
71    """
72    if type(grad) is QGradient:
73        if grad.type() == QGradient.LinearGradient:
74            grad = sip.cast(grad, QLinearGradient)
75        elif grad.type() == QGradient.RadialGradient:
76            grad = sip.cast(grad, QRadialGradient)
77
78    if isinstance(grad, QLinearGradient):
79        new_grad = QLinearGradient(grad.start(), grad.finalStop())
80    elif isinstance(grad, QRadialGradient):
81        new_grad = QRadialGradient(grad.center(), grad.radius(),
82                                   grad.focalPoint())
83    else:
84        raise TypeError
85
86    new_grad.setCoordinateMode(grad.coordinateMode())
87
88    for pos, color in grad.stops():
89        new_grad.setColorAt(pos, color.darker(factor))
90
91    return new_grad
92
93
94def brush_darker(brush, factor):
95    """Return a copy of the brush darkened by factor.
96    """
97    grad = brush.gradient()
98    if grad:
99        return QBrush(gradient_darker(grad, factor))
100    else:
101        brush = QBrush(brush)
102        brush.setColor(brush.color().darker(factor))
103        return brush
Note: See TracBrowser for help on using the repository browser.