Debian Codegraph @

Dekko/utils/QmlVariantListModel.cpp

raw symbols SCIP
1 #include "QmlVariantListModel.h"
2
3 QmlVariantListModel::QmlVariantListModel(QObject *parent) : QAbstractListModel(parent),
4 m_count(0)
5 {
6 }
7
8 QHash<int, QByteArray> QmlVariantListModel::rolenames() const
9 {
10 QHash<int, QByteArray> roles;
11 roles.insert(Qt::UserRole, QByteArrayLiteral("qtVariant"));
12 return roles;
13 }
14
15 QVariant QmlVariantListModel::data(const QModelIndex &index, int role) const
16 {
17 QVariant value;
18 const int row = index.row();
19 if (row >= 0 && row < count() && role == Qt::UserRole) {
20 value = m_values.value(row);
21 }
22 return value;
23 }
24
25 bool QmlVariantListModel::setData(const QModelIndex &index, const QVariant &value, int role)
26 {
27 bool dataSet = false;
28 const int row = index.row();
29 if (row >=0 && row < count()) {
30 m_values.replace(row, value);
31 QModelIndex item = QAbstractListModel::index(row, 0, QModelIndex());
32 emit dataChanged(item, item, QVector<int>(1, role));
33 dataSet = true;
34 }
35 return dataSet;
36 }
37
38 int QmlVariantListModel::rowCount(const QModelIndex &parent) const
39 {
40 Q_UNUSED(parent);
41 return m_values.count();
42 }
43
44 void QmlVariantListModel::append(const QVariant &value)
45 {
46 const int rows = count();
47 beginInsertRows(QModelIndex(), rows, rows);
48 m_values.append(value);
49 endInsertRows();
50 countPossiblyChanged();
51 }
52
53 void QmlVariantListModel::appendList(const QVariantList &values)
54 {
55 if (values.isEmpty()) {
56 return;
57 }
58 const int rows = count();
59 beginInsertRows(QModelIndex(), rows, rows + values.count() - 1);
60 m_values.append(values);
61 endInsertRows();
62 countPossiblyChanged();
63 }
64
65 int QmlVariantListModel::count() const
66 {
67 return rowCount();
68 }
69
70 void QmlVariantListModel::clear()
71 {
72 if (m_values.isEmpty()) {
73 return;
74 }
75 beginRemoveRows(QModelIndex(), 0, rowCount() - 1);
76 m_values.clear();
77 endRemoveRows();
78 countPossiblyChanged();
79 }
80
81 QVariant QmlVariantListModel::get(int index)
82 {
83 QVariant value;
84 if (index >= 0 && index < m_values.size()) {
85 value = m_values.value(index);
86 }
87 return value;
88 }
89
90 bool QmlVariantListModel::isEmpty() const
91 {
92 return m_values.isEmpty();
93 }
94
95 void QmlVariantListModel::insert(int index, const QVariant &value)
96 {
97 beginInsertRows(QModelIndex(), index, index);
98 m_values.insert(index, value);
99 endInsertRows();
100 countPossiblyChanged();
101 }
102
103 void QmlVariantListModel::insertList(int index, const QVariantList &values)
104 {
105 if (values.isEmpty()) {
106 return;
107 }
108 beginInsertRows(QModelIndex(), index, (index + values.count()) - 1);
109 int offset = 0;
110 for(auto value : values) {
111 m_values.insert(index + offset, value);
112 ++offset;
113 }
114 endInsertRows();
115 countPossiblyChanged();
116 }
117
118 QVariantList QmlVariantListModel::list() const
119 {
120 return m_values;
121 }
122
123 void QmlVariantListModel::move(int index, int to)
124 {
125 if (index == to) {
126 // well that's not clever
127 return;
128 }
129 beginMoveRows(QModelIndex(), index, index, QModelIndex(), (index < to ? to + 1 : to));
130 m_values.move(index,to);
131 endMoveRows();
132 countPossiblyChanged();
133 }
134
135 void QmlVariantListModel::prepend(const QVariant &value)
136 {
137 beginInsertColumns(QModelIndex(), 0, 0);
138 m_values.prepend(value);
139 endInsertRows();
140 countPossiblyChanged();
141 }
142
143 void QmlVariantListModel::prependList(const QVariantList &values)
144 {
145 if (values.isEmpty()) {
146 return;
147 }
148 beginInsertRows(QModelIndex(), 0, values.count() - 1);
149 int offset = 0;
150 for (auto value : values) {
151 m_values.insert(offset, value);
152 }
153 endInsertRows();
154 countPossiblyChanged();
155 }
156
157 void QmlVariantListModel::replace(int index, const QVariant &value)
158 {
159 if (index >= 0 && index < count()) {
160 m_values.replace(index, value);
161 QModelIndex item = QAbstractListModel::index(index, 0, QModelIndex());
162 emit dataChanged(item, item, QVector<int>(1, Qt::UserRole));
163 }
164 }
165
166 void QmlVariantListModel::remove(int index)
167 {
168 if (index >= 0 && index < count()) {
169 beginRemoveColumns(QModelIndex(), index, index);
170 m_values.removeAt(index);
171 endRemoveRows();
172 countPossiblyChanged();
173 }
174 }
175
176 void QmlVariantListModel::countPossiblyChanged()
177 {
178 if (m_count != count()) {
179 m_count = count();
180 emit countChanged();
181 }
182 }
183