##// END OF EJS Templates
Added Vertical and Horizontal QXYModelMapper
Marek Rosa -
r1252:4e92004ccd6b
parent child
Show More
@@ -0,0 +1,32
1 #include "qhxymodelmapper.h"
2
3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4
5 QHXYModelMapper::QHXYModelMapper(QObject *parent) :
6 QXYModelMapper(parent)
7 {
8 }
9
10 int QHXYModelMapper::xRow() const
11 {
12 return QXYModelMapper::xSection();
13 }
14
15 void QHXYModelMapper::setXRow(int xRow)
16 {
17 return QXYModelMapper::setXSection(xRow);
18 }
19
20 int QHXYModelMapper::yRow() const
21 {
22 return QXYModelMapper::ySection();
23 }
24
25 void QHXYModelMapper::setYRow(int yRow)
26 {
27 return QXYModelMapper::setYSection(yRow);
28 }
29
30 #include "moc_qhxymodelmapper.cpp"
31
32 QTCOMMERCIALCHART_END_NAMESPACE
@@ -0,0 +1,27
1 #ifndef QHXYMODELMAPPER_H
2 #define QHXYMODELMAPPER_H
3
4 #include <QXYModelMapper>
5
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7
8 class QTCOMMERCIALCHART_EXPORT QHXYModelMapper : public QXYModelMapper
9 {
10 Q_OBJECT
11 Q_PROPERTY(int xRow READ xRow WRITE setXRow)
12 Q_PROPERTY(int yRow READ yRow WRITE setYRow)
13
14 public:
15 explicit QHXYModelMapper(QObject *parent = 0);
16
17 int xRow() const;
18 void setXRow(int xRow);
19
20 int yRow() const;
21 void setYRow(int yRow);
22
23 };
24
25 QTCOMMERCIALCHART_END_NAMESPACE
26
27 #endif // QHXYMODELMAPPER_H
@@ -0,0 +1,32
1 #include "qvxymodelmapper.h"
2
3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4
5 QVXYModelMapper::QVXYModelMapper(QObject *parent) :
6 QXYModelMapper(parent)
7 {
8 }
9
10 int QVXYModelMapper::xColumn() const
11 {
12 return QXYModelMapper::xSection();
13 }
14
15 void QVXYModelMapper::setXColumn(int xColumn)
16 {
17 return QXYModelMapper::setXSection(xColumn);
18 }
19
20 int QVXYModelMapper::yColumn() const
21 {
22 return QXYModelMapper::ySection();
23 }
24
25 void QVXYModelMapper::setYColumn(int yColumn)
26 {
27 return QXYModelMapper::setYSection(yColumn);
28 }
29
30 #include "moc_qvxymodelmapper.cpp"
31
32 QTCOMMERCIALCHART_END_NAMESPACE
@@ -0,0 +1,27
1 #ifndef QVXYMODELMAPPER_H
2 #define QVXYMODELMAPPER_H
3
4 #include <QXYModelMapper>
5
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7
8 class QTCOMMERCIALCHART_EXPORT QVXYModelMapper : public QXYModelMapper
9 {
10 Q_OBJECT
11 Q_PROPERTY(int xColumn READ xColumn WRITE setXColumn)
12 Q_PROPERTY(int yColumn READ yColumn WRITE setYColumn)
13
14 public:
15 explicit QVXYModelMapper(QObject *parent = 0);
16
17 int xColumn() const;
18 void setXColumn(int xColumn);
19
20 int yColumn() const;
21 void setYColumn(int yColumn);
22
23 };
24
25 QTCOMMERCIALCHART_END_NAMESPACE
26
27 #endif // QVXYMODELMAPPER_H
@@ -1,111 +1,112
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 "declarativemodel.h"
21 #include "declarativemodel.h"
22 #include <qdeclarativelist.h>
22 #include <qdeclarativelist.h>
23 #include <QDebug>
23 #include <QDebug>
24
24
25 QTCOMMERCIALCHART_BEGIN_NAMESPACE
25 QTCOMMERCIALCHART_BEGIN_NAMESPACE
26
26
27
27
28 ////////////// Table model row ///////////////////
28 ////////////// Table model row ///////////////////
29
29
30 DeclarativeTableModelElement::DeclarativeTableModelElement(QObject *parent)
30 DeclarativeTableModelElement::DeclarativeTableModelElement(QObject *parent)
31 : QObject(parent)
31 : QObject(parent)
32 {
32 {
33 }
33 }
34
34
35 QVariantList DeclarativeTableModelElement::values()
35 QVariantList DeclarativeTableModelElement::values()
36 {
36 {
37 return m_values;
37 return m_values;
38 }
38 }
39
39
40 void DeclarativeTableModelElement::setValues(QVariantList values)
40 void DeclarativeTableModelElement::setValues(QVariantList values)
41 {
41 {
42 m_values = values;
42 m_values = values;
43 }
43 }
44
44
45 ////////////// Table model ///////////////////
45 ////////////// Table model ///////////////////
46
46
47 DeclarativeTableModel::DeclarativeTableModel(QObject *parent) :
47 DeclarativeTableModel::DeclarativeTableModel(QObject *parent) :
48 ChartTableModel(parent)
48 ChartTableModel(parent)
49 {
49 {
50 }
50 }
51
51
52 void DeclarativeTableModel::classBegin()
52 void DeclarativeTableModel::classBegin()
53 {
53 {
54 }
54 }
55
55
56 void DeclarativeTableModel::componentComplete()
56 void DeclarativeTableModel::componentComplete()
57 {
57 {
58 foreach (QObject *child, children()) {
58 foreach (QObject *child, children()) {
59 if (qobject_cast<DeclarativeTableModelElement *>(child)) {
59 if (qobject_cast<DeclarativeTableModelElement *>(child)) {
60 append(qobject_cast<DeclarativeTableModelElement *>(child)->values());
60 append(qobject_cast<DeclarativeTableModelElement *>(child)->values());
61 }
61 }
62 }
62 }
63 }
63 }
64
64
65 QDeclarativeListProperty<QObject> DeclarativeTableModel::modelChildren()
65 QDeclarativeListProperty<QObject> DeclarativeTableModel::modelChildren()
66 {
66 {
67 return QDeclarativeListProperty<QObject>(this, 0, &DeclarativeTableModel::appendModelChild);
67 return QDeclarativeListProperty<QObject>(this, 0, &DeclarativeTableModel::appendModelChild);
68 }
68 }
69
69
70 void DeclarativeTableModel::appendModelChild(QDeclarativeListProperty<QObject> *list,
70 void DeclarativeTableModel::appendModelChild(QDeclarativeListProperty<QObject> *list,
71 QObject *child)
71 QObject *child)
72 {
72 {
73 // childs are added in componentComplete instead
73 // childs are added in componentComplete instead
74 Q_UNUSED(list)
74 Q_UNUSED(list)
75 Q_UNUSED(child)
75 Q_UNUSED(child)
76 }
76 }
77
77
78 void DeclarativeTableModel::append(QVariantList values)
78 void DeclarativeTableModel::append(QVariantList values)
79 {
79 {
80 // qDebug() << "DeclarativeTableModel::append:" << values;
80 // qDebug() << "DeclarativeTableModel::append:" << values;
81
81
82 while (columnCount() < values.count())
82 while (columnCount() < values.count())
83 insertColumn(columnCount());
83 insertColumn(columnCount());
84
84
85 insertRow(rowCount());
85 insertRow(rowCount());
86
86
87 QModelIndex beginIndex = QModelIndex();
87 QModelIndex beginIndex = QModelIndex();
88 QModelIndex endIndex = QModelIndex();
88 QModelIndex endIndex = QModelIndex();
89 for (int i(0); i < values.count(); i++) {
89 for (int i(0); i < values.count(); i++) {
90 QModelIndex modelIndex = createIndex(rowCount() - 1, i);
90 QModelIndex modelIndex = createIndex(rowCount() - 1, i);
91 if (i == 0)
91 if (i == 0)
92 beginIndex = modelIndex;
92 beginIndex = modelIndex;
93 if (i == (values.count() - 1))
93 if (i == (values.count() - 1))
94 endIndex = modelIndex;
94 endIndex = modelIndex;
95 setData(modelIndex, values.at(i));
95 setData(modelIndex, values.at(i));
96 }
96 }
97 dataChanged(beginIndex, endIndex);
97 dataChanged(beginIndex, endIndex);
98 }
98 }
99
99
100 void DeclarativeTableModel::appendPoint(QXYModelMapper *mapper, DeclarativeXyPoint *point)
100 void DeclarativeTableModel::appendPoint(QXYModelMapper *mapper, DeclarativeXyPoint *point)
101 {
101 {
102 // qDebug() << "DeclarativeTableModel::appendPoint:" << point;
102 // qDebug() << "DeclarativeTableModel::appendPoint:" << point;
103 QVariantList values;
103 QVariantList values;
104 values.insert(mapper->mapX(), point->x());
104 // TODO: XYModelMapper implementation has change, this code has to be updated.
105 values.insert(mapper->mapY(), point->y());
105 // values.insert(mapper->mapX(), point->x());
106 // values.insert(mapper->mapY(), point->y());
106 append(values);
107 append(values);
107 }
108 }
108
109
109 #include "moc_declarativemodel.cpp"
110 #include "moc_declarativemodel.cpp"
110
111
111 QTCOMMERCIALCHART_END_NAMESPACE
112 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,85 +1,87
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 "declarativechart.h"
23 #include "declarativechart.h"
24 #include <QXYSeries>
24 #include <QXYSeries>
25 #include <QXYModelMapper>
25 #include <QXYModelMapper>
26 #include <QDeclarativeListProperty>
26 #include <QDeclarativeListProperty>
27
27
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29
29
30 DeclarativeXySeries::DeclarativeXySeries()
30 DeclarativeXySeries::DeclarativeXySeries()
31 {
31 {
32 // TODO: XYModelMapper implementation has changed, this code has to be updated
33
32 // All the inherited objects must be of type QXYSeries, so it is safe to cast
34 // All the inherited objects must be of type QXYSeries, so it is safe to cast
33 QXYSeries *series = reinterpret_cast<QXYSeries *>(this);
35 // QXYSeries *series = reinterpret_cast<QXYSeries *>(this);
34 // TODO: mapper should be available on the series by default
36 // // TODO: mapper should be available on the series by default
35 QXYModelMapper *mapper = new QXYModelMapper(series);
37 // QXYModelMapper *mapper = new QXYModelMapper(series);
36 mapper->setMapX(0);
38 // mapper->setMapX(0);
37 mapper->setMapY(1);
39 // mapper->setMapY(1);
38 mapper->setFirst(0);
40 // mapper->setFirst(0);
39 mapper->setCount(-1);
41 // mapper->setCount(-1);
40 mapper->setOrientation(Qt::Vertical);
42 // mapper->setOrientation(Qt::Vertical);
41 // series->setModelMapper(mapper);
43 // series->setModelMapper(mapper);
42 }
44 }
43
45
44 DeclarativeXySeries::~DeclarativeXySeries()
46 DeclarativeXySeries::~DeclarativeXySeries()
45 {
47 {
46 }
48 }
47
49
48 bool DeclarativeXySeries::setDeclarativeModel(DeclarativeTableModel *model)
50 bool DeclarativeXySeries::setDeclarativeModel(DeclarativeTableModel *model)
49 {
51 {
50 QAbstractItemModel *m = qobject_cast<QAbstractItemModel *>(model);
52 QAbstractItemModel *m = qobject_cast<QAbstractItemModel *>(model);
51 bool value(false);
53 bool value(false);
52 if (m) {
54 if (m) {
53 // All the inherited objects must be of type QXYSeries, so it is safe to cast
55 // All the inherited objects must be of type QXYSeries, so it is safe to cast
54 // QXYSeries *series = reinterpret_cast<QXYSeries *>(this);
56 // QXYSeries *series = reinterpret_cast<QXYSeries *>(this);
55 // series->setModel(m);
57 // series->setModel(m);
56 } else {
58 } else {
57 qWarning("DeclarativeXySeries: Illegal model");
59 qWarning("DeclarativeXySeries: Illegal model");
58 }
60 }
59 return value;
61 return value;
60 }
62 }
61
63
62 DeclarativeTableModel *DeclarativeXySeries::declarativeModel()
64 DeclarativeTableModel *DeclarativeXySeries::declarativeModel()
63 {
65 {
64 // All the inherited objects must be of type QXYSeries, so it is safe to cast
66 // All the inherited objects must be of type QXYSeries, so it is safe to cast
65 // QXYSeries *series = reinterpret_cast<QXYSeries *>(this);
67 // QXYSeries *series = reinterpret_cast<QXYSeries *>(this);
66 return 0; //qobject_cast<DeclarativeTableModel *>(series->model());
68 return 0; //qobject_cast<DeclarativeTableModel *>(series->model());
67 }
69 }
68
70
69 QColor DeclarativeXySeries::color()
71 QColor DeclarativeXySeries::color()
70 {
72 {
71 // All the inherited objects must be of type QXYSeries, so it is safe to cast
73 // All the inherited objects must be of type QXYSeries, so it is safe to cast
72 QXYSeries *series = reinterpret_cast<QXYSeries *>(this);
74 QXYSeries *series = reinterpret_cast<QXYSeries *>(this);
73 return series->pen().color();
75 return series->pen().color();
74 }
76 }
75
77
76 void DeclarativeXySeries::setColor(QColor color)
78 void DeclarativeXySeries::setColor(QColor color)
77 {
79 {
78 QXYSeries *series = reinterpret_cast<QXYSeries *>(this);
80 QXYSeries *series = reinterpret_cast<QXYSeries *>(this);
79 QPen pen = series->pen();
81 QPen pen = series->pen();
80 pen.setColor(color);
82 pen.setColor(color);
81 series->setPen(pen);
83 series->setPen(pen);
82 }
84 }
83
85
84
86
85 QTCOMMERCIALCHART_END_NAMESPACE
87 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,493 +1,492
1 #include "qpiemodelmapper_p.h"
1 #include "qpiemodelmapper_p.h"
2 #include "qpiemodelmapper.h"
2 #include "qpiemodelmapper.h"
3 #include "qpieseries.h"
3 #include "qpieseries.h"
4 #include "qpieslice.h"
4 #include "qpieslice.h"
5 #include <QAbstractItemModel>
5 #include <QAbstractItemModel>
6
6
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8
8
9 QPieModelMapper::QPieModelMapper(QObject *parent) :
9 QPieModelMapper::QPieModelMapper(QObject *parent) :
10 QObject(parent),
10 QObject(parent),
11 d_ptr(new QPieModelMapperPrivate(this))
11 d_ptr(new QPieModelMapperPrivate(this))
12 {
12 {
13 }
13 }
14
14
15 QAbstractItemModel* QPieModelMapper::model() const
15 QAbstractItemModel* QPieModelMapper::model() const
16 {
16 {
17 Q_D(const QPieModelMapper);
17 Q_D(const QPieModelMapper);
18 return d->m_model;
18 return d->m_model;
19 }
19 }
20
20
21 void QPieModelMapper::setModel(QAbstractItemModel *model)
21 void QPieModelMapper::setModel(QAbstractItemModel *model)
22 {
22 {
23 if (model == 0)
23 if (model == 0)
24 return;
24 return;
25
25
26 Q_D(QPieModelMapper);
26 Q_D(QPieModelMapper);
27 if (d->m_model) {
27 if (d->m_model) {
28 disconnect(d->m_model, 0, d, 0);
28 disconnect(d->m_model, 0, d, 0);
29 }
29 }
30
30
31 d->m_model = model;
31 d->m_model = model;
32 d->initializePieFromModel();
32 d->initializePieFromModel();
33 // connect signals from the model
33 // connect signals from the model
34 connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex)));
34 connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex)));
35 connect(d->m_model, SIGNAL(rowsInserted(QModelIndex,int,int)), d, SLOT(modelRowsAdded(QModelIndex,int,int)));
35 connect(d->m_model, SIGNAL(rowsInserted(QModelIndex,int,int)), d, SLOT(modelRowsAdded(QModelIndex,int,int)));
36 connect(d->m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)), d, SLOT(modelRowsRemoved(QModelIndex,int,int)));
36 connect(d->m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)), d, SLOT(modelRowsRemoved(QModelIndex,int,int)));
37 connect(d->m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), d, SLOT(modelColumnsAdded(QModelIndex,int,int)));
37 connect(d->m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), d, SLOT(modelColumnsAdded(QModelIndex,int,int)));
38 connect(d->m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), d, SLOT(modelColumnsRemoved(QModelIndex,int,int)));
38 connect(d->m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), d, SLOT(modelColumnsRemoved(QModelIndex,int,int)));
39 }
39 }
40
40
41 QPieSeries* QPieModelMapper::series() const
41 QPieSeries* QPieModelMapper::series() const
42 {
42 {
43 Q_D(const QPieModelMapper);
43 Q_D(const QPieModelMapper);
44 return d->m_series;
44 return d->m_series;
45 }
45 }
46
46
47 void QPieModelMapper::setSeries(QPieSeries *series)
47 void QPieModelMapper::setSeries(QPieSeries *series)
48 {
48 {
49 if (series == 0)
50 return;
51
52 Q_D(QPieModelMapper);
49 Q_D(QPieModelMapper);
53 if (d->m_series) {
50 if (d->m_series) {
54 disconnect(d->m_series, 0, d, 0);
51 disconnect(d->m_series, 0, d, 0);
55 }
52 }
56
53
54 if (series == 0)
55 return;
56
57 d->m_series = series;
57 d->m_series = series;
58 d->initializePieFromModel();
58 d->initializePieFromModel();
59 // connect the signals from the series
59 // connect the signals from the series
60 connect(d->m_series, SIGNAL(added(QList<QPieSlice*>)), d, SLOT(slicesAdded(QList<QPieSlice*>)));
60 connect(d->m_series, SIGNAL(added(QList<QPieSlice*>)), d, SLOT(slicesAdded(QList<QPieSlice*>)));
61 connect(d->m_series, SIGNAL(removed(QList<QPieSlice*>)), d, SLOT(slicesRemoved(QList<QPieSlice*>)));
61 connect(d->m_series, SIGNAL(removed(QList<QPieSlice*>)), d, SLOT(slicesRemoved(QList<QPieSlice*>)));
62 // connect(d->m_model, SIGNAL(), d, SLOT());
63 }
62 }
64
63
65 int QPieModelMapper::first() const
64 int QPieModelMapper::first() const
66 {
65 {
67 Q_D(const QPieModelMapper);
66 Q_D(const QPieModelMapper);
68 return d->m_first;
67 return d->m_first;
69 }
68 }
70
69
71 void QPieModelMapper::setFirst(int first)
70 void QPieModelMapper::setFirst(int first)
72 {
71 {
73 Q_D(QPieModelMapper);
72 Q_D(QPieModelMapper);
74 d->m_first = qMax(first, 0);
73 d->m_first = qMax(first, 0);
75 d->initializePieFromModel();
74 d->initializePieFromModel();
76 }
75 }
77
76
78 int QPieModelMapper::count() const
77 int QPieModelMapper::count() const
79 {
78 {
80 Q_D(const QPieModelMapper);
79 Q_D(const QPieModelMapper);
81 return d->m_count;
80 return d->m_count;
82 }
81 }
83
82
84 void QPieModelMapper::setCount(int count)
83 void QPieModelMapper::setCount(int count)
85 {
84 {
86 Q_D(QPieModelMapper);
85 Q_D(QPieModelMapper);
87 d->m_count = qMax(count, -1);
86 d->m_count = qMax(count, -1);
88 d->initializePieFromModel();
87 d->initializePieFromModel();
89 }
88 }
90
89
91 Qt::Orientation QPieModelMapper::orientation() const
90 Qt::Orientation QPieModelMapper::orientation() const
92 {
91 {
93 Q_D(const QPieModelMapper);
92 Q_D(const QPieModelMapper);
94 return d->m_orientation;
93 return d->m_orientation;
95 }
94 }
96
95
97 void QPieModelMapper::setOrientation(Qt::Orientation orientation)
96 void QPieModelMapper::setOrientation(Qt::Orientation orientation)
98 {
97 {
99 Q_D(QPieModelMapper);
98 Q_D(QPieModelMapper);
100 d->m_orientation = orientation;
99 d->m_orientation = orientation;
101 d->initializePieFromModel();
100 d->initializePieFromModel();
102 }
101 }
103
102
104 int QPieModelMapper::valuesIndex() const
103 int QPieModelMapper::valuesIndex() const
105 {
104 {
106 Q_D(const QPieModelMapper);
105 Q_D(const QPieModelMapper);
107 return d->m_valuesIndex;
106 return d->m_valuesIndex;
108 }
107 }
109
108
110 void QPieModelMapper::setValuesIndex(int valuesIndex)
109 void QPieModelMapper::setValuesIndex(int valuesIndex)
111 {
110 {
112 Q_D(QPieModelMapper);
111 Q_D(QPieModelMapper);
113 d->m_valuesIndex = qMax(-1, valuesIndex);
112 d->m_valuesIndex = qMax(-1, valuesIndex);
114 d->initializePieFromModel();
113 d->initializePieFromModel();
115 }
114 }
116
115
117 int QPieModelMapper::labelsIndex() const
116 int QPieModelMapper::labelsIndex() const
118 {
117 {
119 Q_D(const QPieModelMapper);
118 Q_D(const QPieModelMapper);
120 return d->m_labelsIndex;
119 return d->m_labelsIndex;
121 }
120 }
122
121
123 void QPieModelMapper::setLabelsIndex(int labelsIndex)
122 void QPieModelMapper::setLabelsIndex(int labelsIndex)
124 {
123 {
125 Q_D(QPieModelMapper);
124 Q_D(QPieModelMapper);
126 d->m_labelsIndex = qMax(-1, labelsIndex);
125 d->m_labelsIndex = qMax(-1, labelsIndex);
127 d->initializePieFromModel();
126 d->initializePieFromModel();
128 }
127 }
129
128
130 void QPieModelMapper::reset()
129 void QPieModelMapper::reset()
131 {
130 {
132 Q_D(QPieModelMapper);
131 Q_D(QPieModelMapper);
133 d->m_first = 0;
132 d->m_first = 0;
134 d->m_count = -1;
133 d->m_count = -1;
135 d->m_orientation = Qt::Vertical;
134 d->m_orientation = Qt::Vertical;
136 d->m_valuesIndex = -1;
135 d->m_valuesIndex = -1;
137 d->m_labelsIndex = -1;
136 d->m_labelsIndex = -1;
138 }
137 }
139
138
140 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
139 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
141
140
142 QPieModelMapperPrivate::QPieModelMapperPrivate(QPieModelMapper *q) :
141 QPieModelMapperPrivate::QPieModelMapperPrivate(QPieModelMapper *q) :
143 m_series(0),
142 m_series(0),
144 m_model(0),
143 m_model(0),
145 m_first(0),
144 m_first(0),
146 m_count(-1),
145 m_count(-1),
147 m_orientation(Qt::Vertical),
146 m_orientation(Qt::Vertical),
148 m_valuesIndex(-1),
147 m_valuesIndex(-1),
149 m_labelsIndex(-1),
148 m_labelsIndex(-1),
150 m_seriesSignalsBlock(false),
149 m_seriesSignalsBlock(false),
151 m_modelSignalsBlock(false),
150 m_modelSignalsBlock(false),
152 q_ptr(q)
151 q_ptr(q)
153 {
152 {
154 }
153 }
155
154
156 void QPieModelMapperPrivate::blockModelSignals(bool block)
155 void QPieModelMapperPrivate::blockModelSignals(bool block)
157 {
156 {
158 m_modelSignalsBlock = block;
157 m_modelSignalsBlock = block;
159 }
158 }
160
159
161 void QPieModelMapperPrivate::blockSeriesSignals(bool block)
160 void QPieModelMapperPrivate::blockSeriesSignals(bool block)
162 {
161 {
163 m_seriesSignalsBlock = block;
162 m_seriesSignalsBlock = block;
164 }
163 }
165
164
166
165
167 QPieSlice* QPieModelMapperPrivate::pieSlice(QModelIndex index) const
166 QPieSlice* QPieModelMapperPrivate::pieSlice(QModelIndex index) const
168 {
167 {
169 if (m_orientation == Qt::Vertical && (index.column() == m_valuesIndex || index.column() == m_labelsIndex)) {
168 if (m_orientation == Qt::Vertical && (index.column() == m_valuesIndex || index.column() == m_labelsIndex)) {
170 if (index.row() >= m_first && (m_count == - 1 || index.row() < m_first + m_count))
169 if (index.row() >= m_first && (m_count == - 1 || index.row() < m_first + m_count))
171 return m_series->slices().at(index.row() - m_first);
170 return m_series->slices().at(index.row() - m_first);
172 } else if (m_orientation == Qt::Horizontal && (index.row() == m_valuesIndex || index.row() == m_labelsIndex)) {
171 } else if (m_orientation == Qt::Horizontal && (index.row() == m_valuesIndex || index.row() == m_labelsIndex)) {
173 if (index.column() >= m_first && (m_count == - 1 || index.column() < m_first + m_count))
172 if (index.column() >= m_first && (m_count == - 1 || index.column() < m_first + m_count))
174 return m_series->slices().at(index.column() - m_first);
173 return m_series->slices().at(index.column() - m_first);
175 }
174 }
176 return 0; // This part of model has not been mapped to any slice
175 return 0; // This part of model has not been mapped to any slice
177 }
176 }
178
177
179 QModelIndex QPieModelMapperPrivate::valueModelIndex(int slicePos)
178 QModelIndex QPieModelMapperPrivate::valueModelIndex(int slicePos)
180 {
179 {
181 if (m_count != -1 && slicePos >= m_count)
180 if (m_count != -1 && slicePos >= m_count)
182 return QModelIndex(); // invalid
181 return QModelIndex(); // invalid
183
182
184 if (m_orientation == Qt::Vertical)
183 if (m_orientation == Qt::Vertical)
185 return m_model->index(slicePos + m_first, m_valuesIndex);
184 return m_model->index(slicePos + m_first, m_valuesIndex);
186 else
185 else
187 return m_model->index(m_valuesIndex, slicePos + m_first);
186 return m_model->index(m_valuesIndex, slicePos + m_first);
188 }
187 }
189
188
190 QModelIndex QPieModelMapperPrivate::labelModelIndex(int slicePos)
189 QModelIndex QPieModelMapperPrivate::labelModelIndex(int slicePos)
191 {
190 {
192 if (m_count != -1 && slicePos >= m_count)
191 if (m_count != -1 && slicePos >= m_count)
193 return QModelIndex(); // invalid
192 return QModelIndex(); // invalid
194
193
195 if (m_orientation == Qt::Vertical)
194 if (m_orientation == Qt::Vertical)
196 return m_model->index(slicePos + m_first, m_labelsIndex);
195 return m_model->index(slicePos + m_first, m_labelsIndex);
197 else
196 else
198 return m_model->index(m_labelsIndex, slicePos + m_first);
197 return m_model->index(m_labelsIndex, slicePos + m_first);
199 }
198 }
200
199
201 bool QPieModelMapperPrivate::isLabelIndex(QModelIndex index) const
200 bool QPieModelMapperPrivate::isLabelIndex(QModelIndex index) const
202 {
201 {
203 if (m_orientation == Qt::Vertical && index.column() == m_labelsIndex)
202 if (m_orientation == Qt::Vertical && index.column() == m_labelsIndex)
204 return true;
203 return true;
205 else if (m_orientation == Qt::Horizontal && index.row() == m_labelsIndex)
204 else if (m_orientation == Qt::Horizontal && index.row() == m_labelsIndex)
206 return true;
205 return true;
207
206
208 return false;
207 return false;
209 }
208 }
210
209
211 bool QPieModelMapperPrivate::isValueIndex(QModelIndex index) const
210 bool QPieModelMapperPrivate::isValueIndex(QModelIndex index) const
212 {
211 {
213 if (m_orientation == Qt::Vertical && index.column() == m_valuesIndex)
212 if (m_orientation == Qt::Vertical && index.column() == m_valuesIndex)
214 return true;
213 return true;
215 else if (m_orientation == Qt::Horizontal && index.row() == m_valuesIndex)
214 else if (m_orientation == Qt::Horizontal && index.row() == m_valuesIndex)
216 return true;
215 return true;
217
216
218 return false;
217 return false;
219 }
218 }
220
219
221 void QPieModelMapperPrivate::slicesAdded(QList<QPieSlice*> slices)
220 void QPieModelMapperPrivate::slicesAdded(QList<QPieSlice*> slices)
222 {
221 {
223 if (m_seriesSignalsBlock)
222 if (m_seriesSignalsBlock)
224 return;
223 return;
225
224
226 if (slices.count() == 0)
225 if (slices.count() == 0)
227 return;
226 return;
228
227
229 int firstIndex = m_series->slices().indexOf(slices.at(0));
228 int firstIndex = m_series->slices().indexOf(slices.at(0));
230 if (firstIndex == -1)
229 if (firstIndex == -1)
231 return;
230 return;
232
231
233 if (m_count != -1)
232 if (m_count != -1)
234 m_count += slices.count();
233 m_count += slices.count();
235
234
236 for (int i = firstIndex; i < firstIndex + slices.count(); i++) {
235 for (int i = firstIndex; i < firstIndex + slices.count(); i++) {
237 m_slices.insert(i, slices.at(i - firstIndex));
236 m_slices.insert(i, slices.at(i - firstIndex));
238 connect(slices.at(i - firstIndex), SIGNAL(labelChanged()), this, SLOT(sliceLabelChanged()));
237 connect(slices.at(i - firstIndex), SIGNAL(labelChanged()), this, SLOT(sliceLabelChanged()));
239 connect(slices.at(i - firstIndex), SIGNAL(valueChanged()), this, SLOT(sliceValueChanged()));
238 connect(slices.at(i - firstIndex), SIGNAL(valueChanged()), this, SLOT(sliceValueChanged()));
240 }
239 }
241
240
242 blockModelSignals();
241 blockModelSignals();
243 if (m_orientation == Qt::Vertical)
242 if (m_orientation == Qt::Vertical)
244 m_model->insertRows(firstIndex + m_first, slices.count());
243 m_model->insertRows(firstIndex + m_first, slices.count());
245 else
244 else
246 m_model->insertColumns(firstIndex + m_first, slices.count());
245 m_model->insertColumns(firstIndex + m_first, slices.count());
247
246
248 for(int i = firstIndex; i < firstIndex + slices.count(); i++) {
247 for(int i = firstIndex; i < firstIndex + slices.count(); i++) {
249 m_model->setData(valueModelIndex(i), slices.at(i - firstIndex)->value());
248 m_model->setData(valueModelIndex(i), slices.at(i - firstIndex)->value());
250 m_model->setData(labelModelIndex(i), slices.at(i - firstIndex)->label());
249 m_model->setData(labelModelIndex(i), slices.at(i - firstIndex)->label());
251 }
250 }
252 blockModelSignals(false);
251 blockModelSignals(false);
253 }
252 }
254
253
255 void QPieModelMapperPrivate::slicesRemoved(QList<QPieSlice*> slices)
254 void QPieModelMapperPrivate::slicesRemoved(QList<QPieSlice*> slices)
256 {
255 {
257 if (m_seriesSignalsBlock)
256 if (m_seriesSignalsBlock)
258 return;
257 return;
259
258
260 if (slices.count() == 0)
259 if (slices.count() == 0)
261 return;
260 return;
262
261
263 int firstIndex = m_slices.indexOf(slices.at(0));
262 int firstIndex = m_slices.indexOf(slices.at(0));
264 if (firstIndex == -1)
263 if (firstIndex == -1)
265 return;
264 return;
266
265
267 if (m_count != -1)
266 if (m_count != -1)
268 m_count -= slices.count();
267 m_count -= slices.count();
269
268
270 for (int i = firstIndex + slices.count() - 1; i >= firstIndex; i--)
269 for (int i = firstIndex + slices.count() - 1; i >= firstIndex; i--)
271 m_slices.removeAt(i);
270 m_slices.removeAt(i);
272
271
273 blockModelSignals();
272 blockModelSignals();
274 if (m_orientation == Qt::Vertical)
273 if (m_orientation == Qt::Vertical)
275 m_model->removeRows(firstIndex + m_first, slices.count());
274 m_model->removeRows(firstIndex + m_first, slices.count());
276 else
275 else
277 m_model->removeColumns(firstIndex + m_first, slices.count());
276 m_model->removeColumns(firstIndex + m_first, slices.count());
278 blockModelSignals(false);
277 blockModelSignals(false);
279 }
278 }
280
279
281 void QPieModelMapperPrivate::sliceLabelChanged()
280 void QPieModelMapperPrivate::sliceLabelChanged()
282 {
281 {
283 if (m_seriesSignalsBlock)
282 if (m_seriesSignalsBlock)
284 return;
283 return;
285
284
286 blockModelSignals();
285 blockModelSignals();
287 QPieSlice *slice = qobject_cast<QPieSlice *>(QObject::sender());
286 QPieSlice *slice = qobject_cast<QPieSlice *>(QObject::sender());
288 m_model->setData(labelModelIndex(m_series->slices().indexOf(slice)), slice->label());
287 m_model->setData(labelModelIndex(m_series->slices().indexOf(slice)), slice->label());
289 blockModelSignals(false);
288 blockModelSignals(false);
290 }
289 }
291
290
292 void QPieModelMapperPrivate::sliceValueChanged()
291 void QPieModelMapperPrivate::sliceValueChanged()
293 {
292 {
294 if (m_seriesSignalsBlock)
293 if (m_seriesSignalsBlock)
295 return;
294 return;
296
295
297 blockModelSignals();
296 blockModelSignals();
298 QPieSlice *slice = qobject_cast<QPieSlice *>(QObject::sender());
297 QPieSlice *slice = qobject_cast<QPieSlice *>(QObject::sender());
299 m_model->setData(valueModelIndex(m_series->slices().indexOf(slice)), slice->value());
298 m_model->setData(valueModelIndex(m_series->slices().indexOf(slice)), slice->value());
300 blockModelSignals(false);
299 blockModelSignals(false);
301 }
300 }
302
301
303 void QPieModelMapperPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
302 void QPieModelMapperPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
304 {
303 {
305 if (m_modelSignalsBlock)
304 if (m_modelSignalsBlock)
306 return;
305 return;
307
306
308 blockSeriesSignals();
307 blockSeriesSignals();
309 QModelIndex index;
308 QModelIndex index;
310 QPieSlice *slice;
309 QPieSlice *slice;
311 for (int row = topLeft.row(); row <= bottomRight.row(); row++) {
310 for (int row = topLeft.row(); row <= bottomRight.row(); row++) {
312 for (int column = topLeft.column(); column <= bottomRight.column(); column++) {
311 for (int column = topLeft.column(); column <= bottomRight.column(); column++) {
313 index = topLeft.sibling(row, column);
312 index = topLeft.sibling(row, column);
314 slice = pieSlice(index);
313 slice = pieSlice(index);
315 if (slice) {
314 if (slice) {
316 if (isValueIndex(index))
315 if (isValueIndex(index))
317 slice->setValue(m_model->data(index, Qt::DisplayRole).toReal());
316 slice->setValue(m_model->data(index, Qt::DisplayRole).toReal());
318 if (isLabelIndex(index))
317 if (isLabelIndex(index))
319 slice->setLabel(m_model->data(index, Qt::DisplayRole).toString());
318 slice->setLabel(m_model->data(index, Qt::DisplayRole).toString());
320 }
319 }
321 }
320 }
322 }
321 }
323 blockSeriesSignals(false);
322 blockSeriesSignals(false);
324 }
323 }
325
324
326
325
327 void QPieModelMapperPrivate::modelRowsAdded(QModelIndex parent, int start, int end)
326 void QPieModelMapperPrivate::modelRowsAdded(QModelIndex parent, int start, int end)
328 {
327 {
329 Q_UNUSED(parent);
328 Q_UNUSED(parent);
330 if (m_modelSignalsBlock)
329 if (m_modelSignalsBlock)
331 return;
330 return;
332
331
333 blockSeriesSignals();
332 blockSeriesSignals();
334 if (m_orientation == Qt::Vertical)
333 if (m_orientation == Qt::Vertical)
335 insertData(start, end);
334 insertData(start, end);
336 else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie
335 else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie
337 initializePieFromModel();
336 initializePieFromModel();
338 blockSeriesSignals(false);
337 blockSeriesSignals(false);
339 }
338 }
340
339
341 void QPieModelMapperPrivate::modelRowsRemoved(QModelIndex parent, int start, int end)
340 void QPieModelMapperPrivate::modelRowsRemoved(QModelIndex parent, int start, int end)
342 {
341 {
343 Q_UNUSED(parent);
342 Q_UNUSED(parent);
344 if (m_modelSignalsBlock)
343 if (m_modelSignalsBlock)
345 return;
344 return;
346
345
347 blockSeriesSignals();
346 blockSeriesSignals();
348 if (m_orientation == Qt::Vertical)
347 if (m_orientation == Qt::Vertical)
349 removeData(start, end);
348 removeData(start, end);
350 else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie
349 else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie
351 initializePieFromModel();
350 initializePieFromModel();
352 blockSeriesSignals(false);
351 blockSeriesSignals(false);
353 }
352 }
354
353
355 void QPieModelMapperPrivate::modelColumnsAdded(QModelIndex parent, int start, int end)
354 void QPieModelMapperPrivate::modelColumnsAdded(QModelIndex parent, int start, int end)
356 {
355 {
357 Q_UNUSED(parent);
356 Q_UNUSED(parent);
358 if (m_modelSignalsBlock)
357 if (m_modelSignalsBlock)
359 return;
358 return;
360
359
361 blockSeriesSignals();
360 blockSeriesSignals();
362 if (m_orientation == Qt::Horizontal)
361 if (m_orientation == Qt::Horizontal)
363 insertData(start, end);
362 insertData(start, end);
364 else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie
363 else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie
365 initializePieFromModel();
364 initializePieFromModel();
366 blockSeriesSignals(false);
365 blockSeriesSignals(false);
367 }
366 }
368
367
369 void QPieModelMapperPrivate::modelColumnsRemoved(QModelIndex parent, int start, int end)
368 void QPieModelMapperPrivate::modelColumnsRemoved(QModelIndex parent, int start, int end)
370 {
369 {
371 Q_UNUSED(parent);
370 Q_UNUSED(parent);
372 if (m_modelSignalsBlock)
371 if (m_modelSignalsBlock)
373 return;
372 return;
374
373
375 blockSeriesSignals();
374 blockSeriesSignals();
376 if (m_orientation == Qt::Horizontal)
375 if (m_orientation == Qt::Horizontal)
377 removeData(start, end);
376 removeData(start, end);
378 else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie
377 else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie
379 initializePieFromModel();
378 initializePieFromModel();
380 blockSeriesSignals(false);
379 blockSeriesSignals(false);
381 }
380 }
382
381
383 void QPieModelMapperPrivate::insertData(int start, int end)
382 void QPieModelMapperPrivate::insertData(int start, int end)
384 {
383 {
385 if (m_model == 0 || m_series == 0)
384 if (m_model == 0 || m_series == 0)
386 return;
385 return;
387
386
388 if (m_count != -1 && start >= m_first + m_count) {
387 if (m_count != -1 && start >= m_first + m_count) {
389 return;
388 return;
390 } else {
389 } else {
391 int addedCount = end - start + 1;
390 int addedCount = end - start + 1;
392 if (m_count != -1 && addedCount > m_count)
391 if (m_count != -1 && addedCount > m_count)
393 addedCount = m_count;
392 addedCount = m_count;
394 int first = qMax(start, m_first);
393 int first = qMax(start, m_first);
395 int last = qMin(first + addedCount - 1, m_orientation == Qt::Vertical ? m_model->rowCount() - 1 : m_model->columnCount() - 1);
394 int last = qMin(first + addedCount - 1, m_orientation == Qt::Vertical ? m_model->rowCount() - 1 : m_model->columnCount() - 1);
396 for (int i = first; i <= last; i++) {
395 for (int i = first; i <= last; i++) {
397 QPieSlice *slice = new QPieSlice;
396 QPieSlice *slice = new QPieSlice;
398 slice->setValue(m_model->data(valueModelIndex(i - m_first), Qt::DisplayRole).toDouble());
397 slice->setValue(m_model->data(valueModelIndex(i - m_first), Qt::DisplayRole).toDouble());
399 slice->setLabel(m_model->data(labelModelIndex(i - m_first), Qt::DisplayRole).toString());
398 slice->setLabel(m_model->data(labelModelIndex(i - m_first), Qt::DisplayRole).toString());
400 slice->setLabelVisible();
399 slice->setLabelVisible();
401 connect(slice, SIGNAL(labelChanged()), this, SLOT(sliceLabelChanged()));
400 connect(slice, SIGNAL(labelChanged()), this, SLOT(sliceLabelChanged()));
402 connect(slice, SIGNAL(valueChanged()), this, SLOT(sliceValueChanged()));
401 connect(slice, SIGNAL(valueChanged()), this, SLOT(sliceValueChanged()));
403 m_series->insert(i - m_first, slice);
402 m_series->insert(i - m_first, slice);
404 m_slices.insert(i - m_first, slice);
403 m_slices.insert(i - m_first, slice);
405 }
404 }
406
405
407 // remove excess of slices (abouve m_count)
406 // remove excess of slices (abouve m_count)
408 if (m_count != -1 && m_series->slices().size() > m_count)
407 if (m_count != -1 && m_series->slices().size() > m_count)
409 for (int i = m_series->slices().size() - 1; i >= m_count; i--) {
408 for (int i = m_series->slices().size() - 1; i >= m_count; i--) {
410 m_series->remove(m_series->slices().at(i));
409 m_series->remove(m_series->slices().at(i));
411 m_slices.removeAt(i);
410 m_slices.removeAt(i);
412 }
411 }
413 }
412 }
414 }
413 }
415
414
416 void QPieModelMapperPrivate::removeData(int start, int end)
415 void QPieModelMapperPrivate::removeData(int start, int end)
417 {
416 {
418 if (m_model == 0 || m_series == 0)
417 if (m_model == 0 || m_series == 0)
419 return;
418 return;
420
419
421 int removedCount = end - start + 1;
420 int removedCount = end - start + 1;
422 if (m_count != -1 && start >= m_first + m_count) {
421 if (m_count != -1 && start >= m_first + m_count) {
423 return;
422 return;
424 } else {
423 } else {
425 int toRemove = qMin(m_series->slices().size(), removedCount); // first find how many items can actually be removed
424 int toRemove = qMin(m_series->slices().size(), removedCount); // first find how many items can actually be removed
426 int first = qMax(start, m_first); // get the index of the first item that will be removed.
425 int first = qMax(start, m_first); // get the index of the first item that will be removed.
427 int last = qMin(first + toRemove - 1, m_series->slices().size() + m_first - 1); // get the index of the last item that will be removed.
426 int last = qMin(first + toRemove - 1, m_series->slices().size() + m_first - 1); // get the index of the last item that will be removed.
428 for (int i = last; i >= first; i--) {
427 for (int i = last; i >= first; i--) {
429 m_series->remove(m_series->slices().at(i - m_first));
428 m_series->remove(m_series->slices().at(i - m_first));
430 m_slices.removeAt(i - m_first);
429 m_slices.removeAt(i - m_first);
431 }
430 }
432
431
433 if (m_count != -1) {
432 if (m_count != -1) {
434 int itemsAvailable; // check how many are available to be added
433 int itemsAvailable; // check how many are available to be added
435 if (m_orientation == Qt::Vertical)
434 if (m_orientation == Qt::Vertical)
436 itemsAvailable = m_model->rowCount() - m_first - m_series->slices().size();
435 itemsAvailable = m_model->rowCount() - m_first - m_series->slices().size();
437 else
436 else
438 itemsAvailable = m_model->columnCount() - m_first - m_series->slices().size();
437 itemsAvailable = m_model->columnCount() - m_first - m_series->slices().size();
439 int toBeAdded = qMin(itemsAvailable, m_count - m_series->slices().size()); // add not more items than there is space left to be filled.
438 int toBeAdded = qMin(itemsAvailable, m_count - m_series->slices().size()); // add not more items than there is space left to be filled.
440 int currentSize = m_series->slices().size();
439 int currentSize = m_series->slices().size();
441 if (toBeAdded > 0)
440 if (toBeAdded > 0)
442 for (int i = m_series->slices().size(); i < currentSize + toBeAdded; i++) {
441 for (int i = m_series->slices().size(); i < currentSize + toBeAdded; i++) {
443 QPieSlice *slice = new QPieSlice;
442 QPieSlice *slice = new QPieSlice;
444 if (m_orientation == Qt::Vertical) {
443 if (m_orientation == Qt::Vertical) {
445 slice->setValue(m_model->data(m_model->index(i + m_first, m_valuesIndex), Qt::DisplayRole).toDouble());
444 slice->setValue(m_model->data(m_model->index(i + m_first, m_valuesIndex), Qt::DisplayRole).toDouble());
446 slice->setLabel(m_model->data(m_model->index(i + m_first, m_labelsIndex), Qt::DisplayRole).toString());
445 slice->setLabel(m_model->data(m_model->index(i + m_first, m_labelsIndex), Qt::DisplayRole).toString());
447 } else {
446 } else {
448 slice->setValue(m_model->data(m_model->index(m_valuesIndex, i + m_first), Qt::DisplayRole).toDouble());
447 slice->setValue(m_model->data(m_model->index(m_valuesIndex, i + m_first), Qt::DisplayRole).toDouble());
449 slice->setLabel(m_model->data(m_model->index(m_labelsIndex, i + m_first), Qt::DisplayRole).toString());
448 slice->setLabel(m_model->data(m_model->index(m_labelsIndex, i + m_first), Qt::DisplayRole).toString());
450 }
449 }
451 slice->setLabelVisible();
450 slice->setLabelVisible();
452 m_series->insert(i, slice);
451 m_series->insert(i, slice);
453 m_slices.insert(i, slice);
452 m_slices.insert(i, slice);
454 }
453 }
455 }
454 }
456 }
455 }
457 }
456 }
458
457
459 void QPieModelMapperPrivate::initializePieFromModel()
458 void QPieModelMapperPrivate::initializePieFromModel()
460 {
459 {
461 if (m_model == 0 || m_series == 0)
460 if (m_model == 0 || m_series == 0)
462 return;
461 return;
463
462
464 blockSeriesSignals();
463 blockSeriesSignals();
465 // clear current content
464 // clear current content
466 m_series->clear();
465 m_series->clear();
467 m_slices.clear();
466 m_slices.clear();
468
467
469 // create the initial slices set
468 // create the initial slices set
470 int slicePos = 0;
469 int slicePos = 0;
471 QModelIndex valueIndex = valueModelIndex(slicePos);
470 QModelIndex valueIndex = valueModelIndex(slicePos);
472 QModelIndex labelIndex = labelModelIndex(slicePos);
471 QModelIndex labelIndex = labelModelIndex(slicePos);
473 while (valueIndex.isValid() && labelIndex.isValid()) {
472 while (valueIndex.isValid() && labelIndex.isValid()) {
474 QPieSlice *slice = new QPieSlice;
473 QPieSlice *slice = new QPieSlice;
475 slice->setLabel(m_model->data(labelIndex, Qt::DisplayRole).toString());
474 slice->setLabel(m_model->data(labelIndex, Qt::DisplayRole).toString());
476 slice->setValue(m_model->data(valueIndex, Qt::DisplayRole).toDouble());
475 slice->setValue(m_model->data(valueIndex, Qt::DisplayRole).toDouble());
477 connect(slice, SIGNAL(labelChanged()), this, SLOT(sliceLabelChanged()));
476 connect(slice, SIGNAL(labelChanged()), this, SLOT(sliceLabelChanged()));
478 connect(slice, SIGNAL(valueChanged()), this, SLOT(sliceValueChanged()));
477 connect(slice, SIGNAL(valueChanged()), this, SLOT(sliceValueChanged()));
479 m_series->append(slice);
478 m_series->append(slice);
480 m_slices.append(slice);
479 m_slices.append(slice);
481 // m_series->append(m_model->data(labelIndex, Qt::DisplayRole).toString(), m_model->data(valueIndex, Qt::DisplayRole).toDouble());
480 // m_series->append(m_model->data(labelIndex, Qt::DisplayRole).toString(), m_model->data(valueIndex, Qt::DisplayRole).toDouble());
482 slicePos++;
481 slicePos++;
483 valueIndex = valueModelIndex(slicePos);
482 valueIndex = valueModelIndex(slicePos);
484 labelIndex = labelModelIndex(slicePos);
483 labelIndex = labelModelIndex(slicePos);
485 }
484 }
486 m_series->setLabelsVisible(true);
485 m_series->setLabelsVisible(true);
487 blockSeriesSignals(false);
486 blockSeriesSignals(false);
488 }
487 }
489
488
490 #include "moc_qpiemodelmapper_p.cpp"
489 #include "moc_qpiemodelmapper_p.cpp"
491 #include "moc_qpiemodelmapper.cpp"
490 #include "moc_qpiemodelmapper.cpp"
492
491
493 QTCOMMERCIALCHART_END_NAMESPACE
492 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,81 +1,81
1 #include "qxymodelmapper.h"
1 #include "qxymodelmapper.h"
2
2
3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4
4
5 QXYModelMapper::QXYModelMapper(QObject *parent):
5 QXYModelMapper::QXYModelMapper(QObject *parent):
6 QObject(parent),
6 QObject(parent),
7 m_first(0),
7 m_first(0),
8 m_count(-1),
8 m_count(-1),
9 m_orientation(Qt::Vertical),
9 m_orientation(Qt::Vertical),
10 m_mapX(-1),
10 m_xSection(-1),
11 m_mapY(-1)
11 m_ySection(-1)
12 {
12 {
13 }
13 }
14
14
15 int QXYModelMapper::first() const
15 int QXYModelMapper::first() const
16 {
16 {
17 return m_first;
17 return m_first;
18 }
18 }
19
19
20 void QXYModelMapper::setFirst(int first)
20 void QXYModelMapper::setFirst(int first)
21 {
21 {
22 m_first = qMax(first, 0);
22 m_first = qMax(first, 0);
23 emit updated();
23 // emit updated();
24 }
24 }
25
25
26 int QXYModelMapper::count() const
26 int QXYModelMapper::count() const
27 {
27 {
28 return m_count;
28 return m_count;
29 }
29 }
30
30
31 void QXYModelMapper::setCount(int count)
31 void QXYModelMapper::setCount(int count)
32 {
32 {
33 m_count = qMax(count, -1);
33 m_count = qMax(count, -1);
34 emit updated();
34 // emit updated();
35 }
35 }
36
36
37 Qt::Orientation QXYModelMapper::orientation() const
37 Qt::Orientation QXYModelMapper::orientation() const
38 {
38 {
39 return m_orientation;
39 return m_orientation;
40 }
40 }
41
41
42 void QXYModelMapper::setOrientation(Qt::Orientation orientation)
42 void QXYModelMapper::setOrientation(Qt::Orientation orientation)
43 {
43 {
44 m_orientation = orientation;
44 m_orientation = orientation;
45 emit updated();
45 // emit updated();
46 }
46 }
47
47
48 int QXYModelMapper::mapX() const
48 int QXYModelMapper::xSection() const
49 {
49 {
50 return m_mapX;
50 return m_xSection;
51 }
51 }
52
52
53 void QXYModelMapper::setMapX(int mapX)
53 void QXYModelMapper::setXSection(int xSection)
54 {
54 {
55 m_mapX = mapX;
55 m_xSection = xSection;
56 emit updated();
56 // emit updated();
57 }
57 }
58
58
59 int QXYModelMapper::mapY() const
59 int QXYModelMapper::ySection() const
60 {
60 {
61 return m_mapY;
61 return m_ySection;
62 }
62 }
63
63
64 void QXYModelMapper::setMapY(int mapY)
64 void QXYModelMapper::setYSection(int ySection)
65 {
65 {
66 m_mapY = mapY;
66 m_ySection = ySection;
67 emit updated();
67 // emit updated();
68 }
68 }
69
69
70 void QXYModelMapper::reset()
70 void QXYModelMapper::reset()
71 {
71 {
72 m_first = 0;
72 m_first = 0;
73 m_count = -1;
73 m_count = -1;
74 m_orientation = Qt::Vertical;
74 m_orientation = Qt::Vertical;
75 m_mapX = -1;
75 m_xSection = -1;
76 m_mapY = -1;
76 m_ySection = -1;
77 }
77 }
78
78
79 #include "moc_qxymodelmapper.cpp"
79 #include "moc_qxymodelmapper.cpp"
80
80
81 QTCOMMERCIALCHART_END_NAMESPACE
81 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,52 +1,53
1 #ifndef QXYMODELMAPPER_H
1 #ifndef QXYMODELMAPPER_H
2 #define QXYMODELMAPPER_H
2 #define QXYMODELMAPPER_H
3
3
4 #include "qchartglobal.h"
4 #include "qchartglobal.h"
5 #include <QObject>
5 #include <QObject>
6
6
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8
8
9 class QTCOMMERCIALCHART_EXPORT QXYModelMapper : public QObject
9 class QTCOMMERCIALCHART_EXPORT QXYModelMapper : public QObject
10 {
10 {
11 Q_OBJECT
11 Q_OBJECT
12 Q_PROPERTY(int mapX READ mapX WRITE setMapX)
12 Q_PROPERTY(int xSection READ xSection WRITE setXSection)
13 Q_PROPERTY(int mapY READ mapY WRITE setMapY)
13 Q_PROPERTY(int ySection READ ySection WRITE setYSection)
14 Q_PROPERTY(int first READ first WRITE setFirst)
14 Q_PROPERTY(int first READ first WRITE setFirst)
15 Q_PROPERTY(int count READ count WRITE setCount)
15 Q_PROPERTY(int count READ count WRITE setCount)
16 Q_PROPERTY(Qt::Orientation orientation READ orientation WRITE setOrientation)
16 Q_PROPERTY(Qt::Orientation orientation READ orientation WRITE setOrientation)
17 Q_ENUMS(Qt::Orientation)
17 Q_ENUMS(Qt::Orientation)
18
18
19 public:
19 public:
20 explicit QXYModelMapper(QObject *parent = 0);
21
22 int first() const;
20 int first() const;
23 void setFirst(int first);
21 void setFirst(int first);
24
22
25 int count() const;
23 int count() const;
26 void setCount(int count);
24 void setCount(int count);
27
25
26 protected:
27 explicit QXYModelMapper(QObject *parent = 0);
28
28 Qt::Orientation orientation() const;
29 Qt::Orientation orientation() const;
29 void setOrientation(Qt::Orientation orientation);
30 void setOrientation(Qt::Orientation orientation);
30
31
31 int mapX() const;
32 int xSection() const;
32 void setMapX(int mapX);
33 void setXSection(int xSection);
33
34
34 int mapY() const;
35 int ySection() const;
35 void setMapY(int mapY);
36 void setYSection(int ySection);
36
37
37 void reset();
38 void reset();
38
39
39 Q_SIGNALS:
40 Q_SIGNALS:
40 void updated();
41 void updated();
41
42
42 private:
43 private:
43 int m_first;
44 int m_first;
44 int m_count;
45 int m_count;
45 Qt::Orientation m_orientation;
46 Qt::Orientation m_orientation;
46 int m_mapX;
47 int m_xSection;
47 int m_mapY;
48 int m_ySection;
48 };
49 };
49
50
50 QTCOMMERCIALCHART_END_NAMESPACE
51 QTCOMMERCIALCHART_END_NAMESPACE
51
52
52 #endif // QXYMODELMAPPER_H
53 #endif // QXYMODELMAPPER_H
@@ -1,16 +1,21
1 INCLUDEPATH += $$PWD
1 INCLUDEPATH += $$PWD
2 DEPENDPATH += $$PWD
2 DEPENDPATH += $$PWD
3
3
4 SOURCES += \
4 SOURCES += \
5 $$PWD/xychart.cpp \
5 $$PWD/xychart.cpp \
6 $$PWD/qxyseries.cpp \
6 $$PWD/qxyseries.cpp \
7 $$PWD/qxymodelmapper.cpp
7 $$PWD/qxymodelmapper.cpp \
8 $$PWD/qvxymodelmapper.cpp \
9 $$PWD/qhxymodelmapper.cpp
8
10
9 PRIVATE_HEADERS += \
11 PRIVATE_HEADERS += \
10 $$PWD/xychart_p.h \
12 $$PWD/xychart_p.h \
11 $$PWD/qxyseries_p.h
13 $$PWD/qxyseries_p.h
12
14
13
15
14 PUBLIC_HEADERS += \
16 PUBLIC_HEADERS += \
15 $$PWD/qxyseries.h \
17 $$PWD/qxyseries.h \
16 $$PWD/qxymodelmapper.h
18 $$PWD/qxymodelmapper.h \
19 $$PWD/qvxymodelmapper.h \
20 $$PWD/qhxymodelmapper.h
21
@@ -1,270 +1,273
1 #include <QtCore/QString>
1 #include <QtCore/QString>
2 #include <QtTest/QtTest>
2 #include <QtTest/QtTest>
3
3
4 #include <qchart.h>
4 #include <qchart.h>
5 #include <qchartview.h>
5 #include <qchartview.h>
6 #include <qpieseries.h>
6 #include <qpieseries.h>
7 #include <qvpiemodelmapper.h>
7 #include <qvpiemodelmapper.h>
8 #include <qhpiemodelmapper.h>
8 #include <qhpiemodelmapper.h>
9 #include <QStandardItemModel>
9 #include <QStandardItemModel>
10
10
11 QTCOMMERCIALCHART_USE_NAMESPACE
11 QTCOMMERCIALCHART_USE_NAMESPACE
12
12
13 class tst_piemodelmapper : public QObject
13 class tst_piemodelmapper : public QObject
14 {
14 {
15 Q_OBJECT
15 Q_OBJECT
16
16
17 public:
17 public:
18 tst_piemodelmapper();
18 tst_piemodelmapper();
19
19
20 private Q_SLOTS:
20 private Q_SLOTS:
21 void initTestCase();
21 void initTestCase();
22 void cleanupTestCase();
22 void cleanupTestCase();
23 void init();
23 void init();
24 void cleanup();
24 void cleanup();
25 void verticalMapper_data();
25 void verticalMapper_data();
26 void verticalMapper();
26 void verticalMapper();
27 void verticalMapperCustomMapping_data();
27 void verticalMapperCustomMapping_data();
28 void verticalMapperCustomMapping();
28 void verticalMapperCustomMapping();
29 void horizontalMapper_data();
29 void horizontalMapper_data();
30 void horizontalMapper();
30 void horizontalMapper();
31 void horizontalMapperCustomMapping_data();
31 void horizontalMapperCustomMapping_data();
32 void horizontalMapperCustomMapping();
32 void horizontalMapperCustomMapping();
33 void seriesUpdated();
33 void seriesUpdated();
34
34
35
35
36 private:
36 private:
37 QStandardItemModel *m_model;
37 QStandardItemModel *m_model;
38 int m_modelRowCount;
38 int m_modelRowCount;
39 int m_modelColumnCount;
39 int m_modelColumnCount;
40
40
41 QPieSeries *m_series;
41 QPieSeries *m_series;
42 QChart *m_chart;
42 QChart *m_chart;
43 };
43 };
44
44
45 tst_piemodelmapper::tst_piemodelmapper():
45 tst_piemodelmapper::tst_piemodelmapper():
46 m_model(0),
46 m_model(0),
47 m_modelRowCount(10),
47 m_modelRowCount(10),
48 m_modelColumnCount(8)
48 m_modelColumnCount(8)
49 {
49 {
50 }
50 }
51
51
52 void tst_piemodelmapper::init()
52 void tst_piemodelmapper::init()
53 {
53 {
54 m_series = new QPieSeries;
54 m_series = new QPieSeries;
55 m_chart->addSeries(m_series);
55 m_chart->addSeries(m_series);
56 }
56 }
57
57
58 void tst_piemodelmapper::cleanup()
58 void tst_piemodelmapper::cleanup()
59 {
59 {
60 m_chart->removeSeries(m_series);
60 m_chart->removeSeries(m_series);
61 delete m_series;
61 delete m_series;
62 m_series = 0;
62 m_series = 0;
63 }
63 }
64
64
65 void tst_piemodelmapper::initTestCase()
65 void tst_piemodelmapper::initTestCase()
66 {
66 {
67 m_chart = new QChart;
67 m_chart = new QChart;
68 QChartView *chartView = new QChartView(m_chart);
68 QChartView *chartView = new QChartView(m_chart);
69 chartView->show();
69 chartView->show();
70
70
71 m_model = new QStandardItemModel(this);
71 m_model = new QStandardItemModel(this);
72 for (int row = 0; row < m_modelRowCount; ++row) {
72 for (int row = 0; row < m_modelRowCount; ++row) {
73 for (int column = 0; column < m_modelColumnCount; column++) {
73 for (int column = 0; column < m_modelColumnCount; column++) {
74 QStandardItem *item = new QStandardItem(row * column);
74 QStandardItem *item = new QStandardItem(row * column);
75 m_model->setItem(row, column, item);
75 m_model->setItem(row, column, item);
76 }
76 }
77 }
77 }
78 }
78 }
79
79
80 void tst_piemodelmapper::cleanupTestCase()
80 void tst_piemodelmapper::cleanupTestCase()
81 {
81 {
82 m_model->clear();
82 m_model->clear();
83 }
83 }
84
84
85 void tst_piemodelmapper::verticalMapper_data()
85 void tst_piemodelmapper::verticalMapper_data()
86 {
86 {
87 QTest::addColumn<int>("valuesColumn");
87 QTest::addColumn<int>("valuesColumn");
88 QTest::addColumn<int>("labelsColumn");
88 QTest::addColumn<int>("labelsColumn");
89 QTest::addColumn<int>("expectedCount");
89 QTest::addColumn<int>("expectedCount");
90 QTest::newRow("different values and labels columns") << 0 << 1 << m_modelRowCount;
90 QTest::newRow("different values and labels columns") << 0 << 1 << m_modelRowCount;
91 QTest::newRow("same values and labels columns") << 1 << 1 << m_modelRowCount;
91 QTest::newRow("same values and labels columns") << 1 << 1 << m_modelRowCount;
92 QTest::newRow("invalid values column and correct labels column") << -3 << 1 << 0;
92 QTest::newRow("invalid values column and correct labels column") << -3 << 1 << 0;
93 QTest::newRow("values column beyond the size of model and correct labels column") << m_modelColumnCount << 1 << 0;
93 QTest::newRow("values column beyond the size of model and correct labels column") << m_modelColumnCount << 1 << 0;
94 QTest::newRow("values column beyond the size of model and correct labels column") << m_modelColumnCount << -1 << 0;
94 QTest::newRow("values column beyond the size of model and correct labels column") << m_modelColumnCount << -1 << 0;
95 }
95 }
96
96
97 void tst_piemodelmapper::verticalMapper()
97 void tst_piemodelmapper::verticalMapper()
98 {
98 {
99 QFETCH(int, valuesColumn);
99 QFETCH(int, valuesColumn);
100 QFETCH(int, labelsColumn);
100 QFETCH(int, labelsColumn);
101 QFETCH(int, expectedCount);
101 QFETCH(int, expectedCount);
102
102
103 QVPieModelMapper *mapper = new QVPieModelMapper;
103 QVPieModelMapper *mapper = new QVPieModelMapper;
104 mapper->setValuesColumn(valuesColumn);
104 mapper->setValuesColumn(valuesColumn);
105 mapper->setLabelsColumn(labelsColumn);
105 mapper->setLabelsColumn(labelsColumn);
106 mapper->setModel(m_model);
106 mapper->setModel(m_model);
107 mapper->setSeries(m_series);
107 mapper->setSeries(m_series);
108
108
109 QCOMPARE(m_series->count(), expectedCount);
109 QCOMPARE(m_series->count(), expectedCount);
110 QCOMPARE(mapper->valuesColumn(), qMax(-1, valuesColumn));
110 QCOMPARE(mapper->valuesColumn(), qMax(-1, valuesColumn));
111 QCOMPARE(mapper->labelsColumn(), qMax(-1, labelsColumn));
111 QCOMPARE(mapper->labelsColumn(), qMax(-1, labelsColumn));
112
112
113 delete mapper;
113 delete mapper;
114 mapper = 0;
114 mapper = 0;
115 }
115 }
116
116
117 void tst_piemodelmapper::verticalMapperCustomMapping_data()
117 void tst_piemodelmapper::verticalMapperCustomMapping_data()
118 {
118 {
119 QTest::addColumn<int>("first");
119 QTest::addColumn<int>("first");
120 QTest::addColumn<int>("countLimit");
120 QTest::addColumn<int>("countLimit");
121 QTest::addColumn<int>("expectedCount");
121 QTest::addColumn<int>("expectedCount");
122 QTest::newRow("first: 0, unlimited count") << 0 << -1 << m_modelRowCount;
122 QTest::newRow("first: 0, unlimited count") << 0 << -1 << m_modelRowCount;
123 QTest::newRow("first: 3, unlimited count") << 3 << -1 << m_modelRowCount - 3;
123 QTest::newRow("first: 3, unlimited count") << 3 << -1 << m_modelRowCount - 3;
124 QTest::newRow("first: 0, count: 5") << 0 << 5 << qMin(5, m_modelRowCount);
124 QTest::newRow("first: 0, count: 5") << 0 << 5 << qMin(5, m_modelRowCount);
125 QTest::newRow("first: 3, count: 5") << 3 << 5 << qMin(5, m_modelRowCount - 3);
125 QTest::newRow("first: 3, count: 5") << 3 << 5 << qMin(5, m_modelRowCount - 3);
126 QTest::newRow("first: +1 greater then the number of rows in the model, unlimited count") << m_modelRowCount + 1 << -1 << 0;
126 QTest::newRow("first: +1 greater then the number of rows in the model, unlimited count") << m_modelRowCount + 1 << -1 << 0;
127 QTest::newRow("first: +1 greater then the number of rows in the model, count: 5") << m_modelRowCount + 1 << 5 << 0;
127 QTest::newRow("first: +1 greater then the number of rows in the model, count: 5") << m_modelRowCount + 1 << 5 << 0;
128 QTest::newRow("first: 0, count: +3 greater than the number of rows in the model (should limit to the size of model)") << 0 << m_modelRowCount + 3 << m_modelRowCount;
128 QTest::newRow("first: 0, count: +3 greater than the number of rows in the model (should limit to the size of model)") << 0 << m_modelRowCount + 3 << m_modelRowCount;
129 QTest::newRow("first: -3(invalid - should default to 0), unlimited count") << -3 << -1 << m_modelRowCount;
129 QTest::newRow("first: -3(invalid - should default to 0), unlimited count") << -3 << -1 << m_modelRowCount;
130 QTest::newRow("first: 0, count: -3 (invalid - shlould default to -1)") << 0 << -3 << m_modelRowCount;
130 QTest::newRow("first: 0, count: -3 (invalid - shlould default to -1)") << 0 << -3 << m_modelRowCount;
131 QTest::newRow("first: -3(invalid - should default to 0), count: -3 (invalid - shlould default to -1)") << -3 << -3 << m_modelRowCount;
131 QTest::newRow("first: -3(invalid - should default to 0), count: -3 (invalid - shlould default to -1)") << -3 << -3 << m_modelRowCount;
132
132
133 }
133 }
134
134
135 void tst_piemodelmapper::verticalMapperCustomMapping()
135 void tst_piemodelmapper::verticalMapperCustomMapping()
136 {
136 {
137 QFETCH(int, first);
137 QFETCH(int, first);
138 QFETCH(int, countLimit);
138 QFETCH(int, countLimit);
139 QFETCH(int, expectedCount);
139 QFETCH(int, expectedCount);
140
140
141 QCOMPARE(m_series->count(), 0);
141 QCOMPARE(m_series->count(), 0);
142
142
143 QVPieModelMapper *mapper = new QVPieModelMapper;
143 QVPieModelMapper *mapper = new QVPieModelMapper;
144 mapper->setValuesColumn(0);
144 mapper->setValuesColumn(0);
145 mapper->setLabelsColumn(1);
145 mapper->setLabelsColumn(1);
146 mapper->setModel(m_model);
146 mapper->setModel(m_model);
147 mapper->setSeries(m_series);
147 mapper->setSeries(m_series);
148 mapper->setFirst(first);
148 mapper->setFirst(first);
149 mapper->setCount(countLimit);
149 mapper->setCount(countLimit);
150
150
151 QCOMPARE(m_series->count(), expectedCount);
151 QCOMPARE(m_series->count(), expectedCount);
152
152
153 // change values column mapping to invalid
153 // change values column mapping to invalid
154 mapper->setValuesColumn(-1);
154 mapper->setValuesColumn(-1);
155 mapper->setLabelsColumn(1);
155 mapper->setLabelsColumn(1);
156
156
157 QCOMPARE(m_series->count(), 0);
157 QCOMPARE(m_series->count(), 0);
158
158
159 delete mapper;
159 delete mapper;
160 mapper = 0;
160 mapper = 0;
161 }
161 }
162
162
163 void tst_piemodelmapper::horizontalMapper_data()
163 void tst_piemodelmapper::horizontalMapper_data()
164 {
164 {
165 QTest::addColumn<int>("valuesRow");
165 QTest::addColumn<int>("valuesRow");
166 QTest::addColumn<int>("labelsRow");
166 QTest::addColumn<int>("labelsRow");
167 QTest::addColumn<int>("expectedCount");
167 QTest::addColumn<int>("expectedCount");
168 QTest::newRow("different values and labels rows") << 0 << 1 << m_modelColumnCount;
168 QTest::newRow("different values and labels rows") << 0 << 1 << m_modelColumnCount;
169 QTest::newRow("same values and labels rows") << 1 << 1 << m_modelColumnCount;
169 QTest::newRow("same values and labels rows") << 1 << 1 << m_modelColumnCount;
170 QTest::newRow("invalid values row and correct labels row") << -3 << 1 << 0;
170 QTest::newRow("invalid values row and correct labels row") << -3 << 1 << 0;
171 QTest::newRow("values row beyond the size of model and correct labels row") << m_modelRowCount << 1 << 0;
171 QTest::newRow("values row beyond the size of model and correct labels row") << m_modelRowCount << 1 << 0;
172 QTest::newRow("values row beyond the size of model and invalid labels row") << m_modelRowCount << -1 << 0;
172 QTest::newRow("values row beyond the size of model and invalid labels row") << m_modelRowCount << -1 << 0;
173 }
173 }
174
174
175 void tst_piemodelmapper::horizontalMapper()
175 void tst_piemodelmapper::horizontalMapper()
176 {
176 {
177 QFETCH(int, valuesRow);
177 QFETCH(int, valuesRow);
178 QFETCH(int, labelsRow);
178 QFETCH(int, labelsRow);
179 QFETCH(int, expectedCount);
179 QFETCH(int, expectedCount);
180
180
181 QHPieModelMapper *mapper = new QHPieModelMapper;
181 QHPieModelMapper *mapper = new QHPieModelMapper;
182 mapper->setValuesRow(valuesRow);
182 mapper->setValuesRow(valuesRow);
183 mapper->setLabelsRow(labelsRow);
183 mapper->setLabelsRow(labelsRow);
184 mapper->setModel(m_model);
184 mapper->setModel(m_model);
185 mapper->setSeries(m_series);
185 mapper->setSeries(m_series);
186
186
187 QCOMPARE(m_series->count(), expectedCount);
187 QCOMPARE(m_series->count(), expectedCount);
188 QCOMPARE(mapper->valuesRow(), qMax(-1, valuesRow));
188 QCOMPARE(mapper->valuesRow(), qMax(-1, valuesRow));
189 QCOMPARE(mapper->labelsRow(), qMax(-1, labelsRow));
189 QCOMPARE(mapper->labelsRow(), qMax(-1, labelsRow));
190
190
191 delete mapper;
191 delete mapper;
192 mapper = 0;
192 mapper = 0;
193 }
193 }
194
194
195 void tst_piemodelmapper::horizontalMapperCustomMapping_data()
195 void tst_piemodelmapper::horizontalMapperCustomMapping_data()
196 {
196 {
197 QTest::addColumn<int>("first");
197 QTest::addColumn<int>("first");
198 QTest::addColumn<int>("countLimit");
198 QTest::addColumn<int>("countLimit");
199 QTest::addColumn<int>("expectedCount");
199 QTest::addColumn<int>("expectedCount");
200 QTest::newRow("first: 0, unlimited count") << 0 << -1 << m_modelColumnCount;
200 QTest::newRow("first: 0, unlimited count") << 0 << -1 << m_modelColumnCount;
201 QTest::newRow("first: 3, unlimited count") << 3 << -1 << m_modelColumnCount - 3;
201 QTest::newRow("first: 3, unlimited count") << 3 << -1 << m_modelColumnCount - 3;
202 QTest::newRow("first: 0, count: 5") << 0 << 5 << qMin(5, m_modelColumnCount);
202 QTest::newRow("first: 0, count: 5") << 0 << 5 << qMin(5, m_modelColumnCount);
203 QTest::newRow("first: 3, count: 5") << 3 << 5 << qMin(5, m_modelColumnCount - 3);
203 QTest::newRow("first: 3, count: 5") << 3 << 5 << qMin(5, m_modelColumnCount - 3);
204 QTest::newRow("first: +1 greater then the number of columns in the model, unlimited count") << m_modelColumnCount + 1 << -1 << 0;
204 QTest::newRow("first: +1 greater then the number of columns in the model, unlimited count") << m_modelColumnCount + 1 << -1 << 0;
205 QTest::newRow("first: +1 greater then the number of columns in the model, count: 5") << m_modelColumnCount + 1 << 5 << 0;
205 QTest::newRow("first: +1 greater then the number of columns in the model, count: 5") << m_modelColumnCount + 1 << 5 << 0;
206 QTest::newRow("first: 0, count: +3 greater than the number of columns in the model (should limit to the size of model)") << 0 << m_modelColumnCount + 3 << m_modelColumnCount;
206 QTest::newRow("first: 0, count: +3 greater than the number of columns in the model (should limit to the size of model)") << 0 << m_modelColumnCount + 3 << m_modelColumnCount;
207 QTest::newRow("first: -3(invalid - should default to 0), unlimited count") << -3 << -1 << m_modelColumnCount;
207 QTest::newRow("first: -3(invalid - should default to 0), unlimited count") << -3 << -1 << m_modelColumnCount;
208 QTest::newRow("first: 0, count: -3 (invalid - shlould default to -1)") << 0 << -3 << m_modelColumnCount;
208 QTest::newRow("first: 0, count: -3 (invalid - shlould default to -1)") << 0 << -3 << m_modelColumnCount;
209 QTest::newRow("first: -3(invalid - should default to 0), count: -3 (invalid - shlould default to -1)") << -3 << -3 << m_modelColumnCount;
209 QTest::newRow("first: -3(invalid - should default to 0), count: -3 (invalid - shlould default to -1)") << -3 << -3 << m_modelColumnCount;
210 }
210 }
211
211
212 void tst_piemodelmapper::horizontalMapperCustomMapping()
212 void tst_piemodelmapper::horizontalMapperCustomMapping()
213 {
213 {
214 QFETCH(int, first);
214 QFETCH(int, first);
215 QFETCH(int, countLimit);
215 QFETCH(int, countLimit);
216 QFETCH(int, expectedCount);
216 QFETCH(int, expectedCount);
217
217
218 QCOMPARE(m_series->count(), 0);
218 QCOMPARE(m_series->count(), 0);
219
219
220 QHPieModelMapper *mapper = new QHPieModelMapper;
220 QHPieModelMapper *mapper = new QHPieModelMapper;
221 mapper->setValuesRow(0);
221 mapper->setValuesRow(0);
222 mapper->setLabelsRow(1);
222 mapper->setLabelsRow(1);
223 mapper->setModel(m_model);
223 mapper->setModel(m_model);
224 mapper->setSeries(m_series);
224 mapper->setSeries(m_series);
225 mapper->setFirst(first);
225 mapper->setFirst(first);
226 mapper->setCount(countLimit);
226 mapper->setCount(countLimit);
227
227
228 QCOMPARE(m_series->count(), expectedCount);
228 QCOMPARE(m_series->count(), expectedCount);
229
229
230 // change values row mapping to invalid
230 // change values row mapping to invalid
231 mapper->setValuesRow(-1);
231 mapper->setValuesRow(-1);
232 mapper->setLabelsRow(1);
232 mapper->setLabelsRow(1);
233
233
234 QCOMPARE(m_series->count(), 0);
234 QCOMPARE(m_series->count(), 0);
235
235
236 delete mapper;
236 delete mapper;
237 mapper = 0;
237 mapper = 0;
238 }
238 }
239
239
240 void tst_piemodelmapper::seriesUpdated()
240 void tst_piemodelmapper::seriesUpdated()
241 {
241 {
242 QStandardItemModel *otherModel = new QStandardItemModel;
242 QStandardItemModel *otherModel = new QStandardItemModel;
243 for (int row = 0; row < m_modelRowCount; ++row) {
243 for (int row = 0; row < m_modelRowCount; ++row) {
244 for (int column = 0; column < m_modelColumnCount; column++) {
244 for (int column = 0; column < m_modelColumnCount; column++) {
245 QStandardItem *item = new QStandardItem(row * column);
245 QStandardItem *item = new QStandardItem(row * column);
246 otherModel->setItem(row, column, item);
246 otherModel->setItem(row, column, item);
247 }
247 }
248 }
248 }
249
249
250 QVPieModelMapper *mapper = new QVPieModelMapper;
250 QVPieModelMapper *mapper = new QVPieModelMapper;
251 mapper->setValuesColumn(0);
251 mapper->setValuesColumn(0);
252 mapper->setLabelsColumn(1);
252 mapper->setLabelsColumn(1);
253 mapper->setModel(otherModel);
253 mapper->setModel(otherModel);
254 mapper->setSeries(m_series);
254 mapper->setSeries(m_series);
255 QCOMPARE(m_series->count(), m_modelRowCount);
255 QCOMPARE(m_series->count(), m_modelRowCount);
256 QCOMPARE(mapper->count(), -1);
256
257
257 m_series->append("1000", 1000);
258 m_series->append("1000", 1000);
258 QCOMPARE(m_series->count(), m_modelRowCount + 1);
259 QCOMPARE(m_series->count(), m_modelRowCount + 1);
260 QCOMPARE(mapper->count(), -1); // the value should not change as it indicates 'all' items there are in the model
259
261
260 m_series->remove(m_series->slices().last());
262 m_series->remove(m_series->slices().last());
261 QCOMPARE(m_series->count(), m_modelRowCount);
263 QCOMPARE(m_series->count(), m_modelRowCount);
264 QCOMPARE(mapper->count(), -1); // the value should not change as it indicates 'all' items there are in the model
262
265
263 otherModel->clear();
266 otherModel->clear();
264 delete otherModel;
267 delete otherModel;
265 otherModel = 0;
268 otherModel = 0;
266 }
269 }
267
270
268 QTEST_MAIN(tst_piemodelmapper)
271 QTEST_MAIN(tst_piemodelmapper)
269
272
270 #include "tst_qpiemodelmapper.moc"
273 #include "tst_qpiemodelmapper.moc"
General Comments 0
You need to be logged in to leave comments. Login now