Commit f51fcaca authored by Tobias Hunger's avatar Tobias Hunger

JsonWizard: Add File- and Summary pages

These reuse the existing pages we use for other wizards.

Change-Id: Ic505204e0988a6b44d112da35748f81d9f8e2c1f
Reviewed-by: default avatarDaniel Teske <daniel.teske@digia.com>
parent 9ebb63c7
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#include "jsonfilepage.h"
#include <QFileInfo>
#include <QVariant>
namespace ProjectExplorer {
JsonFilePage::JsonFilePage(QWidget *parent) :
Utils::FileWizardPage(parent)
{ }
bool JsonFilePage::validatePage()
{
if (path().isEmpty() || fileName().isEmpty())
return false;
QFileInfo d(path());
if (!d.isDir())
return false;
QString target = d.absoluteFilePath();
if (!target.endsWith(QLatin1Char('/')))
target += QLatin1Char('/');
target += fileName();
wizard()->setProperty("TargetPath", target);
return true;
}
} // namespace ProjectExplorer
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#ifndef JSONFILEPAGE_H
#define JSONFILEPAGE_H
#include <utils/filewizardpage.h>
namespace ProjectExplorer {
// Documentation inside.
class JsonFilePage : public Utils::FileWizardPage
{
Q_OBJECT
public:
JsonFilePage(QWidget *parent = 0);
bool validatePage();
};
} // namespace ProjectExplorer
#endif // JSONFILEPAGE_H
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#include "jsonsummarypage.h"
#include "jsonwizard.h"
#include "../addnewmodel.h"
#include "../projectexplorerconstants.h"
#include "../projectnodes.h"
#include <coreplugin/coreconstants.h>
#include <utils/algorithm.h>
#include <utils/qtcassert.h>
#include <QDir>
#include <QMessageBox>
using namespace Core;
namespace ProjectExplorer {
// --------------------------------------------------------------------
// Helper:
// --------------------------------------------------------------------
static QString generatedProjectFilePath(const QList<JsonWizard::GeneratorFile> &files)
{
foreach (const JsonWizard::GeneratorFile &file, files)
if (file.file.attributes() & GeneratedFile::OpenProjectAttribute)
return file.file.path();
return QString();
}
static IWizardFactory::WizardKind wizardKind(JsonWizard *wiz)
{
IWizardFactory::WizardKind kind = IWizardFactory::ProjectWizard;
const QString kindStr = wiz->value(QLatin1String("kind")).toString();
if (kindStr == QLatin1String(Core::Constants::WIZARD_KIND_PROJECT))
kind = IWizardFactory::ProjectWizard;
else if (kindStr == QLatin1String(Core::Constants::WIZARD_KIND_CLASS))
kind = IWizardFactory::ClassWizard;
else if (kindStr == QLatin1String(Core::Constants::WIZARD_KIND_FILE))
kind = IWizardFactory::FileWizard;
else
QTC_CHECK(false);
return kind;
}
// --------------------------------------------------------------------
// JsonSummaryPage:
// --------------------------------------------------------------------
JsonSummaryPage::JsonSummaryPage(QWidget *parent) :
Internal::ProjectWizardPage(parent)
{ }
void JsonSummaryPage::initializePage()
{
JsonWizard *wiz = qobject_cast<JsonWizard *>(wizard());
QTC_ASSERT(wiz, return);
connect(wiz, &JsonWizard::filesReady, this, &JsonSummaryPage::triggerCommit);
connect(wiz, &JsonWizard::filesReady, this, &JsonSummaryPage::addToProject);
JsonWizard::GeneratorFiles files = wiz->fileList();
QStringList filePaths = Utils::transform(files, [](const JsonWizard::GeneratorFile &f)
{ return f.file.path(); });
IWizardFactory::WizardKind kind = wizardKind(wiz);
bool isProject = (kind == IWizardFactory::ProjectWizard);
setFiles(filePaths);
if (isProject) {
filePaths.clear();
JsonWizard::GeneratorFile f
= Utils::findOrDefault(files, [](const JsonWizard::GeneratorFile &f) {
return f.file.attributes() & GeneratedFile::OpenProjectAttribute;
});
filePaths << f.file.path();
}
Node *contextNode = wiz->value(QLatin1String(ProjectExplorer::Constants::PREFERRED_PROJECT_NODE))
.value<Node *>();
initializeProjectTree(contextNode, filePaths, kind,
isProject ? ProjectExplorer::AddSubProject : ProjectExplorer::AddNewFile);
initializeVersionControls();
}
void JsonSummaryPage::cleanupPage()
{
JsonWizard *wiz = qobject_cast<JsonWizard *>(wizard());
QTC_ASSERT(wiz, return);
wiz->resetFileList();
disconnect(wiz, &JsonWizard::filesReady, this, 0);
}
void JsonSummaryPage::triggerCommit(const JsonWizard::GeneratorFiles &files)
{
GeneratedFiles coreFiles
= Utils::transform(files, [](const JsonWizard::GeneratorFile &f) -> GeneratedFile
{ return f.file; });
QString errorMessage;
if (!runVersionControl(coreFiles, &errorMessage)) {
QMessageBox::critical(wizard(), tr("Failed to Commit to Version Control"),
tr("Error message from Version Control System: '%1'.")
.arg(errorMessage));
}
}
void JsonSummaryPage::addToProject(const JsonWizard::GeneratorFiles &files)
{
JsonWizard *wiz = qobject_cast<JsonWizard *>(wizard());
QTC_ASSERT(wiz, return);
QString generatedProject = generatedProjectFilePath(files);
IWizardFactory::WizardKind kind = wizardKind(wiz);
FolderNode *folder = currentNode();
if (!folder)
return;
if (kind == IWizardFactory::ProjectWizard) {
if (!static_cast<ProjectNode *>(folder)->addSubProjects(QStringList(generatedProject))) {
QMessageBox::critical(wizard(), tr("Failed to Add to Project"),
tr("Failed to add subproject \"%1\"\nto project \"%2\".")
.arg(QDir::toNativeSeparators(generatedProject))
.arg(QDir::toNativeSeparators(folder->path())));
return;
}
wiz->removeAttributeFromAllFiles(GeneratedFile::OpenProjectAttribute);
} else {
QStringList filePaths = Utils::transform(files, [](const JsonWizard::GeneratorFile &f) {
return f.file.path();
});
if (!folder->addFiles(filePaths)) {
QStringList nativeFilePaths = Utils::transform(filePaths, &QDir::toNativeSeparators);
QMessageBox::critical(wizard(), tr("Failed to Add to Project"),
tr("Failed to add one or more files to project\n\"%1\" (%2).")
.arg(QDir::toNativeSeparators(folder->path()),
nativeFilePaths.join(QLatin1String(", "))));
return;
}
}
return;
}
} // namespace ProjectExplorer
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#ifndef JSONSUMMARYPAGE_H
#define JSONSUMMARYPAGE_H
#include "../projectwizardpage.h"
#include "jsonwizard.h"
#include <QWizardPage>
namespace ProjectExplorer {
// Documentation inside.
class JsonSummaryPage : public Internal::ProjectWizardPage
{
Q_OBJECT
public:
JsonSummaryPage(QWidget *parent = 0);
void initializePage();
void cleanupPage();
public slots:
void triggerCommit(const JsonWizard::GeneratorFiles &files);
void addToProject(const JsonWizard::GeneratorFiles &files);
};
} // namespace ProjectExplorer
#endif // JSONSUMMARYPAGE_H
......@@ -65,6 +65,11 @@ Utils::AbstractMacroExpander *JsonWizard::expander() const
return m_expander;
}
void JsonWizard::resetFileList()
{
m_files.clear();
}
JsonWizard::GeneratorFiles JsonWizard::fileList()
{
QString errorMessage;
......@@ -130,6 +135,12 @@ bool JsonWizard::boolFromVariant(const QVariant &v, Utils::AbstractMacroExpander
return v.toBool();
}
void JsonWizard::removeAttributeFromAllFiles(Core::GeneratedFile::Attribute a)
{
for (int i = 0; i < m_files.count(); ++i)
m_files[i].file.setAttributes(m_files.at(i).file.attributes() ^ a);
}
void JsonWizard::accept()
{
......
......@@ -72,6 +72,7 @@ public:
Utils::AbstractMacroExpander *expander() const;
void resetFileList();
GeneratorFiles fileList();
QVariant value(const QString &n) const;
......@@ -79,6 +80,8 @@ public:
static bool boolFromVariant(const QVariant &v, Utils::AbstractMacroExpander *expander);
void removeAttributeFromAllFiles(Core::GeneratedFile::Attribute a);
signals:
void preGenerateFiles(); // emitted before files are generated.
void postGenerateFiles(const JsonWizard::GeneratorFiles &files); // emitted after files are generated.
......
HEADERS += $$PWD/jsonwizard.h \
HEADERS += $$PWD/jsonfilepage.h \
$$PWD/jsonsummarypage.h \
$$PWD/jsonwizard.h \
$$PWD/jsonwizardexpander.h \
$$PWD/jsonwizardfactory.h \
$$PWD/jsonwizardfilegenerator.h \
$$PWD/jsonwizardgeneratorfactory.h \
$$PWD/jsonwizardpagefactory.h
$$PWD/jsonwizardpagefactory.h \
$$PWD/jsonwizardpagefactory_p.h
SOURCES += $$PWD/jsonwizard.cpp \
SOURCES += $$PWD/jsonfilepage.cpp \
$$PWD/jsonsummarypage.cpp \
$$PWD/jsonwizard.cpp \
$$PWD/jsonwizardexpander.cpp \
$$PWD/jsonwizardfactory.cpp \
$$PWD/jsonwizardfilegenerator.cpp \
$$PWD/jsonwizardgeneratorfactory.cpp \
$$PWD/jsonwizardpagefactory.cpp
$$PWD/jsonwizardpagefactory.cpp \
$$PWD/jsonwizardpagefactory_p.cpp
......@@ -374,7 +374,7 @@ void JsonWizardFactory::runWizard(const QString &path, QWidget *parent, const QS
for (auto i = variables.constBegin(); i != variables.constEnd(); ++i)
wizard.setProperty(i.key().toUtf8(), i.value());
wizard.setValue(QStringLiteral("GivenPath"), path);
wizard.setValue(QStringLiteral("InitialPath"), path);
wizard.setValue(QStringLiteral("Platform"), platform);
QString kindStr = QLatin1String(Core::Constants::WIZARD_KIND_UNKNOWN);
......
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#include "jsonwizardpagefactory_p.h"
#include "jsonfilepage.h"
#include "jsonsummarypage.h"
#include <utils/qtcassert.h>
#include <utils/wizardpage.h>
#include <QCoreApplication>
#include <QVariant>
namespace ProjectExplorer {
namespace Internal {
// --------------------------------------------------------------------
// FilePageFactory:
// --------------------------------------------------------------------
FilePageFactory::FilePageFactory()
{
setTypeIdsSuffix(QLatin1String("File"));
}
Utils::WizardPage *FilePageFactory::create(JsonWizard *wizard, Core::Id typeId, const QVariant &data)
{
Q_UNUSED(data);
QTC_ASSERT(canCreate(typeId), return 0);
JsonFilePage *page = new JsonFilePage;
page->setPath(wizard->value(QStringLiteral("InitialPath")).toString());
return page;
}
bool FilePageFactory::validateData(Core::Id typeId, const QVariant &data, QString *errorMessage)
{
QTC_ASSERT(canCreate(typeId), return false);
if (!data.isNull() && (data.type() != QVariant::Map || !data.toMap().isEmpty())) {
*errorMessage = QCoreApplication::translate("ProjectExplorer::JsonWizard",
"\"data\" for a \"File\" page needs to be unset or an empty object.");
return false;
}
return true;
}
// --------------------------------------------------------------------
// SummaryPageFactory:
// --------------------------------------------------------------------
SummaryPageFactory::SummaryPageFactory()
{
setTypeIdsSuffix(QLatin1String("Summary"));
}
Utils::WizardPage *SummaryPageFactory::create(JsonWizard *wizard, Core::Id typeId, const QVariant &data)
{
Q_UNUSED(wizard);
Q_UNUSED(data);
QTC_ASSERT(canCreate(typeId), return 0);
return new JsonSummaryPage;
}
bool SummaryPageFactory::validateData(Core::Id typeId, const QVariant &data, QString *errorMessage)
{
QTC_ASSERT(canCreate(typeId), return false);
if (!data.isNull() && (data.type() != QVariant::Map || !data.toMap().isEmpty())) {
*errorMessage = QCoreApplication::translate("ProjectExplorer::JsonWizard",
"\"data\" for a \"Summary\" page needs to be unset or an empty object.");
return false;
}
return true;
}
} // namespace Internal
} // namespace ProjectExplorer
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#ifndef JSONWIZARDPAGEFACTORY_P_H
#define JSONWIZARDPAGEFACTORY_P_H
#include "jsonwizardpagefactory.h"
namespace ProjectExplorer {
namespace Internal {
class FilePageFactory : public JsonWizardPageFactory
{
public:
FilePageFactory();
Utils::WizardPage *create(JsonWizard *wizard, Core::Id typeId, const QVariant &data);
bool validateData(Core::Id typeId, const QVariant &data, QString *errorMessage);
};
class SummaryPageFactory : public JsonWizardPageFactory
{
public:
SummaryPageFactory();
Utils::WizardPage *create(JsonWizard *wizard, Core::Id typeId, const QVariant &data);
bool validateData(Core::Id typeId, const QVariant &data, QString *errorMessage);
};
} // namespace Internal
} // namespace ProjectExplorer
#endif // JSONWIZARDPAGEFACTORY_P_H