simplify and move arround

This commit is contained in:
Miodrag Milanovic 2018-10-20 16:52:38 +02:00
parent 5d5324c073
commit ba0ab7cb30
3 changed files with 58 additions and 85 deletions

View File

@ -265,11 +265,49 @@ void DesignWidget::newContext(Context *ctx)
std::lock_guard<std::mutex> lock_ui(ctx->ui_mutex); std::lock_guard<std::mutex> lock_ui(ctx->ui_mutex);
std::lock_guard<std::mutex> lock(ctx->mutex); std::lock_guard<std::mutex> lock(ctx->mutex);
getTreeByElementType(ElementType::BEL)->loadContext(ElementType::BEL, ctx); {
getTreeByElementType(ElementType::WIRE)->loadContext(ElementType::WIRE, ctx); std::map<std::pair<int, int>, std::vector<BelId>> belMap;
getTreeByElementType(ElementType::PIP)->loadContext(ElementType::PIP, ctx); for (auto bel : ctx->getBels()) {
getTreeByElementType(ElementType::CELL)->loadContext(ElementType::CELL, ctx); auto loc = ctx->getBelLocation(bel);
getTreeByElementType(ElementType::NET)->loadContext(ElementType::NET, ctx); belMap[std::pair<int, int>(loc.x, loc.y)].push_back(bel);
}
auto belGetter = [](Context *ctx, BelId id) { return ctx->getBelName(id); };
getTreeByElementType(ElementType::BEL)->loadData(std::unique_ptr<TreeModel::ElementXYRoot<BelId>>(
new TreeModel::ElementXYRoot<BelId>(ctx, belMap, belGetter, ElementType::BEL)));
}
#ifdef ARCH_ICE40
{
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);
}
auto wireGetter = [](Context *ctx, WireId id) { return ctx->getWireName(id); };
getTreeByElementType(ElementType::WIRE)->loadData(std::unique_ptr<TreeModel::ElementXYRoot<WireId>>(
new TreeModel::ElementXYRoot<WireId>(ctx, 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);
}
auto pipGetter = [](Context *ctx, PipId id) { return ctx->getPipName(id); };
getTreeByElementType(ElementType::PIP)->loadData(std::unique_ptr<TreeModel::ElementXYRoot<PipId>>(
new TreeModel::ElementXYRoot<PipId>(ctx, pipMap, pipGetter, ElementType::PIP)));
}
#endif
getTreeByElementType(ElementType::CELL)->loadData(std::unique_ptr<TreeModel::IdStringList>(new TreeModel::IdStringList(ElementType::CELL)));
getTreeByElementType(ElementType::NET)->loadData(std::unique_ptr<TreeModel::IdStringList>(new TreeModel::IdStringList(ElementType::NET)));
getTreeByElementType(ElementType::CELL)->updateCells(ctx);
getTreeByElementType(ElementType::NET)->updateNets(ctx);
} }
updateTree(); updateTree();
} }
@ -406,7 +444,7 @@ void DesignWidget::onClickedBel(BelId bel, bool keep)
std::lock_guard<std::mutex> lock_ui(ctx->ui_mutex); std::lock_guard<std::mutex> lock_ui(ctx->ui_mutex);
std::lock_guard<std::mutex> lock(ctx->mutex); std::lock_guard<std::mutex> lock(ctx->mutex);
item = getTreeByElementType(ElementType::BEL)->nodeForIdType(ElementType::BEL, ctx->getBelName(bel)); item = getTreeByElementType(ElementType::BEL)->nodeForId(ctx->getBelName(bel));
if (!item) if (!item)
return; return;
@ -424,7 +462,7 @@ void DesignWidget::onClickedWire(WireId wire, bool keep)
std::lock_guard<std::mutex> lock_ui(ctx->ui_mutex); std::lock_guard<std::mutex> lock_ui(ctx->ui_mutex);
std::lock_guard<std::mutex> lock(ctx->mutex); std::lock_guard<std::mutex> lock(ctx->mutex);
item = getTreeByElementType(ElementType::WIRE)->nodeForIdType(ElementType::WIRE, ctx->getWireName(wire)); item = getTreeByElementType(ElementType::WIRE)->nodeForId(ctx->getWireName(wire));
if (!item) if (!item)
return; return;
@ -442,7 +480,7 @@ void DesignWidget::onClickedPip(PipId pip, bool keep)
std::lock_guard<std::mutex> lock_ui(ctx->ui_mutex); std::lock_guard<std::mutex> lock_ui(ctx->ui_mutex);
std::lock_guard<std::mutex> lock(ctx->mutex); std::lock_guard<std::mutex> lock(ctx->mutex);
item = getTreeByElementType(ElementType::PIP)->nodeForIdType(ElementType::PIP, ctx->getPipName(pip)); item = getTreeByElementType(ElementType::PIP)->nodeForId(ctx->getPipName(pip));
if (!item) if (!item)
return; return;
@ -783,7 +821,7 @@ void DesignWidget::prepareMenuProperty(const QPoint &pos)
if (type == ElementType::NONE) if (type == ElementType::NONE)
continue; continue;
IdString value = ctx->id(selectedProperty->valueText().toStdString()); IdString value = ctx->id(selectedProperty->valueText().toStdString());
auto node = getTreeByElementType(type)->nodeForIdType(type, value); auto node = getTreeByElementType(type)->nodeForId(value);
if (!node) if (!node)
continue; continue;
items.append(*node); items.append(*node);
@ -863,7 +901,9 @@ void DesignWidget::onItemDoubleClicked(QTreeWidgetItem *item, int column)
{ {
QtProperty *selectedProperty = propertyEditor->itemToBrowserItem(item)->property(); QtProperty *selectedProperty = propertyEditor->itemToBrowserItem(item)->property();
ElementType type = getElementTypeByName(selectedProperty->propertyId()); 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) { if (it) {
int num = getIndexByElementType(type); int num = getIndexByElementType(type);
if (tabWidget->currentIndex()!=num) tabWidget->setCurrentIndex(num); if (tabWidget->currentIndex()!=num) tabWidget->setCurrentIndex(num);
@ -914,7 +954,7 @@ void DesignWidget::onHoverPropertyChanged(QtBrowserItem *item)
if (type != ElementType::NONE) { if (type != ElementType::NONE) {
IdString value = ctx->id(selectedProperty->valueText().toStdString()); IdString value = ctx->id(selectedProperty->valueText().toStdString());
if (value != IdString()) { if (value != IdString()) {
auto node = getTreeByElementType(type)->nodeForIdType(type, value); auto node = getTreeByElementType(type)->nodeForId(value);
if (node) { if (node) {
std::vector<DecalXY> decals = getDecals((*node)->type(), (*node)->id()); std::vector<DecalXY> decals = getDecals((*node)->type(), (*node)->id());
if (decals.size() > 0) if (decals.size() > 0)

View File

@ -154,68 +154,11 @@ Model::Model(QObject *parent) : QAbstractItemModel(parent), root_(new Item("Elem
Model::~Model() {} Model::~Model() {}
void Model::loadContext(ElementType type, Context *ctx) void Model::loadData(std::unique_ptr<Item> data)
{ {
if (!ctx)
return;
ctx_ = ctx;
beginResetModel(); beginResetModel();
root_ = std::move(data);
// 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::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);
}
auto belGetter = [](Context *ctx, BelId id) { return ctx->getBelName(id); };
root_ = std::unique_ptr<ElementXYRoot<BelId>>(
new ElementXYRoot<BelId>(ctx, "Bels", nullptr, belMap, belGetter, ElementType::BEL));
}
#ifdef ARCH_ICE40
if (type==ElementType::WIRE)
{
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);
}
auto wireGetter = [](Context *ctx, WireId id) { return ctx->getWireName(id); };
root_ = std::unique_ptr<ElementXYRoot<WireId>>(
new ElementXYRoot<WireId>(ctx, "Wires", nullptr, wireMap, wireGetter, ElementType::WIRE));
}
if (type==ElementType::PIP)
{
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);
}
auto pipGetter = [](Context *ctx, PipId id) { return ctx->getPipName(id); };
root_ = std::unique_ptr<ElementXYRoot<PipId>>(
new ElementXYRoot<PipId>(ctx, "Pips", nullptr, pipMap, pipGetter, ElementType::PIP));
}
#endif
}
if (type==ElementType::CELL) root_ = std::unique_ptr<IdStringList>(new IdStringList(QString("Cells"), nullptr, ElementType::CELL));
if (type==ElementType::NET) root_ = std::unique_ptr<IdStringList>(new IdStringList(QString("Nets"), nullptr, ElementType::NET));
endResetModel(); endResetModel();
if (type==ElementType::CELL) updateCells(ctx);
if (type==ElementType::NET) updateNets(ctx);
} }
void Model::updateCells(Context *ctx) void Model::updateCells(Context *ctx)

View File

@ -147,7 +147,7 @@ class IdStringList : public Item
public: public:
// Create an IdStringList at given partent that will contain elements of // Create an IdStringList at given partent that will contain elements of
// the given type. // 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 // Split a name into alpha/non-alpha parts, which is then used for sorting
// of children. // of children.
@ -282,8 +282,8 @@ template <typename ElementT> class ElementXYRoot : public Item
ElementType child_type_; ElementType child_type_;
public: public:
ElementXYRoot(Context *ctx, QString name, Item *parent, ElementMap map, ElementGetter getter, ElementType type) ElementXYRoot(Context *ctx, ElementMap map, ElementGetter getter, ElementType type)
: Item(name, parent), ctx_(ctx), map_(map), getter_(getter), child_type_(type) : Item("root", nullptr), ctx_(ctx), map_(map), getter_(getter), child_type_(type)
{ {
// Create all X and Y label Items/ElementLists. // Create all X and Y label Items/ElementLists.
@ -352,7 +352,7 @@ class Model : public QAbstractItemModel
Model(QObject *parent = nullptr); Model(QObject *parent = nullptr);
~Model(); ~Model();
void loadContext(ElementType type, Context *ctx); void loadData(std::unique_ptr<Item> data);
void updateCells(Context *ctx); void updateCells(Context *ctx);
void updateNets(Context *ctx); void updateNets(Context *ctx);
Item *nodeFromIndex(const QModelIndex &idx) const; Item *nodeFromIndex(const QModelIndex &idx) const;
@ -367,17 +367,7 @@ class Model : public QAbstractItemModel
QList<QModelIndex> search(QString text); QList<QModelIndex> search(QString text);
boost::optional<Item *> nodeForIdType(ElementType type, IdString id) const boost::optional<Item *> nodeForId(IdString id) const { return root_->getById(id); }
{
switch (type) {
case ElementType::NONE:
return boost::none;
default:
if (root_ == nullptr)
return boost::none;
return root_->getById(id);
}
}
// Override QAbstractItemModel methods // Override QAbstractItemModel methods
int rowCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE; int rowCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;