##// END OF EJS Templates
Adds pimpl to qchart class
Michal Klocek -
r28:15e60e95e515
parent child
Show More
@@ -1,78 +1,112
1 #include "qchart.h"
1 #include "qchart.h"
2 #include "qchartseries.h"
2 #include "qchartseries.h"
3 #include "xylinechartitem_p.h"
3 #include "xylinechartitem_p.h"
4 #include "xyplotdomain_p.h"
4 #include "xyplotdomain_p.h"
5 #include "axis_p.h"
5 #include "axis_p.h"
6 #include "xygrid_p.h"
6 #include "xygrid_p.h"
7 #include <QDebug>
7 #include <QDebug>
8
8
9 QCHART_BEGIN_NAMESPACE
9 QCHART_BEGIN_NAMESPACE
10
10
11 class QChartPrivate
12 {
13 public:
14
15 QChartPrivate(QChart* parent):
16 m_axisX(new Axis(parent)),
17 m_axisY(new Axis(parent)),
18 m_grid(new XYGrid(parent)),
19 m_plotDataIndex(0),
20 m_marginSize(0){}
21
22 Axis* m_axisX;
23 Axis* m_axisY;
24 XYGrid* m_grid;
25 QRect m_rect;
26 QList<const QChartSeries*> m_series;
27 QList<XYPlotDomain> m_plotDataList;
28 QList<QGraphicsItem*> m_items;
29 int m_plotDataIndex;
30 int m_marginSize;
31
32 };
33
34 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
35
11 QChart::QChart(QGraphicsItem* parent):QGraphicsItem(parent),
36 QChart::QChart(QGraphicsItem* parent):QGraphicsItem(parent),
12 m_marginSize(0),
37 d_ptr(new QChartPrivate(this))
13 m_axisX(new Axis(this)),
14 m_axisY(new Axis(this)),
15 m_grid(new XYGrid(this)),
16 m_plotDataIndex(0)
17 {
38 {
18 // setFlags(QGraphicsItem::ItemClipsChildrenToShape);
39 // setFlags(QGraphicsItem::ItemClipsChildrenToShape);
19 // set axis
40 // set axis
20 m_axisY->rotate(90);
41 Q_D(QChart);
21
42 d->m_axisY->rotate(90);
22
43
23 //TODO hardcoded values , to removed soon
44 //TODO hardcoded values , to removed soon
24 XYPlotDomain* data = new XYPlotDomain();
45 XYPlotDomain data;
25 data->m_minX = 0.0;
46 data.m_minX = 0.0;
26 data->m_maxX = 100.0;
47 data.m_maxX = 100.0;
27 data->m_minY = 0.0;
48 data.m_minY = 0.0;
28 data->m_maxY = 100.0;
49 data.m_maxY = 100.0;
29 data->m_ticksX=4;
50 data.m_ticksX=4;
30 data->m_ticksY=4;
51 data.m_ticksY=4;
31
52
32 m_plotDataList.clear();
53 d->m_plotDataList.clear();
33 m_plotDataList << data;
54 d->m_plotDataList << data;
34
55
35 m_grid->setZValue(10);
56 d->m_grid->setZValue(10);
36 m_grid->setXYPlotData(*m_plotDataList.at(0));
57 d->m_grid->setXYPlotData(d->m_plotDataList.at(0));
37 }
58 }
38
59
39 QChart::~QChart(){}
60 QChart::~QChart(){}
40
61
41 QRectF QChart::boundingRect() const
62 QRectF QChart::boundingRect() const
42 {
63 {
43 return m_rect;
64 Q_D(const QChart);
65 return d->m_rect;
44 }
66 }
45
67
46 void QChart::addSeries(QChartSeries* series)
68 void QChart::addSeries(QChartSeries* series)
47 {
69 {
48 m_series<<series;
70 Q_D(QChart);
71 d->m_series<<series;
49
72
50 switch(series->type())
73 switch(series->type())
51 {
74 {
52 case QChartSeries::LINE:
75 case QChartSeries::LINE:
53 XYLineChartItem* item = new XYLineChartItem(reinterpret_cast<QXYChartSeries*>(series),this);
76 XYLineChartItem* item = new XYLineChartItem(reinterpret_cast<QXYChartSeries*>(series),this);
54 item->updateXYPlotData(*m_plotDataList.at(0));
77 item->updateXYPlotData(d->m_plotDataList.at(0));
55 m_items<<item;
78 d->m_items<<item;
56 break;
79 break;
57 }
80 }
58 }
81 }
59
82
60 void QChart::setSize(const QSizeF& size) {
83 void QChart::setSize(const QSizeF& size)
84 {
85 Q_D(QChart);
61 //TODO refactor to setGeometry
86 //TODO refactor to setGeometry
62 m_rect = QRect(QPoint(0,0),size.toSize());
87 d->m_rect = QRect(QPoint(0,0),size.toSize());
63 m_rect.adjust(margin(),margin(),-margin(),-margin());
88 d->m_rect.adjust(margin(),margin(),-margin(),-margin());
64 m_grid->setPos(m_rect.topLeft());
89 d->m_grid->setPos(d->m_rect.topLeft());
65 m_grid->setSize(m_rect.size());
90 d->m_grid->setSize(d->m_rect.size());
66 m_plotDataList.at(0)->m_viewportRect = m_rect;
91 d->m_plotDataList[0].m_viewportRect = d->m_rect;
67 foreach(QGraphicsItem* item , m_items)
92 foreach(QGraphicsItem* item , d->m_items)
68 reinterpret_cast<XYLineChartItem*>(item)->updateXYPlotData(*m_plotDataList.at(0));
93 reinterpret_cast<XYLineChartItem*>(item)->updateXYPlotData(d->m_plotDataList.at(0));
69 update();
94 update();
70
95
71 }
96 }
72
97
98 int QChart::margin() const
99 {
100 Q_D(const QChart);
101 return d->m_marginSize;
102 }
103
73 void QChart::setMargin(int margin)
104 void QChart::setMargin(int margin)
74 {
105 {
75 m_marginSize = margin;
106 Q_D(QChart);
107 d->m_marginSize = margin;
76 }
108 }
77
109
110
111
78 QCHART_END_NAMESPACE
112 QCHART_END_NAMESPACE
@@ -1,45 +1,43
1 #ifndef CHART_H
1 #ifndef CHART_H
2 #define CHART_H
2 #define CHART_H
3
3
4 #include <qchartconfig.h>
4 #include <qchartconfig.h>
5 #include <QGraphicsItem>
5 #include <QGraphicsItem>
6
6
7 QCHART_BEGIN_NAMESPACE
7 QCHART_BEGIN_NAMESPACE
8
8
9 class Axis;
9 class Axis;
10 class XYGrid;
10 class XYGrid;
11 class QChartSeries;
11 class QChartSeries;
12 class XYPlotDomain;
12 class XYPlotDomain;
13 class QChartPrivate;
13
14
14 class QCHART_EXPORT QChart : public QGraphicsItem
15 class QCHART_EXPORT QChart : public QGraphicsItem
15 {
16 {
16
17
17 public:
18 public:
18 QChart(QGraphicsItem* parent = 0);
19 QChart(QGraphicsItem* parent = 0);
19 virtual ~QChart();
20 virtual ~QChart();
20
21
21 //from QGraphicsItem
22 //from QGraphicsItem
22 virtual QRectF boundingRect() const;
23 virtual QRectF boundingRect() const;
23 virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){};
24 virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){};
24
25
25 void addSeries(QChartSeries* series);
26 void addSeries(QChartSeries* series);
26
27
27 virtual void setSize(const QSizeF& rect);
28 virtual void setSize(const QSizeF& rect);
28 void setMargin(int margin);
29 void setMargin(int margin);
29 int margin() const { return m_marginSize;}
30 int margin() const;
31
32 protected:
33 QChartPrivate * const d_ptr;
30
34
31 private:
35 private:
32 QRect m_rect;
36 Q_DISABLE_COPY(QChart)
33 QList<const QChartSeries*> m_series;
37 Q_DECLARE_PRIVATE(QChart)
34 Axis* m_axisX;
38
35 Axis* m_axisY;
36 XYGrid* m_grid;
37 QList<XYPlotDomain*> m_plotDataList;
38 QList<QGraphicsItem*> m_items;
39 int m_plotDataIndex;
40 int m_marginSize;
41 };
39 };
42
40
43 QCHART_END_NAMESPACE
41 QCHART_END_NAMESPACE
44
42
45 #endif
43 #endif
@@ -1,65 +1,64
1 #include "qchartwidget.h"
1 #include "qchartwidget.h"
2 #include "qchartseries.h"
2 #include "qchartseries.h"
3 #include <QGraphicsView>
3 #include <QGraphicsView>
4 #include <QGraphicsScene>
4 #include <QGraphicsScene>
5 #include <QResizeEvent>
5 #include <QResizeEvent>
6
6
7 QCHART_BEGIN_NAMESPACE
7 QCHART_BEGIN_NAMESPACE
8
8
9 class QChartWidgetPrivate
9 class QChartWidgetPrivate
10 {
10 {
11 public:
11 public:
12 QChartWidgetPrivate(QChartWidget *parent) :
12 QChartWidgetPrivate(QChartWidget *parent) :
13 m_view(0),
13 m_view(0),
14 m_scene(0),
14 m_scene(0),
15 m_chart(0),
15 m_chart(0)
16 q_ptr( parent )
17 {
16 {
18 m_scene = new QGraphicsScene();
17 m_scene = new QGraphicsScene();
19 m_view = new QGraphicsView(parent);
18 m_view = new QGraphicsView(parent);
20 m_view->setScene(m_scene);
19 m_view->setScene(m_scene);
21 m_chart = new QChart();
20 m_chart = new QChart();
22 m_scene->addItem(m_chart);
21 m_scene->addItem(m_chart);
23 }
22 }
24
23
25 ~QChartWidgetPrivate() {
24 ~QChartWidgetPrivate() {
26 }
25 }
27
26
28 QGraphicsView *m_view;
27 QGraphicsView *m_view;
29 QGraphicsScene *m_scene;
28 QGraphicsScene *m_scene;
30 QChart* m_chart;
29 QChart* m_chart;
31 QChartWidget * const q_ptr;
32 Q_DECLARE_PUBLIC(QChartWidget);
33 };
30 };
34
31
32 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
33
35 QChartWidget::QChartWidget(QWidget *parent) :
34 QChartWidget::QChartWidget(QWidget *parent) :
36 QWidget(parent),
35 QWidget(parent),
37 d_ptr(new QChartWidgetPrivate(this))
36 d_ptr(new QChartWidgetPrivate(this))
38 {
37 {
39
38
40 }
39 }
41
40
42 QChartWidget::~QChartWidget()
41 QChartWidget::~QChartWidget()
43 {
42 {
44 delete d_ptr;
43 delete d_ptr;
45 }
44 }
46
45
47 void QChartWidget::resizeEvent(QResizeEvent *event)
46 void QChartWidget::resizeEvent(QResizeEvent *event)
48 {
47 {
49 Q_D(QChartWidget);
48 Q_D(QChartWidget);
50 d->m_view->resize(size().width(),size().height());
49 d->m_view->resize(size().width(),size().height());
51 d->m_scene->setSceneRect(0,0,size().width(),size().height());
50 d->m_scene->setSceneRect(0,0,size().width(),size().height());
52 d->m_chart->setSize(size());
51 d->m_chart->setSize(size());
53 QWidget::resizeEvent(event);
52 QWidget::resizeEvent(event);
54 }
53 }
55
54
56
55
57 void QChartWidget::addSeries(QChartSeries* series)
56 void QChartWidget::addSeries(QChartSeries* series)
58 {
57 {
59 Q_D(QChartWidget);
58 Q_D(QChartWidget);
60 d->m_chart->addSeries(series);
59 d->m_chart->addSeries(series);
61 }
60 }
62
61
63 #include "moc_qchartwidget.cpp"
62 #include "moc_qchartwidget.cpp"
64
63
65 QCHART_END_NAMESPACE
64 QCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now