##// END OF EJS Templates
QML custom model demo now implements it's own QAbstractItemModel based model
Tero Ahola -
r1272:5cb84e6f3423
parent child
Show More
@@ -18,33 +18,31
18 18 **
19 19 ****************************************************************************/
20 20
21 #include "charttablemodel.h"
21 #include "customtablemodel.h"
22 22 #include <QVector>
23 23 #include <QRect>
24 24 #include <QColor>
25 25
26 QTCOMMERCIALCHART_USE_NAMESPACE
27
28 ChartTableModel::ChartTableModel(QObject *parent) :
26 CustomTableModel::CustomTableModel(QObject *parent) :
29 27 QAbstractTableModel(parent),
30 28 m_columnCount(0),
31 29 m_rowCount(0)
32 30 {
33 31 }
34 32
35 int ChartTableModel::rowCount(const QModelIndex & parent) const
33 int CustomTableModel::rowCount(const QModelIndex & parent) const
36 34 {
37 35 Q_UNUSED(parent)
38 36 return m_data.count();
39 37 }
40 38
41 int ChartTableModel::columnCount(const QModelIndex & parent) const
39 int CustomTableModel::columnCount(const QModelIndex & parent) const
42 40 {
43 41 Q_UNUSED(parent)
44 42 return m_columnCount;
45 43 }
46 44
47 QVariant ChartTableModel::headerData (int section, Qt::Orientation orientation, int role ) const
45 QVariant CustomTableModel::headerData (int section, Qt::Orientation orientation, int role ) const
48 46 {
49 47 if (role != Qt::DisplayRole)
50 48 return QVariant();
@@ -59,7 +57,7 QVariant ChartTableModel::headerData (int section, Qt::Orientation orientation,
59 57 }
60 58 }
61 59
62 QVariant ChartTableModel::data(const QModelIndex &index, int role) const
60 QVariant CustomTableModel::data(const QModelIndex &index, int role) const
63 61 {
64 62 if (role == Qt::DisplayRole) {
65 63 return m_data[index.row()]->at(index.column());
@@ -69,7 +67,7 QVariant ChartTableModel::data(const QModelIndex &index, int role) const
69 67 return QVariant();
70 68 }
71 69
72 bool ChartTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
70 bool CustomTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
73 71 {
74 72 if (index.isValid() && role == Qt::EditRole) {
75 73 m_data[index.row()]->replace(index.column(), value);
@@ -79,14 +77,14 bool ChartTableModel::setData(const QModelIndex &index, const QVariant &value, i
79 77 return false;
80 78 }
81 79
82 void ChartTableModel::insertColumn(int column, const QModelIndex &parent)
80 void CustomTableModel::insertColumn(int column, const QModelIndex &parent)
83 81 {
84 82 beginInsertColumns(parent, column, column);
85 83 m_columnCount++;
86 84 endInsertColumns();
87 85 }
88 86
89 void ChartTableModel::insertRow(int row, const QModelIndex &parent)
87 void CustomTableModel::insertRow(int row, const QModelIndex &parent)
90 88 {
91 89 beginInsertRows(parent, row, row);
92 90 QVector<QVariant>* dataVec = new QVector<QVariant>(m_columnCount);
@@ -94,23 +92,12 void ChartTableModel::insertRow(int row, const QModelIndex &parent)
94 92 endInsertRows();
95 93 }
96 94
97 //bool ChartTableModel::removeRow(int row, const QModelIndex &parent)
98 //{
99 // Q_UNUSED(parent)
100 // Q_ASSERT(row >= 0 && row < rowCount);
101
102 // beginRemoveRows(parent, row, row);
103 // m_data.removeAt(row);
104 // endRemoveRows();
105 // return true;
106 //}
107
108 bool ChartTableModel::removeRow(int row, const QModelIndex &parent)
95 bool CustomTableModel::removeRow(int row, const QModelIndex &parent)
109 96 {
110 97 return QAbstractTableModel::removeRow(row, parent);
111 98 }
112 99
113 bool ChartTableModel::removeRows(int row, int count, const QModelIndex &parent)
100 bool CustomTableModel::removeRows(int row, int count, const QModelIndex &parent)
114 101 {
115 102 beginRemoveRows(parent, row, row + count - 1);
116 103 bool removed(false);
@@ -122,9 +109,9 bool ChartTableModel::removeRows(int row, int count, const QModelIndex &parent)
122 109 return removed;
123 110 }
124 111
125 Qt::ItemFlags ChartTableModel::flags ( const QModelIndex & index ) const
112 Qt::ItemFlags CustomTableModel::flags ( const QModelIndex & index ) const
126 113 {
127 114 return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
128 115 }
129 116
130 #include "moc_charttablemodel.cpp"
117 #include "moc_customtablemodel.cpp"
@@ -18,23 +18,19
18 18 **
19 19 ****************************************************************************/
20 20
21 #ifndef CHARTTABLEMODEL_H
22 #define CHARTTABLEMODEL_H
21 #ifndef CUSTOMTABLEMODEL_H
22 #define CUSTOMTABLEMODEL_H
23 23
24 #include "qchartglobal.h"
25 24 #include <QAbstractTableModel>
26 25 #include <QHash>
27 #include <QRect>
28 26
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30
31 class QTCOMMERCIALCHART_EXPORT ChartTableModel : public QAbstractTableModel
27 class CustomTableModel : public QAbstractTableModel
32 28 {
33 29 Q_OBJECT
34 30 Q_PROPERTY(int count READ rowCount)
35 31
36 32 public:
37 explicit ChartTableModel(QObject *parent = 0);
33 explicit CustomTableModel(QObject *parent = 0);
38 34
39 35 int rowCount ( const QModelIndex & parent = QModelIndex() ) const;
40 36 int columnCount ( const QModelIndex & parent = QModelIndex() ) const;
@@ -54,6 +50,4 private:
54 50 int m_rowCount;
55 51 };
56 52
57 QTCOMMERCIALCHART_END_NAMESPACE
58
59 #endif // CHARTTABLEMODEL_H
53 #endif // CUSTOMTABLEMODEL_H
@@ -22,10 +22,7
22 22 #include <qdeclarativelist.h>
23 23 #include <QDebug>
24 24
25 QTCOMMERCIALCHART_BEGIN_NAMESPACE
26
27
28 ////////////// Table model row ///////////////////
25 ////////////// Table model element ///////////////////
29 26
30 27 DeclarativeTableModelElement::DeclarativeTableModelElement(QObject *parent)
31 28 : QObject(parent)
@@ -45,7 +42,7 void DeclarativeTableModelElement::setValues(QVariantList values)
45 42 ////////////// Table model ///////////////////
46 43
47 44 DeclarativeTableModel::DeclarativeTableModel(QObject *parent) :
48 ChartTableModel(parent)
45 CustomTableModel(parent)
49 46 {
50 47 }
51 48
@@ -97,18 +94,16 void DeclarativeTableModel::append(QVariantList values)
97 94 dataChanged(beginIndex, endIndex);
98 95 }
99 96
100 void DeclarativeTableModel::appendPoint(QXYModelMapper *mapper, DeclarativeXyPoint *point)
101 {
102 Q_UNUSED(mapper)
103 Q_UNUSED(point)
104 // TODO: XYModelMapper implementation has change, this code has to be updated.
105 // qDebug() << "DeclarativeTableModel::appendPoint:" << point;
106 // QVariantList values;
107 // values.insert(mapper->mapX(), point->x());
108 // values.insert(mapper->mapY(), point->y());
109 // append(values);
110 }
97 //void DeclarativeTableModel::appendPoint(QXYModelMapper *mapper, DeclarativeXyPoint *point)
98 //{
99 // Q_UNUSED(mapper)
100 // Q_UNUSED(point)
101 // // TODO: XYModelMapper implementation has change, this code has to be updated.
102 //// qDebug() << "DeclarativeTableModel::appendPoint:" << point;
103 //// QVariantList values;
104 //// values.insert(mapper->mapX(), point->x());
105 //// values.insert(mapper->mapY(), point->y());
106 //// append(values);
107 //}
111 108
112 109 #include "moc_declarativemodel.cpp"
113
114 QTCOMMERCIALCHART_END_NAMESPACE
@@ -21,21 +21,11
21 21 #ifndef DECLARATIVEMODEL_H
22 22 #define DECLARATIVEMODEL_H
23 23
24 #include "qchartglobal.h"
25 #include "declarativexypoint.h"
26 #include <QPieSlice>
27 #include "../src/charttablemodel.h" // TODO
28 #include <QBarSet>
29 #include <QXYModelMapper>
24 #include "customtablemodel.h"
30 25 #include <QDeclarativeListProperty>
31 26 #include <QVariant>
32 27 #include <QDeclarativeParserStatus>
33 28
34 QTCOMMERCIALCHART_BEGIN_NAMESPACE
35
36 // TODO: move model into demo app,
37 // the ChartModel API will not be implemented by charts declarative plugin
38
39 29 class DeclarativeTableModelElement : public QObject
40 30 {
41 31 Q_OBJECT
@@ -49,7 +39,7 private:
49 39 QVariantList m_values;
50 40 };
51 41
52 class DeclarativeTableModel : public ChartTableModel, public QDeclarativeParserStatus
42 class DeclarativeTableModel : public CustomTableModel, public QDeclarativeParserStatus
53 43 {
54 44 Q_OBJECT
55 45 Q_INTERFACES(QDeclarativeParserStatus)
@@ -59,7 +49,7 class DeclarativeTableModel : public ChartTableModel, public QDeclarativeParserS
59 49 public:
60 50 explicit DeclarativeTableModel(QObject *parent = 0);
61 51 QDeclarativeListProperty<QObject> modelChildren();
62 void appendPoint(QXYModelMapper *mapper, DeclarativeXyPoint *point);
52 // void appendPoint(QXYModelMapper *mapper, DeclarativeXyPoint *point);
63 53
64 54 public: // from QDeclarativeParserStatus
65 55 void classBegin();
@@ -71,6 +61,4 public Q_SLOTS:
71 61 QObject *element);
72 62 };
73 63
74 QTCOMMERCIALCHART_END_NAMESPACE
75
76 64 #endif // DECLARATIVEMODEL_H
@@ -20,13 +20,25
20 20
21 21 #include <QtGui/QApplication>
22 22 #include <QDeclarativeEngine>
23 #include <QtDeclarative>
24 #include <QAbstractItemModel>
25 #include "declarativemodel.h"
26 #include "customtablemodel.h"
23 27 #include "qmlapplicationviewer.h"
24 28
29 const char *uri = "QmlCustomModel";
30
25 31 Q_DECL_EXPORT int main(int argc, char *argv[])
26 32 {
27 33 QScopedPointer<QApplication> app(createApplication(argc, argv));
28 34 QScopedPointer<QmlApplicationViewer> viewer(QmlApplicationViewer::create());
29 35
36 // @uri QmlCustomModel
37 qmlRegisterUncreatableType<QAbstractItemModel>(uri, 1, 0, "AbstractItemModel",
38 QLatin1String("Trying to create uncreatable: AbstractItemModel."));
39 qmlRegisterType<DeclarativeTableModel>(uri, 1, 0, "CustomModel");
40 qmlRegisterType<DeclarativeTableModelElement>(uri, 1, 0, "CustomModelElement");
41
30 42 viewer->setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
31 43 viewer->setSource(QUrl("qrc:/qml/qmlcustommodel/loader.qml"));
32 44 viewer->showExpanded();
@@ -20,6 +20,7
20 20
21 21 import QtQuick 1.0
22 22 import QtCommercial.Chart 1.0
23 import QmlCustomModel 1.0
23 24
24 25 Rectangle {
25 26 width: parent.width
@@ -31,14 +32,14 Rectangle {
31 32 anchors.fill: parent
32 33 theme: ChartView.ChartThemeLight
33 34
34 // For dynamic data you can use the ChartModel API.
35 ChartModel {
36 id: chartModel
37 ChartModelElement { values: ["Volkswagen", 13.5, 4.4] }
38 ChartModelElement { values: ["Toyota", 10.9, 4.2] }
39 ChartModelElement { values: ["Ford", 8.6, 3.0] }
40 ChartModelElement { values: ["Skoda", 8.2, 1.9] }
41 ChartModelElement { values: ["Volvo", 6.8, 1.5] }
35 // For dynamic data we use a custom data model derived from QAbstractiItemModel
36 CustomModel {
37 id: customModel
38 CustomModelElement { values: ["Volkswagen", 13.5, 4.4] }
39 CustomModelElement { values: ["Toyota", 10.9, 4.2] }
40 CustomModelElement { values: ["Ford", 8.6, 3.0] }
41 CustomModelElement { values: ["Skoda", 8.2, 1.9] }
42 CustomModelElement { values: ["Volvo", 6.8, 1.5] }
42 43 }
43 44
44 45 LineSeries {
@@ -46,7 +47,7 Rectangle {
46 47
47 48 // TODO: the new mapper api
48 49 // VXYModelMapper {
49 // model: chartModel
50 // model: customModel
50 51 // xColumn: 0
51 52 // yColumn: 1
52 53 // }
@@ -62,7 +63,7 Rectangle {
62 63
63 64 VPieModelMapper {
64 65 series: pieSeries
65 model: chartModel
66 model: customModel
66 67 labelsColumn: 0
67 68 valuesColumn: 1
68 69 }
@@ -73,9 +74,8 Rectangle {
73 74 // lowerSeries: LineSeries {}
74 75 // }
75 76
76 // TODO: BarSeries with ChartModel base model API
77 77 // BarSeries {
78 // model: chartModel
78 // model: customModel
79 79 // modelMapper.first: 0
80 80 // }
81 81 }
@@ -85,7 +85,7 Rectangle {
85 85 // pieSeries.model.append(["Others", 52.0]);
86 86
87 87 // TODO: show how to use data from a list model in a chart view
88 // i.e. copy the data into a chart model
88 // i.e. copy the data into a custom model
89 89 // ListModel {
90 90 // id: listModel
91 91 // ListElement {
@@ -3,7 +3,11
3 3 }
4 4
5 5 RESOURCES += resources.qrc
6 SOURCES += main.cpp
6 SOURCES += main.cpp\
7 customtablemodel.cpp \
8 declarativemodel.cpp
9 HEADERS += customtablemodel.h \
10 declarativemodel.h
7 11
8 12 include(qmlapplicationviewer/qmlapplicationviewer.pri)
9 13
@@ -20,8 +20,7 SOURCES += \
20 20 declarativeareaseries.cpp \
21 21 declarativescatterseries.cpp \
22 22 declarativepieseries.cpp \
23 declarativebarseries.cpp \
24 declarativemodel.cpp
23 declarativebarseries.cpp
25 24
26 25 HEADERS += \
27 26 declarativechart.h \
@@ -32,8 +31,7 HEADERS += \
32 31 declarativeareaseries.h \
33 32 declarativescatterseries.h \
34 33 declarativepieseries.h \
35 declarativebarseries.h \
36 declarativemodel.h
34 declarativebarseries.h
37 35
38 36 TARGETPATH = QtCommercial/Chart
39 37 target.path = $$[QT_INSTALL_IMPORTS]/$$TARGETPATH
@@ -37,10 +37,6 public:
37 37 explicit DeclarativeAreaSeries(QObject *parent = 0);
38 38
39 39 public:
40 bool setDeclarativeUpperModel(DeclarativeTableModel *model);
41 DeclarativeTableModel *declarativeUpperModel();
42 bool setDeclarativeLowerModel(DeclarativeTableModel *model);
43 DeclarativeTableModel *declarativeLowerModel();
44 40 QXYModelMapper* upperModelMapper() const;
45 41 QXYModelMapper* lowerModelMapper() const;
46 42 public:
@@ -22,10 +22,10
22 22 #define DECLARATIVEBARSERIES_H
23 23
24 24 #include "qchartglobal.h"
25 #include "declarativemodel.h"
26 25 #include <QDeclarativeItem>
27 26 #include <QDeclarativeParserStatus>
28 27 #include <QGroupedBarSeries>
28 #include <QBarSet>
29 29
30 30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31 31
@@ -25,7 +25,7
25 25 #include "qlineseries.h"
26 26 #include "declarativexyseries.h"
27 27 #include <QDeclarativeParserStatus>
28 #include <QDebug>
28 #include <QDeclarativeListProperty>
29 29
30 30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31 31
@@ -27,7 +27,6
27 27 #include <QDeclarativeParserStatus>
28 28 #include <QDeclarativeListProperty>
29 29 #include <QAbstractItemModel>
30 #include "declarativemodel.h"
31 30
32 31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33 32
@@ -24,6 +24,7
24 24 #include "qchartglobal.h"
25 25 #include "qscatterseries.h"
26 26 #include "declarativexyseries.h"
27 #include <QDeclarativeListProperty>
27 28
28 29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 30
@@ -25,6 +25,7
25 25 #include "qsplineseries.h"
26 26 #include "declarativexyseries.h"
27 27 #include <QDeclarativeParserStatus>
28 #include <QDeclarativeListProperty>
28 29
29 30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 31
@@ -23,7 +23,7
23 23
24 24 #include "qchartglobal.h"
25 25 #include "declarativexypoint.h"
26 #include "declarativemodel.h"
26 #include <QColor>
27 27
28 28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 29
@@ -30,7 +30,6
30 30 #include "declarativescatterseries.h"
31 31 #include "declarativebarseries.h"
32 32 #include "declarativepieseries.h"
33 #include "declarativemodel.h"
34 33 #include <QHPieModelMapper>
35 34 #include <QVPieModelMapper>
36 35 #include <QXYModelMapper>
@@ -55,8 +54,6 public:
55 54 qmlRegisterType<DeclarativeGroupedBarSeries>(uri, 1, 0, "GroupedBarSeries");
56 55 qmlRegisterType<DeclarativePieSeries>(uri, 1, 0, "PieSeries");
57 56 qmlRegisterType<DeclarativePieSlice>(uri, 1, 0, "PieSlice");
58 qmlRegisterType<DeclarativeTableModel>(uri, 1, 0, "ChartModel");
59 qmlRegisterType<DeclarativeTableModelElement>(uri, 1, 0, "ChartModelElement");
60 57 qmlRegisterType<DeclarativeBarSet>(uri, 1, 0, "BarSet");
61 58
62 59 qmlRegisterUncreatableType<QPieSeries>(uri, 1, 0, "QPieSeries",
@@ -37,8 +37,7 SOURCES += \
37 37 $$PWD/qabstractseries.cpp \
38 38 $$PWD/chartbackground.cpp \
39 39 $$PWD/chart.cpp \
40 $$PWD/scroller.cpp \
41 $$PWD/charttablemodel.cpp
40 $$PWD/scroller.cpp
42 41 PRIVATE_HEADERS += \
43 42 $$PWD/chartdataset_p.h \
44 43 $$PWD/chartitem_p.h \
@@ -51,8 +50,7 PRIVATE_HEADERS += \
51 50 $$PWD/qchart_p.h \
52 51 $$PWD/qchartview_p.h \
53 52 $$PWD/scroller_p.h \
54 $$PWD/qabstractseries_p.h \
55 $$PWD/charttablemodel.h
53 $$PWD/qabstractseries_p.h
56 54 PUBLIC_HEADERS += \
57 55 $$PWD/qchart.h \
58 56 $$PWD/qchartglobal.h \
General Comments 0
You need to be logged in to leave comments. Login now