BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
ModelView::InsertNewItemCommand Class Reference

Command for unddo/redo to insert new item. More...

Inheritance diagram for ModelView::InsertNewItemCommand:
[legend]
Collaboration diagram for ModelView::InsertNewItemCommand:
[legend]

Classes

struct  InsertNewItemCommandImpl
 

Public Member Functions

 InsertNewItemCommand (item_factory_func_t func, SessionItem *parent, const TagRow &tagrow)
 
 ~InsertNewItemCommand () override
 
std::string description () const
 Returns command description. More...
 
void execute ()
 Execute command. More...
 
bool isObsolete () const
 Returns whether the command is obsolete (which means that it shouldn't be kept in the stack). More...
 
CommandResult result () const
 
void undo ()
 Undo command as it was before execution. More...
 

Protected Member Functions

SessionItemitemFromPath (const Path &path) const
 
SessionModelmodel () const
 
Path pathFromItem (SessionItem *item) const
 
void setDescription (const std::string &text)
 Sets command description. More...
 
void setObsolete (bool flag)
 Sets command obsolete flag. More...
 
void setResult (const CommandResult &command_result)
 

Private Member Functions

void execute_command () override
 
void undo_command () override
 

Private Attributes

std::unique_ptr< InsertNewItemCommandImplp_impl
 

Detailed Description

Command for unddo/redo to insert new item.

Definition at line 28 of file insertnewitemcommand.h.

Constructor & Destructor Documentation

◆ InsertNewItemCommand()

InsertNewItemCommand::InsertNewItemCommand ( item_factory_func_t  func,
SessionItem parent,
const TagRow tagrow 
)

Definition at line 37 of file insertnewitemcommand.cpp.

39  : AbstractItemCommand(parent), p_impl(std::make_unique<InsertNewItemCommandImpl>(func, tagrow))
40 {
41  setResult(nullptr);
42  p_impl->item_path = pathFromItem(parent);
43 }
AbstractItemCommand(SessionItem *receiver)
void setResult(const CommandResult &command_result)
Path pathFromItem(SessionItem *item) const
std::unique_ptr< InsertNewItemCommandImpl > p_impl

References p_impl, ModelView::AbstractItemCommand::pathFromItem(), and ModelView::AbstractItemCommand::setResult().

Here is the call graph for this function:

◆ ~InsertNewItemCommand()

InsertNewItemCommand::~InsertNewItemCommand ( )
overridedefault

Member Function Documentation

◆ description()

std::string AbstractItemCommand::description ( ) const
inherited

Returns command description.

Definition at line 87 of file abstractitemcommand.cpp.

88 {
89  return p_impl->m_text;
90 }
std::unique_ptr< AbstractItemCommandImpl > p_impl

References ModelView::AbstractItemCommand::p_impl.

◆ execute()

void AbstractItemCommand::execute ( )
inherited

Execute command.

Definition at line 56 of file abstractitemcommand.cpp.

57 {
58  if (!p_impl->can_execute())
59  throw std::runtime_error("Can't execute the command. Wrong order.");
60 
62 
63  p_impl->set_after_execute();
64 }
virtual void execute_command()=0

References ModelView::AbstractItemCommand::execute_command(), and ModelView::AbstractItemCommand::p_impl.

Referenced by TEST_F().

Here is the call graph for this function:

◆ execute_command()

void InsertNewItemCommand::execute_command ( )
overrideprivatevirtual

Implements ModelView::AbstractItemCommand.

Definition at line 58 of file insertnewitemcommand.cpp.

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 }
SessionItem * itemFromPath(const Path &path) const
void setDescription(const std::string &text)
Sets command description.
void setObsolete(bool flag)
Sets command obsolete flag.
const int IDENTIFIER
unique identifier
Definition: mvvm_types.h:29

References ModelView::ItemDataRole::IDENTIFIER, ModelView::AbstractItemCommand::itemFromPath(), p_impl, ModelView::AbstractItemCommand::setDescription(), ModelView::AbstractItemCommand::setObsolete(), and ModelView::AbstractItemCommand::setResult().

Here is the call graph for this function:

◆ isObsolete()

