1
# ------------------------------------------------------------------------------
2
# Copyright (c) 2007, Riverbank Computing Limited
3
# All rights reserved.
4
#
5
# This software is provided without warranty under the terms of the BSD license.
6
# However, when used with the GPL version of PyQt the additional terms
7
# described in the PyQt GPL exception also apply
8

9
#
10
# Author: Riverbank Computing Limited
11
# ------------------------------------------------------------------------------
12

13 8
""" Trait definition for a PyQt-based color.
14
"""
15

16

17 8
from pyface.qt import QtGui
18

19 8
from traits.api import Trait, TraitError
20

21

22

23 8
def convert_to_color(object, name, value):
24
    """ Converts a number into a QColor object.
25
    """
26
    # Try the toolkit agnostic format.
27 8
    try:
28 8
        tup = eval(value)
29 8
    except:
30 8
        tup = value
31

32 11
    if isinstance(tup, tuple):
33 11
        if 3 <= len(tup) <= 4:
34 0
            try:
35 0
                color = QtGui.QColor(*tup)
36 0
            except TypeError:
37 0
                raise TraitError
38
        else:
39 0
            raise TraitError
40
    else:
41 11
        if isinstance(value, str):
42
            # Allow for spaces in the string value.
43 8
            value = value.replace(" ", "")
44

45
        # Let the standard ctors handle the value.
46 8
        try:
47 8
            color = QtGui.QColor(value)
48 0
        except TypeError:
49 0
            raise TraitError
50

51 11
    if not color.isValid():
52 8
        raise TraitError
53

54 8
    return color
55

56

57 8
convert_to_color.info = (
58
    "a string of the form (r,g,b) or (r,g,b,a) where r, "
59
    "g, b, and a are integers from 0 to 255, a QColor "
60
    "instance, a Qt.GlobalColor, an integer which in hex "
61
    "is of the form 0xRRGGBB, a string of the form #RGB, "
62
    "#RRGGBB, #RRRGGGBBB or #RRRRGGGGBBBB"
63
)
64

65
# -------------------------------------------------------------------------
66
#  Standard colors:
67
# -------------------------------------------------------------------------
68

69
# Note that this is slightly different from the wx implementation in that the
70
# names do not include spaces and the full set of SVG color keywords is
71
# supported.
72 8
standard_colors = {}
73 11
for name in QtGui.QColor.colorNames():
74 8
    standard_colors[str(name)] = QtGui.QColor(name)
75

76
# -------------------------------------------------------------------------
77
#  Callable that returns an instance of the PyQtToolkitEditorFactory for color
78
#  editors.
79
# -------------------------------------------------------------------------
80

81
### FIXME: We have declared the 'editor' to be a function instead of  the
82
# traitsui.qt4.color_editor.ToolkitEditorFactory class, since the
83
# latter is leading to too many circular imports. In the future, try to see if
84
# there is a better way to do this.
85

86

87 8
def get_color_editor(*args, **traits):
88 0
    from traitsui.qt4.color_editor import ToolkitEditorFactory
89

90 0
    return ToolkitEditorFactory(*args, **traits)
91

92

93 8
def PyQtColor(default="white", allow_none=False, **metadata):
94
    """ Defines PyQt-specific color traits.
95
    """
96 11
    if default is None:
97 8
        allow_none = True
98

99 11
    if allow_none:
100 8
        return Trait(
101
            default,
102
            None,
103
            standard_colors,
104
            convert_to_color,
105
            editor=get_color_editor,
106
            **metadata
107
        )
108

109 8
    return Trait(
110
        default,
111
        standard_colors,
112
        convert_to_color,
113
        editor=get_color_editor,
114
        **metadata
115
    )

Read our documentation on viewing source code .

Loading