From 162c36dbdff85384184d4e188167f991c3d4f653 Mon Sep 17 00:00:00 2001 From: Tobias Hunger <tobias.hunger@theqtcompany.com> Date: Mon, 4 May 2015 13:44:08 +0200 Subject: [PATCH] JsonWizard: Move code for opening files into JsonWizard Move code opening files and projects directly into the JsonWizard. It makes no sense to keep reimplementing this functionality in generators. Change-Id: Ib4686a262fa9b2c78028146d138c5bba5d5b604a Reviewed-by: Orgad Shaneh <orgads@gmail.com> --- .../projectexplorer/jsonwizard/jsonwizard.cpp | 61 +++++++++++++++++++ .../projectexplorer/jsonwizard/jsonwizard.h | 2 + .../jsonwizard/jsonwizardfilegenerator.cpp | 21 +------ 3 files changed, 65 insertions(+), 19 deletions(-) diff --git a/src/plugins/projectexplorer/jsonwizard/jsonwizard.cpp b/src/plugins/projectexplorer/jsonwizard/jsonwizard.cpp index b718058de91..7d2f6707011 100644 --- a/src/plugins/projectexplorer/jsonwizard/jsonwizard.cpp +++ b/src/plugins/projectexplorer/jsonwizard/jsonwizard.cpp @@ -32,6 +32,10 @@ #include "jsonwizardgeneratorfactory.h" +#include "../project.h" +#include "../projectexplorer.h" + +#include <coreplugin/editormanager/editormanager.h> #include <coreplugin/messagemanager.h> #include <utils/algorithm.h> @@ -223,6 +227,8 @@ void JsonWizard::accept() return; } emit allDone(m_files); + + openFiles(m_files); } void JsonWizard::handleNewPages(int pageId) @@ -239,4 +245,59 @@ void JsonWizard::handleError(const QString &message) Core::MessageManager::write(message, Core::MessageManager::ModeSwitch); } +void JsonWizard::openFiles(const JsonWizard::GeneratorFiles &files) +{ + QString errorMessage; + bool openedSomething = false; + foreach (const JsonWizard::GeneratorFile &f, files) { + const Core::GeneratedFile &file = f.file; + if (!QFileInfo(file.path()).exists()) { + errorMessage = QCoreApplication::translate("ProjectExplorer::JsonWizard", + "\"%1\" does not exist in the file system.") + .arg(QDir::toNativeSeparators(file.path())); + break; + } + if (file.attributes() & Core::GeneratedFile::OpenProjectAttribute) { + Project *project = ProjectExplorerPlugin::instance()->openProject(file.path(), &errorMessage); + if (!project) { + if (errorMessage.isEmpty()) { + errorMessage = QCoreApplication::translate("ProjectExplorer::JsonWizard", + "Failed to open \"%1\" as a project.") + .arg(QDir::toNativeSeparators(file.path())); + } + break; + } + openedSomething = true; + } + if (file.attributes() & Core::GeneratedFile::OpenEditorAttribute) { + if (!Core::EditorManager::openEditor(file.path(), file.editorId())) { + errorMessage = QCoreApplication::translate("ProjectExplorer::JsonWizard", + "Failed to open an editor for \"%1\".") + .arg(QDir::toNativeSeparators(file.path())); + break; + } + openedSomething = true; + } + } + + const QString path + = QDir::toNativeSeparators(m_expander.expand(value(QLatin1String("TargetPath")).toString())); + + // Now try to find the project file and open + if (!openedSomething) { + errorMessage = QCoreApplication::translate("ProjectExplorer::JsonWizard", + "No file to open found in \"%1\".") + .arg(path); + } + + if (!errorMessage.isEmpty()) { + const QString text = path.isEmpty() ? tr("Failed to open project.") + : tr("Failed to open project in \"%1\".").arg(path); + QMessageBox msgBox(QMessageBox::Warning, tr("Cannot Open Project"), text); + msgBox.setDetailedText(errorMessage); + msgBox.addButton(QMessageBox::Ok); + msgBox.exec(); + } +} + } // namespace ProjectExplorer diff --git a/src/plugins/projectexplorer/jsonwizard/jsonwizard.h b/src/plugins/projectexplorer/jsonwizard/jsonwizard.h index 6311facca8a..d72bc934aff 100644 --- a/src/plugins/projectexplorer/jsonwizard/jsonwizard.h +++ b/src/plugins/projectexplorer/jsonwizard/jsonwizard.h @@ -101,6 +101,8 @@ private slots: void handleError(const QString &message); private: + void openFiles(const GeneratorFiles &files); + QList<JsonWizardGenerator *> m_generators; GeneratorFiles m_files; diff --git a/src/plugins/projectexplorer/jsonwizard/jsonwizardfilegenerator.cpp b/src/plugins/projectexplorer/jsonwizard/jsonwizardfilegenerator.cpp index 14c5e46d7d5..bf56c3f3546 100644 --- a/src/plugins/projectexplorer/jsonwizard/jsonwizardfilegenerator.cpp +++ b/src/plugins/projectexplorer/jsonwizard/jsonwizardfilegenerator.cpp @@ -210,25 +210,8 @@ bool JsonWizardFileGenerator::polish(const JsonWizard *wizard, Core::GeneratedFi bool JsonWizardFileGenerator::allDone(const JsonWizard *wizard, Core::GeneratedFile *file, QString *errorMessage) { Q_UNUSED(wizard); - if (file->attributes() & Core::GeneratedFile::OpenProjectAttribute) { - Project *project = ProjectExplorerPlugin::instance()->openProject(file->path(), errorMessage); - if (!project) { - *errorMessage = QCoreApplication::translate("ProjectExplorer::JsonWizard", - "Failed to open \"%1\" as a project.") - .arg(QDir::toNativeSeparators(file->path())); - - return false; - } - } - if (file->attributes() & Core::GeneratedFile::OpenEditorAttribute) { - if (!Core::EditorManager::openEditor(file->path(), file->editorId())) { - if (errorMessage) - *errorMessage = QCoreApplication::translate("ProjectExplorer::JsonWizard", - "Failed to open an editor for \"%1\".") - .arg(QDir::toNativeSeparators(file->path())); - return false; - } - } + Q_UNUSED(file); + Q_UNUSED(errorMessage); return true; } -- GitLab