From 9005db570dc5674cfcca745572e3cc34b224d48b Mon Sep 17 00:00:00 2001
From: Gennady Pospelov <g.pospelov@fz-juelich.de>
Date: Thu, 11 Feb 2016 22:32:33 +0100
Subject: [PATCH] Serious bug is fixed in SampleDesigner and MaskEditor which
 was making application hanging in mac and slightly clumsy on Linux. Never put
 invalidate() in drawForeground().

---
 .../Views/MaskWidgets/MaskGraphicsScene.cpp   | 62 ++++++++++---------
 .../Views/MaskWidgets/MaskGraphicsScene.h     |  2 +-
 .../Views/SampleDesigner/DesignerScene.cpp    | 21 ++++++-
 .../Views/SampleDesigner/DesignerScene.h      |  2 +
 4 files changed, 53 insertions(+), 34 deletions(-)

diff --git a/GUI/coregui/Views/MaskWidgets/MaskGraphicsScene.cpp b/GUI/coregui/Views/MaskWidgets/MaskGraphicsScene.cpp
index ccfb583bd34..d381bea00d6 100644
--- a/GUI/coregui/Views/MaskWidgets/MaskGraphicsScene.cpp
+++ b/GUI/coregui/Views/MaskWidgets/MaskGraphicsScene.cpp
@@ -325,7 +325,11 @@ void MaskGraphicsScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
     }
 
     QGraphicsScene::mouseMoveEvent(event);
-    m_currentMousePosition = event->scenePos();
+
+    if( (isDrawingInProgress() && m_context.isPolygonMode()) || m_context.isLineMode()) {
+        m_currentMousePosition = event->scenePos();
+        invalidate();
+    }
 }
 
 //! Finalizes item drawing or pass events to other items
@@ -354,32 +358,29 @@ void MaskGraphicsScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
     }
 }
 
-void MaskGraphicsScene::drawForeground(QPainter *painter, const QRectF &)
+void MaskGraphicsScene::drawForeground(QPainter *painter, const QRectF &rect)
 {
-//    if(PolygonView *polygon = getCurrentPolygon()) {
-//        painter->setPen(QPen(Qt::black, 1, Qt::DashLine));
-//        painter->drawLine(QLineF(polygon->getLastAddedPoint(), m_currentMousePosition));
-//        invalidate();
-//    } else {
-//        if(m_context.isLineMode()) {
-//            const QRectF &plot_scene_rectangle = m_adaptor->getViewportRectangle();
-//            if(!plot_scene_rectangle.contains(m_currentMousePosition)) return;
-
-//            painter->setPen(QPen(Qt::black, 1, Qt::DashLine));
-//            if(m_context.isVerticalLineMode()) {
-//                QPointF p1(m_currentMousePosition.x(), plot_scene_rectangle.bottom());
-//                QPointF p2(m_currentMousePosition.x(), plot_scene_rectangle.top());
-//                painter->drawLine(QLineF(p1, p2));
-//            }
-//            if(m_context.isHorizontalLineMode()) {
-//                QPointF p1(plot_scene_rectangle.left(), m_currentMousePosition.y());
-//                QPointF p2(plot_scene_rectangle.right(), m_currentMousePosition.y());
-//                painter->drawLine(QLineF(p1, p2));
-//            }
-//            invalidate();
-
-//        }
-//    }
+    if(PolygonView *polygon = getCurrentPolygon()) {
+        painter->setPen(QPen(Qt::black, 1, Qt::DashLine));
+        painter->drawLine(QLineF(polygon->getLastAddedPoint(), m_currentMousePosition));
+    } else {
+        if(m_context.isLineMode()) {
+            const QRectF &plot_scene_rectangle = m_adaptor->getViewportRectangle();
+            if(!plot_scene_rectangle.contains(m_currentMousePosition)) return;
+
+            painter->setPen(QPen(Qt::black, 1, Qt::DashLine));
+            if(m_context.isVerticalLineMode()) {
+                QPointF p1(m_currentMousePosition.x(), plot_scene_rectangle.bottom());
+                QPointF p2(m_currentMousePosition.x(), plot_scene_rectangle.top());
+                painter->drawLine(QLineF(p1, p2));
+            }
+            if(m_context.isHorizontalLineMode()) {
+                QPointF p1(plot_scene_rectangle.left(), m_currentMousePosition.y());
+                QPointF p2(plot_scene_rectangle.right(), m_currentMousePosition.y());
+                painter->drawLine(QLineF(p1, p2));
+            }
+        }
+    }
 }
 
 //! creates item context menu if there is IMaskView beneath the mouse right click
