Implement adding new project into opened subdirs project

Reviewed-by: dt <qtc-committer@nokia.com>
This commit is contained in:
Jarek Kobus
2010-08-26 18:33:16 +02:00
parent fc1be8b488
commit 1cefde9e45
30 changed files with 271 additions and 46 deletions

View File

@@ -49,6 +49,12 @@ QList<ProjectExplorer::ProjectNode::ProjectAction> CMakeProjectNode::supportedAc
return QList<ProjectAction>();
}
bool CMakeProjectNode::canAddSubProject(const QString &proFilePath) const
{
Q_UNUSED(proFilePath)
return false;
}
bool CMakeProjectNode::addSubProjects(const QStringList &proFilePaths)
{
Q_UNUSED(proFilePaths)

View File

@@ -43,6 +43,9 @@ public:
CMakeProjectNode(const QString &fileName);
virtual bool hasBuildTargets() const;
virtual QList<ProjectExplorer::ProjectNode::ProjectAction> supportedActions(Node *node) const;
virtual bool canAddSubProject(const QString &proFilePath) const;
virtual bool addSubProjects(const QStringList &proFilePaths);
virtual bool removeSubProjects(const QStringList &proFilePaths);
virtual bool addFiles(const ProjectExplorer::FileType fileType,

View File

@@ -530,7 +530,7 @@ QStringList BaseFileWizard::runWizard(const QString &path, QWidget *parent)
}
if (firstExtensionPageHit)
foreach (IFileWizardExtension *ex, extensions)
ex->firstExtensionPageShown(files);
ex->firstExtensionPageShown(files, generatedProjectFilePath(wizard.data()));
if (accepted)
break;
}
@@ -558,12 +558,23 @@ QStringList BaseFileWizard::runWizard(const QString &path, QWidget *parent)
return QStringList();
}
}
bool removeOpenProjectAttribute = false;
// Run the extensions
foreach (IFileWizardExtension *ex, extensions)
if (!ex->process(files, &errorMessage)) {
foreach (IFileWizardExtension *ex, extensions) {
bool remove;
if (!ex->process(files, generatedProjectFilePath(wizard.data()), &remove, &errorMessage)) {
QMessageBox::critical(parent, tr("File Generation Failure"), errorMessage);
return QStringList();
}
removeOpenProjectAttribute |= remove;
}
if (removeOpenProjectAttribute) {
for (int i = 0; i < files.count(); i++) {
if (files[i].attributes() & Core::GeneratedFile::OpenProjectAttribute)
files[i].setAttributes(Core::GeneratedFile::OpenEditorAttribute);
}
}
// Post generation handler
if (!postGenerateFiles(wizard.data(), files, &errorMessage)) {
@@ -596,6 +607,12 @@ void BaseFileWizard::applyExtensionPageShortTitle(Utils::Wizard *wizard, int pag
item->setTitle(shortTitle);
}
QString BaseFileWizard::generatedProjectFilePath(const QWizard *wizard) const
{
Q_UNUSED(wizard)
return QString();
}
bool BaseFileWizard::postGenerateFiles(const QWizard *, const GeneratedFiles &l, QString *errorMessage)
{
return BaseFileWizard::postGenerateOpenEditors(l, errorMessage);

View File

@@ -200,6 +200,9 @@ protected:
virtual GeneratedFiles generateFiles(const QWizard *w,
QString *errorMessage) const = 0;
// Overwrite for ProjectWizard kind and return the path to the generated project file
virtual QString generatedProjectFilePath(const QWizard *wizard) const;
/* Overwrite to perform steps to be done after files are actually created.
* The default implementation opens editors with the newly generated files. */
virtual bool postGenerateFiles(const QWizard *w, const GeneratedFiles &l, QString *errorMessage);

View File

@@ -57,11 +57,18 @@ public:
virtual QList<QWizardPage *> extensionPages(const IWizard *wizard) = 0;
/* Process the files using the extension parameters */
virtual bool process(const QList<GeneratedFile> &files, QString *errorMessage) = 0;
virtual bool process(const QList<GeneratedFile> &files,
const QString &generatedProjectFilePath,
bool *removeOpenProjectAttribute,
QString *errorMessage) = 0;
public slots:
/* Notification about the first extension page being shown. */
virtual void firstExtensionPageShown(const QList<GeneratedFile> &) {}
virtual void firstExtensionPageShown(const QList<GeneratedFile> &files,
const QString &generatedProjectFilePath) {
Q_UNUSED(files)
Q_UNUSED(generatedProjectFilePath)
}
};
} // namespace Core

View File

@@ -179,6 +179,12 @@ QList<ProjectExplorer::ProjectNode::ProjectAction> GenericProjectNode::supported
<< RemoveFile;
}
bool GenericProjectNode::canAddSubProject(const QString &proFilePath) const
{
Q_UNUSED(proFilePath)
return false;
}
bool GenericProjectNode::addSubProjects(const QStringList &proFilePaths)
{
Q_UNUSED(proFilePaths)

View File

@@ -57,6 +57,8 @@ public:
virtual QList<ProjectExplorer::ProjectNode::ProjectAction> supportedActions(Node *node) const;
virtual bool canAddSubProject(const QString &proFilePath) const;
virtual bool addSubProjects(const QStringList &proFilePaths);
virtual bool removeSubProjects(const QStringList &proFilePaths);

View File

@@ -231,6 +231,17 @@ Core::GeneratedFiles GenericProjectWizard::generateFiles(const QWizard *w,
return files;
}
QString GenericProjectWizard::generatedProjectFilePath(const QWizard *w) const
{
const GenericProjectWizardDialog *wizard = qobject_cast<const GenericProjectWizardDialog *>(w);
const QString projectPath = wizard->path();
const QDir dir(projectPath);
const QString projectName = wizard->projectName();
const QString creatorFileName = QFileInfo(dir, projectName + QLatin1String(".creator")).absoluteFilePath();
return creatorFileName;
}
bool GenericProjectWizard::postGenerateFiles(const QWizard *w, const Core::GeneratedFiles &l, QString *errorMessage)
{
Q_UNUSED(w);

View File

@@ -81,6 +81,8 @@ protected:
virtual Core::GeneratedFiles generateFiles(const QWizard *w,
QString *errorMessage) const;
virtual QString generatedProjectFilePath(const QWizard *w) const;
virtual bool postGenerateFiles(const QWizard *w, const Core::GeneratedFiles &l, QString *errorMessage);
bool isValidDir(const QFileInfo &fileInfo) const;

View File

@@ -451,6 +451,25 @@ bool CustomProjectWizard::postGenerateOpen(const Core::GeneratedFiles &l, QStrin
return BaseFileWizard::postGenerateOpenEditors(l, errorMessage);
}
QString CustomProjectWizard::generatedProjectFilePath(const QWizard *wizard) const
{
const BaseProjectWizardDialog *dialog = qobject_cast<const BaseProjectWizardDialog *>(wizard);
QTC_ASSERT(dialog, return QString())
const QString targetPath = dialog->path() + QLatin1Char('/') + dialog->projectName();
const QChar slash = QLatin1Char('/');
// take the first from parameters()->files list which have cwFile.openProject set
foreach(const Internal::CustomWizardFile &file, parameters()->files) {
if (file.openProject) {
FieldReplacementMap fieldReplacementMap = replacementMap(dialog);
fieldReplacementMap.insert(QLatin1String("ProjectName"), dialog->projectName());
QString target = file.target;
Internal::CustomWizardContext::replaceFields(fieldReplacementMap, &target);
return QDir::toNativeSeparators(targetPath + slash + target);
}
}
return QString();
}
bool CustomProjectWizard::postGenerateFiles(const QWizard *, const Core::GeneratedFiles &l, QString *errorMessage)
{
if (CustomWizardPrivate::verbose)

View File

@@ -162,6 +162,7 @@ signals:
void projectLocationChanged(const QString &path);
protected:
QString generatedProjectFilePath(const QWizard *wizard) const;
virtual bool postGenerateFiles(const QWizard *w, const Core::GeneratedFiles &l, QString *errorMessage);
void initProjectWizardDialog(BaseProjectWizardDialog *w, const QString &defaultPath,

View File

@@ -38,7 +38,6 @@
#include <utils/stringutils.h>
#include <coreplugin/basefilewizard.h>
#include <coreplugin/dialogs/iwizard.h>
#include <coreplugin/filemanager.h>
#include <coreplugin/icore.h>
#include <coreplugin/iversioncontrol.h>
@@ -156,11 +155,13 @@ struct ProjectWizardContext
ProjectWizardPage *page;
bool repositoryExists; // Is VCS 'add' sufficient, or should a repository be created?
QString commonDirectory;
const Core::IWizard *wizard;
};
ProjectWizardContext::ProjectWizardContext() :
page(0),
repositoryExists(false)
repositoryExists(false),
wizard(0)
{
}
@@ -171,6 +172,7 @@ void ProjectWizardContext::clear()
commonDirectory.clear();
page = 0;
repositoryExists = false;
wizard = 0;
}
// ---- ProjectFileWizardExtension
@@ -222,8 +224,12 @@ static int findMatchingProject(const QList<ProjectEntry> &projects,
return bestMatch;
}
void ProjectFileWizardExtension::firstExtensionPageShown(const QList<Core::GeneratedFile> &files)
void ProjectFileWizardExtension::firstExtensionPageShown(
const QList<Core::GeneratedFile> &files,
const QString &generatedProjectFilePath)
{
initProjectChoices(generatedProjectFilePath);
if (debugExtension)
qDebug() << Q_FUNC_INFO << files.size();
// Parametrize wizard page: find best project to add to, set up files display and
@@ -307,13 +313,11 @@ QList<QWizardPage *> ProjectFileWizardExtension::extensionPages(const Core::IWiz
}
// Init context with page and projects
m_context->page = new ProjectWizardPage;
// Project list remains the same over duration of wizard execution
// Note that projects cannot be added to projects.
initProjectChoices(wizard->kind() != Core::IWizard::ProjectWizard);
m_context->wizard = wizard;
return QList<QWizardPage *>() << m_context->page;
}
void ProjectFileWizardExtension::initProjectChoices(bool enabled)
void ProjectFileWizardExtension::initProjectChoices(const QString &generatedProjectFilePath)
{
// Set up project list which remains the same over duration of wizard execution
// As tooltip, set the directory for disambiguation (should someone have
@@ -321,14 +325,24 @@ void ProjectFileWizardExtension::initProjectChoices(bool enabled)
//: No project selected
QStringList projectChoices(tr("<None>"));
QStringList projectToolTips((QString()));
if (enabled) {
typedef QMap<ProjectEntry, bool> ProjectEntryMap;
// Sort by base name and purge duplicated entries (resulting from dependencies)
// via Map.
ProjectEntryMap entryMap;
foreach(ProjectNode *n, AllProjectNodesVisitor::allProjects(ProjectNode::AddNewFile))
ProjectNode::ProjectAction projectAction =
m_context->wizard->kind() == Core::IWizard::ProjectWizard
? ProjectNode::AddSubProject : ProjectNode::AddNewFile;
foreach(ProjectNode *n, AllProjectNodesVisitor::allProjects(projectAction)) {
if (projectAction == ProjectNode::AddNewFile
|| (projectAction == ProjectNode::AddSubProject
&& n->canAddSubProject(generatedProjectFilePath)))
entryMap.insert(ProjectEntry(n), true);
}
m_context->projects.clear();
// Collect names
const ProjectEntryMap::const_iterator cend = entryMap.constEnd();
for (ProjectEntryMap::const_iterator it = entryMap.constBegin(); it != cend; ++it) {
@@ -336,27 +350,44 @@ void ProjectFileWizardExtension::initProjectChoices(bool enabled)
projectChoices.push_back(it.key().fileName);
projectToolTips.push_back(it.key().nativeDirectory);
}
}
m_context->page->setProjects(projectChoices);
m_context->page->setProjectToolTips(projectToolTips);
}
bool ProjectFileWizardExtension::process(const QList<Core::GeneratedFile> &files, QString *errorMessage)
bool ProjectFileWizardExtension::process(
const QList<Core::GeneratedFile> &files,
const QString &generatedProjectFilePath,
bool *removeOpenProjectAttribute, QString *errorMessage)
{
return processProject(files, errorMessage) &&
return processProject(files, generatedProjectFilePath,
removeOpenProjectAttribute, errorMessage) &&
processVersionControl(files, errorMessage);
}
// Add files to project && version control
bool ProjectFileWizardExtension::processProject(const QList<Core::GeneratedFile> &files, QString *errorMessage)
bool ProjectFileWizardExtension::processProject(
const QList<Core::GeneratedFile> &files,
const QString &generatedProjectFilePath,
bool *removeOpenProjectAttribute, QString *errorMessage)
{
typedef QMultiMap<FileType, QString> TypeFileMap;
*removeOpenProjectAttribute = false;
// Add files to project (Entry at 0 is 'None').
const int projectIndex = m_context->page->currentProjectIndex() - 1;
if (projectIndex < 0 || projectIndex >= m_context->projects.size())
return true;
ProjectNode *project = m_context->projects.at(projectIndex).node;
if (m_context->wizard->kind() == Core::IWizard::ProjectWizard) {
if (!project->addSubProjects(QStringList(generatedProjectFilePath))) {
*errorMessage = tr("Failed to add subproject '%1'\nto project '%2'.")
.arg(generatedProjectFilePath).arg(project->path());
return false;
}
*removeOpenProjectAttribute = true;
} else {
// Split into lists by file type and bulk-add them.
TypeFileMap typeFileMap;
const Core::MimeDatabase *mdb = Core::ICore::instance()->mimeDatabase();
@@ -372,6 +403,7 @@ bool ProjectFileWizardExtension::processProject(const QList<Core::GeneratedFile>
return false;
}
}
}
return true;
}

View File

@@ -50,15 +50,20 @@ public:
virtual ~ProjectFileWizardExtension();
virtual QList<QWizardPage *> extensionPages(const Core::IWizard *wizard);
virtual bool process(const QList<Core::GeneratedFile> &files, QString *errorMessage);
virtual bool process(const QList<Core::GeneratedFile> &files,
const QString &generatedProjectFilePath,
bool *removeOpenProjectAttribute, QString *errorMessage);
public slots:
virtual void firstExtensionPageShown(const QList<Core::GeneratedFile> &);
virtual void firstExtensionPageShown(const QList<Core::GeneratedFile> &files,
const QString &generatedProjectFilePath);
private:
void initProjectChoices(bool enabled);
void initProjectChoices(const QString &generatedProjectFilePath);
void initializeVersionControlChoices();
bool processProject(const QList<Core::GeneratedFile> &files, QString *errorMessage);
bool processProject(const QList<Core::GeneratedFile> &files,
const QString &generatedProjectFilePath,
bool *removeOpenProjectAttribute, QString *errorMessage);
bool processVersionControl(const QList<Core::GeneratedFile> &files, QString *errorMessage);
ProjectWizardContext *m_context;

View File

@@ -182,6 +182,8 @@ public:
virtual QList<ProjectAction> supportedActions(Node *node) const = 0;
virtual bool canAddSubProject(const QString &proFilePath) const = 0;
virtual bool addSubProjects(const QStringList &proFilePaths) = 0;
virtual bool removeSubProjects(const QStringList &proFilePaths) = 0;

View File

@@ -180,6 +180,12 @@ QList<ProjectExplorer::ProjectNode::ProjectAction> QmlProjectNode::supportedActi
return actions;
}
bool QmlProjectNode::canAddSubProject(const QString &proFilePath) const
{
Q_UNUSED(proFilePath)
return false;
}
bool QmlProjectNode::addSubProjects(const QStringList &proFilePaths)
{
Q_UNUSED(proFilePaths)

View File

@@ -58,6 +58,8 @@ public:
virtual QList<ProjectExplorer::ProjectNode::ProjectAction> supportedActions(Node *node) const;
virtual bool canAddSubProject(const QString &proFilePath) const;
virtual bool addSubProjects(const QStringList &proFilePaths);
virtual bool removeSubProjects(const QStringList &proFilePaths);

View File

@@ -75,5 +75,16 @@ Core::GeneratedFiles CustomWidgetWizard::generateFiles(const QWizard *w,
return PluginGenerator::generatePlugin(p, *(cw->pluginOptions()), errorMessage);
}
QString CustomWidgetWizard::generatedProjectFilePath(const QWizard *w) const
{
const CustomWidgetWizardDialog *cw = qobject_cast<const CustomWidgetWizardDialog *>(w);
const QChar slash = QLatin1Char('/');
QString baseDir = cw->path();
baseDir += slash;
baseDir += cw->projectName();
baseDir += slash;
return baseDir + cw->projectName() + QLatin1String(".pro");
}
} // namespace Internal
} // namespace Qt4ProjectManager

