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 <cristian.adam@qt.io>
This commit is contained in:
Alessandro Portale
2023-07-07 15:07:03 +02:00
parent d9680baf2a
commit 6ad4761604
3 changed files with 42 additions and 14 deletions

View File

@@ -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;
};

View File

@@ -10,6 +10,7 @@
#include <utils/algorithm.h>
#include <utils/fancylineedit.h>
#include <utils/fileutils.h>
#include <utils/infolabel.h>
#include <utils/itemviews.h>
#include <utils/layoutbuilder.h>
@@ -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

View File

@@ -25,6 +25,7 @@ using VcpkgManifests = QList<VcpkgManifest>;
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