|
1 | 1 | #include "MainTreeModel.hpp" |
| 2 | +#include "pre/utils/ListUtils.hpp" |
2 | 3 | #include <QModelIndex> |
3 | 4 | #include <QIcon> |
4 | 5 | #include <algorithm> |
@@ -49,7 +50,7 @@ void MainTreeModel::insertMaterial(int row) { |
49 | 50 | QModelIndex parent = createIndex(TopLevelItem::MATERIALS, 0, ItemType::TOPLEVEL); |
50 | 51 | beginInsertRows(parent, row, row); |
51 | 52 |
|
52 | | - auto position = bow->section.materials.begin() + row; |
| 53 | + auto position = std::next(bow->section.materials.begin(), row); |
53 | 54 | bow->section.materials.insert(position, material); |
54 | 55 |
|
55 | 56 | endInsertRows(); |
@@ -80,7 +81,7 @@ void MainTreeModel::insertLayer(int row) { |
80 | 81 |
|
81 | 82 | Layer layer { |
82 | 83 | .name = bow->generateLayerName(), |
83 | | - .material = bow->section.materials.empty() ? "" : bow->section.materials[0].name, |
| 84 | + .material = bow->section.materials.empty() ? "" : bow->section.materials.front().name, |
84 | 85 | .height = {{0.0, 0.01}, {1.0, 0.01}} |
85 | 86 | }; |
86 | 87 |
|
@@ -171,7 +172,7 @@ void MainTreeModel::removeMaterial(int row) { |
171 | 172 |
|
172 | 173 | // Remove material |
173 | 174 | // Layers that refer to the material will become invalid |
174 | | - auto position = bow->section.materials.begin() + row; |
| 175 | + auto position = std::next(bow->section.materials.begin(), row); |
175 | 176 | bow->section.materials.erase(position); |
176 | 177 |
|
177 | 178 | endRemoveRows(); |
@@ -310,13 +311,29 @@ void MainTreeModel::swapMaterials(int i, int j) { |
310 | 311 | throw std::invalid_argument("Invalid material indices for swapping"); |
311 | 312 | } |
312 | 313 |
|
| 314 | + // Indices must be in ascending order |
| 315 | + if(i > j) { |
| 316 | + std::swap(i, j); |
| 317 | + } |
| 318 | + |
313 | 319 | QModelIndex parent = createIndex(TopLevelItem::MATERIALS, 0, ItemType::TOPLEVEL); |
314 | 320 | beginMoveRows(parent, i, i, parent, j); |
315 | 321 | beginMoveRows(parent, j, j, parent, i); |
316 | 322 |
|
317 | 323 | // Swap the two materials |
318 | 324 | // Layers that refer to the materials stay valid since they refer to them by name |
319 | | - std::swap(bow->section.materials[i], bow->section.materials[j]); |
| 325 | + swapListNodes(bow->section.materials, i, j); |
| 326 | + |
| 327 | + /* |
| 328 | + auto it1 = std::next(bow->section.materials.begin(), i); |
| 329 | + auto it2 = std::next(bow->section.materials.begin(), j); |
| 330 | + //std::swap(*it1, *it2); |
| 331 | +
|
| 332 | +
|
| 333 | + auto after2 = std::next(it2); |
| 334 | + bow->section.materials.splice(it1, bow->section.materials, it2); // Move it2 before it1 |
| 335 | + bow->section.materials.splice(after2, bow->section.materials, it1); // Move it1 (now after it2) to afterIt2 |
| 336 | + */ |
320 | 337 |
|
321 | 338 | endMoveRows(); |
322 | 339 | } |
@@ -452,7 +469,7 @@ QVariant MainTreeModel::data(const QModelIndex &index, int role) const { |
452 | 469 | } |
453 | 470 |
|
454 | 471 | if(index.parent().row() == TopLevelItem::MATERIALS) { |
455 | | - auto& material = bow->section.materials[index.row()]; |
| 472 | + auto& material = *std::next(bow->section.materials.begin(), index.row()); |
456 | 473 | switch(role) { |
457 | 474 | case Qt::DisplayRole: case Qt::EditRole: return QString::fromStdString(material.name); |
458 | 475 | case Qt::ToolTipRole: return "User-defined material \"" + QString::fromStdString(material.name) + "\""; |
@@ -496,10 +513,11 @@ bool MainTreeModel::setData(const QModelIndex &index, const QVariant &value, int |
496 | 513 | } |
497 | 514 |
|
498 | 515 | // Rename material |
499 | | - std::string oldName = bow->section.materials[index.row()].name; |
500 | | - bow->section.materials[index.row()].name = newName; |
| 516 | + Material& material = *std::next(bow->section.materials.begin(), index.row()); |
| 517 | + std::string oldName = material.name; |
| 518 | + material.name = newName; |
501 | 519 |
|
502 | | - // Change name in layers that use the material |
| 520 | + // Change the name also in layers that use the material |
503 | 521 | for(auto& layer: bow->section.layers) { |
504 | 522 | if(layer.material == oldName) { |
505 | 523 | layer.material = newName; |
|
0 commit comments