aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--gui/designwidget.cc142
-rw-r--r--gui/designwidget.h4
-rw-r--r--gui/treemodel.cc302
-rw-r--r--gui/treemodel.h299
4 files changed, 421 insertions, 326 deletions
diff --git a/gui/designwidget.cc b/gui/designwidget.cc
index e8c05ef9..ad1362c8 100644
--- a/gui/designwidget.cc
+++ b/gui/designwidget.cc
@@ -215,7 +215,7 @@ void DesignWidget::newContext(Context *ctx)
highlightSelected.clear();
this->ctx = ctx;
- treeModel->loadData(ctx);
+ treeModel->loadContext(ctx);
updateTree();
}
@@ -223,9 +223,9 @@ void DesignWidget::updateTree()
{
clearProperties();
- QMap<ContextTreeItem *, int>::iterator i = highlightSelected.begin();
+ QMap<LazyTreeItem *, int>::iterator i = highlightSelected.begin();
while (i != highlightSelected.end()) {
- QMap<ContextTreeItem *, int>::iterator prev = i;
+ QMap<LazyTreeItem *, int>::iterator prev = i;
++i;
if (prev.key()->type() == ElementType::NET && ctx->nets.find(prev.key()->id()) == ctx->nets.end()) {
highlightSelected.erase(prev);
@@ -235,7 +235,7 @@ void DesignWidget::updateTree()
}
}
- treeModel->updateData(ctx);
+ treeModel->updateCellsNets(ctx);
}
QtProperty *DesignWidget::addTopLevelProperty(const QString &id)
{
@@ -309,25 +309,25 @@ QtProperty *DesignWidget::addSubGroup(QtProperty *topItem, const QString &name)
void DesignWidget::onClickedBel(BelId bel, bool keep)
{
- ContextTreeItem *item = treeModel->nodeForIdType(ElementType::BEL, ctx->getBelName(bel).c_str(ctx));
- selectionModel->setCurrentIndex(treeModel->indexFromNode(item),
- keep ? QItemSelectionModel::Select : QItemSelectionModel::ClearAndSelect);
+ //LazyTreeItem *item = treeModel->nodeForIdType(ElementType::BEL, ctx->getBelName(bel).c_str(ctx));
+ //selectionModel->setCurrentIndex(treeModel->indexFromNode(item),
+ // keep ? QItemSelectionModel::Select : QItemSelectionModel::ClearAndSelect);
Q_EMIT selected(getDecals(ElementType::BEL, ctx->getBelName(bel)), keep);
}
void DesignWidget::onClickedWire(WireId wire, bool keep)
{
- ContextTreeItem *item = treeModel->nodeForIdType(ElementType::WIRE, ctx->getWireName(wire).c_str(ctx));
- selectionModel->setCurrentIndex(treeModel->indexFromNode(item),
- keep ? QItemSelectionModel::Select : QItemSelectionModel::ClearAndSelect);
+ //LazyTreeItem *item = treeModel->nodeForIdType(ElementType::WIRE, ctx->getWireName(wire).c_str(ctx));
+ //selectionModel->setCurrentIndex(treeModel->indexFromNode(item),
+ // keep ? QItemSelectionModel::Select : QItemSelectionModel::ClearAndSelect);
Q_EMIT selected(getDecals(ElementType::WIRE, ctx->getWireName(wire)), keep);
}
void DesignWidget::onClickedPip(PipId pip, bool keep)
{
- ContextTreeItem *item = treeModel->nodeForIdType(ElementType::PIP, ctx->getPipName(pip).c_str(ctx));
- selectionModel->setCurrentIndex(treeModel->indexFromNode(item),
- keep ? QItemSelectionModel::Select : QItemSelectionModel::ClearAndSelect);
+ //LazyTreeItem *item = treeModel->nodeForIdType(ElementType::PIP, ctx->getPipName(pip).c_str(ctx));
+ //selectionModel->setCurrentIndex(treeModel->indexFromNode(item),
+ // keep ? QItemSelectionModel::Select : QItemSelectionModel::ClearAndSelect);
Q_EMIT selected(getDecals(ElementType::PIP, ctx->getPipName(pip)), keep);
}
@@ -339,7 +339,7 @@ void DesignWidget::onSelectionChanged(const QItemSelection &, const QItemSelecti
if (selectionModel->selectedIndexes().size() > 1) {
std::vector<DecalXY> decals;
for (auto index : selectionModel->selectedIndexes()) {
- ContextTreeItem *item = treeModel->nodeFromIndex(index);
+ LazyTreeItem *item = treeModel->nodeFromIndex(index);
std::vector<DecalXY> d = getDecals(item->type(), item->id());
std::move(d.begin(), d.end(), std::back_inserter(decals));
}
@@ -349,7 +349,7 @@ void DesignWidget::onSelectionChanged(const QItemSelection &, const QItemSelecti
QModelIndex index = selectionModel->selectedIndexes().at(0);
if (!index.isValid())
return;
- ContextTreeItem *clickItem = treeModel->nodeFromIndex(index);
+ LazyTreeItem *clickItem = treeModel->nodeFromIndex(index);
ElementType type = clickItem->type();
if (type == ElementType::NONE)
@@ -597,7 +597,7 @@ std::vector<DecalXY> DesignWidget::getDecals(ElementType type, IdString value)
return decals;
}
-void DesignWidget::updateHighlightGroup(QList<ContextTreeItem *> items, int group)
+void DesignWidget::updateHighlightGroup(QList<LazyTreeItem *> items, int group)
{
const bool shouldClear = items.size() == 1;
for (auto item : items) {
@@ -621,53 +621,53 @@ void DesignWidget::updateHighlightGroup(QList<ContextTreeItem *> items, int grou
void DesignWidget::prepareMenuProperty(const QPoint &pos)
{
- QTreeWidget *tree = propertyEditor->treeWidget();
- QList<ContextTreeItem *> items;
- for (auto itemContextMenu : tree->selectedItems()) {
- QtBrowserItem *browserItem = propertyEditor->itemToBrowserItem(itemContextMenu);
- if (!browserItem)
- continue;
- QtProperty *selectedProperty = browserItem->property();
- ElementType type = getElementTypeByName(selectedProperty->propertyId());
- if (type == ElementType::NONE)
- continue;
- IdString value = ctx->id(selectedProperty->valueText().toStdString());
- items.append(treeModel->nodeForIdType(type, value.c_str(ctx)));
- }
- int selectedIndex = -1;
- if (items.size() == 1) {
- ContextTreeItem *item = items.at(0);
- if (highlightSelected.contains(item))
- selectedIndex = highlightSelected[item];
- }
-
- QMenu menu(this);
- QAction *selectAction = new QAction("&Select", this);
- connect(selectAction, &QAction::triggered, this, [this, items] {
- std::vector<DecalXY> decals;
- for (auto clickItem : items) {
- std::vector<DecalXY> d = getDecals(clickItem->type(), clickItem->id());
- std::move(d.begin(), d.end(), std::back_inserter(decals));
- }
- Q_EMIT selected(decals, false);
- });
- menu.addAction(selectAction);
-
- QMenu *subMenu = menu.addMenu("Highlight");
- QActionGroup *group = new QActionGroup(this);
- group->setExclusive(true);
- for (int i = 0; i < 8; i++) {
- QPixmap pixmap(32, 32);
- pixmap.fill(QColor(highlightColors[i]));
- QAction *action = new QAction(QIcon(pixmap), ("Group " + std::to_string(i)).c_str(), this);
- action->setCheckable(true);
- subMenu->addAction(action);
- group->addAction(action);
- if (selectedIndex == i)
- action->setChecked(true);
- connect(action, &QAction::triggered, this, [this, i, items] { updateHighlightGroup(items, i); });
- }
- menu.exec(tree->mapToGlobal(pos));
+ //QTreeWidget *tree = propertyEditor->treeWidget();
+ //QList<LazyTreeItem *> items;
+ //for (auto itemContextMenu : tree->selectedItems()) {
+ // QtBrowserItem *browserItem = propertyEditor->itemToBrowserItem(itemContextMenu);
+ // if (!browserItem)
+ // continue;
+ // QtProperty *selectedProperty = browserItem->property();
+ // ElementType type = getElementTypeByName(selectedProperty->propertyId());
+ // if (type == ElementType::NONE)
+ // continue;
+ // IdString value = ctx->id(selectedProperty->valueText().toStdString());
+ // items.append(treeModel->nodeForIdType(type, value.c_str(ctx)));
+ //}
+ //int selectedIndex = -1;
+ //if (items.size() == 1) {
+ // LazyTreeItem *item = items.at(0);
+ // if (highlightSelected.contains(item))
+ // selectedIndex = highlightSelected[item];
+ //}
+
+ //QMenu menu(this);
+ //QAction *selectAction = new QAction("&Select", this);
+ //connect(selectAction, &QAction::triggered, this, [this, items] {
+ // std::vector<DecalXY> decals;
+ // for (auto clickItem : items) {
+ // std::vector<DecalXY> d = getDecals(clickItem->type(), clickItem->id());
+ // std::move(d.begin(), d.end(), std::back_inserter(decals));
+ // }
+ // Q_EMIT selected(decals, false);
+ //});
+ //menu.addAction(selectAction);
+
+ //QMenu *subMenu = menu.addMenu("Highlight");
+ //QActionGroup *group = new QActionGroup(this);
+ //group->setExclusive(true);
+ //for (int i = 0; i < 8; i++) {
+ // QPixmap pixmap(32, 32);
+ // pixmap.fill(QColor(highlightColors[i]));
+ // QAction *action = new QAction(QIcon(pixmap), ("Group " + std::to_string(i)).c_str(), this);
+ // action->setCheckable(true);
+ // subMenu->addAction(action);
+ // group->addAction(action);
+ // if (selectedIndex == i)
+ // action->setChecked(true);
+ // connect(action, &QAction::triggered, this, [this, i, items] { updateHighlightGroup(items, i); });
+ //}
+ //menu.exec(tree->mapToGlobal(pos));
}
void DesignWidget::prepareMenuTree(const QPoint &pos)
@@ -677,13 +677,13 @@ void DesignWidget::prepareMenuTree(const QPoint &pos)
if (selectionModel->selectedIndexes().size() == 0)
return;
- QList<ContextTreeItem *> items;
+ QList<LazyTreeItem *> items;
for (auto index : selectionModel->selectedIndexes()) {
- ContextTreeItem *item = treeModel->nodeFromIndex(index);
+ LazyTreeItem *item = treeModel->nodeFromIndex(index);
items.append(item);
}
if (items.size() == 1) {
- ContextTreeItem *item = items.at(0);
+ LazyTreeItem *item = items.at(0);
if (highlightSelected.contains(item))
selectedIndex = highlightSelected[item];
}
@@ -707,11 +707,11 @@ void DesignWidget::prepareMenuTree(const QPoint &pos)
void DesignWidget::onItemDoubleClicked(QTreeWidgetItem *item, int column)
{
- QtProperty *selectedProperty = propertyEditor->itemToBrowserItem(item)->property();
- ElementType type = getElementTypeByName(selectedProperty->propertyId());
- ContextTreeItem *it = treeModel->nodeForIdType(type, selectedProperty->valueText());
- if (it)
- selectionModel->setCurrentIndex(treeModel->indexFromNode(it), QItemSelectionModel::ClearAndSelect);
+ //QtProperty *selectedProperty = propertyEditor->itemToBrowserItem(item)->property();
+ //ElementType type = getElementTypeByName(selectedProperty->propertyId());
+ //LazyTreeItem *it = treeModel->nodeForIdType(type, selectedProperty->valueText());
+ //if (it)
+ // selectionModel->setCurrentIndex(treeModel->indexFromNode(it), QItemSelectionModel::ClearAndSelect);
}
void DesignWidget::onDoubleClicked(const QModelIndex &index) { Q_EMIT zoomSelected(); }
diff --git a/gui/designwidget.h b/gui/designwidget.h
index 535fd0c3..d6af83a0 100644
--- a/gui/designwidget.h
+++ b/gui/designwidget.h
@@ -51,7 +51,7 @@ class DesignWidget : public QWidget
void updateButtons();
void addToHistory(QModelIndex item);
std::vector<DecalXY> getDecals(ElementType type, IdString value);
- void updateHighlightGroup(QList<ContextTreeItem *> item, int group);
+ void updateHighlightGroup(QList<LazyTreeItem *> item, int group);
Q_SIGNALS:
void info(std::string text);
void selected(std::vector<DecalXY> decal, bool keep);
@@ -99,7 +99,7 @@ class DesignWidget : public QWidget
QAction *actionClear;
QColor highlightColors[8];
- QMap<ContextTreeItem *, int> highlightSelected;
+ QMap<LazyTreeItem *, int> highlightSelected;
QString currentSearch;
QList<QModelIndex> currentSearchIndexes;
diff --git a/gui/treemodel.cc b/gui/treemodel.cc
index d42dc401..fd3ae45b 100644
--- a/gui/treemodel.cc
+++ b/gui/treemodel.cc
@@ -18,229 +18,86 @@
*/
#include "treemodel.h"
+#include "log.h"
NEXTPNR_NAMESPACE_BEGIN
-static bool contextTreeItemLessThan(const ContextTreeItem *v1, const ContextTreeItem *v2)
- {
- return v1->name() < v2->name();
- }
+ContextTreeModel::ContextTreeModel(QObject *parent) :
+ QAbstractItemModel(parent),
+ root_(new StaticTreeItem("Elements", nullptr)) {}
-ContextTreeItem::ContextTreeItem() { parentNode = nullptr; }
+ContextTreeModel::~ContextTreeModel() {}
-ContextTreeItem::ContextTreeItem(QString name)
- : parentNode(nullptr), itemId(IdString()), itemType(ElementType::NONE), itemName(name)
-{
-}
-
-ContextTreeItem::ContextTreeItem(IdString id, ElementType type, QString name)
- : parentNode(nullptr), itemId(id), itemType(type), itemName(name)
-{
-}
-
-ContextTreeItem::~ContextTreeItem()
-{
- if (parentNode)
- parentNode->children.removeOne(this);
- qDeleteAll(children);
-}
-void ContextTreeItem::addChild(ContextTreeItem *item)
-{
- item->parentNode = this;
- children.append(item);
-}
-
-void ContextTreeItem::sort()
-{
- for (auto item : children)
- if (item->count()>1) item->sort();
- qSort(children.begin(), children.end(), contextTreeItemLessThan);
-}
-
-ContextTreeModel::ContextTreeModel(QObject *parent) : QAbstractItemModel(parent) { root = new ContextTreeItem(); }
-
-ContextTreeModel::~ContextTreeModel() { delete root; }
-
-void ContextTreeModel::loadData(Context *ctx)
+void ContextTreeModel::loadContext(Context *ctx)
{
if (!ctx)
return;
beginResetModel();
- delete root;
- root = new ContextTreeItem();
-
- for (int i = 0; i < 6; i++)
- nameToItem[i].clear();
-
- IdString none;
-
- ContextTreeItem *bels_root = new ContextTreeItem("Bels");
- root->addChild(bels_root);
- QMap<QString, ContextTreeItem *> bel_items;
-
- // Add bels to tree
- for (auto bel : ctx->getBels()) {
- IdString id = ctx->getBelName(bel);
- QStringList items = QString(id.c_str(ctx)).split("/");
- QString name;
- ContextTreeItem *parent = bels_root;
- for (int i = 0; i < items.size(); i++) {
- if (!name.isEmpty())
- name += "/";
- name += items.at(i);
- if (!bel_items.contains(name)) {
- if (i == items.size() - 1) {
- ContextTreeItem *item = new ContextTreeItem(id, ElementType::BEL, items.at(i));
- parent->addChild(item);
- nameToItem[0].insert(name, item);
- } else {
- ContextTreeItem *item = new ContextTreeItem(none, ElementType::NONE, items.at(i));
- parent->addChild(item);
- bel_items.insert(name, item);
- }
- }
- parent = bel_items[name];
+ // Currently we lack an API to get a proper hierarchy of bels/pip/wires
+ // cross-arch. So we only do this for ICE40 by querying the ChipDB
+ // directly.
+ // TODO(q3k): once AnyId and the tree API land in Arch, move this over.
+#ifdef ARCH_ICE40
+ {
+ std::map<std::pair<int, int>, std::vector<BelId>> belMap;
+ for (auto bel : ctx->getBels()) {
+ auto loc = ctx->getBelLocation(bel);
+ belMap[std::pair<int, int>(loc.x, loc.y)].push_back(bel);
}
- }
- bels_root->sort();
-
- ContextTreeItem *wire_root = new ContextTreeItem("Wires");
- root->addChild(wire_root);
- QMap<QString, ContextTreeItem *> wire_items;
-
- // Add wires to tree
- for (auto wire : ctx->getWires()) {
- auto id = ctx->getWireName(wire);
- QStringList items = QString(id.c_str(ctx)).split("/");
- QString name;
- ContextTreeItem *parent = wire_root;
- for (int i = 0; i < items.size(); i++) {
- if (!name.isEmpty())
- name += "/";
- name += items.at(i);
- if (!wire_items.contains(name)) {
- if (i == items.size() - 1) {
- ContextTreeItem *item = new ContextTreeItem(id, ElementType::WIRE, items.at(i));
- parent->addChild(item);
- nameToItem[1].insert(name, item);
- } else {
- ContextTreeItem *item = new ContextTreeItem(none, ElementType::NONE, items.at(i));
- parent->addChild(item);
- wire_items.insert(name, item);
- }
- }
- parent = wire_items[name];
+ auto belGetter = [](Context *ctx, BelId id) { return ctx->getBelName(id); };
+ bel_root_ = std::unique_ptr<BelXYRoot>(new BelXYRoot(ctx, "Bels", root_.get(), belMap, belGetter));
+
+ std::map<std::pair<int, int>, std::vector<WireId>> wireMap;
+ for (int i = 0; i < ctx->chip_info->num_wires; i++) {
+ const auto wire = &ctx->chip_info->wire_data[i];
+ WireId wireid;
+ wireid.index = i;
+ wireMap[std::pair<int, int>(wire->x, wire->y)].push_back(wireid);
}
- }
- wire_root->sort();
-
- ContextTreeItem *pip_root = new ContextTreeItem("Pips");
- root->addChild(pip_root);
- QMap<QString, ContextTreeItem *> pip_items;
-
- // Add pips to tree
-#ifndef ARCH_ECP5
- for (auto pip : ctx->getPips()) {
- auto id = ctx->getPipName(pip);
- QStringList items = QString(id.c_str(ctx)).split("/");
- QString name;
- ContextTreeItem *parent = pip_root;
- for (int i = 0; i < items.size(); i++) {
- if (!name.isEmpty())
- name += "/";
- name += items.at(i);
- if (!pip_items.contains(name)) {
- if (i == items.size() - 1) {
- ContextTreeItem *item = new ContextTreeItem(id, ElementType::PIP, items.at(i));
- parent->addChild(item);
- nameToItem[2].insert(name, item);
- } else {
- ContextTreeItem *item = new ContextTreeItem(none, ElementType::NONE, items.at(i));
- parent->addChild(item);
- pip_items.insert(name, item);
- }
- }
- parent = pip_items[name];
+ auto wireGetter = [](Context *ctx, WireId id) { return ctx->getWireName(id); };
+ wire_root_ = std::unique_ptr<WireXYRoot>(new WireXYRoot(ctx, "Wires", root_.get(), wireMap, wireGetter));
+
+ std::map<std::pair<int, int>, std::vector<PipId>> pipMap;
+ for (int i = 0; i < ctx->chip_info->num_pips; i++) {
+ const auto pip = &ctx->chip_info->pip_data[i];
+ PipId pipid;
+ pipid.index = i;
+ pipMap[std::pair<int, int>(pip->x, pip->y)].push_back(pipid);
}
+ printf("generating pip static tree...\n");
+ auto pipGetter = [](Context *ctx, PipId id) { return ctx->getPipName(id); };
+ pip_root_ = std::unique_ptr<PipXYRoot>(new PipXYRoot(ctx, "Pips", root_.get(), pipMap, pipGetter));
}
#endif
- pip_root->sort();
-
- nets_root = new ContextTreeItem("Nets");
- root->addChild(nets_root);
- cells_root = new ContextTreeItem("Cells");
- root->addChild(cells_root);
+ cell_root_ = std::unique_ptr<IdStringList>(new IdStringList(QString("Cells"), root_.get()));
+ net_root_ = std::unique_ptr<IdStringList>(new IdStringList(QString("Nets"), root_.get()));
endResetModel();
+
+ updateCellsNets(ctx);
}
-void ContextTreeModel::updateData(Context *ctx)
+void ContextTreeModel::updateCellsNets(Context *ctx)
{
if (!ctx)
return;
beginResetModel();
- //QModelIndex nets_index = indexFromNode(nets_root);
- // Remove nets not existing any more
- QMap<QString, ContextTreeItem *>::iterator i = nameToItem[3].begin();
- while (i != nameToItem[3].end()) {
- QMap<QString, ContextTreeItem *>::iterator prev = i;
- ++i;
- 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();
- nameToItem[3].erase(prev);
- //endRemoveRows();
- }
- }
- // Add nets to tree
- for (auto &item : ctx->nets) {
- auto id = item.first;
- QString name = QString(id.c_str(ctx));
- if (!nameToItem[3].contains(name)) {
- //beginInsertRows(nets_index, nets_root->count() + 1, nets_root->count() + 1);
- ContextTreeItem *newItem = new ContextTreeItem(id, ElementType::NET, name);
- nets_root->addChild(newItem);
- nameToItem[3].insert(name, newItem);
- //endInsertRows();
- }
+ std::vector<IdString> cells;
+ for (auto &pair : ctx->cells) {
+ cells.push_back(pair.first);
}
+ cell_root_->updateElements(ctx, cells);
- nets_root->sort();
-
- //QModelIndex cell_index = indexFromNode(cells_root);
- // Remove cells not existing any more
- i = nameToItem[4].begin();
- while (i != nameToItem[4].end()) {
- QMap<QString, ContextTreeItem *>::iterator prev = i;
- ++i;
- 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();
- nameToItem[4].erase(prev);
- //endRemoveRows();
- }
- }
- // Add cells to tree
- for (auto &item : ctx->cells) {
- auto id = item.first;
- QString name = QString(id.c_str(ctx));
- if (!nameToItem[4].contains(name)) {
- //beginInsertRows(cell_index, cells_root->count() + 1, cells_root->count() + 1);
- ContextTreeItem *newItem = new ContextTreeItem(id, ElementType::CELL, name);
- cells_root->addChild(newItem);
- nameToItem[4].insert(name, newItem);
- //endInsertRows();
- }
+ std::vector<IdString> nets;
+ for (auto &pair : ctx->nets) {
+ nets.push_back(pair.first);
}
-
- cells_root->sort();
+ net_root_->updateElements(ctx, nets);
endResetModel();
}
@@ -251,18 +108,19 @@ int ContextTreeModel::columnCount(const QModelIndex &parent) const { return 1; }
QModelIndex ContextTreeModel::index(int row, int column, const QModelIndex &parent) const
{
- ContextTreeItem *node = nodeFromIndex(parent);
+ LazyTreeItem *node = nodeFromIndex(parent);
if (row >= node->count())
return QModelIndex();
- return createIndex(row, column, node->at(row));
+
+ return createIndex(row, column, node->child(row));
}
QModelIndex ContextTreeModel::parent(const QModelIndex &child) const
{
- ContextTreeItem *parent = nodeFromIndex(child)->parent();
- if (parent == root)
+ LazyTreeItem *parent = nodeFromIndex(child)->parent();
+ if (parent == root_.get())
return QModelIndex();
- ContextTreeItem *node = parent->parent();
+ LazyTreeItem *node = parent->parent();
return createIndex(node->indexOf(parent), 0, parent);
}
@@ -272,7 +130,7 @@ QVariant ContextTreeModel::data(const QModelIndex &index, int role) const
return QVariant();
if (role != Qt::DisplayRole)
return QVariant();
- ContextTreeItem *node = nodeFromIndex(index);
+ LazyTreeItem *node = nodeFromIndex(index);
return node->name();
}
@@ -285,11 +143,11 @@ QVariant ContextTreeModel::headerData(int section, Qt::Orientation orientation,
return QVariant();
}
-ContextTreeItem *ContextTreeModel::nodeFromIndex(const QModelIndex &idx) const
+LazyTreeItem *ContextTreeModel::nodeFromIndex(const QModelIndex &idx) const
{
if (idx.isValid())
- return (ContextTreeItem *)idx.internalPointer();
- return root;
+ return (LazyTreeItem *)idx.internalPointer();
+ return root_.get();
}
static int getElementIndex(ElementType type)
@@ -307,40 +165,36 @@ static int getElementIndex(ElementType type)
return -1;
}
-ContextTreeItem *ContextTreeModel::nodeForIdType(const ElementType type, const QString name) const
+Qt::ItemFlags ContextTreeModel::flags(const QModelIndex &index) const
{
- int index = getElementIndex(type);
- if (type != ElementType::NONE && nameToItem[index].contains(name))
- return nameToItem[index].value(name);
- return nullptr;
+ LazyTreeItem *node = nodeFromIndex(index);
+ return Qt::ItemIsEnabled | (node->type() != ElementType::NONE ? Qt::ItemIsSelectable : Qt::NoItemFlags);
}
-QModelIndex ContextTreeModel::indexFromNode(ContextTreeItem *node)
+
+void ContextTreeModel::fetchMore(const QModelIndex &parent)
{
- ContextTreeItem *parent = node->parent();
- if (parent == root)
- return QModelIndex();
- return createIndex(parent->indexOf(node), 0, node);
+ nodeFromIndex(parent)->fetchMore();
}
-Qt::ItemFlags ContextTreeModel::flags(const QModelIndex &index) const
+bool ContextTreeModel::canFetchMore(const QModelIndex &parent) const
{
- ContextTreeItem *node = nodeFromIndex(index);
- return Qt::ItemIsEnabled | (node->type() != ElementType::NONE ? Qt::ItemIsSelectable : Qt::NoItemFlags);
+ return nodeFromIndex(parent)->canFetchMore();
}
QList<QModelIndex> ContextTreeModel::search(QString text)
{
QList<QModelIndex> list;
- for (int i = 0; i < 6; i++) {
- for (auto key : nameToItem[i].keys()) {
- if (key.contains(text, Qt::CaseInsensitive)) {
- list.append(indexFromNode(nameToItem[i].value(key)));
- if (list.count() > 500)
- break; // limit to 500 results
- }
- }
- }
+ //for (int i = 0; i < 6; i++) {
+ // for (auto key : nameToItem[i].keys()) {
+ // if (key.contains(text, Qt::CaseInsensitive)) {
+ // list.append(indexFromNode(nameToItem[i].value(key)));
+ // if (list.count() > 500)
+ // break; // limit to 500 results
+ // }
+ // }
+ //}
return list;
}
+
NEXTPNR_NAMESPACE_END
diff --git a/gui/treemodel.h b/gui/treemodel.h
index c14efa90..f193468a 100644
--- a/gui/treemodel.h
+++ b/gui/treemodel.h
@@ -22,6 +22,7 @@
#include <QAbstractItemModel>
#include "nextpnr.h"
+#include "log.h"
NEXTPNR_NAMESPACE_BEGIN
@@ -36,43 +37,279 @@ enum class ElementType
GROUP
};
-class ContextTreeItem
+class LazyTreeItem
+{
+ protected:
+ QString name_;
+ LazyTreeItem *parent_;
+ QList<LazyTreeItem *> children_;
+
+ void addChild(LazyTreeItem *child)
+ {
+ children_.append(child);
+ }
+
+ public:
+ LazyTreeItem(QString name, LazyTreeItem *parent) :
+ name_(name), parent_(parent)
+ {
+ // Register in parent if exists.
+ if (parent_ != nullptr) {
+ parent_->addChild(this);
+ }
+ };
+
+ int count() const
+ {
+ return children_.count();
+ }
+
+ QString name() const
+ {
+ return name_;
+ }
+
+ LazyTreeItem *child(int index)
+ {
+ return children_.at(index);
+ }
+
+ int indexOf(LazyTreeItem *child) const
+ {
+ return children_.indexOf(child, 0);
+ }
+
+ LazyTreeItem *parent()
+ {
+ return parent_;
+ }
+
+ virtual bool canFetchMore() const = 0;
+ virtual void fetchMore() = 0;
+ virtual ElementType type() const = 0;
+ virtual IdString id() const = 0;
+
+ virtual ~LazyTreeItem() {}
+};
+
+class StaticTreeItem : public LazyTreeItem
{
public:
- ContextTreeItem();
- ContextTreeItem(QString name);
- ContextTreeItem(IdString id, ElementType type, QString name);
- ~ContextTreeItem();
-
- void addChild(ContextTreeItem *item);
- int indexOf(ContextTreeItem *n) const { return children.indexOf(n); }
- ContextTreeItem *at(int idx) const { return children.at(idx); }
- int count() const { return children.count(); }
- ContextTreeItem *parent() const { return parentNode; }
- IdString id() const { return itemId; }
- ElementType type() const { return itemType; }
- QString name() const { return itemName; }
- void sort();
+ using LazyTreeItem::LazyTreeItem;
+
+ virtual bool canFetchMore() const override
+ {
+ return false;
+ }
+
+ virtual void fetchMore() override
+ {
+ }
+
+ virtual ~StaticTreeItem() {}
+
+ virtual ElementType type() const override
+ {
+ return ElementType::NONE;
+ }
+
+ virtual IdString id() const override
+ {
+ return IdString();
+ }
+};
+
+template <typename ElementT>
+class ElementList : public LazyTreeItem
+{
+ public:
+ using ElementMap = std::map<std::pair<int, int>, std::vector<ElementT>>;
+ using ElementGetter = std::function<IdString(Context *, ElementT)>;
+
private:
- ContextTreeItem *parentNode;
- QList<ContextTreeItem *> children;
- IdString itemId;
- ElementType itemType;
- QString itemName;
+ Context *ctx_;
+ const ElementMap *map_;
+ int x_, y_;
+ ElementGetter getter_;
+ std::vector<std::unique_ptr<StaticTreeItem>> managed_;
+
+ // scope valid until map gets mutated...
+ const std::vector<ElementT> *elements() const
+ {
+ return &map_->at(std::pair<int, int>(x_, y_));
+ }
+
+ public:
+ ElementList(Context *ctx, QString name, LazyTreeItem *parent, ElementMap *map, int x, int y, ElementGetter getter) :
+ LazyTreeItem(name, parent), ctx_(ctx), map_(map), x_(x), y_(y), getter_(getter)
+ {
+ }
+
+ virtual bool canFetchMore() const override
+ {
+ return children_.size() < elements()->size();
+ }
+
+ void fetchMore(int count)
+ {
+ int start = children_.size();
+ size_t end = std::min(start + count, (int)elements()->size());
+ for (int i = start; i < end; i++) {
+ QString name(getter_(ctx_, elements()->at(i)).c_str(ctx_));
+
+ // Remove X.../Y.../ prefix
+ QString prefix = QString("X%1/Y%2/").arg(x_).arg(y_);
+ if (name.startsWith(prefix))
+ name.remove(0, prefix.size());
+
+ auto item = new StaticTreeItem(name, this);
+ managed_.push_back(std::move(std::unique_ptr<StaticTreeItem>(item)));
+ }
+ }
+
+ virtual void fetchMore() override
+ {
+ fetchMore(100);
+ }
+
+ virtual ElementType type() const override
+ {
+ return ElementType::NONE;
+ }
+
+ virtual IdString id() const override
+ {
+ return IdString();
+ }
+};
+
+class IdStringList : public StaticTreeItem
+{
+ private:
+ std::unordered_map<IdString, std::unique_ptr<StaticTreeItem>> managed_;
+ public:
+ using StaticTreeItem::StaticTreeItem;
+
+ void updateElements(Context *ctx, std::vector<IdString> elements)
+ {
+ // for any elements that are not yet in managed_, created them.
+ std::unordered_set<IdString> element_set;
+ for (auto elem : elements) {
+ element_set.insert(elem);
+ auto existing = managed_.find(elem);
+ if (existing == managed_.end()) {
+ auto item = new StaticTreeItem(elem.c_str(ctx), this);
+ managed_.emplace(elem, std::unique_ptr<StaticTreeItem>(item));
+ }
+ }
+
+ children_.clear();
+ // for any elements that are in managed_ but not in new, delete them.
+ for (auto &pair : managed_) {
+ if (element_set.count(pair.first) != 0) {
+ children_.push_back(pair.second.get());
+ continue;
+ }
+ managed_.erase(pair.first);
+ }
+
+ // sort new children
+ qSort(children_.begin(), children_.end(), [&](const LazyTreeItem *a, const LazyTreeItem *b){
+ QString name_a = a->name();
+ QString name_b = b->name();
+ // Try to extract a common prefix from both strings.
+ QString common;
+ for (int i = 0; i < std::min(name_a.size(), name_b.size()); i++) {
+ const QChar c_a = name_a[i];
+ const QChar c_b = name_b[i];
+ if (c_a == c_b) {
+ common.push_back(c_a);
+ } else {
+ break;
+ }
+ }
+ // No common part? lexical sort.
+ if (common.size() == 0) {
+ return a->name() < b->name();
+ }
+
+ // Get the non-common parts.
+ name_a.remove(0, common.size());
+ name_b.remove(0, common.size());
+ // And see if they're strings.
+ bool ok = true;
+ int num_a = name_a.toInt(&ok);
+ if (!ok) {
+ return a->name() < b->name();
+ }
+ int num_b = name_b.toInt(&ok);
+ if (!ok) {
+ return a->name() < b->name();
+ }
+ return num_a < num_b;
+ });
+ }
+};
+
+template <typename ElementT>
+class ElementXYRoot : public StaticTreeItem
+{
+ public:
+ using ElementMap = std::map<std::pair<int, int>, std::vector<ElementT>>;
+ using ElementGetter = std::function<IdString(Context *, ElementT)>;
+
+
+ private:
+ Context *ctx_;
+ std::vector<std::unique_ptr<LazyTreeItem>> managed_;
+ ElementMap map_;
+ ElementGetter getter_;
+
+ public:
+ ElementXYRoot(Context *ctx, QString name, LazyTreeItem *parent, ElementMap map, ElementGetter getter) :
+ StaticTreeItem(name, parent), ctx_(ctx), map_(map), getter_(getter)
+ {
+ std::vector<int> y_present;
+
+ for (int i = 0; i < ctx->getGridDimX(); i++) {
+ y_present.clear();
+ // first find all the elements in all Y coordinates in this X
+ for (int j = 0; j < ctx->getGridDimY(); j++) {
+ if (map_.count(std::pair<int, int>(i, j)) == 0)
+ continue;
+ y_present.push_back(j);
+ }
+ // no bels in any X coordinate? do not add X tree item.
+ if (y_present.size() == 0)
+ continue;
+
+ // create X item for tree
+ auto item = new StaticTreeItem(QString("X%1").arg(i), this);
+ managed_.push_back(std::move(std::unique_ptr<LazyTreeItem>(item)));
+ for (auto j : y_present) {
+ auto item2 = new ElementList<ElementT>(ctx_, QString("Y%1").arg(j), item, &map_, i, j, getter_);
+ item2->fetchMore(1);
+ managed_.push_back(std::move(std::unique_ptr<LazyTreeItem>(item2)));
+ }
+ }
+ }
};
class ContextTreeModel : public QAbstractItemModel
{
public:
+ using BelXYRoot = ElementXYRoot<BelId>;
+ using WireXYRoot = ElementXYRoot<WireId>;
+ using PipXYRoot = ElementXYRoot<PipId>;
+
ContextTreeModel(QObject *parent = nullptr);
~ContextTreeModel();
- void loadData(Context *ctx);
- void updateData(Context *ctx);
- ContextTreeItem *nodeFromIndex(const QModelIndex &idx) const;
- QModelIndex indexFromNode(ContextTreeItem *node);
- ContextTreeItem *nodeForIdType(const ElementType type, const QString name) const;
+ void loadContext(Context *ctx);
+ void updateCellsNets(Context *ctx);
+ LazyTreeItem *nodeFromIndex(const QModelIndex &idx) const;
QList<QModelIndex> search(QString text);
+
// Override QAbstractItemModel methods
int rowCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
int columnCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
@@ -81,12 +318,16 @@ class ContextTreeModel : public QAbstractItemModel
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const Q_DECL_OVERRIDE;
QVariant headerData(int section, Qt::Orientation orientation, int role) const Q_DECL_OVERRIDE;
Qt::ItemFlags flags(const QModelIndex &index) const Q_DECL_OVERRIDE;
+ void fetchMore(const QModelIndex &parent) Q_DECL_OVERRIDE;
+ bool canFetchMore(const QModelIndex &parent) const Q_DECL_OVERRIDE;
private:
- ContextTreeItem *root;
- QMap<QString, ContextTreeItem *> nameToItem[6];
- ContextTreeItem *nets_root;
- ContextTreeItem *cells_root;
+ std::unique_ptr<LazyTreeItem> root_;
+ std::unique_ptr<BelXYRoot> bel_root_;
+ std::unique_ptr<WireXYRoot> wire_root_;
+ std::unique_ptr<PipXYRoot> pip_root_;
+ std::unique_ptr<IdStringList> cell_root_;
+ std::unique_ptr<IdStringList> net_root_;
};
NEXTPNR_NAMESPACE_END