nextpnr/gui/treemodel.cc

291 lines
8.9 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.
*
*/
#include "treemodel.h"
#include "log.h"
2018-07-28 21:44:00 +08:00
NEXTPNR_NAMESPACE_BEGIN
namespace TreeModel {
2018-08-01 09:59:07 +08:00
// 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;
}
if (number != c.isNumber()) {
number = c.isNumber();
res.push_back(current_part);
current_part.clear();
}
current_part.push_back(c);
}
res.push_back(current_part);
return res;
}
void IdStringList::updateElements(Context *ctx, std::vector<IdString> elements)
{
// 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;
});
}
Model::Model(QObject *parent) :
2018-07-31 22:49:47 +08:00
QAbstractItemModel(parent),
2018-08-01 09:59:07 +08:00
root_(new Item("Elements", nullptr)) {}
2018-07-28 21:44:00 +08:00
Model::~Model() {}
2018-07-28 21:44:00 +08:00
void Model::loadContext(Context *ctx)
2018-07-28 21:44:00 +08:00
{
if (!ctx)
return;
2018-07-29 00:48:32 +08:00
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.
#ifdef ARCH_ICE40
2018-07-31 22:49:47 +08:00
{
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);
2018-07-28 21:44:00 +08:00
}
2018-07-31 22:49:47 +08:00
auto belGetter = [](Context *ctx, BelId id) { return ctx->getBelName(id); };
2018-08-01 07:46:22 +08:00
bel_root_ = std::unique_ptr<BelXYRoot>(new BelXYRoot(ctx, "Bels", root_.get(), belMap, belGetter, ElementType::BEL));
2018-07-31 22:49:47 +08:00
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);
2018-07-28 21:44:00 +08:00
}
2018-07-31 22:49:47 +08:00
auto wireGetter = [](Context *ctx, WireId id) { return ctx->getWireName(id); };
2018-08-01 07:46:22 +08:00
wire_root_ = std::unique_ptr<WireXYRoot>(new WireXYRoot(ctx, "Wires", root_.get(), wireMap, wireGetter, ElementType::WIRE));
2018-07-31 22:49:47 +08:00
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);
2018-07-28 21:44:00 +08:00
}
2018-07-31 22:49:47 +08:00
printf("generating pip static tree...\n");
auto pipGetter = [](Context *ctx, PipId id) { return ctx->getPipName(id); };
2018-08-01 07:46:22 +08:00
pip_root_ = std::unique_ptr<PipXYRoot>(new PipXYRoot(ctx, "Pips", root_.get(), pipMap, pipGetter, ElementType::PIP));
2018-07-28 21:44:00 +08:00
}
#endif
2018-07-28 21:44:00 +08:00
2018-08-01 07:46:22 +08:00
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));
2018-07-29 00:48:32 +08:00
endResetModel();
updateCellsNets(ctx);
2018-07-28 21:44:00 +08:00
}
void Model::updateCellsNets(Context *ctx)
2018-07-28 21:44:00 +08:00
{
if (!ctx)
return;
2018-07-29 16:56:36 +08:00
beginResetModel();
std::vector<IdString> cells;
for (auto &pair : ctx->cells) {
cells.push_back(pair.first);
2018-07-28 21:44:00 +08:00
}
cell_root_->updateElements(ctx, cells);
2018-07-31 22:49:47 +08:00
std::vector<IdString> nets;
for (auto &pair : ctx->nets) {
nets.push_back(pair.first);
2018-07-28 21:44:00 +08:00
}
net_root_->updateElements(ctx, nets);
2018-07-29 16:56:36 +08:00
endResetModel();
2018-07-28 21:44:00 +08:00
}
int Model::rowCount(const QModelIndex &parent) const { return nodeFromIndex(parent)->count(); }
2018-07-28 21:44:00 +08:00
int Model::columnCount(const QModelIndex &parent) const { return 1; }
2018-07-28 21:44:00 +08:00
QModelIndex Model::index(int row, int column, const QModelIndex &parent) const
2018-07-28 21:44:00 +08:00
{
Item *node = nodeFromIndex(parent);
2018-07-28 21:44:00 +08:00
if (row >= node->count())
return QModelIndex();
2018-07-31 22:49:47 +08:00
return createIndex(row, column, node->child(row));
2018-07-28 21:44:00 +08:00
}
QModelIndex Model::parent(const QModelIndex &child) const
2018-07-28 21:44:00 +08:00
{
Item *parent = nodeFromIndex(child)->parent();
2018-07-31 22:49:47 +08:00
if (parent == root_.get())
2018-07-28 21:44:00 +08:00
return QModelIndex();
Item *node = parent->parent();
2018-07-28 21:44:00 +08:00
return createIndex(node->indexOf(parent), 0, parent);
}
QVariant Model::data(const QModelIndex &index, int role) const
2018-07-28 21:44:00 +08:00
{
if (index.column() != 0)
return QVariant();
if (role != Qt::DisplayRole)
return QVariant();
Item *node = nodeFromIndex(index);
2018-07-28 21:44:00 +08:00
return node->name();
}
QVariant Model::headerData(int section, Qt::Orientation orientation, int role) const
2018-07-28 21:44:00 +08:00
{
Q_UNUSED(section);
if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
return QString("Items");
return QVariant();
}
Item *Model::nodeFromIndex(const QModelIndex &idx) const
2018-07-28 21:44:00 +08:00
{
if (idx.isValid())
return (Item *)idx.internalPointer();
2018-07-31 22:49:47 +08:00
return root_.get();
2018-07-28 21:44:00 +08:00
}
Qt::ItemFlags Model::flags(const QModelIndex &index) const
2018-07-28 21:44:00 +08:00
{
Item *node = nodeFromIndex(index);
2018-07-31 22:49:47 +08:00
return Qt::ItemIsEnabled | (node->type() != ElementType::NONE ? Qt::ItemIsSelectable : Qt::NoItemFlags);
2018-07-28 21:44:00 +08:00
}
2018-07-31 22:49:47 +08:00
void Model::fetchMore(const QModelIndex &parent)
2018-07-28 21:44:00 +08:00
{
2018-07-31 22:49:47 +08:00
nodeFromIndex(parent)->fetchMore();
2018-07-28 21:44:00 +08:00
}
bool Model::canFetchMore(const QModelIndex &parent) const
2018-07-28 21:44:00 +08:00
{
2018-07-31 22:49:47 +08:00
return nodeFromIndex(parent)->canFetchMore();
2018-07-28 21:44:00 +08:00
}
2018-07-31 22:49:47 +08:00
QList<QModelIndex> Model::search(QString text)
2018-07-31 02:10:36 +08:00
{
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
// }
// }
//}
2018-07-31 02:10:36 +08:00
return list;
}
}; // namespace TreeModel
NEXTPNR_NAMESPACE_END