2021-09-03 12:56:57 -04:00
|
|
|
/*##############################################################################
|
|
|
|
## Author: Shaun Reed ##
|
2023-01-01 22:26:58 -05:00
|
|
|
## Legal: All Content (c) 2023 Shaun Reed, all rights reserved ##
|
|
|
|
## About: QtkWidget for Qt desktop application ##
|
2021-09-03 12:56:57 -04:00
|
|
|
## ##
|
|
|
|
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
|
|
|
##############################################################################*/
|
2022-08-21 15:37:42 -04:00
|
|
|
#ifndef QTK_QTKWIDGET_H
|
|
|
|
#define QTK_QTKWIDGET_H
|
2021-09-03 12:56:57 -04:00
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
2022-12-18 09:19:04 -05:00
|
|
|
#include <QDockWidget>
|
2021-09-03 12:56:57 -04:00
|
|
|
#include <QMatrix4x4>
|
|
|
|
#include <QOpenGLDebugLogger>
|
|
|
|
#include <QOpenGLFunctions>
|
|
|
|
#include <QOpenGLWidget>
|
2022-12-18 09:19:04 -05:00
|
|
|
#include <QPlainTextEdit>
|
2021-09-03 12:56:57 -04:00
|
|
|
|
2023-01-01 22:26:58 -05:00
|
|
|
#include <qtk/qtkapi.h>
|
|
|
|
#include <qtk/scene.h>
|
2021-09-03 12:56:57 -04:00
|
|
|
|
2022-08-07 13:12:12 -04:00
|
|
|
namespace Qtk {
|
2022-12-18 09:19:04 -05:00
|
|
|
class DebugConsole;
|
|
|
|
|
2022-11-26 18:24:38 +00:00
|
|
|
/**
|
|
|
|
* QtkWidget class to define required QOpenGLWidget functionality.
|
|
|
|
*
|
|
|
|
* This object has a Scene attached which manages the objects to render.
|
|
|
|
* Client input is passed through this widget to control the camera view.
|
|
|
|
*/
|
2022-12-18 09:19:04 -05:00
|
|
|
class QtkWidget : public QOpenGLWidget, protected QOpenGLFunctions {
|
2022-11-24 22:26:53 +00:00
|
|
|
Q_OBJECT;
|
|
|
|
|
|
|
|
public:
|
2022-11-26 18:24:38 +00:00
|
|
|
/*************************************************************************
|
|
|
|
* Contructors / Destructors
|
|
|
|
************************************************************************/
|
|
|
|
|
|
|
|
/**
|
2022-12-18 09:19:04 -05:00
|
|
|
* Qt Designer will call this ctor when creating this widget as a child.
|
|
|
|
*
|
|
|
|
* @param parent Pointer to a parent widget for this QtkWidget or nullptr.
|
2022-11-26 18:24:38 +00:00
|
|
|
*/
|
2023-01-01 22:26:58 -05:00
|
|
|
explicit QtkWidget(QWidget * parent = nullptr);
|
2022-11-26 18:24:38 +00:00
|
|
|
|
|
|
|
/**
|
2022-12-18 09:19:04 -05:00
|
|
|
* Default construct a QtkWidget.
|
2022-11-26 18:24:38 +00:00
|
|
|
*
|
2022-12-18 09:19:04 -05:00
|
|
|
* @param parent Pointer to a parent widget or nullptr if no parent.
|
|
|
|
* @param name An objectName for the new QtkWidget.
|
2022-11-26 18:24:38 +00:00
|
|
|
*/
|
2022-12-18 09:19:04 -05:00
|
|
|
explicit QtkWidget(QWidget * parent, const QString & name);
|
2022-11-26 18:24:38 +00:00
|
|
|
|
|
|
|
/**
|
2022-12-18 09:19:04 -05:00
|
|
|
* Construct a custom QtkWidget.
|
2022-11-26 18:24:38 +00:00
|
|
|
*
|
2022-12-18 09:19:04 -05:00
|
|
|
* @param parent Pointer to a parent widget or nullptr if no parent.
|
|
|
|
* @param name An objectName for the new QtkWidget.
|
|
|
|
* @param scene Pointer to a custom class inheriting from Qtk::Scene.
|
2022-11-26 18:24:38 +00:00
|
|
|
*/
|
2022-12-18 09:19:04 -05:00
|
|
|
QtkWidget(QWidget * parent, const QString & name, Qtk::Scene * scene);
|
2022-11-26 18:24:38 +00:00
|
|
|
|
2023-01-01 22:26:58 -05:00
|
|
|
~QtkWidget();
|
2022-11-24 22:26:53 +00:00
|
|
|
|
2022-11-26 18:24:38 +00:00
|
|
|
/*************************************************************************
|
2023-01-01 22:26:58 -05:00
|
|
|
* Public Methods
|
2022-11-26 18:24:38 +00:00
|
|
|
************************************************************************/
|
|
|
|
|
2023-01-01 22:26:58 -05:00
|
|
|
/**
|
|
|
|
* Constructs a QAction to hide / show this DebugConsole.
|
|
|
|
* @return QAction to toggle visibility of this DebugConsole.
|
|
|
|
*/
|
|
|
|
QAction * getActionToggleConsole();
|
2022-11-26 18:24:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when the widget is first constructed.
|
|
|
|
*/
|
2022-11-24 22:26:53 +00:00
|
|
|
void initializeGL() override;
|
2022-11-26 18:24:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when the application window is resized.
|
|
|
|
*
|
|
|
|
* @param width The new width of the window.
|
|
|
|
* @param height The new height of the window.
|
|
|
|
*/
|
2022-11-24 22:26:53 +00:00
|
|
|
void resizeGL(int width, int height) override;
|
2021-09-03 12:56:57 -04:00
|
|
|
|
2022-11-26 18:24:38 +00:00
|
|
|
/**
|
|
|
|
* Called when OpenGL repaints the widget.
|
|
|
|
*/
|
|
|
|
void paintGL() override;
|
|
|
|
|
|
|
|
/*************************************************************************
|
|
|
|
* Accessors
|
|
|
|
************************************************************************/
|
|
|
|
|
2023-01-01 22:26:58 -05:00
|
|
|
/**
|
|
|
|
* @return The active scene being viewed in this widget.
|
|
|
|
*/
|
2022-11-24 22:26:53 +00:00
|
|
|
inline Qtk::Scene * getScene() { return mScene; }
|
2021-09-03 12:56:57 -04:00
|
|
|
|
2022-11-26 18:24:38 +00:00
|
|
|
/*************************************************************************
|
|
|
|
* Setters
|
|
|
|
************************************************************************/
|
|
|
|
|
2023-01-01 22:26:58 -05:00
|
|
|
/**
|
|
|
|
* @param scene The new scene to view.
|
|
|
|
*/
|
2022-12-18 09:19:04 -05:00
|
|
|
void setScene(Qtk::Scene * scene);
|
2023-01-01 22:26:58 -05:00
|
|
|
|
2022-12-18 09:19:04 -05:00
|
|
|
public slots:
|
2023-01-01 22:26:58 -05:00
|
|
|
|
2022-12-18 09:19:04 -05:00
|
|
|
/**
|
|
|
|
* Toggle visibility of the DebugConsole associated with this QtkWidget.
|
|
|
|
*/
|
|
|
|
void toggleConsole();
|
2021-09-03 12:56:57 -04:00
|
|
|
|
2023-01-01 22:26:58 -05:00
|
|
|
signals:
|
|
|
|
/**
|
|
|
|
* Log a message to the DebugConsole associated with this widget.
|
|
|
|
* @param message The message to log.
|
|
|
|
* @param context The context of the log message.
|
|
|
|
*/
|
|
|
|
void sendLog(const QString & message, DebugContext context = Status);
|
|
|
|
|
|
|
|
protected:
|
2022-11-26 18:24:38 +00:00
|
|
|
/*************************************************************************
|
2023-01-01 22:26:58 -05:00
|
|
|
* Protected Methods
|
2022-11-26 18:24:38 +00:00
|
|
|
************************************************************************/
|
|
|
|
|
2023-01-01 22:26:58 -05:00
|
|
|
/**
|
|
|
|
* @param event Key press event to update camera input manager.
|
|
|
|
*/
|
|
|
|
void keyPressEvent(QKeyEvent * event) override;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param event Key release event to update camera input manager.
|
|
|
|
*/
|
|
|
|
void keyReleaseEvent(QKeyEvent * event) override;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param event Mouse button press event to update camera input manager.
|
|
|
|
*/
|
|
|
|
void mousePressEvent(QMouseEvent * event) override;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param event Mouse button release event to update camera input manager.
|
|
|
|
*/
|
|
|
|
void mouseReleaseEvent(QMouseEvent * event) override;
|
|
|
|
|
|
|
|
protected slots:
|
2022-11-26 18:24:38 +00:00
|
|
|
/**
|
|
|
|
* Called when the `frameSwapped` signal is caught.
|
|
|
|
* See definition of initializeGL()
|
|
|
|
*/
|
2022-11-24 22:26:53 +00:00
|
|
|
void update();
|
2022-11-26 18:24:38 +00:00
|
|
|
|
2022-08-07 13:12:12 -04:00
|
|
|
#ifdef QTK_DEBUG
|
2022-11-26 18:24:38 +00:00
|
|
|
/**
|
|
|
|
* Called when the `messageLogged` signal is caught.
|
|
|
|
* See definition of initializeGL()
|
|
|
|
*
|
|
|
|
* @param msg The message logged.
|
|
|
|
*/
|
2022-12-18 09:19:04 -05:00
|
|
|
void messageLogged(const QOpenGLDebugMessage & msg);
|
2022-08-07 13:12:12 -04:00
|
|
|
#endif
|
2021-09-03 12:56:57 -04:00
|
|
|
|
2022-11-24 22:26:53 +00:00
|
|
|
private:
|
2022-11-26 18:24:38 +00:00
|
|
|
/*************************************************************************
|
|
|
|
* Private Methods
|
|
|
|
************************************************************************/
|
|
|
|
|
2023-01-01 22:26:58 -05:00
|
|
|
/**
|
|
|
|
* Deconstruct any resources we have allocated for this widget.
|
|
|
|
*/
|
|
|
|
void teardownGL();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback function to update input for camera controls
|
|
|
|
*/
|
2022-11-24 22:26:53 +00:00
|
|
|
static void updateCameraInput();
|
2021-09-03 12:56:57 -04:00
|
|
|
|
2022-08-07 13:12:12 -04:00
|
|
|
#ifdef QTK_DEBUG
|
2023-01-01 22:26:58 -05:00
|
|
|
/**
|
|
|
|
* Prints OpenGL context information at start of debug session.
|
|
|
|
*/
|
2022-11-24 22:26:53 +00:00
|
|
|
void printContextInformation();
|
2022-08-07 13:12:12 -04:00
|
|
|
#endif
|
2023-01-01 22:26:58 -05:00
|
|
|
|
2022-11-26 18:24:38 +00:00
|
|
|
/*************************************************************************
|
|
|
|
* Private Members
|
|
|
|
************************************************************************/
|
|
|
|
|
2023-01-01 22:26:58 -05:00
|
|
|
#ifdef QTK_DEBUG
|
|
|
|
QOpenGLDebugLogger * mDebugLogger;
|
|
|
|
#endif
|
2022-11-26 18:24:38 +00:00
|
|
|
Qtk::Scene * mScene;
|
2022-12-18 09:19:04 -05:00
|
|
|
Qtk::DebugConsole * mConsole;
|
2023-01-01 22:26:58 -05:00
|
|
|
bool mConsoleActive = false;
|
2022-08-07 13:12:12 -04:00
|
|
|
};
|
2022-11-24 22:26:53 +00:00
|
|
|
} // namespace Qtk
|
2021-09-03 12:56:57 -04:00
|
|
|
|
2022-11-24 22:26:53 +00:00
|
|
|
#endif // QTK_QTKWIDGET_H
|