GNU Octave  8.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
Object.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 <QString>
31 #include <QVariant>
32 
33 #include "Object.h"
34 #include "QtHandlesUtils.h"
35 #include "octave-qobject.h"
36 #include "qt-graphics-toolkit.h"
37 
38 #include "graphics.h"
39 #include "interpreter.h"
40 
42 
44  const graphics_object& go, QObject *obj)
45 : QObject (), m_octave_qobj (oct_qobj), m_interpreter (interp),
46  m_go (go), m_handle (go.get_handle ()), m_qobject (nullptr)
47 {
48  gh_manager& gh_mgr = m_interpreter.get_gh_manager ();
49 
50  octave::autolock guard (gh_mgr.graphics_lock ());
51 
52  if (! guard)
53  qCritical ("octave::Object::Object: "
54  "creating Object (h=%g) without a valid lock!!!",
55  m_handle.value ());
56 
57  init (obj);
58 }
59 
60 void
61 Object::init (QObject *obj, bool)
62 {
63  if (m_qobject)
64  qCritical ("octave::Object::init: "
65  "resetting QObject while in invalid state");
66 
67  m_qobject = obj;
68 
69  if (m_qobject)
70  {
71  m_qobject->setProperty ("octave::Object",
72  QVariant::fromValue<void *> (this));
73  connect (m_qobject, &QObject::destroyed,
75  }
76 }
77 
79 { }
80 
81 graphics_object
82 Object::object (void) const
83 {
84  gh_manager& gh_mgr = m_interpreter.get_gh_manager ();
85 
86  octave::autolock guard (gh_mgr.graphics_lock (), false);
87 
88  if (! guard)
89  qCritical ("octave::Object::object: "
90  "accessing graphics object (h=%g) without a valid lock!!!",
91  m_handle.value ());
92 
93  return m_go;
94 }
95 
96 void
98 {
99  gh_manager& gh_mgr = m_interpreter.get_gh_manager ();
100 
101  octave::autolock guard (gh_mgr.graphics_lock ());
102 
103  switch (pId)
104  {
105  // Special case for objects being deleted, as it's very likely
106  // that the graphics_object already has been destroyed when this
107  // is executed (because of the async behavior).
108  case base_properties::ID_BEINGDELETED:
109  beingDeleted ();
110  break;
111 
112  default:
113  if (object ().valid_object ())
114  update (pId);
115  break;
116  }
117 }
118 
119 void
121 {
122  gh_manager& gh_mgr = m_interpreter.get_gh_manager ();
123 
124  octave::autolock guard (gh_mgr.graphics_lock ());
125 
126  finalize ();
127 }
128 
129 void
131 {
132  gh_manager& gh_mgr = m_interpreter.get_gh_manager ();
133 
134  octave::autolock guard (gh_mgr.graphics_lock ());
135 
136  if (object ().valid_object ())
137  redraw ();
138 }
139 
140 void
142 {
143  gh_manager& gh_mgr = m_interpreter.get_gh_manager ();
144 
145  octave::autolock guard (gh_mgr.graphics_lock ());
146 
147  if (object ().valid_object ())
148  show ();
149 }
150 
151 void
152 Object::slotPrint (const QString& file_cmd, const QString& term)
153 {
154  gh_manager& gh_mgr = m_interpreter.get_gh_manager ();
155 
156  octave::autolock guard (gh_mgr.graphics_lock ());
157 
158  if (object ().valid_object ())
159  print (file_cmd, term);
160 }
161 
162 void
163 Object::update (int /* pId */)
164 { }
165 
166 void
168 {
169  if (m_qobject)
170  {
171  delete m_qobject;
172  m_qobject = nullptr;
173  }
174  deleteLater ();
175 }
176 
177 void
179 { }
180 
181 void
183 { }
184 
185 void
186 Object::print (const QString& /* file_cmd */, const QString& /* term */)
187 { }
188 
189 void
191 { }
192 
194 {
195  if (obj && obj == m_qobject)
196  m_qobject = nullptr;
197 }
198 
199 Object *
200 Object::parentObject (octave::interpreter& interp, const graphics_object& go)
201 {
202  gh_manager& gh_mgr = interp.get_gh_manager ();
203 
204  octave::autolock guard (gh_mgr.graphics_lock ());
205 
207  (gh_mgr.get_object (go.get_parent ()));
208 
209  return parent;
210 }
211 
212 Object *
214 {
215  QVariant v = obj->property ("octave::Object");
216 
217  if (v.isValid ())
218  return reinterpret_cast<Object *> (qvariant_cast<void *> (v));
219 
220  return nullptr;
221 }
222 
223 void
224 Object::do_connections (const QObject *receiver, const QObject *emitter)
225 {
226  if (! emitter)
227  emitter = this;
228 
229  connect (emitter,
230  SIGNAL (interpreter_event (const octave::fcn_callback&)),
231  receiver,
232  SLOT (interpreter_event (const octave::fcn_callback&)));
233 
234  connect (emitter,
235  SIGNAL (interpreter_event (const octave::meth_callback&)),
236  receiver,
237  SLOT (interpreter_event (const octave::meth_callback&)));
238 
239  connect (emitter,
240  SIGNAL (gh_callback_event (const graphics_handle&,
241  const std::string&)),
242  receiver,
243  SLOT (gh_callback_event (const graphics_handle&,
244  const std::string&)));
245 
246  connect (emitter,
247  SIGNAL (gh_callback_event (const graphics_handle&,
248  const std::string&,
249  const octave_value&)),
250  receiver,
251  SLOT (gh_callback_event (const graphics_handle&,
252  const std::string&,
253  const octave_value&)));
254 
255  connect (emitter,
256  SIGNAL (gh_set_event (const graphics_handle&,
257  const std::string&,
258  const octave_value&)),
259  receiver,
260  SLOT (gh_set_event (const graphics_handle&,
261  const std::string&,
262  const octave_value&)));
263 
264  connect (emitter,
265  SIGNAL (gh_set_event (const graphics_handle&,
266  const std::string&,
267  const octave_value&, bool)),
268  receiver,
269  SLOT (gh_set_event (const graphics_handle&,
270  const std::string&,
271  const octave_value&, bool)));
272 
273  connect (emitter,
274  SIGNAL (gh_set_event (const graphics_handle&,
275  const std::string&,
276  const octave_value&,
277  bool, bool)),
278  receiver,
279  SLOT (gh_set_event (const graphics_handle&,
280  const std::string&,
281  const octave_value&,
282  bool, bool)));
283 }
284 
OCTAVE_END_NAMESPACE(octave)
Definition: Object.h:47
void gh_callback_event(const graphics_handle &h, const std::string &name)
virtual void print(const QString &file_cmd, const QString &term)
Definition: Object.cc:186
static Object * parentObject(octave::interpreter &interp, const graphics_object &go)
Definition: Object.cc:200
virtual void do_connections(const QObject *receiver, const QObject *emitter=nullptr)
Definition: Object.cc:224
graphics_handle m_handle
Definition: Object.h:153
static Object * fromQObject(QObject *obj)
Definition: Object.cc:213
void gh_set_event(const graphics_handle &h, const std::string &name, const octave_value &value)
virtual void show(void)
Definition: Object.cc:182
virtual void beingDeleted(void)
Definition: Object.cc:190
graphics_object object(void) const
Definition: Object.cc:82
void objectDestroyed(QObject *obj=nullptr)
Definition: Object.cc:193
void interpreter_event(const octave::fcn_callback &fcn)
void slotPrint(const QString &file_cmd, const QString &term)
Definition: Object.cc:152
virtual void redraw(void)
Definition: Object.cc:178
QObject * m_qobject
Definition: Object.h:155
void slotShow(void)
Definition: Object.cc:141
virtual void finalize(void)
Definition: Object.cc:167
void slotFinalize(void)
Definition: Object.cc:120
virtual ~Object(void)
Definition: Object.cc:78
graphics_object m_go
Definition: Object.h:146
void slotUpdate(int pId)
Definition: Object.cc:97
virtual void update(int pId)
Definition: Object.cc:163
octave::interpreter & m_interpreter
Definition: Object.h:136
void slotRedraw(void)
Definition: Object.cc:130
void init(QObject *obj, bool callBase=false)
Definition: Object.cc:61
Base class for Octave interfaces that use Qt.
double value(void) const
Definition: oct-handle.h:78
static Object * toolkitObject(const graphics_object &go)
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
std::function< void(void)> fcn_callback
Definition: event-manager.h:43
std::function< void(interpreter &)> meth_callback
Definition: event-manager.h:48