##// END OF EJS Templates
Refactor xyplotdata...
Michal Klocek -
r25:9ee7e26e06fe
parent child
Show More
@@ -1,7 +1,7
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 "xyplotdata_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>
@@ -21,7 +21,7 m_plotDataIndex(0)
21
21
22
22
23 //TODO hardcoded values , to removed soon
23 //TODO hardcoded values , to removed soon
24 XYPlotData* data = new XYPlotData();
24 XYPlotDomain* data = new XYPlotDomain();
25 data->m_minX = 0.0;
25 data->m_minX = 0.0;
26 data->m_maxX = 100.0;
26 data->m_maxX = 100.0;
27 data->m_minY = 0.0;
27 data->m_minY = 0.0;
@@ -51,7 +51,7 void QChart::addSeries(QChartSeries* series)
51 {
51 {
52 case QChartSeries::LINE:
52 case QChartSeries::LINE:
53 XYLineChartItem* item = new XYLineChartItem(reinterpret_cast<QXYChartSeries*>(series),this);
53 XYLineChartItem* item = new XYLineChartItem(reinterpret_cast<QXYChartSeries*>(series),this);
54 item->setXYPlotData(*m_plotDataList.at(0));
54 item->updateXYPlotData(*m_plotDataList.at(0));
55 m_items<<item;
55 m_items<<item;
56 break;
56 break;
57 }
57 }
@@ -63,8 +63,9 void QChart::setSize(const QSizeF& size) {
63 m_rect.adjust(margin(),margin(),-margin(),-margin());
63 m_rect.adjust(margin(),margin(),-margin(),-margin());
64 m_grid->setPos(m_rect.topLeft());
64 m_grid->setPos(m_rect.topLeft());
65 m_grid->setSize(m_rect.size());
65 m_grid->setSize(m_rect.size());
66 m_plotDataList.at(0)->m_viewportRect = m_rect;
66 foreach(QGraphicsItem* item , m_items)
67 foreach(QGraphicsItem* item , m_items)
67 reinterpret_cast<XYLineChartItem*>(item)->setChartSize(m_rect);
68 reinterpret_cast<XYLineChartItem*>(item)->updateXYPlotData(*m_plotDataList.at(0));
68 update();
69 update();
69
70
70 }
71 }
@@ -9,7 +9,7 QCHART_BEGIN_NAMESPACE
9 class Axis;
9 class Axis;
10 class XYGrid;
10 class XYGrid;
11 class QChartSeries;
11 class QChartSeries;
12 class XYPlotData;
12 class XYPlotDomain;
13
13
14 class QCHART_EXPORT QChart : public QGraphicsItem
14 class QCHART_EXPORT QChart : public QGraphicsItem
15 {
15 {
@@ -34,7 +34,7 private:
34 Axis* m_axisX;
34 Axis* m_axisX;
35 Axis* m_axisY;
35 Axis* m_axisY;
36 XYGrid* m_grid;
36 XYGrid* m_grid;
37 QList<XYPlotData*> m_plotDataList;
37 QList<XYPlotDomain*> m_plotDataList;
38 QList<QGraphicsItem*> m_items;
38 QList<QGraphicsItem*> m_items;
39 int m_plotDataIndex;
39 int m_plotDataIndex;
40 int m_marginSize;
40 int m_marginSize;
@@ -9,14 +9,14 SOURCES += \
9 xylinechart/qxychartseries.cpp \
9 xylinechart/qxychartseries.cpp \
10 xylinechart/xylinechartitem.cpp \
10 xylinechart/xylinechartitem.cpp \
11 xylinechart/xygrid.cpp \
11 xylinechart/xygrid.cpp \
12 xylinechart/xyplotdata.cpp \
12 xylinechart/xyplotdomain.cpp \
13 qchart.cpp \
13 qchart.cpp \
14 axis.cpp \
14 axis.cpp \
15 qchartwidget.cpp
15 qchartwidget.cpp
16
16
17 PRIVATE_HEADERS += \
17 PRIVATE_HEADERS += \
18 xylinechart/xylinechartitem_p.h \
18 xylinechart/xylinechartitem_p.h \
19 xylinechart/xyplotdata_p.h \# to be removed
19 xylinechart/xyplotdomain_p.h \
20 xylinechart/xygrid_p.h \
20 xylinechart/xygrid_p.h \
21 axis_p.h
21 axis_p.h
22
22
@@ -18,7 +18,7 void XYGrid::setSize(const QSizeF& size)
18 m_rect.setSize(size.toSize());
18 m_rect.setSize(size.toSize());
19 }
19 }
20
20
21 void XYGrid::setXYPlotData(const XYPlotData& xyPlotData)
21 void XYGrid::setXYPlotData(const XYPlotDomain& xyPlotData)
22 {
22 {
23 m_xyPlotData = xyPlotData;
23 m_xyPlotData = xyPlotData;
24 }
24 }
@@ -2,7 +2,7
2 #define XYGRID_H_
2 #define XYGRID_H_
3
3
4 #include <qchartconfig.h>
4 #include <qchartconfig.h>
5 #include <xyplotdata_p.h>
5 #include <xyplotdomain_p.h>
6 #include <QGraphicsItem>
6 #include <QGraphicsItem>
7
7
8 QCHART_BEGIN_NAMESPACE
8 QCHART_BEGIN_NAMESPACE
@@ -18,12 +18,12 public:
18 virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
18 virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
19
19
20 //TODO: this is just temporary interface
20 //TODO: this is just temporary interface
21 void setXYPlotData(const XYPlotData& xyPlotData);
21 void setXYPlotData(const XYPlotDomain& xyPlotData);
22 void setSize(const QSizeF& rect);
22 void setSize(const QSizeF& rect);
23
23
24 private:
24 private:
25 QRectF m_rect;
25 QRectF m_rect;
26 XYPlotData m_xyPlotData;
26 XYPlotDomain m_xyPlotData;
27 };
27 };
28
28
29 QCHART_END_NAMESPACE
29 QCHART_END_NAMESPACE
@@ -9,21 +9,34
9 QCHART_BEGIN_NAMESPACE
9 QCHART_BEGIN_NAMESPACE
10
10
11 XYLineChartItem::XYLineChartItem(QXYChartSeries* series,QGraphicsItem *parent):QGraphicsItem(parent),
11 XYLineChartItem::XYLineChartItem(QXYChartSeries* series,QGraphicsItem *parent):QGraphicsItem(parent),
12 m_series(series),
12 m_series(series)
13 m_dirtyGeometry(true)
14 {
13 {
15
14
16 }
15 }
17
16
18 void XYLineChartItem::setChartSize(const QRectF& rect)
17 void XYLineChartItem::updateXYPlotData(const XYPlotDomain& data)
19 {
18 {
20 m_rect = rect;
21 m_dirtyGeometry = true;
22 }
23
24 void XYLineChartItem::setXYPlotData(const XYPlotData& data){
25 m_xyPlotData=data;
19 m_xyPlotData=data;
26 m_dirtyGeometry = true;
20
21 if (!m_xyPlotData.m_viewportRect.isValid())
22 return;
23
24 const QRect& rect = m_xyPlotData.m_viewportRect;
25
26 const qreal deltaX = (rect.width()-1)/m_xyPlotData.spanX();
27 const qreal deltaY = (rect.height()-1)/m_xyPlotData.spanY();
28
29 m_polyline.clear();
30 m_polyline.resize(m_series->count());
31
32 for (int j = 0; j < m_series->count(); ++j) {
33 qreal dx = m_series->x(j) - m_xyPlotData.m_minX;
34 qreal dy = m_series->y(j) - m_xyPlotData.m_minY;
35 qreal x = (dx * deltaX) + rect.left();
36 qreal y = - (dy * deltaY) + rect.bottom();
37 m_polyline[j] = QPointF(x, y);
38 }
39
27 }
40 }
28
41
29 QRectF XYLineChartItem::boundingRect() const
42 QRectF XYLineChartItem::boundingRect() const
@@ -34,33 +47,7 QRectF XYLineChartItem::boundingRect() const
34
47
35 void XYLineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget)
48 void XYLineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget)
36 {
49 {
37 if(m_dirtyGeometry) {
50 painter->setClipRect(m_xyPlotData.m_viewportRect.adjusted(+1, +1, -1, -1));
38
39 m_dirtyGeometry=false;
40
41 if (!m_rect.isValid())
42 return;
43
44 painter->setClipRect(m_rect.adjusted(+1, +1, -1, -1));
45
46 const qreal deltaX = (m_rect.width()-1)/m_xyPlotData.spanX();
47 const qreal deltaY = (m_rect.height()-1)/m_xyPlotData.spanY();
48
49 m_polyline.clear();
50 m_polyline.resize(m_series->count());
51
52 for (int j = 0; j < m_series->count(); ++j) {
53 qreal dx = m_series->x(j) - m_xyPlotData.m_minX;
54 qreal dy = m_series->y(j) - m_xyPlotData.m_minY;
55 qreal x = (dx * deltaX) + m_rect.left();
56 qreal y = - (dy * deltaY) + m_rect.bottom();
57 m_polyline[j] = QPointF(x, y);
58 }
59 painter->setPen(m_series->color());
60 painter->drawPolyline(m_polyline);
61 }
62
63 painter->setClipRect(m_rect.adjusted(+1, +1, -1, -1));
64 painter->setPen(m_series->color());
51 painter->setPen(m_series->color());
65 painter->drawPolyline(m_polyline);
52 painter->drawPolyline(m_polyline);
66
53
@@ -3,7 +3,7
3
3
4 #include "qchartconfig.h"
4 #include "qchartconfig.h"
5 #include "qchart.h"
5 #include "qchart.h"
6 #include "xyplotdata_p.h"
6 #include "xyplotdomain_p.h"
7
7
8 QCHART_BEGIN_NAMESPACE
8 QCHART_BEGIN_NAMESPACE
9
9
@@ -20,16 +20,12 public:
20 virtual QRectF boundingRect() const;
20 virtual QRectF boundingRect() const;
21 virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
21 virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
22
22
23 //TODO: this is just temporary interface
23 void updateXYPlotData(const XYPlotDomain& data);
24 void setChartSize(const QRectF& size);
25 void setXYPlotData(const XYPlotData& data);
26
24
27 private:
25 private:
28 QRectF m_rect;
29 QPolygonF m_polyline;
26 QPolygonF m_polyline;
30 QXYChartSeries* m_series;
27 QXYChartSeries* m_series;
31 XYPlotData m_xyPlotData;
28 XYPlotDomain m_xyPlotData;
32 bool m_dirtyGeometry;
33 };
29 };
34
30
35 QCHART_END_NAMESPACE
31 QCHART_END_NAMESPACE
@@ -1,24 +1,30
1 #include "xyplotdata_p.h"
1 #include "xyplotdomain_p.h"
2
2
3 QCHART_BEGIN_NAMESPACE
3 QCHART_BEGIN_NAMESPACE
4
4
5 XYPlotData::XYPlotData()
5 XYPlotDomain::XYPlotDomain():
6 m_ticksX(0),
7 m_ticksY(0),
8 m_minX(0),
9 m_maxX(0),
10 m_minY(0),
11 m_maxY(0)
6 {
12 {
7
13
8 }
14 }
9
15
10 XYPlotData::~XYPlotData()
16 XYPlotDomain::~XYPlotDomain()
11 {
17 {
12 // TODO Auto-generated destructor stub
18 // TODO Auto-generated destructor stub
13 }
19 }
14
20
15 qreal XYPlotData::spanX() const
21 qreal XYPlotDomain::spanX() const
16 {
22 {
17 Q_ASSERT(m_maxX >= m_minX);
23 Q_ASSERT(m_maxX >= m_minX);
18 return m_maxX - m_minX;
24 return m_maxX - m_minX;
19 }
25 }
20
26
21 qreal XYPlotData::spanY() const
27 qreal XYPlotDomain::spanY() const
22 {
28 {
23 Q_ASSERT(m_maxY >= m_minY);
29 Q_ASSERT(m_maxY >= m_minY);
24 return m_maxY - m_minY;
30 return m_maxY - m_minY;
@@ -1,14 +1,14
1 #ifndef PLOTTER_H_
1 #ifndef PLOTDOMAIN_H_
2 #define PLOTTER_H_
2 #define PLOTDOMAIN_H_
3 #include "qchartconfig.h"
3 #include "qchartconfig.h"
4 #include <QtGlobal>
4 #include <QRect>
5
5
6 QCHART_BEGIN_NAMESPACE
6 QCHART_BEGIN_NAMESPACE
7
7
8 class XYPlotData {
8 class XYPlotDomain {
9 public:
9 public:
10 XYPlotData();
10 XYPlotDomain();
11 virtual ~XYPlotData();
11 virtual ~XYPlotDomain();
12
12
13 qreal spanX() const;
13 qreal spanX() const;
14 qreal spanY() const;
14 qreal spanY() const;
@@ -22,8 +22,7 public:
22 qreal m_maxX;
22 qreal m_maxX;
23 qreal m_minY;
23 qreal m_minY;
24 qreal m_maxY;
24 qreal m_maxY;
25
25 QRect m_viewportRect;
26
27 };
26 };
28
27
29 QCHART_END_NAMESPACE
28 QCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now