From cfa1350263ea8dfc677166ff07f42292de32fede 2012-02-28 09:22:45 From: Tero Ahola Date: 2012-02-28 09:22:45 Subject: [PATCH] Fixed drawing issues in Scatter --- diff --git a/src/chartpresenter_p.h b/src/chartpresenter_p.h index 72f7ddc..62d4769 100644 --- a/src/chartpresenter_p.h +++ b/src/chartpresenter_p.h @@ -20,7 +20,14 @@ class ChartPresenter: public QObject { Q_OBJECT public: - enum ZValues { BackgroundZValue = -1 , ShadesZValue, GridZValue, AxisZValue , LineChartZValue }; + enum ZValues { + BackgroundZValue = -1, + ShadesZValue, + GridZValue, + AxisZValue, + LineChartZValue, + ScatterSeriesZValue + }; ChartPresenter(QChart* chart,ChartDataSet *dataset); virtual ~ChartPresenter(); diff --git a/src/scatterseries/qscatterseries.cpp b/src/scatterseries/qscatterseries.cpp index 09a377a..e99b4fa 100644 --- a/src/scatterseries/qscatterseries.cpp +++ b/src/scatterseries/qscatterseries.cpp @@ -49,13 +49,10 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE QScatterSeriesPrivate::QScatterSeriesPrivate() : m_data(QList()), - m_markerPen(QPen()), - m_markerBrush(QBrush()), + m_markerPen(QPen(QColor::Invalid)), + m_markerBrush(QBrush(QColor::Invalid)), m_markerShape(QScatterSeries::MarkerShapeDefault) { - // Initialize pen color to invalid to use a theme color by default - m_markerPen.setColor(QColor::Invalid); - m_markerBrush.setColor(QColor::Invalid); } /*! diff --git a/src/scatterseries/scatterpresenter.cpp b/src/scatterseries/scatterpresenter.cpp index ea67f10..a8c93f8 100644 --- a/src/scatterseries/scatterpresenter.cpp +++ b/src/scatterseries/scatterpresenter.cpp @@ -1,5 +1,6 @@ #include "scatterpresenter_p.h" #include "qscatterseries.h" +#include "chartpresenter_p.h" #include #include #include @@ -23,10 +24,13 @@ ScatterPresenter::ScatterPresenter(QScatterSeries *series, QGraphicsObject *pare connect(series, SIGNAL(changed()), this, SLOT(handleModelChanged())); } - QGraphicsDropShadowEffect *dropShadow = new QGraphicsDropShadowEffect(); - dropShadow->setOffset(2.0); - dropShadow->setBlurRadius(2.0); - setGraphicsEffect(dropShadow); + setZValue(ChartPresenter::ScatterSeriesZValue); + + // TODO: how to draw a drop shadow? +// QGraphicsDropShadowEffect *dropShadow = new QGraphicsDropShadowEffect(); +// dropShadow->setOffset(2.0); +// dropShadow->setBlurRadius(2.0); +// setGraphicsEffect(dropShadow); } void ScatterPresenter::handleDomainChanged(const Domain& domain) @@ -52,17 +56,34 @@ void ScatterPresenter::paint(QPainter *painter, const QStyleOptionGraphicsItem * painter->save(); painter->setClipRect(m_boundingRect); + // TODO: how to draw a drop shadow? + // Now using a custom implementation for drop shadow instead of QGraphicsDropShadowEffect. + // It seems QGraphicsDropShadowEffect is quite heavy, at least on windows without open gl. + QPen dropShadowPen(QColor(0, 0, 0, 70)); + dropShadowPen.setWidth(3); + painter->setPen(dropShadowPen); + painter->setBrush(dropShadowPen.color()); +// painter->setRenderHint(QPainter::Antialiasing); + painter->drawPath(m_path.translated(2, 2)); + // Paint the shape // The custom settings in series override those defined by the theme QPen pen = m_markerPen; if (m_series->pen().color().isValid()) pen = m_series->pen(); + painter->setPen(pen); if (m_series->brush().color().isValid()) painter->setBrush(m_series->brush()); else painter->setBrush(m_markerBrush); - painter->setPen(pen); - painter->drawPath(m_path); + + // If either pen or brush is opaque, we need to draw the polygons one-by-one + if (painter->pen().color().alpha() < 255 || painter->brush().color().alpha() < 255) { + foreach (QPolygonF pol, m_path.toSubpathPolygons()) + painter->drawPolygon(pol); + } else { + painter->drawPath(m_path); + } painter->restore(); } @@ -85,6 +106,7 @@ void ScatterPresenter::changeGeometry() int shape = m_series->shape(); m_path = QPainterPath(); + m_path.setFillRule(Qt::WindingFill); foreach (QPointF point, m_series->data()) { // Convert relative coordinates to absolute pixel coordinates that can be used for drawing