diff options
Diffstat (limited to 'gui/treemodel.cc')
-rw-r--r-- | gui/treemodel.cc | 426 |
1 files changed, 192 insertions, 234 deletions
diff --git a/gui/treemodel.cc b/gui/treemodel.cc index d42dc401..4fc3d4f5 100644 --- a/gui/treemodel.cc +++ b/gui/treemodel.cc @@ -2,6 +2,7 @@ * nextpnr -- Next Generation Place and Route * * Copyright (C) 2018 Miodrag Milanovic <miodrag@symbioticeda.com> + * Copyright (C) 2018 Serge Bazanski <q3k@symbioticeda.com> * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -18,265 +19,236 @@ */ #include "treemodel.h" +#include "log.h" NEXTPNR_NAMESPACE_BEGIN -static bool contextTreeItemLessThan(const ContextTreeItem *v1, const ContextTreeItem *v2) - { - return v1->name() < v2->name(); - } +namespace TreeModel { -ContextTreeItem::ContextTreeItem() { parentNode = nullptr; } -ContextTreeItem::ContextTreeItem(QString name) - : parentNode(nullptr), itemId(IdString()), itemType(ElementType::NONE), itemName(name) +// converts 'aa123bb432' -> ['aa', '123', 'bb', '432'] +std::vector<QString> IdStringList::alphaNumSplit(const QString &str) { -} + std::vector<QString> res; + QString current_part; + + bool number = true; + for (const auto c : str) { + if (current_part.size() == 0 && res.size() == 0) { + current_part.push_back(c); + number = c.isNumber(); + continue; + } -ContextTreeItem::ContextTreeItem(IdString id, ElementType type, QString name) - : parentNode(nullptr), itemId(id), itemType(type), itemName(name) -{ -} + if (number != c.isNumber()) { + number = c.isNumber(); + res.push_back(current_part); + current_part.clear(); + } -ContextTreeItem::~ContextTreeItem() -{ - if (parentNode) - parentNode->children.removeOne(this); - qDeleteAll(children); + current_part.push_back(c); + } + + res.push_back(current_part); + + return res; } -void ContextTreeItem::addChild(ContextTreeItem *item) + +void IdStringList::updateElements(Context *ctx, std::vector<IdString> elements) { - item->parentNode = this; - children.append(item); + // 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 IdStringItem(ctx, elem, this, child_type_); + managed_.emplace(elem, std::unique_ptr<IdStringItem>(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 Item *a, const Item *b){ + auto parts_a = alphaNumSplit(a->name()); + auto parts_b = alphaNumSplit(b->name()); + + // Short-circuit for different part count. + if (parts_a.size() != parts_b.size()) { + return parts_a.size() < parts_b.size(); + } + + for (size_t i = 0; i < parts_a.size(); i++) { + auto &part_a = parts_a.at(i); + auto &part_b = parts_b.at(i); + + bool a_is_number, b_is_number; + int a_number = part_a.toInt(&a_is_number); + int b_number = part_b.toInt(&b_is_number); + + // If both parts are numbers, compare numerically. + // If they're equal, continue to next part. + if (a_is_number && b_is_number) { + if (a_number != b_number) { + return a_number < b_number; + } else { + continue; + } + } + + // For different alpha/nonalpha types, make numeric parts appear + // first. + if (a_is_number != b_is_number) { + return a_is_number; + } + + // If both parts are numbers, compare lexically. + // If they're equal, continue to next part. + if (part_a == part_b) { + continue; + } + return part_a < part_b; + } + + // Same string. + return true; + }); } -void ContextTreeItem::sort() +void IdStringList::search(QList<Item*> &results, QString text, int limit) { - for (auto item : children) - if (item->count()>1) item->sort(); - qSort(children.begin(), children.end(), contextTreeItemLessThan); + for (const auto &child : children_) { + if (limit != -1 && results.size() > limit) + return; + + if (child->name().contains(text)) + results.push_back(child); + } } -ContextTreeModel::ContextTreeModel(QObject *parent) : QAbstractItemModel(parent) { root = new ContextTreeItem(); } -ContextTreeModel::~ContextTreeModel() { delete root; } +Model::Model(QObject *parent) : + QAbstractItemModel(parent), + root_(new Item("Elements", nullptr)) {} + +Model::~Model() {} -void ContextTreeModel::loadData(Context *ctx) +void Model::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, ElementType::BEL)); + + 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, ElementType::WIRE)); + + 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, ElementType::PIP)); } #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(), ElementType::CELL)); + net_root_ = std::unique_ptr<IdStringList>(new IdStringList(QString("Nets"), root_.get(), ElementType::NET)); endResetModel(); + + updateCellsNets(ctx); } -void ContextTreeModel::updateData(Context *ctx) +void Model::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(); } -int ContextTreeModel::rowCount(const QModelIndex &parent) const { return nodeFromIndex(parent)->count(); } +int Model::rowCount(const QModelIndex &parent) const { return nodeFromIndex(parent)->count(); } -int ContextTreeModel::columnCount(const QModelIndex &parent) const { return 1; } +int Model::columnCount(const QModelIndex &parent) const { return 1; } -QModelIndex ContextTreeModel::index(int row, int column, const QModelIndex &parent) const +QModelIndex Model::index(int row, int column, const QModelIndex &parent) const { - ContextTreeItem *node = nodeFromIndex(parent); + Item *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 +QModelIndex Model::parent(const QModelIndex &child) const { - ContextTreeItem *parent = nodeFromIndex(child)->parent(); - if (parent == root) + Item *parent = nodeFromIndex(child)->parent(); + if (parent == root_.get()) return QModelIndex(); - ContextTreeItem *node = parent->parent(); + Item *node = parent->parent(); return createIndex(node->indexOf(parent), 0, parent); } -QVariant ContextTreeModel::data(const QModelIndex &index, int role) const +QVariant Model::data(const QModelIndex &index, int role) const { if (index.column() != 0) return QVariant(); if (role != Qt::DisplayRole) return QVariant(); - ContextTreeItem *node = nodeFromIndex(index); + Item *node = nodeFromIndex(index); return node->name(); } -QVariant ContextTreeModel::headerData(int section, Qt::Orientation orientation, int role) const +QVariant Model::headerData(int section, Qt::Orientation orientation, int role) const { Q_UNUSED(section); if (orientation == Qt::Horizontal && role == Qt::DisplayRole) @@ -285,62 +257,48 @@ QVariant ContextTreeModel::headerData(int section, Qt::Orientation orientation, return QVariant(); } -ContextTreeItem *ContextTreeModel::nodeFromIndex(const QModelIndex &idx) const +Item *Model::nodeFromIndex(const QModelIndex &idx) const { if (idx.isValid()) - return (ContextTreeItem *)idx.internalPointer(); - return root; + return (Item *)idx.internalPointer(); + return root_.get(); } -static int getElementIndex(ElementType type) +Qt::ItemFlags Model::flags(const QModelIndex &index) const { - if (type == ElementType::BEL) - return 0; - if (type == ElementType::WIRE) - return 1; - if (type == ElementType::PIP) - return 2; - if (type == ElementType::NET) - return 3; - if (type == ElementType::CELL) - return 4; - return -1; + Item *node = nodeFromIndex(index); + return Qt::ItemIsEnabled | (node->type() != ElementType::NONE ? Qt::ItemIsSelectable : Qt::NoItemFlags); } -ContextTreeItem *ContextTreeModel::nodeForIdType(const ElementType type, const QString name) const -{ - int index = getElementIndex(type); - if (type != ElementType::NONE && nameToItem[index].contains(name)) - return nameToItem[index].value(name); - return nullptr; -} -QModelIndex ContextTreeModel::indexFromNode(ContextTreeItem *node) +void Model::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 Model::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> Model::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 - } - } + const int limit = 500; + + QList<Item*> list; + cell_root_->search(list, text, limit); + net_root_->search(list, text, limit); + bel_root_->search(list, text, limit); + wire_root_->search(list, text, limit); + pip_root_->search(list, text, limit); + + QList<QModelIndex> res; + for (auto i : list) { + res.push_back(indexFromNode(i)); } - return list; + return res; } + +}; // namespace TreeModel + NEXTPNR_NAMESPACE_END |