View File

@@ -49,6 +49,8 @@ protected:
virtual Core::GeneratedFiles generateFiles(const QWizard *w,
QString *errorMessage) const;
virtual QString generatedProjectFilePath(const QWizard *w) const;
};
} // namespace Internal

View File

@@ -771,10 +771,39 @@ QList<ProjectNode::ProjectAction> Qt4PriFileNode::supportedActions(Node *node) c
return actions;
}
bool Qt4PriFileNode::canAddSubProject(const QString &proFilePath) const
{
QFileInfo fi(proFilePath);
if (fi.suffix() == QLatin1String("pro")
|| fi.suffix() == QLatin1String("pri"))
return true;
return false;
}
bool Qt4PriFileNode::addSubProjects(const QStringList &proFilePaths)
{
Q_UNUSED(proFilePaths)
return false; //changeIncludes(m_includeFile, proFilePaths, AddToProFile);
ProjectExplorer::FindAllFilesVisitor visitor;
accept(&visitor);
const QStringList &allFiles = visitor.filePaths();
QStringList uniqueProFilePaths;
foreach (const QString &proFile, proFilePaths) {
if (!allFiles.contains(proFile)) {
// if proFilePath is like: _path_/projectName/projectName.pro
// we simplify it to: _path_/projectName
QString proFilePath = proFile;
QFileInfo fi(proFile);
QFileInfo parentFi(fi.absolutePath());
if (parentFi.fileName() == fi.baseName())
proFilePath = parentFi.absoluteFilePath();
uniqueProFilePaths.append(proFilePath);
}
}
QStringList failedFiles;
changeFiles(ProjectExplorer::ProjectFileType, uniqueProFilePaths, &failedFiles, AddToProFile);
return failedFiles.isEmpty();
}
bool Qt4PriFileNode::removeSubProjects(const QStringList &proFilePaths)
@@ -1085,6 +1114,9 @@ QStringList Qt4PriFileNode::varNames(ProjectExplorer::FileType type)
case ProjectExplorer::FormType:
vars << QLatin1String("FORMS");
break;
case ProjectExplorer::ProjectFileType:
vars << QLatin1String("SUBDIRS");
break;
case ProjectExplorer::QMLType:
break;
default:

