##// END OF EJS Templates
Fixed QML pie model mapper API
Tero Ahola -
r1265:eb8a020650cc
parent child
Show More
@@ -1,108 +1,102
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 import QtQuick 1.0
21 import QtQuick 1.0
22 import QtCommercial.Chart 1.0
22 import QtCommercial.Chart 1.0
23
23
24 Rectangle {
24 Rectangle {
25 width: parent.width
25 width: parent.width
26 height: parent.height
26 height: parent.height
27
27
28 ChartView {
28 ChartView {
29 id: chart
29 id: chart
30 title: "Custom model example"
30 title: "Custom model example"
31 anchors.fill: parent
31 anchors.fill: parent
32 theme: ChartView.ChartThemeLight
32 theme: ChartView.ChartThemeLight
33
33
34 // For dynamic data you can use the ChartModel API.
34 // For dynamic data you can use the ChartModel API.
35 ChartModel {
35 ChartModel {
36 id: chartModel
36 id: chartModel
37 ChartModelElement { values: ["Volkswagen", 13.5, 4.4] }
37 ChartModelElement { values: ["Volkswagen", 13.5, 4.4] }
38 ChartModelElement { values: ["Toyota", 10.9, 4.2] }
38 ChartModelElement { values: ["Toyota", 10.9, 4.2] }
39 ChartModelElement { values: ["Ford", 8.6, 3.0] }
39 ChartModelElement { values: ["Ford", 8.6, 3.0] }
40 ChartModelElement { values: ["Skoda", 8.2, 1.9] }
40 ChartModelElement { values: ["Skoda", 8.2, 1.9] }
41 ChartModelElement { values: ["Volvo", 6.8, 1.5] }
41 ChartModelElement { values: ["Volvo", 6.8, 1.5] }
42 }
42 }
43
43
44 LineSeries {
44 LineSeries {
45 name: "line"
45 name: "line"
46
46
47 // TODO: the new mapper api
47 // TODO: the new mapper api
48 // VXYModelMapper {
48 // VXYModelMapper {
49 // model: chartModel
49 // model: chartModel
50 // xColumn: 0
50 // xColumn: 0
51 // yColumn: 1
51 // yColumn: 1
52 // }
52 // }
53 }
53 }
54
54
55
55
56 PieSeries {
56 PieSeries {
57 id: pieSeries
57 id: pieSeries
58 size: 0.4
58 size: 0.4
59 horizontalPosition: 0.2
59 horizontalPosition: 0.2
60 verticalPosition: 0.3
60 verticalPosition: 0.3
61
62 VPieModelMapper {
63 model: chartModel
64 labelsColumn: 0
65 valuesColumn: 1
66 }
67 }
61 }
68
62
69 // VPieModelMapper {
63 VPieModelMapper {
70 // series: pieSeries
64 series: pieSeries
71 // model: chartModel
65 model: chartModel
72 // labelsColumn: 1
66 labelsColumn: 0
73 // valuesColumn: 2
67 valuesColumn: 1
74 // }
68 }
75
69
76 // AreaSeries {
70 // AreaSeries {
77 // name: "area"
71 // name: "area"
78 // upperSeries: LineSeries {}
72 // upperSeries: LineSeries {}
79 // lowerSeries: LineSeries {}
73 // lowerSeries: LineSeries {}
80 // }
74 // }
81
75
82 // TODO: BarSeries with ChartModel base model API
76 // TODO: BarSeries with ChartModel base model API
83 // BarSeries {
77 // BarSeries {
84 // model: chartModel
78 // model: chartModel
85 // modelMapper.first: 0
79 // modelMapper.first: 0
86 // }
80 // }
87 }
81 }
88
82
89
83
90 // TODO: you could also implement appending to your model, for example:
84 // TODO: you could also implement appending to your model, for example:
91 // pieSeries.model.append(["Others", 52.0]);
85 // pieSeries.model.append(["Others", 52.0]);
92
86
93 // TODO: show how to use data from a list model in a chart view
87 // TODO: show how to use data from a list model in a chart view
94 // i.e. copy the data into a chart model
88 // i.e. copy the data into a chart model
95 // ListModel {
89 // ListModel {
96 // id: listModel
90 // id: listModel
97 // ListElement {
91 // ListElement {
98 // label: "Volkswagen"
92 // label: "Volkswagen"
99 // value: 13.5
93 // value: 13.5
100 // }
94 // }
101 // ListElement {
95 // ListElement {
102 // label: "Toyota"
96 // label: "Toyota"
103 // value: 10.9
97 // value: 10.9
104 // }
98 // }
105 // // and so on...
99 // // and so on...
106 // }
100 // }
107
101
108 }
102 }
@@ -1,86 +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 #include <QtDeclarative/qdeclarativeextensionplugin.h>
21 #include <QtDeclarative/qdeclarativeextensionplugin.h>
22 #include <QtDeclarative/qdeclarative.h>
22 #include <QtDeclarative/qdeclarative.h>
23 #include "qchart.h"
23 #include "qchart.h"
24 #include "qaxiscategories.h"
24 #include "qaxiscategories.h"
25 #include "declarativechart.h"
25 #include "declarativechart.h"
26 #include "declarativexypoint.h"
26 #include "declarativexypoint.h"
27 #include "declarativelineseries.h"
27 #include "declarativelineseries.h"
28 #include "declarativesplineseries.h"
28 #include "declarativesplineseries.h"
29 #include "declarativeareaseries.h"
29 #include "declarativeareaseries.h"
30 #include "declarativescatterseries.h"
30 #include "declarativescatterseries.h"
31 #include "declarativebarseries.h"
31 #include "declarativebarseries.h"
32 #include "declarativepieseries.h"
32 #include "declarativepieseries.h"
33 #include "declarativemodel.h"
33 #include "declarativemodel.h"
34 #include <QHPieModelMapper>
34 #include <QHPieModelMapper>
35 #include <QVPieModelMapper>
35 #include <QVPieModelMapper>
36 #include <QXYModelMapper>
36 #include <QXYModelMapper>
37
37
38 QTCOMMERCIALCHART_BEGIN_NAMESPACE
38 QTCOMMERCIALCHART_BEGIN_NAMESPACE
39
39
40 class ChartQmlPlugin : public QDeclarativeExtensionPlugin
40 class ChartQmlPlugin : public QDeclarativeExtensionPlugin
41 {
41 {
42 Q_OBJECT
42 Q_OBJECT
43 public:
43 public:
44 virtual void registerTypes(const char *uri)
44 virtual void registerTypes(const char *uri)
45 {
45 {
46 Q_ASSERT(QLatin1String(uri) == QLatin1String("QtCommercial.Chart"));
46 Q_ASSERT(QLatin1String(uri) == QLatin1String("QtCommercial.Chart"));
47
47
48 qmlRegisterType<DeclarativeChart>(uri, 1, 0, "ChartView");
48 qmlRegisterType<DeclarativeChart>(uri, 1, 0, "ChartView");
49 qmlRegisterType<DeclarativeXyPoint>(uri, 1, 0, "XyPoint");
49 qmlRegisterType<DeclarativeXyPoint>(uri, 1, 0, "XyPoint");
50 qmlRegisterType<DeclarativeScatterSeries>(uri, 1, 0, "ScatterSeries");
50 qmlRegisterType<DeclarativeScatterSeries>(uri, 1, 0, "ScatterSeries");
51 qmlRegisterType<DeclarativeLineSeries>(uri, 1, 0, "LineSeries");
51 qmlRegisterType<DeclarativeLineSeries>(uri, 1, 0, "LineSeries");
52 qmlRegisterType<DeclarativeSplineSeries>(uri, 1, 0, "SplineSeries");
52 qmlRegisterType<DeclarativeSplineSeries>(uri, 1, 0, "SplineSeries");
53 qmlRegisterType<DeclarativeAreaSeries>(uri, 1, 0, "AreaSeries");
53 qmlRegisterType<DeclarativeAreaSeries>(uri, 1, 0, "AreaSeries");
54 qmlRegisterType<DeclarativeBarSeries>(uri, 1, 0, "BarSeries");
54 qmlRegisterType<DeclarativeBarSeries>(uri, 1, 0, "BarSeries");
55 qmlRegisterType<DeclarativeGroupedBarSeries>(uri, 1, 0, "GroupedBarSeries");
55 qmlRegisterType<DeclarativeGroupedBarSeries>(uri, 1, 0, "GroupedBarSeries");
56 qmlRegisterType<DeclarativePieSeries>(uri, 1, 0, "PieSeries");
56 qmlRegisterType<DeclarativePieSeries>(uri, 1, 0, "PieSeries");
57 qmlRegisterType<DeclarativePieSlice>(uri, 1, 0, "PieSlice");
57 qmlRegisterType<DeclarativePieSlice>(uri, 1, 0, "PieSlice");
58 qmlRegisterType<DeclarativeTableModel>(uri, 1, 0, "ChartModel");
58 qmlRegisterType<DeclarativeTableModel>(uri, 1, 0, "ChartModel");
59 qmlRegisterType<DeclarativeTableModelElement>(uri, 1, 0, "ChartModelElement");
59 qmlRegisterType<DeclarativeTableModelElement>(uri, 1, 0, "ChartModelElement");
60 qmlRegisterType<DeclarativeBarSet>(uri, 1, 0, "BarSet");
60 qmlRegisterType<DeclarativeBarSet>(uri, 1, 0, "BarSet");
61
61
62 qmlRegisterUncreatableType<QPieSeries>(uri, 1, 0, "QPieSeries",
63 QLatin1String("Trying to create uncreatable: QPieSeries."));
62 qmlRegisterUncreatableType<QAbstractItemModel>(uri, 1, 0, "AbstractItemModel",
64 qmlRegisterUncreatableType<QAbstractItemModel>(uri, 1, 0, "AbstractItemModel",
63 QLatin1String("Trying to create uncreatable: AbstractItemModel."));
65 QLatin1String("Trying to create uncreatable: AbstractItemModel."));
64 qmlRegisterUncreatableType<QPieModelMapper>(uri, 1, 0, "PieModelMapper",
66 qmlRegisterUncreatableType<QPieModelMapper>(uri, 1, 0, "PieModelMapper",
65 QLatin1String("Trying to create uncreatable: PieModelMapper."));
67 QLatin1String("Trying to create uncreatable: PieModelMapper."));
66 qmlRegisterType<QHPieModelMapper>(uri, 1, 0, "HPieModelMapper");
68 qmlRegisterType<QHPieModelMapper>(uri, 1, 0, "HPieModelMapper");
67 qmlRegisterType<QVPieModelMapper>(uri, 1, 0, "VPieModelMapper");
69 qmlRegisterType<QVPieModelMapper>(uri, 1, 0, "VPieModelMapper");
68
70
69 qmlRegisterUncreatableType<QAbstractSeries>(uri, 1, 0, "AbstractSeries",
71 qmlRegisterUncreatableType<QAbstractSeries>(uri, 1, 0, "AbstractSeries",
70 QLatin1String("Trying to create uncreatable: AbstractSeries."));
72 QLatin1String("Trying to create uncreatable: AbstractSeries."));
71 qmlRegisterUncreatableType<QAxis>(uri, 1, 0, "Axis",
73 qmlRegisterUncreatableType<QAxis>(uri, 1, 0, "Axis",
72 QLatin1String("Trying to create uncreatable: Axis."));
74 QLatin1String("Trying to create uncreatable: Axis."));
73 qmlRegisterUncreatableType<QPieModelMapper>(uri, 1, 0, "PieModelMapper",
75 qmlRegisterUncreatableType<QPieModelMapper>(uri, 1, 0, "PieModelMapper",
74 QLatin1String("Trying to create uncreatable: PieModelMapper."));
76 QLatin1String("Trying to create uncreatable: PieModelMapper."));
75 qmlRegisterUncreatableType<QXYModelMapper>(uri, 1, 0, "XYModelMapper",
77 qmlRegisterUncreatableType<QXYModelMapper>(uri, 1, 0, "XYModelMapper",
76 QLatin1String("Trying to create uncreatable: XYModelMapper."));
78 QLatin1String("Trying to create uncreatable: XYModelMapper."));
77 }
79 }
78 };
80 };
79
81
80 #include "plugin.moc"
82 #include "plugin.moc"
81
83
82 QTCOMMERCIALCHART_END_NAMESPACE
84 QTCOMMERCIALCHART_END_NAMESPACE
83
85
84 QTCOMMERCIALCHART_USE_NAMESPACE
86 QTCOMMERCIALCHART_USE_NAMESPACE
85
87
86 Q_EXPORT_PLUGIN2(qtcommercialchartqml, QT_PREPEND_NAMESPACE(ChartQmlPlugin))
88 Q_EXPORT_PLUGIN2(qtcommercialchartqml, QT_PREPEND_NAMESPACE(ChartQmlPlugin))
@@ -1,145 +1,130
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 "charttablemodel.h"
21 #include "charttablemodel.h"
22 #include <QVector>
22 #include <QVector>
23 #include <QTime>
24 #include <QRect>
23 #include <QRect>
25 #include <QColor>
24 #include <QColor>
26
25
27 QTCOMMERCIALCHART_USE_NAMESPACE
26 QTCOMMERCIALCHART_USE_NAMESPACE
28
27
29 ChartTableModel::ChartTableModel(QObject *parent) :
28 ChartTableModel::ChartTableModel(QObject *parent) :
30 QAbstractTableModel(parent)
29 QAbstractTableModel(parent),
30 m_columnCount(0),
31 m_rowCount(0)
31 {
32 {
32 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
33
34 m_columnCount = 2;
35 m_rowCount = 0;
36
37 // m_data
38 for (int i = 0; i < m_rowCount; i++) {
39 QVector<QVariant>* dataVec = new QVector<QVariant>(m_columnCount);
40 for (int k = 0; k < dataVec->size(); k++) {
41 if (k%2 == 0)
42 dataVec->replace(k, i * 50 + qrand()%20);
43 else
44 dataVec->replace(k, qrand()%100);
45 }
46 m_data.append(dataVec);
47 }
48 }
33 }
49
34
50 int ChartTableModel::rowCount(const QModelIndex & parent) const
35 int ChartTableModel::rowCount(const QModelIndex & parent) const
51 {
36 {
52 Q_UNUSED(parent)
37 Q_UNUSED(parent)
53 return m_data.count();
38 return m_data.count();
54 }
39 }
55
40
56 int ChartTableModel::columnCount(const QModelIndex & parent) const
41 int ChartTableModel::columnCount(const QModelIndex & parent) const
57 {
42 {
58 Q_UNUSED(parent)
43 Q_UNUSED(parent)
59 return m_columnCount;
44 return m_columnCount;
60 }
45 }
61
46
62 QVariant ChartTableModel::headerData (int section, Qt::Orientation orientation, int role ) const
47 QVariant ChartTableModel::headerData (int section, Qt::Orientation orientation, int role ) const
63 {
48 {
64 if (role != Qt::DisplayRole)
49 if (role != Qt::DisplayRole)
65 return QVariant();
50 return QVariant();
66
51
67 if (orientation == Qt::Horizontal) {
52 if (orientation == Qt::Horizontal) {
68 if (section % 2 == 0)
53 if (section % 2 == 0)
69 return "x";
54 return "x";
70 else
55 else
71 return "y";
56 return "y";
72 } else {
57 } else {
73 return QString("%1").arg(section + 1);
58 return QString("%1").arg(section + 1);
74 }
59 }
75 }
60 }
76
61
77 QVariant ChartTableModel::data(const QModelIndex &index, int role) const
62 QVariant ChartTableModel::data(const QModelIndex &index, int role) const
78 {
63 {
79 if (role == Qt::DisplayRole) {
64 if (role == Qt::DisplayRole) {
80 return m_data[index.row()]->at(index.column());
65 return m_data[index.row()]->at(index.column());
81 } else if (role == Qt::EditRole) {
66 } else if (role == Qt::EditRole) {
82 return m_data[index.row()]->at(index.column());
67 return m_data[index.row()]->at(index.column());
83 }
68 }
84 return QVariant();
69 return QVariant();
85 }
70 }
86
71
87 bool ChartTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
72 bool ChartTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
88 {
73 {
89 if (index.isValid() && role == Qt::EditRole) {
74 if (index.isValid() && role == Qt::EditRole) {
90 m_data[index.row()]->replace(index.column(), value);
75 m_data[index.row()]->replace(index.column(), value);
91 emit dataChanged(index, index);
76 emit dataChanged(index, index);
92 return true;
77 return true;
93 }
78 }
94 return false;
79 return false;
95 }
80 }
96
81
97 void ChartTableModel::insertColumn(int column, const QModelIndex &parent)
82 void ChartTableModel::insertColumn(int column, const QModelIndex &parent)
98 {
83 {
99 beginInsertColumns(parent, column, column);
84 beginInsertColumns(parent, column, column);
100 m_columnCount++;
85 m_columnCount++;
101 endInsertColumns();
86 endInsertColumns();
102 }
87 }
103
88
104 void ChartTableModel::insertRow(int row, const QModelIndex &parent)
89 void ChartTableModel::insertRow(int row, const QModelIndex &parent)
105 {
90 {
106 beginInsertRows(parent, row, row);
91 beginInsertRows(parent, row, row);
107 QVector<QVariant>* dataVec = new QVector<QVariant>(m_columnCount);
92 QVector<QVariant>* dataVec = new QVector<QVariant>(m_columnCount);
108 m_data.insert(row, dataVec);
93 m_data.insert(row, dataVec);
109 endInsertRows();
94 endInsertRows();
110 }
95 }
111
96
112 //bool ChartTableModel::removeRow(int row, const QModelIndex &parent)
97 //bool ChartTableModel::removeRow(int row, const QModelIndex &parent)
113 //{
98 //{
114 // Q_UNUSED(parent)
99 // Q_UNUSED(parent)
115 // Q_ASSERT(row >= 0 && row < rowCount);
100 // Q_ASSERT(row >= 0 && row < rowCount);
116
101
117 // beginRemoveRows(parent, row, row);
102 // beginRemoveRows(parent, row, row);
118 // m_data.removeAt(row);
103 // m_data.removeAt(row);
119 // endRemoveRows();
104 // endRemoveRows();
120 // return true;
105 // return true;
121 //}
106 //}
122
107
123 bool ChartTableModel::removeRow(int row, const QModelIndex &parent)
108 bool ChartTableModel::removeRow(int row, const QModelIndex &parent)
124 {
109 {
125 return QAbstractTableModel::removeRow(row, parent);
110 return QAbstractTableModel::removeRow(row, parent);
126 }
111 }
127
112
128 bool ChartTableModel::removeRows(int row, int count, const QModelIndex &parent)
113 bool ChartTableModel::removeRows(int row, int count, const QModelIndex &parent)
129 {
114 {
130 beginRemoveRows(parent, row, row + count - 1);
115 beginRemoveRows(parent, row, row + count - 1);
131 bool removed(false);
116 bool removed(false);
132 for (int i(row); i < (row + count); i++) {
117 for (int i(row); i < (row + count); i++) {
133 m_data.removeAt(i);
118 m_data.removeAt(i);
134 removed = true;
119 removed = true;
135 }
120 }
136 endRemoveRows();
121 endRemoveRows();
137 return removed;
122 return removed;
138 }
123 }
139
124
140 Qt::ItemFlags ChartTableModel::flags ( const QModelIndex & index ) const
125 Qt::ItemFlags ChartTableModel::flags ( const QModelIndex & index ) const
141 {
126 {
142 return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
127 return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
143 }
128 }
144
129
145 #include "moc_charttablemodel.cpp"
130 #include "moc_charttablemodel.cpp"
General Comments 0
You need to be logged in to leave comments. Login now