File: qt-graphics-toolkit.cc

package info (click to toggle)
octave 9.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 144,300 kB
  • sloc: cpp: 332,784; ansic: 77,239; fortran: 20,963; objc: 9,396; sh: 8,213; yacc: 4,925; lex: 4,389; perl: 1,544; java: 1,366; awk: 1,259; makefile: 648; xml: 189
file content (509 lines) | stat: -rw-r--r-- 15,232 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2011-2024 The Octave Project Developers
//
// See the file COPYRIGHT.md in the top-level directory of this
// distribution or <https://octave.org/copyright/>.
//
// This file is part of Octave.
//
// Octave is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Octave is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Octave; see the file COPYING.  If not, see
// <https://www.gnu.org/licenses/>.
//
////////////////////////////////////////////////////////////////////////

#if defined (HAVE_CONFIG_H)
#  include "config.h"
#endif

#include <cstdint>

#include <QApplication>
#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 "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
//# define OCTAVE_PTR_TYPE octave_uint32
//# define OCTAVE_INTPTR_TYPE uint32_t
//# define OCTAVE_PTR_SCALAR uint32_scalar_value
//#else
# define OCTAVE_PTR_TYPE octave_uint64
# define OCTAVE_INTPTR_TYPE uint64_t
# define OCTAVE_PTR_SCALAR uint64_scalar_value
//#endif

OCTAVE_BEGIN_NAMESPACE(octave)

static std::string
toolkitObjectProperty (const graphics_object& go)
{
  if (go.isa ("figure"))
    return "__plot_stream__";
  else if (go.isa ("uicontrol")
           || go.isa ("uipanel")
           || go.isa ("uibuttongroup")
           || go.isa ("uimenu")
           || go.isa ("uicontextmenu")
           || go.isa ("uitable")
           || go.isa ("uitoolbar")
           || go.isa ("uipushtool")
           || go.isa ("uitoggletool"))
    return "__object__";
  else
    qCritical ("octave::qt_graphics_toolkit: no __object__ property known for object "
               "of type %s", go.type ().c_str ());

  return "";
}

qt_graphics_toolkit::qt_graphics_toolkit (octave::interpreter& interp)
  : QObject (), base_graphics_toolkit ("qt"), m_interpreter (interp)
{
  // 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).
  // We need to make sure the GUI Object and its proxy are properly
  // created before the initialize method returns, so we use a
  // BlockingQueuedConnection. After the signal is emitted, the interpreter
  // thread is locked until the slot has returned.

  connect (this, &qt_graphics_toolkit::create_object_signal,
           this, &qt_graphics_toolkit::create_object,
           Qt::BlockingQueuedConnection);
}

bool
qt_graphics_toolkit::initialize (const graphics_object& go)
{
  if (go.isa ("figure")
      || (go.isa ("uicontrol") && go.get ("style").string_value () != "frame")
      || go.isa ("uipanel")
      || go.isa ("uibuttongroup")
      || go.isa ("uimenu")
      || go.isa ("uicontextmenu")
      || go.isa ("uitable")
      || go.isa ("uitoolbar")
      || go.isa ("uipushtool")
      || go.isa ("uitoggletool"))
    {
      // FIXME: We need to unlock the mutex here but we have no way to know
      // if it was previously locked by this thread, and thus if we should
      // re-lock it.

      gh_manager& gh_mgr = m_interpreter.get_gh_manager ();

      gh_mgr.unlock ();

      Logger::debug ("qt_graphics_toolkit::initialize %s from thread %p",
                     go.type ().c_str (), QThread::currentThreadId ());

      ObjectProxy *proxy = new ObjectProxy ();
      graphics_object gObj (go);

      OCTAVE_PTR_TYPE tmp (reinterpret_cast<OCTAVE_INTPTR_TYPE> (proxy));
      gObj.get_properties ().set (toolkitObjectProperty (go), tmp);

      emit create_object_signal (go.get_handle ().value ());

      return true;
    }

  return false;
}

void
qt_graphics_toolkit::update (const graphics_object& go, int pId)
{
  // Rule out obvious properties we want to ignore.
  if (pId == figure::properties::ID___PLOT_STREAM__
      || pId == uicontrol::properties::ID___OBJECT__
      || pId == uipanel::properties::ID___OBJECT__
      || pId == uibuttongroup::properties::ID___OBJECT__
      || pId == uimenu::properties::ID___OBJECT__
      || pId == uicontextmenu::properties::ID___OBJECT__
      || pId == uitable::properties::ID___OBJECT__
      || pId == uitoolbar::properties::ID___OBJECT__
      || pId == uipushtool::properties::ID___OBJECT__
      || pId == uitoggletool::properties::ID___OBJECT__
      || pId == base_properties::ID___MODIFIED__)
    return;

  Logger::debug ("qt_graphics_toolkit::update %s(%d) from thread %p",
                 go.type ().c_str (), pId, QThread::currentThreadId ());

  ObjectProxy *proxy = toolkitObjectProxy (go);

  if (proxy)
    {
      if ((go.isa ("uicontrol")
           && pId == uicontrol::properties::ID_STYLE)
          || (go.isa ("uitable")
              && pId == uitable::properties::ID_DATA))
        {
          // Special case: we need to recreate the control widget
          // associated with the octave graphics_object
          // FIXME: For uitable, it would only be necessary to recreate
          // the table widget if the type of the displayed values changes
          // between Boolean and non-Boolean (bug #63388).

          finalize (go);
          initialize (go);
        }
      else
        proxy->update (pId);
    }
}

