GNU Octave  6.2.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
Container.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 <QChildEvent>
31 #include <QVBoxLayout>
32 
33 #include "Canvas.h"
34 #include "Container.h"
35 #include "Object.h"
36 #include "QtHandlesUtils.h"
37 
38 #include "graphics.h"
39 #include "interpreter.h"
40 
41 namespace QtHandles
42 {
43 
45  octave::interpreter& interp)
46  : ContainerBase (xparent), m_octave_qobj (oct_qobj),
47  m_interpreter (interp), m_canvas (nullptr)
48  {
49  setFocusPolicy (Qt::ClickFocus);
50  }
51 
53  { }
54 
55  Canvas*
56  Container::canvas (const graphics_handle& gh, bool xcreate)
57  {
58  if (! m_canvas && xcreate)
59  {
61 
62  octave::autolock guard (gh_mgr.graphics_lock ());
63 
64  graphics_object go = gh_mgr.get_object (gh);
65 
66  if (go)
67  {
68  graphics_object fig = go.get_ancestor ("figure");
69 
71  fig.get ("renderer").string_value ());
72 
73  connect (m_canvas, SIGNAL (interpeter_event (const fcn_callback&)),
74  this, SIGNAL (interpeter_event (const fcn_callback&)));
75 
76  connect (m_canvas, SIGNAL (interpeter_event (const meth_callback&)),
77  this, SIGNAL (interpeter_event (const meth_callback&)));
78 
79  connect (m_canvas,
80  SIGNAL (gh_callback_event (const graphics_handle&,
81  const std::string&)),
82  this,
83  SIGNAL (gh_callback_event (const graphics_handle&,
84  const std::string&)));
85 
86  connect (m_canvas,
87  SIGNAL (gh_callback_event (const graphics_handle&,
88  const std::string&,
89  const octave_value&)),
90  this,
91  SIGNAL (gh_callback_event (const graphics_handle&,
92  const std::string&,
93  const octave_value&)));
94 
95  connect (m_canvas,
96  SIGNAL (gh_set_event (const graphics_handle&,
97  const std::string&,
98  const octave_value&)),
99  this,
100  SIGNAL (gh_set_event (const graphics_handle&,
101  const std::string&,
102  const octave_value&)));
103 
104  connect (m_canvas,
105  SIGNAL (gh_set_event (const graphics_handle&,
106  const std::string&,
107  const octave_value&, bool)),
108  this,
109  SIGNAL (gh_set_event (const graphics_handle&,
110  const std::string&,
111  const octave_value&, bool)));
112 
113  connect (m_canvas,
114  SIGNAL (gh_set_event (const graphics_handle&,
115  const std::string&,
116  const octave_value&, bool, bool)),
117  this,
118  SIGNAL (gh_set_event (const graphics_handle&,
119  const std::string&,
120  const octave_value&, bool, bool)));
121 
122  QWidget *canvasWidget = m_canvas->qWidget ();
123 
124  canvasWidget->lower ();
125  canvasWidget->show ();
126  canvasWidget->setGeometry (0, 0, width (), height ());
127  }
128  }
129 
130  return m_canvas;
131  }
132 
133  void
134  Container::resizeEvent (QResizeEvent* /* event */)
135  {
136  if (m_canvas)
137  m_canvas->qWidget ()->setGeometry (0, 0, width (), height ());
138 
140 
141  octave::autolock guard (gh_mgr.graphics_lock ());
142 
143  for (auto *qObj : children ())
144  {
145  if (qObj->isWidgetType ())
146  {
147  Object *obj = Object::fromQObject (qObj);
148 
149  if (obj)
150  {
151  graphics_object go = obj->object ();
152 
153  if (go.valid_object ())
154  {
155  Matrix bb = go.get_properties ().get_boundingbox (false);
156 
157  obj->qWidget<QWidget> ()->setGeometry
158  (octave::math::round (bb(0)),
159  octave::math::round (bb(1)),
160  octave::math::round (bb(2)),
161  octave::math::round (bb(3)));
162  }
163  }
164  }
165  }
166  }
167 
168  void
169  Container::childEvent (QChildEvent *xevent)
170  {
171  // Enable mouse tracking in child widgets as they are added if the
172  // container also has mouse tracking enabled. There is no need to
173  // do this when child objects are removed.
174 
175  if (xevent->added ())
176  {
177  QObject *obj = xevent->child ();
178 
179  if (obj && obj->isWidgetType ())
180  {
181  QWidget *widget = qobject_cast<QWidget *> (obj);
182 
183  if (widget)
184  widget->setMouseTracking (hasMouseTracking ());
185  }
186  }
187  }
188 }
Definition: dMatrix.h:42
static Canvas * create(octave::base_qobject &oct_qobj, octave::interpreter &interp, const graphics_handle &handle, QWidget *parent, const std::string &name)
Definition: Canvas.cc:1097
virtual QWidget * qWidget(void)=0
Canvas * canvas(const graphics_handle &handle, bool create=true)
Definition: Container.cc:56
void gh_set_event(const graphics_handle &h, const std::string &name, const octave_value &value)
octave::base_qobject & m_octave_qobj
Definition: Container.h:84
Container(QWidget *parent, octave::base_qobject &oct_qobj, octave::interpreter &interp)
Definition: Container.cc:44
void gh_callback_event(const graphics_handle &h, const std::string &name)
octave::interpreter & m_interpreter
Definition: Container.h:85
void childEvent(QChildEvent *event)
Definition: Container.cc:169
void resizeEvent(QResizeEvent *event)
Definition: Container.cc:134
graphics_object object(void) const
Definition: Object.cc:83
static Object * fromQObject(QObject *obj)
Definition: Object.cc:214
T * qWidget(void)
Definition: Object.h:85
virtual Matrix get_boundingbox(bool=false, const Matrix &=Matrix()) const
Definition: graphics.in.h:2268
graphics_object get_object(double val) const
Definition: graphics.in.h:6260
octave::mutex graphics_lock(void)
Definition: graphics.in.h:6393
octave_value get(bool all=false) const
Definition: graphics.in.h:2746
graphics_object get_ancestor(const std::string &type) const
Definition: graphics.cc:3905
base_properties & get_properties(void)
Definition: graphics.in.h:2829
bool valid_object(void) const
Definition: graphics.in.h:2847
Base class for Octave interfaces that use Qt.
gh_manager & get_gh_manager(void)
Definition: interpreter.h:295
std::string string_value(bool force=false) const
Definition: ov.h:927
double round(double x)
Definition: lo-mappers.h:136
std::function< void(octave::interpreter &)> meth_callback
Definition: event-manager.h:47
std::function< void(void)> fcn_callback
Definition: event-manager.h:46