DeviceKitAspect: Use base class convenience

Change-Id: Ib6177138b362030cbc8b686eab9a867b10217a28
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
Christian Kandeler
2024-10-11 12:59:52 +02:00
parent db05d348a2
commit da073e938d
3 changed files with 18 additions and 59 deletions

View File

@@ -158,72 +158,25 @@ class DeviceKitAspectImpl final : public KitAspect
{ {
public: public:
DeviceKitAspectImpl(Kit *workingCopy, const KitAspectFactory *factory) DeviceKitAspectImpl(Kit *workingCopy, const KitAspectFactory *factory)
: KitAspect(workingCopy, factory), : KitAspect(workingCopy, factory)
m_comboBox(createSubWidget<QComboBox>()),
m_model(new DeviceManagerModel(DeviceManager::instance()))
{ {
setManagingPage(Constants::DEVICE_SETTINGS_PAGE_ID); setManagingPage(Constants::DEVICE_SETTINGS_PAGE_ID);
m_comboBox->setSizePolicy(QSizePolicy::Preferred,
m_comboBox->sizePolicy().verticalPolicy());
m_comboBox->setModel(m_model);
m_comboBox->setMinimumContentsLength(16); // Don't stretch too much for Kit Page
refresh();
m_comboBox->setToolTip(factory->description());
connect(m_model, &QAbstractItemModel::modelAboutToBeReset, const auto model = new DeviceManagerModel(DeviceManager::instance(), this);
this, &DeviceKitAspectImpl::modelAboutToReset); auto getter = [](const Kit &k) { return DeviceKitAspect::device(&k)->id().toSetting(); };
connect(m_model, &QAbstractItemModel::modelReset, auto setter = [](Kit &k, const QVariant &id) {
this, &DeviceKitAspectImpl::modelReset); DeviceKitAspect::setDeviceId(&k, Id::fromSetting(id));
connect(m_comboBox, &QComboBox::currentIndexChanged, };
this, &DeviceKitAspectImpl::currentDeviceChanged); auto resetModel = [this, model] {
} model->setTypeFilter(DeviceTypeKitAspect::deviceTypeId(kit()));
};
setListAspectSpec({model, std::move(getter), std::move(setter), std::move(resetModel)});
~DeviceKitAspectImpl() override connect(model, &QAbstractItemModel::modelReset, this, &DeviceKitAspectImpl::refresh);
{
delete m_comboBox;
delete m_model;
} }
private: private:
void addToInnerLayout(Layouting::Layout &builder) override
{
addMutableAction(m_comboBox);
builder.addItem(m_comboBox);
}
void makeReadOnly() override { m_comboBox->setEnabled(false); }
Id settingsPageItemToPreselect() const override { return DeviceKitAspect::deviceId(kit()); } Id settingsPageItemToPreselect() const override { return DeviceKitAspect::deviceId(kit()); }
void refresh() override
{
m_model->setTypeFilter(DeviceTypeKitAspect::deviceTypeId(kit()));
m_comboBox->setCurrentIndex(m_model->indexOf(DeviceKitAspect::device(kit())));
}
void modelAboutToReset()
{
m_selectedId = m_model->deviceId(m_comboBox->currentIndex());
m_ignoreChanges.lock();
}
void modelReset()
{
m_comboBox->setCurrentIndex(m_model->indexForId(m_selectedId));
m_ignoreChanges.unlock();
}
void currentDeviceChanged()
{
if (m_ignoreChanges.isLocked())
return;
DeviceKitAspect::setDeviceId(kit(), m_model->deviceId(m_comboBox->currentIndex()));
}
Guard m_ignoreChanges;
QComboBox *m_comboBox;
DeviceManagerModel *m_model;
Id m_selectedId;
}; };
class DeviceKitAspectFactory : public KitAspectFactory class DeviceKitAspectFactory : public KitAspectFactory

View File

@@ -4,6 +4,7 @@
#include "devicemanagermodel.h" #include "devicemanagermodel.h"
#include "devicemanager.h" #include "devicemanager.h"
#include "../kitaspect.h"
#include "../projectexplorertr.h" #include "../projectexplorertr.h"
#include <utils/qtcassert.h> #include <utils/qtcassert.h>
@@ -147,7 +148,8 @@ QVariant DeviceManagerModel::data(const QModelIndex &index, int role) const
switch (role) { switch (role) {
case Qt::DecorationRole: case Qt::DecorationRole:
return dev->deviceStateIcon(); return dev->deviceStateIcon();
case Qt::UserRole: case Qt::UserRole: // TODO: Any callers?
case KitAspect::IdRole:
return dev->id().toSetting(); return dev->id().toSetting();
case Qt::DisplayRole: case Qt::DisplayRole:
if (d->deviceManager->defaultDevice(dev->type()) == dev) if (d->deviceManager->defaultDevice(dev->type()) == dev)

View File

@@ -178,6 +178,10 @@ void KitAspect::setListAspectSpec(ListAspectSpec &&listAspectSpec)
m_listAspectSpec->setter( m_listAspectSpec->setter(
*kit(), m_comboBox->itemData(m_comboBox->currentIndex(), IdRole)); *kit(), m_comboBox->itemData(m_comboBox->currentIndex(), IdRole));
}); });
connect(m_listAspectSpec->model, &QAbstractItemModel::modelAboutToBeReset,
this, [this] { m_ignoreChanges.lock(); });
connect(m_listAspectSpec->model, &QAbstractItemModel::modelReset,
this, [this] { m_ignoreChanges.unlock(); });
} }
void KitAspect::addToLayoutImpl(Layouting::Layout &layout) void KitAspect::addToLayoutImpl(Layouting::Layout &layout)