##// END OF EJS Templates
Added clicked signal to scatter...
Tero Ahola -
r316:101197172c50
parent child
Show More
@@ -158,6 +158,7 void ChartPresenter::handleSeriesAdded(QChartSeries* series)
158 case QChartSeries::SeriesTypeScatter: {
158 case QChartSeries::SeriesTypeScatter: {
159 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
159 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
160 ScatterPresenter *scatterPresenter = new ScatterPresenter(scatterSeries, m_chart);
160 ScatterPresenter *scatterPresenter = new ScatterPresenter(scatterSeries, m_chart);
161 QObject::connect(scatterPresenter, SIGNAL(clicked()), scatterSeries, SIGNAL(clicked()));
161 QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)),
162 QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)),
162 scatterPresenter, SLOT(handleGeometryChanged(const QRectF&)));
163 scatterPresenter, SLOT(handleGeometryChanged(const QRectF&)));
163 m_chartTheme->decorate(scatterPresenter, scatterSeries, m_chartItems.count());
164 m_chartTheme->decorate(scatterPresenter, scatterSeries, m_chartItems.count());
@@ -3,6 +3,7
3 #include <QPen>
3 #include <QPen>
4 #include <QPainter>
4 #include <QPainter>
5 #include <QGraphicsScene>
5 #include <QGraphicsScene>
6 #include <QGraphicsSceneMouseEvent>
6 #include <QDebug>
7 #include <QDebug>
7 #include <QTime>
8 #include <QTime>
8
9
@@ -44,52 +45,11 void ScatterPresenter::handleModelChanged()
44
45
45 void ScatterPresenter::paint(QPainter *painter, const QStyleOptionGraphicsItem */*option*/, QWidget */*widget*/)
46 void ScatterPresenter::paint(QPainter *painter, const QStyleOptionGraphicsItem */*option*/, QWidget */*widget*/)
46 {
47 {
47 // TODO: Optimization: avoid setting on every paint method call?
48 // The custom settings in series override those defined by the theme
49 int shape = m_series->markerShape();
50
51 painter->save();
48 painter->save();
52 painter->setClipRect(m_boundingRect);
49 painter->setClipRect(m_boundingRect);
53
50
54 // Paint dropshadow
55 QPen dropShadowPen(QColor(0, 0, 0, 70));
56 dropShadowPen.setWidth(3);
57 painter->setPen(dropShadowPen);
58 painter->setBrush(Qt::NoBrush);
59 painter->setRenderHint(QPainter::Antialiasing);
60 for (int i(0); i < m_scenex.count() && i < m_sceney.count(); i++) {
61 if (scene()->width() > m_scenex.at(i) && scene()->height() > m_sceney.at(i))
62 switch (shape) {
63 case QScatterSeries::MarkerShapeDefault:
64 // Fallthrough, defaults to circle
65 case QScatterSeries::MarkerShapeCircle:
66 painter->drawChord(m_scenex.at(i) + 2, m_sceney.at(i) + 2, 9, 9, 0, 5760);
67 break;
68 case QScatterSeries::MarkerShapePoint:
69 //painter->drawPoint(m_scenex.at(i), m_sceney.at(i));
70 break;
71 case QScatterSeries::MarkerShapeRectangle:
72 painter->drawRect(m_scenex.at(i) + 2, m_sceney.at(i) + 2, 8, 8);
73 break;
74 case QScatterSeries::MarkerShapeTiltedRectangle: {
75 // TODO:
76 static const QPointF points[4] = {
77 QPointF(-1.0 + m_scenex.at(i), 0.0 + m_sceney.at(i)),
78 QPointF(0.0 + m_scenex.at(i), 1.0 + m_sceney.at(i)),
79 QPointF(1.0 + m_scenex.at(i), 0.0 + m_sceney.at(i)),
80 QPointF(0.0 + m_scenex.at(i), -1.0 + m_sceney.at(i))
81 };
82 painter->drawPolygon(points, 4);
83 break;
84 }
85 default:
86 // TODO: implement the rest of the shapes
87 Q_ASSERT(false);
88 break;
89 }
90 }
91
92 // Paint the shape
51 // Paint the shape
52 // The custom settings in series override those defined by the theme
93 QPen pen = m_markerPen;
53 QPen pen = m_markerPen;
94 if (m_series->markerPen().color().isValid())
54 if (m_series->markerPen().color().isValid())
95 pen = m_series->markerPen();
55 pen = m_series->markerPen();
@@ -98,61 +58,73 void ScatterPresenter::paint(QPainter *painter, const QStyleOptionGraphicsItem *
98 else
58 else
99 painter->setBrush(m_markerBrush);
59 painter->setBrush(m_markerBrush);
100 painter->setPen(pen);
60 painter->setPen(pen);
101 painter->setRenderHint(QPainter::Antialiasing, false);
61 painter->drawPath(m_path);
102 for (int i(0); i < m_scenex.count() && i < m_sceney.count(); i++) {
62
103 if (scene()->width() > m_scenex.at(i) && scene()->height() > m_sceney.at(i))
63 // TODO: how to draw a drop shadow?
104 switch (shape) {
64 QPen dropShadowPen(QColor(0, 0, 0, 70));
105 case QScatterSeries::MarkerShapeDefault:
65 dropShadowPen.setWidth(3);
106 // Fallthrough, defaults to circle
66 painter->setPen(dropShadowPen);
107 case QScatterSeries::MarkerShapeCircle:
67 painter->setBrush(Qt::NoBrush);
108 painter->drawChord(m_scenex.at(i), m_sceney.at(i), 9, 9, 0, 5760);
68 painter->setRenderHint(QPainter::Antialiasing);
109 break;
69 painter->drawPath(m_path.translated(2, 2));
110 case QScatterSeries::MarkerShapePoint:
111 painter->drawPoint(m_scenex.at(i), m_sceney.at(i));
112 break;
113 case QScatterSeries::MarkerShapeRectangle:
114 painter->drawRect(m_scenex.at(i), m_sceney.at(i), 9, 9);
115 break;
116 case QScatterSeries::MarkerShapeTiltedRectangle: {
117 // TODO:
118 static const QPointF points[4] = {
119 QPointF(-1.0 + m_scenex.at(i), 0.0 + m_sceney.at(i)),
120 QPointF(0.0 + m_scenex.at(i), 1.0 + m_sceney.at(i)),
121 QPointF(1.0 + m_scenex.at(i), 0.0 + m_sceney.at(i)),
122 QPointF(0.0 + m_scenex.at(i), -1.0 + m_sceney.at(i))
123 };
124 painter->drawPolygon(points, 4);
125 break;
126 }
127 default:
128 // TODO: implement the rest of the shapes
129 Q_ASSERT(false);
130 break;
131 }
132 }
133
70
134 painter->restore();
71 painter->restore();
135 }
72 }
136
73
137 void ScatterPresenter::mousePressEvent(QGraphicsSceneMouseEvent *event)
74 void ScatterPresenter::mousePressEvent(QGraphicsSceneMouseEvent *event)
138 {
75 {
139 qDebug() << "ScatterPresenter::mousePressEvent" << event;
76 qDebug() << "ScatterPresenter::mousePressEvent" << event << " cont: "
77 << m_path.contains(event->lastPos());
78
79 if (m_path.contains(event->lastPos()))
80 emit clicked();
140 }
81 }
141
82
142 void ScatterPresenter::changeGeometry()
83 void ScatterPresenter::changeGeometry()
143 {
84 {
144 if (m_boundingRect.isValid()) {
85 if (m_boundingRect.isValid()) {
145
146 prepareGeometryChange();
86 prepareGeometryChange();
147 qreal scalex = m_boundingRect.width() / m_visibleChartArea.spanX();
87 qreal scalex = m_boundingRect.width() / m_visibleChartArea.spanX();
148 qreal scaley = m_boundingRect.height() / m_visibleChartArea.spanY();
88 qreal scaley = m_boundingRect.height() / m_visibleChartArea.spanY();
149 m_scenex.clear();
150 m_sceney.clear();
151
89
152 // Convert relative coordinates to absolute pixel coordinates that can be used for drawing
90 int shape = m_series->markerShape();
91 m_path = QPainterPath();
92
153 foreach (QPointF point, m_series->data()) {
93 foreach (QPointF point, m_series->data()) {
154 m_scenex.append(m_boundingRect.left() + point.x() * scalex - m_visibleChartArea.m_minX * scalex);
94 // Convert relative coordinates to absolute pixel coordinates that can be used for drawing
155 m_sceney.append(m_boundingRect.bottom() - point.y() * scaley + m_visibleChartArea.m_minY * scaley);
95 qreal x = m_boundingRect.left() + point.x() * scalex - m_visibleChartArea.m_minX * scalex;
96 qreal y = m_boundingRect.bottom() - point.y() * scaley + m_visibleChartArea.m_minY * scaley;
97
98 if (scene()->width() > x && scene()->height() > y) {
99 switch (shape) {
100 case QScatterSeries::MarkerShapeDefault:
101 // Fallthrough, defaults to circle
102 case QScatterSeries::MarkerShapeCircle:
103 m_path.addEllipse(x, y, 9, 9);
104 break;
105 case QScatterSeries::MarkerShapePoint:
106 m_path.addEllipse(x, y, 2, 2);
107 break;
108 case QScatterSeries::MarkerShapeRectangle:
109 m_path.addRect(x, y, 9, 9);
110 break;
111 case QScatterSeries::MarkerShapeTiltedRectangle: {
112 // TODO:
113 // static const QPointF points[4] = {
114 // QPointF(-1.0 + x, 0.0 + y),
115 // QPointF(0.0 + x, 1.0 + y),
116 // QPointF(1.0 + x, 0.0 + y),
117 // QPointF(0.0 + x, -1.0 + y)
118 // };
119 //m_path.addPolygon(QPolygon(4, &points));
120 break;
121 }
122 default:
123 // TODO: implement the rest of the shapes
124 Q_ASSERT(false);
125 break;
126 }
127 }
156 }
128 }
157 }
129 }
158 }
130 }
@@ -21,11 +21,13 public:
21 explicit ScatterPresenter(QScatterSeries *series, QGraphicsObject *parent = 0);
21 explicit ScatterPresenter(QScatterSeries *series, QGraphicsObject *parent = 0);
22
22
23 public: // from ChartItem
23 public: // from ChartItem
24 QRectF boundingRect() const { return m_boundingRect; }
24 QRectF boundingRect() const { return m_path.boundingRect(); }
25 QPainterPath shape() const { return m_path; }
25 void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *);
26 void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *);
26 void mousePressEvent (QGraphicsSceneMouseEvent * event);
27 void mousePressEvent (QGraphicsSceneMouseEvent * event);
27
28
28 signals:
29 Q_SIGNALS:
30 void clicked();
29
31
30 public Q_SLOTS:
32 public Q_SLOTS:
31 void handleDomainChanged(const Domain& domain);
33 void handleDomainChanged(const Domain& domain);
@@ -37,11 +39,10 public:
37
39
38 QScatterSeries *m_series;
40 QScatterSeries *m_series;
39 QRectF m_boundingRect;
41 QRectF m_boundingRect;
40 QList<qreal> m_scenex;
41 QList<qreal> m_sceney;
42 Domain m_visibleChartArea;
42 Domain m_visibleChartArea;
43 QPen m_markerPen;
43 QPen m_markerPen;
44 QBrush m_markerBrush;
44 QBrush m_markerBrush;
45 QPainterPath m_path;
45 };
46 };
46
47
47 QTCOMMERCIALCHART_END_NAMESPACE
48 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now