From c9e6a03c32fe6e6901fad8439bf2235ddb8c288c Mon Sep 17 00:00:00 2001
From: Steve Allewell <steve.allewell@gmail.com>
Date: Sun, 3 Jan 2021 13:43:59 +0000
Subject: [PATCH] Fix for rendering on scaled painter

Fixes for rendering seletion areas on scaled painter in recent versions
of Qt which were half a square out.
---
 src/Editor.cpp | 74 ++++++++++++++++++++++++++++----------------------
 1 file changed, 41 insertions(+), 33 deletions(-)

diff --git a/src/Editor.cpp b/src/Editor.cpp
index 4d314e4..263df1e 100644
--- a/src/Editor.cpp
+++ b/src/Editor.cpp
@@ -1388,11 +1388,12 @@ void Editor::renderRubberBandRectangle(QPainter *painter, const QRect&)
     if (m_rubberBand.isValid()) {
         painter->setRenderHint(QPainter::Qt4CompatiblePainting, true);
 
+        painter->resetTransform();
         QStyleOptionRubberBand opt;
         opt.initFrom(this);
         opt.shape = QRubberBand::Rectangle;
         opt.opaque = false;
-        opt.rect = m_rubberBand.adjusted(0, 0, 1, 1);
+        opt.rect = rectToContents(m_rubberBand);
 
         style()->drawControl(QStyle::CE_RubberBand, &opt, painter);
     }
@@ -1408,14 +1409,15 @@ void Editor::renderRubberBandEllipse(QPainter *painter, const QRect&)
     if (m_rubberBand.isValid()) {
         painter->setRenderHint(QPainter::Qt4CompatiblePainting, true);
 
-        painter->setPen(Qt::NoPen);
-        painter->setBrush(QColor(200,225,255));
-        painter->setOpacity(0.5);
-        painter->drawEllipse(m_rubberBand);
+        painter->resetTransform();
+        QStyleOptionRubberBand opt;
+        opt.initFrom(this);
 
-        painter->setPen(Qt::darkBlue);
-        painter->setBrush(Qt::NoBrush);
-        painter->drawEllipse(m_rubberBand);
+        painter->setPen(opt.palette.color(QPalette::WindowText));
+        painter->setBrush(QBrush(opt.palette.color(QPalette::Highlight), Qt::Dense4Pattern));
+        painter->setBackground(QBrush(opt.palette.base()));
+        painter->setBackgroundMode(Qt::TransparentMode);
+        painter->drawEllipse(rectToContents(m_rubberBand));
     }
 
     painter->restore();
@@ -1427,7 +1429,10 @@ void Editor::renderFillPolygon(QPainter *painter, const QRect&)
     QPolygonF polyline;
     painter->save();
 
-    painter->setPen(Qt::green);         // use green for the first point
+    QPen pen(Qt::green);
+    pen.setWidth(0);
+
+    painter->setPen(pen);         // use green for the first point
     painter->setBrush(Qt::green);
 
     QVector<QPoint>::const_iterator i;
@@ -1435,7 +1440,8 @@ void Editor::renderFillPolygon(QPainter *painter, const QRect&)
     for (i = m_polygon.constBegin() ; i != m_polygon.constEnd() ; ++i) {
         QPointF cell = QPointF(*i) + QPointF(0.5, 0.5);
         painter->drawEllipse(QRectF(-0.5, -0.5, 1, 1).translated(cell));
-        painter->setPen(Qt::blue);      // use blue for subsequent points
+        pen.setColor(Qt::blue);
+        painter->setPen(pen);      // use blue for subsequent points
         painter->setBrush(Qt::blue);
         polyline.append(cell);
     }
@@ -1555,9 +1561,9 @@ void Editor::mouseMoveEvent_Draw(QMouseEvent *e)
 
     dynamic_cast<QScrollArea *>(parentWidget()->parentWidget())->ensureVisible(p.x(), p.y());
 
-    m_cellTracking = contentsToCell(p);
-    m_cellEnd = m_cellTracking;
-    m_rubberBand = QRect(m_cellStart, m_cellEnd).normalized();
+    m_cellEnd = m_cellTracking = contentsToCell(p);
+    m_rubberBand = QRect(m_cellStart, QSize(1, 1)).united(QRect(m_cellEnd, QSize(1, 1)));
+
     update();
 }
 
@@ -1703,9 +1709,9 @@ void Editor::mouseMoveEvent_Rectangle(QMouseEvent *e)
 
     dynamic_cast<QScrollArea *>(parentWidget()->parentWidget())->ensureVisible(p.x(), p.y());
 
-    m_cellTracking = contentsToCell(p);
-    m_cellEnd = m_cellTracking;
-    m_rubberBand = QRect(m_cellStart, m_cellEnd).normalized();
+    m_cellEnd = m_cellTracking = contentsToCell(p);
+    m_rubberBand = QRect(m_cellStart, QSize(1, 1)).united(QRect(m_cellEnd, QSize(1, 1)));
+
     update();
 }
 
@@ -1757,9 +1763,9 @@ void Editor::mouseMoveEvent_FillRectangle(QMouseEvent *e)
 
     dynamic_cast<QScrollArea *>(parentWidget()->parentWidget())->ensureVisible(p.x(), p.y());
 
