From 36c54addce2f7ba34f43631276ca4ad901b5dc36 Mon Sep 17 00:00:00 2001 From: Gary Wang Date: Sun, 20 Jun 2021 14:59:44 +0800 Subject: [PATCH] fix: use a workaround to avoid pixelated when zoomed out GH-31 --- app/graphicsscene.cpp | 40 +++++++++++++++++++++++++++++++++++++--- app/graphicsscene.h | 2 +- app/graphicsview.cpp | 4 ++-- 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/app/graphicsscene.cpp b/app/graphicsscene.cpp index 4aecd6d..20ce2b3 100644 --- a/app/graphicsscene.cpp +++ b/app/graphicsscene.cpp @@ -10,6 +10,38 @@ #include #include +class PGraphicsPixmapItem : public QGraphicsPixmapItem +{ +public: + PGraphicsPixmapItem(const QPixmap &pixmap, QGraphicsItem *parent = nullptr) + : QGraphicsPixmapItem(pixmap, parent) + {} + + void setScaleHint(float scaleHint) { + m_scaleHint = scaleHint; + } + + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, + QWidget *widget) override + { + if (transformationMode() == Qt::FastTransformation) { + return QGraphicsPixmapItem::paint(painter, option, widget); + } else { +// painter->setRenderHints(QPainter::Antialiasing); + QSizeF resizedScale(boundingRect().size()); + resizedScale *= m_scaleHint; + painter->drawPixmap(QRectF(offset(), boundingRect().size()).toRect(), + pixmap().scaled(resizedScale.toSize(), + Qt::KeepAspectRatio, + Qt::SmoothTransformation) + ); + } + } + +private: + float m_scaleHint = 1; +}; + GraphicsScene::GraphicsScene(QObject *parent) : QGraphicsScene(parent) { @@ -24,7 +56,8 @@ GraphicsScene::~GraphicsScene() void GraphicsScene::showImage(const QPixmap &pixmap) { this->clear(); - QGraphicsPixmapItem * pixmapItem = this->addPixmap(pixmap); + PGraphicsPixmapItem * pixmapItem = new PGraphicsPixmapItem(pixmap); + this->addItem(pixmapItem); pixmapItem->setShapeMode(QGraphicsPixmapItem::BoundingRectShape); m_theThing = pixmapItem; this->setSceneRect(m_theThing->boundingRect()); @@ -62,11 +95,12 @@ void GraphicsScene::showAnimated(const QString &filepath) this->setSceneRect(m_theThing->boundingRect()); } -bool GraphicsScene::trySetTransformationMode(Qt::TransformationMode mode) +bool GraphicsScene::trySetTransformationMode(Qt::TransformationMode mode, float scaleHint) { - QGraphicsPixmapItem * pixmapItem = qgraphicsitem_cast(m_theThing); + PGraphicsPixmapItem * pixmapItem = qgraphicsitem_cast(m_theThing); if (pixmapItem) { pixmapItem->setTransformationMode(mode); + pixmapItem->setScaleHint(scaleHint); return true; } diff --git a/app/graphicsscene.h b/app/graphicsscene.h index 75b1ed4..af44334 100644 --- a/app/graphicsscene.h +++ b/app/graphicsscene.h @@ -15,7 +15,7 @@ public: void showSvg(const QString &filepath); void showAnimated(const QString &filepath); - bool trySetTransformationMode(Qt::TransformationMode mode); + bool trySetTransformationMode(Qt::TransformationMode mode, float scaleHint); QPixmap renderToPixmap(); diff --git a/app/graphicsview.cpp b/app/graphicsview.cpp index 01f7c0b..932d7ea 100644 --- a/app/graphicsview.cpp +++ b/app/graphicsview.cpp @@ -392,8 +392,8 @@ void GraphicsView::setCheckerboardEnabled(bool enabled, bool invertColor) void GraphicsView::applyTransformationModeByScaleFactor() { if (this->scaleFactor() < 1) { - scene()->trySetTransformationMode(Qt::SmoothTransformation); + scene()->trySetTransformationMode(Qt::SmoothTransformation, this->scaleFactor()); } else { - scene()->trySetTransformationMode(Qt::FastTransformation); + scene()->trySetTransformationMode(Qt::FastTransformation, this->scaleFactor()); } }