##// END OF EJS Templates
Add save and restore to boxwhiskers paint...
Miikka Heikkinen -
r2586:ab9ce3bf2004
parent child
Show More
@@ -1,192 +1,194
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2013 Digia Plc
3 ** Copyright (C) 2013 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Enterprise Charts Add-on.
7 ** This file is part of the Qt Enterprise Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Enterprise licenses may use this file in
10 ** Licensees holding valid Qt Enterprise licenses may use this file in
11 ** accordance with the Qt Enterprise License Agreement provided with the
11 ** accordance with the Qt Enterprise License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "boxwhiskers_p.h"
21 #include "boxwhiskers_p.h"
22 #include <QPainter>
22 #include <QPainter>
23 #include <QWidget>
23 #include <QWidget>
24
24
25 QTCOMMERCIALCHART_BEGIN_NAMESPACE
25 QTCOMMERCIALCHART_BEGIN_NAMESPACE
26
26
27 BoxWhiskers::BoxWhiskers(QBoxSet *set, AbstractDomain *domain, QGraphicsObject *parent) :
27 BoxWhiskers::BoxWhiskers(QBoxSet *set, AbstractDomain *domain, QGraphicsObject *parent) :
28 QGraphicsObject(parent),
28 QGraphicsObject(parent),
29 m_boxSet(set),
29 m_boxSet(set),
30 m_domain(domain)
30 m_domain(domain)
31 {
31 {
32 setAcceptHoverEvents(true);
32 setAcceptHoverEvents(true);
33 setAcceptedMouseButtons(Qt::MouseButtonMask);
33 setAcceptedMouseButtons(Qt::MouseButtonMask);
34 }
34 }
35
35
36 BoxWhiskers::~BoxWhiskers()
36 BoxWhiskers::~BoxWhiskers()
37 {
37 {
38 }
38 }
39
39
40 void BoxWhiskers::mousePressEvent(QGraphicsSceneMouseEvent *event)
40 void BoxWhiskers::mousePressEvent(QGraphicsSceneMouseEvent *event)
41 {
41 {
42 Q_UNUSED(event)
42 Q_UNUSED(event)
43 emit clicked(m_boxSet);
43 emit clicked(m_boxSet);
44 }
44 }
45
45
46 void BoxWhiskers::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
46 void BoxWhiskers::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
47 {
47 {
48 Q_UNUSED(event)
48 Q_UNUSED(event)
49 emit hovered(true, m_boxSet);
49 emit hovered(true, m_boxSet);
50 }
50 }
51
51
52 void BoxWhiskers::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
52 void BoxWhiskers::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
53 {
53 {
54 Q_UNUSED(event)
54 Q_UNUSED(event)
55 emit hovered(false, m_boxSet);
55 emit hovered(false, m_boxSet);
56 }
56 }
57
57
58 void BoxWhiskers::setBrush(const QBrush &brush)
58 void BoxWhiskers::setBrush(const QBrush &brush)
59 {
59 {
60 m_brush = brush;
60 m_brush = brush;
61 m_outlinePen.setColor(m_brush.color());
61 m_outlinePen.setColor(m_brush.color());
62 update();
62 update();
63 }
63 }
64
64
65 void BoxWhiskers::setPen(const QPen &pen)
65 void BoxWhiskers::setPen(const QPen &pen)
66 {
66 {
67 qreal widthDiff = pen.widthF() - m_pen.widthF();
67 qreal widthDiff = pen.widthF() - m_pen.widthF();
68 m_boundingRect.adjust(-widthDiff, -widthDiff, widthDiff, widthDiff);
68 m_boundingRect.adjust(-widthDiff, -widthDiff, widthDiff, widthDiff);
69
69
70 m_pen = pen;
70 m_pen = pen;
71 m_medianPen = pen;
71 m_medianPen = pen;
72 m_medianPen.setCapStyle(Qt::FlatCap);
72 m_medianPen.setCapStyle(Qt::FlatCap);
73 m_outlinePen = pen;
73 m_outlinePen = pen;
74 m_outlinePen.setStyle(Qt::SolidLine);
74 m_outlinePen.setStyle(Qt::SolidLine);
75 m_outlinePen.setColor(m_brush.color());
75 m_outlinePen.setColor(m_brush.color());
76
76
77 update();
77 update();
78 }
78 }
79
79
80 void BoxWhiskers::setBoxWidth(const qreal width)
80 void BoxWhiskers::setBoxWidth(const qreal width)
81 {
81 {
82 m_boxWidth = width;
82 m_boxWidth = width;
83 }
83 }
84
84
85 void BoxWhiskers::setLayout(const BoxWhiskersData &data)
85 void BoxWhiskers::setLayout(const BoxWhiskersData &data)
86 {
86 {
87 m_data = data;
87 m_data = data;
88
88
89 updateGeometry(m_domain);
89 updateGeometry(m_domain);
90 update();
90 update();
91 }
91 }
92
92
93 QSizeF BoxWhiskers::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
93 QSizeF BoxWhiskers::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
94 {
94 {
95 Q_UNUSED(which)
95 Q_UNUSED(which)
96 Q_UNUSED(constraint)
96 Q_UNUSED(constraint)
97
97
98 return QSizeF();
98 return QSizeF();
99 }
99 }
100
100
101 void BoxWhiskers::setGeometry(const QRectF &rect)
101 void BoxWhiskers::setGeometry(const QRectF &rect)
102 {
102 {
103 Q_UNUSED(rect)
103 Q_UNUSED(rect)
104 }
104 }
105
105
106 QRectF BoxWhiskers::boundingRect() const
106 QRectF BoxWhiskers::boundingRect() const
107 {
107 {
108 return m_boundingRect;
108 return m_boundingRect;
109 }
109 }
110
110
111 void BoxWhiskers::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
111 void BoxWhiskers::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
112 {
112 {
113 Q_UNUSED(option)
113 Q_UNUSED(option)
114 Q_UNUSED(widget)
114 Q_UNUSED(widget)
115
115
116 painter->save();
116 painter->setBrush(m_brush);
117 painter->setBrush(m_brush);
117 painter->setClipRect(parentItem()->boundingRect());
118 painter->setClipRect(parentItem()->boundingRect());
118 painter->setPen(m_pen);
119 painter->setPen(m_pen);
119 painter->drawPath(m_boxPath);
120 painter->drawPath(m_boxPath);
120 if (!m_boxOutlined)
121 if (!m_boxOutlined)
121 painter->setPen(m_outlinePen);
122 painter->setPen(m_outlinePen);
122 painter->drawRect(m_middleBox);
123 painter->drawRect(m_middleBox);
123 painter->setPen(m_medianPen);
124 painter->setPen(m_medianPen);
124 qreal halfLine = m_pen.widthF() / 2.0;
125 qreal halfLine = m_pen.widthF() / 2.0;
125 painter->drawLine(QLineF(m_geometryLeft - halfLine, m_geometryMedian,
126 painter->drawLine(QLineF(m_geometryLeft - halfLine, m_geometryMedian,
126 m_geometryRight + halfLine, m_geometryMedian));
127 m_geometryRight + halfLine, m_geometryMedian));
128 painter->restore();
127 }
129 }
128
130
129 void BoxWhiskers::updateGeometry(AbstractDomain *domain)
131 void BoxWhiskers::updateGeometry(AbstractDomain *domain)
130 {
132 {
131 m_domain = domain;
133 m_domain = domain;
132
134
133 prepareGeometryChange();
135 prepareGeometryChange();
134
136
135 QPainterPath path;
137 QPainterPath path;
136 m_boxPath = path;
138 m_boxPath = path;
137 m_boundingRect = m_boxPath.boundingRect();
139 m_boundingRect = m_boxPath.boundingRect();
138
140
139 qreal columnWidth = 1.0 / m_data.m_seriesCount;
141 qreal columnWidth = 1.0 / m_data.m_seriesCount;
140 qreal left = ((1.0 - m_boxWidth) / 2.0) * columnWidth + columnWidth * m_data.m_seriesIndex + m_data.m_index - 0.5;
142 qreal left = ((1.0 - m_boxWidth) / 2.0) * columnWidth + columnWidth * m_data.m_seriesIndex + m_data.m_index - 0.5;
141 qreal barWidth = m_boxWidth * columnWidth;
143 qreal barWidth = m_boxWidth * columnWidth;
142
144
143 QPointF geometryPoint = m_domain->calculateGeometryPoint(QPointF(left, m_data.m_upperExtreme), m_validData);
145 QPointF geometryPoint = m_domain->calculateGeometryPoint(QPointF(left, m_data.m_upperExtreme), m_validData);
144 if (!m_validData)
146 if (!m_validData)
145 return;
147 return;
146 m_geometryLeft = geometryPoint.x();
148 m_geometryLeft = geometryPoint.x();
147 qreal geometryUpperExtreme = geometryPoint.y();
149 qreal geometryUpperExtreme = geometryPoint.y();
148 geometryPoint = m_domain->calculateGeometryPoint(QPointF(left + barWidth, m_data.m_upperQuartile), m_validData);
150 geometryPoint = m_domain->calculateGeometryPoint(QPointF(left + barWidth, m_data.m_upperQuartile), m_validData);
149 if (!m_validData)
151 if (!m_validData)
150 return;
152 return;
151 m_geometryRight = geometryPoint.x();
153 m_geometryRight = geometryPoint.x();
152 qreal geometryUpperQuartile = geometryPoint.y();
154 qreal geometryUpperQuartile = geometryPoint.y();
153 geometryPoint = m_domain->calculateGeometryPoint(QPointF(left, m_data.m_lowerQuartile), m_validData);
155 geometryPoint = m_domain->calculateGeometryPoint(QPointF(left, m_data.m_lowerQuartile), m_validData);
154 if (!m_validData)
156 if (!m_validData)
155 return;
157 return;
156 qreal geometryLowerQuartile = geometryPoint.y();
158 qreal geometryLowerQuartile = geometryPoint.y();
157 geometryPoint = m_domain->calculateGeometryPoint(QPointF(left, m_data.m_lowerExtreme), m_validData);
159 geometryPoint = m_domain->calculateGeometryPoint(QPointF(left, m_data.m_lowerExtreme), m_validData);
158 if (!m_validData)
160 if (!m_validData)
159 return;
161 return;
160 qreal geometryLowerExtreme = geometryPoint.y();
162 qreal geometryLowerExtreme = geometryPoint.y();
161 geometryPoint = m_domain->calculateGeometryPoint(QPointF(left, m_data.m_median), m_validData);
163 geometryPoint = m_domain->calculateGeometryPoint(QPointF(left, m_data.m_median), m_validData);
162 if (!m_validData)
164 if (!m_validData)
163 return;
165 return;
164 m_geometryMedian = geometryPoint.y();
166 m_geometryMedian = geometryPoint.y();
165
167
166 // Upper whisker
168 // Upper whisker
167 path.moveTo(m_geometryLeft, geometryUpperExtreme);
169 path.moveTo(m_geometryLeft, geometryUpperExtreme);
168 path.lineTo(m_geometryRight, geometryUpperExtreme);
170 path.lineTo(m_geometryRight, geometryUpperExtreme);
169 path.moveTo((m_geometryLeft + m_geometryRight) / 2.0, geometryUpperExtreme);
171 path.moveTo((m_geometryLeft + m_geometryRight) / 2.0, geometryUpperExtreme);
170 path.lineTo((m_geometryLeft + m_geometryRight) / 2.0, geometryUpperQuartile);
172 path.lineTo((m_geometryLeft + m_geometryRight) / 2.0, geometryUpperQuartile);
171
173
172 // Middle Box
174 // Middle Box
173 m_middleBox.setCoords(m_geometryLeft, geometryUpperQuartile, m_geometryRight, geometryLowerQuartile);
175 m_middleBox.setCoords(m_geometryLeft, geometryUpperQuartile, m_geometryRight, geometryLowerQuartile);
174
176
175 // Lower whisker
177 // Lower whisker
176 path.moveTo(m_geometryLeft, geometryLowerExtreme);
178 path.moveTo(m_geometryLeft, geometryLowerExtreme);
177 path.lineTo(m_geometryRight, geometryLowerExtreme);
179 path.lineTo(m_geometryRight, geometryLowerExtreme);
178 path.moveTo((m_geometryLeft + m_geometryRight) / 2.0, geometryLowerQuartile);
180 path.moveTo((m_geometryLeft + m_geometryRight) / 2.0, geometryLowerQuartile);
179 path.lineTo((m_geometryLeft + m_geometryRight) / 2.0, geometryLowerExtreme);
181 path.lineTo((m_geometryLeft + m_geometryRight) / 2.0, geometryLowerExtreme);
180
182
181 path.closeSubpath();
183 path.closeSubpath();
182
184
183 m_boxPath = path;
185 m_boxPath = path;
184 m_boundingRect = m_boxPath.boundingRect();
186 m_boundingRect = m_boxPath.boundingRect();
185
187
186 qreal extra = m_pen.widthF();
188 qreal extra = m_pen.widthF();
187 m_boundingRect.adjust(-extra, -extra, extra, extra);
189 m_boundingRect.adjust(-extra, -extra, extra, extra);
188 }
190 }
189
191
190 #include "moc_boxwhiskers_p.cpp"
192 #include "moc_boxwhiskers_p.cpp"
191
193
192 QTCOMMERCIALCHART_END_NAMESPACE
194 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now