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