@@ -1,180 +1,179 | |||
|
1 | 1 | #include "scatterchartitem_p.h" |
|
2 | 2 | #include "qscatterseries.h" |
|
3 | 3 | #include "chartpresenter_p.h" |
|
4 | 4 | #include <QPainter> |
|
5 | 5 | #include <QGraphicsScene> |
|
6 | 6 | |
|
7 | 7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
8 | 8 | |
|
9 | 9 | |
|
10 | 10 | |
|
11 | 11 | |
|
12 | 12 | |
|
13 | 13 | |
|
14 | 14 | |
|
15 | 15 | ScatterChartItem::ScatterChartItem(QScatterSeries *series, QGraphicsItem *parent) : |
|
16 | 16 | XYChartItem(series,parent), |
|
17 | 17 | m_series(series), |
|
18 | 18 | m_items(this), |
|
19 | 19 | m_shape(QScatterSeries::MarkerShapeRectangle), |
|
20 | 20 | m_size(10) |
|
21 | 21 | |
|
22 | 22 | { |
|
23 | 23 | Q_ASSERT(parent); |
|
24 | 24 | Q_ASSERT(series); |
|
25 | 25 | |
|
26 | 26 | QObject::connect(m_series,SIGNAL(updated()), this, SLOT(handleUpdated())); |
|
27 | 27 | |
|
28 | 28 | setZValue(ChartPresenter::ScatterSeriesZValue); |
|
29 | 29 | setFlags(QGraphicsItem::ItemHasNoContents); |
|
30 | 30 | setFlags(QGraphicsItem::ItemClipsChildrenToShape); |
|
31 | 31 | |
|
32 | 32 | handleUpdated(); |
|
33 | 33 | |
|
34 | 34 | m_items.setHandlesChildEvents(false); |
|
35 | 35 | |
|
36 | 36 | // TODO: how to draw a drop shadow? |
|
37 | 37 | // QGraphicsDropShadowEffect *dropShadow = new QGraphicsDropShadowEffect(); |
|
38 | 38 | // dropShadow->setOffset(2.0); |
|
39 | 39 | // dropShadow->setBlurRadius(2.0); |
|
40 | 40 | // setGraphicsEffect(dropShadow); |
|
41 | 41 | } |
|
42 | 42 | |
|
43 | 43 | |
|
44 | 44 | QRectF ScatterChartItem::boundingRect() const |
|
45 | 45 | { |
|
46 | 46 | return m_rect; |
|
47 | 47 | } |
|
48 | 48 | |
|
49 | 49 | void ScatterChartItem::createPoints(int count) |
|
50 | 50 | { |
|
51 | 51 | for (int i = 0; i < count; ++i) { |
|
52 | 52 | |
|
53 | 53 | QGraphicsItem *item; |
|
54 | 54 | |
|
55 | 55 | switch (m_shape) { |
|
56 | 56 | case QScatterSeries::MarkerShapeCircle:{ |
|
57 | 57 | QGraphicsEllipseItem* i = new QGraphicsEllipseItem(0,0,m_size,m_size); |
|
58 | 58 | const QRectF& rect = i->boundingRect(); |
|
59 | 59 | i->setPos(-rect.width()/2,-rect.height()/2); |
|
60 | 60 | item = new Marker(i,this); |
|
61 | 61 | break; |
|
62 | 62 | } |
|
63 | 63 | case QScatterSeries::MarkerShapeRectangle:{ |
|
64 | 64 | QGraphicsRectItem* i = new QGraphicsRectItem(0,0,m_size,m_size); |
|
65 | 65 | i->setPos(-m_size/2,-m_size/2); |
|
66 | 66 | item = new Marker(i,this); |
|
67 | 67 | break; |
|
68 | 68 | } |
|
69 | 69 | default: |
|
70 | 70 | qWarning()<<"Unsupported marker type"; |
|
71 | 71 | break; |
|
72 | 72 | |
|
73 | 73 | } |
|
74 | 74 | m_items.addToGroup(item); |
|
75 | 75 | } |
|
76 | 76 | } |
|
77 | 77 | |
|
78 | 78 | void ScatterChartItem::deletePoints(int count) |
|
79 | 79 | { |
|
80 | 80 | QList<QGraphicsItem *> items = m_items.childItems(); |
|
81 | 81 | |
|
82 | 82 | for (int i = 0; i < count; ++i) { |
|
83 | 83 | delete(items.takeLast()); |
|
84 | 84 | } |
|
85 | 85 | } |
|
86 | 86 | |
|
87 | 87 | void ScatterChartItem::markerSelected(Marker* marker) |
|
88 | 88 | { |
|
89 | 89 | emit XYChartItem::clicked(QPointF(m_series->x(marker->index()), m_series->y(marker->index()))); |
|
90 | 90 | } |
|
91 | 91 | |
|
92 | 92 | void ScatterChartItem::setLayout(QVector<QPointF>& points) |
|
93 | 93 | { |
|
94 | 94 | if(points.size()==0) |
|
95 | 95 | { |
|
96 | 96 | XYChartItem::setLayout(points); |
|
97 | 97 | return; |
|
98 | 98 | } |
|
99 | 99 | |
|
100 | 100 | int diff = XYChartItem::points().size() - points.size(); |
|
101 | 101 | |
|
102 | 102 | if(diff>0) { |
|
103 | 103 | deletePoints(diff); |
|
104 | 104 | } |
|
105 | 105 | else if(diff<0) { |
|
106 | 106 | createPoints(-diff); |
|
107 | 107 | } |
|
108 | 108 | |
|
109 | 109 | if(diff!=0) handleUpdated(); |
|
110 | 110 | |
|
111 | 111 | QList<QGraphicsItem*> items = m_items.childItems(); |
|
112 | 112 | |
|
113 | 113 | for(int i=0; i< points.size();i++) { |
|
114 | 114 | Marker* item = static_cast<Marker*>(items.at(i)); |
|
115 | 115 | const QPointF& point = points.at(i); |
|
116 | 116 | const QRectF& rect = item->boundingRect(); |
|
117 | 117 | item->setIndex(i); |
|
118 | 118 | item->setPos(point.x()-rect.width()/2,point.y()-rect.height()/2); |
|
119 | 119 | if(!clipRect().contains(point)) { |
|
120 | 120 | item->setVisible(false); |
|
121 | 121 | } |
|
122 | 122 | else { |
|
123 | 123 | item->setVisible(true); |
|
124 | 124 | } |
|
125 | 125 | } |
|
126 | 126 | |
|
127 | 127 | prepareGeometryChange(); |
|
128 | 128 | m_rect = clipRect(); |
|
129 | 129 | XYChartItem::setLayout(points); |
|
130 | 130 | } |
|
131 | 131 | |
|
132 | 132 | |
|
133 | 133 | void ScatterChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
134 | 134 | { |
|
135 | 135 | Q_UNUSED(painter) |
|
136 | 136 | Q_UNUSED(option) |
|
137 | 137 | Q_UNUSED(widget) |
|
138 | 138 | } |
|
139 | 139 | |
|
140 | 140 | void ScatterChartItem::setPen(const QPen& pen) |
|
141 | 141 | { |
|
142 | 142 | foreach(QGraphicsItem* item , m_items.childItems()) { |
|
143 | 143 | static_cast<Marker*>(item)->setPen(pen); |
|
144 | 144 | } |
|
145 | 145 | } |
|
146 | 146 | |
|
147 | 147 | void ScatterChartItem::setBrush(const QBrush& brush) |
|
148 | 148 | { |
|
149 | 149 | foreach(QGraphicsItem* item , m_items.childItems()) { |
|
150 | 150 | static_cast<Marker*>(item)->setBrush(brush); |
|
151 | 151 | } |
|
152 | 152 | } |
|
153 | 153 | |
|
154 | 154 | void ScatterChartItem::handleUpdated() |
|
155 | 155 | { |
|
156 | 156 | |
|
157 | 157 | int count = m_items.childItems().count(); |
|
158 | 158 | |
|
159 | 159 | if(count==0) return; |
|
160 | 160 | |
|
161 | 161 | bool recreate = m_size != m_series->size() || m_shape != m_series->shape(); |
|
162 | 162 | |
|
163 | 163 | //TODO: only rewrite on size change |
|
164 | 164 | |
|
165 | 165 | m_size = m_series->size(); |
|
166 | 166 | m_shape = m_series->shape(); |
|
167 | 167 | |
|
168 | 168 | if(recreate){ |
|
169 | 169 | deletePoints(count); |
|
170 | 170 | createPoints(count); |
|
171 | 171 | } |
|
172 | 172 | |
|
173 | 173 | setPen(m_series->pen()); |
|
174 | 174 | setBrush(m_series->brush()); |
|
175 | ||
|
176 | 175 | } |
|
177 | 176 | |
|
178 | 177 | #include "moc_scatterchartitem_p.cpp" |
|
179 | 178 | |
|
180 | 179 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,121 +1,120 | |||
|
1 | 1 | #ifndef SCATTERPRESENTER_H |
|
2 | 2 | #define SCATTERPRESENTER_H |
|
3 | 3 | |
|
4 | 4 | #include "qchartglobal.h" |
|
5 | 5 | #include "xychartitem_p.h" |
|
6 | 6 | #include <QGraphicsEllipseItem> |
|
7 | 7 | #include <QPen> |
|
8 | 8 | |
|
9 | 9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
10 | 10 | |
|
11 | 11 | class QScatterSeries; |
|
12 | 12 | class Marker; |
|
13 | 13 | |
|
14 | 14 | class ScatterChartItem : public XYChartItem |
|
15 | 15 | { |
|
16 | 16 | Q_OBJECT |
|
17 | 17 | public: |
|
18 | 18 | explicit ScatterChartItem(QScatterSeries *series, QGraphicsItem *parent = 0); |
|
19 | 19 | |
|
20 | 20 | public: |
|
21 | 21 | //from QGraphicsItem |
|
22 | 22 | QRectF boundingRect() const; |
|
23 | 23 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); |
|
24 | 24 | |
|
25 | 25 | void setPen(const QPen& pen); |
|
26 | 26 | void setBrush(const QBrush& brush); |
|
27 | 27 | |
|
28 | 28 | void markerSelected(Marker* item); |
|
29 | 29 | |
|
30 | 30 | public slots: |
|
31 | 31 | void handleUpdated(); |
|
32 | 32 | |
|
33 | 33 | private: |
|
34 | 34 | void createPoints(int count); |
|
35 | 35 | void deletePoints(int count); |
|
36 | 36 | |
|
37 | 37 | protected: |
|
38 | 38 | void setLayout(QVector<QPointF>& points); |
|
39 | void mousePressEvent( QGraphicsSceneMouseEvent * event ); | |
|
40 | 39 | |
|
41 | 40 | private: |
|
42 | 41 | QScatterSeries *m_series; |
|
43 | 42 | QGraphicsItemGroup m_items; |
|
44 | 43 | int m_shape; |
|
45 | 44 | int m_size; |
|
46 | 45 | QRectF m_rect; |
|
47 | 46 | |
|
48 | 47 | }; |
|
49 | 48 | |
|
50 | 49 | |
|
51 | 50 | class Marker: public QAbstractGraphicsShapeItem |
|
52 | 51 | { |
|
53 | 52 | |
|
54 | 53 | public: |
|
55 | 54 | |
|
56 | 55 | Marker(QAbstractGraphicsShapeItem* item , ScatterChartItem* parent):QAbstractGraphicsShapeItem(0),m_item(item),m_parent(parent) |
|
57 | 56 | { |
|
58 | 57 | }; |
|
59 | 58 | |
|
60 | 59 | ~Marker() |
|
61 | 60 | { |
|
62 | 61 | delete m_item; |
|
63 | 62 | } |
|
64 | 63 | |
|
65 | 64 | void setIndex(int index) |
|
66 | 65 | { |
|
67 | 66 | m_index=index; |
|
68 | 67 | } |
|
69 | 68 | |
|
70 | 69 | int index() const |
|
71 | 70 | { |
|
72 | 71 | return m_index; |
|
73 | 72 | } |
|
74 | 73 | |
|
75 | 74 | QPainterPath shape() const |
|
76 | 75 | { |
|
77 | 76 | return m_item->shape(); |
|
78 | 77 | } |
|
79 | 78 | |
|
80 | 79 | QRectF boundingRect() const |
|
81 | 80 | { |
|
82 | 81 | return m_item->boundingRect(); |
|
83 | 82 | } |
|
84 | 83 | |
|
85 | 84 | bool contains(const QPointF &point) const |
|
86 | 85 | { |
|
87 | 86 | return m_item->contains(point); |
|
88 | 87 | } |
|
89 | 88 | |
|
90 | 89 | void setPen(const QPen& pen) |
|
91 | 90 | { |
|
92 | 91 | m_item->setPen(pen); |
|
93 | 92 | } |
|
94 | 93 | |
|
95 | 94 | void setBrush(const QBrush& brush) |
|
96 | 95 | { |
|
97 | 96 | m_item->setBrush(brush); |
|
98 | 97 | } |
|
99 | 98 | |
|
100 | 99 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
101 | 100 | { |
|
102 | 101 | m_item->paint(painter,option,widget); |
|
103 | 102 | } |
|
104 | 103 | |
|
105 | 104 | protected: |
|
106 | 105 | |
|
107 | 106 | void mousePressEvent(QGraphicsSceneMouseEvent *event) |
|
108 | 107 | { |
|
109 | 108 | Q_UNUSED(event) |
|
110 | 109 | m_parent->markerSelected(this); |
|
111 | 110 | } |
|
112 | 111 | |
|
113 | 112 | private: |
|
114 | 113 | QAbstractGraphicsShapeItem* m_item; |
|
115 | 114 | ScatterChartItem* m_parent; |
|
116 | 115 | int m_index; |
|
117 | 116 | }; |
|
118 | 117 | |
|
119 | 118 | QTCOMMERCIALCHART_END_NAMESPACE |
|
120 | 119 | |
|
121 | 120 | #endif // SCATTERPRESENTER_H |
General Comments 0
You need to be logged in to leave comments.
Login now