# HG changeset patch
# User Pantxo Diribarne <pantxo.diribarne@gmail.com>
# Date 1524601560 -7200
#      Tue Apr 24 22:26:00 2018 +0200
# Branch stable
# Node ID 1d1664e13b40be3fe9d408bd2758a351b4ac1b31
# Parent  fa9993b556176af287a0bc804fdb743b93e7033f
[mq]: grrrr

diff -r fa9993b55617 -r 1d1664e13b40 libgui/graphics/Backend.cc
--- a/libgui/graphics/Backend.cc	Mon Apr 16 09:34:48 2018 +0200
+++ b/libgui/graphics/Backend.cc	Tue Apr 24 22:26:00 2018 +0200
@@ -75,7 +75,8 @@
     ObjectFactory *factory = ObjectFactory::instance ();
 
     connect (this, SIGNAL (createObject (double)),
-             factory, SLOT (createObject (double)));
+             factory, SLOT (createObject (double)),
+             Qt::BlockingQueuedConnection);
   }
 
   Backend::~Backend (void)
@@ -159,7 +160,8 @@
     if (proxy)
       {
         proxy->finalize ();
-        delete proxy;
+        if (proxy)
+          delete proxy;
 
         graphics_object gObj (go);
 
diff -r fa9993b55617 -r 1d1664e13b40 libgui/graphics/ObjectProxy.cc
--- a/libgui/graphics/ObjectProxy.cc	Mon Apr 16 09:34:48 2018 +0200
+++ b/libgui/graphics/ObjectProxy.cc	Tue Apr 24 22:26:00 2018 +0200
@@ -52,8 +52,6 @@
           {
             disconnect (this, SIGNAL (sendUpdate (int)),
                         m_object, SLOT (slotUpdate (int)));
-            disconnect (this, SIGNAL (sendFinalize (void)),
-                        m_object, SLOT (slotFinalize (void)));
             disconnect (this, SIGNAL (sendRedraw (void)),
                         m_object, SLOT (slotRedraw (void)));
             disconnect (this, SIGNAL (sendPrint (const QString&, const QString&)),
@@ -66,8 +64,6 @@
           {
             connect (this, SIGNAL (sendUpdate (int)),
                      m_object, SLOT (slotUpdate (int)));
-            connect (this, SIGNAL (sendFinalize (void)),
-                     m_object, SLOT (slotFinalize (void)));
             connect (this, SIGNAL (sendRedraw (void)),
                      m_object, SLOT (slotRedraw (void)));
             connect (this, SIGNAL (sendPrint (const QString&, const QString&)),
@@ -80,7 +76,7 @@
   void
   ObjectProxy::setObject (Object *obj)
   {
-    emit sendFinalize ();
+    finalize ();
     init (obj);
   }
 
@@ -96,8 +92,15 @@
   void
   ObjectProxy::finalize (void)
   {
-    emit sendFinalize ();
-    init (nullptr);
+    if (! m_object)
+      return;
+    
+    Qt::ConnectionType t = Qt::BlockingQueuedConnection;
+
+    if (QThread::currentThread () == QCoreApplication::instance ()->thread ())
+      t = Qt::DirectConnection;
+
+    QMetaObject::invokeMethod (m_object, "slotFinalize", t);
   }
 
   void
@@ -130,22 +133,6 @@
     QMetaObject::invokeMethod (m_object, "slotGetPixels", t,
                                Q_RETURN_ARG (uint8NDArray, retval));
 
-    // FIXME: The following may fail for obscure reasons, see bug #53328.
-    //        In absence of a solution, we retry twice before calling error().
-    if (! QMetaObject::invokeMethod (m_object, "slotGetPixels", t,
-                                     Q_RETURN_ARG (uint8NDArray, retval)))
-      {
-        octave_sleep (0.1);
-        if (! QMetaObject::invokeMethod (m_object, "slotGetPixels", t,
-                                         Q_RETURN_ARG (uint8NDArray, retval)))
-          {
-            octave_sleep (0.2);
-            if (! QMetaObject::invokeMethod (m_object, "slotGetPixels", t,
-                                             Q_RETURN_ARG (uint8NDArray, retval)))
-              error ("getframe: unable to retrieve figure pixels");
-          }
-      }
-
     return retval;
   }
 
diff -r fa9993b55617 -r 1d1664e13b40 libinterp/corefcn/graphics.cc
--- a/libinterp/corefcn/graphics.cc	Mon Apr 16 09:34:48 2018 +0200
+++ b/libinterp/corefcn/graphics.cc	Tue Apr 24 22:26:00 2018 +0200
@@ -1962,7 +1962,8 @@
 
       for (int k = 0; k < children.numel (); k++)
         finalize_r (children(k));
-
+      
+      gh_manager::unlock ();
       go.finalize ();
     }
 }
@@ -2664,8 +2665,9 @@
       bp.execute_deletefcn ();
 
       // Notify graphics toolkit.
+      unlock ();
       p->second.finalize ();
-
+      
       // Note: this will be valid only for first explicitly deleted
       // object.  All its children will then have an
       // unknown graphics toolkit.
@@ -2969,7 +2971,10 @@
   graphics_object go = gh_manager::get_object (h);
 
   if (go)
-    go.initialize ();
+    {
+      gh_manager::unlock (); 
+      go.initialize ();
+    }
 }
 
 // ---------------------------------------------------------------------
@@ -9432,7 +9437,6 @@
 %!   assert (get (hax, "uicontextmenu"), hctx2);
 %!   assert (get (hf, "children"), [hctx2; hctx1; hax]);
 %!   delete (hctx2);
-%!   pause (.005);
 %!   assert (get (hf, "uicontextmenu"), []);
 %!   assert (get (hax, "uicontextmenu"), []);
 %!   assert (get (hf, "children"), [hctx1; hax]);
@@ -11236,6 +11240,7 @@
 
       xset (h, xargs);
       xcreatefcn (h);
+      
       xinitialize (h);
 
       retval = h.value ();
@@ -11738,24 +11743,10 @@
 
           graphics_object go = gh_manager::get_object (h);
 
-          // FIXME: when using qt toolkit the print_figure method
-          // returns immediately and Canvas::print doesn't have
-          // enough time to lock the mutex before we lock it here
-          // again.  We thus wait 50 ms (which may not be enough) to
-          // give it a chance: see http://octave.1599824.n4.nabble.com/Printing-issues-with-Qt-toolkit-tp4673270.html
-
           gh_manager::unlock ();
 
           go.get_toolkit ().print_figure (go, term, file, debug_file);
 
-          // FIXME: In ObjectProxy.cc ObjectProxy::init
-          // we now use connect (..., Qt::BlockingQueuedConnection)
-          // which should make the sleep unnecessary.
-          // See bug #44463 and #48519
-          // Remove it and the FIXME block above after testing.
-
-          // octave_sleep (0.05);
-
           gh_manager::lock ();
         }
     }
