##// END OF EJS Templates
Adds missing scatter intercation implementation...
Michal Klocek -
r541:4eee071cd5a1
parent child
Show More
@@ -1,6 +1,7
1 1 #include "mainwindow.h"
2 2 #include <qchartglobal.h>
3 3 #include <qchartview.h>
4 #include <qchartaxis.h>
4 5 #include <QDebug>
5 6
6 7 QTCOMMERCIALCHART_USE_NAMESPACE
@@ -9,7 +10,7 MainWindow::MainWindow(QWidget *parent)
9 10 : QMainWindow(parent)
10 11 {
11 12 QChartView *chartView = new QChartView(this);
12 chartView->setChartTitle("Click to play with points");
13 chartView->setChartTitle("Click to remove scatter point");
13 14 chartView->setRenderHint(QPainter::Antialiasing);
14 15 setCentralWidget(chartView);
15 16
@@ -19,33 +20,19 MainWindow::MainWindow(QWidget *parent)
19 20 *m_scatter << QPointF(x, y);
20 21 }
21 22 }
22 chartView->addSeries(m_scatter);
23 23
24 // Add two more series
25 m_scatter2 = new QScatterSeries();
26 chartView->addSeries(m_scatter2);
27 m_scatter3 = new QScatterSeries();
28 chartView->addSeries(m_scatter3);
24 chartView->addSeries(m_scatter);
25 chartView->axisX()->setRange(0,4.5);
26 chartView->axisY()->setRange(0,4.5);
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 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
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 */
37 m_scatter->remove(point);
51 38 }
@@ -16,7 +16,7 public:
16 16 ~MainWindow();
17 17
18 18 private Q_SLOTS:
19 void clickPoint(QPointF coordinate);
19 void handleClickedPoint(const QPointF& point);
20 20
21 21 private:
22 22 QScatterSeries *m_scatter;
@@ -169,13 +169,19 void ChartTheme::decorate(ScatterChartItem* item, QScatterSeries* series, int in
169 169 Q_ASSERT(item);
170 170 Q_ASSERT(series);
171 171
172 // Use a base color for brush
173 item->setBrush(m_seriesColors.at(index % m_seriesColors.size()));
172 QPen pen;
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
176 QPen pen(QBrush(Qt::SolidPattern), 3);
177 pen.setColor(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1.0));
178 item->setPen(pen);
181 if (brush == series->brush()) {
182 QBrush brush(m_seriesColors.at(count % m_seriesColors.size()));
183 series->setBrush(brush);
184 }
179 185 }
180 186
181 187 void ChartTheme::decorate(PiePresenter* item, QPieSeries* series, int index)
@@ -27,13 +27,8
27 27
28 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 30 \value MarkerShapeCircle
31 \value MarkerShapeRectangle
37 32 */
38 33
39 34 /*!
@@ -54,7 +49,7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
54 49 */
55 50 QScatterSeries::QScatterSeries(QObject *parent) :
56 51 QXYSeries(parent),
57 m_shape(QScatterSeries::MarkerShapeDefault),
52 m_shape(QScatterSeries::MarkerShapeCircle),
58 53 m_size(9.0)
59 54 {
60 55 }
@@ -15,15 +15,8 class QTCOMMERCIALCHART_EXPORT QScatterSeries : public QXYSeries
15 15
16 16 public:
17 17 enum MarkerShape {
18 // TODO: to be defined by the graphics design
19 // TODO: marker shapes: "x", star, rectangle, tilted rect, triangle, circle, dot
20 MarkerShapeDefault = 0,
21 MarkerShapeX,
22 MarkerShapeRectangle,
23 MarkerShapeRoundedRectangle,
24 MarkerShapeTiltedRectangle,
25 MarkerShapeTriangle,
26 MarkerShapeCircle
18 MarkerShapeCircle,
19 MarkerShapeRectangle
27 20 };
28 21
29 22 public:
@@ -23,7 +23,8 ScatterChartItem::ScatterChartItem(QScatterSeries *series, QGraphicsItem *parent
23 23 Q_ASSERT(parent);
24 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 29 setZValue(ChartPresenter::ScatterSeriesZValue);
29 30 setFlags(QGraphicsItem::ItemHasNoContents);
@@ -31,6 +32,8 ScatterChartItem::ScatterChartItem(QScatterSeries *series, QGraphicsItem *parent
31 32
32 33 handleUpdated();
33 34
35 m_items.setHandlesChildEvents(false);
36
34 37 // TODO: how to draw a drop shadow?
35 38 // QGraphicsDropShadowEffect *dropShadow = new QGraphicsDropShadowEffect();
36 39 // dropShadow->setOffset(2.0);
@@ -51,27 +54,23 void ScatterChartItem::createPoints(int count)
51 54 QGraphicsItem *item;
52 55
53 56 switch (m_shape) {
54 case QScatterSeries::MarkerShapeDefault:
55 // Fallthrough, defaults to circle
56 case QScatterSeries::MarkerShapeCircle:
57 item = new QGraphicsEllipseItem(0,0,m_size,m_size);
58 break;
59 case QScatterSeries::MarkerShapeRectangle:
60 item = new QGraphicsRectItem(0,0,m_size,m_size);
57 case QScatterSeries::MarkerShapeCircle:{
58 QGraphicsEllipseItem* i = new QGraphicsEllipseItem(0,0,m_size,m_size);
59 const QRectF& rect = i->boundingRect();
60 i->setPos(-rect.width()/2,-rect.height()/2);
61 item = new Marker(i,this);
61 62 break;
62 case QScatterSeries::MarkerShapeRoundedRectangle:
63 //m_path.addRoundedRect(x, y, size, size, size / 4.0, size / 4.0);
63 }
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 68 break;
65 case QScatterSeries::MarkerShapeTiltedRectangle:
66 // TODO: tilt the rectangle
67 //m_path.addRect(x, y, size, size);
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));
69 }
70 default:
71 qWarning()<<"Unsupported marker type";
74 72 break;
73
75 74 }
76 75 m_items.addToGroup(item);
77 76 }
@@ -86,18 +85,10 void ScatterChartItem::deletePoints(int count)
86 85 }
87 86 }
88 87
89 /*
90
91 void ScatterChartItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
88 void ScatterChartItem::markerSelected(Marker* marker)
92 89 {
93
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
90 emit clicked(QPointF(m_series->x(marker->index()), m_series->y(marker->index())));
99 91 }
100 */
101 92
102 93 void ScatterChartItem::setGeometry(QVector<QPointF>& points)
103 94 {
@@ -117,9 +108,10 void ScatterChartItem::setGeometry(QVector<QPointF>& points)
117 108 QList<QGraphicsItem*> items = m_items.childItems();
118 109
119 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 112 const QPointF& point = points.at(i);
122 113 const QRectF& rect = item->boundingRect();
114 item->setIndex(i);
123 115 item->setPos(point.x()-rect.width()/2,point.y()-rect.height()/2);
124 116 if(!clipRect().contains(point)) {
125 117 item->setVisible(false);
@@ -145,14 +137,14 void ScatterChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *
145 137 void ScatterChartItem::setPen(const QPen& pen)
146 138 {
147 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 144 void ScatterChartItem::setBrush(const QBrush& brush)
153 145 {
154 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 9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 10
11 11 class QScatterSeries;
12 class Marker;
12 13
13 14 class ScatterChartItem : public XYChartItem
14 15 {
@@ -24,8 +25,10 public:
24 25 void setPen(const QPen& pen);
25 26 void setBrush(const QBrush& brush);
26 27
28 void markerSelected(Marker* item);
29
27 30 signals:
28 void clicked(QPointF coordinates);
31 void clicked(const QPointF& point);
29 32
30 33 public slots:
31 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 120 QTCOMMERCIALCHART_END_NAMESPACE
50 121
51 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 124 emit pointRemoved(index);
120 125 m_x.remove(index);
121 126 m_y.remove(index);
@@ -126,7 +131,7 void QXYSeries::remove(qreal x)
126 131 */
127 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 22 void add(const QList<QPointF> points);
23 23 void replace(qreal x,qreal y);
24 24 void replace(const QPointF& point);
25 void remove(qreal x);
25 void remove(qreal x, qreal y);
26 26 void remove(const QPointF& point);
27 27 void removeAll();
28 28
General Comments 0
You need to be logged in to leave comments. Login now