Skip to content

Commit

Permalink
Take account of devicePixelRatio when rendering QCustomPlotQuickItems
Browse files Browse the repository at this point in the history
  • Loading branch information
timangus committed Oct 17, 2023
1 parent d995746 commit 93aa0f4
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions source/thirdparty/qcustomplot/qcustomplotquickitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "qcustomplotquickitem.h"

#include <QMouseEvent>
#include <QQuickWindow>
#include <QCoreApplication>

QCustomPlotQuickItem::QCustomPlotQuickItem(int multisamples, QQuickItem* parent) :
Expand Down Expand Up @@ -47,10 +48,34 @@ QCustomPlotQuickItem::QCustomPlotQuickItem(int multisamples, QQuickItem* parent)

void QCustomPlotQuickItem::paint(QPainter* painter)
{
auto devicePixelRatio = window()->devicePixelRatio();

QRect target
{
0, 0,
static_cast<int>(width()),
static_cast<int>(height())
};

QRect source
{
0, 0,
static_cast<int>(width() * devicePixelRatio),
static_cast<int>((height()) * devicePixelRatio)
};

QPixmap pixmap(
static_cast<int>(width() * devicePixelRatio),
static_cast<int>(height() * devicePixelRatio));
pixmap.fill(Qt::transparent);
QCPPainter qcpPainter(&pixmap);
qcpPainter.scale(devicePixelRatio, devicePixelRatio);
_customPlot.toPainter(&qcpPainter);

if(!isEnabled())
{
// Create a desaturated version of the pixmap
auto image = _customPlot.toPixmap().toImage();
auto image = pixmap.toImage();
const auto bytes = image.depth() >> 3;

for(int y = 0; y < image.height(); y++)
Expand All @@ -74,13 +99,13 @@ void QCustomPlotQuickItem::paint(QPainter* painter)
alphaBackgroundColor.setAlpha(127);

painter->fillRect(QRectF{0.0, 0.0, width(), height()}, alphaBackgroundColor);
painter->drawPixmap(0, 0, QPixmap::fromImage(image));
painter->drawPixmap(target, QPixmap::fromImage(image), source);
}
else
{
painter->setCompositionMode(QPainter::CompositionMode_SourceOver);
painter->fillRect(QRectF{0.0, 0.0, width(), height()}, backgroundColor());
painter->drawPixmap(0, 0, _customPlot.toPixmap());
painter->drawPixmap(target, pixmap, source);
}
}

Expand Down

0 comments on commit 93aa0f4

Please sign in to comment.