Improve simpleprojectwizard

- add Qt modules
- add support for cmake build-system

Change-Id: I80ea5ef78bbffedf8f2c140365ee31718e7ee4a4
Reviewed-by: Alessandro Portale <alessandro.portale@qt.io>
This commit is contained in:
BogDan Vatra
2020-02-26 15:05:29 +02:00
committed by BogDan Vatra
parent b0b50257ec
commit fb252248be

View File

@@ -32,6 +32,7 @@
#include <coreplugin/basefilewizard.h> #include <coreplugin/basefilewizard.h>
#include <coreplugin/icore.h> #include <coreplugin/icore.h>
#include <cmakeprojectmanager/cmakeprojectconstants.h>
#include <projectexplorer/projectexplorerconstants.h> #include <projectexplorer/projectexplorerconstants.h>
#include <projectexplorer/customwizard/customwizard.h> #include <projectexplorer/customwizard/customwizard.h>
#include <projectexplorer/selectablefilesmodel.h> #include <projectexplorer/selectablefilesmodel.h>
@@ -43,9 +44,11 @@
#include <utils/wizard.h> #include <utils/wizard.h>
#include <QApplication> #include <QApplication>
#include <QComboBox>
#include <QDebug> #include <QDebug>
#include <QDir> #include <QDir>
#include <QFileInfo> #include <QFileInfo>
#include <QLineEdit>
#include <QPainter> #include <QPainter>
#include <QPixmap> #include <QPixmap>
#include <QStyle> #include <QStyle>
@@ -72,10 +75,14 @@ public:
void cleanupPage() override { m_filesWidget->cancelParsing(); } void cleanupPage() override { m_filesWidget->cancelParsing(); }
FilePaths selectedFiles() const { return m_filesWidget->selectedFiles(); } FilePaths selectedFiles() const { return m_filesWidget->selectedFiles(); }
FilePaths selectedPaths() const { return m_filesWidget->selectedPaths(); } FilePaths selectedPaths() const { return m_filesWidget->selectedPaths(); }
QString qtModules() const { return m_qtModules; }
QString buildSystem() const { return m_buildSystem; }
private: private:
SimpleProjectWizardDialog *m_simpleProjectWizardDialog; SimpleProjectWizardDialog *m_simpleProjectWizardDialog;
SelectableFilesWidget *m_filesWidget; SelectableFilesWidget *m_filesWidget;
QString m_qtModules;
QString m_buildSystem;
}; };
FilesSelectionWizardPage::FilesSelectionWizardPage(SimpleProjectWizardDialog *simpleProjectWizard) FilesSelectionWizardPage::FilesSelectionWizardPage(SimpleProjectWizardDialog *simpleProjectWizard)
@@ -83,6 +90,31 @@ FilesSelectionWizardPage::FilesSelectionWizardPage(SimpleProjectWizardDialog *si
m_filesWidget(new SelectableFilesWidget(this)) m_filesWidget(new SelectableFilesWidget(this))
{ {
auto layout = new QVBoxLayout(this); auto layout = new QVBoxLayout(this);
{
auto hlayout = new QHBoxLayout;
hlayout->addWidget(new QLabel("Qt modules", this));
auto lineEdit = new QLineEdit("core gui widgets", this);
connect(lineEdit, &QLineEdit::editingFinished, this, [this, lineEdit]{
m_qtModules = lineEdit->text();
});
m_qtModules = lineEdit->text();
hlayout->addWidget(lineEdit);
layout->addLayout(hlayout);
}
{
auto hlayout = new QHBoxLayout;
hlayout->addWidget(new QLabel("Build system", this));
auto comboBox = new QComboBox(this);
connect(comboBox, &QComboBox::currentTextChanged, this, [this](const QString &bs){
m_buildSystem = bs;
});
comboBox->addItems(QStringList() << "qmake" << "cmake");
comboBox->setEditable(false);
comboBox->setCurrentText("qmake");
hlayout->addWidget(comboBox);
layout->addLayout(hlayout);
}
layout->addWidget(m_filesWidget); layout->addWidget(m_filesWidget);
m_filesWidget->setBaseDirEditable(false); m_filesWidget->setBaseDirEditable(false);
@@ -119,6 +151,8 @@ public:
void setPath(const QString &path) { m_firstPage->setPath(path); } void setPath(const QString &path) { m_firstPage->setPath(path); }
FilePaths selectedFiles() const { return m_secondPage->selectedFiles(); } FilePaths selectedFiles() const { return m_secondPage->selectedFiles(); }
FilePaths selectedPaths() const { return m_secondPage->selectedPaths(); } FilePaths selectedPaths() const { return m_secondPage->selectedPaths(); }
QString qtModules() const { return m_secondPage->qtModules(); }
QString buildSystem() const { return m_secondPage->buildSystem(); }
QString projectName() const { return m_firstPage->fileName(); } QString projectName() const { return m_firstPage->fileName(); }
FileWizardPage *m_firstPage; FileWizardPage *m_firstPage;
@@ -133,14 +167,15 @@ void FilesSelectionWizardPage::initializePage()
SimpleProjectWizard::SimpleProjectWizard() SimpleProjectWizard::SimpleProjectWizard()
{ {
setSupportedProjectTypes({Constants::QMAKEPROJECT_ID}); setSupportedProjectTypes({QmakeProjectManager::Constants::QMAKEPROJECT_ID,
CMakeProjectManager::Constants::CMAKEPROJECT_ID});
setIcon(QIcon(QLatin1String(":/qmakeprojectmanager/images/qmakeprojectmanager.png"))); setIcon(QIcon(QLatin1String(":/qmakeprojectmanager/images/qmakeprojectmanager.png")));
setDisplayName(tr("Import as qmake Project (Limited Functionality)")); setDisplayName(tr("Import as qmake or cmake Project (Limited Functionality)"));
setId("Z.DummyProFile"); setId("Z.DummyProFile");
setDescription(tr("Imports existing projects that do not use qmake, CMake or Autotools.<p>" setDescription(tr("Imports existing projects that do not use qmake, CMake or Autotools.<p>"
"This creates a qmake .pro file that allows you to use %1 as a code editor " "This creates a project file that allows you to use %1 as a code editor "
"and as a launcher for debugging and analyzing tools. " "and as a launcher for debugging and analyzing tools. "
"If you want to build the project, you might need to edit the generated .pro file.") "If you want to build the project, you might need to edit the generated project file.")
.arg(Core::Constants::IDE_DISPLAY_NAME)); .arg(Core::Constants::IDE_DISPLAY_NAME));
setCategory(ProjectExplorer::Constants::IMPORT_WIZARD_CATEGORY); setCategory(ProjectExplorer::Constants::IMPORT_WIZARD_CATEGORY);
setDisplayCategory(ProjectExplorer::Constants::IMPORT_WIZARD_CATEGORY_DISPLAY); setDisplayCategory(ProjectExplorer::Constants::IMPORT_WIZARD_CATEGORY_DISPLAY);
@@ -159,12 +194,10 @@ BaseFileWizard *SimpleProjectWizard::create(QWidget *parent,
return wizard; return wizard;
} }
GeneratedFiles SimpleProjectWizard::generateFiles(const QWizard *w, GeneratedFiles generateQmakeFiles(const SimpleProjectWizardDialog *wizard,
QString *errorMessage) const QString *errorMessage)
{ {
Q_UNUSED(errorMessage) Q_UNUSED(errorMessage)
auto wizard = qobject_cast<const SimpleProjectWizardDialog *>(w);
const QString projectPath = wizard->path(); const QString projectPath = wizard->path();
const QDir dir(projectPath); const QDir dir(projectPath);
const QString projectName = wizard->projectName(); const QString projectName = wizard->projectName();
@@ -209,15 +242,112 @@ GeneratedFiles SimpleProjectWizard::generateFiles(const QWizard *w,
+ " This file was created for editing the project sources only.\n" + " This file was created for editing the project sources only.\n"
"# You may attempt to use it for building too, by modifying this file here.\n\n" "# You may attempt to use it for building too, by modifying this file here.\n\n"
"#TARGET = " + projectName + "\n\n" "#TARGET = " + projectName + "\n\n"
"QT = " + wizard->qtModules() + "\n\n"
+ proHeaders + "\n\n" + proHeaders + "\n\n"
+ proSources + "\n\n" + proSources + "\n\n"
+ proIncludes + "\n\n" + proIncludes + "\n\n"
"#DEFINES = \n\n" "#DEFINES = \n\n"
); );
return GeneratedFiles{generatedProFile}; return GeneratedFiles{generatedProFile};
} }
GeneratedFiles generateCmakeFiles(const SimpleProjectWizardDialog *wizard,
QString *errorMessage)
{
Q_UNUSED(errorMessage)
const QString projectPath = wizard->path();
const QDir dir(projectPath);
const QString projectName = wizard->projectName();
const QString projectFileName = QFileInfo(dir, "CMakeLists.txt").absoluteFilePath();
const QStringList paths = Utils::transform(wizard->selectedPaths(), &FilePath::toString);
MimeType headerType = Utils::mimeTypeForName("text/x-chdr");
QStringList nameFilters = headerType.globPatterns();
QString includes = "include_directories(\n";
bool haveIncludes = false;
for (const QString &path : paths) {
QFileInfo fileInfo(path);
QDir thisDir(fileInfo.absoluteFilePath());
if (!thisDir.entryList(nameFilters, QDir::Files).isEmpty()) {
QString relative = dir.relativeFilePath(path);
if (!relative.isEmpty()) {
includes.append(" " + relative + "\n");
haveIncludes = true;
}
}
}
if (haveIncludes)
includes += ")";
else
includes.clear();
QString srcs = "set (SRCS\n";
for (const FilePath &fileName : wizard->selectedFiles())
srcs += " " + dir.relativeFilePath(fileName.toString()) + "\n";
srcs += ")\n";
QString components = "find_package(Qt5 COMPONENTS";
QString libs = "target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE";
bool haveQtModules = false;
for (QString c : wizard->qtModules().split(' ')) {
if (c.isEmpty())
continue;
c[0] = c[0].toUpper();
libs += " Qt5::" + c;
components += " " + c;
haveQtModules = true;
}
if (haveQtModules) {
libs += ")\n";
components += " REQUIRED)";
} else {
libs.clear();
components.clear();
}
GeneratedFile generatedProFile(projectFileName);
generatedProFile.setAttributes(Core::GeneratedFile::OpenProjectAttribute);
generatedProFile.setContents(
"# Created by and for " + QLatin1String(Core::Constants::IDE_DISPLAY_NAME)
+ " This file was created for editing the project sources only.\n"
"# You may attempt to use it for building too, by modifying this file here.\n\n"
"cmake_minimum_required(VERSION 3.5)\n"
"project("+ projectName +")\n\n"
"set(CMAKE_INCLUDE_CURRENT_DIR ON)\n"
"set(CMAKE_AUTOUIC ON)\n"
"set(CMAKE_AUTOMOC ON)\n"
"set(CMAKE_AUTORCC ON)\n"
"set(CMAKE_CXX_STANDARD 11)\n"
"set(CMAKE_CXX_STANDARD_REQUIRED ON)\n"
+ components + "\n\n"
+ includes + "\n\n"
+ srcs + "\n\n"
"add_executable(${CMAKE_PROJECT_NAME} ${SRCS})\n\n"
+ libs
);
return GeneratedFiles{generatedProFile};
}
GeneratedFiles SimpleProjectWizard::generateFiles(const QWizard *w,
QString *errorMessage) const
{
Q_UNUSED(errorMessage)
auto wizard = qobject_cast<const SimpleProjectWizardDialog *>(w);
if (wizard->buildSystem() == "qmake")
return generateQmakeFiles(wizard, errorMessage);
else if (wizard->buildSystem() == "cmake")
return generateCmakeFiles(wizard, errorMessage);
if (errorMessage)
*errorMessage = tr("Unknown build system \"%1\"").arg(wizard->buildSystem());
return {};
}
bool SimpleProjectWizard::postGenerateFiles(const QWizard *w, const GeneratedFiles &l, bool SimpleProjectWizard::postGenerateFiles(const QWizard *w, const GeneratedFiles &l,
QString *errorMessage) const QString *errorMessage) const
{ {