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
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<OffscreenUi>();
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() {

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& 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<OffscreenUi>();
connect(offscreenUi.data(), &OffscreenUi::fileDialogResponse,
this, [=] (QString result) {
auto offscreenUi = DependencyManager::get<OffscreenUi>();
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

View file

@ -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);

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) {
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;

View file

@ -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.