WQtGLWidget.h 9.14 KB
Newer Older
wiebel's avatar
wiebel committed
1 2
//---------------------------------------------------------------------------
//
3
// Project: OpenWalnut ( http://www.openwalnut.org )
wiebel's avatar
wiebel committed
4
//
5 6
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
wiebel's avatar
wiebel committed
7
//
wiebel's avatar
wiebel committed
8
// This file is part of OpenWalnut.
wiebel's avatar
wiebel committed
9
//
wiebel's avatar
wiebel committed
10
// OpenWalnut is free software: you can redistribute it and/or modify
wiebel's avatar
wiebel committed
11 12 13 14
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
wiebel's avatar
wiebel committed
15
// OpenWalnut is distributed in the hope that it will be useful,
wiebel's avatar
wiebel committed
16 17 18 19 20
// 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
wiebel's avatar
wiebel committed
21
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
wiebel's avatar
wiebel committed
22
//
wiebel's avatar
wiebel committed
23 24
//---------------------------------------------------------------------------

25 26
#ifndef WQTGLWIDGET_H
#define WQTGLWIDGET_H
wiebel's avatar
wiebel committed
27

28 29 30 31 32 33 34
#include <string>

#include <boost/shared_ptr.hpp>
#include <boost/signals2/signal.hpp>

#include <QtCore/QTimer>
#include <QtGui/QWidget>
35
#include <QtGui/QMenu>
36 37 38 39 40 41 42
#include <QtOpenGL/QGLWidget>
#include <QtOpenGL/QGLFormat>

#include "core/common/WColor.h" // not forwarded due to duplicated typedef
#include "core/graphicsEngine/WGECamera.h"
#include "core/graphicsEngine/WGEViewer.h"

43 44
#include "WQtGLScreenCapture.h"

45 46
class WSettingAction;

47
#ifndef _WIN32
48
    #define IS_A_QGLWIDGET
49 50 51 52 53
    typedef QGLWidget WQtGLWidgetParent;
#else
    typedef QWidget WQtGLWidgetParent;
#endif

54 55 56 57 58 59
/**
 * A widget containing an open gl display area. This initializes OpenGL context and adds a view to the
 * engine.
 * \ingroup gui
 */
