BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
insertnewitemcommand.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/insertnewitemcommand.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 
16 #include "mvvm/model/path.h"
17 #include "mvvm/model/sessionitem.h"
18 #include <sstream>
19 
20 using namespace ModelView;
21 
22 namespace {
23 std::string generate_description(const std::string& modelType, const TagRow& tagrow);
24 } // namespace
25 
30  std::string initial_identifier;
32  : factory_func(std::move(func)), tagrow(std::move(tagrow))
33  {
34  }
35 };
36 
38  const TagRow& tagrow)
39  : AbstractItemCommand(parent), p_impl(std::make_unique<InsertNewItemCommandImpl>(func, tagrow))
40 {
41  setResult(nullptr);
42  p_impl->item_path = pathFromItem(parent);
43 }
44 
46 
48 {
49  auto parent = itemFromPath(p_impl->item_path);
50  auto item = parent->takeItem(p_impl->tagrow);
51  // saving identifier for later redo
52  if (p_impl->initial_identifier.empty())
53  p_impl->initial_identifier = item->identifier();
54  delete item;
55  setResult(nullptr);
56 }
57 
59 {
60  auto parent = itemFromPath(p_impl->item_path);
61  auto child = p_impl->factory_func().release();
62  // here we restore original identifier to get exactly same item on consequitive undo/redo
63  if (!p_impl->initial_identifier.empty())
64  child->setData(QVariant::fromValue(p_impl->initial_identifier), ItemDataRole::IDENTIFIER,
65  /*direct*/ true);
66 
67  setDescription(generate_description(child->modelType(), p_impl->tagrow));
68  if (parent->insertItem(child, p_impl->tagrow)) {
69  setResult(child);
70  } else {
71  delete child;
72  setObsolete(true);
73  }
74 }
75 
76 namespace {
77 std::string generate_description(const std::string& modelType, const TagRow& tagrow)
78 {
79  std::ostringstream ostr;
80  ostr << "New item type '" << modelType << "' tag:'" << tagrow.tag << "', row:" << tagrow.row;
81  return ostr.str();
82 }
83 } // 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.
InsertNewItemCommand(item_factory_func_t func, SessionItem *parent, const TagRow &tagrow)
std::unique_ptr< InsertNewItemCommandImpl > p_impl
Supports navigation through SessionModel.
Definition: path.h:35
The main object representing an editable/displayable/serializable entity.
Definition: sessionitem.h:38
Aggregate to hold (tag, row) information for SessionModel.
Definition: tagrow.h:25
std::string tag
Definition: tagrow.h:27
Defines class CLASS?
const int IDENTIFIER
unique identifier
Definition: mvvm_types.h:29
materialitems.h Collection of materials to populate MaterialModel.
std::function< std::unique_ptr< SessionItem >()> item_factory_func_t
Definition for item factory funciton.
Definition: filesystem.h:81
Defines class CLASS?
Defines class CLASS?
InsertNewItemCommandImpl(item_factory_func_t func, TagRow tagrow)