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

Provides undo/redo for all commands of SessionModel. More...

Collaboration diagram for ModelView::CommandService:
[legend]

Public Member Functions

 CommandService (SessionModel *model)
 
SessionItemcopyItem (const SessionItem *item, SessionItem *parent, const TagRow &tagrow)
 
SessionIteminsertNewItem (const item_factory_func_t &func, SessionItem *parent, const TagRow &tagrow)
 
void moveItem (SessionItem *item, SessionItem *new_parent, const TagRow &tagrow)
 
void removeItem (SessionItem *parent, const TagRow &tagrow)
 
void setCommandRecordPause (bool value)
 
bool setData (SessionItem *item, const Variant &value, int role)
 
void setUndoRedoEnabled (bool value)
 
UndoStackInterfaceundoStack () const
 

Private Member Functions

template<typename C , typename... Args>
CommandResult process_command (Args &&... args)
 Creates and processes command of given type using given argument list. More...
 
bool provideUndo () const
 

Private Attributes

std::unique_ptr< UndoStackInterfacem_commands
 
SessionModelm_model
 
bool m_pause_record
 

Detailed Description

Provides undo/redo for all commands of SessionModel.

Definition at line 35 of file commandservice.h.

Constructor & Destructor Documentation

◆ CommandService()

CommandService::CommandService ( SessionModel model)

Definition at line 27 of file commandservice.cpp.

27 : m_model(model), m_pause_record(false) {}

Member Function Documentation

◆ copyItem()

SessionItem * CommandService::copyItem ( const SessionItem item,
SessionItem parent,
const TagRow tagrow 
)

Definition at line 49 of file commandservice.cpp.

51 {
52  if (!item)
53  return nullptr;
54 
55  if (parent->model() != m_model)
56  throw std::runtime_error(
57  "CommandService::copyItem() -> Item doesn't belong to given model");
58 
59  int actual_row = tagrow.row < 0 ? parent->itemCount(tagrow.tag) : tagrow.row;
60 
61  return std::get<SessionItem*>(
62  process_command<CopyItemCommand>(item, parent, TagRow{tagrow.tag, actual_row}));
63 }
int itemCount(const std::string &tag) const
Returns number of items in given tag.
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

References ModelView::SessionItem::itemCount(), m_model, ModelView::SessionItem::model(), ModelView::TagRow::row, and ModelView::TagRow::tag.

Here is the call graph for this function:

◆ insertNewItem()

SessionItem * CommandService::insertNewItem ( const item_factory_func_t func,
SessionItem parent,
const TagRow tagrow 
)

Definition at line 37 of file commandservice.cpp.

39 {
40  if (!parent)
41  parent = m_model->rootItem();
42 
43  int actual_row = tagrow.row < 0 ? parent->itemCount(tagrow.tag) : tagrow.row;
44 
45  return std::get<SessionItem*>(
46  process_command<InsertNewItemCommand>(func, parent, TagRow{tagrow.tag, actual_row}));
47 }
SessionItem * rootItem() const
Returns root item of the model.

References ModelView::SessionItem::itemCount(), m_model, ModelView::SessionModel::rootItem(), ModelView::TagRow::row, and ModelView::TagRow::tag.

Here is the call graph for this function:

◆ moveItem()

void CommandService::moveItem ( SessionItem item,
SessionItem new_parent,
const TagRow tagrow 
)

Definition at line 82 of file commandservice.cpp.

83 {
84  if (item->model() != m_model)
85  throw std::runtime_error(
86  "CommandService::removeRow() -> Item doesn't belong to given model");
87 
88  if (new_parent->model() != m_model)
89  throw std::runtime_error(
90  "CommandService::removeRow() -> Parent doesn't belong to given model");
91 
92  int actual_row = tagrow.row < 0 ? new_parent->itemCount(tagrow.tag) : tagrow.row;
93 
94  process_command<MoveItemCommand>(item, new_parent, TagRow{tagrow.tag, actual_row});
95 }

References ModelView::SessionItem::itemCount(), m_model, ModelView::SessionItem::model(), ModelView::TagRow::row, and ModelView::TagRow::tag.

Here is the call graph for this function:

◆ process_command()

template<typename C , typename... Args>
CommandResult ModelView::CommandService::process_command ( Args &&...  args)
private

Creates and processes command of given type using given argument list.

Definition at line 69 of file commandservice.h.

70 {
71  if (provideUndo()) {
72  // making shared because underlying QUndoStack requires ownership
73  auto command = std::make_shared<C>(std::forward<Args>(args)...);
74  m_commands->execute(command);
75  return command->result();
76  } else {
77  C command(std::forward<Args>(args)...);
78  command.execute();
79  return command.result();
80  }
81 }
std::unique_ptr< UndoStackInterface > m_commands

References m_commands, and provideUndo().

Here is the call graph for this function:

◆ provideUndo()

bool CommandService::provideUndo ( ) const
private

Definition at line 107 of file commandservice.cpp.

108 {
109  return m_commands && !m_pause_record;
110 }

References m_commands, and m_pause_record.

Referenced by process_command().

◆ removeItem()

void CommandService::removeItem ( SessionItem parent,
const TagRow tagrow 
)

Definition at line 73 of file commandservice.cpp.

74 {
75  if (parent->model() != m_model)
76  throw std::runtime_error(
77  "CommandService::removeRow() -> Item doesn't belong to given model");
78 
79  process_command<RemoveItemCommand>(parent, tagrow);
80 }

References m_model, and ModelView::SessionItem::model().

Here is the call graph for this function:

◆ setCommandRecordPause()

void CommandService::setCommandRecordPause ( bool  value)

Definition at line 102 of file commandservice.cpp.

103 {
104  m_pause_record = value;
105 }

References m_pause_record.

◆ setData()

bool CommandService::setData ( SessionItem item,
const Variant value,
int  role 
)

Definition at line 65 of file commandservice.cpp.

66 {
67  if (!item)
68  return false;
69 
70  return std::get<bool>(process_command<SetValueCommand>(item, value, role));
71 }

◆ setUndoRedoEnabled()

void CommandService::setUndoRedoEnabled ( bool  value)

Definition at line 29 of file commandservice.cpp.

30 {
31  if (value)
32  m_commands = std::make_unique<UndoStack>();
33  else
34  m_commands.reset();
35 }

References m_commands.

◆ undoStack()

UndoStackInterface * CommandService::undoStack ( ) const

Definition at line 97 of file commandservice.cpp.

98 {
99  return m_commands.get();
100 }

References m_commands.

Member Data Documentation

◆ m_commands

std::unique_ptr<UndoStackInterface> ModelView::CommandService::m_commands
private

Definition at line 62 of file commandservice.h.

Referenced by process_command(), provideUndo(), setUndoRedoEnabled(), and undoStack().

◆ m_model

SessionModel* ModelView::CommandService::m_model
private

Definition at line 61 of file commandservice.h.

Referenced by copyItem(), insertNewItem(), moveItem(), and removeItem().

◆ m_pause_record

bool ModelView::CommandService::m_pause_record
private

Definition at line 63 of file commandservice.h.

Referenced by provideUndo(), and setCommandRecordPause().


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