void
qt_graphics_toolkit::finalize (const graphics_object& go)
{
  // FIXME: We need to unlock the mutex here but we have no way to know if
  // if it was previously locked by this thread, and thus if we should
  // re-lock it.

  gh_manager& gh_mgr = m_interpreter.get_gh_manager ();

  gh_mgr.unlock ();

  Logger::debug ("qt_graphics_toolkit::finalize %s from thread %p",
                 go.type ().c_str (), QThread::currentThreadId ());

  ObjectProxy *proxy = toolkitObjectProxy (go);

  if (proxy)
    {
      proxy->finalize ();
      delete proxy;

      graphics_object gObj (go);

      gObj.get_properties ().set (toolkitObjectProperty (go), Matrix ());
    }
}

void
qt_graphics_toolkit::redraw_figure (const graphics_object& go) const
{
  if (go.get_properties ().is_visible ())
    {
      ObjectProxy *proxy = toolkitObjectProxy (go);

      if (proxy)
        proxy->redraw ();
    }
}

void
qt_graphics_toolkit::show_figure (const graphics_object& go) const
{
  if (go.get_properties ().is_visible ())
    {
      ObjectProxy *proxy = toolkitObjectProxy (go);

      if (proxy)
        proxy->show ();
    }
}

void
qt_graphics_toolkit::print_figure (const graphics_object& go,
                                   const std::string& term,
                                   const std::string& file_cmd,
                                   const std::string& /*debug_file*/) const
{
  ObjectProxy *proxy = toolkitObjectProxy (go);

  if (proxy)
    proxy->print (QString::fromStdString (file_cmd),
                  QString::fromStdString (term));
}

uint8NDArray
qt_graphics_toolkit::get_pixels (const graphics_object& go) const
{
  uint8NDArray retval;

  if (go.isa ("figure"))
    {
      ObjectProxy *proxy = toolkitObjectProxy (go);

      if (proxy)
        retval = proxy->get_pixels ();
    }

  return retval;
}

Matrix
qt_graphics_toolkit::get_text_extent (const graphics_object& go) const
{
  Matrix ext (1, 4, 0.0);

  if (go.isa ("uicontrol"))
    {
      octave_value str = go.get ("string");
      if (! str.isempty ())
        {
          const uicontrol::properties& up =
            dynamic_cast<const uicontrol::properties&> (go.get_properties ());
          Matrix bb = up.get_boundingbox (false);
          QFont font = Utils::computeFont<uicontrol> (up, bb(3));
          QFontMetrics fm (font);

          QString s;
          QSize sz;

          if (str.is_string ())
            {
              s = QString::fromStdString (str.string_value ());
              sz = fm.size (Qt::TextSingleLine, s);
              ext(2) = sz.width ();
              ext(3) = sz.height ();
            }
          else if (str.iscellstr ())
            {
              string_vector sv = str.string_vector_value ();
              double wd = 0.0;
              double hg = 0.0;
              for (octave_idx_type ii = 0; ii < sv.numel (); ii++)
                {
                  s = QString::fromStdString (sv(ii));
                  sz = fm.size (Qt::TextSingleLine, s);
                  wd = std::max (wd, static_cast<double> (sz.width ()));
                  hg = std::max (hg, static_cast<double> (sz.height ()));
                }

              ext(2) = wd;
              // FIXME: Find a better way to determine the height of e.g.
              // listbox uicontrol objects
              ext(3) = hg * sv.numel ();
            }
        }
    }

  return ext;
}

Object *
qt_graphics_toolkit::toolkitObject (const graphics_object& go)
{
  ObjectProxy *proxy = toolkitObjectProxy (go);

  if (proxy)
    return proxy->object ();

  return nullptr;
}

ObjectProxy *
qt_graphics_toolkit::toolkitObjectProxy (const graphics_object& go)
{
  if (go)
    {
      octave_value ov = go.get (toolkitObjectProperty (go));

      if (ov.is_defined () && ! ov.isempty ())
        {
          OCTAVE_INTPTR_TYPE ptr = ov.OCTAVE_PTR_SCALAR ().value ();

          return reinterpret_cast<ObjectProxy *> (ptr);
        }
    }

  return nullptr;
}

void
qt_graphics_toolkit::interpreter_event (const octave::fcn_callback& fcn)
{
  octave::event_manager& evmgr = m_interpreter.get_event_manager ();

  evmgr.post_event (fcn);
}