-    m_cellTracking = contentsToCell(p);
-    m_cellEnd = m_cellTracking;
-    m_rubberBand = QRect(m_cellStart, m_cellEnd).normalized();
+    m_cellEnd = m_cellTracking = contentsToCell(p);
+    m_rubberBand = QRect(m_cellStart, QSize(1, 1)).united(QRect(m_cellEnd, QSize(1, 1)));
+
     update();
 }
 
@@ -1793,9 +1799,9 @@ void Editor::mouseMoveEvent_Ellipse(QMouseEvent *e)
 
     dynamic_cast<QScrollArea *>(parentWidget()->parentWidget())->ensureVisible(p.x(), p.y());
 
-    m_cellTracking = contentsToCell(p);
-    m_cellEnd = m_cellTracking;
-    m_rubberBand = QRect(m_cellStart, m_cellEnd).normalized();
+    m_cellEnd = m_cellTracking = contentsToCell(p);
+    m_rubberBand = QRect(m_cellStart, QSize(1, 1)).united(QRect(m_cellEnd, QSize(1, 1)));
+
     update();
 }
 
@@ -1848,9 +1854,9 @@ void Editor::mouseMoveEvent_FillEllipse(QMouseEvent *e)
 
     dynamic_cast<QScrollArea *>(parentWidget()->parentWidget())->ensureVisible(p.x(), p.y());
 
-    m_cellTracking = contentsToCell(p);
-    m_cellEnd = m_cellTracking;
-    m_rubberBand = QRect(m_cellStart, m_cellEnd).normalized();
+    m_cellEnd = m_cellTracking = contentsToCell(p);
+    m_rubberBand = QRect(m_cellStart, QSize(1, 1)).united(QRect(m_cellEnd, QSize(1, 1)));
+
     update();
 }
 
@@ -1877,7 +1883,7 @@ void Editor::mouseReleaseEvent_FillEllipse(QMouseEvent*)
         painter.setRenderHint(QPainter::Antialiasing, !useFractionals);
         painter.setPen(QPen(Qt::color1));
         painter.setBrush(Qt::color1);
-        painter.drawEllipse(QRect(m_cellStart, m_cellEnd).normalized());
+        painter.drawEllipse(QRect(m_cellStart, QSize(1,1)).united(QRect(m_cellEnd, QSize(1, 1))));
         painter.end();
 
         QUndoCommand *cmd = new FillEllipseCommand(m_document);
@@ -1894,6 +1900,7 @@ void Editor::mousePressEvent_FillPolygon(QMouseEvent *e)
 {
     m_cellStart = m_cellTracking = m_cellEnd = contentsToCell(e->pos());
     m_polygon.append(m_cellStart);
+
     update();
 }
 
@@ -2001,6 +2008,7 @@ void Editor::mouseReleaseEvent_Alphabet(QMouseEvent *e)
 
     m_cellStart = m_cellTracking = m_cellEnd = contentsToCell(e->pos());
     m_cursorStack.push(m_cellEnd);
+
     update();
 }
 
@@ -2023,9 +2031,9 @@ void Editor::mouseMoveEvent_Select(QMouseEvent *e)
 
     dynamic_cast<QScrollArea *>(parentWidget()->parentWidget())->ensureVisible(p.x(), p.y());
 
-    m_cellTracking = contentsToCell(p);
-    m_cellEnd = m_cellTracking;
-    m_rubberBand = QRect(m_cellStart, m_cellEnd).normalized();
+    m_cellEnd = m_cellTracking = contentsToCell(p);
+    m_rubberBand = QRect(m_cellStart, QSize(1, 1)).united(QRect(m_cellEnd, QSize(1, 1)));
+
     update();
 
     QToolTip::showText(QCursor::pos(), QString::fromLatin1("%1,%2 %3 x %4").arg(m_rubberBand.left()).arg(m_rubberBand.top()).arg(m_rubberBand.width()).arg(m_rubberBand.height()));
@@ -2034,7 +2042,7 @@ void Editor::mouseMoveEvent_Select(QMouseEvent *e)
 
 void Editor::mouseReleaseEvent_Select(QMouseEvent*)
 {
-    m_selectionArea = QRect(m_cellStart, m_cellEnd).normalized();
+    m_selectionArea = m_rubberBand;
     emit(selectionMade(true));
 }
 
@@ -2052,9 +2060,9 @@ void Editor::mouseMoveEvent_Backstitch(QMouseEvent *e)
 
     dynamic_cast<QScrollArea *>(parentWidget()->parentWidget())->ensureVisible(p.x(), p.y());
 
-    m_cellTracking = contentsToSnap(p);
-    m_cellEnd = m_cellTracking;
-    m_rubberBand = (snapToCells(m_cellStart).united(snapToCells(m_cellEnd))).normalized();
+    m_cellEnd = m_cellTracking = contentsToSnap(p);
+    m_rubberBand = snapToCells(m_cellStart).united(snapToCells(m_cellEnd));
+
     update();
 }
 
-- 
GitLab

