From ba0ab7cb3069b76c2f6644afb211f66c54c7208c Mon Sep 17 00:00:00 2001 From: Miodrag Milanovic Date: Sat, 20 Oct 2018 16:52:38 +0200 Subject: simplify and move arround --- gui/designwidget.cc | 62 +++++++++++++++++++++++++++++++++++++++++++---------- gui/treemodel.cc | 61 ++-------------------------------------------------- gui/treemodel.h | 20 +++++------------ 3 files changed, 58 insertions(+), 85 deletions(-) (limited to 'gui') diff --git a/gui/designwidget.cc b/gui/designwidget.cc index a75147d0..300c8271 100644 --- a/gui/designwidget.cc +++ b/gui/designwidget.cc @@ -265,11 +265,49 @@ void DesignWidget::newContext(Context *ctx) std::lock_guard lock_ui(ctx->ui_mutex); std::lock_guard lock(ctx->mutex); - getTreeByElementType(ElementType::BEL)->loadContext(ElementType::BEL, ctx); - getTreeByElementType(ElementType::WIRE)->loadContext(ElementType::WIRE, ctx); - getTreeByElementType(ElementType::PIP)->loadContext(ElementType::PIP, ctx); - getTreeByElementType(ElementType::CELL)->loadContext(ElementType::CELL, ctx); - getTreeByElementType(ElementType::NET)->loadContext(ElementType::NET, ctx); + { + std::map, std::vector> belMap; + for (auto bel : ctx->getBels()) { + auto loc = ctx->getBelLocation(bel); + belMap[std::pair(loc.x, loc.y)].push_back(bel); + } + auto belGetter = [](Context *ctx, BelId id) { return ctx->getBelName(id); }; + + getTreeByElementType(ElementType::BEL)->loadData(std::unique_ptr>( + new TreeModel::ElementXYRoot(ctx, belMap, belGetter, ElementType::BEL))); + } + +#ifdef ARCH_ICE40 + { + std::map, std::vector> 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(wire->x, wire->y)].push_back(wireid); + } + auto wireGetter = [](Context *ctx, WireId id) { return ctx->getWireName(id); }; + getTreeByElementType(ElementType::WIRE)->loadData(std::unique_ptr>( + new TreeModel::ElementXYRoot(ctx, wireMap, wireGetter, ElementType::WIRE))); + } + + { + std::map, std::vector> 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(pip->x, pip->y)].push_back(pipid); + } + auto pipGetter = [](Context *ctx, PipId id) { return ctx->getPipName(id); }; + getTreeByElementType(ElementType::PIP)->loadData(std::unique_ptr>( + new TreeModel::ElementXYRoot(ctx, pipMap, pipGetter, ElementType::PIP))); + } +#endif + getTreeByElementType(ElementType::CELL)->loadData(std::unique_ptr(new TreeModel::IdStringList(ElementType::CELL))); + getTreeByElementType(ElementType::NET)->loadData(std::unique_ptr(new TreeModel::IdStringList(ElementType::NET))); + getTreeByElementType(ElementType::CELL)->updateCells(ctx); + getTreeByElementType(ElementType::NET)->updateNets(ctx); } updateTree(); } @@ -406,7 +444,7 @@ void DesignWidget::onClickedBel(BelId bel, bool keep) std::lock_guard lock_ui(ctx->ui_mutex); std::lock_guard lock(ctx->mutex); - item = getTreeByElementType(ElementType::BEL)->nodeForIdType(ElementType::BEL, ctx->getBelName(bel)); + item = getTreeByElementType(ElementType::BEL)->nodeForId(ctx->getBelName(bel)); if (!item) return; @@ -424,7 +462,7 @@ void DesignWidget::onClickedWire(WireId wire, bool keep) std::lock_guard lock_ui(ctx->ui_mutex); std::lock_guard lock(ctx->mutex); - item = getTreeByElementType(ElementType::WIRE)->nodeForIdType(ElementType::WIRE, ctx->getWireName(wire)); + item = getTreeByElementType(ElementType::WIRE)->nodeForId(ctx->getWireName(wire)); if (!item) return; @@ -442,7 +480,7 @@ void DesignWidget::onClickedPip(PipId pip, bool keep) std::lock_guard lock_ui(ctx->ui_mutex); std::lock_guard lock(ctx->mutex); - item = getTreeByElementType(ElementType::PIP)->nodeForIdType(ElementType::PIP, ctx->getPipName(pip)); + item = getTreeByElementType(ElementType::PIP)->nodeForId(ctx->getPipName(pip)); if (!item) return; @@ -783,7 +821,7 @@ void DesignWidget::prepareMenuProperty(const QPoint &pos) if (type == ElementType::NONE) continue; IdString value = ctx->id(selectedProperty->valueText().toStdString()); - auto node = getTreeByElementType(type)->nodeForIdType(type, value); + auto node = getTreeByElementType(type)->nodeForId(value); if (!node) continue; items.append(*node); @@ -863,7 +901,9 @@ void DesignWidget::onItemDoubleClicked(QTreeWidgetItem *item, int column) { QtProperty *selectedProperty = propertyEditor->itemToBrowserItem(item)->property(); ElementType type = getElementTypeByName(selectedProperty->propertyId()); - auto it = getTreeByElementType(type)->nodeForIdType(type, ctx->id(selectedProperty->valueText().toStdString())); + if (type == ElementType::NONE) + return; + auto it = getTreeByElementType(type)->nodeForId(ctx->id(selectedProperty->valueText().toStdString())); if (it) { int num = getIndexByElementType(type); if (tabWidget->currentIndex()!=num) tabWidget->setCurrentIndex(num); @@ -914,7 +954,7 @@ void DesignWidget::onHoverPropertyChanged(QtBrowserItem *item) if (type != ElementType::NONE) { IdString value = ctx->id(selectedProperty->valueText().toStdString()); if (value != IdString()) { - auto node = getTreeByElementType(type)->nodeForIdType(type, value); + auto node = getTreeByElementType(type)->nodeForId(value); if (node) { std::vector decals = getDecals((*node)->type(), (*node)->id()); if (decals.size() > 0) diff --git a/gui/treemodel.cc b/gui/treemodel.cc index d26738d6..ee18dd57 100644 --- a/gui/treemodel.cc +++ b/gui/treemodel.cc @@ -154,68 +154,11 @@ Model::Model(QObject *parent) : QAbstractItemModel(parent), root_(new Item("Elem Model::~Model() {} -void Model::loadContext(ElementType type, Context *ctx) +void Model::loadData(std::unique_ptr data) { - if (!ctx) - return; - ctx_ = ctx; - beginResetModel(); - - // 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. - { - if (type==ElementType::BEL) - { - std::map, std::vector> belMap; - for (auto bel : ctx->getBels()) { - auto loc = ctx->getBelLocation(bel); - belMap[std::pair(loc.x, loc.y)].push_back(bel); - } - auto belGetter = [](Context *ctx, BelId id) { return ctx->getBelName(id); }; - root_ = std::unique_ptr>( - new ElementXYRoot(ctx, "Bels", nullptr, belMap, belGetter, ElementType::BEL)); - } -#ifdef ARCH_ICE40 - if (type==ElementType::WIRE) - { - std::map, std::vector> 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(wire->x, wire->y)].push_back(wireid); - } - auto wireGetter = [](Context *ctx, WireId id) { return ctx->getWireName(id); }; - root_ = std::unique_ptr>( - new ElementXYRoot(ctx, "Wires", nullptr, wireMap, wireGetter, ElementType::WIRE)); - } - - if (type==ElementType::PIP) - { - std::map, std::vector> 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(pip->x, pip->y)].push_back(pipid); - } - auto pipGetter = [](Context *ctx, PipId id) { return ctx->getPipName(id); }; - root_ = std::unique_ptr>( - new ElementXYRoot(ctx, "Pips", nullptr, pipMap, pipGetter, ElementType::PIP)); - } -#endif - } - - if (type==ElementType::CELL) root_ = std::unique_ptr(new IdStringList(QString("Cells"), nullptr, ElementType::CELL)); - if (type==ElementType::NET) root_ = std::unique_ptr(new IdStringList(QString("Nets"), nullptr, ElementType::NET)); - + root_ = std::move(data); endResetModel(); - - if (type==ElementType::CELL) updateCells(ctx); - if (type==ElementType::NET) updateNets(ctx); } void Model::updateCells(Context *ctx) diff --git a/gui/treemodel.h b/gui/treemodel.h index 9c2b6952..b4f23cd0 100644 --- a/gui/treemodel.h +++ b/gui/treemodel.h @@ -147,7 +147,7 @@ class IdStringList : public Item public: // Create an IdStringList at given partent that will contain elements of // the given type. - IdStringList(QString name, Item *parent, ElementType type) : Item(name, parent), child_type_(type) {} + IdStringList(ElementType type) : Item("root", nullptr), child_type_(type) {} // Split a name into alpha/non-alpha parts, which is then used for sorting // of children. @@ -282,8 +282,8 @@ template class ElementXYRoot : public Item ElementType child_type_; public: - ElementXYRoot(Context *ctx, QString name, Item *parent, ElementMap map, ElementGetter getter, ElementType type) - : Item(name, parent), ctx_(ctx), map_(map), getter_(getter), child_type_(type) + ElementXYRoot(Context *ctx, ElementMap map, ElementGetter getter, ElementType type) + : Item("root", nullptr), ctx_(ctx), map_(map), getter_(getter), child_type_(type) { // Create all X and Y label Items/ElementLists. @@ -352,7 +352,7 @@ class Model : public QAbstractItemModel Model(QObject *parent = nullptr); ~Model(); - void loadContext(ElementType type, Context *ctx); + void loadData(std::unique_ptr data); void updateCells(Context *ctx); void updateNets(Context *ctx); Item *nodeFromIndex(const QModelIndex &idx) const; @@ -367,17 +367,7 @@ class Model : public QAbstractItemModel QList search(QString text); - boost::optional nodeForIdType(ElementType type, IdString id) const - { - switch (type) { - case ElementType::NONE: - return boost::none; - default: - if (root_ == nullptr) - return boost::none; - return root_->getById(id); - } - } + boost::optional nodeForId(IdString id) const { return root_->getById(id); } // Override QAbstractItemModel methods int rowCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE; -- cgit v1.2.3