@@ -77,6 +77,11 bool CustomTableModel::setData(const QModelIndex &index, const QVariant &value, | |||
|
77 | 77 | return false; |
|
78 | 78 | } |
|
79 | 79 | |
|
80 | QVariant CustomTableModel::at(int row, int column) | |
|
81 | { | |
|
82 | return data(index(row, column)); | |
|
83 | } | |
|
84 | ||
|
80 | 85 | void CustomTableModel::insertColumn(int column, const QModelIndex &parent) |
|
81 | 86 | { |
|
82 | 87 | beginInsertColumns(parent, column, column); |
@@ -27,7 +27,8 | |||
|
27 | 27 | class CustomTableModel : public QAbstractTableModel |
|
28 | 28 | { |
|
29 | 29 | Q_OBJECT |
|
30 |
Q_PROPERTY(int |
|
|
30 | Q_PROPERTY(int rowCount READ rowCount) | |
|
31 | Q_PROPERTY(int columnCount READ columnCount) | |
|
31 | 32 | |
|
32 | 33 | public: |
|
33 | 34 | explicit CustomTableModel(QObject *parent = 0); |
@@ -42,6 +43,7 public: | |||
|
42 | 43 | void insertRow(int row, const QModelIndex &parent = QModelIndex()); |
|
43 | 44 | Q_INVOKABLE bool removeRows(int row, int count, const QModelIndex & parent = QModelIndex()); |
|
44 | 45 | Q_INVOKABLE bool removeRow (int row, const QModelIndex &parent = QModelIndex()); |
|
46 | Q_INVOKABLE QVariant at(int row, int column); | |
|
45 | 47 | |
|
46 | 48 | private: |
|
47 | 49 | QList<QVector<QVariant> * > m_data; |
@@ -28,41 +28,47 Rectangle { | |||
|
28 | 28 | |
|
29 | 29 | ChartView { |
|
30 | 30 | id: chart |
|
31 | title: "Custom model example" | |
|
31 | title: "Top-5 car brand shares in Finland" | |
|
32 | 32 | anchors.fill: parent |
|
33 | 33 | theme: ChartView.ChartThemeLight |
|
34 | 34 | axisX.max: 10 |
|
35 | 35 | axisX.min: 0 |
|
36 | 36 | axisY.max: 20 |
|
37 | 37 | axisY.min: 0 |
|
38 | animationOptions: ChartView.SeriesAnimations | |
|
39 | axisXLabels: [0, "2007", 1, "2008", 2, "2009", 3, "2010", 4, "2011", 5, "2012"] | |
|
38 | 40 | |
|
39 | 41 | // For dynamic data we use a custom data model derived from QAbstractiItemModel |
|
40 | 42 | CustomModel { |
|
41 | 43 | id: customModel |
|
42 | CustomModelElement { values: [0, "Manufacturer", 1, 2] } | |
|
43 |
CustomModelElement { values: [1, "Volkswagen", 1 |
|
|
44 |
CustomModelElement { values: [2, "Toyota", 1 |
|
|
45 |
CustomModelElement { values: [3, "Ford", |
|
|
46 |
CustomModelElement { values: [4, "Skoda", |
|
|
47 |
CustomModelElement { values: [5, "Volvo", |
|
|
44 | CustomModelElement { values: [0, "Manufacturer", 0, 1, 2, 3, 4] } | |
|
45 | CustomModelElement { values: [1, "Volkswagen", 10.3, 12.0, 12.8, 13.0, 13.8] } | |
|
46 | CustomModelElement { values: [2, "Toyota", 13.8, 13.5, 16.2, 13.7, 10.7] } | |
|
47 | CustomModelElement { values: [3, "Ford", 6.4, 7.1, 8.9, 8.2, 8.6] } | |
|
48 | CustomModelElement { values: [4, "Skoda", 4.7, 5.8, 6.9, 8.3, 8.2] } | |
|
49 | CustomModelElement { values: [5, "Volvo", 7.1, 6.7, 6.5, 6.3, 7.0] } | |
|
50 | CustomModelElement { values: [6, "Others", 57.7, 54.9, 48.7, 50.5, 51.7] } | |
|
48 | 51 | } |
|
49 | 52 | |
|
50 |
|
|
|
51 |
name: " |
|
|
52 |
|
|
|
53 | BarSeries { | |
|
54 | name: "Others" | |
|
55 | barMargin: 0 | |
|
56 | HBarModelMapper { | |
|
53 | 57 | model: customModel |
|
54 |
|
|
|
55 |
|
|
|
58 | firstBarSetRow: 6 | |
|
59 | lastBarSetRow: 6 | |
|
56 | 60 | first: 2 |
|
57 | 61 | } |
|
58 | 62 | } |
|
59 | 63 | |
|
60 | 64 | LineSeries { |
|
61 | name: "Toyota" | |
|
65 | id: lineSeries | |
|
66 | name: "Volkswagen" | |
|
62 | 67 | HXYModelMapper { |
|
68 | id: lineSeriesMapper | |
|
63 | 69 | model: customModel |
|
64 | 70 | xRow: 0 |
|
65 |
yRow: |
|
|
71 | yRow: 1 | |
|
66 | 72 | first: 2 |
|
67 | 73 | } |
|
68 | 74 | } |
@@ -71,64 +77,29 Rectangle { | |||
|
71 | 77 | id: pieSeries |
|
72 | 78 | size: 0.4 |
|
73 | 79 | horizontalPosition: 0.7 |
|
74 |
verticalPosition: 0. |
|
|
75 | } | |
|
80 | verticalPosition: 0.4 | |
|
81 | onClicked: { | |
|
82 | // Show the selection by exploding the slice | |
|
83 | for (var i = 0; i < pieSeries.count; i++) | |
|
84 | pieSeries.at(i).exploded = false; | |
|
85 | slice.exploded = true; | |
|
76 | 86 | |
|
77 | // VPieModelMapper { | |
|
78 | // series: pieSeries | |
|
79 | // model: customModel | |
|
80 | // labelsColumn: 1 | |
|
81 | // valuesColumn: 2 | |
|
82 | // first: 1 | |
|
83 | // } | |
|
84 | HPieModelMapper { | |
|
85 | series: pieSeries | |
|
86 | model: customModel | |
|
87 | labelsRow: 1 | |
|
88 | valuesRow: 2 | |
|
89 | first: 2 | |
|
87 | // Update the line series to show the yearly data for this slice | |
|
88 | lineSeries.name = slice.label; | |
|
89 | for (var j = 0; j < customModel.rowCount; j++) { | |
|
90 | if (customModel.at(j, 1) == slice.label) { | |
|
91 | lineSeriesMapper.yRow = j; | |
|
90 | 92 | } |
|
91 | ||
|
92 | AreaSeries { | |
|
93 | name: "Ford" | |
|
94 | upperSeries: LineSeries { | |
|
95 | HXYModelMapper { | |
|
96 | model: customModel | |
|
97 | xRow: 0 | |
|
98 | yRow: 3 | |
|
99 | first: 2 | |
|
100 | 93 | } |
|
101 | 94 | } |
|
102 | 95 | } |
|
103 | 96 | |
|
104 | GroupedBarSeries { | |
|
105 | name: "Skoda and Volvo" | |
|
106 | HBarModelMapper { | |
|
97 | VPieModelMapper { | |
|
98 | series: pieSeries | |
|
107 | 99 |
|
|
108 | firstBarSetRow: 4 | |
|
109 | lastBarSetRow: 5 | |
|
110 |
|
|
|
111 | } | |
|
100 | labelsColumn: 1 | |
|
101 | valuesColumn: 2 | |
|
102 | first: 1 | |
|
112 | 103 | } |
|
113 | 104 | } |
|
114 | ||
|
115 | ||
|
116 | // TODO: you could also implement appending to your model, for example: | |
|
117 | // pieSeries.model.append(["Others", 52.0]); | |
|
118 | ||
|
119 | // TODO: show how to use data from a list model in a chart view | |
|
120 | // i.e. copy the data into a custom model | |
|
121 | // ListModel { | |
|
122 | // id: listModel | |
|
123 | // ListElement { | |
|
124 | // label: "Volkswagen" | |
|
125 | // value: 13.5 | |
|
126 | // } | |
|
127 | // ListElement { | |
|
128 | // label: "Toyota" | |
|
129 | // value: 10.9 | |
|
130 | // } | |
|
131 | // // and so on... | |
|
132 | // } | |
|
133 | ||
|
134 | 105 | } |
@@ -27,60 +27,6 | |||
|
27 | 27 | |
|
28 | 28 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
29 | 29 | |
|
30 | DeclarativePieSlice::DeclarativePieSlice(QObject *parent) : | |
|
31 | QPieSlice(parent) | |
|
32 | { | |
|
33 | } | |
|
34 | ||
|
35 | QColor DeclarativePieSlice::color() | |
|
36 | { | |
|
37 | return brush().color(); | |
|
38 | } | |
|
39 | ||
|
40 | void DeclarativePieSlice::setColor(QColor color) | |
|
41 | { | |
|
42 | QBrush b = brush(); | |
|
43 | b.setColor(color); | |
|
44 | setBrush(b); | |
|
45 | } | |
|
46 | ||
|
47 | QColor DeclarativePieSlice::borderColor() | |
|
48 | { | |
|
49 | return pen().color(); | |
|
50 | } | |
|
51 | ||
|
52 | void DeclarativePieSlice::setBorderColor(QColor color) | |
|
53 | { | |
|
54 | QPen p = pen(); | |
|
55 | p.setColor(color); | |
|
56 | setPen(p); | |
|
57 | } | |
|
58 | ||
|
59 | int DeclarativePieSlice::borderWidth() | |
|
60 | { | |
|
61 | return pen().width(); | |
|
62 | } | |
|
63 | ||
|
64 | void DeclarativePieSlice::setBorderWidth(int width) | |
|
65 | { | |
|
66 | QPen p = pen(); | |
|
67 | p.setWidth(width); | |
|
68 | setPen(p); | |
|
69 | } | |
|
70 | ||
|
71 | QColor DeclarativePieSlice::labelColor() | |
|
72 | { | |
|
73 | return labelBrush().color(); | |
|
74 | } | |
|
75 | ||
|
76 | void DeclarativePieSlice::setLabelColor(QColor color) | |
|
77 | { | |
|
78 | // TODO: use brush instead for label color | |
|
79 | QBrush b = labelBrush(); | |
|
80 | b.setColor(color); | |
|
81 | setLabelBrush(b); | |
|
82 | } | |
|
83 | ||
|
84 | 30 | DeclarativePieSeries::DeclarativePieSeries(QObject *parent) : |
|
85 | 31 | QPieSeries(parent) |
|
86 | 32 | { |
@@ -93,8 +39,8 void DeclarativePieSeries::classBegin() | |||
|
93 | 39 | void DeclarativePieSeries::componentComplete() |
|
94 | 40 | { |
|
95 | 41 | foreach(QObject *child, children()) { |
|
96 |
if (qobject_cast< |
|
|
97 |
QPieSeries::append(qobject_cast< |
|
|
42 | if (qobject_cast<QPieSlice *>(child)) { | |
|
43 | QPieSeries::append(qobject_cast<QPieSlice *>(child)); | |
|
98 | 44 | } else if(qobject_cast<QVPieModelMapper *>(child)) { |
|
99 | 45 | QVPieModelMapper *mapper = qobject_cast<QVPieModelMapper *>(child); |
|
100 | 46 | mapper->setSeries(this); |
@@ -117,28 +63,28 void DeclarativePieSeries::appendSeriesChildren(QDeclarativeListProperty<QObject | |||
|
117 | 63 | Q_UNUSED(element); |
|
118 | 64 | } |
|
119 | 65 | |
|
120 |
|
|
|
66 | QPieSlice *DeclarativePieSeries::at(int index) | |
|
121 | 67 | { |
|
122 | 68 | QList<QPieSlice*> sliceList = slices(); |
|
123 | 69 | if (index < sliceList.count()) |
|
124 |
return |
|
|
70 | return sliceList[index]; | |
|
125 | 71 | |
|
126 | 72 | return 0; |
|
127 | 73 | } |
|
128 | 74 | |
|
129 |
|
|
|
75 | QPieSlice* DeclarativePieSeries::find(QString label) | |
|
130 | 76 | { |
|
131 | 77 | foreach (QPieSlice *slice, slices()) { |
|
132 | 78 | if (slice->label() == label) |
|
133 | return qobject_cast<DeclarativePieSlice *>(slice); | |
|
79 | return slice; | |
|
134 | 80 | } |
|
135 | 81 | return 0; |
|
136 | 82 | } |
|
137 | 83 | |
|
138 |
|
|
|
84 | QPieSlice* DeclarativePieSeries::append(QString label, qreal value) | |
|
139 | 85 | { |
|
140 | 86 | // TODO: parameter order is wrong, switch it: |
|
141 |
|
|
|
87 | QPieSlice *slice = new QPieSlice(this); | |
|
142 | 88 | slice->setLabel(label); |
|
143 | 89 | slice->setValue(value); |
|
144 | 90 | QPieSeries::append(slice); |
@@ -32,26 +32,6 QTCOMMERCIALCHART_BEGIN_NAMESPACE | |||
|
32 | 32 | |
|
33 | 33 | class QChart; |
|
34 | 34 | |
|
35 | class DeclarativePieSlice: public QPieSlice | |
|
36 | { | |
|
37 | Q_OBJECT | |
|
38 | Q_PROPERTY(QColor color READ color WRITE setColor) | |
|
39 | Q_PROPERTY(QColor borderColor READ borderColor WRITE setBorderColor) | |
|
40 | Q_PROPERTY(int borderWidth READ borderWidth WRITE setBorderWidth) | |
|
41 | Q_PROPERTY(QColor labelColor READ labelColor WRITE setLabelColor) | |
|
42 | ||
|
43 | public: | |
|
44 | explicit DeclarativePieSlice(QObject *parent = 0); | |
|
45 | QColor color(); | |
|
46 | void setColor(QColor color); | |
|
47 | QColor borderColor(); | |
|
48 | void setBorderColor(QColor color); | |
|
49 | int borderWidth(); | |
|
50 | void setBorderWidth(int width); | |
|
51 | QColor labelColor(); | |
|
52 | void setLabelColor(QColor color); | |
|
53 | }; | |
|
54 | ||
|
55 | 35 | class DeclarativePieSeries : public QPieSeries, public QDeclarativeParserStatus |
|
56 | 36 | { |
|
57 | 37 | Q_OBJECT |
@@ -62,9 +42,9 class DeclarativePieSeries : public QPieSeries, public QDeclarativeParserStatus | |||
|
62 | 42 | public: |
|
63 | 43 | explicit DeclarativePieSeries(QObject *parent = 0); |
|
64 | 44 | QDeclarativeListProperty<QObject> seriesChildren(); |
|
65 |
Q_INVOKABLE |
|
|
66 |
Q_INVOKABLE |
|
|
67 |
Q_INVOKABLE |
|
|
45 | Q_INVOKABLE QPieSlice *at(int index); | |
|
46 | Q_INVOKABLE QPieSlice *find(QString label); | |
|
47 | Q_INVOKABLE QPieSlice *append(QString label, qreal value); | |
|
68 | 48 | |
|
69 | 49 | public: |
|
70 | 50 | void classBegin(); |
@@ -58,7 +58,7 public: | |||
|
58 | 58 | qmlRegisterType<DeclarativeStackedBarSeries>(uri, 1, 0, "StackedBarSeries"); |
|
59 | 59 | qmlRegisterType<DeclarativePercentBarSeries>(uri, 1, 0, "PercentBarSeries"); |
|
60 | 60 | qmlRegisterType<DeclarativePieSeries>(uri, 1, 0, "PieSeries"); |
|
61 |
qmlRegisterType< |
|
|
61 | qmlRegisterType<QPieSlice>(uri, 1, 0, "PieSlice"); | |
|
62 | 62 | qmlRegisterType<DeclarativeBarSet>(uri, 1, 0, "BarSet"); |
|
63 | 63 | qmlRegisterType<QHXYModelMapper>(uri, 1, 0, "HXYModelMapper"); |
|
64 | 64 | qmlRegisterType<QVXYModelMapper>(uri, 1, 0, "VXYModelMapper"); |
@@ -71,8 +71,6 public: | |||
|
71 | 71 | QLatin1String("Trying to create uncreatable: QScatterSeries.")); |
|
72 | 72 | qmlRegisterUncreatableType<QPieSeries>(uri, 1, 0, "QPieSeries", |
|
73 | 73 | QLatin1String("Trying to create uncreatable: QPieSeries.")); |
|
74 | qmlRegisterUncreatableType<QPieSlice>(uri, 1, 0, "QPieSlice", | |
|
75 | QLatin1String("Trying to create uncreatable: QPieSlice.")); | |
|
76 | 74 | qmlRegisterUncreatableType<QAbstractItemModel>(uri, 1, 0, "AbstractItemModel", |
|
77 | 75 | QLatin1String("Trying to create uncreatable: AbstractItemModel.")); |
|
78 | 76 | qmlRegisterUncreatableType<QXYModelMapper>(uri, 1, 0, "XYModelMapper", |
@@ -452,6 +452,63 qreal QPieSlice::angleSpan() const | |||
|
452 | 452 | return d_ptr->m_data.m_angleSpan; |
|
453 | 453 | } |
|
454 | 454 | |
|
455 | QColor QPieSlice::color() | |
|
456 | { | |
|
457 | return brush().color(); | |
|
458 | } | |
|
459 | ||
|
460 | void QPieSlice::setColor(QColor color) | |
|
461 | { | |
|
462 | QBrush b = brush(); | |
|
463 | if (color != b.color()) { | |
|
464 | b.setColor(color); | |
|
465 | setBrush(b); | |
|
466 | } | |
|
467 | } | |
|
468 | ||
|
469 | QColor QPieSlice::borderColor() | |
|
470 | { | |
|
471 | return pen().color(); | |
|
472 | } | |
|
473 | ||
|
474 | void QPieSlice::setBorderColor(QColor color) | |
|
475 | { | |
|
476 | QPen p = pen(); | |
|
477 | if (color != p.color()) { | |
|
478 | p.setColor(color); | |
|
479 | setPen(p); | |
|
480 | emit borderColorChanged(); | |
|
481 | } | |
|
482 | } | |
|
483 | ||
|
484 | int QPieSlice::borderWidth() | |
|
485 | { | |
|
486 | return pen().width(); | |
|
487 | } | |
|
488 | ||
|
489 | void QPieSlice::setBorderWidth(int width) | |
|
490 | { | |
|
491 | QPen p = pen(); | |
|
492 | if (width != p.width()) { | |
|
493 | p.setWidth(width); | |
|
494 | setPen(p); | |
|
495 | } | |
|
496 | } | |
|
497 | ||
|
498 | QColor QPieSlice::labelColor() | |
|
499 | { | |
|
500 | return labelBrush().color(); | |
|
501 | } | |
|
502 | ||
|
503 | void QPieSlice::setLabelColor(QColor color) | |
|
504 | { | |
|
505 | QBrush b = labelBrush(); | |
|
506 | if (color != b.color()) { | |
|
507 | b.setColor(color); | |
|
508 | setLabelBrush(b); | |
|
509 | } | |
|
510 | } | |
|
511 | ||
|
455 | 512 | /*! |
|
456 | 513 | Returns the series that this slice belongs to. |
|
457 | 514 | |
@@ -483,6 +540,10 QPieSlicePrivate *QPieSlicePrivate::fromSlice(QPieSlice *slice) | |||
|
483 | 540 | void QPieSlicePrivate::setPen(const QPen &pen, bool themed) |
|
484 | 541 | { |
|
485 | 542 | if (m_data.m_slicePen != pen) { |
|
543 | if (m_data.m_slicePen.color() != pen.color()) | |
|
544 | emit q_ptr->borderColorChanged(); | |
|
545 | if (m_data.m_slicePen.width() != pen.width()) | |
|
546 | emit q_ptr->borderWidthChanged(); | |
|
486 | 547 | m_data.m_slicePen = pen; |
|
487 | 548 | m_data.m_slicePen.setThemed(themed); |
|
488 | 549 | emit q_ptr->penChanged(); |
@@ -492,6 +553,8 void QPieSlicePrivate::setPen(const QPen &pen, bool themed) | |||
|
492 | 553 | void QPieSlicePrivate::setBrush(const QBrush &brush, bool themed) |
|
493 | 554 | { |
|
494 | 555 | if (m_data.m_sliceBrush != brush) { |
|
556 | if (m_data.m_sliceBrush.color() != brush.color()) | |
|
557 | emit q_ptr->colorChanged(); | |
|
495 | 558 | m_data.m_sliceBrush = brush; |
|
496 | 559 | m_data.m_sliceBrush.setThemed(themed); |
|
497 | 560 | emit q_ptr->brushChanged(); |
@@ -501,6 +564,8 void QPieSlicePrivate::setBrush(const QBrush &brush, bool themed) | |||
|
501 | 564 | void QPieSlicePrivate::setLabelBrush(const QBrush &brush, bool themed) |
|
502 | 565 | { |
|
503 | 566 | if (m_data.m_labelBrush != brush) { |
|
567 | if (m_data.m_labelBrush.color() != brush.color()) | |
|
568 | emit q_ptr->labelColorChanged(); | |
|
504 | 569 | m_data.m_labelBrush = brush; |
|
505 | 570 | m_data.m_labelBrush.setThemed(themed); |
|
506 | 571 | emit q_ptr->labelBrushChanged(); |
@@ -47,6 +47,10 class QTCOMMERCIALCHART_EXPORT QPieSlice : public QObject | |||
|
47 | 47 | Q_PROPERTY(qreal percentage READ percentage NOTIFY percentageChanged) |
|
48 | 48 | Q_PROPERTY(qreal startAngle READ startAngle NOTIFY startAngleChanged) |
|
49 | 49 | Q_PROPERTY(qreal angleSpan READ angleSpan NOTIFY angleSpanChanged) |
|
50 | Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged) | |
|
51 | Q_PROPERTY(QColor borderColor READ borderColor WRITE setBorderColor NOTIFY borderColorChanged) | |
|
52 | Q_PROPERTY(int borderWidth READ borderWidth WRITE setBorderWidth NOTIFY borderWidthChanged) | |
|
53 | Q_PROPERTY(QColor labelColor READ labelColor WRITE setLabelColor NOTIFY labelColorChanged) | |
|
50 | 54 | |
|
51 | 55 | public: |
|
52 | 56 | explicit QPieSlice(QObject *parent = 0); |
@@ -87,6 +91,15 public: | |||
|
87 | 91 | qreal startAngle() const; |
|
88 | 92 | qreal angleSpan() const; |
|
89 | 93 | |
|
94 | QColor color(); | |
|
95 | void setColor(QColor color); | |
|
96 | QColor borderColor(); | |
|
97 | void setBorderColor(QColor color); | |
|
98 | int borderWidth(); | |
|
99 | void setBorderWidth(int width); | |
|
100 | QColor labelColor(); | |
|
101 | void setLabelColor(QColor color); | |
|
102 | ||
|
90 | 103 | QPieSeries *series() const; |
|
91 | 104 | |
|
92 | 105 | Q_SIGNALS: |
@@ -103,6 +116,10 Q_SIGNALS: | |||
|
103 | 116 | void percentageChanged(); |
|
104 | 117 | void startAngleChanged(); |
|
105 | 118 | void angleSpanChanged(); |
|
119 | void colorChanged(); | |
|
120 | void borderColorChanged(); | |
|
121 | void borderWidthChanged(); | |
|
122 | void labelColorChanged(); | |
|
106 | 123 | void clicked(); |
|
107 | 124 | void hovered(bool state); |
|
108 | 125 |
General Comments 0
You need to be logged in to leave comments.
Login now