##// END OF EJS Templates
Adds missing scatter intercation implementation...
Michal Klocek -
r541:4eee071cd5a1
parent child
Show More
@@ -1,6 +1,7
1 #include "mainwindow.h"
1 #include "mainwindow.h"
2 #include <qchartglobal.h>
2 #include <qchartglobal.h>
3 #include <qchartview.h>
3 #include <qchartview.h>
4 #include <qchartaxis.h>
4 #include <QDebug>
5 #include <QDebug>
5
6
6 QTCOMMERCIALCHART_USE_NAMESPACE
7 QTCOMMERCIALCHART_USE_NAMESPACE
@@ -9,7 +10,7 MainWindow::MainWindow(QWidget *parent)
9 : QMainWindow(parent)
10 : QMainWindow(parent)
10 {
11 {
11 QChartView *chartView = new QChartView(this);
12 QChartView *chartView = new QChartView(this);
12 chartView->setChartTitle("Click to play with points");
13 chartView->setChartTitle("Click to remove scatter point");
13 chartView->setRenderHint(QPainter::Antialiasing);
14 chartView->setRenderHint(QPainter::Antialiasing);
14 setCentralWidget(chartView);
15 setCentralWidget(chartView);
15
16
@@ -19,33 +20,19 MainWindow::MainWindow(QWidget *parent)
19 *m_scatter << QPointF(x, y);
20 *m_scatter << QPointF(x, y);
20 }
21 }
21 }
22 }
22 chartView->addSeries(m_scatter);
23
23
24 // Add two more series
24 chartView->addSeries(m_scatter);
25 m_scatter2 = new QScatterSeries();
25 chartView->axisX()->setRange(0,4.5);
26 chartView->addSeries(m_scatter2);
26 chartView->axisY()->setRange(0,4.5);
27 m_scatter3 = new QScatterSeries();
28 chartView->addSeries(m_scatter3);
29
27
30 connect(m_scatter, SIGNAL(clicked(QPointF)), this, SLOT(clickPoint(QPointF)));
28 connect(m_scatter, SIGNAL(clicked(const QPointF&)), this, SLOT(handleClickedPoint(const QPointF&)));
31 }
29 }
32
30
33 MainWindow::~MainWindow()
31 MainWindow::~MainWindow()
34 {
32 {
35 }
33 }
36
34
37 void MainWindow::clickPoint(QPointF coordinate)
35 void MainWindow::handleClickedPoint(const QPointF& point)
38 {
36 {
39 // Remove the clicked point from the series and add points to the two other series we have
37 m_scatter->remove(point);
40 //TODO: fix me
41 /*
42 int index = m_scatter->closestPoint(coordinate);
43 QPointF point = m_scatter->data().at(index);
44 Q_ASSERT(m_scatter->removeAt(index));
45 point.rx() += 0.25;
46 point.ry() += 0.25;
47 *m_scatter2 << point;
48 point.ry() -= 0.25;
49 *m_scatter3 << point;
50 */
51 }
38 }
@@ -16,7 +16,7 public:
16 ~MainWindow();
16 ~MainWindow();
17
17
18 private Q_SLOTS:
18 private Q_SLOTS:
19 void clickPoint(QPointF coordinate);
19 void handleClickedPoint(const QPointF& point);
20
20
21 private:
21 private:
22 QScatterSeries *m_scatter;
22 QScatterSeries *m_scatter;
@@ -169,13 +169,19 void ChartTheme::decorate(ScatterChartItem* item, QScatterSeries* series, int in
169 Q_ASSERT(item);
169 Q_ASSERT(item);
170 Q_ASSERT(series);
170 Q_ASSERT(series);
171
171
172 // Use a base color for brush
172 QPen pen;
173 item->setBrush(m_seriesColors.at(index % m_seriesColors.size()));
173 QBrush brush;
174
175 if (pen == series->pen()) {
176 pen.setColor(colorAt(m_seriesGradients.at(count % m_seriesGradients.size()), 1.0));
177 pen.setWidthF(2);
178 series->setPen(pen);
179 }
174
180
175 // Take pen near from gradient start, effectively using a lighter color for outline
181 if (brush == series->brush()) {
176 QPen pen(QBrush(Qt::SolidPattern), 3);
182 QBrush brush(m_seriesColors.at(count % m_seriesColors.size()));
177 pen.setColor(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1.0));
183 series->setBrush(brush);
178 item->setPen(pen);
184 }
179 }
185 }
180
186
181 void ChartTheme::decorate(PiePresenter* item, QPieSeries* series, int index)
187 void ChartTheme::decorate(PiePresenter* item, QPieSeries* series, int index)
@@ -27,13 +27,8
27
27
28 This enum describes the shape used when rendering marker items.
28 This enum describes the shape used when rendering marker items.
29
29
30 \value MarkerShapeDefault
31 \value MarkerShapeX
32 \value MarkerShapeRectangle
33 \value MarkerShapeRoundedRectangle
34 \value MarkerShapeTiltedRectangle
35 \value MarkerShapeTriangle
36 \value MarkerShapeCircle
30 \value MarkerShapeCircle
31 \value MarkerShapeRectangle
37 */
32 */
38
33
39 /*!
34 /*!
@@ -54,7 +49,7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
54 */
49 */
55 QScatterSeries::QScatterSeries(QObject *parent) :
50 QScatterSeries::QScatterSeries(QObject *parent) :
56 QXYSeries(parent),
51 QXYSeries(parent),
57 m_shape(QScatterSeries::MarkerShapeDefault),
52 m_shape(QScatterSeries::MarkerShapeCircle),
58 m_size(9.0)
53 m_size(9.0)
59 {
54 {
60 }
55 }
@@ -15,15 +15,8 class QTCOMMERCIALCHART_EXPORT QScatterSeries : public QXYSeries
15
15
16 public:
16 public:
17 enum MarkerShape {
17 enum MarkerShape {
18 // TODO: to be defined by the graphics design
18 MarkerShapeCircle,
19 // TODO: marker shapes: "x", star, rectangle, tilted rect, triangle, circle, dot
19 MarkerShapeRectangle
20 MarkerShapeDefault = 0,
21 MarkerShapeX,
22 MarkerShapeRectangle,
23 MarkerShapeRoundedRectangle,
24 MarkerShapeTiltedRectangle,
25 MarkerShapeTriangle,
26 MarkerShapeCircle
27 };
20 };
28
21
29 public:
22 public:
@@ -23,7 +23,8 ScatterChartItem::ScatterChartItem(QScatterSeries *series, QGraphicsItem *parent
23 Q_ASSERT(parent);
23 Q_ASSERT(parent);
24 Q_ASSERT(series);
24 Q_ASSERT(series);
25
25
26 connect(m_series,SIGNAL(updated()), this, SLOT(handleUpdated()));
26 QObject::connect(m_series,SIGNAL(updated()), this, SLOT(handleUpdated()));
27 QObject::connect(this,SIGNAL(clicked(const QPointF&)), m_series, SIGNAL(clicked(const QPointF&)));
27
28
28 setZValue(ChartPresenter::ScatterSeriesZValue);
29 setZValue(ChartPresenter::ScatterSeriesZValue);
29 setFlags(QGraphicsItem::ItemHasNoContents);
30 setFlags(QGraphicsItem::ItemHasNoContents);
@@ -31,6 +32,8 ScatterChartItem::ScatterChartItem(QScatterSeries *series, QGraphicsItem *parent
31
32
32 handleUpdated();
33 handleUpdated();
33
34
35 m_items.setHandlesChildEvents(false);
36
34 // TODO: how to draw a drop shadow?
37 // TODO: how to draw a drop shadow?
35 // QGraphicsDropShadowEffect *dropShadow = new QGraphicsDropShadowEffect();
38 // QGraphicsDropShadowEffect *dropShadow = new QGraphicsDropShadowEffect();
36 // dropShadow->setOffset(2.0);
39 // dropShadow->setOffset(2.0);
@@ -51,27 +54,23 void ScatterChartItem::createPoints(int count)
51 QGraphicsItem *item;
54 QGraphicsItem *item;
52
55
53 switch (m_shape) {
56 switch (m_shape) {
54 case QScatterSeries::MarkerShapeDefault:
57 case QScatterSeries::MarkerShapeCircle:{
55 // Fallthrough, defaults to circle
58 QGraphicsEllipseItem* i = new QGraphicsEllipseItem(0,0,m_size,m_size);
56 case QScatterSeries::MarkerShapeCircle:
59 const QRectF& rect = i->boundingRect();
57 item = new QGraphicsEllipseItem(0,0,m_size,m_size);
60 i->setPos(-rect.width()/2,-rect.height()/2);
58 break;
61 item = new Marker(i,this);
59 case QScatterSeries::MarkerShapeRectangle:
60 item = new QGraphicsRectItem(0,0,m_size,m_size);
61 break;
62 break;
62 case QScatterSeries::MarkerShapeRoundedRectangle:
63 }
63 //m_path.addRoundedRect(x, y, size, size, size / 4.0, size / 4.0);
64 case QScatterSeries::MarkerShapeRectangle:{
65 QGraphicsRectItem* i = new QGraphicsRectItem(0,0,m_size,m_size);
66 i->setPos(-m_size/2,-m_size/2);
67 item = new Marker(i,this);
64 break;
68 break;
65 case QScatterSeries::MarkerShapeTiltedRectangle:
69 }
66 // TODO: tilt the rectangle
70 default:
67 //m_path.addRect(x, y, size, size);
71 qWarning()<<"Unsupported marker type";
68 //break;
69 case QScatterSeries::MarkerShapeTriangle:
70 //QPolygonF polygon;
71 //polygon << QPointF(0.0, -size) << QPointF(size / 2.0, 0.0) << QPointF(-size / 2, 0.0);
72 // TODO: the position is not exactly right...
73 //m_path.addPolygon(polygon.translated(x + size / 2.0, y + size));
74 break;
72 break;
73
75 }
74 }
76 m_items.addToGroup(item);
75 m_items.addToGroup(item);
77 }
76 }
@@ -86,18 +85,10 void ScatterChartItem::deletePoints(int count)
86 }
85 }
87 }
86 }
88
87
89 /*
88 void ScatterChartItem::markerSelected(Marker* marker)
90
91 void ScatterChartItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
92 {
89 {
93
90 emit clicked(QPointF(m_series->x(marker->index()), m_series->y(marker->index())));
94 QPointF clickedPoint(
95 m_minX + (event->lastPos().x() / m_clippingRect.width()) * (m_maxX-m_minX),
96 m_maxY - (event->lastPos().y() / m_clippingRect.height()) * (m_maxY-m_minY));
97 emit clicked(clickedPoint);
98
99 }
91 }
100 */
101
92
102 void ScatterChartItem::setGeometry(QVector<QPointF>& points)
93 void ScatterChartItem::setGeometry(QVector<QPointF>& points)
103 {
94 {
@@ -117,9 +108,10 void ScatterChartItem::setGeometry(QVector<QPointF>& points)
117 QList<QGraphicsItem*> items = m_items.childItems();
108 QList<QGraphicsItem*> items = m_items.childItems();
118
109
119 for(int i=0; i< points.size();i++) {
110 for(int i=0; i< points.size();i++) {
120 QGraphicsItem* item = items.at(i);
111 Marker* item = static_cast<Marker*>(items.at(i));
121 const QPointF& point = points.at(i);
112 const QPointF& point = points.at(i);
122 const QRectF& rect = item->boundingRect();
113 const QRectF& rect = item->boundingRect();
114 item->setIndex(i);
123 item->setPos(point.x()-rect.width()/2,point.y()-rect.height()/2);
115 item->setPos(point.x()-rect.width()/2,point.y()-rect.height()/2);
124 if(!clipRect().contains(point)) {
116 if(!clipRect().contains(point)) {
125 item->setVisible(false);
117 item->setVisible(false);
@@ -145,14 +137,14 void ScatterChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *
145 void ScatterChartItem::setPen(const QPen& pen)
137 void ScatterChartItem::setPen(const QPen& pen)
146 {
138 {
147 foreach(QGraphicsItem* item , m_items.childItems()) {
139 foreach(QGraphicsItem* item , m_items.childItems()) {
148 static_cast<QAbstractGraphicsShapeItem*>(item)->setPen(pen);
140 static_cast<Marker*>(item)->setPen(pen);
149 }
141 }
150 }
142 }
151
143
152 void ScatterChartItem::setBrush(const QBrush& brush)
144 void ScatterChartItem::setBrush(const QBrush& brush)
153 {
145 {
154 foreach(QGraphicsItem* item , m_items.childItems()) {
146 foreach(QGraphicsItem* item , m_items.childItems()) {
155 static_cast<QAbstractGraphicsShapeItem*>(item)->setBrush(brush);
147 static_cast<Marker*>(item)->setBrush(brush);
156 }
148 }
157 }
149 }
158
150
@@ -9,6 +9,7
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10
10
11 class QScatterSeries;
11 class QScatterSeries;
12 class Marker;
12
13
13 class ScatterChartItem : public XYChartItem
14 class ScatterChartItem : public XYChartItem
14 {
15 {
@@ -24,8 +25,10 public:
24 void setPen(const QPen& pen);
25 void setPen(const QPen& pen);
25 void setBrush(const QBrush& brush);
26 void setBrush(const QBrush& brush);
26
27
28 void markerSelected(Marker* item);
29
27 signals:
30 signals:
28 void clicked(QPointF coordinates);
31 void clicked(const QPointF& point);
29
32
30 public slots:
33 public slots:
31 void handleUpdated();
34 void handleUpdated();
@@ -46,6 +49,74 private:
46
49
47 };
50 };
48
51
52
53 class Marker: public QAbstractGraphicsShapeItem
54 {
55
56 public:
57
58 Marker(QAbstractGraphicsShapeItem* item , ScatterChartItem* parent):QAbstractGraphicsShapeItem(0),m_item(item),m_parent(parent)
59 {
60 };
61
62 ~Marker()
63 {
64 delete m_item;
65 }
66
67 void setIndex(int index)
68 {
69 m_index=index;
70 }
71
72 int index() const
73 {
74 return m_index;
75 }
76
77 QPainterPath shape() const
78 {
79 return m_item->shape();
80 }
81
82 QRectF boundingRect() const
83 {
84 return m_item->boundingRect();
85 }
86
87 bool contains(const QPointF &point) const
88 {
89 return m_item->contains(point);
90 }
91
92 void setPen(const QPen& pen)
93 {
94 m_item->setPen(pen);
95 }
96
97 void setBrush(const QBrush& brush)
98 {
99 m_item->setBrush(brush);
100 }
101
102 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
103 {
104 m_item->paint(painter,option,widget);
105 }
106
107 protected:
108
109 void mousePressEvent( QGraphicsSceneMouseEvent * event )
110 {
111 m_parent->markerSelected(this);
112 }
113
114 private:
115 QAbstractGraphicsShapeItem* m_item;
116 ScatterChartItem* m_parent;
117 int m_index;
118 };
119
49 QTCOMMERCIALCHART_END_NAMESPACE
120 QTCOMMERCIALCHART_END_NAMESPACE
50
121
51 #endif // SCATTERPRESENTER_H
122 #endif // SCATTERPRESENTER_H
@@ -111,11 +111,16 void QXYSeries::replace(const QPointF& point)
111 }
111 }
112
112
113 /*!
113 /*!
114 Removes current \a x and y value.
114 Removes current \a x and \a y value.
115 */
115 */
116 void QXYSeries::remove(qreal x)
116 void QXYSeries::remove(qreal x,qreal y)
117 {
117 {
118 int index = m_x.indexOf(x);
118 int index =-1;
119 do{
120 index = m_x.indexOf(x,index+1);
121 }while(index !=-1 && m_y.at(index)!=y);
122
123 if(index==-1) return;
119 emit pointRemoved(index);
124 emit pointRemoved(index);
120 m_x.remove(index);
125 m_x.remove(index);
121 m_y.remove(index);
126 m_y.remove(index);
@@ -126,7 +131,7 void QXYSeries::remove(qreal x)
126 */
131 */
127 void QXYSeries::remove(const QPointF& point)
132 void QXYSeries::remove(const QPointF& point)
128 {
133 {
129 remove(point.x());
134 remove(point.x(),point.y());
130 }
135 }
131
136
132 /*!
137 /*!
@@ -22,7 +22,7 public:
22 void add(const QList<QPointF> points);
22 void add(const QList<QPointF> points);
23 void replace(qreal x,qreal y);
23 void replace(qreal x,qreal y);
24 void replace(const QPointF& point);
24 void replace(const QPointF& point);
25 void remove(qreal x);
25 void remove(qreal x, qreal y);
26 void remove(const QPointF& point);
26 void remove(const QPointF& point);
27 void removeAll();
27 void removeAll();
28
28
General Comments 0
You need to be logged in to leave comments. Login now