35 bool isValidItemRole(
const ViewItem* view,
int item_role)
62 const std::string msg(
"Error in ViewModelController: ");
64 throw std::runtime_error(msg +
"ViewModel is not defined");
67 throw std::runtime_error(msg +
"RowStrategy is not defined");
70 throw std::runtime_error(msg +
"Children is not defined");
87 auto next_parent = row.at(0).get();
103 auto view = pos->second;
111 for (
auto child : view->
children()) {
113 [child](
const auto& it) { return it.second == child; });
133 auto parent_view = pos->second;
137 auto next_parent = row.at(0).get();
140 parent_view = next_parent;
150 std::vector<ViewItem*> result;
151 auto on_index = [&](
const QModelIndex& index) {
153 if (view_item->item() == item)
154 result.push_back(view_item);
190 auto on_model_destroyed = [
this](
auto) {
191 p_impl->m_viewModel->setRootViewItem(std::make_unique<RootViewItem>(
nullptr));
195 auto on_model_reset = [
this](
auto) {
197 p_impl->setRootSessionItemIntern(root_item ? root_item :
model()->rootItem());
198 p_impl->m_viewModel->endResetModel();
202 auto on_model_about_to_be_reset = [
this](
auto) {
p_impl->m_viewModel->beginResetModel(); };
208 p_impl->m_viewModel = view_model;
214 std::unique_ptr<ChildrenStrategyInterface> children_strategy)
216 p_impl->m_childrenStrategy = std::move(children_strategy);
221 p_impl->m_rowStrategy = std::move(row_strategy);
234 throw std::runtime_error(
235 "Error in ViewModelController: atttemp to set nulptr as root item");
238 throw std::runtime_error(
239 "Error in ViewModelController: atttemp to use item from alien model as new root.");
241 p_impl->m_viewModel->beginResetModel();
242 p_impl->setRootSessionItemIntern(item);
243 p_impl->m_viewModel->endResetModel();
248 return p_impl->m_viewModel->rootItem()->item();
255 return p_impl->findViews(item);
260 return p_impl->m_rowStrategy->horizontalHeaderLabels();
267 if (isValidItemRole(view, role)) {
268 auto index =
p_impl->m_viewModel->indexFromItem(view);
276 p_impl->insert_view(parent, tagrow);
283 auto item_to_remove = parent->
getItem(tagrow.
tag, tagrow.
row);
288 p_impl->m_viewModel->beginResetModel();
289 p_impl->m_viewModel->setRootViewItem(std::make_unique<RootViewItem>(
nullptr));
290 p_impl->m_itemToVview.clear();
291 p_impl->m_rootItemPath = {};
292 p_impl->m_viewModel->endResetModel();
294 p_impl->remove_row_of_views(item_to_remove);
304 for (
auto view : views)
305 p_impl->remove_children_of_view(view);
307 p_impl->iterate(item, views.at(0));
void setOnItemRemoved(Callbacks::item_tagrow_t f, Callbacks::slot_t client={}) override
Sets callback to be notified on item remove.
void setOnItemInserted(Callbacks::item_tagrow_t f, Callbacks::slot_t client={}) override
Sets callback to be notified on item insert.
void setOnModelDestroyed(Callbacks::model_t f, Callbacks::slot_t client={}) override
Sets the callback for notifications on model destruction.
void setOnAboutToRemoveItem(Callbacks::item_tagrow_t f, Callbacks::slot_t client={}) override
Sets callback to be notified when the item is about to be removed.
void setOnModelAboutToBeReset(Callbacks::model_t f, Callbacks::slot_t client={}) override
Sets the callback to be notified before model's full reset (root item recreated).
void setOnDataChange(Callbacks::item_int_t f, Callbacks::slot_t client={}) override
Sets callback to be notified on item's data change.
void setOnModelReset(Callbacks::model_t f, Callbacks::slot_t client={}) override
Sets the callback to be notified after model was fully reset (root item recreated).
Templated class for all objects willing to listen for changes in concrete SessionModel.
SessionModel * model() const
Supports navigation through SessionModel.
The main object representing an editable/displayable/serializable entity.
SessionItem * getItem(const std::string &tag, int row=0) const
Returns item at given row of given tag.
SessionModel * model() const
Returns the model to which given item belongs to.
Main class to hold hierarchy of SessionItem objects.
Aggregate to hold (tag, row) information for SessionModel.
Represents the view of SessionItem's data in a single cell of ViewModel.
int row() const
Returns the row where the item is located in its parent's child table, or -1 if the item has no paren...
std::vector< ViewItem * > children() const
ViewItem * parent() const
SessionItem * item() const
Base class for all view models to show content of SessionModel in Qt views.
void removeRow(ViewItem *parent, int row)
void appendRow(ViewItem *parent, std::vector< std::unique_ptr< ViewItem >> items)
Appends row of items to given parent.
ViewItem * rootItem() const
Returns a pointer to invisible root item.
void clearRows(ViewItem *parent)
void insertRow(ViewItem *parent, int row, std::vector< std::unique_ptr< ViewItem >> items)
Insert a row of items at index 'row' to given parent.
void setRootViewItem(std::unique_ptr< ViewItem > root_item)
Sets new root item. Previous item will be deleted, model will be reset.
ViewItem * itemFromIndex(const QModelIndex &index) const
Returns a pointer to the RefViewItem associated with the given index.
Propagates changes from SessionModel to its ViewModelBase.
void setChildrenStrategy(std::unique_ptr< ChildrenStrategyInterface > children_strategy)
SessionItem * rootSessionItem() const
void setRootSessionItem(SessionItem *item)
std::unique_ptr< ViewModelControllerImpl > p_impl
virtual void onAboutToRemoveItem(SessionItem *parent, TagRow tagrow)
std::vector< ViewItem * > findViews(const ModelView::SessionItem *item) const
Returns all ViewItem's displaying given SessionItem.
virtual void onItemRemoved(SessionItem *parent, TagRow tagrow)
void update_branch(const SessionItem *item)
ViewModelController(SessionModel *session_model, ViewModelBase *view_model=nullptr)
void setViewModel(ViewModelBase *view_model)
SessionModel * sessionModel() const
Returns SessionModel handled by this controller.
virtual void onDataChange(SessionItem *item, int role)
void setRowStrategy(std::unique_ptr< RowStrategyInterface > row_strategy)
QStringList horizontalHeaderLabels() const
virtual void onItemInserted(SessionItem *parent, TagRow tagrow)
const int TOOLTIP
tooltip for item's data
const int APPEARANCE
appearance flag
MVVM_VIEWMODEL_EXPORT QVector< int > ItemRoleToQtRole(int role)
Returns vector of Qt roles corresponding to given ItemDataRole.
int IndexOfItem(It begin, It end, const T &item)
Returns index corresponding to the first occurance of the item in the container.
MVVM_VIEWMODEL_EXPORT void iterate_model(const QAbstractItemModel *model, const QModelIndex &parent, const std::function< void(const QModelIndex &child)> &fun)
Iterates through QAbstractItem model.
MVVM_MODEL_EXPORT SessionItem * ItemFromPath(const SessionModel &moodel, const Path &path)
Returns item found in the model following given Path.
MVVM_MODEL_EXPORT Path PathFromItem(const SessionItem *item)
Constructs path to find given item. Item must belong to a model.
MVVM_MODEL_EXPORT bool IsItemAncestor(const SessionItem *item, const SessionItem *candidate)
Returns true if 'candidate' is one of ancestor of given item.
materialitems.h Collection of materials to populate MaterialModel.
std::unique_ptr< RowStrategyInterface > m_rowStrategy
void check_initialization()
Path m_rootItemPath
correspondence of item and its view
std::unique_ptr< ChildrenStrategyInterface > m_childrenStrategy
ViewModelController * m_self
void remove_children_of_view(ViewItem *view)
std::vector< ViewItem * > findViews(const SessionItem *item) const
ViewModelBase * m_viewModel
std::map< SessionItem *, ViewItem * > m_itemToVview
void setRootSessionItemIntern(SessionItem *item)
void iterate(const SessionItem *item, ViewItem *parent)
void remove_row_of_views(SessionItem *item)
Remove row of ViewItem's corresponding to given item.
ViewModelControllerImpl(ViewModelController *controller, ViewModelBase *view_model)
void insert_view(SessionItem *parent, const TagRow &tagrow)