BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
copyitemcommand.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // qt-mvvm: Model-view-view-model framework for large GUI applications
4 //
5 //! @file mvvm/model/mvvm/commands/copyitemcommand.cpp
6 //! @brief Implements class CLASS?
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2020
11 //! @authors Gennady Pospelov et al, Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
19 #include "mvvm/model/path.h"
20 #include "mvvm/model/sessionitem.h"
22 #include <sstream>
23 
24 using namespace ModelView;
25 
26 namespace {
27 std::string generate_description(const std::string& modelType, const TagRow& tagrow);
28 } // namespace
29 
32  std::unique_ptr<ItemBackupStrategy> backup_strategy;
35 };
36 
38  : AbstractItemCommand(parent), p_impl(std::make_unique<CopyItemCommandImpl>(std::move(tagrow)))
39 {
40  setResult(nullptr);
41 
42  setDescription(generate_description(item->modelType(), p_impl->tagrow));
43  p_impl->backup_strategy = CreateItemBackupStrategy(parent->model());
44  p_impl->item_path = pathFromItem(parent);
45 
46  auto copy_strategy = CreateItemCopyStrategy(parent->model()); // to modify id's
47  auto item_copy = copy_strategy->createCopy(item);
48 
49  p_impl->backup_strategy->saveItem(item_copy.get());
50 }
51 
53 
55 {
56  auto parent = itemFromPath(p_impl->item_path);
57  delete parent->takeItem(p_impl->tagrow);
58  setResult(nullptr);
59 }
60 
62 {
63  auto parent = itemFromPath(p_impl->item_path);
64  auto item = p_impl->backup_strategy->restoreItem();
65  if (parent->insertItem(item.get(), p_impl->tagrow)) {
66  auto result = item.release();
68  } else {
69  setResult(nullptr);
70  setObsolete(true);
71  }
72 }
73 
74 namespace {
75 std::string generate_description(const std::string& modelType, const TagRow& tagrow)
76 {
77  std::ostringstream ostr;
78  ostr << "Copy item'" << modelType << "' tag:'" << tagrow.tag << "', row:" << tagrow.row;
79  return ostr.str();
80 }
81 } // namespace
Abstract command interface to manipulate SessionItem in model context.
SessionItem * itemFromPath(const Path &path) const
void setDescription(const std::string &text)
Sets command description.
void setResult(const CommandResult &command_result)
Path pathFromItem(SessionItem *item) const
void setObsolete(bool flag)
Sets command obsolete flag.
std::unique_ptr< CopyItemCommandImpl > p_impl
CopyItemCommand(const SessionItem *item, SessionItem *parent, TagRow tagrow)
Supports navigation through SessionModel.
Definition: path.h:35
The main object representing an editable/displayable/serializable entity.
Definition: sessionitem.h:38
SessionModel * model() const
Returns the model to which given item belongs to.
model_type modelType() const
Returns item's model type.
Definition: sessionitem.cpp:80
Aggregate to hold (tag, row) information for SessionModel.
Definition: tagrow.h:25
std::string tag
Definition: tagrow.h:27
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
materialitems.h Collection of materials to populate MaterialModel.
MVVM_MODEL_EXPORT std::unique_ptr< ItemCopyStrategy > CreateItemCopyStrategy(const SessionModel *model)
Returns strategy for item copying.
MVVM_MODEL_EXPORT std::unique_ptr< ItemBackupStrategy > CreateItemBackupStrategy(const SessionModel *model)
Creates strategy suitable for item saving/restoring.
Definition: filesystem.h:81
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
std::unique_ptr< ItemBackupStrategy > backup_strategy