boxwhiskers.cpp
194 lines
| 5.6 KiB
| text/x-c
|
CppLexer
|
r2548 | /**************************************************************************** | ||
** | ||||
** Copyright (C) 2013 Digia Plc | ||||
** All rights reserved. | ||||
** For any questions to Digia, please use contact form at http://qt.digia.com | ||||
** | ||||
|
r2574 | ** This file is part of the Qt Enterprise Charts Add-on. | ||
|
r2548 | ** | ||
** $QT_BEGIN_LICENSE$ | ||||
|
r2574 | ** Licensees holding valid Qt Enterprise licenses may use this file in | ||
** accordance with the Qt Enterprise License Agreement provided with the | ||||
|
r2548 | ** Software or, alternatively, in accordance with the terms contained in | ||
** a written agreement between you and Digia. | ||||
** | ||||
** If you have questions regarding the use of this file, please use | ||||
** contact form at http://qt.digia.com | ||||
** $QT_END_LICENSE$ | ||||
** | ||||
****************************************************************************/ | ||||
#include "boxwhiskers_p.h" | ||||
#include <QPainter> | ||||
#include <QWidget> | ||||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
BoxWhiskers::BoxWhiskers(QBoxSet *set, AbstractDomain *domain, QGraphicsObject *parent) : | ||||
QGraphicsObject(parent), | ||||
m_boxSet(set), | ||||
m_domain(domain) | ||||
{ | ||||
setAcceptHoverEvents(true); | ||||
setAcceptedMouseButtons(Qt::MouseButtonMask); | ||||
} | ||||
BoxWhiskers::~BoxWhiskers() | ||||
{ | ||||
} | ||||
void BoxWhiskers::mousePressEvent(QGraphicsSceneMouseEvent *event) | ||||
{ | ||||
Q_UNUSED(event) | ||||
emit clicked(m_boxSet); | ||||
} | ||||
void BoxWhiskers::hoverEnterEvent(QGraphicsSceneHoverEvent *event) | ||||
{ | ||||
Q_UNUSED(event) | ||||
emit hovered(true, m_boxSet); | ||||
} | ||||
void BoxWhiskers::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) | ||||
{ | ||||
Q_UNUSED(event) | ||||
emit hovered(false, m_boxSet); | ||||
} | ||||
void BoxWhiskers::setBrush(const QBrush &brush) | ||||
{ | ||||
m_brush = brush; | ||||
|
r2573 | m_outlinePen.setColor(m_brush.color()); | ||
|
r2548 | update(); | ||
} | ||||
void BoxWhiskers::setPen(const QPen &pen) | ||||
{ | ||||
|
r2573 | qreal widthDiff = pen.widthF() - m_pen.widthF(); | ||
m_boundingRect.adjust(-widthDiff, -widthDiff, widthDiff, widthDiff); | ||||
|
r2548 | m_pen = pen; | ||
|
r2573 | m_medianPen = pen; | ||
m_medianPen.setCapStyle(Qt::FlatCap); | ||||
m_outlinePen = pen; | ||||
m_outlinePen.setStyle(Qt::SolidLine); | ||||
m_outlinePen.setColor(m_brush.color()); | ||||
|
r2548 | update(); | ||
} | ||||
|
r2584 | void BoxWhiskers::setBoxWidth(const qreal width) | ||
{ | ||||
m_boxWidth = width; | ||||
} | ||||
|
r2548 | void BoxWhiskers::setLayout(const BoxWhiskersData &data) | ||
{ | ||||
m_data = data; | ||||
|
r2554 | updateGeometry(m_domain); | ||
|
r2548 | update(); | ||
} | ||||
QSizeF BoxWhiskers::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const | ||||
{ | ||||
Q_UNUSED(which) | ||||
Q_UNUSED(constraint) | ||||
return QSizeF(); | ||||
} | ||||
void BoxWhiskers::setGeometry(const QRectF &rect) | ||||
{ | ||||
Q_UNUSED(rect) | ||||
} | ||||
QRectF BoxWhiskers::boundingRect() const | ||||
{ | ||||
return m_boundingRect; | ||||
} | ||||
void BoxWhiskers::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | ||||
{ | ||||
Q_UNUSED(option) | ||||
Q_UNUSED(widget) | ||||
|
r2586 | painter->save(); | ||
|
r2548 | painter->setBrush(m_brush); | ||
painter->setClipRect(parentItem()->boundingRect()); | ||||
|
r2573 | painter->setPen(m_pen); | ||
|
r2548 | painter->drawPath(m_boxPath); | ||
|
r2573 | if (!m_boxOutlined) | ||
painter->setPen(m_outlinePen); | ||||
painter->drawRect(m_middleBox); | ||||
painter->setPen(m_medianPen); | ||||
qreal halfLine = m_pen.widthF() / 2.0; | ||||
painter->drawLine(QLineF(m_geometryLeft - halfLine, m_geometryMedian, | ||||
m_geometryRight + halfLine, m_geometryMedian)); | ||||
|
r2586 | painter->restore(); | ||
|
r2548 | } | ||
|
r2554 | void BoxWhiskers::updateGeometry(AbstractDomain *domain) | ||
|
r2548 | { | ||
|
r2554 | m_domain = domain; | ||
|
r2548 | prepareGeometryChange(); | ||
QPainterPath path; | ||||
|
r2554 | m_boxPath = path; | ||
m_boundingRect = m_boxPath.boundingRect(); | ||||
|
r2548 | |||
qreal columnWidth = 1.0 / m_data.m_seriesCount; | ||||
|
r2584 | qreal left = ((1.0 - m_boxWidth) / 2.0) * columnWidth + columnWidth * m_data.m_seriesIndex + m_data.m_index - 0.5; | ||
qreal barWidth = m_boxWidth * columnWidth; | ||||
|
r2554 | |||
QPointF geometryPoint = m_domain->calculateGeometryPoint(QPointF(left, m_data.m_upperExtreme), m_validData); | ||||
if (!m_validData) | ||||
return; | ||||
|
r2573 | m_geometryLeft = geometryPoint.x(); | ||
|
r2554 | qreal geometryUpperExtreme = geometryPoint.y(); | ||
geometryPoint = m_domain->calculateGeometryPoint(QPointF(left + barWidth, m_data.m_upperQuartile), m_validData); | ||||
if (!m_validData) | ||||
return; | ||||
|
r2573 | m_geometryRight = geometryPoint.x(); | ||
|
r2554 | qreal geometryUpperQuartile = geometryPoint.y(); | ||
geometryPoint = m_domain->calculateGeometryPoint(QPointF(left, m_data.m_lowerQuartile), m_validData); | ||||
if (!m_validData) | ||||
return; | ||||
qreal geometryLowerQuartile = geometryPoint.y(); | ||||
geometryPoint = m_domain->calculateGeometryPoint(QPointF(left, m_data.m_lowerExtreme), m_validData); | ||||
if (!m_validData) | ||||
return; | ||||
qreal geometryLowerExtreme = geometryPoint.y(); | ||||
geometryPoint = m_domain->calculateGeometryPoint(QPointF(left, m_data.m_median), m_validData); | ||||
if (!m_validData) | ||||
return; | ||||
|
r2573 | m_geometryMedian = geometryPoint.y(); | ||
|
r2548 | |||
// Upper whisker | ||||
|
r2573 | path.moveTo(m_geometryLeft, geometryUpperExtreme); | ||
path.lineTo(m_geometryRight, geometryUpperExtreme); | ||||
path.moveTo((m_geometryLeft + m_geometryRight) / 2.0, geometryUpperExtreme); | ||||
path.lineTo((m_geometryLeft + m_geometryRight) / 2.0, geometryUpperQuartile); | ||||
|
r2548 | |||
// Middle Box | ||||
|
r2573 | m_middleBox.setCoords(m_geometryLeft, geometryUpperQuartile, m_geometryRight, geometryLowerQuartile); | ||
|
r2548 | |||
// Lower whisker | ||||
|
r2573 | path.moveTo(m_geometryLeft, geometryLowerExtreme); | ||
path.lineTo(m_geometryRight, geometryLowerExtreme); | ||||
path.moveTo((m_geometryLeft + m_geometryRight) / 2.0, geometryLowerQuartile); | ||||
path.lineTo((m_geometryLeft + m_geometryRight) / 2.0, geometryLowerExtreme); | ||||
|
r2548 | |||
path.closeSubpath(); | ||||
m_boxPath = path; | ||||
|
r2554 | m_boundingRect = m_boxPath.boundingRect(); | ||
|
r2565 | |||
|
r2584 | qreal extra = m_pen.widthF(); | ||
|
r2565 | m_boundingRect.adjust(-extra, -extra, extra, extra); | ||
|
r2548 | } | ||
#include "moc_boxwhiskers_p.cpp" | ||||
QTCOMMERCIALCHART_END_NAMESPACE | ||||