@@ -735,10 +736,11 @@ PolygonView *MaskGraphicsScene::getCurrentPolygon() const
     PolygonView *result(0);
     if(isDrawingInProgress() && m_context.isPolygonMode()) {
         if(m_currentItem) {
-            if(IMaskView *view = m_ItemToView[m_currentItem]) {
-                if(view->type() == MaskEditorHelper::POLYGON)
-                    result = dynamic_cast<PolygonView *>(view);
-            }
+//            if(IMaskView *view = m_ItemToView[m_currentItem]) {
+//                if(view->type() == MaskEditorHelper::POLYGON)
+//                    result = dynamic_cast<PolygonView *>(view);
+//            }
+              result = dynamic_cast<PolygonView *>(m_ItemToView[m_currentItem]);
         }
     }
     return result;
diff --git a/GUI/coregui/Views/MaskWidgets/MaskGraphicsScene.h b/GUI/coregui/Views/MaskWidgets/MaskGraphicsScene.h
index 82079044eb6..7cbd82c7d79 100644
--- a/GUI/coregui/Views/MaskWidgets/MaskGraphicsScene.h
+++ b/GUI/coregui/Views/MaskWidgets/MaskGraphicsScene.h
@@ -71,7 +71,7 @@ protected:
     void mousePressEvent(QGraphicsSceneMouseEvent *event);
     void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
     void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
-    void drawForeground(QPainter *painter, const QRectF &);
+    void drawForeground(QPainter *painter, const QRectF &rect);
     void contextMenuEvent(QGraphicsSceneContextMenuEvent *event);
 
 private:
diff --git a/GUI/coregui/Views/SampleDesigner/DesignerScene.cpp b/GUI/coregui/Views/SampleDesigner/DesignerScene.cpp
index e5f641e4b6c..c2494500753 100644
--- a/GUI/coregui/Views/SampleDesigner/DesignerScene.cpp
+++ b/GUI/coregui/Views/SampleDesigner/DesignerScene.cpp
@@ -338,11 +338,9 @@ void DesignerScene::deleteSelectedItems()
 //! shows appropriate layer interface to drop while moving ILayerView
 void DesignerScene::drawForeground(QPainter *painter, const QRectF & /* rect */)
 {
-    ILayerView *layer = dynamic_cast<ILayerView *>(mouseGrabberItem());
-    if (layer && !m_layer_interface_line.isNull()) {
+    if (isLayerDragged()) {
         painter->setPen(QPen(Qt::darkBlue, 2, Qt::DashLine));
         painter->drawLine(m_layer_interface_line);
-        invalidate();
     }
 }
 
@@ -452,6 +450,14 @@ const DesignerMimeData *DesignerScene::checkDragEvent(QGraphicsSceneDragDropEven
     return mimeData;
 }
 
+void DesignerScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
+{
+    if(isLayerDragged()) {
+        invalidate(); // to redraw vertical dashed line which denotes where to drag the layer
+    }
+    QGraphicsScene::mouseMoveEvent(event);
+}
+
 //! Returns true if there is MultiLayerView nearby during drag event.
 bool DesignerScene::isMultiLayerNearby(QGraphicsSceneDragDropEvent *event)
 {
@@ -490,6 +496,15 @@ bool DesignerScene::isAcceptedByMultiLayer(const DesignerMimeData *mimeData, QGr
     return false;
 }
 
+bool DesignerScene::isLayerDragged() const
+{
+    ILayerView *layer = dynamic_cast<ILayerView *>(mouseGrabberItem());
+    if (layer && !m_layer_interface_line.isNull()) {
+        return true;
+    }
+    return false;
+}
+
 void DesignerScene::onSmartAlign()
 {
     m_aligner->smartAlign();
diff --git a/GUI/coregui/Views/SampleDesigner/DesignerScene.h b/GUI/coregui/Views/SampleDesigner/DesignerScene.h
index c1012d048b3..006319cc85e 100644
--- a/GUI/coregui/Views/SampleDesigner/DesignerScene.h
+++ b/GUI/coregui/Views/SampleDesigner/DesignerScene.h
@@ -82,6 +82,7 @@ public slots:
 protected:
     void drawForeground(QPainter* painter, const QRectF& rect);
     const DesignerMimeData *checkDragEvent(QGraphicsSceneDragDropEvent * event);
+    void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
 
 private:
 
@@ -93,6 +94,7 @@ private:
     bool isMultiLayerNearby(QGraphicsSceneDragDropEvent *event);
     void adjustSceneRect();
     bool isAcceptedByMultiLayer(const DesignerMimeData *mimeData, QGraphicsSceneDragDropEvent *event);
+    bool isLayerDragged() const;
 
     SampleModel *m_sampleModel;
     InstrumentModel *m_instrumentModel;
-- 
GitLab