Commit 5ccd389a authored by Gerrit Erichsen's avatar Gerrit Erichsen
Browse files

LocationsTableModel -> NodesTableModel communication implemented

parent b867f987
......@@ -567,6 +567,11 @@ void CharLGui::makeConnections()
this, SLOT(onLockConfiguration(bool)));
connect(m_calculationWidget, SIGNAL(requestRunAction(bool)),
this, SLOT(onRunClicked(bool)));
connect(m_modelLocations, &LocationsTableModel::removedLocation,
m_modelNodes, &NodesTableModel::onLocationRemoved);
connect(m_modelLocations, &LocationsTableModel::changedNode,
m_modelNodes, &NodesTableModel::onLocationChangedNode);
connect(m_nodeWidget, &NodesWidget::nodesCreated, this, &CharLGui::matchLocationsToNodesDialog);
connect(m_locationsWidget, &LocationsWidget::locationsCreated, this, &CharLGui::matchLocationsToNodesDialog);
......
......@@ -225,7 +225,8 @@ QVariant LocationsTableModel::data(const QModelIndex &index, int role) const
}
else
{
return m_data[index.row()]->getNodeID();
int id = m_data[index.row()]->getNodeID();
return m_nodesTableModel->data(m_nodesTableModel->index(id, 0));
}
default:
return QVariant();
......@@ -406,8 +407,14 @@ bool LocationsTableModel::setData(const QModelIndex &index, const QVariant &valu
value.toInt(&converted);
if (converted)
{
int old = m_data[index.row()]->getNodeID();
m_data[index.row()]->setNodeID(value.toInt());
emit dataChanged(index, index);
if (old != m_data[index.row()]->getNodeID())
{
emit dataChanged(index, index);
emit changedNode(index.row(), old,
m_data[index.row()]->getNodeID());
}
return true;
}
return false;
......@@ -582,16 +589,16 @@ void LocationsTableModel::addItem()
/// \return if the index was valid
/// if index was valid, remove the item of the data vector at given index.
/////////////////////////////////////////////////////////////////////////////////////
bool LocationsTableModel::removeItem(const QModelIndex &index)
bool LocationsTableModel::removeItem(const QModelIndex & index)
{
if (index.row() >= 0 && index.row() < this->rowCount())
{
emit layoutAboutToBeChanged();
// if commented line is executed, all rows disappear for some reason
// beginRemoveRows(QModelIndex(), index.row(), index.row());
beginRemoveRows(QModelIndex(), index.row(), index.row());
m_data.remove(index.row());
endRemoveRows();
emit layoutChanged();
// endRemoveRows();
emit removedLocation(index.row());
return true;
}
return false;
......
......@@ -150,6 +150,10 @@ public:
bool matchLocationsToNodes();
void setNodesTableModel(NodesTableModel * nodesTableModel);
signals:
void changedNode(int locationId, int previousNode, int newNode);
void removedLocation(int formerId);
private:
QVector<LocationsTableItem*> m_data; // collection of table items
......
......@@ -54,14 +54,9 @@ int NodesTableItem::getLocationsId(int index) const
return -1;
}
void NodesTableItem::setMapPolygon(QPolygonF mapPolygon)
bool NodesTableItem::hasLocation(int locationId) const
{
m_mapPolygon = mapPolygon;
if (m_polygonObject != nullptr)
{
m_polygonObject->setGeoPoly(mapPolygon);
}
return m_locations.contains(locationId);
}
void NodesTableItem::setPolygonObject(PolygonObject *polygonObject)
......@@ -93,6 +88,20 @@ void NodesTableItem::removeLocation(int id)
}
}
//////////
/// \brief NodesTableItem::downgradeAllIdsAbove
/// \param removedId
/// assumes that removedId was deleted and therefore all other ids with a value above removedId
/// are down by one. this functions adjusts the ids this instance contains accordingly
/////////
void NodesTableItem::downgradeAllIdsAbove(int removedId)
{
for (auto id : m_locations)
{
if (id > removedId) --id;
}
}
//////////////////////////////////////////
/// MODEL IMPLEMENTATION //////////////////////////////////////////////////////////////////////
//////////////////////////////////////////
......@@ -364,3 +373,27 @@ NodesTableItem * NodesTableModel::getItemNonConst(int row)
}
return nullptr;
}
void NodesTableModel::onLocationChangedNode(int locationId, int oldNodeIndex, int newNodeIndex)
{
if (oldNodeIndex >= 0 && oldNodeIndex < m_data.count())
{
m_data[oldNodeIndex]->removeLocation(locationId);
}
if (newNodeIndex >= 0 && newNodeIndex < m_data.count())
{
m_data[newNodeIndex]->addLocation(locationId);
}
}
void NodesTableModel::onLocationRemoved(int idLocationHad)
{
for (auto item : m_data)
{
if (item->hasLocation(idLocationHad))
{
item->removeLocation(idLocationHad);
}
item->downgradeAllIdsAbove(idLocationHad);
}
}
......@@ -56,6 +56,7 @@ public:
PolygonObject * getPolygonObject() const;
int getLocationCount() const;
int getLocationsId(int index) const;
bool hasLocation(int locationId) const;
//set functions ---------------------------------------
void setMapPolygon(QPolygonF mapPolygon);
......@@ -68,6 +69,7 @@ public:
//locations associated functions
void addLocation(int id);
void removeLocation(int id);
void downgradeAllIdsAbove(int removedId);
private:
//properties of Nodes accessible by tablemodel ----
......@@ -108,6 +110,12 @@ public:
bool removeItem(const QModelIndex &index);
virtual const AbstractCharLItem * getItem(int row) const;
virtual NodesTableItem * getItemNonConst(int row);
public slots:
void onLocationChangedNode(int locationId,
int oldNodeIndex,
int newNodeIndex);
void onLocationRemoved(int idLocationHad);
signals:
void nameChanged(QModelIndex, QString);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment