CMake file generation: Generate main.cpp and main.qml

Task-number: QDS-5254
Change-Id: Ide893002df72adde77ef018482a4bd13c207780f
Reviewed-by: Thomas Hartmann <thomas.hartmann@qt.io>
This commit is contained in:
Tapani Mattila
2021-10-20 17:37:22 +03:00
parent 46d9fa36df
commit 0fc1bb6187
6 changed files with 148 additions and 16 deletions

View File

@@ -38,6 +38,7 @@ add_qtc_plugin(QmlDesigner
designermcumanager.cpp designermcumanager.h
richtexteditordialog.cpp richtexteditordialog.h
editorproxy.cpp editorproxy.h
boilerplate.qrc
EXPLICIT_MOC
components/propertyeditor/propertyeditorvalue.h
components/connectioneditor/connectionviewwidget.h

View File

@@ -0,0 +1,5 @@
<RCC>
<qresource prefix="/boilerplatetemplates">
<file>qmlprojectmaincpp.tpl</file>
</qresource>
</RCC>

View File

@@ -30,9 +30,11 @@
#include <projectexplorer/projectexplorerconstants.h>
#include <projectexplorer/project.h>
#include <projectexplorer/runcontrol.h>
#include <projectexplorer/session.h>
#include <qmlprojectmanager/qmlprojectmanagerconstants.h>
#include <qmlprojectmanager/qmlmainfileaspect.h>
#include <utils/fileutils.h>
@@ -44,21 +46,15 @@
using namespace Utils;
namespace QmlDesigner {
namespace GenerateCmakeLists {
const QDir::Filters FILES_ONLY = QDir::Files;
const QDir::Filters DIRS_ONLY = QDir::Dirs|QDir::Readable|QDir::NoDotAndDotDot;
const char CMAKEFILENAME[] = "CMakeLists.txt";
const char QMLDIRFILENAME[] = "qmldir";
namespace GenerateCmake {
void generateMenuEntry()
{
Core::ActionContainer *buildMenu =
Core::ActionManager::actionContainer(ProjectExplorer::Constants::M_BUILDPROJECT);
const Core::Context projectCntext(QmlProjectManager::Constants::QML_PROJECT_ID);
auto action = new QAction("Generate CMakeLists.txt files");
QObject::connect(action, &QAction::triggered, GenerateCmakeLists::onGenerateCmakeLists);
QObject::connect(action, &QAction::triggered, GenerateCmake::onGenerateCmakeLists);
Core::Command *cmd = Core::ActionManager::registerAction(action, "QmlProject.CreateCMakeLists");
buildMenu->addAction(cmd, ProjectExplorer::Constants::G_BUILD_RUN);
@@ -71,9 +67,33 @@ void generateMenuEntry()
void onGenerateCmakeLists()
{
generateMainCmake(ProjectExplorer::SessionManager::startupProject()->projectDirectory());
FilePath rootDir = ProjectExplorer::SessionManager::startupProject()->projectDirectory();
GenerateCmakeLists::generateMainCmake(rootDir);
GenerateEntryPoints::generateMainCpp(rootDir);
GenerateEntryPoints::generateMainQml(rootDir);
}
bool writeFile(const FilePath &filePath, const QString &fileContent)
{
QFile file(filePath.toString());
file.open(QIODevice::WriteOnly);
QTextStream stream(&file);
stream << fileContent;
file.close();
return true;
}
}
namespace GenerateCmakeLists {
const QDir::Filters FILES_ONLY = QDir::Files;
const QDir::Filters DIRS_ONLY = QDir::Dirs|QDir::Readable|QDir::NoDotAndDotDot;
const char CMAKEFILENAME[] = "CMakeLists.txt";
const char QMLDIRFILENAME[] = "qmldir";
QStringList processDirectory(const FilePath &dir)
{
QStringList moduleNames;
@@ -275,11 +295,7 @@ QStringList getDirectoryTreeResources(const FilePath &dir)
void createCmakeFile(const FilePath &dir, const QString &content)
{
FilePath filePath = dir.pathAppended(CMAKEFILENAME);
QFile cmakeFile(filePath.toString());
cmakeFile.open(QIODevice::WriteOnly);
QTextStream stream(&cmakeFile);
stream << content;
cmakeFile.close();
GenerateCmake::writeFile(filePath, content);
}
bool isFileBlacklisted(const QString &fileName)
@@ -289,4 +305,48 @@ bool isFileBlacklisted(const QString &fileName)
}
}
namespace GenerateEntryPoints {
bool generateEntryPointFiles(const FilePath &dir)
{
bool cppOk = generateMainCpp(dir);
bool qmlOk = generateMainQml(dir);
return cppOk && qmlOk;
}
const char MAIN_CPPFILE_CONTENT[] = ":/boilerplatetemplates/qmlprojectmaincpp.tpl";
const char MAIN_CPPFILE_NAME[] = "main.cpp";
bool generateMainCpp(const FilePath &dir)
{
QFile templatefile(MAIN_CPPFILE_CONTENT);
templatefile.open(QIODevice::ReadOnly);
QTextStream stream(&templatefile);
QString content = stream.readAll();
templatefile.close();
FilePath filePath = dir.pathAppended(MAIN_CPPFILE_NAME);
return GenerateCmake::writeFile(filePath, content);
}
const char MAIN_QMLFILE_CONTENT[] = "import %1Qml\n\n%2 {\n}\n";
const char MAIN_QMLFILE_NAME[] = "main.qml";
bool generateMainQml(const FilePath &dir)
{
FilePath filePath = dir.pathAppended(MAIN_QMLFILE_NAME);
QString projectName = ProjectExplorer::SessionManager::startupProject()->displayName();
ProjectExplorer::RunConfiguration *runConfiguration = ProjectExplorer::SessionManager::startupRunConfiguration();
QString mainClass;
if (const auto aspect = runConfiguration->aspect<QmlProjectManager::QmlMainFileAspect>())
mainClass = FilePath::fromString(aspect->mainScript()).baseName();
return GenerateCmake::writeFile(filePath, QString(MAIN_QMLFILE_CONTENT).arg(projectName).arg(mainClass));
}
}
}

View File

@@ -30,9 +30,12 @@
#include <utils/fileutils.h>
namespace QmlDesigner {
namespace GenerateCmakeLists {
namespace GenerateCmake {
void generateMenuEntry();
void onGenerateCmakeLists();
bool writeFile(const Utils::FilePath &filePath, const QString &fileContent);
}
namespace GenerateCmakeLists {
void generateMainCmake(const Utils::FilePath &rootDir);
void generateSubdirCmake(const Utils::FilePath &dir);
QString generateModuleCmake(const Utils::FilePath &dir);
@@ -43,4 +46,9 @@ QStringList getDirectoryTreeResources(const Utils::FilePath &dir);
void createCmakeFile(const Utils::FilePath &filePath, const QString &content);
bool isFileBlacklisted(const QString &fileName);
}
namespace GenerateEntryPoints {
bool generateEntryPointFiles(const Utils::FilePath &dir);
bool generateMainCpp(const Utils::FilePath &dir);
bool generateMainQml(const Utils::FilePath &dir);
}
}

View File

@@ -223,7 +223,7 @@ bool QmlDesignerPlugin::initialize(const QStringList & /*arguments*/, QString *e
if (DesignerSettings::getValue(DesignerSettingsKey::STANDALONE_MODE).toBool())
GenerateResource::generateMenuEntry();
GenerateCmakeLists::generateMenuEntry();
GenerateCmake::generateMenuEntry();
const QString fontPath
= Core::ICore::resourcePath(

View File

@@ -0,0 +1,58 @@
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Quick Studio Components.
**
** $QT_BEGIN_LICENSE:GPL$
** 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 The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QGuiApplication>
#include <QQmlApplicationEngine>
int main(int argc, char *argv[])
{
qputenv("QT_LOGGING_RULES", "qt.qml.connections=false");
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
const QUrl url(u"qrc:Main/main.qml"_qs);
QObject::connect(
&engine, &QQmlApplicationEngine::objectCreated, &app,
[url](QObject *obj, const QUrl &objUrl) {
if (!obj && url == objUrl)
QCoreApplication::exit(-1);
},
Qt::QueuedConnection);
engine.addImportPath(QCoreApplication::applicationDirPath() + "/qml");
engine.addImportPath(":/");
engine.load(url);
if (engine.rootObjects().isEmpty()) {
return -1;
}
return app.exec();
}