File: QCMakePresetItemModel.cxx

package info (click to toggle)
cmake 4.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 147,412 kB
  • sloc: ansic: 403,924; cpp: 290,826; sh: 4,091; python: 3,357; yacc: 3,106; lex: 1,189; f90: 532; asm: 471; lisp: 375; cs: 270; java: 266; fortran: 230; perl: 217; objc: 215; xml: 198; makefile: 98; javascript: 83; pascal: 63; tcl: 55; php: 25; ruby: 22
file content (150 lines) | stat: -rw-r--r-- 4,166 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file LICENSE.rst or https://cmake.org/licensing for details.  */
#include "QCMakePresetItemModel.h"

#include "QCMakeSizeType.h"
#include <QFont>

QCMakePresetItemModel::QCMakePresetItemModel(QObject* parent)
  : QAbstractItemModel(parent)
{
}

QVariant QCMakePresetItemModel::data(QModelIndex const& index, int role) const
{
  switch (role) {
    case Qt::AccessibleDescriptionRole:
      // Separators have to return "separator" for the
      // AccessibleDescriptionRole. This was determined by looking at
      // QComboBoxDelegate::isSeparator() (located in qcombobox_p.h.)
      if (index.internalId() == SEPARATOR_INDEX) {
        return QString("separator");
      }
      return QString{};
    case Qt::DisplayRole: {
      if (index.internalId() == CUSTOM_INDEX) {
        return QString("<custom>");
      }
      if (index.internalId() == SEPARATOR_INDEX) {
        return QVariant{};
      }
      auto const& preset =
        this->m_presets[static_cast<cm_qsizetype>(index.internalId())];
      return preset.displayName.isEmpty() ? preset.name : preset.displayName;
    }
    case Qt::ToolTipRole:
      if (index.internalId() == CUSTOM_INDEX) {
        return QString("Specify all settings manually");
      }
      if (index.internalId() == SEPARATOR_INDEX) {
        return QVariant{};
      }
      return this->m_presets[static_cast<cm_qsizetype>(index.internalId())]
        .description;
    case Qt::UserRole:
      if (index.internalId() == CUSTOM_INDEX) {
        return QVariant{};
      }
      if (index.internalId() == SEPARATOR_INDEX) {
        return QVariant{};
      }
      return QVariant::fromValue(
        this->m_presets[static_cast<cm_qsizetype>(index.internalId())]);
    case Qt::FontRole:
      if (index.internalId() == CUSTOM_INDEX) {
        QFont font;
        font.setItalic(true);
        return font;
      }
      return QFont{};
    default:
      return QVariant{};
  }
}

Qt::ItemFlags QCMakePresetItemModel::flags(QModelIndex const& index) const
{
  Qt::ItemFlags flags =
    Qt::ItemIsEditable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled;
  if (index.internalId() != SEPARATOR_INDEX &&
      (index.internalId() == CUSTOM_INDEX ||
       this->m_presets[static_cast<cm_qsizetype>(index.internalId())]
         .enabled)) {
    flags |= Qt::ItemIsSelectable | Qt::ItemIsEnabled;
  }
  return flags;
}

int QCMakePresetItemModel::rowCount(QModelIndex const& parent) const
{
  if (parent.isValid()) {
    return 0;
  }
  if (this->m_presets.empty()) {
    return 1;
  }
  // NOLINTNEXTLINE(readability-redundant-casting)
  return static_cast<int>(this->m_presets.size() + 2);
}

int QCMakePresetItemModel::columnCount(QModelIndex const& parent) const
{
  if (parent.isValid()) {
    return 0;
  }
  return 1;
}

QModelIndex QCMakePresetItemModel::index(int row, int column,
                                         QModelIndex const& parent) const
{
  if (parent.isValid() || column != 0 || row < 0 ||
      row >= this->rowCount(QModelIndex{})) {
    return QModelIndex{};
  }

  if (this->m_presets.empty() || row == this->m_presets.size() + 1) {
    return this->createIndex(row, column, CUSTOM_INDEX);
  }

  if (row == this->m_presets.size()) {
    return this->createIndex(row, column, SEPARATOR_INDEX);
  }

  return this->createIndex(row, column, static_cast<quintptr>(row));
}

QModelIndex QCMakePresetItemModel::parent(QModelIndex const& /*index*/) const
{
  return QModelIndex{};
}

QVector<QCMakePreset> const& QCMakePresetItemModel::presets() const
{
  return this->m_presets;
}

void QCMakePresetItemModel::setPresets(QVector<QCMakePreset> const& presets)
{
  this->beginResetModel();
  this->m_presets = presets;
  this->endResetModel();
}

int QCMakePresetItemModel::presetNameToRow(QString const& name) const
{
  if (this->m_presets.empty()) {
    return 0;
  }

  int index = 0;
  for (auto const& preset : this->m_presets) {
    if (preset.name == name) {
      return index;
    }
    index++;
  }

  // NOLINTNEXTLINE(readability-redundant-casting)
  return static_cast<int>(this->m_presets.size() + 1);
}