From 0e43d4b36f2b9d2df5731ebc0a3e1a97f8020563 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Haris=20Gu=C5=A1i=C4=87?= Date: Mon, 11 Oct 2021 03:32:00 +0200 Subject: [PATCH] Fix boundary bug with pixelate and invert tools (#1957) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix invert tool selection Signed-off-by: Haris Gušić * Fix pixelate tool Signed-off-by: Haris Gušić --- src/tools/invert/inverttool.cpp | 18 ++++-------------- src/tools/pixelate/pixelatetool.cpp | 21 ++++++--------------- 2 files changed, 10 insertions(+), 29 deletions(-) diff --git a/src/tools/invert/inverttool.cpp b/src/tools/invert/inverttool.cpp index 3f52ef4f..d8093ee6 100644 --- a/src/tools/invert/inverttool.cpp +++ b/src/tools/invert/inverttool.cpp @@ -37,11 +37,7 @@ QString InvertTool::description() const QRect InvertTool::boundingRect() const { - return QRect(std::min(points().first.x(), points().second.x()), - std::min(points().first.y(), points().second.y()), - std::abs(points().first.x() - points().second.x()), - std::abs(points().first.y() - points().second.y())) - .normalized(); + return QRect(points().first, points().second).normalized(); } CaptureTool* InvertTool::copy(QObject* parent) @@ -53,26 +49,20 @@ CaptureTool* InvertTool::copy(QObject* parent) void InvertTool::process(QPainter& painter, const QPixmap& pixmap) { - QPoint p0 = points().first; - QPoint p1 = points().second; - QRect selection = QRect(p0, p1).normalized(); + QRect selection = boundingRect(); // Invert selection QPixmap inv = pixmap.copy(selection); QImage img = inv.toImage(); img.invertPixels(); - painter.drawImage(selection, img); + painter.drawImage(selection.intersected(pixmap.rect()), img); } void InvertTool::drawSearchArea(QPainter& painter, const QPixmap& pixmap) { Q_UNUSED(pixmap) - painter.fillRect(std::min(points().first.x(), points().second.x()), - std::min(points().first.y(), points().second.y()), - std::abs(points().first.x() - points().second.x()), - std::abs(points().first.y() - points().second.y()), - QBrush(Qt::black)); + painter.fillRect(boundingRect(), QBrush(Qt::black)); } void InvertTool::paintMousePreview(QPainter& painter, diff --git a/src/tools/pixelate/pixelatetool.cpp b/src/tools/pixelate/pixelatetool.cpp index d1ddef79..654b77a7 100644 --- a/src/tools/pixelate/pixelatetool.cpp +++ b/src/tools/pixelate/pixelatetool.cpp @@ -18,6 +18,7 @@ QIcon PixelateTool::icon(const QColor& background, bool inEditor) const Q_UNUSED(inEditor) return QIcon(iconPath(background) + "pixelate.svg"); } + QString PixelateTool::name() const { return tr("Pixelate"); @@ -35,11 +36,7 @@ QString PixelateTool::description() const QRect PixelateTool::boundingRect() const { - return QRect(std::min(points().first.x(), points().second.x()), - std::min(points().first.y(), points().second.y()), - std::abs(points().first.x() - points().second.x()), - std::abs(points().first.y() - points().second.y())) - .normalized(); + return QRect(points().first, points().second).normalized(); } CaptureTool* PixelateTool::copy(QObject* parent) @@ -51,12 +48,10 @@ CaptureTool* PixelateTool::copy(QObject* parent) void PixelateTool::process(QPainter& painter, const QPixmap& pixmap) { - QPoint p0 = points().first; - QPoint p1 = points().second; - QRect selection = QRect(p0, p1).normalized(); + QRect selection = boundingRect().intersected(pixmap.rect()); auto pixelRatio = pixmap.devicePixelRatio(); - QRect selectionScaled = - QRect(p0 * pixelRatio, p1 * pixelRatio).normalized(); + QRect selectionScaled = QRect(selection.topLeft() * pixelRatio, + selection.bottomRight() * pixelRatio); // If thickness is less than 1, use old blur process if (thickness() <= 1) { @@ -91,11 +86,7 @@ void PixelateTool::process(QPainter& painter, const QPixmap& pixmap) void PixelateTool::drawSearchArea(QPainter& painter, const QPixmap& pixmap) { Q_UNUSED(pixmap) - painter.fillRect(std::min(points().first.x(), points().second.x()), - std::min(points().first.y(), points().second.y()), - std::abs(points().first.x() - points().second.x()), - std::abs(points().first.y() - points().second.y()), - QBrush(Qt::black)); + painter.fillRect(boundingRect(), QBrush(Qt::black)); } void PixelateTool::paintMousePreview(QPainter& painter,