11.08., 9:00 - 11:00: Due to updates GitLab will be unavailable for some minutes between 09:00 and 11:00.

qtcanvas.h 6.88 KB
Newer Older
1 2
/**********************************************************************
 *                                                                    *
3
 * cgt - CAMP Graphics Toolbox, Copyright (C) 2012-2015               *
4 5 6
 *     Chair for Computer Aided Medical Procedures                    *
 *     Technische Universitaet Muenchen, Germany.                     *
 *     <http://campar.in.tum.de/>                                     *
7
 *                                                                    *
8 9 10 11
 * forked from tgt - Tiny Graphics Toolbox, Copyright (C) 2006-2011   *
 *     Visualization and Computer Graphics Group, Department of       *
 *     Computer Science, University of Muenster, Germany.             *
 *     <http://viscg.uni-muenster.de>                                 *
12
 *                                                                    *
13
 * This file is part of the cgt library. This library is free         *
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 * software; you can redistribute it and/or modify it under the terms *
 * of the GNU Lesser General Public License version 2.1 as published  *
 * by the Free Software Foundation.                                   *
 *                                                                    *
 * This library is distributed in the hope that it will be useful,    *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of     *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the       *
 * GNU Lesser General Public License for more details.                *
 *                                                                    *
 * You should have received a copy of the GNU Lesser General Public   *
 * License in the file "LICENSE.txt" along with this library.         *
 * If not, see <http://www.gnu.org/licenses/>.                        *
 *                                                                    *
 **********************************************************************/

29 30
#ifndef CGT_QTCANVAS_H
#define CGT_QTCANVAS_H
31

32 33
#include "cgt/glcanvas.h"
#include "cgt/types.h"
34

35
#include <QOpenGLWidget>
36 37 38 39 40 41
#include <QGLFormat>
#include <QMouseEvent>
#include <QKeyEvent>

#include <deque>

42
namespace cgt {
43 44

/**
45
 * Qt implementation of GLCanvas. Inherits QGLWidget and combines the Qt methods and cgt methods.
46
 */
47
class CGT_API QtCanvas : public QOpenGLWidget, public GLCanvas {
48 49
    Q_OBJECT;

50 51 52 53 54 55 56 57
public:
    /**
     * The constructor. Allows the user to specify a shared widget that this canvas will share
     * the OpenGL context with. Also, it is possible to specify whether or not a custom
     * event-loop has to be used.
     *
     * @param parent The parent widget of this canvas.
     * @param shared If this is true, this canvas will share its OpenGL context with the static \a shareWidget_.
58
     * @param f Qt::WindowFlags can be passed to this constructor to control the qt features, like stereo-buffering.
59 60 61 62 63 64
     * @param useCustomEventloop Are the incoming events supposed to be handled by the standard callbacks or not?
     * @param name A name can be passed for debugging purposes.
     */
    QtCanvas(const std::string& title = "",
             const ivec2& size = ivec2(DEFAULT_WINDOW_WIDTH, DEFAULT_WINDOW_HEIGHT),
             const Buffers buffers = RGBADD,
65
             QWidget* parent = 0, bool shared = false, Qt::WindowFlags f = 0, char* name = 0);
66

67
    QtCanvas(QWidget* parent, bool shared = false, Qt::WindowFlags f = 0, char* name = 0);
68 69 70 71

    /**
     * Destructor. Closes window (if canvas is a window).
     */
72
    virtual ~QtCanvas();
73

74 75 76
    virtual void moveThreadAffinity(void* threadPointer) override;
    virtual void* getCurrentThreadPointer() override;

77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92

    /**
     * Called by the Qt framework once at the beginning, before rendering starts.
     * This function calls the corresponding GLCanvas method \a initialize.
     */
    virtual void initializeGL();

    /**
     * This is called by the Qt framework every time the canvas is resized.
     * This function calls the corresponding GLCanvas method \a sizeChanged.
     *
     * @param w The new width of the canvas.
     * @param h The new height of the canvas.
     */
    virtual void resizeGL(int w, int h);

grunau's avatar
grunau committed
93 94 95 96 97 98 99 100
    /*
     * Return the recommended size for the canvas.
     *
     * This method is called by Qt to determine how much space to allocate to display the canvas.
     * The returned size is the same as reported by getSize().
     */
    virtual QSize sizeHint() const;

101 102
    virtual void setSize(ivec2 newSize);

103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
    /**
     * Called by Qt if there is a paint event; it uses the \a painter_ to paint() something.
     */
    virtual void paintGL();

    /**
     * If you manually want to cause a paint-event, use this function. It will call paintGL()
     * via updateGL(). This will cause immediate repainting.
     */
    virtual void repaint();

    /**
     * If you manually want to cause a paint-event, use this function. It will call QWidget::update()
     * and repaint when entering main loop next time.
     */
    virtual void update();

120 121 122 123 124 125

    /// Acqures this canvas as current context
    virtual void acquireAsCurrentContext();
    /// Releases this canvas as current context
    virtual void releaseAsCurrentContext();

126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
    /// swap buffers
    virtual void swap();

    virtual void toggleFullScreen();

    virtual void enterEvent(QEvent* e);
    virtual void leaveEvent(QEvent* e);
    virtual void mousePressEvent(QMouseEvent* e);
    virtual void mouseReleaseEvent (QMouseEvent* e);
    virtual void mouseMoveEvent(QMouseEvent*  e);
    virtual void mouseDoubleClickEvent(QMouseEvent* e);
    virtual void wheelEvent(QWheelEvent* e);
    virtual void timerEvent(QTimerEvent* e);

    virtual void keyPressEvent(QKeyEvent* event);
    virtual void keyReleaseEvent(QKeyEvent* event);

    ///
144
    /// Helpers used to generate cgt-Events out of qt-Events
145 146
    ///

147 148 149 150 151
    // map one Qt-mousebutton to one cgt-mousebutton
    static cgt::MouseEvent::MouseButtons getButton(QMouseEvent* e);
    // map a set of Qt-mousebuttons to a set of cgt-mousebuttons
    static cgt::MouseEvent::MouseButtons getButtons(QMouseEvent* e);
    static cgt::Event::Modifier getModifier(QInputEvent* e);
152
    static KeyEvent::KeyCode getKey(int key);
153 154
    static QSurfaceFormat getQGLSurfaceFormat(const Buffers buffers);
    static QSurfaceFormat getQGLSurfaceFormat();
155

156 157 158 159 160
signals:
    void s_sizeChangedExternally(int w, int h);

protected slots:
    void sizeChangedExternally(int w, int h);
Jakob Weiss's avatar
Jakob Weiss committed
161
    void changeFullScreen(bool fullscreen);
162

163
protected:
164 165
    static QOpenGLWidget* shareWidget_;     ///< widget that this canvas shares the OpenGL context with
                                            ///< TODO: This is very bad design. Context sharing should happen on another level and not using a static
166 167 168
    
signals:
    void fullScreenChanged(bool fullscreen);
169 170 171 172 173

};

} // namespace

174
#endif // CGT_QTCANVAS_H