QCMakePresetItemModel.cxx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 "QCMakePresetItemModel.h"
  4. #include <QFont>
  5. QCMakePresetItemModel::QCMakePresetItemModel(QObject* parent)
  6. : QAbstractItemModel(parent)
  7. {
  8. }
  9. QVariant QCMakePresetItemModel::data(const QModelIndex& index, int role) const
  10. {
  11. switch (role) {
  12. case Qt::AccessibleDescriptionRole:
  13. // Separators have to return "separator" for the
  14. // AccessibleDescriptionRole. This was determined by looking at
  15. // QComboBoxDelegate::isSeparator() (located in qcombobox_p.h.)
  16. if (index.internalId() == SEPARATOR_INDEX) {
  17. return QString::fromLocal8Bit("separator");
  18. }
  19. return QString{};
  20. case Qt::DisplayRole: {
  21. if (index.internalId() == CUSTOM_INDEX) {
  22. return QString::fromLocal8Bit("<custom>");
  23. }
  24. if (index.internalId() == SEPARATOR_INDEX) {
  25. return QVariant{};
  26. }
  27. auto const& preset = this->m_presets[index.internalId()];
  28. return preset.displayName.isEmpty() ? preset.name : preset.displayName;
  29. }
  30. case Qt::ToolTipRole:
  31. if (index.internalId() == CUSTOM_INDEX) {
  32. return QString::fromLocal8Bit("Specify all settings manually");
  33. }
  34. if (index.internalId() == SEPARATOR_INDEX) {
  35. return QVariant{};
  36. }
  37. return this->m_presets[index.internalId()].description;
  38. case Qt::UserRole:
  39. if (index.internalId() == CUSTOM_INDEX) {
  40. return QVariant{};
  41. }
  42. if (index.internalId() == SEPARATOR_INDEX) {
  43. return QVariant{};
  44. }
  45. return QVariant::fromValue(this->m_presets[index.internalId()]);
  46. case Qt::FontRole:
  47. if (index.internalId() == CUSTOM_INDEX) {
  48. QFont font;
  49. font.setItalic(true);
  50. return font;
  51. }
  52. return QFont{};
  53. default:
  54. return QVariant{};
  55. }
  56. }
  57. Qt::ItemFlags QCMakePresetItemModel::flags(const QModelIndex& index) const
  58. {
  59. Qt::ItemFlags flags =
  60. Qt::ItemIsEditable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled;
  61. if (index.internalId() != SEPARATOR_INDEX &&
  62. (index.internalId() == CUSTOM_INDEX ||
  63. this->m_presets[index.internalId()].enabled)) {
  64. flags |= Qt::ItemIsSelectable | Qt::ItemIsEnabled;
  65. }
  66. return flags;
  67. }
  68. int QCMakePresetItemModel::rowCount(const QModelIndex& parent) const
  69. {
  70. if (parent.isValid()) {
  71. return 0;
  72. }
  73. if (this->m_presets.empty()) {
  74. return 1;
  75. }
  76. return this->m_presets.size() + 2;
  77. }
  78. int QCMakePresetItemModel::columnCount(const QModelIndex& parent) const
  79. {
  80. if (parent.isValid()) {
  81. return 0;
  82. }
  83. return 1;
  84. }
  85. QModelIndex QCMakePresetItemModel::index(int row, int column,
  86. const QModelIndex& parent) const
  87. {
  88. if (parent.isValid() || column != 0 || row < 0 ||
  89. row >= this->rowCount(QModelIndex{})) {
  90. return QModelIndex{};
  91. }
  92. if (this->m_presets.empty() || row == this->m_presets.size() + 1) {
  93. return this->createIndex(row, column, CUSTOM_INDEX);
  94. }
  95. if (row == this->m_presets.size()) {
  96. return this->createIndex(row, column, SEPARATOR_INDEX);
  97. }
  98. return this->createIndex(row, column, static_cast<quintptr>(row));
  99. }
  100. QModelIndex QCMakePresetItemModel::parent(const QModelIndex& /*index*/) const
  101. {
  102. return QModelIndex{};
  103. }
  104. QVector<QCMakePreset> const& QCMakePresetItemModel::presets() const
  105. {
  106. return this->m_presets;
  107. }
  108. void QCMakePresetItemModel::setPresets(QVector<QCMakePreset> const& presets)
  109. {
  110. this->beginResetModel();
  111. this->m_presets = presets;
  112. this->endResetModel();
  113. }
  114. int QCMakePresetItemModel::presetNameToRow(const QString& name) const
  115. {
  116. if (this->m_presets.empty()) {
  117. return 0;
  118. }
  119. int index = 0;
  120. for (auto const& preset : this->m_presets) {
  121. if (preset.name == name) {
  122. return index;
  123. }
  124. index++;
  125. }
  126. return this->m_presets.size() + 1;
  127. }