aboutsummaryrefslogtreecommitdiffstats
path: root/gui/treemodel.h
diff options
context:
space:
mode:
authorSergiusz Bazanski <q3k@q3k.org>2018-08-01 01:59:58 +0100
committerSergiusz Bazanski <q3k@q3k.org>2018-08-01 01:59:58 +0100
commitbf43c27567c475cbd8f2c9c66295d492cfd565e8 (patch)
treef293b4d3cd5abddaca1c9dd12a7970755253c02b /gui/treemodel.h
parente9e7004bf9b6abf27bf61063b4abbe5895a11073 (diff)
downloadnextpnr-bf43c27567c475cbd8f2c9c66295d492cfd565e8.tar.gz
nextpnr-bf43c27567c475cbd8f2c9c66295d492cfd565e8.tar.bz2
nextpnr-bf43c27567c475cbd8f2c9c66295d492cfd565e8.zip
gui: restore lookup into tree after new model
Diffstat (limited to 'gui/treemodel.h')
-rw-r--r--gui/treemodel.h83
1 files changed, 76 insertions, 7 deletions
diff --git a/gui/treemodel.h b/gui/treemodel.h
index 69116112..37785f7a 100644
--- a/gui/treemodel.h
+++ b/gui/treemodel.h
@@ -21,8 +21,9 @@
#define TREEMODEL_H
#include <QAbstractItemModel>
+#include <boost/optional.hpp>
+
#include "nextpnr.h"
-#include "log.h"
NEXTPNR_NAMESPACE_BEGIN
@@ -75,11 +76,22 @@ class LazyTreeItem
return children_.at(index);
}
- int indexOf(LazyTreeItem *child) const
+ int indexOf(const LazyTreeItem *child) const
+ {
+ // Dropping the const for indexOf to work.
+ return children_.indexOf((LazyTreeItem *)child, 0);
+ }
+
+ int indexOf(LazyTreeItem *child)
{
return children_.indexOf(child, 0);
}
+ const LazyTreeItem *parent() const
+ {
+ return parent_;
+ }
+
LazyTreeItem *parent()
{
return parent_;
@@ -146,7 +158,7 @@ class ElementList : public LazyTreeItem
const ElementMap *map_;
int x_, y_;
ElementGetter getter_;
- std::vector<std::unique_ptr<StaticTreeItem>> managed_;
+ std::unordered_map<IdString, std::unique_ptr<StaticTreeItem>> managed_;
ElementType child_type_;
// scope valid until map gets mutated...
@@ -180,7 +192,7 @@ class ElementList : public LazyTreeItem
name.remove(0, prefix.size());
auto item = new IdStringItem(ctx_, idstring, this, child_type_);
- managed_.push_back(std::move(std::unique_ptr<StaticTreeItem>(item)));
+ managed_[idstring] = std::move(std::unique_ptr<StaticTreeItem>(item));
}
}
@@ -193,6 +205,18 @@ class ElementList : public LazyTreeItem
{
return IdString();
}
+
+ boost::optional<LazyTreeItem*> getById(IdString id)
+ {
+ // Search requires us to load all our elements...
+ while (canFetchMore()) fetchMore();
+
+ auto res = managed_.find(id);
+ if (res != managed_.end()) {
+ return res->second.get();
+ }
+ return boost::none;
+ }
};
class IdStringList : public StaticTreeItem
@@ -232,6 +256,11 @@ class IdStringList : public StaticTreeItem
return res;
}
+ IdStringItem *getById(IdString id) const
+ {
+ return managed_.at(id).get();
+ }
+
void updateElements(Context *ctx, std::vector<IdString> elements)
{
// for any elements that are not yet in managed_, created them.
@@ -310,7 +339,8 @@ class ElementXYRoot : public StaticTreeItem
private:
Context *ctx_;
- std::vector<std::unique_ptr<LazyTreeItem>> managed_;
+ std::vector<std::unique_ptr<StaticTreeItem>> managed_labels_;
+ std::vector<std::unique_ptr<ElementList<ElementT>>> managed_lists_;
ElementMap map_;
ElementGetter getter_;
ElementType child_type_;
@@ -335,13 +365,26 @@ class ElementXYRoot : public StaticTreeItem
// create X item for tree
auto item = new StaticTreeItem(QString("X%1").arg(i), this, child_type_);
- managed_.push_back(std::move(std::unique_ptr<LazyTreeItem>(item)));
+ managed_labels_.push_back(std::move(std::unique_ptr<StaticTreeItem>(item)));
for (auto j : y_present) {
auto item2 = new ElementList<ElementT>(ctx_, QString("Y%1").arg(j), item, &map_, i, j, getter_, child_type_);
item2->fetchMore(1);
- managed_.push_back(std::move(std::unique_ptr<LazyTreeItem>(item2)));
+ managed_lists_.push_back(std::move(std::unique_ptr<ElementList<ElementT>>(item2)));
+ }
+ }
+ }
+
+ boost::optional<LazyTreeItem*> getById(IdString id)
+ {
+ // For now, scan linearly all ElementLists.
+ // TODO(q3k) fix this once we have tree API from arch
+ for (auto &l : managed_lists_) {
+ auto res = l->getById(id);
+ if (res) {
+ return res;
}
}
+ return boost::none;
}
};
@@ -358,7 +401,33 @@ class ContextTreeModel : public QAbstractItemModel
void loadContext(Context *ctx);
void updateCellsNets(Context *ctx);
LazyTreeItem *nodeFromIndex(const QModelIndex &idx) const;
+ QModelIndex indexFromNode(LazyTreeItem *node)
+ {
+ const LazyTreeItem *parent = node->parent();
+ if (parent == nullptr)
+ return QModelIndex();
+
+ return createIndex(parent->indexOf(node), 0, node);
+ }
+
QList<QModelIndex> search(QString text);
+ boost::optional<LazyTreeItem*> nodeForIdType(ElementType type, IdString id) const
+ {
+ switch (type) {
+ case ElementType::BEL:
+ return bel_root_->getById(id);
+ case ElementType::WIRE:
+ return wire_root_->getById(id);
+ case ElementType::PIP:
+ return pip_root_->getById(id);
+ case ElementType::CELL:
+ return cell_root_->getById(id);
+ case ElementType::NET:
+ return net_root_->getById(id);
+ default:
+ return boost::none;
+ }
+ }
// Override QAbstractItemModel methods
int rowCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;