From 545e19fa201e7d617dad14a2f43c1e4c766f4e53 Mon Sep 17 00:00:00 2001
From: shibakaneki <didier@sente.ch>
Date: Mon, 28 Nov 2011 14:54:22 +0100
Subject: [PATCH] Updated the painting algorithm for the cache

---
 src/tools/UBGraphicsCache.cpp | 36 +++++++++++++++++++++++++++++++----
 src/tools/UBGraphicsCache.h   |  3 +++
 2 files changed, 35 insertions(+), 4 deletions(-)

diff --git a/src/tools/UBGraphicsCache.cpp b/src/tools/UBGraphicsCache.cpp
index 0c5da394..231399c3 100644
--- a/src/tools/UBGraphicsCache.cpp
+++ b/src/tools/UBGraphicsCache.cpp
@@ -87,7 +87,7 @@ void UBGraphicsCache::init()
 
 void UBGraphicsCache::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
 {
-    Q_UNUSED(option);
+    //Q_UNUSED(option);
     Q_UNUSED(widget);
 
     setZValue(CACHE_ZVALUE);
@@ -95,6 +95,7 @@ void UBGraphicsCache::paint(QPainter *painter, const QStyleOptionGraphicsItem *o
     painter->setBrush(mMaskColor);
     painter->setPen(mMaskColor);
 
+    // Draw the hole
     QPainterPath path;
     path.addRect(rect());
 
@@ -119,20 +120,32 @@ void UBGraphicsCache::mousePressEvent(QGraphicsSceneMouseEvent *event)
     Q_UNUSED(event);
     mShapePos = event->pos();
     mDrawMask = true;
-    update();
+
+    // Note: if refresh issues occure, replace the following 3 lines by: update();
+    update(updateRect(event->pos()));
+    mOldShapeWidth = mShapeWidth;
+    mOldShapePos = event->pos();
 }
 
 void UBGraphicsCache::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
 {
     mShapePos = event->pos();
-    update();
+
+    // Note: if refresh issues occure, replace the following 3 lines by: update();
+    update(updateRect(event->pos()));
+    mOldShapeWidth = mShapeWidth;
+    mOldShapePos = event->pos();
 }
 
 void UBGraphicsCache::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
 {
     Q_UNUSED(event);
     mDrawMask = false;
-    update();
+
+    // Note: if refresh issues occure, replace the following 3 lines by: update();
+    update(updateRect(event->pos()));
+    mOldShapeWidth = mShapeWidth;
+    mOldShapePos = event->pos();
 }
 
 int UBGraphicsCache::shapeWidth()
@@ -145,3 +158,18 @@ void UBGraphicsCache::setShapeWidth(int width)
     mShapeWidth = width;
     update();
 }
+
+QRectF UBGraphicsCache::updateRect(QPointF currentPoint)
+{
+    QRectF r;
+    int x;
+    int y;
+
+    x = qMin(currentPoint.x() - mShapeWidth, mOldShapePos.x() - mOldShapeWidth);
+    y = qMin(currentPoint.y() - mShapeWidth, mOldShapePos.y() - mOldShapeWidth);
+    r = QRect(  x,
+                y,
+                qAbs(currentPoint.x() - mOldShapePos.x()) + 2*mShapeWidth,
+                qAbs(currentPoint.y() - mOldShapePos.y()) + 2*mShapeWidth);
+    return r;
+}
diff --git a/src/tools/UBGraphicsCache.h b/src/tools/UBGraphicsCache.h
index 0bc8a678..6a8ce4cd 100644
--- a/src/tools/UBGraphicsCache.h
+++ b/src/tools/UBGraphicsCache.h
@@ -56,12 +56,15 @@ protected:
 
 private:
     void init();
+    QRectF updateRect(QPointF currentPoint);
 
     QColor mMaskColor;
     eMaskShape mMaskShape;
     int mShapeWidth;
     bool mDrawMask;
     QPointF mShapePos;
+    int mOldShapeWidth;
+    QPointF mOldShapePos;
 };
 
 #endif // UBGRAPHICSCACHE_H
-- 
2.18.1