ProjectWizardPage: Refactor code a little

Move functionality from the projectfilewizardextension into the
projectwizardpage itself and reduce the size of the context shared
between the extension and the page to a bare minimum.

Change-Id: I785fc103ea7d12e1da4bfdaf1d4664ed83a9989b
Reviewed-by: Daniel Teske <daniel.teske@digia.com>
This commit is contained in:
Tobias Hunger
2014-07-11 14:42:23 +02:00
parent 57a09d8cec
commit dd0032c83d
4 changed files with 121 additions and 112 deletions

View File

@@ -254,27 +254,19 @@ public:
ProjectWizardContext();
void clear();
QList<IVersionControl*> versionControls;
QList<IVersionControl*> activeVersionControls;
QPointer<ProjectWizardPage> page; // this is managed by the wizard!
bool repositoryExists; // Is VCS 'add' sufficient, or should a repository be created?
QString commonDirectory;
const IWizardFactory *wizard;
};
ProjectWizardContext::ProjectWizardContext() :
page(0),
repositoryExists(false),
wizard(0)
{
}
void ProjectWizardContext::clear()
{
activeVersionControls.clear();
commonDirectory.clear();
page = 0;
repositoryExists = false;
wizard = 0;
}
@@ -307,8 +299,8 @@ void ProjectFileWizardExtension::firstExtensionPageShown(
QStringList fileNames;
foreach (const GeneratedFile &f, files)
fileNames.push_back(f.path());
m_context->commonDirectory = Utils::commonPath(fileNames);
m_context->page->setFilesDisplay(m_context->commonDirectory, fileNames);
QString commonDirectory = Utils::commonPath(fileNames);
m_context->page->setFiles(fileNames);
QStringList filePaths;
ProjectExplorer::ProjectAction projectAction;
@@ -323,7 +315,7 @@ void ProjectFileWizardExtension::firstExtensionPageShown(
Node *contextNode = extraValues.value(QLatin1String(Constants::PREFERRED_PROJECT_NODE)).value<Node *>();
BestNodeSelector selector(m_context->commonDirectory, filePaths);
BestNodeSelector selector(commonDirectory, filePaths);
AddNewTree *tree = getChoices(filePaths, m_context->wizard->kind(), contextNode, &selector);
m_context->page->setAdditionalInfo(selector.deployingProjects());
@@ -333,63 +325,7 @@ void ProjectFileWizardExtension::firstExtensionPageShown(
m_context->page->setBestNode(selector.bestChoice());
m_context->page->setAddingSubProject(projectAction == ProjectExplorer::AddSubProject);
// Store all version controls for later use:
if (m_context->versionControls.isEmpty()) {
foreach (IVersionControl *vc, ExtensionSystem::PluginManager::getObjects<IVersionControl>()) {
m_context->versionControls.append(vc);
connect(vc, SIGNAL(configurationChanged()), this, SLOT(initializeVersionControlChoices()));
}
}
initializeVersionControlChoices();
}
void ProjectFileWizardExtension::initializeVersionControlChoices()
{
if (m_context->page.isNull())
return;
// Figure out version control situation:
// 1) Directory is managed and VCS supports "Add" -> List it
// 2) Directory is managed and VCS does not support "Add" -> None available
// 3) Directory is not managed -> Offer all VCS that support "CreateRepository"
IVersionControl *currentSelection = 0;
int currentIdx = m_context->page->versionControlIndex() - 1;
if (currentIdx >= 0 && currentIdx <= m_context->activeVersionControls.size() - 1)
currentSelection = m_context->activeVersionControls.at(currentIdx);
m_context->activeVersionControls.clear();
QStringList versionControlChoices = QStringList(tr("<None>"));
if (!m_context->commonDirectory.isEmpty()) {
IVersionControl *managingControl = VcsManager::findVersionControlForDirectory(m_context->commonDirectory);
if (managingControl) {
// Under VCS
if (managingControl->supportsOperation(IVersionControl::AddOperation)) {
versionControlChoices.append(managingControl->displayName());
m_context->activeVersionControls.push_back(managingControl);
m_context->repositoryExists = true;
}
} else {
// Create
foreach (IVersionControl *vc, m_context->versionControls)
if (vc->supportsOperation(IVersionControl::CreateRepositoryOperation)) {
versionControlChoices.append(vc->displayName());
m_context->activeVersionControls.append(vc);
}
m_context->repositoryExists = false;
}
} // has a common root.
m_context->page->setVersionControls(versionControlChoices);
// Enable adding to version control by default.
if (m_context->repositoryExists && versionControlChoices.size() >= 2)
m_context->page->setVersionControlIndex(1);
if (!m_context->repositoryExists) {
int newIdx = m_context->activeVersionControls.indexOf(currentSelection) + 1;
m_context->page->setVersionControlIndex(newIdx);
}
m_context->page->initializeVersionControls();
}
QList<QWizardPage *> ProjectFileWizardExtension::extensionPages(const IWizardFactory *wizard)
@@ -410,7 +346,7 @@ bool ProjectFileWizardExtension::processFiles(
{
if (!processProject(files, removeOpenProjectAttribute, errorMessage))
return false;
if (!processVersionControl(files, errorMessage)) {
if (!m_context->page->runVersionControl(files, errorMessage)) {
QString message;
if (errorMessage) {
message = *errorMessage;
@@ -457,34 +393,6 @@ bool ProjectFileWizardExtension::processProject(
return true;
}
bool ProjectFileWizardExtension::processVersionControl(const QList<GeneratedFile> &files, QString *errorMessage)
{
// Add files to version control (Entry at 0 is 'None').
const int vcsIndex = m_context->page->versionControlIndex() - 1;
if (vcsIndex < 0 || vcsIndex >= m_context->activeVersionControls.size())
return true;
QTC_ASSERT(!m_context->commonDirectory.isEmpty(), return false);
IVersionControl *versionControl = m_context->activeVersionControls.at(vcsIndex);
// Create repository?
if (!m_context->repositoryExists) {
QTC_ASSERT(versionControl->supportsOperation(IVersionControl::CreateRepositoryOperation), return false);
if (!versionControl->vcsCreateRepository(m_context->commonDirectory)) {
*errorMessage = tr("A version control system repository could not be created in \"%1\".").arg(m_context->commonDirectory);
return false;
}
}
// Add files if supported.
if (versionControl->supportsOperation(IVersionControl::AddOperation)) {
foreach (const GeneratedFile &generatedFile, files) {
if (!versionControl->vcsAdd(generatedFile.path())) {
*errorMessage = tr("Failed to add \"%1\" to the version control system.").arg(generatedFile.path());
return false;
}
}
}
return true;
}
static ICodeStylePreferences *codeStylePreferences(Project *project, Id languageId)
{
if (!languageId.isValid())

View File

@@ -55,12 +55,10 @@ public:
public slots:
void firstExtensionPageShown(const QList<Core::GeneratedFile> &files, const QVariantMap &extraValues);
void initializeVersionControlChoices();
private:
bool processProject(const QList<Core::GeneratedFile> &files,
bool *removeOpenProjectAttribute, QString *errorMessage);
bool processVersionControl(const QList<Core::GeneratedFile> &files, QString *errorMessage);
ProjectWizardContext *m_context;
};

View File

@@ -32,8 +32,13 @@
#include "ui_projectwizardpage.h"
#include <coreplugin/icore.h>
#include <coreplugin/iversioncontrol.h>
#include <coreplugin/vcsmanager.h>
#include <extensionsystem/pluginmanager.h>
#include <utils/algorithm.h>
#include <utils/fileutils.h>
#include <utils/qtcassert.h>
#include <utils/stringutils.h>
#include <utils/wizard.h>
#include <vcsbase/vcsbaseconstants.h>
@@ -50,13 +55,16 @@
\sa ProjectExplorer::Internal::ProjectFileWizardExtension
*/
using namespace ProjectExplorer;
using namespace Internal;
using namespace Core;
namespace ProjectExplorer {
namespace Internal {
ProjectWizardPage::ProjectWizardPage(QWidget *parent) :
QWizardPage(parent),
m_ui(new Ui::WizardPage),
m_model(0)
m_model(0),
m_repositoryExists(false)
{
m_ui->setupUi(this);
m_ui->vcsManageButton->setText(Core::ICore::msgShowOptionsDialog());
@@ -64,6 +72,9 @@ ProjectWizardPage::ProjectWizardPage(QWidget *parent) :
this, SLOT(slotProjectChanged(int)));
connect(m_ui->vcsManageButton, SIGNAL(clicked()), this, SLOT(slotManageVcs()));
setProperty(Utils::SHORT_TITLE_PROPERTY, tr("Summary"));
connect(Core::VcsManager::instance(), SIGNAL(configurationChanged(const IVersionControl*)),
this, SLOT(initializeVersionControls()));
}
ProjectWizardPage::~ProjectWizardPage()
@@ -136,6 +147,81 @@ void ProjectWizardPage::setAddingSubProject(bool addingSubProject)
: tr("Add to &project:"));
}
void ProjectWizardPage::initializeVersionControls()
{
// Figure out version control situation:
// 1) Directory is managed and VCS supports "Add" -> List it
// 2) Directory is managed and VCS does not support "Add" -> None available
// 3) Directory is not managed -> Offer all VCS that support "CreateRepository"
IVersionControl *currentSelection = 0;
int currentIdx = versionControlIndex() - 1;
if (currentIdx >= 0 && currentIdx <= m_activeVersionControls.size() - 1)
currentSelection = m_activeVersionControls.at(currentIdx);
m_activeVersionControls.clear();
QStringList versionControlChoices = QStringList(tr("<None>"));
if (!m_commonDirectory.isEmpty()) {
IVersionControl *managingControl = VcsManager::findVersionControlForDirectory(m_commonDirectory);
if (managingControl) {
// Under VCS
if (managingControl->supportsOperation(IVersionControl::AddOperation)) {
versionControlChoices.append(managingControl->displayName());
m_activeVersionControls.push_back(managingControl);
m_repositoryExists = true;
}
} else {
// Create
foreach (IVersionControl *vc, ExtensionSystem::PluginManager::getObjects<IVersionControl>()) {
if (vc->supportsOperation(IVersionControl::CreateRepositoryOperation)) {
versionControlChoices.append(vc->displayName());
m_activeVersionControls.append(vc);
}
}
m_repositoryExists = false;
}
} // has a common root.
setVersionControls(versionControlChoices);
// Enable adding to version control by default.
if (m_repositoryExists && versionControlChoices.size() >= 2)
setVersionControlIndex(1);
if (!m_repositoryExists) {
int newIdx = m_activeVersionControls.indexOf(currentSelection) + 1;
setVersionControlIndex(newIdx);
}
}
bool ProjectWizardPage::runVersionControl(const QList<GeneratedFile> &files, QString *errorMessage)
{
// Add files to version control (Entry at 0 is 'None').
const int vcsIndex = versionControlIndex() - 1;
if (vcsIndex < 0 || vcsIndex >= m_activeVersionControls.size())
return true;
QTC_ASSERT(!m_commonDirectory.isEmpty(), return false);
IVersionControl *versionControl = m_activeVersionControls.at(vcsIndex);
// Create repository?
if (!m_repositoryExists) {
QTC_ASSERT(versionControl->supportsOperation(IVersionControl::CreateRepositoryOperation), return false);
if (!versionControl->vcsCreateRepository(m_commonDirectory)) {
*errorMessage = tr("A version control system repository could not be created in \"%1\".").arg(m_commonDirectory);
return false;
}
}
// Add files if supported.
if (versionControl->supportsOperation(IVersionControl::AddOperation)) {
foreach (const GeneratedFile &generatedFile, files) {
if (!versionControl->vcsAdd(generatedFile.path())) {
*errorMessage = tr("Failed to add \"%1\" to the version control system.").arg(generatedFile.path());
return false;
}
}
}
return true;
}
void ProjectWizardPage::setNoneLabel(const QString &label)
{
m_ui->projectComboBox->setItemText(0, label);
@@ -163,23 +249,24 @@ void ProjectWizardPage::setVersionControlIndex(int idx)
m_ui->addToVersionControlComboBox->setCurrentIndex(idx);
}
void ProjectWizardPage::setFilesDisplay(const QString &commonPath, const QStringList &files)
void ProjectWizardPage::setFiles(const QStringList &fileNames)
{
m_commonDirectory = Utils::commonPath(fileNames);
QString fileMessage;
{
QTextStream str(&fileMessage);
str << "<qt>"
<< (commonPath.isEmpty() ? tr("Files to be added:") : tr("Files to be added in"))
<< (m_commonDirectory.isEmpty() ? tr("Files to be added:") : tr("Files to be added in"))
<< "<pre>";
QStringList formattedFiles;
if (commonPath.isEmpty()) {
formattedFiles = files;
if (m_commonDirectory.isEmpty()) {
formattedFiles = fileNames;
} else {
str << QDir::toNativeSeparators(commonPath) << ":\n\n";
const int prefixSize = commonPath.size() + 1;
foreach (const QString &f, files)
formattedFiles.append(f.right(f.size() - prefixSize));
str << QDir::toNativeSeparators(m_commonDirectory) << ":\n\n";
const int prefixSize = m_commonDirectory.size() + 1;
formattedFiles = Utils::transform(fileNames, [prefixSize](const QString &f)
{ return f.mid(prefixSize); });
}
// Alphabetically, and files in sub-directories first
Utils::sort(formattedFiles, [](const QString &filePath1, const QString &filePath2) -> bool {
@@ -217,3 +304,6 @@ void ProjectWizardPage::slotManageVcs()
Core::ICore::showOptionsDialog(VcsBase::Constants::VCS_SETTINGS_CATEGORY,
VcsBase::Constants::VCS_COMMON_SETTINGS_ID);
}
} // namespace Internal
} // namespace ProjectExplorer

View File

@@ -30,6 +30,8 @@
#ifndef PROJECTWIZARDPAGE_H
#define PROJECTWIZARDPAGE_H
#include <coreplugin/generatedfile.h>
#include <QWizardPage>
QT_BEGIN_NAMESPACE
@@ -37,6 +39,8 @@ class QTreeView;
class QModelIndex;
QT_END_NAMESPACE
namespace Core { class IVersionControl; }
namespace ProjectExplorer {
class FolderNode;
namespace Internal {
@@ -61,27 +65,36 @@ public:
void setNoneLabel(const QString &label);
void setAdditionalInfo(const QString &text);
void setVersionControls(const QStringList &);
int versionControlIndex() const;
void setVersionControlIndex(int);
// Returns the common path
void setFilesDisplay(const QString &commonPath, const QStringList &files);
void setFiles(const QStringList &files);
void setAddingSubProject(bool addingSubProject);
bool runVersionControl(const QList<Core::GeneratedFile> &files, QString *errorMessage);
public slots:
void initializeVersionControls();
private slots:
void slotProjectChanged(int);
void slotManageVcs();
private:
void setVersionControls(const QStringList &);
void setProjectToolTip(const QString &);
bool expandTree(const QModelIndex &root);
Ui::WizardPage *m_ui;
QStringList m_projectToolTips;
AddNewModel *m_model;
QList<Core::IVersionControl*> m_activeVersionControls;
QString m_commonDirectory;
bool m_repositoryExists;
};
} // namespace Internal