bool AbstractItemCommand::isObsolete ( ) const
inherited

Returns whether the command is obsolete (which means that it shouldn't be kept in the stack).

Definition at line 80 of file abstractitemcommand.cpp.

81 {
82  return p_impl->m_isObsolete;
83 }

References ModelView::AbstractItemCommand::p_impl.

Referenced by ModelView::AbstractItemCommand::AbstractItemCommandImpl::can_undo(), and TEST_F().

◆ itemFromPath()

SessionItem * AbstractItemCommand::itemFromPath ( const Path path) const
protectedinherited

Definition at line 116 of file abstractitemcommand.cpp.

117 {
118  return Utils::ItemFromPath(*p_impl->m_model, path);
119 }
MVVM_MODEL_EXPORT SessionItem * ItemFromPath(const SessionModel &moodel, const Path &path)
Returns item found in the model following given Path.
Definition: modelutils.cpp:36

References ModelView::Utils::ItemFromPath(), and ModelView::AbstractItemCommand::p_impl.

Referenced by ModelView::CopyItemCommand::execute_command(), execute_command(), ModelView::MoveItemCommand::execute_command(), ModelView::RemoveItemCommand::execute_command(), ModelView::SetValueCommand::swap_values(), ModelView::CopyItemCommand::undo_command(), undo_command(), ModelView::MoveItemCommand::undo_command(), and ModelView::RemoveItemCommand::undo_command().

Here is the call graph for this function:

◆ model()

SessionModel * AbstractItemCommand::model ( ) const
protectedinherited

Definition at line 121 of file abstractitemcommand.cpp.

122 {
123  return p_impl->m_model;
124 }

References ModelView::AbstractItemCommand::p_impl.

◆ pathFromItem()

Path AbstractItemCommand::pathFromItem ( SessionItem item) const
protectedinherited

Definition at line 111 of file abstractitemcommand.cpp.

112 {
113  return Utils::PathFromItem(item);
114 }
MVVM_MODEL_EXPORT Path PathFromItem(const SessionItem *item)
Constructs path to find given item. Item must belong to a model.
Definition: modelutils.cpp:22

References ModelView::Utils::PathFromItem().

Referenced by ModelView::CopyItemCommand::CopyItemCommand(), InsertNewItemCommand(), ModelView::MoveItemCommand::MoveItemCommand(), ModelView::RemoveItemCommand::RemoveItemCommand(), ModelView::SetValueCommand::SetValueCommand(), ModelView::MoveItemCommand::execute_command(), and ModelView::MoveItemCommand::undo_command().

Here is the call graph for this function:

◆ result()

CommandResult AbstractItemCommand::result ( ) const
inherited

◆ setDescription()

void AbstractItemCommand::setDescription ( const std::string &  text)
protectedinherited

◆ setObsolete()

void AbstractItemCommand::setObsolete ( bool  flag)
protectedinherited

◆ setResult()

◆ undo()

void AbstractItemCommand::undo ( )
inherited

Undo command as it was before execution.

Definition at line 68 of file abstractitemcommand.cpp.

69 {
70  if (!p_impl->can_undo())
71  throw std::runtime_error("Can't undo the command. Wrong order.");
72 
73  undo_command();
74 
75  p_impl->set_after_undo();
76 }

References ModelView::AbstractItemCommand::p_impl, and ModelView::AbstractItemCommand::undo_command().

Referenced by TEST_F().

Here is the call graph for this function:

◆ undo_command()

void InsertNewItemCommand::undo_command ( )
overrideprivatevirtual

Implements ModelView::AbstractItemCommand.

Definition at line 47 of file insertnewitemcommand.cpp.

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 }

References ModelView::AbstractItemCommand::itemFromPath(), p_impl, and ModelView::AbstractItemCommand::setResult().

Here is the call graph for this function:

Member Data Documentation

◆ p_impl

std::unique_ptr<InsertNewItemCommandImpl> ModelView::InsertNewItemCommand::p_impl
private

Definition at line 38 of file insertnewitemcommand.h.

Referenced by InsertNewItemCommand(), execute_command(), and undo_command().


The documentation for this class was generated from the following files: