src/model.cpp

changeset 250
2837b549e616
parent 242
16855456992d
child 251
94b0a30a1886
equal deleted inserted replaced
249:37d3c819cafa 250:2837b549e616
45 { 45 {
46 QString result; 46 QString result;
47 if (circ.fraction.divisions != 16) { 47 if (circ.fraction.divisions != 16) {
48 result += QString::number(circ.fraction.divisions) + QStringLiteral("\\"); 48 result += QString::number(circ.fraction.divisions) + QStringLiteral("\\");
49 } 49 }
50 const int factor = gcd(circ.fraction.segments, circ.fraction.divisions); 50 const unsigned int factor = gcd(circ.fraction.segments, circ.fraction.divisions);
51 int num = circ.fraction.segments / factor; 51 unsigned int num = circ.fraction.segments / factor;
52 int denom = circ.fraction.divisions / factor; 52 unsigned int denom = circ.fraction.divisions / factor;
53 if (denom < 4) { 53 if (denom < 4) {
54 num *= 4 / denom; 54 num *= 4 / denom;
55 denom = 4; 55 denom = 4;
56 } 56 }
57 result += QStringLiteral("%1-%2").arg(num).arg(denom); 57 result += QStringLiteral("%1-%2").arg(num).arg(denom);
173 { 173 {
174 } 174 }
175 175
176 ModelId Model::append(const ModelElement &value) 176 ModelId Model::append(const ModelElement &value)
177 { 177 {
178 const int position = static_cast<int>(this->body.size()); 178 const std::size_t position = this->size();
179 const ModelId id = this->runningId; 179 const ModelId id = this->runningId;
180 this->runningId.value += 1; 180 this->runningId.value += 1;
181 Q_EMIT this->beginInsertRows({}, position, position); 181 const int row = narrow<int>(signed_cast(this->size()));
182 Q_EMIT this->beginInsertRows({}, row, row);
182 this->body.push_back({value, id}); 183 this->body.push_back({value, id});
183 this->positions[id] = position; 184 this->positions[id] = position;
184 Q_EMIT this->endInsertRows(); 185 Q_EMIT this->endInsertRows();
185 return id; 186 return id;
186 } 187 }
187 188
188 const ModelElement &Model::at(int position) const 189 const ModelElement &Model::at(std::size_t position) const
189 { 190 {
190 return this->body[position].data; 191 return this->body[position].data;
191 } 192 }
192 193
193 ModelId Model::idAt(int position) const 194 ModelId Model::idAt(std::size_t position) const
194 { 195 {
195 return this->body[position].id; 196 return this->body[position].id;
196 } 197 }
197 198
198 void Model::assignAt(int position, const ModelElement &element) 199 void Model::assignAt(std::size_t position, const ModelElement &element)
199 { 200 {
200 this->body[position].data = element; 201 this->body[position].data = element;
201 const QModelIndex index = this->index(position); 202 const QModelIndex index = this->index(narrow<int>(signed_cast(position)));
202 Q_EMIT this->dataChanged(index, index); 203 Q_EMIT this->dataChanged(index, index);
203 } 204 }
204 205
205 std::optional<int> Model::find(ModelId id) const 206 std::optional<int> Model::find(ModelId id) const
206 { 207 {
214 if (it != map.end()) { 215 if (it != map.end()) {
215 map.erase(it); 216 map.erase(it);
216 } 217 }
217 } 218 }
218 219
219 void Model::remove(int index) 220 void Model::remove(const std::size_t index)
220 { 221 {
221 if (index >= 0 and index < this->size()) { 222 if (index < this->body.size()) {
222 Q_EMIT this->beginRemoveRows({}, index, index); 223 const int row = narrow<int>(signed_cast(index));
224 Q_EMIT this->beginRemoveRows({}, row, row);
223 removeFromMap(this->positions, this->body[index].id); 225 removeFromMap(this->positions, this->body[index].id);
224 this->body.erase(this->body.begin() + index); 226 this->body.erase(this->body.begin() + row);
225 for (int i = index; i < this->size(); ++i) { 227 for (std::size_t i = index; i < this->body.size(); ++i) {
226 this->positions[this->body[i].id] = i; 228 this->positions[this->body[i].id] = i;
227 } 229 }
228 Q_EMIT this->endRemoveRows(); 230 Q_EMIT this->endRemoveRows();
229 } 231 }
230 } 232 }
231 233
232 int Model::rowCount(const QModelIndex &) const 234 int Model::rowCount(const QModelIndex &) const
233 { 235 {
234 return this->size(); 236 return narrow<int>(signed_cast(this->size()));
235 } 237 }
236 238
237 QVariant Model::data(const QModelIndex &index, int role) const 239 QVariant Model::data(const QModelIndex &index, int role) const
238 { 240 {
239 const int i = index.row(); 241 const std::size_t i = unsigned_cast(index.row());
240 const ModelElement& element = this->body[i].data; 242 const ModelElement& element = this->body[i].data;
241 switch(role) 243 switch(role)
242 { 244 {
243 case Qt::DecorationRole: 245 case Qt::DecorationRole:
244 return iconForElement(element); 246 return iconForElement(element);
255 default: 257 default:
256 return {}; 258 return {};
257 } 259 }
258 } 260 }
259 261
260 const ModelElement &Model::operator[](int index) const 262 const ModelElement &Model::operator[](std::size_t index) const
261 { 263 {
262 return this->body[index].data; 264 return this->body[index].data;
263 } 265 }
264 266
265 int Model::size() const 267 std::size_t Model::size() const
266 { 268 {
267 return this->body.size(); 269 return this->body.size();
268 } 270 }
269 271
270 void save(const Model &model, QIODevice *device) 272 void save(const Model &model, QIODevice *device)
271 { 273 {
272 QTextStream out{device}; 274 QTextStream out{device};
273 for (int i = 0; i < model.size(); ++i) { 275 for (std::size_t i = 0; i < model.size(); ++i) {
274 out << modelElementToString(model[i]) << "\r\n"; 276 out << modelElementToString(model[i]) << "\r\n";
275 } 277 }
276 } 278 }
277 279
278 /** 280 /**

mercurial