source: orange/Orange/OrangeCanvas/scheme/annotations.py @ 11202:dba4e6f2678e

Revision 11202:dba4e6f2678e, 4.3 KB checked in by Ales Erjavec <ales.erjavec@…>, 17 months ago (diff)

Added annotation font and color serialization.

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