gui: move tree model into own namespace

This commit is contained in:
Sergiusz Bazanski 2018-08-01 02:19:30 +01:00
parent 049e487f3f
commit 6baf8216ed
4 changed files with 74 additions and 66 deletions

View File

@ -34,7 +34,7 @@ DesignWidget::DesignWidget(QWidget *parent) : QWidget(parent), ctx(nullptr), sel
{ {
// Add tree view // Add tree view
treeView = new QTreeView(); treeView = new QTreeView();
treeModel = new ContextTreeModel(); treeModel = new TreeModel::Model();
treeView->setModel(treeModel); treeView->setModel(treeModel);
treeView->setContextMenuPolicy(Qt::CustomContextMenu); treeView->setContextMenuPolicy(Qt::CustomContextMenu);
treeView->setSelectionMode(QAbstractItemView::ExtendedSelection); treeView->setSelectionMode(QAbstractItemView::ExtendedSelection);
@ -223,9 +223,9 @@ void DesignWidget::updateTree()
{ {
clearProperties(); clearProperties();
QMap<LazyTreeItem *, int>::iterator i = highlightSelected.begin(); QMap<TreeModel::Item *, int>::iterator i = highlightSelected.begin();
while (i != highlightSelected.end()) { while (i != highlightSelected.end()) {
QMap<LazyTreeItem *, int>::iterator prev = i; QMap<TreeModel::Item *, int>::iterator prev = i;
++i; ++i;
if (prev.key()->type() == ElementType::NET && ctx->nets.find(prev.key()->id()) == ctx->nets.end()) { if (prev.key()->type() == ElementType::NET && ctx->nets.find(prev.key()->id()) == ctx->nets.end()) {
highlightSelected.erase(prev); highlightSelected.erase(prev);
@ -348,7 +348,7 @@ void DesignWidget::onSelectionChanged(const QItemSelection &, const QItemSelecti
if (selectionModel->selectedIndexes().size() > 1) { if (selectionModel->selectedIndexes().size() > 1) {
std::vector<DecalXY> decals; std::vector<DecalXY> decals;
for (auto index : selectionModel->selectedIndexes()) { for (auto index : selectionModel->selectedIndexes()) {
LazyTreeItem *item = treeModel->nodeFromIndex(index); TreeModel::Item *item = treeModel->nodeFromIndex(index);
std::vector<DecalXY> d = getDecals(item->type(), item->id()); std::vector<DecalXY> d = getDecals(item->type(), item->id());
std::move(d.begin(), d.end(), std::back_inserter(decals)); std::move(d.begin(), d.end(), std::back_inserter(decals));
} }
@ -358,7 +358,7 @@ void DesignWidget::onSelectionChanged(const QItemSelection &, const QItemSelecti
QModelIndex index = selectionModel->selectedIndexes().at(0); QModelIndex index = selectionModel->selectedIndexes().at(0);
if (!index.isValid()) if (!index.isValid())
return; return;
LazyTreeItem *clickItem = treeModel->nodeFromIndex(index); TreeModel::Item *clickItem = treeModel->nodeFromIndex(index);
ElementType type = clickItem->type(); ElementType type = clickItem->type();
if (type == ElementType::NONE) if (type == ElementType::NONE)
@ -606,7 +606,7 @@ std::vector<DecalXY> DesignWidget::getDecals(ElementType type, IdString value)
return decals; return decals;
} }
void DesignWidget::updateHighlightGroup(QList<LazyTreeItem *> items, int group) void DesignWidget::updateHighlightGroup(QList<TreeModel::Item *> items, int group)
{ {
const bool shouldClear = items.size() == 1; const bool shouldClear = items.size() == 1;
for (auto item : items) { for (auto item : items) {
@ -631,7 +631,7 @@ void DesignWidget::updateHighlightGroup(QList<LazyTreeItem *> items, int group)
void DesignWidget::prepareMenuProperty(const QPoint &pos) void DesignWidget::prepareMenuProperty(const QPoint &pos)
{ {
QTreeWidget *tree = propertyEditor->treeWidget(); QTreeWidget *tree = propertyEditor->treeWidget();
QList<LazyTreeItem *> items; QList<TreeModel::Item *> items;
for (auto itemContextMenu : tree->selectedItems()) { for (auto itemContextMenu : tree->selectedItems()) {
QtBrowserItem *browserItem = propertyEditor->itemToBrowserItem(itemContextMenu); QtBrowserItem *browserItem = propertyEditor->itemToBrowserItem(itemContextMenu);
if (!browserItem) if (!browserItem)
@ -648,7 +648,7 @@ void DesignWidget::prepareMenuProperty(const QPoint &pos)
} }
int selectedIndex = -1; int selectedIndex = -1;
if (items.size() == 1) { if (items.size() == 1) {
LazyTreeItem *item = items.at(0); TreeModel::Item *item = items.at(0);
if (highlightSelected.contains(item)) if (highlightSelected.contains(item))
selectedIndex = highlightSelected[item]; selectedIndex = highlightSelected[item];
} }
@ -689,13 +689,13 @@ void DesignWidget::prepareMenuTree(const QPoint &pos)
if (selectionModel->selectedIndexes().size() == 0) if (selectionModel->selectedIndexes().size() == 0)
return; return;
QList<LazyTreeItem *> items; QList<TreeModel::Item *> items;
for (auto index : selectionModel->selectedIndexes()) { for (auto index : selectionModel->selectedIndexes()) {
LazyTreeItem *item = treeModel->nodeFromIndex(index); TreeModel::Item *item = treeModel->nodeFromIndex(index);
items.append(item); items.append(item);
} }
if (items.size() == 1) { if (items.size() == 1) {
LazyTreeItem *item = items.at(0); TreeModel::Item *item = items.at(0);
if (highlightSelected.contains(item)) if (highlightSelected.contains(item))
selectedIndex = highlightSelected[item]; selectedIndex = highlightSelected[item];
} }

View File

@ -51,7 +51,7 @@ class DesignWidget : public QWidget
void updateButtons(); void updateButtons();
void addToHistory(QModelIndex item); void addToHistory(QModelIndex item);
std::vector<DecalXY> getDecals(ElementType type, IdString value); std::vector<DecalXY> getDecals(ElementType type, IdString value);
void updateHighlightGroup(QList<LazyTreeItem *> item, int group); void updateHighlightGroup(QList<TreeModel::Item *> item, int group);
Q_SIGNALS: Q_SIGNALS:
void info(std::string text); void info(std::string text);
void selected(std::vector<DecalXY> decal, bool keep); void selected(std::vector<DecalXY> decal, bool keep);
@ -77,7 +77,7 @@ class DesignWidget : public QWidget
QTreeView *treeView; QTreeView *treeView;
QItemSelectionModel *selectionModel; QItemSelectionModel *selectionModel;
ContextTreeModel *treeModel; TreeModel::Model *treeModel;
QLineEdit *searchEdit; QLineEdit *searchEdit;
QtVariantPropertyManager *variantManager; QtVariantPropertyManager *variantManager;
QtVariantPropertyManager *readOnlyManager; QtVariantPropertyManager *readOnlyManager;
@ -99,7 +99,7 @@ class DesignWidget : public QWidget
QAction *actionClear; QAction *actionClear;
QColor highlightColors[8]; QColor highlightColors[8];
QMap<LazyTreeItem *, int> highlightSelected; QMap<TreeModel::Item *, int> highlightSelected;
QString currentSearch; QString currentSearch;
QList<QModelIndex> currentSearchIndexes; QList<QModelIndex> currentSearchIndexes;

View File

@ -22,13 +22,15 @@
NEXTPNR_NAMESPACE_BEGIN NEXTPNR_NAMESPACE_BEGIN
ContextTreeModel::ContextTreeModel(QObject *parent) : namespace TreeModel {
Model::Model(QObject *parent) :
QAbstractItemModel(parent), QAbstractItemModel(parent),
root_(new StaticTreeItem("Elements", nullptr, ElementType::NONE)) {} root_(new StaticTreeItem("Elements", nullptr, ElementType::NONE)) {}
ContextTreeModel::~ContextTreeModel() {} Model::~Model() {}
void ContextTreeModel::loadContext(Context *ctx) void Model::loadContext(Context *ctx)
{ {
if (!ctx) if (!ctx)
return; return;
@ -80,7 +82,7 @@ void ContextTreeModel::loadContext(Context *ctx)
updateCellsNets(ctx); updateCellsNets(ctx);
} }
void ContextTreeModel::updateCellsNets(Context *ctx) void Model::updateCellsNets(Context *ctx)
{ {
if (!ctx) if (!ctx)
return; return;
@ -102,39 +104,39 @@ void ContextTreeModel::updateCellsNets(Context *ctx)
endResetModel(); endResetModel();
} }
int ContextTreeModel::rowCount(const QModelIndex &parent) const { return nodeFromIndex(parent)->count(); } int Model::rowCount(const QModelIndex &parent) const { return nodeFromIndex(parent)->count(); }
int ContextTreeModel::columnCount(const QModelIndex &parent) const { return 1; } int Model::columnCount(const QModelIndex &parent) const { return 1; }
QModelIndex ContextTreeModel::index(int row, int column, const QModelIndex &parent) const QModelIndex Model::index(int row, int column, const QModelIndex &parent) const
{ {
LazyTreeItem *node = nodeFromIndex(parent); Item *node = nodeFromIndex(parent);
if (row >= node->count()) if (row >= node->count())
return QModelIndex(); return QModelIndex();
return createIndex(row, column, node->child(row)); return createIndex(row, column, node->child(row));
} }
QModelIndex ContextTreeModel::parent(const QModelIndex &child) const QModelIndex Model::parent(const QModelIndex &child) const
{ {
LazyTreeItem *parent = nodeFromIndex(child)->parent(); Item *parent = nodeFromIndex(child)->parent();
if (parent == root_.get()) if (parent == root_.get())
return QModelIndex(); return QModelIndex();
LazyTreeItem *node = parent->parent(); Item *node = parent->parent();
return createIndex(node->indexOf(parent), 0, parent); return createIndex(node->indexOf(parent), 0, parent);
} }
QVariant ContextTreeModel::data(const QModelIndex &index, int role) const QVariant Model::data(const QModelIndex &index, int role) const
{ {
if (index.column() != 0) if (index.column() != 0)
return QVariant(); return QVariant();
if (role != Qt::DisplayRole) if (role != Qt::DisplayRole)
return QVariant(); return QVariant();
LazyTreeItem *node = nodeFromIndex(index); Item *node = nodeFromIndex(index);
return node->name(); return node->name();
} }
QVariant ContextTreeModel::headerData(int section, Qt::Orientation orientation, int role) const QVariant Model::headerData(int section, Qt::Orientation orientation, int role) const
{ {
Q_UNUSED(section); Q_UNUSED(section);
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
@ -143,31 +145,31 @@ QVariant ContextTreeModel::headerData(int section, Qt::Orientation orientation,
return QVariant(); return QVariant();
} }
LazyTreeItem *ContextTreeModel::nodeFromIndex(const QModelIndex &idx) const Item *Model::nodeFromIndex(const QModelIndex &idx) const
{ {
if (idx.isValid()) if (idx.isValid())
return (LazyTreeItem *)idx.internalPointer(); return (Item *)idx.internalPointer();
return root_.get(); return root_.get();
} }
Qt::ItemFlags ContextTreeModel::flags(const QModelIndex &index) const Qt::ItemFlags Model::flags(const QModelIndex &index) const
{ {
LazyTreeItem *node = nodeFromIndex(index); Item *node = nodeFromIndex(index);
return Qt::ItemIsEnabled | (node->type() != ElementType::NONE ? Qt::ItemIsSelectable : Qt::NoItemFlags); return Qt::ItemIsEnabled | (node->type() != ElementType::NONE ? Qt::ItemIsSelectable : Qt::NoItemFlags);
} }
void ContextTreeModel::fetchMore(const QModelIndex &parent) void Model::fetchMore(const QModelIndex &parent)
{ {
nodeFromIndex(parent)->fetchMore(); nodeFromIndex(parent)->fetchMore();
} }
bool ContextTreeModel::canFetchMore(const QModelIndex &parent) const bool Model::canFetchMore(const QModelIndex &parent) const
{ {
return nodeFromIndex(parent)->canFetchMore(); return nodeFromIndex(parent)->canFetchMore();
} }
QList<QModelIndex> ContextTreeModel::search(QString text) QList<QModelIndex> Model::search(QString text)
{ {
QList<QModelIndex> list; QList<QModelIndex> list;
//for (int i = 0; i < 6; i++) { //for (int i = 0; i < 6; i++) {
@ -182,4 +184,6 @@ QList<QModelIndex> ContextTreeModel::search(QString text)
return list; return list;
} }
}; // namespace TreeModel
NEXTPNR_NAMESPACE_END NEXTPNR_NAMESPACE_END

View File

@ -38,21 +38,23 @@ enum class ElementType
GROUP GROUP
}; };
class LazyTreeItem namespace TreeModel {
class Item
{ {
protected: protected:
QString name_; QString name_;
LazyTreeItem *parent_; Item *parent_;
QList<LazyTreeItem *> children_; QList<Item *> children_;
ElementType type_; ElementType type_;
void addChild(LazyTreeItem *child) void addChild(Item *child)
{ {
children_.append(child); children_.append(child);
} }
public: public:
LazyTreeItem(QString name, LazyTreeItem *parent, ElementType type) : Item(QString name, Item *parent, ElementType type) :
name_(name), parent_(parent), type_(type) name_(name), parent_(parent), type_(type)
{ {
// Register in parent if exists. // Register in parent if exists.
@ -71,28 +73,28 @@ class LazyTreeItem
return name_; return name_;
} }
LazyTreeItem *child(int index) Item *child(int index)
{ {
return children_.at(index); return children_.at(index);
} }
int indexOf(const LazyTreeItem *child) const int indexOf(const Item *child) const
{ {
// Dropping the const for indexOf to work. // Dropping the const for indexOf to work.
return children_.indexOf((LazyTreeItem *)child, 0); return children_.indexOf((Item *)child, 0);
} }
int indexOf(LazyTreeItem *child) int indexOf(Item *child)
{ {
return children_.indexOf(child, 0); return children_.indexOf(child, 0);
} }
const LazyTreeItem *parent() const const Item *parent() const
{ {
return parent_; return parent_;
} }
LazyTreeItem *parent() Item *parent()
{ {
return parent_; return parent_;
} }
@ -106,13 +108,13 @@ class LazyTreeItem
virtual void fetchMore() = 0; virtual void fetchMore() = 0;
virtual IdString id() const = 0; virtual IdString id() const = 0;
virtual ~LazyTreeItem() {} virtual ~Item() {}
}; };
class StaticTreeItem : public LazyTreeItem class StaticTreeItem : public Item
{ {
public: public:
using LazyTreeItem::LazyTreeItem; using Item::Item;
virtual bool canFetchMore() const override virtual bool canFetchMore() const override
{ {
@ -137,7 +139,7 @@ class IdStringItem : public StaticTreeItem
IdString id_; IdString id_;
public: public:
IdStringItem(Context *ctx, IdString str, LazyTreeItem *parent, ElementType type) : IdStringItem(Context *ctx, IdString str, Item *parent, ElementType type) :
StaticTreeItem(QString(str.c_str(ctx)), parent, type), id_(str) {} StaticTreeItem(QString(str.c_str(ctx)), parent, type), id_(str) {}
virtual IdString id() const override virtual IdString id() const override
@ -147,7 +149,7 @@ class IdStringItem : public StaticTreeItem
}; };
template <typename ElementT> template <typename ElementT>
class ElementList : public LazyTreeItem class ElementList : public Item
{ {
public: public:
using ElementMap = std::map<std::pair<int, int>, std::vector<ElementT>>; using ElementMap = std::map<std::pair<int, int>, std::vector<ElementT>>;
@ -168,8 +170,8 @@ class ElementList : public LazyTreeItem
} }
public: public:
ElementList(Context *ctx, QString name, LazyTreeItem *parent, ElementMap *map, int x, int y, ElementGetter getter, ElementType type) : ElementList(Context *ctx, QString name, Item *parent, ElementMap *map, int x, int y, ElementGetter getter, ElementType type) :
LazyTreeItem(name, parent, ElementType::NONE), ctx_(ctx), map_(map), x_(x), y_(y), getter_(getter), child_type_(type) Item(name, parent, ElementType::NONE), ctx_(ctx), map_(map), x_(x), y_(y), getter_(getter), child_type_(type)
{ {
} }
@ -206,7 +208,7 @@ class ElementList : public LazyTreeItem
return IdString(); return IdString();
} }
boost::optional<LazyTreeItem*> getById(IdString id) boost::optional<Item*> getById(IdString id)
{ {
// Search requires us to load all our elements... // Search requires us to load all our elements...
while (canFetchMore()) fetchMore(); while (canFetchMore()) fetchMore();
@ -225,7 +227,7 @@ class IdStringList : public StaticTreeItem
std::unordered_map<IdString, std::unique_ptr<IdStringItem>> managed_; std::unordered_map<IdString, std::unique_ptr<IdStringItem>> managed_;
ElementType child_type_; ElementType child_type_;
public: public:
IdStringList(QString name, LazyTreeItem *parent, ElementType type) : IdStringList(QString name, Item *parent, ElementType type) :
StaticTreeItem(name, parent, ElementType::NONE), child_type_(type) {} StaticTreeItem(name, parent, ElementType::NONE), child_type_(type) {}
using StaticTreeItem::StaticTreeItem; using StaticTreeItem::StaticTreeItem;
@ -285,7 +287,7 @@ class IdStringList : public StaticTreeItem
} }
// sort new children // sort new children
qSort(children_.begin(), children_.end(), [&](const LazyTreeItem *a, const LazyTreeItem *b){ qSort(children_.begin(), children_.end(), [&](const Item *a, const Item *b){
auto parts_a = alphaNumSplit(a->name()); auto parts_a = alphaNumSplit(a->name());
auto parts_b = alphaNumSplit(b->name()); auto parts_b = alphaNumSplit(b->name());
@ -346,7 +348,7 @@ class ElementXYRoot : public StaticTreeItem
ElementType child_type_; ElementType child_type_;
public: public:
ElementXYRoot(Context *ctx, QString name, LazyTreeItem *parent, ElementMap map, ElementGetter getter, ElementType type) : ElementXYRoot(Context *ctx, QString name, Item *parent, ElementMap map, ElementGetter getter, ElementType type) :
StaticTreeItem(name, parent, ElementType::NONE), ctx_(ctx), map_(map), getter_(getter), child_type_(type) StaticTreeItem(name, parent, ElementType::NONE), ctx_(ctx), map_(map), getter_(getter), child_type_(type)
{ {
std::vector<int> y_present; std::vector<int> y_present;
@ -374,7 +376,7 @@ class ElementXYRoot : public StaticTreeItem
} }
} }
boost::optional<LazyTreeItem*> getById(IdString id) boost::optional<Item*> getById(IdString id)
{ {
// For now, scan linearly all ElementLists. // For now, scan linearly all ElementLists.
// TODO(q3k) fix this once we have tree API from arch // TODO(q3k) fix this once we have tree API from arch
@ -388,22 +390,22 @@ class ElementXYRoot : public StaticTreeItem
} }
}; };
class ContextTreeModel : public QAbstractItemModel class Model : public QAbstractItemModel
{ {
public: public:
using BelXYRoot = ElementXYRoot<BelId>; using BelXYRoot = ElementXYRoot<BelId>;
using WireXYRoot = ElementXYRoot<WireId>; using WireXYRoot = ElementXYRoot<WireId>;
using PipXYRoot = ElementXYRoot<PipId>; using PipXYRoot = ElementXYRoot<PipId>;
ContextTreeModel(QObject *parent = nullptr); Model(QObject *parent = nullptr);
~ContextTreeModel(); ~Model();
void loadContext(Context *ctx); void loadContext(Context *ctx);
void updateCellsNets(Context *ctx); void updateCellsNets(Context *ctx);
LazyTreeItem *nodeFromIndex(const QModelIndex &idx) const; Item *nodeFromIndex(const QModelIndex &idx) const;
QModelIndex indexFromNode(LazyTreeItem *node) QModelIndex indexFromNode(Item *node)
{ {
const LazyTreeItem *parent = node->parent(); const Item *parent = node->parent();
if (parent == nullptr) if (parent == nullptr)
return QModelIndex(); return QModelIndex();
@ -411,7 +413,7 @@ class ContextTreeModel : public QAbstractItemModel
} }
QList<QModelIndex> search(QString text); QList<QModelIndex> search(QString text);
boost::optional<LazyTreeItem*> nodeForIdType(ElementType type, IdString id) const boost::optional<Item*> nodeForIdType(ElementType type, IdString id) const
{ {
switch (type) { switch (type) {
case ElementType::BEL: case ElementType::BEL:
@ -441,7 +443,7 @@ class ContextTreeModel : public QAbstractItemModel
bool canFetchMore(const QModelIndex &parent) const Q_DECL_OVERRIDE; bool canFetchMore(const QModelIndex &parent) const Q_DECL_OVERRIDE;
private: private:
std::unique_ptr<LazyTreeItem> root_; std::unique_ptr<Item> root_;
std::unique_ptr<BelXYRoot> bel_root_; std::unique_ptr<BelXYRoot> bel_root_;
std::unique_ptr<WireXYRoot> wire_root_; std::unique_ptr<WireXYRoot> wire_root_;
std::unique_ptr<PipXYRoot> pip_root_; std::unique_ptr<PipXYRoot> pip_root_;
@ -449,6 +451,8 @@ class ContextTreeModel : public QAbstractItemModel
std::unique_ptr<IdStringList> net_root_; std::unique_ptr<IdStringList> net_root_;
}; };
}; // namespace TreeModel
NEXTPNR_NAMESPACE_END NEXTPNR_NAMESPACE_END
#endif // TREEMODEL_H #endif // TREEMODEL_H