// NOTE: to make this work with MOC, the defines must be set before MOC runs (ensured in Build system)
60
class WQtGLWidget: public WQtGLWidgetParent
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
{
    Q_OBJECT

public:
    /**
     * Default constructor.
     *
     * \param nameOfViewer Name of the Viewer
     * \param parent Parent widget.
     * \param projectionMode decides whether the widget uses perspective or othographic projection
     * \param shareWidget this widget will share OpenGL display lists and texture objects with shareWidget
     *
     * \return
     */
    explicit WQtGLWidget( std::string nameOfViewer, QWidget* parent = 0,
76
        WGECamera::ProjectionMode projectionMode = WGECamera::ORTHOGRAPHIC, const QWidget * shareWidget = 0 );
77 78 79 80 81 82 83

    /**
     * Destructor.
     */
    virtual ~WQtGLWidget();

    /**
84
     * Returns the recommended size for the widget to allow
85
     * parent widgets to give it a proper initial layout
86 87
     *
     * \return size hint of widget
88 89 90 91 92 93 94 95
     */
    QSize sizeHint() const;

    /**
     * List of currently possible camera manipulators.
     */
    enum CameraManipulators
    {
96
        TRACKBALL, TWO_D, NO_OP
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
    };

    /**
     * Sets the camera manipulator to use.
     *
     * \param manipulator the manipulator.
     */
    void setCameraManipulator( CameraManipulators manipulator );

    /**
     * Returns the actually set camera manipulator.
     *
     * \return the manipulator.
     */
    CameraManipulators getCameraManipulators();

    /**
     * Get the included viewer.
     *
     * \return a shared pointer to the viewer
     */
    boost::shared_ptr< WGEViewer > getViewer() const;

    /**
     * Creates and returns a default OpenGL format description with vertical sync enabled.
     *
     * \return the format descriptor
     */
    static const QGLFormat getDefaultFormat();

127 128 129 130 131 132 133 134 135 136 137 138 139 140
    /**
     * Returns the setting for throwing.
     *
     * \return the setting
     */
    WSettingAction* getThrowingSetting() const;

    /**
     * Returns the action used to configure the background color of this widget.
     *
     * \return the action.
     */
    QAction* getBackgroundColorAction() const;

141 142 143 144 145 146 147 148
    /**
     * Adds a screen capture dock using this view's screen capture callback.
     *
     * \param parent the main window parent
     * \return the capture dock instance.
     */
    WQtGLScreenCapture* getScreenCapture( WMainWindow* parent ) const;

149 150 151 152 153 154 155
    /**
     * The presets menu.
     *
     * \return the menu
     */
    QMenu* getCameraPresetsMenu();

156 157 158 159 160 161
    /**
     * The action to trigger a camera reset.
     *
     * \return the action
     */
    QAction* getCameraResetAction();
162 163 164 165 166 167 168
signals:

    /**
     * Signals that the first frame was rendered.
     */
    void renderedFirstFrame();

169 170 171 172 173 174
public slots:
    /**
     * Resets the contained view using the installed manipulator.
     */
    void reset();

175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
    /**
     * Sets the left preset view of the main viewer.
     */
    void setPresetViewLeft();

    /**
     * Sets the right preset view of the main viewer.
     */
    void setPresetViewRight();

    /**
     * Sets the superior preset view of the main viewer.
     */
    void setPresetViewSuperior();

    /**
     * Sets the inferior preset view of the main viewer.
     */
    void setPresetViewInferior();

    /**
     * Sets the anterior preset view of the main viewer.
     */
    void setPresetViewAnterior();

    /**
     * Sets the posterior preset view of the main viewer.
     */
    void setPresetViewPosterior();

205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
protected:
    /**
     * The viewer to the scene.
     */
    boost::shared_ptr<WGEViewer> m_Viewer;

    /**
     * The name of the viewer.
     */
    std::string m_nameOfViewer;


    /**
     * Event handler for double clicks.
     *
     * \param event the event description.
     */
    virtual void mouseDoubleClickEvent( QMouseEvent* event );

    /**
     * Event handler for  resize events.
     *
     * \param event the event description.
     */
    virtual void resizeEvent( QResizeEvent* event );

    /**
     * Event handler for  resize events.
     *
     * \param width the new width.
     * \param height the new height.
     */
    virtual void resizeGL( int width, int height );

    /**
     * Event handler for key press.
     *
     * \param event the event description.
     */
    virtual void keyPressEvent( QKeyEvent* event );

    /**
     * Event handler for key release.
     *
     * \param event the event description.
     */
    virtual void keyReleaseEvent( QKeyEvent* event );

    /**
     * Event handler for mouse button press.
     *
     * \param event the event description.
     */
    virtual void mousePressEvent( QMouseEvent* event );

    /**
     * Event handler for mouse button release.
     *
     * \param event the event description.
     */
    virtual void mouseReleaseEvent( QMouseEvent* event );

    /**
     * Event handler for mouse moves.
     *
     * \param event the event description.
     */
    virtual void mouseMoveEvent( QMouseEvent* event );

    /**
     * Event handler for the mouse wheel.
     *
     * \param event the event description.
     */
    virtual void wheelEvent( QWheelEvent* event );

    /**
     * QT Callback for handling repaints.
     *
     * \param event event descriptor.
     */
    virtual void paintEvent( QPaintEvent* event );

    /**
     * QT Callback for handling repaints.
     */
    virtual void paintGL();

    /**
     * Simply translate the mouse button from an event to an int.
     *
     * \param event the QT Event.
     *
     * \return the translated button number.
     */
    int translateButton( QMouseEvent* event );

    /**
     * Stores the current manipulator.
     */
    CameraManipulators m_CurrentManipulator;

    /**
     * Camera projection mode used to initialize widget. May not be the actual one!
     */
    WGECamera::ProjectionMode m_initialProjectionMode;

312 313 314 315 316 317 318 319 320 321 322 323
    /**
     * Custom event dispatcher. Gets called by QT's Event system every time an event got sent to this widget. This event handler
     * processes the notifyrender events. Others get forwarded.
     *
     * \note QT Doc says: use event() for custom events.
     *
     * \param event the event that got transmitted.
     *
     * \return true if the event got handled properly.
     */
    virtual bool event( QEvent* event );

324 325 326 327 328 329 330 331 332 333
private:
    /**
     * Timer for periodic repaints.
     */
    QTimer m_Timer;

    /**
     * Holds the recommended size for the widget
     */
    QSize m_recommendedSize;
334 335 336 337 338

    /**
     * This flag is set to true if the first paint call occured. See the paint method for details.
     */
    bool m_firstPaint;
339 340 341 342 343 344 345 346 347 348 349

    /**
     * The setting specifying whether the viewer's camera can be thrown.
     */
    WSettingAction* m_allowThrowSetting;

    /**
     * Action to trigger some colordialog for background-color-selection.
     */
    QAction* m_changeBGColorAction;

350 351 352 353 354
    /**
     * Called by the WGEViewer to notify about the first frame rendered
     */
    void notifyFirstRenderedFrame();

355 356 357 358 359
    /**
     * Camera menu
     */
    QMenu* m_cameraPresetMenu;

360 361 362 363 364
    /**
     * Camera reset.
     */
    QAction* m_cameraResetAction;

365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
private slots:
    /**
     * Function to handle updates in the setting \ref m_allowThrowSetting.
     */
    void updateThrowing();

    /**
     * Asks user for a new color for this widget.
     */
    void changeBGColor();

    /**
     * Updates the color of this widget according to the current settings.
     */
    void updateViewerBackground();
380
};
wiebel's avatar
wiebel committed
381

382
#endif  // WQTGLWIDGET_H