source: orange/Orange/OrangeCanvas/scheme/annotations.py @ 11367:9630e1bb6112

Revision 11367:9630e1bb6112, 4.8 KB checked in by Ales Erjavec <ales.erjavec@…>, 14 months ago (diff)

Added rst documentation for scheme package.

Fixing docstrings in the process.

Line 
1"""
2==================
3Scheme Annotations
4==================
5
6"""
7
8from PyQt4.QtCore import QObject, QString
9from PyQt4.QtCore import pyqtSignal as Signal
10from PyQt4.QtCore import pyqtProperty as Property
11
12from .utils import check_type
13
14
15class BaseSchemeAnnotation(QObject):
16    """
17    Base class for scheme annotations.
18    """
19    # Signal emitted when the geometry of the annotation changes
20    geometry_changed = Signal()
21
22
23class SchemeArrowAnnotation(BaseSchemeAnnotation):
24    """
25    An arrow annotation in the scheme.
26    """
27
28    color_changed = Signal(unicode)
29
30    def __init__(self, start_pos, end_pos, color="red", anchor=None,
31                 parent=None):
32        BaseSchemeAnnotation.__init__(self, parent)
33        self.__start_pos = start_pos
34        self.__end_pos = end_pos
35        self.__color = color
36        self.__anchor = anchor
37
38    def set_line(self, start_pos, end_pos):
39        """
40        Set arrow lines start and end position (``(x, y)`` tuples).
41        """
42        if self.__start_pos != start_pos or self.__end_pos != end_pos:
43            self.__start_pos = start_pos
44            self.__end_pos = end_pos
45            self.geometry_changed.emit()
46
47    def start_pos(self):
48        """
49        Start position of the arrow (base point).
50        """
51        return self.__start_pos
52
53    start_pos = Property(tuple, fget=start_pos)
54
55    def end_pos(self):
56        """
57        End position of the arrow (arrow head points toward the end).
58        """
59        return self.__end_pos
60
61    end_pos = Property(tuple, fget=end_pos)
62
63    def set_geometry(self, (start_pos, end_pos)):
64        """
65        Set the geometry of the arrow as a start and end position tuples
66        (e.g. ``set_geometry(((0, 0), (100, 0))``).
67
68        """
69        self.set_line(start_pos, end_pos)
70
71    def geometry(self):
72        """
73        Return the start and end positions of the arrow.
74        """
75        return (self.start_pos, self.end_pos)
76
77    geometry = Property(tuple, fget=geometry, fset=set_geometry)
78
79    def set_color(self, color):
80        """
81        Set the fill color for the arrow as a string (`#RGB`, `#RRGGBB`,
82        `#RRRGGGBBB`, `#RRRRGGGGBBBB` format or one of SVG color keyword
83        names).
84
85        """
86        check_type(color, (basestring, QString))
87        color = unicode(color)
88        if self.__color != color:
89            self.__color = color
90            self.color_changed.emit(color)
91
92    def color(self):
93        """
94        The arrow's fill color.
95        """
96        return self.__color
97
98    color = Property(unicode, fget=color, fset=set_color)
99
100
101class SchemeTextAnnotation(BaseSchemeAnnotation):
102    """
103    Text annotation in the scheme.
104    """
105
106    # Signal emitted when the annotation text changes.
107    text_changed = Signal(unicode)
108
109    # Signal emitted when the annotation text font changes.
110    font_changed = Signal(dict)
111
112    def __init__(self, rect, text="", font=None, anchor=None, parent=None):
113        BaseSchemeAnnotation.__init__(self, parent)
114        self.__rect = rect
115        self.__text = text
116        self.__font = {} if font is None else font
117        self.__anchor = anchor
118
119    def set_rect(self, (x, y, w, h)):
120        """
121        Set the text geometry bounding rectangle (``(x, y, width, height)``
122        tuple).
123
124        """
125        rect = (x, y, w, h)
126        if self.__rect != rect:
127            self.__rect = rect
128            self.geometry_changed.emit()
129
130    def rect(self):
131        """
132        Text bounding rectangle
133        """
134        return self.__rect
135
136    rect = Property(tuple, fget=rect, fset=set_rect)
137
138    def set_geometry(self, rect):
139        """
140        Set the text geometry (same as ``set_rect``)
141        """
142        self.set_rect(rect)
143
144    def geometry(self):
145        """
146        Text annotation geometry (same as ``rect``
147        """
148        return self.rect
149
150    geometry = Property(tuple, fget=geometry, fset=set_geometry)
151
152    def set_text(self, text):
153        """
154        Set the annotation text.
155        """
156        check_type(text, (basestring, QString))
157        text = unicode(text)
158        if self.__text != text:
159            self.__text = text
160            self.text_changed.emit(text)
161
162    def text(self):
163        """
164        Annotation text.
165        """
166        return self.__text
167
168    text = Property(tuple, fget=text, fset=set_text)
169
170    def set_font(self, font):
171        """
172        Set the annotation's font as a dictionary of font properties
173        (at the moment only family and size are used).
174
175            >>> annotation.set_font({"family": "Helvetica", "size": 16})
176
177        """
178        check_type(font, dict)
179        font = dict(font)
180        if self.__font != font:
181            self.__font = font
182            self.font_changed.emit(font)
183
184    def font(self):
185        """
186        Annotation's font property dictionary.
187        """
188        return dict(self.__font)
189
190    font = Property(unicode, fget=font, fset=set_font)
Note: See TracBrowser for help on using the repository browser.