From 749bc1ee9bcb3929162a16f28858dbb8dcc6134f Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Tue, 17 Mar 2015 08:38:39 -0700 Subject: [PATCH] support for loading of JS files by drag, web page urls, and openfile events --- interface/src/Application.cpp | 60 +++++++++++++++++++++++--------- interface/src/Application.h | 2 ++ interface/src/GLCanvas.cpp | 4 +-- interface/src/ui/DataWebPage.cpp | 3 ++ 4 files changed, 50 insertions(+), 19 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 0c27ecb7de..5c7fb5feef 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -891,9 +891,10 @@ bool Application::event(QEvent* event) { DependencyManager::get()->handleLookupString(fileEvent->url().toString()); } else if (url.path().toLower().endsWith(SVO_EXTENSION)) { emit svoImportRequested(url.url()); + } else if (url.path().toLower().endsWith(JS_EXTENSION)) { + askToLoadScript(url.toString()); } } - return false; } @@ -1454,31 +1455,43 @@ void Application::wheelEvent(QWheelEvent* event) { void Application::dropEvent(QDropEvent *event) { QString snapshotPath; const QMimeData *mimeData = event->mimeData(); + bool atLeastOneFileAccepted = false; foreach (QUrl url, mimeData->urls()) { auto lower = url.path().toLower(); if (lower.endsWith(SNAPSHOT_EXTENSION)) { snapshotPath = url.toLocalFile(); - break; + + + SnapshotMetaData* snapshotData = Snapshot::parseSnapshotData(snapshotPath); + if (snapshotData) { + if (!snapshotData->getDomain().isEmpty()) { + DependencyManager::get()->getDomainHandler().setHostnameAndPort(snapshotData->getDomain()); + } + + _myAvatar->setPosition(snapshotData->getLocation()); + _myAvatar->setOrientation(snapshotData->getOrientation()); + atLeastOneFileAccepted = true; + break; // don't process further files + } else { + QMessageBox msgBox; + msgBox.setText("No location details were found in the file " + + snapshotPath + ", try dragging in an authentic Hifi snapshot."); + + msgBox.setStandardButtons(QMessageBox::Ok); + msgBox.exec(); + } } else if (lower.endsWith(SVO_EXTENSION)) { emit svoImportRequested(url.url()); event->acceptProposedAction(); - return; + atLeastOneFileAccepted = true; + } else if (lower.endsWith(JS_EXTENSION)) { + askToLoadScript(url.url()); + atLeastOneFileAccepted = true; } } - - SnapshotMetaData* snapshotData = Snapshot::parseSnapshotData(snapshotPath); - if (snapshotData) { - if (!snapshotData->getDomain().isEmpty()) { - DependencyManager::get()->getDomainHandler().setHostnameAndPort(snapshotData->getDomain()); - } - - _myAvatar->setPosition(snapshotData->getLocation()); - _myAvatar->setOrientation(snapshotData->getOrientation()); - } else { - QMessageBox msgBox; - msgBox.setText("No location details were found in this JPG, try dragging in an authentic Hifi snapshot."); - msgBox.setStandardButtons(QMessageBox::Ok); - msgBox.exec(); + + if (atLeastOneFileAccepted) { + event->acceptProposedAction(); } } @@ -3599,6 +3612,19 @@ void Application::registerScriptEngineWithApplicationServices(ScriptEngine* scri workerThread->start(); } +void Application::askToLoadScript(const QString& scriptFilenameOrURL) { + QMessageBox::StandardButton reply; + QString message = "Would you like to run this script:\n" + scriptFilenameOrURL; + reply = QMessageBox::question(getWindow(), "Run Script", message, QMessageBox::Yes|QMessageBox::No); + + if (reply == QMessageBox::Yes) { + qDebug() << "Chose to run the script: " << scriptFilenameOrURL; + loadScript(scriptFilenameOrURL); + } else { + qDebug() << "Declined to run the script: " << scriptFilenameOrURL; + } +} + ScriptEngine* Application::loadScript(const QString& scriptFilename, bool isUserLoaded, bool loadScriptFromEditor, bool activateMainWindow) { diff --git a/interface/src/Application.h b/interface/src/Application.h index b013692393..88295c8054 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -95,6 +95,7 @@ static const float NODE_KILLED_BLUE = 0.0f; static const QString SNAPSHOT_EXTENSION = ".jpg"; static const QString SVO_EXTENSION = ".svo"; +static const QString JS_EXTENSION = ".js"; static const float BILLBOARD_FIELD_OF_VIEW = 30.0f; // degrees static const float BILLBOARD_DISTANCE = 5.56f; // meters @@ -336,6 +337,7 @@ public slots: void loadDialog(); void loadScriptURLDialog(); void toggleLogDialog(); + void askToLoadScript(const QString& scriptFilenameOrURL); ScriptEngine* loadScript(const QString& scriptFilename = QString(), bool isUserLoaded = true, bool loadScriptFromEditor = false, bool activateMainWindow = false); void scriptFinished(const QString& scriptName); diff --git a/interface/src/GLCanvas.cpp b/interface/src/GLCanvas.cpp index 4587fca0f4..c615083335 100644 --- a/interface/src/GLCanvas.cpp +++ b/interface/src/GLCanvas.cpp @@ -168,10 +168,10 @@ void GLCanvas::wheelEvent(QWheelEvent* event) { } void GLCanvas::dragEnterEvent(QDragEnterEvent* event) { - const QMimeData *mimeData = event->mimeData(); + const QMimeData* mimeData = event->mimeData(); foreach (QUrl url, mimeData->urls()) { auto lower = url.path().toLower(); - if (lower.endsWith(SNAPSHOT_EXTENSION) || lower.endsWith(SVO_EXTENSION)) { + if (lower.endsWith(SNAPSHOT_EXTENSION) || lower.endsWith(SVO_EXTENSION) || lower.endsWith(JS_EXTENSION)) { event->acceptProposedAction(); break; } diff --git a/interface/src/ui/DataWebPage.cpp b/interface/src/ui/DataWebPage.cpp index bec2c98f55..c24e34fb64 100644 --- a/interface/src/ui/DataWebPage.cpp +++ b/interface/src/ui/DataWebPage.cpp @@ -37,6 +37,9 @@ bool DataWebPage::acceptNavigationRequest(QWebFrame* frame, const QNetworkReques if (request.url().path().toLower().endsWith(SVO_EXTENSION)) { Application::getInstance()->importSVOFromURL(request.url()); return false; + } else if (request.url().path().toLower().endsWith(JS_EXTENSION)) { + Application::getInstance()->askToLoadScript(request.url().toString()); + return false; } return true; } else {