Skip to content
Snippets Groups Projects
Commit f8e2695c authored by Pospelov, Gennady's avatar Pospelov, Gennady
Browse files

New methods in ModelMapper to notify subscribers on item deletion

parent d4423818
No related branches found
No related tags found
No related merge requests found
......@@ -32,6 +32,13 @@ FitParameterAbsModel::FitParameterAbsModel(FitParameterContainerItem *fitParCont
m_columnNames.insert(ITEM_MAX, FitParameterItem::P_MAX);
connectModel(fitParContainer->model());
m_root_item->mapper()->setOnItemDestroy(
[this](SessionItem *parent) {
Q_ASSERT(parent == m_root_item);
m_root_item = 0;
});
}
Qt::ItemFlags FitParameterAbsModel::flags(const QModelIndex &index) const
......@@ -81,6 +88,8 @@ QModelIndex FitParameterAbsModel::index(int row, int column, const QModelIndex &
QModelIndex FitParameterAbsModel::parent(const QModelIndex &child) const
{
if(!m_root_item) return QModelIndex();
if (!child.isValid())
return QModelIndex();
......@@ -140,6 +149,8 @@ int FitParameterAbsModel::columnCount(const QModelIndex &parent) const
QVariant FitParameterAbsModel::data(const QModelIndex &index, int role) const
{
if(!m_root_item) return QVariant();
if ( !index.isValid() || index.column() < 0 || index.column() >= MAX_COLUMNS)
return QVariant();
......@@ -158,6 +169,8 @@ QVariant FitParameterAbsModel::data(const QModelIndex &index, int role) const
bool FitParameterAbsModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if(!m_root_item) return false;
if (!index.isValid())
return false;
if (SessionItem *item = itemForIndex(index)) {
......@@ -265,6 +278,7 @@ void FitParameterAbsModel::connectModel(QAbstractItemModel *sourceModel, bool is
connect(sourceModel, SIGNAL(modelAboutToBeReset()), this, SLOT(onSourceAboutToBeReset()));
}
else {
......
......@@ -72,6 +72,11 @@ void ModelMapper::setOnAnyChildChange(std::function<void (SessionItem *)> f, con
m_onAnyChildChange.push_back(call_item_t(f, caller));
}
void ModelMapper::setOnItemDestroy(std::function<void (SessionItem *)> f, const void *caller)
{
m_onItemDestroy.push_back(call_item_t(f, caller));
}
//! Cancells all subscribtion of given caller
void ModelMapper::unsubscribe(const void *caller)
{
......@@ -82,6 +87,7 @@ void ModelMapper::unsubscribe(const void *caller)
clean_container(m_onChildrenChange, caller);
clean_container(m_onSiblingsChange, caller);
clean_container(m_onAnyChildChange, caller);
clean_container(m_onItemDestroy, caller);
}
void ModelMapper::setModel(SessionModel *model)
......@@ -128,7 +134,7 @@ void ModelMapper::callOnValueChange()
f.first();
}
}
if(m_active) emit valueChange();
// if(m_active) emit valueChange();
}
void ModelMapper::callOnPropertyChange(const QString &name)
......@@ -138,7 +144,7 @@ void ModelMapper::callOnPropertyChange(const QString &name)
f.first(name);
}
}
if(m_active) emit propertyChange(name);
// if(m_active) emit propertyChange(name);
}
void ModelMapper::callOnChildPropertyChange(SessionItem *item, const QString &name)
......@@ -148,7 +154,7 @@ void ModelMapper::callOnChildPropertyChange(SessionItem *item, const QString &na
f.first(item, name);
}
}
if(m_active) emit childPropertyChange(item, name);
// if(m_active) emit childPropertyChange(item, name);
}
void ModelMapper::callOnParentChange(SessionItem *new_parent)
......@@ -158,7 +164,7 @@ void ModelMapper::callOnParentChange(SessionItem *new_parent)
f.first(new_parent);
}
}
if(m_active) emit parentChange(new_parent);
// if(m_active) emit parentChange(new_parent);
}
void ModelMapper::callOnChildrenChange(SessionItem *item)
......@@ -168,7 +174,7 @@ void ModelMapper::callOnChildrenChange(SessionItem *item)
f.first(item);
}
}
if(m_active) emit childrenChange(item);
// if(m_active) emit childrenChange(item);
}
void ModelMapper::callOnSiblingsChange()
......@@ -178,7 +184,7 @@ void ModelMapper::callOnSiblingsChange()
f.first();
}
}
if(m_active) emit siblingsChange();
// if(m_active) emit siblingsChange();
}
void ModelMapper::callOnAnyChildChange(SessionItem *item)
......@@ -188,7 +194,18 @@ void ModelMapper::callOnAnyChildChange(SessionItem *item)
f.first(item);
}
}
if(m_active) emit anyChildChange(item);
// if(m_active) emit anyChildChange(item);
}
//! Notifies subscribers if an item owning given mapper is about to be destroyed
void ModelMapper::callOnItemDestroy()
{
if (m_active && m_onItemDestroy.size() > 0) {
for (auto f : m_onItemDestroy) {
f.first(m_item);
}
}
// if(m_active) emit itemDestroy(new_parent);
}
void ModelMapper::clearMapper()
......@@ -202,6 +219,7 @@ void ModelMapper::clearMapper()
m_onChildrenChange.clear();
m_onSiblingsChange.clear();
m_onAnyChildChange.clear();
m_onItemDestroy.clear();
}
void ModelMapper::onDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight,
......
......@@ -54,8 +54,12 @@ public:
void setActive(bool state) {m_active = state;}
void setOnItemDestroy(std::function<void(SessionItem*)> f, const void *caller=0);
void unsubscribe(const void *caller);
void callOnItemDestroy();
signals:
void valueChange();
void propertyChange(const QString &name);
......@@ -64,6 +68,7 @@ signals:
void childrenChange(SessionItem *item);
void siblingsChange();
void anyChildChange(SessionItem *item);
void itemDestroy(SessionItem *item);
public slots:
void onDataChanged(const QModelIndex & topLeft, const QModelIndex & bottomRight,
......@@ -108,6 +113,7 @@ private:
std::vector<call_item_t> m_onChildrenChange;
std::vector<call_t> m_onSiblingsChange;
std::vector<call_item_t> m_onAnyChildChange;
std::vector<call_item_t> m_onItemDestroy;
QModelIndex m_aboutToDelete;
};
......
......@@ -57,6 +57,9 @@ SessionItem::SessionItem(const QString &modelType)
SessionItem::~SessionItem()
{
if(m_mapper)
m_mapper->callOnItemDestroy();
QVector<SessionItem*>::const_iterator it;
for (it = m_children.constBegin(); it != m_children.constEnd(); ++it) {
SessionItem *child = *it;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment