##// END OF EJS Templates
Changes background item...
Michal Klocek -
r639:4f21953d9289
parent child
Show More
@@ -1,401 +1,390
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 #include "chartanimator_p.h"
7 7 //series
8 8 #include "qbarseries.h"
9 9 #include "qstackedbarseries.h"
10 10 #include "qpercentbarseries.h"
11 11 #include "qlineseries.h"
12 12 #include "qareaseries.h"
13 13 #include "qpieseries.h"
14 14 #include "qscatterseries.h"
15 15 #include "qsplineseries.h"
16 16 //items
17 17 #include "axisitem_p.h"
18 18 #include "areachartitem_p.h"
19 19 #include "barpresenter_p.h"
20 20 #include "stackedbarpresenter_p.h"
21 21 #include "percentbarpresenter_p.h"
22 22 #include "linechartitem_p.h"
23 23 #include "piechartitem_p.h"
24 24 #include "scatterchartitem_p.h"
25 25 #include "splinechartitem_p.h"
26 26
27 27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 28
29 29 ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(chart),
30 30 m_chart(chart),
31 31 m_animator(0),
32 32 m_dataset(dataset),
33 33 m_chartTheme(0),
34 34 m_zoomIndex(0),
35 m_marginSize(0),
36 35 m_rect(QRectF(QPoint(0,0),m_chart->size())),
37 36 m_options(QChart::NoAnimation)
38 37 {
39 38 createConnections();
40 39 setChartTheme(QChart::ChartThemeDefault);
41 40 }
42 41
43 42 ChartPresenter::~ChartPresenter()
44 43 {
45 44 delete m_chartTheme;
46 45 }
47 46
48 47 void ChartPresenter::createConnections()
49 48 {
50 49 QObject::connect(m_chart,SIGNAL(geometryChanged()),this,SLOT(handleGeometryChanged()));
51 50 QObject::connect(m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),this,SLOT(handleSeriesAdded(QSeries*,Domain*)));
52 51 QObject::connect(m_dataset,SIGNAL(seriesRemoved(QSeries*)),this,SLOT(handleSeriesRemoved(QSeries*)));
53 52 QObject::connect(m_dataset,SIGNAL(axisAdded(QChartAxis*,Domain*)),this,SLOT(handleAxisAdded(QChartAxis*,Domain*)));
54 53 QObject::connect(m_dataset,SIGNAL(axisRemoved(QChartAxis*)),this,SLOT(handleAxisRemoved(QChartAxis*)));
55 54 }
56 55
57 56
58 57 QRectF ChartPresenter::geometry() const
59 58 {
60 59 return m_rect;
61 60 }
62 61
63 62 void ChartPresenter::handleGeometryChanged()
64 63 {
65 64 QRectF rect(QPoint(0,0),m_chart->size());
66 rect.adjust(m_marginSize,m_marginSize, -m_marginSize, -m_marginSize);
65 rect.adjust(m_chart->padding(),m_chart->padding(), -m_chart->padding(), -m_chart->padding());
67 66
68 67 //rewrite zoom stack
69 68 for(int i=0;i<m_zoomStack.count();i++){
70 69 QRectF r = m_zoomStack[i];
71 70 qreal w = rect.width()/m_rect.width();
72 71 qreal h = rect.height()/m_rect.height();
73 72 QPointF tl = r.topLeft();
74 73 tl.setX(tl.x()*w);
75 74 tl.setY(tl.y()*h);
76 75 QPointF br = r.bottomRight();
77 76 br.setX(br.x()*w);
78 77 br.setY(br.y()*h);
79 78 r.setTopLeft(tl);
80 79 r.setBottomRight(br);
81 80 m_zoomStack[i]=r;
82 81 }
83 82
84 83 m_rect = rect;
85 84 Q_ASSERT(m_rect.isValid());
86 85 emit geometryChanged(m_rect);
87 86 }
88 87
89 int ChartPresenter::margin() const
90 {
91 return m_marginSize;
92 }
93
94 void ChartPresenter::setMargin(int margin)
95 {
96 m_marginSize = margin;
97 }
98
99 88 void ChartPresenter::handleAxisAdded(QChartAxis* axis,Domain* domain)
100 89 {
101 90 AxisItem* item = new AxisItem(axis,this,axis==m_dataset->axisX()?AxisItem::X_AXIS : AxisItem::Y_AXIS,m_chart);
102 91
103 92 if(m_options.testFlag(QChart::GridAxisAnimations)){
104 93 m_animator->addAnimation(item);
105 94 }
106 95
107 96 if(axis==m_dataset->axisX()){
108 97 m_chartTheme->decorate(axis,true);
109 98 QObject::connect(domain,SIGNAL(rangeXChanged(qreal,qreal,int)),item,SLOT(handleRangeChanged(qreal,qreal,int)));
110 99 //initialize
111 100 item->handleRangeChanged(domain->minX(),domain->maxX(),domain->tickXCount());
112 101
113 102 }
114 103 else{
115 104 m_chartTheme->decorate(axis,false);
116 105 QObject::connect(domain,SIGNAL(rangeYChanged(qreal,qreal,int)),item,SLOT(handleRangeChanged(qreal,qreal,int)));
117 106 //initialize
118 107 item->handleRangeChanged(domain->minY(),domain->maxY(),domain->tickYCount());
119 108 }
120 109
121 110 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
122 111 //initialize
123 112 item->handleGeometryChanged(m_rect);
124 113 m_axisItems.insert(axis, item);
125 114 }
126 115
127 116 void ChartPresenter::handleAxisRemoved(QChartAxis* axis)
128 117 {
129 118 AxisItem* item = m_axisItems.take(axis);
130 119 Q_ASSERT(item);
131 120 if(m_animator) m_animator->removeAnimation(item);
132 121 delete item;
133 122 }
134 123
135 124
136 125 void ChartPresenter::handleSeriesAdded(QSeries* series,Domain* domain)
137 126 {
138 127 ChartItem *item = 0 ;
139 128
140 129 switch(series->type())
141 130 {
142 131 case QSeries::SeriesTypeLine: {
143 132
144 133 QLineSeries* lineSeries = static_cast<QLineSeries*>(series);
145 134 LineChartItem* line = new LineChartItem(lineSeries,m_chart);
146 135 if(m_options.testFlag(QChart::SeriesAnimations)) {
147 136 m_animator->addAnimation(line);
148 137 }
149 138 m_chartTheme->decorate(lineSeries, m_dataset->seriesIndex(series));
150 139 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),line,SLOT(handleGeometryChanged(const QRectF&)));
151 140 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),line,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
152 141 item = line;
153 142 break;
154 143 }
155 144
156 145 case QSeries::SeriesTypeArea: {
157 146
158 147 QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series);
159 148 AreaChartItem* area = new AreaChartItem(areaSeries,m_chart);
160 149 if(m_options.testFlag(QChart::SeriesAnimations)) {
161 150 m_animator->addAnimation(area->upperLineItem());
162 151 if(areaSeries->lowerSeries()) m_animator->addAnimation(area->lowerLineItem());
163 152 }
164 153 m_chartTheme->decorate(areaSeries, m_dataset->seriesIndex(series));
165 154 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),area,SLOT(handleGeometryChanged(const QRectF&)));
166 155 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),area,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
167 156 item=area;
168 157 break;
169 158 }
170 159
171 160 case QSeries::SeriesTypeBar: {
172 161 QBarSeries* barSeries = static_cast<QBarSeries*>(series);
173 162 BarPresenter* bar = new BarPresenter(barSeries,m_chart);
174 163 if(m_options.testFlag(QChart::SeriesAnimations)) {
175 164 // m_animator->addAnimation(bar);
176 165 }
177 166 m_chartTheme->decorate(barSeries, m_dataset->seriesIndex(barSeries));
178 167 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),bar,SLOT(handleGeometryChanged(const QRectF&)));
179 168 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),bar,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
180 169 item=bar;
181 170 break;
182 171 }
183 172
184 173 case QSeries::SeriesTypeStackedBar: {
185 174 QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series);
186 175 StackedBarPresenter* bar = new StackedBarPresenter(stackedBarSeries,m_chart);
187 176 if(m_options.testFlag(QChart::SeriesAnimations)) {
188 177 // m_animator->addAnimation(bar);
189 178 }
190 179 m_chartTheme->decorate(stackedBarSeries, m_dataset->seriesIndex(stackedBarSeries));
191 180 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),bar,SLOT(handleGeometryChanged(const QRectF&)));
192 181 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),bar,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
193 182 item=bar;
194 183 break;
195 184 }
196 185
197 186 case QSeries::SeriesTypePercentBar: {
198 187 QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series);
199 188 PercentBarPresenter* bar = new PercentBarPresenter(percentBarSeries,m_chart);
200 189 if(m_options.testFlag(QChart::SeriesAnimations)) {
201 190 // m_animator->addAnimation(bar);
202 191 }
203 192 m_chartTheme->decorate(percentBarSeries, m_dataset->seriesIndex(percentBarSeries));
204 193 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),bar,SLOT(handleGeometryChanged(const QRectF&)));
205 194 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),bar,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
206 195 item=bar;
207 196 break;
208 197 }
209 198
210 199 case QSeries::SeriesTypeScatter: {
211 200 QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series);
212 201 ScatterChartItem *scatter = new ScatterChartItem(scatterSeries, m_chart);
213 202 if(m_options.testFlag(QChart::SeriesAnimations)) {
214 203 m_animator->addAnimation(scatter);
215 204 }
216 205 m_chartTheme->decorate(scatterSeries, m_dataset->seriesIndex(series));
217 206 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),scatter,SLOT(handleGeometryChanged(const QRectF&)));
218 207 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),scatter,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
219 208 item = scatter;
220 209 break;
221 210 }
222 211
223 212 case QSeries::SeriesTypePie: {
224 213 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
225 214 PieChartItem* pie = new PieChartItem(m_chart, pieSeries);
226 215 if(m_options.testFlag(QChart::SeriesAnimations)) {
227 216 m_animator->addAnimation(pie);
228 217 }
229 218 m_chartTheme->decorate(pieSeries, m_dataset->seriesIndex(series));
230 219 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),pie,SLOT(handleGeometryChanged(const QRectF&)));
231 220 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),pie,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
232 221 // Hide all from background when there is only piechart
233 222 // TODO: refactor this ugly code... should be one setting for this
234 223 if (m_chartItems.count() == 0) {
235 224 m_chart->axisX()->hide();
236 225 m_chart->axisY()->hide();
237 226 m_chart->setChartBackgroundBrush(Qt::transparent);
238 227 }
239 228 item=pie;
240 229 break;
241 230 }
242 231
243 232 case QSeries::SeriesTypeSpline: {
244 233 QSplineSeries* splineSeries = static_cast<QSplineSeries*>(series);
245 234 SplineChartItem* spline = new SplineChartItem(splineSeries, m_chart);
246 235 if(m_options.testFlag(QChart::SeriesAnimations)) {
247 236 m_animator->addAnimation(spline);
248 237 }
249 238 m_chartTheme->decorate(splineSeries, m_dataset->seriesIndex(series));
250 239 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),spline,SLOT(handleGeometryChanged(const QRectF&)));
251 240 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),spline,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
252 241 item=spline;
253 242 break;
254 243 }
255 244 default: {
256 245 qDebug()<< "Series type" << series->type() << "not implemented.";
257 246 break;
258 247 }
259 248 }
260 249
261 250 //initialize
262 251 item->handleDomainChanged(domain->minX(),domain->maxX(),domain->minY(),domain->maxY());
263 252 if(m_rect.isValid()) item->handleGeometryChanged(m_rect);
264 253 m_chartItems.insert(series,item);
265 254 zoomReset();
266 255 }
267 256
268 257 void ChartPresenter::handleSeriesRemoved(QSeries* series)
269 258 {
270 259 ChartItem* item = m_chartItems.take(series);
271 260 Q_ASSERT(item);
272 261 if(m_animator) {
273 262 //small hack to handle area animations
274 263 if(series->type()==QSeries::SeriesTypeArea){
275 264 QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series);
276 265 AreaChartItem* area = static_cast<AreaChartItem*>(item);
277 266 m_animator->removeAnimation(area->upperLineItem());
278 267 if(areaSeries->lowerSeries()) m_animator->addAnimation(area->lowerLineItem());
279 268 }else
280 269 m_animator->removeAnimation(item);
281 270 }
282 271 delete item;
283 272 }
284 273
285 274 void ChartPresenter::setChartTheme(QChart::ChartTheme theme)
286 275 {
287 276 if(m_chartTheme && m_chartTheme->id() == theme) return;
288 277 delete m_chartTheme;
289 278 m_chartTheme = ChartTheme::createTheme(theme);
290 279 m_chartTheme->decorate(m_chart);
291 280 m_chartTheme->decorate(m_chart->legend());
292 281 resetAllElements();
293 282 }
294 283
295 284 QChart::ChartTheme ChartPresenter::chartTheme()
296 285 {
297 286 return m_chartTheme->id();
298 287 }
299 288
300 289 void ChartPresenter::setAnimationOptions(QChart::AnimationOptions options)
301 290 {
302 291 if(m_options!=options) {
303 292
304 293 m_options=options;
305 294
306 295 if(m_options!=QChart::NoAnimation && !m_animator) {
307 296 m_animator= new ChartAnimator(this);
308 297
309 298 }
310 299 resetAllElements();
311 300 }
312 301
313 302 }
314 303
315 304 void ChartPresenter::resetAllElements()
316 305 {
317 306 QList<QChartAxis*> axisList = m_axisItems.uniqueKeys();
318 307 QList<QSeries*> seriesList = m_chartItems.uniqueKeys();
319 308
320 309 foreach(QChartAxis* axis, axisList) {
321 310 handleAxisRemoved(axis);
322 311 handleAxisAdded(axis,m_dataset->domain(axis));
323 312 }
324 313 foreach(QSeries* series, seriesList) {
325 314 handleSeriesRemoved(series);
326 315 handleSeriesAdded(series,m_dataset->domain(series));
327 316 }
328 317 }
329 318
330 319 void ChartPresenter::zoomIn()
331 320 {
332 321 QRectF rect = geometry();
333 322 rect.setWidth(rect.width()/2);
334 323 rect.setHeight(rect.height()/2);
335 324 rect.moveCenter(geometry().center());
336 325 zoomIn(rect);
337 326 }
338 327
339 328 void ChartPresenter::zoomIn(const QRectF& rect)
340 329 {
341 330 QRectF r = rect.normalized();
342 r.translate(-m_marginSize, -m_marginSize);
331 r.translate(-m_chart->padding(), -m_chart->padding());
343 332 if(m_animator) {
344 333
345 334 QPointF point(r.center().x()/geometry().width(),r.center().y()/geometry().height());
346 335 m_animator->setState(ChartAnimator::ZoomInState,point);
347 336 }
348 337 m_dataset->zoomInDomain(r,geometry().size());
349 338 m_zoomStack<<r;
350 339 m_zoomIndex++;
351 340 if(m_animator) {
352 341 m_animator->setState(ChartAnimator::ShowState);
353 342 }
354 343 }
355 344
356 345 void ChartPresenter::zoomOut()
357 346 {
358 347 if(m_zoomIndex==0) return;
359 348 if(m_animator)
360 349 {
361 350 m_animator->setState(ChartAnimator::ZoomOutState);
362 351 }
363 352 m_dataset->zoomOutDomain(m_zoomStack[m_zoomIndex-1],geometry().size());
364 353 m_zoomIndex--;
365 354 m_zoomStack.resize(m_zoomIndex);
366 355 if(m_animator){
367 356 m_animator->setState(ChartAnimator::ShowState);
368 357 }
369 358 }
370 359
371 360 void ChartPresenter::zoomReset()
372 361 {
373 362 m_zoomIndex=0;
374 363 m_zoomStack.resize(m_zoomIndex);
375 364 }
376 365
377 366 void ChartPresenter::scroll(int dx,int dy)
378 367 {
379 368 if(m_animator){
380 369 if(dx<0) m_animator->setState(ChartAnimator::ScrollLeftState,QPointF());
381 370 if(dx>0) m_animator->setState(ChartAnimator::ScrollRightState,QPointF());
382 371 if(dy<0) m_animator->setState(ChartAnimator::ScrollUpState,QPointF());
383 372 if(dy>0) m_animator->setState(ChartAnimator::ScrollDownState,QPointF());
384 373 }
385 374
386 375 m_dataset->scrollDomain(dx,dy,geometry().size());
387 376
388 377 if(m_animator){
389 378 m_animator->setState(ChartAnimator::ShowState);
390 379 }
391 380 }
392 381
393 382 QChart::AnimationOptions ChartPresenter::animationOptions() const
394 383 {
395 384 return m_options;
396 385 }
397 386
398 387
399 388 #include "moc_chartpresenter_p.cpp"
400 389
401 390 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,86 +1,85
1 1 #ifndef CHARTPRESENTER_H_
2 2 #define CHARTPRESENTER_H_
3 3
4 4 #include "qchartglobal.h"
5 5 #include "qchart.h" //becouse of QChart::ChartThemeId //TODO
6 6 #include "qchartaxis.h"
7 7 #include <QRectF>
8 8
9 9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 10
11 11 class ChartItem;
12 12 class QSeries;
13 13 class ChartDataSet;
14 14 class Domain;
15 15 class AxisItem;
16 16 class ChartTheme;
17 17 class ChartAnimator;
18 18
19 19 class ChartPresenter: public QObject
20 20 {
21 21 Q_OBJECT
22 22 public:
23 23 enum ZValues {
24 24 BackgroundZValue = -1,
25 25 ShadesZValue,
26 26 GridZValue,
27 27 AxisZValue,
28 28 LineChartZValue,
29 29 BarSeriesZValue,
30 30 ScatterSeriesZValue,
31 31 PieSeriesZValue,
32 32 LegendZValue
33 33 };
34 34
35 35 ChartPresenter(QChart* chart,ChartDataSet *dataset);
36 36 virtual ~ChartPresenter();
37 37
38 38 void setMargin(int margin);
39 39 int margin() const;
40 40
41 41 QRectF geometry() const;
42 42 ChartAnimator* animator() const {return m_animator;};
43 43
44 44 void setChartTheme(QChart::ChartTheme theme);
45 45 QChart::ChartTheme chartTheme();
46 46
47 47 void setAnimationOptions(QChart::AnimationOptions options);
48 48 QChart::AnimationOptions animationOptions() const;
49 49
50 50 void zoomIn();
51 51 void zoomIn(const QRectF& rect);
52 52 void zoomOut();
53 53 void zoomReset();
54 54 void scroll(int dx,int dy);
55 55 private:
56 56 void createConnections();
57 57 void resetAllElements();
58 58
59 59 public slots:
60 60 void handleSeriesAdded(QSeries* series,Domain* domain);
61 61 void handleSeriesRemoved(QSeries* series);
62 62 void handleAxisAdded(QChartAxis* axis,Domain* domain);
63 63 void handleAxisRemoved(QChartAxis* axis);
64 64 void handleGeometryChanged();
65 65
66 66 signals:
67 67 void geometryChanged(const QRectF& rect);
68 68
69 69 private:
70 70 QChart* m_chart;
71 71 ChartAnimator* m_animator;
72 72 ChartDataSet* m_dataset;
73 73 ChartTheme *m_chartTheme;
74 74 int m_zoomIndex;
75 int m_marginSize;
76 75 QMap<QSeries*,ChartItem*> m_chartItems;
77 76 QMap<QChartAxis*,AxisItem*> m_axisItems;
78 77 QVector<QRectF> m_zoomStack;
79 78 QRectF m_rect;
80 79 QChart::AnimationOptions m_options;
81 80
82 81 };
83 82
84 83 QTCOMMERCIALCHART_END_NAMESPACE
85 84
86 85 #endif /* CHARTPRESENTER_H_ */
@@ -1,357 +1,372
1 1 #include "qchart.h"
2 2 #include "qchartaxis.h"
3 3 #include "qlegend.h"
4 4 #include "chartpresenter_p.h"
5 5 #include "chartdataset_p.h"
6 #include "chartbackground_p.h"
6 7 #include <QGraphicsScene>
7 8 #include <QGraphicsSceneResizeEvent>
8 9 #include <QDebug>
9 10
10 11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11 12
12 13 /*!
13 14 \enum QChart::ChartTheme
14 15
15 16 This enum describes the theme used by the chart.
16 17
17 18 \value ChartThemeDefault Follows the GUI style of the Operating System
18 19 \value ChartThemeVanilla
19 20 \value ChartThemeIcy
20 21 \value ChartThemeGrayscale
21 22 \value ChartThemeScientific
22 23 \value ChartThemeBlueCerulean
23 24 \value ChartThemeLight
24 25 \value ChartThemeCount Not really a theme; the total count of themes.
25 26 */
26 27
27 28 /*!
28 29 \enum QChart::AnimationOption
29 30
30 31 For enabling/disabling animations. Defaults to NoAnimation.
31 32
32 33 \value NoAnimation
33 34 \value GridAxisAnimations
34 35 \value SeriesAnimations
35 36 \value AllAnimations
36 37 */
37 38
38 39 /*!
39 40 \class QChart
40 41 \brief QtCommercial chart API.
41 42
42 43 QChart is a QGraphicsWidget that you can show in a QGraphicsScene. It manages the graphical
43 44 representation of different types of QChartSeries and other chart related objects like
44 45 QChartAxis and QChartLegend. If you simply want to show a chart in a layout, you can use the
45 46 convenience class QChartView instead of QChart.
46 47 \sa QChartView
47 48 */
48 49
49 50 /*!
50 51 Constructs a chart object which is a child of a\a parent. Parameter \a wFlags is passed to the QGraphicsWidget constructor.
51 52 */
52 53 QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags),
53 54 m_backgroundItem(0),
54 55 m_titleItem(0),
55 56 m_legend(new QLegend(this)),
56 57 m_dataset(new ChartDataSet(this)),
57 m_presenter(new ChartPresenter(this,m_dataset))
58 m_presenter(new ChartPresenter(this,m_dataset)),
59 m_padding(50),
60 m_backgroundPadding(10)
58 61 {
59 62 connect(m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),m_legend,SLOT(handleSeriesAdded(QSeries*,Domain*)));
60 63 connect(m_dataset,SIGNAL(seriesRemoved(QSeries*)),m_legend,SLOT(handleSeriesRemoved(QSeries*)));
61 64 }
62 65
63 66 /*!
64 67 Destroys the object and it's children, like QChartSeries and QChartAxis object added to it.
65 68 */
66 69 QChart::~QChart()
67 70 {
68 71 disconnect(m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),m_legend,SLOT(handleSeriesAdded(QSeries*,Domain*)));
69 72 disconnect(m_dataset,SIGNAL(seriesRemoved(QSeries*)),m_legend,SLOT(handleSeriesRemoved(QSeries*)));
70 73 }
71 74
72 75 /*!
73 76 Adds the \a series and optional \a axisY onto the chart and takes the ownership of the objects.
74 77 If auto scaling is enabled, re-scales the axes the series is bound to (both the x axis and
75 78 the y axis).
76 79 */
77 80 void QChart::addSeries(QSeries* series, QChartAxis* axisY)
78 81 {
79 82 m_dataset->addSeries(series, axisY);
80 83 }
81 84
82 85 /*!
83 86 Removes the \a series specified in a perameter from the QChartView.
84 87 It releses its ownership of the specified QChartSeries object.
85 88 It does not delete the pointed QChartSeries data object
86 89 \sa addSeries(), removeAllSeries()
87 90 */
88 91 void QChart::removeSeries(QSeries* series)
89 92 {
90 93 m_dataset->removeSeries(series);
91 94 }
92 95
93 96 /*!
94 97 Removes all the QChartSeries that have been added to the QChartView
95 98 It also deletes the pointed QChartSeries data objects
96 99 \sa addSeries(), removeSeries()
97 100 */
98 101 void QChart::removeAllSeries()
99 102 {
100 103 m_dataset->removeAllSeries();
101 104 }
102 105
103 106 /*!
104 107 Sets the \a brush that is used for painting the background of the chart area.
105 108 */
106 109 void QChart::setChartBackgroundBrush(const QBrush& brush)
107 110 {
108 111 createChartBackgroundItem();
109 112 m_backgroundItem->setBrush(brush);
110 113 m_backgroundItem->update();
111 114 }
112 115
113 116 /*!
114 117 Sets the \a pen that is used for painting the background of the chart area.
115 118 */
116 119 void QChart::setChartBackgroundPen(const QPen& pen)
117 120 {
118 121 createChartBackgroundItem();
119 122 m_backgroundItem->setPen(pen);
120 123 m_backgroundItem->update();
121 124 }
122 125
123 126 /*!
124 127 Sets the chart \a title. The description text that is drawn above the chart.
125 128 */
126 129 void QChart::setChartTitle(const QString& title)
127 130 {
128 131 createChartTitleItem();
129 132 m_titleItem->setText(title);
130 133 updateLayout();
131 134 }
132 135
133 136 /*!
134 137 Returns the chart title. The description text that is drawn above the chart.
135 138 */
136 139 QString QChart::chartTitle() const
137 140 {
138 141 if(m_titleItem)
139 142 return m_titleItem->text();
140 143 else
141 144 return QString();
142 145 }
143 146
144 147 /*!
145 148 Sets the \a font that is used for rendering the description text that is rendered above the chart.
146 149 */
147 150 void QChart::setChartTitleFont(const QFont& font)
148 151 {
149 152 createChartTitleItem();
150 153 m_titleItem->setFont(font);
151 154 updateLayout();
152 155 }
153 156
154 157 /*!
155 158 Sets the \a brush used for rendering the title text.
156 159 */
157 160 void QChart::setChartTitleBrush(const QBrush &brush)
158 161 {
159 162 createChartTitleItem();
160 163 m_titleItem->setBrush(brush);
161 164 updateLayout();
162 165 }
163 166
164 167 /*!
165 168 Returns the brush used for rendering the title text.
166 169 */
167 170 QBrush QChart::chartTitleBrush()
168 171 {
169 172 createChartTitleItem();
170 173 return m_titleItem->brush();
171 174 }
172 175
173 176 void QChart::createChartBackgroundItem()
174 177 {
175 178 if(!m_backgroundItem) {
176 m_backgroundItem = new QGraphicsRectItem(this);
179 m_backgroundItem = new ChartBackground(this);
177 180 m_backgroundItem->setPen(Qt::NoPen);
178 181 m_backgroundItem->setZValue(ChartPresenter::BackgroundZValue);
179 182 }
180 183 }
181 184
182 185 void QChart::createChartTitleItem()
183 186 {
184 187 if(!m_titleItem) {
185 188 m_titleItem = new QGraphicsSimpleTextItem(this);
186 189 m_titleItem->setZValue(ChartPresenter::BackgroundZValue);
187 190 }
188 191 }
189 192
190 193 /*!
191 Returns the chart margin, which is the distance between the widget edge and the part of the chart where the actual data can be displayed.
192 \sa setMargin()
193 */
194 int QChart::margin() const
195 {
196 return m_presenter->margin();
197 }
198
199 /*!
200 Sets the chart \a margin, which is the distance between the widget edge and the part of the chart where the actual data can be displayed.
201 \sa margin()
202 */
203 void QChart::setMargin(int margin)
204 {
205 m_presenter->setMargin(margin);
206 updateLayout();
207 }
208
209 /*!
210 194 Sets the \a theme used by the chart for rendering the graphical representation of the data
211 195 \sa ChartTheme, chartTheme()
212 196 */
213 197 void QChart::setChartTheme(QChart::ChartTheme theme)
214 198 {
215 199 m_presenter->setChartTheme(theme);
216 200 }
217 201
218 202 /*!
219 203 Returns the theme enum used by the chart.
220 204 \sa ChartTheme, setChartTheme()
221 205 */
222 206 QChart::ChartTheme QChart::chartTheme() const
223 207 {
224 208 return m_presenter->chartTheme();
225 209 }
226 210
227 211 /*!
228 212 Zooms in the view by a factor of 2
229 213 */
230 214 void QChart::zoomIn()
231 215 {
232 216 m_presenter->zoomIn();
233 217 }
234 218
235 219 /*!
236 220 Zooms in the view to a maximum level at which \a rect is still fully visible.
237 221 */
238 222 void QChart::zoomIn(const QRectF& rect)
239 223 {
240 224
241 225 if(!rect.isValid()) return;
242 226 m_presenter->zoomIn(rect);
243 227 }
244 228
245 229 /*!
246 230 Restores the view zoom level to the previous one.
247 231 */
248 232 void QChart::zoomOut()
249 233 {
250 234 m_presenter->zoomOut();
251 235 }
252 236
253 237 /*!
254 238 Resets to the default view.
255 239 */
256 240 void QChart::zoomReset()
257 241 {
258 242 m_presenter->zoomReset();
259 243 }
260 244
261 245 /*!
262 246 Returns the pointer to the x axis object of the chart
263 247 */
264 248 QChartAxis* QChart::axisX() const
265 249 {
266 250 return m_dataset->axisX();
267 251 }
268 252
269 253 /*!
270 254 Returns the pointer to the y axis object of the chart
271 255 */
272 256 QChartAxis* QChart::axisY() const
273 257 {
274 258 return m_dataset->axisY();
275 259 }
276 260
277 261 /*!
278 262 Returns the legend object of the chart
279 263 */
280 264 QLegend* QChart::legend()
281 265 {
282 266 return m_legend;
283 267 }
284 268
285 269 /*!
286 270 Resizes and updates the chart area using the \a event data
287 271 */
288 272 void QChart::resizeEvent(QGraphicsSceneResizeEvent *event)
289 273 {
290 274
291 275 m_rect = QRectF(QPoint(0,0),event->newSize());
292 276 updateLayout();
293 277 QGraphicsWidget::resizeEvent(event);
294 278 update();
295 279 }
296 280
297 281 /*!
298 282 Sets animation \a options for the chart
299 283 */
300 284 void QChart::setAnimationOptions(AnimationOptions options)
301 285 {
302 286 m_presenter->setAnimationOptions(options);
303 287 }
304 288
305 289 /*!
306 290 Returns animation options for the chart
307 291 */
308 292 QChart::AnimationOptions QChart::animationOptions() const
309 293 {
310 294 return m_presenter->animationOptions();
311 295 }
312 296
313 297 void QChart::scrollLeft()
314 298 {
315 299 m_presenter->scroll(-m_presenter->geometry().width()/(axisX()->ticksCount()-1),0);
316 300 }
317 301
318 302 void QChart::scrollRight()
319 303 {
320 304 m_presenter->scroll(m_presenter->geometry().width()/(axisX()->ticksCount()-1),0);
321 305 }
322 306 void QChart::scrollUp()
323 307 {
324 308 m_presenter->scroll(0,m_presenter->geometry().width()/(axisY()->ticksCount()-1));
325 309 }
326 310 void QChart::scrollDown()
327 311 {
328 312 m_presenter->scroll(0,-m_presenter->geometry().width()/(axisY()->ticksCount()-1));
329 313 }
330 314
331 315 void QChart::updateLayout()
332 316 {
333 317 if(!m_rect.isValid()) return;
334 318
335 QRectF rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
319 QRectF rect = m_rect.adjusted(m_padding,m_padding, -m_padding, -m_padding);
336 320
337 321 // recalculate title position
338 322 if (m_titleItem) {
339 323 QPointF center = m_rect.center() -m_titleItem->boundingRect().center();
340 m_titleItem->setPos(center.x(),m_rect.top()/2 + margin()/2);
324 m_titleItem->setPos(center.x(),m_rect.top()/2 + m_padding/2);
341 325 }
342 326
343 327 //recalculate background gradient
344 328 if (m_backgroundItem) {
345 m_backgroundItem->setRect(rect);
329 m_backgroundItem->setRect(m_rect.adjusted(m_backgroundPadding,m_backgroundPadding, -m_backgroundPadding, -m_backgroundPadding));
346 330 }
347 331
348 332 // recalculate legend position
349 333 if (m_legend) {
350 334 m_legend->setMaximumSize(rect.size());
351 335 m_legend->setPos(rect.topLeft());
352 336 m_legend->setPreferredLayout(QLegend::PreferredLayoutHorizontal);
353 337 }
354 338 }
339
340
341 int QChart::padding() const
342 {
343 return m_padding;
344 }
345
346 void QChart::setPadding(int padding)
347 {
348 if(m_padding==padding){
349 m_padding = padding;
350 m_presenter->handleGeometryChanged();
351 updateLayout();
352 }
353 }
354
355 void QChart::setBackgroundPadding(int padding)
356 {
357 if(m_backgroundPadding!=padding){
358 m_backgroundPadding = padding;
359 updateLayout();
360 }
361 }
362
363 void QChart::setBackgroundDiameter(int diameter)
364 {
365 createChartBackgroundItem();
366 m_backgroundItem->setDimeter(diameter);
367 m_backgroundItem->update();
368 }
369
355 370 #include "moc_qchart.cpp"
356 371
357 372 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,109 +1,117
1 1 #ifndef QCHART_H
2 2 #define QCHART_H
3 3
4 4 #include <qchartglobal.h>
5 5 #include <qseries.h>
6 6 #include <QGraphicsWidget>
7 7 #include <QLinearGradient>
8 8 #include <QFont>
9 9
10 10 class QGraphicsSceneResizeEvent;
11 11
12 12 QTCOMMERCIALCHART_BEGIN_NAMESPACE
13 13
14 14 class AxisItem;
15 15 class QSeries;
16 16 class PlotDomain;
17 17 class BarPresenter;
18 18 class QChartAxis;
19 19 class ChartTheme;
20 20 class ChartItem;
21 21 class ChartDataSet;
22 22 class ChartPresenter;
23 23 class QLegend;
24 class ChartBackground;
25
24 26
25 27 class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsWidget
26 28 {
27 29 Q_OBJECT
28 30 public:
29 31 enum ChartTheme {
30 32 ChartThemeDefault,
31 33 ChartThemeVanilla,
32 34 ChartThemeIcy,
33 35 ChartThemeGrayscale,
34 36 ChartThemeScientific,
35 37 ChartThemeBlueCerulean,
36 38 ChartThemeLight,
37 39 ChartThemeCount
38 40 };
39 41
40 42 enum AnimationOption {
41 43 NoAnimation = 0x0,
42 44 GridAxisAnimations = 0x1,
43 45 SeriesAnimations =0x2,
44 46 AllAnimations = 0x3
45 47 };
46 48 Q_DECLARE_FLAGS(AnimationOptions, AnimationOption)
47 49
48 50 public:
49 51 QChart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0);
50 52 ~QChart();
51 53
52 54 void addSeries(QSeries* series, QChartAxis* axisY = 0);
53 55 void removeSeries(QSeries* series); //returns ownership , deletes axis if no series attached
54 56 void removeAllSeries(); // deletes series and axis
55 57
56 void setMargin(int margin);
57 int margin() const;
58 58 void setChartTheme(QChart::ChartTheme theme);
59 59 QChart::ChartTheme chartTheme() const;
60 60
61 61 void setChartTitle(const QString& title);
62 62 QString chartTitle() const;
63 63 void setChartTitleFont(const QFont& font);
64 64 void setChartTitleBrush(const QBrush &brush);
65 65 QBrush chartTitleBrush();
66 66 void setChartBackgroundBrush(const QBrush& brush);
67 67 void setChartBackgroundPen(const QPen& pen);
68 68
69 69 void setAnimationOptions(AnimationOptions options);
70 70 AnimationOptions animationOptions() const;
71 71
72 72 void zoomIn();
73 73 void zoomIn(const QRectF& rect);
74 74 void zoomOut();
75 75 void zoomReset();
76 76 void scrollLeft();
77 77 void scrollRight();
78 78 void scrollUp();
79 79 void scrollDown();
80 80
81 81 QChartAxis* axisX() const;
82 82 QChartAxis* axisY() const;
83 83
84 84 // TODO: take (and give) legend instead of this.
85 85 QLegend* legend();
86 86
87
88 int padding() const;
89
87 90 protected:
88 91 void resizeEvent(QGraphicsSceneResizeEvent *event);
89 92
90 93 private:
91 94 inline void createChartBackgroundItem();
92 95 inline void createChartTitleItem();
96 void setPadding(int padding);
97 void setBackgroundPadding(int padding);
98 void setBackgroundDiameter(int diameter);
93 99 void updateLayout();
94 100
95 101 private:
96 102 Q_DISABLE_COPY(QChart)
97 QGraphicsRectItem* m_backgroundItem;
103 ChartBackground* m_backgroundItem;
98 104 QGraphicsSimpleTextItem* m_titleItem;
99 105 QRectF m_rect;
100 106 QLegend* m_legend;
101 107 ChartDataSet *m_dataset;
102 108 ChartPresenter *m_presenter;
109 int m_padding;
110 int m_backgroundPadding;
103 111 };
104 112
105 113 QTCOMMERCIALCHART_END_NAMESPACE
106 114
107 115 Q_DECLARE_OPERATORS_FOR_FLAGS(QTCOMMERCIALCHART_NAMESPACE::QChart::AnimationOptions)
108 116
109 117 #endif
@@ -1,406 +1,397
1 1 #include "qchartview.h"
2 2 #include "qchart.h"
3 3 #include "qchartaxis.h"
4 4 #include <QGraphicsView>
5 5 #include <QGraphicsScene>
6 6 #include <QRubberBand>
7 7 #include <QResizeEvent>
8 8 #include <QDebug>
9 9
10 10 /*!
11 11 \enum QChartView::RubberBandPolicy
12 12
13 13 This enum describes the different types of rubber bands that can be used for zoom rect selection
14 14
15 15 \value NoRubberBand
16 16 \value VerticalRubberBand
17 17 \value HorizonalRubberBand
18 18 \value RectangleRubberBand
19 19 */
20 20
21 21 /*!
22 22 \class QChartView
23 23 \brief Standalone charting widget.
24 24
25 25 QChartView is a standalone widget that can display charts. It does not require separate
26 26 QGraphicsScene to work. It manages the graphical representation of different types of
27 27 QChartSeries and other chart related objects like QChartAxis and QChartLegend. If you want to
28 28 display a chart in your existing QGraphicsScene, you can use the QChart class instead.
29 29
30 30 \sa QChart
31 31 */
32 32
33 33 QTCOMMERCIALCHART_BEGIN_NAMESPACE
34 34
35 35 /*!
36 36 Constructs a chartView object which is a child of a\a parent.
37 37 */
38 38 QChartView::QChartView(QWidget *parent) :
39 39 QGraphicsView(parent),
40 40 m_scene(new QGraphicsScene(this)),
41 41 m_chart(new QChart()),
42 42 m_rubberBand(0),
43 43 m_verticalRubberBand(false),
44 44 m_horizonalRubberBand(false)
45 45 {
46 46 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
47 47 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
48 48 setScene(m_scene);
49 m_chart->setMargin(50);
50 49 m_scene->addItem(m_chart);
51 50 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
52 51 }
53 52
54 53
55 54 /*!
56 55 Destroys the object and it's children, like QChartSeries and QChartAxis object added to it.
57 56 */
58 57 QChartView::~QChartView()
59 58 {
60 59 }
61 60
62 61 /*!
63 62 Resizes and updates the chart area using the \a event data
64 63 */
65 64 void QChartView::resizeEvent(QResizeEvent *event)
66 65 {
67 66 m_scene->setSceneRect(0,0,size().width(),size().height());
68 67 m_chart->resize(size());
69 68 QWidget::resizeEvent(event);
70 69 }
71 70
72 71 /*!
73 72 Adds the \a series and optional \a axisY onto the chart and takes the ownership of the objects.
74 73 If auto scaling is enabled, re-scales the axes the series is bound to (both the x axis and
75 74 the y axis).
76 75 \sa removeSeries(), removeAllSeries()
77 76 */
78 77 void QChartView::addSeries(QSeries* series,QChartAxis *axisY)
79 78 {
80 79 m_chart->addSeries(series,axisY);
81 80 }
82 81
83 82 /*!
84 83 Removes the \a series specified in a perameter from the QChartView.
85 84 It releses its ownership of the specified QChartSeries object.
86 85 It does not delete the pointed QChartSeries data object
87 86 \sa addSeries(), removeAllSeries()
88 87 */
89 88 void QChartView::removeSeries(QSeries* series)
90 89 {
91 90 m_chart->removeSeries(series);
92 91 }
93 92
94 93 /*!
95 94 Removes all the QChartSeries that have been added to the QChartView
96 95 It also deletes the pointed QChartSeries data objects
97 96 \sa addSeries(), removeSeries()
98 97 */
99 98 void QChartView::removeAllSeries()
100 99 {
101 100 m_chart->removeAllSeries();
102 101 }
103 102
104 103 /*!
105 104 Zooms in the view by a factor of 2
106 105 */
107 106 void QChartView::zoomIn()
108 107 {
109 108 m_chart->zoomIn();
110 109 }
111 110
112 111 /*!
113 112 Zooms in the view to a maximum level at which \a rect is still fully visible.
114 113 */
115 114 void QChartView::zoomIn(const QRect& rect)
116 115 {
117 116 m_chart->zoomIn(rect);
118 117 }
119 118
120 119 /*!
121 120 Restores the view zoom level to the previous one.
122 121 */
123 122 void QChartView::zoomOut()
124 123 {
125 124 m_chart->zoomOut();
126 125 }
127 126
128 127 /*!
129 Returns the chart margin, which is the distance between the widget edge and the part of the chart where the actual data can be displayed.
130 */
131 int QChartView::margin() const
132 {
133 return m_chart->margin();
134 }
135
136 /*!
137 128 Sets the chart \a title. A description text that is drawn above the chart.
138 129 */
139 130 void QChartView::setChartTitle(const QString& title)
140 131 {
141 132 m_chart->setChartTitle(title);
142 133 }
143 134
144 135 /*!
145 136 Returns the chart's title. A description text that is drawn above the chart.
146 137 */
147 138 QString QChartView::chartTitle() const
148 139 {
149 140 return m_chart->chartTitle();
150 141 }
151 142
152 143 /*!
153 144 Sets the \a font that is used for rendering the description text that is rendered above the chart.
154 145 */
155 146 void QChartView::setChartTitleFont(const QFont& font)
156 147 {
157 148 m_chart->setChartTitleFont(font);
158 149 }
159 150
160 151 /*!
161 152 Sets the \a brush used for rendering the title text.
162 153 */
163 154 void QChartView::setChartTitleBrush(const QBrush &brush)
164 155 {
165 156 m_chart->setChartTitleBrush(brush);
166 157 }
167 158
168 159 /*!
169 160 Returns the brush used for rendering the title text.
170 161 */
171 162 QBrush QChartView::chartTitleBrush()
172 163 {
173 164 return m_chart->chartTitleBrush();
174 165 }
175 166
176 167 /*!
177 168 Sets the \a brush that is used for painting the background of the chart area of the QChartView widget.
178 169 */
179 170 void QChartView::setChartBackgroundBrush(const QBrush& brush)
180 171 {
181 172 m_chart->setChartBackgroundBrush(brush);
182 173 }
183 174
184 175 /*!
185 176 Sets the \a pen that is used for painting the background of the chart area of the QChartView widget.
186 177 */
187 178 void QChartView::setChartBackgroundPen(const QPen& pen)
188 179 {
189 180 m_chart->setChartBackgroundPen(pen);
190 181 }
191 182
192 183 /*!
193 184 Sets the RubberBandPlicy to \a policy. Selected policy determines the way zooming is performed.
194 185 */
195 186 void QChartView::setRubberBandPolicy(const RubberBandPolicy policy)
196 187 {
197 188 switch(policy) {
198 189 case VerticalRubberBand:
199 190 m_verticalRubberBand = true;
200 191 m_horizonalRubberBand = false;
201 192 break;
202 193 case HorizonalRubberBand:
203 194 m_verticalRubberBand = false;
204 195 m_horizonalRubberBand = true;
205 196 break;
206 197 case RectangleRubberBand:
207 198 m_verticalRubberBand = true;
208 199 m_horizonalRubberBand = true;
209 200 break;
210 201 case NoRubberBand:
211 202 default:
212 203 delete m_rubberBand;
213 204 m_rubberBand=0;
214 205 m_horizonalRubberBand = false;
215 206 m_verticalRubberBand = false;
216 207 return;
217 208 }
218 209 if(!m_rubberBand) {
219 210 m_rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
220 211 m_rubberBand->setEnabled(true);
221 212 }
222 213 }
223 214
224 215 /*!
225 216 Returns the RubberBandPolicy that is currently being used by the widget.
226 217 */
227 218 QChartView::RubberBandPolicy QChartView::rubberBandPolicy() const
228 219 {
229 220 if(m_horizonalRubberBand && m_verticalRubberBand) return RectangleRubberBand;
230 221 if(m_horizonalRubberBand) return HorizonalRubberBand;
231 222 if(m_verticalRubberBand) return VerticalRubberBand;
232 223 return NoRubberBand;
233 224 }
234 225
235 226 /*!
236 227 If Left mouse button is pressed and the RubberBandPolicy is enabled the \a event is accepted and the rubber band is displayed on the screen allowing the user to select the zoom area.
237 228 If different mouse button is pressed and/or the RubberBandPolicy is disabled then the \a event is passed to QGraphicsView::mousePressEvent() implementation.
238 229 */
239 230 void QChartView::mousePressEvent(QMouseEvent *event)
240 231 {
241 232 if(m_rubberBand && m_rubberBand->isEnabled() && event->button() == Qt::LeftButton) {
242 233
243 int margin = m_chart->margin();
244 QRect rect(margin, margin, width() - 2 * margin, height() - 2 * margin);
234 int padding = m_chart->padding();
235 QRect rect(padding, padding, width() - 2 * padding, height() - 2 * padding);
245 236
246 237 if (rect.contains(event->pos())) {
247 238 m_rubberBandOrigin = event->pos();
248 239 m_rubberBand->setGeometry(QRect(m_rubberBandOrigin, QSize()));
249 240 m_rubberBand->show();
250 241 event->accept();
251 242 }
252 243 }
253 244 else {
254 245 QGraphicsView::mousePressEvent(event);
255 246 }
256 247 }
257 248
258 249 /*!
259 250 If RubberBand rectange specification has been initiated in pressEvent then \a event data is used to update RubberBand geometry.
260 251 In other case the defualt QGraphicsView::mouseMoveEvent implementation is called.
261 252 */
262 253 void QChartView::mouseMoveEvent(QMouseEvent *event)
263 254 {
264 255 if(m_rubberBand && m_rubberBand->isVisible()) {
265 int margin = m_chart->margin();
266 QRect rect(margin, margin, width() - 2 * margin, height() - 2 * margin);
256 int padding = m_chart->padding();
257 QRect rect(padding, padding, width() - 2 * padding, height() - 2 * padding);
267 258 int width = event->pos().x() - m_rubberBandOrigin.x();
268 259 int height = event->pos().y() - m_rubberBandOrigin.y();
269 260 if(!m_verticalRubberBand) {
270 261 m_rubberBandOrigin.setY(rect.top());
271 262 height = rect.height();
272 263 }
273 264 if(!m_horizonalRubberBand) {
274 265 m_rubberBandOrigin.setX(rect.left());
275 266 width= rect.width();
276 267 }
277 268 m_rubberBand->setGeometry(QRect(m_rubberBandOrigin.x(),m_rubberBandOrigin.y(), width,height).normalized());
278 269 }
279 270 else {
280 271 QGraphicsView::mouseMoveEvent(event);
281 272 }
282 273 }
283 274
284 275 /*!
285 276 If left mouse button is release and RubberBand is enabled then \a event is accepted and the view is zoomed in to rect specified by RubberBand
286 277 If it is the right mouse button \a event then RubberBand is dissmissed and zoom is canceled.
287 278 */
288 279 void QChartView::mouseReleaseEvent(QMouseEvent *event)
289 280 {
290 281 if(m_rubberBand) {
291 282 if (event->button() == Qt::LeftButton && m_rubberBand->isVisible()) {
292 283 m_rubberBand->hide();
293 284 QRect rect = m_rubberBand->geometry();
294 285 m_chart->zoomIn(rect);
295 286 event->accept();
296 287 }
297 288
298 289 if(event->button()==Qt::RightButton)
299 290 m_chart->zoomReset();
300 291 }
301 292 else {
302 293 QGraphicsView::mouseReleaseEvent(event);
303 294 }
304 295 }
305 296
306 297 /*!
307 298 Pressing + and - keys performs zoomIn() and zoomOut() respectivly.
308 299 In other \a event is passed to the QGraphicsView::keyPressEvent() implementation
309 300 */
310 301 void QChartView::keyPressEvent(QKeyEvent *event)
311 302 {
312 303 switch (event->key()) {
313 304 case Qt::Key_Plus:
314 305 zoomIn();
315 306 break;
316 307 case Qt::Key_Minus:
317 308 zoomOut();
318 309 break;
319 310 default:
320 311 QGraphicsView::keyPressEvent(event);
321 312 break;
322 313 }
323 314 }
324 315
325 316 /*!
326 317 Sets the \a theme used by the chart for rendering the graphical representation of the data
327 318 \sa QChart::ChartTheme, chartTheme()
328 319 */
329 320 void QChartView::setChartTheme(QChart::ChartTheme theme)
330 321 {
331 322 m_chart->setChartTheme(theme);
332 323 }
333 324
334 325 /*!
335 326 Returns the theme enum used by the chart.
336 327 \sa setChartTheme()
337 328 */
338 329 QChart::ChartTheme QChartView::chartTheme() const
339 330 {
340 331 return m_chart->chartTheme();
341 332 }
342 333
343 334 /*!
344 335 Returns the pointer to the x axis object of the chart
345 336 */
346 337 QChartAxis* QChartView::axisX() const
347 338 {
348 339 return m_chart->axisX();
349 340 }
350 341
351 342 /*!
352 343 Returns the pointer to the y axis object of the chart
353 344 */
354 345 QChartAxis* QChartView::axisY() const
355 346 {
356 347 return m_chart->axisY();
357 348 }
358 349
359 350 /*!
360 351 Returns the pointer to legend object of the chart
361 352 */
362 353 QLegend* QChartView::legend() const
363 354 {
364 355 return m_chart->legend();
365 356 }
366 357
367 358 /*!
368 359 Sets animation \a options for the chart
369 360 */
370 361 void QChartView::setAnimationOptions(QChart::AnimationOptions options)
371 362 {
372 363 m_chart->setAnimationOptions(options);
373 364 }
374 365
375 366 /*!
376 367 Returns animation options for the chart
377 368 */
378 369 QChart::AnimationOptions QChartView::animationOptions() const
379 370 {
380 371 return m_chart->animationOptions();
381 372 }
382 373
383 374 void QChartView::scrollLeft()
384 375 {
385 376 m_chart->scrollLeft();
386 377 }
387 378
388 379 void QChartView::scrollRight()
389 380 {
390 381 m_chart->scrollRight();
391 382 }
392 383
393 384 void QChartView::scrollUp()
394 385 {
395 386 m_chart->scrollUp();
396 387 }
397 388
398 389 void QChartView::scrollDown()
399 390 {
400 391 m_chart->scrollDown();
401 392 }
402 393
403 394
404 395 #include "moc_qchartview.cpp"
405 396
406 397 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,83 +1,82
1 1 #ifndef QCHARTWIDGET_H
2 2 #define QCHARTWIDGET_H
3 3
4 4 #include "qchartglobal.h"
5 5 #include "qchartaxis.h"
6 6 #include "qseries.h"
7 7 #include "qchart.h"
8 8 #include <QGraphicsView>
9 9
10 10 class QGraphicsScene;
11 11 class QRubberBand;
12 12
13 13 QTCOMMERCIALCHART_BEGIN_NAMESPACE
14 14
15 15 class QChart;
16 16
17 17 class QTCOMMERCIALCHART_EXPORT QChartView : public QGraphicsView
18 18 {
19 19 Q_OBJECT
20 20
21 21 public:
22 22 enum RubberBandPolicy { NoRubberBand, VerticalRubberBand, HorizonalRubberBand, RectangleRubberBand };
23 23
24 24 explicit QChartView(QWidget *parent = 0);
25 25 ~QChartView();
26 26
27 27 //implement from QWidget
28 28 void resizeEvent(QResizeEvent *event);
29 29
30 30 void addSeries(QSeries* series,QChartAxis* axisY=0);// takes series ownership , takes axis ownership
31 31 void removeSeries(QSeries* series); //returns ownership , deletes axis if no series attached
32 32 void removeAllSeries(); // deletes series and axis
33 int margin() const;
34 33
35 34 void setChartTitle(const QString& title);
36 35 QString chartTitle() const;
37 36 void setChartTitleFont(const QFont& font);
38 37 void setChartTitleBrush(const QBrush &brush);
39 38 QBrush chartTitleBrush();
40 39 void setChartBackgroundBrush(const QBrush& brush);
41 40 void setChartBackgroundPen(const QPen& pen);
42 41
43 42 void zoomIn();
44 43 void zoomIn(const QRect& rect);
45 44 void zoomOut();
46 45 void scrollLeft();
47 46 void scrollRight();
48 47 void scrollUp();
49 48 void scrollDown();
50 49
51 50 void setRubberBandPolicy(const RubberBandPolicy );
52 51 RubberBandPolicy rubberBandPolicy() const;
53 52
54 53 void setChartTheme(QChart::ChartTheme theme);
55 54 QChart::ChartTheme chartTheme() const;
56 55
57 56 void setAnimationOptions(QChart::AnimationOptions options);
58 57 QChart::AnimationOptions animationOptions() const;
59 58
60 59 QChartAxis* axisX() const;
61 60 QChartAxis* axisY() const;
62 61
63 62 QLegend* legend() const;
64 63
65 64 protected:
66 65 void mousePressEvent(QMouseEvent *event);
67 66 void mouseMoveEvent(QMouseEvent *event);
68 67 void mouseReleaseEvent(QMouseEvent *event);
69 68 void keyPressEvent(QKeyEvent *event);
70 69
71 70 private:
72 71 QGraphicsScene *m_scene;
73 72 QChart* m_chart;
74 73 QPoint m_rubberBandOrigin;
75 74 QRubberBand* m_rubberBand;
76 75 bool m_verticalRubberBand;
77 76 bool m_horizonalRubberBand;
78 77 Q_DISABLE_COPY(QChartView)
79 78 };
80 79
81 80 QTCOMMERCIALCHART_END_NAMESPACE
82 81
83 82 #endif // QCHARTWIDGET_H
@@ -1,142 +1,144
1 1 !include( ../common.pri ):error( Couldn't find the common.pri file! )
2 2 TARGET = QtCommercialChart
3 3 DESTDIR = $$CHART_BUILD_LIB_DIR
4 4 TEMPLATE = lib
5 5 QT += core \
6 6 gui
7 7 win32-msvc*: LIBS += User32.lib
8 8 CONFIG += debug_and_release
9 9 CONFIG(debug, debug|release):TARGET = QtCommercialChartd
10 10 SOURCES += \
11 11 chartdataset.cpp \
12 12 chartpresenter.cpp \
13 13 charttheme.cpp \
14 14 domain.cpp \
15 15 qchart.cpp \
16 16 qchartview.cpp \
17 17 qseries.cpp \
18 18 qlegend.cpp \
19 legendmarker.cpp
19 legendmarker.cpp \
20 chartbackground.cpp
20 21 PRIVATE_HEADERS += \
21 22 chartdataset_p.h \
22 23 chartitem_p.h \
23 24 chartpresenter_p.h \
24 25 charttheme_p.h \
25 26 domain_p.h \
26 legendmarker_p.h
27 legendmarker_p.h \
28 chartbackground_p.h
27 29 PUBLIC_HEADERS += \
28 30 qchart.h \
29 31 qchartglobal.h \
30 32 qseries.h \
31 33 qchartview.h \
32 qlegend.h
34 qlegend.h
33 35
34 36 include(animations/animations.pri)
35 37 include(axis/axis.pri)
36 38 include(xychart/xychart.pri)
37 39 include(linechart/linechart.pri)
38 40 include(areachart/areachart.pri)
39 41 include(barchart/barchart.pri)
40 42 include(piechart/piechart.pri)
41 43 include(scatterseries/scatter.pri)
42 44 include(splinechart/splinechart.pri)
43 45
44 46 THEMES += themes/chartthemedefault_p.h \
45 47 themes/chartthemeicy_p.h \
46 48 themes/chartthemegrayscale_p.h \
47 49 themes/chartthemescientific_p.h \
48 50 themes/chartthemevanilla_p.h \
49 51 themes/chartthemebluecerulean_p.h \
50 52 themes/chartthemelight_p.h
51 53
52 54 HEADERS += $$PUBLIC_HEADERS
53 55 HEADERS += $$PRIVATE_HEADERS
54 56 HEADERS += $$THEMES
55 57 INCLUDEPATH += linechart \
56 58 barchart \
57 59 themes \
58 60 .
59 61 OBJECTS_DIR = $$CHART_BUILD_DIR/lib
60 62 MOC_DIR = $$CHART_BUILD_DIR/lib
61 63 UI_DIR = $$CHART_BUILD_DIR/lib
62 64 RCC_DIR = $$CHART_BUILD_DIR/lib
63 65 DEFINES += QTCOMMERCIALCHART_LIBRARY
64 66
65 67 #qt public headers
66 68 #this is very primitive and lame parser , TODO: make perl script insted
67 69 !exists($$CHART_BUILD_PUBLIC_HEADER_DIR)
68 70 {
69 71 system($$QMAKE_MKDIR $$CHART_BUILD_PUBLIC_HEADER_DIR)
70 72 }
71 73
72 74 for(file, PUBLIC_HEADERS) {
73 75 name = $$split(file,'/')
74 76 name = $$last(name)
75 77 class = "$$cat($$file)"
76 78 class = $$find(class,class)
77 79 !isEmpty(class){
78 80 class = $$split(class,QTCOMMERCIALCHART_EXPORT)
79 81 class = $$member(class,1)
80 82 class = $$split(class,' ')
81 83 class = $$replace(class,' ','')
82 84 class = $$member(class,0)
83 85 win32:{
84 86 command = "echo $${LITERAL_HASH}include \"$$name\" > $$CHART_BUILD_PUBLIC_HEADER_DIR/$$class"
85 87 }else{
86 88 command = "echo \"$${LITERAL_HASH}include \\\"$$name\\\"\" > $$CHART_BUILD_PUBLIC_HEADER_DIR/$$class"
87 89 }
88 90 PUBLIC_QT_HEADERS += $$CHART_BUILD_PUBLIC_HEADER_DIR/$$class
89 91 system($$command)
90 92 }
91 93 }
92 94
93 95 public_headers.path = $$[QT_INSTALL_HEADERS]/QtCommercialChart
94 96 public_headers.files = $$PUBLIC_HEADERS $$PUBLIC_QT_HEADERS
95 97
96 98 target.path = $$[QT_INSTALL_LIBS]
97 99 INSTALLS += target public_headers
98 100
99 101 install_build_public_headers.name = build_public_headers
100 102 install_build_public_headers.output = $$CHART_BUILD_PUBLIC_HEADER_DIR/${QMAKE_FILE_BASE}.h
101 103 install_build_public_headers.input = PUBLIC_HEADERS
102 104 install_build_public_headers.commands = $$QMAKE_COPY \
103 105 ${QMAKE_FILE_NAME} \
104 106 $$CHART_BUILD_PUBLIC_HEADER_DIR
105 107 install_build_public_headers.CONFIG += target_predeps \
106 108 no_link
107 109
108 110 install_build_private_headers.name = buld_private_headers
109 111 install_build_private_headers.output = $$CHART_BUILD_PRIVATE_HEADER_DIR/${QMAKE_FILE_BASE}.h
110 112 install_build_private_headers.input = PRIVATE_HEADERS
111 113 install_build_private_headers.commands = $$QMAKE_COPY \
112 114 ${QMAKE_FILE_NAME} \
113 115 $$CHART_BUILD_PRIVATE_HEADER_DIR
114 116 install_build_private_headers.CONFIG += target_predeps \
115 117 no_link
116 118
117 119 QMAKE_EXTRA_COMPILERS += install_build_public_headers \
118 120 install_build_private_headers \
119 121
120 122 chartversion.target = qchartversion_p.h
121 123 chartversion.commands = @echo \
122 124 "build_time" \
123 125 > \
124 126 $$chartversion.target;
125 127 chartversion.depends = $$HEADERS \
126 128 $$SOURCES
127 129 PRE_TARGETDEPS += qchartversion_p.h
128 130 QMAKE_CLEAN += qchartversion_p.h
129 131 QMAKE_EXTRA_TARGETS += chartversion
130 132 unix:QMAKE_DISTCLEAN += -r \
131 133 $$CHART_BUILD_HEADER_DIR \
132 134 $$CHART_BUILD_LIB_DIR
133 135 win32:QMAKE_DISTCLEAN += /Q \
134 136 $$CHART_BUILD_HEADER_DIR \
135 137 $$CHART_BUILD_LIB_DIR
136 138
137 139 # treat warnings as errors
138 140 win32-msvc*: {
139 141 QMAKE_CXXFLAGS += /WX
140 142 } else {
141 143 QMAKE_CXXFLAGS += -Werror
142 144 }
General Comments 0
You need to be logged in to leave comments. Login now