# HG changeset patch
# User Remi Thebault <remi.thebault@gmail.com>
# Date 1603668358 -3600
#      Mon Oct 26 00:25:58 2020 +0100
# Node ID 1d0a453bbce2a7e0da315197a893119ef6b52de3
# Parent  c3970f97f9b8f31f7df25acd9f02317cfd01f149
enable mouse move of plot legends in pan mode

* libgui/graphics/Canvas.h: add m_movingLegend field
* libgui/graphics/Canvas.cc: change mouse press/move/release events to handle
                             moving legend with mouse

diff -r c3970f97f9b8 -r 1d0a453bbce2 libgui/graphics/Canvas.cc
--- a/libgui/graphics/Canvas.cc	Sun Oct 25 09:41:51 2020 +0100
+++ b/libgui/graphics/Canvas.cc	Mon Oct 26 00:25:58 2020 +0100
@@ -471,26 +471,48 @@ namespace QtHandles
             break;
 
           case PanMode:
-            {
-              axes::properties& ap = Utils::properties<axes> (ax);
+            if (m_movingLegend)
+              {
+                axes::properties &ap = Utils::properties<axes> (ax);
+
+                graphics_object figObj (ax.get_ancestor ("figure"));
+                figure::properties &fp = Utils::properties<figure> (figObj);
+
+                Matrix fpos = fp.get_position ().matrix_value ();
 
-              graphics_object figObj (ax.get_ancestor ("figure"));
+                double curr_x = m_mouseCurrent.x () / fpos (2);
+                double curr_y = (fpos (3) - m_mouseCurrent.y ()) / fpos (3);
+                double new_x = event->x () / fpos (2);
+                double new_y = (fpos (3) - event->y ()) / fpos (3);
 
-              std::string mode = pan_mode (figObj);
+                Matrix pos = ap.get_position ().matrix_value ();
+                pos (0) += new_x - curr_x;
+                pos (1) += new_y - curr_y;
+                ap.set_position (pos);
 
-              ColumnVector p0 = ap.pixel2coord (m_mouseCurrent.x (),
-                                                m_mouseCurrent.y ());
-              ColumnVector p1 = ap.pixel2coord (event->x (),
-                                                event->y ());
+                m_mouseCurrent = event->pos ();
+                redraw (true);
+              }
+            else
+              {
+                axes::properties &ap = Utils::properties<axes> (ax);
 
-              ap.translate_view (mode, p0(0), p1(0), p0(1), p1(1));
+                graphics_object figObj (ax.get_ancestor ("figure"));
+
+                std::string mode = pan_mode (figObj);
 
-              // Update current mouse position
-              m_mouseCurrent = event->pos ();
+                ColumnVector p0 = ap.pixel2coord (m_mouseCurrent.x (),
+                                                  m_mouseCurrent.y ());
+                ColumnVector p1 = ap.pixel2coord (event->x (), event->y ());
+
+                ap.translate_view (mode, p0 (0), p1 (0), p0 (1), p1 (1));
 
-              // Force immediate redraw
-              redraw (true);
-            }
+                // Update current mouse position
+                m_mouseCurrent = event->pos ();
+
+                // Force immediate redraw
+                redraw (true);
+              }
 
           default:
             break;
@@ -630,6 +652,13 @@ namespace QtHandles
           Utils::properties<figure> (figObj)
             .set_currentaxes (axesObj.get_handle ().as_octave_value ());
 
+        // check if legend is selected
+        bool valid_legend = axesObj.valid_object ()
+          && axesObj.get_properties ().handlevisibility_is ("on")
+          && axesObj.get_properties ().get_tag () == "legend";
+
+        m_movingLegend = false;
+
         Figure *fig = dynamic_cast<Figure *> (qt_graphics_toolkit::toolkitObject (figObj));
 
         MouseMode newMouseMode = NoMode;
@@ -696,6 +725,16 @@ namespace QtHandles
             break;
 
           case PanMode:
+            if (valid_legend && event->modifiers () == Qt::NoModifier
+                && event->buttons () == Qt::LeftButton)
+              {
+                m_mouseAnchor = m_mouseCurrent = event->pos ();
+                m_mouseAxes = axesObj.get_handle ();
+                m_mouseMode = newMouseMode;
+                m_movingLegend = true;
+                break;
+              }
+            // fallthrough
           case RotateMode:
           case ZoomInMode:
           case ZoomOutMode:
@@ -892,6 +931,7 @@ namespace QtHandles
     m_rectMode = false;
     m_mouseAxes = graphics_handle ();
     m_mouseMode = NoMode;
+    m_movingLegend = false;
   }
 
   void
diff -r c3970f97f9b8 -r 1d0a453bbce2 libgui/graphics/Canvas.h
--- a/libgui/graphics/Canvas.h	Sun Oct 25 09:41:51 2020 +0100
+++ b/libgui/graphics/Canvas.h	Mon Oct 26 00:25:58 2020 +0100
@@ -127,7 +127,8 @@ namespace QtHandles
         m_mouseMode (NoMode),
         m_clickMode (false),
         m_eventMask (0),
-        m_rectMode (false)
+        m_rectMode (false),
+        m_movingLegend (false)
     { }
 
     void canvasToggleAxes (const graphics_handle& handle);
@@ -169,6 +170,7 @@ namespace QtHandles
     graphics_handle m_mouseAxes;
     int m_eventMask;
     bool m_rectMode;
+    bool m_movingLegend;
   };
 
 }
