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

Main class to hold hierarchy of SessionItem objects. More...

Inheritance diagram for ModelView::SessionModel:
[legend]

Public Member Functions

 SessionModel (const SessionModel &other)=delete
 
 SessionModel (std::string model_type={}, std::shared_ptr< ItemPool > pool={})
 Main c-tor. More...
 
virtual ~SessionModel ()
 
void clear (std::function< void(SessionItem *)> callback={})
 Removes all items from the model. More...
 
SessionItemcopyItem (const SessionItem *item, SessionItem *parent, const TagRow &tagrow={})
 Copy item and insert it in parent's tag and row. Item could belong to any model/parent. More...
 
Variant data (SessionItem *item, int role) const
 Returns the data for given item and role. More...
 
const ItemFactoryInterfacefactory () const
 Returns item factory which can generate all items supported by this model. More...
 
SessionItemfindItem (const identifier_type &id)
 Returns SessionItem for given identifier. More...
 
template<typename T >
T * insertItem (SessionItem *parent=nullptr, const TagRow &tagrow={})
 Inserts item into given parent under given tagrow. More...
 
SessionIteminsertNewItem (const model_type &modelType, SessionItem *parent=nullptr, const TagRow &tagrow={})
 Insert new item using item's modelType. More...
 
ModelMappermapper ()
 Returns model mapper. Can be used to subscribe to various model's signal. More...
 
std::string modelType () const
 Returns model type. More...
 
void moveItem (SessionItem *item, SessionItem *new_parent, const TagRow &tagrow)
 Move item from it's current parent to a new parent under given tag and row. More...
 
SessionModeloperator= (const SessionModel &other)=delete
 
template<typename T >
void registerItem (const std::string &label={})
 Register used defined item to use with the model. More...
 
void removeItem (SessionItem *parent, const TagRow &tagrow)
 Removes given row from parent. More...
 
SessionItemrootItem () const
 Returns root item of the model. More...
 
bool setData (SessionItem *item, const Variant &value, int role)
 Sets the data for given item. More...
 
void setItemCatalogue (std::unique_ptr< ItemCatalogue > catalogue)
 Sets brand new catalog of user-defined items. More...
 
void setUndoRedoEnabled (bool value)
 Sets undo/redo either enabled or disabled. By default undo/redo is disabled. More...
 
template<typename T = SessionItem>
T * topItem () const
 Returns top item of the given type. More...
 
template<typename T = SessionItem>
std::vector< T * > topItems () const
 Returns top items of the given type. More...
 
UndoStackInterfaceundoStack () const
 Returns command stack to perform undo/redo. More...
 

Private Member Functions

SessionItemintern_insert (const item_factory_func_t &func, SessionItem *parent, const TagRow &tagrow)
 Insert new item into given parent using factory function provided. More...
 
void intern_register (const model_type &modelType, const item_factory_func_t &func, const std::string &label)
 
void registerInPool (SessionItem *item)
 Registers item in pool. This will allow to find item pointer using its unique identifier. More...
 
void unregisterFromPool (SessionItem *item)
 Unregister item from pool. More...
 

Private Attributes

std::unique_ptr< SessionModelImpl > p_impl
 

Friends

class SessionItem
 

Detailed Description

Main class to hold hierarchy of SessionItem objects.

Definition at line 37 of file sessionmodel.h.

Constructor & Destructor Documentation

◆ SessionModel() [1/2]

SessionModel::SessionModel ( std::string  model_type = {},
std::shared_ptr< ItemPool pool = {} 
)
explicit

Main c-tor.

Definition at line 66 of file sessionmodel.cpp.

67  : p_impl(std::make_unique<SessionModelImpl>(this, std::move(model_type), std::move(pool)))
68 
69 {
70  p_impl->createRootItem();
71 }
std::unique_ptr< SessionModelImpl > p_impl
Definition: sessionmodel.h:98
std::string model_type
Definition: types.h:23

References p_impl.

◆ ~SessionModel()

