ProjectExplorer: Start with selected kit

If there's one; when clicking "Manage Kits..." in project mode.

Change-Id: I06a92d8e286f7b2b54844b7f41029d7c64684398
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
This commit is contained in:
hjk
2023-08-18 17:02:13 +02:00
parent 70aad61e97
commit c18de1ecb6
7 changed files with 68 additions and 55 deletions

View File

@@ -188,6 +188,12 @@ KitNode *KitModel::kitNode(const QModelIndex &index)
return (n && n->level() == 2) ? static_cast<KitNode *>(n) : nullptr; return (n && n->level() == 2) ? static_cast<KitNode *>(n) : nullptr;
} }
QModelIndex KitModel::indexOf(Id kitId) const
{
KitNode *n = findItemAtLevel<2>([kitId](KitNode *n) { return n->kit()->id() == kitId; });
return n ? indexForItem(n) : QModelIndex();
}
QModelIndex KitModel::indexOf(Kit *k) const QModelIndex KitModel::indexOf(Kit *k) const
{ {
KitNode *n = findWorkingCopy(k); KitNode *n = findWorkingCopy(k);

View File

@@ -5,6 +5,7 @@
#include "projectexplorer_export.h" #include "projectexplorer_export.h"
#include <utils/id.h>
#include <utils/treemodel.h> #include <utils/treemodel.h>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
@@ -36,6 +37,7 @@ public:
Kit *kit(const QModelIndex &); Kit *kit(const QModelIndex &);
KitNode *kitNode(const QModelIndex &); KitNode *kitNode(const QModelIndex &);
QModelIndex indexOf(Kit *k) const; QModelIndex indexOf(Kit *k) const;
QModelIndex indexOf(Utils::Id kitId) const;
void setDefaultKit(const QModelIndex &index); void setDefaultKit(const QModelIndex &index);
bool isDefaultKit(Kit *k) const; bool isDefaultKit(Kit *k) const;

View File

@@ -11,6 +11,8 @@
#include "kitmanagerconfigwidget.h" #include "kitmanagerconfigwidget.h"
#include "kitmanager.h" #include "kitmanager.h"
#include <coreplugin/dialogs/ioptionspage.h>
#include <utils/qtcassert.h> #include <utils/qtcassert.h>
#include <QHBoxLayout> #include <QHBoxLayout>
@@ -20,15 +22,21 @@
#include <QTreeView> #include <QTreeView>
#include <QVBoxLayout> #include <QVBoxLayout>
namespace ProjectExplorer { using namespace Utils;
namespace Internal {
namespace ProjectExplorer::Internal {
// Page pre-selection
static Id selectedKitId;
void setSelectectKitId(const Utils::Id &kitId)
{
selectedKitId = kitId;
}
// KitOptionsPageWidget // KitOptionsPageWidget
class KitOptionsPageWidget;
static QPointer<KitOptionsPageWidget> kitOptionsPageWidget;
class KitOptionsPageWidget : public Core::IOptionsPageWidget class KitOptionsPageWidget : public Core::IOptionsPageWidget
{ {
public: public:
@@ -44,6 +52,8 @@ public:
void makeDefaultKit(); void makeDefaultKit();
void updateState(); void updateState();
void scrollToSelectedKit();
void apply() final { m_model->apply(); } void apply() final { m_model->apply(); }
public: public:
@@ -62,7 +72,6 @@ public:
KitOptionsPageWidget::KitOptionsPageWidget() KitOptionsPageWidget::KitOptionsPageWidget()
{ {
kitOptionsPageWidget = this;
m_kitsView = new QTreeView(this); m_kitsView = new QTreeView(this);
m_kitsView->setUniformRowHeights(true); m_kitsView->setUniformRowHeights(true);
m_kitsView->header()->setStretchLastSection(true); m_kitsView->header()->setStretchLastSection(true);
@@ -140,9 +149,22 @@ KitOptionsPageWidget::KitOptionsPageWidget()
m_model->updateVisibility(); m_model->updateVisibility();
} }
}); });
scrollToSelectedKit();
updateState(); updateState();
} }
void KitOptionsPageWidget::scrollToSelectedKit()
{
QModelIndex index = m_model->indexOf(selectedKitId);
m_selectionModel->select(index,
QItemSelectionModel::Clear
| QItemSelectionModel::SelectCurrent
| QItemSelectionModel::Rows);
m_kitsView->scrollTo(index);
}
void KitOptionsPageWidget::kitSelectionChanged() void KitOptionsPageWidget::kitSelectionChanged()
{ {
QModelIndex current = currentIndex(); QModelIndex current = currentIndex();
@@ -238,13 +260,12 @@ QModelIndex KitOptionsPageWidget::currentIndex() const
return idxs.at(0); return idxs.at(0);
} }
} // namespace Internal // KitOptionsPage
// -------------------------------------------------------------------------- class KitsSettingsPage : public Core::IOptionsPage
// KitOptionsPage: {
// -------------------------------------------------------------------------- public:
KitsSettingsPage()
KitOptionsPage::KitOptionsPage()
{ {
setId(Constants::KITS_SETTINGS_PAGE_ID); setId(Constants::KITS_SETTINGS_PAGE_ID);
setDisplayName(Tr::tr("Kits")); setDisplayName(Tr::tr("Kits"));
@@ -253,22 +274,8 @@ KitOptionsPage::KitOptionsPage()
setCategoryIconPath(":/projectexplorer/images/settingscategory_kits.png"); setCategoryIconPath(":/projectexplorer/images/settingscategory_kits.png");
setWidgetCreator([] { return new Internal::KitOptionsPageWidget; }); setWidgetCreator([] { return new Internal::KitOptionsPageWidget; });
} }
};
void KitOptionsPage::showKit(Kit *k) const KitsSettingsPage theKitsSettingsPage;
{
if (!k)
return;
Internal::KitOptionsPageWidget *widget = Internal::kitOptionsPageWidget; } // ProjectExplorer::Internal
if (!widget)
return;
QModelIndex index = widget->m_model->indexOf(k);
widget->m_selectionModel->select(index,
QItemSelectionModel::Clear
| QItemSelectionModel::SelectCurrent
| QItemSelectionModel::Rows);
widget->m_kitsView->scrollTo(index);
}
} // namespace ProjectExplorer

