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