Utils: allow passing data via info bar combo box

Change-Id: I47daa83a533a76eb4ad0ab3f5cf8c04faf389b98
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
David Schulz
2022-03-18 11:38:35 +01:00
parent 1960799be2
commit 2b286c755b
4 changed files with 31 additions and 13 deletions

View File

@@ -103,7 +103,15 @@ void InfoBarEntry::setCancelButtonInfo(const QString &_cancelButtonText, CallBac
m_cancelButtonCallBack = callBack;
}
void InfoBarEntry::setComboInfo(const QStringList &list, InfoBarEntry::ComboCallBack callBack)
void InfoBarEntry::setComboInfo(const QStringList &list, ComboCallBack callBack)
{
m_comboInfo = Utils::transform(list, [](const QString &string) {
return ComboInfo{string, string};
});
m_comboCallBack = callBack;
}
void InfoBarEntry::setComboInfo(const QList<ComboInfo> &list, ComboCallBack callBack)
{
m_comboCallBack = callBack;
m_comboInfo = list;
@@ -308,9 +316,10 @@ void InfoBarDisplay::update()
if (!info.m_comboInfo.isEmpty()) {
auto cb = new QComboBox();
cb->addItems(info.m_comboInfo);
connect(cb, &QComboBox::currentTextChanged, [info](const QString &text) {
info.m_comboCallBack(text);
for (const InfoBarEntry::ComboInfo &comboInfo : qAsConst(info.m_comboInfo))
cb->addItem(comboInfo.displayText, comboInfo.data);
connect(cb, &QComboBox::currentIndexChanged, [cb, info]() {
info.m_comboCallBack({cb->currentText(), cb->currentData()});
});
hbox->addWidget(cb);

View File

@@ -60,8 +60,14 @@ public:
void addCustomButton(const QString &_buttonText, CallBack callBack);
void setCancelButtonInfo(CallBack callBack);
void setCancelButtonInfo(const QString &_cancelButtonText, CallBack callBack);
using ComboCallBack = std::function<void(const QString &)>;
struct ComboInfo
{
QString displayText;
QVariant data;
};
using ComboCallBack = std::function<void(const ComboInfo &)>;
void setComboInfo(const QStringList &list, ComboCallBack callBack);
void setComboInfo(const QList<ComboInfo> &infos, ComboCallBack callBack);
void removeCancelButton();
using DetailsWidgetCreator = std::function<QWidget*()>;
@@ -83,7 +89,7 @@ private:
DetailsWidgetCreator m_detailsWidgetCreator;
bool m_useCancelButton = true;
ComboCallBack m_comboCallBack;
QStringList m_comboInfo;
QList<ComboInfo> m_comboInfo;
friend class InfoBar;
friend class InfoBarDisplay;
};

View File

@@ -143,12 +143,15 @@ void McuSupportPlugin::askUserAboutMcuSupportKitsUpgrade()
Utils::InfoBarEntry info(upgradeMcuSupportKits,
tr("New version of Qt for MCUs detected. Upgrade existing Kits?"),
Utils::InfoBarEntry::GlobalSuppression::Enabled);
static McuKitManager::UpgradeOption selectedOption = McuKitManager::UpgradeOption::Keep;
using McuKitManager::UpgradeOption;
static UpgradeOption selectedOption = UpgradeOption::Keep;
const QStringList options = {tr("Create new kits"), tr("Replace existing kits")};
info.setComboInfo(options, [options](const QString &selected) {
selectedOption = options.indexOf(selected) == 0 ? McuKitManager::UpgradeOption::Keep
: McuKitManager::UpgradeOption::Replace;
const QList<Utils::InfoBarEntry::ComboInfo> infos
= {{tr("Create new kits"), QVariant::fromValue(UpgradeOption::Keep)},
{tr("Replace existing kits"), QVariant::fromValue(UpgradeOption::Replace)}};
info.setComboInfo(infos, [](const Utils::InfoBarEntry::ComboInfo &selected) {
selectedOption = selected.data.value<UpgradeOption>();
});
info.addCustomButton(tr("Proceed"), [upgradeMcuSupportKits] {

View File

@@ -3254,8 +3254,8 @@ void TextEditorWidgetPrivate::updateSyntaxInfoBar(const Highlighter::Definitions
BaseTextEditor::tr("More than one highlight definition was found for this file. "
"Which one should be used to highlight this file?"));
info.setComboInfo(Utils::transform(definitions, &Highlighter::Definition::name),
[this](const QString &definition) {
this->configureGenericHighlighter(Highlighter::definitionForName(definition));
[this](const InfoBarEntry::ComboInfo &info) {
this->configureGenericHighlighter(Highlighter::definitionForName(info.displayText));
});
info.addCustomButton(BaseTextEditor::tr("Remember My Choice"), [multiple, this]() {