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