Skip to content
Snippets Groups Projects
Commit e10ae899 authored by Matthias Puchner's avatar Matthias Puchner
Browse files

add undo/redo for adding a layer

parent eb9cc7af
No related branches found
No related tags found
1 merge request!714Undo/Redo of sample editor
......@@ -50,6 +50,29 @@ void CommandRemoveLayer::undo()
emit m_ec->modified();
}
// --------------------------------------------------------------------------------------------- //
CommandAddLayer::CommandAddLayer(SampleEditorController* ec, int atIndex, QUndoCommand* parent)
: m_ec(ec)
, m_atIndex(atIndex)
{
setText("Add layer");
}
void CommandAddLayer::redo()
{
m_ec->addLayerFromUndo(m_atIndex);
}
void CommandAddLayer::undo()
{
// no backup of the layer has to be stored, since redo always creates the layer
// from scratch - no contents required for this
m_ec->removeLayerFromUndo(m_atIndex);
}
// --------------------------------------------------------------------------------------------- //
CommandChangeValue::CommandChangeValue(const QString& label, SampleEditorController* ec,
double oldValue, double newValue, const QString& path,
QUndoCommand* parent /*= nullptr*/)
......
......@@ -40,6 +40,19 @@ private:
bool m_isFirst = true;
};
//! Command to add a layer to a multilayer
class CommandAddLayer : public QUndoCommand {
public:
CommandAddLayer(SampleEditorController* ec, int atIndex, QUndoCommand* parent = nullptr);
void redo() override;
void undo() override;
private:
SampleEditorController* m_ec;
int m_atIndex;
};
//! Command to remove a layer from a multilayer
class CommandRemoveLayer : public QUndoCommand {
public:
......
......@@ -62,8 +62,13 @@ MultiLayerItem* SampleEditorController::multiLayerItem() const
void SampleEditorController::addLayer(LayerItem* before)
{
const int rowInMultiLayer = (before != nullptr) ? m_multiLayerItem->layers().indexOf(before)
: m_multiLayerItem->layers().size();
const int newIndex = (before != nullptr) ? m_multiLayerItem->layers().indexOf(before)
: m_multiLayerItem->layers().size();
m_undoStack.push(new CommandAddLayer(this, newIndex));
}
void SampleEditorController::addLayerFromUndo(int atIndex)
{
// -- find a color for the new layer
QColor color;
......@@ -85,11 +90,10 @@ void SampleEditorController::addLayer(LayerItem* before)
[&](const QColor& a, const QColor& b) { return usage[a.name()] < usage[b.name()]; });
const QColor above = (rowInMultiLayer > 0)
? m_multiLayerItem->layers()[rowInMultiLayer - 1]->color()
: QColor();
const QColor below = (rowInMultiLayer < m_multiLayerItem->layers().size())
? m_multiLayerItem->layers()[rowInMultiLayer]->color()
const QColor above =
(atIndex > 0) ? m_multiLayerItem->layers()[atIndex - 1]->color() : QColor();
const QColor below = (atIndex < m_multiLayerItem->layers().size())
? m_multiLayerItem->layers()[atIndex]->color()
: QColor();
for (const auto& col : sortedByUsage)
......@@ -100,7 +104,7 @@ void SampleEditorController::addLayer(LayerItem* before)
}
// - create new layer
LayerItem* layer = m_multiLayerItem->addLayer(rowInMultiLayer);
LayerItem* layer = m_multiLayerItem->addLayer(atIndex);
layer->setMaterial(materialItems()->defaultMaterial());
layer->setColor(color);
......
......@@ -74,6 +74,7 @@ public:
ProjectDocument* projectDocument() const;
void addLayer(LayerItem* before);
void addLayerFromUndo(int atIndex);
void removeLayer(LayerItem* layerItem);
void removeLayerFromUndo(int atIndex);
void addLayout(LayerForm* layerItem);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment