nextpnr/gui/treemodel.cc

253 lines
7.0 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 <micko@yosyshq.com>
* Copyright (C) 2018 Serge Bazanski <q3k@q3k.org>
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> IdList::alphaNumSplit(const QString &str)
2018-08-01 09:59:07 +08:00
{
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);
}
2018-08-01 10:59:27 +08:00
2018-08-01 09:59:07 +08:00
res.push_back(current_part);
return res;
}
void IdList::updateElements(Context *ctx, std::vector<IdStringList> elements)
2018-08-01 09:59:07 +08:00
{
bool changed = false;
2018-08-01 09:59:07 +08:00
// For any elements that are not yet in managed_, created them.
pool<IdStringList> element_set;
2018-08-01 09:59:07 +08:00
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));
changed = true;
2018-08-01 09:59:07 +08:00
}
}
2018-08-01 10:59:27 +08:00
2018-08-01 09:59:07 +08:00
// For any elements that are in managed_ but not in new, delete them.
auto it = managed_.begin();
while (it != managed_.end()) {
if (element_set.count(it->first) != 0) {
++it;
} else {
it = managed_.erase(it);
changed = true;
2018-08-01 09:59:07 +08:00
}
}
// Return early if there are no changes.
if (!changed)
return;
// Rebuild children list.
children_.clear();
for (auto &pair : managed_) {
if (element_set.count(pair.first) != 0) {
children_.push_back(pair.second.get());
}
2018-08-01 09:59:07 +08:00
}
// Sort new children
2020-01-18 22:23:35 +08:00
std::sort(children_.begin(), children_.end(), [&](const Item *a, const Item *b) {
2018-08-01 09:59:07 +08:00
auto parts_a = alphaNumSplit(a->name());
auto parts_b = alphaNumSplit(b->name());
2020-02-28 10:56:33 +08:00
for (size_t i = 0; i < parts_a.size() && i < parts_b.size(); i++) {
2018-08-01 09:59:07 +08:00
auto &part_a = parts_a.at(i);
auto &part_b = parts_b.at(i);
2018-08-01 10:59:27 +08:00
2018-08-01 09:59:07 +08:00
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;
}
2020-02-28 10:56:33 +08:00
// One string is equal to or a subset of the other, compare length.
return parts_a.size() < parts_b.size();
2018-08-01 09:59:07 +08:00
});
}
void IdList::search(QList<Item *> &results, QString text, int limit)
2018-08-01 10:11:22 +08:00
{
for (const auto &child : children_) {
if (limit != -1 && results.size() > limit)
return;
if (child->name().contains(text))
results.push_back(child);
}
}
2018-08-01 10:59:27 +08:00
Model::Model(QObject *parent) : QAbstractItemModel(parent), root_(new Item("Elements", nullptr)) {}
2018-07-28 21:44:00 +08:00
Model::~Model() {}
2018-07-28 21:44:00 +08:00
2018-11-11 17:53:48 +08:00
void Model::loadData(Context *ctx, std::unique_ptr<Item> data)
2018-07-28 21:44:00 +08:00
{
2018-07-29 00:48:32 +08:00
beginResetModel();
2018-11-11 17:53:48 +08:00
ctx_ = ctx;
2018-10-20 22:52:38 +08:00
root_ = std::move(data);
2018-07-29 00:48:32 +08:00
endResetModel();
2018-07-28 21:44:00 +08:00
}
void Model::updateElements(std::vector<IdStringList> elements)
2018-07-28 21:44:00 +08:00
{
2018-11-11 17:53:48 +08:00
if (!ctx_)
2018-07-28 21:44:00 +08:00
return;
2018-07-29 16:56:36 +08:00
beginResetModel();
2018-11-11 17:53:48 +08:00
root_->updateElements(ctx_, elements);
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
}
void Model::fetchMore(const QModelIndex &parent)
2018-07-28 21:44:00 +08:00
{
if (ctx_ == nullptr)
return;
std::lock_guard<std::mutex> lock_ui(ctx_->ui_mutex);
std::lock_guard<std::mutex> lock(ctx_->mutex);
2018-07-31 22:49:47 +08:00
nodeFromIndex(parent)->fetchMore();
2018-07-28 21:44:00 +08:00
}
2018-08-01 10:59:27 +08:00
bool Model::canFetchMore(const QModelIndex &parent) const { return nodeFromIndex(parent)->canFetchMore(); }
2018-07-31 22:49:47 +08:00
QList<QModelIndex> Model::search(QString text)
2018-07-31 02:10:36 +08:00
{
2018-08-01 10:11:22 +08:00
const int limit = 500;
2018-08-01 10:59:27 +08:00
QList<Item *> list;
root_->search(list, text, limit);
2018-08-01 10:11:22 +08:00
QList<QModelIndex> res;
for (auto i : list) {
res.push_back(indexFromNode(i));
}
return res;
2018-07-31 02:10:36 +08:00
}
}; // namespace TreeModel
NEXTPNR_NAMESPACE_END