source: orange/Orange/OrangeCanvas/canvas/items/utils.py @ 11172:b9add0151621

Revision 11172:b9add0151621, 1.7 KB checked in by Ales Erjavec <ales.erjavec@…>, 18 months ago (diff)

Compatibility fixes for PyQt4 < 4.9

Line 
1import numpy
2
3from PyQt4.QtGui import QColor, QRadialGradient, QGraphicsItem
4
5
6def saturated(color, factor=150):
7    """Return a saturated color.
8    """
9    h = color.hsvHueF()
10    s = color.hsvSaturationF()
11    v = color.valueF()
12    a = color.alphaF()
13    s = factor * s / 100.0
14    s = max(min(1.0, s), 0.0)
15    return QColor.fromHsvF(h, s, v, a).convertTo(color.spec())
16
17
18def sample_path(path, num=10):
19    """Sample `num` equidistant points from the `path` (`QPainterPath`).
20    """
21    space = numpy.linspace(0.0, 1.0, num, endpoint=True)
22    return [path.pointAtPercent(float(p)) for p in space]
23
24
25def radial_gradient(color, color_light=50):
26    """
27    radial_gradient(QColor, QColor)
28    radial_gradient(QColor, int)
29
30    Return a radial gradient. `color_light` can be a QColor or an int.
31    In the later case the light color is derived from `color` using
32    `saturated(color, color_light)`.
33
34    """
35    if not isinstance(color_light, QColor):
36        color_light = saturated(color, color_light)
37    gradient = QRadialGradient(0.5, 0.5, 0.5)
38    gradient.setColorAt(0.0, color_light)
39    gradient.setColorAt(0.5, color_light)
40    gradient.setColorAt(1.0, color)
41    gradient.setCoordinateMode(QRadialGradient.ObjectBoundingMode)
42    return gradient
43
44
45def toGraphicsObjectIfPossible(item):
46    """Return the item as a QGraphicsObject if possible.
47
48    This function is intended as a workaround for a problem with older
49    versions of PyQt (< 4.9), where methods returning 'QGraphicsItem *'
50    lose the type of the QGraphicsObject subclasses and instead return
51    generic QGraphicsItem wrappers.
52
53    """
54    obj = item.toGraphicsObject()
55    return item if obj is None else obj
Note: See TracBrowser for help on using the repository browser.