##// END OF EJS Templates
Mapper class added for xyseries and pieseries. Model support commented out for barseries for now.
Marek Rosa -
r1164:9203960160af
parent child
Show More
@@ -0,0 +1,81
1 #include "qpiemodelmapper.h"
2
3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4
5 QPieModelMapper::QPieModelMapper(QObject *parent) :
6 QObject(parent),
7 m_first(0),
8 m_count(-1),
9 m_orientation(Qt::Vertical),
10 m_mapValues(-1),
11 m_mapLabels(-1)
12 {
13 }
14
15 int QPieModelMapper::first() const
16 {
17 return m_first;
18 }
19
20 void QPieModelMapper::setFirst(int first)
21 {
22 m_first = qMax(first, 0);
23 emit updated();
24 }
25
26 int QPieModelMapper::count() const
27 {
28 return m_count;
29 }
30
31 void QPieModelMapper::setCount(int count)
32 {
33 m_count = qMax(count, -1);
34 emit updated();
35 }
36
37 Qt::Orientation QPieModelMapper::orientation() const
38 {
39 return m_orientation;
40 }
41
42 void QPieModelMapper::setOrientation(Qt::Orientation orientation)
43 {
44 m_orientation = orientation;
45 emit updated();
46 }
47
48 int QPieModelMapper::mapValues() const
49 {
50 return m_mapValues;
51 }
52
53 void QPieModelMapper::setMapValues(int mapValues)
54 {
55 m_mapValues = mapValues;
56 emit updated();
57 }
58
59 int QPieModelMapper::mapLabels() const
60 {
61 return m_mapLabels;
62 }
63
64 void QPieModelMapper::setMapLabels(int mapLabels)
65 {
66 m_mapLabels = mapLabels;
67 emit updated();
68 }
69
70 void QPieModelMapper::reset()
71 {
72 m_first = 0;
73 m_count = -1;
74 m_orientation = Qt::Vertical;
75 m_mapValues = -1;
76 m_mapLabels = -1;
77 }
78
79 #include "moc_qpiemodelmapper.cpp"
80
81 QTCOMMERCIALCHART_END_NAMESPACE
@@ -0,0 +1,46
1 #ifndef QPIEMODELMAPPER_H
2 #define QPIEMODELMAPPER_H
3
4 #include "qchartglobal.h"
5 #include <QObject>
6
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8
9 class QTCOMMERCIALCHART_EXPORT QPieModelMapper : public QObject
10 {
11 Q_OBJECT
12 public:
13 explicit QPieModelMapper(QObject *parent = 0);
14
15 int first() const;
16 void setFirst(int first);
17
18 int count() const;
19 void setCount(int count);
20
21 Qt::Orientation orientation() const;
22 void setOrientation(Qt::Orientation orientation);
23
24 int mapValues() const;
25 void setMapValues(int mapValues);
26
27 int mapLabels() const;
28 void setMapLabels(int mapLabels);
29
30 void reset();
31
32 Q_SIGNALS:
33 void updated();
34
35 private:
36 int m_first;
37 int m_count;
38 Qt::Orientation m_orientation;
39 int m_mapValues;
40 int m_mapLabels;
41
42 };
43
44 QTCOMMERCIALCHART_END_NAMESPACE
45
46 #endif // QPIEMODELMAPPER_H
@@ -0,0 +1,81
1 #include "qxymodelmapper.h"
2
3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4
5 QXYModelMapper::QXYModelMapper(QObject *parent):
6 QObject(parent),
7 m_first(0),
8 m_count(-1),
9 m_orientation(Qt::Vertical),
10 m_mapX(-1),
11 m_mapY(-1)
12 {
13 }
14
15 int QXYModelMapper::first() const
16 {
17 return m_first;
18 }
19
20 void QXYModelMapper::setFirst(int first)
21 {
22 m_first = qMax(first, 0);
23 emit updated();
24 }
25
26 int QXYModelMapper::count() const
27 {
28 return m_count;
29 }
30
31 void QXYModelMapper::setCount(int count)
32 {
33 m_count = qMax(count, -1);
34 emit updated();
35 }
36
37 Qt::Orientation QXYModelMapper::orientation() const
38 {
39 return m_orientation;
40 }
41
42 void QXYModelMapper::setOrientation(Qt::Orientation orientation)
43 {
44 m_orientation = orientation;
45 emit updated();
46 }
47
48 int QXYModelMapper::mapX() const
49 {
50 return m_mapX;
51 }
52
53 void QXYModelMapper::setMapX(int mapX)
54 {
55 m_mapX = mapX;
56 emit updated();
57 }
58
59 int QXYModelMapper::mapY() const
60 {
61 return m_mapY;
62 }
63
64 void QXYModelMapper::setMapY(int mapY)
65 {
66 m_mapY = mapY;
67 emit updated();
68 }
69
70 void QXYModelMapper::reset()
71 {
72 m_first = 0;
73 m_count = -1;
74 m_orientation = Qt::Vertical;
75 m_mapX = -1;
76 m_mapY = -1;
77 }
78
79 #include "moc_qxymodelmapper.cpp"
80
81 QTCOMMERCIALCHART_END_NAMESPACE
@@ -0,0 +1,47
1 #ifndef QXYMODELMAPPER_H
2 #define QXYMODELMAPPER_H
3
4 #include "qchartglobal.h"
5 #include <QObject>
6 #include <Qt>
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
10 class QTCOMMERCIALCHART_EXPORT QXYModelMapper : public QObject
11 {
12 Q_OBJECT
13
14 public:
15 explicit QXYModelMapper(QObject *parent = 0);
16
17 int first() const;
18 void setFirst(int first);
19
20 int count() const;
21 void setCount(int count);
22
23 Qt::Orientation orientation() const;
24 void setOrientation(Qt::Orientation orientation);
25
26 int mapX() const;
27 void setMapX(int mapX);
28
29 int mapY() const;
30 void setMapY(int mapY);
31
32 void reset();
33
34 Q_SIGNALS:
35 void updated();
36
37 private:
38 int m_first;
39 int m_count;
40 Qt::Orientation m_orientation;
41 int m_mapX;
42 int m_mapY;
43 };
44
45 QTCOMMERCIALCHART_END_NAMESPACE
46
47 #endif // QXYMODELMAPPER_H
@@ -1,100 +1,111
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "tablewidget.h"
21 #include "tablewidget.h"
22 #include "customtablemodel.h"
22 #include "customtablemodel.h"
23 #include <QGridLayout>
23 #include <QGridLayout>
24 #include <QTableView>
24 #include <QTableView>
25 #include <QChart>
25 #include <QChart>
26 #include <QChartView>
26 #include <QChartView>
27 #include <QLineSeries>
27 #include <QLineSeries>
28 #include "qxymodelmapper.h"
28 #include <QHeaderView>
29 #include <QHeaderView>
29
30
30 QTCOMMERCIALCHART_USE_NAMESPACE
31 QTCOMMERCIALCHART_USE_NAMESPACE
31
32
32 TableWidget::TableWidget(QWidget *parent)
33 TableWidget::TableWidget(QWidget *parent)
33 : QWidget(parent)
34 : QWidget(parent)
34 {
35 {
35 // create simple model for storing data
36 // create simple model for storing data
36 // user's table data model
37 // user's table data model
37 //! [1]
38 //! [1]
38 CustomTableModel *model = new CustomTableModel;
39 CustomTableModel *model = new CustomTableModel;
39 //! [1]
40 //! [1]
40
41
41 //! [2]
42 //! [2]
42 // create table view and add model to it
43 // create table view and add model to it
43 QTableView *tableView = new QTableView;
44 QTableView *tableView = new QTableView;
44 tableView->setModel(model);
45 tableView->setModel(model);
45 tableView->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
46 tableView->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
46 tableView->verticalHeader()->setResizeMode(QHeaderView::Stretch);
47 tableView->verticalHeader()->setResizeMode(QHeaderView::Stretch);
47 //! [2]
48 //! [2]
48
49
49 //! [3]
50 //! [3]
50 QChart *chart = new QChart;
51 QChart *chart = new QChart;
51 chart->setAnimationOptions(QChart::AllAnimations);
52 chart->setAnimationOptions(QChart::AllAnimations);
52 //! [3]
53 //! [3]
53
54
54 // series 1
55 // series 1
55 //! [4]
56 //! [4]
56 QLineSeries *series = new QLineSeries;
57 QLineSeries *series = new QLineSeries;
57 series->setModel(model);
58 series->setModel(model);
58 series->setModelMapping(0, 1, Qt::Vertical);
59
60 QXYModelMapper *mapper = new QXYModelMapper;
61 mapper->setMapX(0);
62 mapper->setMapY(1);
63 series->setModelMapper(mapper);
64 // series->setModelMapping(0, 1, Qt::Vertical);
59 chart->addSeries(series);
65 chart->addSeries(series);
60 //! [4]
66 //! [4]
61
67
62 //! [5]
68 //! [5]
63 // for storing color hex from the series
69 // for storing color hex from the series
64 QString seriesColorHex = "#000000";
70 QString seriesColorHex = "#000000";
65
71
66 // get the color of the series and use it for showing the mapped area
72 // get the color of the series and use it for showing the mapped area
67 seriesColorHex = "#" + QString::number(series->pen().color().rgb(), 16).right(6).toUpper();
73 seriesColorHex = "#" + QString::number(series->pen().color().rgb(), 16).right(6).toUpper();
68 model->addMapping(seriesColorHex, QRect(0, 0, 2, model->rowCount()));
74 model->addMapping(seriesColorHex, QRect(0, 0, 2, model->rowCount()));
69 //! [5]
75 //! [5]
70
76
71 // series 2
77 // series 2
72 //! [6]
78 //! [6]
73 series = new QLineSeries;
79 series = new QLineSeries;
74 series->setModel(model);
80 series->setModel(model);
75 series->setModelMapping(2,3, Qt::Vertical);
81
82 mapper = new QXYModelMapper;
83 mapper->setMapX(2);
84 mapper->setMapY(3);
85 series->setModelMapper(mapper);
86 // series->setModelMapping(2,3, Qt::Vertical);
76 chart->addSeries(series);
87 chart->addSeries(series);
77 //! [6]
88 //! [6]
78
89
79 //! [7]
90 //! [7]
80 // get the color of the series and use it for showing the mapped area
91 // get the color of the series and use it for showing the mapped area
81 seriesColorHex = "#" + QString::number(series->pen().color().rgb(), 16).right(6).toUpper();
92 seriesColorHex = "#" + QString::number(series->pen().color().rgb(), 16).right(6).toUpper();
82 model->addMapping(seriesColorHex, QRect(2, 0, 2, model->rowCount()));
93 model->addMapping(seriesColorHex, QRect(2, 0, 2, model->rowCount()));
83 //! [7]
94 //! [7]
84
95
85 //! [8]
96 //! [8]
86 QChartView *chartView = new QChartView(chart);
97 QChartView *chartView = new QChartView(chart);
87 chartView->setRenderHint(QPainter::Antialiasing);
98 chartView->setRenderHint(QPainter::Antialiasing);
88 chartView->setMinimumSize(640, 480);
99 chartView->setMinimumSize(640, 480);
89 //! [8]
100 //! [8]
90
101
91 //! [9]
102 //! [9]
92 // create main layout
103 // create main layout
93 QGridLayout* mainLayout = new QGridLayout;
104 QGridLayout* mainLayout = new QGridLayout;
94 mainLayout->addWidget(tableView, 1, 0);
105 mainLayout->addWidget(tableView, 1, 0);
95 mainLayout->addWidget(chartView, 1, 1);
106 mainLayout->addWidget(chartView, 1, 1);
96 mainLayout->setColumnStretch(1, 1);
107 mainLayout->setColumnStretch(1, 1);
97 mainLayout->setColumnStretch(0, 0);
108 mainLayout->setColumnStretch(0, 0);
98 setLayout(mainLayout);
109 setLayout(mainLayout);
99 //! [9]
110 //! [9]
100 }
111 }
@@ -1,62 +1,65
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "declarativepieseries.h"
21 #include "declarativepieseries.h"
22 #include "declarativechart.h"
22 #include "declarativechart.h"
23 #include "qchart.h"
23 #include "qchart.h"
24 #include <qdeclarativelist.h>
24 #include <qdeclarativelist.h>
25 #include "qpiemodelmapper.h"
25
26
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27
28
28 DeclarativePieSeries::DeclarativePieSeries(QObject *parent) :
29 DeclarativePieSeries::DeclarativePieSeries(QObject *parent) :
29 QPieSeries(parent)
30 QPieSeries(parent)
30 {
31 {
31 }
32 }
32
33
33 QPieSlice *DeclarativePieSeries::slice(int index)
34 QPieSlice *DeclarativePieSeries::slice(int index)
34 {
35 {
35 QList<QPieSlice*> sliceList = slices();
36 QList<QPieSlice*> sliceList = slices();
36 if (index < sliceList.count())
37 if (index < sliceList.count())
37 return sliceList[index];
38 return sliceList[index];
38
39
39 return 0;
40 return 0;
40 }
41 }
41
42
42 bool DeclarativePieSeries::setPieModel(DeclarativePieModel *model)
43
44 void DeclarativePieSeries::setPieModel(DeclarativePieModel *model)
43 {
45 {
44 QAbstractItemModel *m = qobject_cast<QAbstractItemModel *>(model);
46 QAbstractItemModel *m = qobject_cast<QAbstractItemModel *>(model);
45 bool value(false);
46 if (m) {
47 if (m) {
47 value = QPieSeries::setModel(m);
48 QPieSeries::setModel(m);
48 setModelMapping(0, 1, Qt::Vertical);
49 QPieModelMapper *mapper = new QPieModelMapper;
50 mapper->setMapValues(0);
51 mapper->setMapLabels(1);
52 QPieSeries::setModelMapper(mapper);
49 } else {
53 } else {
50 qWarning("DeclarativePieSeries: Illegal model");
54 qWarning("DeclarativePieSeries: Illegal model");
51 }
55 }
52 return value;
53 }
56 }
54
57
55 DeclarativePieModel *DeclarativePieSeries::pieModel()
58 DeclarativePieModel *DeclarativePieSeries::pieModel()
56 {
59 {
57 return qobject_cast<DeclarativePieModel *>(model());
60 return qobject_cast<DeclarativePieModel *>(model());
58 }
61 }
59
62
60 #include "moc_declarativepieseries.cpp"
63 #include "moc_declarativepieseries.cpp"
61
64
62 QTCOMMERCIALCHART_END_NAMESPACE
65 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,56 +1,56
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #ifndef DECLARATIVEPIESERIES_H
21 #ifndef DECLARATIVEPIESERIES_H
22 #define DECLARATIVEPIESERIES_H
22 #define DECLARATIVEPIESERIES_H
23
23
24 #include "qchartglobal.h"
24 #include "qchartglobal.h"
25 #include "qpieslice.h"
25 #include "qpieslice.h"
26 #include "qpieseries.h"
26 #include "qpieseries.h"
27 #include <QDeclarativeListProperty>
27 #include <QDeclarativeListProperty>
28 #include <QAbstractItemModel>
28 #include <QAbstractItemModel>
29 #include <QVariant>
29 #include <QVariant>
30 #include "declarativemodel.h"
30 #include "declarativemodel.h"
31
31
32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33
33
34 class QChart;
34 class QChart;
35
35
36 class DeclarativePieSeries : public QPieSeries
36 class DeclarativePieSeries : public QPieSeries
37 {
37 {
38 Q_OBJECT
38 Q_OBJECT
39 Q_PROPERTY(DeclarativePieModel *model READ pieModel WRITE setPieModel)
39 Q_PROPERTY(DeclarativePieModel *model READ pieModel WRITE setPieModel)
40
40
41 public:
41 public:
42 explicit DeclarativePieSeries(QObject *parent = 0);
42 explicit DeclarativePieSeries(QObject *parent = 0);
43
43
44 public:
44 public:
45 Q_INVOKABLE QPieSlice *slice(int index);
45 Q_INVOKABLE QPieSlice *slice(int index);
46
46
47 public Q_SLOTS:
47 public Q_SLOTS:
48
48
49 public:
49 public:
50 bool setPieModel(DeclarativePieModel *model);
50 void setPieModel(DeclarativePieModel *model);
51 DeclarativePieModel *pieModel();
51 DeclarativePieModel *pieModel();
52 };
52 };
53
53
54 QTCOMMERCIALCHART_END_NAMESPACE
54 QTCOMMERCIALCHART_END_NAMESPACE
55
55
56 #endif // DECLARATIVEPIESERIES_H
56 #endif // DECLARATIVEPIESERIES_H
@@ -1,59 +1,63
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 //#include "DeclarativeXySeries.h"
21 //#include "DeclarativeXySeries.h"
22 #include "declarativexyseries.h"
22 #include "declarativexyseries.h"
23 #include "qxyseries.h"
23 #include "qxyseries.h"
24 #include "qxymodelmapper.h"
24 #include "declarativechart.h"
25 #include "declarativechart.h"
25
26
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27
28
28 DeclarativeXySeries::DeclarativeXySeries()
29 DeclarativeXySeries::DeclarativeXySeries()
29 {
30 {
30 }
31 }
31
32
32 DeclarativeXySeries::~DeclarativeXySeries()
33 DeclarativeXySeries::~DeclarativeXySeries()
33 {
34 {
34 }
35 }
35
36
36 bool DeclarativeXySeries::setDeclarativeModel(DeclarativeXyModel *model)
37 bool DeclarativeXySeries::setDeclarativeModel(DeclarativeXyModel *model)
37 {
38 {
38 QAbstractItemModel *m = qobject_cast<QAbstractItemModel *>(model);
39 QAbstractItemModel *m = qobject_cast<QAbstractItemModel *>(model);
39 bool value(false);
40 bool value(false);
40 if (m) {
41 if (m) {
41 // All the inherited objects must be of type QXYSeries, so it is safe to cast
42 // All the inherited objects must be of type QXYSeries, so it is safe to cast
42 QXYSeries *series = reinterpret_cast<QXYSeries *>(this);
43 QXYSeries *series = reinterpret_cast<QXYSeries *>(this);
43 value = series->setModel(m);
44 series->setModel(m);
44 series->setModelMapping(0, 1, Qt::Vertical);
45 QXYModelMapper *mapper = new QXYModelMapper;
46 mapper->setMapX(0);
47 mapper->setMapY(1);
48 series->setModelMapper(mapper);
45 } else {
49 } else {
46 qWarning("DeclarativeXySeries: Illegal model");
50 qWarning("DeclarativeXySeries: Illegal model");
47 }
51 }
48 return value;
52 return value;
49 }
53 }
50
54
51 DeclarativeXyModel *DeclarativeXySeries::declarativeModel()
55 DeclarativeXyModel *DeclarativeXySeries::declarativeModel()
52 {
56 {
53 // All the inherited objects must be of type QXYSeries, so it is safe to cast
57 // All the inherited objects must be of type QXYSeries, so it is safe to cast
54 QXYSeries *series = reinterpret_cast<QXYSeries *>(this);
58 QXYSeries *series = reinterpret_cast<QXYSeries *>(this);
55 Q_ASSERT(series);
59 Q_ASSERT(series);
56 return qobject_cast<DeclarativeXyModel *>(series->model());
60 return qobject_cast<DeclarativeXyModel *>(series->model());
57 }
61 }
58
62
59 QTCOMMERCIALCHART_END_NAMESPACE
63 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,285 +1,284
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "qareaseries.h"
21 #include "qareaseries.h"
22 #include "qareaseries_p.h"
22 #include "qareaseries_p.h"
23 #include "qlineseries.h"
23 #include "qlineseries.h"
24 #include "areachartitem_p.h"
24 #include "areachartitem_p.h"
25 #include "legendmarker_p.h"
25 #include "legendmarker_p.h"
26 #include "domain_p.h"
26 #include "domain_p.h"
27 #include "chartdataset_p.h"
27 #include "chartdataset_p.h"
28 #include "charttheme_p.h"
28 #include "charttheme_p.h"
29 #include "chartanimator_p.h"
29 #include "chartanimator_p.h"
30
30
31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32
32
33 /*!
33 /*!
34 \class QAreaSeries
34 \class QAreaSeries
35 \brief The QAreaSeries class is used for making area charts.
35 \brief The QAreaSeries class is used for making area charts.
36
36
37 \mainclass
37 \mainclass
38
38
39 An area chart is used to show quantitative data. It is based on line chart, in the way that area between axis and the line
39 An area chart is used to show quantitative data. It is based on line chart, in the way that area between axis and the line
40 is emphasized with color. Since the area chart is based on line chart, QAreaSeries constructor needs QLineSeries instance,
40 is emphasized with color. Since the area chart is based on line chart, QAreaSeries constructor needs QLineSeries instance,
41 which defines "upper" boundary of the area. "Lower" boundary is defined by default by axis X. Instead of axis X "lower" boundary can be specified by other line.
41 which defines "upper" boundary of the area. "Lower" boundary is defined by default by axis X. Instead of axis X "lower" boundary can be specified by other line.
42 In that case QAreaSeries should be initiated with two QLineSerie instances. Please note terms "upper" and "lower" boundary can be misleading in cases
42 In that case QAreaSeries should be initiated with two QLineSerie instances. Please note terms "upper" and "lower" boundary can be misleading in cases
43 where "lower" boundary had bigger values than the "upper" one, however the main point that area between these two boundary lines will be filled.
43 where "lower" boundary had bigger values than the "upper" one, however the main point that area between these two boundary lines will be filled.
44
44
45 See the \l {AreaChart Example} {area chart example} to learn how to create a simple area chart.
45 See the \l {AreaChart Example} {area chart example} to learn how to create a simple area chart.
46 \image examples_areachart.png
46 \image examples_areachart.png
47 */
47 */
48
48
49 /*!
49 /*!
50 \fn virtual QSeriesType QAreaSeries::type() const
50 \fn virtual QSeriesType QAreaSeries::type() const
51 \brief Returns type of series.
51 \brief Returns type of series.
52 \sa QAbstractSeries, QSeriesType
52 \sa QAbstractSeries, QSeriesType
53 */
53 */
54
54
55 /*!
55 /*!
56 \fn QLineSeries* QAreaSeries::upperSeries() const
56 \fn QLineSeries* QAreaSeries::upperSeries() const
57 \brief Returns upperSeries used to define one of area boundaries.
57 \brief Returns upperSeries used to define one of area boundaries.
58 */
58 */
59
59
60 /*!
60 /*!
61 \fn QLineSeries* QAreaSeries::lowerSeries() const
61 \fn QLineSeries* QAreaSeries::lowerSeries() const
62 \brief Returns lowerSeries used to define one of area boundaries. Note if QAreaSeries where counstucted wihtout a\ lowerSeries
62 \brief Returns lowerSeries used to define one of area boundaries. Note if QAreaSeries where counstucted wihtout a\ lowerSeries
63 this function return Null pointer.
63 this function return Null pointer.
64 */
64 */
65
65
66 /*!
66 /*!
67 \fn QPen QAreaSeries::pen() const
67 \fn QPen QAreaSeries::pen() const
68 \brief Returns the pen used to draw line for this series.
68 \brief Returns the pen used to draw line for this series.
69 \sa setPen()
69 \sa setPen()
70 */
70 */
71
71
72 /*!
72 /*!
73 \fn QPen QAreaSeries::brush() const
73 \fn QPen QAreaSeries::brush() const
74 \brief Returns the brush used to draw line for this series.
74 \brief Returns the brush used to draw line for this series.
75 \sa setBrush()
75 \sa setBrush()
76 */
76 */
77
77
78 /*!
78 /*!
79 \fn bool QAreaSeries::pointsVisible() const
79 \fn bool QAreaSeries::pointsVisible() const
80 \brief Returns if the points are drawn for this series.
80 \brief Returns if the points are drawn for this series.
81 \sa setPointsVisible()
81 \sa setPointsVisible()
82 */
82 */
83
83
84 /*!
84 /*!
85 \fn void QAreaSeries::clicked(const QPointF& point)
85 \fn void QAreaSeries::clicked(const QPointF& point)
86 \brief Signal is emitted when user clicks the \a point on area chart.
86 \brief Signal is emitted when user clicks the \a point on area chart.
87 */
87 */
88
88
89 /*!
89 /*!
90 \fn void QAreaSeries::selected()
90 \fn void QAreaSeries::selected()
91
91
92 The signal is emitted if the user selects/deselects the XY series. The logic for maintaining selections should be
92 The signal is emitted if the user selects/deselects the XY series. The logic for maintaining selections should be
93 implemented by the user of QAreaSeries API.
93 implemented by the user of QAreaSeries API.
94 */
94 */
95
95
96 /*!
96 /*!
97 \fn void QAreaSeriesPrivate::updated()
97 \fn void QAreaSeriesPrivate::updated()
98 \brief \internal
98 \brief \internal
99 */
99 */
100
100
101 /*!
101 /*!
102 Constructs area series object which is a child of \a upperSeries. Area will be spanned between \a
102 Constructs area series object which is a child of \a upperSeries. Area will be spanned between \a
103 upperSeries line and \a lowerSeries line. If no \a lowerSeries is passed to constructor, area is specified by axis x (y=0) instead.
103 upperSeries line and \a lowerSeries line. If no \a lowerSeries is passed to constructor, area is specified by axis x (y=0) instead.
104 When series object is added to QChartView or QChart instance ownerships is transferred.
104 When series object is added to QChartView or QChart instance ownerships is transferred.
105 */
105 */
106 QAreaSeries::QAreaSeries(QLineSeries *upperSeries, QLineSeries *lowerSeries)
106 QAreaSeries::QAreaSeries(QLineSeries *upperSeries, QLineSeries *lowerSeries)
107 : QAbstractSeries(*new QAreaSeriesPrivate(upperSeries,lowerSeries,this),upperSeries)
107 : QAbstractSeries(*new QAreaSeriesPrivate(upperSeries,lowerSeries,this),upperSeries)
108 {
108 {
109 }
109 }
110
110
111 /*!
111 /*!
112 Destroys the object. Series added to QChartView or QChart instances are owned by those,
112 Destroys the object. Series added to QChartView or QChart instances are owned by those,
113 and are deleted when mentioned object are destroyed.
113 and are deleted when mentioned object are destroyed.
114 */
114 */
115 QAreaSeries::~QAreaSeries()
115 QAreaSeries::~QAreaSeries()
116 {
116 {
117 }
117 }
118
118
119
119
120 QAbstractSeries::SeriesType QAreaSeries::type() const
120 QAbstractSeries::SeriesType QAreaSeries::type() const
121 {
121 {
122 return QAbstractSeries::SeriesTypeArea;
122 return QAbstractSeries::SeriesTypeArea;
123 }
123 }
124
124
125 QLineSeries* QAreaSeries::upperSeries() const
125 QLineSeries* QAreaSeries::upperSeries() const
126 {
126 {
127 Q_D(const QAreaSeries);
127 Q_D(const QAreaSeries);
128 return d->m_upperSeries;
128 return d->m_upperSeries;
129 }
129 }
130
130
131 QLineSeries* QAreaSeries::lowerSeries() const
131 QLineSeries* QAreaSeries::lowerSeries() const
132 {
132 {
133 Q_D(const QAreaSeries);
133 Q_D(const QAreaSeries);
134 return d->m_lowerSeries;
134 return d->m_lowerSeries;
135 }
135 }
136
136
137 /*!
137 /*!
138 Sets \a pen used for drawing area outline.
138 Sets \a pen used for drawing area outline.
139 */
139 */
140 void QAreaSeries::setPen(const QPen &pen)
140 void QAreaSeries::setPen(const QPen &pen)
141 {
141 {
142 Q_D(QAreaSeries);
142 Q_D(QAreaSeries);
143 if (d->m_pen != pen) {
143 if (d->m_pen != pen) {
144 d->m_pen = pen;
144 d->m_pen = pen;
145 emit d->updated();
145 emit d->updated();
146 }
146 }
147 }
147 }
148
148
149 QPen QAreaSeries::pen() const
149 QPen QAreaSeries::pen() const
150 {
150 {
151 Q_D(const QAreaSeries);
151 Q_D(const QAreaSeries);
152 return d->m_pen;
152 return d->m_pen;
153 }
153 }
154
154
155 /*!
155 /*!
156 Sets \a brush used for filling the area.
156 Sets \a brush used for filling the area.
157 */
157 */
158 void QAreaSeries::setBrush(const QBrush &brush)
158 void QAreaSeries::setBrush(const QBrush &brush)
159 {
159 {
160 Q_D(QAreaSeries);
160 Q_D(QAreaSeries);
161 if (d->m_brush != brush) {
161 if (d->m_brush != brush) {
162 d->m_brush = brush;
162 d->m_brush = brush;
163 emit d->updated();
163 emit d->updated();
164 }
164 }
165 }
165 }
166
166
167 QBrush QAreaSeries::brush() const
167 QBrush QAreaSeries::brush() const
168 {
168 {
169 Q_D(const QAreaSeries);
169 Q_D(const QAreaSeries);
170 return d->m_brush;
170 return d->m_brush;
171 }
171 }
172 /*!
172 /*!
173 Sets if data points are \a visible and should be drawn on line.
173 Sets if data points are \a visible and should be drawn on line.
174 */
174 */
175 void QAreaSeries::setPointsVisible(bool visible)
175 void QAreaSeries::setPointsVisible(bool visible)
176 {
176 {
177 Q_D(QAreaSeries);
177 Q_D(QAreaSeries);
178 if (d->m_pointsVisible != visible) {
178 if (d->m_pointsVisible != visible) {
179 d->m_pointsVisible = visible;
179 d->m_pointsVisible = visible;
180 emit d->updated();
180 emit d->updated();
181 }
181 }
182 }
182 }
183
183
184 bool QAreaSeries::pointsVisible() const
184 bool QAreaSeries::pointsVisible() const
185 {
185 {
186 Q_D(const QAreaSeries);
186 Q_D(const QAreaSeries);
187 return d->m_pointsVisible;
187 return d->m_pointsVisible;
188 }
188 }
189
189
190 /*!
190 /*!
191 Does nothing at present. Paremeter \a model is not used. Always returns false.
191 Does nothing at present. Paremeter \a model is not used. Always returns false.
192 To set the model for area series set the models for upperSeries, lowerSeries
192 To set the model for area series set the models for upperSeries, lowerSeries
193 */
193 */
194 bool QAreaSeries::setModel(QAbstractItemModel* model)
194 void QAreaSeries::setModel(QAbstractItemModel* model)
195 {
195 {
196 Q_UNUSED(model);
196 Q_UNUSED(model);
197 qWarning()<<"Not implemented";
197 qWarning()<<"Not implemented";
198 return false;
199 }
198 }
200
199
201 /*!
200 /*!
202 Does nothing at present. Always returns 0;
201 Does nothing at present. Always returns 0;
203 To get the model set for area series call upperSeries->model(), lowerSeries->model()
202 To get the model set for area series call upperSeries->model(), lowerSeries->model()
204 */
203 */
205 QAbstractItemModel* QAreaSeries::model() const
204 QAbstractItemModel* QAreaSeries::model() const
206 {
205 {
207 return 0;
206 return 0;
208 }
207 }
209
208
210 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
209 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
211
210
212 QAreaSeriesPrivate::QAreaSeriesPrivate(QLineSeries *upperSeries, QLineSeries *lowerSeries,QAreaSeries* q) :
211 QAreaSeriesPrivate::QAreaSeriesPrivate(QLineSeries *upperSeries, QLineSeries *lowerSeries,QAreaSeries* q) :
213 QAbstractSeriesPrivate(q),
212 QAbstractSeriesPrivate(q),
214 m_upperSeries(upperSeries),
213 m_upperSeries(upperSeries),
215 m_lowerSeries(lowerSeries),
214 m_lowerSeries(lowerSeries),
216 m_pointsVisible(false)
215 m_pointsVisible(false)
217 {
216 {
218 }
217 }
219
218
220 void QAreaSeriesPrivate::scaleDomain(Domain& domain)
219 void QAreaSeriesPrivate::scaleDomain(Domain& domain)
221 {
220 {
222 Q_Q(QAreaSeries);
221 Q_Q(QAreaSeries);
223
222
224 qreal minX(domain.minX());
223 qreal minX(domain.minX());
225 qreal minY(domain.minY());
224 qreal minY(domain.minY());
226 qreal maxX(domain.maxX());
225 qreal maxX(domain.maxX());
227 qreal maxY(domain.maxY());
226 qreal maxY(domain.maxY());
228 int tickXCount(domain.tickXCount());
227 int tickXCount(domain.tickXCount());
229 int tickYCount(domain.tickYCount());
228 int tickYCount(domain.tickYCount());
230
229
231 QLineSeries* upperSeries = q->upperSeries();
230 QLineSeries* upperSeries = q->upperSeries();
232 QLineSeries* lowerSeries = q->lowerSeries();
231 QLineSeries* lowerSeries = q->lowerSeries();
233
232
234 const QList<QPointF>& points = upperSeries->points();
233 const QList<QPointF>& points = upperSeries->points();
235
234
236 for (int i = 0; i < points.count(); i++)
235 for (int i = 0; i < points.count(); i++)
237 {
236 {
238 qreal x = points[i].x();
237 qreal x = points[i].x();
239 qreal y = points[i].y();
238 qreal y = points[i].y();
240 minX = qMin(minX, x);
239 minX = qMin(minX, x);
241 minY = qMin(minY, y);
240 minY = qMin(minY, y);
242 maxX = qMax(maxX, x);
241 maxX = qMax(maxX, x);
243 maxY = qMax(maxY, y);
242 maxY = qMax(maxY, y);
244 }
243 }
245 if(lowerSeries) {
244 if(lowerSeries) {
246
245
247 const QList<QPointF>& points = lowerSeries->points();
246 const QList<QPointF>& points = lowerSeries->points();
248
247
249 for (int i = 0; i < points.count(); i++)
248 for (int i = 0; i < points.count(); i++)
250 {
249 {
251 qreal x = points[i].x();
250 qreal x = points[i].x();
252 qreal y = points[i].y();
251 qreal y = points[i].y();
253 minX = qMin(minX, x);
252 minX = qMin(minX, x);
254 minY = qMin(minY, y);
253 minY = qMin(minY, y);
255 maxX = qMax(maxX, x);
254 maxX = qMax(maxX, x);
256 maxY = qMax(maxY, y);
255 maxY = qMax(maxY, y);
257 }}
256 }}
258
257
259 domain.setRange(minX,maxX,minY,maxY,tickXCount,tickYCount);
258 domain.setRange(minX,maxX,minY,maxY,tickXCount,tickYCount);
260 }
259 }
261
260
262 Chart* QAreaSeriesPrivate::createGraphics(ChartPresenter* presenter)
261 Chart* QAreaSeriesPrivate::createGraphics(ChartPresenter* presenter)
263 {
262 {
264 Q_Q(QAreaSeries);
263 Q_Q(QAreaSeries);
265
264
266 AreaChartItem* area = new AreaChartItem(q,presenter);
265 AreaChartItem* area = new AreaChartItem(q,presenter);
267 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
266 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
268 presenter->animator()->addAnimation(area->upperLineItem());
267 presenter->animator()->addAnimation(area->upperLineItem());
269 if(q->lowerSeries()) presenter->animator()->addAnimation(area->lowerLineItem());
268 if(q->lowerSeries()) presenter->animator()->addAnimation(area->lowerLineItem());
270 }
269 }
271 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
270 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
272 return area;
271 return area;
273 }
272 }
274
273
275 QList<LegendMarker*> QAreaSeriesPrivate::createLegendMarker(QLegend* legend)
274 QList<LegendMarker*> QAreaSeriesPrivate::createLegendMarker(QLegend* legend)
276 {
275 {
277 Q_Q(QAreaSeries);
276 Q_Q(QAreaSeries);
278 QList<LegendMarker*> list;
277 QList<LegendMarker*> list;
279 return list << new AreaLegendMarker(q,legend);
278 return list << new AreaLegendMarker(q,legend);
280 }
279 }
281
280
282 #include "moc_qareaseries.cpp"
281 #include "moc_qareaseries.cpp"
283 #include "moc_qareaseries_p.cpp"
282 #include "moc_qareaseries_p.cpp"
284
283
285 QTCOMMERCIALCHART_END_NAMESPACE
284 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,71 +1,71
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #ifndef QAREASERIES_H
21 #ifndef QAREASERIES_H
22 #define QAREASERIES_H
22 #define QAREASERIES_H
23
23
24 #include <qchartglobal.h>
24 #include <qchartglobal.h>
25 #include <qabstractseries.h>
25 #include <qabstractseries.h>
26 #include <QPen>
26 #include <QPen>
27 #include <QBrush>
27 #include <QBrush>
28
28
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 class QLineSeries;
30 class QLineSeries;
31 class QAreaSeriesPrivate;
31 class QAreaSeriesPrivate;
32
32
33 class QTCOMMERCIALCHART_EXPORT QAreaSeries : public QAbstractSeries
33 class QTCOMMERCIALCHART_EXPORT QAreaSeries : public QAbstractSeries
34 {
34 {
35 Q_OBJECT
35 Q_OBJECT
36 public:
36 public:
37 explicit QAreaSeries(QLineSeries *upperSeries, QLineSeries *lowerSeries = 0);
37 explicit QAreaSeries(QLineSeries *upperSeries, QLineSeries *lowerSeries = 0);
38 ~QAreaSeries();
38 ~QAreaSeries();
39
39
40 public:
40 public:
41 QAbstractSeries::SeriesType type() const;
41 QAbstractSeries::SeriesType type() const;
42
42
43 QLineSeries* upperSeries() const;
43 QLineSeries* upperSeries() const;
44 QLineSeries* lowerSeries() const;
44 QLineSeries* lowerSeries() const;
45
45
46 void setPen(const QPen &pen);
46 void setPen(const QPen &pen);
47 QPen pen() const;
47 QPen pen() const;
48
48
49 void setBrush(const QBrush &brush);
49 void setBrush(const QBrush &brush);
50 QBrush brush() const;
50 QBrush brush() const;
51
51
52 void setPointsVisible(bool visible = true);
52 void setPointsVisible(bool visible = true);
53 bool pointsVisible() const;
53 bool pointsVisible() const;
54
54
55 bool setModel(QAbstractItemModel* model);
55 void setModel(QAbstractItemModel* model);
56 QAbstractItemModel* model() const;
56 QAbstractItemModel* model() const;
57
57
58 Q_SIGNALS:
58 Q_SIGNALS:
59 void clicked(const QPointF &point);
59 void clicked(const QPointF &point);
60 void selected();
60 void selected();
61
61
62 private:
62 private:
63 Q_DECLARE_PRIVATE(QAreaSeries);
63 Q_DECLARE_PRIVATE(QAreaSeries);
64 Q_DISABLE_COPY(QAreaSeries);
64 Q_DISABLE_COPY(QAreaSeries);
65 friend class AreaLegendMarker;
65 friend class AreaLegendMarker;
66 friend class AreaChartItem;
66 friend class AreaChartItem;
67 };
67 };
68
68
69 QTCOMMERCIALCHART_END_NAMESPACE
69 QTCOMMERCIALCHART_END_NAMESPACE
70
70
71 #endif
71 #endif
@@ -1,704 +1,702
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "qbarseries.h"
21 #include "qbarseries.h"
22 #include "qbarseries_p.h"
22 #include "qbarseries_p.h"
23 #include "qbarset.h"
23 #include "qbarset.h"
24 #include "qbarset_p.h"
24 #include "qbarset_p.h"
25 #include "domain_p.h"
25 #include "domain_p.h"
26 #include "legendmarker_p.h"
26 #include "legendmarker_p.h"
27 #include "chartdataset_p.h"
27 #include "chartdataset_p.h"
28 #include "charttheme_p.h"
28 #include "charttheme_p.h"
29 #include "chartanimator_p.h"
29 #include "chartanimator_p.h"
30
30
31 #include <QAbstractItemModel>
31 #include <QAbstractItemModel>
32 #include <QModelIndex>
32 #include <QModelIndex>
33
33
34 QTCOMMERCIALCHART_BEGIN_NAMESPACE
34 QTCOMMERCIALCHART_BEGIN_NAMESPACE
35
35
36 /*!
36 /*!
37 \class QBarSeries
37 \class QBarSeries
38 \brief part of QtCommercial chart API.
38 \brief part of QtCommercial chart API.
39 \mainclass
39 \mainclass
40
40
41 QBarSeries represents a series of data shown as bars. One QBarSeries can contain multiple
41 QBarSeries represents a series of data shown as bars. One QBarSeries can contain multiple
42 QBarSet data sets. QBarSeries groups the data from sets to categories, which are defined
42 QBarSet data sets. QBarSeries groups the data from sets to categories, which are defined
43 by QStringList.
43 by QStringList.
44
44
45 See the \l {BarChart Example} {bar chart example} to learn how to create a simple bar chart.
45 See the \l {BarChart Example} {bar chart example} to learn how to create a simple bar chart.
46 \image examples_barchart.png
46 \image examples_barchart.png
47
47
48 \sa QBarSet, QStackedBarSeries, QPercentBarSeries
48 \sa QBarSet, QStackedBarSeries, QPercentBarSeries
49 */
49 */
50
50
51 /*!
51 /*!
52 \fn void QBarSeries::clicked(QBarSet *barset, QString category)
52 \fn void QBarSeries::clicked(QBarSet *barset, QString category)
53
53
54 The signal is emitted if the user clicks with a mouse on top of QBarSet \a barset of category \a category
54 The signal is emitted if the user clicks with a mouse on top of QBarSet \a barset of category \a category
55 contained by the series.
55 contained by the series.
56 */
56 */
57
57
58 /*!
58 /*!
59 \fn void QBarSeries::hovered(QBarSet* barset, bool status)
59 \fn void QBarSeries::hovered(QBarSet* barset, bool status)
60
60
61 The signal is emitted if mouse is hovered on top of series.
61 The signal is emitted if mouse is hovered on top of series.
62 Parameter \a barset is the pointer of barset, where hover happened.
62 Parameter \a barset is the pointer of barset, where hover happened.
63 Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series.
63 Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series.
64 */
64 */
65
65
66 /*!
66 /*!
67 Constructs empty QBarSeries. Parameter \a categories defines the categories for chart.
67 Constructs empty QBarSeries. Parameter \a categories defines the categories for chart.
68 QBarSeries is QObject which is a child of a \a parent.
68 QBarSeries is QObject which is a child of a \a parent.
69 */
69 */
70 QBarSeries::QBarSeries(/*QBarCategories categories,*/ QObject *parent) :
70 QBarSeries::QBarSeries(/*QBarCategories categories,*/ QObject *parent) :
71 QAbstractSeries(*new QBarSeriesPrivate(/*categories,*/ this),parent)
71 QAbstractSeries(*new QBarSeriesPrivate(/*categories,*/ this),parent)
72 {
72 {
73 }
73 }
74
74
75 /*!
75 /*!
76 Destructs barseries and owned barsets.
76 Destructs barseries and owned barsets.
77 */
77 */
78 QBarSeries::~QBarSeries()
78 QBarSeries::~QBarSeries()
79 {
79 {
80 // NOTE: d_ptr destroyed by QObject
80 // NOTE: d_ptr destroyed by QObject
81 }
81 }
82
82
83 /*!
83 /*!
84 \internal
84 \internal
85 */
85 */
86 QBarSeries::QBarSeries(QBarSeriesPrivate &d, QObject *parent) :
86 QBarSeries::QBarSeries(QBarSeriesPrivate &d, QObject *parent) :
87 QAbstractSeries(d,parent)
87 QAbstractSeries(d,parent)
88 {
88 {
89 }
89 }
90
90
91 /*!
91 /*!
92 Returns the type of series. Derived classes override this.
92 Returns the type of series. Derived classes override this.
93 */
93 */
94 QAbstractSeries::SeriesType QBarSeries::type() const
94 QAbstractSeries::SeriesType QBarSeries::type() const
95 {
95 {
96 return QAbstractSeries::SeriesTypeBar;
96 return QAbstractSeries::SeriesTypeBar;
97 }
97 }
98
98
99 void QBarSeries::setCategories(QBarCategories categories)
99 void QBarSeries::setCategories(QBarCategories categories)
100 {
100 {
101 Q_D(QBarSeries);
101 Q_D(QBarSeries);
102 d->setCategories(categories);
102 d->setCategories(categories);
103 emit d->categoriesUpdated();
103 emit d->categoriesUpdated();
104 }
104 }
105
105
106 /*!
106 /*!
107 Adds a set of bars to series. Takes ownership of \a set.
107 Adds a set of bars to series. Takes ownership of \a set.
108 */
108 */
109 bool QBarSeries::appendBarSet(QBarSet *set)
109 bool QBarSeries::appendBarSet(QBarSet *set)
110 {
110 {
111 Q_D(QBarSeries);
111 Q_D(QBarSeries);
112 if ((d->m_barSets.contains(set)) || (set == 0)) {
112 if ((d->m_barSets.contains(set)) || (set == 0)) {
113 // Fail if set is already in list or set is null.
113 // Fail if set is already in list or set is null.
114 return false;
114 return false;
115 }
115 }
116 d->m_barSets.append(set);
116 d->m_barSets.append(set);
117 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), d, SLOT(barsetChanged()));
117 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), d, SLOT(barsetChanged()));
118 emit d->restructuredBars();
118 emit d->restructuredBars();
119 return true;
119 return true;
120 }
120 }
121
121
122 /*!
122 /*!
123 Removes a set of bars from series. Releases ownership of \a set. Doesn't delete \a set.
123 Removes a set of bars from series. Releases ownership of \a set. Doesn't delete \a set.
124 */
124 */
125 bool QBarSeries::removeBarSet(QBarSet *set)
125 bool QBarSeries::removeBarSet(QBarSet *set)
126 {
126 {
127 Q_D(QBarSeries);
127 Q_D(QBarSeries);
128 if (!d->m_barSets.contains(set)) {
128 if (!d->m_barSets.contains(set)) {
129 // Fail if set is not in list
129 // Fail if set is not in list
130 return false;
130 return false;
131 }
131 }
132 d->m_barSets.removeOne(set);
132 d->m_barSets.removeOne(set);
133 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), d, SLOT(barsetChanged()));
133 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), d, SLOT(barsetChanged()));
134 emit d->restructuredBars();
134 emit d->restructuredBars();
135 return true;
135 return true;
136 }
136 }
137
137
138 /*!
138 /*!
139 Adds a list of barsets to series. Takes ownership of \a sets.
139 Adds a list of barsets to series. Takes ownership of \a sets.
140 */
140 */
141 bool QBarSeries::appendBarSets(QList<QBarSet* > sets)
141 bool QBarSeries::appendBarSets(QList<QBarSet* > sets)
142 {
142 {
143 Q_D(QBarSeries);
143 Q_D(QBarSeries);
144 foreach (QBarSet* set, sets) {
144 foreach (QBarSet* set, sets) {
145 if ((set == 0) || (d->m_barSets.contains(set))) {
145 if ((set == 0) || (d->m_barSets.contains(set))) {
146 // Fail if any of the sets is null or is already appended.
146 // Fail if any of the sets is null or is already appended.
147 return false;
147 return false;
148 }
148 }
149 if (sets.count(set) != 1) {
149 if (sets.count(set) != 1) {
150 // Also fail if same set is more than once in given list.
150 // Also fail if same set is more than once in given list.
151 return false;
151 return false;
152 }
152 }
153 }
153 }
154
154
155 foreach (QBarSet* set, sets) {
155 foreach (QBarSet* set, sets) {
156 d->m_barSets.append(set);
156 d->m_barSets.append(set);
157 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), d, SLOT(barsetChanged()));
157 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), d, SLOT(barsetChanged()));
158 }
158 }
159 emit d->restructuredBars();
159 emit d->restructuredBars();
160 return true;
160 return true;
161 }
161 }
162
162
163 /*!
163 /*!
164 Removes a list of barsets from series. Releases ownership of \a sets. Doesn't delete \a sets.
164 Removes a list of barsets from series. Releases ownership of \a sets. Doesn't delete \a sets.
165 */
165 */
166 bool QBarSeries::removeBarSets(QList<QBarSet* > sets)
166 bool QBarSeries::removeBarSets(QList<QBarSet* > sets)
167 {
167 {
168 Q_D(QBarSeries);
168 Q_D(QBarSeries);
169
169
170 bool setsRemoved = false;
170 bool setsRemoved = false;
171 foreach (QBarSet* set, sets) {
171 foreach (QBarSet* set, sets) {
172 if (d->m_barSets.contains(set)) {
172 if (d->m_barSets.contains(set)) {
173 d->m_barSets.removeOne(set);
173 d->m_barSets.removeOne(set);
174 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), d, SLOT(barsetChanged()));
174 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), d, SLOT(barsetChanged()));
175 setsRemoved = true;
175 setsRemoved = true;
176 }
176 }
177 }
177 }
178
178
179 if (setsRemoved) {
179 if (setsRemoved) {
180 emit d->restructuredBars();
180 emit d->restructuredBars();
181 }
181 }
182 return setsRemoved;
182 return setsRemoved;
183 }
183 }
184
184
185 /*!
185 /*!
186 Returns number of sets in series.
186 Returns number of sets in series.
187 */
187 */
188 int QBarSeries::barsetCount() const
188 int QBarSeries::barsetCount() const
189 {
189 {
190 Q_D(const QBarSeries);
190 Q_D(const QBarSeries);
191 return d->m_barSets.count();
191 return d->m_barSets.count();
192 }
192 }
193
193
194 /*!
194 /*!
195 Returns number of categories in series
195 Returns number of categories in series
196 */
196 */
197 int QBarSeries::categoryCount() const
197 int QBarSeries::categoryCount() const
198 {
198 {
199 Q_D(const QBarSeries);
199 Q_D(const QBarSeries);
200 return d->m_categories.count();
200 return d->m_categories.count();
201 }
201 }
202
202
203 /*!
203 /*!
204 Returns a list of sets in series. Keeps ownership of sets.
204 Returns a list of sets in series. Keeps ownership of sets.
205 */
205 */
206 QList<QBarSet*> QBarSeries::barSets() const
206 QList<QBarSet*> QBarSeries::barSets() const
207 {
207 {
208 Q_D(const QBarSeries);
208 Q_D(const QBarSeries);
209 return d->m_barSets;
209 return d->m_barSets;
210 }
210 }
211
211
212 /*!
212 /*!
213 \fn bool QBarSeries::setModel(QAbstractItemModel *model)
213 \fn bool QBarSeries::setModel(QAbstractItemModel *model)
214 Sets the \a model to be used as a data source
214 Sets the \a model to be used as a data source
215 */
215 */
216 bool QBarSeries::setModel(QAbstractItemModel *model)
216 void QBarSeries::setModel(QAbstractItemModel */*model*/)
217 {
217 {
218 Q_D(QBarSeries);
218 // Q_D(QBarSeries);
219 return d->setModel(model);
219 // d->setModel(model);
220 }
220 }
221
221
222 /*!
222 /*!
223 \fn bool QBarSeries::setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation)
223 \fn bool QBarSeries::setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation)
224 Sets column/row specified by \a categories to be used as a list of bar series categories.
224 Sets column/row specified by \a categories to be used as a list of bar series categories.
225 Parameter \a bottomBoundry indicates the column/row where the first bar set is located in the model.
225 Parameter \a bottomBoundry indicates the column/row where the first bar set is located in the model.
226 Parameter \a topBoundry indicates the column/row where the last bar set is located in the model.
226 Parameter \a topBoundry indicates the column/row where the last bar set is located in the model.
227 All the columns/rows inbetween those two values are also used as data for bar sets.
227 All the columns/rows inbetween those two values are also used as data for bar sets.
228 The \a orientation parameter specifies whether the data is in columns or in rows.
228 The \a orientation parameter specifies whether the data is in columns or in rows.
229 */
229 */
230 void QBarSeries::setModelMapping(int categories, int bottomBoundary, int topBoundary, Qt::Orientation orientation)
230 //void QBarSeries::setModelMapping(int categories, int bottomBoundary, int topBoundary, Qt::Orientation orientation)
231 {
231 //{
232 Q_D(QBarSeries);
232 // Q_D(QBarSeries);
233 d->setModelMapping(categories,bottomBoundary,topBoundary,orientation);
233 // d->setModelMapping(categories,bottomBoundary,topBoundary,orientation);
234 }
234 //}
235
235
236 void QBarSeries::setModelMappingRange(int first, int count)
236 //void QBarSeries::setModelMappingRange(int first, int count)
237 {
237 //{
238 Q_D(QBarSeries);
238 // Q_D(QBarSeries);
239 d->setModelMappingRange(first, count);
239 // d->setModelMappingRange(first, count);
240 }
240 //}
241
241
242 /*!
242 /*!
243 Returns the bar categories of the series.
243 Returns the bar categories of the series.
244 */
244 */
245 QBarCategories QBarSeries::categories() const
245 QBarCategories QBarSeries::categories() const
246 {
246 {
247 Q_D(const QBarSeries);
247 Q_D(const QBarSeries);
248 return d->m_categories;
248 return d->m_categories;
249 }
249 }
250
250
251 /*!
251 /*!
252 Sets the visibility of labels in series to \a visible
252 Sets the visibility of labels in series to \a visible
253 */
253 */
254 void QBarSeries::setLabelsVisible(bool visible)
254 void QBarSeries::setLabelsVisible(bool visible)
255 {
255 {
256 foreach (QBarSet* s, barSets()) {
256 foreach (QBarSet* s, barSets()) {
257 s->setLabelsVisible(visible);
257 s->setLabelsVisible(visible);
258 }
258 }
259 }
259 }
260
260
261 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
261 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
262
262
263 QBarSeriesPrivate::QBarSeriesPrivate(/*QBarCategories categories,*/ QBarSeries *q) :
263 QBarSeriesPrivate::QBarSeriesPrivate(/*QBarCategories categories,*/ QBarSeries *q) :
264 QAbstractSeriesPrivate(q),
264 QAbstractSeriesPrivate(q),
265 // m_categories(categories),
265 // m_categories(categories),
266 m_mapCategories(-1),
266 m_mapCategories(-1),
267 m_mapBarBottom(-1),
267 m_mapBarBottom(-1),
268 m_mapBarTop(-1)
268 m_mapBarTop(-1)
269 {
269 {
270 }
270 }
271
271
272 void QBarSeriesPrivate::setCategories(QBarCategories categories)
272 void QBarSeriesPrivate::setCategories(QBarCategories categories)
273 {
273 {
274 m_categories = categories;
274 m_categories = categories;
275 }
275 }
276
276
277
277
278 QBarSet* QBarSeriesPrivate::barsetAt(int index)
278 QBarSet* QBarSeriesPrivate::barsetAt(int index)
279 {
279 {
280 return m_barSets.at(index);
280 return m_barSets.at(index);
281 }
281 }
282
282
283 QString QBarSeriesPrivate::categoryName(int category)
283 QString QBarSeriesPrivate::categoryName(int category)
284 {
284 {
285 return m_categories.at(category);
285 return m_categories.at(category);
286 }
286 }
287
287
288 qreal QBarSeriesPrivate::min()
288 qreal QBarSeriesPrivate::min()
289 {
289 {
290 if (m_barSets.count() <= 0) {
290 if (m_barSets.count() <= 0) {
291 return 0;
291 return 0;
292 }
292 }
293 qreal min = INT_MAX;
293 qreal min = INT_MAX;
294
294
295 for (int i = 0; i < m_barSets.count(); i++) {
295 for (int i = 0; i < m_barSets.count(); i++) {
296 int categoryCount = m_barSets.at(i)->count();
296 int categoryCount = m_barSets.at(i)->count();
297 for (int j = 0; j < categoryCount; j++) {
297 for (int j = 0; j < categoryCount; j++) {
298 qreal temp = m_barSets.at(i)->at(j);
298 qreal temp = m_barSets.at(i)->at(j);
299 if (temp < min)
299 if (temp < min)
300 min = temp;
300 min = temp;
301 }
301 }
302 }
302 }
303 return min;
303 return min;
304 }
304 }
305
305
306 qreal QBarSeriesPrivate::max()
306 qreal QBarSeriesPrivate::max()
307 {
307 {
308 if (m_barSets.count() <= 0) {
308 if (m_barSets.count() <= 0) {
309 return 0;
309 return 0;
310 }
310 }
311 qreal max = INT_MIN;
311 qreal max = INT_MIN;
312
312
313 for (int i = 0; i < m_barSets.count(); i++) {
313 for (int i = 0; i < m_barSets.count(); i++) {
314 int categoryCount = m_barSets.at(i)->count();
314 int categoryCount = m_barSets.at(i)->count();
315 for (int j = 0; j < categoryCount; j++) {
315 for (int j = 0; j < categoryCount; j++) {
316 qreal temp = m_barSets.at(i)->at(j);
316 qreal temp = m_barSets.at(i)->at(j);
317 if (temp > max)
317 if (temp > max)
318 max = temp;
318 max = temp;
319 }
319 }
320 }
320 }
321
321
322 return max;
322 return max;
323 }
323 }
324
324
325 qreal QBarSeriesPrivate::valueAt(int set, int category)
325 qreal QBarSeriesPrivate::valueAt(int set, int category)
326 {
326 {
327 if ((set < 0) || (set >= m_barSets.count())) {
327 if ((set < 0) || (set >= m_barSets.count())) {
328 // No set, no value.
328 // No set, no value.
329 return 0;
329 return 0;
330 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
330 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
331 // No category, no value.
331 // No category, no value.
332 return 0;
332 return 0;
333 }
333 }
334
334
335 return m_barSets.at(set)->at(category);
335 return m_barSets.at(set)->at(category);
336 }
336 }
337
337
338 qreal QBarSeriesPrivate::percentageAt(int set, int category)
338 qreal QBarSeriesPrivate::percentageAt(int set, int category)
339 {
339 {
340 if ((set < 0) || (set >= m_barSets.count())) {
340 if ((set < 0) || (set >= m_barSets.count())) {
341 // No set, no value.
341 // No set, no value.
342 return 0;
342 return 0;
343 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
343 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
344 // No category, no value.
344 // No category, no value.
345 return 0;
345 return 0;
346 }
346 }
347
347
348 qreal value = m_barSets.at(set)->at(category);
348 qreal value = m_barSets.at(set)->at(category);
349 qreal sum = categorySum(category);
349 qreal sum = categorySum(category);
350 if ( qFuzzyIsNull(sum) ) {
350 if ( qFuzzyIsNull(sum) ) {
351 return 0;
351 return 0;
352 }
352 }
353
353
354 return value / sum;
354 return value / sum;
355 }
355 }
356
356
357 qreal QBarSeriesPrivate::categorySum(int category)
357 qreal QBarSeriesPrivate::categorySum(int category)
358 {
358 {
359 qreal sum(0);
359 qreal sum(0);
360 int count = m_barSets.count(); // Count sets
360 int count = m_barSets.count(); // Count sets
361 for (int set = 0; set < count; set++) {
361 for (int set = 0; set < count; set++) {
362 if (category < m_barSets.at(set)->count())
362 if (category < m_barSets.at(set)->count())
363 sum += m_barSets.at(set)->at(category);
363 sum += m_barSets.at(set)->at(category);
364 }
364 }
365 return sum;
365 return sum;
366 }
366 }
367
367
368 qreal QBarSeriesPrivate::absoluteCategorySum(int category)
368 qreal QBarSeriesPrivate::absoluteCategorySum(int category)
369 {
369 {
370 qreal sum(0);
370 qreal sum(0);
371 int count = m_barSets.count(); // Count sets
371 int count = m_barSets.count(); // Count sets
372 for (int set = 0; set < count; set++) {
372 for (int set = 0; set < count; set++) {
373 if (category < m_barSets.at(set)->count())
373 if (category < m_barSets.at(set)->count())
374 sum += qAbs(m_barSets.at(set)->at(category));
374 sum += qAbs(m_barSets.at(set)->at(category));
375 }
375 }
376 return sum;
376 return sum;
377 }
377 }
378
378
379 qreal QBarSeriesPrivate::maxCategorySum()
379 qreal QBarSeriesPrivate::maxCategorySum()
380 {
380 {
381 qreal max = INT_MIN;
381 qreal max = INT_MIN;
382 int count = m_categories.count();
382 int count = m_categories.count();
383 for (int i = 0; i < count; i++) {
383 for (int i = 0; i < count; i++) {
384 qreal sum = categorySum(i);
384 qreal sum = categorySum(i);
385 if (sum > max)
385 if (sum > max)
386 max = sum;
386 max = sum;
387 }
387 }
388 return max;
388 return max;
389 }
389 }
390
390
391 bool QBarSeriesPrivate::setModel(QAbstractItemModel *model)
391 //void QBarSeriesPrivate::setModel(QAbstractItemModel *model)
392 {
392 //{
393 // disconnect signals from old model
393 // // disconnect signals from old model
394 if(m_model)
394 // if(m_model)
395 {
395 // {
396 disconnect(m_model, 0, this, 0);
396 // disconnect(m_model, 0, this, 0);
397 m_mapCategories = -1;
397 // m_mapCategories = -1;
398 m_mapBarBottom = -1;
398 // m_mapBarBottom = -1;
399 m_mapBarTop = -1;
399 // m_mapBarTop = -1;
400 m_mapOrientation = Qt::Vertical;
400 // m_mapOrientation = Qt::Vertical;
401 }
401 // }
402
402
403 // set new model
403 // // set new model
404 if(model)
404 // if(model)
405 {
405 // {
406 m_model = model;
406 // m_model = model;
407 return true;
407 // }
408 }
408 // else
409 else
409 // {
410 {
410 // m_model = 0;
411 m_model = 0;
411 // }
412 return false;
412 //}
413 }
414 }
415
413
416 void QBarSeriesPrivate::setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation)
414 //void QBarSeriesPrivate::setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation)
417 {
415 //{
418 Q_Q(QBarSeries);
416 // Q_Q(QBarSeries);
419
417
420 if (m_model == 0)
418 // if (m_model == 0)
421 return;
419 // return;
422
423 m_mapCategories = categories;
424 m_mapBarBottom = bottomBoundry;
425 m_mapBarTop = topBoundry;
426 m_mapOrientation = orientation;
427
428 // connect the signals
429 connect(m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex,QModelIndex)));
430 if (m_mapOrientation == Qt::Vertical) {
431 connect(m_model,SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(modelDataAdded(QModelIndex,int,int)));
432 connect(m_model,SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SLOT(modelDataRemoved(QModelIndex,int,int)));
433 } else {
434 connect(m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), this, SLOT(modelDataAdded(QModelIndex,int,int)));
435 connect(m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), this, SLOT(modelDataRemoved(QModelIndex,int,int)));
436 }
437
420
438 // create the initial bars
421 // m_mapCategories = categories;
439 m_categories.clear();
422 // m_mapBarBottom = bottomBoundry;
440 if (m_mapOrientation == Qt::Vertical) {
423 // m_mapBarTop = topBoundry;
441 int rowCount = 0;
424 // m_mapOrientation = orientation;
442 if(m_mapCount == -1)
443 rowCount = m_model->rowCount() - m_mapFirst;
444 else
445 rowCount = qMin(m_mapCount, m_model->rowCount() - m_mapFirst);
446 for (int k = m_mapFirst; k < m_mapFirst + rowCount; k++) {
447 m_categories << m_model->data(m_model->index(k, m_mapCategories), Qt::DisplayRole).toString();
448 }
449
425
450 for (int i = m_mapBarBottom; i <= m_mapBarTop; i++) {
426 // // connect the signals
451 QBarSet* barSet = new QBarSet(m_model->headerData(i, Qt::Horizontal, Qt::DisplayRole).toString());
427 // connect(m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex,QModelIndex)));
452 for(int m = m_mapFirst; m < m_mapFirst + rowCount; m++)
428 // if (m_mapOrientation == Qt::Vertical) {
453 *barSet << m_model->data(m_model->index(m, i), Qt::DisplayRole).toDouble();
429 // connect(m_model,SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(modelDataAdded(QModelIndex,int,int)));
454 q->appendBarSet(barSet);
430 // connect(m_model,SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SLOT(modelDataRemoved(QModelIndex,int,int)));
455 }
431 // } else {
456 } else {
432 // connect(m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), this, SLOT(modelDataAdded(QModelIndex,int,int)));
457 int columnCount = 0;
433 // connect(m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), this, SLOT(modelDataRemoved(QModelIndex,int,int)));
458 if(m_mapCount == -1)
434 // }
459 columnCount = m_model->columnCount() - m_mapFirst;
460 else
461 columnCount = qMin(m_mapCount, m_model->columnCount() - m_mapFirst);
462 for (int k = m_mapFirst; k < m_mapFirst + columnCount; k++) {
463 m_categories << m_model->data(m_model->index(m_mapCategories, k), Qt::DisplayRole).toString();
464 }
465
435
466 for (int i = m_mapBarBottom; i <= m_mapBarTop; i++) {
436 // // create the initial bars
467 QBarSet* barSet = new QBarSet(m_model->headerData(i, Qt::Vertical, Qt::DisplayRole).toString());
437 // m_categories.clear();
468 for(int m = m_mapFirst; m < m_mapFirst + columnCount; m++)
438 // if (m_mapOrientation == Qt::Vertical) {
469 *barSet << m_model->data(m_model->index(i, m), Qt::DisplayRole).toDouble();
439 // int rowCount = 0;
470 q->appendBarSet(barSet);
440 // if(m_mapCount == -1)
471 }
441 // rowCount = m_model->rowCount() - m_mapFirst;
472 }
442 // else
473 }
443 // rowCount = qMin(m_mapCount, m_model->rowCount() - m_mapFirst);
444 // for (int k = m_mapFirst; k < m_mapFirst + rowCount; k++) {
445 // m_categories << m_model->data(m_model->index(k, m_mapCategories), Qt::DisplayRole).toString();
446 // }
474
447
475 void QBarSeriesPrivate::setModelMappingRange(int first, int count)
448 // for (int i = m_mapBarBottom; i <= m_mapBarTop; i++) {
476 {
449 // QBarSet* barSet = new QBarSet(m_model->headerData(i, Qt::Horizontal, Qt::DisplayRole).toString());
477 m_mapFirst = first;
450 // for(int m = m_mapFirst; m < m_mapFirst + rowCount; m++)
478 m_mapCount = count;
451 // *barSet << m_model->data(m_model->index(m, i), Qt::DisplayRole).toDouble();
479 }
452 // q->appendBarSet(barSet);
480
453 // }
481 void QBarSeriesPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
454 // } else {
482 {
455 // int columnCount = 0;
483 for (int row = topLeft.row(); row <= bottomRight.row(); row++) {
456 // if(m_mapCount == -1)
484 for (int column = topLeft.column(); column <= bottomRight.column(); column++) {
457 // columnCount = m_model->columnCount() - m_mapFirst;
485 if (m_mapOrientation == Qt::Vertical)
458 // else
486 {
459 // columnCount = qMin(m_mapCount, m_model->columnCount() - m_mapFirst);
487 // model update is relevant to BarSeries if the change was made to the part of the model that was mapped to BarSeries
460 // for (int k = m_mapFirst; k < m_mapFirst + columnCount; k++) {
488 if ( row >= m_mapFirst && (m_mapCount == - 1 || row < m_mapFirst + m_mapCount)) {
461 // m_categories << m_model->data(m_model->index(m_mapCategories, k), Qt::DisplayRole).toString();
489 if (column >= m_mapBarBottom && column <= m_mapBarTop)
462 // }
490 barsetAt(column - m_mapBarBottom)->replace(row - m_mapFirst, m_model->data(topLeft, Qt::DisplayRole).toDouble());
463
491 // if (column == m_mapCategories);// TODO:
464 // for (int i = m_mapBarBottom; i <= m_mapBarTop; i++) {
492 }
465 // QBarSet* barSet = new QBarSet(m_model->headerData(i, Qt::Vertical, Qt::DisplayRole).toString());
493 }
466 // for(int m = m_mapFirst; m < m_mapFirst + columnCount; m++)
494 else
467 // *barSet << m_model->data(m_model->index(i, m), Qt::DisplayRole).toDouble();
495 {
468 // q->appendBarSet(barSet);
496 // model update is relevant to BarSeries if the change was made to the part of the model that was mapped to BarSeries
469 // }
497 if (column >= m_mapFirst && (m_mapCount == - 1 || column < m_mapFirst + m_mapCount)) {
470 // }
498 if (row >= m_mapBarBottom && row <= m_mapBarTop)
471 //}
499 barsetAt(row - m_mapBarBottom)->replace(column - m_mapFirst, m_model->data(topLeft, Qt::DisplayRole).toDouble());
472
500 // if (row == m_mapCategories);// TODO:
473 //void QBarSeriesPrivate::setModelMappingRange(int first, int count)
501 }
474 //{
502 }
475 // m_mapFirst = first;
503 }
476 // m_mapCount = count;
504 }
477 //}
505 }
478
479 //void QBarSeriesPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
480 //{
481 // for (int row = topLeft.row(); row <= bottomRight.row(); row++) {
482 // for (int column = topLeft.column(); column <= bottomRight.column(); column++) {
483 // if (m_mapOrientation == Qt::Vertical)
484 // {
485 // // model update is relevant to BarSeries if the change was made to the part of the model that was mapped to BarSeries
486 // if ( row >= m_mapFirst && (m_mapCount == - 1 || row < m_mapFirst + m_mapCount)) {
487 // if (column >= m_mapBarBottom && column <= m_mapBarTop)
488 // barsetAt(column - m_mapBarBottom)->replace(row - m_mapFirst, m_model->data(topLeft, Qt::DisplayRole).toDouble());
489 // // if (column == m_mapCategories);// TODO:
490 // }
491 // }
492 // else
493 // {
494 // // model update is relevant to BarSeries if the change was made to the part of the model that was mapped to BarSeries
495 // if (column >= m_mapFirst && (m_mapCount == - 1 || column < m_mapFirst + m_mapCount)) {
496 // if (row >= m_mapBarBottom && row <= m_mapBarTop)
497 // barsetAt(row - m_mapBarBottom)->replace(column - m_mapFirst, m_model->data(topLeft, Qt::DisplayRole).toDouble());
498 // // if (row == m_mapCategories);// TODO:
499 // }
500 // }
501 // }
502 // }
503 //}
506
504
507 void QBarSeriesPrivate::modelDataAdded(QModelIndex parent, int start, int end)
505 void QBarSeriesPrivate::modelDataAdded(QModelIndex parent, int start, int end)
508 {
506 {
509 Q_UNUSED(parent);
507 Q_UNUSED(parent);
510 Q_UNUSED(start);
508 Q_UNUSED(start);
511 Q_UNUSED(end);
509 Q_UNUSED(end);
512 initializeDataFromModel();
510 // initializeDataFromModel();
513 // // series uses model as a data sourceupda
511 // // series uses model as a data sourceupda
514 // int addedCount = end - start + 1;
512 // int addedCount = end - start + 1;
515 // if (m_mapCount != -1 && start >= m_mapFirst + m_mapCount) {
513 // if (m_mapCount != -1 && start >= m_mapFirst + m_mapCount) {
516 // return;
514 // return;
517 // } else {
515 // } else {
518
516
519 // for (int bar = m_mapBarBottom; bar <= m_mapBarTop; bar++) {
517 // for (int bar = m_mapBarBottom; bar <= m_mapBarTop; bar++) {
520 // QBarSet *barSet = barsetAt(bar - m_mapBarBottom);
518 // QBarSet *barSet = barsetAt(bar - m_mapBarBottom);
521 // // adding items to unlimited map
519 // // adding items to unlimited map
522 // if (m_mapCount == -1 && start >= m_mapFirst) {
520 // if (m_mapCount == -1 && start >= m_mapFirst) {
523 // for (int i = start; i <= end; i++) {
521 // for (int i = start; i <= end; i++) {
524 // if (bar == m_mapBarBottom)
522 // if (bar == m_mapBarBottom)
525 // insertCategory(i - m_mapFirst, m_model->data(m_model->index(i, m_mapCategories), Qt::DisplayRole).toString());
523 // insertCategory(i - m_mapFirst, m_model->data(m_model->index(i, m_mapCategories), Qt::DisplayRole).toString());
526 // barSet->insert(i - m_mapFirst, m_model->data(m_model->index(i, bar), Qt::DisplayRole).toDouble());
524 // barSet->insert(i - m_mapFirst, m_model->data(m_model->index(i, bar), Qt::DisplayRole).toDouble());
527 // }
525 // }
528 // } else if (m_mapCount == - 1 && start < m_mapFirst) {
526 // } else if (m_mapCount == - 1 && start < m_mapFirst) {
529 // // not all newly added items
527 // // not all newly added items
530 // for (int i = m_mapFirst; i < m_mapFirst + addedCount; i++) {
528 // for (int i = m_mapFirst; i < m_mapFirst + addedCount; i++) {
531 // if (bar == m_mapBarBottom)
529 // if (bar == m_mapBarBottom)
532 // insertCategory(i - m_mapFirst, m_model->data(m_model->index(i, m_mapCategories), Qt::DisplayRole).toString());
530 // insertCategory(i - m_mapFirst, m_model->data(m_model->index(i, m_mapCategories), Qt::DisplayRole).toString());
533 // barSet->insert(i - m_mapFirst, m_model->data(m_model->index(i, bar), Qt::DisplayRole).toDouble());
531 // barSet->insert(i - m_mapFirst, m_model->data(m_model->index(i, bar), Qt::DisplayRole).toDouble());
534 // }
532 // }
535 // }
533 // }
536
534
537 // // adding items to limited map
535 // // adding items to limited map
538 // else if (start >= m_mapFirst) {
536 // else if (start >= m_mapFirst) {
539 // // remove the items that will no longer fit into the map
537 // // remove the items that will no longer fit into the map
540 // // int toRemove = addedCount - (count - points().size());
538 // // int toRemove = addedCount - (count - points().size());
541 // for (int i = start; i <= end; i++) {
539 // for (int i = start; i <= end; i++) {
542 // if (bar == m_mapBarBottom)
540 // if (bar == m_mapBarBottom)
543 // insertCategory(i - m_mapFirst, m_model->data(m_model->index(i, m_mapCategories), Qt::DisplayRole).toString());
541 // insertCategory(i - m_mapFirst, m_model->data(m_model->index(i, m_mapCategories), Qt::DisplayRole).toString());
544 // barSet->insert(i - m_mapFirst, m_model->data(m_model->index(i, bar), Qt::DisplayRole).toDouble());
542 // barSet->insert(i - m_mapFirst, m_model->data(m_model->index(i, bar), Qt::DisplayRole).toDouble());
545 // }
543 // }
546 // if (m_barSets.size() > m_mapCount)
544 // if (m_barSets.size() > m_mapCount)
547 // for (int i = m_barSets.size() - 1; i >= m_mapCount; i--) {
545 // for (int i = m_barSets.size() - 1; i >= m_mapCount; i--) {
548 // if (bar == m_mapBarBottom)
546 // if (bar == m_mapBarBottom)
549 // removeCategory(i);
547 // removeCategory(i);
550 // barSet->remove(i);
548 // barSet->remove(i);
551 // }
549 // }
552 // } else {
550 // } else {
553 // //
551 // //
554 // for (int i = m_mapFirst; i < m_mapFirst + addedCount; i++) {
552 // for (int i = m_mapFirst; i < m_mapFirst + addedCount; i++) {
555 // if (bar == m_mapBarBottom)
553 // if (bar == m_mapBarBottom)
556 // insertCategory(i - m_mapFirst, m_model->data(m_model->index(i, m_mapCategories), Qt::DisplayRole).toString());
554 // insertCategory(i - m_mapFirst, m_model->data(m_model->index(i, m_mapCategories), Qt::DisplayRole).toString());
557 // barSet->insert(i - m_mapFirst, m_model->data(m_model->index(i, bar), Qt::DisplayRole).toDouble());
555 // barSet->insert(i - m_mapFirst, m_model->data(m_model->index(i, bar), Qt::DisplayRole).toDouble());
558 // }
556 // }
559 // if (m_barSets.size() > m_mapCount)
557 // if (m_barSets.size() > m_mapCount)
560 // for (int i = m_barSets.size() - 1; i >= m_mapCount; i--) {
558 // for (int i = m_barSets.size() - 1; i >= m_mapCount; i--) {
561 // if (bar == m_mapBarBottom)
559 // if (bar == m_mapBarBottom)
562 // removeCategory(i);
560 // removeCategory(i);
563 // barSet->remove(i);
561 // barSet->remove(i);
564 // }
562 // }
565 // }
563 // }
566 // }
564 // }
567 // emit restructuredBars();
565 // emit restructuredBars();
568 // emit barsetChanged();
566 // emit barsetChanged();
569 // emit categoriesUpdated();
567 // emit categoriesUpdated();
570 // }
568 // }
571 }
569 }
572
570
573 void QBarSeriesPrivate::modelDataRemoved(QModelIndex parent, int start, int end)
571 void QBarSeriesPrivate::modelDataRemoved(QModelIndex parent, int start, int end)
574 {
572 {
575 Q_UNUSED(parent);
573 Q_UNUSED(parent);
576 Q_UNUSED(start);
574 Q_UNUSED(start);
577 Q_UNUSED(end);
575 Q_UNUSED(end);
578 initializeDataFromModel();
576 // initializeDataFromModel();
579 }
577 }
580
578
581 void QBarSeriesPrivate::initializeDataFromModel()
579 //void QBarSeriesPrivate::initializeDataFromModel()
582 {
580 //{
583 Q_Q(QBarSeries);
581 // Q_Q(QBarSeries);
584
582
585 if (m_model == 0)
583 // if (m_model == 0)
586 return;
584 // return;
587
585
588 // connect the signals
586 // // connect the signals
589 // connect(m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex,QModelIndex)));
587 //// connect(m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex,QModelIndex)));
588 //// if (m_mapOrientation == Qt::Vertical) {
589 //// connect(m_model,SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(modelDataAdded(QModelIndex,int,int)));
590 //// connect(m_model,SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SLOT(modelDataRemoved(QModelIndex,int,int)));
591 //// } else {
592 //// connect(m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), this, SLOT(modelDataAdded(QModelIndex,int,int)));
593 //// connect(m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), this, SLOT(modelDataRemoved(QModelIndex,int,int)));
594 //// }
595
596 // // create the initial bars
597 // m_categories.clear();
598 // m_barSets.clear();
599 //// emit restructuredBars();
590 // if (m_mapOrientation == Qt::Vertical) {
600 // if (m_mapOrientation == Qt::Vertical) {
591 // connect(m_model,SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(modelDataAdded(QModelIndex,int,int)));
601 // int rowCount = 0;
592 // connect(m_model,SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SLOT(modelDataRemoved(QModelIndex,int,int)));
602 // if(m_mapCount == -1)
603 // rowCount = m_model->rowCount() - m_mapFirst;
604 // else
605 // rowCount = qMin(m_mapCount, m_model->rowCount() - m_mapFirst);
606 // for (int k = m_mapFirst; k < m_mapFirst + rowCount; k++) {
607 // m_categories << m_model->data(m_model->index(k, m_mapCategories), Qt::DisplayRole).toString();
608 // }
609
610 // for (int i = m_mapBarBottom; i <= m_mapBarTop; i++) {
611 // QBarSet* barSet = new QBarSet(m_model->headerData(i, Qt::Horizontal, Qt::DisplayRole).toString());
612 // for(int m = m_mapFirst; m < m_mapFirst + rowCount; m++)
613 // *barSet << m_model->data(m_model->index(m, i), Qt::DisplayRole).toDouble();
614 // q->appendBarSet(barSet);
615 // }
593 // } else {
616 // } else {
594 // connect(m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), this, SLOT(modelDataAdded(QModelIndex,int,int)));
617 // int columnCount = 0;
595 // connect(m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), this, SLOT(modelDataRemoved(QModelIndex,int,int)));
618 // if(m_mapCount == -1)
596 // }
619 // columnCount = m_model->columnCount() - m_mapFirst;
620 // else
621 // columnCount = qMin(m_mapCount, m_model->columnCount() - m_mapFirst);
622 // for (int k = m_mapFirst; k < m_mapFirst + columnCount; k++) {
623 // m_categories << m_model->data(m_model->index(m_mapCategories, k), Qt::DisplayRole).toString();
624 // }
597
625
598 // create the initial bars
626 // for (int i = m_mapBarBottom; i <= m_mapBarTop; i++) {
599 m_categories.clear();
627 // QBarSet* barSet = new QBarSet(m_model->headerData(i, Qt::Vertical, Qt::DisplayRole).toString());
600 m_barSets.clear();
628 // for(int m = m_mapFirst; m < m_mapFirst + columnCount; m++)
629 // *barSet << m_model->data(m_model->index(i, m), Qt::DisplayRole).toDouble();
630 // q->appendBarSet(barSet);
631 // }
632 // }
601 // emit restructuredBars();
633 // emit restructuredBars();
602 if (m_mapOrientation == Qt::Vertical) {
634 //// emit updatedBars();
603 int rowCount = 0;
635 //}
604 if(m_mapCount == -1)
605 rowCount = m_model->rowCount() - m_mapFirst;
606 else
607 rowCount = qMin(m_mapCount, m_model->rowCount() - m_mapFirst);
608 for (int k = m_mapFirst; k < m_mapFirst + rowCount; k++) {
609 m_categories << m_model->data(m_model->index(k, m_mapCategories), Qt::DisplayRole).toString();
610 }
611
612 for (int i = m_mapBarBottom; i <= m_mapBarTop; i++) {
613 QBarSet* barSet = new QBarSet(m_model->headerData(i, Qt::Horizontal, Qt::DisplayRole).toString());
614 for(int m = m_mapFirst; m < m_mapFirst + rowCount; m++)
615 *barSet << m_model->data(m_model->index(m, i), Qt::DisplayRole).toDouble();
616 q->appendBarSet(barSet);
617 }
618 } else {
619 int columnCount = 0;
620 if(m_mapCount == -1)
621 columnCount = m_model->columnCount() - m_mapFirst;
622 else
623 columnCount = qMin(m_mapCount, m_model->columnCount() - m_mapFirst);
624 for (int k = m_mapFirst; k < m_mapFirst + columnCount; k++) {
625 m_categories << m_model->data(m_model->index(m_mapCategories, k), Qt::DisplayRole).toString();
626 }
627
628 for (int i = m_mapBarBottom; i <= m_mapBarTop; i++) {
629 QBarSet* barSet = new QBarSet(m_model->headerData(i, Qt::Vertical, Qt::DisplayRole).toString());
630 for(int m = m_mapFirst; m < m_mapFirst + columnCount; m++)
631 *barSet << m_model->data(m_model->index(i, m), Qt::DisplayRole).toDouble();
632 q->appendBarSet(barSet);
633 }
634 }
635 emit restructuredBars();
636 // emit updatedBars();
637 }
638
636
639 void QBarSeriesPrivate::insertCategory(int index, const QString category)
637 void QBarSeriesPrivate::insertCategory(int index, const QString category)
640 {
638 {
641 m_categories.insert(index, category);
639 m_categories.insert(index, category);
642 emit categoriesUpdated();
640 emit categoriesUpdated();
643 }
641 }
644
642
645 void QBarSeriesPrivate::removeCategory(int index)
643 void QBarSeriesPrivate::removeCategory(int index)
646 {
644 {
647 m_categories.removeAt(index);
645 m_categories.removeAt(index);
648 emit categoriesUpdated();
646 emit categoriesUpdated();
649 }
647 }
650
648
651 void QBarSeriesPrivate::barsetChanged()
649 void QBarSeriesPrivate::barsetChanged()
652 {
650 {
653 emit updatedBars();
651 emit updatedBars();
654 }
652 }
655
653
656 void QBarSeriesPrivate::scaleDomain(Domain& domain)
654 void QBarSeriesPrivate::scaleDomain(Domain& domain)
657 {
655 {
658 qreal minX(domain.minX());
656 qreal minX(domain.minX());
659 qreal minY(domain.minY());
657 qreal minY(domain.minY());
660 qreal maxX(domain.maxX());
658 qreal maxX(domain.maxX());
661 qreal maxY(domain.maxY());
659 qreal maxY(domain.maxY());
662 int tickXCount(domain.tickXCount());
660 int tickXCount(domain.tickXCount());
663 int tickYCount(domain.tickYCount());
661 int tickYCount(domain.tickYCount());
664
662
665 qreal x = m_categories.count();
663 qreal x = m_categories.count();
666 qreal y = max();
664 qreal y = max();
667 minX = qMin(minX, x);
665 minX = qMin(minX, x);
668 minY = qMin(minY, y);
666 minY = qMin(minY, y);
669 maxX = qMax(maxX, x);
667 maxX = qMax(maxX, x);
670 maxY = qMax(maxY, y);
668 maxY = qMax(maxY, y);
671 tickXCount = x+1;
669 tickXCount = x+1;
672
670
673 domain.setRange(minX,maxX,minY,maxY,tickXCount,tickYCount);
671 domain.setRange(minX,maxX,minY,maxY,tickXCount,tickYCount);
674 }
672 }
675
673
676 Chart* QBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
674 Chart* QBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
677 {
675 {
678 Q_Q(QBarSeries);
676 Q_Q(QBarSeries);
679
677
680 BarChartItem* bar = new BarChartItem(q,presenter);
678 BarChartItem* bar = new BarChartItem(q,presenter);
681 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
679 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
682 presenter->animator()->addAnimation(bar);
680 presenter->animator()->addAnimation(bar);
683 }
681 }
684 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
682 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
685 return bar;
683 return bar;
686
684
687 }
685 }
688
686
689 QList<LegendMarker*> QBarSeriesPrivate::createLegendMarker(QLegend* legend)
687 QList<LegendMarker*> QBarSeriesPrivate::createLegendMarker(QLegend* legend)
690 {
688 {
691 Q_Q(QBarSeries);
689 Q_Q(QBarSeries);
692 QList<LegendMarker*> markers;
690 QList<LegendMarker*> markers;
693 foreach(QBarSet* set, q->barSets()) {
691 foreach(QBarSet* set, q->barSets()) {
694 BarLegendMarker* marker = new BarLegendMarker(q,set,legend);
692 BarLegendMarker* marker = new BarLegendMarker(q,set,legend);
695 markers << marker;
693 markers << marker;
696 }
694 }
697
695
698 return markers;
696 return markers;
699 }
697 }
700
698
701 #include "moc_qbarseries.cpp"
699 #include "moc_qbarseries.cpp"
702 #include "moc_qbarseries_p.cpp"
700 #include "moc_qbarseries_p.cpp"
703
701
704 QTCOMMERCIALCHART_END_NAMESPACE
702 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,82 +1,82
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #ifndef BARSERIES_H
21 #ifndef BARSERIES_H
22 #define BARSERIES_H
22 #define BARSERIES_H
23
23
24 #include <qabstractseries.h>
24 #include <qabstractseries.h>
25 #include <QStringList>
25 #include <QStringList>
26
26
27 class QModelIndex;
27 class QModelIndex;
28
28
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30
30
31 typedef QStringList QBarCategories;
31 typedef QStringList QBarCategories;
32
32
33 class QBarSet;
33 class QBarSet;
34 class BarCategory;
34 class BarCategory;
35 class QBarSeriesPrivate;
35 class QBarSeriesPrivate;
36
36
37 // Container for series
37 // Container for series
38 class QTCOMMERCIALCHART_EXPORT QBarSeries : public QAbstractSeries
38 class QTCOMMERCIALCHART_EXPORT QBarSeries : public QAbstractSeries
39 {
39 {
40 Q_OBJECT
40 Q_OBJECT
41 public:
41 public:
42 explicit QBarSeries(/*QBarCategories categories,*/ QObject *parent = 0);
42 explicit QBarSeries(/*QBarCategories categories,*/ QObject *parent = 0);
43 virtual ~QBarSeries();
43 virtual ~QBarSeries();
44
44
45 QAbstractSeries::SeriesType type() const;
45 QAbstractSeries::SeriesType type() const;
46 void setCategories(QBarCategories categories);
46 void setCategories(QBarCategories categories);
47
47
48 bool appendBarSet(QBarSet *set); // Takes ownership of set
48 bool appendBarSet(QBarSet *set); // Takes ownership of set
49 bool removeBarSet(QBarSet *set); // Releases ownership, doesn't delete set
49 bool removeBarSet(QBarSet *set); // Releases ownership, doesn't delete set
50 bool appendBarSets(QList<QBarSet* > sets);
50 bool appendBarSets(QList<QBarSet* > sets);
51 bool removeBarSets(QList<QBarSet* > sets);
51 bool removeBarSets(QList<QBarSet* > sets);
52 int barsetCount() const;
52 int barsetCount() const;
53 int categoryCount() const;
53 int categoryCount() const;
54 QList<QBarSet*> barSets() const;
54 QList<QBarSet*> barSets() const;
55 QBarCategories categories() const;
55 QBarCategories categories() const;
56
56
57 void setLabelsVisible(bool visible = true);
57 void setLabelsVisible(bool visible = true);
58 // TODO:
58 // TODO:
59 // void setGroupedDrawing(bool on = true); // By default this is on. Bars are grouped next to each other. If off, bars are drawn at their x-position (propably on top of each other)
59 // void setGroupedDrawing(bool on = true); // By default this is on. Bars are grouped next to each other. If off, bars are drawn at their x-position (propably on top of each other)
60 // void setBarMargin(int margin); // Margin that is left between bars (if drawn as grouped bars)
60 // void setBarMargin(int margin); // Margin that is left between bars (if drawn as grouped bars)
61
61
62 bool setModel(QAbstractItemModel *model);
62 void setModel(QAbstractItemModel *model);
63 void setModelMapping(int categories, int bottomBoundary, int topBoundary, Qt::Orientation orientation = Qt::Vertical);
63 // void setModelMapping(int categories, int bottomBoundary, int topBoundary, Qt::Orientation orientation = Qt::Vertical);
64 void setModelMappingRange(int first, int count = -1);
64 // void setModelMappingRange(int first, int count = -1);
65
65
66 protected:
66 protected:
67 explicit QBarSeries(QBarSeriesPrivate &d,QObject *parent = 0);
67 explicit QBarSeries(QBarSeriesPrivate &d,QObject *parent = 0);
68
68
69 Q_SIGNALS:
69 Q_SIGNALS:
70 void clicked(QBarSet *barset, QString category);
70 void clicked(QBarSet *barset, QString category);
71 void hovered(QBarSet* barset, bool status);
71 void hovered(QBarSet* barset, bool status);
72
72
73 protected:
73 protected:
74 Q_DECLARE_PRIVATE(QBarSeries)
74 Q_DECLARE_PRIVATE(QBarSeries)
75 friend class BarChartItem;
75 friend class BarChartItem;
76 friend class PercentBarChartItem;
76 friend class PercentBarChartItem;
77 friend class StackedBarChartItem;
77 friend class StackedBarChartItem;
78 };
78 };
79
79
80 QTCOMMERCIALCHART_END_NAMESPACE
80 QTCOMMERCIALCHART_END_NAMESPACE
81
81
82 #endif // BARSERIES_H
82 #endif // BARSERIES_H
@@ -1,69 +1,69
1 #ifndef QBARSERIES_P_H
1 #ifndef QBARSERIES_P_H
2 #define QBARSERIES_P_H
2 #define QBARSERIES_P_H
3
3
4 #include "qbarseries.h"
4 #include "qbarseries.h"
5 #include "qabstractseries_p.h"
5 #include "qabstractseries_p.h"
6 #include <QStringList>
6 #include <QStringList>
7 #include <QAbstractSeries>
7 #include <QAbstractSeries>
8
8
9 class QModelIndex;
9 class QModelIndex;
10
10
11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12
12
13 // Container for series
13 // Container for series
14 class QBarSeriesPrivate : public QAbstractSeriesPrivate
14 class QBarSeriesPrivate : public QAbstractSeriesPrivate
15 {
15 {
16 Q_OBJECT
16 Q_OBJECT
17 public:
17 public:
18 QBarSeriesPrivate(/*QBarCategories categories,*/ QBarSeries *parent);
18 QBarSeriesPrivate(/*QBarCategories categories,*/ QBarSeries *parent);
19 void setCategories(QBarCategories categories);
19 void setCategories(QBarCategories categories);
20
20
21 void scaleDomain(Domain& domain);
21 void scaleDomain(Domain& domain);
22 Chart* createGraphics(ChartPresenter* presenter);
22 Chart* createGraphics(ChartPresenter* presenter);
23 QList<LegendMarker*> createLegendMarker(QLegend* legend);
23 QList<LegendMarker*> createLegendMarker(QLegend* legend);
24
24
25 bool setModel(QAbstractItemModel *model);
25 // void setModel(QAbstractItemModel *model);
26 void setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation = Qt::Vertical);
26 // void setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation = Qt::Vertical);
27 void setModelMappingRange(int first, int count = -1);
27 // void setModelMappingRange(int first, int count = -1);
28
28
29 void insertCategory(int index, const QString category);
29 void insertCategory(int index, const QString category);
30 void removeCategory(int index);
30 void removeCategory(int index);
31
31
32 QBarSet* barsetAt(int index);
32 QBarSet* barsetAt(int index);
33 QString categoryName(int category);
33 QString categoryName(int category);
34 qreal min();
34 qreal min();
35 qreal max();
35 qreal max();
36 qreal valueAt(int set, int category);
36 qreal valueAt(int set, int category);
37 qreal percentageAt(int set, int category);
37 qreal percentageAt(int set, int category);
38 qreal categorySum(int category);
38 qreal categorySum(int category);
39 qreal absoluteCategorySum(int category);
39 qreal absoluteCategorySum(int category);
40 qreal maxCategorySum();
40 qreal maxCategorySum();
41
41
42 Q_SIGNALS:
42 Q_SIGNALS:
43 void clicked(QBarSet *barset, QString category);
43 void clicked(QBarSet *barset, QString category);
44 void updatedBars();
44 void updatedBars();
45 void restructuredBars();
45 void restructuredBars();
46 void categoriesUpdated();
46 void categoriesUpdated();
47
47
48 private Q_SLOTS:
48 private Q_SLOTS:
49 // slots for updating bars when data in model changes
49 // slots for updating bars when data in model changes
50 void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight);
50 // void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight);
51 void modelDataAdded(QModelIndex parent, int start, int end);
51 void modelDataAdded(QModelIndex parent, int start, int end);
52 void modelDataRemoved(QModelIndex parent, int start, int end);
52 void modelDataRemoved(QModelIndex parent, int start, int end);
53 void initializeDataFromModel();
53 // void initializeDataFromModel();
54 void barsetChanged();
54 void barsetChanged();
55
55
56 protected:
56 protected:
57 QList<QBarSet *> m_barSets;
57 QList<QBarSet *> m_barSets;
58 QBarCategories m_categories;
58 QBarCategories m_categories;
59
59
60 int m_mapCategories;
60 int m_mapCategories;
61 int m_mapBarBottom;
61 int m_mapBarBottom;
62 int m_mapBarTop;
62 int m_mapBarTop;
63 private:
63 private:
64 Q_DECLARE_PUBLIC(QBarSeries)
64 Q_DECLARE_PUBLIC(QBarSeries)
65 };
65 };
66
66
67 QTCOMMERCIALCHART_END_NAMESPACE
67 QTCOMMERCIALCHART_END_NAMESPACE
68
68
69 #endif // QBARSERIESPRIVATE_P_H
69 #endif // QBARSERIESPRIVATE_P_H
@@ -1,18 +1,20
1 INCLUDEPATH += $$PWD
1 INCLUDEPATH += $$PWD
2 DEPENDPATH += $$PWD
2 DEPENDPATH += $$PWD
3
3
4 SOURCES += \
4 SOURCES += \
5 $$PWD/qpieseries.cpp \
5 $$PWD/qpieseries.cpp \
6 $$PWD/piesliceitem.cpp \
6 $$PWD/piesliceitem.cpp \
7 $$PWD/piechartitem.cpp \
7 $$PWD/piechartitem.cpp \
8 $$PWD/qpieslice.cpp
8 $$PWD/qpieslice.cpp \
9 $$PWD/qpiemodelmapper.cpp
9
10
10 PRIVATE_HEADERS += \
11 PRIVATE_HEADERS += \
11 $$PWD/pieslicedata_p.h \
12 $$PWD/pieslicedata_p.h \
12 $$PWD/piechartitem_p.h \
13 $$PWD/piechartitem_p.h \
13 $$PWD/piesliceitem_p.h \
14 $$PWD/piesliceitem_p.h \
14 $$PWD/qpieseries_p.h
15 $$PWD/qpieseries_p.h
15
16
16 PUBLIC_HEADERS += \
17 PUBLIC_HEADERS += \
17 $$PWD/qpieseries.h \
18 $$PWD/qpieseries.h \
18 $$PWD/qpieslice.h
19 $$PWD/qpieslice.h \
20 $$PWD/qpiemodelmapper.h
@@ -1,809 +1,798
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "qpieseries.h"
21 #include "qpieseries.h"
22 #include "qpieseries_p.h"
22 #include "qpieseries_p.h"
23 #include "qpieslice.h"
23 #include "qpieslice.h"
24 #include "pieslicedata_p.h"
24 #include "pieslicedata_p.h"
25 #include "chartdataset_p.h"
25 #include "chartdataset_p.h"
26 #include "charttheme_p.h"
26 #include "charttheme_p.h"
27 #include "chartanimator_p.h"
27 #include "chartanimator_p.h"
28 #include "legendmarker_p.h"
28 #include "legendmarker_p.h"
29 #include <QAbstractItemModel>
29 #include <QAbstractItemModel>
30 #include "qpiemodelmapper.h"
30
31
31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32
33
33 /*!
34 /*!
34 \class QPieSeries
35 \class QPieSeries
35 \brief Pie series API for QtCommercial Charts
36 \brief Pie series API for QtCommercial Charts
36
37
37 The pie series defines a pie chart which consists of pie slices which are defined as QPieSlice objects.
38 The pie series defines a pie chart which consists of pie slices which are defined as QPieSlice objects.
38 The slices can have any values as the QPieSeries will calculate its relative value to the sum of all slices.
39 The slices can have any values as the QPieSeries will calculate its relative value to the sum of all slices.
39 The actual slice size is determined by that relative value.
40 The actual slice size is determined by that relative value.
40
41
41 Pie size and position on the chart is controlled by using relative values which range from 0.0 to 1.0
42 Pie size and position on the chart is controlled by using relative values which range from 0.0 to 1.0
42 These relate to the actual chart rectangle.
43 These relate to the actual chart rectangle.
43
44
44 By default the pie is defined as a full pie but it can also be a partial pie.
45 By default the pie is defined as a full pie but it can also be a partial pie.
45 This can be done by setting a starting angle and angle span to the series.
46 This can be done by setting a starting angle and angle span to the series.
46 Full pie is 360 degrees where 0 is at 12 a'clock.
47 Full pie is 360 degrees where 0 is at 12 a'clock.
47
48
48 See the \l {PieChart Example} {pie chart example} to learn how to create a simple pie chart.
49 See the \l {PieChart Example} {pie chart example} to learn how to create a simple pie chart.
49 \image examples_piechart.png
50 \image examples_piechart.png
50 */
51 */
51
52
52 /*!
53 /*!
53 \property QPieSeries::horizontalPosition
54 \property QPieSeries::horizontalPosition
54 \brief Defines the horizontal position of the pie.
55 \brief Defines the horizontal position of the pie.
55
56
56 The value is a relative value to the chart rectangle where:
57 The value is a relative value to the chart rectangle where:
57
58
58 \list
59 \list
59 \o 0.0 is the absolute left.
60 \o 0.0 is the absolute left.
60 \o 1.0 is the absolute right.
61 \o 1.0 is the absolute right.
61 \endlist
62 \endlist
62
63
63 Default value is 0.5 (center).
64 Default value is 0.5 (center).
64 */
65 */
65
66
66 /*!
67 /*!
67 \property QPieSeries::verticalPosition
68 \property QPieSeries::verticalPosition
68 \brief Defines the vertical position of the pie.
69 \brief Defines the vertical position of the pie.
69
70
70 The value is a relative value to the chart rectangle where:
71 The value is a relative value to the chart rectangle where:
71
72
72 \list
73 \list
73 \o 0.0 is the absolute top.
74 \o 0.0 is the absolute top.
74 \o 1.0 is the absolute bottom.
75 \o 1.0 is the absolute bottom.
75 \endlist
76 \endlist
76
77
77 Default value is 0.5 (center).
78 Default value is 0.5 (center).
78 */
79 */
79
80
80 /*!
81 /*!
81 \property QPieSeries::size
82 \property QPieSeries::size
82 \brief Defines the pie size.
83 \brief Defines the pie size.
83
84
84 The value is a relative value to the chart rectangle where:
85 The value is a relative value to the chart rectangle where:
85
86
86 \list
87 \list
87 \o 0.0 is the minimum size (pie not drawn).
88 \o 0.0 is the minimum size (pie not drawn).
88 \o 1.0 is the maximum size that can fit the chart.
89 \o 1.0 is the maximum size that can fit the chart.
89 \endlist
90 \endlist
90
91
91 Default value is 0.7.
92 Default value is 0.7.
92 */
93 */
93
94
94 /*!
95 /*!
95 \property QPieSeries::startAngle
96 \property QPieSeries::startAngle
96 \brief Defines the starting angle of the pie.
97 \brief Defines the starting angle of the pie.
97
98
98 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
99 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
99
100
100 Default is value is 0.
101 Default is value is 0.
101 */
102 */
102
103
103 /*!
104 /*!
104 \property QPieSeries::endAngle
105 \property QPieSeries::endAngle
105 \brief Defines the ending angle of the pie.
106 \brief Defines the ending angle of the pie.
106
107
107 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
108 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
108
109
109 Default is value is 360.
110 Default is value is 360.
110 */
111 */
111
112
112
113
113 /*!
114 /*!
114 Constructs a series object which is a child of \a parent.
115 Constructs a series object which is a child of \a parent.
115 */
116 */
116 QPieSeries::QPieSeries(QObject *parent) :
117 QPieSeries::QPieSeries(QObject *parent) :
117 QAbstractSeries(*new QPieSeriesPrivate(this),parent)
118 QAbstractSeries(*new QPieSeriesPrivate(this),parent)
118 {
119 {
119
120
120 }
121 }
121
122
122 /*!
123 /*!
123 Destroys the series and its slices.
124 Destroys the series and its slices.
124 */
125 */
125 QPieSeries::~QPieSeries()
126 QPieSeries::~QPieSeries()
126 {
127 {
127 // NOTE: d_prt destroyed by QObject
128 // NOTE: d_prt destroyed by QObject
128 }
129 }
129
130
130 /*!
131 /*!
131 Returns QChartSeries::SeriesTypePie.
132 Returns QChartSeries::SeriesTypePie.
132 */
133 */
133 QAbstractSeries::SeriesType QPieSeries::type() const
134 QAbstractSeries::SeriesType QPieSeries::type() const
134 {
135 {
135 return QAbstractSeries::SeriesTypePie;
136 return QAbstractSeries::SeriesTypePie;
136 }
137 }
137
138
138 /*!
139 /*!
139 Appends an array of \a slices to the series.
140 Appends an array of \a slices to the series.
140 Slice ownership is passed to the series.
141 Slice ownership is passed to the series.
141 */
142 */
142 bool QPieSeries::append(QList<QPieSlice*> slices)
143 bool QPieSeries::append(QList<QPieSlice*> slices)
143 {
144 {
144 Q_D(QPieSeries);
145 Q_D(QPieSeries);
145
146
146 if (slices.count() == 0)
147 if (slices.count() == 0)
147 return false;
148 return false;
148
149
149 foreach (QPieSlice* s, slices) {
150 foreach (QPieSlice* s, slices) {
150 if (!s || d->m_slices.contains(s))
151 if (!s || d->m_slices.contains(s))
151 return false;
152 return false;
152 }
153 }
153
154
154 foreach (QPieSlice* s, slices) {
155 foreach (QPieSlice* s, slices) {
155 s->setParent(this);
156 s->setParent(this);
156 d->m_slices << s;
157 d->m_slices << s;
157 }
158 }
158
159
159 d->updateDerivativeData();
160 d->updateDerivativeData();
160
161
161 foreach (QPieSlice* s, slices) {
162 foreach (QPieSlice* s, slices) {
162 connect(s, SIGNAL(changed()), d, SLOT(sliceChanged()));
163 connect(s, SIGNAL(changed()), d, SLOT(sliceChanged()));
163 connect(s, SIGNAL(clicked()), d, SLOT(sliceClicked()));
164 connect(s, SIGNAL(clicked()), d, SLOT(sliceClicked()));
164 connect(s, SIGNAL(hovered(bool)), d, SLOT(sliceHovered(bool)));
165 connect(s, SIGNAL(hovered(bool)), d, SLOT(sliceHovered(bool)));
165 }
166 }
166
167
167 emit d->added(slices);
168 emit d->added(slices);
168
169
169 return true;
170 return true;
170 }
171 }
171
172
172 /*!
173 /*!
173 Appends a single \a slice to the series.
174 Appends a single \a slice to the series.
174 Slice ownership is passed to the series.
175 Slice ownership is passed to the series.
175 */
176 */
176 bool QPieSeries::append(QPieSlice* slice)
177 bool QPieSeries::append(QPieSlice* slice)
177 {
178 {
178 return append(QList<QPieSlice*>() << slice);
179 return append(QList<QPieSlice*>() << slice);
179 }
180 }
180
181
181 /*!
182 /*!
182 Appends a single \a slice to the series and returns a reference to the series.
183 Appends a single \a slice to the series and returns a reference to the series.
183 Slice ownership is passed to the series.
184 Slice ownership is passed to the series.
184 */
185 */
185 QPieSeries& QPieSeries::operator << (QPieSlice* slice)
186 QPieSeries& QPieSeries::operator << (QPieSlice* slice)
186 {
187 {
187 append(slice);
188 append(slice);
188 return *this;
189 return *this;
189 }
190 }
190
191
191
192
192 /*!
193 /*!
193 Appends a single slice to the series with give \a value and \a name.
194 Appends a single slice to the series with give \a value and \a name.
194 Slice ownership is passed to the series.
195 Slice ownership is passed to the series.
195 */
196 */
196 QPieSlice* QPieSeries::append(qreal value, QString name)
197 QPieSlice* QPieSeries::append(qreal value, QString name)
197 {
198 {
198 QPieSlice* slice = new QPieSlice(value, name);
199 QPieSlice* slice = new QPieSlice(value, name);
199 append(slice);
200 append(slice);
200 return slice;
201 return slice;
201 }
202 }
202
203
203 /*!
204 /*!
204 Inserts a single \a slice to the series before the slice at \a index position.
205 Inserts a single \a slice to the series before the slice at \a index position.
205 Slice ownership is passed to the series.
206 Slice ownership is passed to the series.
206 */
207 */
207 bool QPieSeries::insert(int index, QPieSlice* slice)
208 bool QPieSeries::insert(int index, QPieSlice* slice)
208 {
209 {
209 Q_D(QPieSeries);
210 Q_D(QPieSeries);
210
211
211 if (index < 0 || index > d->m_slices.count())
212 if (index < 0 || index > d->m_slices.count())
212 return false;
213 return false;
213
214
214 if (!slice || d->m_slices.contains(slice))
215 if (!slice || d->m_slices.contains(slice))
215 return false;
216 return false;
216
217
217 slice->setParent(this);
218 slice->setParent(this);
218 d->m_slices.insert(index, slice);
219 d->m_slices.insert(index, slice);
219
220
220 d->updateDerivativeData();
221 d->updateDerivativeData();
221
222
222 connect(slice, SIGNAL(changed()), d, SLOT(sliceChanged()));
223 connect(slice, SIGNAL(changed()), d, SLOT(sliceChanged()));
223 connect(slice, SIGNAL(clicked()), d, SLOT(sliceClicked()));
224 connect(slice, SIGNAL(clicked()), d, SLOT(sliceClicked()));
224 connect(slice, SIGNAL(hovered(bool)), d, SLOT(sliceHovered(bool)));
225 connect(slice, SIGNAL(hovered(bool)), d, SLOT(sliceHovered(bool)));
225
226
226 emit d->added(QList<QPieSlice*>() << slice);
227 emit d->added(QList<QPieSlice*>() << slice);
227
228
228 return true;
229 return true;
229 }
230 }
230
231
231 /*!
232 /*!
232 Removes a single \a slice from the series and deletes the slice.
233 Removes a single \a slice from the series and deletes the slice.
233
234
234 Do not reference the pointer after this call.
235 Do not reference the pointer after this call.
235 */
236 */
236 bool QPieSeries::remove(QPieSlice* slice)
237 bool QPieSeries::remove(QPieSlice* slice)
237 {
238 {
238 Q_D(QPieSeries);
239 Q_D(QPieSeries);
239
240
240 if (!d->m_slices.removeOne(slice))
241 if (!d->m_slices.removeOne(slice))
241 return false;
242 return false;
242
243
243 d->updateDerivativeData();
244 d->updateDerivativeData();
244
245
245 emit d->removed(QList<QPieSlice*>() << slice);
246 emit d->removed(QList<QPieSlice*>() << slice);
246
247
247 delete slice;
248 delete slice;
248 slice = 0;
249 slice = 0;
249
250
250 return true;
251 return true;
251 }
252 }
252
253
253 /*!
254 /*!
254 Clears all slices from the series.
255 Clears all slices from the series.
255 */
256 */
256 void QPieSeries::clear()
257 void QPieSeries::clear()
257 {
258 {
258 Q_D(QPieSeries);
259 Q_D(QPieSeries);
259 if (d->m_slices.count() == 0)
260 if (d->m_slices.count() == 0)
260 return;
261 return;
261
262
262 QList<QPieSlice*> slices = d->m_slices;
263 QList<QPieSlice*> slices = d->m_slices;
263 foreach (QPieSlice* s, d->m_slices) {
264 foreach (QPieSlice* s, d->m_slices) {
264 d->m_slices.removeOne(s);
265 d->m_slices.removeOne(s);
265 delete s;
266 delete s;
266 }
267 }
267
268
268 d->updateDerivativeData();
269 d->updateDerivativeData();
269
270
270 emit d->removed(slices);
271 emit d->removed(slices);
271 }
272 }
272
273
273 /*!
274 /*!
274 returns the number of the slices in this series.
275 returns the number of the slices in this series.
275 */
276 */
276 int QPieSeries::count() const
277 int QPieSeries::count() const
277 {
278 {
278 Q_D(const QPieSeries);
279 Q_D(const QPieSeries);
279 return d->m_slices.count();
280 return d->m_slices.count();
280 }
281 }
281
282
282 /*!
283 /*!
283 Returns true is the series is empty.
284 Returns true is the series is empty.
284 */
285 */
285 bool QPieSeries::isEmpty() const
286 bool QPieSeries::isEmpty() const
286 {
287 {
287 Q_D(const QPieSeries);
288 Q_D(const QPieSeries);
288 return d->m_slices.isEmpty();
289 return d->m_slices.isEmpty();
289 }
290 }
290
291
291 /*!
292 /*!
292 Returns a list of slices that belong to this series.
293 Returns a list of slices that belong to this series.
293 */
294 */
294 QList<QPieSlice*> QPieSeries::slices() const
295 QList<QPieSlice*> QPieSeries::slices() const
295 {
296 {
296 Q_D(const QPieSeries);
297 Q_D(const QPieSeries);
297 return d->m_slices;
298 return d->m_slices;
298 }
299 }
299
300
300 void QPieSeries::setHorizontalPosition(qreal relativePosition)
301 void QPieSeries::setHorizontalPosition(qreal relativePosition)
301 {
302 {
302 Q_D(QPieSeries);
303 Q_D(QPieSeries);
303 if (d->setRealValue(d->m_pieRelativeHorPos, relativePosition, 1.0))
304 if (d->setRealValue(d->m_pieRelativeHorPos, relativePosition, 1.0))
304 emit d->piePositionChanged();
305 emit d->piePositionChanged();
305 }
306 }
306
307
307 void QPieSeries::setVerticalPosition(qreal relativePosition)
308 void QPieSeries::setVerticalPosition(qreal relativePosition)
308 {
309 {
309 Q_D(QPieSeries);
310 Q_D(QPieSeries);
310 if (d->setRealValue(d->m_pieRelativeVerPos, relativePosition, 1.0))
311 if (d->setRealValue(d->m_pieRelativeVerPos, relativePosition, 1.0))
311 emit d->piePositionChanged();
312 emit d->piePositionChanged();
312 }
313 }
313
314
314 qreal QPieSeries::horizontalPosition() const
315 qreal QPieSeries::horizontalPosition() const
315 {
316 {
316 Q_D(const QPieSeries);
317 Q_D(const QPieSeries);
317 return d->m_pieRelativeHorPos;
318 return d->m_pieRelativeHorPos;
318 }
319 }
319
320
320 qreal QPieSeries::verticalPosition() const
321 qreal QPieSeries::verticalPosition() const
321 {
322 {
322 Q_D(const QPieSeries);
323 Q_D(const QPieSeries);
323 return d->m_pieRelativeVerPos;
324 return d->m_pieRelativeVerPos;
324 }
325 }
325
326
326 void QPieSeries::setPieSize(qreal relativeSize)
327 void QPieSeries::setPieSize(qreal relativeSize)
327 {
328 {
328 Q_D(QPieSeries);
329 Q_D(QPieSeries);
329 if (d->setRealValue(d->m_pieRelativeSize, relativeSize, 1.0))
330 if (d->setRealValue(d->m_pieRelativeSize, relativeSize, 1.0))
330 emit d->pieSizeChanged();
331 emit d->pieSizeChanged();
331 }
332 }
332
333
333 qreal QPieSeries::pieSize() const
334 qreal QPieSeries::pieSize() const
334 {
335 {
335 Q_D(const QPieSeries);
336 Q_D(const QPieSeries);
336 return d->m_pieRelativeSize;
337 return d->m_pieRelativeSize;
337 }
338 }
338
339
339
340
340 void QPieSeries::setPieStartAngle(qreal angle)
341 void QPieSeries::setPieStartAngle(qreal angle)
341 {
342 {
342 Q_D(QPieSeries);
343 Q_D(QPieSeries);
343 if (d->setRealValue(d->m_pieStartAngle, angle, d->m_pieEndAngle))
344 if (d->setRealValue(d->m_pieStartAngle, angle, d->m_pieEndAngle))
344 d->updateDerivativeData();
345 d->updateDerivativeData();
345 }
346 }
346
347
347 qreal QPieSeries::pieStartAngle() const
348 qreal QPieSeries::pieStartAngle() const
348 {
349 {
349 Q_D(const QPieSeries);
350 Q_D(const QPieSeries);
350 return d->m_pieStartAngle;
351 return d->m_pieStartAngle;
351 }
352 }
352
353
353 /*!
354 /*!
354 Sets the end angle of the pie.
355 Sets the end angle of the pie.
355
356
356 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
357 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
357
358
358 \a angle must be greater than start angle.
359 \a angle must be greater than start angle.
359
360
360 \sa pieEndAngle(), pieStartAngle(), setPieStartAngle()
361 \sa pieEndAngle(), pieStartAngle(), setPieStartAngle()
361 */
362 */
362 void QPieSeries::setPieEndAngle(qreal angle)
363 void QPieSeries::setPieEndAngle(qreal angle)
363 {
364 {
364 Q_D(QPieSeries);
365 Q_D(QPieSeries);
365
366
366 if (d->setRealValue(d->m_pieEndAngle, angle, 360.0, d->m_pieStartAngle))
367 if (d->setRealValue(d->m_pieEndAngle, angle, 360.0, d->m_pieStartAngle))
367 d->updateDerivativeData();
368 d->updateDerivativeData();
368 }
369 }
369
370
370 /*!
371 /*!
371 Returns the end angle of the pie.
372 Returns the end angle of the pie.
372
373
373 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
374 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
374
375
375 \sa setPieEndAngle(), pieStartAngle(), setPieStartAngle()
376 \sa setPieEndAngle(), pieStartAngle(), setPieStartAngle()
376 */
377 */
377 qreal QPieSeries::pieEndAngle() const
378 qreal QPieSeries::pieEndAngle() const
378 {
379 {
379 Q_D(const QPieSeries);
380 Q_D(const QPieSeries);
380 return d->m_pieEndAngle;
381 return d->m_pieEndAngle;
381 }
382 }
382
383
383 /*!
384 /*!
384 Sets the all the slice labels \a visible or invisible.
385 Sets the all the slice labels \a visible or invisible.
385
386
386 \sa QPieSlice::isLabelVisible(), QPieSlice::setLabelVisible()
387 \sa QPieSlice::isLabelVisible(), QPieSlice::setLabelVisible()
387 */
388 */
388 void QPieSeries::setLabelsVisible(bool visible)
389 void QPieSeries::setLabelsVisible(bool visible)
389 {
390 {
390 Q_D(QPieSeries);
391 Q_D(QPieSeries);
391 foreach (QPieSlice* s, d->m_slices)
392 foreach (QPieSlice* s, d->m_slices)
392 s->setLabelVisible(visible);
393 s->setLabelVisible(visible);
393 }
394 }
394
395
395 /*!
396 /*!
396 Returns the sum of all slice values in this series.
397 Returns the sum of all slice values in this series.
397
398
398 \sa QPieSlice::value(), QPieSlice::setValue(), QPieSlice::percentage()
399 \sa QPieSlice::value(), QPieSlice::setValue(), QPieSlice::percentage()
399 */
400 */
400 qreal QPieSeries::sum() const
401 qreal QPieSeries::sum() const
401 {
402 {
402 Q_D(const QPieSeries);
403 Q_D(const QPieSeries);
403 return d->m_sum;
404 return d->m_sum;
404 }
405 }
405
406
406 /*!
407 /*!
407 \fn void QPieSeries::clicked(QPieSlice* slice)
408 \fn void QPieSeries::clicked(QPieSlice* slice)
408
409
409 This signal is emitted when a \a slice has been clicked.
410 This signal is emitted when a \a slice has been clicked.
410
411
411 \sa QPieSlice::clicked()
412 \sa QPieSlice::clicked()
412 */
413 */
413
414
414 /*!
415 /*!
415 \fn void QPieSeries::hovered(QPieSlice* slice, bool state)
416 \fn void QPieSeries::hovered(QPieSlice* slice, bool state)
416
417
417 This signal is emitted when user has hovered over or away from the \a slice.
418 This signal is emitted when user has hovered over or away from the \a slice.
418
419
419 \a state is true when user has hovered over the slice and false when hover has moved away from the slice.
420 \a state is true when user has hovered over the slice and false when hover has moved away from the slice.
420
421
421 \sa QPieSlice::hovered()
422 \sa QPieSlice::hovered()
422 */
423 */
423
424
424 /*!
425 /*!
425 \fn bool QPieSeries::setModel(QAbstractItemModel *model)
426 \fn bool QPieSeries::setModel(QAbstractItemModel *model)
426 Sets the \a model to be used as a data source
427 Sets the \a model to be used as a data source
427 */
428 */
428 bool QPieSeries::setModel(QAbstractItemModel* model)
429 void QPieSeries::setModel(QAbstractItemModel* model)
429 {
430 {
430 Q_D(QPieSeries);
431 Q_D(QPieSeries);
431 // disconnect signals from old model
432 // disconnect signals from old model
432 if(d->m_model)
433 if(d->m_model)
433 {
434 {
434 disconnect(d->m_model, 0, this, 0);
435 disconnect(d->m_model, 0, this, 0);
435 d->m_mapValues = -1;
436 d->m_mapLabels = -1;
437 d->m_mapOrientation = Qt::Vertical;
438 }
436 }
439
437
440 // set new model
438 // set new model
441 if(model)
439 if(model)
442 {
440 {
443 d->m_model = model;
441 d->m_model = model;
444 connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex)));
442 if (d->m_mapper)
445 connect(d->m_model,SIGNAL(rowsInserted(QModelIndex,int,int)), d, SLOT(modelRowsAdded(QModelIndex,int,int)));
443 d->setMapping();
446 connect(d->m_model,SIGNAL(rowsRemoved(QModelIndex,int,int)), d, SLOT(modelRowsRemoved(QModelIndex,int,int)));
447 connect(d->m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), d, SLOT(modelColumnsAdded(QModelIndex,int,int)));
448 connect(d->m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), d, SLOT(modelColumnsRemoved(QModelIndex,int,int)));
449 return true;
450 }
444 }
451 else
445 else
452 {
446 {
453 d->m_model = 0;
447 d->m_model = 0;
454 return false;
455 }
448 }
456 }
449 }
457
450
458 /*!
451 void QPieSeries::setModelMapper(QPieModelMapper *mapper)
459 \fn bool QPieSeries::setModelMapping(int modelValuesLine, int modelLabelsLine, Qt::Orientation orientation)
460 Sets column/row specified by \a modelValuesLine to be used as a list of pie slice values for the pie.
461 Parameter \a modelValuesLine indicates the column/row where the values for the pie slices are located in the model.
462 Parameter \a modelLabelsLine indicates the column/row where the labels for the pie slices are located in the model.
463 The \a orientation parameter specifies whether the data is in columns or in rows.
464 */
465 void QPieSeries::setModelMapping(int modelValuesLine, int modelLabelsLine, Qt::Orientation orientation)
466 {
452 {
467 Q_D(QPieSeries);
453 Q_D(QPieSeries);
454 // disconnect signals from old mapper
455 if (d->m_mapper) {
456 QObject::disconnect(d->m_mapper, 0, this, 0);
457 }
468
458
469 if (d->m_model == 0)
459 if (mapper) {
470 return;
460 d->m_mapper = mapper;
471
461 if (d->m_model)
472 d->m_mapValues = modelValuesLine;
462 d->setMapping();
473 d->m_mapLabels = modelLabelsLine;
463 } else {
474 d->m_mapOrientation = orientation;
464 d->m_mapper = 0;
475
465 }
476 d->initializePieFromModel();
477 // // connect the signals
478 // connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex)));
479
480
481 // // create the initial slices set
482 // if (d->m_mapOrientation == Qt::Vertical) {
483 // for (int i = 0; i < d->m_model->rowCount(); i++)
484 // append(d->m_model->data(d->m_model->index(i, d->m_mapValues), Qt::DisplayRole).toDouble(), d->m_model->data(d->m_model->index(i, d->m_mapLabels), Qt::DisplayRole).toString());
485 // } else {
486 // for (int i = 0; i < d->m_model->columnCount(); i++)
487 // append(d->m_model->data(d->m_model->index(d->m_mapValues, i), Qt::DisplayRole).toDouble(), d->m_model->data(d->m_model->index(d->m_mapLabels, i), Qt::DisplayRole).toString());
488 // }
489 }
466 }
490
467
491 void QPieSeries::setModelMappingRange(int first, int count)
468 QPieModelMapper* QPieSeries::modelMapper() const
492 {
469 {
493 Q_D(QPieSeries);
470 Q_D(const QPieSeries);
494 d->m_mapFirst = first;
471 return d->m_mapper;
495 d->m_mapCount = count;
496 d->initializePieFromModel();
497 }
472 }
498
473
499 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
474 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
500
475
501
476
502 QPieSeriesPrivate::QPieSeriesPrivate(QPieSeries *parent) :
477 QPieSeriesPrivate::QPieSeriesPrivate(QPieSeries *parent) :
503 QAbstractSeriesPrivate(parent),
478 QAbstractSeriesPrivate(parent),
504 m_pieRelativeHorPos(0.5),
479 m_pieRelativeHorPos(0.5),
505 m_pieRelativeVerPos(0.5),
480 m_pieRelativeVerPos(0.5),
506 m_pieRelativeSize(0.7),
481 m_pieRelativeSize(0.7),
507 m_pieStartAngle(0),
482 m_pieStartAngle(0),
508 m_pieEndAngle(360),
483 m_pieEndAngle(360),
509 m_sum(0),
484 m_sum(0),
510 m_mapValues(0),
485 m_mapper(0)
511 m_mapLabels(0)
512 {
486 {
513
487
514 }
488 }
515
489
516 QPieSeriesPrivate::~QPieSeriesPrivate()
490 QPieSeriesPrivate::~QPieSeriesPrivate()
517 {
491 {
518
492
519 }
493 }
520
494
521 void QPieSeriesPrivate::updateDerivativeData()
495 void QPieSeriesPrivate::updateDerivativeData()
522 {
496 {
523 m_sum = 0;
497 m_sum = 0;
524
498
525 // nothing to do?
499 // nothing to do?
526 if (m_slices.count() == 0)
500 if (m_slices.count() == 0)
527 return;
501 return;
528
502
529 // calculate sum of all slices
503 // calculate sum of all slices
530 foreach (QPieSlice* s, m_slices)
504 foreach (QPieSlice* s, m_slices)
531 m_sum += s->value();
505 m_sum += s->value();
532
506
533 // nothing to show..
507 // nothing to show..
534 if (qFuzzyIsNull(m_sum))
508 if (qFuzzyIsNull(m_sum))
535 return;
509 return;
536
510
537 // update slice attributes
511 // update slice attributes
538 qreal sliceAngle = m_pieStartAngle;
512 qreal sliceAngle = m_pieStartAngle;
539 qreal pieSpan = m_pieEndAngle - m_pieStartAngle;
513 qreal pieSpan = m_pieEndAngle - m_pieStartAngle;
540 QVector<QPieSlice*> changed;
514 QVector<QPieSlice*> changed;
541 foreach (QPieSlice* s, m_slices) {
515 foreach (QPieSlice* s, m_slices) {
542
516
543 PieSliceData data = PieSliceData::data(s);
517 PieSliceData data = PieSliceData::data(s);
544 data.m_percentage = s->value() / m_sum;
518 data.m_percentage = s->value() / m_sum;
545 data.m_angleSpan = pieSpan * data.m_percentage;
519 data.m_angleSpan = pieSpan * data.m_percentage;
546 data.m_startAngle = sliceAngle;
520 data.m_startAngle = sliceAngle;
547 sliceAngle += data.m_angleSpan;
521 sliceAngle += data.m_angleSpan;
548
522
549 if (PieSliceData::data(s) != data) {
523 if (PieSliceData::data(s) != data) {
550 PieSliceData::data(s) = data;
524 PieSliceData::data(s) = data;
551 changed << s;
525 changed << s;
552 }
526 }
553 }
527 }
554
528
555 // emit signals
529 // emit signals
556 foreach (QPieSlice* s, changed)
530 foreach (QPieSlice* s, changed)
557 PieSliceData::data(s).emitChangedSignal(s);
531 PieSliceData::data(s).emitChangedSignal(s);
558 }
532 }
559
533
560 QPieSeriesPrivate* QPieSeriesPrivate::seriesData(QPieSeries &series)
534 QPieSeriesPrivate* QPieSeriesPrivate::seriesData(QPieSeries &series)
561 {
535 {
562 return series.d_func();
536 return series.d_func();
563 }
537 }
564
538
565 void QPieSeriesPrivate::sliceChanged()
539 void QPieSeriesPrivate::sliceChanged()
566 {
540 {
567 Q_ASSERT(m_slices.contains(qobject_cast<QPieSlice *>(sender())));
541 Q_ASSERT(m_slices.contains(qobject_cast<QPieSlice *>(sender())));
568 updateDerivativeData();
542 updateDerivativeData();
569 }
543 }
570
544
571 void QPieSeriesPrivate::sliceClicked()
545 void QPieSeriesPrivate::sliceClicked()
572 {
546 {
573 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
547 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
574 Q_ASSERT(m_slices.contains(slice));
548 Q_ASSERT(m_slices.contains(slice));
575 Q_Q(QPieSeries);
549 Q_Q(QPieSeries);
576 emit q->clicked(slice);
550 emit q->clicked(slice);
577 }
551 }
578
552
579 void QPieSeriesPrivate::sliceHovered(bool state)
553 void QPieSeriesPrivate::sliceHovered(bool state)
580 {
554 {
581 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
555 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
582 Q_ASSERT(m_slices.contains(slice));
556 Q_ASSERT(m_slices.contains(slice));
583 Q_Q(QPieSeries);
557 Q_Q(QPieSeries);
584 emit q->hovered(slice, state);
558 emit q->hovered(slice, state);
585 }
559 }
586
560
587 void QPieSeriesPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
561 void QPieSeriesPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
588 {
562 {
589 for (int row = topLeft.row(); row <= bottomRight.row(); row++) {
563 for (int row = topLeft.row(); row <= bottomRight.row(); row++) {
590 for (int column = topLeft.column(); column <= bottomRight.column(); column++) {
564 for (int column = topLeft.column(); column <= bottomRight.column(); column++) {
591 if (m_mapOrientation == Qt::Vertical)
565 if (m_mapper->orientation() == Qt::Vertical)
592 {
566 {
593 if ( topLeft.row() >= m_mapFirst && (m_mapCount == - 1 || topLeft.row() < m_mapFirst + m_mapCount)) {
567 if ( topLeft.row() >= m_mapper->first() && (m_mapper->count() == - 1 || topLeft.row() < m_mapper->first() + m_mapper->count())) {
594 if (topLeft.column() == m_mapValues)
568 if (topLeft.column() == m_mapper->mapValues())
595 m_slices.at(topLeft.row() - m_mapFirst)->setValue(m_model->data(topLeft, Qt::DisplayRole).toDouble());
569 m_slices.at(topLeft.row() - m_mapper->first())->setValue(m_model->data(topLeft, Qt::DisplayRole).toDouble());
596 if (topLeft.column() == m_mapLabels)
570 if (topLeft.column() == m_mapper->mapLabels())
597 m_slices.at(topLeft.row() - m_mapFirst)->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString());
571 m_slices.at(topLeft.row() - m_mapper->first())->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString());
598 }
572 }
599 }
573 }
600 else
574 else
601 {
575 {
602 if (topLeft.column() >= m_mapFirst && (m_mapCount == - 1 || topLeft.column() < m_mapFirst + m_mapCount)) {
576 if (topLeft.column() >= m_mapper->first() && (m_mapper->count() == - 1 || topLeft.column() < m_mapper->first() + m_mapper->count())) {
603 if (topLeft.row() == m_mapValues)
577 if (topLeft.row() == m_mapper->mapValues())
604 m_slices.at(topLeft.column() - m_mapFirst)->setValue(m_model->data(topLeft, Qt::DisplayRole).toDouble());
578 m_slices.at(topLeft.column() - m_mapper->first())->setValue(m_model->data(topLeft, Qt::DisplayRole).toDouble());
605 if (topLeft.row() == m_mapLabels)
579 if (topLeft.row() == m_mapper->mapLabels())
606 m_slices.at(topLeft.column() - m_mapFirst)->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString());
580 m_slices.at(topLeft.column() - m_mapper->first())->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString());
607 }
581 }
608 }
582 }
609 }
583 }
610 }
584 }
611 }
585 }
612
586
613
587
614 void QPieSeriesPrivate::modelRowsAdded(QModelIndex parent, int start, int end)
588 void QPieSeriesPrivate::modelRowsAdded(QModelIndex parent, int start, int end)
615 {
589 {
616 Q_UNUSED(parent);
590 Q_UNUSED(parent);
617 if (m_mapOrientation == Qt::Vertical)
591 if (m_mapper->orientation() == Qt::Vertical)
618 insertData(start, end);
592 insertData(start, end);
619 else if (start <= m_mapValues || start <= m_mapLabels) // if the changes affect the map - reinitialize the pie
593 else if (start <= m_mapper->mapValues() || start <= m_mapper->mapLabels()) // if the changes affect the map - reinitialize the pie
620 initializePieFromModel();
594 initializePieFromModel();
621 }
595 }
622
596
623 void QPieSeriesPrivate::modelRowsRemoved(QModelIndex parent, int start, int end)
597 void QPieSeriesPrivate::modelRowsRemoved(QModelIndex parent, int start, int end)
624 {
598 {
625 Q_UNUSED(parent);
599 Q_UNUSED(parent);
626 if (m_mapOrientation == Qt::Vertical)
600 if (m_mapper->orientation() == Qt::Vertical)
627 removeData(start, end);
601 removeData(start, end);
628 else if (start <= m_mapValues || start <= m_mapLabels) // if the changes affect the map - reinitialize the pie
602 else if (start <= m_mapper->mapValues() || start <= m_mapper->mapLabels()) // if the changes affect the map - reinitialize the pie
629 initializePieFromModel();
603 initializePieFromModel();
630 }
604 }
631
605
632 void QPieSeriesPrivate::modelColumnsAdded(QModelIndex parent, int start, int end)
606 void QPieSeriesPrivate::modelColumnsAdded(QModelIndex parent, int start, int end)
633 {
607 {
634 Q_UNUSED(parent);
608 Q_UNUSED(parent);
635 if (m_mapOrientation == Qt::Horizontal)
609 if (m_mapper->orientation() == Qt::Horizontal)
636 insertData(start, end);
610 insertData(start, end);
637 else if (start <= m_mapValues || start <= m_mapLabels) // if the changes affect the map - reinitialize the pie
611 else if (start <= m_mapper->mapValues() || start <= m_mapper->mapLabels()) // if the changes affect the map - reinitialize the pie
638 initializePieFromModel();
612 initializePieFromModel();
639 }
613 }
640
614
641 void QPieSeriesPrivate::modelColumnsRemoved(QModelIndex parent, int start, int end)
615 void QPieSeriesPrivate::modelColumnsRemoved(QModelIndex parent, int start, int end)
642 {
616 {
643 Q_UNUSED(parent);
617 Q_UNUSED(parent);
644 if (m_mapOrientation == Qt::Horizontal)
618 if (m_mapper->orientation() == Qt::Horizontal)
645 removeData(start, end);
619 removeData(start, end);
646 else if (start <= m_mapValues || start <= m_mapLabels) // if the changes affect the map - reinitialize the pie
620 else if (start <= m_mapper->mapValues() || start <= m_mapper->mapLabels()) // if the changes affect the map - reinitialize the pie
647 initializePieFromModel();
621 initializePieFromModel();
648 }
622 }
649
623
650 void QPieSeriesPrivate::insertData(int start, int end)
624 void QPieSeriesPrivate::insertData(int start, int end)
651 {
625 {
652 Q_Q(QPieSeries);
626 Q_Q(QPieSeries);
653 if (m_mapCount != -1 && start >= m_mapFirst + m_mapCount) {
627 if (m_mapper->count() != -1 && start >= m_mapper->first() + m_mapper->count()) {
654 return;
628 return;
655 } else {
629 } else {
656 int addedCount = end - start + 1;
630 int addedCount = end - start + 1;
657 if (m_mapCount != -1 && addedCount > m_mapCount)
631 if (m_mapper->count() != -1 && addedCount > m_mapper->count())
658 addedCount = m_mapCount;
632 addedCount = m_mapper->count();
659 int first = qMax(start, m_mapFirst);
633 int first = qMax(start, m_mapper->first());
660 int last = qMin(first + addedCount - 1, m_mapOrientation == Qt::Vertical ? m_model->rowCount() - 1 : m_model->columnCount() - 1);
634 int last = qMin(first + addedCount - 1, m_mapper->orientation() == Qt::Vertical ? m_model->rowCount() - 1 : m_model->columnCount() - 1);
661 for (int i = first; i <= last; i++) {
635 for (int i = first; i <= last; i++) {
662 QPieSlice *slice = new QPieSlice;
636 QPieSlice *slice = new QPieSlice;
663 if (m_mapOrientation == Qt::Vertical) {
637 if (m_mapper->orientation() == Qt::Vertical) {
664 slice->setValue(m_model->data(m_model->index(i, m_mapValues), Qt::DisplayRole).toDouble());
638 slice->setValue(m_model->data(m_model->index(i, m_mapper->mapValues()), Qt::DisplayRole).toDouble());
665 slice->setLabel(m_model->data(m_model->index(i, m_mapLabels), Qt::DisplayRole).toString());
639 slice->setLabel(m_model->data(m_model->index(i, m_mapper->mapLabels()), Qt::DisplayRole).toString());
666 } else {
640 } else {
667 slice->setValue(m_model->data(m_model->index(m_mapValues, i), Qt::DisplayRole).toDouble());
641 slice->setValue(m_model->data(m_model->index(m_mapper->mapValues(), i), Qt::DisplayRole).toDouble());
668 slice->setLabel(m_model->data(m_model->index(m_mapLabels, i), Qt::DisplayRole).toString());
642 slice->setLabel(m_model->data(m_model->index(m_mapper->mapLabels(), i), Qt::DisplayRole).toString());
669 }
643 }
670 slice->setLabelVisible();
644 slice->setLabelVisible();
671 q->insert(i - m_mapFirst, slice);
645 q->insert(i - m_mapper->first(), slice);
672 }
646 }
673 if (m_mapCount != -1 && m_slices.size() > m_mapCount)
647 if (m_mapper->count() != -1 && m_slices.size() > m_mapper->count())
674 for (int i = m_slices.size() - 1; i >= m_mapCount; i--)
648 for (int i = m_slices.size() - 1; i >= m_mapper->count(); i--)
675 q->remove(q->slices().at(i));
649 q->remove(q->slices().at(i));
676 }
650 }
677 }
651 }
678
652
679 void QPieSeriesPrivate::removeData(int start, int end)
653 void QPieSeriesPrivate::removeData(int start, int end)
680 {
654 {
681 Q_Q(QPieSeries);
655 Q_Q(QPieSeries);
682 int removedCount = end - start + 1;
656 int removedCount = end - start + 1;
683 if (m_mapCount != -1 && start >= m_mapFirst + m_mapCount) {
657 if (m_mapper->count() != -1 && start >= m_mapper->first() + m_mapper->count()) {
684 return;
658 return;
685 } else {
659 } else {
686 int toRemove = qMin(m_slices.size(), removedCount); // first find how many items can actually be removed
660 int toRemove = qMin(m_slices.size(), removedCount); // first find how many items can actually be removed
687 int first = qMax(start, m_mapFirst); // get the index of the first item that will be removed.
661 int first = qMax(start, m_mapper->first()); // get the index of the first item that will be removed.
688 int last = qMin(first + toRemove - 1, m_slices.size() + m_mapFirst - 1); // get the index of the last item that will be removed.
662 int last = qMin(first + toRemove - 1, m_slices.size() + m_mapper->first() - 1); // get the index of the last item that will be removed.
689 for (int i = last; i >= first; i--)
663 for (int i = last; i >= first; i--)
690 q->remove(q->slices().at(i - m_mapFirst));
664 q->remove(q->slices().at(i - m_mapper->first()));
691
665
692 if (m_mapCount != -1) {
666 if (m_mapper->count() != -1) {
693 int itemsAvailable; // check how many are available to be added
667 int itemsAvailable; // check how many are available to be added
694 if (m_mapOrientation == Qt::Vertical)
668 if (m_mapper->orientation() == Qt::Vertical)
695 itemsAvailable = m_model->rowCount() - m_mapFirst - m_slices.size();
669 itemsAvailable = m_model->rowCount() - m_mapper->first() - m_slices.size();
696 else
670 else
697 itemsAvailable = m_model->columnCount() - m_mapFirst - m_slices.size();
671 itemsAvailable = m_model->columnCount() - m_mapper->first() - m_slices.size();
698 int toBeAdded = qMin(itemsAvailable, m_mapCount - m_slices.size()); // add not more items than there is space left to be filled.
672 int toBeAdded = qMin(itemsAvailable, m_mapper->count() - m_slices.size()); // add not more items than there is space left to be filled.
699 int currentSize = m_slices.size();
673 int currentSize = m_slices.size();
700 if (toBeAdded > 0)
674 if (toBeAdded > 0)
701 for (int i = m_slices.size(); i < currentSize + toBeAdded; i++) {
675 for (int i = m_slices.size(); i < currentSize + toBeAdded; i++) {
702 QPieSlice *slice = new QPieSlice;
676 QPieSlice *slice = new QPieSlice;
703 if (m_mapOrientation == Qt::Vertical) {
677 if (m_mapper->orientation() == Qt::Vertical) {
704 slice->setValue(m_model->data(m_model->index(i + m_mapFirst, m_mapValues), Qt::DisplayRole).toDouble());
678 slice->setValue(m_model->data(m_model->index(i + m_mapper->first(), m_mapper->mapValues()), Qt::DisplayRole).toDouble());
705 slice->setLabel(m_model->data(m_model->index(i + m_mapFirst, m_mapLabels), Qt::DisplayRole).toString());
679 slice->setLabel(m_model->data(m_model->index(i + m_mapper->first(), m_mapper->mapLabels()), Qt::DisplayRole).toString());
706 } else {
680 } else {
707 slice->setValue(m_model->data(m_model->index(m_mapValues, i + m_mapFirst), Qt::DisplayRole).toDouble());
681 slice->setValue(m_model->data(m_model->index(m_mapper->mapValues(), i + m_mapper->first()), Qt::DisplayRole).toDouble());
708 slice->setLabel(m_model->data(m_model->index(m_mapLabels, i + m_mapFirst), Qt::DisplayRole).toString());
682 slice->setLabel(m_model->data(m_model->index(m_mapper->mapLabels(), i + m_mapper->first()), Qt::DisplayRole).toString());
709 }
683 }
710 slice->setLabelVisible();
684 slice->setLabelVisible();
711 q->insert(i, slice);
685 q->insert(i, slice);
712 }
686 }
713 }
687 }
714 }
688 }
715 }
689 }
716
690
691 void QPieSeriesPrivate::setMapping()
692 {
693 initializePieFromModel();
694
695 // connect signals from the model
696 connect(m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex,QModelIndex)));
697 connect(m_model, SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(modelRowsAdded(QModelIndex,int,int)));
698 connect(m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SLOT(modelRowsRemoved(QModelIndex,int,int)));
699 connect(m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), this, SLOT(modelColumnsAdded(QModelIndex,int,int)));
700 connect(m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), this, SLOT(modelColumnsRemoved(QModelIndex,int,int)));
701
702 // connect the signal from the mapper
703 connect(m_mapper, SIGNAL(updated()), this, SLOT(initializePieFromModel()));
704 }
705
717 void QPieSeriesPrivate::initializePieFromModel()
706 void QPieSeriesPrivate::initializePieFromModel()
718 {
707 {
719 Q_Q(QPieSeries);
708 Q_Q(QPieSeries);
720 // clear current content
709 // clear current content
721 q->clear();
710 q->clear();
722
711
723 // create the initial slices set
712 // create the initial slices set
724 if (m_mapOrientation == Qt::Vertical) {
713 if (m_mapper->orientation() == Qt::Vertical) {
725 if (m_mapValues >= m_model->columnCount() || m_mapLabels >= m_model->columnCount())
714 if (m_mapper->mapValues() >= m_model->columnCount() || m_mapper->mapLabels() >= m_model->columnCount())
726 return; // mapped columns are not existing
715 return; // mapped columns are not existing
727
716
728 int sliceCount = 0;
717 int sliceCount = 0;
729 if(m_mapCount == -1)
718 if(m_mapper->count() == -1)
730 sliceCount = m_model->rowCount() - m_mapFirst;
719 sliceCount = m_model->rowCount() - m_mapper->first();
731 else
720 else
732 sliceCount = qMin(m_mapCount, m_model->rowCount() - m_mapFirst);
721 sliceCount = qMin(m_mapper->count(), m_model->rowCount() - m_mapper->first());
733 for (int i = m_mapFirst; i < m_mapFirst + sliceCount; i++)
722 for (int i = m_mapper->first(); i < m_mapper->first() + sliceCount; i++)
734 q->append(m_model->data(m_model->index(i, m_mapValues), Qt::DisplayRole).toDouble(), m_model->data(m_model->index(i, m_mapLabels), Qt::DisplayRole).toString());
723 q->append(m_model->data(m_model->index(i, m_mapper->mapValues()), Qt::DisplayRole).toDouble(), m_model->data(m_model->index(i, m_mapper->mapLabels()), Qt::DisplayRole).toString());
735 } else {
724 } else {
736 if (m_mapValues >= m_model->rowCount() || m_mapLabels >= m_model->rowCount())
725 if (m_mapper->mapValues() >= m_model->rowCount() || m_mapper->mapLabels() >= m_model->rowCount())
737 return; // mapped columns are not existing
726 return; // mapped columns are not existing
738
727
739 int sliceCount = 0;
728 int sliceCount = 0;
740 if(m_mapCount == -1)
729 if(m_mapper->count() == -1)
741 sliceCount = m_model->columnCount() - m_mapFirst;
730 sliceCount = m_model->columnCount() - m_mapper->first();
742 else
731 else
743 sliceCount = qMin(m_mapCount, m_model->columnCount() - m_mapFirst);
732 sliceCount = qMin(m_mapper->count(), m_model->columnCount() - m_mapper->first());
744 for (int i = m_mapFirst; i < m_mapFirst + sliceCount; i++)
733 for (int i = m_mapper->first(); i < m_mapper->first() + sliceCount; i++)
745 q->append(m_model->data(m_model->index(m_mapValues, i), Qt::DisplayRole).toDouble(), m_model->data(m_model->index(m_mapLabels, i), Qt::DisplayRole).toString());
734 q->append(m_model->data(m_model->index(m_mapper->mapValues(), i), Qt::DisplayRole).toDouble(), m_model->data(m_model->index(m_mapper->mapLabels(), i), Qt::DisplayRole).toString());
746 }
735 }
747 q->setLabelsVisible(true);
736 q->setLabelsVisible(true);
748 }
737 }
749
738
750 bool QPieSeriesPrivate::setRealValue(qreal &value, qreal newValue, qreal max, qreal min)
739 bool QPieSeriesPrivate::setRealValue(qreal &value, qreal newValue, qreal max, qreal min)
751 {
740 {
752 // Remove rounding errors
741 // Remove rounding errors
753 qreal roundedValue = newValue;
742 qreal roundedValue = newValue;
754 if (qFuzzyIsNull(min) && qFuzzyIsNull(newValue))
743 if (qFuzzyIsNull(min) && qFuzzyIsNull(newValue))
755 roundedValue = 0.0;
744 roundedValue = 0.0;
756 else if (qFuzzyCompare(newValue, max))
745 else if (qFuzzyCompare(newValue, max))
757 roundedValue = max;
746 roundedValue = max;
758 else if (qFuzzyCompare(newValue, min))
747 else if (qFuzzyCompare(newValue, min))
759 roundedValue = min;
748 roundedValue = min;
760
749
761 // Check if the position is valid after removing the rounding errors
750 // Check if the position is valid after removing the rounding errors
762 if (roundedValue < min || roundedValue > max) {
751 if (roundedValue < min || roundedValue > max) {
763 qWarning("QPieSeries: Illegal value");
752 qWarning("QPieSeries: Illegal value");
764 return false;
753 return false;
765 }
754 }
766
755
767 if (!qFuzzyIsNull(value - roundedValue)) {
756 if (!qFuzzyIsNull(value - roundedValue)) {
768 value = roundedValue;
757 value = roundedValue;
769 return true;
758 return true;
770 }
759 }
771
760
772 // The change was so small it is considered a rounding error
761 // The change was so small it is considered a rounding error
773 return false;
762 return false;
774 }
763 }
775
764
776 void QPieSeriesPrivate::scaleDomain(Domain& domain)
765 void QPieSeriesPrivate::scaleDomain(Domain& domain)
777 {
766 {
778 Q_UNUSED(domain);
767 Q_UNUSED(domain);
779 #ifndef QT_NO_DEBUG
768 #ifndef QT_NO_DEBUG
780 qWarning() << __FILE__<<__FUNCTION__<<"not implemented";
769 qWarning() << __FILE__<<__FUNCTION__<<"not implemented";
781 #endif
770 #endif
782 }
771 }
783
772
784 Chart* QPieSeriesPrivate::createGraphics(ChartPresenter* presenter)
773 Chart* QPieSeriesPrivate::createGraphics(ChartPresenter* presenter)
785 {
774 {
786 Q_Q(QPieSeries);
775 Q_Q(QPieSeries);
787 PieChartItem* pie = new PieChartItem(q,presenter);
776 PieChartItem* pie = new PieChartItem(q,presenter);
788 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
777 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
789 presenter->animator()->addAnimation(pie);
778 presenter->animator()->addAnimation(pie);
790 }
779 }
791 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
780 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
792 return pie;
781 return pie;
793 }
782 }
794
783
795 QList<LegendMarker*> QPieSeriesPrivate::createLegendMarker(QLegend* legend)
784 QList<LegendMarker*> QPieSeriesPrivate::createLegendMarker(QLegend* legend)
796 {
785 {
797 Q_Q(QPieSeries);
786 Q_Q(QPieSeries);
798 QList<LegendMarker*> markers;
787 QList<LegendMarker*> markers;
799 foreach(QPieSlice* slice, q->slices()) {
788 foreach(QPieSlice* slice, q->slices()) {
800 PieLegendMarker* marker = new PieLegendMarker(q,slice,legend);
789 PieLegendMarker* marker = new PieLegendMarker(q,slice,legend);
801 markers << marker;
790 markers << marker;
802 }
791 }
803 return markers;
792 return markers;
804 }
793 }
805
794
806 #include "moc_qpieseries.cpp"
795 #include "moc_qpieseries.cpp"
807 #include "moc_qpieseries_p.cpp"
796 #include "moc_qpieseries_p.cpp"
808
797
809 QTCOMMERCIALCHART_END_NAMESPACE
798 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,89 +1,90
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #ifndef PIESERIES_H
21 #ifndef PIESERIES_H
22 #define PIESERIES_H
22 #define PIESERIES_H
23
23
24 #include <qabstractseries.h>
24 #include <qabstractseries.h>
25
25
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 class QPieSeriesPrivate;
27 class QPieSeriesPrivate;
28 class QPieSlice;
28 class QPieSlice;
29 class QPieModelMapper;
29
30
30 class QTCOMMERCIALCHART_EXPORT QPieSeries : public QAbstractSeries
31 class QTCOMMERCIALCHART_EXPORT QPieSeries : public QAbstractSeries
31 {
32 {
32 Q_OBJECT
33 Q_OBJECT
33 Q_PROPERTY(qreal horizontalPosition READ horizontalPosition WRITE setHorizontalPosition)
34 Q_PROPERTY(qreal horizontalPosition READ horizontalPosition WRITE setHorizontalPosition)
34 Q_PROPERTY(qreal verticalPosition READ verticalPosition WRITE setVerticalPosition)
35 Q_PROPERTY(qreal verticalPosition READ verticalPosition WRITE setVerticalPosition)
35 Q_PROPERTY(qreal size READ pieSize WRITE setPieSize)
36 Q_PROPERTY(qreal size READ pieSize WRITE setPieSize)
36 Q_PROPERTY(qreal startAngle READ pieStartAngle WRITE setPieStartAngle)
37 Q_PROPERTY(qreal startAngle READ pieStartAngle WRITE setPieStartAngle)
37 Q_PROPERTY(qreal endAngle READ pieEndAngle WRITE setPieEndAngle)
38 Q_PROPERTY(qreal endAngle READ pieEndAngle WRITE setPieEndAngle)
38
39
39 public:
40 public:
40 explicit QPieSeries(QObject *parent = 0);
41 explicit QPieSeries(QObject *parent = 0);
41 virtual ~QPieSeries();
42 virtual ~QPieSeries();
42
43
43 QAbstractSeries::SeriesType type() const;
44 QAbstractSeries::SeriesType type() const;
44
45
45 bool append(QPieSlice* slice);
46 bool append(QPieSlice* slice);
46 bool append(QList<QPieSlice*> slices);
47 bool append(QList<QPieSlice*> slices);
47 QPieSeries& operator << (QPieSlice* slice);
48 QPieSeries& operator << (QPieSlice* slice);
48 QPieSlice* append(qreal value, QString name);
49 QPieSlice* append(qreal value, QString name);
49 bool insert(int index, QPieSlice* slice);
50 bool insert(int index, QPieSlice* slice);
50 bool remove(QPieSlice* slice);
51 bool remove(QPieSlice* slice);
51 void clear();
52 void clear();
52
53
53 QList<QPieSlice*> slices() const;
54 QList<QPieSlice*> slices() const;
54 int count() const;
55 int count() const;
55 bool isEmpty() const;
56 bool isEmpty() const;
56
57
57 qreal sum() const;
58 qreal sum() const;
58
59
59 void setHorizontalPosition(qreal relativePosition);
60 void setHorizontalPosition(qreal relativePosition);
60 qreal horizontalPosition() const;
61 qreal horizontalPosition() const;
61 void setVerticalPosition(qreal relativePosition);
62 void setVerticalPosition(qreal relativePosition);
62 qreal verticalPosition() const;
63 qreal verticalPosition() const;
63
64
64 void setPieSize(qreal relativeSize);
65 void setPieSize(qreal relativeSize);
65 qreal pieSize() const;
66 qreal pieSize() const;
66
67
67 void setPieStartAngle(qreal startAngle);
68 void setPieStartAngle(qreal startAngle);
68 qreal pieStartAngle() const;
69 qreal pieStartAngle() const;
69 void setPieEndAngle(qreal endAngle);
70 void setPieEndAngle(qreal endAngle);
70 qreal pieEndAngle() const;
71 qreal pieEndAngle() const;
71
72
72 void setLabelsVisible(bool visible = true);
73 void setLabelsVisible(bool visible = true);
73
74
74 bool setModel(QAbstractItemModel* model);
75 void setModel(QAbstractItemModel* model);
75 void setModelMapping(int modelValuesLine, int modelLabelsLine, Qt::Orientation orientation = Qt::Vertical);
76 void setModelMapper(QPieModelMapper *mapper);
76 void setModelMappingRange(int first, int count = -1);
77 QPieModelMapper* modelMapper() const;
77
78
78 Q_SIGNALS:
79 Q_SIGNALS:
79 void clicked(QPieSlice* slice);
80 void clicked(QPieSlice* slice);
80 void hovered(QPieSlice* slice, bool state);
81 void hovered(QPieSlice* slice, bool state);
81
82
82 private:
83 private:
83 Q_DECLARE_PRIVATE(QPieSeries)
84 Q_DECLARE_PRIVATE(QPieSeries)
84 Q_DISABLE_COPY(QPieSeries)
85 Q_DISABLE_COPY(QPieSeries)
85 };
86 };
86
87
87 QTCOMMERCIALCHART_END_NAMESPACE
88 QTCOMMERCIALCHART_END_NAMESPACE
88
89
89 #endif // PIESERIES_H
90 #endif // PIESERIES_H
@@ -1,91 +1,91
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #ifndef QPIESERIES_P_H
21 #ifndef QPIESERIES_P_H
22 #define QPIESERIES_P_H
22 #define QPIESERIES_P_H
23
23
24 #include "qpieseries.h"
24 #include "qpieseries.h"
25 #include "qabstractseries_p.h"
25 #include "qabstractseries_p.h"
26
26
27 class QModelIndex;
27 class QModelIndex;
28
28
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 class QLegendPrivate;
30 class QLegendPrivate;
31 class QPieModelMapper;
31
32
32 class QPieSeriesPrivate : public QAbstractSeriesPrivate
33 class QPieSeriesPrivate : public QAbstractSeriesPrivate
33 {
34 {
34 Q_OBJECT
35 Q_OBJECT
35
36
36 public:
37 public:
37 QPieSeriesPrivate(QPieSeries *parent);
38 QPieSeriesPrivate(QPieSeries *parent);
38 ~QPieSeriesPrivate();
39 ~QPieSeriesPrivate();
39
40
40 void scaleDomain(Domain& domain);
41 void scaleDomain(Domain& domain);
41 Chart* createGraphics(ChartPresenter *presenter);
42 Chart* createGraphics(ChartPresenter *presenter);
42 QList<LegendMarker*> createLegendMarker(QLegend *legend);
43 QList<LegendMarker*> createLegendMarker(QLegend *legend);
43
44
44 void updateDerivativeData();
45 void updateDerivativeData();
45
46
46 static QPieSeriesPrivate* seriesData(QPieSeries &series);
47 static QPieSeriesPrivate* seriesData(QPieSeries &series);
47
48
48 Q_SIGNALS:
49 Q_SIGNALS:
49 void added(QList<QPieSlice*> slices);
50 void added(QList<QPieSlice*> slices);
50 void removed(QList<QPieSlice*> slices);
51 void removed(QList<QPieSlice*> slices);
51 void piePositionChanged();
52 void piePositionChanged();
52 void pieSizeChanged();
53 void pieSizeChanged();
53
54
54 public Q_SLOTS:
55 public Q_SLOTS:
55 void sliceChanged();
56 void sliceChanged();
56 void sliceClicked();
57 void sliceClicked();
57 void sliceHovered(bool state);
58 void sliceHovered(bool state);
59 void initializePieFromModel();
58 void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight);
60 void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight);
59 void modelRowsAdded(QModelIndex parent, int start, int end);
61 void modelRowsAdded(QModelIndex parent, int start, int end);
60 void modelRowsRemoved(QModelIndex parent, int start, int end);
62 void modelRowsRemoved(QModelIndex parent, int start, int end);
61 void modelColumnsAdded(QModelIndex parent, int start, int end);
63 void modelColumnsAdded(QModelIndex parent, int start, int end);
62 void modelColumnsRemoved(QModelIndex parent, int start, int end);
64 void modelColumnsRemoved(QModelIndex parent, int start, int end);
63 bool setRealValue(qreal &value, qreal newValue, qreal max, qreal min = 0.0);
65 bool setRealValue(qreal &value, qreal newValue, qreal max, qreal min = 0.0);
64
66
65 private:
67 private:
66 void initializePieFromModel();
68 void setMapping();
67 void insertData(int start, int end);
69 void insertData(int start, int end);
68 void removeData(int start, int end);
70 void removeData(int start, int end);
69
71
70 public:
72 public:
71 QList<QPieSlice*> m_slices;
73 QList<QPieSlice*> m_slices;
72 qreal m_pieRelativeHorPos;
74 qreal m_pieRelativeHorPos;
73 qreal m_pieRelativeVerPos;
75 qreal m_pieRelativeVerPos;
74 qreal m_pieRelativeSize;
76 qreal m_pieRelativeSize;
75 qreal m_pieStartAngle;
77 qreal m_pieStartAngle;
76 qreal m_pieEndAngle;
78 qreal m_pieEndAngle;
77 qreal m_sum;
79 qreal m_sum;
78
80
79 // model map
81 // model map
80 int m_mapValues;
82 QPieModelMapper *m_mapper;
81 int m_mapLabels;
82 bool m_modelReady;
83
83
84 private:
84 private:
85 friend class QLegendPrivate;
85 friend class QLegendPrivate;
86 Q_DECLARE_PUBLIC(QPieSeries)
86 Q_DECLARE_PUBLIC(QPieSeries)
87 };
87 };
88
88
89 QTCOMMERCIALCHART_END_NAMESPACE
89 QTCOMMERCIALCHART_END_NAMESPACE
90
90
91 #endif // QPIESERIES_P_H
91 #endif // QPIESERIES_P_H
@@ -1,156 +1,138
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "qabstractseries.h"
21 #include "qabstractseries.h"
22 #include "qabstractseries_p.h"
22 #include "qabstractseries_p.h"
23 #include "chartdataset_p.h"
23 #include "chartdataset_p.h"
24
24
25 QTCOMMERCIALCHART_BEGIN_NAMESPACE
25 QTCOMMERCIALCHART_BEGIN_NAMESPACE
26
26
27 /*!
27 /*!
28 \class QAbstractSeries
28 \class QAbstractSeries
29 \brief Base class for all QtCommercial Chart series.
29 \brief Base class for all QtCommercial Chart series.
30 \mainclass
30 \mainclass
31
31
32 Usually you use the series type specific inherited classes instead of the base class.
32 Usually you use the series type specific inherited classes instead of the base class.
33 \sa QXYSeries, QLineSeries, QSplineSeries, QScatterSeries, QAreaSeries, QBarSeries, QStackedBarSeries,
33 \sa QXYSeries, QLineSeries, QSplineSeries, QScatterSeries, QAreaSeries, QBarSeries, QStackedBarSeries,
34 QPercentBarSeries, QPieSeries
34 QPercentBarSeries, QPieSeries
35 */
35 */
36
36
37 /*!
37 /*!
38 \enum QAbstractSeries::SeriesType
38 \enum QAbstractSeries::SeriesType
39
39
40 The type of the series object.
40 The type of the series object.
41
41
42 \value SeriesTypeLine
42 \value SeriesTypeLine
43 \value SeriesTypeArea
43 \value SeriesTypeArea
44 \value SeriesTypeBar
44 \value SeriesTypeBar
45 \value SeriesTypeStackedBar
45 \value SeriesTypeStackedBar
46 \value SeriesTypePercentBar
46 \value SeriesTypePercentBar
47 \value SeriesTypePie
47 \value SeriesTypePie
48 \value SeriesTypeScatter
48 \value SeriesTypeScatter
49 \value SeriesTypeSpline
49 \value SeriesTypeSpline
50 */
50 */
51
51
52 /*!
52 /*!
53 \fn QSeriesType QAbstractSeries::type() const
53 \fn QSeriesType QAbstractSeries::type() const
54 \brief The type of the series.
54 \brief The type of the series.
55 */
55 */
56
56
57 /*!
57 /*!
58 \fn bool QAbstractSeries::setModel(QAbstractItemModel *model)
58 \fn bool QAbstractSeries::setModel(QAbstractItemModel *model)
59 \brief Use the \a model to provide data for the series. The model overrides possible user data
59 \brief Use the \a model to provide data for the series. The model overrides possible user data
60 set with QChartSeries type specific data setters. For example if you call both
60 set with QChartSeries type specific data setters. For example if you call both
61 QScatterSeries::addData() and QScatterSeries::setModel, only the data provided by the model is
61 QScatterSeries::addData() and QScatterSeries::setModel, only the data provided by the model is
62 used by the series. Returns true if the model is valid for the series.
62 used by the series. Returns true if the model is valid for the series.
63 */
63 */
64
64
65 /*!
65 /*!
66 \property QAbstractSeries::name
66 \property QAbstractSeries::name
67 \brief name of the series property
67 \brief name of the series property
68 */
68 */
69
69
70 /*!
70 /*!
71 \fn void QAbstractSeries::setName(const QString& name)
71 \fn void QAbstractSeries::setName(const QString& name)
72 \brief Sets a \a name for the series.
72 \brief Sets a \a name for the series.
73
73
74 The name of a series is shown in the legend for QXYSeries.
74 The name of a series is shown in the legend for QXYSeries.
75 \sa QChart::setTitle()
75 \sa QChart::setTitle()
76 \sa QPieSlice::setLabel()
76 \sa QPieSlice::setLabel()
77 \sa QBarSet::setName()
77 \sa QBarSet::setName()
78 */
78 */
79
79
80 /*!
80 /*!
81 \internal
81 \internal
82 \brief Constructs ChartSeries object with \a parent.
82 \brief Constructs ChartSeries object with \a parent.
83 */
83 */
84 QAbstractSeries::QAbstractSeries(QAbstractSeriesPrivate &d, QObject *parent) :
84 QAbstractSeries::QAbstractSeries(QAbstractSeriesPrivate &d, QObject *parent) :
85 QObject(parent),
85 QObject(parent),
86 d_ptr(&d)
86 d_ptr(&d)
87 {
87 {
88 }
88 }
89
89
90 /*!
90 /*!
91 \brief Virtual destructor for the chart series.
91 \brief Virtual destructor for the chart series.
92 */
92 */
93 QAbstractSeries::~QAbstractSeries()
93 QAbstractSeries::~QAbstractSeries()
94 {
94 {
95 if(d_ptr->m_dataset) qFatal("Still binded series detected !");
95 if(d_ptr->m_dataset) qFatal("Still binded series detected !");
96 }
96 }
97
97
98 /*!
98 /*!
99 \brief Returns the pointer to the model that is used as the series data source
99 \brief Returns the pointer to the model that is used as the series data source
100 */
100 */
101 QAbstractItemModel* QAbstractSeries::model() const
101 QAbstractItemModel* QAbstractSeries::model() const
102 {
102 {
103 return d_ptr->m_model;
103 return d_ptr->m_model;
104 }
104 }
105
105
106 int QAbstractSeries::mapFirst() const
107 {
108 return d_ptr->m_mapFirst;
109 }
110
111 int QAbstractSeries::mapCount() const
112 {
113 return d_ptr->m_mapCount;
114 }
115
116 int QAbstractSeries::mapOrientation() const
117 {
118 return d_ptr->m_mapOrientation;
119 }
120
121 void QAbstractSeries::setName(const QString& name)
106 void QAbstractSeries::setName(const QString& name)
122 {
107 {
123 d_ptr->m_name = name;
108 d_ptr->m_name = name;
124 }
109 }
125
110
126 /*!
111 /*!
127 \brief Returns the name of the series.
112 \brief Returns the name of the series.
128 \sa setName()
113 \sa setName()
129 */
114 */
130 QString QAbstractSeries::name() const
115 QString QAbstractSeries::name() const
131 {
116 {
132 return d_ptr->m_name;
117 return d_ptr->m_name;
133 }
118 }
134
119
135 ///////////////////////////////////////////////////////////////////////////////////////////////////
120 ///////////////////////////////////////////////////////////////////////////////////////////////////
136
121
137 QAbstractSeriesPrivate::QAbstractSeriesPrivate(QAbstractSeries* q):
122 QAbstractSeriesPrivate::QAbstractSeriesPrivate(QAbstractSeries* q):
138 q_ptr(q),
123 q_ptr(q),
139 m_model(0),
124 m_model(0),
140 m_dataset(0),
125 m_dataset(0)
141 m_mapFirst(0),
142 m_mapCount(-1),
143 m_mapOrientation(Qt::Vertical)
144 {
126 {
145 }
127 }
146
128
147 QAbstractSeriesPrivate::~QAbstractSeriesPrivate()
129 QAbstractSeriesPrivate::~QAbstractSeriesPrivate()
148 {
130 {
149 }
131 }
150
132
151 #include "moc_qabstractseries.cpp"
133 #include "moc_qabstractseries.cpp"
152 #include "moc_qabstractseries_p.cpp"
134 #include "moc_qabstractseries_p.cpp"
153
135
154 QTCOMMERCIALCHART_END_NAMESPACE
136 QTCOMMERCIALCHART_END_NAMESPACE
155
137
156
138
@@ -1,77 +1,75
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #ifndef QABSTRACTSERIES_H
21 #ifndef QABSTRACTSERIES_H
22 #define QABSTRACTSERIES_H
22 #define QABSTRACTSERIES_H
23
23
24 #include <qchartglobal.h>
24 #include <qchartglobal.h>
25 #include <QObject>
25 #include <QObject>
26 #include <QPen>
26 #include <QPen>
27
27
28 class QAbstractItemModel;
28 class QAbstractItemModel;
29
29
30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31
31
32 class QAbstractSeriesPrivate;
32 class QAbstractSeriesPrivate;
33 class QChart;
33 class QChart;
34 //class QModelMapper;
34
35
35 class QTCOMMERCIALCHART_EXPORT QAbstractSeries : public QObject
36 class QTCOMMERCIALCHART_EXPORT QAbstractSeries : public QObject
36 {
37 {
37 Q_OBJECT
38 Q_OBJECT
38 Q_PROPERTY(QString name READ name WRITE setName)
39 Q_PROPERTY(QString name READ name WRITE setName)
39 Q_ENUMS(SeriesType)
40 Q_ENUMS(SeriesType)
40
41
41 public:
42 public:
42 enum SeriesType {
43 enum SeriesType {
43 SeriesTypeLine,
44 SeriesTypeLine,
44 SeriesTypeArea,
45 SeriesTypeArea,
45 SeriesTypeBar,
46 SeriesTypeBar,
46 SeriesTypeStackedBar,
47 SeriesTypeStackedBar,
47 SeriesTypePercentBar,
48 SeriesTypePercentBar,
48 SeriesTypePie,
49 SeriesTypePie,
49 SeriesTypeScatter,
50 SeriesTypeScatter,
50 SeriesTypeSpline
51 SeriesTypeSpline
51 };
52 };
52
53
53 protected:
54 protected:
54 QAbstractSeries(QAbstractSeriesPrivate &d, QObject *parent = 0);
55 QAbstractSeries(QAbstractSeriesPrivate &d, QObject *parent = 0);
55
56
56 public:
57 public:
57 ~QAbstractSeries();
58 ~QAbstractSeries();
58 virtual SeriesType type() const = 0;
59 virtual SeriesType type() const = 0;
59 virtual bool setModel(QAbstractItemModel* model) = 0;
60 virtual void setModel(QAbstractItemModel *model) = 0;
60 QAbstractItemModel* model() const;
61 QAbstractItemModel* model() const;
61 int mapFirst() const;
62 int mapCount() const;
63 int mapOrientation() const;
64 void setName(const QString& name);
62 void setName(const QString& name);
65 QString name() const;
63 QString name() const;
66 QChart* chart() const;
64 QChart* chart() const;
67
65
68 protected:
66 protected:
69 QScopedPointer<QAbstractSeriesPrivate> d_ptr;
67 QScopedPointer<QAbstractSeriesPrivate> d_ptr;
70 friend class ChartDataSet;
68 friend class ChartDataSet;
71 friend class ChartPresenter;
69 friend class ChartPresenter;
72 friend class QLegendPrivate;
70 friend class QLegendPrivate;
73 };
71 };
74
72
75 QTCOMMERCIALCHART_END_NAMESPACE
73 QTCOMMERCIALCHART_END_NAMESPACE
76
74
77 #endif
75 #endif
@@ -1,72 +1,74
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 // W A R N I N G
21 // W A R N I N G
22 // -------------
22 // -------------
23 //
23 //
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 // implementation detail. This header file may change from version to
25 // implementation detail. This header file may change from version to
26 // version without notice, or even be removed.
26 // version without notice, or even be removed.
27 //
27 //
28 // We mean it.
28 // We mean it.
29
29
30 #ifndef QABSTRACTSERIES_P_H
30 #ifndef QABSTRACTSERIES_P_H
31 #define QABSTRACTSERIES_P_H
31 #define QABSTRACTSERIES_P_H
32
32
33 #include "qabstractseries.h"
33 #include "qabstractseries.h"
34
34
35 class QAbstractItemModel;
35 class QAbstractItemModel;
36
36
37 QTCOMMERCIALCHART_BEGIN_NAMESPACE
37 QTCOMMERCIALCHART_BEGIN_NAMESPACE
38
38
39 class Domain;
39 class Domain;
40 class ChartPresenter;
40 class ChartPresenter;
41 class Chart;
41 class Chart;
42 class LegendMarker;
42 class LegendMarker;
43 class QLegend;
43 class QLegend;
44 class ChartDataSet;
44 class ChartDataSet;
45 //class QModelMapper;
45
46
46 class QAbstractSeriesPrivate : public QObject
47 class QAbstractSeriesPrivate : public QObject
47 {
48 {
48 Q_OBJECT
49 Q_OBJECT
49 public:
50 public:
50 QAbstractSeriesPrivate(QAbstractSeries *q);
51 QAbstractSeriesPrivate(QAbstractSeries *q);
51 ~QAbstractSeriesPrivate();
52 ~QAbstractSeriesPrivate();
52
53
53 virtual void scaleDomain(Domain& domain) = 0;
54 virtual void scaleDomain(Domain& domain) = 0;
54 virtual Chart* createGraphics(ChartPresenter* presenter) = 0;
55 virtual Chart* createGraphics(ChartPresenter* presenter) = 0;
55 virtual QList<LegendMarker*> createLegendMarker(QLegend* legend) = 0;
56 virtual QList<LegendMarker*> createLegendMarker(QLegend* legend) = 0;
56
57
57 protected:
58 protected:
58 QAbstractSeries *q_ptr;
59 QAbstractSeries *q_ptr;
59 QAbstractItemModel *m_model;
60 QAbstractItemModel *m_model;
61 // QModelMapper *m_mapper;
60 ChartDataSet *m_dataset;
62 ChartDataSet *m_dataset;
61 int m_mapFirst;
63 // int m_mapFirst;
62 int m_mapCount;
64 // int m_mapCount;
63 Qt::Orientation m_mapOrientation;
65 // Qt::Orientation m_mapOrientation;
64 QString m_name;
66 QString m_name;
65
67
66 friend class QAbstractSeries;
68 friend class QAbstractSeries;
67 friend class ChartDataSet;
69 friend class ChartDataSet;
68 };
70 };
69
71
70 QTCOMMERCIALCHART_END_NAMESPACE
72 QTCOMMERCIALCHART_END_NAMESPACE
71
73
72 #endif
74 #endif
@@ -1,270 +1,266
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "qsplineseries.h"
21 #include "qsplineseries.h"
22 #include "qsplineseries_p.h"
22 #include "qsplineseries_p.h"
23 #include "splinechartitem_p.h"
23 #include "splinechartitem_p.h"
24 #include "chartdataset_p.h"
24 #include "chartdataset_p.h"
25 #include "charttheme_p.h"
25 #include "charttheme_p.h"
26 #include "chartanimator_p.h"
26 #include "chartanimator_p.h"
27 #include <QAbstractItemModel>
27 #include <QAbstractItemModel>
28
28
29 /*!
29 /*!
30 \class QSplineSeries
30 \class QSplineSeries
31 \brief Series type used to store data needed to draw a spline.
31 \brief Series type used to store data needed to draw a spline.
32
32
33 QSplineSeries stores the data points along with the segment control points needed by QPainterPath to draw spline
33 QSplineSeries stores the data points along with the segment control points needed by QPainterPath to draw spline
34 Control points are automatically calculated when data changes. The algorithm computes the points so that the normal spline can be drawn.
34 Control points are automatically calculated when data changes. The algorithm computes the points so that the normal spline can be drawn.
35
35
36 \image examples_splinechart.png
36 \image examples_splinechart.png
37
37
38 Creating basic spline chart is simple:
38 Creating basic spline chart is simple:
39 \code
39 \code
40 QSplineSeries* series = new QSplineSeries();
40 QSplineSeries* series = new QSplineSeries();
41 series->append(0, 6);
41 series->append(0, 6);
42 series->append(2, 4);
42 series->append(2, 4);
43 ...
43 ...
44 chart->addSeries(series);
44 chart->addSeries(series);
45 \endcode
45 \endcode
46 */
46 */
47
47
48 /*!
48 /*!
49 \fn QSeriesType QSplineSeries::type() const
49 \fn QSeriesType QSplineSeries::type() const
50 Returns the type of the series
50 Returns the type of the series
51 */
51 */
52
52
53 /*!
54 \fn QSeriesType QSplineSeries::controlPoint(int index) const
55 Returns the control point specified by \a index
56 */
57
58 QTCOMMERCIALCHART_BEGIN_NAMESPACE
53 QTCOMMERCIALCHART_BEGIN_NAMESPACE
59
54
60 /*!
55 /*!
61 Constructs empty series object which is a child of \a parent.
56 Constructs empty series object which is a child of \a parent.
62 When series object is added to QChartView or QChart instance then the ownerships is transferred.
57 When series object is added to QChartView or QChart instance then the ownerships is transferred.
63 */
58 */
64
59
65 QSplineSeries::QSplineSeries(QObject *parent) :
60 QSplineSeries::QSplineSeries(QObject *parent) :
66 QLineSeries(*new QSplineSeriesPrivate(this),parent)
61 QLineSeries(*new QSplineSeriesPrivate(this),parent)
67 {
62 {
68 }
63 }
69
64
70 QSplineSeries::~QSplineSeries()
65 QSplineSeries::~QSplineSeries()
71 {
66 {
72 Q_D(QSplineSeries);
67 Q_D(QSplineSeries);
73 if(d->m_dataset){
68 if(d->m_dataset){
74 d->m_dataset->removeSeries(this);
69 d->m_dataset->removeSeries(this);
75 }
70 }
76 }
71 }
77
72
78 QAbstractSeries::SeriesType QSplineSeries::type() const
73 QAbstractSeries::SeriesType QSplineSeries::type() const
79 {
74 {
80 return QAbstractSeries::SeriesTypeSpline;
75 return QAbstractSeries::SeriesTypeSpline;
81 }
76 }
82
77
83 /*!
78 void QSplineSeries::setModel(QAbstractItemModel *model)
84 Sets the \a modelX to be used as a data source for x coordinate and \a modelY to be used
85 as a data source for y coordinate. The \a orientation parameter specifies whether the data
86 is in columns or in rows.
87 \sa setModel()
88 */
89 void QSplineSeries::setModelMapping(int modelX, int modelY, Qt::Orientation orientation)
90 {
79 {
91 Q_D(QSplineSeries);
80 Q_D(QSplineSeries);
92 QXYSeries::setModelMapping(modelX, modelY, orientation);
81 QXYSeries::setModel(model);
93 d->updateControlPoints();
82 if (d->m_model && d->m_mapper)
83 d->updateControlPoints();
94 }
84 }
95
85
96 void QSplineSeries::setModelMappingRange(int first, int count)
86 void QSplineSeries::setModelMapper(QXYModelMapper *mapper)
97 {
87 {
98 Q_D(QSplineSeries);
88 Q_D(QSplineSeries);
99 QXYSeries::setModelMappingRange(first, count);
89 QXYSeries::setModelMapper(mapper);
100 d->updateControlPoints();
90 if (d->m_model && d->m_mapper)
101
91 d->updateControlPoints();
102 }
92 }
103
93
104 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
94 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
105
95
106 QSplineSeriesPrivate::QSplineSeriesPrivate(QSplineSeries* q):QLineSeriesPrivate(q)
96 QSplineSeriesPrivate::QSplineSeriesPrivate(QSplineSeries* q):QLineSeriesPrivate(q)
107 {
97 {
108 QObject::connect(this,SIGNAL(pointAdded(int)), this, SLOT(updateControlPoints()));
98 QObject::connect(this,SIGNAL(pointAdded(int)), this, SLOT(updateControlPoints()));
109 QObject::connect(this,SIGNAL(pointRemoved(int)), this, SLOT(updateControlPoints()));
99 QObject::connect(this,SIGNAL(pointRemoved(int)), this, SLOT(updateControlPoints()));
110 QObject::connect(this,SIGNAL(pointReplaced(int)), this, SLOT(updateControlPoints()));
100 QObject::connect(this,SIGNAL(pointReplaced(int)), this, SLOT(updateControlPoints()));
111 };
101 };
112
102
113 /*!
103 /*!
114 Calculates control points which are needed by QPainterPath.cubicTo function to draw the cubic Bezier cureve between two points.
104 Calculates control points which are needed by QPainterPath.cubicTo function to draw the cubic Bezier cureve between two points.
115 */
105 */
116 void QSplineSeriesPrivate::calculateControlPoints()
106 void QSplineSeriesPrivate::calculateControlPoints()
117 {
107 {
118 Q_Q(QSplineSeries);
108 Q_Q(QSplineSeries);
119
109
120 const QList<QPointF>& points = q->points();
110 const QList<QPointF>& points = q->points();
121
111
122 int n = points.count() - 1;
112 int n = points.count() - 1;
123
113
124 if (n == 1)
114 if (n == 1)
125 {
115 {
126 //for n==1
116 //for n==1
127 m_controlPoints[0].setX((2 * points[0].x() + points[1].x()) / 3);
117 m_controlPoints[0].setX((2 * points[0].x() + points[1].x()) / 3);
128 m_controlPoints[0].setY((2 * points[0].y() + points[1].y()) / 3);
118 m_controlPoints[0].setY((2 * points[0].y() + points[1].y()) / 3);
129 m_controlPoints[1].setX(2 * m_controlPoints[0].x() - points[0].x());
119 m_controlPoints[1].setX(2 * m_controlPoints[0].x() - points[0].x());
130 m_controlPoints[1].setY(2 * m_controlPoints[0].y() - points[0].y());
120 m_controlPoints[1].setY(2 * m_controlPoints[0].y() - points[0].y());
131 return;
121 return;
132 }
122 }
133
123
134 // Calculate first Bezier control points
124 // Calculate first Bezier control points
135 // Right hand side vector
125 // Right hand side vector
136 // Set of equations for P0 to Pn points.
126 // Set of equations for P0 to Pn points.
137 //
127 //
138 // | 2 1 0 0 ... 0 0 0 ... 0 0 0 | | P1_1 | | P0 + 2 * P1 |
128 // | 2 1 0 0 ... 0 0 0 ... 0 0 0 | | P1_1 | | P0 + 2 * P1 |
139 // | 1 4 1 0 ... 0 0 0 ... 0 0 0 | | P1_2 | | 4 * P1 + 2 * P2 |
129 // | 1 4 1 0 ... 0 0 0 ... 0 0 0 | | P1_2 | | 4 * P1 + 2 * P2 |
140 // | 0 1 4 1 ... 0 0 0 ... 0 0 0 | | P1_3 | | 4 * P2 + 2 * P3 |
130 // | 0 1 4 1 ... 0 0 0 ... 0 0 0 | | P1_3 | | 4 * P2 + 2 * P3 |
141 // | . . . . . . . . . . . . | | ... | | ... |
131 // | . . . . . . . . . . . . | | ... | | ... |
142 // | 0 0 0 0 ... 1 4 1 ... 0 0 0 | * | P1_i | = | 4 * P(i-1) + 2 * Pi |
132 // | 0 0 0 0 ... 1 4 1 ... 0 0 0 | * | P1_i | = | 4 * P(i-1) + 2 * Pi |
143 // | . . . . . . . . . . . . | | ... | | ... |
133 // | . . . . . . . . . . . . | | ... | | ... |
144 // | 0 0 0 0 0 0 0 0 ... 1 4 1 | | P1_(n-1)| | 4 * P(n-2) + 2 * P(n-1) |
134 // | 0 0 0 0 0 0 0 0 ... 1 4 1 | | P1_(n-1)| | 4 * P(n-2) + 2 * P(n-1) |
145 // | 0 0 0 0 0 0 0 0 ... 0 2 7 | | P1_n | | 8 * P(n-1) + Pn |
135 // | 0 0 0 0 0 0 0 0 ... 0 2 7 | | P1_n | | 8 * P(n-1) + Pn |
146 //
136 //
147 QVector<qreal> vector;
137 QVector<qreal> vector;
148 vector.resize(n);
138 vector.resize(n);
149
139
150 vector[0] = points[0].x() + 2 * points[1].x();
140 vector[0] = points[0].x() + 2 * points[1].x();
151
141
152
142
153 for (int i = 1; i < n - 1; ++i){
143 for (int i = 1; i < n - 1; ++i){
154 vector[i] = 4 * points[i].x() + 2 * points[i + 1].x();
144 vector[i] = 4 * points[i].x() + 2 * points[i + 1].x();
155 }
145 }
156
146
157 vector[n - 1] = (8 * points[n-1].x() + points[n].x()) / 2.0;
147 vector[n - 1] = (8 * points[n-1].x() + points[n].x()) / 2.0;
158
148
159 QVector<qreal> xControl = firstControlPoints(vector);
149 QVector<qreal> xControl = firstControlPoints(vector);
160
150
161 vector[0] = points[0].y() + 2 * points[1].y();
151 vector[0] = points[0].y() + 2 * points[1].y();
162
152
163 for (int i = 1; i < n - 1; ++i) {
153 for (int i = 1; i < n - 1; ++i) {
164 vector[i] = 4 * points[i].y() + 2 * points[i + 1].y();
154 vector[i] = 4 * points[i].y() + 2 * points[i + 1].y();
165 }
155 }
166
156
167 vector[n - 1] = (8 * points[n-1].y() + points[n].y()) / 2.0;
157 vector[n - 1] = (8 * points[n-1].y() + points[n].y()) / 2.0;
168
158
169 QVector<qreal> yControl = firstControlPoints(vector);
159 QVector<qreal> yControl = firstControlPoints(vector);
170
160
171 for (int i = 0,j =0; i < n; ++i, ++j) {
161 for (int i = 0,j =0; i < n; ++i, ++j) {
172
162
173 m_controlPoints[j].setX(xControl[i]);
163 m_controlPoints[j].setX(xControl[i]);
174 m_controlPoints[j].setY(yControl[i]);
164 m_controlPoints[j].setY(yControl[i]);
175
165
176 j++;
166 j++;
177
167
178 if (i < n - 1){
168 if (i < n - 1){
179 m_controlPoints[j].setX(2 * points[i+1].x() - xControl[i + 1]);
169 m_controlPoints[j].setX(2 * points[i+1].x() - xControl[i + 1]);
180 m_controlPoints[j].setY(2 * points[i+1].y() - yControl[i + 1]);
170 m_controlPoints[j].setY(2 * points[i+1].y() - yControl[i + 1]);
181 }else{
171 }else{
182 m_controlPoints[j].setX((points[n].x() + xControl[n - 1]) / 2);
172 m_controlPoints[j].setX((points[n].x() + xControl[n - 1]) / 2);
183 m_controlPoints[j].setY((points[n].y() + yControl[n - 1]) / 2);
173 m_controlPoints[j].setY((points[n].y() + yControl[n - 1]) / 2);
184 }
174 }
185 }
175 }
186 }
176 }
187
177
188 QVector<qreal> QSplineSeriesPrivate::firstControlPoints(const QVector<qreal>& vector)
178 QVector<qreal> QSplineSeriesPrivate::firstControlPoints(const QVector<qreal>& vector)
189 {
179 {
190 QVector<qreal> result;
180 QVector<qreal> result;
191
181
192 int count = vector.count();
182 int count = vector.count();
193 result.resize(count);
183 result.resize(count);
194 result[0] = vector[0] / 2.0;
184 result[0] = vector[0] / 2.0;
195
185
196 QVector<qreal> temp;
186 QVector<qreal> temp;
197 temp.resize(count);
187 temp.resize(count);
198 temp[0] = 0;
188 temp[0] = 0;
199
189
200 qreal b = 2.0;
190 qreal b = 2.0;
201
191
202 for (int i = 1; i < count; i++) {
192 for (int i = 1; i < count; i++) {
203 temp[i] = 1 / b;
193 temp[i] = 1 / b;
204 b = (i < count - 1 ? 4.0 : 3.5) - temp[i];
194 b = (i < count - 1 ? 4.0 : 3.5) - temp[i];
205 result[i]=(vector[i] - result[i - 1]) / b;
195 result[i]=(vector[i] - result[i - 1]) / b;
206 }
196 }
207 for (int i = 1; i < count; i++)
197 for (int i = 1; i < count; i++)
208 result[count - i - 1] -= temp[count - i] * result[count - i];
198 result[count - i - 1] -= temp[count - i] * result[count - i];
209
199
210 return result;
200 return result;
211 }
201 }
212
202
213 QPointF QSplineSeriesPrivate::controlPoint(int index) const
203 QPointF QSplineSeriesPrivate::controlPoint(int index) const
214 {
204 {
215 // Q_D(const QSplineSeries);
205 // Q_D(const QSplineSeries);
216 // return d->m_controlPoints[index];
206 // return d->m_controlPoints[index];
217 return m_controlPoints[index];
207 return m_controlPoints[index];
218 }
208 }
219
209
220 /*!
210 /*!
221 Updates the control points, besed on currently avaiable knots.
211 Updates the control points, besed on currently avaiable knots.
222 */
212 */
223 void QSplineSeriesPrivate::updateControlPoints()
213 void QSplineSeriesPrivate::updateControlPoints()
224 {
214 {
225 Q_Q(QSplineSeries);
215 Q_Q(QSplineSeries);
226 if (q->count() > 1) {
216 if (q->count() > 1) {
227 m_controlPoints.resize(2*q->count()-2);
217 m_controlPoints.resize(2*q->count()-2);
228 calculateControlPoints();
218 calculateControlPoints();
229 }
219 }
230 }
220 }
231
221
222 void QSplineSeriesPrivate::mappingUpdated()
223 {
224 updateControlPoints();
225 emit updated();
226 }
227
232 void QSplineSeriesPrivate::modelRowsAdded(QModelIndex parent, int start, int end)
228 void QSplineSeriesPrivate::modelRowsAdded(QModelIndex parent, int start, int end)
233 {
229 {
234 updateControlPoints();
230 updateControlPoints();
235 QXYSeriesPrivate::modelRowsAdded(parent, start, end);
231 QXYSeriesPrivate::modelRowsAdded(parent, start, end);
236 }
232 }
237
233
238 void QSplineSeriesPrivate::modelRowsRemoved(QModelIndex parent, int start, int end)
234 void QSplineSeriesPrivate::modelRowsRemoved(QModelIndex parent, int start, int end)
239 {
235 {
240 updateControlPoints();
236 updateControlPoints();
241 QXYSeriesPrivate::modelRowsRemoved(parent, start, end);
237 QXYSeriesPrivate::modelRowsRemoved(parent, start, end);
242 }
238 }
243
239
244 void QSplineSeriesPrivate::modelColumnsAdded(QModelIndex parent, int start, int end)
240 void QSplineSeriesPrivate::modelColumnsAdded(QModelIndex parent, int start, int end)
245 {
241 {
246 updateControlPoints();
242 updateControlPoints();
247 QXYSeriesPrivate::modelColumnsAdded(parent, start, end);
243 QXYSeriesPrivate::modelColumnsAdded(parent, start, end);
248 }
244 }
249
245
250 void QSplineSeriesPrivate::modelColumnsRemoved(QModelIndex parent, int start, int end)
246 void QSplineSeriesPrivate::modelColumnsRemoved(QModelIndex parent, int start, int end)
251 {
247 {
252 updateControlPoints();
248 updateControlPoints();
253 QXYSeriesPrivate::modelColumnsRemoved(parent, start, end);
249 QXYSeriesPrivate::modelColumnsRemoved(parent, start, end);
254 }
250 }
255
251
256 Chart* QSplineSeriesPrivate::createGraphics(ChartPresenter* presenter)
252 Chart* QSplineSeriesPrivate::createGraphics(ChartPresenter* presenter)
257 {
253 {
258 Q_Q(QSplineSeries);
254 Q_Q(QSplineSeries);
259 SplineChartItem* spline = new SplineChartItem(q,presenter);
255 SplineChartItem* spline = new SplineChartItem(q,presenter);
260 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
256 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
261 presenter->animator()->addAnimation(spline);
257 presenter->animator()->addAnimation(spline);
262 }
258 }
263 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
259 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
264 return spline;
260 return spline;
265 }
261 }
266
262
267 #include "moc_qsplineseries.cpp"
263 #include "moc_qsplineseries.cpp"
268 #include "moc_qsplineseries_p.cpp"
264 #include "moc_qsplineseries_p.cpp"
269
265
270 QTCOMMERCIALCHART_END_NAMESPACE
266 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,56 +1,54
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #ifndef QSPLINESERIES_H
21 #ifndef QSPLINESERIES_H
22 #define QSPLINESERIES_H
22 #define QSPLINESERIES_H
23
23
24 #include <qchartglobal.h>
24 #include <qchartglobal.h>
25 #include <qlineseries.h>
25 #include <qlineseries.h>
26 #include <QList>
26 #include <QList>
27 #include <QPointF>
27 #include <QPointF>
28 #include <QtGlobal>
28 #include <QtGlobal>
29
29
30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31
31
32 class QSplineSeriesPrivate;
32 class QSplineSeriesPrivate;
33
33
34 class QTCOMMERCIALCHART_EXPORT QSplineSeries : public QLineSeries
34 class QTCOMMERCIALCHART_EXPORT QSplineSeries : public QLineSeries
35 {
35 {
36 Q_OBJECT
36 Q_OBJECT
37 public:
37 public:
38
38
39 explicit QSplineSeries(QObject *parent = 0);
39 explicit QSplineSeries(QObject *parent = 0);
40 ~QSplineSeries();
40 ~QSplineSeries();
41 QAbstractSeries::SeriesType type() const;
41 QAbstractSeries::SeriesType type() const;
42
42
43 // QPointF controlPoint(int index) const;
43 void setModel(QAbstractItemModel *model);
44
44 void setModelMapper(QXYModelMapper *mapper);
45 void setModelMapping(int modelX, int modelY, Qt::Orientation orientation = Qt::Vertical);
46 void setModelMappingRange(int first, int count = -1);
47
45
48 private:
46 private:
49 Q_DECLARE_PRIVATE(QSplineSeries);
47 Q_DECLARE_PRIVATE(QSplineSeries);
50 Q_DISABLE_COPY(QSplineSeries);
48 Q_DISABLE_COPY(QSplineSeries);
51 friend class SplineChartItem;
49 friend class SplineChartItem;
52 };
50 };
53
51
54 QTCOMMERCIALCHART_END_NAMESPACE
52 QTCOMMERCIALCHART_END_NAMESPACE
55
53
56 #endif // QSPLINESERIES_H
54 #endif // QSPLINESERIES_H
@@ -1,68 +1,69
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 // W A R N I N G
21 // W A R N I N G
22 // -------------
22 // -------------
23 //
23 //
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 // implementation detail. This header file may change from version to
25 // implementation detail. This header file may change from version to
26 // version without notice, or even be removed.
26 // version without notice, or even be removed.
27 //
27 //
28 // We mean it.
28 // We mean it.
29
29
30 #ifndef QSPLINESERIES_P_H
30 #ifndef QSPLINESERIES_P_H
31 #define QSPLINESERIES_P_H
31 #define QSPLINESERIES_P_H
32
32
33 #include "qlineseries_p.h"
33 #include "qlineseries_p.h"
34
34
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36
36
37
37
38 class QSplineSeriesPrivate: public QLineSeriesPrivate
38 class QSplineSeriesPrivate: public QLineSeriesPrivate
39 {
39 {
40 Q_OBJECT
40 Q_OBJECT
41 public:
41 public:
42 Chart* createGraphics(ChartPresenter* presenter);
42 Chart* createGraphics(ChartPresenter* presenter);
43 QSplineSeriesPrivate(QSplineSeries* q);
43 QSplineSeriesPrivate(QSplineSeries* q);
44
44
45 QPointF controlPoint(int index) const;
45 QPointF controlPoint(int index) const;
46
46
47 public Q_SLOTS:
47 public Q_SLOTS:
48 void updateControlPoints();
48 void updateControlPoints();
49
49
50 protected Q_SLOTS:
50 protected Q_SLOTS:
51 void modelRowsAdded(QModelIndex parent, int start, int end);
51 void modelRowsAdded(QModelIndex parent, int start, int end);
52 void modelRowsRemoved(QModelIndex parent, int start, int end);
52 void modelRowsRemoved(QModelIndex parent, int start, int end);
53 void modelColumnsAdded(QModelIndex parent, int start, int end);
53 void modelColumnsAdded(QModelIndex parent, int start, int end);
54 void modelColumnsRemoved(QModelIndex parent, int start, int end);
54 void modelColumnsRemoved(QModelIndex parent, int start, int end);
55 void mappingUpdated();
55
56
56 private:
57 private:
57 void calculateControlPoints();
58 void calculateControlPoints();
58 QVector<qreal> firstControlPoints(const QVector<qreal>& vector);
59 QVector<qreal> firstControlPoints(const QVector<qreal>& vector);
59
60
60 public:
61 public:
61 QVector<QPointF> m_controlPoints;
62 QVector<QPointF> m_controlPoints;
62 private:
63 private:
63 Q_DECLARE_PUBLIC(QSplineSeries)
64 Q_DECLARE_PUBLIC(QSplineSeries)
64 };
65 };
65
66
66 QTCOMMERCIALCHART_END_NAMESPACE
67 QTCOMMERCIALCHART_END_NAMESPACE
67
68
68 #endif
69 #endif
@@ -1,520 +1,518
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "qxyseries.h"
21 #include "qxyseries.h"
22 #include "qxyseries_p.h"
22 #include "qxyseries_p.h"
23 #include "domain_p.h"
23 #include "domain_p.h"
24 #include "legendmarker_p.h"
24 #include "legendmarker_p.h"
25 #include <QAbstractItemModel>
25 #include <QAbstractItemModel>
26 #include "qxymodelmapper.h"
26
27
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28
29
29 /*!
30 /*!
30 \class QXYSeries
31 \class QXYSeries
31 \brief The QXYSeries class is a base class for line, spline and scatter series.
32 \brief The QXYSeries class is a base class for line, spline and scatter series.
32 */
33 */
33
34
34 /*!
35 /*!
35 \fn QPen QXYSeries::pen() const
36 \fn QPen QXYSeries::pen() const
36 \brief Returns pen used to draw points for series.
37 \brief Returns pen used to draw points for series.
37 \sa setPen()
38 \sa setPen()
38 */
39 */
39
40
40 /*!
41 /*!
41 \fn QBrush QXYSeries::brush() const
42 \fn QBrush QXYSeries::brush() const
42 \brief Returns brush used to draw points for series.
43 \brief Returns brush used to draw points for series.
43 \sa setBrush()
44 \sa setBrush()
44 */
45 */
45
46
46 /*!
47 /*!
47 \fn void QXYSeries::clicked(const QPointF& point)
48 \fn void QXYSeries::clicked(const QPointF& point)
48 \brief Signal is emitted when user clicks the \a point on chart.
49 \brief Signal is emitted when user clicks the \a point on chart.
49 */
50 */
50
51
51
52
52 /*!
53 /*!
53 \fn void QXYSeriesPrivate::pointReplaced(int index)
54 \fn void QXYSeriesPrivate::pointReplaced(int index)
54 \brief \internal \a index
55 \brief \internal \a index
55 */
56 */
56
57
57 /*!
58 /*!
58 \fn void QXYSeriesPrivate::pointAdded(int index)
59 \fn void QXYSeriesPrivate::pointAdded(int index)
59 \brief \internal \a index
60 \brief \internal \a index
60 */
61 */
61
62
62 /*!
63 /*!
63 \fn void QXYSeriesPrivate::pointRemoved(int index)
64 \fn void QXYSeriesPrivate::pointRemoved(int index)
64 \brief \internal \a index
65 \brief \internal \a index
65 */
66 */
66
67
67 /*!
68 /*!
68 \fn void QXYSeriesPrivate::updated()
69 \fn void QXYSeriesPrivate::updated()
69 \brief \internal
70 \brief \internal
70 */
71 */
71
72
72 /*!
73 /*!
73 \internal
74 \internal
74
75
75 Constructs empty series object which is a child of \a parent.
76 Constructs empty series object which is a child of \a parent.
76 When series object is added to QChartView or QChart instance ownerships is transferred.
77 When series object is added to QChartView or QChart instance ownerships is transferred.
77 */
78 */
78 QXYSeries::QXYSeries(QXYSeriesPrivate &d,QObject *parent) : QAbstractSeries(d, parent)
79 QXYSeries::QXYSeries(QXYSeriesPrivate &d,QObject *parent) : QAbstractSeries(d, parent)
79 {
80 {
80
81
81 }
82 }
82 /*!
83 /*!
83 Destroys the object. Series added to QChartView or QChart instances are owned by those,
84 Destroys the object. Series added to QChartView or QChart instances are owned by those,
84 and are deleted when mentioned object are destroyed.
85 and are deleted when mentioned object are destroyed.
85 */
86 */
86 QXYSeries::~QXYSeries()
87 QXYSeries::~QXYSeries()
87 {
88 {
88 }
89 }
89
90
90 /*!
91 /*!
91 Adds data point \a x \a y to the series. Points are connected with lines on the chart.
92 Adds data point \a x \a y to the series. Points are connected with lines on the chart.
92 */
93 */
93 void QXYSeries::append(qreal x,qreal y)
94 void QXYSeries::append(qreal x,qreal y)
94 {
95 {
95 append(QPointF(x,y));
96 append(QPointF(x,y));
96 }
97 }
97
98
98 /*!
99 /*!
99 This is an overloaded function.
100 This is an overloaded function.
100 Adds data \a point to the series. Points are connected with lines on the chart.
101 Adds data \a point to the series. Points are connected with lines on the chart.
101 */
102 */
102 void QXYSeries::append(const QPointF &point)
103 void QXYSeries::append(const QPointF &point)
103 {
104 {
104 Q_D(QXYSeries);
105 Q_D(QXYSeries);
105 d->m_points<<point;
106 d->m_points<<point;
106 emit d->pointAdded(d->m_points.count()-1);
107 emit d->pointAdded(d->m_points.count()-1);
107 }
108 }
108
109
109 /*!
110 /*!
110 This is an overloaded function.
111 This is an overloaded function.
111 Adds list of data \a points to the series. Points are connected with lines on the chart.
112 Adds list of data \a points to the series. Points are connected with lines on the chart.
112 */
113 */
113 void QXYSeries::append(const QList<QPointF> &points)
114 void QXYSeries::append(const QList<QPointF> &points)
114 {
115 {
115 foreach(const QPointF& point , points) {
116 foreach(const QPointF& point , points) {
116 append(point);
117 append(point);
117 }
118 }
118 }
119 }
119
120
120
121
121 void QXYSeries::replace(qreal oldX,qreal oldY,qreal newX,qreal newY)
122 void QXYSeries::replace(qreal oldX,qreal oldY,qreal newX,qreal newY)
122 {
123 {
123 replace(QPointF(oldX,oldY),QPointF(newX,newY));
124 replace(QPointF(oldX,oldY),QPointF(newX,newY));
124 }
125 }
125
126
126 void QXYSeries::replace(const QPointF &oldPoint,const QPointF &newPoint)
127 void QXYSeries::replace(const QPointF &oldPoint,const QPointF &newPoint)
127 {
128 {
128 Q_D(QXYSeries);
129 Q_D(QXYSeries);
129 int index = d->m_points.indexOf(oldPoint);
130 int index = d->m_points.indexOf(oldPoint);
130 if(index==-1) return;
131 if(index==-1) return;
131 d->m_points[index] = newPoint;
132 d->m_points[index] = newPoint;
132 emit d->pointReplaced(index);
133 emit d->pointReplaced(index);
133 }
134 }
134
135
135 /*!
136 /*!
136 Removes current \a x and \a y value.
137 Removes current \a x and \a y value.
137 */
138 */
138 void QXYSeries::remove(qreal x,qreal y)
139 void QXYSeries::remove(qreal x,qreal y)
139 {
140 {
140 remove(QPointF(x,y));
141 remove(QPointF(x,y));
141 }
142 }
142
143
143 /*!
144 /*!
144 Removes current \a point x value. Note \a point y value is ignored.
145 Removes current \a point x value. Note \a point y value is ignored.
145 */
146 */
146 void QXYSeries::remove(const QPointF &point)
147 void QXYSeries::remove(const QPointF &point)
147 {
148 {
148 Q_D(QXYSeries);
149 Q_D(QXYSeries);
149 int index = d->m_points.indexOf(point);
150 int index = d->m_points.indexOf(point);
150 if(index==-1) return;
151 if(index==-1) return;
151 d->m_points.remove(index);
152 d->m_points.remove(index);
152 emit d->pointRemoved(index);
153 emit d->pointRemoved(index);
153 }
154 }
154
155
155 /*!
156 /*!
156 Removes all data points from the series.
157 Removes all data points from the series.
157 */
158 */
158 void QXYSeries::removeAll()
159 void QXYSeries::removeAll()
159 {
160 {
160 Q_D(QXYSeries);
161 Q_D(QXYSeries);
161 foreach(const QPointF& point, d->m_points) {
162 foreach(const QPointF& point, d->m_points) {
162 remove(point);
163 remove(point);
163 }
164 }
164 }
165 }
165
166
166 /*!
167 /*!
167 \internal \a pos
168 \internal \a pos
168 */
169 */
169 QList<QPointF> QXYSeries::points() const
170 QList<QPointF> QXYSeries::points() const
170 {
171 {
171 // Q_ASSERT(false);
172 // Q_ASSERT(false);
172 Q_D(const QXYSeries);
173 Q_D(const QXYSeries);
173 if (d->m_model) {
174 if (d->m_model && d->m_mapper) {
174 QList<QPointF> result;
175 QList<QPointF> result;
175 if (d->m_mapOrientation == Qt::Vertical){
176 if (d->m_mapper->orientation() == Qt::Vertical){
176 // consecutive data is read from model's column
177 // consecutive data is read from model's column
177 if (d->m_mapX >= d->m_model->columnCount() || d->m_mapY >= d->m_model->columnCount())
178 if (d->m_mapper->mapX() >= d->m_model->columnCount() || d->m_mapper->mapY() >= d->m_model->columnCount())
178 return result; // mapped columns are not existing
179 return result; // mapped columns are not existing
179
180
180 for(int i = d->m_mapFirst; i< d->m_mapFirst + count(); ++i) {
181 for(int i = d->m_mapper->first(); i< d->m_mapper->first() + count(); ++i) {
181 qreal x = d->m_model->data(d->m_model->index(i, d->m_mapX), Qt::DisplayRole).toReal();
182 qreal x = d->m_model->data(d->m_model->index(i, d->m_mapper->mapX()), Qt::DisplayRole).toReal();
182 qreal y = d->m_model->data(d->m_model->index(i, d->m_mapY), Qt::DisplayRole).toReal();
183 qreal y = d->m_model->data(d->m_model->index(i, d->m_mapper->mapY()), Qt::DisplayRole).toReal();
183 result << QPointF(x,y);
184 result << QPointF(x,y);
184 }
185 }
185 return result;
186 return result;
186 }
187 }
187 else{
188 else{
188 // consecutive data is read from model's row
189 // consecutive data is read from model's row
189 if (d->m_mapX >= d->m_model->rowCount() || d->m_mapY >= d->m_model->rowCount())
190 if (d->m_mapper->mapX() >= d->m_model->rowCount() || d->m_mapper->mapY() >= d->m_model->rowCount())
190 return result; // mapped rows are not existing
191 return result; // mapped rows are not existing
191
192
192 for(int i = d->m_mapFirst; i< d->m_mapFirst + count(); ++i) {
193 for(int i = d->m_mapper->first(); i< d->m_mapper->first() + count(); ++i) {
193 qreal x = d->m_model->data(d->m_model->index(d->m_mapX, i), Qt::DisplayRole).toReal();
194 qreal x = d->m_model->data(d->m_model->index(d->m_mapper->mapX(), i), Qt::DisplayRole).toReal();
194 qreal y = d->m_model->data(d->m_model->index(d->m_mapY, i), Qt::DisplayRole).toReal();
195 qreal y = d->m_model->data(d->m_model->index(d->m_mapper->mapY(), i), Qt::DisplayRole).toReal();
195 result << QPointF(x,y);
196 result << QPointF(x,y);
196 }
197 }
197 return result;
198 return result;
198 }
199 }
199 } else {
200 } else {
200 // model is not specified, return the data from series' internal data store
201 // model is not specified, return the data from series' internal data store
201 return d->m_points.toList();
202 return d->m_points.toList();
202 }
203 }
203 }
204 }
204
205
205 /*!
206 /*!
206 Returns number of data points within series.
207 Returns number of data points within series.
207 */
208 */
208 int QXYSeries::count() const
209 int QXYSeries::count() const
209 {
210 {
210 Q_D(const QXYSeries);
211 Q_D(const QXYSeries);
211
212
212 if (d->m_model) {
213 if (d->m_model && d->m_mapper) {
213 if (d->m_mapOrientation == Qt::Vertical) {
214
215 if (d->m_mapper->orientation() == Qt::Vertical) {
214 // data is in a column. Return the number of mapped items if the model's column have enough items
216 // data is in a column. Return the number of mapped items if the model's column have enough items
215 // or the number of items that can be mapped
217 // or the number of items that can be mapped
216 if (d->m_mapX >= d->m_model->columnCount() || d->m_mapY >= d->m_model->columnCount())
218 if (d->m_mapper->mapX() >= d->m_model->columnCount() || d->m_mapper->mapY() >= d->m_model->columnCount())
217 return 0; // mapped columns are not existing
219 return 0; // mapped columns are not existing
218 else if (d->m_mapCount != -1)
220 else if (d->m_mapper->count() != -1)
219 return qMin(d->m_mapCount, qMax(d->m_model->rowCount() - d->m_mapFirst, 0));
221 return qMin(d->m_mapper->count(), qMax(d->m_model->rowCount() - d->m_mapper->first(), 0));
220 else
222 else
221 return qMax(d->m_model->rowCount() - d->m_mapFirst, 0);
223 return qMax(d->m_model->rowCount() - d->m_mapper->first(), 0);
222 } else {
224 } else {
223 // data is in a row. Return the number of mapped items if the model's row have enough items
225 // data is in a row. Return the number of mapped items if the model's row have enough items
224 // or the number of items that can be mapped
226 // or the number of items that can be mapped
225 if (d->m_mapX >= d->m_model->rowCount() || d->m_mapY >= d->m_model->rowCount())
227 if (d->m_mapper->mapX() >= d->m_model->rowCount() || d->m_mapper->mapY() >= d->m_model->rowCount())
226 return 0; // mapped rows are not existing
228 return 0; // mapped rows are not existing
227 else if (d->m_mapCount != -1)
229 else if (d->m_mapper->count() != -1)
228 return qMin(d->m_mapCount, qMax(d->m_model->columnCount() - d->m_mapFirst, 0));
230 return qMin(d->m_mapper->count(), qMax(d->m_model->columnCount() - d->m_mapper->first(), 0));
229 else
231 else
230 return qMax(d->m_model->columnCount() - d->m_mapFirst, 0);
232 return qMax(d->m_model->columnCount() - d->m_mapper->first(), 0);
231 }
233 }
232 }
234 }
233
235
234 // model is not specified, return the number of points in the series internal data store
236 // model is not specified, return the number of points in the series internal data store
235 return d->m_points.count();
237 return d->m_points.count();
236 }
238 }
237
239
238
240
239 /*!
241 /*!
240 Sets \a pen used for drawing points on the chart. If the pen is not defined, the
242 Sets \a pen used for drawing points on the chart. If the pen is not defined, the
241 pen from chart theme is used.
243 pen from chart theme is used.
242 \sa QChart::setTheme()
244 \sa QChart::setTheme()
243 */
245 */
244 void QXYSeries::setPen(const QPen &pen)
246 void QXYSeries::setPen(const QPen &pen)
245 {
247 {
246 Q_D(QXYSeries);
248 Q_D(QXYSeries);
247 if (d->m_pen!=pen) {
249 if (d->m_pen!=pen) {
248 d->m_pen = pen;
250 d->m_pen = pen;
249 emit d->updated();
251 emit d->updated();
250 }
252 }
251 }
253 }
252
254
253 QPen QXYSeries::pen() const
255 QPen QXYSeries::pen() const
254 {
256 {
255 Q_D(const QXYSeries);
257 Q_D(const QXYSeries);
256 return d->m_pen;
258 return d->m_pen;
257 }
259 }
258
260
259 /*!
261 /*!
260 Sets \a brush used for drawing points on the chart. If the brush is not defined, brush
262 Sets \a brush used for drawing points on the chart. If the brush is not defined, brush
261 from chart theme setting is used.
263 from chart theme setting is used.
262 \sa QChart::setTheme()
264 \sa QChart::setTheme()
263 */
265 */
264 void QXYSeries::setBrush(const QBrush &brush)
266 void QXYSeries::setBrush(const QBrush &brush)
265 {
267 {
266 Q_D(QXYSeries);
268 Q_D(QXYSeries);
267 if (d->m_brush!=brush) {
269 if (d->m_brush!=brush) {
268 d->m_brush = brush;
270 d->m_brush = brush;
269 emit d->updated();
271 emit d->updated();
270 }
272 }
271 }
273 }
272
274
273 QBrush QXYSeries::brush() const
275 QBrush QXYSeries::brush() const
274 {
276 {
275 Q_D(const QXYSeries);
277 Q_D(const QXYSeries);
276 return d->m_brush;
278 return d->m_brush;
277 }
279 }
278
280
279
281
280 /*!
282 /*!
281 Sets if data points are \a visible and should be drawn on line.
283 Sets if data points are \a visible and should be drawn on line.
282 */
284 */
283 void QXYSeries::setPointsVisible(bool visible)
285 void QXYSeries::setPointsVisible(bool visible)
284 {
286 {
285 Q_D(QXYSeries);
287 Q_D(QXYSeries);
286 if (d->m_pointsVisible != visible){
288 if (d->m_pointsVisible != visible){
287 d->m_pointsVisible = visible;
289 d->m_pointsVisible = visible;
288 emit d->updated();
290 emit d->updated();
289 }
291 }
290 }
292 }
291
293
292 /*!
294 /*!
293 Returns true if drawing the data points of the series is enabled.
295 Returns true if drawing the data points of the series is enabled.
294 */
296 */
295 bool QXYSeries::pointsVisible() const
297 bool QXYSeries::pointsVisible() const
296 {
298 {
297 Q_D(const QXYSeries);
299 Q_D(const QXYSeries);
298 return d->m_pointsVisible;
300 return d->m_pointsVisible;
299 }
301 }
300
302
301
303
302 /*!
304 /*!
303 Stream operator for adding a data \a point to the series.
305 Stream operator for adding a data \a point to the series.
304 \sa append()
306 \sa append()
305 */
307 */
306 QXYSeries& QXYSeries::operator<< (const QPointF &point)
308 QXYSeries& QXYSeries::operator<< (const QPointF &point)
307 {
309 {
308 append(point);
310 append(point);
309 return *this;
311 return *this;
310 }
312 }
311
313
312
314
313 /*!
315 /*!
314 Stream operator for adding a list of \a points to the series.
316 Stream operator for adding a list of \a points to the series.
315 \sa append()
317 \sa append()
316 */
318 */
317
319
318 QXYSeries& QXYSeries::operator<< (const QList<QPointF>& points)
320 QXYSeries& QXYSeries::operator<< (const QList<QPointF>& points)
319 {
321 {
320 append(points);
322 append(points);
321 return *this;
323 return *this;
322 }
324 }
323
325
324 /*!
326 /*!
325 \fn bool QXYSeries::setModel(QAbstractItemModel *model)
327 \fn bool QXYSeries::setModel(QAbstractItemModel *model)
326 Sets the \a model to be used as a data source
328 Sets the \a model to be used as a data source
327 \sa setModelMapping()
329 \sa setModelMapping()
328 */
330 */
329 bool QXYSeries::setModel(QAbstractItemModel *model)
331 void QXYSeries::setModel(QAbstractItemModel *model)
330 {
332 {
331 Q_D(QXYSeries);
333 Q_D(QXYSeries);
332 // disconnect signals from old model
334 // disconnect signals from old model
333 if (d->m_model) {
335 if (d->m_model) {
334 QObject::disconnect(d->m_model, 0, this, 0);
336 QObject::disconnect(d->m_model, 0, this, 0);
335 d->m_mapX = -1;
336 d->m_mapY = -1;
337 d->m_mapFirst = 0;
338 d->m_mapCount = -1;
339 d->m_mapOrientation = Qt::Vertical;
340 }
337 }
341
338
342 // set new model
339 // set new model
343 if (model) {
340 if (model) {
344 d->m_model = model;
341 d->m_model = model;
345 return true;
342 if (d->m_mapper)
343 d->setMapping();
346 } else {
344 } else {
347 d->m_model = 0;
345 d->m_model = 0;
348 return false;
349 }
346 }
350 }
347 }
351
348
352 /*!
349 void QXYSeries::setModelMapper(QXYModelMapper *mapper)
353 Sets the \a modelX to be used as a data source for x coordinate and \a modelY to be used
354 as a data source for y coordinate. The \a orientation parameter specifies whether the data
355 is in columns or in rows.
356 \sa setModel()
357 */
358 void QXYSeries::setModelMapping(int modelX, int modelY, Qt::Orientation orientation)
359 {
350 {
360 Q_D(QXYSeries);
351 Q_D(QXYSeries);
361 if (d->m_model == 0)
352 // disconnect signals from old mapper
362 return;
353 if (d->m_mapper) {
363 d->m_mapX = modelX;
354 QObject::disconnect(d->m_mapper, 0, this, 0);
364 d->m_mapY = modelY;
355 }
365 d->m_mapOrientation = orientation;
366
367 // connect the signals from the model
368 connect(d->m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex)));
369 // if (d->m_mapOrientation == Qt::Vertical) {
370 connect(d->m_model,SIGNAL(rowsInserted(QModelIndex,int,int)), d, SLOT(modelRowsAdded(QModelIndex,int,int)));
371 connect(d->m_model,SIGNAL(rowsRemoved(QModelIndex,int,int)), d, SLOT(modelRowsRemoved(QModelIndex,int,int)));
372 // } else {
373 connect(d->m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), d, SLOT(modelColumnsAdded(QModelIndex,int,int)));
374 connect(d->m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), d, SLOT(modelColumnsRemoved(QModelIndex,int,int)));
375 // }
376 }
377
356
378 void QXYSeries::setModelMappingRange(int first, int count)
357 if (mapper) {
379 {
358 d->m_mapper = mapper;
380 Q_D(QXYSeries);
359 if (d->m_model)
381 d->m_mapFirst = qMax(first, 0);
360 d->setMapping();
382 d->m_mapCount = qMax(count, -1);
361 } else {
383 emit d->reinitialized();
362 d->m_mapper = 0;
363 }
384 }
364 }
385
365
386 int QXYSeries::mapX() const
366 QXYModelMapper* QXYSeries::modelMapper() const
387 {
367 {
388 Q_D(const QXYSeries);
368 Q_D(const QXYSeries);
389 return d->m_mapX;
369 return d->m_mapper;
390 }
370 }
391
371
392 int QXYSeries::mapY() const
372 /*!
393 {
373 Sets the \a modelX to be used as a data source for x coordinate and \a modelY to be used
394 Q_D(const QXYSeries);
374 as a data source for y coordinate. The \a orientation parameter specifies whether the data
395 return d->m_mapY;
375 is in columns or in rows.
376 \sa setModel()
377 */
396
378
397 }
398
379
399 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
380 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
400
381
401
382
402 QXYSeriesPrivate::QXYSeriesPrivate(QXYSeries *q) : QAbstractSeriesPrivate(q),
383 QXYSeriesPrivate::QXYSeriesPrivate(QXYSeries *q) : QAbstractSeriesPrivate(q),
403 m_mapX(-1),
384 m_mapper(0),
404 m_mapY(-1),
405 m_pointsVisible(false)
385 m_pointsVisible(false)
406 {
386 {
407 }
387 }
408
388
409 void QXYSeriesPrivate::scaleDomain(Domain& domain)
389 void QXYSeriesPrivate::scaleDomain(Domain& domain)
410 {
390 {
411 qreal minX(domain.minX());
391 qreal minX(domain.minX());
412 qreal minY(domain.minY());
392 qreal minY(domain.minY());
413 qreal maxX(domain.maxX());
393 qreal maxX(domain.maxX());
414 qreal maxY(domain.maxY());
394 qreal maxY(domain.maxY());
415 int tickXCount(domain.tickXCount());
395 int tickXCount(domain.tickXCount());
416 int tickYCount(domain.tickYCount());
396 int tickYCount(domain.tickYCount());
417
397
418 Q_Q(QXYSeries);
398 Q_Q(QXYSeries);
419
399
420 const QList<QPointF>& points = q->points();
400 const QList<QPointF>& points = q->points();
421
401
422 // if(points.isEmpty()){
402 // if(points.isEmpty()){
423 // minX=0.0;
403 // minX=0.0;
424 // minY=0.0;
404 // minY=0.0;
425 // maxX=1.0;
405 // maxX=1.0;
426 // maxY=1.0;
406 // maxY=1.0;
427 // }
407 // }
428
408
429 // for (int i = 0; i < points.count(); i++)
409 // for (int i = 0; i < points.count(); i++)
430 // {
410 // {
431 // qreal x = points[i].x();
411 // qreal x = points[i].x();
432 // qreal y = points[i].y();
412 // qreal y = points[i].y();
433 // minX = qMin(minX, x);
413 // minX = qMin(minX, x);
434 // minY = qMin(minY, y);
414 // minY = qMin(minY, y);
435 // maxX = qMax(maxX, x);
415 // maxX = qMax(maxX, x);
436 // maxY = qMax(maxY, y);
416 // maxY = qMax(maxY, y);
437 // }
417 // }
438
418
439 // domain.setRange(minX,maxX,minY,maxY,tickXCount,tickYCount);
419 // domain.setRange(minX,maxX,minY,maxY,tickXCount,tickYCount);
440
420
441 if (!points.isEmpty()) {
421 if (!points.isEmpty()) {
442 for (int i = 0; i < points.count(); i++) {
422 for (int i = 0; i < points.count(); i++) {
443 qreal x = points[i].x();
423 qreal x = points[i].x();
444 qreal y = points[i].y();
424 qreal y = points[i].y();
445 minX = qMin(minX, x);
425 minX = qMin(minX, x);
446 minY = qMin(minY, y);
426 minY = qMin(minY, y);
447 maxX = qMax(maxX, x);
427 maxX = qMax(maxX, x);
448 maxY = qMax(maxY, y);
428 maxY = qMax(maxY, y);
449 }
429 }
450 domain.setRange(minX,maxX,minY,maxY,tickXCount,tickYCount);
430 domain.setRange(minX,maxX,minY,maxY,tickXCount,tickYCount);
451 }
431 }
452 }
432 }
453
433
454 QList<LegendMarker*> QXYSeriesPrivate::createLegendMarker(QLegend* legend)
434 QList<LegendMarker*> QXYSeriesPrivate::createLegendMarker(QLegend* legend)
455 {
435 {
456 Q_Q(QXYSeries);
436 Q_Q(QXYSeries);
457 QList<LegendMarker*> list;
437 QList<LegendMarker*> list;
458 return list << new XYLegendMarker(q,legend);
438 return list << new XYLegendMarker(q,legend);
459 }
439 }
460
440
441 void QXYSeriesPrivate::setMapping()
442 {
443 // connect signals from the model
444 connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex,QModelIndex)));
445 connect(m_model,SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(modelRowsAdded(QModelIndex,int,int)));
446 connect(m_model,SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SLOT(modelRowsRemoved(QModelIndex,int,int)));
447 connect(m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), this, SLOT(modelColumnsAdded(QModelIndex,int,int)));
448 connect(m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), this, SLOT(modelColumnsRemoved(QModelIndex,int,int)));
449
450 // connect the signal from the mapper
451 connect(m_mapper, SIGNAL(updated()), this, SLOT(mappingUpdated()));
452 }
453
454 void QXYSeriesPrivate::mappingUpdated()
455 {
456 emit reinitialized();
457 }
458
461 void QXYSeriesPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
459 void QXYSeriesPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
462 {
460 {
463 for (int row = topLeft.row(); row <= bottomRight.row(); row++) {
461 for (int row = topLeft.row(); row <= bottomRight.row(); row++) {
464 for (int column = topLeft.column(); column <= bottomRight.column(); column++) {
462 for (int column = topLeft.column(); column <= bottomRight.column(); column++) {
465 if (m_mapOrientation == Qt::Vertical) {
463 if (m_mapper->orientation() == Qt::Vertical) {
466 if ((column == m_mapX || column == m_mapY) // modified item is in a mapped column
464 if ((column == m_mapper->mapX() || column == m_mapper->mapY()) // modified item is in a mapped column
467 && row >= m_mapFirst // modfied item in not before first item
465 && row >= m_mapper->first() // modfied item in not before first item
468 && (m_mapCount == -1 || row < m_mapFirst + m_mapCount)) // map is not limited or item lays before the end of map
466 && (m_mapper->count() == -1 || row < m_mapper->first() + m_mapper->count())) // map is not limited or item lays before the end of map
469 emit pointReplaced(row - m_mapFirst);
467 emit pointReplaced(row - m_mapper->first());
470 } else {
468 } else {
471 if ((row == m_mapX || row == m_mapY) // modified item is in a mapped row
469 if ((row == m_mapper->mapX() || row == m_mapper->mapY()) // modified item is in a mapped row
472 && column >= m_mapFirst // modfied item in not before first item
470 && column >= m_mapper->first() // modfied item in not before first item
473 && (m_mapCount == -1 || column < m_mapFirst + m_mapCount)) // map is not limited or item lays before the end of map
471 && (m_mapper->count() == -1 || column < m_mapper->first() + m_mapper->count())) // map is not limited or item lays before the end of map
474 emit pointReplaced(column - m_mapFirst);
472 emit pointReplaced(column - m_mapper->first());
475 }
473 }
476 }
474 }
477 }
475 }
478 }
476 }
479
477
480
478
481 void QXYSeriesPrivate::modelRowsAdded(QModelIndex parent, int start, int end)
479 void QXYSeriesPrivate::modelRowsAdded(QModelIndex parent, int start, int end)
482 {
480 {
483 Q_UNUSED(parent);
481 Q_UNUSED(parent);
484 if (m_mapOrientation == Qt::Vertical)
482 if (m_mapper->orientation() == Qt::Vertical)
485 emit pointsAdded(start, end);
483 emit pointsAdded(start, end);
486 else if (start <= m_mapX || start <= m_mapY)
484 else if (start <= m_mapper->mapX() || start <= m_mapper->mapY())
487 emit reinitialized();
485 emit reinitialized();
488 }
486 }
489
487
490 void QXYSeriesPrivate::modelRowsRemoved(QModelIndex parent, int start, int end)
488 void QXYSeriesPrivate::modelRowsRemoved(QModelIndex parent, int start, int end)
491 {
489 {
492 Q_UNUSED(parent);
490 Q_UNUSED(parent);
493 if (m_mapOrientation == Qt::Vertical)
491 if (m_mapper->orientation() == Qt::Vertical)
494 emit pointsRemoved(start, end);
492 emit pointsRemoved(start, end);
495 else if (start <= m_mapX || start <= m_mapY)
493 else if (start <= m_mapper->mapX() || start <= m_mapper->mapY())
496 emit reinitialized();
494 emit reinitialized();
497 }
495 }
498
496
499 void QXYSeriesPrivate::modelColumnsAdded(QModelIndex parent, int start, int end)
497 void QXYSeriesPrivate::modelColumnsAdded(QModelIndex parent, int start, int end)
500 {
498 {
501 Q_UNUSED(parent);
499 Q_UNUSED(parent);
502 if (m_mapOrientation == Qt::Horizontal)
500 if (m_mapper->orientation() == Qt::Horizontal)
503 emit pointsAdded(start, end);
501 emit pointsAdded(start, end);
504 else if (start <= m_mapX || start <= m_mapY)
502 else if (start <= m_mapper->mapX() || start <= m_mapper->mapY())
505 emit reinitialized();
503 emit reinitialized();
506 }
504 }
507
505
508 void QXYSeriesPrivate::modelColumnsRemoved(QModelIndex parent, int start, int end)
506 void QXYSeriesPrivate::modelColumnsRemoved(QModelIndex parent, int start, int end)
509 {
507 {
510 Q_UNUSED(parent);
508 Q_UNUSED(parent);
511 if (m_mapOrientation == Qt::Horizontal)
509 if (m_mapper->orientation() == Qt::Horizontal)
512 emit pointsRemoved(start, end);
510 emit pointsRemoved(start, end);
513 else if (start <= m_mapX || start <= m_mapY)
511 else if (start <= m_mapper->mapX() || start <= m_mapper->mapY())
514 emit reinitialized();
512 emit reinitialized();
515 }
513 }
516
514
517 #include "moc_qxyseries.cpp"
515 #include "moc_qxyseries.cpp"
518 #include "moc_qxyseries_p.cpp"
516 #include "moc_qxyseries_p.cpp"
519
517
520 QTCOMMERCIALCHART_END_NAMESPACE
518 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,85 +1,84
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #ifndef QXYSERIES_H
21 #ifndef QXYSERIES_H
22 #define QXYSERIES_H
22 #define QXYSERIES_H
23
23
24 #include <qchartglobal.h>
24 #include <qchartglobal.h>
25 #include <qabstractseries.h>
25 #include <qabstractseries.h>
26 #include <QPen>
26 #include <QPen>
27 #include <QBrush>
27 #include <QBrush>
28
28
29 class QModelIndex;
29 class QModelIndex;
30
30
31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32
32
33 class QXYSeriesPrivate;
33 class QXYSeriesPrivate;
34 class QXYModelMapper;
34
35
35 class QTCOMMERCIALCHART_EXPORT QXYSeries : public QAbstractSeries
36 class QTCOMMERCIALCHART_EXPORT QXYSeries : public QAbstractSeries
36 {
37 {
37 Q_OBJECT
38 Q_OBJECT
38 protected:
39 protected:
39 explicit QXYSeries(QXYSeriesPrivate &d,QObject *parent = 0);
40 explicit QXYSeries(QXYSeriesPrivate &d,QObject *parent = 0);
40 ~QXYSeries();
41 ~QXYSeries();
41
42
42 public:
43 public:
43 void append(qreal x, qreal y);
44 void append(qreal x, qreal y);
44 void append(const QPointF &point);
45 void append(const QPointF &point);
45 void append(const QList<QPointF> &points);
46 void append(const QList<QPointF> &points);
46 void replace(qreal oldX,qreal oldY,qreal newX,qreal newY);
47 void replace(qreal oldX,qreal oldY,qreal newX,qreal newY);
47 void replace(const QPointF &oldPoint,const QPointF &newPoint);
48 void replace(const QPointF &oldPoint,const QPointF &newPoint);
48 void remove(qreal x, qreal y);
49 void remove(qreal x, qreal y);
49 void remove(const QPointF &point);
50 void remove(const QPointF &point);
50 void removeAll();
51 void removeAll();
51
52
52 int count() const;
53 int count() const;
53 QList<QPointF> points() const;
54 QList<QPointF> points() const;
54
55
55 QXYSeries& operator << (const QPointF &point);
56 QXYSeries& operator << (const QPointF &point);
56 QXYSeries& operator << (const QList<QPointF> &points);
57 QXYSeries& operator << (const QList<QPointF> &points);
57
58
58 void setPen(const QPen &pen);
59 void setPen(const QPen &pen);
59 QPen pen() const;
60 QPen pen() const;
60
61
61 void setBrush(const QBrush &brush);
62 void setBrush(const QBrush &brush);
62 QBrush brush() const;
63 QBrush brush() const;
63
64
64 void setPointsVisible(bool visible = true);
65 void setPointsVisible(bool visible = true);
65 bool pointsVisible() const;
66 bool pointsVisible() const;
66
67
67 bool setModel(QAbstractItemModel *model);
68 void setModel(QAbstractItemModel *model);
68 virtual void setModelMapping(int modelX, int modelY, Qt::Orientation orientation = Qt::Vertical);
69 virtual void setModelMapper(QXYModelMapper *mapper);
69 virtual void setModelMappingRange(int first, int count = -1);
70 QXYModelMapper* modelMapper() const;
70 int mapX() const;
71 int mapY() const;
72
71
73 Q_SIGNALS:
72 Q_SIGNALS:
74 void clicked(const QPointF &point);
73 void clicked(const QPointF &point);
75
74
76 private:
75 private:
77 Q_DECLARE_PRIVATE(QXYSeries);
76 Q_DECLARE_PRIVATE(QXYSeries);
78 Q_DISABLE_COPY(QXYSeries);
77 Q_DISABLE_COPY(QXYSeries);
79 friend class XYLegendMarker;
78 friend class XYLegendMarker;
80 friend class XYChartItem;
79 friend class XYChartItem;
81 };
80 };
82
81
83 QTCOMMERCIALCHART_END_NAMESPACE
82 QTCOMMERCIALCHART_END_NAMESPACE
84
83
85 #endif
84 #endif
@@ -1,87 +1,88
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 // W A R N I N G
21 // W A R N I N G
22 // -------------
22 // -------------
23 //
23 //
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 // implementation detail. This header file may change from version to
25 // implementation detail. This header file may change from version to
26 // version without notice, or even be removed.
26 // version without notice, or even be removed.
27 //
27 //
28 // We mean it.
28 // We mean it.
29
29
30 #ifndef QXYSERIES_P_H
30 #ifndef QXYSERIES_P_H
31 #define QXYSERIES_P_H
31 #define QXYSERIES_P_H
32
32
33 #include "qabstractseries_p.h"
33 #include "qabstractseries_p.h"
34
34
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36
36
37 class QXYSeries;
37 class QXYSeries;
38 class QXYModelMapper;
38
39
39 class QXYSeriesPrivate: public QAbstractSeriesPrivate
40 class QXYSeriesPrivate: public QAbstractSeriesPrivate
40 {
41 {
41 Q_OBJECT
42 Q_OBJECT
42
43
43 public:
44 public:
44 QXYSeriesPrivate(QXYSeries* q);
45 QXYSeriesPrivate(QXYSeries* q);
45
46
46 void scaleDomain(Domain& domain);
47 void scaleDomain(Domain& domain);
47 QList<LegendMarker*> createLegendMarker(QLegend* legend);
48 QList<LegendMarker*> createLegendMarker(QLegend* legend);
48
49
49 protected Q_SLOTS:
50 protected Q_SLOTS:
50 void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight);
51 void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight);
51 virtual void modelRowsAdded(QModelIndex parent, int start, int end);
52 virtual void modelRowsAdded(QModelIndex parent, int start, int end);
52 virtual void modelRowsRemoved(QModelIndex parent, int start, int end);
53 virtual void modelRowsRemoved(QModelIndex parent, int start, int end);
53 virtual void modelColumnsAdded(QModelIndex parent, int start, int end);
54 virtual void modelColumnsAdded(QModelIndex parent, int start, int end);
54 virtual void modelColumnsRemoved(QModelIndex parent, int start, int end);
55 virtual void modelColumnsRemoved(QModelIndex parent, int start, int end);
56 virtual void mappingUpdated();
55
57
56 private:
58 private:
59 void setMapping();
57 void insertData(int start, int end);
60 void insertData(int start, int end);
58 void removeData(int start, int end);
61 void removeData(int start, int end);
59
62
60 Q_SIGNALS:
63 Q_SIGNALS:
61 void updated();
64 void updated();
62 void pointReplaced(int index);
65 void pointReplaced(int index);
63 void pointRemoved(int index);
66 void pointRemoved(int index);
64 void pointsRemoved(int start, int end);
67 void pointsRemoved(int start, int end);
65 void pointAdded(int index);
68 void pointAdded(int index);
66 void pointsAdded(int start, int end);
69 void pointsAdded(int start, int end);
67 void reinitialized();
70 void reinitialized();
68
71
69 protected:
72 protected:
70 QVector<QPointF> m_points;
73 QVector<QPointF> m_points;
71
74
72 QPen m_pen;
75 QPen m_pen;
73 QBrush m_brush;
76 QBrush m_brush;
74
77 QXYModelMapper* m_mapper;
75 int m_mapX;
76 int m_mapY;
77 bool m_pointsVisible;
78 bool m_pointsVisible;
78
79
79 private:
80 private:
80 Q_DECLARE_PUBLIC(QXYSeries);
81 Q_DECLARE_PUBLIC(QXYSeries);
81 friend class QScatterSeries;
82 friend class QScatterSeries;
82
83
83 };
84 };
84
85
85 QTCOMMERCIALCHART_END_NAMESPACE
86 QTCOMMERCIALCHART_END_NAMESPACE
86
87
87 #endif
88 #endif
@@ -1,14 +1,16
1 INCLUDEPATH += $$PWD
1 INCLUDEPATH += $$PWD
2 DEPENDPATH += $$PWD
2 DEPENDPATH += $$PWD
3
3
4 SOURCES += \
4 SOURCES += \
5 $$PWD/xychartitem.cpp \
5 $$PWD/xychartitem.cpp \
6 $$PWD/qxyseries.cpp
6 $$PWD/qxyseries.cpp \
7 $$PWD/qxymodelmapper.cpp
7
8
8 PRIVATE_HEADERS += \
9 PRIVATE_HEADERS += \
9 $$PWD/xychartitem_p.h \
10 $$PWD/xychartitem_p.h \
10 $$PWD/qxyseries_p.h
11 $$PWD/qxyseries_p.h
11
12
12
13
13 PUBLIC_HEADERS += \
14 PUBLIC_HEADERS += \
14 $$PWD/qxyseries.h No newline at end of file
15 $$PWD/qxyseries.h \
16 $$PWD/qxymodelmapper.h
@@ -1,271 +1,276
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "xychartitem_p.h"
21 #include "xychartitem_p.h"
22 #include "qxyseries.h"
22 #include "qxyseries.h"
23 #include "qxyseries_p.h"
23 #include "qxyseries_p.h"
24 #include "chartpresenter_p.h"
24 #include "chartpresenter_p.h"
25 #include "chartanimator_p.h"
25 #include "chartanimator_p.h"
26 #include <QPainter>
26 #include <QPainter>
27 #include <QGraphicsSceneMouseEvent>
27 #include <QGraphicsSceneMouseEvent>
28 #include <QAbstractItemModel>
28 #include <QAbstractItemModel>
29 #include "qxymodelmapper.h"
29
30
30
31
31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32
33
33 //TODO: optimize : remove points which are not visible
34 //TODO: optimize : remove points which are not visible
34
35
35 XYChartItem::XYChartItem(QXYSeries *series, ChartPresenter *presenter):ChartItem(presenter),
36 XYChartItem::XYChartItem(QXYSeries *series, ChartPresenter *presenter):ChartItem(presenter),
36 m_minX(0),
37 m_minX(0),
37 m_maxX(0),
38 m_maxX(0),
38 m_minY(0),
39 m_minY(0),
39 m_maxY(0),
40 m_maxY(0),
40 m_series(series)
41 m_series(series)
41 {
42 {
42 connect(series->d_func(),SIGNAL(pointReplaced(int)),this,SLOT(handlePointReplaced(int)));
43 connect(series->d_func(),SIGNAL(pointReplaced(int)),this,SLOT(handlePointReplaced(int)));
43 connect(series->d_func(),SIGNAL(pointAdded(int)),this,SLOT(handlePointAdded(int)));
44 connect(series->d_func(),SIGNAL(pointAdded(int)),this,SLOT(handlePointAdded(int)));
44 connect(series->d_func(),SIGNAL(pointsAdded(int, int)),this,SLOT(handlePointsAdded(int, int)));
45 connect(series->d_func(),SIGNAL(pointsAdded(int, int)),this,SLOT(handlePointsAdded(int, int)));
45 connect(series->d_func(),SIGNAL(pointRemoved(int)),this,SLOT(handlePointRemoved(int)));
46 connect(series->d_func(),SIGNAL(pointRemoved(int)),this,SLOT(handlePointRemoved(int)));
46 connect(series->d_func(),SIGNAL(pointsRemoved(int, int)),this,SLOT(handlePointsRemoved(int, int)));
47 connect(series->d_func(),SIGNAL(pointsRemoved(int, int)),this,SLOT(handlePointsRemoved(int, int)));
47 connect(series->d_func(),SIGNAL(reinitialized()),this,SLOT(handleReinitialized()));
48 connect(series->d_func(),SIGNAL(reinitialized()),this,SLOT(handleReinitialized()));
48 connect(this,SIGNAL(clicked(QPointF)),series,SIGNAL(clicked(QPointF)));
49 connect(this,SIGNAL(clicked(QPointF)),series,SIGNAL(clicked(QPointF)));
49 }
50 }
50
51
51 QPointF XYChartItem::calculateGeometryPoint(const QPointF &point) const
52 QPointF XYChartItem::calculateGeometryPoint(const QPointF &point) const
52 {
53 {
53 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
54 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
54 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
55 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
55 qreal x = (point.x() - m_minX)* deltaX;
56 qreal x = (point.x() - m_minX)* deltaX;
56 qreal y = (point.y() - m_minY)*-deltaY + m_size.height();
57 qreal y = (point.y() - m_minY)*-deltaY + m_size.height();
57 return QPointF(x,y);
58 return QPointF(x,y);
58 }
59 }
59
60
60
61
61 QPointF XYChartItem::calculateGeometryPoint(int index) const
62 QPointF XYChartItem::calculateGeometryPoint(int index) const
62 {
63 {
63 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
64 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
64 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
65 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
65 const QList<QPointF>& vector = m_series->points();
66 const QList<QPointF>& vector = m_series->points();
66 qreal x = (vector[index].x() - m_minX)* deltaX;
67 qreal x = (vector[index].x() - m_minX)* deltaX;
67 qreal y = (vector[index].y() - m_minY)*-deltaY + m_size.height();
68 qreal y = (vector[index].y() - m_minY)*-deltaY + m_size.height();
68 return QPointF(x,y);
69 return QPointF(x,y);
69 }
70 }
70
71
71 QVector<QPointF> XYChartItem::calculateGeometryPoints() const
72 QVector<QPointF> XYChartItem::calculateGeometryPoints() const
72 {
73 {
73 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
74 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
74 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
75 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
75
76
76 QVector<QPointF> result;
77 QVector<QPointF> result;
77 result.resize(m_series->count());
78 result.resize(m_series->count());
78 const QList<QPointF>& vector = m_series->points();
79 const QList<QPointF>& vector = m_series->points();
79 for (int i = 0; i < m_series->count(); ++i) {
80 for (int i = 0; i < m_series->count(); ++i) {
80 qreal x = (vector[i].x() - m_minX)* deltaX;
81 qreal x = (vector[i].x() - m_minX)* deltaX;
81 qreal y = (vector[i].y() - m_minY)*-deltaY + m_size.height();
82 qreal y = (vector[i].y() - m_minY)*-deltaY + m_size.height();
82 result[i].setX(x);
83 result[i].setX(x);
83 result[i].setY(y);
84 result[i].setY(y);
84 }
85 }
85 return result;
86 return result;
86 }
87 }
87
88
88 QPointF XYChartItem::calculateDomainPoint(const QPointF &point) const
89 QPointF XYChartItem::calculateDomainPoint(const QPointF &point) const
89 {
90 {
90 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
91 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
91 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
92 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
92 qreal x = point.x()/deltaX +m_minX;
93 qreal x = point.x()/deltaX +m_minX;
93 qreal y = (point.y()-m_size.height())/(-deltaY)+ m_minY;
94 qreal y = (point.y()-m_size.height())/(-deltaY)+ m_minY;
94 return QPointF(x,y);
95 return QPointF(x,y);
95 }
96 }
96
97
97 void XYChartItem::updateLayout(QVector<QPointF> &oldPoints, QVector<QPointF> &newPoints,int index)
98 void XYChartItem::updateLayout(QVector<QPointF> &oldPoints, QVector<QPointF> &newPoints,int index)
98 {
99 {
99 if (animator()) {
100 if (animator()) {
100 animator()->updateLayout(this,oldPoints,newPoints,index);
101 animator()->updateLayout(this,oldPoints,newPoints,index);
101 } else {
102 } else {
102 setLayout(newPoints);
103 setLayout(newPoints);
103 }
104 }
104 }
105 }
105
106
106 void XYChartItem::setLayout(QVector<QPointF> &points)
107 void XYChartItem::setLayout(QVector<QPointF> &points)
107 {
108 {
108 m_points = points;
109 m_points = points;
109 update();
110 update();
110 }
111 }
111
112
112 //handlers
113 //handlers
113
114
114 void XYChartItem::handlePointAdded(int index)
115 void XYChartItem::handlePointAdded(int index)
115 {
116 {
116 if (m_series->model() == 0) {
117 if (m_series->model() == 0) {
117 Q_ASSERT(index<m_series->count());
118 Q_ASSERT(index<m_series->count());
118 Q_ASSERT(index>=0);
119 Q_ASSERT(index>=0);
119 }
120 }
120 QVector<QPointF> points = m_points;
121 QVector<QPointF> points = m_points;
121 QPointF point;
122 QPointF point;
122 point = calculateGeometryPoint(index);
123 point = calculateGeometryPoint(index);
123 points.insert(index, point);
124 points.insert(index, point);
124 updateLayout(m_points, points, index);
125 updateLayout(m_points, points, index);
125 update();
126 update();
126 }
127 }
127
128
128 void XYChartItem::handlePointsAdded(int start, int end)
129 void XYChartItem::handlePointsAdded(int start, int end)
129 {
130 {
130 if (m_series->model() == 0) {
131 if (m_series->model() == 0) {
131 for (int i = start; i <= end; i++)
132 for (int i = start; i <= end; i++)
132 handlePointAdded(i);
133 handlePointAdded(i);
133 } else if (m_series->mapCount() != -1 && start >= m_series->mapFirst() + m_series->mapCount()) {
134 return;
135 } else {
134 } else {
136 int addedCount = end - start + 1;
135 int mapFirst = m_series->modelMapper()->first();
137 if (m_series->mapCount() != -1 && addedCount > m_series->mapCount())
136 int mapCount = m_series->modelMapper()->count();
138 addedCount = m_series->mapCount();
137 if (mapCount != -1 && start >= mapFirst + mapCount) {
139 int first = qMax(start, m_series->mapFirst()); // get the index of the first item that will be added
138 return;
140 int last = qMin(first + addedCount - 1, m_series->count() + m_series->mapFirst() - 1); // get the index of the last item that will be added
139 } else {
141 for (int i = first; i <= last; i++) {
140 int addedCount = end - start + 1;
142 handlePointAdded(i - m_series->mapFirst());
141 if (mapCount != -1 && addedCount > mapCount)
142 addedCount = mapCount;
143 int first = qMax(start, mapFirst); // get the index of the first item that will be added
144 int last = qMin(first + addedCount - 1, mapCount + mapFirst - 1); // get the index of the last item that will be added
145 for (int i = first; i <= last; i++) {
146 handlePointAdded(i - mapFirst);
147 }
148 // the map is limited therefore the items that are now outside the map
149 // need to be removed from the drawn points
150 if (mapCount != -1 && m_points.size() > mapCount)
151 for (int i = m_points.size() - 1; i >= mapCount; i--)
152 handlePointRemoved(i);
143 }
153 }
144 // the map is limited therefore the items that are now outside the map
145 // need to be removed from the drawn points
146 if (m_series->mapCount() != -1 && m_points.size() > m_series->mapCount())
147 for (int i = m_points.size() - 1; i >= m_series->mapCount(); i--)
148 handlePointRemoved(i);
149 }
154 }
150 }
155 }
151
156
152 void XYChartItem::handlePointRemoved(int index)
157 void XYChartItem::handlePointRemoved(int index)
153 {
158 {
154 if (m_series->model() == 0) {
159 if (m_series->model() == 0) {
155 Q_ASSERT(index<m_series->count() + 1);
160 Q_ASSERT(index<m_series->count() + 1);
156 Q_ASSERT(index>=0);
161 Q_ASSERT(index>=0);
157 }
162 }
158 QVector<QPointF> points = m_points;
163 QVector<QPointF> points = m_points;
159 points.remove(index);
164 points.remove(index);
160 updateLayout(m_points, points, index);
165 updateLayout(m_points, points, index);
161 update();
166 update();
162 }
167 }
163
168
164 void XYChartItem::handlePointsRemoved(int start, int end)
169 void XYChartItem::handlePointsRemoved(int start, int end)
165 {
170 {
166 Q_UNUSED(start)
171 Q_UNUSED(start)
167 Q_UNUSED(end)
172 Q_UNUSED(end)
168 if (m_series->model() == 0) {
173 if (m_series->model() == 0) {
169 for (int i = end; i >= start; i--)
174 for (int i = end; i >= start; i--)
170 handlePointRemoved(i);
175 handlePointRemoved(i);
171 } else {
176 } else {
172 // series uses model as a data source
177 // series uses model as a data source
173 int mapFirst = m_series->mapFirst();
178 int mapFirst = m_series->modelMapper()->first();
174 int mapCount = m_series->mapCount();
179 int mapCount = m_series->modelMapper()->count();
175 int removedCount = end - start + 1;
180 int removedCount = end - start + 1;
176 if (mapCount != -1 && start >= mapFirst + mapCount) {
181 if (mapCount != -1 && start >= mapFirst + mapCount) {
177 return;
182 return;
178 } else {
183 } else {
179 int toRemove = qMin(m_points.size(), removedCount); // first find how many items can actually be removed
184 int toRemove = qMin(m_points.size(), removedCount); // first find how many items can actually be removed
180 int first = qMax(start, mapFirst); // get the index of the first item that will be removed.
185 int first = qMax(start, mapFirst); // get the index of the first item that will be removed.
181 int last = qMin(first + toRemove - 1, m_points.size() + mapFirst - 1); // get the index of the last item that will be removed.
186 int last = qMin(first + toRemove - 1, m_points.size() + mapFirst - 1); // get the index of the last item that will be removed.
182 if (last - first == 0) {
187 if (last - first == 0) {
183 for (int i = last; i >= first; i--) {
188 for (int i = last; i >= first; i--) {
184 handlePointRemoved(i - mapFirst);
189 handlePointRemoved(i - mapFirst);
185
190
186 }
191 }
187 } else {
192 } else {
188 QVector<QPointF> points = m_points;
193 QVector<QPointF> points = m_points;
189 for (int i = last; i >= first; i--)
194 for (int i = last; i >= first; i--)
190 points.remove(i - mapFirst);
195 points.remove(i - mapFirst);
191 setLayout(points);
196 setLayout(points);
192 update();
197 update();
193 }
198 }
194 if (mapCount != -1) {
199 if (mapCount != -1) {
195 int itemsAvailable; // check how many are available to be added
200 int itemsAvailable; // check how many are available to be added
196 if (m_series->mapOrientation() == Qt::Vertical)
201 if (m_series->modelMapper()->orientation() == Qt::Vertical)
197 itemsAvailable = m_series->model()->rowCount() - mapFirst - m_points.size();
202 itemsAvailable = m_series->model()->rowCount() - mapFirst - m_points.size();
198 else
203 else
199 itemsAvailable = m_series->model()->columnCount() - mapFirst - m_points.size();
204 itemsAvailable = m_series->model()->columnCount() - mapFirst - m_points.size();
200 int toBeAdded = qMin(itemsAvailable, mapCount - m_points.size()); // add not more items than there is space left to be filled.
205 int toBeAdded = qMin(itemsAvailable, mapCount - m_points.size()); // add not more items than there is space left to be filled.
201 int currentSize = m_points.size();
206 int currentSize = m_points.size();
202 if (toBeAdded > 0)
207 if (toBeAdded > 0)
203 for (int i = m_points.size(); i < currentSize + toBeAdded; i++) {
208 for (int i = m_points.size(); i < currentSize + toBeAdded; i++) {
204 handlePointAdded(i);
209 handlePointAdded(i);
205 }
210 }
206 }
211 }
207 }
212 }
208 }
213 }
209
214
210 }
215 }
211
216
212 void XYChartItem::handlePointReplaced(int index)
217 void XYChartItem::handlePointReplaced(int index)
213 {
218 {
214 Q_ASSERT(index<m_series->count());
219 Q_ASSERT(index<m_series->count());
215 Q_ASSERT(index>=0);
220 Q_ASSERT(index>=0);
216 QPointF point = calculateGeometryPoint(index);
221 QPointF point = calculateGeometryPoint(index);
217 QVector<QPointF> points = m_points;
222 QVector<QPointF> points = m_points;
218 points.replace(index,point);
223 points.replace(index,point);
219 updateLayout(m_points,points,index);
224 updateLayout(m_points,points,index);
220 update();
225 update();
221 }
226 }
222
227
223 void XYChartItem::handleReinitialized()
228 void XYChartItem::handleReinitialized()
224 {
229 {
225 QVector<QPointF> points = calculateGeometryPoints();
230 QVector<QPointF> points = calculateGeometryPoints();
226 if (points.isEmpty())
231 if (points.isEmpty())
227 setLayout(points);
232 setLayout(points);
228 else
233 else
229 updateLayout(m_points,points);
234 updateLayout(m_points,points);
230 update();
235 update();
231 }
236 }
232
237
233 void XYChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
238 void XYChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
234 {
239 {
235 m_minX=minX;
240 m_minX=minX;
236 m_maxX=maxX;
241 m_maxX=maxX;
237 m_minY=minY;
242 m_minY=minY;
238 m_maxY=maxY;
243 m_maxY=maxY;
239 if (isEmpty()) return;
244 if (isEmpty()) return;
240 QVector<QPointF> points = calculateGeometryPoints();
245 QVector<QPointF> points = calculateGeometryPoints();
241 updateLayout(m_points,points);
246 updateLayout(m_points,points);
242 update();
247 update();
243 }
248 }
244
249
245 void XYChartItem::handleGeometryChanged(const QRectF &rect)
250 void XYChartItem::handleGeometryChanged(const QRectF &rect)
246 {
251 {
247 Q_ASSERT(rect.isValid());
252 Q_ASSERT(rect.isValid());
248 m_size=rect.size();
253 m_size=rect.size();
249 m_clipRect=rect.translated(-rect.topLeft());
254 m_clipRect=rect.translated(-rect.topLeft());
250 setPos(rect.topLeft());
255 setPos(rect.topLeft());
251
256
252 if (isEmpty()) return;
257 if (isEmpty()) return;
253 QVector<QPointF> points = calculateGeometryPoints();
258 QVector<QPointF> points = calculateGeometryPoints();
254 updateLayout(m_points,points);
259 updateLayout(m_points,points);
255 update();
260 update();
256 }
261 }
257
262
258
263
259 bool XYChartItem::isEmpty()
264 bool XYChartItem::isEmpty()
260 {
265 {
261 return !m_clipRect.isValid() || qFuzzyIsNull(m_maxX - m_minX) || qFuzzyIsNull(m_maxY - m_minY) || m_series->points().isEmpty();
266 return !m_clipRect.isValid() || qFuzzyIsNull(m_maxX - m_minX) || qFuzzyIsNull(m_maxY - m_minY) || m_series->points().isEmpty();
262 }
267 }
263
268
264 void XYChartItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
269 void XYChartItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
265 {
270 {
266 emit clicked(calculateDomainPoint(event->pos()));
271 emit clicked(calculateDomainPoint(event->pos()));
267 }
272 }
268
273
269 #include "moc_xychartitem_p.cpp"
274 #include "moc_xychartitem_p.cpp"
270
275
271 QTCOMMERCIALCHART_END_NAMESPACE
276 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,625 +1,631
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include <QtTest/QtTest>
21 #include <QtTest/QtTest>
22 #include <qlineseries.h>
22 #include <qlineseries.h>
23 #include <qchartview.h>
23 #include <qchartview.h>
24 #include <QStandardItemModel>
24 #include <QStandardItemModel>
25 #include <tst_definitions.h>
25 #include <tst_definitions.h>
26
26
27 Q_DECLARE_METATYPE(QList<QPointF>)
27 Q_DECLARE_METATYPE(QList<QPointF>)
28
28
29 QTCOMMERCIALCHART_USE_NAMESPACE
29 QTCOMMERCIALCHART_USE_NAMESPACE
30
30
31 class tst_QLineSeries : public QObject
31 class tst_QLineSeries : public QObject
32 {
32 {
33 Q_OBJECT
33 Q_OBJECT
34
34
35 public slots:
35 public slots:
36 void initTestCase();
36 void initTestCase();
37 void cleanupTestCase();
37 void cleanupTestCase();
38 void init();
38 void init();
39 void cleanup();
39 void cleanup();
40
40
41 private slots:
41 private slots:
42 void qlineseries_data();
42 void qlineseries_data();
43 void qlineseries();
43 void qlineseries();
44 void append_raw_data();
44 void append_raw_data();
45 void append_raw();
45 void append_raw();
46 void append_chart_data();
46 void append_chart_data();
47 void append_chart();
47 void append_chart();
48 void append_chart_animation_data();
48 void append_chart_animation_data();
49 void append_chart_animation();
49 void append_chart_animation();
50 void chart_append_data();
50 void chart_append_data();
51 void chart_append();
51 void chart_append();
52 void count_raw_data();
52 void count_raw_data();
53 void count_raw();
53 void count_raw();
54 void oper_data();
54 void oper_data();
55 void oper();
55 void oper();
56 void pen_data();
56 void pen_data();
57 void pen();
57 void pen();
58 void pointsVisible_raw_data();
58 void pointsVisible_raw_data();
59 void pointsVisible_raw();
59 void pointsVisible_raw();
60 void remove_raw_data();
60 void remove_raw_data();
61 void remove_raw();
61 void remove_raw();
62 void remove_chart_data();
62 void remove_chart_data();
63 void remove_chart();
63 void remove_chart();
64 void remove_chart_animation_data();
64 void remove_chart_animation_data();
65 void remove_chart_animation();
65 void remove_chart_animation();
66 void removeAll_raw_data();
66 void removeAll_raw_data();
67 void removeAll_raw();
67 void removeAll_raw();
68 void removeAll_chart_data();
68 void removeAll_chart_data();
69 void removeAll_chart();
69 void removeAll_chart();
70 void removeAll_chart_animation_data();
70 void removeAll_chart_animation_data();
71 void removeAll_chart_animation();
71 void removeAll_chart_animation();
72 void replace_raw_data();
72 void replace_raw_data();
73 void replace_raw();
73 void replace_raw();
74 void replace_chart_data();
74 void replace_chart_data();
75 void replace_chart();
75 void replace_chart();
76 void replace_chart_animation_data();
76 void replace_chart_animation_data();
77 void replace_chart_animation();
77 void replace_chart_animation();
78 void setModel_data();
78 void setModel_data();
79 void setModel();
79 void setModel();
80 void setModelMapping_data();
80 void setModelMapping_data();
81 void setModelMapping();
81 void setModelMapping();
82 void setModelMappingRange_data();
82 void setModelMappingRange_data();
83 void setModelMappingRange();
83 void setModelMappingRange();
84 void modelUpdated();
84 void modelUpdated();
85 void modelUpdatedCustomMapping();
85 void modelUpdatedCustomMapping();
86 private:
86 private:
87 void append_data();
87 void append_data();
88 void count_data();
88 void count_data();
89 void pointsVisible_data();
89 void pointsVisible_data();
90
90
91 private:
91 private:
92 QChartView* m_view;
92 QChartView* m_view;
93 QChart* m_chart;
93 QChart* m_chart;
94 QLineSeries* m_series;
94 QLineSeries* m_series;
95 };
95 };
96
96
97 void tst_QLineSeries::initTestCase()
97 void tst_QLineSeries::initTestCase()
98 {
98 {
99 }
99 }
100
100
101 void tst_QLineSeries::cleanupTestCase()
101 void tst_QLineSeries::cleanupTestCase()
102 {
102 {
103 }
103 }
104
104
105 void tst_QLineSeries::init()
105 void tst_QLineSeries::init()
106 {
106 {
107 m_view = new QChartView(new QChart());
107 m_view = new QChartView(new QChart());
108 m_chart = m_view->chart();
108 m_chart = m_view->chart();
109 m_series = new QLineSeries();
109 m_series = new QLineSeries();
110 }
110 }
111
111
112 void tst_QLineSeries::cleanup()
112 void tst_QLineSeries::cleanup()
113 {
113 {
114 delete m_series;
114 delete m_series;
115 delete m_view;
115 delete m_view;
116 m_view = 0;
116 m_view = 0;
117 m_chart = 0;
117 m_chart = 0;
118 m_series = 0;
118 m_series = 0;
119 }
119 }
120
120
121 void tst_QLineSeries::qlineseries_data()
121 void tst_QLineSeries::qlineseries_data()
122 {
122 {
123
123
124 }
124 }
125
125
126 void tst_QLineSeries::qlineseries()
126 void tst_QLineSeries::qlineseries()
127 {
127 {
128 QLineSeries series;
128 // QLineSeries series;
129
129
130 QCOMPARE(series.count(),0);
130 // QCOMPARE(series.count(),0);
131 QCOMPARE(series.brush(), QBrush());
131 // QCOMPARE(series.brush(), QBrush());
132 QCOMPARE(series.points(), QList<QPointF>());
132 // QCOMPARE(series.points(), QList<QPointF>());
133 QCOMPARE(series.pen(), QPen());
133 // QCOMPARE(series.pen(), QPen());
134 QCOMPARE(series.pointsVisible(), false);
134 // QCOMPARE(series.pointsVisible(), false);
135
135
136 series.append(QList<QPointF>());
136 // series.append(QList<QPointF>());
137 series.append(0.0,0.0);
137 // series.append(0.0,0.0);
138 series.append(QPointF());
138 // series.append(QPointF());
139
139
140 series.remove(0.0,0.0);
140 // series.remove(0.0,0.0);
141 series.remove(QPointF());
141 // series.remove(QPointF());
142 series.removeAll();
142 // series.removeAll();
143
143
144 series.replace(QPointF(),QPointF());
144 // series.replace(QPointF(),QPointF());
145 series.replace(0,0,0,0);
145 // series.replace(0,0,0,0);
146 series.setBrush(QBrush());
146 // series.setBrush(QBrush());
147
147
148 QCOMPARE(series.setModel((QAbstractItemModel*)0), false);
148 // QCOMPARE(series.setModel((QAbstractItemModel*)0), false);
149
149
150 series.setModelMapping(-1, -1, Qt::Orientation(0));
150 // series.setModelMapping(-1, -1, Qt::Orientation(0));
151
151
152 series.setPen(QPen());
152 // series.setPen(QPen());
153 series.setPointsVisible(false);
153 // series.setPointsVisible(false);
154
154
155 m_chart->addSeries(&series);
155 // m_chart->addSeries(&series);
156 m_view->show();
156 // m_view->show();
157 QTest::qWaitForWindowShown(m_view);
157 // QTest::qWaitForWindowShown(m_view);
158 }
158 }
159
159
160 void tst_QLineSeries::append_data()
160 void tst_QLineSeries::append_data()
161 {
161 {
162 QTest::addColumn< QList<QPointF> >("points");
162 QTest::addColumn< QList<QPointF> >("points");
163 QTest::newRow("0,0 1,1 2,2 3,3") << (QList<QPointF>() << QPointF(0,0) << QPointF(1,1) << QPointF(2,2) << QPointF(3,3));
163 QTest::newRow("0,0 1,1 2,2 3,3") << (QList<QPointF>() << QPointF(0,0) << QPointF(1,1) << QPointF(2,2) << QPointF(3,3));
164 QTest::newRow("0,0 -1,-1 -2,-2 -3,-3") << (QList<QPointF>() << QPointF(0,0) << QPointF(-1,-1) << QPointF(-2,-2) << QPointF(-3,-3));
164 QTest::newRow("0,0 -1,-1 -2,-2 -3,-3") << (QList<QPointF>() << QPointF(0,0) << QPointF(-1,-1) << QPointF(-2,-2) << QPointF(-3,-3));
165 }
165 }
166
166
167
167
168 void tst_QLineSeries::append_raw_data()
168 void tst_QLineSeries::append_raw_data()
169 {
169 {
170 append_data();
170 append_data();
171 }
171 }
172
172
173 void tst_QLineSeries::append_raw()
173 void tst_QLineSeries::append_raw()
174 {
174 {
175 QFETCH(QList<QPointF>, points);
175 QFETCH(QList<QPointF>, points);
176 QSignalSpy spy0(m_series, SIGNAL(clicked(QPointF const&)));
176 QSignalSpy spy0(m_series, SIGNAL(clicked(QPointF const&)));
177 m_series->append(points);
177 m_series->append(points);
178 TRY_COMPARE(spy0.count(), 0);
178 TRY_COMPARE(spy0.count(), 0);
179 QCOMPARE(m_series->points(), points);
179 QCOMPARE(m_series->points(), points);
180 }
180 }
181
181
182 void tst_QLineSeries::chart_append_data()
182 void tst_QLineSeries::chart_append_data()
183 {
183 {
184 append_data();
184 append_data();
185 }
185 }
186
186
187 void tst_QLineSeries::chart_append()
187 void tst_QLineSeries::chart_append()
188 {
188 {
189 append_raw();
189 append_raw();
190 m_chart->addSeries(m_series);
190 m_chart->addSeries(m_series);
191 m_view->show();
191 m_view->show();
192 QTest::qWaitForWindowShown(m_view);
192 QTest::qWaitForWindowShown(m_view);
193 }
193 }
194
194
195 void tst_QLineSeries::append_chart_data()
195 void tst_QLineSeries::append_chart_data()
196 {
196 {
197 append_data();
197 append_data();
198 }
198 }
199
199
200 void tst_QLineSeries::append_chart()
200 void tst_QLineSeries::append_chart()
201 {
201 {
202 m_view->show();
202 m_view->show();
203 m_chart->addSeries(m_series);
203 m_chart->addSeries(m_series);
204 QTest::qWaitForWindowShown(m_view);
204 QTest::qWaitForWindowShown(m_view);
205 append_raw();
205 append_raw();
206
206
207 }
207 }
208
208
209 void tst_QLineSeries::append_chart_animation_data()
209 void tst_QLineSeries::append_chart_animation_data()
210 {
210 {
211 append_data();
211 append_data();
212 }
212 }
213
213
214 void tst_QLineSeries::append_chart_animation()
214 void tst_QLineSeries::append_chart_animation()
215 {
215 {
216 m_chart->setAnimationOptions(QChart::AllAnimations);
216 m_chart->setAnimationOptions(QChart::AllAnimations);
217 append_chart();
217 append_chart();
218 }
218 }
219
219
220 void tst_QLineSeries::count_data()
220 void tst_QLineSeries::count_data()
221 {
221 {
222 QTest::addColumn<int>("count");
222 QTest::addColumn<int>("count");
223 QTest::newRow("0") << 0;
223 QTest::newRow("0") << 0;
224 QTest::newRow("5") << 5;
224 QTest::newRow("5") << 5;
225 QTest::newRow("10") << 5;
225 QTest::newRow("10") << 5;
226 }
226 }
227
227
228 void tst_QLineSeries::count_raw_data()
228 void tst_QLineSeries::count_raw_data()
229 {
229 {
230 count_data();
230 count_data();
231 }
231 }
232
232
233 void tst_QLineSeries::count_raw()
233 void tst_QLineSeries::count_raw()
234 {
234 {
235 QFETCH(int, count);
235 QFETCH(int, count);
236
236
237 QSignalSpy spy0(m_series, SIGNAL(clicked(QPointF const&)));
237 QSignalSpy spy0(m_series, SIGNAL(clicked(QPointF const&)));
238
238
239 for(int i=0 ; i< count; ++i)
239 for(int i=0 ; i< count; ++i)
240 m_series->append(i,i);
240 m_series->append(i,i);
241
241
242 TRY_COMPARE(spy0.count(), 0);
242 TRY_COMPARE(spy0.count(), 0);
243 QCOMPARE(m_series->count(), count);
243 QCOMPARE(m_series->count(), count);
244 }
244 }
245
245
246 void tst_QLineSeries::oper_data()
246 void tst_QLineSeries::oper_data()
247 {
247 {
248 append_data();
248 append_data();
249 }
249 }
250
250
251 void tst_QLineSeries::oper()
251 void tst_QLineSeries::oper()
252 {
252 {
253 QFETCH(QList<QPointF>, points);
253 QFETCH(QList<QPointF>, points);
254 QLineSeries series;
254 QLineSeries series;
255
255
256 QSignalSpy spy0(&series, SIGNAL(clicked(QPointF const&)));
256 QSignalSpy spy0(&series, SIGNAL(clicked(QPointF const&)));
257
257
258 foreach(const QPointF& point,points)
258 foreach(const QPointF& point,points)
259 {
259 {
260 series<<point;
260 series<<point;
261 }
261 }
262
262
263 QCOMPARE(series.points(), points);
263 QCOMPARE(series.points(), points);
264 TRY_COMPARE(spy0.count(), 0);
264 TRY_COMPARE(spy0.count(), 0);
265 }
265 }
266
266
267
267
268 void tst_QLineSeries::pen_data()
268 void tst_QLineSeries::pen_data()
269 {
269 {
270 QTest::addColumn<QPen>("pen");
270 QTest::addColumn<QPen>("pen");
271 QTest::newRow("null") << QPen();
271 QTest::newRow("null") << QPen();
272 QTest::newRow("blue") << QPen(Qt::blue);
272 QTest::newRow("blue") << QPen(Qt::blue);
273 QTest::newRow("black") << QPen(Qt::black);
273 QTest::newRow("black") << QPen(Qt::black);
274 QTest::newRow("red") << QPen(Qt::red);
274 QTest::newRow("red") << QPen(Qt::red);
275 }
275 }
276
276
277 void tst_QLineSeries::pen()
277 void tst_QLineSeries::pen()
278 {
278 {
279 QFETCH(QPen, pen);
279 QFETCH(QPen, pen);
280 QLineSeries series;
280 QLineSeries series;
281
281
282 QSignalSpy spy0(&series, SIGNAL(clicked(QPointF const&)));
282 QSignalSpy spy0(&series, SIGNAL(clicked(QPointF const&)));
283 series.setPen(pen);
283 series.setPen(pen);
284
284
285 TRY_COMPARE(spy0.count(), 0);
285 TRY_COMPARE(spy0.count(), 0);
286 QCOMPARE(series.pen(), pen);
286 QCOMPARE(series.pen(), pen);
287
287
288 m_chart->addSeries(&series);
288 m_chart->addSeries(&series);
289
289
290 if(pen!=QPen()) QCOMPARE(series.pen(), pen);
290 if(pen!=QPen()) QCOMPARE(series.pen(), pen);
291
291
292 m_chart->setTheme(QChart::ChartThemeDark);
292 m_chart->setTheme(QChart::ChartThemeDark);
293
293
294 if(pen!=QPen()) QCOMPARE(series.pen(), pen);
294 if(pen!=QPen()) QCOMPARE(series.pen(), pen);
295 }
295 }
296
296
297 void tst_QLineSeries::pointsVisible_data()
297 void tst_QLineSeries::pointsVisible_data()
298 {
298 {
299 QTest::addColumn<bool>("pointsVisible");
299 QTest::addColumn<bool>("pointsVisible");
300 QTest::newRow("true") << true;
300 QTest::newRow("true") << true;
301 QTest::newRow("false") << false;
301 QTest::newRow("false") << false;
302 }
302 }
303
303
304 void tst_QLineSeries::pointsVisible_raw_data()
304 void tst_QLineSeries::pointsVisible_raw_data()
305 {
305 {
306 pointsVisible_data();
306 pointsVisible_data();
307 }
307 }
308
308
309 void tst_QLineSeries::pointsVisible_raw()
309 void tst_QLineSeries::pointsVisible_raw()
310 {
310 {
311 QFETCH(bool, pointsVisible);
311 QFETCH(bool, pointsVisible);
312 QSignalSpy spy0(m_series, SIGNAL(clicked(QPointF const&)));
312 QSignalSpy spy0(m_series, SIGNAL(clicked(QPointF const&)));
313 m_series->setPointsVisible(pointsVisible);
313 m_series->setPointsVisible(pointsVisible);
314 TRY_COMPARE(spy0.count(), 0);
314 TRY_COMPARE(spy0.count(), 0);
315 QCOMPARE(m_series->pointsVisible(), pointsVisible);
315 QCOMPARE(m_series->pointsVisible(), pointsVisible);
316 }
316 }
317
317
318 void tst_QLineSeries::remove_raw_data()
318 void tst_QLineSeries::remove_raw_data()
319 {
319 {
320 append_data();
320 append_data();
321 }
321 }
322
322
323 void tst_QLineSeries::remove_raw()
323 void tst_QLineSeries::remove_raw()
324 {
324 {
325 QFETCH(QList<QPointF>, points);
325 QFETCH(QList<QPointF>, points);
326 QSignalSpy spy0(m_series, SIGNAL(clicked(QPointF const&)));
326 QSignalSpy spy0(m_series, SIGNAL(clicked(QPointF const&)));
327 m_series->append(points);
327 m_series->append(points);
328 TRY_COMPARE(spy0.count(), 0);
328 TRY_COMPARE(spy0.count(), 0);
329 QCOMPARE(m_series->points(), points);
329 QCOMPARE(m_series->points(), points);
330
330
331 foreach(const QPointF& point,points)
331 foreach(const QPointF& point,points)
332 m_series->remove(point);
332 m_series->remove(point);
333
333
334 TRY_COMPARE(spy0.count(), 0);
334 TRY_COMPARE(spy0.count(), 0);
335 QCOMPARE(m_series->points().count(), 0);
335 QCOMPARE(m_series->points().count(), 0);
336 }
336 }
337
337
338 void tst_QLineSeries::remove_chart_data()
338 void tst_QLineSeries::remove_chart_data()
339 {
339 {
340 append_data();
340 append_data();
341 }
341 }
342
342
343 void tst_QLineSeries::remove_chart()
343 void tst_QLineSeries::remove_chart()
344 {
344 {
345 m_view->show();
345 m_view->show();
346 m_chart->addSeries(m_series);
346 m_chart->addSeries(m_series);
347 QTest::qWaitForWindowShown(m_view);
347 QTest::qWaitForWindowShown(m_view);
348 remove_raw();
348 remove_raw();
349 }
349 }
350
350
351 void tst_QLineSeries::remove_chart_animation_data()
351 void tst_QLineSeries::remove_chart_animation_data()
352 {
352 {
353 append_data();
353 append_data();
354 }
354 }
355
355
356 void tst_QLineSeries::remove_chart_animation()
356 void tst_QLineSeries::remove_chart_animation()
357 {
357 {
358 m_chart->setAnimationOptions(QChart::AllAnimations);
358 m_chart->setAnimationOptions(QChart::AllAnimations);
359 remove_chart();
359 remove_chart();
360 }
360 }
361
361
362
362
363 void tst_QLineSeries::removeAll_raw_data()
363 void tst_QLineSeries::removeAll_raw_data()
364 {
364 {
365 append_data();
365 append_data();
366 }
366 }
367
367
368 void tst_QLineSeries::removeAll_raw()
368 void tst_QLineSeries::removeAll_raw()
369 {
369 {
370 QFETCH(QList<QPointF>, points);
370 QFETCH(QList<QPointF>, points);
371 QSignalSpy spy0(m_series, SIGNAL(clicked(QPointF const&)));
371 QSignalSpy spy0(m_series, SIGNAL(clicked(QPointF const&)));
372 m_series->append(points);
372 m_series->append(points);
373 TRY_COMPARE(spy0.count(), 0);
373 TRY_COMPARE(spy0.count(), 0);
374 QCOMPARE(m_series->points(), points);
374 QCOMPARE(m_series->points(), points);
375 m_series->removeAll();
375 m_series->removeAll();
376 TRY_COMPARE(spy0.count(), 0);
376 TRY_COMPARE(spy0.count(), 0);
377 QCOMPARE(m_series->points().count(), 0);
377 QCOMPARE(m_series->points().count(), 0);
378 }
378 }
379
379
380 void tst_QLineSeries::removeAll_chart_data()
380 void tst_QLineSeries::removeAll_chart_data()
381 {
381 {
382 append_data();
382 append_data();
383 }
383 }
384
384
385 void tst_QLineSeries::removeAll_chart()
385 void tst_QLineSeries::removeAll_chart()
386 {
386 {
387 m_view->show();
387 m_view->show();
388 m_chart->addSeries(m_series);
388 m_chart->addSeries(m_series);
389 QTest::qWaitForWindowShown(m_view);
389 QTest::qWaitForWindowShown(m_view);
390 removeAll_raw();
390 removeAll_raw();
391 }
391 }
392
392
393 void tst_QLineSeries::removeAll_chart_animation_data()
393 void tst_QLineSeries::removeAll_chart_animation_data()
394 {
394 {
395 append_data();
395 append_data();
396 }
396 }
397
397
398 void tst_QLineSeries::removeAll_chart_animation()
398 void tst_QLineSeries::removeAll_chart_animation()
399 {
399 {
400 m_chart->setAnimationOptions(QChart::AllAnimations);
400 m_chart->setAnimationOptions(QChart::AllAnimations);
401 removeAll_chart();
401 removeAll_chart();
402 }
402 }
403
403
404 void tst_QLineSeries::replace_raw_data()
404 void tst_QLineSeries::replace_raw_data()
405 {
405 {
406 append_data();
406 append_data();
407 }
407 }
408
408
409 void tst_QLineSeries::replace_raw()
409 void tst_QLineSeries::replace_raw()
410 {
410 {
411 QFETCH(QList<QPointF>, points);
411 QFETCH(QList<QPointF>, points);
412 QSignalSpy spy0(m_series, SIGNAL(clicked(QPointF const&)));
412 QSignalSpy spy0(m_series, SIGNAL(clicked(QPointF const&)));
413 m_series->append(points);
413 m_series->append(points);
414 TRY_COMPARE(spy0.count(), 0);
414 TRY_COMPARE(spy0.count(), 0);
415 QCOMPARE(m_series->points(), points);
415 QCOMPARE(m_series->points(), points);
416
416
417 foreach(const QPointF& point,points)
417 foreach(const QPointF& point,points)
418 m_series->replace(point.x(),point.y(),point.x(),0);
418 m_series->replace(point.x(),point.y(),point.x(),0);
419
419
420 QList<QPointF> newPoints = m_series->points();
420 QList<QPointF> newPoints = m_series->points();
421
421
422 QCOMPARE(newPoints.count(), points.count());
422 QCOMPARE(newPoints.count(), points.count());
423
423
424 for(int i =0 ; i<points.count() ; ++i) {
424 for(int i =0 ; i<points.count() ; ++i) {
425 QCOMPARE(points[i].x(), newPoints[i].x());
425 QCOMPARE(points[i].x(), newPoints[i].x());
426 QCOMPARE(newPoints[i].y(), 0.0);
426 QCOMPARE(newPoints[i].y(), 0.0);
427 }
427 }
428 }
428 }
429
429
430
430
431 void tst_QLineSeries::replace_chart_data()
431 void tst_QLineSeries::replace_chart_data()
432 {
432 {
433 append_data();
433 append_data();
434 }
434 }
435
435
436 void tst_QLineSeries::replace_chart()
436 void tst_QLineSeries::replace_chart()
437 {
437 {
438 m_view->show();
438 m_view->show();
439 m_chart->addSeries(m_series);
439 m_chart->addSeries(m_series);
440 QTest::qWaitForWindowShown(m_view);
440 QTest::qWaitForWindowShown(m_view);
441 replace_raw();
441 replace_raw();
442 }
442 }
443
443
444 void tst_QLineSeries::replace_chart_animation_data()
444 void tst_QLineSeries::replace_chart_animation_data()
445 {
445 {
446 append_data();
446 append_data();
447 }
447 }
448
448
449 void tst_QLineSeries::replace_chart_animation()
449 void tst_QLineSeries::replace_chart_animation()
450 {
450 {
451 m_chart->setAnimationOptions(QChart::AllAnimations);
451 m_chart->setAnimationOptions(QChart::AllAnimations);
452 replace_chart();
452 replace_chart();
453 }
453 }
454
454
455 void tst_QLineSeries::setModel_data()
455 void tst_QLineSeries::setModel_data()
456 {
456 {
457
457
458 }
458 }
459
459
460 void tst_QLineSeries::setModel()
460 void tst_QLineSeries::setModel()
461 {
461 {
462 QLineSeries series;
462 QLineSeries series;
463 series.setModel(0);
463 series.setModel(0);
464 QVERIFY2(series.model() == 0, "Model should be unset");
464 QVERIFY2(series.model() == 0, "Model should be unset");
465
465
466 QStandardItemModel *stdModel = new QStandardItemModel();
466 QStandardItemModel *stdModel = new QStandardItemModel();
467 series.setModel(stdModel);
467 series.setModel(stdModel);
468 QVERIFY2((series.model()) == stdModel, "Model should be stdModel");
468 QVERIFY2((series.model()) == stdModel, "Model should be stdModel");
469
469
470 // unset the model
470 // unset the model
471 series.setModel(0);
471 series.setModel(0);
472 QVERIFY2(series.model() == 0, "Model should be unset");
472 QVERIFY2(series.model() == 0, "Model should be unset");
473
473
474 }
474 }
475
475
476 Q_DECLARE_METATYPE(Qt::Orientation)
476 Q_DECLARE_METATYPE(Qt::Orientation)
477 void tst_QLineSeries::setModelMapping_data()
477 void tst_QLineSeries::setModelMapping_data()
478 {
478 {
479 QTest::addColumn<int>("modelX");
479 QTest::addColumn<int>("modelX");
480 QTest::addColumn<int>("modelY");
480 QTest::addColumn<int>("modelY");
481 QTest::addColumn<Qt::Orientation>("orientation");
481 QTest::addColumn<Qt::Orientation>("orientation");
482 QTest::newRow("different x and y, vertical") << 0 << 1 << Qt::Vertical;
482 QTest::newRow("different x and y, vertical") << 0 << 1 << Qt::Vertical;
483 QTest::newRow("same x and y, vertical") << 0 << 0 << Qt::Vertical;
483 QTest::newRow("same x and y, vertical") << 0 << 0 << Qt::Vertical;
484 QTest::newRow("invalid x, corrent y, vertical") << -1 << 1 << Qt::Vertical;
484 QTest::newRow("invalid x, corrent y, vertical") << -1 << 1 << Qt::Vertical;
485
485
486 QTest::newRow("different x and y, horizontal") << 0 << 1 << Qt::Horizontal;
486 QTest::newRow("different x and y, horizontal") << 0 << 1 << Qt::Horizontal;
487 QTest::newRow("same x and y, horizontal") << 0 << 0 << Qt::Horizontal;
487 QTest::newRow("same x and y, horizontal") << 0 << 0 << Qt::Horizontal;
488 QTest::newRow("invalid x, corrent y, horizontal") << -1 << 1 << Qt::Horizontal;
488 QTest::newRow("invalid x, corrent y, horizontal") << -1 << 1 << Qt::Horizontal;
489 }
489 }
490
490
491 void tst_QLineSeries::setModelMapping()
491 void tst_QLineSeries::setModelMapping()
492 {
492 {
493 QSKIP("Model mapping has been rewriten, test case needs update", SkipAll);
494
493 QFETCH(int, modelX);
495 QFETCH(int, modelX);
494 QFETCH(int, modelY);
496 QFETCH(int, modelY);
495 QFETCH(Qt::Orientation, orientation);
497 QFETCH(Qt::Orientation, orientation);
496
498
497 QLineSeries series;
499 // QLineSeries series;
498
500
499 // model has not been set so setting mapping should do nothing
501 // // model has not been set so setting mapping should do nothing
500 series.setModelMapping(modelX, modelY, orientation);
502 // series.setModelMapping(modelX, modelY, orientation);
501 QCOMPARE(series.mapX(), -1);
503 // QCOMPARE(series.mapX(), -1);
502 QCOMPARE(series.mapY(), -1);
504 // QCOMPARE(series.mapY(), -1);
503 QVERIFY2(series.mapOrientation() == Qt::Vertical, "The orientation by default should be Qt::Vertical");
505 // QVERIFY2(series.mapOrientation() == Qt::Vertical, "The orientation by default should be Qt::Vertical");
504
506
505 // now let us set the model
507 // // now let us set the model
506 series.setModel(new QStandardItemModel());
508 // series.setModel(new QStandardItemModel());
507 series.setModelMapping(modelX, modelY, orientation);
509 // series.setModelMapping(modelX, modelY, orientation);
508 QCOMPARE(series.mapX(), modelX);
510 // QCOMPARE(series.mapX(), modelX);
509 QCOMPARE(series.mapY(), modelY);
511 // QCOMPARE(series.mapY(), modelY);
510 QVERIFY2(series.mapOrientation() == orientation, "not good");
512 // QVERIFY2(series.mapOrientation() == orientation, "not good");
511
513
512 // now let us remove the model, the values should go back to default ones.
514 // // now let us remove the model, the values should go back to default ones.
513 series.setModel(0);
515 // series.setModel(0);
514 QCOMPARE(series.mapX(), -1);
516 // QCOMPARE(series.mapX(), -1);
515 QCOMPARE(series.mapY(), -1);
517 // QCOMPARE(series.mapY(), -1);
516 QVERIFY2(series.mapOrientation() == Qt::Vertical, "The orientation by default should be Qt::Vertical");
518 // QVERIFY2(series.mapOrientation() == Qt::Vertical, "The orientation by default should be Qt::Vertical");
517 }
519 }
518
520
519 void tst_QLineSeries::setModelMappingRange_data()
521 void tst_QLineSeries::setModelMappingRange_data()
520 {
522 {
521 QTest::addColumn<int>("first");
523 QTest::addColumn<int>("first");
522 QTest::addColumn<int>("count");
524 QTest::addColumn<int>("count");
523 QTest::newRow("first: 0, count: unlimited") << 0 << -1;
525 QTest::newRow("first: 0, count: unlimited") << 0 << -1;
524 QTest::newRow("first: 0, count: 5") << 0 << 5;
526 QTest::newRow("first: 0, count: 5") << 0 << 5;
525 QTest::newRow("first: 3, count: unlimited") << 3 << -1;
527 QTest::newRow("first: 3, count: unlimited") << 3 << -1;
526 QTest::newRow("first: 3, count: 5") << 3 << 5;
528 QTest::newRow("first: 3, count: 5") << 3 << 5;
527 QTest::newRow("first: -3, count: 5") << -3 << 5;
529 QTest::newRow("first: -3, count: 5") << -3 << 5;
528 QTest::newRow("first: 3, count: -5") << 3 << -5;
530 QTest::newRow("first: 3, count: -5") << 3 << -5;
529 QTest::newRow("first: -3, count: -5") << 3 << -5;
531 QTest::newRow("first: -3, count: -5") << 3 << -5;
530 QTest::newRow("first: -3, count: 0") << -3 << 0;
532 QTest::newRow("first: -3, count: 0") << -3 << 0;
531 QTest::newRow("first: 0, count: -5") << 0 << -5;
533 QTest::newRow("first: 0, count: -5") << 0 << -5;
532 QTest::newRow("first: 0, count: 0") << 0 << 0;
534 QTest::newRow("first: 0, count: 0") << 0 << 0;
533 }
535 }
534
536
535 void tst_QLineSeries::setModelMappingRange()
537 void tst_QLineSeries::setModelMappingRange()
536 {
538 {
539 QSKIP("Model mapping has been rewriten, test case needs update", SkipAll);
540
537 QFETCH(int, first);
541 QFETCH(int, first);
538 QFETCH(int, count);
542 QFETCH(int, count);
539 QLineSeries series;
543 QLineSeries series;
540
544
541 QStandardItemModel *model = new QStandardItemModel(0, 2);
545 QStandardItemModel *model = new QStandardItemModel(0, 2);
542 series.setModel(model);
546 series.setModel(model);
543 series.setModelMapping(0, 1);
547 // series.setModelMapping(0, 1);
544 series.setModelMappingRange(first, count);
548 // series.setModelMappingRange(first, count);
545
549
546 QCOMPARE(series.mapFirst(), qMax(first, 0)); // regardles of what value was used to set the range, first should not be less than 0
550 // QCOMPARE(series.mapFirst(), qMax(first, 0)); // regardles of what value was used to set the range, first should not be less than 0
547 QCOMPARE(series.mapCount(), qMax(count, -1)); // regardles of what value was used to set the range, first should not be less than 0
551 // QCOMPARE(series.mapCount(), qMax(count, -1)); // regardles of what value was used to set the range, first should not be less than 0
548 QVERIFY2(series.count() == 0, "No rows in the model, count should be 0");
552 // QVERIFY2(series.count() == 0, "No rows in the model, count should be 0");
549
553
550 for (int row = 0; row < 3; ++row) {
554 // for (int row = 0; row < 3; ++row) {
551 for (int column = 0; column < 2; column++) {
555 // for (int column = 0; column < 2; column++) {
552 QStandardItem *item = new QStandardItem(row * column);
556 // QStandardItem *item = new QStandardItem(row * column);
553 model->setItem(row, column, item);
557 // model->setItem(row, column, item);
554 }
558 // }
555 }
559 // }
556 if (qMax(count, -1) != -1)
560 // if (qMax(count, -1) != -1)
557 QVERIFY2(series.count() == qMin(model->rowCount() - qMax(first, 0), qMax(count, -1)), "Count should be the number of items in a model after first item, but not more than count and not less than 0");
561 // QVERIFY2(series.count() == qMin(model->rowCount() - qMax(first, 0), qMax(count, -1)), "Count should be the number of items in a model after first item, but not more than count and not less than 0");
558 else
562 // else
559 QVERIFY2(series.count() == model->rowCount() - qMax(first, 0), "Count should be the number of items in a model after first item, but not less then 0");
563 // QVERIFY2(series.count() == model->rowCount() - qMax(first, 0), "Count should be the number of items in a model after first item, but not less then 0");
560
564
561 // let's add few more rows to the model
565 // // let's add few more rows to the model
562 for (int row = 0; row < 10; ++row) {
566 // for (int row = 0; row < 10; ++row) {
563 QList<QStandardItem *> newRow;
567 // QList<QStandardItem *> newRow;
564 for (int column = 0; column < 2; column++) {
568 // for (int column = 0; column < 2; column++) {
565 newRow.append(new QStandardItem(row * column));
569 // newRow.append(new QStandardItem(row * column));
566 }
570 // }
567 model->appendRow(newRow);
571 // model->appendRow(newRow);
568 }
572 // }
569 if (qMax(count, -1) != -1)
573 // if (qMax(count, -1) != -1)
570 QVERIFY2(series.count() == qMin(model->rowCount() - qMax(first, 0), qMax(count, -1)), "Count should be the number of items in a model after first item, but not more than count, but not more than count and not less than 0");
574 // QVERIFY2(series.count() == qMin(model->rowCount() - qMax(first, 0), qMax(count, -1)), "Count should be the number of items in a model after first item, but not more than count, but not more than count and not less than 0");
571 else
575 // else
572 QVERIFY2(series.count() == model->rowCount() - qMax(first, 0), "Count should be the number of items in a model after first item, but not less then 0");
576 // QVERIFY2(series.count() == model->rowCount() - qMax(first, 0), "Count should be the number of items in a model after first item, but not less then 0");
573
577
574 // unset the model, values should be default
578 // // unset the model, values should be default
575 series.setModel(0);
579 // series.setModel(0);
576 QCOMPARE(series.mapFirst(), 0);
580 // QCOMPARE(series.mapFirst(), 0);
577 QCOMPARE(series.mapCount(), -1);
581 // QCOMPARE(series.mapCount(), -1);
578 QVERIFY2(series.count() == 0, "No rows in the model, count should be 0");
582 // QVERIFY2(series.count() == 0, "No rows in the model, count should be 0");
579 }
583 }
580
584
581 void tst_QLineSeries::modelUpdated()
585 void tst_QLineSeries::modelUpdated()
582 {
586 {
583 QStandardItemModel *model = new QStandardItemModel;
587 QSKIP("Model mapping has been rewriten, test case needs update", SkipAll);
584 for (int row = 0; row < 10; ++row) {
585 QList<QStandardItem *> newRow;
586 for (int column = 0; column < 2; column++) {
587 newRow.append(new QStandardItem(row * column));
588 }
589 model->appendRow(newRow);
590 }
591
588
592 QLineSeries series;
589 // QStandardItemModel *model = new QStandardItemModel;
593 series.setModel(model);
590 // for (int row = 0; row < 10; ++row) {
594 series.setModelMapping(0, 1);
591 // QList<QStandardItem *> newRow;
592 // for (int column = 0; column < 2; column++) {
593 // newRow.append(new QStandardItem(row * column));
594 // }
595 // model->appendRow(newRow);
596 // }
597
598 // QLineSeries series;
599 // series.setModel(model);
600 // series.setModelMapping(0, 1);
595
601
596 model->setData(model->index(3, 1), 34);
602 // model->setData(model->index(3, 1), 34);
597 // check that the update data is correctly taken from the model
603 // // check that the update data is correctly taken from the model
598 QVERIFY(qFuzzyCompare(series.points().at(3).y(), 34));
604 // QVERIFY(qFuzzyCompare(series.points().at(3).y(), 34));
599 }
605 }
600
606
601 void tst_QLineSeries::modelUpdatedCustomMapping()
607 void tst_QLineSeries::modelUpdatedCustomMapping()
602 {
608 {
603
609 QSKIP("Model mapping has been rewriten, test case needs update", SkipAll);
604 QStandardItemModel *model = new QStandardItemModel;
610 // QStandardItemModel *model = new QStandardItemModel;
605 for (int row = 0; row < 10; ++row) {
611 // for (int row = 0; row < 10; ++row) {
606 QList<QStandardItem *> newRow;
612 // QList<QStandardItem *> newRow;
607 for (int column = 0; column < 2; column++) {
613 // for (int column = 0; column < 2; column++) {
608 newRow.append(new QStandardItem(row * column));
614 // newRow.append(new QStandardItem(row * column));
609 }
615 // }
610 model->appendRow(newRow);
616 // model->appendRow(newRow);
611 }
617 // }
612
618
613 QLineSeries series;
619 // QLineSeries series;
614 series.setModel(model);
620 // series.setModel(model);
615 series.setModelMapping(0, 1);
621 // series.setModelMapping(0, 1);
616 series.setModelMappingRange(3, 4);
622 // series.setModelMappingRange(3, 4);
617
623
618 model->setData(model->index(3, 1), 34);
624 // model->setData(model->index(3, 1), 34);
619 QVERIFY(qFuzzyCompare(series.points().at(0).y(), 34));
625 // QVERIFY(qFuzzyCompare(series.points().at(0).y(), 34));
620 }
626 }
621
627
622 QTEST_MAIN(tst_QLineSeries)
628 QTEST_MAIN(tst_QLineSeries)
623
629
624 #include "tst_qlineseries.moc"
630 #include "tst_qlineseries.moc"
625
631
@@ -1,276 +1,276
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "customtablemodel.h"
21 #include "customtablemodel.h"
22 #include <QVector>
22 #include <QVector>
23 #include <QTime>
23 #include <QTime>
24 #include <QRect>
24 #include <QRect>
25 #include <QColor>
25 #include <QColor>
26
26
27 CustomTableModel::CustomTableModel(QObject *parent) :
27 CustomTableModel::CustomTableModel(QObject *parent) :
28 QAbstractTableModel(parent)
28 QAbstractTableModel(parent)
29 {
29 {
30 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
30 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
31
31
32 m_columnCount = 3;
32 m_columnCount = 7;
33 m_rowCount = 9;
33 m_rowCount = 9;
34
34
35 m_labels.append("Apples");
35 m_labels.append("Apples");
36 m_labels.append("Oranges");
36 m_labels.append("Oranges");
37 m_labels.append("Pears");
37 m_labels.append("Pears");
38 m_labels.append("Peaches");
38 m_labels.append("Peaches");
39 m_labels.append("Coconuts");
39 m_labels.append("Coconuts");
40 m_labels.append("Bananas");
40 m_labels.append("Bananas");
41 m_labels.append("Kiwis");
41 m_labels.append("Kiwis");
42 m_labels.append("Grapes");
42 m_labels.append("Grapes");
43 m_labels.append("Plums");
43 m_labels.append("Plums");
44
44
45 // m_data
45 // m_data
46 for (int i = 0; i < m_rowCount; i++)
46 for (int i = 0; i < m_rowCount; i++)
47 {
47 {
48 QVector<qreal>* dataVec = new QVector<qreal>(m_columnCount);
48 QVector<qreal>* dataVec = new QVector<qreal>(m_columnCount);
49 for (int k = 0; k < dataVec->size(); k++)
49 for (int k = 0; k < dataVec->size(); k++)
50 {
50 {
51 if (k%2 == 0)
51 if (k%2 == 0)
52 dataVec->replace(k, i * 50 + qrand()%20);
52 dataVec->replace(k, i * 50 + qrand()%20);
53 else
53 else
54 dataVec->replace(k, qrand()%100);
54 dataVec->replace(k, qrand()%100);
55 }
55 }
56 m_data.append(dataVec);
56 m_data.append(dataVec);
57 // m_labels.append(QString("Row: %1").arg((i + 1)));
57 // m_labels.append(QString("Row: %1").arg((i + 1)));
58 }
58 }
59 }
59 }
60
60
61 int CustomTableModel::rowCount(const QModelIndex & parent) const
61 int CustomTableModel::rowCount(const QModelIndex & parent) const
62 {
62 {
63 Q_UNUSED(parent)
63 Q_UNUSED(parent)
64 return m_data.count();
64 return m_data.count();
65 }
65 }
66
66
67 int CustomTableModel::columnCount(const QModelIndex & parent) const
67 int CustomTableModel::columnCount(const QModelIndex & parent) const
68 {
68 {
69 Q_UNUSED(parent)
69 Q_UNUSED(parent)
70 return m_columnCount;
70 return m_columnCount + 1;
71 }
71 }
72
72
73 QVariant CustomTableModel::headerData (int section, Qt::Orientation orientation, int role ) const
73 QVariant CustomTableModel::headerData (int section, Qt::Orientation orientation, int role ) const
74 {
74 {
75 if (role != Qt::DisplayRole)
75 if (role != Qt::DisplayRole)
76 return QVariant();
76 return QVariant();
77
77
78 if (orientation == Qt::Horizontal)
78 if (orientation == Qt::Horizontal)
79 {
79 {
80 switch(section)
80 switch(section)
81 {
81 {
82 case 0:
82 case 7:
83 return "Fruit";
83 return "Fruit";
84 case 1:
84 case 1:
85 return "Count";
85 return "Count";
86 case 2:
86 case 2:
87 return "Ordered";
87 return "Ordered";
88 default:
88 default:
89 if (section%2 == 0)
89 if (section%2 == 0)
90 return "x";
90 return "x";
91 else
91 else
92 return "y";
92 return "y";
93 }
93 }
94 }
94 }
95 else
95 else
96 return QString("%1").arg(section /*+ 1*/);
96 return QString("%1").arg(section /*+ 1*/);
97 }
97 }
98
98
99 QVariant CustomTableModel::data(const QModelIndex & index, int role) const
99 QVariant CustomTableModel::data(const QModelIndex & index, int role) const
100 {
100 {
101 if (role == Qt::DisplayRole)
101 if (role == Qt::DisplayRole)
102 {
102 {
103 switch(index.column())
103 switch(index.column())
104 {
104 {
105 case 0:
105 case 7:
106 return m_labels[index.row()];
106 return m_labels[index.row()];
107 default:
107 default:
108 return m_data[index.row()]->at(index.column());
108 return m_data[index.row()]->at(index.column());
109 break;
109 break;
110 }
110 }
111 }
111 }
112 else if (role == Qt::EditRole)
112 else if (role == Qt::EditRole)
113 {
113 {
114 switch(index.column())
114 switch(index.column())
115 {
115 {
116 case 0:
116 case 7:
117 return m_labels[index.row()];
117 return m_labels[index.row()];
118 default:
118 default:
119 return m_data[index.row()]->at(index.column());
119 return m_data[index.row()]->at(index.column());
120 break;
120 break;
121 }
121 }
122 }
122 }
123 else if (role == Qt::BackgroundRole)
123 else if (role == Qt::BackgroundRole)
124 {
124 {
125 QRect rect;
125 QRect rect;
126 foreach(rect, m_mapping)
126 foreach(rect, m_mapping)
127 if(rect.contains(index.column(), index.row()))
127 if(rect.contains(index.column(), index.row()))
128 return QColor(m_mapping.key(rect));
128 return QColor(m_mapping.key(rect));
129
129
130 // cell not mapped return white color
130 // cell not mapped return white color
131 return QColor(Qt::white);
131 return QColor(Qt::white);
132 }
132 }
133 return QVariant();
133 return QVariant();
134 }
134 }
135
135
136 bool CustomTableModel::setData ( const QModelIndex & index, const QVariant & value, int role)
136 bool CustomTableModel::setData ( const QModelIndex & index, const QVariant & value, int role)
137 {
137 {
138 if (index.isValid() && role == Qt::EditRole)
138 if (index.isValid() && role == Qt::EditRole)
139 {
139 {
140 switch(index.column())
140 switch(index.column())
141 {
141 {
142 case 0:
142 case 7:
143 m_labels.replace(index.row(), value.toString());
143 m_labels.replace(index.row(), value.toString());
144 break;
144 break;
145 default:
145 default:
146 m_data[index.row()]->replace(index.column(), value.toDouble());
146 m_data[index.row()]->replace(index.column(), value.toDouble());
147 break;
147 break;
148 }
148 }
149 emit dataChanged(index, index);
149 emit dataChanged(index, index);
150 return true;
150 return true;
151 }
151 }
152 return false;
152 return false;
153 }
153 }
154
154
155 Qt::ItemFlags CustomTableModel::flags ( const QModelIndex & index ) const
155 Qt::ItemFlags CustomTableModel::flags ( const QModelIndex & index ) const
156 {
156 {
157 return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
157 return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
158 }
158 }
159
159
160 bool CustomTableModel::insertRows ( int row, int count, const QModelIndex & parent)
160 bool CustomTableModel::insertRows ( int row, int count, const QModelIndex & parent)
161 {
161 {
162 Q_UNUSED(parent)
162 Q_UNUSED(parent)
163
163
164 if (row < 0)
164 if (row < 0)
165 row = 0;
165 row = 0;
166 beginInsertRows(QModelIndex(), row /*dataTable.count()*/, row + count - 1);
166 beginInsertRows(QModelIndex(), row /*dataTable.count()*/, row + count - 1);
167 for (int i = row; i < row + count; i++)
167 for (int i = row; i < row + count; i++)
168 {
168 {
169 // m_points.insert(row, QPointF(10,20));
169 // m_points.insert(row, QPointF(10,20));
170 QVector<qreal>* dataVec = new QVector<qreal>(m_columnCount);
170 QVector<qreal>* dataVec = new QVector<qreal>(m_columnCount);
171 QVector<QColor>* colorVec = new QVector<QColor>(m_columnCount);
171 QVector<QColor>* colorVec = new QVector<QColor>(m_columnCount);
172 for (int k = 0; k < dataVec->size(); k++)
172 for (int k = 0; k < dataVec->size(); k++)
173 {
173 {
174 if (k%2 == 0)
174 if (k%2 == 0)
175 // dataVec->replace(k, i * 50 + qrand()%20);
175 // dataVec->replace(k, i * 50 + qrand()%20);
176 {
176 {
177 int difference = 0;
177 int difference = 0;
178 if (i < m_data.size())
178 if (i < m_data.size())
179 {
179 {
180 if (i - 1 >= 0)
180 if (i - 1 >= 0)
181 {
181 {
182 if (row > 0)
182 if (row > 0)
183 difference = (int)((qAbs(m_data[i]->at(k) - m_data[row - 1]->at(k)))/count);
183 difference = (int)((qAbs(m_data[i]->at(k) - m_data[row - 1]->at(k)))/count);
184 else
184 else
185 difference = (int)((qAbs(m_data[i]->at(k)/count)));
185 difference = (int)((qAbs(m_data[i]->at(k)/count)));
186 dataVec->replace(k, m_data[i - 1]->at(k) + qrand()%qMax(1, difference));
186 dataVec->replace(k, m_data[i - 1]->at(k) + qrand()%qMax(1, difference));
187 }
187 }
188 else
188 else
189 dataVec->replace(k, qrand()%40 + 10);
189 dataVec->replace(k, qrand()%40 + 10);
190 }
190 }
191 else
191 else
192 {
192 {
193 if (i - 1 >= 0)
193 if (i - 1 >= 0)
194 {
194 {
195 dataVec->replace(k, m_data[i - 1]->at(k) + qrand()%40 + 10);
195 dataVec->replace(k, m_data[i - 1]->at(k) + qrand()%40 + 10);
196 }
196 }
197 else
197 else
198 {
198 {
199 dataVec->replace(k, qrand()%40 + 10);
199 dataVec->replace(k, qrand()%40 + 10);
200 }
200 }
201 }
201 }
202 }
202 }
203 else
203 else
204 dataVec->replace(k, qrand()%100);
204 dataVec->replace(k, qrand()%100);
205 colorVec->replace(k, QColor(Qt::white));
205 colorVec->replace(k, QColor(Qt::white));
206 }
206 }
207 m_data.insert(i, dataVec);
207 m_data.insert(i, dataVec);
208 m_labels.insert(i,(QString("Row: %1").arg(i + 1)));
208 m_labels.insert(i,(QString("Row: %1").arg(i + 1)));
209 }
209 }
210 endInsertRows();
210 endInsertRows();
211 return true;
211 return true;
212 }
212 }
213
213
214 bool CustomTableModel::removeRows ( int row, int count, const QModelIndex & parent)
214 bool CustomTableModel::removeRows ( int row, int count, const QModelIndex & parent)
215 {
215 {
216 if (row > this->rowCount() - 1)
216 if (row > this->rowCount() - 1)
217 return false;
217 return false;
218 if (row < 0)
218 if (row < 0)
219 row = 0;
219 row = 0;
220 if (row + count > rowCount())
220 if (row + count > rowCount())
221 return false;
221 return false;
222 beginRemoveRows(parent, row, row + count - 1);
222 beginRemoveRows(parent, row, row + count - 1);
223 for (int i = row; i < row + count; i++)
223 for (int i = row; i < row + count; i++)
224 {
224 {
225 QVector<qreal>* item = m_data.at(row);
225 QVector<qreal>* item = m_data.at(row);
226 m_data.removeAt(row);
226 m_data.removeAt(row);
227 delete item;
227 delete item;
228 m_labels.removeAt(row);
228 m_labels.removeAt(row);
229 }
229 }
230 endRemoveRows();
230 endRemoveRows();
231 return true;
231 return true;
232 }
232 }
233
233
234 bool CustomTableModel::insertColumns ( int column, int count, const QModelIndex & parent)
234 bool CustomTableModel::insertColumns ( int column, int count, const QModelIndex & parent)
235 {
235 {
236 if (column < 0)
236 if (column < 0)
237 column = 0;
237 column = 0;
238 beginInsertColumns(parent, column, column + count - 1);
238 beginInsertColumns(parent, column, column + count - 1);
239 m_columnCount += count;
239 m_columnCount += count;
240 for (int i = column; i < column + count; i++)
240 for (int i = column; i < column + count; i++)
241 for (int k = 0; k < rowCount(); k++)
241 for (int k = 0; k < rowCount(); k++)
242 if (k - 1 >= 0) {
242 if (k - 1 >= 0) {
243 m_data[k]->insert(i, m_data[k - 1]->at(i) + qrand()%40 + 10);
243 m_data[k]->insert(i, m_data[k - 1]->at(i) + qrand()%40 + 10);
244 } else {
244 } else {
245 m_data[k]->insert(i, qrand()%40);
245 m_data[k]->insert(i, qrand()%40);
246 }
246 }
247 endInsertColumns();
247 endInsertColumns();
248 return true;
248 return true;
249 }
249 }
250
250
251 bool CustomTableModel::removeColumns ( int column, int count, const QModelIndex & parent)
251 bool CustomTableModel::removeColumns ( int column, int count, const QModelIndex & parent)
252 {
252 {
253 if (column > columnCount() - 1)
253 if (column > columnCount() - 1)
254 return false;
254 return false;
255 if (column < 0)
255 if (column < 0)
256 column = 0;
256 column = 0;
257 if (column + count > columnCount())
257 if (column + count > columnCount())
258 return false;
258 return false;
259 beginRemoveColumns(parent, column, column + count -1);
259 beginRemoveColumns(parent, column, column + count -1);
260 m_columnCount -= count;
260 m_columnCount -= count;
261 for (int i = column; i < column + count; i++)
261 for (int i = column; i < column + count; i++)
262 for (int k = 0; k < rowCount(); k++)
262 for (int k = 0; k < rowCount(); k++)
263 m_data[k]->remove(column);
263 m_data[k]->remove(column);
264 endRemoveColumns();
264 endRemoveColumns();
265 return true;
265 return true;
266 }
266 }
267
267
268 void CustomTableModel::addMapping(QString color, QRect area)
268 void CustomTableModel::addMapping(QString color, QRect area)
269 {
269 {
270 m_mapping.insertMulti(color, area);
270 m_mapping.insertMulti(color, area);
271 }
271 }
272
272
273 void CustomTableModel::addMapping(QString color, int left, int top, int right, int bottom)
273 void CustomTableModel::addMapping(QString color, int left, int top, int right, int bottom)
274 {
274 {
275 addMapping(color, QRect(QPoint(left, top), QPoint(right, bottom)));
275 addMapping(color, QRect(QPoint(left, top), QPoint(right, bottom)));
276 }
276 }
@@ -1,412 +1,460
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "tablewidget.h"
21 #include "tablewidget.h"
22 #include <QGridLayout>
22 #include <QGridLayout>
23 #include <QTableView>
23 #include <QTableView>
24 #include <QChart>
24 #include <QChart>
25 #include <QStyledItemDelegate>
25 #include <QStyledItemDelegate>
26 #include <QLineSeries>
26 #include <QLineSeries>
27 #include <QSplineSeries>
27 #include <QSplineSeries>
28 #include <QScatterSeries>
28 #include <QScatterSeries>
29 #include <QXYModelMapper>
29 #include "customtablemodel.h"
30 #include "customtablemodel.h"
30 #include <QPieSeries>
31 #include <QPieSeries>
32 #include <QPieModelMapper>
31 #include <QPieSlice>
33 #include <QPieSlice>
32 #include <QAreaSeries>
34 #include <QAreaSeries>
33 #include <QBarSeries>
35 #include <QBarSeries>
34 #include <QBarSet>
36 #include <QBarSet>
35 #include <QPushButton>
37 #include <QPushButton>
36 #include <QRadioButton>
38 #include <QRadioButton>
37 #include <QLabel>
39 #include <QLabel>
38 #include <QSpinBox>
40 #include <QSpinBox>
39 #include <QTime>
41 #include <QTime>
40 #include <QHeaderView>
42 #include <QHeaderView>
41
43
42 TableWidget::TableWidget(QWidget *parent)
44 TableWidget::TableWidget(QWidget *parent)
43 : QWidget(parent)
45 : QWidget(parent)
44 // specialPie(0)
46 // specialPie(0)
45 {
47 {
46 setGeometry(1900, 100, 1000, 600);
48 setGeometry(1900, 100, 1000, 600);
47 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
49 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
48 // create simple model for storing data
50 // create simple model for storing data
49 // user's table data model
51 // user's table data model
50 m_model = new CustomTableModel;
52 m_model = new CustomTableModel;
51 m_tableView = new QTableView;
53 m_tableView = new QTableView;
52 m_tableView->setModel(m_model);
54 m_tableView->setModel(m_model);
53 // m_tableView->setMinimumHeight(300);
55 // m_tableView->setMinimumHeight(300);
54 m_tableView->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
56 m_tableView->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
55 m_tableView->verticalHeader()->setResizeMode(QHeaderView::Stretch);
57 m_tableView->verticalHeader()->setResizeMode(QHeaderView::Stretch);
56
58
57 m_chart = new QChart;
59 m_chart = new QChart;
58 m_chart->legend()->setVisible(true);
60 m_chart->legend()->setVisible(true);
59 m_chart->setAnimationOptions(QChart::SeriesAnimations);
61 m_chart->setAnimationOptions(QChart::SeriesAnimations);
60 m_chartView = new QChartView(m_chart);
62 m_chartView = new QChartView(m_chart);
61 m_chartView->setRenderHint(QPainter::Antialiasing);
63 m_chartView->setRenderHint(QPainter::Antialiasing);
62 m_chartView->setMinimumSize(640, 480);
64 m_chartView->setMinimumSize(640, 480);
63
65
64 // add, remove data buttons
66 // add, remove data buttons
65 QPushButton* addRowAboveButton = new QPushButton("Add row above");
67 QPushButton* addRowAboveButton = new QPushButton("Add row above");
66 connect(addRowAboveButton, SIGNAL(clicked()), this, SLOT(addRowAbove()));
68 connect(addRowAboveButton, SIGNAL(clicked()), this, SLOT(addRowAbove()));
67
69
68 QPushButton* addRowBelowButton = new QPushButton("Add row below");
70 QPushButton* addRowBelowButton = new QPushButton("Add row below");
69 connect(addRowBelowButton, SIGNAL(clicked()), this, SLOT(addRowBelow()));
71 connect(addRowBelowButton, SIGNAL(clicked()), this, SLOT(addRowBelow()));
70
72
71 QPushButton* removeRowButton = new QPushButton("Remove row");
73 QPushButton* removeRowButton = new QPushButton("Remove row");
72 connect(removeRowButton, SIGNAL(clicked()), this, SLOT(removeRow()));
74 connect(removeRowButton, SIGNAL(clicked()), this, SLOT(removeRow()));
73
75
74 QPushButton* addColumnRightButton = new QPushButton("Add column to the right");
76 QPushButton* addColumnRightButton = new QPushButton("Add column to the right");
75 connect(addColumnRightButton, SIGNAL(clicked()), this, SLOT(addColumnRight()));
77 connect(addColumnRightButton, SIGNAL(clicked()), this, SLOT(addColumnRight()));
76
78
77 QPushButton* removeColumnButton = new QPushButton("Remove column");
79 QPushButton* removeColumnButton = new QPushButton("Remove column");
78 connect(removeColumnButton, SIGNAL(clicked()), this, SLOT(removeColumn()));
80 connect(removeColumnButton, SIGNAL(clicked()), this, SLOT(removeColumn()));
79
81
80 QPushButton* specialPieButton = new QPushButton("Test pie");
82 QPushButton* specialPieButton = new QPushButton("Test pie");
81 connect(specialPieButton, SIGNAL(clicked()), this, SLOT(testPie()));
83 connect(specialPieButton, SIGNAL(clicked()), this, SLOT(testPie()));
82
84
83
85
84 QLabel *spinBoxLabel = new QLabel("Rows affected:");
86 // QLabel *spinBoxLabel = new QLabel("Rows affected:");
85
87
86 // spin box for setting number of affected items (add, remove)
88 // spin box for setting number of affected items (add, remove)
87 m_linesCountSpinBox = new QSpinBox;
89 m_linesCountSpinBox = new QSpinBox;
88 m_linesCountSpinBox->setRange(1, 10);
90 m_linesCountSpinBox->setRange(1, 10);
89 m_linesCountSpinBox->setValue(1);
91 m_linesCountSpinBox->setValue(1);
90
92
91 // buttons layout
93 // buttons layout
92 QVBoxLayout* buttonsLayout = new QVBoxLayout;
94 QVBoxLayout* buttonsLayout = new QVBoxLayout;
93 // buttonsLayout->addWidget(spinBoxLabel);
95 // buttonsLayout->addWidget(spinBoxLabel);
94 // buttonsLayout->addWidget(m_linesCountSpinBox);
96 // buttonsLayout->addWidget(m_linesCountSpinBox);
95 // buttonsLayout->addWidget(addRowAboveButton);
97 // buttonsLayout->addWidget(addRowAboveButton);
96 buttonsLayout->addWidget(addRowBelowButton);
98 buttonsLayout->addWidget(addRowBelowButton);
97 buttonsLayout->addWidget(removeRowButton);
99 buttonsLayout->addWidget(removeRowButton);
98 // buttonsLayout->addWidget(addColumnRightButton);
100 // buttonsLayout->addWidget(addColumnRightButton);
99 // buttonsLayout->addWidget(removeColumnButton);
101 // buttonsLayout->addWidget(removeColumnButton);
100 // buttonsLayout->addWidget(specialPieButton);
102 buttonsLayout->addWidget(specialPieButton);
101 buttonsLayout->addStretch();
103 buttonsLayout->addStretch();
102
104
103 // chart type radio buttons
105 // chart type radio buttons
104 m_lineRadioButton = new QRadioButton("Line");
106 m_lineRadioButton = new QRadioButton("Line");
105 m_splineRadioButton = new QRadioButton("Spline");
107 m_splineRadioButton = new QRadioButton("Spline");
106 m_scatterRadioButton = new QRadioButton("Scatter");
108 m_scatterRadioButton = new QRadioButton("Scatter");
107 m_pieRadioButton = new QRadioButton("Pie");
109 m_pieRadioButton = new QRadioButton("Pie");
108 m_areaRadioButton = new QRadioButton("Area");
110 m_areaRadioButton = new QRadioButton("Area");
109 m_barRadioButton = new QRadioButton("Bar");
111 m_barRadioButton = new QRadioButton("Bar");
110
112
111 connect(m_lineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
113 connect(m_lineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
112 connect(m_splineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
114 connect(m_splineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
113 connect(m_scatterRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
115 connect(m_scatterRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
114 connect(m_pieRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
116 connect(m_pieRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
115 connect(m_areaRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
117 connect(m_areaRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
116 connect(m_barRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
118 connect(m_barRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
117 m_pieRadioButton->setChecked(true);
119 m_lineRadioButton->setChecked(true);
118
120
119 // radio buttons layout
121 // radio buttons layout
120 QVBoxLayout* radioLayout = new QVBoxLayout;
122 QVBoxLayout* radioLayout = new QVBoxLayout;
121 radioLayout->addWidget(m_lineRadioButton);
123 radioLayout->addWidget(m_lineRadioButton);
122 // radioLayout->addWidget(m_splineRadioButton);
124 radioLayout->addWidget(m_splineRadioButton);
123 // radioLayout->addWidget(m_scatterRadioButton);
125 // radioLayout->addWidget(m_scatterRadioButton);
124 radioLayout->addWidget(m_pieRadioButton);
126 radioLayout->addWidget(m_pieRadioButton);
125 // radioLayout->addWidget(m_areaRadioButton);
127 // radioLayout->addWidget(m_areaRadioButton);
126 radioLayout->addWidget(m_barRadioButton);
128 radioLayout->addWidget(m_barRadioButton);
127 radioLayout->addStretch();
129 radioLayout->addStretch();
128
130
129 // create main layout
131 // create main layout
130 QGridLayout* mainLayout = new QGridLayout;
132 QGridLayout* mainLayout = new QGridLayout;
131 mainLayout->addLayout(buttonsLayout, 2, 0);
133 mainLayout->addLayout(buttonsLayout, 2, 0);
132 // mainLayout->addLayout(radioLayout, 2, 1);
134 mainLayout->addLayout(radioLayout, 3, 0);
133 mainLayout->addWidget(m_tableView, 1, 0);
135 mainLayout->addWidget(m_tableView, 1, 0);
134 mainLayout->addWidget(m_chartView, 1, 1, 2, 1);
136 mainLayout->addWidget(m_chartView, 1, 1, 2, 1);
135 setLayout(mainLayout);
137 setLayout(mainLayout);
136 m_lineRadioButton->setFocus();
138 m_lineRadioButton->setFocus();
137 }
139 }
138
140
139 void TableWidget::addRowAbove()
141 void TableWidget::addRowAbove()
140 {
142 {
141 m_model->insertRows(m_tableView->currentIndex().row(), m_linesCountSpinBox->value());
143 m_model->insertRows(m_tableView->currentIndex().row(), m_linesCountSpinBox->value());
142
144
143 }
145 }
144
146
145 void TableWidget::addRowBelow()
147 void TableWidget::addRowBelow()
146 {
148 {
147 m_model->insertRows(m_tableView->currentIndex().row() + 1, m_linesCountSpinBox->value());
149 m_model->insertRows(m_tableView->currentIndex().row() + 1, m_linesCountSpinBox->value());
148
150
149 }
151 }
150
152
151 void TableWidget::removeRow()
153 void TableWidget::removeRow()
152 {
154 {
153 m_model->removeRows(m_tableView->currentIndex().row(), qMin(m_model->rowCount() - m_tableView->currentIndex().row(), m_linesCountSpinBox->value()));
155 m_model->removeRows(m_tableView->currentIndex().row(), qMin(m_model->rowCount() - m_tableView->currentIndex().row(), m_linesCountSpinBox->value()));
154 }
156 }
155
157
156 void TableWidget::addColumnRight()
158 void TableWidget::addColumnRight()
157 {
159 {
158 m_model->insertColumns(m_tableView->currentIndex().column() + 1, m_linesCountSpinBox->value());
160 m_model->insertColumns(m_tableView->currentIndex().column() + 1, m_linesCountSpinBox->value());
159 }
161 }
160
162
161 void TableWidget::removeColumn()
163 void TableWidget::removeColumn()
162 {
164 {
163 m_model->removeColumns(m_tableView->currentIndex().column(), qMin(m_model->columnCount() - m_tableView->currentIndex().column(), m_linesCountSpinBox->value()));
165 m_model->removeColumns(m_tableView->currentIndex().column(), qMin(m_model->columnCount() - m_tableView->currentIndex().column(), m_linesCountSpinBox->value()));
164 }
166 }
165
167
166 void TableWidget::updateChartType(bool toggle)
168 void TableWidget::updateChartType(bool toggle)
167 {
169 {
168 // this if is needed, so that the function is only called once.
170 // this if is needed, so that the function is only called once.
169 // For the radioButton that was enabled.
171 // For the radioButton that was enabled.
170 if (toggle) {
172 if (toggle) {
171 // specialPie = 0;
173 // specialPie = 0;
172 m_chart->removeAllSeries();
174 m_chart->removeAllSeries();
173 m_chart->axisX()->setNiceNumbersEnabled(false);
175 m_chart->axisX()->setNiceNumbersEnabled(false);
174 m_chart->axisY()->setNiceNumbersEnabled(false);
176 m_chart->axisY()->setNiceNumbersEnabled(false);
175
177
176 // renable axes of the chart (pie hides them)
178 // renable axes of the chart (pie hides them)
177 // x axis
179 // x axis
178 QAxis *axis = m_chart->axisX();
180 QAxis *axis = m_chart->axisX();
179 axis->setAxisVisible(true);
181 axis->setAxisVisible(true);
180 axis->setGridLineVisible(true);
182 axis->setGridLineVisible(true);
181 axis->setLabelsVisible(true);
183 axis->setLabelsVisible(true);
182
184
183 // y axis
185 // y axis
184 axis = m_chart->axisY();
186 axis = m_chart->axisY();
185 axis->setAxisVisible(true);
187 axis->setAxisVisible(true);
186 axis->setGridLineVisible(true);
188 axis->setGridLineVisible(true);
187 axis->setLabelsVisible(true);
189 axis->setLabelsVisible(true);
188
190
189 m_model->clearMapping();
191 m_model->clearMapping();
190
192
191 QString seriesColorHex = "#000000";
193 QString seriesColorHex = "#000000";
192 QPen pen;
194 QPen pen;
193 pen.setWidth(2);
195 pen.setWidth(2);
194
196
195 if (m_lineRadioButton->isChecked())
197 if (m_lineRadioButton->isChecked())
196 {
198 {
197 m_chart->setAnimationOptions(QChart::NoAnimation);
199 m_chart->setAnimationOptions(QChart::NoAnimation);
198
200
199 // series 1
201 // series 1
200 m_series = new QLineSeries;
202 m_series = new QLineSeries;
201 m_series->setModel(m_model);
203 m_series->setModel(m_model);
202 m_series->setModelMapping(0,1, Qt::Vertical);
204
203 m_series->setModelMappingRange(3, 4);
205 QXYModelMapper *mapper = new QXYModelMapper;
206 mapper->setMapX(0);
207 mapper->setMapY(1);
208 mapper->setFirst(3);
209 mapper->setCount(4);
210 m_series->setModelMapper(mapper);
211 // m_series->setModelMapping(0,1, Qt::Vertical);
212 // m_series->setModelMappingRange(3, 4);
204 m_chart->addSeries(m_series);
213 m_chart->addSeries(m_series);
205 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
214 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
206 m_model->addMapping(seriesColorHex, QRect(0, 3, 2, 4));
215 m_model->addMapping(seriesColorHex, QRect(0, 3, 2, 4));
207
216
208 // series 2
217 // series 2
209 m_series = new QLineSeries;
218 m_series = new QLineSeries;
210 m_series->setModel(m_model);
219 m_series->setModel(m_model);
211 m_series->setModelMapping(2,3, Qt::Vertical);
220
221 mapper = new QXYModelMapper;
222 mapper->setMapX(3);
223 mapper->setMapY(4);
224 // mapper->setFirst(3);
225 // mapper->setCount(4);
226 m_series->setModelMapper(mapper);
227 // m_series->setModelMapping(2,3, Qt::Vertical);
212 m_chart->addSeries(m_series);
228 m_chart->addSeries(m_series);
213 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
229 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
214 m_model->addMapping(seriesColorHex, QRect(2, 0, 2, 1000));
230 m_model->addMapping(seriesColorHex, QRect(3, 0, 2, 1000));
215
231
216 // series 3
232 // series 3
217 m_series = new QLineSeries;
233 m_series = new QLineSeries;
218 m_series->setModel(m_model);
234 m_series->setModel(m_model);
219 m_series->setModelMapping(4,5, Qt::Vertical);
235
220 m_series->setModelMappingRange(2, -1);
236 mapper = new QXYModelMapper;
237 mapper->setMapX(5);
238 mapper->setMapY(6);
239 mapper->setFirst(2);
240 mapper->setCount(-1);
241 m_series->setModelMapper(mapper);
242 // m_series->setModelMapping(4,5, Qt::Vertical);
243 // m_series->setModelMappingRange(2, -1);
221 m_chart->addSeries(m_series);
244 m_chart->addSeries(m_series);
222 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
245 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
223 m_model->addMapping(seriesColorHex, QRect(4, 2, 2, 1000));
246 m_model->addMapping(seriesColorHex, QRect(5, 2, 2, 1000));
224 }
247 }
225 else if (m_splineRadioButton->isChecked())
248 else if (m_splineRadioButton->isChecked())
226 {
249 {
227 m_chart->setAnimationOptions(QChart::NoAnimation);
250 m_chart->setAnimationOptions(QChart::NoAnimation);
228
251
229 // series 1
252 // series 1
230 m_series = new QSplineSeries;
253 m_series = new QSplineSeries;
231 m_series->setModel(m_model);
254 m_series->setModel(m_model);
232 m_series->setModelMapping(0,1, Qt::Vertical);
255
233 // m_series->setModelMappingRange(1, 4);
256 QXYModelMapper *mapper = new QXYModelMapper;
234 // series->setModelMapping(0,1, Qt::Horizontal);
257 mapper->setMapX(0);
258 mapper->setMapY(1);
259 mapper->setFirst(0);
260 mapper->setCount(-1);
261
262 m_series->setModelMapper(mapper);
263
235 m_chart->addSeries(m_series);
264 m_chart->addSeries(m_series);
236 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
265 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
237 m_model->addMapping(seriesColorHex, QRect(0, 0, 2, 1000));
266 m_model->addMapping(seriesColorHex, QRect(0, 0, 2, 1000));
238
267
239 // series 2
268 // series 2
240 m_series = new QSplineSeries;
269 m_series = new QSplineSeries;
241 m_series->setModel(m_model);
270 m_series->setModel(m_model);
242 m_series->setModelMapping(2,3, Qt::Vertical);
271
243 m_series->setModelMappingRange(2, 4);
272 mapper = new QXYModelMapper;
244 // series->setModelMapping(2,3, Qt::Horizontal);
273 mapper->setMapX(2);
274 mapper->setMapY(3);
275 mapper->setFirst(2);
276 mapper->setCount(4);
277
278 m_series->setModelMapper(mapper);
279
245 m_chart->addSeries(m_series);
280 m_chart->addSeries(m_series);
246 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
281 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
247 m_model->addMapping(seriesColorHex, QRect(2, 2, 2, 4));
282 m_model->addMapping(seriesColorHex, QRect(2, 2, 2, 4));
248
283
249 // series 3
284 // series 3
250 m_series = new QSplineSeries;
285 m_series = new QSplineSeries;
251 m_series->setModel(m_model);
286 m_series->setModel(m_model);
252 m_series->setModelMapping(4,5, Qt::Vertical);
253 m_series->setModelMappingRange(2, -1);
254 // series->setModelMapping(4,5, Qt::Horizontal);
255 m_chart->addSeries(m_series);
256 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
257 m_model->addMapping(seriesColorHex, QRect(4, 2, 2, 1000));
258 }
259 else if (m_scatterRadioButton->isChecked())
260 {
261 m_chart->setAnimationOptions(QChart::NoAnimation);
262
287
263 // series 1
288 mapper = new QXYModelMapper;
264 m_series = new QScatterSeries;
289 mapper->setMapX(4);
265 m_series->setModel(m_model);
290 mapper->setMapY(5);
266 m_series->setModelMapping(0,1, Qt::Vertical);
291 mapper->setFirst(2);
267 // m_series->setModelMappingRange(2, 0);
292 mapper->setCount(-1);
268 // series->setModelMapping(0,1, Qt::Horizontal);
269 m_chart->addSeries(m_series);
270
293
271 seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
294 m_series->setModelMapper(mapper);
272 m_model->addMapping(seriesColorHex, QRect(0, 2, 2, 1000));
273
295
274 // series 2
275 m_series = new QScatterSeries;
276 m_series->setModel(m_model);
277 m_series->setModelMapping(2,3, Qt::Vertical);
278 // m_series->setModelMappingRange(1, 6);
279 // series->setModelMapping(2,3, Qt::Horizontal);
280 m_chart->addSeries(m_series);
281
282 seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
283 m_model->addMapping(seriesColorHex, QRect(2, 1, 2, 6));
284
285 // series 3
286 m_series = new QScatterSeries;
287 m_series->setModel(m_model);
288 m_series->setModelMapping(4,5, Qt::Vertical);
289 // series->setModelMapping(4,5, Qt::Horizontal);
290 m_chart->addSeries(m_series);
296 m_chart->addSeries(m_series);
291 seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
297 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
292 m_model->addMapping(seriesColorHex, QRect(4, 0, 2, 1000));
298 m_model->addMapping(seriesColorHex, QRect(4, 2, 2, 1000));
293 }
294 else if (m_pieRadioButton->isChecked())
295 {
296 m_chart->setAnimationOptions(QChart::SeriesAnimations);
297
298 // pie 1
299 QPieSeries* pieSeries = new QPieSeries;
300 pieSeries->setModel(m_model);
301 pieSeries->setModelMappingRange(2, 5);
302 pieSeries->setModelMapping(1, 0, Qt::Vertical);
303 pieSeries->setLabelsVisible(true);
304 pieSeries->setPieSize(0.75);
305 // pieSeries->setHorizontalPosition(0.2);
306 // pieSeries->setVerticalPosition(0.3);
307
308 m_chart->addSeries(pieSeries);
309 seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
310 m_model->addMapping(seriesColorHex, QRect(0, 2, 2, 5));
311
312 // // pie 2
313 // pieSeries = new QPieSeries;
314 // pieSeries->setModel(m_model);
315
316 // pieSeries->setModelMapping(1,1, Qt::Vertical);
317 // pieSeries->setModelMappingRange(2, -1);
318 // pieSeries->setLabelsVisible(true);
319 // pieSeries->setPieSize(0.35);
320 // pieSeries->setHorizontalPosition(0.8);
321 // pieSeries->setVerticalPosition(0.3);
322 // m_chart->addSeries(pieSeries);
323 // seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
324 // m_model->addMapping(seriesColorHex, QRect(1, 2, 1, 1000));
325
326 // // pie 3
327 // pieSeries = new QPieSeries;
328 // pieSeries->setModel(m_model);
329 // pieSeries->setModelMapping(2,2, Qt::Vertical);
330 // pieSeries->setLabelsVisible(true);
331 // pieSeries->setPieSize(0.35);
332 // pieSeries->setHorizontalPosition(0.5);
333 // pieSeries->setVerticalPosition(0.75);
334 // m_chart->addSeries(pieSeries);
335 // seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
336 // m_model->addMapping(seriesColorHex, QRect(2, 0, 1, 1000));
337
338 // // special pie
339 // specialPie = new QPieSeries;
340 // specialPie->append(17, "1");
341 // specialPie->append(45, "2");
342 // specialPie->append(77, "3");
343 // specialPie->append(37, "4");
344 // specialPie->append(27, "5");
345 // specialPie->append(47, "6");
346 // specialPie->setPieSize(0.35);
347 // specialPie->setHorizontalPosition(0.8);
348 // specialPie->setVerticalPosition(0.75);
349 // specialPie->setLabelsVisible(true);
350 // m_chart->addSeries(specialPie);
351 }
352 else if (m_areaRadioButton->isChecked())
353 {
354 m_chart->setAnimationOptions(QChart::NoAnimation);
355
356 QLineSeries* upperLineSeries = new QLineSeries;
357 upperLineSeries->setModel(m_model);
358 upperLineSeries->setModelMapping(0, 1, Qt::Vertical);
359 // upperLineSeries->setModelMappingRange(1, 5);
360 QLineSeries* lowerLineSeries = new QLineSeries;
361 lowerLineSeries->setModel(m_model);
362 lowerLineSeries->setModelMapping(2, 3, Qt::Vertical);
363 QAreaSeries* areaSeries = new QAreaSeries(upperLineSeries, lowerLineSeries);
364 m_chart->addSeries(areaSeries);
365 seriesColorHex = "#" + QString::number(areaSeries->brush().color().rgb(), 16).right(6).toUpper();
366 m_model->addMapping(seriesColorHex, QRect(0, 1, 2, 5));
367 m_model->addMapping(seriesColorHex, QRect(2, 0, 2, 1000));
368 }
369 else if (m_barRadioButton->isChecked())
370 {
371 m_chart->setAnimationOptions(QChart::SeriesAnimations);
372
373 QBarSeries* barSeries = new QBarSeries();
374 barSeries->setCategories(QStringList());
375 barSeries->setModel(m_model);
376 // barSeries->setModelMappingRange(2, 5);
377 barSeries->setModelMapping(5, 2, 4, Qt::Vertical);
378 m_chart->addSeries(barSeries);
379 QList<QBarSet*> barsets = barSeries->barSets();
380 for (int i = 0; i < barsets.count(); i++) {
381 seriesColorHex = "#" + QString::number(barsets.at(i)->brush().color().rgb(), 16).right(6).toUpper();
382 m_model->addMapping(seriesColorHex, QRect(2 + i, 0, 1, 1000));
383 }
384 }
385
386
387 if (!m_barRadioButton->isChecked()) {
388 m_chart->axisX()->setRange(0, 500);
389 m_chart->axisY()->setRange(0, 220);
390 }
299 }
300 // else if (m_scatterRadioButton->isChecked())
301 // {
302 // m_chart->setAnimationOptions(QChart::NoAnimation);
303
304 // // series 1
305 // m_series = new QScatterSeries;
306 // m_series->setModel(m_model);
307 // m_series->setModelMapping(0,1, Qt::Vertical);
308 // // m_series->setModelMappingRange(2, 0);
309 // // series->setModelMapping(0,1, Qt::Horizontal);
310 // m_chart->addSeries(m_series);
311
312 // seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
313 // m_model->addMapping(seriesColorHex, QRect(0, 2, 2, 1000));
314
315 // // series 2
316 // m_series = new QScatterSeries;
317 // m_series->setModel(m_model);
318 // m_series->setModelMapping(2,3, Qt::Vertical);
319 // // m_series->setModelMappingRange(1, 6);
320 // // series->setModelMapping(2,3, Qt::Horizontal);
321 // m_chart->addSeries(m_series);
322
323 // seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
324 // m_model->addMapping(seriesColorHex, QRect(2, 1, 2, 6));
325
326 // // series 3
327 // m_series = new QScatterSeries;
328 // m_series->setModel(m_model);
329 // m_series->setModelMapping(4,5, Qt::Vertical);
330 // // series->setModelMapping(4,5, Qt::Horizontal);
331 // m_chart->addSeries(m_series);
332 // seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
333 // m_model->addMapping(seriesColorHex, QRect(4, 0, 2, 1000));
334 // }
335 else if (m_pieRadioButton->isChecked())
336 {
337 m_chart->setAnimationOptions(QChart::SeriesAnimations);
338
339 // pie 1
340 QPieSeries* pieSeries = new QPieSeries;
341 pieSeries->setModel(m_model);
342
343 QPieModelMapper *mapper = new QPieModelMapper;
344 mapper->setMapValues(1);
345 mapper->setMapLabels(1);
346 mapper->setFirst(2);
347 mapper->setCount(5);
348 pieSeries->setModelMapper(mapper);
349
350 pieSeries->setLabelsVisible(true);
351 pieSeries->setPieSize(0.75);
352 // pieSeries->setHorizontalPosition(0.2);
353 // pieSeries->setVerticalPosition(0.3);
354
355 m_chart->addSeries(pieSeries);
356 seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
357 m_model->addMapping(seriesColorHex, QRect(0, 2, 2, 5));
358
359 // // pie 2
360 // pieSeries = new QPieSeries;
361 // pieSeries->setModel(m_model);
362
363 // pieSeries->setModelMapping(1,1, Qt::Vertical);
364 // pieSeries->setModelMappingRange(2, -1);
365 // pieSeries->setLabelsVisible(true);
366 // pieSeries->setPieSize(0.35);
367 // pieSeries->setHorizontalPosition(0.8);
368 // pieSeries->setVerticalPosition(0.3);
369 // m_chart->addSeries(pieSeries);
370 // seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
371 // m_model->addMapping(seriesColorHex, QRect(1, 2, 1, 1000));
372
373 // // pie 3
374 // pieSeries = new QPieSeries;
375 // pieSeries->setModel(m_model);
376 // pieSeries->setModelMapping(2,2, Qt::Vertical);
377 // pieSeries->setLabelsVisible(true);
378 // pieSeries->setPieSize(0.35);
379 // pieSeries->setHorizontalPosition(0.5);
380 // pieSeries->setVerticalPosition(0.75);
381 // m_chart->addSeries(pieSeries);
382 // seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
383 // m_model->addMapping(seriesColorHex, QRect(2, 0, 1, 1000));
384
385 // // special pie
386 // specialPie = new QPieSeries;
387 // specialPie->append(17, "1");
388 // specialPie->append(45, "2");
389 // specialPie->append(77, "3");
390 // specialPie->append(37, "4");
391 // specialPie->append(27, "5");
392 // specialPie->append(47, "6");
393 // specialPie->setPieSize(0.35);
394 // specialPie->setHorizontalPosition(0.8);
395 // specialPie->setVerticalPosition(0.75);
396 // specialPie->setLabelsVisible(true);
397 // m_chart->addSeries(specialPie);
398 }
399 // else if (m_areaRadioButton->isChecked())
400 // {
401 // m_chart->setAnimationOptions(QChart::NoAnimation);
402
403 // QLineSeries* upperLineSeries = new QLineSeries;
404 // upperLineSeries->setModel(m_model);
405 // upperLineSeries->setModelMapping(0, 1, Qt::Vertical);
406 // // upperLineSeries->setModelMappingRange(1, 5);
407 // QLineSeries* lowerLineSeries = new QLineSeries;
408 // lowerLineSeries->setModel(m_model);
409 // lowerLineSeries->setModelMapping(2, 3, Qt::Vertical);
410 // QAreaSeries* areaSeries = new QAreaSeries(upperLineSeries, lowerLineSeries);
411 // m_chart->addSeries(areaSeries);
412 // seriesColorHex = "#" + QString::number(areaSeries->brush().color().rgb(), 16).right(6).toUpper();
413 // m_model->addMapping(seriesColorHex, QRect(0, 1, 2, 5));
414 // m_model->addMapping(seriesColorHex, QRect(2, 0, 2, 1000));
415 // }
416 // else if (m_barRadioButton->isChecked())
417 // {
418 // m_chart->setAnimationOptions(QChart::SeriesAnimations);
419
420 // QBarSeries* barSeries = new QBarSeries();
421 // barSeries->setCategories(QStringList());
422 // barSeries->setModel(m_model);
423 // // barSeries->setModelMappingRange(2, 5);
424 // barSeries->setModelMapping(5, 2, 4, Qt::Vertical);
425 // m_chart->addSeries(barSeries);
426 // QList<QBarSet*> barsets = barSeries->barSets();
427 // for (int i = 0; i < barsets.count(); i++) {
428 // seriesColorHex = "#" + QString::number(barsets.at(i)->brush().color().rgb(), 16).right(6).toUpper();
429 // m_model->addMapping(seriesColorHex, QRect(2 + i, 0, 1, 1000));
430 // }
431 // }
432
433
434 // if (!m_barRadioButton->isChecked()) {
435 // m_chart->axisX()->setRange(0, 500);
436 // m_chart->axisY()->setRange(0, 220);
437 // }
391 m_chart->legend()->setVisible(true);
438 m_chart->legend()->setVisible(true);
392
439
393 // repaint table view colors
440 // repaint table view colors
394 m_tableView->repaint();
441 m_tableView->repaint();
395 m_tableView->setFocus();
442 m_tableView->setFocus();
396 }
443 }
397 }
444 }
398
445
399 void TableWidget::testPie()
446 void TableWidget::testPie()
400 {
447 {
401 m_tableView->setColumnWidth(10, 250);
448 m_series->modelMapper()->setMapX(4);
449 // m_tableView->setColumnWidth(10, 250);
402 // if (specialPie) {
450 // if (specialPie) {
403 // specialPie->remove(specialPie->slices().at(2));
451 // specialPie->remove(specialPie->slices().at(2));
404 // // specialPie->insert(4, new QPieSlice(45, "Hello"));//specialPie->slices.at(2));
452 // // specialPie->insert(4, new QPieSlice(45, "Hello"));//specialPie->slices.at(2));
405 // specialPie->append(4, "heloo");
453 // specialPie->append(4, "heloo");
406 // }
454 // }
407 }
455 }
408
456
409 TableWidget::~TableWidget()
457 TableWidget::~TableWidget()
410 {
458 {
411
459
412 }
460 }
General Comments 0
You need to be logged in to leave comments. Login now