From 8afdb27c1b39cacc03d3ce872364122542757ed5 Mon Sep 17 00:00:00 2001 From: vladest Date: Tue, 15 Aug 2017 22:55:44 +0200 Subject: [PATCH] Reworked WindowScriptingInterface save method for async methods --- interface/src/assets/ATPAssetMigrator.cpp | 61 ++++++++++--------- .../scripting/WindowScriptingInterface.cpp | 21 ++++--- .../src/scripting/WindowScriptingInterface.h | 4 +- scripts/system/edit.js | 19 +++--- scripts/system/libraries/entityList.js | 19 +++--- 5 files changed, 73 insertions(+), 51 deletions(-) diff --git a/interface/src/assets/ATPAssetMigrator.cpp b/interface/src/assets/ATPAssetMigrator.cpp index a86c012a55..4f79d32734 100644 --- a/interface/src/assets/ATPAssetMigrator.cpp +++ b/interface/src/assets/ATPAssetMigrator.cpp @@ -296,9 +296,6 @@ void ATPAssetMigrator::checkIfFinished() { // are we out of pending replacements? if so it is time to save the entity-server file if (_doneReading && _pendingReplacements.empty()) { saveEntityServerFile(); - - // reset after the attempted save, success or fail - reset(); } } @@ -336,39 +333,45 @@ bool ATPAssetMigrator::wantsToMigrateResource(const QUrl& url) { void ATPAssetMigrator::saveEntityServerFile() { // show a dialog to ask the user where they want to save the file - QString saveName = OffscreenUi::getSaveFileName(_dialogParent, "Save Migrated Entities File"); - - QFile saveFile { saveName }; - - if (saveFile.open(QIODevice::WriteOnly)) { - QJsonObject rootObject; - rootObject[ENTITIES_OBJECT_KEY] = _entitiesArray; - - QJsonDocument newDocument { rootObject }; - QByteArray jsonDataForFile; - - if (gzip(newDocument.toJson(), jsonDataForFile, -1)) { - - saveFile.write(jsonDataForFile); - saveFile.close(); + auto offscreenUi = DependencyManager::get(); + connect(offscreenUi.data(), &OffscreenUi::fileDialogResponse, this, [=] (QString saveName) { + QFile saveFile { saveName }; - QString infoMessage = QString("Your new entities file has been saved at\n%1.").arg(saveName); + if (saveFile.open(QIODevice::WriteOnly)) { + QJsonObject rootObject; + rootObject[ENTITIES_OBJECT_KEY] = _entitiesArray; - if (_errorCount > 0) { - infoMessage += QString("\nThere were %1 models that could not be migrated.\n").arg(_errorCount); - infoMessage += "Check the warnings in your log for details.\n"; - infoMessage += "You can re-attempt migration on those models\nby restarting this process with the newly saved file."; + QJsonDocument newDocument { rootObject }; + QByteArray jsonDataForFile; + + if (gzip(newDocument.toJson(), jsonDataForFile, -1)) { + + saveFile.write(jsonDataForFile); + saveFile.close(); + + QString infoMessage = QString("Your new entities file has been saved at\n%1.").arg(saveName); + + if (_errorCount > 0) { + infoMessage += QString("\nThere were %1 models that could not be migrated.\n").arg(_errorCount); + infoMessage += "Check the warnings in your log for details.\n"; + infoMessage += "You can re-attempt migration on those models\nby restarting this process with the newly saved file."; + } + + OffscreenUi::asyncInformation(_dialogParent, "Success", infoMessage); + } else { + OffscreenUi::asyncWarning(_dialogParent, "Error", "Could not gzip JSON data for new entities file."); } - OffscreenUi::asyncInformation(_dialogParent, "Success", infoMessage); } else { - OffscreenUi::asyncWarning(_dialogParent, "Error", "Could not gzip JSON data for new entities file."); + OffscreenUi::asyncWarning(_dialogParent, "Error", + QString("Could not open file at %1 to write new entities file to.").arg(saveName)); } + // reset after the attempted save, success or fail + reset(); + }); + + OffscreenUi::getSaveFileNameAsync(_dialogParent, "Save Migrated Entities File"); - } else { - OffscreenUi::asyncWarning(_dialogParent, "Error", - QString("Could not open file at %1 to write new entities file to.").arg(saveName)); - } } void ATPAssetMigrator::reset() { diff --git a/interface/src/scripting/WindowScriptingInterface.cpp b/interface/src/scripting/WindowScriptingInterface.cpp index ad33c5177c..dc8031b98b 100644 --- a/interface/src/scripting/WindowScriptingInterface.cpp +++ b/interface/src/scripting/WindowScriptingInterface.cpp @@ -225,8 +225,7 @@ QScriptValue WindowScriptingInterface::browse(const QString& title, const QStrin /// \param const QString& title title of the window /// \param const QString& directory directory to start the file browser at /// \param const QString& nameFilter filter to filter filenames by - see `QFileDialog` -/// \return QScriptValue file path as a string if one was selected, otherwise `QScriptValue::NullValue` -QScriptValue WindowScriptingInterface::save(const QString& title, const QString& directory, const QString& nameFilter) { +void WindowScriptingInterface::save(const QString& title, const QString& directory, const QString& nameFilter) { ensureReticleVisible(); QString path = directory; if (path.isEmpty()) { @@ -235,11 +234,19 @@ QScriptValue WindowScriptingInterface::save(const QString& title, const QString& #ifndef Q_OS_WIN path = fixupPathForMac(directory); #endif - QString result = OffscreenUi::getSaveFileName(nullptr, title, path, nameFilter); - if (!result.isEmpty()) { - setPreviousBrowseLocation(QFileInfo(result).absolutePath()); - } - return result.isEmpty() ? QScriptValue::NullValue : QScriptValue(result); + auto offscreenUi = DependencyManager::get(); + connect(offscreenUi.data(), &OffscreenUi::fileDialogResponse, + this, [=] (QString result) { + auto offscreenUi = DependencyManager::get(); + disconnect(offscreenUi.data(), &OffscreenUi::fileDialogResponse, + this, nullptr); + if (!result.isEmpty()) { + setPreviousBrowseLocation(QFileInfo(result).absolutePath()); + } + emit saveFileChanged(result); + }); + + OffscreenUi::getSaveFileNameAsync(nullptr, title, path, nameFilter); } /// Display a select asset dialog that lets the user select an asset from the Asset Server. If `directory` is an invalid diff --git a/interface/src/scripting/WindowScriptingInterface.h b/interface/src/scripting/WindowScriptingInterface.h index 1338743c20..6b7f480d67 100644 --- a/interface/src/scripting/WindowScriptingInterface.h +++ b/interface/src/scripting/WindowScriptingInterface.h @@ -55,7 +55,7 @@ public slots: CustomPromptResult customPrompt(const QVariant& config); void browseDir(const QString& title = "", const QString& directory = ""); QScriptValue browse(const QString& title = "", const QString& directory = "", const QString& nameFilter = ""); - QScriptValue save(const QString& title = "", const QString& directory = "", const QString& nameFilter = ""); + void save(const QString& title = "", const QString& directory = "", const QString& nameFilter = ""); void browseAssets(const QString& title = "", const QString& directory = "", const QString& nameFilter = ""); void showAssetServer(const QString& upload = ""); void copyToClipboard(const QString& text); @@ -89,6 +89,8 @@ signals: void messageBoxClosed(int id, int button); void browseDirChanged(QString browseDir); void assetsDirChanged(QString assetsDir); + void saveFileChanged(QString saveFile); + // triggered when window size or position changes void geometryChanged(QRect geometry); diff --git a/scripts/system/edit.js b/scripts/system/edit.js index c141c7cd52..74a74ddb7f 100644 --- a/scripts/system/edit.js +++ b/scripts/system/edit.js @@ -1458,6 +1458,16 @@ function toggleSelectedEntitiesVisible() { } } +function onFileSaveChanged(filename) { + Window.saveFileChanged.disconnect(onFileSaveChanged); + if (filename !== "") { + var success = Clipboard.exportEntities(filename, selectionManager.selections); + if (!success) { + Window.notifyEditError("Export failed."); + } + } +} + function handeMenuEvent(menuItem) { if (menuItem === "Allow Selecting of Small Models") { allowSmallModels = Menu.isOptionChecked("Allow Selecting of Small Models"); @@ -1475,13 +1485,8 @@ function handeMenuEvent(menuItem) { if (!selectionManager.hasSelection()) { Window.notifyEditError("No entities have been selected."); } else { - var filename = Window.save("Select Where to Save", "", "*.json"); - if (filename) { - var success = Clipboard.exportEntities(filename, selectionManager.selections); - if (!success) { - Window.notifyEditError("Export failed."); - } - } + Window.saveFileChanged.connect(onFileSaveChanged); + Window.save("Select Where to Save", "", "*.json"); } } else if (menuItem === "Import Entities" || menuItem === "Import Entities from URL") { var importURL = null; diff --git a/scripts/system/libraries/entityList.js b/scripts/system/libraries/entityList.js index 64a05fcebf..d2d815f9e3 100644 --- a/scripts/system/libraries/entityList.js +++ b/scripts/system/libraries/entityList.js @@ -108,6 +108,16 @@ EntityListTool = function(opts) { webView.emitScriptEvent(JSON.stringify(data)); }; + function onFileSaveChanged(filename) { + Window.saveFileChanged.disconnect(onFileSaveChanged); + if (filename !== "") { + var success = Clipboard.exportEntities(filename, selectionManager.selections); + if (!success) { + Window.notifyEditError("Export failed."); + } + } + } + webView.webEventReceived.connect(function(data) { try { data = JSON.parse(data); @@ -139,13 +149,8 @@ EntityListTool = function(opts) { if (!selectionManager.hasSelection()) { Window.notifyEditError("No entities have been selected."); } else { - var filename = Window.save("Select Where to Save", "", "*.json"); - if (filename) { - var success = Clipboard.exportEntities(filename, selectionManager.selections); - if (!success) { - Window.notifyEditError("Export failed."); - } - } + Window.saveFileChanged.connect(onFileSaveChanged); + Window.save("Select Where to Save", "", "*.json"); } } else if (data.type == "pal") { var sessionIds = {}; // Collect the sessionsIds of all selected entitities, w/o duplicates.