View File

@@ -140,6 +140,8 @@ public:
bool hasBuildTargets() const { return false; }
bool canAddSubProject(const QString &proFilePath) const;
bool addSubProjects(const QStringList &proFilePaths);
bool removeSubProjects(const QStringList &proFilePaths);

View File

@@ -104,5 +104,14 @@ Core::GeneratedFiles
return Core::GeneratedFiles() << source << profile;
}
QString ConsoleAppWizard::generatedProjectFilePath(const QWizard *w) const
{
const ConsoleAppWizardDialog *wizard = qobject_cast< const ConsoleAppWizardDialog *>(w);
const QtProjectParameters params = wizard->parameters();
const QString projectPath = params.projectPath();
return Core::BaseFileWizard::buildFileName(projectPath, params.fileName, profileSuffix());
}
} // namespace Internal
} // namespace Qt4ProjectManager

View File

@@ -51,6 +51,8 @@ protected:
virtual Core::GeneratedFiles generateFiles(const QWizard *w,
QString *errorMessage) const;
QString generatedProjectFilePath(const QWizard *w) const;
};
} // namespace Internal

View File

@@ -74,5 +74,13 @@ Core::GeneratedFiles
return Core::GeneratedFiles() << profile;
}
QString EmptyProjectWizard::generatedProjectFilePath(const QWizard *w) const
{
const EmptyProjectWizardDialog *wizard = qobject_cast< const EmptyProjectWizardDialog *>(w);
const QtProjectParameters params = wizard->parameters();
const QString projectPath = params.projectPath();
return Core::BaseFileWizard::buildFileName(projectPath, params.fileName, profileSuffix());
}
} // namespace Internal
} // namespace Qt4ProjectManager

