GNU Octave  8.1.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-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 <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 
42 
44  octave::interpreter& interp)
45 : ContainerBase (xparent), m_octave_qobj (oct_qobj),
46  m_interpreter (interp), m_canvas (nullptr)
47 {
48  setFocusPolicy (Qt::ClickFocus);
49 }
50 
52 { }
53 
54 Canvas *
55 Container::canvas (const graphics_handle& gh, bool xcreate)
56 {
57  if (! m_canvas && xcreate)
58  {
59  gh_manager& gh_mgr = m_interpreter.get_gh_manager ();
60 
61  octave::autolock guard (gh_mgr.graphics_lock ());
62 
63  graphics_object go = gh_mgr.get_object (gh);
64 
65  if (go)
66  {
67  graphics_object fig = go.get_ancestor ("figure");
68 
70  fig.get ("renderer").string_value ());
71 
74 
77 
78  connect (m_canvas,
79  SIGNAL (gh_callback_event (const graphics_handle&,
80  const std::string&)),
81  this,
82  SIGNAL (gh_callback_event (const graphics_handle&,
83  const std::string&)));
84 
85  connect (m_canvas,
86  SIGNAL (gh_callback_event (const graphics_handle&,
87  const std::string&,
88  const octave_value&)),
89  this,
90  SIGNAL (gh_callback_event (const graphics_handle&,
91  const std::string&,
92  const octave_value&)));
93 
94  connect (m_canvas,
95  SIGNAL (gh_set_event (const graphics_handle&,
96  const std::string&,
97  const octave_value&)),
98  this,
99  SIGNAL (gh_set_event (const graphics_handle&,
100  const std::string&,
101  const octave_value&)));
102 
103  connect (m_canvas,
104  SIGNAL (gh_set_event (const graphics_handle&,
105  const std::string&,
106  const octave_value&, bool)),
107  this,
108  SIGNAL (gh_set_event (const graphics_handle&,
109  const std::string&,
110  const octave_value&, bool)));
111 
112  connect (m_canvas,
113  SIGNAL (gh_set_event (const graphics_handle&,
114  const std::string&,
115  const octave_value&, bool, bool)),
116  this,
117  SIGNAL (gh_set_event (const graphics_handle&,
118  const std::string&,
119  const octave_value&, bool, bool)));
120 
121  QWidget *canvasWidget = m_canvas->qWidget ();
122 
123  canvasWidget->lower ();
124  canvasWidget->show ();
125  canvasWidget->setGeometry (0, 0, width (), height ());
126  }
127  }
128 
129  return m_canvas;
130 }
131 
132 void
133 Container::resizeEvent (QResizeEvent * /* event */)
134 {
135  if (m_canvas)
136  m_canvas->qWidget ()->setGeometry (0, 0, width (), height ());
137 
138  gh_manager& gh_mgr = m_interpreter.get_gh_manager ();
139 
140  octave::autolock guard (gh_mgr.graphics_lock ());
141 
142  for (auto *qObj : children ())
143  {
144  if (qObj->isWidgetType ())
145  {
146  Object *obj = Object::fromQObject (qObj);
147 
148  if (obj)
149  {
150  graphics_object go = obj->object ();
151 
152  if (go.valid_object ())
153  {
154  Matrix bb = go.get_properties ().get_boundingbox (false);
155 
156  obj->qWidget<QWidget> ()->setGeometry
157  (octave::math::round (bb(0)),
158  octave::math::round (bb(1)),
159  octave::math::round (bb(2)),
160  octave::math::round (bb(3)));
161  }
162  }
163  }
164  }
165 }
166 
167 void
168 Container::childEvent (QChildEvent *xevent)
169 {
170  // Enable mouse tracking in child widgets as they are added if the
171  // container also has mouse tracking enabled. There is no need to
172  // do this when child objects are removed.
173 
174  if (xevent->added ())
175  {
176  QObject *obj = xevent->child ();
177 
178  if (obj && obj->isWidgetType ())
179  {
180  QWidget *widget = qobject_cast<QWidget *> (obj);
181 
182  if (widget)
183  widget->setMouseTracking (hasMouseTracking ());
184  }
185  }
186 }
187 
OCTAVE_END_NAMESPACE(octave)
Definition: Canvas.h:50
static Canvas * create(octave::base_qobject &oct_qobj, octave::interpreter &interp, const graphics_handle &handle, QWidget *parent, const std::string &name)
Definition: Canvas.cc:1088
void interpreter_event(const octave::fcn_callback &fcn)
virtual QWidget * qWidget(void)=0
octave::base_qobject & m_octave_qobj
Definition: Container.h:83
void gh_set_event(const graphics_handle &h, const std::string &name, const octave_value &value)
Canvas * m_canvas
Definition: Container.h:85
void gh_callback_event(const graphics_handle &h, const std::string &name)
octave::interpreter & m_interpreter
Definition: Container.h:84
~Container(void)
Definition: Container.cc:51
void interpreter_event(const octave::fcn_callback &fcn)
Canvas * canvas(const graphics_handle &handle, bool create=true)
Definition: Container.cc:55
void resizeEvent(QResizeEvent *event)
Definition: Container.cc:133
void childEvent(QChildEvent *event)
Definition: Container.cc:168
Definition: dMatrix.h:42
Definition: Object.h:47
T * qWidget(void)
Definition: Object.h:81
static Object * fromQObject(QObject *obj)
Definition: Object.cc:213
graphics_object object(void) const
Definition: Object.cc:82
Base class for Octave interfaces that use Qt.
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
double round(double x)
Definition: lo-mappers.h:136