GNU Octave  6.2.0
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-2021 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if defined (HAVE_CONFIG_H)
27 # include "config.h"
28 #endif
29 
30 #include <QCoreApplication>
31 #include <QString>
32 #include <QThread>
33 
34 #include "Object.h"
35 #include "ObjectProxy.h"
36 
37 #include "oct-mutex.h"
38 #include "utils.h"
39 
40 namespace QtHandles
41 {
42 
44  : QObject (), m_object (nullptr)
45  {
46  init (obj);
47  }
48 
49  void
51  {
52  if (obj != m_object)
53  {
54  if (m_object)
55  {
56  disconnect (this, SIGNAL (sendUpdate (int)),
57  m_object, SLOT (slotUpdate (int)));
58  disconnect (this, SIGNAL (sendRedraw (void)),
59  m_object, SLOT (slotRedraw (void)));
60  disconnect (this, SIGNAL (sendShow (void)),
61  m_object, SLOT (slotShow (void)));
62  }
63 
64  m_object = obj;
65 
66  if (m_object)
67  {
68  connect (this, SIGNAL (sendUpdate (int)),
69  m_object, SLOT (slotUpdate (int)));
70  connect (this, SIGNAL (sendRedraw (void)),
71  m_object, SLOT (slotRedraw (void)));
72  connect (this, SIGNAL (sendShow (void)),
73  m_object, SLOT (slotShow (void)));
74  }
75  }
76  }
77 
78  void
80  {
81  // Eventually destroy previous Object
82  if (m_object)
83  finalize ();
84 
85  init (obj);
86  }
87 
88  void
90  {
91  emit sendUpdate (pId);
92  }
93 
94  void
96  {
97  if (! m_object)
98  error ("ObjectProxy::finalize: invalid GUI Object");
99 
100  Qt::ConnectionType t = Qt::BlockingQueuedConnection;
101 
102  if (QThread::currentThread () == QCoreApplication::instance ()->thread ())
103  t = Qt::DirectConnection;
104 
105  if (! QMetaObject::invokeMethod (m_object, "slotFinalize", t))
106  error ("ObjectProxy::finalize: unable to delete GUI Object");
107  }
108 
109  void
111  {
112  emit sendRedraw ();
113  }
114 
115  void
117  {
118  emit sendShow ();
119  }
120 
121  void
122  ObjectProxy::print (const QString& file_cmd, const QString& term)
123  {
124  if (! m_object)
125  error ("ObjectProxy::print: invalid GUI Object");
126 
127  Qt::ConnectionType t = Qt::BlockingQueuedConnection;
128 
129  if (QThread::currentThread () == QCoreApplication::instance ()->thread ())
130  t = Qt::DirectConnection;
131 
132  if (! QMetaObject::invokeMethod (m_object, "slotPrint", t,
133  Q_ARG (QString, file_cmd),
134  Q_ARG (QString, term)))
135  error ("ObjectProxy::print: unable to print figure");
136  }
137 
140  {
141  if (! m_object)
142  error ("ObjectProxy::finalize: invalid GUI Object");
143 
145 
146  // The ObjectProxy is generally ran from the interpreter thread
147  // while the actual Figure (Object) lives in the gui thread. The
148  // following ensures synchronous execution of the Figure method and
149  // allows retrieving a return value.
150 
151  Qt::ConnectionType t = Qt::BlockingQueuedConnection;
152 
153  if (QThread::currentThread () == QCoreApplication::instance ()->thread ())
154  t = Qt::DirectConnection;
155 
156  QMetaObject::invokeMethod (m_object, "slotGetPixels", t,
157  Q_RETURN_ARG (uint8NDArray, retval));
158 
159  return retval;
160  }
161 
162 };
void init(Object *obj)
Definition: ObjectProxy.cc:50
uint8NDArray get_pixels(void)
Definition: ObjectProxy.cc:139
void setObject(Object *obj)
Definition: ObjectProxy.cc:79
void print(const QString &file_cmd, const QString &term)
Definition: ObjectProxy.cc:122
ObjectProxy(Object *obj=nullptr)
Definition: ObjectProxy.cc:43
void sendUpdate(int pId)
void update(int pId)
Definition: ObjectProxy.cc:89
void error(const char *fmt,...)
Definition: error.cc:968
octave_value::octave_value(const Array< char > &chm, char type) return retval
Definition: ov.cc:811