View File

@@ -3,20 +3,10 @@
#pragma once #pragma once
#include "projectexplorer_export.h" namespace Utils { class Id; }
#include <coreplugin/dialogs/ioptionspage.h> namespace ProjectExplorer::Internal {
namespace ProjectExplorer { void setSelectectKitId(const Utils::Id &kitId);
class Kit; } // ProjectExplorer::Internal
class PROJECTEXPLORER_EXPORT KitOptionsPage : public Core::IOptionsPage
{
public:
KitOptionsPage();
static void showKit(Kit *k);
};
} // namespace ProjectExplorer

View File

@@ -636,7 +636,6 @@ public:
DesktopDeviceFactory m_desktopDeviceFactory; DesktopDeviceFactory m_desktopDeviceFactory;
ToolChainOptionsPage m_toolChainOptionsPage; ToolChainOptionsPage m_toolChainOptionsPage;
KitOptionsPage m_kitOptionsPage;
TaskHub m_taskHub; TaskHub m_taskHub;

View File

@@ -741,8 +741,17 @@ public:
void handleManageKits() void handleManageKits()
{ {
if (ProjectItem *projectItem = m_projectsModel.rootItem()->childAt(0)) { const QModelIndexList selected = m_selectorTree->selectionModel()->selectedIndexes();
KitOptionsPage::showKit(KitManager::kit(Id::fromSetting(projectItem->data(0, KitIdRole)))); if (!selected.isEmpty()) {
TreeItem *treeItem = m_projectsModel.itemForIndex(selected.front());
while (treeItem) {
const Id kitId = Id::fromSetting(treeItem->data(0, KitIdRole));
if (kitId.isValid()) {
setSelectectKitId(kitId);
break;
}
treeItem = treeItem->parent();
}
} }
ICore::showOptionsDialog(Constants::KITS_SETTINGS_PAGE_ID); ICore::showOptionsDialog(Constants::KITS_SETTINGS_PAGE_ID);
} }

View File

@@ -180,7 +180,7 @@ void TargetSetupWidget::manageKit()
if (!m_kit) if (!m_kit)
return; return;
KitOptionsPage::showKit(m_kit); setSelectectKitId(m_kit->id());
Core::ICore::showOptionsDialog(Constants::KITS_SETTINGS_PAGE_ID, parentWidget()); Core::ICore::showOptionsDialog(Constants::KITS_SETTINGS_PAGE_ID, parentWidget());
} }