source: orange/Orange/OrangeCanvas/scheme/node.py @ 11302:8a0398ce4dad

Revision 11302:8a0398ce4dad, 4.6 KB checked in by Ales Erjavec <ales.erjavec@…>, 15 months ago (diff)

More informative exception messages.

Line 
1"""
2===========
3Scheme Node
4===========
5
6"""
7
8from PyQt4.QtCore import QObject
9from PyQt4.QtCore import pyqtSignal as Signal
10from PyQt4.QtCore import pyqtProperty as Property
11
12
13class SchemeNode(QObject):
14    """A widget node instantiation in the scheme.
15
16    Parameters
17    ----------
18    description : `WidgetDescription`
19        Widget description.
20    title : `str`
21        Node title string (if None description.name is used).
22    position : tuple
23        (x, y) two-tuple for node position in a visual display.
24    properties : `dict`
25        Additional instance properties (settings, widget geometry, ...)
26    parent : `QObject`
27        Parent object.
28
29    """
30
31    def __init__(self, description, title=None, position=None,
32                 properties=None, parent=None):
33        QObject.__init__(self, parent)
34        self.description = description
35
36        if title is None:
37            title = description.name
38
39        self.__title = title
40        self.__position = position or (0, 0)
41        self.__progress = -1
42        self.__processing_state = 0
43        self.properties = properties or {}
44
45    def input_channels(self):
46        """Return the input channels for the node.
47        """
48        return self.description.inputs
49
50    def output_channels(self):
51        """Return the output channels for the node.
52        """
53        return self.description.outputs
54
55    def input_channel(self, name):
56        """Return the input channel matching `name`. Raise an ValueError
57        if not found.
58
59        """
60        for channel in self.input_channels():
61            if channel.name == name:
62                return channel
63        raise ValueError("%r is not a valid input channel name for %r." % \
64                         (name, self.description.name))
65
66    def output_channel(self, name):
67        """Return the output channel matching `name`. Raise an ValueError
68        if not found.
69
70        """
71        for channel in self.output_channels():
72            if channel.name == name:
73                return channel
74        raise ValueError("%r is not a valid output channel name for %r." % \
75                         (name, self.description.name))
76
77    def __str__(self):
78        return u"SchemeNode(description_id=%s, title=%r, ...)" % \
79                (str(self.description.id), self.title)
80
81    def __repr__(self):
82        return str(self)
83
84    title_changed = Signal(unicode)
85    """The title of the node has changed"""
86
87    def set_title(self, title):
88        """Set the node's title
89        """
90        if self.__title != title:
91            self.__title = unicode(title)
92            self.title_changed.emit(self.__title)
93
94    def title(self):
95        """Return the nodes title.
96        """
97        return self.__title
98
99    title = Property(unicode, fset=set_title, fget=title)
100
101    position_changed = Signal(tuple)
102    """Position of the node in the scheme has changed"""
103
104    def set_position(self, pos):
105        """Set the position of the node
106        """
107        if self.__position != pos:
108            self.__position = pos
109            self.position_changed.emit(pos)
110
111    def position(self):
112        """(x, y) tuple containing the position of the node in the scheme.
113        """
114        return self.__position
115
116    position = Property(tuple, fset=set_position, fget=position)
117
118    progress_changed = Signal(float)
119    """Node's progress value has changed."""
120
121    def set_progress(self, value):
122        """Set the progress value.
123        """
124        if self.__progress != value:
125            self.__progress = value
126            self.progress_changed.emit(value)
127
128    def progress(self):
129        """Return the current progress value. -1 if progress is not set.
130        """
131        return self.__progress
132
133    progress = Property(float, fset=set_progress, fget=progress)
134
135    processing_state_changed = Signal(int)
136    """Node's processing state has changed."""
137
138    def set_processing_state(self, state):
139        """Set the node's processing state
140        """
141        if self.__processing_state != state:
142            self.__processing_state = state
143            self.processing_state_changed.emit(state)
144
145    def processing_state(self):
146        """Return the node's processing state, 0 for not processing, 1 the
147        node is busy.
148
149        """
150        return self.__processing_state
151
152    processing_state = Property(int, fset=set_processing_state,
153                                  fget=processing_state)
154
155    def set_tool_tip(self, tool_tip):
156        if self.__tool_tip != tool_tip:
157            self.__tool_tip = tool_tip
158
159    def tool_tip(self):
160        return self.__tool_tip
161
162    tool_tip = Property(str, fset=set_tool_tip,
163                          fget=tool_tip)
Note: See TracBrowser for help on using the repository browser.