Reworked WindowScriptingInterface save method for async methods

This commit is contained in:
vladest 2017-08-15 22:55:44 +02:00
parent 51a5c30353
commit 8afdb27c1b
5 changed files with 73 additions and 51 deletions

View file

@ -296,9 +296,6 @@ void ATPAssetMigrator::checkIfFinished() {
// are we out of pending replacements? if so it is time to save the entity-server file // are we out of pending replacements? if so it is time to save the entity-server file
if (_doneReading && _pendingReplacements.empty()) { if (_doneReading && _pendingReplacements.empty()) {
saveEntityServerFile(); saveEntityServerFile();
// reset after the attempted save, success or fail
reset();
} }
} }
@ -336,8 +333,8 @@ bool ATPAssetMigrator::wantsToMigrateResource(const QUrl& url) {
void ATPAssetMigrator::saveEntityServerFile() { void ATPAssetMigrator::saveEntityServerFile() {
// show a dialog to ask the user where they want to save the file // show a dialog to ask the user where they want to save the file
QString saveName = OffscreenUi::getSaveFileName(_dialogParent, "Save Migrated Entities File"); auto offscreenUi = DependencyManager::get<OffscreenUi>();
connect(offscreenUi.data(), &OffscreenUi::fileDialogResponse, this, [=] (QString saveName) {
QFile saveFile { saveName }; QFile saveFile { saveName };
if (saveFile.open(QIODevice::WriteOnly)) { if (saveFile.open(QIODevice::WriteOnly)) {
@ -369,6 +366,12 @@ void ATPAssetMigrator::saveEntityServerFile() {
OffscreenUi::asyncWarning(_dialogParent, "Error", OffscreenUi::asyncWarning(_dialogParent, "Error",
QString("Could not open file at %1 to write new entities file to.").arg(saveName)); 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");
} }
void ATPAssetMigrator::reset() { void ATPAssetMigrator::reset() {

View file

@ -225,8 +225,7 @@ QScriptValue WindowScriptingInterface::browse(const QString& title, const QStrin
/// \param const QString& title title of the window /// \param const QString& title title of the window
/// \param const QString& directory directory to start the file browser at /// \param const QString& directory directory to start the file browser at
/// \param const QString& nameFilter filter to filter filenames by - see `QFileDialog` /// \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` void WindowScriptingInterface::save(const QString& title, const QString& directory, const QString& nameFilter) {
QScriptValue WindowScriptingInterface::save(const QString& title, const QString& directory, const QString& nameFilter) {
ensureReticleVisible(); ensureReticleVisible();
QString path = directory; QString path = directory;
if (path.isEmpty()) { if (path.isEmpty()) {
@ -235,11 +234,19 @@ QScriptValue WindowScriptingInterface::save(const QString& title, const QString&
#ifndef Q_OS_WIN #ifndef Q_OS_WIN
path = fixupPathForMac(directory); path = fixupPathForMac(directory);
#endif #endif
QString result = OffscreenUi::getSaveFileName(nullptr, title, path, nameFilter); auto offscreenUi = DependencyManager::get<OffscreenUi>();
connect(offscreenUi.data(), &OffscreenUi::fileDialogResponse,
this, [=] (QString result) {
auto offscreenUi = DependencyManager::get<OffscreenUi>();
disconnect(offscreenUi.data(), &OffscreenUi::fileDialogResponse,
this, nullptr);
if (!result.isEmpty()) { if (!result.isEmpty()) {
setPreviousBrowseLocation(QFileInfo(result).absolutePath()); setPreviousBrowseLocation(QFileInfo(result).absolutePath());
} }
return result.isEmpty() ? QScriptValue::NullValue : QScriptValue(result); 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 /// Display a select asset dialog that lets the user select an asset from the Asset Server. If `directory` is an invalid

View file

@ -55,7 +55,7 @@ public slots:
CustomPromptResult customPrompt(const QVariant& config); CustomPromptResult customPrompt(const QVariant& config);
void browseDir(const QString& title = "", const QString& directory = ""); void browseDir(const QString& title = "", const QString& directory = "");
QScriptValue browse(const QString& title = "", const QString& directory = "", const QString& nameFilter = ""); 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 browseAssets(const QString& title = "", const QString& directory = "", const QString& nameFilter = "");
void showAssetServer(const QString& upload = ""); void showAssetServer(const QString& upload = "");
void copyToClipboard(const QString& text); void copyToClipboard(const QString& text);
@ -89,6 +89,8 @@ signals:
void messageBoxClosed(int id, int button); void messageBoxClosed(int id, int button);
void browseDirChanged(QString browseDir); void browseDirChanged(QString browseDir);
void assetsDirChanged(QString assetsDir); void assetsDirChanged(QString assetsDir);
void saveFileChanged(QString saveFile);
// triggered when window size or position changes // triggered when window size or position changes
void geometryChanged(QRect geometry); void geometryChanged(QRect geometry);

View file

@ -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) { function handeMenuEvent(menuItem) {
if (menuItem === "Allow Selecting of Small Models") { if (menuItem === "Allow Selecting of Small Models") {
allowSmallModels = Menu.isOptionChecked("Allow Selecting of Small Models"); allowSmallModels = Menu.isOptionChecked("Allow Selecting of Small Models");
@ -1475,13 +1485,8 @@ function handeMenuEvent(menuItem) {
if (!selectionManager.hasSelection()) { if (!selectionManager.hasSelection()) {
Window.notifyEditError("No entities have been selected."); Window.notifyEditError("No entities have been selected.");
} else { } else {
var filename = Window.save("Select Where to Save", "", "*.json"); Window.saveFileChanged.connect(onFileSaveChanged);
if (filename) { Window.save("Select Where to Save", "", "*.json");
var success = Clipboard.exportEntities(filename, selectionManager.selections);
if (!success) {
Window.notifyEditError("Export failed.");
}
}
} }
} else if (menuItem === "Import Entities" || menuItem === "Import Entities from URL") { } else if (menuItem === "Import Entities" || menuItem === "Import Entities from URL") {
var importURL = null; var importURL = null;

View file

@ -108,6 +108,16 @@ EntityListTool = function(opts) {
webView.emitScriptEvent(JSON.stringify(data)); 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) { webView.webEventReceived.connect(function(data) {
try { try {
data = JSON.parse(data); data = JSON.parse(data);
@ -139,13 +149,8 @@ EntityListTool = function(opts) {
if (!selectionManager.hasSelection()) { if (!selectionManager.hasSelection()) {
Window.notifyEditError("No entities have been selected."); Window.notifyEditError("No entities have been selected.");
} else { } else {
var filename = Window.save("Select Where to Save", "", "*.json"); Window.saveFileChanged.connect(onFileSaveChanged);
if (filename) { Window.save("Select Where to Save", "", "*.json");
var success = Clipboard.exportEntities(filename, selectionManager.selections);
if (!success) {
Window.notifyEditError("Export failed.");
}
}
} }
} else if (data.type == "pal") { } else if (data.type == "pal") {
var sessionIds = {}; // Collect the sessionsIds of all selected entitities, w/o duplicates. var sessionIds = {}; // Collect the sessionsIds of all selected entitities, w/o duplicates.