##// END OF EJS Templates
Added hovered support to areachart. Might need to be re-thought
Marek Rosa -
r2264:6a6c0b791c1a
parent child
Show More
@@ -1,152 +1,168
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "areachartitem_p.h"
22 22 #include "qareaseries.h"
23 23 #include "qareaseries_p.h"
24 24 #include "qlineseries.h"
25 25 #include "chartpresenter_p.h"
26 26 #include "domain_p.h"
27 27 #include <QPainter>
28 28 #include <QGraphicsSceneMouseEvent>
29 29 #include <QDebug>
30 30
31 31
32 32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33 33
34 34 //TODO: optimize : remove points which are not visible
35 35
36 36 AreaChartItem::AreaChartItem(QAreaSeries *areaSeries, ChartPresenter *presenter)
37 37 : ChartItem(presenter),
38 38 m_series(areaSeries),
39 39 m_upper(0),
40 40 m_lower(0),
41 41 m_pointsVisible(false)
42 42 {
43 setAcceptHoverEvents(true);
43 44 setZValue(ChartPresenter::LineChartZValue);
44 45 m_upper = new AreaBoundItem(this, m_series->upperSeries(), presenter);
45 46 if (m_series->lowerSeries())
46 47 m_lower = new AreaBoundItem(this, m_series->lowerSeries(), presenter);
47 48
48 49 QObject::connect(m_series->d_func(), SIGNAL(updated()), this, SLOT(handleUpdated()));
49 50 QObject::connect(m_series, SIGNAL(visibleChanged()), this, SLOT(handleUpdated()));
50 51 QObject::connect(m_series, SIGNAL(opacityChanged()), this, SLOT(handleUpdated()));
51 52 QObject::connect(this, SIGNAL(clicked(QPointF)), areaSeries, SIGNAL(clicked(QPointF)));
53 QObject::connect(this, SIGNAL(hovered(QPointF,bool)), areaSeries, SIGNAL(hovered(QPointF,bool)));
52 54
53 55 handleUpdated();
54 56 }
55 57
56 58 AreaChartItem::~AreaChartItem()
57 59 {
58 60 delete m_upper;
59 61 delete m_lower;
60 62 }
61 63
62 64 QRectF AreaChartItem::boundingRect() const
63 65 {
64 66 return m_rect;
65 67 }
66 68
67 69 QPainterPath AreaChartItem::shape() const
68 70 {
69 71 return m_path;
70 72 }
71 73
72 74 void AreaChartItem::updatePath()
73 75 {
74 76 QPainterPath path;
75 77
76 78 path = m_upper->path();
77 79
78 80 if (m_lower) {
79 81 path.connectPath(m_lower->path().toReversed());
80 82 } else {
81 83 QPointF first = path.pointAtPercent(0);
82 84 QPointF last = path.pointAtPercent(1);
83 85 path.lineTo(last.x(), m_clipRect.bottom());
84 86 path.lineTo(first.x(), m_clipRect.bottom());
85 87 }
86 88 path.closeSubpath();
87 89 prepareGeometryChange();
88 90 m_path = path;
89 91 m_rect = path.boundingRect();
90 92 update();
91 93 }
92 94
93 95 void AreaChartItem::handleUpdated()
94 96 {
95 97 setVisible(m_series->isVisible());
96 98 m_pointsVisible = m_series->pointsVisible();
97 99 m_linePen = m_series->pen();
98 100 m_brush = m_series->brush();
99 101 m_pointPen = m_series->pen();
100 102 m_pointPen.setWidthF(2 * m_pointPen.width());
101 103 setOpacity(m_series->opacity());
102 104
103 105 update();
104 106 }
105 107
106 108 void AreaChartItem::handleDomainUpdated()
107 109 {
108 110 m_upper->setDomain(domain());
109 111 m_upper->handleDomainUpdated();
110 112 if (m_lower) {
111 113 m_lower->setDomain(domain());
112 114 m_lower->handleDomainUpdated();
113 115 }
114 116 }
115 117
116 118 void AreaChartItem::handleGeometryChanged(const QRectF &rect)
117 119 {
118 120 m_clipRect = rect.translated(-rect.topLeft());
119 121 setPos(rect.topLeft());
120 122 m_upper->handleGeometryChanged(rect);
121 123 if (m_lower)
122 124 m_lower->handleGeometryChanged(rect);
123 125 }
124 126
125 127 void AreaChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
126 128 {
127 129 Q_UNUSED(widget)
128 130 Q_UNUSED(option)
129 131
130 132 painter->save();
131 133 painter->setPen(m_linePen);
132 134 painter->setBrush(m_brush);
133 135 painter->setClipRect(m_clipRect);
134 136 painter->drawPath(m_path);
135 137 if (m_pointsVisible) {
136 138 painter->setPen(m_pointPen);
137 139 painter->drawPoints(m_upper->geometryPoints());
138 140 if (m_lower)
139 141 painter->drawPoints(m_lower->geometryPoints());
140 142 }
141 143 painter->restore();
142 144 }
143 145
144 146 void AreaChartItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
145 147 {
146 148 emit clicked(m_upper->calculateDomainPoint(event->pos()));
147 149 ChartItem::mousePressEvent(event);
148 150 }
149 151
152 void AreaChartItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
153 {
154 emit hovered(m_upper->calculateDomainPoint(event->pos()), true);
155 event->accept();
156 // QGraphicsItem::hoverEnterEvent(event);
157 }
158
159 void AreaChartItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
160 {
161 emit hovered(m_upper->calculateDomainPoint(event->pos()), false);
162 event->accept();
163 // QGraphicsItem::hoverEnterEvent(event);
164 }
165
150 166 #include "moc_areachartitem_p.cpp"
151 167
152 168 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,106 +1,109
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 // W A R N I N G
22 22 // -------------
23 23 //
24 24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 25 // implementation detail. This header file may change from version to
26 26 // version without notice, or even be removed.
27 27 //
28 28 // We mean it.
29 29
30 30 #ifndef AREACHARTITEM_H
31 31 #define AREACHARTITEM_H
32 32
33 33 #include "qchartglobal.h"
34 34 #include "linechartitem_p.h"
35 35 #include <QPen>
36 36
37 37 QTCOMMERCIALCHART_BEGIN_NAMESPACE
38 38
39 39 class QAreaSeries;
40 40 class AreaChartItem;
41 41
42 42 class AreaChartItem : public ChartItem
43 43 {
44 44 Q_OBJECT
45 45 public:
46 46 AreaChartItem(QAreaSeries *areaSeries, ChartPresenter *presenter);
47 47 ~AreaChartItem();
48 48
49 49 //from QGraphicsItem
50 50 QRectF boundingRect() const;
51 51 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
52 52 QPainterPath shape() const;
53 53
54 54 LineChartItem *upperLineItem() const { return m_upper; }
55 55 LineChartItem *lowerLineItem() const { return m_lower; }
56 56
57 57 void updatePath();
58 58
59 59 protected:
60 60 void mousePressEvent(QGraphicsSceneMouseEvent *event);
61 void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
62 void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
61 63
62 64 Q_SIGNALS:
63 void clicked(const QPointF &point);
65 void clicked(const QPointF &point);
66 void hovered(const QPointF &point, bool state);
64 67
65 68 public Q_SLOTS:
66 69 void handleUpdated();
67 70 void handleDomainUpdated();
68 71 void handleGeometryChanged(const QRectF &size);
69 72
70 73 private:
71 74 QAreaSeries *m_series;
72 75 LineChartItem *m_upper;
73 76 LineChartItem *m_lower;
74 77 QPainterPath m_path;
75 78 QRectF m_rect;
76 79 QRectF m_clipRect;
77 80 QPen m_linePen;
78 81 QPen m_pointPen;
79 82 QBrush m_brush;
80 83 bool m_pointsVisible;
81 84
82 85 };
83 86
84 87 class AreaBoundItem : public LineChartItem
85 88 {
86 89 public:
87 90 AreaBoundItem(AreaChartItem *item, QLineSeries *lineSeries, ChartPresenter *presenter)
88 91 : LineChartItem(lineSeries, 0), m_item(item)
89 92 {
90 93 setPresenter(presenter);
91 94 }
92 95 ~AreaBoundItem() {}
93 96
94 97 void updateGeometry()
95 98 {
96 99 LineChartItem::updateGeometry();
97 100 m_item->updatePath();
98 101 }
99 102
100 103 private:
101 104 AreaChartItem *m_item;
102 105 };
103 106
104 107 QTCOMMERCIALCHART_END_NAMESPACE
105 108
106 109 #endif
@@ -1,85 +1,86
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #ifndef QAREASERIES_H
22 22 #define QAREASERIES_H
23 23
24 24 #include <qchartglobal.h>
25 25 #include <qabstractseries.h>
26 26 #include <QPen>
27 27 #include <QBrush>
28 28
29 29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 30 class QLineSeries;
31 31 class QAreaSeriesPrivate;
32 32
33 33 class QTCOMMERCIALCHART_EXPORT QAreaSeries : public QAbstractSeries
34 34 {
35 35 Q_OBJECT
36 36 Q_PROPERTY(QLineSeries *upperSeries READ upperSeries)
37 37 Q_PROPERTY(QLineSeries *lowerSeries READ lowerSeries)
38 38 Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
39 39 Q_PROPERTY(QColor borderColor READ borderColor WRITE setBorderColor NOTIFY borderColorChanged)
40 40
41 41 public:
42 42 explicit QAreaSeries(QObject *parent = 0);
43 43 explicit QAreaSeries(QLineSeries *upperSeries, QLineSeries *lowerSeries = 0);
44 44 ~QAreaSeries();
45 45
46 46 public:
47 47 QAbstractSeries::SeriesType type() const;
48 48
49 49 void setUpperSeries(QLineSeries *series);
50 50 QLineSeries *upperSeries() const;
51 51 void setLowerSeries(QLineSeries *series);
52 52 QLineSeries *lowerSeries() const;
53 53
54 54 void setPen(const QPen &pen);
55 55 QPen pen() const;
56 56
57 57 void setBrush(const QBrush &brush);
58 58 QBrush brush() const;
59 59
60 60 void setColor(const QColor &color);
61 61 QColor color() const;
62 62
63 63 void setBorderColor(const QColor &color);
64 64 QColor borderColor() const;
65 65
66 66 void setPointsVisible(bool visible = true);
67 67 bool pointsVisible() const;
68 68
69 69 Q_SIGNALS:
70 void clicked(const QPointF &point);
70 void clicked(const QPointF &point);
71 void hovered(const QPointF &point, bool state);
71 72 void selected();
72 73 void colorChanged(QColor color);
73 74 void borderColorChanged(QColor color);
74 75
75 76 private:
76 77 Q_DECLARE_PRIVATE(QAreaSeries)
77 78 Q_DISABLE_COPY(QAreaSeries)
78 79 friend class AreaLegendMarker;
79 80 friend class AreaChartItem;
80 81 friend class QAreaLegendMarkerPrivate;
81 82 };
82 83
83 84 QTCOMMERCIALCHART_END_NAMESPACE
84 85
85 86 #endif // QAREASERIES_H
General Comments 0
You need to be logged in to leave comments. Login now