##// END OF EJS Templates
Drop shadow in scatter
Tero Ahola -
r346:d59509c2ed03
parent child
Show More
@@ -1,134 +1,129
1 1 #include "scatterpresenter_p.h"
2 2 #include "qscatterseries.h"
3 3 #include <QPen>
4 4 #include <QPainter>
5 5 #include <QGraphicsScene>
6 6 #include <QGraphicsSceneMouseEvent>
7 #include <QGraphicsDropShadowEffect>
7 8 #include <QDebug>
8 9 #include <QTime>
9 10
10 11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11 12
12 13 ScatterPresenter::ScatterPresenter(QScatterSeries *series, QGraphicsObject *parent) :
13 14 ChartItem(parent),
14 15 m_series(series),
15 16 m_boundingRect(),
16 //m_markerColor(QColor()),
17 // m_markerColor(QColor(255, 0, 0)),
18 17 m_visibleChartArea()
19 18 {
20 19 if (parent)
21 20 m_boundingRect = parent->boundingRect();
22 21
23 22 if (series) {
24 23 connect(series, SIGNAL(changed()), this, SLOT(handleModelChanged()));
25 24 }
25
26 QGraphicsDropShadowEffect *dropShadow = new QGraphicsDropShadowEffect();
27 dropShadow->setOffset(2.0);
28 setGraphicsEffect(dropShadow);
26 29 }
27 30
28 31 void ScatterPresenter::handleDomainChanged(const Domain& domain)
29 32 {
30 33 m_visibleChartArea = domain;
31 34 changeGeometry();
32 35 }
33 36
34 37 void ScatterPresenter::handleGeometryChanged(const QRectF& rect)
35 38 {
36 39 m_boundingRect = rect;
37 40 changeGeometry();
38 41 }
39 42
40 43 void ScatterPresenter::handleModelChanged()
41 44 {
42 45 // TODO: more fine grained modelChanged signaling
43 46 changeGeometry();
44 47 }
45 48
46 49 void ScatterPresenter::paint(QPainter *painter, const QStyleOptionGraphicsItem */*option*/, QWidget */*widget*/)
47 50 {
48 51 painter->save();
49 52 painter->setClipRect(m_boundingRect);
50 53
51 54 // Paint the shape
52 55 // The custom settings in series override those defined by the theme
53 56 QPen pen = m_markerPen;
54 57 if (m_series->markerPen().color().isValid())
55 58 pen = m_series->markerPen();
56 59 if (m_series->markerBrush().color().isValid())
57 60 painter->setBrush(m_series->markerBrush());
58 61 else
59 62 painter->setBrush(m_markerBrush);
60 63 painter->setPen(pen);
61 64 painter->drawPath(m_path);
62 65
63 // TODO: how to draw a drop shadow?
64 QPen dropShadowPen(QColor(0, 0, 0, 70));
65 dropShadowPen.setWidth(3);
66 painter->setPen(dropShadowPen);
67 painter->setBrush(Qt::NoBrush);
68 painter->setRenderHint(QPainter::Antialiasing);
69 painter->drawPath(m_path.translated(2, 2));
70
71 66 painter->restore();
72 67 }
73 68
74 69 void ScatterPresenter::mousePressEvent(QGraphicsSceneMouseEvent *event)
75 70 {
76 71 qDebug() << "ScatterPresenter::mousePressEvent" << event << " cont: "
77 72 << m_path.contains(event->lastPos());
78 73
79 74 if (m_path.contains(event->lastPos()))
80 75 emit clicked();
81 76 }
82 77
83 78 void ScatterPresenter::changeGeometry()
84 79 {
85 80 if (m_boundingRect.isValid()) {
86 81 prepareGeometryChange();
87 82 qreal scalex = m_boundingRect.width() / m_visibleChartArea.spanX();
88 83 qreal scaley = m_boundingRect.height() / m_visibleChartArea.spanY();
89 84
90 85 int shape = m_series->markerShape();
91 86 m_path = QPainterPath();
92 87
93 88 foreach (QPointF point, m_series->data()) {
94 89 // Convert relative coordinates to absolute pixel coordinates that can be used for drawing
95 90 qreal x = m_boundingRect.left() + point.x() * scalex - m_visibleChartArea.m_minX * scalex;
96 91 qreal y = m_boundingRect.bottom() - point.y() * scaley + m_visibleChartArea.m_minY * scaley;
97 92
98 93 if (scene()->width() > x && scene()->height() > y) {
99 94 switch (shape) {
100 95 case QScatterSeries::MarkerShapeDefault:
101 96 // Fallthrough, defaults to circle
102 97 case QScatterSeries::MarkerShapeCircle:
103 98 m_path.addEllipse(x, y, 9, 9);
104 99 break;
105 100 case QScatterSeries::MarkerShapePoint:
106 101 m_path.addEllipse(x, y, 2, 2);
107 102 break;
108 103 case QScatterSeries::MarkerShapeRectangle:
109 104 m_path.addRect(x, y, 9, 9);
110 105 break;
111 106 case QScatterSeries::MarkerShapeTiltedRectangle: {
112 107 // TODO:
113 108 // static const QPointF points[4] = {
114 109 // QPointF(-1.0 + x, 0.0 + y),
115 110 // QPointF(0.0 + x, 1.0 + y),
116 111 // QPointF(1.0 + x, 0.0 + y),
117 112 // QPointF(0.0 + x, -1.0 + y)
118 113 // };
119 114 //m_path.addPolygon(QPolygon(4, &points));
120 115 break;
121 116 }
122 117 default:
123 118 // TODO: implement the rest of the shapes
124 119 Q_ASSERT(false);
125 120 break;
126 121 }
127 122 }
128 123 }
129 124 }
130 125 }
131 126
132 127 #include "moc_scatterpresenter_p.cpp"
133 128
134 129 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now