##// END OF EJS Templates
Adds clicked(Point) to lineSeries, changes visible points handling
Michal Klocek -
r544:e21e24989fda
parent child
Show More
@@ -96,12 +96,13 void ChartTheme::decorate(LineChartItem* item, QLineSeries* series,int index)
96 {
96 {
97 QPen pen;
97 QPen pen;
98 if(pen != series->pen()){
98 if(pen != series->pen()){
99 item->setLinePen(series->pen());
99 series->setPen(series->pen());
100 return;
100 return;
101 }else{
102 pen.setColor(m_seriesColors.at(index%m_seriesColors.size()));
103 pen.setWidthF(2);
104 series->setPen(pen);
101 }
105 }
102 pen.setColor(m_seriesColors.at(index%m_seriesColors.size()));
103 pen.setWidthF(2);
104 item->setLinePen(pen);
105 }
106 }
106
107
107 void ChartTheme::decorate(BarPresenter* item, QBarSeries* series,int index)
108 void ChartTheme::decorate(BarPresenter* item, QBarSeries* series,int index)
@@ -2,6 +2,7
2 #include "qlineseries.h"
2 #include "qlineseries.h"
3 #include "chartpresenter_p.h"
3 #include "chartpresenter_p.h"
4 #include <QPainter>
4 #include <QPainter>
5 #include <QGraphicsSceneMouseEvent>
5
6
6
7
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
@@ -10,11 +11,11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10
11
11 LineChartItem::LineChartItem(QLineSeries* series,QGraphicsItem *parent):XYChartItem(series,parent),
12 LineChartItem::LineChartItem(QLineSeries* series,QGraphicsItem *parent):XYChartItem(series,parent),
12 m_series(series),
13 m_series(series),
13 m_items(this)
14 m_pointsVisible(false)
14 {
15 {
15 setZValue(ChartPresenter::LineChartZValue);
16 setZValue(ChartPresenter::LineChartZValue);
16 QObject::connect(series,SIGNAL(updated()),this,SLOT(handleUpdated()));
17 QObject::connect(series,SIGNAL(updated()),this,SLOT(handleUpdated()));
17 QObject::connect(this,SIGNAL(clicked()),series,SIGNAL(clicked()));
18 QObject::connect(this,SIGNAL(clicked(const QPointF&)),series,SIGNAL(clicked(const QPointF&)));
18 handleUpdated();
19 handleUpdated();
19 }
20 }
20
21
@@ -28,79 +29,31 QPainterPath LineChartItem::shape() const
28 return m_path;
29 return m_path;
29 }
30 }
30
31
31 void LineChartItem::createPoints(int count)
32 {
33 for (int i = 0; i < count; ++i) {
34 QGraphicsRectItem* item = new QGraphicsRectItem(0,0,3,3);
35 m_items.addToGroup(item);
36 }
37 }
38
39 void LineChartItem::deletePoints(int count)
40 {
41 QList<QGraphicsItem *> items = m_items.childItems();
42
43 for (int i = 0; i < count; ++i) {
44 delete(items.takeLast());
45 }
46 }
47
48 void LineChartItem::setGeometry(QVector<QPointF>& points)
32 void LineChartItem::setGeometry(QVector<QPointF>& points)
49 {
33 {
50 if(points.size()==0) return;
34 if(points.size()==0) return;
51
35
52 int diff = XYChartItem::points().size() - points.size();
53
54 if(diff>0) {
55 deletePoints(diff);
56 }
57 else if(diff<0) {
58 createPoints(-diff);
59 }
60
61 QList<QGraphicsItem*> items = m_items.childItems();
36 QList<QGraphicsItem*> items = m_items.childItems();
62
37
63 QPainterPath path;
38 QPainterPath linePath(points.at(0));
64 const QPointF& point = points.at(0);
65 path.moveTo(point);
66 QGraphicsItem* item = items.at(0);
67 item->setPos(point.x()-1,point.y()-1);
68 if(!clipRect().contains(point)) {
69 item->setVisible(false);
70 }
71 else {
72 item->setVisible(true);
73 }
74
39
75 for(int i=1; i< points.size();i++) {
40 for(int i=1; i< points.size();i++) {
76 QGraphicsItem* item = items.at(i);
41 linePath.lineTo(points.at(i));
77 const QPointF& point = points.at(i);
78 item->setPos(point.x()-1,point.y()-1);
79 if(!clipRect().contains(point)) {
80 item->setVisible(false);
81 }
82 else {
83 item->setVisible(true);
84 }
85 path.lineTo(point);
86 }
42 }
87
43
88 prepareGeometryChange();
44 prepareGeometryChange();
89 m_path = path;
45 m_path = linePath;
90 m_rect = path.boundingRect();
46 m_rect = linePath.boundingRect();
91
47
92 XYChartItem::setGeometry(points);
48 XYChartItem::setGeometry(points);
93 }
49 }
94
50
95 void LineChartItem::setLinePen(const QPen& pen)
96 {
97 m_pen = pen;
98 }
99
100 void LineChartItem::handleUpdated()
51 void LineChartItem::handleUpdated()
101 {
52 {
102 m_items.setVisible(m_series->pointsVisible());
53 m_pointsVisible = m_series->pointsVisible();
103 setLinePen(m_series->pen());
54 m_linePen = m_series->pen();
55 m_pointPen = m_series->pen();
56 m_pointPen.setWidthF(2*m_pointPen.width());
104 update();
57 update();
105 }
58 }
106
59
@@ -111,15 +64,19 void LineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *opt
111 Q_UNUSED(widget);
64 Q_UNUSED(widget);
112 Q_UNUSED(option);
65 Q_UNUSED(option);
113 painter->save();
66 painter->save();
114 painter->setPen(m_pen);
67 painter->setPen(m_linePen);
115 painter->setClipRect(clipRect());
68 painter->setClipRect(clipRect());
116 painter->drawPath(m_path);
69 painter->drawPath(m_path);
70 if(m_pointsVisible){
71 painter->setPen(m_pointPen);
72 painter->drawPoints(points());
73 }
117 painter->restore();
74 painter->restore();
118 }
75 }
119
76
120 void LineChartItem::mousePressEvent( QGraphicsSceneMouseEvent * event )
77 void LineChartItem::mousePressEvent( QGraphicsSceneMouseEvent * event )
121 {
78 {
122 emit clicked();
79 emit clicked(calculateDomainPoint(event->pos()));
123 }
80 }
124
81
125 #include "moc_linechartitem_p.cpp"
82 #include "moc_linechartitem_p.cpp"
@@ -21,29 +21,24 public:
21 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
21 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
22 QPainterPath shape() const;
22 QPainterPath shape() const;
23
23
24 void setLinePen(const QPen& pen);
25 void setPointsVisible(bool visible);
26
27 public slots:
24 public slots:
28 void handleUpdated();
25 void handleUpdated();
29
26
30 signals:
27 signals:
31 void clicked();
28 void clicked(const QPointF& point);
32
29
33 protected:
30 protected:
34 virtual void setGeometry(QVector<QPointF>& points);
31 virtual void setGeometry(QVector<QPointF>& points);
35 void mousePressEvent( QGraphicsSceneMouseEvent * event );
32 void mousePressEvent( QGraphicsSceneMouseEvent * event );
36
33
37 private:
34 private:
38 void createPoints(int count);
39 void deletePoints(int count);
40
41 private:
42 QLineSeries* m_series;
35 QLineSeries* m_series;
43 QGraphicsItemGroup m_items;
36 QGraphicsItemGroup m_items;
44 QPainterPath m_path;
37 QPainterPath m_path;
45 QRectF m_rect;
38 QRectF m_rect;
46 QPen m_pen;
39 QPen m_linePen;
40 QPen m_pointPen;
41 bool m_pointsVisible;
47
42
48 };
43 };
49
44
@@ -23,7 +23,7 public:
23 bool pointsVisible() const {return m_pointsVisible;}
23 bool pointsVisible() const {return m_pointsVisible;}
24
24
25 signals:
25 signals:
26 void clicked();
26 void clicked(const QPointF& point);
27
27
28 public: // from QChartSeries
28 public: // from QChartSeries
29 virtual QSeriesType type() const {return QSeries::SeriesTypeLine;}
29 virtual QSeriesType type() const {return QSeries::SeriesTypeLine;}
@@ -56,6 +56,15 QVector<QPointF> XYChartItem::calculateGeometryPoints() const
56 return points;
56 return points;
57 }
57 }
58
58
59 QPointF XYChartItem::calculateDomainPoint(const QPointF& point) const
60 {
61 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
62 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
63 qreal x = point.x()/deltaX +m_minX;
64 qreal y = (point.y()-m_size.height())/(-deltaY)+ m_minY;
65 return QPointF(x,y);
66 }
67
59 void XYChartItem::updatePoints(QVector<QPointF>& points)
68 void XYChartItem::updatePoints(QVector<QPointF>& points)
60 {
69 {
61 if(m_animator){
70 if(m_animator){
@@ -31,6 +31,7 protected:
31 virtual void setGeometry(QVector<QPointF>& points);
31 virtual void setGeometry(QVector<QPointF>& points);
32 QPointF calculateGeometryPoint(const QPointF& point) const;
32 QPointF calculateGeometryPoint(const QPointF& point) const;
33 QPointF calculateGeometryPoint(int index) const;
33 QPointF calculateGeometryPoint(int index) const;
34 QPointF calculateDomainPoint(const QPointF& point) const;
34 QVector<QPointF> calculateGeometryPoints() const;
35 QVector<QPointF> calculateGeometryPoints() const;
35
36
36 private:
37 private:
General Comments 0
You need to be logged in to leave comments. Login now