Mon, 05 Mar 2018 23:59:47 +0200
migrated model swapping to mvc and made Model::swapObjects to emit dataChanged signals as a temporary solution
src/editHistory.cpp | file | annotate | diff | comparison | revisions | |
src/editHistory.h | file | annotate | diff | comparison | revisions | |
src/lddocument.cpp | file | annotate | diff | comparison | revisions | |
src/lddocument.h | file | annotate | diff | comparison | revisions | |
src/model.cpp | file | annotate | diff | comparison | revisions | |
src/model.h | file | annotate | diff | comparison | revisions | |
src/toolsets/movetoolset.cpp | file | annotate | diff | comparison | revisions |
--- a/src/editHistory.cpp Mon Mar 05 22:44:48 2018 +0200 +++ b/src/editHistory.cpp Mon Mar 05 23:59:47 2018 +0200 @@ -188,15 +188,16 @@ parent()->document()->setObjectAt(row, newState); } -SwapHistoryEntry::SwapHistoryEntry (int a, int b, EditHistory* parent) : +SwapHistoryEntry::SwapHistoryEntry (const QModelIndex& index_1, const QModelIndex& index_2, EditHistory* parent) : AbstractHistoryEntry {parent}, - m_a (a), - m_b (b) {} + row_1 (index_1.row()), + row_2 (index_2.row()) {} void SwapHistoryEntry::undo() { - parent()->document()->swapObjects(LDObject::fromID (m_a), LDObject::fromID (m_b)); + Model* model = parent()->document(); + model->swapObjects(model->index(row_1), model->index(row_2)); } void SwapHistoryEntry::redo()
--- a/src/editHistory.h Mon Mar 05 22:44:48 2018 +0200 +++ b/src/editHistory.h Mon Mar 05 23:59:47 2018 +0200 @@ -119,11 +119,11 @@ class SwapHistoryEntry : public AbstractHistoryEntry { public: - SwapHistoryEntry (int a, int b, EditHistory* parent); + SwapHistoryEntry (const QModelIndex& index_1, const QModelIndex& index_2, EditHistory* parent); void undo() override; void redo() override; private: - int m_a; - int m_b; + int row_1; + int row_2; };
--- a/src/lddocument.cpp Mon Mar 05 22:44:48 2018 +0200 +++ b/src/lddocument.cpp Mon Mar 05 23:59:47 2018 +0200 @@ -41,6 +41,15 @@ this, SLOT(handleNewObject(QModelIndex)) ); + + connect( + this, + &Model::objectsSwapped, + [&](const QModelIndex& index_1, const QModelIndex& index_2) + { + history()->add<SwapHistoryEntry>(index_1, index_2); + } + ); } LDDocument::~LDDocument() @@ -417,20 +426,6 @@ model.addFromString(object->asText()); } } -// ============================================================================= -// -bool LDDocument::swapObjects (LDObject* one, LDObject* other) -{ - if (Model::swapObjects(one, other)) - { - history()->add<SwapHistoryEntry>(one->id(), other->id()); - return true; - } - else - { - return false; - } -} // ============================================================================= //
--- a/src/lddocument.h Mon Mar 05 22:44:48 2018 +0200 +++ b/src/lddocument.h Mon Mar 05 23:59:47 2018 +0200 @@ -74,7 +74,6 @@ void setName (QString value); void setSavePosition (long value); void setTabIndex (int value); - bool swapObjects (LDObject* one, LDObject* other); int tabIndex() const; void undo(); void vertexChanged (const Vertex& a, const Vertex& b);
--- a/src/model.cpp Mon Mar 05 22:44:48 2018 +0200 +++ b/src/model.cpp Mon Mar 05 23:59:47 2018 +0200 @@ -82,16 +82,14 @@ /* * Swaps one object with another, assuming they both are in this model. */ -bool Model::swapObjects(LDObject* one, LDObject* other) +bool Model::swapObjects(const QModelIndex& index_1, const QModelIndex& index_2) { - QModelIndex a = indexOf(one); - QModelIndex b = indexOf(other); - - if (a.isValid() and b.isValid() and a != b) + if (index_1.isValid() and index_2.isValid() and index_1 != index_2) { - _objects[b.row()] = one; - _objects[a.row()] = other; - emit objectsSwapped(a, b); + qSwap(_objects[index_1.row()], _objects[index_2.row()]); + emit objectsSwapped(index_1, index_2); + emit dataChanged(index_1, index_1); + emit dataChanged(index_2, index_2); return true; } else
--- a/src/model.h Mon Mar 05 22:44:48 2018 +0200 +++ b/src/model.h Mon Mar 05 23:59:47 2018 +0200 @@ -89,7 +89,7 @@ void insertCopy(int position, LDObject* object); void insertFromArchive(int row, Serializer::Archive& archive); - bool swapObjects(LDObject* one, LDObject* other); + bool swapObjects(const QModelIndex& index_1, const QModelIndex& index_2); bool setObjectAt(int idx, Serializer::Archive& archive); template<typename T, typename... Args> T* emplace(Args&& ...args); template<typename T, typename... Args> T* emplaceAt(int position, Args&& ...args);
--- a/src/toolsets/movetoolset.cpp Mon Mar 05 22:44:48 2018 +0200 +++ b/src/toolsets/movetoolset.cpp Mon Mar 05 23:59:47 2018 +0200 @@ -45,10 +45,10 @@ { LDObject* obj = objs[i]; - int idx = currentDocument()->indexOf(obj).row(); - int target = idx + (up ? -1 : 1); + QModelIndex index = currentDocument()->indexOf(obj); + int target = index.row() + (up ? -1 : 1); - if ((up and idx == 0) or (not up and idx == countof(currentDocument()->objects()) - 1)) + if ((up and index.row() == 0) or (not up and index.row() == countof(currentDocument()->objects()) - 1)) { // One of the objects hit the extrema. If this happens, this should be the first // object to be iterated on. Thus, nothing has changed yet and it's safe to just @@ -56,7 +56,7 @@ return; } - currentDocument()->swapObjects(obj, currentDocument()->getObject(target)); + currentDocument()->swapObjects(index, currentDocument()->index(target)); } }