GNU Octave  6.2.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
GLCanvas.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 "GLCanvas.h"
31 #include "gl-select.h"
32 
33 #include "gl-render.h"
34 #include "gl2ps-print.h"
35 #include "graphics.h"
36 #include "interpreter.h"
37 
38 namespace QtHandles
39 {
40 #if defined (HAVE_QOPENGLWIDGET)
41 # define OCTAVE_QT_OPENGL_WIDGET_FORMAT_ARGS
42 #else
43 # if defined (Q_OS_WIN32)
44 # define OCTAVE_QT_OPENGL_WIDGET_FORMAT_ARGS \
45  QGLFormat (QGL::SampleBuffers | QGL::AlphaChannel \
46  | QGL::IndirectRendering),
47 # else
48 # define OCTAVE_QT_OPENGL_WIDGET_FORMAT_ARGS \
49  QGLFormat (QGL::SampleBuffers | QGL::AlphaChannel),
50 # endif
51 #endif
52 
54  octave::interpreter& interp,
55  const graphics_handle& gh, QWidget *xparent)
57  Canvas (oct_qobj, interp, gh), m_glfcns (), m_renderer (m_glfcns)
58  {
59  setFocusPolicy (Qt::ClickFocus);
60  setFocus ();
61  }
62 
64  { }
65 
66  void
68  {
69  m_glfcns.init ();
70  }
71 
72  void
74  {
76 
77  octave::autolock guard (gh_mgr.graphics_lock ());
78 
79  graphics_object go = gh_mgr.get_object (gh);
80 
81  if (go)
82  {
83  graphics_object fig = go.get_ancestor ("figure");
84  double dpr = fig.get ("__device_pixel_ratio__").double_value ();
85  m_renderer.set_viewport (dpr * width (), dpr * height ());
87  m_renderer.draw (go);
88  }
89  }
90 
93  {
95 
97 
98  graphics_object go = gh_mgr.get_object (gh);
99 
100  if (go && go.isa ("figure"))
101  {
102  Matrix pos = go.get ("position").matrix_value ();
103  double dpr = go.get ("__device_pixel_ratio__").double_value ();
104  pos(2) *= dpr;
105  pos(3) *= dpr;
106 
107  // Make sure we have a valid current context
108  if (! begin_rendering ())
109  return retval;
110 
111  // When the figure is not visible or its size is frozen for printing,
112  // we use a framebuffer object to make sure we are rendering on a
113  // suitably large frame.
114  if (go.get ("visible").string_value () == "off"
115  || go.get ("__printing__").string_value () == "on")
116  {
117  OCTAVE_QT_OPENGL_FBO
118  fbo (pos(2), pos(3),
119  OCTAVE_QT_OPENGL_FBO::Attachment::Depth);
120 
121  fbo.bind ();
122 
123  m_renderer.set_viewport (pos(2), pos(3));
125  m_renderer.draw (go);
126  retval = m_renderer.get_pixels (pos(2), pos(3));
127 
128  fbo.release ();
129  }
130  else
131  {
132  m_renderer.set_viewport (pos(2), pos(3));
134  m_renderer.draw (go);
135  retval = m_renderer.get_pixels (pos(2), pos(3));
136  }
137 
138  end_rendering ();
139  }
140 
141  return retval;
142  }
143 
144  void
145  GLCanvas::do_print (const QString& file_cmd, const QString& term,
146  const graphics_handle& handle)
147  {
149 
150  octave::autolock guard (gh_mgr.graphics_lock ());
151 
152  graphics_object go = gh_mgr.get_object (handle);
153 
154  if (go.valid_object ())
155  {
156  graphics_object fig (go.get_ancestor ("figure"));
157 
158  // Make sure we have a valid current context
159  if (! begin_rendering ())
160  error ("print: no valid OpenGL offscreen context");
161 
162  try
163  {
164  if (fig.get ("visible").string_value () == "on")
165  octave::gl2ps_print (m_glfcns, fig, file_cmd.toStdString (),
166  term.toStdString ());
167  else
168  {
169  // When the figure is not visible, we use a framebuffer object
170  // to make sure we are rendering on a suitably large frame.
171  Matrix pos = fig.get ("position").matrix_value ();
172  double dpr = fig.get ("__device_pixel_ratio__").double_value ();
173  pos(2) *= dpr;
174  pos(3) *= dpr;
175 
176  OCTAVE_QT_OPENGL_FBO
177  fbo (pos(2), pos(3),
178  OCTAVE_QT_OPENGL_FBO::Attachment::Depth);
179 
180  fbo.bind ();
181 
182  octave::gl2ps_print (m_glfcns, fig, file_cmd.toStdString (),
183  term.toStdString ());
184 
185  fbo.release ();
186  }
187  }
188  catch (octave::execution_exception& ee)
189  {
190  emit interpreter_event
191  ([ee] (void)
192  {
193  // INTERPRETER THREAD
194 
195  throw ee;
196  });
197  }
198 
199  end_rendering ();
200  }
201  }
202 
204  GLCanvas::selectFromAxes (const graphics_object& ax, const QPoint& pt)
205  {
206  makeCurrent ();
207 
208  if (ax)
209  {
211 
212  s.set_viewport (width (), height ());
213  return s.select (ax, pt.x (), height () - pt.y (),
215  }
216 
217  return graphics_object ();
218  }
219 
220  void
221  GLCanvas::drawZoomBox (const QPoint& p1, const QPoint& p2)
222  {
223  Matrix overlaycolor (3, 1);
224  overlaycolor(0) = 0.45;
225  overlaycolor(1) = 0.62;
226  overlaycolor(2) = 0.81;
227  double overlayalpha = 0.1;
228  Matrix bordercolor = overlaycolor;
229  double borderalpha = 0.9;
230  double borderwidth = 1.5;
231 
232  m_renderer.draw_zoom_box (width (), height (),
233  p1.x (), p1.y (), p2.x (), p2.y (),
234  overlaycolor, overlayalpha,
235  bordercolor, borderalpha, borderwidth);
236  }
237 
238  void
240  {
241  canvasPaintEvent ();
242  }
243 
244  void
245  GLCanvas::mouseDoubleClickEvent (QMouseEvent *xevent)
246  {
248  }
249 
250  void
251  GLCanvas::mouseMoveEvent (QMouseEvent *xevent)
252  {
253  canvasMouseMoveEvent (xevent);
254  }
255 
256  void
257  GLCanvas::mousePressEvent (QMouseEvent *xevent)
258  {
259  canvasMousePressEvent (xevent);
260  }
261 
262  void
263  GLCanvas::mouseReleaseEvent (QMouseEvent *xevent)
264  {
265  canvasMouseReleaseEvent (xevent);
266  }
267 
268  void
269  GLCanvas::wheelEvent (QWheelEvent *xevent)
270  {
271  canvasWheelEvent (xevent);
272  }
273 
274  void
275  GLCanvas::keyPressEvent (QKeyEvent *xevent)
276  {
277  if (! canvasKeyPressEvent (xevent))
278  OCTAVE_QT_OPENGL_WIDGET::keyPressEvent (xevent);
279  }
280 
281  void
282  GLCanvas::keyReleaseEvent (QKeyEvent *xevent)
283  {
284  if (! canvasKeyReleaseEvent (xevent))
285  OCTAVE_QT_OPENGL_WIDGET::keyReleaseEvent (xevent);
286  }
287 
288  bool
290  {
291  bool retval = true;
292 
293  if (! isValid ())
294  {
295 # if defined (HAVE_QT_OFFSCREEN)
296  static bool os_ctx_ok = true;
297  if (os_ctx_ok && ! m_os_context.isValid ())
298  {
299  // Try to initialize offscreen context
300  m_os_surface.create ();
301  if (! m_os_context.create ())
302  {
303  os_ctx_ok = false;
304  return false;
305  }
306  }
307 
308  retval = m_os_context.makeCurrent (&m_os_surface);
309 # else
310  retval = false;
311 # endif
312  }
313  else
314  makeCurrent ();
315 
316  return retval;
317  }
318 
319  void
321  {
322  doneCurrent ();
323  }
324 }
#define OCTAVE_QT_OPENGL_WIDGET_FORMAT_ARGS
Definition: GLCanvas.cc:48
Definition: dMatrix.h:42
void canvasMousePressEvent(QMouseEvent *event)
Definition: Canvas.cc:583
bool canvasKeyReleaseEvent(QKeyEvent *event)
Definition: Canvas.cc:1073
void canvasMouseReleaseEvent(QMouseEvent *event)
Definition: Canvas.cc:796
bool canvasKeyPressEvent(QKeyEvent *event)
Definition: Canvas.cc:1042
void canvasMouseDoubleClickEvent(QMouseEvent *event)
Definition: Canvas.cc:550
void canvasPaintEvent(void)
Definition: Canvas.cc:276
octave::interpreter & m_interpreter
Definition: Canvas.h:157
void canvasMouseMoveEvent(QMouseEvent *event)
Definition: Canvas.cc:448
void canvasWheelEvent(QWheelEvent *event)
Definition: Canvas.cc:906
void interpreter_event(const octave::fcn_callback &fcn)
void do_print(const QString &file_cmd, const QString &term, const graphics_handle &handle)
Definition: GLCanvas.cc:145
void end_rendering(void)
Definition: GLCanvas.cc:320
void drawZoomBox(const QPoint &p1, const QPoint &p2)
Definition: GLCanvas.cc:221
bool begin_rendering(void)
Definition: GLCanvas.cc:289
octave::qopengl_functions m_glfcns
Definition: GLCanvas.h:94
void wheelEvent(QWheelEvent *event)
Definition: GLCanvas.cc:269
void keyPressEvent(QKeyEvent *event)
Definition: GLCanvas.cc:275
void mouseMoveEvent(QMouseEvent *event)
Definition: GLCanvas.cc:251
void draw(const graphics_handle &handle)
Definition: GLCanvas.cc:73
uint8NDArray do_getPixels(const graphics_handle &handle)
Definition: GLCanvas.cc:92
void mousePressEvent(QMouseEvent *event)
Definition: GLCanvas.cc:257
octave::opengl_renderer m_renderer
Definition: GLCanvas.h:95
graphics_object selectFromAxes(const graphics_object &ax, const QPoint &pt)
Definition: GLCanvas.cc:204
void paintGL(void)
Definition: GLCanvas.cc:239
void mouseDoubleClickEvent(QMouseEvent *event)
Definition: GLCanvas.cc:245
void keyReleaseEvent(QKeyEvent *event)
Definition: GLCanvas.cc:282
GLCanvas(octave::base_qobject &oct_qobj, octave::interpreter &interp, const graphics_handle &handle, QWidget *parent)
Definition: GLCanvas.cc:53
void mouseReleaseEvent(QMouseEvent *event)
Definition: GLCanvas.cc:263
void initializeGL(void)
Definition: GLCanvas.cc:67
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
bool isa(const std::string &go_name) const
Definition: graphics.in.h:2827
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
virtual void draw(const graphics_object &go, bool toplevel=true)
Definition: gl-render.cc:717
virtual uint8NDArray get_pixels(int width, int height)
Definition: gl-render.cc:1218
virtual void set_viewport(int w, int h)
Definition: gl-render.cc:3973
virtual void set_device_pixel_ratio(double dpr)
Definition: gl-render.h:64
virtual void draw_zoom_box(int width, int height, int x1, int y1, int x2, int y2, const Matrix &overlaycolor, double overlayalpha, const Matrix &bordercolor, double borderalpha, double borderwidth)
Definition: gl-render.cc:1153
graphics_object select(const graphics_object &ax, int x, int y, int flags=0)
Definition: gl-select.cc:80
std::string string_value(bool force=false) const
Definition: ov.h:927
Matrix matrix_value(bool frc_str_conv=false) const
Definition: ov.h:806
double double_value(bool frc_str_conv=false) const
Definition: ov.h:794
void error(const char *fmt,...)
Definition: error.cc:968
void gl2ps_print(opengl_functions &glfcns, const graphics_object &fig, const std::string &stream, const std::string &term)
@ select_ignore_hittest
Definition: gl-select.h:40
octave_value::octave_value(const Array< char > &chm, char type) return retval
Definition: ov.cc:811