BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
removeitemcommand.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/removeitemcommand.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 
18 #include "mvvm/model/path.h"
19 #include "mvvm/model/sessionitem.h"
21 #include <sstream>
22 
23 using namespace ModelView;
24 
25 namespace {
26 std::string generate_description(const TagRow& tagrow);
27 } // namespace
28 
31  std::unique_ptr<ItemBackupStrategy> backup_strategy;
34 };
35 
37  : AbstractItemCommand(parent)
38  , p_impl(std::make_unique<RemoveItemCommandImpl>(std::move(tagrow)))
39 {
40  setResult(false);
41 
42  setDescription(generate_description(p_impl->tagrow));
43  p_impl->backup_strategy = CreateItemBackupStrategy(parent->model());
44  p_impl->item_path = pathFromItem(parent);
45 }
46 
48 
50 {
51  auto parent = itemFromPath(p_impl->item_path);
52  auto reco_item = p_impl->backup_strategy->restoreItem();
53  parent->insertItem(reco_item.release(), p_impl->tagrow);
54 }
55 
57 {
58  auto parent = itemFromPath(p_impl->item_path);
59  if (auto child = parent->takeItem(p_impl->tagrow); child) {
60  p_impl->backup_strategy->saveItem(child);
61  delete child;
62  setResult(true);
63  } else {
64  setResult(false);
65  setObsolete(true);
66  }
67 }
68 
69 namespace {
70 std::string generate_description(const TagRow& tagrow)
71 {
72  std::ostringstream ostr;
73  ostr << "Remove item from tag '" << tagrow.tag << "', row " << tagrow.row;
74  return ostr.str();
75 }
76 } // 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.
Supports navigation through SessionModel.
Definition: path.h:35
std::unique_ptr< RemoveItemCommandImpl > p_impl
RemoveItemCommand(SessionItem *parent, TagRow tagrow)
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.
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?
materialitems.h Collection of materials to populate MaterialModel.
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?
Defines class CLASS?
std::unique_ptr< ItemBackupStrategy > backup_strategy