qtk/src/designer-plugins/qtkwidget.h

279 lines
8.7 KiB
C
Raw Normal View History

2021-09-03 12:56:57 -04:00
/*##############################################################################
## Author: Shaun Reed ##
## 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 ##
##############################################################################*/
#ifndef QTK_QTKWIDGET_H
#define QTK_QTKWIDGET_H
2021-09-03 12:56:57 -04:00
#include <iostream>
#include <QDockWidget>
2021-09-03 12:56:57 -04:00
#include <QMatrix4x4>
#include <QOpenGLDebugLogger>
#include <QOpenGLFunctions>
#include <QOpenGLWidget>
#include <QPlainTextEdit>
2021-09-03 12:56:57 -04:00
2025-03-15 10:14:16 -04:00
#include "qtk/qtkapi.h"
#include "qtk/scene.h"
2021-09-03 12:56:57 -04:00
2025-03-08 11:47:19 -05:00
namespace Qtk
{
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.
*/
2025-03-08 11:47:19 -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
************************************************************************/
/**
* 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
*/
explicit QtkWidget(QWidget * parent = nullptr);
2022-11-26 18:24:38 +00:00
/**
* Default construct a QtkWidget.
2022-11-26 18:24:38 +00: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
*/
explicit QtkWidget(QWidget * parent, const QString & name);
2022-11-26 18:24:38 +00:00
/**
* Construct a custom QtkWidget.
2022-11-26 18:24:38 +00: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
*/
QtkWidget(QWidget * parent, const QString & name, Qtk::Scene * scene);
2022-11-26 18:24:38 +00:00
~QtkWidget();
2022-11-24 22:26:53 +00:00
2022-11-26 18:24:38 +00:00
/*************************************************************************
* Public Methods
2022-11-26 18:24:38 +00: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
************************************************************************/
/**
* @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
/**
* @return Pointer to the QOpenGLDebugLogger attached to this widget.
*/
2025-03-08 11:47:19 -05:00
inline QOpenGLDebugLogger * getOpenGLDebugLogger()
{
return mDebugLogger;
}
2022-11-26 18:24:38 +00:00
/*************************************************************************
* Setters
************************************************************************/
/**
* @param scene The new scene to view.
*/
void setScene(Qtk::Scene * scene);
2021-09-03 12:56:57 -04:00
/**
* @param window The QMainWindow that owns this QtkWidget.
*/
void setMainWindow(QMainWindow * window) { mMainWindow = window; }
2025-03-15 12:28:08 -04:00
/*************************************************************************
* Public Members
************************************************************************/
/// Static manager for all QtkWidget instances.
static class QtkWidgetManager mWidgetManager;
public slots:
2022-11-26 18:24:38 +00:00
/**
* Toggle visibility of the DebugConsole associated with this QtkWidget.
2022-11-26 18:24:38 +00:00
*/
void toggleConsole();
2022-11-26 18:24:38 +00:00
signals:
2022-11-26 18:24:38 +00:00
/**
* Log a message to the DebugConsole associated with this widget.
* @param message The message to log.
* @param context The context of the log message.
2022-11-26 18:24:38 +00:00
*/
void sendLog(const QString & message, DebugContext context = Status);
2021-09-03 12:56:57 -04:00
2023-12-27 19:36:47 +00:00
// TODO: Use this signal in treeview and toolbox to update object
// properties
2025-03-08 11:24:13 -05:00
void objectFocusChanged(QString objectName);
2023-12-27 19:36:47 +00:00
2022-11-24 22:26:53 +00:00
protected:
2022-11-26 18:24:38 +00:00
/*************************************************************************
* Protected Methods
************************************************************************/
2023-12-27 19:36:47 +00:00
void dragEnterEvent(QDragEnterEvent * event) override;
void dropEvent(QDropEvent * event) override;
/**
* @param event Key press event to update camera input manager.
*/
2022-11-24 22:26:53 +00:00
void keyPressEvent(QKeyEvent * event) override;
/**
* @param event Key release event to update camera input manager.
*/
2022-11-24 22:26:53 +00:00
void keyReleaseEvent(QKeyEvent * event) override;
/**
* @param event Mouse button press event to update camera input manager.
*/
2022-11-24 22:26:53 +00:00
void mousePressEvent(QMouseEvent * event) override;
/**
* @param event Mouse button release event to update camera input manager.
*/
2022-11-24 22:26:53 +00:00
void mouseReleaseEvent(QMouseEvent * event) override;
2021-09-03 12:56:57 -04:00
protected slots:
/**
* Called when the `frameSwapped` signal is caught.
* See definition of initializeGL()
*/
void update();
/**
* Called when the `messageLogged` signal is caught.
* See definition of initializeGL()
2023-12-27 19:36:47 +00:00
* https://doc.qt.io/qt-6/qopengldebuglogger.html#signals
*
* @param msg The message logged.
*/
void messageLogged(const QOpenGLDebugMessage & msg);
2022-11-24 22:26:53 +00:00
private:
2022-11-26 18:24:38 +00:00
/*************************************************************************
* Private Methods
************************************************************************/
/**
* 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
/**
* Prints OpenGL context information at start of debug session.
*/
2022-11-24 22:26:53 +00:00
void printContextInformation();
2022-11-26 18:24:38 +00:00
/*************************************************************************
* Private Members
************************************************************************/
QOpenGLDebugLogger * mDebugLogger;
2022-11-26 18:24:38 +00:00
Qtk::Scene * mScene;
Qtk::DebugConsole * mConsole;
2025-03-09 11:43:38 -04:00
bool mConsoleActive = true;
QMainWindow * mMainWindow = Q_NULLPTR;
};
2025-03-15 12:28:08 -04:00
/**
* Struct to help manage QtkWidget instances.
* When a QtkWidget is constructed we register it by calling add_widget().
* Widgets can be retrieved by their unique name.
*/
struct QtkWidgetManager {
/**
* Get a QtkWidget by name.
*
* @param name The name of the QtkWidget to retrieve.
*/
QtkWidget * get_widget(const QString & name = "QtkWidget")
{
if (mQtkWidgets.count(name) == 0) {
qDebug() << this
<< " Attempt to get QtkWidget that doesn't exist: " << name;
return Q_NULLPTR;
}
return mQtkWidgets[name];
}
/**
* Register a QtkWidget with this manager.
*
* If the QObject::objectName is unset the widget and QObject is renamed
* 'Unamed-QtkWidget'.
*
* @param widget Pointer to the QtkWidget to register to this manager.
*/
void add_widget(QtkWidget * widget)
{
auto name = widget->objectName();
name = name == "" ? "Unamed-QtkWidget" : name;
if (mQtkWidgets.count(name)) {
qDebug()
<< "[QtkWidgetManager " << this
<< " ] Failed to add a QtkWidget with the previously used name '"
<< name << "'";
return;
2025-03-15 12:28:08 -04:00
}
qDebug() << this << " Adding new QtkWidget named '" << name << "'";
mQtkWidgets[name] = widget;
}
private:
std::unordered_map<QString, QtkWidget *> mQtkWidgets;
};
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