GNU Octave  8.1.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-2023 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 
41 
43 : QObject (), m_object (nullptr)
44 {
45  init (obj);
46 }
47 
48 void
50 {
51  if (obj != m_object)
52  {
53  if (m_object)
54  {
55  disconnect (this, &ObjectProxy::sendUpdate,
57  disconnect (this, &ObjectProxy::sendRedraw,
59  disconnect (this, &ObjectProxy::sendShow,
61  }
62 
63  m_object = obj;
64 
65  if (m_object)
66  {
67  connect (this, &ObjectProxy::sendUpdate,
69  connect (this, &ObjectProxy::sendRedraw,
71  connect (this, &ObjectProxy::sendShow,
73  }
74  }
75 }
76 
77 void
79 {
80  // Eventually destroy previous Object
81  if (m_object)
82  finalize ();
83 
84  init (obj);
85 }
86 
87 void
89 {
90  emit sendUpdate (pId);
91 }
92 
93 void
95 {
96  if (! m_object)
97  error ("ObjectProxy::finalize: invalid GUI Object");
98 
99  Qt::ConnectionType t = Qt::BlockingQueuedConnection;
100 
101  if (QThread::currentThread () == QCoreApplication::instance ()->thread ())
102  t = Qt::DirectConnection;
103 
104  if (! QMetaObject::invokeMethod (m_object, "slotFinalize", t))
105  error ("ObjectProxy::finalize: unable to delete GUI Object");
106 }
107 
108 void
110 {
111  emit sendRedraw ();
112 }
113 
114 void
116 {
117  emit sendShow ();
118 }
119 
120 void
121 ObjectProxy::print (const QString& file_cmd, const QString& term)
122 {
123  if (! m_object)
124  error ("ObjectProxy::print: invalid GUI Object");
125 
126  Qt::ConnectionType t = Qt::BlockingQueuedConnection;
127 
128  if (QThread::currentThread () == QCoreApplication::instance ()->thread ())
129  t = Qt::DirectConnection;
130 
131  if (! QMetaObject::invokeMethod (m_object, "slotPrint", t,
132  Q_ARG (QString, file_cmd),
133  Q_ARG (QString, term)))
134  error ("ObjectProxy::print: unable to print figure");
135 }
136 
139 {
140  if (! m_object)
141  error ("ObjectProxy::finalize: invalid GUI Object");
142 
143  uint8NDArray retval;
144 
145  // The ObjectProxy is generally ran from the interpreter thread
146  // while the actual Figure (Object) lives in the gui thread. The
147  // following ensures synchronous execution of the Figure method and
148  // allows retrieving a return value.
149 
150  Qt::ConnectionType t = Qt::BlockingQueuedConnection;
151 
152  if (QThread::currentThread () == QCoreApplication::instance ()->thread ())
153  t = Qt::DirectConnection;
154 
155  QMetaObject::invokeMethod (m_object, "slotGetPixels", t,
156  Q_RETURN_ARG (uint8NDArray, retval));
157 
158  return retval;
159 }
160 
OCTAVE_END_NAMESPACE(octave)
Object * m_object
Definition: ObjectProxy.h:65
void print(const QString &file_cmd, const QString &term)
Definition: ObjectProxy.cc:121
void show(void)
Definition: ObjectProxy.cc:115
void redraw(void)
Definition: ObjectProxy.cc:109
void update(int pId)
Definition: ObjectProxy.cc:88
void finalize(void)
Definition: ObjectProxy.cc:94
void setObject(Object *obj)
Definition: ObjectProxy.cc:78
void init(Object *obj)
Definition: ObjectProxy.cc:49
void sendShow(void)
uint8NDArray get_pixels(void)
Definition: ObjectProxy.cc:138
void sendRedraw(void)
void sendUpdate(int pId)
Definition: Object.h:47
void slotShow(void)
Definition: Object.cc:141
void slotUpdate(int pId)
Definition: Object.cc:97
void slotRedraw(void)
Definition: Object.cc:130
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
void error(const char *fmt,...)
Definition: error.cc:979