##// END OF EJS Templates
Removes PIMPL for now...
Michal Klocek -
r53:c6b7296833b8
parent child
Show More
@@ -1,171 +1,151
1 1 #include "qchart.h"
2 2 #include "qchartseries.h"
3 3 #include "qscatterseries.h"
4 4 #include "qscatterseries_p.h"
5 5 #include "qpieseries.h"
6 6 #include "qxychartseries.h"
7 7 #include "xylinechartitem_p.h"
8 8 #include "xyplotdomain_p.h"
9 9 #include "axis_p.h"
10 10 #include "xygrid_p.h"
11 11 #include <QGraphicsScene>
12 12 #include <QDebug>
13 13
14 14 QTCOMMERCIALCHART_BEGIN_NAMESPACE
15 15
16 class QChartPrivate
17 {
18 public:
19
20 QChartPrivate(QChart* parent):
21 m_axisX(new Axis(parent)),
22 m_axisY(new Axis(parent)),
23 m_grid(new XYGrid(parent)),
24 m_plotDataIndex(0),
25 m_marginSize(0){}
26
27 Axis* m_axisX;
28 Axis* m_axisY;
29 XYGrid* m_grid;
30 QRect m_rect;
31 QList<const QChartSeries*> m_series;
32 QList<XYPlotDomain> m_plotDomainList;
33 QList<XYLineChartItem*> m_xyLineChartItems;
34 QList<QGraphicsItem*> m_items;
35 int m_plotDataIndex;
36 int m_marginSize;
37
38 };
39
40 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
41
42 16 QChart::QChart(QGraphicsObject* parent) : QGraphicsObject(parent),
43 d(new QChartPrivate(this))
17 m_axisX(new Axis(this)),
18 m_axisY(new Axis(this)),
19 m_grid(new XYGrid(this)),
20 m_plotDataIndex(0),
21 m_marginSize(0)
44 22 {
45 23 // setFlags(QGraphicsItem::ItemClipsChildrenToShape);
46 24 // set axis
47 d->m_axisY->rotate(90);
25 m_axisY->rotate(90);
48 26 }
49 27
50 28 QChart::~QChart(){}
51 29
52 30 QRectF QChart::boundingRect() const
53 31 {
54 return d->m_rect;
32 return m_rect;
55 33 }
56 34
57 35 void QChart::addSeries(QChartSeries* series)
58 36 {
59 37 // TODO: we should check the series not already added
60 38
61 d->m_series<<series;
39 m_series<<series;
62 40
63 41 switch(series->type())
64 42 {
65 43 case QChartSeries::SeriesTypeLine: {
66 44
67 45 QXYChartSeries* xyseries = static_cast<QXYChartSeries*>(series);
68 46
69 47 XYPlotDomain domain;
70 48 //TODO "nice numbers algorithm"
71 49 domain.m_ticksX=4;
72 50 domain.m_ticksY=4;
73 51
74 52 for (int i = 0 ; i < xyseries->count() ; i++)
75 53 {
76 54 qreal x = xyseries->x(i);
77 55 qreal y = xyseries->y(i);
78 56 domain.m_minX = qMin(domain.m_minX,x);
79 57 domain.m_minY = qMin(domain.m_minY,y);
80 58 domain.m_maxX = qMax(domain.m_maxX,x);
81 59 domain.m_maxY = qMax(domain.m_maxY,y);
82 60 }
83 61
84 62 XYLineChartItem* item = new XYLineChartItem(xyseries,this);
85 63 item->updateXYPlotDomain(domain);
86 d->m_plotDomainList<<domain;
87 d->m_xyLineChartItems<<item;
64 m_plotDomainList<<domain;
65 m_xyLineChartItems<<item;
88 66 break;
89 67 }
90 68 // TODO: Not tested:
91 69 // case QChartSeries::SeriesTypeScatter: {
92 70 // QScatterSeries *scatter = qobject_cast<QScatterSeries *>(series);
93 71 // if (scatter) {
94 72 // scatter->d->setParentItem(this);
95 73 // scene()->addItem(scatter->d);
96 74 // }
97 75 // break;
98 76 // }
99 77 }
100 78 }
101 79
102 80 QChartSeries* QChart::createSeries(QList<qreal> x, QList<qreal> y, QChartSeries::QChartSeriesType type)
103 81 {
104 82 // TODO: support also other types; not only scatter and pie
105 83 Q_ASSERT(type == QChartSeries::SeriesTypeScatter
106 84 || type == QChartSeries::SeriesTypePie);
107 85
108 86 switch (type) {
109 87 case QChartSeries::SeriesTypeScatter: {
110 88 QScatterSeries *scatterSeries = new QScatterSeries(x, y, this);
111 89 connect(this, SIGNAL(sizeChanged(QRectF, qreal, qreal)),
112 90 scatterSeries, SLOT(chartSizeChanged(QRectF, qreal, qreal)));
113 91 scatterSeries->d->setParentItem(this);
114 92 return scatterSeries;
115 93 }
116 94 case QChartSeries::SeriesTypePie: {
117 95 // TODO: we now have also a list of y values as a parameter, it is ignored
118 96 // we should use a generic data class instead of list of x and y values
119 97 QPieSeries *pieSeries = new QPieSeries(x, this);
120 98 connect(this, SIGNAL(sizeChanged(QRectF, qreal, qreal)),
121 99 pieSeries, SLOT(chartSizeChanged(QRectF, qreal, qreal)));
122 100 return pieSeries;
123 101 }
124 102 default:
125 103 break;
126 104 }
127 105
128 106 return 0;
129 107 }
130 108
131 109 void QChart::setSize(const QSizeF& size)
132 110 {
133 d->m_rect = QRect(QPoint(0,0),size.toSize());
134 d->m_rect.adjust(margin(),margin(), -margin(), -margin());
135 d->m_grid->setPos(d->m_rect.topLeft());
136 d->m_grid->setSize(d->m_rect.size());
111 m_rect = QRect(QPoint(0,0),size.toSize());
112 m_rect.adjust(margin(),margin(), -margin(), -margin());
113 m_grid->setPos(m_rect.topLeft());
114 m_grid->setSize(m_rect.size());
137 115
138 116 // TODO: calculate the scale
139 117 // TODO: calculate the origo
140 118 // TODO: not sure if emitting a signal here is the best from performance point of view
141 119 const qreal xscale = size.width() / 100;
142 120 const qreal yscale = size.height() / 100;
143 121 emit sizeChanged(QRectF(0, 0, size.width(), size.height()), xscale, yscale);
144 122
145 for (int i(0); i < d->m_plotDomainList.size(); i++)
146 d->m_plotDomainList[i].m_viewportRect = d->m_rect;
123 for (int i(0); i < m_plotDomainList.size(); i++)
124 m_plotDomainList[i].m_viewportRect = m_rect;
147 125
148 126 // TODO: line chart items are updated separately as they don't support update
149 127 // via sizeChanged signal
150 foreach(XYLineChartItem* item , d->m_xyLineChartItems)
151 item->updateXYPlotDomain(d->m_plotDomainList.at(d->m_plotDataIndex));
128 foreach(XYLineChartItem* item ,m_xyLineChartItems)
129 item->updateXYPlotDomain(m_plotDomainList.at(m_plotDataIndex));
130
131
132 if (m_plotDomainList.count())
133 m_grid->setXYPlotData(m_plotDomainList.at(m_plotDataIndex));
152 134
153 if (d->m_plotDomainList.count())
154 d->m_grid->setXYPlotData(d->m_plotDomainList.at(d->m_plotDataIndex));
155 135 update();
156 136 }
157 137
158 138 int QChart::margin() const
159 139 {
160 return d->m_marginSize;
140 return m_marginSize;
161 141 }
162 142
163 143 void QChart::setMargin(int margin)
164 144 {
165 d->m_marginSize = margin;
145 m_marginSize = margin;
166 146 }
167 147
168 148 #include "moc_qchart.cpp"
169 149
170 150
171 151 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,55 +1,63
1 1 #ifndef CHART_H
2 2 #define CHART_H
3 3
4 4 #include <qchartglobal.h>
5 5 #include <qchartseries.h>
6 6 #include <QGraphicsObject>
7 7
8 8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 9
10 10 class Axis;
11 11 class XYGrid;
12 12 class QChartSeries;
13 13 class XYPlotDomain;
14 class QChartPrivate;
14 class XYLineChartItem;
15 15
16 16 // TODO: We don't need to have QChart tied to QGraphicsItem:
17 17 //class QTCOMMERCIALCHART_EXPORT QChart
18 18 //class QTCOMMERCIALCHART_EXPORT QChartGraphicsItem : public QGraphicsItem {
19 19 // public: QChartGraphicsItem(QChart &chart);
20 20
21 21 /*!
22 22 * TODO: define the responsibilities
23 23 */
24 24 class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsObject
25 25 {
26 26 Q_OBJECT
27 27 public:
28 28 QChart(QGraphicsObject* parent = 0);
29 29 ~QChart();
30 30
31 31 //from QGraphicsItem
32 32 QRectF boundingRect() const;
33 33 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){};
34 34
35 35 void addSeries(QChartSeries* series);
36 36 //TODO: QChartSeries* createSeries(QSeriesData *data, QChartSeries::QChartSeriesType type);
37 37 // TODO: who owns the series now? maybe owned by chart and returned a reference instead...
38 38 QChartSeries* createSeries(QList<qreal> x, QList<qreal> y, QChartSeries::QChartSeriesType type);
39 39
40 40 virtual void setSize(const QSizeF& rect);
41 41 void setMargin(int margin);
42 42 int margin() const;
43 43
44 44 signals:
45 45 void sizeChanged(QRectF rect, qreal xscale, qreal yscale);
46 46
47 47 private:
48 // Q_DECLARE_PRIVATE(QChart)
49 48 Q_DISABLE_COPY(QChart)
50 QChartPrivate *d;
49 Axis* m_axisX;
50 Axis* m_axisY;
51 XYGrid* m_grid;
52 QRect m_rect;
53 QList<const QChartSeries*> m_series;
54 QList<XYPlotDomain> m_plotDomainList;
55 QList<XYLineChartItem*> m_xyLineChartItems;
56 QList<QGraphicsItem*> m_items;
57 int m_plotDataIndex;
58 int m_marginSize;
51 59 };
52 60
53 61 QTCOMMERCIALCHART_END_NAMESPACE
54 62
55 63 #endif
@@ -1,77 +1,51
1 1 #include "qchartwidget.h"
2 2 #include "qchartseries.h"
3 3 #include <QGraphicsView>
4 4 #include <QGraphicsScene>
5 5 #include <QResizeEvent>
6 6
7 7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 8
9 class QChartWidgetPrivate
10 {
11 public:
12 QChartWidgetPrivate(QChartWidget *parent) :
13 m_view(0),
14 m_scene(0),
15 m_chart(0)
9 QChartWidget::QChartWidget(QWidget *parent) :
10 QWidget(parent)
16 11 {
12 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
17 13 m_scene = new QGraphicsScene();
18 14 m_view = new QGraphicsView(parent);
19 15 m_view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
20 m_view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
21 16 m_view->setScene(m_scene);
22 17 m_chart = new QChart();
23 18 m_scene->addItem(m_chart);
24 19 m_view->show();
25 20 }
26 21
27 ~QChartWidgetPrivate() {
28 }
29
30 QGraphicsView *m_view;
31 QGraphicsScene *m_scene;
32 QChart* m_chart;
33 };
34
35 ///////////////////////////////////////////////////////////////////////////////////////////////////
36
37 QChartWidget::QChartWidget(QWidget *parent) :
38 QWidget(parent),
39 d_ptr(new QChartWidgetPrivate(this))
40 {
41 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
42 }
43
44 22 QChartWidget::~QChartWidget()
45 23 {
46 delete d_ptr;
47 24 }
48 25
49 26 void QChartWidget::resizeEvent(QResizeEvent *event)
50 27 {
51 Q_D(QChartWidget);
52 d->m_view->resize(size().width(),size().height());
53 d->m_scene->setSceneRect(0,0,size().width(),size().height());
54 d->m_chart->setSize(size());
28 m_view->resize(size().width(),size().height());
29 m_scene->setSceneRect(0,0,size().width(),size().height());
30 m_chart->setSize(size());
55 31 QWidget::resizeEvent(event);
56 32 }
57 33
58 34 QSize QChartWidget::sizeHint() const
59 35 {
60 36 // TODO: calculate size hint based on contents?
61 37 return QSize(100, 100);
62 38 }
63 39
64 40 void QChartWidget::addSeries(QChartSeries* series)
65 41 {
66 Q_D(QChartWidget);
67 d->m_chart->addSeries(series);
42 m_chart->addSeries(series);
68 43 }
69 44
70 45 QChartSeries* QChartWidget::createSeries(QList<qreal> x, QList<qreal> y, QChartSeries::QChartSeriesType type)
71 46 {
72 Q_D(QChartWidget);
73 return d->m_chart->createSeries(x, y, type);
47 return m_chart->createSeries(x, y, type);
74 48 }
75 49 #include "moc_qchartwidget.cpp"
76 50
77 51 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,38 +1,41
1 1 #ifndef QCHARTWIDGET_H
2 2 #define QCHARTWIDGET_H
3 3
4 4 #include "qchartglobal.h"
5 5 #include "qchart.h"
6 6 #include <QWidget>
7 7
8 class QGraphicsView;
9 class QGraphicsScene;
10
8 11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 12
10 13 class QChartSeries;
11 14 class QChartWidgetPrivate;
12 15
13 16 class QTCOMMERCIALCHART_EXPORT QChartWidget : public QWidget
14 17 {
15 18 Q_OBJECT
16 19 public:
17 20 explicit QChartWidget(QWidget *parent = 0);
18 21 ~QChartWidget();
19 22
20 23 //implement from QWidget
21 24 void resizeEvent(QResizeEvent *event);
22 25 QSize sizeHint() const;
23 26
24 27 // TODO: addSeries and createSeries are optional solutions
25 28 void addSeries(QChartSeries* series);
26 29 QChartSeries* createSeries(QList<qreal> x, QList<qreal> y, QChartSeries::QChartSeriesType type);
27 protected:
28 QChartWidgetPrivate * const d_ptr;
29 30
30 31 private:
31 32 Q_DISABLE_COPY(QChartWidget)
32 Q_DECLARE_PRIVATE(QChartWidget)
33 QGraphicsView *m_view;
34 QGraphicsScene *m_scene;
35 QChart* m_chart;
33 36
34 37 };
35 38
36 39 QTCOMMERCIALCHART_END_NAMESPACE
37 40
38 41 #endif // QCHARTWIDGET_H
General Comments 0
You need to be logged in to leave comments. Login now