View File

@@ -49,6 +49,8 @@ protected:
virtual Core::GeneratedFiles generateFiles(const QWizard *w,
QString *errorMessage) const;
QString generatedProjectFilePath(const QWizard *w) const;
};
} // namespace Internal

View File

@@ -237,6 +237,15 @@ Core::GeneratedFiles GuiAppWizard::generateFiles(const QWizard *w,
return rc;
}
QString GuiAppWizard::generatedProjectFilePath(const QWizard *w) const
{
const GuiAppWizardDialog *dialog = qobject_cast<const GuiAppWizardDialog *>(w);
const QtProjectParameters projectParams = dialog->projectParameters();
const QString projectPath = projectParams.projectPath();
return buildFileName(projectPath, projectParams.fileName, profileSuffix());
}
bool GuiAppWizard::parametrizeTemplate(const QString &templatePath, const QString &templateName,
const GuiAppParameters &params,
QString *target, QString *errorMessage)

View File

@@ -61,6 +61,8 @@ protected:
virtual Core::GeneratedFiles generateFiles(const QWizard *w,
QString *errorMessage) const;
QString generatedProjectFilePath(const QWizard *w) const;
private:
static bool parametrizeTemplate(const QString &templatePath, const QString &templateName,
const GuiAppParameters &params,

View File

@@ -137,5 +137,15 @@ Core::GeneratedFiles LibraryWizard::generateFiles(const QWizard *w,
return rc;
}
QString LibraryWizard::generatedProjectFilePath(const QWizard *w) const
{
const LibraryWizardDialog *dialog = qobject_cast<const LibraryWizardDialog *>(w);
const QtProjectParameters projectParams = dialog->parameters();
const QString projectPath = projectParams.projectPath();
return buildFileName(projectPath, projectParams.fileName, profileSuffix());
}
} // namespace Internal
} // namespace Qt4ProjectManager

View File

@@ -54,6 +54,8 @@ protected:
virtual Core::GeneratedFiles generateFiles(const QWizard *w,
QString *errorMessage) const;
QString generatedProjectFilePath(const QWizard *w) const;
};
} // namespace Internal

View File

@@ -184,5 +184,14 @@ Core::GeneratedFiles TestWizard::generateFiles(const QWizard *w, QString *errorM
return Core::GeneratedFiles() << source << profile;
}
QString TestWizard::generatedProjectFilePath(const QWizard *w) const
{
const TestWizardDialog *wizardDialog = qobject_cast<const TestWizardDialog *>(w);
const QtProjectParameters projectParams = wizardDialog->projectParameters();
const QString projectPath = projectParams.projectPath();
return buildFileName(projectPath, projectParams.fileName, profileSuffix());
}
} // namespace Internal
} // namespace Qt4ProjectManager

View File

@@ -50,6 +50,7 @@ protected:
virtual Core::GeneratedFiles generateFiles(const QWizard *w,
QString *errorMessage) const;
QString generatedProjectFilePath(const QWizard *w) const;
signals:
public slots: