BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
setvaluecommand.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/setvaluecommand.cpp
6 //! @brief Defines 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/core/variant.h"
17 #include "mvvm/model/modelutils.h"
18 #include "mvvm/model/path.h"
19 #include "mvvm/model/sessionitem.h"
20 #include <sstream>
21 
22 namespace {
23 std::string generate_description(const std::string& str, int role);
24 } // namespace
25 
26 using namespace ModelView;
27 
29  Variant m_value; //! Value to set as a result of command execution.
30  int m_role;
32  SetValueCommandImpl(Variant value, int role) : m_value(std::move(value)), m_role(role) {}
33 };
34 
35 // ----------------------------------------------------------------------------
36 
38  : AbstractItemCommand(item)
39  , p_impl(std::make_unique<SetValueCommandImpl>(std::move(value), role))
40 {
41  setResult(false);
42 
43  setDescription(generate_description(p_impl->m_value.toString().toStdString(), role));
44  p_impl->m_item_path = pathFromItem(item);
45 }
46 
48 
50 {
51  swap_values();
52 }
53 
55 {
56  swap_values();
57 }
58 
60 {
61  auto item = itemFromPath(p_impl->m_item_path);
62  auto old = item->data<Variant>(p_impl->m_role);
63  auto result = item->setData(p_impl->m_value, p_impl->m_role, /*direct*/ true);
66  p_impl->m_value = old;
67 }
68 
69 namespace {
70 std::string generate_description(const std::string& str, int role)
71 {
72  std::ostringstream ostr;
73  ostr << "Set value: " << str << ", role:" << role;
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
The main object representing an editable/displayable/serializable entity.
Definition: sessionitem.h:38
SetValueCommand(SessionItem *item, Variant value, int role)
std::unique_ptr< SetValueCommandImpl > p_impl
Defines class CLASS?
materialitems.h Collection of materials to populate MaterialModel.
Definition: filesystem.h:81
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
int m_role
Value to set as a result of command execution.
Defines class CLASS?
QVariant Variant
Definition: variant.h:23