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"
#include "GUI/View/SampleDesigner/MultiLayerForm.h"
#include "GUI/View/SampleDesigner/SampleEditorController.h"
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
namespace {
static constexpr int COMMAND_ID_CHANGE_VALUE = 11;
}
CommandRemoveLayer::CommandRemoveLayer(SampleEditorController* ec, LayerItem* layerItem,
QUndoCommand* parent /*= nullptr*/)
: QUndoCommand(parent), m_ec(ec)
{
m_indexOfLayer = ec->multiLayerItem()->layers().indexOf(layerItem);
m_layerItemBackup = m_backupModel.insertItem<LayerItem>();
auto source = layerItem;
for (auto child : source->children()) {
auto tag = source->tagFromItem(child);
m_backupModel.copyItem(child, m_layerItemBackup, tag);
}
}
void CommandRemoveLayer::redo()
{
auto* layer = m_ec->multiLayerItem()->layers()[m_indexOfLayer];
m_ec->multiLayerForm()->onAboutToRemoveLayer(layer);
m_ec->multiLayerItem()->removeLayer(layer);
m_ec->multiLayerForm()->updateRowVisibilities();
}
void CommandRemoveLayer::undo()
{
if (m_indexOfLayer < m_ec->multiLayerItem()->layers().size())
m_ec->multiLayerItem()->addLayer(m_indexOfLayer);
else
m_ec->multiLayerItem()->addLayer(-1);
auto* restoredLayer = m_ec->multiLayerItem()->layers()[m_indexOfLayer];
for (auto child : m_layerItemBackup->children()) {
auto tag = m_layerItemBackup->tagFromItem(child);
m_backupModel.copyItem(child, restoredLayer, tag);
}
m_ec->multiLayerForm()->onLayerAdded(restoredLayer);
}
CommandChangeValue::CommandChangeValue(const QString& label, SampleEditorController* ec,
double oldValue, double newValue, QString path,
QUndoCommand* parent /*= nullptr*/)
: QUndoCommand(parent), m_ec(ec), m_oldValue(oldValue), m_newValue(newValue), m_path(path)
{
setText("change " + label + "\n");
}
int CommandChangeValue::id() const
{
return COMMAND_ID_CHANGE_VALUE;
}
bool CommandChangeValue::mergeWith(const QUndoCommand* command)
{
if (command->id() != id()) // make sure other is also an AppendText command
return false;
const auto other = static_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);
}