GNU Octave  4.4.1
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-2018 Michael Goffioul
4 
5 This file is part of Octave.
6 
7 Octave is free software: you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <https://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if defined (HAVE_CONFIG_H)
24 # include "config.h"
25 #endif
26 
27 #include "gl-render.h"
28 #include "gl2ps-print.h"
29 #include "graphics.h"
30 #include "octave-link.h"
31 
32 #include "GLCanvas.h"
33 #include "gl-select.h"
34 
35 namespace QtHandles
36 {
37 #if defined (HAVE_QOPENGLWIDGET)
38 # define OCTAVE_QT_OPENGL_WIDGET_FORMAT_ARGS
39 #else
40 # if defined (Q_OS_WIN32)
41 # define OCTAVE_QT_OPENGL_WIDGET_FORMAT_ARGS \
42  QGLFormat (QGL::SampleBuffers | QGL::AlphaChannel \
43  | QGL::IndirectRendering),
44 # else
45 # define OCTAVE_QT_OPENGL_WIDGET_FORMAT_ARGS \
46  QGLFormat (QGL::SampleBuffers | QGL::AlphaChannel),
47 # endif
48 #endif
49 
52  Canvas (gh)
53  {
54  setFocusPolicy (Qt::ClickFocus);
55  setFocus ();
56  }
57 
59  { }
60 
61  void
63  {
66 
67  if (go)
68  {
70 
71  r.set_viewport (width (), height ());
72  r.draw (go);
73  }
74  }
75 
78  {
81 
82  if (go && go.isa ("figure"))
83  {
84  Matrix pos = go.get ("position").matrix_value ();
85 
86  // Make sure we have a valid current context
87  if (! begin_rendering ())
88  return retval;
89 
90  // When the figure is not visible or its size is frozen for printing,
91  // we use a framebuffer object to make sure we are rendering on a
92  // suitably large frame.
93  if (go.get ("visible").string_value () == "off"
94  || go.get ("__printing__").string_value () == "on")
95  {
96  OCTAVE_QT_OPENGL_FBO
97  fbo (pos(2), pos(3),OCTAVE_QT_OPENGL_FBO::Attachment::Depth);
98 
99  fbo.bind ();
100 
102  r.set_viewport (pos(2), pos(3));
103  r.draw (go);
104  retval = r.get_pixels (pos(2), pos(3));
105 
106  fbo.release ();
107  }
108  else
109  {
111  r.set_viewport (pos(2), pos(3));
112  r.draw (go);
113  retval = r.get_pixels (pos(2), pos(3));
114  }
115 
116  end_rendering ();
117  }
118 
119  return retval;
120  }
121 
122  void
123  GLCanvas::do_print (const QString& file_cmd, const QString& term,
124  const graphics_handle& handle)
125  {
128 
129  if (obj.valid_object ())
130  {
131  graphics_object figObj (obj.get_ancestor ("figure"));
132  try
133  {
134  // Make sure we have a valid current context
135  if (! begin_rendering ())
136  error ("print: no valid OpenGL offscreen context");
137 
138  octave::gl2ps_print (figObj, file_cmd.toStdString (),
139  term.toStdString ());
140  }
141  catch (octave::execution_exception& e)
142  {
143  octave_link::post_exception (std::current_exception ());
144  end_rendering ();
145  }
146  }
147  }
148 
149  void
151  {
153  }
154 
155  void
157  {
159  }
160 
161  void
163  {
164  canvasAutoAxes (gh);
165  }
166 
168  GLCanvas::selectFromAxes (const graphics_object& ax, const QPoint& pt)
169  {
170  makeCurrent ();
171 
172  if (ax)
173  {
175 
176  s.set_viewport (width (), height ());
177  return s.select (ax, pt.x (), height () - pt.y (),
179  }
180 
181  return graphics_object ();
182  }
183 
184  inline void
185  glDrawZoomBox (const QPoint& p1, const QPoint& p2)
186  {
187  glVertex2d (p1.x (), p1.y ());
188  glVertex2d (p2.x (), p1.y ());
189  glVertex2d (p2.x (), p2.y ());
190  glVertex2d (p1.x (), p2.y ());
191  glVertex2d (p1.x (), p1.y ());
192  }
193 
194  void
195  GLCanvas::drawZoomBox (const QPoint& p1, const QPoint& p2)
196  {
197  glMatrixMode (GL_MODELVIEW);
198  glPushMatrix ();
199  glLoadIdentity ();
200 
201  glMatrixMode (GL_PROJECTION);
202  glPushMatrix ();
203  glLoadIdentity ();
204  glOrtho (0, width (), height (), 0, 1, -1);
205 
206  glPushAttrib (GL_DEPTH_BUFFER_BIT | GL_CURRENT_BIT);
207  glDisable (GL_DEPTH_TEST);
208 
209  glBegin (GL_POLYGON);
210  glColor4f (0.45, 0.62, 0.81, 0.1);
211  glDrawZoomBox (p1, p2);
212  glEnd ();
213 
214  glLineWidth (1.5);
215  glBegin (GL_LINE_STRIP);
216  glColor4f (0.45, 0.62, 0.81, 0.9);
217  glDrawZoomBox (p1, p2);
218  glEnd ();
219 
220  glPopAttrib ();
221 
222  glMatrixMode (GL_MODELVIEW);
223  glPopMatrix ();
224 
225  glMatrixMode (GL_PROJECTION);
226  glPopMatrix ();
227  }
228 
229  void
231  {
232  canvasPaintEvent ();
233  }
234 
235  void
236  GLCanvas::mouseDoubleClickEvent (QMouseEvent *xevent)
237  {
239  }
240 
241  void
242  GLCanvas::mouseMoveEvent (QMouseEvent *xevent)
243  {
244  canvasMouseMoveEvent (xevent);
245  }
246 
247  void
248  GLCanvas::mousePressEvent (QMouseEvent *xevent)
249  {
250  canvasMousePressEvent (xevent);
251  }
252 
253  void
254  GLCanvas::mouseReleaseEvent (QMouseEvent *xevent)
255  {
256  canvasMouseReleaseEvent (xevent);
257  }
258 
259  void
260  GLCanvas::wheelEvent (QWheelEvent *xevent)
261  {
262  canvasWheelEvent (xevent);
263  }
264 
265  void
266  GLCanvas::keyPressEvent (QKeyEvent *xevent)
267  {
268  if (! canvasKeyPressEvent (xevent))
269  OCTAVE_QT_OPENGL_WIDGET::keyPressEvent (xevent);
270  }
271 
272  void
273  GLCanvas::keyReleaseEvent (QKeyEvent *xevent)
274  {
275  if (! canvasKeyReleaseEvent (xevent))
276  OCTAVE_QT_OPENGL_WIDGET::keyReleaseEvent (xevent);
277  }
278 
279  bool
281  {
282  bool retval = true;
283 
284  if (! isValid ())
285  {
286 # if defined (HAVE_QT_OFFSCREEN)
287  static bool os_ctx_ok = true;
288  if (os_ctx_ok && ! m_os_context.isValid ())
289  {
290  // Try to initialize offscreen context
291  m_os_surface.create ();
292  if (! m_os_context.create ())
293  {
294  os_ctx_ok = false;
295  return false;
296  }
297  }
298 
299  retval = m_os_context.makeCurrent (&m_os_surface);
300 # else
301  retval = false;
302 # endif
303  }
304  else
305  makeCurrent ();
306 
307  return retval;
308  }
309 
310  void
312  {
313  doneCurrent ();
314  }
315 }
void autoAxes(const graphics_handle &handle)
Definition: GLCanvas.cc:162
void end_rendering(void)
Definition: GLCanvas.cc:311
std::string string_value(bool force=false) const
Definition: ov.h:955
void mouseDoubleClickEvent(QMouseEvent *event)
Definition: GLCanvas.cc:236
graphics_object selectFromAxes(const graphics_object &ax, const QPoint &pt)
Definition: GLCanvas.cc:168
virtual void draw(const graphics_object &go, bool toplevel=true)
Definition: gl-render.cc:620
GLCanvas(QWidget *parent, const graphics_handle &handle)
Definition: GLCanvas.cc:50
void error(const char *fmt,...)
Definition: error.cc:578
graphics_handle gh
Definition: graphics.cc:11812
s
Definition: file-io.cc:2729
void wheelEvent(QWheelEvent *event)
Definition: GLCanvas.cc:260
i e
Definition: data.cc:2591
void mouseMoveEvent(QMouseEvent *event)
Definition: GLCanvas.cc:242
void canvasToggleGrid(const graphics_handle &handle)
Definition: Canvas.cc:224
void canvasMouseDoubleClickEvent(QMouseEvent *event)
Definition: Canvas.cc:561
bool begin_rendering(void)
Definition: GLCanvas.cc:280
bool isa(const std::string &go_name) const
Definition: graphics.in.h:2786
void draw(const graphics_handle &handle)
Definition: GLCanvas.cc:62
void keyReleaseEvent(QKeyEvent *event)
Definition: GLCanvas.cc:273
void toggleAxes(const graphics_handle &handle)
Definition: GLCanvas.cc:150
void canvasToggleAxes(const graphics_handle &handle)
Definition: Canvas.cc:195
void glDrawZoomBox(const QPoint &p1, const QPoint &p2)
Definition: GLCanvas.cc:185
bool valid_object(void) const
Definition: graphics.in.h:2806
void canvasMousePressEvent(QMouseEvent *event)
Definition: Canvas.cc:594
void canvasWheelEvent(QWheelEvent *event)
Definition: Canvas.cc:903
bool canvasKeyPressEvent(QKeyEvent *event)
Definition: Canvas.cc:1016
void canvasMouseMoveEvent(QMouseEvent *event)
Definition: Canvas.cc:462
octave_value get(bool all=false) const
Definition: graphics.in.h:2711
void canvasAutoAxes(const graphics_handle &handle)
Definition: Canvas.cc:272
void gl2ps_print(const graphics_object &fig, const std::string &stream, const std::string &term)
octave_value retval
Definition: data.cc:6246
Definition: dMatrix.h:36
uint8NDArray do_getPixels(const graphics_handle &handle)
Definition: GLCanvas.cc:77
void canvasPaintEvent(void)
Definition: Canvas.cc:298
virtual uint8NDArray get_pixels(int width, int height)
Definition: gl-render.cc:1045
virtual void set_viewport(int w, int h)
Definition: gl-render.cc:3809
void canvasMouseReleaseEvent(QMouseEvent *event)
Definition: Canvas.cc:804
void paintGL(void)
Definition: GLCanvas.cc:230
bool canvasKeyReleaseEvent(QKeyEvent *event)
Definition: Canvas.cc:1043
void toggleGrid(const graphics_handle &handle)
Definition: GLCanvas.cc:156
#define OCTAVE_QT_OPENGL_WIDGET_FORMAT_ARGS
Definition: GLCanvas.cc:45
void mousePressEvent(QMouseEvent *event)
Definition: GLCanvas.cc:248
void drawZoomBox(const QPoint &p1, const QPoint &p2)
Definition: GLCanvas.cc:195
static graphics_object get_object(double val)
Definition: graphics.in.h:6098
void mouseReleaseEvent(QMouseEvent *event)
Definition: GLCanvas.cc:254
is a function handle
Definition: bsxfun.cc:337
graphics_object get_ancestor(const std::string &type) const
Definition: graphics.cc:3613
void keyPressEvent(QKeyEvent *event)
Definition: GLCanvas.cc:266
Matrix matrix_value(bool frc_str_conv=false) const
Definition: ov.h:834
void do_print(const QString &file_cmd, const QString &term, const graphics_handle &handle)
Definition: GLCanvas.cc:123