SessionModel::~SessionModel ( )
virtual

Definition at line 73 of file sessionmodel.cpp.

74 {
75  // Explicitely call root item's destructor. It uses p_impl pointer during own descruction
76  // and we have to keep pimpl pointer intact. Without line below will crash on MacOS because
77  // of pecularities of MacOS libc++. See explanations here:
78  // http://ibob.github.io/blog/2019/11/07/dont-use-unique_ptr-for-pimpl/
79  p_impl->m_root_item.reset();
80 
81  p_impl->m_mapper->callOnModelDestroyed();
82 }

References p_impl.

◆ SessionModel() [2/2]

ModelView::SessionModel::SessionModel ( const SessionModel other)
delete

Member Function Documentation

◆ clear()

void SessionModel::clear ( std::function< void(SessionItem *)>  callback = {})

Removes all items from the model.

If callback is provided, use it to rebuild content of root item (used while restoring the model from serialized content).

Definition at line 194 of file sessionmodel.cpp.

195 {
196  if (undoStack())
197  undoStack()->clear();
199  p_impl->createRootItem();
200  if (callback)
201  callback(rootItem());
203 }
ModelMapper * mapper()
Returns model mapper. Can be used to subscribe to various model's signal.
SessionItem * rootItem() const
Returns root item of the model.
UndoStackInterface * undoStack() const
Returns command stack to perform undo/redo.

References ModelView::ModelMapper::callOnModelAboutToBeReset(), ModelView::ModelMapper::callOnModelReset(), ModelView::UndoStackInterface::clear(), mapper(), p_impl, rootItem(), and undoStack().

Referenced by gui2::SLDElementController::clearScene(), ModelView::JsonModelConverter::from_json(), and TEST_F().

Here is the call graph for this function:

◆ copyItem()

SessionItem * SessionModel::copyItem ( const SessionItem item,
SessionItem parent,
const TagRow tagrow = {} 
)

Copy item and insert it in parent's tag and row. Item could belong to any model/parent.

Definition at line 111 of file sessionmodel.cpp.

113 {
114  return p_impl->m_commands->copyItem(item, parent, tagrow);
115 }

References p_impl.

Referenced by gui2::MaterialModel::cloneMaterial(), and TEST_F().

◆ data()

Variant SessionModel::data ( SessionItem item,
int  role 
) const

Returns the data for given item and role.

Definition at line 119 of file sessionmodel.cpp.

120 {
121  return item->data<Variant>(role);
122 }
T data(int role=ItemDataRole::DATA) const
Returns data of given type T for given role.
Definition: sessionitem.h:148
QVariant Variant
Definition: variant.h:23

References ModelView::SessionItem::data().

Referenced by gui2::ExperimentalDataModel::addGraph(), and TEST_F().

Here is the call graph for this function:

◆ factory()

const ItemFactoryInterface * SessionModel::factory ( ) const

Returns item factory which can generate all items supported by this model.

Definition at line 161 of file sessionmodel.cpp.

162 {
163  return p_impl->m_itemManager->factory();
164 }

References p_impl.

Referenced by InsertNewItemCommandTest::create_command(), ModelView::CreateItemBackupStrategy(), ModelView::CreateItemCopyStrategy(), ModelView::JsonModelConverter::from_json(), insertNewItem(), TEST_F(), and ModelView::JsonModelConverter::to_json().

◆ findItem()

◆ insertItem()

template<typename T >
T * SessionModel::insertItem ( SessionItem parent = nullptr,
const TagRow tagrow = {} 
)

Inserts item into given parent under given tagrow.

Definition at line 104 of file sessionmodel.h.

105 {
106  return static_cast<T*>(intern_insert(ItemFactoryFunction<T>(), parent, tagrow));
107 }
SessionItem * intern_insert(const item_factory_func_t &func, SessionItem *parent, const TagRow &tagrow)
Insert new item into given parent using factory function provided.

References intern_insert().

