diff libgui/graphics/qt-graphics-toolkit.cc @ 27318:ae53e56e16f2

eliminate separate ObjectFactory class in Qt graphics toolkit * __init_qt__.cc (__init__): Move qt_graphics_toolkit object to GUI application thread. * qt-graphics-toolkit.h, qt-graphics-toolkit.cc (qt_graphics_toolkit::m_factory): Delete data member and all use. (qt_graphics_toolkit::create_object_signal): Rename from createObject. Eliminate pointer to qt_graphics_toolkit argument. (qt_graphics_toolkit::create_object): New slot, adapted from ObjectFactory::createObject slot. * ObjectFactory.h, ObjectFactory.cc: Delete. * libgui/graphics/module.mk: Update.
author John W. Eaton <jwe@octave.org>
date Fri, 02 Aug 2019 14:28:48 -0500
parents 718116e9c7d3
children 6b2d20317b26
line wrap: on
line diff
--- a/libgui/graphics/qt-graphics-toolkit.cc	Fri Aug 02 12:18:48 2019 -0500
+++ b/libgui/graphics/qt-graphics-toolkit.cc	Fri Aug 02 14:28:48 2019 -0500
@@ -30,14 +30,32 @@
 #include <QFontMetrics>
 #include <QThread>
 
+#include "ButtonGroup.h"
+#include "CheckBoxControl.h"
+#include "ContextMenu.h"
+#include "EditControl.h"
+#include "Figure.h"
+#include "ListBoxControl.h"
 #include "Logger.h"
+#include "Menu.h"
 #include "Object.h"
-#include "ObjectFactory.h"
 #include "ObjectProxy.h"
+#include "Panel.h"
+#include "PopupMenuControl.h"
+#include "PushButtonControl.h"
+#include "PushTool.h"
 #include "QtHandlesUtils.h"
+#include "RadioButtonControl.h"
+#include "SliderControl.h"
+#include "Table.h"
+#include "TextControl.h"
+#include "ToggleButtonControl.h"
+#include "ToggleTool.h"
+#include "ToolBar.h"
 #include "qt-graphics-toolkit.h"
 
 #include "event-manager.h"
+#include "graphics.h"
 #include "interpreter.h"
 
 //#if INTPTR_MAX == INT32_MAX
@@ -76,20 +94,15 @@
   }
 
   qt_graphics_toolkit::qt_graphics_toolkit (octave::interpreter& interp)
-    : QObject (), base_graphics_toolkit ("qt"), m_interpreter (interp),
-      m_factory (new ObjectFactory ())
+    : QObject (), base_graphics_toolkit ("qt"), m_interpreter (interp)
   {
-    if (QThread::currentThread () != QApplication::instance ()->thread ())
-      m_factory->moveToThread (QApplication::instance ()->thread ());
+    // Implemented with a signal/slot connection in order to properly
+    // cross from the interpreter thread (where requests to create
+    // graphics object are initiated) to the GUI application thread
+    // (where they are actually created and displayed).
 
-    connect (this, SIGNAL (createObject (qt_graphics_toolkit *, double)),
-             m_factory, SLOT (createObject (qt_graphics_toolkit *, double)),
-             Qt::BlockingQueuedConnection);
-  }
-
-  qt_graphics_toolkit::~qt_graphics_toolkit (void)
-  {
-    delete m_factory;
+    connect (this, SIGNAL (create_object_signal (double)),
+             this, SLOT (create_object (double)));
   }
 
   bool
@@ -120,7 +133,7 @@
         OCTAVE_PTR_TYPE tmp (reinterpret_cast<OCTAVE_INTPTR_TYPE> (proxy));
         gObj.get_properties ().set (toolkitObjectProperty (go), tmp);
 
-        emit createObject (this, go.get_handle ().value ());
+        emit create_object_signal (go.get_handle ().value ());
 
         return true;
       }
@@ -337,4 +350,99 @@
 
     evmgr.post_event (meth);
   }
+
+  void
+  qt_graphics_toolkit::create_object (double handle)
+  {
+    gh_manager::auto_lock lock;
+
+    graphics_object go (gh_manager::get_object (graphics_handle (handle)));
+
+    if (go.valid_object ())
+      {
+        if (go.get_properties ().is_beingdeleted ())
+          qWarning ("qt_graphics_toolkit::create_object: object is being deleted");
+        else
+          {
+            ObjectProxy *proxy = qt_graphics_toolkit::toolkitObjectProxy (go);
+
+            if (proxy)
+              {
+                Logger::debug ("qt_graphics_toolkit::create_object: "
+                               "create %s from thread %08x",
+                               go.type ().c_str (), QThread::currentThreadId ());
+
+                Object *obj = nullptr;
+
+                if (go.isa ("figure"))
+                  obj = Figure::create (go);
+                else if (go.isa ("uicontrol"))
+                  {
+                    uicontrol::properties& up =
+                      Utils::properties<uicontrol> (go);
+
+                    if (up.style_is ("pushbutton"))
+                      obj = PushButtonControl::create (go);
+                    else if (up.style_is ("edit"))
+                      obj = EditControl::create (go);
+                    else if (up.style_is ("checkbox"))
+                      obj = CheckBoxControl::create (go);
+                    else if (up.style_is ("radiobutton"))
+                      obj = RadioButtonControl::create (go);
+                    else if (up.style_is ("togglebutton"))
+                      obj = ToggleButtonControl::create (go);
+                    else if (up.style_is ("text"))
+                      obj = TextControl::create (go);
+                    else if (up.style_is ("popupmenu"))
+                      obj = PopupMenuControl::create (go);
+                    else if (up.style_is ("slider"))
+                      obj = SliderControl::create (go);
+                    else if (up.style_is ("listbox"))
+                      obj = ListBoxControl::create (go);
+                  }
+                else if (go.isa ("uibuttongroup"))
+                  obj = ButtonGroup::create (go);
+                else if (go.isa ("uipanel"))
+                  obj = Panel::create (go);
+                else if (go.isa ("uimenu"))
+                  obj = Menu::create (go);
+                else if (go.isa ("uicontextmenu"))
+                  obj = ContextMenu::create (go);
+                else if (go.isa ("uitable"))
+                  obj = Table::create (go);
+                else if (go.isa ("uitoolbar"))
+                  obj = ToolBar::create (go);
+                else if (go.isa ("uipushtool"))
+                  obj = PushTool::create (go);
+                else if (go.isa ("uitoggletool"))
+                  obj = ToggleTool::create (go);
+                else
+                  qWarning ("qt_graphics_toolkit::create_object: unsupported type '%s'",
+                            go.type ().c_str ());
+
+                if (obj)
+                  {
+                    proxy->setObject (obj);
+
+                    connect (obj,
+                             SIGNAL (interpreter_event (const octave::fcn_callback&)),
+                             this,
+                             SLOT (interpreter_event (const octave::fcn_callback&)));
+
+                    connect (obj,
+                             SIGNAL (interpreter_event (const octave::meth_callback&)),
+                             this,
+                             SLOT (interpreter_event (const octave::meth_callback&)));
+                  }
+              }
+            else
+              qWarning ("qt_graphics_toolkit::create_object: no proxy for handle %g",
+                        handle);
+          }
+      }
+    else
+      qWarning ("qt_graphics_toolkit::create_object: invalid object for handle %g",
+                handle);
+  }
+
 };