annotate libgui/graphics/GLCanvas.h @ 31762:d94ceed56929

GLCanvas: avoid multiple inheritance from both QWidget and QObject To avoid having GLCanvas inherit from both QWidget and QObject, create a separate class, GLWidget, derived from QOpenGLWidget and use that as a member of GLCanvas instead of a base class. * Canvas.h (class Canvas): Derive from QWidget instead of QObject. (Canvas::Canvas): New argument for parent widget. Pass parent to QWidget base class constructor. * GLCanvas.h, GLCanvas.cc (class GLCanvas): Split QOpenGLWidget parts into separate GLWidget class. GLCanvas now processes things related to the interpreter and forwards to GLWidget for rendering. Use unwind_action objects to ensure OpenGL context is restored. (GLCanvas::m_glwidget): New data member for GLWidget. * libbgui/graphics/module.mk (OCTAVE_GUI_GRAPHICS_MOC): Add moc-GLCanvas.cc to the list.
author John W. Eaton <jwe@octave.org>
date Fri, 20 Jan 2023 08:49:19 -0500
parents 345a3f5890e7
children 1d1eff97670e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
1 ////////////////////////////////////////////////////////////////////////
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
2 //
31706
597f3ee61a48 update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 31648
diff changeset
3 // Copyright (C) 2011-2023 The Octave Project Developers
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
4 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
5 // See the file COPYRIGHT.md in the top-level directory of this
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
6 // distribution or <https://octave.org/copyright/>.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
7 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
8 // This file is part of Octave.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
9 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
10 // Octave is free software: you can redistribute it and/or modify it
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
11 // under the terms of the GNU General Public License as published by
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
12 // the Free Software Foundation, either version 3 of the License, or
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
13 // (at your option) any later version.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
14 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
15 // Octave is distributed in the hope that it will be useful, but
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
18 // GNU General Public License for more details.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
19 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
20 // You should have received a copy of the GNU General Public License
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
21 // along with Octave; see the file COPYING. If not, see
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
22 // <https://www.gnu.org/licenses/>.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
23 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
24 ////////////////////////////////////////////////////////////////////////
18498
2e7cad6f180c Initial integration of QtHandles.
John W. Eaton <jwe@octave.org>
parents:
diff changeset
25
21203
710e700cdd7f maint: Clean up naming and indentation of #ifdef blocks in libgui
Rik <rik@octave.org>
parents: 20003
diff changeset
26 #if ! defined (octave_GLCanvas_h)
710e700cdd7f maint: Clean up naming and indentation of #ifdef blocks in libgui
Rik <rik@octave.org>
parents: 20003
diff changeset
27 #define octave_GLCanvas_h 1
18498
2e7cad6f180c Initial integration of QtHandles.
John W. Eaton <jwe@octave.org>
parents:
diff changeset
28
31762
d94ceed56929 GLCanvas: avoid multiple inheritance from both QWidget and QObject
John W. Eaton <jwe@octave.org>
parents: 31752
diff changeset
29 #include <QOffscreenSurface>
d94ceed56929 GLCanvas: avoid multiple inheritance from both QWidget and QObject
John W. Eaton <jwe@octave.org>
parents: 31752
diff changeset
30 #include <QOpenGLContext>
31752
345a3f5890e7 assume QOpenGLWidget is available
John W. Eaton <jwe@octave.org>
parents: 31751
diff changeset
31 #include <QOpenGLFramebufferObject>
31762
d94ceed56929 GLCanvas: avoid multiple inheritance from both QWidget and QObject
John W. Eaton <jwe@octave.org>
parents: 31752
diff changeset
32 #include <QOpenGLWidget>
18498
2e7cad6f180c Initial integration of QtHandles.
John W. Eaton <jwe@octave.org>
parents:
diff changeset
33
2e7cad6f180c Initial integration of QtHandles.
John W. Eaton <jwe@octave.org>
parents:
diff changeset
34 #include "Canvas.h"
2e7cad6f180c Initial integration of QtHandles.
John W. Eaton <jwe@octave.org>
parents:
diff changeset
35
25863
8a6bf76abf31 eliminate some duplicate OpenGL drawing code
John W. Eaton <jwe@octave.org>
parents: 25862
diff changeset
36 #include "gl-render.h"
25862
e5a73a8c116c use wrapper class to call OpenGL functions
John W. Eaton <jwe@octave.org>
parents: 25103
diff changeset
37 #include "qopengl-functions.h"
e5a73a8c116c use wrapper class to call OpenGL functions
John W. Eaton <jwe@octave.org>
parents: 25103
diff changeset
38
31646
c6d54dd31a7e maint: Use macros to begin/end C++ namespaces.
John W. Eaton <jwe@octave.org>
parents: 30564
diff changeset
39 OCTAVE_BEGIN_NAMESPACE(octave)
c6d54dd31a7e maint: Use macros to begin/end C++ namespaces.
John W. Eaton <jwe@octave.org>
parents: 30564
diff changeset
40
31762
d94ceed56929 GLCanvas: avoid multiple inheritance from both QWidget and QObject
John W. Eaton <jwe@octave.org>
parents: 31752
diff changeset
41 class GLWidget : public QOpenGLWidget
22411
c69805d1fa64 maint: Style check C++ code in libgui/
Rik <rik@octave.org>
parents: 22323
diff changeset
42 {
31762
d94ceed56929 GLCanvas: avoid multiple inheritance from both QWidget and QObject
John W. Eaton <jwe@octave.org>
parents: 31752
diff changeset
43 Q_OBJECT
d94ceed56929 GLCanvas: avoid multiple inheritance from both QWidget and QObject
John W. Eaton <jwe@octave.org>
parents: 31752
diff changeset
44
22411
c69805d1fa64 maint: Style check C++ code in libgui/
Rik <rik@octave.org>
parents: 22323
diff changeset
45 public:
31762
d94ceed56929 GLCanvas: avoid multiple inheritance from both QWidget and QObject
John W. Eaton <jwe@octave.org>
parents: 31752
diff changeset
46
d94ceed56929 GLCanvas: avoid multiple inheritance from both QWidget and QObject
John W. Eaton <jwe@octave.org>
parents: 31752
diff changeset
47 GLWidget (Canvas& parent_canvas);
d94ceed56929 GLCanvas: avoid multiple inheritance from both QWidget and QObject
John W. Eaton <jwe@octave.org>
parents: 31752
diff changeset
48
d94ceed56929 GLCanvas: avoid multiple inheritance from both QWidget and QObject
John W. Eaton <jwe@octave.org>
parents: 31752
diff changeset
49 ~GLWidget (void);
31646
c6d54dd31a7e maint: Use macros to begin/end C++ namespaces.
John W. Eaton <jwe@octave.org>
parents: 30564
diff changeset
50
25862
e5a73a8c116c use wrapper class to call OpenGL functions
John W. Eaton <jwe@octave.org>
parents: 25103
diff changeset
51 void initializeGL (void);
e5a73a8c116c use wrapper class to call OpenGL functions
John W. Eaton <jwe@octave.org>
parents: 25103
diff changeset
52
31762
d94ceed56929 GLCanvas: avoid multiple inheritance from both QWidget and QObject
John W. Eaton <jwe@octave.org>
parents: 31752
diff changeset
53 void draw (graphics_object go);
d94ceed56929 GLCanvas: avoid multiple inheritance from both QWidget and QObject
John W. Eaton <jwe@octave.org>
parents: 31752
diff changeset
54 uint8NDArray do_getPixels (graphics_object go);
24798
45470049a43f Allow getframe and print to work without osmesa (bug #53186).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents: 24534
diff changeset
55 void do_print (const QString& file_cmd, const QString& term,
31762
d94ceed56929 GLCanvas: avoid multiple inheritance from both QWidget and QObject
John W. Eaton <jwe@octave.org>
parents: 31752
diff changeset
56 graphics_object go);
22411
c69805d1fa64 maint: Style check C++ code in libgui/
Rik <rik@octave.org>
parents: 22323
diff changeset
57 void drawZoomBox (const QPoint& p1, const QPoint& p2);
c69805d1fa64 maint: Style check C++ code in libgui/
Rik <rik@octave.org>
parents: 22323
diff changeset
58 void resize (int /* x */, int /* y */,
c69805d1fa64 maint: Style check C++ code in libgui/
Rik <rik@octave.org>
parents: 22323
diff changeset
59 int /* width */, int /* height */) { }
c69805d1fa64 maint: Style check C++ code in libgui/
Rik <rik@octave.org>
parents: 22323
diff changeset
60 graphics_object selectFromAxes (const graphics_object& ax,
c69805d1fa64 maint: Style check C++ code in libgui/
Rik <rik@octave.org>
parents: 22323
diff changeset
61 const QPoint& pt);
31762
d94ceed56929 GLCanvas: avoid multiple inheritance from both QWidget and QObject
John W. Eaton <jwe@octave.org>
parents: 31752
diff changeset
62
d94ceed56929 GLCanvas: avoid multiple inheritance from both QWidget and QObject
John W. Eaton <jwe@octave.org>
parents: 31752
diff changeset
63 bool begin_rendering (void);
d94ceed56929 GLCanvas: avoid multiple inheritance from both QWidget and QObject
John W. Eaton <jwe@octave.org>
parents: 31752
diff changeset
64 void end_rendering (void);
18498
2e7cad6f180c Initial integration of QtHandles.
John W. Eaton <jwe@octave.org>
parents:
diff changeset
65
22411
c69805d1fa64 maint: Style check C++ code in libgui/
Rik <rik@octave.org>
parents: 22323
diff changeset
66 protected:
31762
d94ceed56929 GLCanvas: avoid multiple inheritance from both QWidget and QObject
John W. Eaton <jwe@octave.org>
parents: 31752
diff changeset
67
22411
c69805d1fa64 maint: Style check C++ code in libgui/
Rik <rik@octave.org>
parents: 22323
diff changeset
68 void paintGL (void);
23449
c763214a8260 maint: Use convention 'int *x' for naming pointers.
Rik <rik@octave.org>
parents: 23220
diff changeset
69 void mouseDoubleClickEvent (QMouseEvent *event);
c763214a8260 maint: Use convention 'int *x' for naming pointers.
Rik <rik@octave.org>
parents: 23220
diff changeset
70 void mouseMoveEvent (QMouseEvent *event);
c763214a8260 maint: Use convention 'int *x' for naming pointers.
Rik <rik@octave.org>
parents: 23220
diff changeset
71 void mousePressEvent (QMouseEvent *event);
c763214a8260 maint: Use convention 'int *x' for naming pointers.
Rik <rik@octave.org>
parents: 23220
diff changeset
72 void mouseReleaseEvent (QMouseEvent *event);
c763214a8260 maint: Use convention 'int *x' for naming pointers.
Rik <rik@octave.org>
parents: 23220
diff changeset
73 void wheelEvent (QWheelEvent *event);
c763214a8260 maint: Use convention 'int *x' for naming pointers.
Rik <rik@octave.org>
parents: 23220
diff changeset
74 void keyPressEvent (QKeyEvent *event);
c763214a8260 maint: Use convention 'int *x' for naming pointers.
Rik <rik@octave.org>
parents: 23220
diff changeset
75 void keyReleaseEvent (QKeyEvent *event);
25103
078b795c5219 maint: style check C++ ahead of 4.4 release.
Rik <rik@octave.org>
parents: 25054
diff changeset
76
24798
45470049a43f Allow getframe and print to work without osmesa (bug #53186).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents: 24534
diff changeset
77 private:
25103
078b795c5219 maint: style check C++ ahead of 4.4 release.
Rik <rik@octave.org>
parents: 25054
diff changeset
78
31762
d94ceed56929 GLCanvas: avoid multiple inheritance from both QWidget and QObject
John W. Eaton <jwe@octave.org>
parents: 31752
diff changeset
79 Canvas& m_parent_canvas;
24798
45470049a43f Allow getframe and print to work without osmesa (bug #53186).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents: 24534
diff changeset
80
31762
d94ceed56929 GLCanvas: avoid multiple inheritance from both QWidget and QObject
John W. Eaton <jwe@octave.org>
parents: 31752
diff changeset
81 qopengl_functions m_glfcns;
d94ceed56929 GLCanvas: avoid multiple inheritance from both QWidget and QObject
John W. Eaton <jwe@octave.org>
parents: 31752
diff changeset
82 opengl_renderer m_renderer;
25862
e5a73a8c116c use wrapper class to call OpenGL functions
John W. Eaton <jwe@octave.org>
parents: 25103
diff changeset
83
25103
078b795c5219 maint: style check C++ ahead of 4.4 release.
Rik <rik@octave.org>
parents: 25054
diff changeset
84 QOpenGLContext m_os_context;
24798
45470049a43f Allow getframe and print to work without osmesa (bug #53186).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents: 24534
diff changeset
85 QOffscreenSurface m_os_surface;
22411
c69805d1fa64 maint: Style check C++ code in libgui/
Rik <rik@octave.org>
parents: 22323
diff changeset
86 };
18498
2e7cad6f180c Initial integration of QtHandles.
John W. Eaton <jwe@octave.org>
parents:
diff changeset
87
31762
d94ceed56929 GLCanvas: avoid multiple inheritance from both QWidget and QObject
John W. Eaton <jwe@octave.org>
parents: 31752
diff changeset
88 class GLCanvas : public Canvas
d94ceed56929 GLCanvas: avoid multiple inheritance from both QWidget and QObject
John W. Eaton <jwe@octave.org>
parents: 31752
diff changeset
89 {
d94ceed56929 GLCanvas: avoid multiple inheritance from both QWidget and QObject
John W. Eaton <jwe@octave.org>
parents: 31752
diff changeset
90 public:
d94ceed56929 GLCanvas: avoid multiple inheritance from both QWidget and QObject
John W. Eaton <jwe@octave.org>
parents: 31752
diff changeset
91
d94ceed56929 GLCanvas: avoid multiple inheritance from both QWidget and QObject
John W. Eaton <jwe@octave.org>
parents: 31752
diff changeset
92 GLCanvas (octave::interpreter& interp, const graphics_handle& handle,
d94ceed56929 GLCanvas: avoid multiple inheritance from both QWidget and QObject
John W. Eaton <jwe@octave.org>
parents: 31752
diff changeset
93 QWidget *parent);
d94ceed56929 GLCanvas: avoid multiple inheritance from both QWidget and QObject
John W. Eaton <jwe@octave.org>
parents: 31752
diff changeset
94
d94ceed56929 GLCanvas: avoid multiple inheritance from both QWidget and QObject
John W. Eaton <jwe@octave.org>
parents: 31752
diff changeset
95 ~GLCanvas (void);
d94ceed56929 GLCanvas: avoid multiple inheritance from both QWidget and QObject
John W. Eaton <jwe@octave.org>
parents: 31752
diff changeset
96
d94ceed56929 GLCanvas: avoid multiple inheritance from both QWidget and QObject
John W. Eaton <jwe@octave.org>
parents: 31752
diff changeset
97 void draw (const graphics_handle& handle);
d94ceed56929 GLCanvas: avoid multiple inheritance from both QWidget and QObject
John W. Eaton <jwe@octave.org>
parents: 31752
diff changeset
98 uint8NDArray do_getPixels (const graphics_handle& handle);
d94ceed56929 GLCanvas: avoid multiple inheritance from both QWidget and QObject
John W. Eaton <jwe@octave.org>
parents: 31752
diff changeset
99 void do_print (const QString& file_cmd, const QString& term,
d94ceed56929 GLCanvas: avoid multiple inheritance from both QWidget and QObject
John W. Eaton <jwe@octave.org>
parents: 31752
diff changeset
100 const graphics_handle& handle);
d94ceed56929 GLCanvas: avoid multiple inheritance from both QWidget and QObject
John W. Eaton <jwe@octave.org>
parents: 31752
diff changeset
101 void drawZoomBox (const QPoint& p1, const QPoint& p2);
d94ceed56929 GLCanvas: avoid multiple inheritance from both QWidget and QObject
John W. Eaton <jwe@octave.org>
parents: 31752
diff changeset
102 void resize (int /* x */, int /* y */,
d94ceed56929 GLCanvas: avoid multiple inheritance from both QWidget and QObject
John W. Eaton <jwe@octave.org>
parents: 31752
diff changeset
103 int /* width */, int /* height */) { }
d94ceed56929 GLCanvas: avoid multiple inheritance from both QWidget and QObject
John W. Eaton <jwe@octave.org>
parents: 31752
diff changeset
104 graphics_object selectFromAxes (const graphics_object& ax,
d94ceed56929 GLCanvas: avoid multiple inheritance from both QWidget and QObject
John W. Eaton <jwe@octave.org>
parents: 31752
diff changeset
105 const QPoint& pt);
d94ceed56929 GLCanvas: avoid multiple inheritance from both QWidget and QObject
John W. Eaton <jwe@octave.org>
parents: 31752
diff changeset
106
d94ceed56929 GLCanvas: avoid multiple inheritance from both QWidget and QObject
John W. Eaton <jwe@octave.org>
parents: 31752
diff changeset
107 QWidget * qWidget (void) { return m_glwidget; }
d94ceed56929 GLCanvas: avoid multiple inheritance from both QWidget and QObject
John W. Eaton <jwe@octave.org>
parents: 31752
diff changeset
108
d94ceed56929 GLCanvas: avoid multiple inheritance from both QWidget and QObject
John W. Eaton <jwe@octave.org>
parents: 31752
diff changeset
109 private:
d94ceed56929 GLCanvas: avoid multiple inheritance from both QWidget and QObject
John W. Eaton <jwe@octave.org>
parents: 31752
diff changeset
110
d94ceed56929 GLCanvas: avoid multiple inheritance from both QWidget and QObject
John W. Eaton <jwe@octave.org>
parents: 31752
diff changeset
111 GLWidget *m_glwidget;
d94ceed56929 GLCanvas: avoid multiple inheritance from both QWidget and QObject
John W. Eaton <jwe@octave.org>
parents: 31752
diff changeset
112
d94ceed56929 GLCanvas: avoid multiple inheritance from both QWidget and QObject
John W. Eaton <jwe@octave.org>
parents: 31752
diff changeset
113 bool begin_rendering (void);
d94ceed56929 GLCanvas: avoid multiple inheritance from both QWidget and QObject
John W. Eaton <jwe@octave.org>
parents: 31752
diff changeset
114 void end_rendering (void);
d94ceed56929 GLCanvas: avoid multiple inheritance from both QWidget and QObject
John W. Eaton <jwe@octave.org>
parents: 31752
diff changeset
115 };
d94ceed56929 GLCanvas: avoid multiple inheritance from both QWidget and QObject
John W. Eaton <jwe@octave.org>
parents: 31752
diff changeset
116
31646
c6d54dd31a7e maint: Use macros to begin/end C++ namespaces.
John W. Eaton <jwe@octave.org>
parents: 30564
diff changeset
117 OCTAVE_END_NAMESPACE(octave)
18498
2e7cad6f180c Initial integration of QtHandles.
John W. Eaton <jwe@octave.org>
parents:
diff changeset
118
2e7cad6f180c Initial integration of QtHandles.
John W. Eaton <jwe@octave.org>
parents:
diff changeset
119 #endif