##// END OF EJS Templates
Fix test application to have valid window size
Michal Klocek -
r147:ede69107bdd8
parent child
Show More
@@ -1,248 +1,249
1 #include "qchart.h"
1 #include "qchart.h"
2 #include "qchartaxis.h"
2 #include "qchartaxis.h"
3 #include "chartpresenter_p.h"
3 #include "chartpresenter_p.h"
4 #include "chartdataset_p.h"
4 #include "chartdataset_p.h"
5 #include "charttheme_p.h"
5 #include "charttheme_p.h"
6 //series
6 //series
7 #include "barchartseries.h"
7 #include "barchartseries.h"
8 #include "stackedbarchartseries.h"
8 #include "stackedbarchartseries.h"
9 #include "percentbarchartseries.h"
9 #include "percentbarchartseries.h"
10 #include "qlinechartseries.h"
10 #include "qlinechartseries.h"
11 #include "qpieseries.h"
11 #include "qpieseries.h"
12 //items
12 //items
13 #include "axisitem_p.h"
13 #include "axisitem_p.h"
14 #include "bargroup.h"
14 #include "bargroup.h"
15 #include "stackedbargroup.h"
15 #include "stackedbargroup.h"
16 #include "linechartitem_p.h"
16 #include "linechartitem_p.h"
17 #include "percentbargroup.h"
17 #include "percentbargroup.h"
18 #include "linechartanimationitem_p.h"
18 #include "linechartanimationitem_p.h"
19 #include "piepresenter.h"
19 #include "piepresenter.h"
20
20
21 QTCOMMERCIALCHART_BEGIN_NAMESPACE
21 QTCOMMERCIALCHART_BEGIN_NAMESPACE
22
22
23 ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(chart),
23 ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(chart),
24 m_chart(chart),
24 m_chart(chart),
25 m_dataset(dataset),
25 m_dataset(dataset),
26 m_chartTheme(0),
26 m_chartTheme(0),
27 m_domainIndex(0),
27 m_domainIndex(0),
28 m_marginSize(0),
28 m_marginSize(0),
29 m_rect(QRectF(QPoint(0,0),m_chart->size()))
29 m_rect(QRectF(QPoint(0,0),m_chart->size()))
30 {
30 {
31 setTheme(QChart::ChartThemeDefault);
31 setTheme(QChart::ChartThemeDefault);
32 createConnections();
32 createConnections();
33 createDeafultAxis();
33 createDeafultAxis();
34 }
34 }
35
35
36 ChartPresenter::~ChartPresenter()
36 ChartPresenter::~ChartPresenter()
37 {
37 {
38 }
38 }
39
39
40 void ChartPresenter::createDeafultAxis()
40 void ChartPresenter::createDeafultAxis()
41 {
41 {
42 //default axis
42 //default axis
43 QChartAxis* axisX = new QChartAxis(this);
43 QChartAxis* axisX = new QChartAxis(this);
44 QChartAxis* axisY = new QChartAxis(this);
44 QChartAxis* axisY = new QChartAxis(this);
45
45
46 m_axis << new AxisItem(axisX,AxisItem::X_AXIS,m_chart);
46 m_axis << new AxisItem(axisX,AxisItem::X_AXIS,m_chart);
47 m_axis << new AxisItem(axisY,AxisItem::Y_AXIS,m_chart);
47 m_axis << new AxisItem(axisY,AxisItem::Y_AXIS,m_chart);
48
48
49 foreach(AxisItem* item, m_axis) {
49 foreach(AxisItem* item, m_axis) {
50 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
50 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
51 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&)));
51 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&)));
52 }
52 }
53 }
53 }
54
54
55 void ChartPresenter::createConnections()
55 void ChartPresenter::createConnections()
56 {
56 {
57 QObject::connect(m_chart,SIGNAL(geometryChanged()),this,SLOT(handleGeometryChanged()));
57 QObject::connect(m_chart,SIGNAL(geometryChanged()),this,SLOT(handleGeometryChanged()));
58 QObject::connect(m_dataset,SIGNAL(seriesAdded(QChartSeries*)),this,SLOT(handleSeriesAdded(QChartSeries*)));
58 QObject::connect(m_dataset,SIGNAL(seriesAdded(QChartSeries*)),this,SLOT(handleSeriesAdded(QChartSeries*)));
59 }
59 }
60
60
61 void ChartPresenter::handleGeometryChanged()
61 void ChartPresenter::handleGeometryChanged()
62 {
62 {
63 m_rect = QRectF(QPoint(0,0),m_chart->size());
63 m_rect = QRectF(QPoint(0,0),m_chart->size());
64 m_rect.adjust(m_marginSize,m_marginSize, -m_marginSize, -m_marginSize);
64 m_rect.adjust(m_marginSize,m_marginSize, -m_marginSize, -m_marginSize);
65 Q_ASSERT(m_rect.isValid());
65 emit geometryChanged(m_rect);
66 emit geometryChanged(m_rect);
66 }
67 }
67
68
68 int ChartPresenter::margin() const
69 int ChartPresenter::margin() const
69 {
70 {
70 return m_marginSize;
71 return m_marginSize;
71 }
72 }
72
73
73 void ChartPresenter::setMargin(int margin)
74 void ChartPresenter::setMargin(int margin)
74 {
75 {
75 m_marginSize = margin;
76 m_marginSize = margin;
76 }
77 }
77
78
78 void ChartPresenter::handleSeriesAdded(QChartSeries* series)
79 void ChartPresenter::handleSeriesAdded(QChartSeries* series)
79 {
80 {
80 switch(series->type())
81 switch(series->type())
81 {
82 {
82 case QChartSeries::SeriesTypeLine: {
83 case QChartSeries::SeriesTypeLine: {
83 QLineChartSeries* lineSeries = static_cast<QLineChartSeries*>(series);
84 QLineChartSeries* lineSeries = static_cast<QLineChartSeries*>(series);
84 LineChartItem* item = new LineChartAnimationItem(this,lineSeries,m_chart);
85 LineChartItem* item = new LineChartAnimationItem(this,lineSeries,m_chart);
85 m_chartTheme->decorate(item,lineSeries,m_chartItems.count());
86 m_chartTheme->decorate(item,lineSeries,m_chartItems.count());
86 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
87 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
87 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&)));
88 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&)));
88 QObject::connect(lineSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
89 QObject::connect(lineSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
89 m_chartItems.insert(series,item);
90 m_chartItems.insert(series,item);
90 break;
91 break;
91 }
92 }
92
93
93 case QChartSeries::SeriesTypeBar: {
94 case QChartSeries::SeriesTypeBar: {
94 BarChartSeries* barSeries = static_cast<BarChartSeries*>(series);
95 BarChartSeries* barSeries = static_cast<BarChartSeries*>(series);
95 BarGroup* item = new BarGroup(*barSeries,m_chart);
96 BarGroup* item = new BarGroup(*barSeries,m_chart);
96 m_chartTheme->decorate(item,barSeries,m_chartItems.count());
97 m_chartTheme->decorate(item,barSeries,m_chartItems.count());
97 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
98 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
98 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&)));
99 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&)));
99 QObject::connect(barSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
100 QObject::connect(barSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
100 m_chartItems.insert(series,item);
101 m_chartItems.insert(series,item);
101 // m_axisXItem->setVisible(false);
102 // m_axisXItem->setVisible(false);
102 break;
103 break;
103 }
104 }
104
105
105 case QChartSeries::SeriesTypeStackedBar: {
106 case QChartSeries::SeriesTypeStackedBar: {
106
107
107 StackedBarChartSeries* stackedBarSeries = static_cast<StackedBarChartSeries*>(series);
108 StackedBarChartSeries* stackedBarSeries = static_cast<StackedBarChartSeries*>(series);
108 StackedBarGroup* item = new StackedBarGroup(*stackedBarSeries,m_chart);
109 StackedBarGroup* item = new StackedBarGroup(*stackedBarSeries,m_chart);
109 m_chartTheme->decorate(item,stackedBarSeries,m_chartItems.count());
110 m_chartTheme->decorate(item,stackedBarSeries,m_chartItems.count());
110 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
111 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
111 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&)));
112 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&)));
112 QObject::connect(stackedBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
113 QObject::connect(stackedBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
113 m_chartItems.insert(series,item);
114 m_chartItems.insert(series,item);
114 break;
115 break;
115 }
116 }
116
117
117 case QChartSeries::SeriesTypePercentBar: {
118 case QChartSeries::SeriesTypePercentBar: {
118
119
119 PercentBarChartSeries* percentBarSeries = static_cast<PercentBarChartSeries*>(series);
120 PercentBarChartSeries* percentBarSeries = static_cast<PercentBarChartSeries*>(series);
120 PercentBarGroup* item = new PercentBarGroup(*percentBarSeries,m_chart);
121 PercentBarGroup* item = new PercentBarGroup(*percentBarSeries,m_chart);
121 m_chartTheme->decorate(item,percentBarSeries ,m_chartItems.count());
122 m_chartTheme->decorate(item,percentBarSeries ,m_chartItems.count());
122 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
123 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
123 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&)));
124 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&)));
124 QObject::connect(percentBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
125 QObject::connect(percentBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
125 m_chartItems.insert(series,item);
126 m_chartItems.insert(series,item);
126 break;
127 break;
127 }
128 }
128 /*
129 /*
129 case QChartSeries::SeriesTypeScatter: {
130 case QChartSeries::SeriesTypeScatter: {
130 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
131 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
131 scatterSeries->d->m_theme = m_chartTheme->themeForSeries();
132 scatterSeries->d->m_theme = m_chartTheme->themeForSeries();
132 scatterSeries->d->setParentItem(this);
133 scatterSeries->d->setParentItem(this);
133 scatterSeries->d->m_boundingRect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
134 scatterSeries->d->m_boundingRect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
134 m_chartItems << scatterSeries->d;
135 m_chartItems << scatterSeries->d;
135 m_chartTheme->addObserver(scatterSeries->d);
136 m_chartTheme->addObserver(scatterSeries->d);
136
137
137 foreach (qreal x, scatterSeries->d->m_x) {
138 foreach (qreal x, scatterSeries->d->m_x) {
138 domain.m_minX = qMin(domain.m_minX, x);
139 domain.m_minX = qMin(domain.m_minX, x);
139 domain.m_maxX = qMax(domain.m_maxX, x);
140 domain.m_maxX = qMax(domain.m_maxX, x);
140 }
141 }
141 foreach (qreal y, scatterSeries->d->m_y) {
142 foreach (qreal y, scatterSeries->d->m_y) {
142 domain.m_minY = qMin(domain.m_minY, y);
143 domain.m_minY = qMin(domain.m_minY, y);
143 domain.m_maxY = qMax(domain.m_maxY, y);
144 domain.m_maxY = qMax(domain.m_maxY, y);
144 }
145 }
145
146
146 break;
147 break;
147 }
148 }
148 */
149 */
149
150
150 case QChartSeries::SeriesTypePie: {
151 case QChartSeries::SeriesTypePie: {
151 QPieSeries *pieSeries = qobject_cast<QPieSeries *>(series);
152 QPieSeries *pieSeries = qobject_cast<QPieSeries *>(series);
152 PiePresenter* pie = new PiePresenter(m_chart, pieSeries);
153 PiePresenter* pie = new PiePresenter(m_chart, pieSeries);
153 pieSeries->m_piePresenter = pie; // TODO: remove this pointer passing use signals&slots
154 pieSeries->m_piePresenter = pie; // TODO: remove this pointer passing use signals&slots
154 QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)), pie, SLOT(handleGeometryChanged(const QRectF&)));
155 QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)), pie, SLOT(handleGeometryChanged(const QRectF&)));
155 QObject::connect(m_dataset, SIGNAL(domainChanged(const Domain&)), pie, SLOT(handleDomainChanged(const Domain&)));
156 QObject::connect(m_dataset, SIGNAL(domainChanged(const Domain&)), pie, SLOT(handleDomainChanged(const Domain&)));
156 m_chartItems.insert(series, pie);
157 m_chartItems.insert(series, pie);
157 break;
158 break;
158 }
159 }
159
160
160 default: {
161 default: {
161 qDebug()<< "Series type" << series->type() << "not implemented.";
162 qDebug()<< "Series type" << series->type() << "not implemented.";
162 break;
163 break;
163 }
164 }
164 }
165 }
165 }
166 }
166
167
167 void ChartPresenter::handleSeriesChanged(QChartSeries* series)
168 void ChartPresenter::handleSeriesChanged(QChartSeries* series)
168 {
169 {
169 //TODO:
170 //TODO:
170 }
171 }
171
172
172 void ChartPresenter::zoomInToRect(const QRectF& rect)
173 void ChartPresenter::zoomInToRect(const QRectF& rect)
173 {
174 {
174 if(!rect.isValid()) return;
175 if(!rect.isValid()) return;
175 QRectF r = rect.normalized();
176 QRectF r = rect.normalized();
176 r.translate(-m_marginSize, -m_marginSize);
177 r.translate(-m_marginSize, -m_marginSize);
177 Domain domain (m_dataset->domain().subDomain(rect,m_rect.width(),m_rect.height()));
178 Domain domain (m_dataset->domain().subDomain(rect,m_rect.width(),m_rect.height()));
178 m_dataset->addDomain(domain);
179 m_dataset->addDomain(domain);
179 }
180 }
180
181
181 void ChartPresenter::zoomIn()
182 void ChartPresenter::zoomIn()
182 {
183 {
183 if (!m_dataset->nextDomain()) {
184 if (!m_dataset->nextDomain()) {
184 QRectF rect = m_rect;
185 QRectF rect = m_rect;
185 rect.setWidth(rect.width()/2);
186 rect.setWidth(rect.width()/2);
186 rect.setHeight(rect.height()/2);
187 rect.setHeight(rect.height()/2);
187 rect.moveCenter(m_rect.center());
188 rect.moveCenter(m_rect.center());
188 Domain domain (m_dataset->domain().subDomain(rect,m_rect.width(),m_rect.height()));
189 Domain domain (m_dataset->domain().subDomain(rect,m_rect.width(),m_rect.height()));
189 m_dataset->addDomain(domain);
190 m_dataset->addDomain(domain);
190 }
191 }
191 }
192 }
192
193
193 void ChartPresenter::zoomOut()
194 void ChartPresenter::zoomOut()
194 {
195 {
195 m_dataset->previousDomain();
196 m_dataset->previousDomain();
196 }
197 }
197
198
198 void ChartPresenter::zoomReset()
199 void ChartPresenter::zoomReset()
199 {
200 {
200 m_dataset->clearDomains();
201 m_dataset->clearDomains();
201 }
202 }
202
203
203 void ChartPresenter::setTheme(QChart::ChartThemeId theme)
204 void ChartPresenter::setTheme(QChart::ChartThemeId theme)
204 {
205 {
205 delete m_chartTheme;
206 delete m_chartTheme;
206
207
207 m_chartTheme = ChartTheme::createTheme(theme);
208 m_chartTheme = ChartTheme::createTheme(theme);
208
209
209 m_chartTheme->decorate(m_chart);
210 m_chartTheme->decorate(m_chart);
210 QMapIterator<QChartSeries*,ChartItem*> i(m_chartItems);
211 QMapIterator<QChartSeries*,ChartItem*> i(m_chartItems);
211
212
212 int index=0;
213 int index=0;
213 while (i.hasNext()) {
214 while (i.hasNext()) {
214 i.next();
215 i.next();
215 index++;
216 index++;
216 m_chartTheme->decorate(i.value(),i.key(),index);
217 m_chartTheme->decorate(i.value(),i.key(),index);
217 }
218 }
218 }
219 }
219
220
220
221
221 QChart::ChartThemeId ChartPresenter::theme()
222 QChart::ChartThemeId ChartPresenter::theme()
222 {
223 {
223 return (QChart::ChartThemeId) 0;
224 return (QChart::ChartThemeId) 0;
224 }
225 }
225
226
226 /*
227 /*
227 void ChartPresenter::setAxisX(const QChartAxis& axis)
228 void ChartPresenter::setAxisX(const QChartAxis& axis)
228 {
229 {
229 setAxis(m_axisXItem,axis);
230 setAxis(m_axisXItem,axis);
230 }
231 }
231 void ChartPresenter::setAxisY(const QChartAxis& axis)
232 void ChartPresenter::setAxisY(const QChartAxis& axis)
232 {
233 {
233 setAxis(m_axisYItem.at(0),axis);
234 setAxis(m_axisYItem.at(0),axis);
234 }
235 }
235
236
236 void ChartPresenter::setAxisY(const QList<QChartAxis>& axis)
237 void ChartPresenter::setAxisY(const QList<QChartAxis>& axis)
237 {
238 {
238 //TODO not implemented
239 //TODO not implemented
239 }
240 }
240
241
241 void ChartPresenter::setAxis(AxisItem *item, const QChartAxis& axis)
242 void ChartPresenter::setAxis(AxisItem *item, const QChartAxis& axis)
242 {
243 {
243 item->setVisible(axis.isAxisVisible());
244 item->setVisible(axis.isAxisVisible());
244 }
245 }
245 */
246 */
246 #include "moc_chartpresenter_p.cpp"
247 #include "moc_chartpresenter_p.cpp"
247
248
248 QTCOMMERCIALCHART_END_NAMESPACE
249 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,17 +1,18
1 #include <QtCore/QtGlobal>
1 #include <QtCore/QtGlobal>
2 #if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0))
2 #if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0))
3 #include <QApplication>
3 #include <QApplication>
4 #else
4 #else
5 #include <QtWidgets/QApplication>
5 #include <QtWidgets/QApplication>
6 #endif
6 #endif
7 #include "mainwidget.h"
7 #include "mainwidget.h"
8
8
9 int main(int argc, char *argv[])
9 int main(int argc, char *argv[])
10 {
10 {
11 QApplication a(argc, argv);
11 QApplication a(argc, argv);
12
12
13 MainWidget w;
13 MainWidget w;
14 w.resize(1000,600);
14 w.show();
15 w.show();
15
16
16 return a.exec();
17 return a.exec();
17 }
18 }
General Comments 0
You need to be logged in to leave comments. Login now