##// END OF EJS Templates
Scatter series documentation; now uses snippets
Tero Ahola -
r300:fbedbdf7642a
parent child
Show More
@@ -1,51 +1,66
1 #include <QtGui/QApplication>
1 #include <QtGui/QApplication>
2 #include <QMainWindow>
2 #include <QMainWindow>
3 #include <qchartglobal.h>
3 #include <qchartglobal.h>
4 #include <qchartview.h>
4 #include <qchartview.h>
5 #include <qscatterseries.h>
5 #include <qscatterseries.h>
6
6
7 QTCOMMERCIALCHART_USE_NAMESPACE
7 QTCOMMERCIALCHART_USE_NAMESPACE
8
8
9 int main(int argc, char *argv[])
9 int main(int argc, char *argv[])
10 {
10 {
11 QApplication a(argc, argv);
11 QApplication a(argc, argv);
12
12
13 // Create chart widget
13 //! [1]
14 // Create chart view
14 QChartView *chartView = new QChartView();
15 QChartView *chartView = new QChartView();
15
16
16 //! [1]
17 // Add scatter series with simple test data
17 // Add scatter series with simple test data
18 QScatterSeries *scatter = new QScatterSeries();
18 QScatterSeries *scatter = new QScatterSeries();
19 *scatter << QPointF(0.5, 5.0)
19 *scatter << QPointF(0.5, 5.0)
20 << QPointF(1.0, 4.5)
20 << QPointF(1.0, 4.5)
21 << QPointF(1.0, 5.5)
21 << QPointF(1.0, 5.5)
22 << QPointF(1.5, 5.0)
22 << QPointF(1.5, 5.0)
23 << QPointF(2.0, 4.5)
23 << QPointF(2.0, 4.5)
24 << QPointF(2.0, 5.5)
25 << QPointF(2.5, 5.0);
24 << QPointF(2.5, 5.0);
26 // Chart takes ownership
25 // Chart takes ownership
27 chartView->addSeries(scatter);
26 chartView->addSeries(scatter);
28 //! [1]
27 //! [1]
29
28
30 // Add another scatter series
29 // Add some more data
30 //! [2]
31 scatter->addData(QPointF(2.0, 5.5));
32 //! [2]
33
34 // And more
35 //! [3]
36 *scatter << QPointF(2.0, 5.5);
37 //! [3]
38
39 // Add another scatter series (re-use the previous pointer)
31 // - more data with random component
40 // - more data with random component
32 QScatterSeries *scatter2 = new QScatterSeries();
41 scatter = new QScatterSeries();
33 for (qreal i(0.0); i < 20; i += 0.15) {
42 for (qreal i(0.0); i < 20; i += 0.15) {
34 (*scatter2) << QPointF(i + (qreal)(rand() % 100) / 100.0,
43 (*scatter) << QPointF(i + (qreal)(rand() % 100) / 100.0,
35 i + (qreal)(rand() % 100) / 100.0);
44 i + (qreal)(rand() % 100) / 100.0);
36 }
45 }
46 //! [4]
37 QBrush brush(QColor(255, 0, 0, 100), Qt::SolidPattern);
47 QBrush brush(QColor(255, 0, 0, 100), Qt::SolidPattern);
38 scatter2->setMarkerBrush(brush);
48 scatter->setMarkerBrush(brush);
49 //! [4]
50 //! [5]
39 QPen pen(QColor(0, 255, 0, 80), 3);
51 QPen pen(QColor(0, 255, 0, 80), 3);
40 scatter2->setMarkerPen(pen);
52 scatter->setMarkerPen(pen);
41 scatter2->setMarkerShape(QScatterSeries::MarkerShapeRectangle);
53 //! [5]
42 chartView->addSeries(scatter2);
54 //! [6]
55 scatter->setMarkerShape(QScatterSeries::MarkerShapeRectangle);
56 //! [6]
57 chartView->addSeries(scatter);
43
58
44 // Use the chart widget as the central widget
59 // Use the chart widget as the central widget
45 QMainWindow w;
60 QMainWindow w;
46 w.resize(640, 480);
61 w.resize(640, 480);
47 w.setCentralWidget(chartView);
62 w.setCentralWidget(chartView);
48 w.show();
63 w.show();
49
64
50 return a.exec();
65 return a.exec();
51 }
66 }
@@ -1,208 +1,228
1 #include "qscatterseries.h"
1 #include "qscatterseries.h"
2 #include "scatterseries_p.h"
2 #include "scatterseries_p.h"
3 #include "qchart.h"
3 #include "qchart.h"
4
4
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
5 /*!
6 \class QScatterSeries
7 \brief QtCommercial Chart series API for showing scatter series.
6
8
7 QScatterSeriesPrivate::QScatterSeriesPrivate() :
9 \mainclass
8 m_data(QList<QPointF>()),
10
9 m_markerPen(QPen()),
11 Example on how to create a chart with scatter series:
10 m_markerBrush(QBrush()),
12 \snippet ../example/scatter/main.cpp 1
11 m_markerShape(QScatterSeries::MarkerShapeDefault)
13
12 {
14 The example code would result the following:
13 // Initialize pen color to invalid to use a theme color by default
15
14 m_markerPen.setColor(QColor::Invalid);
16 \image scatter_example1.jpg
15 m_markerBrush.setColor(QColor::Invalid);
17 */
16 }
17
18
18 /*!
19 /*!
19 \enum QScatterSeries::MarkerShape
20 \enum QScatterSeries::MarkerShape
20
21
21 This enum describes the shape used when rendering marker items.
22 This enum describes the shape used when rendering marker items.
22
23
23 \value MarkerShapeDefault
24 \value MarkerShapeDefault
24 \value MarkerShapePoint
25 \value MarkerShapePoint
25 \value MarkerShapeX
26 \value MarkerShapeX
26 \value MarkerShapeRectangle
27 \value MarkerShapeRectangle
27 \value MarkerShapeTiltedRectangle
28 \value MarkerShapeTiltedRectangle
28 \value MarkerShapeTriangle
29 \value MarkerShapeTriangle
29 \value MarkerShapeCircle
30 \value MarkerShapeCircle
30 */
31 */
31
32
32 /*!
33 /*!
33 \class QScatterSeries
34 \fn QChartSeriesType QScatterSeries::type() const
34 \brief QtCommercial Chart series API for showing scatter series.
35 \brief Returns QChartSeries::SeriesTypeScatter.
36 */
35
37
36 \snippet ../../example/scatter/main.cpp 1
38 /*!
37 Example on how to create a chart with scatter series:
39 \fn void QScatterSeries::clicked()
38 \code
40 \brief TODO
39 #include <qchartglobal.h>
41 */
40 #include <qchartview.h>
41 #include <qscatterseries.h>
42 ...
43 QTCOMMERCIALCHART_USE_NAMESPACE
44
45 // Create chart widget
46 QChartView *chartView = new QChartView();
47 QScatterSeries *scatter = new QScatterSeries();
48 *scatter << QPointF(0.5, 5.0) << QPointF(1.0, 4.5) << QPointF(1.0, 5.5) << QPointF(1.5, 5.0);
49 chartView->addSeries(scatter);
50 // Then add the QChartView into a layout...
51 \endcode
52
42
53 The example code would result the following:
43 /*!
44 \fn void QScatterSeries::hoverEnter()
45 \brief TODO
46 */
54
47
55 \image scatter_example1.jpg
48 /*!
49 \fn void QScatterSeries::hoverLeave()
50 \brief TODO
56 */
51 */
57
52
58 /*!
53 /*!
54 \fn void QScatterSeries::changed()
55 \brief TODO
56 */
57
58 QTCOMMERCIALCHART_BEGIN_NAMESPACE
59
60 QScatterSeriesPrivate::QScatterSeriesPrivate() :
61 m_data(QList<QPointF>()),
62 m_markerPen(QPen()),
63 m_markerBrush(QBrush()),
64 m_markerShape(QScatterSeries::MarkerShapeDefault)
65 {
66 // Initialize pen color to invalid to use a theme color by default
67 m_markerPen.setColor(QColor::Invalid);
68 m_markerBrush.setColor(QColor::Invalid);
69 }
70
71 /*!
59 Constructs a series object which is a child of \a parent.
72 Constructs a series object which is a child of \a parent.
60 */
73 */
61 QScatterSeries::QScatterSeries(QObject *parent) :
74 QScatterSeries::QScatterSeries(QObject *parent) :
62 QChartSeries(parent),
75 QChartSeries(parent),
63 d(new QScatterSeriesPrivate())
76 d(new QScatterSeriesPrivate())
64 {
77 {
65 }
78 }
66
79
67 /*!
80 /*!
68 Destroys the object. Note that adding series to QChart transfers the ownership to the chart.
81 Destroys the object. Note that adding series to QChart transfers the ownership to the chart.
69 */
82 */
70 QScatterSeries::~QScatterSeries()
83 QScatterSeries::~QScatterSeries()
71 {
84 {
72 delete d;
85 delete d;
73 }
86 }
74
87
75 /*!
88 /*!
76 Add single data point to the series.
89 Add single data point with \a value to the series.
77 For example:
90 For example:
78 \code
91 \snippet ../example/scatter/main.cpp 2
79 mySeries.addData(QPointF(0.5, 5.0));
80 \endcode
81 */
92 */
82 void QScatterSeries::addData(QPointF value)
93 void QScatterSeries::addData(QPointF value)
83 {
94 {
84 d->m_data.append(value);
95 d->m_data.append(value);
85 emit changed();
96 emit changed();
86 }
97 }
87
98
88 /*!
99 /*!
89 Stream operator for adding a data point to the series.
100 Add list of \a points to the series.
90 \sa addData(), QScatterSeries::addData(QPointF value)
101 */
102 void QScatterSeries::addData(QList<QPointF> points)
103 {
104 d->m_data.append(data);
105 emit changed();
106 }
91
107
92 For example:
108 /*!
93 \code
109 Stream operator for adding a data point with \a value to the series.
94 mySeries << QPointF(0.5, 5.0)
110 \sa addData()
95 << QPointF(1.0, 4.5);
96 \endcode
97
111
112 For example:
113 \snippet ../example/scatter/main.cpp 3
98 */
114 */
99 QScatterSeries& QScatterSeries::operator << (const QPointF &value)
115 QScatterSeries& QScatterSeries::operator << (const QPointF &value)
100 {
116 {
101 d->m_data.append(value);
117 d->m_data.append(value);
102 emit changed();
118 emit changed();
103 return *this;
119 return *this;
104 }
120 }
105
121
106 /*!
122 /*!
107 Replaces the data of the series with the given list of data points.
123 Stream operator for adding a list of points to the series.
124 \sa addData()
125 */
126 QScatterSeries& QScatterSeries::operator << (QList<QPointF> value)
127 {
128 d->m_data.append(value);
129 emit changed();
130 return *this;
131 }
132
133 /*!
134 Replaces the data of the series with the given list of data \a points.
108 */
135 */
109 void QScatterSeries::setData(QList<QPointF> data)
136 void QScatterSeries::setData(QList<QPointF> points)
110 {
137 {
111 d->m_data = data;
138 d->m_data = data;
112 emit changed();
139 emit changed();
113 }
140 }
114
141
115 /*!
142 /*!
116 Returns the current list of data points of the series.
143 Returns the current list of data points of the series.
117 */
144 */
118 QList<QPointF> QScatterSeries::data()
145 QList<QPointF> QScatterSeries::data()
119 {
146 {
120 return d->m_data;
147 return d->m_data;
121 }
148 }
122
149
123 /*!
150 /*!
124 Overrides the default pen used for drawing a marker item with a user defined pen. The default
151 Overrides the default pen used for drawing a marker item with a user defined \a pen. The
125 pen is defined by chart theme setting.
152 default pen is defined by chart theme setting.
126
153
127 For example:
154 For example:
128 \code
155 \snippet ../example/scatter/main.cpp 5
129 QPen pen(QColor(0, 255, 0, 80), 3);
130 myScatter->setMarkerPen(pen);
131 \endcode
132
156
133 Would present your scatter markers with an opaque, uglyish green outlines:
157 Would present your scatter markers with an opaque, uglyish green outlines instead of the
158 beatiful markers defined by the chart's theme:
134 \image scatter_example_pen.jpg
159 \image scatter_example_pen.jpg
135
160
136 \sa setMarkerBrush()
161 \sa setMarkerBrush()
137 \sa QChart::setTheme()
162 \sa QChart::setChartTheme()
138 */
163 */
139 void QScatterSeries::setMarkerPen(QPen pen)
164 void QScatterSeries::setMarkerPen(QPen pen)
140 {
165 {
141 d->m_markerPen = pen;
166 d->m_markerPen = pen;
142 }
167 }
143
168
144 /*!
169 /*!
145 Returns the pen used for drawing markers.
170 Returns the pen used for drawing markers.
146 */
171 */
147 QPen QScatterSeries::markerPen()
172 QPen QScatterSeries::markerPen()
148 {
173 {
149 return d->m_markerPen;
174 return d->m_markerPen;
150 }
175 }
151
176
152 /*!
177 /*!
153 Overrides the default brush of the marker items with a user defined brush. The default
178 Overrides the default brush of the marker items with a user defined \a brush. The default brush
154 brush is defined by chart theme setting.
179 is defined by chart theme setting.
155
180
156 For example:
181 For example:
157 \code
182 \snippet ../example/scatter/main.cpp 4
158 QBrush brush(QColor(255, 0, 0, 100), Qt::SolidPattern);
159 myRandomScatter->setMarkerBrush(brush);
160 \endcode
161
183
162 Would fill your scatter markers with an opaque red color:
184 Would fill your scatter markers with an opaque red color:
163 \image scatter_example_brush.jpg
185 \image scatter_example_brush.jpg
164
186
165 \sa setMarkerPen()
187 \sa setMarkerPen()
166 \sa QChart::setTheme()
188 \sa QChart::setChartTheme()
167 */
189 */
168 void QScatterSeries::setMarkerBrush(QBrush brush)
190 void QScatterSeries::setMarkerBrush(QBrush brush)
169 {
191 {
170 d->m_markerBrush = brush;
192 d->m_markerBrush = brush;
171 }
193 }
172
194
173 /*!
195 /*!
174 Returns the brush used for drawing markers.
196 Returns the brush used for drawing markers.
175 */
197 */
176 QBrush QScatterSeries::markerBrush()
198 QBrush QScatterSeries::markerBrush()
177 {
199 {
178 return d->m_markerBrush;
200 return d->m_markerBrush;
179 }
201 }
180
202
181 /*!
203 /*!
182 Overrides the default shape of the marker items with a user defined shape. The default
204 Overrides the default shape of the marker items with a user defined \a shape. The default shape
183 shape is defined by chart theme setting.
205 is defined by chart theme setting.
184
206
185 For example:
207 For example:
186 \code
208 \snippet ../example/scatter/main.cpp 6
187 myScatter->setMarkerShape(QScatterSeries::MarkerShapeRectangle);
188 \endcode
189
209
190 Would make your scatter marker items rectangle:
210 Would make your scatter marker items rectangle:
191 \image scatter_example_shape.jpg
211 \image scatter_example_shape.jpg
192 */
212 */
193 void QScatterSeries::setMarkerShape(MarkerShape shape)
213 void QScatterSeries::setMarkerShape(MarkerShape shape)
194 {
214 {
195 d->m_markerShape = shape;
215 d->m_markerShape = shape;
196 }
216 }
197
217
198 /*!
218 /*!
199 Returns the shape used for drawing markers.
219 Returns the shape used for drawing markers.
200 */
220 */
201 QScatterSeries::MarkerShape QScatterSeries::markerShape()
221 QScatterSeries::MarkerShape QScatterSeries::markerShape()
202 {
222 {
203 return (QScatterSeries::MarkerShape) d->m_markerShape;
223 return (QScatterSeries::MarkerShape) d->m_markerShape;
204 }
224 }
205
225
206 #include "moc_qscatterseries.cpp"
226 #include "moc_qscatterseries.cpp"
207
227
208 QTCOMMERCIALCHART_END_NAMESPACE
228 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,66 +1,71
1 #ifndef QSCATTERSERIES_H
1 #ifndef QSCATTERSERIES_H
2 #define QSCATTERSERIES_H
2 #define QSCATTERSERIES_H
3
3
4 #include "qchartseries.h"
4 #include "qchartseries.h"
5 #include <QRectF>
5 #include <QRectF>
6 #include <QColor>
6 #include <QColor>
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 class QScatterSeriesPrivate;
9 class QScatterSeriesPrivate;
10
10
11 class QTCOMMERCIALCHART_EXPORT QScatterSeries : public QChartSeries
11 class QTCOMMERCIALCHART_EXPORT QScatterSeries : public QChartSeries
12 {
12 {
13 Q_OBJECT
13 Q_OBJECT
14
14
15 public:
15 public:
16 enum MarkerShape {
16 enum MarkerShape {
17 // TODO: to be defined by the graphics design
17 // TODO: to be defined by the graphics design
18 // TODO: marker shapes: "x", star, rectangle, tilted rect, triangle, circle, dot
18 // TODO: marker shapes: "x", star, rectangle, tilted rect, triangle, circle, dot
19 MarkerShapeDefault = 0,
19 MarkerShapeDefault = 0,
20 MarkerShapePoint,
20 MarkerShapePoint,
21 MarkerShapeX,
21 MarkerShapeX,
22 MarkerShapeRectangle,
22 MarkerShapeRectangle,
23 MarkerShapeTiltedRectangle,
23 MarkerShapeTiltedRectangle,
24 MarkerShapeTriangle,
24 MarkerShapeTriangle,
25 MarkerShapeCircle
25 MarkerShapeCircle
26 };
26 };
27
27
28 public:
28 public:
29 QScatterSeries(QObject *parent = 0);
29 QScatterSeries(QObject *parent = 0);
30 ~QScatterSeries();
30 ~QScatterSeries();
31
31
32 public: // from QChartSeries
32 public: // from QChartSeries
33 QChartSeriesType type() const { return QChartSeries::SeriesTypeScatter; }
33 QChartSeriesType type() const { return QChartSeries::SeriesTypeScatter; }
34
34
35 public:
35 public:
36 // TODO: the name of the function? addPoint? addData? addValue?
37 void addData(QPointF value);
36 void addData(QPointF value);
37 void addData(QList<QPointF> points);
38 void setData(QList<QPointF> points);
38 QScatterSeries& operator << (const QPointF &value);
39 QScatterSeries& operator << (const QPointF &value);
39 void setData(QList<QPointF> data);
40 QScatterSeries& operator << (QList<QPointF> points);
40 QList<QPointF> data();
41 QList<QPointF> data();
41 //TODO: insertData?
42 //TODO: insertData?
42
43
43 void setMarkerPen(QPen pen);
44 QPen markerPen();
44 QPen markerPen();
45 void setMarkerBrush(QBrush brush);
46 QBrush markerBrush();
45 QBrush markerBrush();
47 void setMarkerShape(MarkerShape shape);
48 MarkerShape markerShape();
46 MarkerShape markerShape();
49 // TODO: marker size?
47 // TODO: marker size?
50
48
49 public Q_SLOTS:
50 void setMarkerPen(QPen pen);
51 void setMarkerBrush(QBrush brush);
52 void setMarkerShape(MarkerShape shape);
53
51 Q_SIGNALS:
54 Q_SIGNALS:
52 // TODO: move to PIMPL for simplicity or does the user ever need these signals?
55 void clicked(/* TODO: parameters? */);
56 void hoverEnter(/* TODO: parameters? */);
57 void hoverLeave(/* TODO: parameters? */);
58 // TODO: move to PIMPL for simplicity or does the user ever need changed signals?
53 // TODO: more finegrained signaling for performance reasons
59 // TODO: more finegrained signaling for performance reasons
54 // (check QPieSeries implementation with change sets)
60 // (check QPieSeries implementation with change sets)
55 void changed();
61 void changed();
56
62
57 //public Q_SLOTS:
58 private:
63 private:
59 Q_DECLARE_PRIVATE(QScatterSeries)
64 Q_DECLARE_PRIVATE(QScatterSeries)
60 Q_DISABLE_COPY(QScatterSeries)
65 Q_DISABLE_COPY(QScatterSeries)
61 QScatterSeriesPrivate *const d;
66 QScatterSeriesPrivate *const d;
62 };
67 };
63
68
64 QTCOMMERCIALCHART_END_NAMESPACE
69 QTCOMMERCIALCHART_END_NAMESPACE
65
70
66 #endif // QSCATTERSERIES_H
71 #endif // QSCATTERSERIES_H
@@ -1,157 +1,162
1 #include "scatterpresenter_p.h"
1 #include "scatterpresenter_p.h"
2 #include "qscatterseries.h"
2 #include "qscatterseries.h"
3 #include <QPen>
3 #include <QPen>
4 #include <QPainter>
4 #include <QPainter>
5 #include <QGraphicsScene>
5 #include <QGraphicsScene>
6 #include <QDebug>
6 #include <QDebug>
7 #include <QTime>
7 #include <QTime>
8
8
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10
10
11 ScatterPresenter::ScatterPresenter(QScatterSeries *series, QGraphicsObject *parent) :
11 ScatterPresenter::ScatterPresenter(QScatterSeries *series, QGraphicsObject *parent) :
12 ChartItem(parent),
12 ChartItem(parent),
13 m_series(series),
13 m_series(series),
14 m_boundingRect(),
14 m_boundingRect(),
15 //m_markerColor(QColor()),
15 //m_markerColor(QColor()),
16 // m_markerColor(QColor(255, 0, 0)),
16 // m_markerColor(QColor(255, 0, 0)),
17 m_visibleChartArea()
17 m_visibleChartArea()
18 {
18 {
19 if (parent)
19 if (parent)
20 m_boundingRect = parent->boundingRect();
20 m_boundingRect = parent->boundingRect();
21
21
22 if (series) {
22 if (series) {
23 connect(series, SIGNAL(changed()), this, SLOT(handleModelChanged()));
23 connect(series, SIGNAL(changed()), this, SLOT(handleModelChanged()));
24 }
24 }
25 }
25 }
26
26
27 void ScatterPresenter::handleDomainChanged(const Domain& domain)
27 void ScatterPresenter::handleDomainChanged(const Domain& domain)
28 {
28 {
29 m_visibleChartArea = domain;
29 m_visibleChartArea = domain;
30 changeGeometry();
30 changeGeometry();
31 }
31 }
32
32
33 void ScatterPresenter::handleGeometryChanged(const QRectF& rect)
33 void ScatterPresenter::handleGeometryChanged(const QRectF& rect)
34 {
34 {
35 m_boundingRect = rect;
35 m_boundingRect = rect;
36 changeGeometry();
36 changeGeometry();
37 }
37 }
38
38
39 void ScatterPresenter::handleModelChanged()
39 void ScatterPresenter::handleModelChanged()
40 {
40 {
41 // TODO: more fine grained modelChanged signaling
41 // TODO: more fine grained modelChanged signaling
42 changeGeometry();
42 changeGeometry();
43 }
43 }
44
44
45 void ScatterPresenter::paint(QPainter *painter, const QStyleOptionGraphicsItem */*option*/, QWidget */*widget*/)
45 void ScatterPresenter::paint(QPainter *painter, const QStyleOptionGraphicsItem */*option*/, QWidget */*widget*/)
46 {
46 {
47 // TODO: Optimization: avoid setting on every paint method call?
47 // TODO: Optimization: avoid setting on every paint method call?
48 // The custom settings in series override those defined by the theme
48 // The custom settings in series override those defined by the theme
49 int shape = m_series->markerShape();
49 int shape = m_series->markerShape();
50
50
51 painter->save();
51 painter->save();
52 painter->setClipRect(m_boundingRect);
52 painter->setClipRect(m_boundingRect);
53
53
54 // Paint dropshadow
54 // Paint dropshadow
55 QPen dropShadowPen(QColor(0, 0, 0, 70));
55 QPen dropShadowPen(QColor(0, 0, 0, 70));
56 dropShadowPen.setWidth(3);
56 dropShadowPen.setWidth(3);
57 painter->setPen(dropShadowPen);
57 painter->setPen(dropShadowPen);
58 painter->setBrush(Qt::NoBrush);
58 painter->setBrush(Qt::NoBrush);
59 painter->setRenderHint(QPainter::Antialiasing);
59 painter->setRenderHint(QPainter::Antialiasing);
60 for (int i(0); i < m_scenex.count() && i < m_sceney.count(); i++) {
60 for (int i(0); i < m_scenex.count() && i < m_sceney.count(); i++) {
61 if (scene()->width() > m_scenex.at(i) && scene()->height() > m_sceney.at(i))
61 if (scene()->width() > m_scenex.at(i) && scene()->height() > m_sceney.at(i))
62 switch (shape) {
62 switch (shape) {
63 case QScatterSeries::MarkerShapeDefault:
63 case QScatterSeries::MarkerShapeDefault:
64 // Fallthrough, defaults to circle
64 // Fallthrough, defaults to circle
65 case QScatterSeries::MarkerShapeCircle:
65 case QScatterSeries::MarkerShapeCircle:
66 painter->drawChord(m_scenex.at(i) + 2, m_sceney.at(i) + 2, 9, 9, 0, 5760);
66 painter->drawChord(m_scenex.at(i) + 2, m_sceney.at(i) + 2, 9, 9, 0, 5760);
67 break;
67 break;
68 case QScatterSeries::MarkerShapePoint:
68 case QScatterSeries::MarkerShapePoint:
69 //painter->drawPoint(m_scenex.at(i), m_sceney.at(i));
69 //painter->drawPoint(m_scenex.at(i), m_sceney.at(i));
70 break;
70 break;
71 case QScatterSeries::MarkerShapeRectangle:
71 case QScatterSeries::MarkerShapeRectangle:
72 painter->drawRect(m_scenex.at(i) + 2, m_sceney.at(i) + 2, 8, 8);
72 painter->drawRect(m_scenex.at(i) + 2, m_sceney.at(i) + 2, 8, 8);
73 break;
73 break;
74 case QScatterSeries::MarkerShapeTiltedRectangle: {
74 case QScatterSeries::MarkerShapeTiltedRectangle: {
75 // TODO:
75 // TODO:
76 static const QPointF points[4] = {
76 static const QPointF points[4] = {
77 QPointF(-1.0 + m_scenex.at(i), 0.0 + m_sceney.at(i)),
77 QPointF(-1.0 + m_scenex.at(i), 0.0 + m_sceney.at(i)),
78 QPointF(0.0 + m_scenex.at(i), 1.0 + m_sceney.at(i)),
78 QPointF(0.0 + m_scenex.at(i), 1.0 + m_sceney.at(i)),
79 QPointF(1.0 + m_scenex.at(i), 0.0 + m_sceney.at(i)),
79 QPointF(1.0 + m_scenex.at(i), 0.0 + m_sceney.at(i)),
80 QPointF(0.0 + m_scenex.at(i), -1.0 + m_sceney.at(i))
80 QPointF(0.0 + m_scenex.at(i), -1.0 + m_sceney.at(i))
81 };
81 };
82 painter->drawPolygon(points, 4);
82 painter->drawPolygon(points, 4);
83 break;
83 break;
84 }
84 }
85 default:
85 default:
86 // TODO: implement the rest of the shapes
86 // TODO: implement the rest of the shapes
87 Q_ASSERT(false);
87 Q_ASSERT(false);
88 break;
88 break;
89 }
89 }
90 }
90 }
91
91
92 // Paint the shape
92 // Paint the shape
93 QPen pen = m_markerPen;
93 QPen pen = m_markerPen;
94 if (m_series->markerPen().color().isValid())
94 if (m_series->markerPen().color().isValid())
95 pen = m_series->markerPen();
95 pen = m_series->markerPen();
96 if (m_series->markerBrush().color().isValid())
96 if (m_series->markerBrush().color().isValid())
97 painter->setBrush(m_series->markerBrush());
97 painter->setBrush(m_series->markerBrush());
98 else
98 else
99 painter->setBrush(m_markerBrush);
99 painter->setBrush(m_markerBrush);
100 painter->setPen(pen);
100 painter->setPen(pen);
101 painter->setRenderHint(QPainter::Antialiasing, false);
101 painter->setRenderHint(QPainter::Antialiasing, false);
102 for (int i(0); i < m_scenex.count() && i < m_sceney.count(); i++) {
102 for (int i(0); i < m_scenex.count() && i < m_sceney.count(); i++) {
103 if (scene()->width() > m_scenex.at(i) && scene()->height() > m_sceney.at(i))
103 if (scene()->width() > m_scenex.at(i) && scene()->height() > m_sceney.at(i))
104 switch (shape) {
104 switch (shape) {
105 case QScatterSeries::MarkerShapeDefault:
105 case QScatterSeries::MarkerShapeDefault:
106 // Fallthrough, defaults to circle
106 // Fallthrough, defaults to circle
107 case QScatterSeries::MarkerShapeCircle:
107 case QScatterSeries::MarkerShapeCircle:
108 painter->drawChord(m_scenex.at(i), m_sceney.at(i), 9, 9, 0, 5760);
108 painter->drawChord(m_scenex.at(i), m_sceney.at(i), 9, 9, 0, 5760);
109 break;
109 break;
110 case QScatterSeries::MarkerShapePoint:
110 case QScatterSeries::MarkerShapePoint:
111 painter->drawPoint(m_scenex.at(i), m_sceney.at(i));
111 painter->drawPoint(m_scenex.at(i), m_sceney.at(i));
112 break;
112 break;
113 case QScatterSeries::MarkerShapeRectangle:
113 case QScatterSeries::MarkerShapeRectangle:
114 painter->drawRect(m_scenex.at(i), m_sceney.at(i), 9, 9);
114 painter->drawRect(m_scenex.at(i), m_sceney.at(i), 9, 9);
115 break;
115 break;
116 case QScatterSeries::MarkerShapeTiltedRectangle: {
116 case QScatterSeries::MarkerShapeTiltedRectangle: {
117 // TODO:
117 // TODO:
118 static const QPointF points[4] = {
118 static const QPointF points[4] = {
119 QPointF(-1.0 + m_scenex.at(i), 0.0 + m_sceney.at(i)),
119 QPointF(-1.0 + m_scenex.at(i), 0.0 + m_sceney.at(i)),
120 QPointF(0.0 + m_scenex.at(i), 1.0 + m_sceney.at(i)),
120 QPointF(0.0 + m_scenex.at(i), 1.0 + m_sceney.at(i)),
121 QPointF(1.0 + m_scenex.at(i), 0.0 + m_sceney.at(i)),
121 QPointF(1.0 + m_scenex.at(i), 0.0 + m_sceney.at(i)),
122 QPointF(0.0 + m_scenex.at(i), -1.0 + m_sceney.at(i))
122 QPointF(0.0 + m_scenex.at(i), -1.0 + m_sceney.at(i))
123 };
123 };
124 painter->drawPolygon(points, 4);
124 painter->drawPolygon(points, 4);
125 break;
125 break;
126 }
126 }
127 default:
127 default:
128 // TODO: implement the rest of the shapes
128 // TODO: implement the rest of the shapes
129 Q_ASSERT(false);
129 Q_ASSERT(false);
130 break;
130 break;
131 }
131 }
132 }
132 }
133
133
134 painter->restore();
134 painter->restore();
135 }
135 }
136
136
137 void ScatterPresenter::mousePressEvent(QGraphicsSceneMouseEvent *event)
138 {
139 qDebug() << "ScatterPresenter::mousePressEvent" << event;
140 }
141
137 void ScatterPresenter::changeGeometry()
142 void ScatterPresenter::changeGeometry()
138 {
143 {
139 if (m_boundingRect.isValid()) {
144 if (m_boundingRect.isValid()) {
140
145
141 prepareGeometryChange();
146 prepareGeometryChange();
142 qreal scalex = m_boundingRect.width() / m_visibleChartArea.spanX();
147 qreal scalex = m_boundingRect.width() / m_visibleChartArea.spanX();
143 qreal scaley = m_boundingRect.height() / m_visibleChartArea.spanY();
148 qreal scaley = m_boundingRect.height() / m_visibleChartArea.spanY();
144 m_scenex.clear();
149 m_scenex.clear();
145 m_sceney.clear();
150 m_sceney.clear();
146
151
147 // Convert relative coordinates to absolute pixel coordinates that can be used for drawing
152 // Convert relative coordinates to absolute pixel coordinates that can be used for drawing
148 foreach (QPointF point, m_series->data()) {
153 foreach (QPointF point, m_series->data()) {
149 m_scenex.append(m_boundingRect.left() + point.x() * scalex - m_visibleChartArea.m_minX * scalex);
154 m_scenex.append(m_boundingRect.left() + point.x() * scalex - m_visibleChartArea.m_minX * scalex);
150 m_sceney.append(m_boundingRect.bottom() - point.y() * scaley + m_visibleChartArea.m_minY * scaley);
155 m_sceney.append(m_boundingRect.bottom() - point.y() * scaley + m_visibleChartArea.m_minY * scaley);
151 }
156 }
152 }
157 }
153 }
158 }
154
159
155 #include "moc_scatterpresenter_p.cpp"
160 #include "moc_scatterpresenter_p.cpp"
156
161
157 QTCOMMERCIALCHART_END_NAMESPACE
162 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,48 +1,49
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 "chartitem_p.h"
5 #include "chartitem_p.h"
6 #include <QObject>
6 #include <QObject>
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
12
13 /*!
13 /*!
14 * The "business logic" of scatter series. This is a QObject that does not have a parent QObject.
14 * The "business logic" of scatter series. This is a QObject that does not have a parent QObject.
15 * The QGraphicsItem parent owns the object instead.
15 * The QGraphicsItem parent owns the object instead.
16 */
16 */
17 class ScatterPresenter : public QObject, public ChartItem
17 class ScatterPresenter : public QObject, public ChartItem
18 {
18 {
19 Q_OBJECT
19 Q_OBJECT
20 public:
20 public:
21 explicit ScatterPresenter(QScatterSeries *series, QGraphicsObject *parent = 0);
21 explicit ScatterPresenter(QScatterSeries *series, QGraphicsObject *parent = 0);
22
22
23 public: // from ChartItem
23 public: // from ChartItem
24 QRectF boundingRect() const { return m_boundingRect; }
24 QRectF boundingRect() const { return m_boundingRect; }
25 void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *);
25 void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *);
26 void mousePressEvent (QGraphicsSceneMouseEvent * event);
26
27
27 signals:
28 signals:
28
29
29 public Q_SLOTS:
30 public Q_SLOTS:
30 void handleDomainChanged(const Domain& domain);
31 void handleDomainChanged(const Domain& domain);
31 void handleGeometryChanged(const QRectF& rect);
32 void handleGeometryChanged(const QRectF& rect);
32 void handleModelChanged();
33 void handleModelChanged();
33
34
34 public:
35 public:
35 void changeGeometry();
36 void changeGeometry();
36
37
37 QScatterSeries *m_series;
38 QScatterSeries *m_series;
38 QRectF m_boundingRect;
39 QRectF m_boundingRect;
39 QList<qreal> m_scenex;
40 QList<qreal> m_scenex;
40 QList<qreal> m_sceney;
41 QList<qreal> m_sceney;
41 Domain m_visibleChartArea;
42 Domain m_visibleChartArea;
42 QPen m_markerPen;
43 QPen m_markerPen;
43 QBrush m_markerBrush;
44 QBrush m_markerBrush;
44 };
45 };
45
46
46 QTCOMMERCIALCHART_END_NAMESPACE
47 QTCOMMERCIALCHART_END_NAMESPACE
47
48
48 #endif // SCATTERPRESENTER_H
49 #endif // SCATTERPRESENTER_H
General Comments 0
You need to be logged in to leave comments. Login now