GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
ObjectProxy.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2011-2018 Michael Goffioul
4 
5 This file is part of Octave.
6 
7 Octave is free software: you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <https://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if defined (HAVE_CONFIG_H)
24 # include "config.h"
25 #endif
26 
27 #include <QCoreApplication>
28 #include <QString>
29 #include <QThread>
30 
31 #include "oct-mutex.h"
32 #include "utils.h"
33 
34 #include "Object.h"
35 #include "ObjectProxy.h"
36 
37 namespace QtHandles
38 {
39 
41  : QObject (), m_object (nullptr)
42  {
43  init (obj);
44  }
45 
46  void
48  {
49  if (obj != m_object)
50  {
51  if (m_object)
52  {
53  disconnect (this, SIGNAL (sendUpdate (int)),
54  m_object, SLOT (slotUpdate (int)));
55  disconnect (this, SIGNAL (sendFinalize (void)),
56  m_object, SLOT (slotFinalize (void)));
57  disconnect (this, SIGNAL (sendRedraw (void)),
58  m_object, SLOT (slotRedraw (void)));
59  disconnect (this, SIGNAL (sendPrint (const QString&, const QString&)),
60  m_object, SLOT (slotPrint (const QString&, const QString&)));
61  }
62 
63  m_object = obj;
64 
65  if (m_object)
66  {
67  connect (this, SIGNAL (sendUpdate (int)),
68  m_object, SLOT (slotUpdate (int)));
69  connect (this, SIGNAL (sendFinalize (void)),
70  m_object, SLOT (slotFinalize (void)));
71  connect (this, SIGNAL (sendRedraw (void)),
72  m_object, SLOT (slotRedraw (void)));
73  connect (this, SIGNAL (sendPrint (const QString&, const QString&)),
74  m_object, SLOT (slotPrint (const QString&, const QString&)),
75  Qt::BlockingQueuedConnection);
76  }
77  }
78  }
79 
80  void
82  {
83  emit sendFinalize ();
84  init (obj);
85  }
86 
87  void
89  {
91  emit sendUpdate (pId);
92  else if (m_object)
93  m_object->slotUpdate (pId);
94  }
95 
96  void
98  {
99  emit sendFinalize ();
100  init (nullptr);
101  }
102 
103  void
105  {
106  emit sendRedraw ();
107  }
108 
109  void
110  ObjectProxy::print (const QString& file_cmd, const QString& term)
111  {
112  emit sendPrint (file_cmd, term);
113  }
114 
117  {
119 
120  // The ObjectProxy is generally ran from the interpreter thread
121  // while the actual Figure (Object) lives in the gui thread. The
122  // following ensures synchronous execution of the Figure method and
123  // allows retrieving a return value.
124 
125  Qt::ConnectionType t = Qt::BlockingQueuedConnection;
126 
127  if (QThread::currentThread () == QCoreApplication::instance ()->thread ())
128  t = Qt::DirectConnection;
129 
130  QMetaObject::invokeMethod (m_object, "slotGetPixels", t,
131  Q_RETURN_ARG (uint8NDArray, retval));
132 
133  // FIXME: The following may fail for obscure reasons, see bug #53328.
134  // In absence of a solution, we retry twice before calling error().
135  if (! QMetaObject::invokeMethod (m_object, "slotGetPixels", t,
136  Q_RETURN_ARG (uint8NDArray, retval)))
137  {
138  octave_sleep (0.1);
139  if (! QMetaObject::invokeMethod (m_object, "slotGetPixels", t,
140  Q_RETURN_ARG (uint8NDArray, retval)))
141  {
142  octave_sleep (0.2);
143  if (! QMetaObject::invokeMethod (m_object, "slotGetPixels", t,
144  Q_RETURN_ARG (uint8NDArray, retval)))
145  error ("getframe: unable to retrieve figure pixels");
146  }
147  }
148 
149  return retval;
150  }
151 
152 };
ObjectProxy(Object *obj=nullptr)
Definition: ObjectProxy.cc:40
OCTINTERP_API void octave_sleep(double seconds)
void setObject(Object *obj)
Definition: ObjectProxy.cc:81
void sendPrint(const QString &file_cmd, const QString &term)
void slotUpdate(int pId)
Definition: Object.cc:85
void error(const char *fmt,...)
Definition: error.cc:578
void update(int pId)
Definition: ObjectProxy.cc:88
OCTAVE_EXPORT octave_value_list return the number of command line arguments passed to Octave If called with the optional argument the function t
Definition: ov-usr-fcn.cc:997
void init(Object *obj)
Definition: ObjectProxy.cc:47
octave_value retval
Definition: data.cc:6246
void sendUpdate(int pId)
uint8NDArray get_pixels(void)
Definition: ObjectProxy.cc:116
static bool is_thread(void)
void print(const QString &file_cmd, const QString &term)
Definition: ObjectProxy.cc:110