Guard: Make it possible to lock/unlock manually

Use it in some KitAspectWidget subclasses.

Change-Id: Ie683d5af6a44d0042456418af729a3d718396803
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
Jarek Kobus
2022-07-22 11:54:45 +02:00
parent 3f05594db1
commit 063efbe693
3 changed files with 25 additions and 8 deletions

View File

@@ -97,6 +97,17 @@ bool Guard::isLocked() const
return m_lockCount; return m_lockCount;
} }
void Guard::lock()
{
++m_lockCount;
}
void Guard::unlock()
{
QTC_CHECK(m_lockCount > 0);
--m_lockCount;
}
GuardLocker::GuardLocker(Guard &guard) GuardLocker::GuardLocker(Guard &guard)
: m_guard(guard) : m_guard(guard)
{ {

View File

@@ -38,6 +38,12 @@ public:
Guard(); Guard();
~Guard(); ~Guard();
bool isLocked() const; bool isLocked() const;
// Prefer using GuardLocker when possible. These two methods are provided only for cases
// when locking and unlocking are done in separate methods, so that GuardLocker can't be
// used.
void lock();
void unlock();
private: private:
int m_lockCount = 0; int m_lockCount = 0;
friend class GuardLocker; friend class GuardLocker;

View File

@@ -946,23 +946,23 @@ private:
void modelAboutToReset() void modelAboutToReset()
{ {
m_selectedId = m_model->deviceId(m_comboBox->currentIndex()); m_selectedId = m_model->deviceId(m_comboBox->currentIndex());
m_ignoreChange = true; m_ignoreChanges.lock();
} }
void modelReset() void modelReset()
{ {
m_comboBox->setCurrentIndex(m_model->indexForId(m_selectedId)); m_comboBox->setCurrentIndex(m_model->indexForId(m_selectedId));
m_ignoreChange = false; m_ignoreChanges.unlock();
} }
void currentDeviceChanged() void currentDeviceChanged()
{ {
if (m_ignoreChange) if (m_ignoreChanges.isLocked())
return; return;
DeviceKitAspect::setDeviceId(m_kit, m_model->deviceId(m_comboBox->currentIndex())); DeviceKitAspect::setDeviceId(m_kit, m_model->deviceId(m_comboBox->currentIndex()));
} }
bool m_ignoreChange = false; Guard m_ignoreChanges;
QComboBox *m_comboBox; QComboBox *m_comboBox;
QWidget *m_manageButton; QWidget *m_manageButton;
DeviceManagerModel *m_model; DeviceManagerModel *m_model;
@@ -1218,23 +1218,23 @@ private:
void modelAboutToReset() void modelAboutToReset()
{ {
m_selectedId = m_model->deviceId(m_comboBox->currentIndex()); m_selectedId = m_model->deviceId(m_comboBox->currentIndex());
m_ignoreChange = true; m_ignoreChanges.lock();
} }
void modelReset() void modelReset()
{ {
m_comboBox->setCurrentIndex(m_model->indexForId(m_selectedId)); m_comboBox->setCurrentIndex(m_model->indexForId(m_selectedId));
m_ignoreChange = false; m_ignoreChanges.unlock();
} }
void currentDeviceChanged() void currentDeviceChanged()
{ {
if (m_ignoreChange) if (m_ignoreChanges.isLocked())
return; return;
BuildDeviceKitAspect::setDeviceId(m_kit, m_model->deviceId(m_comboBox->currentIndex())); BuildDeviceKitAspect::setDeviceId(m_kit, m_model->deviceId(m_comboBox->currentIndex()));
} }
bool m_ignoreChange = false; Guard m_ignoreChanges;
QComboBox *m_comboBox; QComboBox *m_comboBox;
QWidget *m_manageButton; QWidget *m_manageButton;
DeviceManagerModel *m_model; DeviceManagerModel *m_model;