@@ -1,54 +1,56 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2012 Digia Plc |
|
4 | 4 | ** All rights reserved. |
|
5 | 5 | ** For any questions to Digia, please use contact form at http://qt.digia.com |
|
6 | 6 | ** |
|
7 | 7 | ** This file is part of the Qt Commercial Charts Add-on. |
|
8 | 8 | ** |
|
9 | 9 | ** $QT_BEGIN_LICENSE$ |
|
10 | 10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
11 | 11 | ** accordance with the Qt Commercial License Agreement provided with the |
|
12 | 12 | ** Software or, alternatively, in accordance with the terms contained in |
|
13 | 13 | ** a written agreement between you and Digia. |
|
14 | 14 | ** |
|
15 | 15 | ** If you have questions regarding the use of this file, please use |
|
16 | 16 | ** contact form at http://qt.digia.com |
|
17 | 17 | ** $QT_END_LICENSE$ |
|
18 | 18 | ** |
|
19 | 19 | ****************************************************************************/ |
|
20 | 20 | |
|
21 | 21 | import QtQuick 1.0 |
|
22 | 22 | import QtCommercial.Chart 1.0 |
|
23 | 23 | |
|
24 | 24 | Rectangle { |
|
25 | 25 | anchors.fill: parent |
|
26 | 26 | |
|
27 | 27 | ChartView { |
|
28 | 28 | title: "Scatters" |
|
29 | 29 | anchors.fill: parent |
|
30 | 30 | theme: ChartView.ChartThemeBlueCerulean |
|
31 | axisX.max: 4 | |
|
32 | axisY.max: 4 | |
|
31 | 33 | |
|
32 | 34 | ScatterSeries { |
|
33 | 35 | id: scatter1 |
|
34 | 36 | name: "Scatter1" |
|
35 | 37 | XyPoint { x: 1.5; y: 1.5 } |
|
36 | 38 | XyPoint { x: 1.5; y: 1.6 } |
|
37 | 39 | XyPoint { x: 1.57; y: 1.55 } |
|
38 | 40 | XyPoint { x: 1.8; y: 1.8 } |
|
39 | 41 | XyPoint { x: 1.9; y: 1.6 } |
|
40 | 42 | XyPoint { x: 2.1; y: 1.3 } |
|
41 | 43 | XyPoint { x: 2.5; y: 2.1 } |
|
42 | 44 | } |
|
43 | 45 | |
|
44 | 46 | ScatterSeries { |
|
45 | 47 | name: "Scatter2" |
|
46 | 48 | XyPoint { x: 2.0; y: 2.0 } |
|
47 | 49 | XyPoint { x: 2.0; y: 2.1 } |
|
48 | 50 | XyPoint { x: 2.07; y: 2.05 } |
|
49 | 51 | XyPoint { x: 2.2; y: 2.9 } |
|
50 | 52 | XyPoint { x: 2.4; y: 2.7 } |
|
51 | 53 | XyPoint { x: 2.67; y: 2.65 } |
|
52 | 54 | } |
|
53 | 55 | } |
|
54 | 56 | } |
@@ -1,102 +1,120 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2012 Digia Plc |
|
4 | 4 | ** All rights reserved. |
|
5 | 5 | ** For any questions to Digia, please use contact form at http://qt.digia.com |
|
6 | 6 | ** |
|
7 | 7 | ** This file is part of the Qt Commercial Charts Add-on. |
|
8 | 8 | ** |
|
9 | 9 | ** $QT_BEGIN_LICENSE$ |
|
10 | 10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
11 | 11 | ** accordance with the Qt Commercial License Agreement provided with the |
|
12 | 12 | ** Software or, alternatively, in accordance with the terms contained in |
|
13 | 13 | ** a written agreement between you and Digia. |
|
14 | 14 | ** |
|
15 | 15 | ** If you have questions regarding the use of this file, please use |
|
16 | 16 | ** contact form at http://qt.digia.com |
|
17 | 17 | ** $QT_END_LICENSE$ |
|
18 | 18 | ** |
|
19 | 19 | ****************************************************************************/ |
|
20 | 20 | |
|
21 | 21 | import QtQuick 1.0 |
|
22 | 22 | import QtCommercial.Chart 1.0 |
|
23 | 23 | import QmlCustomModel 1.0 |
|
24 | 24 | |
|
25 | 25 | Rectangle { |
|
26 | 26 | width: parent.width |
|
27 | 27 | height: parent.height |
|
28 | 28 | |
|
29 | 29 | ChartView { |
|
30 | 30 | id: chart |
|
31 | 31 | title: "Custom model example" |
|
32 | 32 | anchors.fill: parent |
|
33 | 33 | theme: ChartView.ChartThemeLight |
|
34 | axisX.max: 20 | |
|
35 | axisY.max: 20 | |
|
34 | 36 | |
|
35 | 37 | // For dynamic data we use a custom data model derived from QAbstractiItemModel |
|
36 | 38 | CustomModel { |
|
37 | 39 | id: customModel |
|
38 |
CustomModelElement { values: [ |
|
|
39 |
CustomModelElement { values: [ |
|
|
40 |
CustomModelElement { values: [ |
|
|
41 |
CustomModelElement { values: [ |
|
|
42 |
CustomModelElement { values: [ |
|
|
40 | CustomModelElement { values: [index, "Manufacturer", 1, 2] } | |
|
41 | CustomModelElement { values: [1, "Volkswagen", 13.5, 12.5] } | |
|
42 | CustomModelElement { values: [2, "Toyota", 10.9, 9.9] } | |
|
43 | CustomModelElement { values: [3, "Ford", 8.6, 7.6] } | |
|
44 | CustomModelElement { values: [4, "Skoda", 8.2, 7.2] } | |
|
45 | CustomModelElement { values: [5, "Volvo", 6.8, 5.8] } | |
|
43 | 46 | } |
|
44 | 47 | |
|
45 | 48 | LineSeries { |
|
46 |
name: " |
|
|
47 | ||
|
48 | // TODO: the new mapper api | |
|
49 | // VXYModelMapper { | |
|
50 |
|
|
|
51 |
|
|
|
52 | // yColumn: 1 | |
|
53 | // } | |
|
49 | name: "Volkswagen" | |
|
50 | HXYModelMapper { | |
|
51 | model: customModel | |
|
52 | xRow: 0 | |
|
53 | yRow: 1 | |
|
54 | first: 2 | |
|
55 | } | |
|
54 | 56 |
|
|
55 | 57 | |
|
58 | LineSeries { | |
|
59 | name: "Toyota" | |
|
60 | HXYModelMapper { | |
|
61 | model: customModel | |
|
62 | xRow: 0 | |
|
63 | yRow: 2 | |
|
64 | first: 2 | |
|
65 | } | |
|
66 | } | |
|
56 | 67 | |
|
57 | 68 | PieSeries { |
|
58 | 69 | id: pieSeries |
|
59 | 70 | size: 0.4 |
|
60 | 71 | horizontalPosition: 0.2 |
|
61 | 72 | verticalPosition: 0.3 |
|
62 | 73 | } |
|
63 | 74 | |
|
64 | 75 | VPieModelMapper { |
|
65 | 76 | series: pieSeries |
|
66 | 77 | model: customModel |
|
67 |
labelsColumn: |
|
|
68 |
valuesColumn: |
|
|
78 | labelsColumn: 1 | |
|
79 | valuesColumn: 2 | |
|
80 | first: 1 | |
|
69 | 81 | } |
|
70 | 82 | |
|
71 |
|
|
|
72 |
|
|
|
73 |
|
|
|
74 | // lowerSeries: LineSeries {} | |
|
75 | // } | |
|
83 | AreaSeries { | |
|
84 | name: "Ford" | |
|
85 | upperSeries: LineSeries { | |
|
86 | HXYModelMapper { | |
|
87 | model: customModel | |
|
88 | xRow: 0 | |
|
89 | yRow: 3 | |
|
90 | first: 2 | |
|
91 | } | |
|
92 | } | |
|
93 | } | |
|
76 | 94 |
|
|
77 | 95 | // BarSeries { |
|
78 | 96 | // model: customModel |
|
79 | 97 | // modelMapper.first: 0 |
|
80 | 98 | // } |
|
81 | 99 | } |
|
82 | 100 | |
|
83 | 101 | |
|
84 | 102 | // TODO: you could also implement appending to your model, for example: |
|
85 | 103 | // pieSeries.model.append(["Others", 52.0]); |
|
86 | 104 | |
|
87 | 105 | // TODO: show how to use data from a list model in a chart view |
|
88 | 106 | // i.e. copy the data into a custom model |
|
89 | 107 | // ListModel { |
|
90 | 108 | // id: listModel |
|
91 | 109 | // ListElement { |
|
92 | 110 | // label: "Volkswagen" |
|
93 | 111 | // value: 13.5 |
|
94 | 112 | // } |
|
95 | 113 | // ListElement { |
|
96 | 114 | // label: "Toyota" |
|
97 | 115 | // value: 10.9 |
|
98 | 116 | // } |
|
99 | 117 | // // and so on... |
|
100 | 118 | // } |
|
101 | 119 | |
|
102 | 120 | } |
@@ -1,250 +1,250 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2012 Digia Plc |
|
4 | 4 | ** All rights reserved. |
|
5 | 5 | ** For any questions to Digia, please use contact form at http://qt.digia.com |
|
6 | 6 | ** |
|
7 | 7 | ** This file is part of the Qt Commercial Charts Add-on. |
|
8 | 8 | ** |
|
9 | 9 | ** $QT_BEGIN_LICENSE$ |
|
10 | 10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
11 | 11 | ** accordance with the Qt Commercial License Agreement provided with the |
|
12 | 12 | ** Software or, alternatively, in accordance with the terms contained in |
|
13 | 13 | ** a written agreement between you and Digia. |
|
14 | 14 | ** |
|
15 | 15 | ** If you have questions regarding the use of this file, please use |
|
16 | 16 | ** contact form at http://qt.digia.com |
|
17 | 17 | ** $QT_END_LICENSE$ |
|
18 | 18 | ** |
|
19 | 19 | ****************************************************************************/ |
|
20 | 20 | |
|
21 | 21 | #include "declarativechart.h" |
|
22 | 22 | #include <QPainter> |
|
23 | 23 | #include "declarativelineseries.h" |
|
24 | 24 | #include "declarativeareaseries.h" |
|
25 | 25 | #include "declarativebarseries.h" |
|
26 | 26 | #include "declarativepieseries.h" |
|
27 | 27 | #include "declarativesplineseries.h" |
|
28 | 28 | #include "declarativescatterseries.h" |
|
29 | 29 | |
|
30 | 30 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
31 | 31 | |
|
32 | 32 | DeclarativeChart::DeclarativeChart(QDeclarativeItem *parent) |
|
33 | 33 | : QDeclarativeItem(parent), |
|
34 | 34 | m_chart(new QChart(this)), |
|
35 | 35 | m_legend(LegendTop) |
|
36 | 36 | { |
|
37 | 37 | setFlag(QGraphicsItem::ItemHasNoContents, false); |
|
38 | m_chart->axisX()->setNiceNumbersEnabled(false); | |
|
38 | // m_chart->axisX()->setNiceNumbersEnabled(false); | |
|
39 | 39 | } |
|
40 | 40 | |
|
41 | 41 | DeclarativeChart::~DeclarativeChart() |
|
42 | 42 | { |
|
43 | 43 | delete m_chart; |
|
44 | 44 | } |
|
45 | 45 | |
|
46 | 46 | void DeclarativeChart::childEvent(QChildEvent *event) |
|
47 | 47 | { |
|
48 | 48 | if (event->type() == QEvent::ChildAdded) { |
|
49 | 49 | if (qobject_cast<QAbstractSeries *>(event->child())) { |
|
50 | 50 | m_chart->addSeries(qobject_cast<QAbstractSeries *>(event->child())); |
|
51 | 51 | } |
|
52 | 52 | } |
|
53 | 53 | } |
|
54 | 54 | |
|
55 | 55 | void DeclarativeChart::componentComplete() |
|
56 | 56 | { |
|
57 | 57 | // qDebug() << "DeclarativeChart::componentComplete(), maxX: " << axisX()->max(); |
|
58 | 58 | foreach(QObject *child, children()) { |
|
59 | 59 | if (qobject_cast<QAbstractSeries *>(child)) { |
|
60 | 60 | // qDebug() << "DeclarativeChart::componentComplete(), add: " << child; |
|
61 | 61 | m_chart->addSeries(qobject_cast<QAbstractSeries *>(child)); |
|
62 | 62 | } |
|
63 | 63 | } |
|
64 | 64 | // qDebug() << "DeclarativeChart::componentComplete(), maxX: " << axisX()->max(); |
|
65 | 65 | |
|
66 | 66 | QDeclarativeItem::componentComplete(); |
|
67 | 67 | } |
|
68 | 68 | |
|
69 | 69 | void DeclarativeChart::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry) |
|
70 | 70 | { |
|
71 | 71 | // qDebug() << "DeclarativeChart::geometryChanged" << newGeometry.width() << newGeometry.height(); |
|
72 | 72 | Q_UNUSED(oldGeometry) |
|
73 | 73 | |
|
74 | 74 | if (newGeometry.isValid()) { |
|
75 | 75 | if (newGeometry.width() > 0 && newGeometry.height() > 0) { |
|
76 | 76 | m_chart->resize(newGeometry.width(), newGeometry.height()); |
|
77 | 77 | } |
|
78 | 78 | } |
|
79 | 79 | } |
|
80 | 80 | |
|
81 | 81 | void DeclarativeChart::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
82 | 82 | { |
|
83 | 83 | Q_UNUSED(option) |
|
84 | 84 | Q_UNUSED(widget) |
|
85 | 85 | |
|
86 | 86 | // TODO: optimized? |
|
87 | 87 | painter->setRenderHint(QPainter::Antialiasing, true); |
|
88 | 88 | } |
|
89 | 89 | |
|
90 | 90 | void DeclarativeChart::setTheme(DeclarativeChart::Theme theme) |
|
91 | 91 | { |
|
92 | 92 | m_chart->setTheme((QChart::ChartTheme) theme); |
|
93 | 93 | } |
|
94 | 94 | |
|
95 | 95 | DeclarativeChart::Theme DeclarativeChart::theme() |
|
96 | 96 | { |
|
97 | 97 | return (DeclarativeChart::Theme) m_chart->theme(); |
|
98 | 98 | } |
|
99 | 99 | |
|
100 | 100 | void DeclarativeChart::setAnimationOptions(DeclarativeChart::Animation animations) |
|
101 | 101 | { |
|
102 | 102 | m_chart->setAnimationOptions((QChart::AnimationOption) animations); |
|
103 | 103 | } |
|
104 | 104 | |
|
105 | 105 | DeclarativeChart::Animation DeclarativeChart::animationOptions() |
|
106 | 106 | { |
|
107 | 107 | if (m_chart->animationOptions().testFlag(QChart::AllAnimations)) |
|
108 | 108 | return DeclarativeChart::AllAnimations; |
|
109 | 109 | else if (m_chart->animationOptions().testFlag(QChart::GridAxisAnimations)) |
|
110 | 110 | return DeclarativeChart::GridAxisAnimations; |
|
111 | 111 | else if (m_chart->animationOptions().testFlag(QChart::SeriesAnimations)) |
|
112 | 112 | return DeclarativeChart::SeriesAnimations; |
|
113 | 113 | else |
|
114 | 114 | return DeclarativeChart::NoAnimation; |
|
115 | 115 | } |
|
116 | 116 | |
|
117 | 117 | void DeclarativeChart::setLegend(DeclarativeChart::Legend legend) |
|
118 | 118 | { |
|
119 | 119 | if (legend != m_legend) { |
|
120 | 120 | m_legend = legend; |
|
121 | 121 | switch (m_legend) { |
|
122 | 122 | case LegendDisabled: |
|
123 | 123 | m_chart->legend()->setVisible(false); |
|
124 | 124 | break; |
|
125 | 125 | case LegendTop: |
|
126 | 126 | m_chart->legend()->setVisible(true); |
|
127 | 127 | m_chart->legend()->setAlignment(QLegend::AlignmentTop); |
|
128 | 128 | break; |
|
129 | 129 | case LegendBottom: |
|
130 | 130 | m_chart->legend()->setVisible(true); |
|
131 | 131 | m_chart->legend()->setAlignment(QLegend::AlignmentBottom); |
|
132 | 132 | break; |
|
133 | 133 | case LegendLeft: |
|
134 | 134 | m_chart->legend()->setVisible(true); |
|
135 | 135 | m_chart->legend()->setAlignment(QLegend::AlignmentLeft); |
|
136 | 136 | break; |
|
137 | 137 | case LegendRight: |
|
138 | 138 | m_chart->legend()->setVisible(true); |
|
139 | 139 | m_chart->legend()->setAlignment(QLegend::AlignmentRight); |
|
140 | 140 | break; |
|
141 | 141 | default: |
|
142 | 142 | m_chart->legend()->setVisible(false); |
|
143 | 143 | break; |
|
144 | 144 | } |
|
145 | 145 | } |
|
146 | 146 | } |
|
147 | 147 | |
|
148 | 148 | DeclarativeChart::Legend DeclarativeChart::legend() |
|
149 | 149 | { |
|
150 | 150 | return m_legend; |
|
151 | 151 | } |
|
152 | 152 | |
|
153 | 153 | QAxis *DeclarativeChart::axisX() |
|
154 | 154 | { |
|
155 | 155 | return m_chart->axisX(); |
|
156 | 156 | } |
|
157 | 157 | |
|
158 | 158 | QAxis *DeclarativeChart::axisY() |
|
159 | 159 | { |
|
160 | 160 | return m_chart->axisY(); |
|
161 | 161 | } |
|
162 | 162 | |
|
163 | 163 | QVariantList DeclarativeChart::axisXLabels() |
|
164 | 164 | { |
|
165 | 165 | QVariantList labels; |
|
166 | 166 | foreach (qreal value, m_chart->axisX()->categories()->values()) { |
|
167 | 167 | labels.append(value); |
|
168 | 168 | labels.append(m_chart->axisX()->categories()->label(value)); |
|
169 | 169 | } |
|
170 | 170 | return labels; |
|
171 | 171 | } |
|
172 | 172 | |
|
173 | 173 | void DeclarativeChart::setAxisXLabels(QVariantList list) |
|
174 | 174 | { |
|
175 | 175 | QVariant value(QVariant::Invalid); |
|
176 | 176 | foreach (QVariant element, list) { |
|
177 | 177 | if (value.isValid() && element.type() == QVariant::String) { |
|
178 | 178 | m_chart->axisX()->categories()->insert(value.toDouble(), element.toString()); |
|
179 | 179 | value = QVariant(QVariant::Invalid); |
|
180 | 180 | } else { |
|
181 | 181 | if (element.canConvert(QVariant::Double)) |
|
182 | 182 | value = element; |
|
183 | 183 | } |
|
184 | 184 | } |
|
185 | 185 | } |
|
186 | 186 | |
|
187 | 187 | int DeclarativeChart::count() |
|
188 | 188 | { |
|
189 | 189 | return m_chart->series().count(); |
|
190 | 190 | } |
|
191 | 191 | |
|
192 | 192 | QAbstractSeries *DeclarativeChart::series(int index) |
|
193 | 193 | { |
|
194 | 194 | if (index < m_chart->series().count()) { |
|
195 | 195 | return m_chart->series().at(index); |
|
196 | 196 | } |
|
197 | 197 | return 0; |
|
198 | 198 | } |
|
199 | 199 | |
|
200 | 200 | QAbstractSeries *DeclarativeChart::series(QString seriesName) |
|
201 | 201 | { |
|
202 | 202 | foreach(QAbstractSeries *series, m_chart->series()) { |
|
203 | 203 | if (series->name() == seriesName) |
|
204 | 204 | return series; |
|
205 | 205 | } |
|
206 | 206 | return 0; |
|
207 | 207 | } |
|
208 | 208 | |
|
209 | 209 | QAbstractSeries *DeclarativeChart::createSeries(DeclarativeChart::SeriesType type, QString name) |
|
210 | 210 | { |
|
211 | 211 | QAbstractSeries *series = 0; |
|
212 | 212 | switch (type) { |
|
213 | 213 | case DeclarativeChart::SeriesTypeLine: |
|
214 | 214 | series = new DeclarativeLineSeries(); |
|
215 | 215 | break; |
|
216 | 216 | case DeclarativeChart::SeriesTypeArea: |
|
217 | 217 | series = new DeclarativeAreaSeries(); |
|
218 | 218 | break; |
|
219 | 219 | case DeclarativeChart::SeriesTypeBar: |
|
220 | 220 | series = new DeclarativeBarSeries(); |
|
221 | 221 | break; |
|
222 | 222 | case DeclarativeChart::SeriesTypeStackedBar: |
|
223 | 223 | // TODO |
|
224 | 224 | break; |
|
225 | 225 | case DeclarativeChart::SeriesTypePercentBar: |
|
226 | 226 | // TODO |
|
227 | 227 | break; |
|
228 | 228 | case DeclarativeChart::SeriesTypeGroupedBar: |
|
229 | 229 | series = new DeclarativeGroupedBarSeries(); |
|
230 | 230 | break; |
|
231 | 231 | case DeclarativeChart::SeriesTypePie: |
|
232 | 232 | series = new DeclarativePieSeries(); |
|
233 | 233 | break; |
|
234 | 234 | case DeclarativeChart::SeriesTypeScatter: |
|
235 | 235 | series = new DeclarativeScatterSeries(); |
|
236 | 236 | break; |
|
237 | 237 | case DeclarativeChart::SeriesTypeSpline: |
|
238 | 238 | series = new DeclarativeSplineSeries(); |
|
239 | 239 | break; |
|
240 | 240 | default: |
|
241 | 241 | qWarning() << "Illegal series type"; |
|
242 | 242 | } |
|
243 | 243 | series->setName(name); |
|
244 | 244 | m_chart->addSeries(series); |
|
245 | 245 | return series; |
|
246 | 246 | } |
|
247 | 247 | |
|
248 | 248 | #include "moc_declarativechart.cpp" |
|
249 | 249 | |
|
250 | 250 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,55 +1,61 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2012 Digia Plc |
|
4 | 4 | ** All rights reserved. |
|
5 | 5 | ** For any questions to Digia, please use contact form at http://qt.digia.com |
|
6 | 6 | ** |
|
7 | 7 | ** This file is part of the Qt Commercial Charts Add-on. |
|
8 | 8 | ** |
|
9 | 9 | ** $QT_BEGIN_LICENSE$ |
|
10 | 10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
11 | 11 | ** accordance with the Qt Commercial License Agreement provided with the |
|
12 | 12 | ** Software or, alternatively, in accordance with the terms contained in |
|
13 | 13 | ** a written agreement between you and Digia. |
|
14 | 14 | ** |
|
15 | 15 | ** If you have questions regarding the use of this file, please use |
|
16 | 16 | ** contact form at http://qt.digia.com |
|
17 | 17 | ** $QT_END_LICENSE$ |
|
18 | 18 | ** |
|
19 | 19 | ****************************************************************************/ |
|
20 | 20 | |
|
21 | 21 | #ifndef DECLARATIVELINESERIES_H |
|
22 | 22 | #define DECLARATIVELINESERIES_H |
|
23 | 23 | |
|
24 | 24 | #include "qchartglobal.h" |
|
25 | 25 | #include "qlineseries.h" |
|
26 | 26 | #include "declarativexyseries.h" |
|
27 | 27 | #include <QDeclarativeParserStatus> |
|
28 | 28 | #include <QDeclarativeListProperty> |
|
29 | #include <QDeclarativeParserStatus> | |
|
29 | 30 | |
|
30 | 31 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
31 | 32 | |
|
32 | class DeclarativeLineSeries : public QLineSeries, public DeclarativeXySeries | |
|
33 | class DeclarativeLineSeries : public QLineSeries, public DeclarativeXySeries, public QDeclarativeParserStatus | |
|
33 | 34 | { |
|
34 | 35 | Q_OBJECT |
|
36 | Q_INTERFACES(QDeclarativeParserStatus) | |
|
35 | 37 | Q_PROPERTY(QColor color READ penColor WRITE setPenColor) |
|
36 | 38 | Q_PROPERTY(QDeclarativeListProperty<QObject> declarativeChildren READ declarativeChildren) |
|
37 | 39 | Q_CLASSINFO("DefaultProperty", "declarativeChildren") |
|
38 | 40 | |
|
39 | 41 | public: |
|
40 | 42 | explicit DeclarativeLineSeries(QObject *parent = 0); |
|
41 | 43 | QDeclarativeListProperty<QObject> declarativeChildren(); |
|
42 | 44 | |
|
45 | public: // from QDeclarativeParserStatus | |
|
46 | void classBegin() { DeclarativeXySeries::classBegin(); } | |
|
47 | void componentComplete() { DeclarativeXySeries::componentComplete(); } | |
|
48 | ||
|
43 | 49 | public: // from QLineSeries |
|
44 | 50 | Q_INVOKABLE void append(qreal x, qreal y) { QLineSeries::append(x, y); } |
|
45 | 51 | Q_INVOKABLE void remove(qreal x, qreal y) { QLineSeries::remove(x, y); } |
|
46 | 52 | Q_INVOKABLE void clear() { QLineSeries::removeAll(); } |
|
47 | 53 | Q_INVOKABLE DeclarativeXyPoint *at(int index) { return DeclarativeXySeries::at(index); } |
|
48 | 54 | |
|
49 | 55 | public Q_SLOTS: |
|
50 | 56 | static void appendDeclarativeChildren(QDeclarativeListProperty<QObject> *list, QObject *element); |
|
51 | 57 | }; |
|
52 | 58 | |
|
53 | 59 | QTCOMMERCIALCHART_END_NAMESPACE |
|
54 | 60 | |
|
55 | 61 | #endif // DECLARATIVELINESERIES_H |
@@ -1,57 +1,63 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2012 Digia Plc |
|
4 | 4 | ** All rights reserved. |
|
5 | 5 | ** For any questions to Digia, please use contact form at http://qt.digia.com |
|
6 | 6 | ** |
|
7 | 7 | ** This file is part of the Qt Commercial Charts Add-on. |
|
8 | 8 | ** |
|
9 | 9 | ** $QT_BEGIN_LICENSE$ |
|
10 | 10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
11 | 11 | ** accordance with the Qt Commercial License Agreement provided with the |
|
12 | 12 | ** Software or, alternatively, in accordance with the terms contained in |
|
13 | 13 | ** a written agreement between you and Digia. |
|
14 | 14 | ** |
|
15 | 15 | ** If you have questions regarding the use of this file, please use |
|
16 | 16 | ** contact form at http://qt.digia.com |
|
17 | 17 | ** $QT_END_LICENSE$ |
|
18 | 18 | ** |
|
19 | 19 | ****************************************************************************/ |
|
20 | 20 | |
|
21 | 21 | #ifndef DECLARATIVESCATTERSERIES_H |
|
22 | 22 | #define DECLARATIVESCATTERSERIES_H |
|
23 | 23 | |
|
24 | 24 | #include "qchartglobal.h" |
|
25 | 25 | #include "qscatterseries.h" |
|
26 | 26 | #include "declarativexyseries.h" |
|
27 | 27 | #include <QDeclarativeListProperty> |
|
28 | #include <QDeclarativeParserStatus> | |
|
28 | 29 | |
|
29 | 30 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
30 | 31 | |
|
31 | class DeclarativeScatterSeries : public QScatterSeries, public DeclarativeXySeries | |
|
32 | class DeclarativeScatterSeries : public QScatterSeries, public DeclarativeXySeries, public QDeclarativeParserStatus | |
|
32 | 33 | { |
|
33 | 34 | Q_OBJECT |
|
35 | Q_INTERFACES(QDeclarativeParserStatus) | |
|
34 | 36 | Q_PROPERTY(QColor color READ brushColor WRITE setBrushColor) |
|
35 | 37 | Q_PROPERTY(QColor borderColor READ penColor WRITE setPenColor) |
|
36 | 38 | Q_PROPERTY(QDeclarativeListProperty<QObject> declarativeChildren READ declarativeChildren) |
|
37 | 39 | Q_CLASSINFO("DefaultProperty", "declarativeChildren") |
|
38 | 40 | |
|
39 | 41 | public: |
|
40 | 42 | explicit DeclarativeScatterSeries(QObject *parent = 0); |
|
41 | 43 | QDeclarativeListProperty<QObject> declarativeChildren(); |
|
42 | 44 | QColor brushColor(); |
|
43 | 45 | void setBrushColor(QColor color); |
|
44 | 46 | |
|
47 | public: // from QDeclarativeParserStatus | |
|
48 | void classBegin() { DeclarativeXySeries::classBegin(); } | |
|
49 | void componentComplete() { DeclarativeXySeries::componentComplete(); } | |
|
50 | ||
|
45 | 51 | public: // from QScatterSeries |
|
46 | 52 | Q_INVOKABLE void append(qreal x, qreal y) { QScatterSeries::append(x, y); } |
|
47 | 53 | Q_INVOKABLE void remove(qreal x, qreal y) { QScatterSeries::remove(x, y); } |
|
48 | 54 | Q_INVOKABLE void clear() { QScatterSeries::removeAll(); } |
|
49 | 55 | Q_INVOKABLE DeclarativeXyPoint *at(int index) { return DeclarativeXySeries::at(index); } |
|
50 | 56 | |
|
51 | 57 | public Q_SLOTS: |
|
52 | 58 | static void appendDeclarativeChildren(QDeclarativeListProperty<QObject> *list, QObject *element); |
|
53 | 59 | }; |
|
54 | 60 | |
|
55 | 61 | QTCOMMERCIALCHART_END_NAMESPACE |
|
56 | 62 | |
|
57 | 63 | #endif // DECLARATIVESCATTERSERIES_H |
@@ -1,55 +1,61 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2012 Digia Plc |
|
4 | 4 | ** All rights reserved. |
|
5 | 5 | ** For any questions to Digia, please use contact form at http://qt.digia.com |
|
6 | 6 | ** |
|
7 | 7 | ** This file is part of the Qt Commercial Charts Add-on. |
|
8 | 8 | ** |
|
9 | 9 | ** $QT_BEGIN_LICENSE$ |
|
10 | 10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
11 | 11 | ** accordance with the Qt Commercial License Agreement provided with the |
|
12 | 12 | ** Software or, alternatively, in accordance with the terms contained in |
|
13 | 13 | ** a written agreement between you and Digia. |
|
14 | 14 | ** |
|
15 | 15 | ** If you have questions regarding the use of this file, please use |
|
16 | 16 | ** contact form at http://qt.digia.com |
|
17 | 17 | ** $QT_END_LICENSE$ |
|
18 | 18 | ** |
|
19 | 19 | ****************************************************************************/ |
|
20 | 20 | |
|
21 | 21 | #ifndef DECLARATIVESPLINESERIES_H |
|
22 | 22 | #define DECLARATIVESPLINESERIES_H |
|
23 | 23 | |
|
24 | 24 | #include "qchartglobal.h" |
|
25 | 25 | #include "qsplineseries.h" |
|
26 | 26 | #include "declarativexyseries.h" |
|
27 | 27 | #include <QDeclarativeParserStatus> |
|
28 | 28 | #include <QDeclarativeListProperty> |
|
29 | #include <QDeclarativeParserStatus> | |
|
29 | 30 | |
|
30 | 31 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
31 | 32 | |
|
32 | class DeclarativeSplineSeries : public QSplineSeries, public DeclarativeXySeries | |
|
33 | class DeclarativeSplineSeries : public QSplineSeries, public DeclarativeXySeries, public QDeclarativeParserStatus | |
|
33 | 34 | { |
|
34 | 35 | Q_OBJECT |
|
36 | Q_INTERFACES(QDeclarativeParserStatus) | |
|
35 | 37 | Q_PROPERTY(QColor color READ penColor WRITE setPenColor) |
|
36 | 38 | Q_PROPERTY(QDeclarativeListProperty<QObject> declarativeChildren READ declarativeChildren) |
|
37 | 39 | Q_CLASSINFO("DefaultProperty", "declarativeChildren") |
|
38 | 40 | |
|
39 | 41 | public: |
|
40 | 42 | explicit DeclarativeSplineSeries(QObject *parent = 0); |
|
41 | 43 | QDeclarativeListProperty<QObject> declarativeChildren(); |
|
42 | 44 | |
|
45 | public: // from QDeclarativeParserStatus | |
|
46 | void classBegin() { DeclarativeXySeries::classBegin(); } | |
|
47 | void componentComplete() { DeclarativeXySeries::componentComplete(); } | |
|
48 | ||
|
43 | 49 | public: // from QSplineSeries |
|
44 | 50 | Q_INVOKABLE void append(qreal x, qreal y) { QSplineSeries::append(x, y); } |
|
45 | 51 | Q_INVOKABLE void remove(qreal x, qreal y) { QSplineSeries::remove(x, y); } |
|
46 | 52 | Q_INVOKABLE void clear() { QSplineSeries::removeAll(); } |
|
47 | 53 | Q_INVOKABLE DeclarativeXyPoint *at(int index) { return DeclarativeXySeries::at(index); } |
|
48 | 54 | |
|
49 | 55 | public Q_SLOTS: |
|
50 | 56 | static void appendDeclarativeChildren(QDeclarativeListProperty<QObject> *list, QObject *element); |
|
51 | 57 | }; |
|
52 | 58 | |
|
53 | 59 | QTCOMMERCIALCHART_END_NAMESPACE |
|
54 | 60 | |
|
55 | 61 | #endif // DECLARATIVESPLINESERIES_H |
@@ -1,67 +1,91 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2012 Digia Plc |
|
4 | 4 | ** All rights reserved. |
|
5 | 5 | ** For any questions to Digia, please use contact form at http://qt.digia.com |
|
6 | 6 | ** |
|
7 | 7 | ** This file is part of the Qt Commercial Charts Add-on. |
|
8 | 8 | ** |
|
9 | 9 | ** $QT_BEGIN_LICENSE$ |
|
10 | 10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
11 | 11 | ** accordance with the Qt Commercial License Agreement provided with the |
|
12 | 12 | ** Software or, alternatively, in accordance with the terms contained in |
|
13 | 13 | ** a written agreement between you and Digia. |
|
14 | 14 | ** |
|
15 | 15 | ** If you have questions regarding the use of this file, please use |
|
16 | 16 | ** contact form at http://qt.digia.com |
|
17 | 17 | ** $QT_END_LICENSE$ |
|
18 | 18 | ** |
|
19 | 19 | ****************************************************************************/ |
|
20 | 20 | |
|
21 | 21 | //#include "DeclarativeXySeries.h" |
|
22 | 22 | #include "declarativexyseries.h" |
|
23 | 23 | #include "declarativechart.h" |
|
24 | 24 | #include <QXYSeries> |
|
25 | #include <QXYModelMapper> | |
|
25 | #include <QVXYModelMapper> | |
|
26 | #include <QHXYModelMapper> | |
|
26 | 27 | #include <QDeclarativeListProperty> |
|
27 | 28 | |
|
28 | 29 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
29 | 30 | |
|
30 | 31 | DeclarativeXySeries::DeclarativeXySeries() |
|
31 | 32 | { |
|
32 | 33 | } |
|
33 | 34 | |
|
34 | 35 | DeclarativeXySeries::~DeclarativeXySeries() |
|
35 | 36 | { |
|
36 | 37 | } |
|
37 | 38 | |
|
39 | void DeclarativeXySeries::classBegin() | |
|
40 | { | |
|
41 | } | |
|
42 | ||
|
43 | void DeclarativeXySeries::componentComplete() | |
|
44 | { | |
|
45 | // All the inherited objects must be of type QXYSeries, so it is safe to cast | |
|
46 | QXYSeries *series = reinterpret_cast<QXYSeries *>(this); | |
|
47 | foreach(QObject *child, series->children()) { | |
|
48 | if (qobject_cast<DeclarativeXyPoint *>(child)) { | |
|
49 | // TODO: | |
|
50 | // series->append(qobject_cast<DeclarativeXyPoint *>(child)); | |
|
51 | } else if(qobject_cast<QVXYModelMapper *>(child)) { | |
|
52 | QVXYModelMapper *mapper = qobject_cast<QVXYModelMapper *>(child); | |
|
53 | mapper->setSeries(series); | |
|
54 | } else if(qobject_cast<QHXYModelMapper *>(child)) { | |
|
55 | QHXYModelMapper *mapper = qobject_cast<QHXYModelMapper *>(child); | |
|
56 | mapper->setSeries(series); | |
|
57 | } | |
|
58 | } | |
|
59 | } | |
|
60 | ||
|
61 | ||
|
38 | 62 | QColor DeclarativeXySeries::penColor() |
|
39 | 63 | { |
|
40 | 64 | // All the inherited objects must be of type QXYSeries, so it is safe to cast |
|
41 | 65 | QXYSeries *series = reinterpret_cast<QXYSeries *>(this); |
|
42 | 66 | return series->pen().color(); |
|
43 | 67 | } |
|
44 | 68 | |
|
45 | 69 | void DeclarativeXySeries::setPenColor(QColor color) |
|
46 | 70 | { |
|
47 | 71 | QXYSeries *series = reinterpret_cast<QXYSeries *>(this); |
|
48 | 72 | QPen pen = series->pen(); |
|
49 | 73 | pen.setColor(color); |
|
50 | 74 | series->setPen(pen); |
|
51 | 75 | } |
|
52 | 76 | |
|
53 | 77 | DeclarativeXyPoint *DeclarativeXySeries::at(int index) |
|
54 | 78 | { |
|
55 | 79 | QXYSeries *series = reinterpret_cast<QXYSeries *>(this); |
|
56 | 80 | if (index < series->count()) { |
|
57 | 81 | QPointF point = series->points().at(index); |
|
58 | 82 | DeclarativeXyPoint *xyPoint = new DeclarativeXyPoint(series); |
|
59 | 83 | xyPoint->setX(point.x()); |
|
60 | 84 | xyPoint->setY(point.y()); |
|
61 | 85 | return xyPoint; |
|
62 | 86 | } |
|
63 | 87 | return 0; |
|
64 | 88 | } |
|
65 | 89 | |
|
66 | 90 | |
|
67 | 91 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,47 +1,49 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2012 Digia Plc |
|
4 | 4 | ** All rights reserved. |
|
5 | 5 | ** For any questions to Digia, please use contact form at http://qt.digia.com |
|
6 | 6 | ** |
|
7 | 7 | ** This file is part of the Qt Commercial Charts Add-on. |
|
8 | 8 | ** |
|
9 | 9 | ** $QT_BEGIN_LICENSE$ |
|
10 | 10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
11 | 11 | ** accordance with the Qt Commercial License Agreement provided with the |
|
12 | 12 | ** Software or, alternatively, in accordance with the terms contained in |
|
13 | 13 | ** a written agreement between you and Digia. |
|
14 | 14 | ** |
|
15 | 15 | ** If you have questions regarding the use of this file, please use |
|
16 | 16 | ** contact form at http://qt.digia.com |
|
17 | 17 | ** $QT_END_LICENSE$ |
|
18 | 18 | ** |
|
19 | 19 | ****************************************************************************/ |
|
20 | 20 | |
|
21 | 21 | #ifndef DECLARATIVE_XY_SERIES_H |
|
22 | 22 | #define DECLARATIVE_XY_SERIES_H |
|
23 | 23 | |
|
24 | 24 | #include "qchartglobal.h" |
|
25 | 25 | #include "declarativexypoint.h" |
|
26 | 26 | #include <QColor> |
|
27 | 27 | |
|
28 | 28 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
29 | 29 | |
|
30 | 30 | class QChart; |
|
31 | 31 | class QAbstractSeries; |
|
32 | 32 | |
|
33 | 33 | class DeclarativeXySeries |
|
34 | 34 | { |
|
35 | 35 | public: |
|
36 | 36 | explicit DeclarativeXySeries(); |
|
37 | 37 | ~DeclarativeXySeries(); |
|
38 | 38 | |
|
39 | 39 | public: |
|
40 | void classBegin(); | |
|
41 | void componentComplete(); | |
|
40 | 42 | QColor penColor(); |
|
41 | 43 | void setPenColor(QColor color); |
|
42 | 44 | DeclarativeXyPoint *at(int index); |
|
43 | 45 | }; |
|
44 | 46 | |
|
45 | 47 | QTCOMMERCIALCHART_END_NAMESPACE |
|
46 | 48 | |
|
47 | 49 | #endif // DECLARATIVE_XY_SERIES_H |
@@ -1,88 +1,92 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2012 Digia Plc |
|
4 | 4 | ** All rights reserved. |
|
5 | 5 | ** For any questions to Digia, please use contact form at http://qt.digia.com |
|
6 | 6 | ** |
|
7 | 7 | ** This file is part of the Qt Commercial Charts Add-on. |
|
8 | 8 | ** |
|
9 | 9 | ** $QT_BEGIN_LICENSE$ |
|
10 | 10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
11 | 11 | ** accordance with the Qt Commercial License Agreement provided with the |
|
12 | 12 | ** Software or, alternatively, in accordance with the terms contained in |
|
13 | 13 | ** a written agreement between you and Digia. |
|
14 | 14 | ** |
|
15 | 15 | ** If you have questions regarding the use of this file, please use |
|
16 | 16 | ** contact form at http://qt.digia.com |
|
17 | 17 | ** $QT_END_LICENSE$ |
|
18 | 18 | ** |
|
19 | 19 | ****************************************************************************/ |
|
20 | 20 | |
|
21 | 21 | #include <QtDeclarative/qdeclarativeextensionplugin.h> |
|
22 | 22 | #include <QtDeclarative/qdeclarative.h> |
|
23 | 23 | #include "qchart.h" |
|
24 | 24 | #include "qaxiscategories.h" |
|
25 | 25 | #include "declarativechart.h" |
|
26 | 26 | #include "declarativexypoint.h" |
|
27 | 27 | #include "declarativelineseries.h" |
|
28 | 28 | #include "declarativesplineseries.h" |
|
29 | 29 | #include "declarativeareaseries.h" |
|
30 | 30 | #include "declarativescatterseries.h" |
|
31 | 31 | #include "declarativebarseries.h" |
|
32 | 32 | #include "declarativepieseries.h" |
|
33 | #include <QVXYModelMapper> | |
|
34 | #include <QHXYModelMapper> | |
|
33 | 35 | #include <QHPieModelMapper> |
|
34 | 36 | #include <QVPieModelMapper> |
|
35 | #include <QXYModelMapper> | |
|
36 | 37 | |
|
37 | 38 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
38 | 39 | |
|
39 | 40 | class ChartQmlPlugin : public QDeclarativeExtensionPlugin |
|
40 | 41 | { |
|
41 | 42 | Q_OBJECT |
|
42 | 43 | public: |
|
43 | 44 | virtual void registerTypes(const char *uri) |
|
44 | 45 | { |
|
45 | 46 | Q_ASSERT(QLatin1String(uri) == QLatin1String("QtCommercial.Chart")); |
|
46 | 47 | |
|
47 | 48 | qmlRegisterType<DeclarativeChart>(uri, 1, 0, "ChartView"); |
|
48 | 49 | qmlRegisterType<DeclarativeXyPoint>(uri, 1, 0, "XyPoint"); |
|
49 | 50 | qmlRegisterType<DeclarativeScatterSeries>(uri, 1, 0, "ScatterSeries"); |
|
50 | 51 | qmlRegisterType<DeclarativeLineSeries>(uri, 1, 0, "LineSeries"); |
|
51 | 52 | qmlRegisterType<DeclarativeSplineSeries>(uri, 1, 0, "SplineSeries"); |
|
52 | 53 | qmlRegisterType<DeclarativeAreaSeries>(uri, 1, 0, "AreaSeries"); |
|
53 | 54 | qmlRegisterType<DeclarativeBarSeries>(uri, 1, 0, "BarSeries"); |
|
54 | 55 | qmlRegisterType<DeclarativeGroupedBarSeries>(uri, 1, 0, "GroupedBarSeries"); |
|
55 | 56 | qmlRegisterType<DeclarativePieSeries>(uri, 1, 0, "PieSeries"); |
|
56 | 57 | qmlRegisterType<DeclarativePieSlice>(uri, 1, 0, "PieSlice"); |
|
57 | 58 | qmlRegisterType<DeclarativeBarSet>(uri, 1, 0, "BarSet"); |
|
59 | qmlRegisterType<QHXYModelMapper>(uri, 1, 0, "HXYModelMapper"); | |
|
60 | qmlRegisterType<QVXYModelMapper>(uri, 1, 0, "VXYModelMapper"); | |
|
61 | qmlRegisterType<QHPieModelMapper>(uri, 1, 0, "HPieModelMapper"); | |
|
62 | qmlRegisterType<QVPieModelMapper>(uri, 1, 0, "VPieModelMapper"); | |
|
58 | 63 | |
|
59 | 64 | |
|
60 | 65 | qmlRegisterUncreatableType<QScatterSeries>(uri, 1, 0, "QScatterSeries", |
|
61 | 66 | QLatin1String("Trying to create uncreatable: QScatterSeries.")); |
|
62 | 67 | qmlRegisterUncreatableType<QPieSeries>(uri, 1, 0, "QPieSeries", |
|
63 | 68 | QLatin1String("Trying to create uncreatable: QPieSeries.")); |
|
64 | 69 | qmlRegisterUncreatableType<QAbstractItemModel>(uri, 1, 0, "AbstractItemModel", |
|
65 | 70 | QLatin1String("Trying to create uncreatable: AbstractItemModel.")); |
|
71 | qmlRegisterUncreatableType<QXYModelMapper>(uri, 1, 0, "XYModelMapper", | |
|
72 | QLatin1String("Trying to create uncreatable: XYModelMapper.")); | |
|
66 | 73 | qmlRegisterUncreatableType<QPieModelMapper>(uri, 1, 0, "PieModelMapper", |
|
67 | 74 | QLatin1String("Trying to create uncreatable: PieModelMapper.")); |
|
68 | qmlRegisterType<QHPieModelMapper>(uri, 1, 0, "HPieModelMapper"); | |
|
69 | qmlRegisterType<QVPieModelMapper>(uri, 1, 0, "VPieModelMapper"); | |
|
70 | ||
|
71 | 75 | qmlRegisterUncreatableType<QAbstractSeries>(uri, 1, 0, "AbstractSeries", |
|
72 | 76 | QLatin1String("Trying to create uncreatable: AbstractSeries.")); |
|
73 | 77 | qmlRegisterUncreatableType<QAxis>(uri, 1, 0, "Axis", |
|
74 | 78 | QLatin1String("Trying to create uncreatable: Axis.")); |
|
75 | 79 | qmlRegisterUncreatableType<QPieModelMapper>(uri, 1, 0, "PieModelMapper", |
|
76 | 80 | QLatin1String("Trying to create uncreatable: PieModelMapper.")); |
|
77 | 81 | qmlRegisterUncreatableType<QXYModelMapper>(uri, 1, 0, "XYModelMapper", |
|
78 | 82 | QLatin1String("Trying to create uncreatable: XYModelMapper.")); |
|
79 | 83 | } |
|
80 | 84 | }; |
|
81 | 85 | |
|
82 | 86 | #include "plugin.moc" |
|
83 | 87 | |
|
84 | 88 | QTCOMMERCIALCHART_END_NAMESPACE |
|
85 | 89 | |
|
86 | 90 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
87 | 91 | |
|
88 | 92 | Q_EXPORT_PLUGIN2(qtcommercialchartqml, QT_PREPEND_NAMESPACE(ChartQmlPlugin)) |
@@ -1,414 +1,417 | |||
|
1 | 1 | #include "qxymodelmapper.h" |
|
2 | 2 | #include "qxymodelmapper_p.h" |
|
3 | 3 | #include "qxyseries.h" |
|
4 | 4 | #include <QAbstractItemModel> |
|
5 | 5 | |
|
6 | 6 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
7 | 7 | |
|
8 | 8 | QXYModelMapper::QXYModelMapper(QObject *parent): |
|
9 | 9 | QObject(parent), |
|
10 | 10 | d_ptr(new QXYModelMapperPrivate(this)) |
|
11 | 11 | { |
|
12 | 12 | } |
|
13 | 13 | |
|
14 | 14 | QAbstractItemModel* QXYModelMapper::model() const |
|
15 | 15 | { |
|
16 | 16 | Q_D(const QXYModelMapper); |
|
17 | 17 | return d->m_model; |
|
18 | 18 | } |
|
19 | 19 | |
|
20 | 20 | void QXYModelMapper::setModel(QAbstractItemModel *model) |
|
21 | 21 | { |
|
22 | 22 | if (model == 0) |
|
23 | 23 | return; |
|
24 | 24 | |
|
25 | 25 | Q_D(QXYModelMapper); |
|
26 | 26 | if (d->m_model) { |
|
27 | 27 | disconnect(d->m_model, 0, d, 0); |
|
28 | 28 | } |
|
29 | 29 | |
|
30 | 30 | d->m_model = model; |
|
31 | 31 | d->initializeXYFromModel(); |
|
32 | 32 | // connect signals from the model |
|
33 | 33 | connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex))); |
|
34 | 34 | connect(d->m_model, SIGNAL(rowsInserted(QModelIndex,int,int)), d, SLOT(modelRowsAdded(QModelIndex,int,int))); |
|
35 | 35 | connect(d->m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)), d, SLOT(modelRowsRemoved(QModelIndex,int,int))); |
|
36 | 36 | connect(d->m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), d, SLOT(modelColumnsAdded(QModelIndex,int,int))); |
|
37 | 37 | connect(d->m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), d, SLOT(modelColumnsRemoved(QModelIndex,int,int))); |
|
38 | 38 | } |
|
39 | 39 | |
|
40 | 40 | QXYSeries* QXYModelMapper::series() const |
|
41 | 41 | { |
|
42 | 42 | Q_D(const QXYModelMapper); |
|
43 | 43 | return d->m_series; |
|
44 | 44 | } |
|
45 | 45 | |
|
46 | 46 | void QXYModelMapper::setSeries(QXYSeries *series) |
|
47 | 47 | { |
|
48 | 48 | Q_D(QXYModelMapper); |
|
49 | 49 | if (d->m_series) { |
|
50 | 50 | disconnect(d->m_series, 0, d, 0); |
|
51 | 51 | } |
|
52 | 52 | |
|
53 | 53 | if (series == 0) |
|
54 | 54 | return; |
|
55 | 55 | |
|
56 | 56 | d->m_series = series; |
|
57 | 57 | d->initializeXYFromModel(); |
|
58 | 58 | // connect the signals from the series |
|
59 | 59 | connect(d->m_series, SIGNAL(pointAdded(int)), d, SLOT(handlePointAdded(int))); |
|
60 | 60 | connect(d->m_series, SIGNAL(pointRemoved(int)), d, SLOT(handlePointRemoved(int))); |
|
61 | 61 | connect(d->m_series, SIGNAL(pointReplaced(int)), d, SLOT(handlePointReplaced(int))); |
|
62 | 62 | } |
|
63 | 63 | |
|
64 | 64 | int QXYModelMapper::first() const |
|
65 | 65 | { |
|
66 | 66 | Q_D(const QXYModelMapper); |
|
67 | 67 | return d->m_first; |
|
68 | 68 | } |
|
69 | 69 | |
|
70 | 70 | void QXYModelMapper::setFirst(int first) |
|
71 | 71 | { |
|
72 | 72 | Q_D(QXYModelMapper); |
|
73 | 73 | d->m_first = qMax(first, 0); |
|
74 | 74 | d->initializeXYFromModel(); |
|
75 | 75 | } |
|
76 | 76 | |
|
77 | 77 | int QXYModelMapper::count() const |
|
78 | 78 | { |
|
79 | 79 | Q_D(const QXYModelMapper); |
|
80 | 80 | return d->m_count; |
|
81 | 81 | } |
|
82 | 82 | |
|
83 | 83 | void QXYModelMapper::setCount(int count) |
|
84 | 84 | { |
|
85 | 85 | Q_D(QXYModelMapper); |
|
86 | 86 | d->m_count = qMax(count, -1); |
|
87 | 87 | d->initializeXYFromModel(); |
|
88 | 88 | } |
|
89 | 89 | |
|
90 | 90 | Qt::Orientation QXYModelMapper::orientation() const |
|
91 | 91 | { |
|
92 | 92 | Q_D(const QXYModelMapper); |
|
93 | 93 | return d->m_orientation; |
|
94 | 94 | } |
|
95 | 95 | |
|
96 | 96 | void QXYModelMapper::setOrientation(Qt::Orientation orientation) |
|
97 | 97 | { |
|
98 | 98 | Q_D(QXYModelMapper); |
|
99 | 99 | d->m_orientation = orientation; |
|
100 | 100 | d->initializeXYFromModel(); |
|
101 | 101 | } |
|
102 | 102 | |
|
103 | 103 | int QXYModelMapper::xSection() const |
|
104 | 104 | { |
|
105 | 105 | Q_D(const QXYModelMapper); |
|
106 | 106 | return d->m_xSection; |
|
107 | 107 | } |
|
108 | 108 | |
|
109 | 109 | void QXYModelMapper::setXSection(int xSection) |
|
110 | 110 | { |
|
111 | 111 | Q_D(QXYModelMapper); |
|
112 | 112 | d->m_xSection = xSection; |
|
113 | 113 | d->initializeXYFromModel(); |
|
114 | 114 | } |
|
115 | 115 | |
|
116 | 116 | int QXYModelMapper::ySection() const |
|
117 | 117 | { |
|
118 | 118 | Q_D(const QXYModelMapper); |
|
119 | 119 | return d->m_ySection; |
|
120 | 120 | } |
|
121 | 121 | |
|
122 | 122 | void QXYModelMapper::setYSection(int ySection) |
|
123 | 123 | { |
|
124 | 124 | Q_D(QXYModelMapper); |
|
125 | 125 | d->m_ySection = ySection; |
|
126 | 126 | d->initializeXYFromModel(); |
|
127 | 127 | } |
|
128 | 128 | |
|
129 | 129 | void QXYModelMapper::reset() |
|
130 | 130 | { |
|
131 | 131 | Q_D(QXYModelMapper); |
|
132 | 132 | d->m_first = 0; |
|
133 | 133 | d->m_count = -1; |
|
134 | 134 | d->m_orientation = Qt::Vertical; |
|
135 | 135 | d->m_xSection = -1; |
|
136 | 136 | d->m_ySection = -1; |
|
137 | 137 | d->initializeXYFromModel(); |
|
138 | 138 | } |
|
139 | 139 | |
|
140 | 140 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
141 | 141 | |
|
142 | 142 | QXYModelMapperPrivate::QXYModelMapperPrivate(QXYModelMapper *q) : |
|
143 | 143 | m_series(0), |
|
144 | 144 | m_model(0), |
|
145 | 145 | m_first(0), |
|
146 | 146 | m_count(-1), |
|
147 | 147 | m_orientation(Qt::Vertical), |
|
148 | 148 | m_xSection(-1), |
|
149 | 149 | m_ySection(-1), |
|
150 | 150 | m_seriesSignalsBlock(false), |
|
151 | 151 | m_modelSignalsBlock(false), |
|
152 | 152 | q_ptr(q) |
|
153 | 153 | { |
|
154 | 154 | } |
|
155 | 155 | |
|
156 | 156 | void QXYModelMapperPrivate::blockModelSignals(bool block) |
|
157 | 157 | { |
|
158 | 158 | m_modelSignalsBlock = block; |
|
159 | 159 | } |
|
160 | 160 | |
|
161 | 161 | void QXYModelMapperPrivate::blockSeriesSignals(bool block) |
|
162 | 162 | { |
|
163 | 163 | m_seriesSignalsBlock = block; |
|
164 | 164 | } |
|
165 | 165 | |
|
166 | 166 | QModelIndex QXYModelMapperPrivate::xModelIndex(int xPos) |
|
167 | 167 | { |
|
168 | 168 | if (m_count != -1 && xPos >= m_count) |
|
169 | 169 | return QModelIndex(); // invalid |
|
170 | 170 | |
|
171 | 171 | if (m_orientation == Qt::Vertical) |
|
172 | 172 | return m_model->index(xPos + m_first, m_xSection); |
|
173 | 173 | else |
|
174 | 174 | return m_model->index(m_xSection, xPos + m_first); |
|
175 | 175 | } |
|
176 | 176 | |
|
177 | 177 | QModelIndex QXYModelMapperPrivate::yModelIndex(int yPos) |
|
178 | 178 | { |
|
179 | 179 | if (m_count != -1 && yPos >= m_count) |
|
180 | 180 | return QModelIndex(); // invalid |
|
181 | 181 | |
|
182 | 182 | if (m_orientation == Qt::Vertical) |
|
183 | 183 | return m_model->index(yPos + m_first, m_ySection); |
|
184 | 184 | else |
|
185 | 185 | return m_model->index(m_ySection, yPos + m_first); |
|
186 | 186 | } |
|
187 | 187 | |
|
188 | 188 | void QXYModelMapperPrivate::handlePointAdded(int pointPos) |
|
189 | 189 | { |
|
190 | 190 | if (m_seriesSignalsBlock) |
|
191 | 191 | return; |
|
192 | 192 | |
|
193 | 193 | if (m_count != -1) |
|
194 | 194 | m_count += 1; |
|
195 | 195 | |
|
196 | 196 | blockModelSignals(); |
|
197 | 197 | if (m_orientation == Qt::Vertical) |
|
198 | 198 | m_model->insertRows(pointPos + m_first, 1); |
|
199 | 199 | else |
|
200 | 200 | m_model->insertColumns(pointPos + m_first, 1); |
|
201 | 201 | |
|
202 | 202 | m_model->setData(xModelIndex(pointPos), m_series->points().at(pointPos).x()); |
|
203 | 203 | m_model->setData(yModelIndex(pointPos), m_series->points().at(pointPos).y()); |
|
204 | 204 | blockModelSignals(false); |
|
205 | 205 | } |
|
206 | 206 | |
|
207 | 207 | void QXYModelMapperPrivate::handlePointRemoved(int pointPos) |
|
208 | 208 | { |
|
209 | 209 | if (m_seriesSignalsBlock) |
|
210 | 210 | return; |
|
211 | 211 | |
|
212 | 212 | if (m_count != -1) |
|
213 | 213 | m_count -= 1; |
|
214 | 214 | |
|
215 | 215 | blockModelSignals(); |
|
216 | 216 | if (m_orientation == Qt::Vertical) |
|
217 | 217 | m_model->removeRow(pointPos + m_first); |
|
218 | 218 | else |
|
219 | 219 | m_model->removeColumn(pointPos + m_first); |
|
220 | 220 | blockModelSignals(false); |
|
221 | 221 | } |
|
222 | 222 | |
|
223 | 223 | void QXYModelMapperPrivate::handlePointReplaced(int pointPos) |
|
224 | 224 | { |
|
225 | 225 | if (m_seriesSignalsBlock) |
|
226 | 226 | return; |
|
227 | 227 | |
|
228 | 228 | blockModelSignals(); |
|
229 | 229 | m_model->setData(xModelIndex(pointPos), m_series->points().at(pointPos).x()); |
|
230 | 230 | m_model->setData(yModelIndex(pointPos), m_series->points().at(pointPos).y()); |
|
231 | 231 | blockModelSignals(false); |
|
232 | 232 | } |
|
233 | 233 | |
|
234 | 234 | void QXYModelMapperPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight) |
|
235 | 235 | { |
|
236 | if (m_model == 0 || m_series == 0) | |
|
237 | return; | |
|
238 | ||
|
236 | 239 | if (m_modelSignalsBlock) |
|
237 | 240 | return; |
|
238 | 241 | |
|
239 | 242 | blockSeriesSignals(); |
|
240 | 243 | QModelIndex index; |
|
241 | 244 | QPointF oldPoint; |
|
242 | 245 | QPointF newPoint; |
|
243 | 246 | for (int row = topLeft.row(); row <= bottomRight.row(); row++) { |
|
244 | 247 | for (int column = topLeft.column(); column <= bottomRight.column(); column++) { |
|
245 | 248 | index = topLeft.sibling(row, column); |
|
246 | 249 | if (m_orientation == Qt::Vertical && (index.column() == m_xSection|| index.column() == m_ySection)) { |
|
247 | 250 | if (index.row() >= m_first && (m_count == - 1 || index.row() < m_first + m_count)) { |
|
248 | 251 | oldPoint = m_series->points().at(index.row() - m_first); |
|
249 | 252 | newPoint.setX(m_model->data(m_model->index(index.row(), m_xSection)).toReal()); |
|
250 | 253 | newPoint.setY(m_model->data(m_model->index(index.row(), m_ySection)).toReal()); |
|
251 | 254 | } |
|
252 | 255 | } else if (m_orientation == Qt::Horizontal && (index.row() == m_xSection || index.row() == m_ySection)) { |
|
253 | 256 | if (index.column() >= m_first && (m_count == - 1 || index.column() < m_first + m_count)) { |
|
254 | 257 | oldPoint = m_series->points().at(index.column() - m_first); |
|
255 | 258 | newPoint.setX(m_model->data(m_model->index(m_xSection, index.column())).toReal()); |
|
256 | 259 | newPoint.setY(m_model->data(m_model->index(m_ySection, index.column())).toReal()); |
|
257 | 260 | } |
|
258 | 261 | } else { |
|
259 | 262 | continue; |
|
260 | 263 | } |
|
261 | 264 | m_series->replace(oldPoint, newPoint); |
|
262 | 265 | } |
|
263 | 266 | blockSeriesSignals(false); |
|
264 | 267 | } |
|
265 | 268 | } |
|
266 | 269 | |
|
267 | 270 | void QXYModelMapperPrivate::modelRowsAdded(QModelIndex parent, int start, int end) |
|
268 | 271 | { |
|
269 | 272 | Q_UNUSED(parent); |
|
270 | 273 | if (m_modelSignalsBlock) |
|
271 | 274 | return; |
|
272 | 275 | |
|
273 | 276 | blockSeriesSignals(); |
|
274 | 277 | if (m_orientation == Qt::Vertical) |
|
275 | 278 | insertData(start, end); |
|
276 | 279 | else if (start <= m_xSection || start <= m_ySection) // if the changes affect the map - reinitialize the xy |
|
277 | 280 | initializeXYFromModel(); |
|
278 | 281 | blockSeriesSignals(false); |
|
279 | 282 | } |
|
280 | 283 | |
|
281 | 284 | void QXYModelMapperPrivate::modelRowsRemoved(QModelIndex parent, int start, int end) |
|
282 | 285 | { |
|
283 | 286 | Q_UNUSED(parent); |
|
284 | 287 | if (m_modelSignalsBlock) |
|
285 | 288 | return; |
|
286 | 289 | |
|
287 | 290 | blockSeriesSignals(); |
|
288 | 291 | if (m_orientation == Qt::Vertical) |
|
289 | 292 | removeData(start, end); |
|
290 | 293 | else if (start <= m_xSection || start <= m_ySection) // if the changes affect the map - reinitialize the xy |
|
291 | 294 | initializeXYFromModel(); |
|
292 | 295 | blockSeriesSignals(false); |
|
293 | 296 | } |
|
294 | 297 | |
|
295 | 298 | void QXYModelMapperPrivate::modelColumnsAdded(QModelIndex parent, int start, int end) |
|
296 | 299 | { |
|
297 | 300 | Q_UNUSED(parent); |
|
298 | 301 | if (m_modelSignalsBlock) |
|
299 | 302 | return; |
|
300 | 303 | |
|
301 | 304 | blockSeriesSignals(); |
|
302 | 305 | if (m_orientation == Qt::Horizontal) |
|
303 | 306 | insertData(start, end); |
|
304 | 307 | else if (start <= m_xSection || start <= m_ySection) // if the changes affect the map - reinitialize the xy |
|
305 | 308 | initializeXYFromModel(); |
|
306 | 309 | blockSeriesSignals(false); |
|
307 | 310 | } |
|
308 | 311 | |
|
309 | 312 | void QXYModelMapperPrivate::modelColumnsRemoved(QModelIndex parent, int start, int end) |
|
310 | 313 | { |
|
311 | 314 | Q_UNUSED(parent); |
|
312 | 315 | if (m_modelSignalsBlock) |
|
313 | 316 | return; |
|
314 | 317 | |
|
315 | 318 | blockSeriesSignals(); |
|
316 | 319 | if (m_orientation == Qt::Horizontal) |
|
317 | 320 | removeData(start, end); |
|
318 | 321 | else if (start <= m_xSection || start <= m_ySection) // if the changes affect the map - reinitialize the xy |
|
319 | 322 | initializeXYFromModel(); |
|
320 | 323 | blockSeriesSignals(false); |
|
321 | 324 | } |
|
322 | 325 | |
|
323 | 326 | void QXYModelMapperPrivate::insertData(int start, int end) |
|
324 | 327 | { |
|
325 | 328 | if (m_model == 0 || m_series == 0) |
|
326 | 329 | return; |
|
327 | 330 | |
|
328 | 331 | if (m_count != -1 && start >= m_first + m_count) { |
|
329 | 332 | return; |
|
330 | 333 | } else { |
|
331 | 334 | int addedCount = end - start + 1; |
|
332 | 335 | if (m_count != -1 && addedCount > m_count) |
|
333 | 336 | addedCount = m_count; |
|
334 | 337 | int first = qMax(start, m_first); |
|
335 | 338 | int last = qMin(first + addedCount - 1, m_orientation == Qt::Vertical ? m_model->rowCount() - 1 : m_model->columnCount() - 1); |
|
336 | 339 | for (int i = first; i <= last; i++) { |
|
337 | 340 | QPointF point; |
|
338 | 341 | point.setX(m_model->data(xModelIndex(i - m_first), Qt::DisplayRole).toDouble()); |
|
339 | 342 | point.setY(m_model->data(yModelIndex(i - m_first), Qt::DisplayRole).toDouble()); |
|
340 | 343 | m_series->insert(i - m_first, point); |
|
341 | 344 | } |
|
342 | 345 | |
|
343 | 346 | // remove excess of slices (abouve m_count) |
|
344 | 347 | if (m_count != -1 && m_series->points().size() > m_count) |
|
345 | 348 | for (int i = m_series->points().size() - 1; i >= m_count; i--) { |
|
346 | 349 | m_series->remove(m_series->points().at(i)); |
|
347 | 350 | } |
|
348 | 351 | } |
|
349 | 352 | } |
|
350 | 353 | |
|
351 | 354 | void QXYModelMapperPrivate::removeData(int start, int end) |
|
352 | 355 | { |
|
353 | 356 | if (m_model == 0 || m_series == 0) |
|
354 | 357 | return; |
|
355 | 358 | |
|
356 | 359 | int removedCount = end - start + 1; |
|
357 | 360 | if (m_count != -1 && start >= m_first + m_count) { |
|
358 | 361 | return; |
|
359 | 362 | } else { |
|
360 | 363 | int toRemove = qMin(m_series->points().size(), removedCount); // first find how many items can actually be removed |
|
361 | 364 | int first = qMax(start, m_first); // get the index of the first item that will be removed. |
|
362 | 365 | int last = qMin(first + toRemove - 1, m_series->points().size() + m_first - 1); // get the index of the last item that will be removed. |
|
363 | 366 | for (int i = last; i >= first; i--) { |
|
364 | 367 | m_series->remove(m_series->points().at(i - m_first)); |
|
365 | 368 | } |
|
366 | 369 | |
|
367 | 370 | if (m_count != -1) { |
|
368 | 371 | int itemsAvailable; // check how many are available to be added |
|
369 | 372 | if (m_orientation == Qt::Vertical) |
|
370 | 373 | itemsAvailable = m_model->rowCount() - m_first - m_series->points().size(); |
|
371 | 374 | else |
|
372 | 375 | itemsAvailable = m_model->columnCount() - m_first - m_series->points().size(); |
|
373 | 376 | int toBeAdded = qMin(itemsAvailable, m_count - m_series->points().size()); // add not more items than there is space left to be filled. |
|
374 | 377 | int currentSize = m_series->points().size(); |
|
375 | 378 | if (toBeAdded > 0) |
|
376 | 379 | for (int i = m_series->points().size(); i < currentSize + toBeAdded; i++) { |
|
377 | 380 | QPointF point; |
|
378 | 381 | point.setX(m_model->data(xModelIndex(i), Qt::DisplayRole).toDouble()); |
|
379 | 382 | point.setY(m_model->data(yModelIndex(i), Qt::DisplayRole).toDouble()); |
|
380 | 383 | m_series->insert(i, point); |
|
381 | 384 | } |
|
382 | 385 | } |
|
383 | 386 | } |
|
384 | 387 | } |
|
385 | 388 | |
|
386 | 389 | void QXYModelMapperPrivate::initializeXYFromModel() |
|
387 | 390 | { |
|
388 | 391 | if (m_model == 0 || m_series == 0) |
|
389 | 392 | return; |
|
390 | 393 | |
|
391 | 394 | blockSeriesSignals(); |
|
392 | 395 | // clear current content |
|
393 | 396 | m_series->clear(); |
|
394 | 397 | |
|
395 | 398 | // create the initial slices set |
|
396 | 399 | int pointPos = 0; |
|
397 | 400 | QModelIndex xIndex = xModelIndex(pointPos); |
|
398 | 401 | QModelIndex yIndex = yModelIndex(pointPos); |
|
399 | 402 | while (xIndex.isValid() && yIndex.isValid()) { |
|
400 | 403 | QPointF point; |
|
401 | 404 | point.setX(m_model->data(xIndex, Qt::DisplayRole).toDouble()); |
|
402 | 405 | point.setY(m_model->data(yIndex, Qt::DisplayRole).toDouble()); |
|
403 | 406 | m_series->append(point); |
|
404 | 407 | pointPos++; |
|
405 | 408 | xIndex = xModelIndex(pointPos); |
|
406 | 409 | yIndex = yModelIndex(pointPos); |
|
407 | 410 | } |
|
408 | 411 | blockSeriesSignals(false); |
|
409 | 412 | } |
|
410 | 413 | |
|
411 | 414 | #include "moc_qxymodelmapper.cpp" |
|
412 | 415 | #include "moc_qxymodelmapper_p.cpp" |
|
413 | 416 | |
|
414 | 417 | QTCOMMERCIALCHART_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now