BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
JsonItemConverterTest::TestModel Class Reference
Inheritance diagram for JsonItemConverterTest::TestModel:
[legend]
Collaboration diagram for JsonItemConverterTest::TestModel:
[legend]

Public Member Functions

 TestModel ()
 
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...
 
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
 

Detailed Description

Definition at line 46 of file jsonitemconverter.test.cpp.

Constructor & Destructor Documentation

◆ TestModel()

JsonItemConverterTest::TestModel::TestModel ( )
inline

Definition at line 48 of file jsonitemconverter.test.cpp.

48  : SessionModel("TestModel")
49  {
50  auto catalogue = std::make_unique<ModelView::ItemCatalogue>();
51  catalogue->registerItem<TestItem>();
52  setItemCatalogue(std::move(catalogue));
53  }
SessionModel(std::string model_type={}, std::shared_ptr< ItemPool > pool={})
Main c-tor.
void setItemCatalogue(std::unique_ptr< ItemCatalogue > catalogue)
Sets brand new catalog of user-defined items.

Member Function Documentation

◆ clear()

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

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.
std::unique_ptr< SessionModelImpl > p_impl
Definition: sessionmodel.h:98
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(), ModelView::SessionModel::mapper(), ModelView::SessionModel::p_impl, ModelView::SessionModel::rootItem(), and ModelView::SessionModel::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 = {} 
)
inherited

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 ModelView::SessionModel::p_impl.

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

◆ data()

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

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
inherited

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 ModelView::SessionModel::p_impl.

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

◆ findItem()

◆ insertItem()

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

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 ModelView::SessionModel::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 = {} 
)
inherited

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(), ModelView::SessionModel::factory(), ModelView::SessionModel::intern_insert(), and ModelView::SessionModel::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 
)
privateinherited

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 ModelView::SessionModel::p_impl.

Referenced by ModelView::SessionModel::insertItem(), and ModelView::SessionModel::insertNewItem().

◆ intern_register()

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

Definition at line 228 of file sessionmodel.cpp.

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

References ModelView::SessionModel::modelType(), and ModelView::SessionModel::p_impl.

Referenced by ModelView::SessionModel::registerItem().

Here is the call graph for this function:

◆ mapper()

◆ modelType()

◆ moveItem()

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

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 ModelView::SessionModel::p_impl.

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

◆ registerInPool()

void SessionModel::registerInPool ( SessionItem item)
privateinherited

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 ModelView::SessionModel::p_impl.

Here is the call graph for this function:

◆ registerItem()

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

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 ModelView::SessionModel::intern_register(), and ModelView::SessionModel::modelType().

Referenced by TEST_F().

Here is the call graph for this function:

◆ removeItem()

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

Removes given row from parent.

Definition at line 96 of file sessionmodel.cpp.

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

References ModelView::SessionModel::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 
)
inherited

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 ModelView::SessionModel::p_impl.

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

◆ setItemCatalogue()

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

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 ModelView::SessionModel::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)
inherited

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 ModelView::SessionModel::p_impl.

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

◆ topItem()

template<typename T >
T * SessionModel::topItem
inherited

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
inherited

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 ModelView::SessionModel::rootItem().

Referenced by TEST_F().

Here is the call graph for this function:

◆ undoStack()

◆ unregisterFromPool()

void SessionModel::unregisterFromPool ( SessionItem item)
privateinherited

Unregister item from pool.

Definition at line 215 of file sessionmodel.cpp.

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

References ModelView::SessionModel::p_impl.

Member Data Documentation

◆ p_impl


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