aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDoris Verria <doris.verria@qt.io>2024-10-08 18:05:38 +0200
committerDoris Verria <doris.verria@qt.io>2024-10-09 17:52:33 +0200
commit95f876e77df236f09ce8998bc285045c4a5cfc0e (patch)
tree4c3695935dfa630123278c27d2c4758a55ed94ee
parent160bcd5f11ed9f21d01798c48398919ab9464750 (diff)
StyleGenerator: Generate CMakeLists.txt filewip/figmastyle
By generating a CMake module for the style, you can directly use the style in a CMake project by adding add_subdirectory($styleFolder). Add some documentation in the howTo.txt so instructions are shown when running the tool. Change-Id: Ic90015f3b350ee662c07b58af6e902b0f036b87b Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
-rw-r--r--tools/qqcstylegenerator/howto.txt14
-rw-r--r--tools/qqcstylegenerator/src/stylegenerator.h78
2 files changed, 90 insertions, 2 deletions
diff --git a/tools/qqcstylegenerator/howto.txt b/tools/qqcstylegenerator/howto.txt
index 33ccf1b2c9..ed4206af20 100644
--- a/tools/qqcstylegenerator/howto.txt
+++ b/tools/qqcstylegenerator/howto.txt
@@ -1,11 +1,21 @@
The name of this style will be <i>@styleName@</I>, and the import path will be <I>@importPath@</i>.
-If you assign the import path to the environment variable <i>QML_IMPORT_PATH</i>, an application can use this style by for example launching it with the name of the style as argument:
+You can use the style in your project in several ways:
+
+1. You can add the style folder to your CMake project and add the following to your project's CMakeLists.txt
+
+<code>add_subdirectory(@styleName@)</code>
+
+Then you can use compile time selection of the style by importing the style from QML using:
+
+<code>import @styleName@</code>
+
+2. If you assign the import path to the environment variable <i>QML_IMPORT_PATH</i>, an application can use this style by for example launching it with the name of the style as argument:
<code>export QML_IMPORT_PATH=@importPath@
./yourapp -style=@styleName@</code>
-Another alternative is build the style into an application as a resource. The style folder contains a resource file for this, <I>@styleName@.qrc</i>, that you can add to your CMake project, e.g:
+3. Another alternative is to build the style into an application as a resource. The style folder contains a resource file for this, <I>@styleName@.qrc</i>, that you can add to your CMake project, e.g:
<code>qt_add_executable(application main.cpp @importPath@/@styleName@/@styleName@.qrc)</code>
diff --git a/tools/qqcstylegenerator/src/stylegenerator.h b/tools/qqcstylegenerator/src/stylegenerator.h
index 816a59f6a1..f882876431 100644
--- a/tools/qqcstylegenerator/src/stylegenerator.h
+++ b/tools/qqcstylegenerator/src/stylegenerator.h
@@ -105,6 +105,8 @@ public:
generateIndexThemeFile();
if (!m_abort)
generateQrcFile();
+ if (!m_abort)
+ generateCMakeFile();
} catch (std::exception &e) {
error(e.what());
}
@@ -1202,6 +1204,82 @@ private:
progress();
}
+ QString generateCMakeContent(const QString &styleName)
+ {
+ const QString styleTargetDir = QFileInfo(m_bridge->m_targetDirectory).absoluteFilePath();
+
+ QString content;
+ content += "cmake_minimum_required(VERSION 3.16)\n";
+ content += "project(" + styleName + "LANGUAGES CXX)\n\n";
+ content += "set(CMAKE_AUTOMOC ON)\n\n";
+ content += "find_package(Qt6 REQUIRED COMPONENTS Quick QuickControls2)\n\n";
+ content += "set_source_files_properties(Config.qml\n";
+ content += " PROPERTIES\n";
+ content += " QT_QML_SINGLETON_TYPE TRUE\n";
+ content += ")\n\n";
+ content += "qt_add_qml_module(${PROJECT_NAME}\n";
+ content += " URI " + styleName + "\n";
+
+ QStringList qmlFiles;
+ QStringList resourceFiles;
+
+ QDirIterator it(styleTargetDir, QDirIterator::Subdirectories);
+ while (it.hasNext()) {
+ QString filePath = it.next();
+ QFileInfo fileInfo(filePath);
+
+ if (fileInfo.isDir())
+ continue;
+
+ if (fileInfo.isFile()) {
+ QString relativeFilePath = QDir(styleTargetDir).relativeFilePath(filePath);
+
+ if (fileInfo.suffix() == "qml")
+ qmlFiles << relativeFilePath;
+ else
+ resourceFiles << relativeFilePath;
+ }
+ }
+
+ content += " QML_FILES\n";
+ for (const QString& qmlFile : qmlFiles) {
+ content += " " + qmlFile + "\n";
+ }
+
+ content += " RESOURCES\n";
+ for (const QString& resourceFile : resourceFiles) {
+ content += " " + resourceFile + "\n";
+ }
+
+ content += ")\n\n";
+
+ content += "target_link_libraries(${PROJECT_NAME} PRIVATE\n";
+ content += " Qt6::Quick\n";
+ content += " Qt6::QuickControls2\n";
+ content += ")\n\n";
+
+ content += "install(TARGETS ${PROJECT_NAME}\n";
+ content += " RUNTIME DESTINATION \"${CMAKE_INSTALL_BINDIR}/" + styleName + "\"\n";
+ content += " LIBRARY DESTINATION \"${CMAKE_INSTALL_BINDIR}/" + styleName + "\"\n";
+ content += ")\n";
+
+ content += "install(FILES ${CMAKE_CURRENT_BINARY_DIR}/qmldir\n";
+ content += " DESTINATION \"${CMAKE_INSTALL_BINDIR}/" + styleName + "\"\n";
+ content += ")\n";
+
+ return content;
+ }
+
+ void generateCMakeFile()
+ {
+ debug("Generating CMake file");
+ const QString styleName = QFileInfo(m_bridge->m_targetDirectory).fileName();
+
+ QString content = generateCMakeContent(styleName);
+
+ createTextFileInStylefolder("CMakeLists.txt", content);
+ progress();
+ }
void generateIndexThemeFile()
{
if (!m_bridge->m_selectedControls.contains("Icons"))