diff options
author | Kyle Edwards <kyle.edwards@kitware.com> | 2020-09-29 14:17:47 (GMT) |
---|---|---|
committer | Kyle Edwards <kyle.edwards@kitware.com> | 2020-10-05 13:49:59 (GMT) |
commit | a4382f72d7fb924f9649ae352d70a36df2d662a8 (patch) | |
tree | 15855284a47a3f3ab4a14f945559266ebabbb0a5 /Source/QtDialog/QCMakePresetComboBox.cxx | |
parent | 8617479061039e2b357b7efc922f1b88648dce42 (diff) | |
download | CMake-a4382f72d7fb924f9649ae352d70a36df2d662a8.zip CMake-a4382f72d7fb924f9649ae352d70a36df2d662a8.tar.gz CMake-a4382f72d7fb924f9649ae352d70a36df2d662a8.tar.bz2 |
CMake GUI: Add presets functionality
Diffstat (limited to 'Source/QtDialog/QCMakePresetComboBox.cxx')
-rw-r--r-- | Source/QtDialog/QCMakePresetComboBox.cxx | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/Source/QtDialog/QCMakePresetComboBox.cxx b/Source/QtDialog/QCMakePresetComboBox.cxx new file mode 100644 index 0000000..efadb73 --- /dev/null +++ b/Source/QtDialog/QCMakePresetComboBox.cxx @@ -0,0 +1,64 @@ +/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying + file Copyright.txt or https://cmake.org/licensing for details. */ +#include "QCMakePresetComboBox.h" + +#include "QCMakePresetItemModel.h" + +QCMakePresetComboBox::QCMakePresetComboBox(QWidget* parent) + : QComboBox(parent) +{ + this->m_model = new QCMakePresetItemModel(this); + this->setModel(this->m_model); + + QObject::connect(this->m_model, &QCMakePresetItemModel::modelAboutToBeReset, + this, [this]() { this->m_resetting = true; }); + QObject::connect(this->m_model, &QCMakePresetItemModel::modelReset, this, + [this]() { + this->setPresetName(this->m_lastPreset); + this->m_resetting = false; + this->emitPresetChanged(); + }); + QObject::connect( + this, + static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), + this, [this](int /*row*/) { + if (!this->m_resetting) { + this->emitPresetChanged(); + } + }); +} + +const QVector<QCMakePreset>& QCMakePresetComboBox::presets() const +{ + return this->m_model->presets(); +} + +QString QCMakePresetComboBox::presetName() const +{ + auto preset = this->currentData(); + if (preset.canConvert<QCMakePreset>()) { + return preset.value<QCMakePreset>().name; + } + return QString{}; +} + +void QCMakePresetComboBox::setPresets(const QVector<QCMakePreset>& presets) +{ + this->m_model->setPresets(presets); +} + +void QCMakePresetComboBox::setPresetName(const QString& name) +{ + this->setCurrentIndex(this->m_model->presetNameToRow(name)); + if (this->signalsBlocked()) { + this->m_lastPreset = this->presetName(); + } +} + +void QCMakePresetComboBox::emitPresetChanged() +{ + if (this->presetName() != this->m_lastPreset) { + emit this->presetChanged(this->presetName()); + this->m_lastPreset = this->presetName(); + } +} |