Skip to content
Snippets Groups Projects
SampleEditorCommands.cpp 3.42 KiB
Newer Older
//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      GUI/View/SampleDesigner/SampleEditorCommands.cpp
//! @brief     Implements command classes for LayerOrientedSampleEditor
//!
//! @homepage  http://www.bornagainproject.org
//! @license   GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2021
//! @authors   Scientific Computing Group at MLZ (see CITATION, AUTHORS)
//
//  ************************************************************************************************

#include "GUI/View/SampleDesigner/SampleEditorCommands.h"
#include "GUI/Model/Sample/LayerItem.h"
#include "GUI/Model/Sample/MultiLayerItem.h"
Wuttke, Joachim's avatar
Wuttke, Joachim committed
#include "GUI/Support/XML/Backup.h"
#include "GUI/View/SampleDesigner/MultiLayerForm.h"
#include "GUI/View/SampleDesigner/SampleEditorController.h"
#include <utility>
Wuttke, Joachim's avatar
Wuttke, Joachim committed

constexpr int COMMAND_ID_CHANGE_VALUE = 11;

} // namespace


CommandRemoveLayer::CommandRemoveLayer(SampleEditorController* ec, LayerItem* layerItem,
                                       QUndoCommand* parent /*= nullptr*/)
    : QUndoCommand(parent)
    , m_ec(ec)
Matthias Puchner's avatar
Matthias Puchner committed
    setText("Remove layer");
    m_indexOfLayer = ec->multiLayerItem()->layers().indexOf(layerItem);
Matthias Puchner's avatar
Matthias Puchner committed
    m_layerItemBackup = GUI::Util::createBackup(layerItem);
    m_ec->removeLayerFromUndo(m_indexOfLayer);
    LayerItem* restoredLayer = m_ec->multiLayerItem()->addLayer(m_indexOfLayer);
Matthias Puchner's avatar
Matthias Puchner committed
    GUI::Util::restoreBackup(restoredLayer, m_layerItemBackup);
    m_ec->multiLayerForm()->onLayerAdded(restoredLayer);
    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,
Matthias Puchner's avatar
Matthias Puchner committed
                                       double oldValue, double newValue, const QString& path,
                                       QUndoCommand* parent /*= nullptr*/)
    : QUndoCommand(parent)
    , m_ec(ec)
    , m_oldValue(oldValue)
    , m_newValue(newValue)
Matthias Puchner's avatar
Matthias Puchner committed
    , m_path(path)
{
    setText("change " + label + "\n");
}

int CommandChangeValue::id() const
{
    return COMMAND_ID_CHANGE_VALUE;
}

bool CommandChangeValue::mergeWith(const QUndoCommand* command)
{
Matthias Puchner's avatar
Matthias Puchner committed
    if (command->id() != id()) // make sure other is also a changeValue command
    const auto* const other = dynamic_cast<const CommandChangeValue*>(command);

    if (m_path != other->m_path)
        return false;

    m_newValue = other->m_newValue;
    return true;
}

void CommandChangeValue::redo()
{
    if (m_isFirst)
        m_isFirst = false;
    else
        m_ec->setDoubleFromUndo(m_newValue, m_path);
}

void CommandChangeValue::undo()
{
    m_ec->setDoubleFromUndo(m_oldValue, m_path);
}