From 6ad4761604ed05acb04e8fbd9a90503876bf9699 Mon Sep 17 00:00:00 2001 From: Alessandro Portale Date: Fri, 7 Jul 2023 15:07:03 +0200 Subject: [PATCH] Vcpkg: Prevent user from re-inserting a preexisting dependency If the selected package already exists as dependency in the project, the dialog shows an info label and prevents re-adding of the package bz disabling the OK button. Task-number: QTCREATORBUG-29333 Change-Id: Icd368bb2b0bde72ebe5efaea6a81e3cf96830ce3 Reviewed-by: Cristian Adam --- src/plugins/vcpkg/vcpkgmanifesteditor.cpp | 13 +++++--- src/plugins/vcpkg/vcpkgsearch.cpp | 40 ++++++++++++++++++----- src/plugins/vcpkg/vcpkgsearch.h | 3 +- 3 files changed, 42 insertions(+), 14 deletions(-) diff --git a/src/plugins/vcpkg/vcpkgmanifesteditor.cpp b/src/plugins/vcpkg/vcpkgmanifesteditor.cpp index 0335f39af71..e2d3ab66554 100644 --- a/src/plugins/vcpkg/vcpkgmanifesteditor.cpp +++ b/src/plugins/vcpkg/vcpkgmanifesteditor.cpp @@ -99,7 +99,8 @@ public: Utils::Theme::IconsBaseColor}}).icon(); m_searchPkgAction = toolBar()->addAction(vcpkgIcon, Tr::tr("Add vcpkg package...")); connect(m_searchPkgAction, &QAction::triggered, this, [this] { - const Search::VcpkgManifest package = Search::showVcpkgPackageSearchDialog(); + const Search::VcpkgManifest package = + Search::showVcpkgPackageSearchDialog(documentToManifest()); if (!package.name.isEmpty()) { const QByteArray modifiedDocument = addDependencyToManifest(textDocument()->contents(), package.name); @@ -111,10 +112,7 @@ public: Utils::Theme::IconsBaseColor}}).icon(); m_cmakeCodeAction = toolBar()->addAction(cmakeIcon, Tr::tr("CMake code...")); connect(m_cmakeCodeAction, &QAction::triggered, this, [this] { - bool ok = false; - const Search::VcpkgManifest manifest = - Search::parseVcpkgManifest(textDocument()->contents(), &ok); - CMakeCodeDialog dlg(manifest.dependencies); + CMakeCodeDialog dlg(documentToManifest().dependencies); dlg.exec(); }); @@ -138,6 +136,11 @@ public: } private: + Search::VcpkgManifest documentToManifest() const + { + return Search::parseVcpkgManifest(textDocument()->contents()); + } + QAction *m_searchPkgAction; QAction *m_cmakeCodeAction; }; diff --git a/src/plugins/vcpkg/vcpkgsearch.cpp b/src/plugins/vcpkg/vcpkgsearch.cpp index 175e889a888..ddf42bd8a61 100644 --- a/src/plugins/vcpkg/vcpkgsearch.cpp +++ b/src/plugins/vcpkg/vcpkgsearch.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include @@ -28,17 +29,20 @@ namespace Vcpkg::Internal::Search { class VcpkgPackageSearchDialog : public QDialog { public: - explicit VcpkgPackageSearchDialog(QWidget *parent); + explicit VcpkgPackageSearchDialog(const VcpkgManifest &preexistingPackages, QWidget *parent); VcpkgManifest selectedPackage() const; private: void listPackages(const QString &filter); void showPackageDetails(const QString &packageName); + void updateStatus(); VcpkgManifests m_allPackages; VcpkgManifest m_selectedPackage; + const VcpkgManifest m_projectManifest; + FancyLineEdit *m_packagesFilter; ListWidget *m_packagesList; QLineEdit *m_vcpkgName; @@ -46,11 +50,14 @@ private: QLabel *m_vcpkgLicense; QTextBrowser *m_vcpkgDescription; QLabel *m_vcpkgHomepage; + InfoLabel *m_infoLabel; QDialogButtonBox *m_buttonBox; }; -VcpkgPackageSearchDialog::VcpkgPackageSearchDialog(QWidget *parent) +VcpkgPackageSearchDialog::VcpkgPackageSearchDialog(const VcpkgManifest &preexistingPackages, + QWidget *parent) : QDialog(parent) + , m_projectManifest(preexistingPackages) { resize(920, 400); setWindowTitle(Tr::tr("Add vcpkg package")); @@ -75,7 +82,11 @@ VcpkgPackageSearchDialog::VcpkgPackageSearchDialog(QWidget *parent) m_vcpkgHomepage->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Preferred); m_vcpkgHomepage->setTextInteractionFlags(Qt::TextBrowserInteraction); - m_buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Close); + m_infoLabel = new InfoLabel(Tr::tr("This package is already a project dependency."), + InfoLabel::Information); + m_infoLabel->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred); + + m_buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); using namespace Layouting; Column { @@ -96,12 +107,13 @@ VcpkgPackageSearchDialog::VcpkgPackageSearchDialog(QWidget *parent) }, } }, - m_buttonBox, + Row { m_infoLabel, m_buttonBox }, }.attachTo(this); m_allPackages = vcpkgManifests(settings().vcpkgRoot()); listPackages({}); + updateStatus(); connect(m_packagesFilter, &FancyLineEdit::filterChanged, this, &VcpkgPackageSearchDialog::listPackages); @@ -151,7 +163,16 @@ void VcpkgPackageSearchDialog::showPackageDetails(const QString &packageName) .arg(manifest.homepage.toDisplayString())); m_selectedPackage = manifest; - m_buttonBox->button(QDialogButtonBox::Ok)->setEnabled(!manifest.name.isEmpty()); + updateStatus(); +} + +void VcpkgPackageSearchDialog::updateStatus() +{ + const QString package = selectedPackage().name; + const bool isProjectDependency = m_projectManifest.dependencies.contains(package); + m_infoLabel->setVisible(isProjectDependency); + m_buttonBox->button(QDialogButtonBox::Ok)->setEnabled(!(package.isEmpty() + || isProjectDependency)); } VcpkgManifest parseVcpkgManifest(const QByteArray &vcpkgManifestJsonData, bool *ok) @@ -219,10 +240,13 @@ VcpkgManifests vcpkgManifests(const FilePath &vcpkgRoot) return result; } -VcpkgManifest showVcpkgPackageSearchDialog(QWidget *parent) +VcpkgManifest showVcpkgPackageSearchDialog(const VcpkgManifest &projectManifest, QWidget *parent) { - VcpkgPackageSearchDialog dlg(parent ? parent : Core::ICore::dialogParent()); - return (dlg.exec() == QDialog::Accepted) ? dlg.selectedPackage() : VcpkgManifest(); + QWidget *dlgParent = parent ? parent : Core::ICore::dialogParent(); + VcpkgPackageSearchDialog dlg(projectManifest, dlgParent); + const VcpkgManifest result = (dlg.exec() == QDialog::Accepted) ? dlg.selectedPackage() + : VcpkgManifest(); + return result; } } // namespace Vcpkg::Internal::Search diff --git a/src/plugins/vcpkg/vcpkgsearch.h b/src/plugins/vcpkg/vcpkgsearch.h index 2f9242257e6..912dff00b6a 100644 --- a/src/plugins/vcpkg/vcpkgsearch.h +++ b/src/plugins/vcpkg/vcpkgsearch.h @@ -25,6 +25,7 @@ using VcpkgManifests = QList; VcpkgManifest parseVcpkgManifest(const QByteArray &vcpkgManifestJsonData, bool *ok = nullptr); VcpkgManifests vcpkgManifests(const Utils::FilePath &vcpkgRoot); -VcpkgManifest showVcpkgPackageSearchDialog(QWidget *parent = nullptr); +VcpkgManifest showVcpkgPackageSearchDialog(const VcpkgManifest &projectManifest, + QWidget *parent = nullptr); } // namespace Vcpkg::Internal::Search