Commit 093a54cb authored by Tobias Hunger's avatar Tobias Hunger

Make BaseFileWizard async

The side-effect is that WizardEventLoop can go!

Change-Id: I0eae0e0fa91a48e2a5010b47cc0de86758969904
Reviewed-by: default avatarEike Ziller <eike.ziller@theqtcompany.com>
parent 9d0c3976
......@@ -30,6 +30,136 @@
#include "basefilewizard.h"
Core::BaseFileWizard::BaseFileWizard(QWidget *parent) :
Utils::Wizard(parent)
{ }
#include "basefilewizardfactory.h"
#include "ifilewizardextension.h"
#include <extensionsystem/pluginmanager.h>
#include <QMessageBox>
using namespace Utils;
namespace Core {
BaseFileWizard::BaseFileWizard(const BaseFileWizardFactory *factory,
const QVariantMap &extraValues,
QWidget *parent) :
Wizard(parent),
m_extraValues(extraValues),
m_factory(factory)
{
// Compile extension pages, purge out unused ones
QList<IFileWizardExtension *> extensionList
= ExtensionSystem::PluginManager::getObjects<IFileWizardExtension>();
for (auto it = extensionList.begin(); it != extensionList.end(); ) {
const QList<QWizardPage *> extensionPages = (*it)->extensionPages(factory);
if (extensionPages.empty()) {
it = extensionList.erase(it);
} else {
m_extensionPages += extensionPages;
++it;
}
}
if (!m_extensionPages.empty())
m_firstExtensionPage = m_extensionPages.front();
}
void BaseFileWizard::initializePage(int id)
{
if (page(id) == m_firstExtensionPage) {
generateFileList();
QList<IFileWizardExtension *> extensionList
= ExtensionSystem::PluginManager::getObjects<IFileWizardExtension>();
foreach (IFileWizardExtension *ex, extensionList)
ex->firstExtensionPageShown(m_files, m_extraValues);
}
}
QList<QWizardPage *> BaseFileWizard::extensionPages()
{
return m_extensionPages;
}
void BaseFileWizard::accept()
{
if (m_files.isEmpty())
generateFileList();
QString errorMessage;
// Compile result list and prompt for overwrite
switch (m_factory->promptOverwrite(&m_files, &errorMessage)) {
case BaseFileWizardFactory::OverwriteCanceled:
reject();
return;
case BaseFileWizardFactory::OverwriteError:
QMessageBox::critical(0, tr("Existing files"), errorMessage);
reject();
return;
case BaseFileWizardFactory::OverwriteOk:
break;
}
QList<IFileWizardExtension *> extensionList
= ExtensionSystem::PluginManager::getObjects<IFileWizardExtension>();
foreach (IFileWizardExtension *ex, extensionList) {
for (int i = 0; i < m_files.count(); i++) {
ex->applyCodeStyle(&m_files[i]);
}
}
// Write
if (!m_factory->writeFiles(m_files, &errorMessage)) {
QMessageBox::critical(parentWidget(), tr("File Generation Failure"), errorMessage);
reject();
return;
}
bool removeOpenProjectAttribute = false;
// Run the extensions
foreach (IFileWizardExtension *ex, extensionList) {
bool remove;
if (!ex->processFiles(m_files, &remove, &errorMessage)) {
if (!errorMessage.isEmpty())
QMessageBox::critical(parentWidget(), tr("File Generation Failure"), errorMessage);
reject();
return;
}
removeOpenProjectAttribute |= remove;
}
if (removeOpenProjectAttribute) {
for (int i = 0; i < m_files.count(); i++) {
if (m_files[i].attributes() & GeneratedFile::OpenProjectAttribute)
m_files[i].setAttributes(GeneratedFile::OpenEditorAttribute);
}
}
// Post generation handler
if (!m_factory->postGenerateFiles(this, m_files, &errorMessage))
if (!errorMessage.isEmpty())
QMessageBox::critical(0, tr("File Generation Failure"), errorMessage);
Wizard::accept();
}
void BaseFileWizard::reject()
{
m_files.clear();
Wizard::reject();
}
void BaseFileWizard::generateFileList()
{
QString errorMessage;
m_files = m_factory->generateFiles(this, &errorMessage);
if (m_files.empty()) {
QMessageBox::critical(parentWidget(), tr("File Generation Failure"), errorMessage);
reject();
}
}
} // namespace Core
......@@ -33,16 +33,39 @@
#include "core_global.h"
#include "generatedfile.h"
#include <utils/wizard.h>
#include <QVariantMap>
namespace Core {
class BaseFileWizardFactory;
class CORE_EXPORT BaseFileWizard : public Utils::Wizard
{
Q_OBJECT
public:
explicit BaseFileWizard(QWidget *parent = 0);
explicit BaseFileWizard(const BaseFileWizardFactory *factory, const QVariantMap &extraValues,
QWidget *parent = 0);
void initializePage(int id);
QList<QWizardPage *> extensionPages();
void accept();
void reject();
private:
void generateFileList();
QVariantMap m_extraValues;
const BaseFileWizardFactory *m_factory;
QList<QWizardPage *> m_extensionPages;
QWizardPage *m_firstExtensionPage = 0;
GeneratedFiles m_files;
};
} // namespace Core
......
......@@ -46,12 +46,6 @@
#include <QDir>
#include <QFileInfo>
#include <QDebug>
#include <QSharedData>
#include <QEventLoop>
#include <QScopedPointer>
#include <QMessageBox>
#include <QWizard>
#include <QIcon>
enum { debugWizard = 0 };
......@@ -67,101 +61,6 @@ static int indexOfFile(const GeneratedFiles &f, const QString &path)
return -1;
}
/*!
\class Core::Internal::WizardEventLoop
\brief The WizardEventLoop class implements a special event
loop that runs a QWizard and terminates if the page changes.
Used by Core::BaseFileWizard to intercept the change from the standard wizard pages
to the extension pages (as the latter require the list of Core::GeneratedFile generated).
Synopsis:
\code
Wizard wizard(parent);
WizardEventLoop::WizardResult wr;
do {
wr = WizardEventLoop::execWizardPage(wizard);
} while (wr == WizardEventLoop::PageChanged);
\endcode
\sa Core::GeneratedFile, Core::BaseFileWizardParameters, Core::BaseFileWizard, Core::StandardFileWizard
*/
class WizardEventLoop : public QEventLoop
{
Q_OBJECT
WizardEventLoop(QObject *parent);
public:
enum WizardResult { Accepted, Rejected , PageChanged };
static WizardResult execWizardPage(QWizard &w);
private slots:
void pageChanged(int);
void accepted();
void rejected();
private:
WizardResult execWizardPageI();
WizardResult m_result;
};
WizardEventLoop::WizardEventLoop(QObject *parent) :
QEventLoop(parent),
m_result(Rejected)
{
}
WizardEventLoop::WizardResult WizardEventLoop::execWizardPage(QWizard &wizard)
{
/* Install ourselves on the wizard. Main trick is here to connect
* to the page changed signal and quit() on it. */
WizardEventLoop *eventLoop = wizard.findChild<WizardEventLoop *>();
if (!eventLoop) {
eventLoop = new WizardEventLoop(&wizard);
connect(&wizard, SIGNAL(currentIdChanged(int)), eventLoop, SLOT(pageChanged(int)));
connect(&wizard, SIGNAL(accepted()), eventLoop, SLOT(accepted()));
connect(&wizard, SIGNAL(rejected()), eventLoop, SLOT(rejected()));
wizard.setWindowFlags(wizard.windowFlags());
wizard.show();
}
const WizardResult result = eventLoop->execWizardPageI();
// Quitting?
if (result != PageChanged)
delete eventLoop;
if (debugWizard)
qDebug() << "WizardEventLoop::runWizard" << wizard.pageIds() << " returns " << result;
return result;
}
WizardEventLoop::WizardResult WizardEventLoop::execWizardPageI()
{
m_result = Rejected;
exec();
return m_result;
}
void WizardEventLoop::pageChanged(int /*page*/)
{
m_result = PageChanged;
quit(); // !
}
void WizardEventLoop::accepted()
{
m_result = Accepted;
quit();
}
void WizardEventLoop::rejected()
{
m_result = Rejected;
quit();
}
/*!
\class Core::BaseFileWizard
\brief The BaseFileWizard class implements a generic wizard for
......@@ -186,27 +85,6 @@ Utils::Wizard *BaseFileWizardFactory::runWizardImpl(const QString &path, QWidget
{
QTC_ASSERT(!path.isEmpty(), return 0);
QString errorMessage;
// Compile extension pages, purge out unused ones
QList<IFileWizardExtension *> extensionList = ExtensionSystem::PluginManager::getObjects<IFileWizardExtension>();
WizardPageList allExtensionPages;
for (auto it = extensionList.begin(); it != extensionList.end(); ) {
const WizardPageList extensionPages = (*it)->extensionPages(this);
if (extensionPages.empty()) {
it = extensionList.erase(it);
} else {
allExtensionPages += extensionPages;
++it;
}
}
if (debugWizard)
qDebug() << Q_FUNC_INFO << path << parent << "exs" << extensionList.size() << allExtensionPages.size();
QWizardPage *firstExtensionPage = 0;
if (!allExtensionPages.empty())
firstExtensionPage = allExtensionPages.front();
// Create dialog and run it. Ensure that the dialog is deleted when
// leaving the func, but not before the IFileWizardExtension::process
// has been called
......@@ -216,92 +94,12 @@ Utils::Wizard *BaseFileWizardFactory::runWizardImpl(const QString &path, QWidget
if (flags().testFlag(ForceCapitalLetterForFileName))
dialogParameterFlags |= WizardDialogParameters::ForceCapitalLetterForFileName;
const QScopedPointer<QWizard> wizard(create(parent, WizardDialogParameters(path,
allExtensionPages,
platform,
requiredFeatures(),
dialogParameterFlags,
extraValues)));
QTC_ASSERT(!wizard.isNull(), return 0);
ICore::registerWindow(wizard.data(), Context("Core.NewWizard"));
GeneratedFiles files;
// Run the wizard: Call generate files on switching to the first extension
// page is OR after 'Accepted' if there are no extension pages
while (true) {
const WizardEventLoop::WizardResult wr = WizardEventLoop::execWizardPage(*wizard);
if (wr == WizardEventLoop::Rejected) {
files.clear();
break;
}
const bool accepted = wr == WizardEventLoop::Accepted;
const bool firstExtensionPageHit = wr == WizardEventLoop::PageChanged
&& wizard->page(wizard->currentId()) == firstExtensionPage;
const bool needGenerateFiles = firstExtensionPageHit || (accepted && allExtensionPages.empty());
if (needGenerateFiles) {
QString errorMessage;
files = generateFiles(wizard.data(), &errorMessage);
if (files.empty()) {
QMessageBox::critical(0, tr("File Generation Failure"), errorMessage);
break;
}
}
if (firstExtensionPageHit)
foreach (IFileWizardExtension *ex, extensionList)
ex->firstExtensionPageShown(files, extraValues);
if (accepted)
break;
}
if (files.empty())
return 0;
// Compile result list and prompt for overwrite
switch (promptOverwrite(&files, &errorMessage)) {
case OverwriteCanceled:
return 0;
case OverwriteError:
QMessageBox::critical(0, tr("Existing files"), errorMessage);
return 0;
case OverwriteOk:
break;
}
foreach (IFileWizardExtension *ex, extensionList) {
for (int i = 0; i < files.count(); i++) {
ex->applyCodeStyle(&files[i]);
}
}
// Write
if (!writeFiles(files, &errorMessage)) {
QMessageBox::critical(parent, tr("File Generation Failure"), errorMessage);
return 0;
}
bool removeOpenProjectAttribute = false;
// Run the extensions
foreach (IFileWizardExtension *ex, extensionList) {
bool remove;
if (!ex->processFiles(files, &remove, &errorMessage)) {
if (!errorMessage.isEmpty())
QMessageBox::critical(parent, tr("File Generation Failure"), errorMessage);
return 0;
}
removeOpenProjectAttribute |= remove;
}
if (removeOpenProjectAttribute) {
for (int i = 0; i < files.count(); i++) {
if (files[i].attributes() & GeneratedFile::OpenProjectAttribute)
files[i].setAttributes(GeneratedFile::OpenEditorAttribute);
}
}
// Post generation handler
if (!postGenerateFiles(wizard.data(), files, &errorMessage))
if (!errorMessage.isEmpty())
QMessageBox::critical(0, tr("File Generation Failure"), errorMessage);
return 0;
Utils::Wizard *wizard = create(parent, WizardDialogParameters(path, platform,
requiredFeatures(),
dialogParameterFlags,
extraValues));
QTC_CHECK(wizard);
return wizard;
}
/*!
......@@ -326,7 +124,7 @@ Utils::Wizard *BaseFileWizardFactory::runWizardImpl(const QString &path, QWidget
Re-implement (calling the base implementation) to create files with CustomGeneratorAttribute set.
*/
bool BaseFileWizardFactory::writeFiles(const GeneratedFiles &files, QString *errorMessage)
bool BaseFileWizardFactory::writeFiles(const GeneratedFiles &files, QString *errorMessage) const
{
const GeneratedFile::Attributes noWriteAttributes
= GeneratedFile::CustomGeneratorAttribute|GeneratedFile::KeepExistingFileAttribute;
......@@ -343,7 +141,8 @@ bool BaseFileWizardFactory::writeFiles(const GeneratedFiles &files, QString *err
The default implementation opens editors with the newly generated files.
*/
bool BaseFileWizardFactory::postGenerateFiles(const QWizard *, const GeneratedFiles &l, QString *errorMessage)
bool BaseFileWizardFactory::postGenerateFiles(const QWizard *, const GeneratedFiles &l,
QString *errorMessage) const
{
return BaseFileWizardFactory::postGenerateOpenEditors(l, errorMessage);
}
......@@ -500,9 +299,6 @@ QString BaseFileWizardFactory::preferredSuffix(const QString &mimeType)
files from path and name.
\sa Core::GeneratedFile, Core::BaseFileWizardParameters, Core::BaseFileWizard
\sa Core::Internal::WizardEventLoop
*/
} // namespace Core
#include "basefilewizardfactory.moc"
......@@ -62,12 +62,10 @@ public:
};
Q_DECLARE_FLAGS(DialogParameterFlags, DialogParameterEnum)
explicit WizardDialogParameters(const QString &defaultPath, const WizardPageList &extensionPages,
const QString &platform, const FeatureSet &requiredFeatures,
DialogParameterFlags flags,
explicit WizardDialogParameters(const QString &defaultPath, const QString &platform,
const FeatureSet &requiredFeatures, DialogParameterFlags flags,
QVariantMap extraValues)
: m_defaultPath(defaultPath),
m_extensionPages(extensionPages),
m_selectedPlatform(platform),
m_requiredFeatures(requiredFeatures),
m_parameterFlags(flags),
......@@ -77,9 +75,6 @@ public:
QString defaultPath() const
{ return m_defaultPath; }
WizardPageList extensionPages() const
{ return m_extensionPages; }
QString selectedPlatform() const
{ return m_selectedPlatform; }
......@@ -94,7 +89,6 @@ public:
private:
QString m_defaultPath;
WizardPageList m_extensionPages;
QString m_selectedPlatform;
FeatureSet m_requiredFeatures;
DialogParameterFlags m_parameterFlags;
......@@ -105,20 +99,20 @@ class CORE_EXPORT BaseFileWizardFactory : public IWizardFactory
{
Q_OBJECT
friend class BaseFileWizard;
public:
static QString buildFileName(const QString &path, const QString &baseName, const QString &extension);
protected:
typedef QList<QWizardPage *> WizardPageList;
virtual BaseFileWizard *create(QWidget *parent, const WizardDialogParameters &parameters) const = 0;
virtual GeneratedFiles generateFiles(const QWizard *w,
QString *errorMessage) const = 0;
virtual bool writeFiles(const GeneratedFiles &files, QString *errorMessage);
virtual bool writeFiles(const GeneratedFiles &files, QString *errorMessage) const;
virtual bool postGenerateFiles(const QWizard *w, const GeneratedFiles &l, QString *errorMessage);
virtual bool postGenerateFiles(const QWizard *w, const GeneratedFiles &l, QString *errorMessage) const;
static QString preferredSuffix(const QString &mimeType);
......
......@@ -257,6 +257,7 @@ Utils::Wizard *IWizardFactory::runWizard(const QString &path, QWidget *parent, c
ICore::validateNewDialogIsRunning();
wizard->deleteLater();
});
wizard->show();
Core::ICore::registerWindow(wizard, Core::Context("Core.NewWizard"));
} else {
s_isWizardRunning = false;
......
......@@ -63,8 +63,7 @@ QString FormClassWizard::formSuffix() const
Core::BaseFileWizard *FormClassWizard::create(QWidget *parent, const Core::WizardDialogParameters &parameters) const
{
FormClassWizardDialog *wizardDialog = new FormClassWizardDialog(parameters.extensionPages(),
parent);
FormClassWizardDialog *wizardDialog = new FormClassWizardDialog(this, parent);
wizardDialog->setPath(parameters.defaultPath());
return wizardDialog;
}
......
......@@ -40,9 +40,9 @@ namespace Designer {
namespace Internal {
// ----------------- FormClassWizardDialog
FormClassWizardDialog::FormClassWizardDialog(const WizardPageList &extensionPages,
FormClassWizardDialog::FormClassWizardDialog(const Core::BaseFileWizardFactory *factory,
QWidget *parent) :
Core::BaseFileWizard(parent),
Core::BaseFileWizard(factory, QVariantMap(), parent),
m_formPage(new FormTemplateWizardPage),
m_classPage(new FormClassWizardPage)
{
......@@ -51,7 +51,7 @@ FormClassWizardDialog::FormClassWizardDialog(const WizardPageList &extensionPage
setPage(FormPageId, m_formPage);
setPage(ClassPageId, m_classPage);
foreach (QWizardPage *p, extensionPages)
foreach (QWizardPage *p, extensionPages())
addPage(p);
}
......
......@@ -49,8 +49,7 @@ class FormClassWizardDialog : public Core::BaseFileWizard
public:
typedef QList<QWizardPage *> WizardPageList;
explicit FormClassWizardDialog(const WizardPageList &extensionPages,
QWidget *parent = 0);
explicit FormClassWizardDialog(const Core::BaseFileWizardFactory *factory, QWidget *parent = 0);
QString path() const;
......
......@@ -60,8 +60,9 @@ static const char *const ConfigFileTemplate =
//
//////////////////////////////////////////////////////////////////////////////
GenericProjectWizardDialog::GenericProjectWizardDialog(QWidget *parent) :
Core::BaseFileWizard(parent)
GenericProjectWizardDialog::GenericProjectWizardDialog(const Core::BaseFileWizardFactory *factory,
QWidget *parent) :
Core::BaseFileWizard(factory, QVariantMap(), parent)
{
setWindowTitle(tr("Import Existing Project"));
......@@ -129,13 +130,14 @@ GenericProjectWizard::GenericProjectWizard()
setFlags(Core::IWizardFactory::PlatformIndependent);
}
Core::BaseFileWizard *GenericProjectWizard::create(QWidget *parent, const Core::WizardDialogParameters &parameters) const
Core::BaseFileWizard *GenericProjectWizard::create(QWidget *parent,
const Core::WizardDialogParameters &parameters) const
{
GenericProjectWizardDialog *wizard = new GenericProjectWizardDialog(parent);
GenericProjectWizardDialog *wizard = new GenericProjectWizardDialog(this, parent);
wizard->setPath(parameters.defaultPath());
foreach (QWizardPage *p, parameters.extensionPages())
foreach (QWizardPage *p, wizard->extensionPages())
wizard->addPage(p);
return wizard;
......@@ -200,7 +202,8 @@ Core::GeneratedFiles GenericProjectWizard::generateFiles(const QWizard *w,
return files;
}
bool GenericProjectWizard::postGenerateFiles(const QWizard *w, const Core::GeneratedFiles &l, QString *errorMessage)
bool GenericProjectWizard::postGenerateFiles(const QWizard *w, const Core::GeneratedFiles &l,
QString *errorMessage) const
{
Q_UNUSED(w);
return ProjectExplorer::CustomProjectWizard::postGenerateOpen(l, errorMessage);
......
......@@ -47,7 +47,7 @@ class GenericProjectWizardDialog : public Core::BaseFileWizard
Q_OBJECT
public:
explicit GenericProjectWizardDialog(QWidget *parent = 0);
explicit GenericProjectWizardDialog(const Core::BaseFileWizardFactory *factory, QWidget *parent = 0);
QString path() const;
void setPath(const QString &path);
......@@ -69,8 +69,9 @@ public:
protected:
Core::BaseFileWizard *create(QWidget *parent, const Core::WizardDialogParameters &parameters) const;
Core::GeneratedFiles generateFiles(const QWizard *w, QString *errorMessage) const;
bool postGenerateFiles(const QWizard *w, const Core::GeneratedFiles &l, QString *errorMessage);
Core::GeneratedFiles generateFiles(const QWizard *w, QString *errorMessage) const override;
bool postGenerateFiles(const QWizard *w, const Core::GeneratedFiles &l,