nextpnr/gui/treemodel.h

428 lines
14 KiB
C
Raw Normal View History

2018-07-28 21:44:00 +08:00
/*
* nextpnr -- Next Generation Place and Route
*
* Copyright (C) 2018 Miodrag Milanovic <miodrag@symbioticeda.com>
2018-08-01 09:59:07 +08:00
* Copyright (C) 2018 Serge Bazanski <q3k@symbioticeda.com>
2018-07-28 21:44:00 +08:00
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
*/
#ifndef TREEMODEL_H
#define TREEMODEL_H
#include <QAbstractItemModel>
#include <boost/optional.hpp>
2018-07-28 21:44:00 +08:00
#include "nextpnr.h"
NEXTPNR_NAMESPACE_BEGIN
enum class ElementType
{
NONE,
BEL,
WIRE,
PIP,
NET,
CELL,
GROUP
};
namespace TreeModel {
2018-08-01 09:59:07 +08:00
// Item is a leaf or non-leaf item in the TreeModel hierarchy. It does not
// manage any memory.
// It has a list of children, and when created it registers itself as a child
// of its parent.
// It has some PNR-specific members, like type (if any), idstring (if ay).
// They should be overwritten by deriving classes to make them relate to an
// object somewhere in the arch universe.
// It also has provisions for lazy loading of data, via the canFetchMore and
// fetchMore methods.
class Item
2018-07-31 22:49:47 +08:00
{
protected:
2018-08-01 09:59:07 +08:00
// Human-friendly name of this item.
2018-07-31 22:49:47 +08:00
QString name_;
2018-08-01 09:59:07 +08:00
// Parent or nullptr if root.
Item *parent_;
2018-08-01 09:59:07 +08:00
// Children that are loaded into memory.
QList<Item *> children_;
2018-07-31 22:49:47 +08:00
void addChild(Item *child)
2018-07-31 22:49:47 +08:00
{
children_.append(child);
}
public:
2018-08-01 09:59:07 +08:00
Item(QString name, Item *parent) :
name_(name), parent_(parent)
2018-07-31 22:49:47 +08:00
{
// Register in parent if exists.
if (parent_ != nullptr) {
parent_->addChild(this);
}
};
2018-08-01 09:59:07 +08:00
// Number of children.
int count() const { return children_.count(); }
2018-07-31 22:49:47 +08:00
2018-08-01 09:59:07 +08:00
// Name getter.
QString name() const { return name_; }
2018-07-31 22:49:47 +08:00
2018-08-01 09:59:07 +08:00
// Child getter.
Item *child(int index) { return children_.at(index); }
2018-07-31 22:49:47 +08:00
2018-08-01 09:59:07 +08:00
// Parent getter.
const Item *parent() const { return parent_; }
Item *parent() { return parent_; }
// indexOf gets index of child in children array.
int indexOf(const Item *child) const
{
// Dropping the const for indexOf to work.
return children_.indexOf((Item *)child, 0);
}
2018-08-01 09:59:07 +08:00
int indexOf(Item *child) { return children_.indexOf(child, 0); }
2018-08-01 09:59:07 +08:00
// Arch id and type that correspond to this element.
virtual IdString id() const { return IdString(); }
virtual ElementType type() const { return ElementType::NONE; }
2018-07-31 22:49:47 +08:00
2018-08-01 09:59:07 +08:00
// Lazy loading methods.
virtual bool canFetchMore() const { return false; }
virtual void fetchMore() {}
2018-07-31 22:49:47 +08:00
~Item() {}
2018-07-31 22:49:47 +08:00
};
2018-08-01 09:59:07 +08:00
// IdString is an Item that corresponds to a real element in Arch.
class IdStringItem : public Item
2018-08-01 08:27:20 +08:00
{
private:
IdString id_;
2018-08-01 09:59:07 +08:00
ElementType type_;
2018-08-01 08:27:20 +08:00
public:
IdStringItem(Context *ctx, IdString str, Item *parent, ElementType type) :
2018-08-01 09:59:07 +08:00
Item(QString(str.c_str(ctx)), parent), id_(str), type_(type) {}
2018-08-01 08:27:20 +08:00
virtual IdString id() const override
{
return id_;
}
2018-08-01 09:59:07 +08:00
virtual ElementType type() const override
{
return type_;
}
2018-08-01 08:27:20 +08:00
};
2018-08-01 09:59:07 +08:00
// IdString list is a static list of IdStrings which can be set/updates from
// a vector of IdStrings. It will render each IdStrings as a child, with the
// list sorted in a smart way.
class IdStringList : public Item
{
private:
// Children that we manage the memory for, stored for quick lookup from
// IdString to child.
std::unordered_map<IdString, std::unique_ptr<IdStringItem>> managed_;
// Type of children that the list creates.
ElementType child_type_;
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) {}
// Split a name into alpha/non-alpha parts, which is then used for sorting
// of children.
static std::vector<QString> alphaNumSplit(const QString &str);
// getById finds a child for the given IdString.
IdStringItem *getById(IdString id) const
{
return managed_.at(id).get();
}
// (Re-)create children from a list of IdStrings.
void updateElements(Context *ctx, std::vector<IdString> elements);
2018-08-01 10:11:22 +08:00
// Find children that contain the given text.
void search(QList<Item*> &results, QString text, int limit);
2018-08-01 09:59:07 +08:00
};
// ElementList is a dynamic list of ElementT (BelId,WireId,...) that are
// automatically generated based on an overall map of elements.
// ElementList is emitted from ElementXYRoot, and contains the actual
// Bels/Wires/Pips underneath it.
2018-07-31 22:49:47 +08:00
template <typename ElementT>
class ElementList : public Item
2018-07-31 22:49:47 +08:00
{
public:
2018-08-01 09:59:07 +08:00
// A map from tile (X,Y) to list of ElementTs in that tile.
2018-07-31 22:49:47 +08:00
using ElementMap = std::map<std::pair<int, int>, std::vector<ElementT>>;
2018-08-01 09:59:07 +08:00
// A method that converts an ElementT to an IdString.
2018-07-31 22:49:47 +08:00
using ElementGetter = std::function<IdString(Context *, ElementT)>;
private:
Context *ctx_;
2018-08-01 09:59:07 +08:00
// ElementMap given to use by our constructor.
2018-07-31 22:49:47 +08:00
const ElementMap *map_;
2018-08-01 09:59:07 +08:00
// The X, Y that this list handles.
2018-07-31 22:49:47 +08:00
int x_, y_;
ElementGetter getter_;
2018-08-01 09:59:07 +08:00
// Children that we manage the memory for, stored for quick lookup from
// IdString to child.
std::unordered_map<IdString, std::unique_ptr<Item>> managed_;
2018-08-01 09:59:07 +08:00
// Type of children that he list creates.
2018-08-01 07:46:22 +08:00
ElementType child_type_;
2018-07-31 22:49:47 +08:00
2018-08-01 09:59:07 +08:00
// Gets elements that this list should create from the map. This pointer is
// short-lived (as it will change when the map mutates.
2018-07-31 22:49:47 +08:00
const std::vector<ElementT> *elements() const
{
2018-08-01 10:17:02 +08:00
return &map_->at(std::make_pair(x_, y_));
2018-07-31 22:49:47 +08:00
}
public:
ElementList(Context *ctx, QString name, Item *parent, ElementMap *map, int x, int y, ElementGetter getter, ElementType type) :
2018-08-01 09:59:07 +08:00
Item(name, parent), ctx_(ctx), map_(map), x_(x), y_(y), getter_(getter), child_type_(type)
2018-07-31 22:49:47 +08:00
{
}
2018-08-01 09:59:07 +08:00
// Lazy loading of elements.
2018-07-31 22:49:47 +08:00
virtual bool canFetchMore() const override
{
2018-08-01 09:14:20 +08:00
return (size_t)children_.size() < elements()->size();
2018-07-31 22:49:47 +08:00
}
void fetchMore(int count)
{
2018-08-01 09:14:20 +08:00
size_t start = children_.size();
size_t end = std::min(start + count, elements()->size());
for (size_t i = start; i < end; i++) {
2018-08-01 08:27:20 +08:00
auto idstring = getter_(ctx_, elements()->at(i));
QString name(idstring.c_str(ctx_));
2018-07-31 22:49:47 +08:00
// Remove X.../Y.../ prefix
QString prefix = QString("X%1/Y%2/").arg(x_).arg(y_);
if (name.startsWith(prefix))
name.remove(0, prefix.size());
2018-08-01 08:27:20 +08:00
auto item = new IdStringItem(ctx_, idstring, this, child_type_);
managed_[idstring] = std::move(std::unique_ptr<Item>(item));
2018-07-31 22:49:47 +08:00
}
}
virtual void fetchMore() override
{
fetchMore(100);
}
2018-08-01 09:59:07 +08:00
// getById finds a child for the given IdString.
boost::optional<Item*> 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;
}
2018-08-01 10:11:22 +08:00
// Find children that contain the given text.
void search(QList<Item*> &results, QString text, int limit)
{
// Last chance to bail out from loading entire tree into memory.
if (limit != -1 && results.size() > limit)
return;
// Search requires us to load all our elements...
while (canFetchMore()) fetchMore();
for (const auto &child : children_) {
if (limit != -1 && results.size() > limit)
return;
if (child->name().contains(text))
results.push_back(child);
}
}
2018-07-31 22:49:47 +08:00
};
2018-08-01 09:59:07 +08:00
// ElementXYRoot is the root of an ElementT multi-level lazy loading list.
// It can take any of {BelId,WireId,PipId} and create a tree that
// hierarchizes them by X and Y tile positions, when given a map from X,Y to
// list of ElementTs in that tile.
2018-07-31 22:49:47 +08:00
template <typename ElementT>
class ElementXYRoot : public Item
2018-07-31 22:49:47 +08:00
{
public:
2018-08-01 09:59:07 +08:00
// A map from tile (X,Y) to list of ElementTs in that tile.
2018-07-31 22:49:47 +08:00
using ElementMap = std::map<std::pair<int, int>, std::vector<ElementT>>;
2018-08-01 09:59:07 +08:00
// A method that converts an ElementT to an IdString.
2018-07-31 22:49:47 +08:00
using ElementGetter = std::function<IdString(Context *, ElementT)>;
private:
Context *ctx_;
2018-08-01 09:59:07 +08:00
// X-index children that we manage the memory for.
std::vector<std::unique_ptr<Item>> managed_labels_;
2018-08-01 09:59:07 +08:00
// Y-index children (ElementLists) that we manage the memory for.
std::vector<std::unique_ptr<ElementList<ElementT>>> managed_lists_;
2018-08-01 09:59:07 +08:00
// Source of truth for elements to display.
2018-07-31 22:49:47 +08:00
ElementMap map_;
ElementGetter getter_;
2018-08-01 09:59:07 +08:00
// Type of children that he list creates in X->Y->...
2018-08-01 07:46:22 +08:00
ElementType child_type_;
2018-07-31 22:49:47 +08:00
public:
ElementXYRoot(Context *ctx, QString name, Item *parent, ElementMap map, ElementGetter getter, ElementType type) :
2018-08-01 09:59:07 +08:00
Item(name, parent), ctx_(ctx), map_(map), getter_(getter), child_type_(type)
2018-07-31 22:49:47 +08:00
{
2018-08-01 09:59:07 +08:00
// Create all X and Y label Items/ElementLists.
// Y coordinates at which an element exists for a given X - taken out
// of loop to limit heap allocation/deallocation.
2018-07-31 22:49:47 +08:00
std::vector<int> y_present;
for (int i = 0; i < ctx->getGridDimX(); i++) {
y_present.clear();
2018-08-01 09:59:07 +08:00
// First find all the elements in all Y coordinates in this X.
2018-07-31 22:49:47 +08:00
for (int j = 0; j < ctx->getGridDimY(); j++) {
2018-08-01 10:17:02 +08:00
if (map_.count(std::make_pair(i, j)) == 0)
2018-07-31 22:49:47 +08:00
continue;
y_present.push_back(j);
}
2018-08-01 09:59:07 +08:00
// No elements in any X coordinate? Do not add X tree item.
2018-07-31 22:49:47 +08:00
if (y_present.size() == 0)
continue;
2018-08-01 09:59:07 +08:00
// Create X list Item.
auto item = new Item(QString("X%1").arg(i), this);
managed_labels_.push_back(std::move(std::unique_ptr<Item>(item)));
2018-08-01 09:59:07 +08:00
2018-07-31 22:49:47 +08:00
for (auto j : y_present) {
2018-08-01 09:59:07 +08:00
// Create Y list ElementList.
2018-08-01 07:46:22 +08:00
auto item2 = new ElementList<ElementT>(ctx_, QString("Y%1").arg(j), item, &map_, i, j, getter_, child_type_);
2018-08-01 09:59:07 +08:00
// Pre-populate list with one element, other Qt will never ask for more.
2018-07-31 22:49:47 +08:00
item2->fetchMore(1);
managed_lists_.push_back(std::move(std::unique_ptr<ElementList<ElementT>>(item2)));
}
}
}
2018-08-01 09:59:07 +08:00
// getById finds a child for the given IdString.
boost::optional<Item*> 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;
2018-07-31 22:49:47 +08:00
}
}
return boost::none;
2018-07-31 22:49:47 +08:00
}
2018-08-01 10:11:22 +08:00
// Find children that contain the given text.
void search(QList<Item*> &results, QString text, int limit)
{
for (auto &l : managed_lists_) {
if (limit != -1 && results.size() > limit)
return;
l->search(results, text, limit);
}
}
2018-07-31 22:49:47 +08:00
};
class Model : public QAbstractItemModel
2018-07-28 21:44:00 +08:00
{
private:
Context *ctx_ = nullptr;
2018-07-28 21:44:00 +08:00
public:
2018-07-31 22:49:47 +08:00
using BelXYRoot = ElementXYRoot<BelId>;
using WireXYRoot = ElementXYRoot<WireId>;
using PipXYRoot = ElementXYRoot<PipId>;
Model(QObject *parent = nullptr);
~Model();
2018-07-28 21:44:00 +08:00
void loadContext(Context *ctx);
void updateCellsNets(Context *ctx);
Item *nodeFromIndex(const QModelIndex &idx) const;
QModelIndex indexFromNode(Item *node)
{
const Item *parent = node->parent();
if (parent == nullptr)
return QModelIndex();
return createIndex(parent->indexOf(node), 0, node);
}
2018-07-31 02:10:36 +08:00
QList<QModelIndex> search(QString text);
2018-08-01 10:11:22 +08:00
boost::optional<Item*> 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;
}
}
2018-07-31 22:49:47 +08:00
2018-07-28 21:44:00 +08:00
// Override QAbstractItemModel methods
int rowCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
int columnCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
QModelIndex parent(const QModelIndex &child) const Q_DECL_OVERRIDE;
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;
2018-07-31 22:49:47 +08:00
void fetchMore(const QModelIndex &parent) Q_DECL_OVERRIDE;
bool canFetchMore(const QModelIndex &parent) const Q_DECL_OVERRIDE;
2018-07-28 21:44:00 +08:00
private:
2018-08-01 09:59:07 +08:00
// Tree elements that we manage the memory for.
std::unique_ptr<Item> root_;
2018-07-31 22:49:47 +08:00
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_;
2018-07-28 21:44:00 +08:00
};
}; // namespace TreeModel
2018-07-28 21:44:00 +08:00
NEXTPNR_NAMESPACE_END
#endif // TREEMODEL_H