##// END OF EJS Templates
Refactors click signal to line,area,spline,scatter charts
Michal Klocek -
r571:0bb609016fcc
parent child
Show More
@@ -60,9 +60,14 m_index(0)
60 m_series<<series3;
60 m_series<<series3;
61 m_titles<<chartTitle()+": AreaChart";
61 m_titles<<chartTitle()+": AreaChart";
62 //![3]
62 //![3]
63
63 addSeries(series0);
64 addSeries(series0);
64 setChartTitle(m_titles.at(0));
65 setChartTitle(m_titles.at(0));
65
66
67 foreach (QSeries* series, m_series) {
68 QObject::connect(series,SIGNAL(clicked(const QPointF&)),this,SLOT(handlePointClicked(const QPointF&)));
69 }
70
66 m_timer.start();
71 m_timer.start();
67 }
72 }
68
73
@@ -84,3 +89,8 void ChartView::handleTimeout()
84 setChartTitle(m_titles.at(m_index));
89 setChartTitle(m_titles.at(m_index));
85 }
90 }
86 //![4]
91 //![4]
92
93 void ChartView::handlePointClicked(const QPointF& point)
94 {
95 setChartTitle(m_titles.at(m_index) + QString(" x: %1 y: %2").arg(point.x()).arg(point.y()));
96 }
@@ -16,6 +16,7 public:
16
16
17 public slots:
17 public slots:
18 void handleTimeout();
18 void handleTimeout();
19 void handlePointClicked(const QPointF& point);
19
20
20 private:
21 private:
21 QTimer m_timer;
22 QTimer m_timer;
@@ -3,6 +3,7
3 #include "qlineseries.h"
3 #include "qlineseries.h"
4 #include "chartpresenter_p.h"
4 #include "chartpresenter_p.h"
5 #include <QPainter>
5 #include <QPainter>
6 #include <QGraphicsSceneMouseEvent>
6
7
7
8
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
@@ -22,6 +23,7 m_pointsVisible(false)
22 }
23 }
23
24
24 QObject::connect(areaSeries,SIGNAL(updated()),this,SLOT(handleUpdated()));
25 QObject::connect(areaSeries,SIGNAL(updated()),this,SLOT(handleUpdated()));
26 QObject::connect(this,SIGNAL(clicked(const QPointF&)),areaSeries,SIGNAL(clicked(const QPointF&)));
25
27
26 handleUpdated();
28 handleUpdated();
27 }
29 }
@@ -108,6 +110,11 void AreaChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *opt
108 painter->restore();
110 painter->restore();
109 }
111 }
110
112
113 void AreaChartItem::mousePressEvent( QGraphicsSceneMouseEvent * event )
114 {
115 emit clicked(m_upper->calculateDomainPoint(event->pos()));
116 }
117
111 #include "moc_areachartitem_p.cpp"
118 #include "moc_areachartitem_p.cpp"
112
119
113 QTCOMMERCIALCHART_END_NAMESPACE
120 QTCOMMERCIALCHART_END_NAMESPACE
@@ -26,6 +26,13 public:
26 LineChartItem* lowerLineItem() const { return m_lower ;}
26 LineChartItem* lowerLineItem() const { return m_lower ;}
27
27
28 void updatePath();
28 void updatePath();
29
30 protected:
31 void mousePressEvent( QGraphicsSceneMouseEvent * event );
32
33 signals:
34 void clicked(const QPointF& point);
35
29 public slots:
36 public slots:
30 void handleUpdated();
37 void handleUpdated();
31 void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY);
38 void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY);
@@ -92,7 +92,10 QAreaSeries::~QAreaSeries()
92 */
92 */
93 void QAreaSeries::setPen(const QPen& pen)
93 void QAreaSeries::setPen(const QPen& pen)
94 {
94 {
95 m_pen=pen;
95 if(m_pen!=pen){
96 m_pen=pen;
97 emit updated();
98 }
96 }
99 }
97
100
98 /*!
101 /*!
@@ -100,14 +103,20 void QAreaSeries::setPen(const QPen& pen)
100 */
103 */
101 void QAreaSeries::setBrush(const QBrush& brush)
104 void QAreaSeries::setBrush(const QBrush& brush)
102 {
105 {
103 m_brush=brush;
106 if(m_brush!=brush){
107 m_brush=brush;
108 emit updated();
109 }
104 }
110 }
105 /*!
111 /*!
106 Sets if data points are \a visible and should be drawn on line.
112 Sets if data points are \a visible and should be drawn on line.
107 */
113 */
108 void QAreaSeries::setPointsVisible(bool visible)
114 void QAreaSeries::setPointsVisible(bool visible)
109 {
115 {
110 m_pointsVisible=visible;
116 if(m_pointsVisible!=visible){
117 m_pointsVisible=visible;
118 emit updated();
119 }
111 }
120 }
112
121
113 #include "moc_qareaseries.cpp"
122 #include "moc_qareaseries.cpp"
@@ -34,6 +34,7 public: // from QChartSeries
34
34
35 signals:
35 signals:
36 void updated();
36 void updated();
37 void clicked(const QPointF& point);
37
38
38 private:
39 private:
39 QBrush m_brush;
40 QBrush m_brush;
@@ -2,7 +2,6
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>
6
5
7
6
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
@@ -15,7 +14,6 m_pointsVisible(false)
15 {
14 {
16 setZValue(ChartPresenter::LineChartZValue);
15 setZValue(ChartPresenter::LineChartZValue);
17 QObject::connect(series,SIGNAL(updated()),this,SLOT(handleUpdated()));
16 QObject::connect(series,SIGNAL(updated()),this,SLOT(handleUpdated()));
18 QObject::connect(this,SIGNAL(clicked(const QPointF&)),series,SIGNAL(clicked(const QPointF&)));
19 handleUpdated();
17 handleUpdated();
20 }
18 }
21
19
@@ -78,11 +76,6 void LineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *opt
78 painter->restore();
76 painter->restore();
79 }
77 }
80
78
81 void LineChartItem::mousePressEvent( QGraphicsSceneMouseEvent * event )
82 {
83 emit clicked(calculateDomainPoint(event->pos()));
84 }
85
86 #include "moc_linechartitem_p.cpp"
79 #include "moc_linechartitem_p.cpp"
87
80
88 QTCOMMERCIALCHART_END_NAMESPACE
81 QTCOMMERCIALCHART_END_NAMESPACE
@@ -24,12 +24,8 public:
24 public slots:
24 public slots:
25 void handleUpdated();
25 void handleUpdated();
26
26
27 signals:
28 void clicked(const QPointF& point);
29
30 protected:
27 protected:
31 void setLayout(QVector<QPointF>& points);
28 void setLayout(QVector<QPointF>& points);
32 void mousePressEvent( QGraphicsSceneMouseEvent * event );
33
29
34 private:
30 private:
35 QLineSeries* m_series;
31 QLineSeries* m_series;
@@ -65,7 +65,7 void QLineSeries::setLinePen(const QPen& pen)
65 {
65 {
66 if(pen!=m_pen){
66 if(pen!=m_pen){
67 m_pen=pen;
67 m_pen=pen;
68 emit updated();
68 emit QXYSeries::updated();
69 }
69 }
70 }
70 }
71
71
@@ -76,7 +76,7 void QLineSeries::setPointsVisible(bool visible)
76 {
76 {
77 if(m_pointsVisible!=visible){
77 if(m_pointsVisible!=visible){
78 m_pointsVisible=visible;
78 m_pointsVisible=visible;
79 emit updated();
79 emit QXYSeries::updated();
80 }
80 }
81 }
81 }
82
82
@@ -93,6 +93,4 QDebug operator<< (QDebug debug, const QLineSeries series)
93 return debug.space();
93 return debug.space();
94 }
94 }
95
95
96 #include "moc_qlineseries.cpp"
97
98 QTCOMMERCIALCHART_END_NAMESPACE
96 QTCOMMERCIALCHART_END_NAMESPACE
@@ -11,7 +11,6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11
11
12 class QTCOMMERCIALCHART_EXPORT QLineSeries : public QXYSeries
12 class QTCOMMERCIALCHART_EXPORT QLineSeries : public QXYSeries
13 {
13 {
14 Q_OBJECT
15 public:
14 public:
16 QLineSeries(QObject* parent=0);
15 QLineSeries(QObject* parent=0);
17 virtual ~QLineSeries();
16 virtual ~QLineSeries();
@@ -22,9 +21,6 public:
22 void setPointsVisible(bool visible);
21 void setPointsVisible(bool visible);
23 bool pointsVisible() const {return m_pointsVisible;}
22 bool pointsVisible() const {return m_pointsVisible;}
24
23
25 signals:
26 void clicked(const QPointF& point);
27
28 public: // from QChartSeries
24 public: // from QChartSeries
29 virtual QSeriesType type() const {return QSeries::SeriesTypeLine;}
25 virtual QSeriesType type() const {return QSeries::SeriesTypeLine;}
30 friend QDebug operator<< (QDebug d, const QLineSeries series);
26 friend QDebug operator<< (QDebug d, const QLineSeries series);
@@ -124,6 +124,7 void QChart::setChartTitle(const QString& title)
124 {
124 {
125 createChartTitleItem();
125 createChartTitleItem();
126 m_titleItem->setText(title);
126 m_titleItem->setText(title);
127 updateLayout();
127 }
128 }
128
129
129 /*!
130 /*!
@@ -144,6 +145,7 void QChart::setChartTitleFont(const QFont& font)
144 {
145 {
145 createChartTitleItem();
146 createChartTitleItem();
146 m_titleItem->setFont(font);
147 m_titleItem->setFont(font);
148 updateLayout();
147 }
149 }
148
150
149 /*!
151 /*!
@@ -153,6 +155,7 void QChart::setChartTitleBrush(const QBrush &brush)
153 {
155 {
154 createChartTitleItem();
156 createChartTitleItem();
155 m_titleItem->setBrush(brush);
157 m_titleItem->setBrush(brush);
158 updateLayout();
156 }
159 }
157
160
158 /*!
161 /*!
@@ -197,6 +200,7 int QChart::margin() const
197 void QChart::setMargin(int margin)
200 void QChart::setMargin(int margin)
198 {
201 {
199 m_presenter->setMargin(margin);
202 m_presenter->setMargin(margin);
203 updateLayout();
200 }
204 }
201
205
202 /*!
206 /*!
@@ -282,19 +286,7 void QChart::resizeEvent(QGraphicsSceneResizeEvent *event)
282 {
286 {
283
287
284 m_rect = QRectF(QPoint(0,0),event->newSize());
288 m_rect = QRectF(QPoint(0,0),event->newSize());
285 QRectF rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
289 updateLayout();
286
287 // recalculate title position
288 if (m_titleItem) {
289 QPointF center = m_rect.center() -m_titleItem->boundingRect().center();
290 m_titleItem->setPos(center.x(),m_rect.top()/2 + margin()/2);
291 }
292
293 //recalculate background gradient
294 if (m_backgroundItem) {
295 m_backgroundItem->setRect(rect);
296 }
297
298 QGraphicsWidget::resizeEvent(event);
290 QGraphicsWidget::resizeEvent(event);
299 update();
291 update();
300 }
292 }
@@ -328,6 +320,23 void QChart::scroll(int dx,int dy)
328 m_presenter->scroll(0,-m_presenter->geometry().width()/(axisY()->ticksCount()-1));
320 m_presenter->scroll(0,-m_presenter->geometry().width()/(axisY()->ticksCount()-1));
329 }
321 }
330
322
323 void QChart::updateLayout()
324 {
325 if(!m_rect.isValid()) return;
326
327 QRectF rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
328
329 // recalculate title position
330 if (m_titleItem) {
331 QPointF center = m_rect.center() -m_titleItem->boundingRect().center();
332 m_titleItem->setPos(center.x(),m_rect.top()/2 + margin()/2);
333 }
334
335 //recalculate background gradient
336 if (m_backgroundItem) {
337 m_backgroundItem->setRect(rect);
338 }
339 }
331 #include "moc_qchart.cpp"
340 #include "moc_qchart.cpp"
332
341
333 QTCOMMERCIALCHART_END_NAMESPACE
342 QTCOMMERCIALCHART_END_NAMESPACE
@@ -85,6 +85,7 protected:
85 private:
85 private:
86 inline void createChartBackgroundItem();
86 inline void createChartBackgroundItem();
87 inline void createChartTitleItem();
87 inline void createChartTitleItem();
88 void updateLayout();
88
89
89 private:
90 private:
90 Q_DISABLE_COPY(QChart)
91 Q_DISABLE_COPY(QChart)
@@ -96,8 +96,4 void QScatterSeries::setSize(qreal size)
96 emit updated();
96 emit updated();
97 }
97 }
98
98
99
100
101 #include "moc_qscatterseries.cpp"
102
103 QTCOMMERCIALCHART_END_NAMESPACE
99 QTCOMMERCIALCHART_END_NAMESPACE
@@ -11,7 +11,6 class QScatterSeriesPrivate;
11
11
12 class QTCOMMERCIALCHART_EXPORT QScatterSeries : public QXYSeries
12 class QTCOMMERCIALCHART_EXPORT QScatterSeries : public QXYSeries
13 {
13 {
14 Q_OBJECT
15
14
16 public:
15 public:
17 enum MarkerShape {
16 enum MarkerShape {
@@ -32,9 +31,6 public:
32 qreal size() const;
31 qreal size() const;
33 void setSize(qreal size);
32 void setSize(qreal size);
34
33
35 signals:
36 void clicked(const QPointF& point);
37
38 private:
34 private:
39 MarkerShape m_shape;
35 MarkerShape m_shape;
40 qreal m_size;
36 qreal m_size;
@@ -24,7 +24,6 ScatterChartItem::ScatterChartItem(QScatterSeries *series, QGraphicsItem *parent
24 Q_ASSERT(series);
24 Q_ASSERT(series);
25
25
26 QObject::connect(m_series,SIGNAL(updated()), this, SLOT(handleUpdated()));
26 QObject::connect(m_series,SIGNAL(updated()), this, SLOT(handleUpdated()));
27 QObject::connect(this,SIGNAL(clicked(const QPointF&)), m_series, SIGNAL(clicked(const QPointF&)));
28
27
29 setZValue(ChartPresenter::ScatterSeriesZValue);
28 setZValue(ChartPresenter::ScatterSeriesZValue);
30 setFlags(QGraphicsItem::ItemHasNoContents);
29 setFlags(QGraphicsItem::ItemHasNoContents);
@@ -87,7 +86,7 void ScatterChartItem::deletePoints(int count)
87
86
88 void ScatterChartItem::markerSelected(Marker* marker)
87 void ScatterChartItem::markerSelected(Marker* marker)
89 {
88 {
90 emit clicked(QPointF(m_series->x(marker->index()), m_series->y(marker->index())));
89 emit XYChartItem::clicked(QPointF(m_series->x(marker->index()), m_series->y(marker->index())));
91 }
90 }
92
91
93 void ScatterChartItem::setLayout(QVector<QPointF>& points)
92 void ScatterChartItem::setLayout(QVector<QPointF>& points)
@@ -176,6 +175,10 void ScatterChartItem::handleUpdated()
176
175
177 }
176 }
178
177
178 void ScatterChartItem::mousePressEvent( QGraphicsSceneMouseEvent * event )
179 {
180 }
181
179 #include "moc_scatterchartitem_p.cpp"
182 #include "moc_scatterchartitem_p.cpp"
180
183
181 QTCOMMERCIALCHART_END_NAMESPACE
184 QTCOMMERCIALCHART_END_NAMESPACE
@@ -27,9 +27,6 public:
27
27
28 void markerSelected(Marker* item);
28 void markerSelected(Marker* item);
29
29
30 signals:
31 void clicked(const QPointF& point);
32
33 public slots:
30 public slots:
34 void handleUpdated();
31 void handleUpdated();
35
32
@@ -39,6 +36,7 private:
39
36
40 protected:
37 protected:
41 void setLayout(QVector<QPointF>& points);
38 void setLayout(QVector<QPointF>& points);
39 void mousePressEvent( QGraphicsSceneMouseEvent * event );
42
40
43 private:
41 private:
44 QScatterSeries *m_series;
42 QScatterSeries *m_series;
@@ -18,8 +18,6 public:
18 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
18 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
19 QPainterPath shape() const;
19 QPainterPath shape() const;
20
20
21 void setPointsVisible(bool visible);
22
23 public slots:
21 public slots:
24 void handleUpdated();
22 void handleUpdated();
25
23
@@ -51,6 +51,7 public:
51 void modelDataRemoved(QModelIndex parent, int start, int end);
51 void modelDataRemoved(QModelIndex parent, int start, int end);
52
52
53 signals:
53 signals:
54 void clicked(const QPointF& point);
54 void updated();
55 void updated();
55 void pointReplaced(int index);
56 void pointReplaced(int index);
56 void pointRemoved(int index);
57 void pointRemoved(int index);
@@ -3,6 +3,7
3 #include "chartpresenter_p.h"
3 #include "chartpresenter_p.h"
4 #include "chartanimator_p.h"
4 #include "chartanimator_p.h"
5 #include <QPainter>
5 #include <QPainter>
6 #include <QGraphicsSceneMouseEvent>
6
7
7
8
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
@@ -19,7 +20,7 m_series(series)
19 QObject::connect(series,SIGNAL(pointReplaced(int)),this,SLOT(handlePointReplaced(int)));
20 QObject::connect(series,SIGNAL(pointReplaced(int)),this,SLOT(handlePointReplaced(int)));
20 QObject::connect(series,SIGNAL(pointAdded(int)),this,SLOT(handlePointAdded(int)));
21 QObject::connect(series,SIGNAL(pointAdded(int)),this,SLOT(handlePointAdded(int)));
21 QObject::connect(series,SIGNAL(pointRemoved(int)),this,SLOT(handlePointRemoved(int)));
22 QObject::connect(series,SIGNAL(pointRemoved(int)),this,SLOT(handlePointRemoved(int)));
22
23 QObject::connect(this,SIGNAL(clicked(const QPointF&)),series,SIGNAL(clicked(const QPointF&)));
23 }
24 }
24
25
25 QPointF XYChartItem::calculateGeometryPoint(const QPointF& point) const
26 QPointF XYChartItem::calculateGeometryPoint(const QPointF& point) const
@@ -151,6 +152,11 bool XYChartItem::isEmpty()
151 return !m_clipRect.isValid() || m_maxX - m_minX == 0 || m_maxY - m_minY ==0 ;
152 return !m_clipRect.isValid() || m_maxX - m_minX == 0 || m_maxY - m_minY ==0 ;
152 }
153 }
153
154
155 void XYChartItem::mousePressEvent( QGraphicsSceneMouseEvent * event )
156 {
157 emit clicked(calculateDomainPoint(event->pos()));
158 }
159
154 #include "moc_xychartitem_p.cpp"
160 #include "moc_xychartitem_p.cpp"
155
161
156 QTCOMMERCIALCHART_END_NAMESPACE
162 QTCOMMERCIALCHART_END_NAMESPACE
@@ -27,12 +27,16 public slots:
27 void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY);
27 void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY);
28 void handleGeometryChanged(const QRectF& size);
28 void handleGeometryChanged(const QRectF& size);
29
29
30 signals:
31 void clicked(const QPointF& point);
32
30 protected:
33 protected:
31 virtual void setLayout(QVector<QPointF>& points);
34 virtual void setLayout(QVector<QPointF>& points);
32 QPointF calculateGeometryPoint(const QPointF& point) const;
35 QPointF calculateGeometryPoint(const QPointF& point) const;
33 QPointF calculateGeometryPoint(int index) const;
36 QPointF calculateGeometryPoint(int index) const;
34 QPointF calculateDomainPoint(const QPointF& point) const;
37 QPointF calculateDomainPoint(const QPointF& point) const;
35 QVector<QPointF> calculateGeometryPoints() const;
38 QVector<QPointF> calculateGeometryPoints() const;
39 void mousePressEvent( QGraphicsSceneMouseEvent * event );
36
40
37 private:
41 private:
38 void applyLayout(QVector<QPointF>& points);
42 void applyLayout(QVector<QPointF>& points);
@@ -50,6 +54,7 private:
50 QVector<QPointF> m_points;
54 QVector<QPointF> m_points;
51
55
52 friend class XYAnimation;
56 friend class XYAnimation;
57 friend class AreaChartItem;
53
58
54 };
59 };
55
60
General Comments 0
You need to be logged in to leave comments. Login now