##// 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 60 m_series<<series3;
61 61 m_titles<<chartTitle()+": AreaChart";
62 62 //![3]
63
63 64 addSeries(series0);
64 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 71 m_timer.start();
67 72 }
68 73
@@ -84,3 +89,8 void ChartView::handleTimeout()
84 89 setChartTitle(m_titles.at(m_index));
85 90 }
86 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 17 public slots:
18 18 void handleTimeout();
19 void handlePointClicked(const QPointF& point);
19 20
20 21 private:
21 22 QTimer m_timer;
@@ -3,6 +3,7
3 3 #include "qlineseries.h"
4 4 #include "chartpresenter_p.h"
5 5 #include <QPainter>
6 #include <QGraphicsSceneMouseEvent>
6 7
7 8
8 9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
@@ -22,6 +23,7 m_pointsVisible(false)
22 23 }
23 24
24 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 28 handleUpdated();
27 29 }
@@ -108,6 +110,11 void AreaChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *opt
108 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 118 #include "moc_areachartitem_p.cpp"
112 119
113 120 QTCOMMERCIALCHART_END_NAMESPACE
@@ -26,6 +26,13 public:
26 26 LineChartItem* lowerLineItem() const { return m_lower ;}
27 27
28 28 void updatePath();
29
30 protected:
31 void mousePressEvent( QGraphicsSceneMouseEvent * event );
32
33 signals:
34 void clicked(const QPointF& point);
35
29 36 public slots:
30 37 void handleUpdated();
31 38 void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY);
@@ -92,7 +92,10 QAreaSeries::~QAreaSeries()
92 92 */
93 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 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 112 Sets if data points are \a visible and should be drawn on line.
107 113 */
108 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 122 #include "moc_qareaseries.cpp"
@@ -34,6 +34,7 public: // from QChartSeries
34 34
35 35 signals:
36 36 void updated();
37 void clicked(const QPointF& point);
37 38
38 39 private:
39 40 QBrush m_brush;
@@ -2,7 +2,6
2 2 #include "qlineseries.h"
3 3 #include "chartpresenter_p.h"
4 4 #include <QPainter>
5 #include <QGraphicsSceneMouseEvent>
6 5
7 6
8 7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
@@ -15,7 +14,6 m_pointsVisible(false)
15 14 {
16 15 setZValue(ChartPresenter::LineChartZValue);
17 16 QObject::connect(series,SIGNAL(updated()),this,SLOT(handleUpdated()));
18 QObject::connect(this,SIGNAL(clicked(const QPointF&)),series,SIGNAL(clicked(const QPointF&)));
19 17 handleUpdated();
20 18 }
21 19
@@ -78,11 +76,6 void LineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *opt
78 76 painter->restore();
79 77 }
80 78
81 void LineChartItem::mousePressEvent( QGraphicsSceneMouseEvent * event )
82 {
83 emit clicked(calculateDomainPoint(event->pos()));
84 }
85
86 79 #include "moc_linechartitem_p.cpp"
87 80
88 81 QTCOMMERCIALCHART_END_NAMESPACE
@@ -24,12 +24,8 public:
24 24 public slots:
25 25 void handleUpdated();
26 26
27 signals:
28 void clicked(const QPointF& point);
29
30 27 protected:
31 28 void setLayout(QVector<QPointF>& points);
32 void mousePressEvent( QGraphicsSceneMouseEvent * event );
33 29
34 30 private:
35 31 QLineSeries* m_series;
@@ -65,7 +65,7 void QLineSeries::setLinePen(const QPen& pen)
65 65 {
66 66 if(pen!=m_pen){
67 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 77 if(m_pointsVisible!=visible){
78 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 93 return debug.space();
94 94 }
95 95
96 #include "moc_qlineseries.cpp"
97
98 96 QTCOMMERCIALCHART_END_NAMESPACE
@@ -11,7 +11,6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11 11
12 12 class QTCOMMERCIALCHART_EXPORT QLineSeries : public QXYSeries
13 13 {
14 Q_OBJECT
15 14 public:
16 15 QLineSeries(QObject* parent=0);
17 16 virtual ~QLineSeries();
@@ -22,9 +21,6 public:
22 21 void setPointsVisible(bool visible);
23 22 bool pointsVisible() const {return m_pointsVisible;}
24 23
25 signals:
26 void clicked(const QPointF& point);
27
28 24 public: // from QChartSeries
29 25 virtual QSeriesType type() const {return QSeries::SeriesTypeLine;}
30 26 friend QDebug operator<< (QDebug d, const QLineSeries series);
@@ -124,6 +124,7 void QChart::setChartTitle(const QString& title)
124 124 {
125 125 createChartTitleItem();
126 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 146 createChartTitleItem();
146 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 156 createChartTitleItem();
155 157 m_titleItem->setBrush(brush);
158 updateLayout();
156 159 }
157 160
158 161 /*!
@@ -197,6 +200,7 int QChart::margin() const
197 200 void QChart::setMargin(int margin)
198 201 {
199 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 288 m_rect = QRectF(QPoint(0,0),event->newSize());
285 QRectF rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
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
289 updateLayout();
298 290 QGraphicsWidget::resizeEvent(event);
299 291 update();
300 292 }
@@ -328,6 +320,23 void QChart::scroll(int dx,int dy)
328 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 340 #include "moc_qchart.cpp"
332 341
333 342 QTCOMMERCIALCHART_END_NAMESPACE
@@ -85,6 +85,7 protected:
85 85 private:
86 86 inline void createChartBackgroundItem();
87 87 inline void createChartTitleItem();
88 void updateLayout();
88 89
89 90 private:
90 91 Q_DISABLE_COPY(QChart)
@@ -96,8 +96,4 void QScatterSeries::setSize(qreal size)
96 96 emit updated();
97 97 }
98 98
99
100
101 #include "moc_qscatterseries.cpp"
102
103 99 QTCOMMERCIALCHART_END_NAMESPACE
@@ -11,7 +11,6 class QScatterSeriesPrivate;
11 11
12 12 class QTCOMMERCIALCHART_EXPORT QScatterSeries : public QXYSeries
13 13 {
14 Q_OBJECT
15 14
16 15 public:
17 16 enum MarkerShape {
@@ -32,9 +31,6 public:
32 31 qreal size() const;
33 32 void setSize(qreal size);
34 33
35 signals:
36 void clicked(const QPointF& point);
37
38 34 private:
39 35 MarkerShape m_shape;
40 36 qreal m_size;
@@ -24,7 +24,6 ScatterChartItem::ScatterChartItem(QScatterSeries *series, QGraphicsItem *parent
24 24 Q_ASSERT(series);
25 25
26 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 28 setZValue(ChartPresenter::ScatterSeriesZValue);
30 29 setFlags(QGraphicsItem::ItemHasNoContents);
@@ -87,7 +86,7 void ScatterChartItem::deletePoints(int count)
87 86
88 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 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 182 #include "moc_scatterchartitem_p.cpp"
180 183
181 184 QTCOMMERCIALCHART_END_NAMESPACE
@@ -27,9 +27,6 public:
27 27
28 28 void markerSelected(Marker* item);
29 29
30 signals:
31 void clicked(const QPointF& point);
32
33 30 public slots:
34 31 void handleUpdated();
35 32
@@ -39,6 +36,7 private:
39 36
40 37 protected:
41 38 void setLayout(QVector<QPointF>& points);
39 void mousePressEvent( QGraphicsSceneMouseEvent * event );
42 40
43 41 private:
44 42 QScatterSeries *m_series;
@@ -18,8 +18,6 public:
18 18 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
19 19 QPainterPath shape() const;
20 20
21 void setPointsVisible(bool visible);
22
23 21 public slots:
24 22 void handleUpdated();
25 23
@@ -51,6 +51,7 public:
51 51 void modelDataRemoved(QModelIndex parent, int start, int end);
52 52
53 53 signals:
54 void clicked(const QPointF& point);
54 55 void updated();
55 56 void pointReplaced(int index);
56 57 void pointRemoved(int index);
@@ -3,6 +3,7
3 3 #include "chartpresenter_p.h"
4 4 #include "chartanimator_p.h"
5 5 #include <QPainter>
6 #include <QGraphicsSceneMouseEvent>
6 7
7 8
8 9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
@@ -19,7 +20,7 m_series(series)
19 20 QObject::connect(series,SIGNAL(pointReplaced(int)),this,SLOT(handlePointReplaced(int)));
20 21 QObject::connect(series,SIGNAL(pointAdded(int)),this,SLOT(handlePointAdded(int)));
21 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 26 QPointF XYChartItem::calculateGeometryPoint(const QPointF& point) const
@@ -151,6 +152,11 bool XYChartItem::isEmpty()
151 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 160 #include "moc_xychartitem_p.cpp"
155 161
156 162 QTCOMMERCIALCHART_END_NAMESPACE
@@ -27,12 +27,16 public slots:
27 27 void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY);
28 28 void handleGeometryChanged(const QRectF& size);
29 29
30 signals:
31 void clicked(const QPointF& point);
32
30 33 protected:
31 34 virtual void setLayout(QVector<QPointF>& points);
32 35 QPointF calculateGeometryPoint(const QPointF& point) const;
33 36 QPointF calculateGeometryPoint(int index) const;
34 37 QPointF calculateDomainPoint(const QPointF& point) const;
35 38 QVector<QPointF> calculateGeometryPoints() const;
39 void mousePressEvent( QGraphicsSceneMouseEvent * event );
36 40
37 41 private:
38 42 void applyLayout(QVector<QPointF>& points);
@@ -50,6 +54,7 private:
50 54 QVector<QPointF> m_points;
51 55
52 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