# HG changeset patch
# User Pantxo Diribarne <pantxo.diribarne@gmail.com>
# Date 1559054628 -7200
#      Tue May 28 16:43:48 2019 +0200
# Node ID b7f3efb45ca9b5a24a79f1fa2146a2a6232e5a63
# Parent  c2a8365c73fc54ea13e2936eb0a7b077eff88a1e
Implement figure "windowscrollwheelfcn" property for Qt toolkit (bug #56028)

* QtHandlesUtils.[h,cc] (makeScrollEventStruct): New method to prepare a
  ScrollWheel event structure.

* Canvas.cc (Canvas::canvasWheelEvent): Post "windowscrollwheelfcn" whenever
  a WheelEvent happens.

* NEWS: Announce implementation.

diff -r c2a8365c73fc -r b7f3efb45ca9 NEWS
--- a/NEWS	Tue May 28 06:44:55 2019 +0200
+++ b/NEWS	Tue May 28 16:43:48 2019 +0200
@@ -21,7 +21,11 @@
   rendering of characters.  The default is "on" which produces smooth,
   more visually appealing text.
 
-- The graphics properties `"pointer"`, `"pointershapecdata"`, and
+- The figure property `"windowscrollwheelfcn"`is now implemented.
+  This makes it possible to provide a callback function to be executed when
+  users manipulate the mouse wheel on a given figure.
+
+- The figure properties `"pointer"`, `"pointershapecdata"`, and
   `"pointershapehotspot"` are now implemented.  This makes it possible
   to change the shape of the cursor (pointer in Matlab-speak) displayed
   in a plot window.
diff -r c2a8365c73fc -r b7f3efb45ca9 doc/interpreter/genpropdoc.m
--- a/doc/interpreter/genpropdoc.m	Tue May 28 06:44:55 2019 +0200
+++ b/doc/interpreter/genpropdoc.m	Tue May 28 16:43:48 2019 +0200
@@ -594,7 +594,19 @@
         s.valid = valid_fcn;
 
       case "windowscrollwheelfcn"
-        s.doc = doc_unused;
+        s.doc = "Function that is executed when a user manipulates \
+the mouse wheel over this figure.  \
+The function is called with two input arguments.  The first \
+argument holds the handle of the calling figure.  The second argument holds \
+an event structure which has the following members:\n\
+@table @code\n\
+@item VerticalScrollCount:\n\
+The number of wheel steps, typically 1 when scrolling down and -1 when \
+scrolling up.\n\
+@item VerticalScrollAmount:\n\
+The number of lines a wheel step should scroll. This value is always 3.\n\
+@end table\
+\n\n__fcnmsg__";
         s.valid = valid_fcn;
 
       case "windowstyle"
diff -r c2a8365c73fc -r b7f3efb45ca9 libgui/graphics/Canvas.cc
--- a/libgui/graphics/Canvas.cc	Tue May 28 06:44:55 2019 +0200
+++ b/libgui/graphics/Canvas.cc	Tue May 28 16:43:48 2019 +0200
@@ -906,6 +906,8 @@
       {
         std::string mode;
 
+        graphics_object figObj (obj.get_ancestor ("figure"));
+
         graphics_object axesObj;
 
         Matrix children = obj.get_properties ().get_children ();
@@ -931,8 +933,6 @@
           {
             MouseMode newMouseMode = NoMode;
 
-            graphics_object figObj (obj.get_ancestor ("figure"));
-
             Figure *fig = dynamic_cast<Figure *> (Backend::toolkitObject (figObj));
 
             if (fig)
@@ -1006,6 +1006,13 @@
             if (redrawFigure)
               redraw (false);
           }
+        
+        if (! figObj.get ("windowscrollwheelfcn").isempty ())
+          {
+            octave_scalar_map eventData = Utils::makeScrollEventStruct (event);
+            gh_manager::post_callback (m_handle, "windowscrollwheelfcn",
+                                       eventData);
+          }
       }
   }
 
diff -r c2a8365c73fc -r b7f3efb45ca9 libgui/graphics/QtHandlesUtils.cc
--- a/libgui/graphics/QtHandlesUtils.cc	Tue May 28 06:44:55 2019 +0200
+++ b/libgui/graphics/QtHandlesUtils.cc	Tue May 28 16:43:48 2019 +0200
@@ -388,6 +388,22 @@
       return retval;
     }
 
+    octave_scalar_map
+    makeScrollEventStruct (QWheelEvent *event)
+    {
+      octave_scalar_map retval;
+
+      // We assume this is a standard mouse with 15 degrees steps and Qt returns
+      // 1/8 of a degree
+      retval.setfield ("VerticalScrollCount",
+                       octave_value (- event->angleDelta().y () / 120));
+      // FIXME: Is there any way to access the number of lines a scroll step
+      // should correpond to?
+      retval.setfield ("VerticalScrollAmount", octave_value (3));
+
+      return retval;
+    }
+
   }
 
 }
diff -r c2a8365c73fc -r b7f3efb45ca9 libgui/graphics/QtHandlesUtils.h
--- a/libgui/graphics/QtHandlesUtils.h	Tue May 28 06:44:55 2019 +0200
+++ b/libgui/graphics/QtHandlesUtils.h	Tue May 28 16:43:48 2019 +0200
@@ -35,6 +35,7 @@
 
 class QKeyEvent;
 class QMouseEvent;
+class QWheelEvent;
 
 namespace QtHandles
 {
@@ -78,6 +79,7 @@
                                int height = -1);
 
     octave_scalar_map makeKeyEventStruct (QKeyEvent *event);
+    octave_scalar_map makeScrollEventStruct (QWheelEvent *event);
   }
 
 }
