source: orange/orange/OrangeWidgets/plot/owlegend.py @ 8423:38b77a0b9e4d

Revision 8423:38b77a0b9e4d, 3.9 KB checked in by Noughmad <Noughmad@…>, 22 months ago (diff)

Add a Point item, make Curve and NetworkCurve use it.

NetworkCurve's NodeItem is also changes to be a subclass of Point, and EdgeItem is a subclass of QGraphicsLineItem.

Line 
1
2
3from PyQt4.QtGui import QGraphicsItem, QGraphicsTextItem, QGraphicsRectItem, QColor
4from PyQt4.QtCore import QPointF, QRectF, Qt
5
6from owpoint import *
7from owcurve import *
8
9PointColor = 1
10PointSize = 2
11PointSymbol = 4
12
13class OWLegend(QGraphicsItem):
14    def __init__(self, scene):
15        QGraphicsItemGroup.__init__(self, None, scene)
16        self.curves = []
17        self.items = []
18        self.attributes = []
19        self.point_attrs = {}
20        self.point_vals = {}
21        self.default_values = {
22                               PointColor : Qt.black, 
23                               PointSize : 8, 
24                               PointSymbol : OWPoint.Ellipse
25                               }
26        self.box_rect = QRectF()
27        self.setFiltersChildEvents(True)
28       
29    def clear(self):
30        self.curves = []
31        self.point_attrs = {}
32        self.point_vals = {}
33        self.update()
34       
35
36    def add_curve(self, curve, attributes = []):
37        for point_attribute, data_attribute, value in attributes:
38            if point_attribute not in self.point_attrs:
39                self.point_attrs[point_attribute] = data_attribute
40               
41            if point_attribute == PointColor:
42                point_val = curve.color()
43            elif point_attribute == PointSize:
44                point_val = curve.pointSize()
45            else:
46                point_val = curve.symbol()
47
48            if not point_attribute in self.point_vals:
49                self.point_vals[point_attribute] = {}
50            self.point_vals[point_attribute][point_val] = value
51        self.curves.append(curve)
52        self.update()
53       
54    def update(self):
55        for i in self.items:
56            self.scene().removeItem(i)
57        del self.items[:]
58        y = 10
59        length = 0
60        if self.point_attrs:
61            ## Using the owplot API to specify paremeters
62            ## NOTE: The API is neither finished nor used
63            for p_a, d_a in self.point_attrs.iteritems():
64                ## We construct a separate box for each attribute
65                title_item = QGraphicsTextItem( d_a, self )
66                title_item.setPos(QPointF(10, y-10))
67                self.items.append(title_item)
68                y = y + 20
69                for p_v, d_v in self.point_vals[p_a].iteritems():
70                    color = p_v if p_a == PointColor else self.default_values[PointColor]
71                    symbol = p_v if p_a == PointSymbol else self.default_values[PointSymbol]
72                    size = p_v if p_a == PointSize else self.default_values[PointSize]
73                    self.items.append( point_item(10, y,  color, symbol, size, self) )
74                    text = QGraphicsTextItem(d_v, self)
75                    text.setPos(QPointF(20, y-10))
76                    self.items.append(text)
77                    y = y + 20
78                y = y + 10
79        else:
80            for curve in self.curves:
81                self.items.append(curve.pointItem(10, y, curve.pointSize(), self))
82                text = QGraphicsTextItem(curve.name, self)
83                length = max(length, text.boundingRect().width())
84                text.setPos(QPointF(20, y-10))
85                self.items.append(text)
86                y = y + 20
87            if self.curves:
88                self.box_rect = QRectF(0, 0, 20 + length, y-10)
89                box_item = QGraphicsRectItem(self.box_rect, self)
90                box_item.setBrush(Qt.white)
91                box_item.setZValue(-1)
92                self.items.append(box_item)
93            else:
94                box_rect = QRectF()
95               
96    def mouseMoveEvent(self, event):
97        self.setPos(self.pos() + event.scenePos() - event.lastScenePos())
98        event.accept()
99           
100    def mousePressEvent(self, event):
101        event.accept()
102
103    def boundingRect(self):
104        return self.box_rect
105       
106    def paint(self, painter, option, widget=None):
107        pass
108   
Note: See TracBrowser for help on using the repository browser.