diff --git a/src/designer-plugins/qtkwidget.cpp b/src/designer-plugins/qtkwidget.cpp index 1fd5bec..89e2dc5 100644 --- a/src/designer-plugins/qtkwidget.cpp +++ b/src/designer-plugins/qtkwidget.cpp @@ -21,6 +21,9 @@ using namespace Qtk; +/// Static manager for all QtkWidget instances. +QtkWidgetManager QtkWidget::mWidgetManager; + /******************************************************************************* * Constructors, Destructors ******************************************************************************/ @@ -36,9 +39,11 @@ QtkWidget::QtkWidget(QWidget * parent, const QString & name, Scene * scene) : QOpenGLWidget(parent), mDebugLogger(Q_NULLPTR), mConsole(new DebugConsole(this, name)), mScene(Q_NULLPTR) { + setObjectName(name); + mWidgetManager.add_widget(this); + setAcceptDrops(true); setScene(scene); - setObjectName(name); QSurfaceFormat format; format.setRenderableType(QSurfaceFormat::OpenGL); format.setProfile(QSurfaceFormat::CoreProfile); diff --git a/src/designer-plugins/qtkwidget.h b/src/designer-plugins/qtkwidget.h index edfa471..5fb2222 100644 --- a/src/designer-plugins/qtkwidget.h +++ b/src/designer-plugins/qtkwidget.h @@ -119,6 +119,13 @@ namespace Qtk */ void setScene(Qtk::Scene * scene); + /************************************************************************* + * Public Members + ************************************************************************/ + + /// Static manager for all QtkWidget instances. + static class QtkWidgetManager mWidgetManager; + public slots: /** @@ -212,6 +219,54 @@ namespace Qtk Qtk::DebugConsole * mConsole; bool mConsoleActive = true; }; + + /** + * 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 << "'"; + } + qDebug() << this << " Adding new QtkWidget named '" << name << "'"; + mQtkWidgets[name] = widget; + return; + } + + private: + std::unordered_map mQtkWidgets; + }; } // namespace Qtk #endif // QTK_QTKWIDGET_H diff --git a/src/designer-plugins/toolbox.cpp b/src/designer-plugins/toolbox.cpp index c3a7ab7..e03213a 100644 --- a/src/designer-plugins/toolbox.cpp +++ b/src/designer-plugins/toolbox.cpp @@ -8,7 +8,7 @@ */ #include "toolbox.h" -#include "app/qtkmainwindow.h" +#include "qtkwidget.h" #include "ui_toolbox.h" #include @@ -25,7 +25,7 @@ ToolBox::ToolBox(QWidget * parent) : QDockWidget(parent), ui(new Ui::ToolBox) void ToolBox::updateFocus(const QString & name) { auto object = - MainWindow::getMainWindow()->getQtkWidget()->getScene()->getObject(name); + Qtk::QtkWidget::mWidgetManager.get_widget()->getScene()->getObject(name); if (object != Q_NULLPTR) { removePages(); createPageProperties(object);