From 5015c5c3a4371ab5f93ca3e542fff52a795ae0f6 Mon Sep 17 00:00:00 2001 From: Shaun Reed Date: Sat, 11 Mar 2023 21:14:48 -0500 Subject: [PATCH] WIP drag and drop --- src/app/qtkwidget.cpp | 33 +++++++++++++++++++++++++++++++++ src/app/qtkwidget.h | 8 ++++++++ 2 files changed, 41 insertions(+) diff --git a/src/app/qtkwidget.cpp b/src/app/qtkwidget.cpp index 48a19a3..4fd448e 100644 --- a/src/app/qtkwidget.cpp +++ b/src/app/qtkwidget.cpp @@ -8,6 +8,7 @@ #include #include +#include #include #include @@ -31,6 +32,7 @@ QtkWidget::QtkWidget(QWidget * parent, const QString & name) : QtkWidget::QtkWidget(QWidget * parent, const QString & name, Scene * scene) : QOpenGLWidget(parent), mDebugLogger(Q_NULLPTR), mConsole(new DebugConsole(this, name)), mScene(Q_NULLPTR) { + setAcceptDrops(true); setScene(scene); setObjectName(name); QSurfaceFormat format; @@ -77,6 +79,9 @@ void QtkWidget::initializeGL() { connect( mDebugLogger, SIGNAL(messageLogged(QOpenGLDebugMessage)), this, SLOT(messageLogged(QOpenGLDebugMessage))); +// connect( +// mDebugLogger, SIGNAL(messageLogged(QOpenGLDebugMessage)), mConsole, +// SLOT(sendLog(QOpenGLDebugMessage))); mDebugLogger->startLogging(); } @@ -140,6 +145,34 @@ void QtkWidget::toggleConsole() { * Protected Methods ******************************************************************************/ +void QtkWidget::dragEnterEvent(QDragEnterEvent * event) { + if(event->mimeData()->hasFormat("text/plain")) { + event->acceptProposedAction(); + } +} + +void QtkWidget::dropEvent(QDropEvent * event) { + mConsole->sendLog(event->mimeData()->text()); + if (event->mimeData()->hasUrls()) { + auto urls = event->mimeData()->urls(); +// if (urls.size() > 1) { +// qDebug() << "Cannot accept drop of multiple files.\n"; +// event->ignore(); +// } + auto url = urls.front(); + if (url.fileName().endsWith(".obj")) { + auto fileName = url.fileName().replace(".obj", "").toStdString(); + auto filePath = url.toLocalFile().toStdString(); + mScene->blockSignals(true); + mScene->addObject(new Qtk::Model(url.fileName().replace(".obj", "").toStdString().c_str(), ":/models/models/scythe/scythe.obj")); + mScene->blockSignals(false); + mScene->sceneUpdated(mScene->getSceneName()); +// mScene->mModels.push_back(new Qtk::Model(url.fileName().replace(".obj", "").toStdString().c_str(), url.path().toStdString().c_str())); +// event->acceptProposedAction(); + } + } +} + void QtkWidget::keyPressEvent(QKeyEvent * event) { if(event->isAutoRepeat()) { // Do not repeat input while a key is held down diff --git a/src/app/qtkwidget.h b/src/app/qtkwidget.h index 81f9114..1d7089b 100644 --- a/src/app/qtkwidget.h +++ b/src/app/qtkwidget.h @@ -136,6 +136,10 @@ namespace Qtk { * Protected Methods ************************************************************************/ + void dragEnterEvent(QDragEnterEvent * event) override; + + void dropEvent(QDropEvent * event) override; + /** * @param event Key press event to update camera input manager. */ @@ -175,6 +179,10 @@ namespace Qtk { /************************************************************************* * Private Methods ************************************************************************/ + inline void loadModel(const QString & path) { + auto * model = new Model("loadedModel", path.toStdString().c_str()); + mScene->mModels.push_back(model); + } /** * Deconstruct any resources we have allocated for this widget.