Skip to content

Commit

Permalink
implemented a std::deque to handle the insert order:
Browse files Browse the repository at this point in the history
This ensures, that both selected and inserted elements are inserted in the correct order.
Selected elements are handled as stack to emplace them to the old position.
New elements are pushed back now.
  • Loading branch information
Febbe committed Jan 15, 2020
1 parent 5853b49 commit 96dfc7f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
23 changes: 12 additions & 11 deletions src/control/tools/EditSelectionContents.cpp
Expand Up @@ -61,8 +61,13 @@ EditSelectionContents::~EditSelectionContents() {
* Add an element to the this selection
*/
void EditSelectionContents::addElement(Element* e, Layer::ElementIndex order) {
g_assert(this->selected.size() == this->insertOrder.size());
this->selected.emplace_back(e);
this->indexWithinLayer.emplace(e, order);
if (order == Layer::InvalidElementIndex) {
this->insertOrder.emplace_back(e, order);
} else {
this->insertOrder.emplace_front(e, order);
}
}

/**
Expand Down Expand Up @@ -271,6 +276,7 @@ void EditSelectionContents::fillUndoItem(DeleteUndoAction* undo) {
}

this->selected.clear();
this->insertOrder.clear();
}

/**
Expand Down Expand Up @@ -328,10 +334,8 @@ void EditSelectionContents::finalizeSelection(double x, double y, double width,

bool move = mx != 0 || my != 0;


for (auto it = this->selected.rbegin(); it != this->selected.rend(); ++it) {
Element* e = *it;

g_assert(this->selected.size() == this->insertOrder.size());
for (auto&& [e, index]: this->insertOrder) {
if (move) {
e->move(mx, my);
}
Expand All @@ -341,14 +345,11 @@ void EditSelectionContents::finalizeSelection(double x, double y, double width,
if (rotate) {
e->rotate(x, y, this->lastWidth / 2, this->lastHeight / 2, this->rotation);
}

auto layerIndex = indexWithinLayer.find(e);
if (layerIndex == indexWithinLayer.end() ||
layerIndex->second ==
Layer::InvalidElementIndex) { // if the element didn't have a source layer (e.g, clipboard)
if (index == Layer::InvalidElementIndex) {
// if the element didn't have a source layer (e.g, clipboard)
layer->addElement(e);
} else {
layer->insertElement(e, layerIndex->second);
layer->insertElement(e, index);
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions src/control/tools/EditSelectionContents.h
Expand Up @@ -11,8 +11,8 @@

#pragma once

#include <map>
#include <string>
#include <deque>
#include <utility>
#include <vector>

#include "control/Tool.h"
Expand Down Expand Up @@ -175,12 +175,13 @@ class EditSelectionContents: public ElementContainer, public Serializeable {
/**
* The selected element (the only one which are handled by this instance)
*/
vector<Element*> selected;
std::vector<Element*> selected;

/**
* Mapping of elements in the selection to the indexes from the original selection layer.
* Defines a insert order over the selection.
*/
std::map<Element*, Layer::ElementIndex> indexWithinLayer;
std::deque<std::pair<Element*, Layer::ElementIndex>> insertOrder;

/**
* The rendered elements
Expand Down

0 comments on commit 96dfc7f

Please sign in to comment.