##// END OF EJS Templates
Header data to QML custom model demo
Tero Ahola -
r1387:122ce0e59427
parent child
Show More
1 NO CONTENT: new file 100644, binary diff hidden
NO CONTENT: new file 100644, binary diff hidden
@@ -1,122 +1,136
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 <QRect>
23 #include <QRect>
24 #include <QColor>
24 #include <QColor>
25 #include <QDebug>
25
26
26 CustomTableModel::CustomTableModel(QObject *parent) :
27 CustomTableModel::CustomTableModel(QObject *parent) :
27 QAbstractTableModel(parent),
28 QAbstractTableModel(parent),
28 m_columnCount(0),
29 m_columnCount(0),
29 m_rowCount(0)
30 m_rowCount(0)
30 {
31 {
31 }
32 }
32
33
33 int CustomTableModel::rowCount(const QModelIndex & parent) const
34 int CustomTableModel::rowCount(const QModelIndex & parent) const
34 {
35 {
35 Q_UNUSED(parent)
36 Q_UNUSED(parent)
36 return m_data.count();
37 return m_data.count();
37 }
38 }
38
39
39 int CustomTableModel::columnCount(const QModelIndex & parent) const
40 int CustomTableModel::columnCount(const QModelIndex & parent) const
40 {
41 {
41 Q_UNUSED(parent)
42 Q_UNUSED(parent)
42 return m_columnCount;
43 return m_columnCount;
43 }
44 }
44
45
45 QVariant CustomTableModel::headerData (int section, Qt::Orientation orientation, int role ) const
46 QVariant CustomTableModel::headerData(int section, Qt::Orientation orientation, int role) const
46 {
47 {
47 if (role != Qt::DisplayRole)
48 if (role != Qt::DisplayRole)
48 return QVariant();
49 return QVariant();
49
50
50 if (orientation == Qt::Horizontal) {
51 if (orientation == Qt::Horizontal) {
51 if (section % 2 == 0)
52 if (m_rowHeaders.count() > section)
52 return "x";
53 return m_rowHeaders[section];
53 else
54 else
54 return "y";
55 return QAbstractTableModel::headerData(section, orientation, role);
55 } else {
56 } else {
56 return QString("%1").arg(section + 1);
57 return QAbstractTableModel::headerData(section, orientation, role);
57 }
58 }
58 }
59 }
59
60
61 bool CustomTableModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role)
62 {
63 if (orientation == Qt::Horizontal) {
64 while (m_rowHeaders.count() <= section)
65 m_rowHeaders.append(QVariant());
66 m_rowHeaders.replace(section, value);
67 } else {
68 return QAbstractTableModel::setHeaderData(section, orientation, value, role);
69 }
70 emit headerDataChanged(orientation, section, section);
71 return true;
72 }
73
60 QVariant CustomTableModel::data(const QModelIndex &index, int role) const
74 QVariant CustomTableModel::data(const QModelIndex &index, int role) const
61 {
75 {
62 if (role == Qt::DisplayRole) {
76 if (role == Qt::DisplayRole) {
63 return m_data[index.row()]->at(index.column());
77 return m_data[index.row()]->at(index.column());
64 } else if (role == Qt::EditRole) {
78 } else if (role == Qt::EditRole) {
65 return m_data[index.row()]->at(index.column());
79 return m_data[index.row()]->at(index.column());
66 }
80 }
67 return QVariant();
81 return QVariant();
68 }
82 }
69
83
70 bool CustomTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
84 bool CustomTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
71 {
85 {
72 if (index.isValid() && role == Qt::EditRole) {
86 if (index.isValid() && role == Qt::EditRole) {
73 m_data[index.row()]->replace(index.column(), value);
87 m_data[index.row()]->replace(index.column(), value);
74 emit dataChanged(index, index);
88 emit dataChanged(index, index);
75 return true;
89 return true;
76 }
90 }
77 return false;
91 return false;
78 }
92 }
79
93
80 QVariant CustomTableModel::at(int row, int column)
94 QVariant CustomTableModel::at(int row, int column)
81 {
95 {
82 return data(index(row, column));
96 return data(index(row, column));
83 }
97 }
84
98
85 void CustomTableModel::insertColumn(int column, const QModelIndex &parent)
99 void CustomTableModel::insertColumn(int column, const QModelIndex &parent)
86 {
100 {
87 beginInsertColumns(parent, column, column);
101 beginInsertColumns(parent, column, column);
88 m_columnCount++;
102 m_columnCount++;
89 endInsertColumns();
103 endInsertColumns();
90 }
104 }
91
105
92 void CustomTableModel::insertRow(int row, const QModelIndex &parent)
106 void CustomTableModel::insertRow(int row, const QModelIndex &parent)
93 {
107 {
94 beginInsertRows(parent, row, row);
108 beginInsertRows(parent, row, row);
95 QVector<QVariant>* dataVec = new QVector<QVariant>(m_columnCount);
109 QVector<QVariant>* dataVec = new QVector<QVariant>(m_columnCount);
96 m_data.insert(row, dataVec);
110 m_data.insert(row, dataVec);
97 endInsertRows();
111 endInsertRows();
98 }
112 }
99
113
100 bool CustomTableModel::removeRow(int row, const QModelIndex &parent)
114 bool CustomTableModel::removeRow(int row, const QModelIndex &parent)
101 {
115 {
102 return QAbstractTableModel::removeRow(row, parent);
116 return QAbstractTableModel::removeRow(row, parent);
103 }
117 }
104
118
105 bool CustomTableModel::removeRows(int row, int count, const QModelIndex &parent)
119 bool CustomTableModel::removeRows(int row, int count, const QModelIndex &parent)
106 {
120 {
107 beginRemoveRows(parent, row, row + count - 1);
121 beginRemoveRows(parent, row, row + count - 1);
108 bool removed(false);
122 bool removed(false);
109 for (int i(row); i < (row + count); i++) {
123 for (int i(row); i < (row + count); i++) {
110 m_data.removeAt(i);
124 m_data.removeAt(i);
111 removed = true;
125 removed = true;
112 }
126 }
113 endRemoveRows();
127 endRemoveRows();
114 return removed;
128 return removed;
115 }
129 }
116
130
117 Qt::ItemFlags CustomTableModel::flags ( const QModelIndex & index ) const
131 Qt::ItemFlags CustomTableModel::flags ( const QModelIndex & index ) const
118 {
132 {
119 return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
133 return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
120 }
134 }
121
135
122 #include "moc_customtablemodel.cpp"
136 #include "moc_customtablemodel.cpp"
@@ -1,54 +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 CUSTOMTABLEMODEL_H
21 #ifndef CUSTOMTABLEMODEL_H
22 #define CUSTOMTABLEMODEL_H
22 #define CUSTOMTABLEMODEL_H
23
23
24 #include <QAbstractTableModel>
24 #include <QAbstractTableModel>
25 #include <QHash>
25 #include <QHash>
26
26
27 class CustomTableModel : public QAbstractTableModel
27 class CustomTableModel : public QAbstractTableModel
28 {
28 {
29 Q_OBJECT
29 Q_OBJECT
30 Q_PROPERTY(int rowCount READ rowCount)
30 Q_PROPERTY(int rowCount READ rowCount)
31 Q_PROPERTY(int columnCount READ columnCount)
31 Q_PROPERTY(int columnCount READ columnCount)
32
32
33 public:
33 public:
34 explicit CustomTableModel(QObject *parent = 0);
34 explicit CustomTableModel(QObject *parent = 0);
35
35
36 int rowCount ( const QModelIndex & parent = QModelIndex() ) const;
36 int rowCount ( const QModelIndex & parent = QModelIndex() ) const;
37 int columnCount ( const QModelIndex & parent = QModelIndex() ) const;
37 int columnCount ( const QModelIndex & parent = QModelIndex() ) const;
38 QVariant headerData (int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const;
38 QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const;
39 bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role = Qt::EditRole);
39 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
40 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
40 bool setData ( const QModelIndex & index, const QVariant & value, int role = Qt::EditRole );
41 bool setData ( const QModelIndex & index, const QVariant & value, int role = Qt::EditRole );
41 Qt::ItemFlags flags ( const QModelIndex & index ) const;
42 Qt::ItemFlags flags ( const QModelIndex & index ) const;
42 void insertColumn(int column, const QModelIndex &parent = QModelIndex());
43 void insertColumn(int column, const QModelIndex &parent = QModelIndex());
43 void insertRow(int row, const QModelIndex &parent = QModelIndex());
44 void insertRow(int row, const QModelIndex &parent = QModelIndex());
44 Q_INVOKABLE bool removeRows(int row, int count, const QModelIndex & parent = QModelIndex());
45 Q_INVOKABLE bool removeRows(int row, int count, const QModelIndex & parent = QModelIndex());
45 Q_INVOKABLE bool removeRow (int row, const QModelIndex &parent = QModelIndex());
46 Q_INVOKABLE bool removeRow (int row, const QModelIndex &parent = QModelIndex());
46 Q_INVOKABLE QVariant at(int row, int column);
47 Q_INVOKABLE QVariant at(int row, int column);
47
48
48 private:
49 private:
49 QList<QVector<QVariant> * > m_data;
50 QList<QVector<QVariant> * > m_data;
51 QList<QVariant> m_rowHeaders;
50 int m_columnCount;
52 int m_columnCount;
51 int m_rowCount;
53 int m_rowCount;
52 };
54 };
53
55
54 #endif // CUSTOMTABLEMODEL_H
56 #endif // CUSTOMTABLEMODEL_H
@@ -1,97 +1,109
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 ////////////// Table model element ///////////////////
25 ////////////// Table model element ///////////////////
26
26
27 DeclarativeTableModelElement::DeclarativeTableModelElement(QObject *parent)
27 DeclarativeTableModelElement::DeclarativeTableModelElement(QObject *parent)
28 : QObject(parent)
28 : QObject(parent)
29 {
29 {
30 }
30 }
31
31
32 QString DeclarativeTableModelElement::rowHeader()
33 {
34 return m_rowHeader;
35 }
36
37 void DeclarativeTableModelElement::setRowHeader(QString header)
38 {
39 m_rowHeader = header;
40 }
41
32 QVariantList DeclarativeTableModelElement::values()
42 QVariantList DeclarativeTableModelElement::values()
33 {
43 {
34 return m_values;
44 return m_values;
35 }
45 }
36
46
37 void DeclarativeTableModelElement::setValues(QVariantList values)
47 void DeclarativeTableModelElement::setValues(QVariantList values)
38 {
48 {
39 m_values = values;
49 m_values = values;
40 }
50 }
41
51
42 ////////////// Table model ///////////////////
52 ////////////// Table model ///////////////////
43
53
44 DeclarativeTableModel::DeclarativeTableModel(QObject *parent) :
54 DeclarativeTableModel::DeclarativeTableModel(QObject *parent) :
45 CustomTableModel(parent)
55 CustomTableModel(parent)
46 {
56 {
47 }
57 }
48
58
49 void DeclarativeTableModel::classBegin()
59 void DeclarativeTableModel::classBegin()
50 {
60 {
51 }
61 }
52
62
53 void DeclarativeTableModel::componentComplete()
63 void DeclarativeTableModel::componentComplete()
54 {
64 {
55 foreach (QObject *child, children()) {
65 foreach (QObject *child, children()) {
56 if (qobject_cast<DeclarativeTableModelElement *>(child)) {
66 if (qobject_cast<DeclarativeTableModelElement *>(child)) {
57 append(qobject_cast<DeclarativeTableModelElement *>(child)->values());
67 DeclarativeTableModelElement *element = qobject_cast<DeclarativeTableModelElement *>(child);
68 append(element->values());
69 setHeaderData(rowCount() - 1, Qt::Horizontal, element->rowHeader());
58 }
70 }
59 }
71 }
60 }
72 }
61
73
62 QDeclarativeListProperty<QObject> DeclarativeTableModel::modelChildren()
74 QDeclarativeListProperty<QObject> DeclarativeTableModel::modelChildren()
63 {
75 {
64 return QDeclarativeListProperty<QObject>(this, 0, &DeclarativeTableModel::appendModelChild);
76 return QDeclarativeListProperty<QObject>(this, 0, &DeclarativeTableModel::appendModelChild);
65 }
77 }
66
78
67 void DeclarativeTableModel::appendModelChild(QDeclarativeListProperty<QObject> *list,
79 void DeclarativeTableModel::appendModelChild(QDeclarativeListProperty<QObject> *list,
68 QObject *child)
80 QObject *child)
69 {
81 {
70 // childs are added in componentComplete instead
82 // childs are added in componentComplete instead
71 Q_UNUSED(list)
83 Q_UNUSED(list)
72 Q_UNUSED(child)
84 Q_UNUSED(child)
73 }
85 }
74
86
75 void DeclarativeTableModel::append(QVariantList values)
87 void DeclarativeTableModel::append(QVariantList values)
76 {
88 {
77 // qDebug() << "DeclarativeTableModel::append:" << values;
89 // qDebug() << "DeclarativeTableModel::append:" << values;
78
90
79 while (columnCount() < values.count())
91 while (columnCount() < values.count())
80 insertColumn(columnCount());
92 insertColumn(columnCount());
81
93
82 insertRow(rowCount());
94 insertRow(rowCount());
83
95
84 QModelIndex beginIndex = QModelIndex();
96 QModelIndex beginIndex = QModelIndex();
85 QModelIndex endIndex = QModelIndex();
97 QModelIndex endIndex = QModelIndex();
86 for (int i(0); i < values.count(); i++) {
98 for (int i(0); i < values.count(); i++) {
87 QModelIndex modelIndex = createIndex(rowCount() - 1, i);
99 QModelIndex modelIndex = createIndex(rowCount() - 1, i);
88 if (i == 0)
100 if (i == 0)
89 beginIndex = modelIndex;
101 beginIndex = modelIndex;
90 if (i == (values.count() - 1))
102 if (i == (values.count() - 1))
91 endIndex = modelIndex;
103 endIndex = modelIndex;
92 setData(modelIndex, values.at(i));
104 setData(modelIndex, values.at(i));
93 }
105 }
94 dataChanged(beginIndex, endIndex);
106 dataChanged(beginIndex, endIndex);
95 }
107 }
96
108
97 #include "moc_declarativemodel.cpp"
109 #include "moc_declarativemodel.cpp"
@@ -1,63 +1,67
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 DECLARATIVEMODEL_H
21 #ifndef DECLARATIVEMODEL_H
22 #define DECLARATIVEMODEL_H
22 #define DECLARATIVEMODEL_H
23
23
24 #include "customtablemodel.h"
24 #include "customtablemodel.h"
25 #include <QDeclarativeListProperty>
25 #include <QDeclarativeListProperty>
26 #include <QVariant>
26 #include <QVariant>
27 #include <QDeclarativeParserStatus>
27 #include <QDeclarativeParserStatus>
28
28
29 class DeclarativeTableModelElement : public QObject
29 class DeclarativeTableModelElement : public QObject
30 {
30 {
31 Q_OBJECT
31 Q_OBJECT
32 Q_PROPERTY(QString rowHeader READ rowHeader WRITE setRowHeader)
32 Q_PROPERTY(QVariantList values READ values WRITE setValues)
33 Q_PROPERTY(QVariantList values READ values WRITE setValues)
33
34
34 public:
35 public:
35 explicit DeclarativeTableModelElement(QObject *parent = 0);
36 explicit DeclarativeTableModelElement(QObject *parent = 0);
37 QString rowHeader();
38 void setRowHeader(QString header);
36 QVariantList values();
39 QVariantList values();
37 void setValues(QVariantList values);
40 void setValues(QVariantList values);
38 private:
41 private:
42 QString m_rowHeader;
39 QVariantList m_values;
43 QVariantList m_values;
40 };
44 };
41
45
42 class DeclarativeTableModel : public CustomTableModel, public QDeclarativeParserStatus
46 class DeclarativeTableModel : public CustomTableModel, public QDeclarativeParserStatus
43 {
47 {
44 Q_OBJECT
48 Q_OBJECT
45 Q_INTERFACES(QDeclarativeParserStatus)
49 Q_INTERFACES(QDeclarativeParserStatus)
46 Q_PROPERTY(QDeclarativeListProperty<QObject> modelChildren READ modelChildren)
50 Q_PROPERTY(QDeclarativeListProperty<QObject> modelChildren READ modelChildren)
47 Q_CLASSINFO("DefaultProperty", "modelChildren")
51 Q_CLASSINFO("DefaultProperty", "modelChildren")
48
52
49 public:
53 public:
50 explicit DeclarativeTableModel(QObject *parent = 0);
54 explicit DeclarativeTableModel(QObject *parent = 0);
51 QDeclarativeListProperty<QObject> modelChildren();
55 QDeclarativeListProperty<QObject> modelChildren();
52
56
53 public: // from QDeclarativeParserStatus
57 public: // from QDeclarativeParserStatus
54 void classBegin();
58 void classBegin();
55 void componentComplete();
59 void componentComplete();
56
60
57 public Q_SLOTS:
61 public Q_SLOTS:
58 void append(QVariantList slices);
62 void append(QVariantList slices);
59 static void appendModelChild(QDeclarativeListProperty<QObject> *list,
63 static void appendModelChild(QDeclarativeListProperty<QObject> *list,
60 QObject *element);
64 QObject *element);
61 };
65 };
62
66
63 #endif // DECLARATIVEMODEL_H
67 #endif // DECLARATIVEMODEL_H
@@ -1,155 +1,154
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 import QmlCustomModel 1.0
23 import QmlCustomModel 1.0
24
24
25 Rectangle {
25 Rectangle {
26 width: parent.width
26 anchors.fill: parent
27 height: parent.heigh
28
27
29 //![1]
28 //![1]
30 ChartView {
29 ChartView {
31 id: chartView
30 id: chartView
32 title: "Top-5 car brand shares in Finland"
31 title: "Top-5 car brand shares in Finland"
33 anchors.fill: parent
32 anchors.fill: parent
34 axisX.max: 10
33 axisX.max: 10
35 axisX.min: 0
34 axisX.min: 0
36 axisY.max: 20
35 axisY.max: 20
37 axisY.min: 0
36 axisY.min: 0
38 animationOptions: ChartView.SeriesAnimations
37 animationOptions: ChartView.SeriesAnimations
39 axisXLabels: [0, "2007", 1, "2008", 2, "2009", 3, "2010", 4, "2011", 5, "2012"]
38 axisXLabels: [0, "2007", 1, "2008", 2, "2009", 3, "2010", 4, "2011", 5, "2012"]
40 // ...
39 // ...
41 //![1]
40 //![1]
42
41
43 //![2]
42 //![2]
44 CustomModel {
43 CustomModel {
45 id: customModel
44 id: customModel
46 CustomModelElement { values: [0, "Manufacturer", 0, 1, 2, 3, 4] }
45 CustomModelElement { rowHeader: ""; values: [0, "Manufacturer", 0, 1, 2, 3, 4] }
47 CustomModelElement { values: [1, "Volkswagen", 10.3, 12.0, 12.8, 13.0, 13.8] }
46 CustomModelElement { rowHeader: "Volkswagen"; values: [1, "Volkswagen", 10.3, 12.0, 12.8, 13.0, 13.8] }
48 CustomModelElement { values: [2, "Toyota", 13.8, 13.5, 16.2, 13.7, 10.7] }
47 CustomModelElement { rowHeader: "Toyota"; values: [2, "Toyota", 13.8, 13.5, 16.2, 13.7, 10.7] }
49 CustomModelElement { values: [3, "Ford", 6.4, 7.1, 8.9, 8.2, 8.6] }
48 CustomModelElement { rowHeader: "Ford"; values: [3, "Ford", 6.4, 7.1, 8.9, 8.2, 8.6] }
50 CustomModelElement { values: [4, "Skoda", 4.7, 5.8, 6.9, 8.3, 8.2] }
49 CustomModelElement { rowHeader: "Skoda"; values: [4, "Skoda", 4.7, 5.8, 6.9, 8.3, 8.2] }
51 CustomModelElement { values: [5, "Volvo", 7.1, 6.7, 6.5, 6.3, 7.0] }
50 CustomModelElement { rowHeader: "Volvo"; values: [5, "Volvo", 7.1, 6.7, 6.5, 6.3, 7.0] }
52 CustomModelElement { values: [6, "Others", 57.7, 54.9, 48.7, 50.5, 51.7] }
51 CustomModelElement { rowHeader: "Others"; values: [6, "Others", 57.7, 54.9, 48.7, 50.5, 51.7] }
53 }
52 }
54 //![2]
53 //![2]
55
54
56 //![5]
55 //![5]
57 BarSeries {
56 BarSeries {
58 name: "Others"
57 name: "Others"
59 barMargin: 0
58 barMargin: 0
60 visible: false
59 visible: false
61 HBarModelMapper {
60 HBarModelMapper {
62 model: customModel
61 model: customModel
63 firstBarSetRow: 6
62 firstBarSetRow: 6
64 lastBarSetRow: 6
63 lastBarSetRow: 6
65 first: 2
64 first: 2
66 }
65 }
67 }
66 }
68 //![5]
67 //![5]
69
68
70 //![4]
69 //![4]
71 LineSeries {
70 LineSeries {
72 name: "Volkswagen"
71 name: "Volkswagen"
73 visible: false
72 visible: false
74 HXYModelMapper {
73 HXYModelMapper {
75 model: customModel
74 model: customModel
76 xRow: 0
75 xRow: 0
77 yRow: 1
76 yRow: 1
78 first: 2
77 first: 2
79 }
78 }
80 }
79 }
81 //![4]
80 //![4]
82
81
83 LineSeries {
82 LineSeries {
84 name: "Toyota"
83 name: "Toyota"
85 visible: false
84 visible: false
86 HXYModelMapper {
85 HXYModelMapper {
87 model: customModel
86 model: customModel
88 xRow: 0
87 xRow: 0
89 yRow: 2
88 yRow: 2
90 first: 2
89 first: 2
91 }
90 }
92 }
91 }
93
92
94 LineSeries {
93 LineSeries {
95 name: "Ford"
94 name: "Ford"
96 visible: false
95 visible: false
97 HXYModelMapper {
96 HXYModelMapper {
98 model: customModel
97 model: customModel
99 xRow: 0
98 xRow: 0
100 yRow: 2
99 yRow: 3
101 first: 2
100 first: 2
102 }
101 }
103 }
102 }
104
103
105 LineSeries {
104 LineSeries {
106 name: "Skoda"
105 name: "Skoda"
107 visible: false
106 visible: false
108 HXYModelMapper {
107 HXYModelMapper {
109 model: customModel
108 model: customModel
110 xRow: 0
109 xRow: 0
111 yRow: 3
110 yRow: 4
112 first: 2
111 first: 2
113 }
112 }
114 }
113 }
115
114
116 LineSeries {
115 LineSeries {
117 name: "Volvo"
116 name: "Volvo"
118 visible: false
117 visible: false
119 HXYModelMapper {
118 HXYModelMapper {
120 model: customModel
119 model: customModel
121 xRow: 0
120 xRow: 0
122 yRow: 4
121 yRow: 5
123 first: 2
122 first: 2
124 }
123 }
125 }
124 }
126
125
127 //![3]
126 //![3]
128 PieSeries {
127 PieSeries {
129 id: pieSeries
128 id: pieSeries
130 size: 0.4
129 size: 0.4
131 horizontalPosition: 0.7
130 horizontalPosition: 0.7
132 verticalPosition: 0.4
131 verticalPosition: 0.4
133 onClicked: {
132 onClicked: {
134 // Show the selection by exploding the slice
133 // Show the selection by exploding the slice
135 slice.exploded = !slice.exploded;
134 slice.exploded = !slice.exploded;
136
135
137 // Update the line series to show the yearly data for this slice
136 // Update the line series to show the yearly data for this slice
138 for (var i = 0; i < chartView.count; i++) {
137 for (var i = 0; i < chartView.count; i++) {
139 if (chartView.series(i).name == slice.label) {
138 if (chartView.series(i).name == slice.label) {
140 chartView.series(i).visible = slice.exploded;
139 chartView.series(i).visible = slice.exploded;
141 }
140 }
142 }
141 }
143 }
142 }
144 }
143 }
145 //![3]
144 //![3]
146
145
147 VPieModelMapper {
146 VPieModelMapper {
148 series: pieSeries
147 series: pieSeries
149 model: customModel
148 model: customModel
150 labelsColumn: 1
149 labelsColumn: 1
151 valuesColumn: 2
150 valuesColumn: 2
152 first: 1
151 first: 1
153 }
152 }
154 }
153 }
155 }
154 }
General Comments 0
You need to be logged in to leave comments. Login now