Referenced by ModelView::Data1DItem::setAxis(), TEST(), and TEST_F().

Here is the call graph for this function:

◆ insertNewItem()

SessionItem * SessionModel::insertNewItem ( const model_type modelType,
SessionItem parent = nullptr,
const TagRow tagrow = {} 
)

Insert new item using item's modelType.

Definition at line 86 of file sessionmodel.cpp.

88 {
89  // intentionally passing by value inside lambda
90  auto create_func = [this, modelType]() { return factory()->createItem(modelType); };
91  return intern_insert(create_func, parent, tagrow);
92 }
virtual std::unique_ptr< SessionItem > createItem(const model_type &modelType) const =0
std::string modelType() const
Returns model type.
const ItemFactoryInterface * factory() const
Returns item factory which can generate all items supported by this model.

References ModelView::ItemFactoryInterface::createItem(), factory(), intern_insert(), and modelType().

Referenced by TEST_F().

Here is the call graph for this function:

◆ intern_insert()

SessionItem * SessionModel::intern_insert ( const item_factory_func_t func,
SessionItem parent,
const TagRow tagrow 
)
private

Insert new item into given parent using factory function provided.

Definition at line 222 of file sessionmodel.cpp.

224 {
225  return p_impl->m_commands->insertNewItem(func, parent, tagrow);
226 }

References p_impl.

Referenced by insertItem(), and insertNewItem().

◆ intern_register()

void SessionModel::intern_register ( const model_type modelType,
const item_factory_func_t func,
const std::string &  label 
)
private

Definition at line 228 of file sessionmodel.cpp.

230 {
231  p_impl->m_itemManager->factory()->registerItem(modelType, func, label);
232 }

References modelType(), and p_impl.

Referenced by registerItem().

Here is the call graph for this function:

◆ mapper()

◆ modelType()

std::string SessionModel::modelType ( ) const

Returns model type.

Definition at line 133 of file sessionmodel.cpp.

134 {
135  return p_impl->m_modelType;
136 }

References p_impl.

Referenced by ModelView::JsonModelConverter::from_json(), insertNewItem(), intern_register(), registerItem(), ModelView::ProjectUtils::SuggestFileName(), and ModelView::JsonModelConverter::to_json().

◆ moveItem()

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

Move item from it's current parent to a new parent under given tag and row.

Old and new parents should belong to this model.

Definition at line 104 of file sessionmodel.cpp.

105 {
106  p_impl->m_commands->moveItem(item, new_parent, tagrow);
107 }

References p_impl.

Referenced by gui2::DataViewModel::dropMimeData(), gui2::ExperimentalDataModel::mergeCanvases(), ModelView::Utils::MoveDown(), ModelView::Utils::MoveUp(), and TEST_F().

◆ operator=()

SessionModel& ModelView::SessionModel::operator= ( const SessionModel other)
delete

◆ registerInPool()

void SessionModel::registerInPool ( SessionItem item)
private

Registers item in pool. This will allow to find item pointer using its unique identifier.

Definition at line 207 of file sessionmodel.cpp.

208 {
209  p_impl->m_itemManager->registerInPool(item);
210  item->activate(); // activates buisiness logic
211 }
virtual void activate()
Definition: sessionitem.h:123

References ModelView::SessionItem::activate(), and p_impl.

Here is the call graph for this function:

◆ registerItem()

template<typename T >
void SessionModel::registerItem ( const std::string &  label = {})

Register used defined item to use with the model.

It will become possible to undo/redo operations with this item, as well as serialize it to/from JSON.

Definition at line 135 of file sessionmodel.h.

136 {
137  intern_register(T().modelType(), ItemFactoryFunction<T>(), label);
138 }
void intern_register(const model_type &modelType, const item_factory_func_t &func, const std::string &label)

References intern_register(), and modelType().

Referenced by TEST_F().

Here is the call graph for this function:

◆ removeItem()

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

Removes given row from parent.

Definition at line 96 of file sessionmodel.cpp.

