From f8f7f47ec3bdc649bfda5d811b2e993078417f2d Mon Sep 17 00:00:00 2001 From: Tapani Mattila Date: Mon, 22 Nov 2021 13:41:20 +0200 Subject: [PATCH] CMake generator: Make only some files selected by default Task-number: QDS-5561 Change-Id: I1a76385f55681ba60bdb43eafdede6697893598f Reviewed-by: Qt CI Bot Reviewed-by: Thomas Hartmann --- src/plugins/qmldesigner/CMakeLists.txt | 2 + .../qmldesigner/checkablefilelistmodel.cpp | 111 ++++++++++++++++++ .../qmldesigner/checkablefilelistmodel.h | 65 ++++++++++ .../qmldesigner/cmakegeneratordialog.cpp | 95 ++++----------- .../qmldesigner/cmakegeneratordialog.h | 30 ++--- .../qmldesigner/generatecmakelists.cpp | 33 ++---- .../qmldesigner/generatecmakelistsconstants.h | 51 ++++++++ src/plugins/qmldesigner/qmldesignerplugin.pri | 3 + src/plugins/qmldesigner/qmldesignerplugin.qbs | 3 + 9 files changed, 277 insertions(+), 116 deletions(-) create mode 100644 src/plugins/qmldesigner/checkablefilelistmodel.cpp create mode 100644 src/plugins/qmldesigner/checkablefilelistmodel.h create mode 100644 src/plugins/qmldesigner/generatecmakelistsconstants.h diff --git a/src/plugins/qmldesigner/CMakeLists.txt b/src/plugins/qmldesigner/CMakeLists.txt index 901a631a421..7bc91483f9e 100644 --- a/src/plugins/qmldesigner/CMakeLists.txt +++ b/src/plugins/qmldesigner/CMakeLists.txt @@ -30,6 +30,8 @@ add_qtc_plugin(QmlDesigner cmakegeneratordialog.h cmakegeneratordialog.cpp generateresource.cpp generateresource.h generatecmakelists.cpp generatecmakelists.h + generatecmakelistsconstants.h + checkablefilelistmodel.cpp checkablefilelistmodel.h openuiqmlfiledialog.cpp openuiqmlfiledialog.h openuiqmlfiledialog.ui qmldesignerconstants.h qmldesignericons.h diff --git a/src/plugins/qmldesigner/checkablefilelistmodel.cpp b/src/plugins/qmldesigner/checkablefilelistmodel.cpp new file mode 100644 index 00000000000..eb8336e9c34 --- /dev/null +++ b/src/plugins/qmldesigner/checkablefilelistmodel.cpp @@ -0,0 +1,111 @@ +/**************************************************************************** +** +** Copyright (C) 2021 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the Qt Design Tooling +** +** 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 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** 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. +** +****************************************************************************/ + +#include "checkablefilelistmodel.h" + +using namespace Utils; + +namespace QmlDesigner { + +CheckableFileListModel::CheckableFileListModel(const FilePath &rootDir, const FilePaths &files, bool checkedByDefault, QObject *parent) + :QStandardItemModel(parent), + rootDir(rootDir) +{ + for (const FilePath &file: files) { + appendRow(new CheckableStandardItem(file.toString(), checkedByDefault)); + } +} + +QList CheckableFileListModel::checkedItems() const +{ +#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) + QList allItems = findItems("*", Qt::MatchWildcard); +#else + QList allItems = findItems(".*", Qt::MatchRegularExpression); +#endif + QList checkedItems; + for (QStandardItem *standardItem : allItems) { + CheckableStandardItem *item = static_cast(standardItem); + if (item->isChecked()) + checkedItems.append(item); + } + + return checkedItems; +} + +QVariant CheckableFileListModel::data(const QModelIndex &index, int role) const +{ + if (index.isValid()) { + if (role == Qt::CheckStateRole) { + CheckableStandardItem *item = static_cast(QStandardItemModel::item(index.row())); + return item->isChecked() ? Qt::Checked : Qt::Unchecked; + } + else if (role == Qt::DisplayRole) { + QVariant data = QStandardItemModel::data(index, role); + FilePath fullPath = FilePath::fromString(data.toString()); + FilePath relativePath = fullPath.relativeChildPath(rootDir); + return QVariant(relativePath.toString()); + } + } + + return QStandardItemModel::data(index, role); +} + +bool CheckableFileListModel::setData(const QModelIndex &index, const QVariant &value, int role) +{ + if (index.isValid() && role == Qt::CheckStateRole) + { + CheckableStandardItem *item = static_cast(QStandardItemModel::item(index.row())); + item->setChecked(value.value()); + + return true; + } + + return QStandardItemModel::setData(index, value, role); +} + +CheckableStandardItem::CheckableStandardItem(const QString &text, bool checked) + :QStandardItem(text), + checked(checked) +{ + setFlags(flags() |= Qt::ItemIsUserCheckable); +} + +void CheckableStandardItem::setChecked(bool checked) +{ + this->checked = checked; +} + +bool CheckableStandardItem::isChecked() const +{ + return this->checked; +} + +int CheckableStandardItem::type() const +{ + return QStandardItem::UserType + 0x74d4f1; +} + +} //QmlDesigner diff --git a/src/plugins/qmldesigner/checkablefilelistmodel.h b/src/plugins/qmldesigner/checkablefilelistmodel.h new file mode 100644 index 00000000000..6721596b635 --- /dev/null +++ b/src/plugins/qmldesigner/checkablefilelistmodel.h @@ -0,0 +1,65 @@ +/**************************************************************************** +** +** Copyright (C) 2021 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the Qt Design Tooling +** +** 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 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** 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. +** +****************************************************************************/ + +#ifndef CHECKABLEFILELISTMODEL_H +#define CHECKABLEFILELISTMODEL_H + +#include +#include + +namespace QmlDesigner { + +class CheckableStandardItem : public QStandardItem +{ +public: + explicit CheckableStandardItem(const QString &text = QString(), bool checked = false); + bool isChecked() const; + void setChecked(bool checked); + int type() const; + +private: + bool checked; +}; + +class CheckableFileListModel : public QStandardItemModel +{ +public: + CheckableFileListModel(const Utils::FilePath &rootDir, + const Utils::FilePaths &files, + bool checkedByDefault = false, + QObject *parent = nullptr); + QVariant data(const QModelIndex &index, int role) const; + bool setData(const QModelIndex &index, const QVariant &value, int role); + QList checkedItems() const; + +protected: + Utils::FilePath rootDir; +}; + +} //QmlDesigner + +Q_DECLARE_METATYPE(QmlDesigner::CheckableStandardItem) + +#endif // CHECKABLEFILELISTMODEL_H diff --git a/src/plugins/qmldesigner/cmakegeneratordialog.cpp b/src/plugins/qmldesigner/cmakegeneratordialog.cpp index 59625c6035f..2f910cf0eb5 100644 --- a/src/plugins/qmldesigner/cmakegeneratordialog.cpp +++ b/src/plugins/qmldesigner/cmakegeneratordialog.cpp @@ -23,8 +23,8 @@ ** ****************************************************************************/ - #include "cmakegeneratordialog.h" +#include "generatecmakelistsconstants.h" #include #include @@ -51,7 +51,7 @@ CmakeGeneratorDialog::CmakeGeneratorDialog(const FilePath &rootDir, const FilePa connect(buttons, &QDialogButtonBox::accepted, this, &QDialog::accept); connect(buttons, &QDialogButtonBox::rejected, this, &QDialog::reject); - model = new CheckableFileListModel(rootDir, files, this); + model = new CMakeGeneratorDialogModel(rootDir, files, this); QListView *list = new QListView(this); list->setModel(model); @@ -72,82 +72,35 @@ FilePaths CmakeGeneratorDialog::getFilePaths() return paths; } -CheckableFileListModel::CheckableFileListModel(const FilePath &rootDir, const FilePaths &files, QObject *parent) - :QStandardItemModel(parent), - rootDir(rootDir) +CMakeGeneratorDialogModel::CMakeGeneratorDialogModel(const Utils::FilePath &rootDir, const Utils::FilePaths &files, QObject *parent) + :CheckableFileListModel(rootDir, files, parent) { - for (const FilePath &file: files) { - appendRow(new CheckableStandardItem(file.toString(), true)); + for (int i=0; i(QStandardItemModel::item(i)); + item->setChecked(CMakeGeneratorDialogModel::checkedByDefault(FilePath::fromString(item->text()))); } } -QList CheckableFileListModel::checkedItems() const +bool CMakeGeneratorDialogModel::checkedByDefault(const FilePath &path) const { -#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) - QList allItems = findItems("*", Qt::MatchWildcard); -#else - QList allItems = findItems(".*", Qt::MatchRegularExpression); -#endif - QList checkedItems; - for (QStandardItem *standardItem : allItems) { - CheckableStandardItem *item = static_cast(standardItem); - if (item->isChecked()) - checkedItems.append(item); + if (path.exists()) { + QString relativePath = path.relativeChildPath(rootDir).toString(); + if (relativePath.compare(QmlDesigner::GenerateCmake::Constants::FILENAME_CMAKELISTS) == 0) + return false; + if (relativePath.endsWith(QmlDesigner::GenerateCmake::Constants::FILENAME_CMAKELISTS) + && relativePath.length() > QString(QmlDesigner::GenerateCmake::Constants::FILENAME_CMAKELISTS).length()) + return true; + if (relativePath.compare(QmlDesigner::GenerateCmake::Constants::FILENAME_MODULES) == 0) + return true; + if (relativePath.compare( + FilePath::fromString(QmlDesigner::GenerateCmake::Constants::DIRNAME_CPP) + .pathAppended(QmlDesigner::GenerateCmake::Constants::FILENAME_MAINCPP_HEADER) + .toString()) + == 0) + return true; } - return checkedItems; -} - -CheckableStandardItem::CheckableStandardItem(const QString &text, bool checked) - :QStandardItem(text), - checked(checked) -{ - setFlags(flags() |= Qt::ItemIsUserCheckable); -} - -void CheckableStandardItem::setChecked(bool checked) -{ - this->checked = checked; -} - -bool CheckableStandardItem::isChecked() const -{ - return this->checked; -} - -int CheckableStandardItem::type() const -{ - return QStandardItem::UserType + 0x74d4f1; -} - -QVariant CheckableFileListModel::data(const QModelIndex &index, int role) const -{ - if (index.isValid()) { - if (role == Qt::CheckStateRole) { - CheckableStandardItem *item = static_cast(QStandardItemModel::item(index.row())); - return item->isChecked() ? Qt::Checked : Qt::Unchecked; - } - else if (role == Qt::DisplayRole) { - QVariant data = QStandardItemModel::data(index, role); - QString relativePath = data.toString().remove(rootDir.toString()); - return QVariant(relativePath); - } - } - - return QStandardItemModel::data(index, role); -} - -bool CheckableFileListModel::setData(const QModelIndex &index, const QVariant &value, int role) -{ - if (index.isValid() && role == Qt::CheckStateRole) - { - CheckableStandardItem *item = static_cast(QStandardItemModel::item(index.row())); - item->setChecked(value.value()); - - return true; - } - - return QStandardItemModel::setData(index, value, role); + return !path.exists(); } } diff --git a/src/plugins/qmldesigner/cmakegeneratordialog.h b/src/plugins/qmldesigner/cmakegeneratordialog.h index 34201365100..8a11666ccb7 100644 --- a/src/plugins/qmldesigner/cmakegeneratordialog.h +++ b/src/plugins/qmldesigner/cmakegeneratordialog.h @@ -27,36 +27,22 @@ #ifndef CMAKEGENERATORDIALOG_H #define CMAKEGENERATORDIALOG_H +#include "checkablefilelistmodel.h" + #include #include -#include + namespace QmlDesigner { namespace GenerateCmake { -class CheckableStandardItem : public QStandardItem +class CMakeGeneratorDialogModel : public CheckableFileListModel { public: - explicit CheckableStandardItem(const QString &text = QString(), bool checked = false); - bool isChecked() const; - void setChecked(bool checked); - int type() const; - -private: - bool checked; -}; - -class CheckableFileListModel : public QStandardItemModel -{ -public: - CheckableFileListModel(const Utils::FilePath &rootDir, const Utils::FilePaths &files, QObject *parent = nullptr); - QVariant data(const QModelIndex &index, int role) const; - bool setData(const QModelIndex &index, const QVariant &value, int role); - QList checkedItems() const; - -private: - Utils::FilePath rootDir; + CMakeGeneratorDialogModel(const Utils::FilePath &rootDir, const Utils::FilePaths &files, QObject *parent = nullptr); +protected: + virtual bool checkedByDefault(const Utils::FilePath &file) const; }; class CmakeGeneratorDialog : public QDialog @@ -72,6 +58,4 @@ private: } } -Q_DECLARE_METATYPE(QmlDesigner::GenerateCmake::CheckableStandardItem) - #endif // CMAKEGENERATORDIALOG_H diff --git a/src/plugins/qmldesigner/generatecmakelists.cpp b/src/plugins/qmldesigner/generatecmakelists.cpp index 52b84d7796d..51c199d5855 100644 --- a/src/plugins/qmldesigner/generatecmakelists.cpp +++ b/src/plugins/qmldesigner/generatecmakelists.cpp @@ -24,6 +24,7 @@ ****************************************************************************/ #include "generatecmakelists.h" +#include "generatecmakelistsconstants.h" #include "cmakegeneratordialog.h" #include @@ -47,6 +48,7 @@ #include using namespace Utils; +using namespace QmlDesigner::GenerateCmake::Constants; namespace QmlDesigner { @@ -118,17 +120,6 @@ bool isErrorFatal(int error) return false; } -const char DIRNAME_CONTENT[] = "content"; -const char DIRNAME_IMPORT[] = "imports"; -const char DIRNAME_CPP[] = "src"; - -const char FILENAME_CMAKELISTS[] = "CMakeLists.txt"; -const char FILENAME_APPMAINQML[] = "App.qml"; -const char FILENAME_MAINQML[] = "main.qml"; -const char FILENAME_MAINCPP[] = "main.cpp"; -const char FILENAME_MAINCPP_HEADER[] = "import_qml_plugins.h"; -const char FILENAME_MODULES[] = "qmlmodules"; - int isProjectCorrectlyFormed(const FilePath &rootDir) { int errors = NoError; @@ -292,8 +283,6 @@ QStringList moduleNames; const QDir::Filters FILES_ONLY = QDir::Files; const QDir::Filters DIRS_ONLY = QDir::Dirs|QDir::Readable|QDir::NoDotAndDotDot; -const char QMLDIRFILENAME[] = "qmldir"; - const char MAIN_CMAKEFILE_TEMPLATE_PATH[] = ":/boilerplatetemplates/qmlprojectmaincmakelists.tpl"; const char QMLMODULES_FILE_TEMPLATE_PATH[] = ":/boilerplatetemplates/qmlprojectmodules.tpl"; @@ -325,7 +314,7 @@ void generateMainCmake(const FilePath &rootDir) modulesAsPlugins.append(" " + moduleName + "plugin\n"); QString moduleFileContent = GenerateCmake::readTemplate(QMLMODULES_FILE_TEMPLATE_PATH).arg(appName).arg(modulesAsPlugins); - GenerateCmake::queueFile(rootDir.pathAppended(GenerateCmake::FILENAME_MODULES), moduleFileContent); + GenerateCmake::queueFile(rootDir.pathAppended(FILENAME_MODULES), moduleFileContent); } const char DO_NOT_EDIT_FILE_COMMENT[] = "### This file is automatically generated by Qt Design Studio.\n### Do not change\n\n"; @@ -353,7 +342,7 @@ const char MODULEFILE_TEMPLATE_PATH[] = ":/boilerplatetemplates/qmlprojectmodule void generateModuleCmake(const FilePath &dir) { QString fileTemplate = GenerateCmake::readTemplate(MODULEFILE_TEMPLATE_PATH); - const QStringList qmldirFilesOnly(QMLDIRFILENAME); + const QStringList qmldirFilesOnly(FILENAME_QMLDIR); QString singletonContent; FilePaths qmldirFileList = dir.dirEntries(qmldirFilesOnly, FILES_ONLY); @@ -467,14 +456,14 @@ QStringList getDirectoryTreeResources(const FilePath &dir) void queueCmakeFile(const FilePath &dir, const QString &content) { - FilePath filePath = dir.pathAppended(GenerateCmake::FILENAME_CMAKELISTS); + FilePath filePath = dir.pathAppended(FILENAME_CMAKELISTS); GenerateCmake::queueFile(filePath, content); } bool isFileBlacklisted(const QString &fileName) { - return (!fileName.compare(QMLDIRFILENAME) || - !fileName.compare(GenerateCmake::FILENAME_CMAKELISTS)); + return (!fileName.compare(FILENAME_QMLDIR) || + !fileName.compare(FILENAME_CMAKELISTS)); } } @@ -494,10 +483,10 @@ const char MAIN_CPPFILE_HEADER_PLUGIN_LINE[] = "Q_IMPORT_QML_PLUGIN(%1)\n"; bool generateMainCpp(const FilePath &dir) { - FilePath srcDir = dir.pathAppended(GenerateCmake::DIRNAME_CPP); + FilePath srcDir = dir.pathAppended(DIRNAME_CPP); QString cppContent = GenerateCmake::readTemplate(MAIN_CPPFILE_TEMPLATE_PATH); - FilePath cppFilePath = srcDir.pathAppended(GenerateCmake::FILENAME_MAINCPP); + FilePath cppFilePath = srcDir.pathAppended(FILENAME_MAINCPP); bool cppOk = GenerateCmake::queueFile(cppFilePath, cppContent); QString modulesAsPlugins; @@ -507,7 +496,7 @@ bool generateMainCpp(const FilePath &dir) QString headerContent = GenerateCmake::readTemplate(MAIN_CPPFILE_HEADER_TEMPLATE_PATH) .arg(modulesAsPlugins); - FilePath headerFilePath = srcDir.pathAppended(GenerateCmake::FILENAME_MAINCPP_HEADER); + FilePath headerFilePath = srcDir.pathAppended(FILENAME_MAINCPP_HEADER); bool headerOk = GenerateCmake::queueFile(headerFilePath, headerContent); return cppOk && headerOk; @@ -518,7 +507,7 @@ const char MAIN_QMLFILE_TEMPLATE_PATH[] = ":/boilerplatetemplates/qmlprojectmain bool generateMainQml(const FilePath &dir) { QString content = GenerateCmake::readTemplate(MAIN_QMLFILE_TEMPLATE_PATH); - FilePath filePath = dir.pathAppended(GenerateCmake::FILENAME_MAINQML); + FilePath filePath = dir.pathAppended(FILENAME_MAINQML); return GenerateCmake::queueFile(filePath, content); } diff --git a/src/plugins/qmldesigner/generatecmakelistsconstants.h b/src/plugins/qmldesigner/generatecmakelistsconstants.h new file mode 100644 index 00000000000..59b11723e4c --- /dev/null +++ b/src/plugins/qmldesigner/generatecmakelistsconstants.h @@ -0,0 +1,51 @@ +/**************************************************************************** +** +** Copyright (C) 2021 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the Qt Design Tooling +** +** 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 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** 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. +** +****************************************************************************/ + +#ifndef GENERATECMAKELISTSCONSTANTS_H +#define GENERATECMAKELISTSCONSTANTS_H + +#pragma once + +namespace QmlDesigner { +namespace GenerateCmake { +namespace Constants { + +const char DIRNAME_CONTENT[] = "content"; +const char DIRNAME_IMPORT[] = "imports"; +const char DIRNAME_CPP[] = "src"; + +const char FILENAME_CMAKELISTS[] = "CMakeLists.txt"; +const char FILENAME_APPMAINQML[] = "App.qml"; +const char FILENAME_MAINQML[] = "main.qml"; +const char FILENAME_MAINCPP[] = "main.cpp"; +const char FILENAME_MAINCPP_HEADER[] = "import_qml_plugins.h"; +const char FILENAME_MODULES[] = "qmlmodules"; +const char FILENAME_QMLDIR[] = "qmldir"; + +} //Constants +} //GenerateCmake +} //QmlDesigner + +#endif // GENERATECMAKELISTSCONSTANTS_H diff --git a/src/plugins/qmldesigner/qmldesignerplugin.pri b/src/plugins/qmldesigner/qmldesignerplugin.pri index cc249ece07a..f994bba8a28 100644 --- a/src/plugins/qmldesigner/qmldesignerplugin.pri +++ b/src/plugins/qmldesigner/qmldesignerplugin.pri @@ -6,6 +6,8 @@ HEADERS += $$PWD/qmldesignerconstants.h \ $$PWD/editorproxy.h \ $$PWD/generateresource.h \ $$PWD/generatecmakelists.h \ + $$PWD/generatecmakelistsconstants.h \ + $$PWD/checkablefilelistmodel.h \ $$PWD/cmakegeneratordialog.h \ $$PWD/settingspage.h \ $$PWD/designmodecontext.h \ @@ -23,6 +25,7 @@ SOURCES += $$PWD/qmldesignerplugin.cpp \ $$PWD/editorproxy.cpp \ $$PWD/generateresource.cpp \ $$PWD/generatecmakelists.cpp \ + $$PWD/checkablefilelistmodel.cpp \ $$PWD/cmakegeneratordialog.cpp \ $$PWD/settingspage.cpp \ $$PWD/designmodecontext.cpp \ diff --git a/src/plugins/qmldesigner/qmldesignerplugin.qbs b/src/plugins/qmldesigner/qmldesignerplugin.qbs index 4e20e2b52ed..df80c0d0665 100644 --- a/src/plugins/qmldesigner/qmldesignerplugin.qbs +++ b/src/plugins/qmldesigner/qmldesignerplugin.qbs @@ -1011,6 +1011,9 @@ Project { "generateresource.h", "generatecmakelists.cpp", "generatecmakelists.h", + "generatecmakelistsconstants.h", + "checkablefilelistmodel.cpp", + "checkablefilelistmodel.h", "cmakegeneratordialog.cpp", "cmakegeneratordialog.h", "designersettings.cpp",