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

Command for unddo/redo framework to move item from one parent to another. More...

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

Classes

struct  MoveItemCommandImpl
 

Public Member Functions

 MoveItemCommand (SessionItem *item, SessionItem *new_parent, TagRow tagrow)
 
 ~MoveItemCommand () 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< MoveItemCommandImplp_impl
 

Detailed Description

Command for unddo/redo framework to move item from one parent to another.

Definition at line 28 of file moveitemcommand.h.

Constructor & Destructor Documentation

◆ MoveItemCommand()

MoveItemCommand::MoveItemCommand ( SessionItem item,
SessionItem new_parent,
TagRow  tagrow 
)

Definition at line 41 of file moveitemcommand.cpp.

42  : AbstractItemCommand(new_parent), p_impl(std::make_unique<MoveItemCommandImpl>(tagrow))
43 {
44  setResult(true);
45 
46  check_input_data(item, new_parent);
47  setDescription(generate_description(p_impl->target_tagrow));
48 
49  p_impl->target_parent_path = pathFromItem(new_parent);
50  p_impl->original_parent_path = pathFromItem(item->parent());
51  p_impl->original_tagrow = item->tagRow();
52 
53  if (Utils::IsSinglePropertyTag(*item->parent(), p_impl->original_tagrow.tag))
54  throw std::runtime_error("MoveItemCommand::MoveItemCommand() -> Single property tag.");
55 
56  if (Utils::IsSinglePropertyTag(*new_parent, p_impl->target_tagrow.tag))
57  throw std::runtime_error("MoveItemCommand::MoveItemCommand() -> Single property tag.");
58 
59  if (item->parent() == new_parent) {
60  if (p_impl->target_tagrow.row >= new_parent->itemCount(p_impl->target_tagrow.tag))
61  throw std::runtime_error(
62  "MoveCommand::MoveCommand() -> move index exceeds number of items in a tag");
63  }
64 }
void setDescription(const std::string &text)
Sets command description.
AbstractItemCommand(SessionItem *receiver)
void setResult(const CommandResult &command_result)
Path pathFromItem(SessionItem *item) const
std::unique_ptr< MoveItemCommandImpl > p_impl
int itemCount(const std::string &tag) const
Returns number of items in given tag.
SessionItem * parent() const
Returns parent item. Will return nullptr if item doesn't have a parent.
TagRow tagRow() const
Returns TagRow of this item under which it is accessible through its parent.
MVVM_MODEL_EXPORT bool IsSinglePropertyTag(const SessionItem &item, const std::string &tag)
Returns true if given item has registered tag, and it belongs to single property.
Definition: itemutils.cpp:91

References ModelView::Utils::IsSinglePropertyTag(), ModelView::SessionItem::itemCount(), p_impl, ModelView::SessionItem::parent(), ModelView::AbstractItemCommand::pathFromItem(), ModelView::AbstractItemCommand::setDescription(), ModelView::AbstractItemCommand::setResult(), and ModelView::SessionItem::tagRow().

Here is the call graph for this function:

◆ ~MoveItemCommand()

MoveItemCommand::~MoveItemCommand ( )
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 MoveItemCommand::execute_command ( )
overrideprivatevirtual

Implements ModelView::AbstractItemCommand.

Definition at line 83 of file moveitemcommand.cpp.

84 {
85  // first find items
86  auto original_parent = itemFromPath(p_impl->original_parent_path);
87  auto target_parent = itemFromPath(p_impl->target_parent_path);
88 
89  // then make manipulations
90  auto taken = original_parent->takeItem(p_impl->original_tagrow);
91 
92  if (!taken)
93  throw std::runtime_error("MoveItemCommand::execute() -> Can't take an item.");
94 
95  bool succeeded = target_parent->insertItem(taken, p_impl->target_tagrow);
96  if (!succeeded)
97  throw std::runtime_error("MoveItemCommand::execute() -> Can't insert item.");
98 
99  // adjusting new addresses
100  p_impl->target_parent_path = pathFromItem(target_parent);
101  p_impl->original_parent_path = pathFromItem(original_parent);
102 }
SessionItem * itemFromPath(const Path &path) const

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

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(), ModelView::InsertNewItemCommand::execute_command(), execute_command(), ModelView::RemoveItemCommand::execute_command(), ModelView::SetValueCommand::swap_values(), ModelView::CopyItemCommand::undo_command(), ModelView::InsertNewItemCommand::undo_command(), 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(), ModelView::InsertNewItemCommand::InsertNewItemCommand(), MoveItemCommand(), ModelView::RemoveItemCommand::RemoveItemCommand(), ModelView::SetValueCommand::SetValueCommand(), execute_command(), and 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 MoveItemCommand::undo_command ( )
overrideprivatevirtual

Implements ModelView::AbstractItemCommand.

Definition at line 68 of file moveitemcommand.cpp.

69 {
70  // first find items
71  auto current_parent = itemFromPath(p_impl->target_parent_path);
72  auto target_parent = itemFromPath(p_impl->original_parent_path);
73 
74  // then make manipulations
75  auto taken = current_parent->takeItem(p_impl->target_tagrow);
76  target_parent->insertItem(taken, p_impl->original_tagrow);
77 
78  // adjusting new addresses
79  p_impl->target_parent_path = pathFromItem(current_parent);
80  p_impl->original_parent_path = pathFromItem(target_parent);
81 }

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

Here is the call graph for this function:

Member Data Documentation

◆ p_impl

std::unique_ptr<MoveItemCommandImpl> ModelView::MoveItemCommand::p_impl
private

Definition at line 38 of file moveitemcommand.h.

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


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