From fbedbdf7642a9729b1a154c42f054c0b1e45b362 2012-02-24 06:43:20 From: Tero Ahola Date: 2012-02-24 06:43:20 Subject: [PATCH] Scatter series documentation; now uses snippets --- diff --git a/example/scatter/main.cpp b/example/scatter/main.cpp index 3a2d258..65b6c08 100644 --- a/example/scatter/main.cpp +++ b/example/scatter/main.cpp @@ -10,10 +10,10 @@ int main(int argc, char *argv[]) { QApplication a(argc, argv); - // Create chart widget + //! [1] + // Create chart view QChartView *chartView = new QChartView(); - //! [1] // Add scatter series with simple test data QScatterSeries *scatter = new QScatterSeries(); *scatter << QPointF(0.5, 5.0) @@ -21,25 +21,40 @@ int main(int argc, char *argv[]) << QPointF(1.0, 5.5) << QPointF(1.5, 5.0) << QPointF(2.0, 4.5) - << QPointF(2.0, 5.5) << QPointF(2.5, 5.0); // Chart takes ownership chartView->addSeries(scatter); //! [1] - // Add another scatter series + // Add some more data + //! [2] + scatter->addData(QPointF(2.0, 5.5)); + //! [2] + + // And more + //! [3] + *scatter << QPointF(2.0, 5.5); + //! [3] + + // Add another scatter series (re-use the previous pointer) // - more data with random component - QScatterSeries *scatter2 = new QScatterSeries(); + scatter = new QScatterSeries(); for (qreal i(0.0); i < 20; i += 0.15) { - (*scatter2) << QPointF(i + (qreal)(rand() % 100) / 100.0, + (*scatter) << QPointF(i + (qreal)(rand() % 100) / 100.0, i + (qreal)(rand() % 100) / 100.0); } + //! [4] QBrush brush(QColor(255, 0, 0, 100), Qt::SolidPattern); - scatter2->setMarkerBrush(brush); + scatter->setMarkerBrush(brush); + //! [4] + //! [5] QPen pen(QColor(0, 255, 0, 80), 3); - scatter2->setMarkerPen(pen); - scatter2->setMarkerShape(QScatterSeries::MarkerShapeRectangle); - chartView->addSeries(scatter2); + scatter->setMarkerPen(pen); + //! [5] + //! [6] + scatter->setMarkerShape(QScatterSeries::MarkerShapeRectangle); + //! [6] + chartView->addSeries(scatter); // Use the chart widget as the central widget QMainWindow w; diff --git a/src/scatterseries/qscatterseries.cpp b/src/scatterseries/qscatterseries.cpp index 701f699..e17489c 100644 --- a/src/scatterseries/qscatterseries.cpp +++ b/src/scatterseries/qscatterseries.cpp @@ -2,18 +2,19 @@ #include "scatterseries_p.h" #include "qchart.h" -QTCOMMERCIALCHART_BEGIN_NAMESPACE +/*! + \class QScatterSeries + \brief QtCommercial Chart series API for showing scatter series. -QScatterSeriesPrivate::QScatterSeriesPrivate() : - m_data(QList()), - m_markerPen(QPen()), - m_markerBrush(QBrush()), - m_markerShape(QScatterSeries::MarkerShapeDefault) -{ - // Initialize pen color to invalid to use a theme color by default - m_markerPen.setColor(QColor::Invalid); - m_markerBrush.setColor(QColor::Invalid); -} + \mainclass + + Example on how to create a chart with scatter series: + \snippet ../example/scatter/main.cpp 1 + + The example code would result the following: + + \image scatter_example1.jpg +*/ /*! \enum QScatterSeries::MarkerShape @@ -30,32 +31,44 @@ QScatterSeriesPrivate::QScatterSeriesPrivate() : */ /*! - \class QScatterSeries - \brief QtCommercial Chart series API for showing scatter series. + \fn QChartSeriesType QScatterSeries::type() const + \brief Returns QChartSeries::SeriesTypeScatter. +*/ - \snippet ../../example/scatter/main.cpp 1 - Example on how to create a chart with scatter series: - \code - #include - #include - #include - ... - QTCOMMERCIALCHART_USE_NAMESPACE - - // Create chart widget - QChartView *chartView = new QChartView(); - QScatterSeries *scatter = new QScatterSeries(); - *scatter << QPointF(0.5, 5.0) << QPointF(1.0, 4.5) << QPointF(1.0, 5.5) << QPointF(1.5, 5.0); - chartView->addSeries(scatter); - // Then add the QChartView into a layout... - \endcode +/*! + \fn void QScatterSeries::clicked() + \brief TODO +*/ - The example code would result the following: +/*! + \fn void QScatterSeries::hoverEnter() + \brief TODO +*/ - \image scatter_example1.jpg +/*! + \fn void QScatterSeries::hoverLeave() + \brief TODO */ /*! + \fn void QScatterSeries::changed() + \brief TODO +*/ + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +QScatterSeriesPrivate::QScatterSeriesPrivate() : + m_data(QList()), + m_markerPen(QPen()), + m_markerBrush(QBrush()), + m_markerShape(QScatterSeries::MarkerShapeDefault) +{ + // Initialize pen color to invalid to use a theme color by default + m_markerPen.setColor(QColor::Invalid); + m_markerBrush.setColor(QColor::Invalid); +} + +/*! Constructs a series object which is a child of \a parent. */ QScatterSeries::QScatterSeries(QObject *parent) : @@ -73,11 +86,9 @@ QScatterSeries::~QScatterSeries() } /*! - Add single data point to the series. + Add single data point with \a value to the series. For example: - \code - mySeries.addData(QPointF(0.5, 5.0)); - \endcode + \snippet ../example/scatter/main.cpp 2 */ void QScatterSeries::addData(QPointF value) { @@ -86,15 +97,20 @@ void QScatterSeries::addData(QPointF value) } /*! - Stream operator for adding a data point to the series. - \sa addData(), QScatterSeries::addData(QPointF value) + Add list of \a points to the series. +*/ +void QScatterSeries::addData(QList points) +{ + d->m_data.append(data); + emit changed(); +} - For example: - \code - mySeries << QPointF(0.5, 5.0) - << QPointF(1.0, 4.5); - \endcode +/*! + Stream operator for adding a data point with \a value to the series. + \sa addData() + For example: + \snippet ../example/scatter/main.cpp 3 */ QScatterSeries& QScatterSeries::operator << (const QPointF &value) { @@ -104,9 +120,20 @@ QScatterSeries& QScatterSeries::operator << (const QPointF &value) } /*! - Replaces the data of the series with the given list of data points. + Stream operator for adding a list of points to the series. + \sa addData() +*/ +QScatterSeries& QScatterSeries::operator << (QList value) +{ + d->m_data.append(value); + emit changed(); + return *this; +} + +/*! + Replaces the data of the series with the given list of data \a points. */ -void QScatterSeries::setData(QList data) +void QScatterSeries::setData(QList points) { d->m_data = data; emit changed(); @@ -121,20 +148,18 @@ QList QScatterSeries::data() } /*! - Overrides the default pen used for drawing a marker item with a user defined pen. The default - pen is defined by chart theme setting. + Overrides the default pen used for drawing a marker item with a user defined \a pen. The + default pen is defined by chart theme setting. For example: - \code - QPen pen(QColor(0, 255, 0, 80), 3); - myScatter->setMarkerPen(pen); - \endcode + \snippet ../example/scatter/main.cpp 5 - Would present your scatter markers with an opaque, uglyish green outlines: + Would present your scatter markers with an opaque, uglyish green outlines instead of the + beatiful markers defined by the chart's theme: \image scatter_example_pen.jpg \sa setMarkerBrush() - \sa QChart::setTheme() + \sa QChart::setChartTheme() */ void QScatterSeries::setMarkerPen(QPen pen) { @@ -150,20 +175,17 @@ QPen QScatterSeries::markerPen() } /*! - Overrides the default brush of the marker items with a user defined brush. The default - brush is defined by chart theme setting. + Overrides the default brush of the marker items with a user defined \a brush. The default brush + is defined by chart theme setting. For example: - \code - QBrush brush(QColor(255, 0, 0, 100), Qt::SolidPattern); - myRandomScatter->setMarkerBrush(brush); - \endcode + \snippet ../example/scatter/main.cpp 4 Would fill your scatter markers with an opaque red color: \image scatter_example_brush.jpg \sa setMarkerPen() - \sa QChart::setTheme() + \sa QChart::setChartTheme() */ void QScatterSeries::setMarkerBrush(QBrush brush) { @@ -179,13 +201,11 @@ QBrush QScatterSeries::markerBrush() } /*! - Overrides the default shape of the marker items with a user defined shape. The default - shape is defined by chart theme setting. + Overrides the default shape of the marker items with a user defined \a shape. The default shape + is defined by chart theme setting. For example: - \code - myScatter->setMarkerShape(QScatterSeries::MarkerShapeRectangle); - \endcode + \snippet ../example/scatter/main.cpp 6 Would make your scatter marker items rectangle: \image scatter_example_shape.jpg diff --git a/src/scatterseries/qscatterseries.h b/src/scatterseries/qscatterseries.h index ef65037..640637a 100644 --- a/src/scatterseries/qscatterseries.h +++ b/src/scatterseries/qscatterseries.h @@ -33,28 +33,33 @@ public: // from QChartSeries QChartSeriesType type() const { return QChartSeries::SeriesTypeScatter; } public: - // TODO: the name of the function? addPoint? addData? addValue? void addData(QPointF value); + void addData(QList points); + void setData(QList points); QScatterSeries& operator << (const QPointF &value); - void setData(QList data); + QScatterSeries& operator << (QList points); QList data(); //TODO: insertData? - void setMarkerPen(QPen pen); QPen markerPen(); - void setMarkerBrush(QBrush brush); QBrush markerBrush(); - void setMarkerShape(MarkerShape shape); MarkerShape markerShape(); // TODO: marker size? +public Q_SLOTS: + void setMarkerPen(QPen pen); + void setMarkerBrush(QBrush brush); + void setMarkerShape(MarkerShape shape); + Q_SIGNALS: - // TODO: move to PIMPL for simplicity or does the user ever need these signals? + void clicked(/* TODO: parameters? */); + void hoverEnter(/* TODO: parameters? */); + void hoverLeave(/* TODO: parameters? */); + // TODO: move to PIMPL for simplicity or does the user ever need changed signals? // TODO: more finegrained signaling for performance reasons // (check QPieSeries implementation with change sets) void changed(); -//public Q_SLOTS: private: Q_DECLARE_PRIVATE(QScatterSeries) Q_DISABLE_COPY(QScatterSeries) diff --git a/src/scatterseries/scatterpresenter.cpp b/src/scatterseries/scatterpresenter.cpp index cda4d02..a7e52ca 100644 --- a/src/scatterseries/scatterpresenter.cpp +++ b/src/scatterseries/scatterpresenter.cpp @@ -134,6 +134,11 @@ void ScatterPresenter::paint(QPainter *painter, const QStyleOptionGraphicsItem * painter->restore(); } +void ScatterPresenter::mousePressEvent(QGraphicsSceneMouseEvent *event) +{ + qDebug() << "ScatterPresenter::mousePressEvent" << event; +} + void ScatterPresenter::changeGeometry() { if (m_boundingRect.isValid()) { diff --git a/src/scatterseries/scatterpresenter_p.h b/src/scatterseries/scatterpresenter_p.h index a5f5a2a..a6819c5 100644 --- a/src/scatterseries/scatterpresenter_p.h +++ b/src/scatterseries/scatterpresenter_p.h @@ -23,6 +23,7 @@ public: public: // from ChartItem QRectF boundingRect() const { return m_boundingRect; } void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *); + void mousePressEvent (QGraphicsSceneMouseEvent * event); signals: