forked from qt-creator/qt-creator
KitChooser: Change kit pre-selection strategy
In case there is a startup project, it will be listed in an additional entry at the top of the kit list, followed by a separator. Kits are remembered by Id now, not by index anymore *unless* it's the startup kit entry, i.e. if the startup entry is selected and the project is changed, it will again select the startup entry, possibly indicatin another kit. In case a non-startup entry is selected, the kit will be remembered by id, changing projects will try to re-select *that* kit. Change-Id: I827d2670d9a8681570d9c67405cd662cf6a01b4c Reviewed-by: Orgad Shaneh <orgads@gmail.com>
This commit is contained in:
@@ -28,7 +28,10 @@
|
||||
#include "kitconfigwidget.h"
|
||||
#include "kitinformation.h"
|
||||
#include "kitmanager.h"
|
||||
#include "project.h"
|
||||
#include "projectexplorerconstants.h"
|
||||
#include "session.h"
|
||||
#include "target.h"
|
||||
|
||||
#include <coreplugin/icore.h>
|
||||
|
||||
@@ -37,6 +40,8 @@
|
||||
#include <QPushButton>
|
||||
#include <QSettings>
|
||||
|
||||
using namespace Core;
|
||||
|
||||
namespace ProjectExplorer {
|
||||
|
||||
const char lastKitKey[] = "LastSelectedKit";
|
||||
@@ -58,7 +63,7 @@ KitChooser::KitChooser(QWidget *parent) :
|
||||
connect(m_chooser, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
|
||||
this, &KitChooser::onCurrentIndexChanged);
|
||||
connect(m_chooser, static_cast<void (QComboBox::*)(int)>(&QComboBox::activated),
|
||||
this, &KitChooser::activated);
|
||||
this, &KitChooser::onActivated);
|
||||
connect(m_manageButton, &QAbstractButton::clicked, this, &KitChooser::onManageButtonClicked);
|
||||
connect(KitManager::instance(), &KitManager::kitsChanged, this, &KitChooser::populate);
|
||||
}
|
||||
@@ -68,13 +73,22 @@ void KitChooser::onManageButtonClicked()
|
||||
Core::ICore::showOptionsDialog(Constants::KITS_SETTINGS_PAGE_ID, this);
|
||||
}
|
||||
|
||||
void KitChooser::onCurrentIndexChanged(int index)
|
||||
void KitChooser::onCurrentIndexChanged()
|
||||
{
|
||||
if (Kit *kit = kitAt(index))
|
||||
setToolTip(kitToolTip(kit));
|
||||
else
|
||||
setToolTip(QString());
|
||||
emit currentIndexChanged(index);
|
||||
const Id id = Id::fromSetting(m_chooser->currentData());
|
||||
Kit *kit = KitManager::kit(id);
|
||||
setToolTip(kit ? kitToolTip(kit) : QString());
|
||||
emit currentIndexChanged();
|
||||
}
|
||||
|
||||
void KitChooser::onActivated()
|
||||
{
|
||||
// Active user interaction.
|
||||
Id id = Id::fromSetting(m_chooser->currentData());
|
||||
if (m_hasStartupKit && m_chooser->currentIndex() == 0)
|
||||
id = Id(); // Special value to indicate startup kit.
|
||||
ICore::settings()->setValue(lastKitKey, id.toSetting());
|
||||
emit activated();
|
||||
}
|
||||
|
||||
QString KitChooser::kitText(const Kit *k) const
|
||||
@@ -90,17 +104,39 @@ QString KitChooser::kitToolTip(Kit *k) const
|
||||
void KitChooser::populate()
|
||||
{
|
||||
m_chooser->clear();
|
||||
|
||||
const Id lastKit = Id::fromSetting(ICore::settings()->value(lastKitKey));
|
||||
bool didActivate = false;
|
||||
|
||||
if (Project *project = SessionManager::startupProject()) {
|
||||
if (Target *target = project->activeTarget()) {
|
||||
Kit *kit = target->kit();
|
||||
if (m_kitPredicate(kit)) {
|
||||
QString display = tr("Kit of Active Project: %1").arg(kitText(kit));
|
||||
m_chooser->addItem(display, kit->id().toSetting());
|
||||
m_chooser->setItemData(0, kitToolTip(kit), Qt::ToolTipRole);
|
||||
if (!lastKit.isValid()) {
|
||||
m_chooser->setCurrentIndex(0);
|
||||
didActivate = true;
|
||||
}
|
||||
m_chooser->insertSeparator(1);
|
||||
m_hasStartupKit = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach (Kit *kit, KitManager::sortKits(KitManager::kits())) {
|
||||
if (m_kitPredicate(kit)) {
|
||||
m_chooser->addItem(kitText(kit), qVariantFromValue(kit->id()));
|
||||
m_chooser->addItem(kitText(kit), kit->id().toSetting());
|
||||
m_chooser->setItemData(m_chooser->count() - 1, kitToolTip(kit), Qt::ToolTipRole);
|
||||
if (!didActivate && kit->id() == lastKit) {
|
||||
m_chooser->setCurrentIndex(m_chooser->count() - 1);
|
||||
didActivate = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const int n = m_chooser->count();
|
||||
const int index = Core::ICore::settings()->value(QLatin1String(lastKitKey)).toInt();
|
||||
if (0 <= index && index < n)
|
||||
m_chooser->setCurrentIndex(index);
|
||||
m_chooser->setEnabled(n > 1);
|
||||
|
||||
if (n > 1)
|
||||
@@ -112,15 +148,15 @@ void KitChooser::populate()
|
||||
|
||||
Kit *KitChooser::currentKit() const
|
||||
{
|
||||
const int index = m_chooser->currentIndex();
|
||||
Core::ICore::settings()->setValue(QLatin1String(lastKitKey), index);
|
||||
return index == -1 ? nullptr : kitAt(index);
|
||||
const Id id = Id::fromSetting(m_chooser->currentData());
|
||||
return KitManager::kit(id);
|
||||
}
|
||||
|
||||
void KitChooser::setCurrentKitId(Core::Id id)
|
||||
{
|
||||
QVariant v = id.toSetting();
|
||||
for (int i = 0, n = m_chooser->count(); i != n; ++i) {
|
||||
if (kitAt(i)->id() == id) {
|
||||
if (m_chooser->itemData(i) == v) {
|
||||
m_chooser->setCurrentIndex(i);
|
||||
break;
|
||||
}
|
||||
@@ -139,10 +175,4 @@ void KitChooser::setKitPredicate(const Kit::Predicate &predicate)
|
||||
populate();
|
||||
}
|
||||
|
||||
Kit *KitChooser::kitAt(int index) const
|
||||
{
|
||||
auto id = qvariant_cast<Core::Id>(m_chooser->itemData(index));
|
||||
return KitManager::kit(id);
|
||||
}
|
||||
|
||||
} // namespace ProjectExplorer
|
||||
|
@@ -58,8 +58,8 @@ public:
|
||||
Kit *currentKit() const;
|
||||
|
||||
signals:
|
||||
void currentIndexChanged(int);
|
||||
void activated(int);
|
||||
void currentIndexChanged();
|
||||
void activated();
|
||||
|
||||
public slots:
|
||||
void populate();
|
||||
@@ -69,13 +69,14 @@ protected:
|
||||
virtual QString kitToolTip(Kit *k) const;
|
||||
|
||||
private:
|
||||
void onCurrentIndexChanged(int index);
|
||||
void onActivated();
|
||||
void onCurrentIndexChanged();
|
||||
void onManageButtonClicked();
|
||||
Kit *kitAt(int index) const;
|
||||
|
||||
Kit::Predicate m_kitPredicate;
|
||||
QComboBox *m_chooser;
|
||||
QPushButton *m_manageButton;
|
||||
bool m_hasStartupKit = false;
|
||||
};
|
||||
|
||||
} // namespace ProjectExplorer
|
||||
|
Reference in New Issue
Block a user