97 {
98  p_impl->m_commands->removeItem(parent, tagrow);
99 }

References p_impl.

Referenced by ModelView::Utils::DeleteItemFromModel(), gui2::ExperimentalDataModel::mergeCanvases(), gui2::ExperimentalDataModel::removeCanvas(), gui2::ExperimentalDataModel::removeGraph(), TEST(), and TEST_F().

◆ rootItem()

◆ setData()

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

Sets the data for given item.

Definition at line 126 of file sessionmodel.cpp.

127 {
128  return p_impl->m_commands->setData(item, value, role);
129 }

References p_impl.

Referenced by ModelView::SessionItem::set_data_internal(), TEST(), and TEST_F().

◆ setItemCatalogue()

void SessionModel::setItemCatalogue ( std::unique_ptr< ItemCatalogue catalogue)

Sets brand new catalog of user-defined items.

They become available for undo/redo and serialization. Internally user catalog will be merged with the catalog of standard items.

Definition at line 176 of file sessionmodel.cpp.

177 {
178  // adding standard items to the user catalogue
179  std::unique_ptr<ItemCatalogue> full_catalogue = std::move(catalogue);
180  full_catalogue->merge(*CreateStandardItemCatalogue());
181  p_impl->m_itemManager->setItemFactory(std::make_unique<ItemFactory>(std::move(full_catalogue)));
182 }
MVVM_MODEL_EXPORT std::unique_ptr< ItemCatalogue > CreateStandardItemCatalogue()
Creates a catalog of items supported by SessionModel out-of-the-box.

References ModelView::CreateStandardItemCatalogue(), and p_impl.

Referenced by ToyItems::SampleModel::SampleModel(), gui2::SampleModel::SampleModel(), gui2::SLDElementModel::SLDElementModel(), gui2::ExperimentalDataModel::init_model(), and gui2::MaterialModel::init_model().

Here is the call graph for this function:

◆ setUndoRedoEnabled()

void SessionModel::setUndoRedoEnabled ( bool  value)

Sets undo/redo either enabled or disabled. By default undo/redo is disabled.

Definition at line 186 of file sessionmodel.cpp.

187 {
188  p_impl->m_commands->setUndoRedoEnabled(value);
189 }

References p_impl.

Referenced by gui2::ExperimentalDataModel::init_model(), and TEST_F().

◆ topItem()

template<typename T >
T * SessionModel::topItem

Returns top item of the given type.

If more than one item exists, return the first one. The top item is an item that is a child of an invisible root item.

Definition at line 126 of file sessionmodel.h.

127 {
128  auto items = topItems<T>();
129  return items.empty() ? nullptr : items.front();
130 }

Referenced by gui2::SimPlotController::onInstrumentChange(), gui2::QuickSimController::process_multilayer(), gui2::LayerEditorWidget::setModels(), gui2::InstrumentPropertyEditor::setModels(), gui2::QuickSimController::submit_specular_simulation(), and TEST_F().

◆ topItems()

template<typename T >
std::vector< T * > SessionModel::topItems

Returns top items of the given type.

The top item is an item that is a child of an invisible root item.

Definition at line 112 of file sessionmodel.h.

113 {
114  std::vector<T*> result;
115  for (auto child : rootItem()->children()) {
116  if (auto item = dynamic_cast<T*>(child))
117  result.push_back(item);
118  }
119 
120  return result;
121 }

References rootItem().

Referenced by TEST_F().

Here is the call graph for this function:

◆ undoStack()

◆ unregisterFromPool()

void SessionModel::unregisterFromPool ( SessionItem item)
private

Unregister item from pool.

Definition at line 215 of file sessionmodel.cpp.

216 {
217  p_impl->m_itemManager->unregisterFromPool(item);
218 }

References p_impl.

Friends And Related Function Documentation

◆ SessionItem

friend class SessionItem
friend

Definition at line 90 of file sessionmodel.h.

Member Data Documentation

◆ p_impl


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