add proper info on model changes

This commit is contained in:
Miodrag Milanovic 2018-07-28 18:48:32 +02:00
parent 9a30b6330b
commit ba2531edc0
2 changed files with 27 additions and 8 deletions

View File

@ -160,6 +160,10 @@ DesignWidget::DesignWidget(QWidget *parent) : QWidget(parent), ctx(nullptr), sel
connect(treeView, &QTreeWidget::customContextMenuRequested, this, &DesignWidget::prepareMenuTree); connect(treeView, &QTreeWidget::customContextMenuRequested, this, &DesignWidget::prepareMenuTree);
selectionModel = treeView->selectionModel();
connect(selectionModel, SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
SLOT(onSelectionChanged(const QItemSelection &, const QItemSelection &)));
history_index = -1; history_index = -1;
history_ignore = false; history_ignore = false;
@ -210,7 +214,6 @@ void DesignWidget::newContext(Context *ctx)
highlightSelected.clear(); highlightSelected.clear();
this->ctx = ctx; this->ctx = ctx;
treeView->setModel(nullptr);
treeModel->loadData(ctx); treeModel->loadData(ctx);
updateTree(); updateTree();
} }
@ -232,10 +235,6 @@ void DesignWidget::updateTree()
} }
treeModel->updateData(ctx); treeModel->updateData(ctx);
treeView->setModel(treeModel);
selectionModel = treeView->selectionModel();
connect(selectionModel, SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
SLOT(onSelectionChanged(const QItemSelection &, const QItemSelection &)));
} }
QtProperty *DesignWidget::addTopLevelProperty(const QString &id) QtProperty *DesignWidget::addTopLevelProperty(const QString &id)
{ {
@ -310,21 +309,24 @@ QtProperty *DesignWidget::addSubGroup(QtProperty *topItem, const QString &name)
void DesignWidget::onClickedBel(BelId bel, bool keep) void DesignWidget::onClickedBel(BelId bel, bool keep)
{ {
ContextTreeItem *item = treeModel->nodeForIdType(ElementType::BEL, ctx->getBelName(bel).c_str(ctx)); ContextTreeItem *item = treeModel->nodeForIdType(ElementType::BEL, ctx->getBelName(bel).c_str(ctx));
selectionModel->setCurrentIndex(treeModel->indexFromNode(item), keep ? QItemSelectionModel::Select : QItemSelectionModel::ClearAndSelect); selectionModel->setCurrentIndex(treeModel->indexFromNode(item),
keep ? QItemSelectionModel::Select : QItemSelectionModel::ClearAndSelect);
Q_EMIT selected(getDecals(ElementType::BEL, ctx->getBelName(bel)), keep); Q_EMIT selected(getDecals(ElementType::BEL, ctx->getBelName(bel)), keep);
} }
void DesignWidget::onClickedWire(WireId wire, bool keep) void DesignWidget::onClickedWire(WireId wire, bool keep)
{ {
ContextTreeItem *item = treeModel->nodeForIdType(ElementType::WIRE, ctx->getWireName(wire).c_str(ctx)); ContextTreeItem *item = treeModel->nodeForIdType(ElementType::WIRE, ctx->getWireName(wire).c_str(ctx));
selectionModel->setCurrentIndex(treeModel->indexFromNode(item), keep ? QItemSelectionModel::Select : QItemSelectionModel::ClearAndSelect); selectionModel->setCurrentIndex(treeModel->indexFromNode(item),
keep ? QItemSelectionModel::Select : QItemSelectionModel::ClearAndSelect);
Q_EMIT selected(getDecals(ElementType::WIRE, ctx->getWireName(wire)), keep); Q_EMIT selected(getDecals(ElementType::WIRE, ctx->getWireName(wire)), keep);
} }
void DesignWidget::onClickedPip(PipId pip, bool keep) void DesignWidget::onClickedPip(PipId pip, bool keep)
{ {
ContextTreeItem *item = treeModel->nodeForIdType(ElementType::PIP, ctx->getPipName(pip).c_str(ctx)); ContextTreeItem *item = treeModel->nodeForIdType(ElementType::PIP, ctx->getPipName(pip).c_str(ctx));
selectionModel->setCurrentIndex(treeModel->indexFromNode(item), keep ? QItemSelectionModel::Select : QItemSelectionModel::ClearAndSelect); selectionModel->setCurrentIndex(treeModel->indexFromNode(item),
keep ? QItemSelectionModel::Select : QItemSelectionModel::ClearAndSelect);
Q_EMIT selected(getDecals(ElementType::PIP, ctx->getPipName(pip)), keep); Q_EMIT selected(getDecals(ElementType::PIP, ctx->getPipName(pip)), keep);
} }

View File

@ -53,6 +53,9 @@ void ContextTreeModel::loadData(Context *ctx)
{ {
if (!ctx) if (!ctx)
return; return;
beginResetModel();
delete root; delete root;
root = new ContextTreeItem(); root = new ContextTreeItem();
@ -153,6 +156,8 @@ void ContextTreeModel::loadData(Context *ctx)
cells_root = new ContextTreeItem("Cells"); cells_root = new ContextTreeItem("Cells");
root->addChild(cells_root); root->addChild(cells_root);
endResetModel();
} }
void ContextTreeModel::updateData(Context *ctx) void ContextTreeModel::updateData(Context *ctx)
@ -160,14 +165,18 @@ void ContextTreeModel::updateData(Context *ctx)
if (!ctx) if (!ctx)
return; return;
QModelIndex nets_index = indexFromNode(nets_root);
// Remove nets not existing any more // Remove nets not existing any more
QMap<QString, ContextTreeItem *>::iterator i = nameToItem[3].begin(); QMap<QString, ContextTreeItem *>::iterator i = nameToItem[3].begin();
while (i != nameToItem[3].end()) { while (i != nameToItem[3].end()) {
QMap<QString, ContextTreeItem *>::iterator prev = i; QMap<QString, ContextTreeItem *>::iterator prev = i;
++i; ++i;
if (ctx->nets.find(ctx->id(prev.key().toStdString())) == ctx->nets.end()) { if (ctx->nets.find(ctx->id(prev.key().toStdString())) == ctx->nets.end()) {
int pos = prev.value()->parent()->indexOf(prev.value());
beginRemoveRows(nets_index, pos, pos);
delete prev.value(); delete prev.value();
nameToItem[3].erase(prev); nameToItem[3].erase(prev);
endRemoveRows();
} }
} }
// Add nets to tree // Add nets to tree
@ -175,20 +184,26 @@ void ContextTreeModel::updateData(Context *ctx)
auto id = item.first; auto id = item.first;
QString name = QString(id.c_str(ctx)); QString name = QString(id.c_str(ctx));
if (!nameToItem[3].contains(name)) { if (!nameToItem[3].contains(name)) {
beginInsertRows(nets_index, nets_root->count() + 1, nets_root->count() + 1);
ContextTreeItem *newItem = new ContextTreeItem(id, ElementType::NET, name); ContextTreeItem *newItem = new ContextTreeItem(id, ElementType::NET, name);
nets_root->addChild(newItem); nets_root->addChild(newItem);
nameToItem[3].insert(name, newItem); nameToItem[3].insert(name, newItem);
endInsertRows();
} }
} }
QModelIndex cell_index = indexFromNode(cells_root);
// Remove cells not existing any more // Remove cells not existing any more
i = nameToItem[4].begin(); i = nameToItem[4].begin();
while (i != nameToItem[4].end()) { while (i != nameToItem[4].end()) {
QMap<QString, ContextTreeItem *>::iterator prev = i; QMap<QString, ContextTreeItem *>::iterator prev = i;
++i; ++i;
if (ctx->cells.find(ctx->id(prev.key().toStdString())) == ctx->cells.end()) { if (ctx->cells.find(ctx->id(prev.key().toStdString())) == ctx->cells.end()) {
int pos = prev.value()->parent()->indexOf(prev.value());
beginRemoveRows(cell_index, pos, pos);
delete prev.value(); delete prev.value();
nameToItem[4].erase(prev); nameToItem[4].erase(prev);
endRemoveRows();
} }
} }
// Add cells to tree // Add cells to tree
@ -196,9 +211,11 @@ void ContextTreeModel::updateData(Context *ctx)
auto id = item.first; auto id = item.first;
QString name = QString(id.c_str(ctx)); QString name = QString(id.c_str(ctx));
if (!nameToItem[4].contains(name)) { if (!nameToItem[4].contains(name)) {
beginInsertRows(cell_index, cells_root->count() + 1, cells_root->count() + 1);
ContextTreeItem *newItem = new ContextTreeItem(id, ElementType::CELL, name); ContextTreeItem *newItem = new ContextTreeItem(id, ElementType::CELL, name);
cells_root->addChild(newItem); cells_root->addChild(newItem);
nameToItem[4].insert(name, newItem); nameToItem[4].insert(name, newItem);
endInsertRows();
} }
} }
} }