# HG changeset patch # User Santeri Piippo # Date 1520287187 -7200 # Node ID bf0ac547b9349ae522e13ddf4f00e55d53018c57 # Parent c022fb3855b100cd2c2e2e1a1678400639577b56 migrated model swapping to mvc and made Model::swapObjects to emit dataChanged signals as a temporary solution diff -r c022fb3855b1 -r bf0ac547b934 src/editHistory.cpp --- 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() diff -r c022fb3855b1 -r bf0ac547b934 src/editHistory.h --- 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; }; diff -r c022fb3855b1 -r bf0ac547b934 src/lddocument.cpp --- 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(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(one->id(), other->id()); - return true; - } - else - { - return false; - } -} // ============================================================================= // diff -r c022fb3855b1 -r bf0ac547b934 src/lddocument.h --- 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); diff -r c022fb3855b1 -r bf0ac547b934 src/model.cpp --- 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 diff -r c022fb3855b1 -r bf0ac547b934 src/model.h --- 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 T* emplace(Args&& ...args); template T* emplaceAt(int position, Args&& ...args); diff -r c022fb3855b1 -r bf0ac547b934 src/toolsets/movetoolset.cpp --- 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)); } }