void
qt_graphics_toolkit::interpreter_event (const octave::meth_callback& meth)
{
  octave::event_manager& evmgr = m_interpreter.get_event_manager ();

  evmgr.post_event (meth);
}

void
qt_graphics_toolkit::create_object (double handle)
{
  gh_manager& gh_mgr = m_interpreter.get_gh_manager ();

  octave::autolock guard (gh_mgr.graphics_lock ());

  graphics_object go (gh_mgr.get_object (graphics_handle (handle)));

  if (! go.valid_object ())
    {
      qWarning ("qt_graphics_toolkit::create_object: invalid object for handle %g",
                handle);
      return;
    }

  if (go.get_properties ().is_beingdeleted ())
    {
      qWarning ("qt_graphics_toolkit::create_object: object is being deleted");
      return;
    }

  ObjectProxy *proxy = qt_graphics_toolkit::toolkitObjectProxy (go);

  if (! proxy)
    {
      qWarning ("qt_graphics_toolkit::create_object: no proxy for handle %g",
                handle);
      return;
    }

  Logger::debug ("qt_graphics_toolkit::create_object: "
                 "create %s from thread %p",
                 go.type ().c_str (), QThread::currentThreadId ());

  Object *obj = nullptr;

  if (go.isa ("figure"))
    obj = Figure::create (m_interpreter, go);
  else if (go.isa ("uicontrol"))
    {
      uicontrol::properties& up =
        Utils::properties<uicontrol> (go);

      if (up.style_is ("pushbutton"))
        obj = PushButtonControl::create (m_interpreter, go);
      else if (up.style_is ("edit"))
        obj = EditControl::create (m_interpreter, go);
      else if (up.style_is ("checkbox"))
        obj = CheckBoxControl::create (m_interpreter, go);
      else if (up.style_is ("radiobutton"))
        obj = RadioButtonControl::create (m_interpreter, go);
      else if (up.style_is ("togglebutton"))
        obj = ToggleButtonControl::create (m_interpreter, go);
      else if (up.style_is ("text"))
        obj = TextControl::create (m_interpreter, go);
      else if (up.style_is ("popupmenu"))
        obj = PopupMenuControl::create (m_interpreter, go);
      else if (up.style_is ("slider"))
        obj = SliderControl::create (m_interpreter, go);
      else if (up.style_is ("listbox"))
        obj = ListBoxControl::create (m_interpreter, go);
    }
  else if (go.isa ("uibuttongroup"))
    obj = ButtonGroup::create (m_interpreter, go);
  else if (go.isa ("uipanel"))
    obj = Panel::create (m_interpreter, go);
  else if (go.isa ("uimenu"))
    obj = Menu::create (m_interpreter, go);
  else if (go.isa ("uicontextmenu"))
    obj = ContextMenu::create (m_interpreter, go);
  else if (go.isa ("uitable"))
    obj = Table::create (m_interpreter, go);
  else if (go.isa ("uitoolbar"))
    obj = ToolBar::create (m_interpreter, go);
  else if (go.isa ("uipushtool"))
    obj = PushTool::create (m_interpreter, go);
  else if (go.isa ("uitoggletool"))
    obj = ToggleTool::create (m_interpreter, go);
  else
    qWarning ("qt_graphics_toolkit::create_object: unsupported type '%s'",
              go.type ().c_str ());

  if (obj)
    {
      proxy->setObject (obj);
      obj->do_connections (this);
    }
}

void qt_graphics_toolkit::gh_callback_event (const graphics_handle& h,
                                             const std::string& nm)
{
  gh_manager& gh_mgr = m_interpreter.get_gh_manager ();

  gh_mgr.post_callback (h, nm);
}

void qt_graphics_toolkit::gh_callback_event (const graphics_handle& h,
                                             const std::string& nm,
                                             const octave_value& data)
{
  gh_manager& gh_mgr = m_interpreter.get_gh_manager ();

  gh_mgr.post_callback (h, nm, data);
}

void qt_graphics_toolkit::gh_set_event (const graphics_handle& h,
                                        const std::string& nm,
                                        const octave_value& value)
{
  gh_manager& gh_mgr = m_interpreter.get_gh_manager ();

  gh_mgr.post_set (h, nm, value);
}

void qt_graphics_toolkit::gh_set_event (const graphics_handle& h,
                                        const std::string& nm,
                                        const octave_value& value,
                                        bool notify_toolkit)
{
  gh_manager& gh_mgr = m_interpreter.get_gh_manager ();

  gh_mgr.post_set (h, nm, value, notify_toolkit);
}

void qt_graphics_toolkit::gh_set_event (const graphics_handle& h,
                                        const std::string& nm,
                                        const octave_value& value,
                                        bool notify_toolkit,
                                        bool redraw_figure)
{
  gh_manager& gh_mgr = m_interpreter.get_gh_manager ();

  gh_mgr.post_set (h, nm, value, notify_toolkit, redraw_figure);
}

OCTAVE_END_NAMESPACE(octave);