BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
SampleEditorCommands.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/View/SampleDesigner/SampleEditorCommands.cpp
6 //! @brief Implements command classes for LayerOrientedSampleEditor
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2021
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
18 #include "GUI/Support/XML/Backup.h"
21 #include <utility>
22 
23 namespace {
24 
25 constexpr int COMMAND_ID_CHANGE_VALUE = 11;
26 
27 } // namespace
28 
29 
31  QUndoCommand* parent /*= nullptr*/)
32  : QUndoCommand(parent)
33  , m_ec(ec)
34 {
35  setText("Remove layer");
36  m_indexOfLayer = ec->sampleItem()->layers().indexOf(layerItem);
38 }
39 
41 {
43 }
44 
46 {
47  LayerItem* restoredLayer = m_ec->sampleItem()->addLayer(m_indexOfLayer);
49  m_ec->sampleForm()->onLayerAdded(restoredLayer);
50  emit m_ec->modified();
51 }
52 
53 // --------------------------------------------------------------------------------------------- //
54 
55 CommandAddLayer::CommandAddLayer(SampleEditorController* ec, int atIndex, QUndoCommand* /*parent*/)
56  : m_ec(ec)
57  , m_atIndex(atIndex)
58 {
59  setText("Add layer");
60 }
61 
63 {
65 }
66 
68 {
69  // no backup of the layer has to be stored, since redo always creates the layer
70  // from scratch - no contents required for this
72 }
73 
74 // --------------------------------------------------------------------------------------------- //
75 
77  double oldValue, double newValue, const QString& path,
78  QUndoCommand* parent /*= nullptr*/)
79  : QUndoCommand(parent)
80  , m_ec(ec)
81  , m_oldValue(oldValue)
82  , m_newValue(newValue)
83  , m_path(path)
84 {
85  setText("change " + label + "\n");
86 }
87 
89 {
90  return COMMAND_ID_CHANGE_VALUE;
91 }
92 
93 bool CommandChangeValue::mergeWith(const QUndoCommand* command)
94 {
95  if (command->id() != id()) // make sure other is also a changeValue command
96  return false;
97 
98  const auto* const other = dynamic_cast<const CommandChangeValue*>(command);
99 
100  if (m_path != other->m_path)
101  return false;
102 
103  m_newValue = other->m_newValue;
104  return true;
105 }
106 
108 {
109  if (m_isFirst)
110  m_isFirst = false;
111  else
113 }
114 
116 {
118 }
Defines GUI::Util namespace.
Defines class LayerItem.
Defines class MultiLayerForm.
Defines class MultiLayerItem.
Defines command classes for LayerOrientedSampleEditor.
Defines class SampleEditorController.
@ other
The unit has no enum value defined in here (e.g. when defined as an explicit string)
void undo() override
SampleEditorController * m_ec
void redo() override
CommandAddLayer(SampleEditorController *ec, int atIndex, QUndoCommand *parent=nullptr)
Command to change a double value.
bool mergeWith(const QUndoCommand *command) override
int id() const override
SampleEditorController * m_ec
CommandChangeValue(const QString &label, SampleEditorController *ec, double oldValue, double newValue, const QString &path, QUndoCommand *parent=nullptr)
SampleEditorController * m_ec
CommandRemoveLayer(SampleEditorController *ec, LayerItem *layerItem, QUndoCommand *parent=nullptr)
void onLayerAdded(LayerItem *layerItem)
Create widgets for the new layer.
LayerItem * addLayer(int index=-1)
Creates and inserts a layer at given index.
QVector< LayerItem * > layers() const
Class to modify a sample from the layer oriented sample editor.
MultiLayerItem * sampleItem() const
The item on which this controller operates.
void addLayerFromUndo(int atIndex)
MultiLayerForm * sampleForm() const
The current form.
void setDoubleFromUndo(double newValue, const QString &path)
void removeLayerFromUndo(int atIndex)
void restoreBackup(T *t, const QByteArray &backup)
Definition: Backup.h:36
QByteArray createBackup(const T *t)
Definition: Backup.h:24