QCMakePresetComboBox.cxx 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "QCMakePresetComboBox.h"
  4. #include "QCMakePresetItemModel.h"
  5. QCMakePresetComboBox::QCMakePresetComboBox(QWidget* parent)
  6. : QComboBox(parent)
  7. {
  8. this->m_model = new QCMakePresetItemModel(this);
  9. this->setModel(this->m_model);
  10. QObject::connect(this->m_model, &QCMakePresetItemModel::modelAboutToBeReset,
  11. this, [this]() { this->m_resetting = true; });
  12. QObject::connect(this->m_model, &QCMakePresetItemModel::modelReset, this,
  13. [this]() {
  14. this->setPresetName(this->m_lastPreset);
  15. this->m_resetting = false;
  16. this->emitPresetChanged();
  17. });
  18. QObject::connect(
  19. this,
  20. static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
  21. this, [this](int /*row*/) {
  22. if (!this->m_resetting) {
  23. this->emitPresetChanged();
  24. }
  25. });
  26. }
  27. const QVector<QCMakePreset>& QCMakePresetComboBox::presets() const
  28. {
  29. return this->m_model->presets();
  30. }
  31. QString QCMakePresetComboBox::presetName() const
  32. {
  33. auto preset = this->currentData();
  34. if (preset.canConvert<QCMakePreset>()) {
  35. return preset.value<QCMakePreset>().name;
  36. }
  37. return QString{};
  38. }
  39. void QCMakePresetComboBox::setPresets(const QVector<QCMakePreset>& presets)
  40. {
  41. this->m_model->setPresets(presets);
  42. }
  43. void QCMakePresetComboBox::setPresetName(const QString& name)
  44. {
  45. this->setCurrentIndex(this->m_model->presetNameToRow(name));
  46. if (this->signalsBlocked()) {
  47. this->m_lastPreset = this->presetName();
  48. }
  49. }
  50. void QCMakePresetComboBox::emitPresetChanged()
  51. {
  52. if (this->presetName() != this->m_lastPreset) {
  53. emit this->presetChanged(this->presetName());
  54. this->m_lastPreset = this->presetName();
  55. }
  56. }