From 6baf8216ed1c6bd730a130a4aa4ad489ad70b2a7 Mon Sep 17 00:00:00 2001 From: Sergiusz Bazanski Date: Wed, 1 Aug 2018 02:19:30 +0100 Subject: gui: move tree model into own namespace --- gui/designwidget.cc | 22 +++++++++--------- gui/designwidget.h | 6 ++--- gui/treemodel.cc | 46 ++++++++++++++++++++----------------- gui/treemodel.h | 66 ++++++++++++++++++++++++++++------------------------- 4 files changed, 74 insertions(+), 66 deletions(-) diff --git a/gui/designwidget.cc b/gui/designwidget.cc index d5c0c963..c75991eb 100644 --- a/gui/designwidget.cc +++ b/gui/designwidget.cc @@ -34,7 +34,7 @@ DesignWidget::DesignWidget(QWidget *parent) : QWidget(parent), ctx(nullptr), sel { // Add tree view treeView = new QTreeView(); - treeModel = new ContextTreeModel(); + treeModel = new TreeModel::Model(); treeView->setModel(treeModel); treeView->setContextMenuPolicy(Qt::CustomContextMenu); treeView->setSelectionMode(QAbstractItemView::ExtendedSelection); @@ -223,9 +223,9 @@ void DesignWidget::updateTree() { clearProperties(); - QMap::iterator i = highlightSelected.begin(); + QMap::iterator i = highlightSelected.begin(); while (i != highlightSelected.end()) { - QMap::iterator prev = i; + QMap::iterator prev = i; ++i; if (prev.key()->type() == ElementType::NET && ctx->nets.find(prev.key()->id()) == ctx->nets.end()) { highlightSelected.erase(prev); @@ -348,7 +348,7 @@ void DesignWidget::onSelectionChanged(const QItemSelection &, const QItemSelecti if (selectionModel->selectedIndexes().size() > 1) { std::vector decals; for (auto index : selectionModel->selectedIndexes()) { - LazyTreeItem *item = treeModel->nodeFromIndex(index); + TreeModel::Item *item = treeModel->nodeFromIndex(index); std::vector d = getDecals(item->type(), item->id()); 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); if (!index.isValid()) return; - LazyTreeItem *clickItem = treeModel->nodeFromIndex(index); + TreeModel::Item *clickItem = treeModel->nodeFromIndex(index); ElementType type = clickItem->type(); if (type == ElementType::NONE) @@ -606,7 +606,7 @@ std::vector DesignWidget::getDecals(ElementType type, IdString value) return decals; } -void DesignWidget::updateHighlightGroup(QList items, int group) +void DesignWidget::updateHighlightGroup(QList items, int group) { const bool shouldClear = items.size() == 1; for (auto item : items) { @@ -631,7 +631,7 @@ void DesignWidget::updateHighlightGroup(QList items, int group) void DesignWidget::prepareMenuProperty(const QPoint &pos) { QTreeWidget *tree = propertyEditor->treeWidget(); - QList items; + QList items; for (auto itemContextMenu : tree->selectedItems()) { QtBrowserItem *browserItem = propertyEditor->itemToBrowserItem(itemContextMenu); if (!browserItem) @@ -648,7 +648,7 @@ void DesignWidget::prepareMenuProperty(const QPoint &pos) } int selectedIndex = -1; if (items.size() == 1) { - LazyTreeItem *item = items.at(0); + TreeModel::Item *item = items.at(0); if (highlightSelected.contains(item)) selectedIndex = highlightSelected[item]; } @@ -689,13 +689,13 @@ void DesignWidget::prepareMenuTree(const QPoint &pos) if (selectionModel->selectedIndexes().size() == 0) return; - QList items; + QList items; for (auto index : selectionModel->selectedIndexes()) { - LazyTreeItem *item = treeModel->nodeFromIndex(index); + TreeModel::Item *item = treeModel->nodeFromIndex(index); items.append(item); } if (items.size() == 1) { - LazyTreeItem *item = items.at(0); + TreeModel::Item *item = items.at(0); if (highlightSelected.contains(item)) selectedIndex = highlightSelected[item]; } diff --git a/gui/designwidget.h b/gui/designwidget.h index d6af83a0..628586f4 100644 --- a/gui/designwidget.h +++ b/gui/designwidget.h @@ -51,7 +51,7 @@ class DesignWidget : public QWidget void updateButtons(); void addToHistory(QModelIndex item); std::vector getDecals(ElementType type, IdString value); - void updateHighlightGroup(QList item, int group); + void updateHighlightGroup(QList item, int group); Q_SIGNALS: void info(std::string text); void selected(std::vector decal, bool keep); @@ -77,7 +77,7 @@ class DesignWidget : public QWidget QTreeView *treeView; QItemSelectionModel *selectionModel; - ContextTreeModel *treeModel; + TreeModel::Model *treeModel; QLineEdit *searchEdit; QtVariantPropertyManager *variantManager; QtVariantPropertyManager *readOnlyManager; @@ -99,7 +99,7 @@ class DesignWidget : public QWidget QAction *actionClear; QColor highlightColors[8]; - QMap highlightSelected; + QMap highlightSelected; QString currentSearch; QList currentSearchIndexes; diff --git a/gui/treemodel.cc b/gui/treemodel.cc index e3d9b4ae..ac256104 100644 --- a/gui/treemodel.cc +++ b/gui/treemodel.cc @@ -22,13 +22,15 @@ NEXTPNR_NAMESPACE_BEGIN -ContextTreeModel::ContextTreeModel(QObject *parent) : +namespace TreeModel { + +Model::Model(QObject *parent) : QAbstractItemModel(parent), root_(new StaticTreeItem("Elements", nullptr, ElementType::NONE)) {} -ContextTreeModel::~ContextTreeModel() {} +Model::~Model() {} -void ContextTreeModel::loadContext(Context *ctx) +void Model::loadContext(Context *ctx) { if (!ctx) return; @@ -80,7 +82,7 @@ void ContextTreeModel::loadContext(Context *ctx) updateCellsNets(ctx); } -void ContextTreeModel::updateCellsNets(Context *ctx) +void Model::updateCellsNets(Context *ctx) { if (!ctx) return; @@ -102,39 +104,39 @@ void ContextTreeModel::updateCellsNets(Context *ctx) 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()) return QModelIndex(); 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()) return QModelIndex(); - LazyTreeItem *node = parent->parent(); + Item *node = parent->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) return QVariant(); if (role != Qt::DisplayRole) return QVariant(); - LazyTreeItem *node = nodeFromIndex(index); + Item *node = nodeFromIndex(index); 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); if (orientation == Qt::Horizontal && role == Qt::DisplayRole) @@ -143,31 +145,31 @@ QVariant ContextTreeModel::headerData(int section, Qt::Orientation orientation, return QVariant(); } -LazyTreeItem *ContextTreeModel::nodeFromIndex(const QModelIndex &idx) const +Item *Model::nodeFromIndex(const QModelIndex &idx) const { if (idx.isValid()) - return (LazyTreeItem *)idx.internalPointer(); + return (Item *)idx.internalPointer(); 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); } -void ContextTreeModel::fetchMore(const QModelIndex &parent) +void Model::fetchMore(const QModelIndex &parent) { nodeFromIndex(parent)->fetchMore(); } -bool ContextTreeModel::canFetchMore(const QModelIndex &parent) const +bool Model::canFetchMore(const QModelIndex &parent) const { return nodeFromIndex(parent)->canFetchMore(); } -QList ContextTreeModel::search(QString text) +QList Model::search(QString text) { QList list; //for (int i = 0; i < 6; i++) { @@ -182,4 +184,6 @@ QList ContextTreeModel::search(QString text) return list; } +}; // namespace TreeModel + NEXTPNR_NAMESPACE_END diff --git a/gui/treemodel.h b/gui/treemodel.h index 620b4e21..8793d8e5 100644 --- a/gui/treemodel.h +++ b/gui/treemodel.h @@ -38,21 +38,23 @@ enum class ElementType GROUP }; -class LazyTreeItem +namespace TreeModel { + +class Item { protected: QString name_; - LazyTreeItem *parent_; - QList children_; + Item *parent_; + QList children_; ElementType type_; - void addChild(LazyTreeItem *child) + void addChild(Item *child) { children_.append(child); } public: - LazyTreeItem(QString name, LazyTreeItem *parent, ElementType type) : + Item(QString name, Item *parent, ElementType type) : name_(name), parent_(parent), type_(type) { // Register in parent if exists. @@ -71,28 +73,28 @@ class LazyTreeItem return name_; } - LazyTreeItem *child(int index) + Item *child(int index) { return children_.at(index); } - int indexOf(const LazyTreeItem *child) const + int indexOf(const Item *child) const { // 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); } - const LazyTreeItem *parent() const + const Item *parent() const { return parent_; } - LazyTreeItem *parent() + Item *parent() { return parent_; } @@ -106,13 +108,13 @@ class LazyTreeItem virtual void fetchMore() = 0; virtual IdString id() const = 0; - virtual ~LazyTreeItem() {} + virtual ~Item() {} }; -class StaticTreeItem : public LazyTreeItem +class StaticTreeItem : public Item { public: - using LazyTreeItem::LazyTreeItem; + using Item::Item; virtual bool canFetchMore() const override { @@ -137,7 +139,7 @@ class IdStringItem : public StaticTreeItem IdString id_; 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) {} virtual IdString id() const override @@ -147,7 +149,7 @@ class IdStringItem : public StaticTreeItem }; template -class ElementList : public LazyTreeItem +class ElementList : public Item { public: using ElementMap = std::map, std::vector>; @@ -168,8 +170,8 @@ class ElementList : public LazyTreeItem } public: - ElementList(Context *ctx, QString name, LazyTreeItem *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) + ElementList(Context *ctx, QString name, Item *parent, ElementMap *map, int x, int y, ElementGetter getter, ElementType 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(); } - boost::optional getById(IdString id) + boost::optional getById(IdString id) { // Search requires us to load all our elements... while (canFetchMore()) fetchMore(); @@ -225,7 +227,7 @@ class IdStringList : public StaticTreeItem std::unordered_map> managed_; ElementType child_type_; public: - IdStringList(QString name, LazyTreeItem *parent, ElementType type) : + IdStringList(QString name, Item *parent, ElementType type) : StaticTreeItem(name, parent, ElementType::NONE), child_type_(type) {} using StaticTreeItem::StaticTreeItem; @@ -285,7 +287,7 @@ class IdStringList : public StaticTreeItem } // 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_b = alphaNumSplit(b->name()); @@ -346,7 +348,7 @@ class ElementXYRoot : public StaticTreeItem ElementType child_type_; 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) { std::vector y_present; @@ -374,7 +376,7 @@ class ElementXYRoot : public StaticTreeItem } } - boost::optional getById(IdString id) + boost::optional getById(IdString id) { // For now, scan linearly all ElementLists. // 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: using BelXYRoot = ElementXYRoot; using WireXYRoot = ElementXYRoot; using PipXYRoot = ElementXYRoot; - ContextTreeModel(QObject *parent = nullptr); - ~ContextTreeModel(); + Model(QObject *parent = nullptr); + ~Model(); void loadContext(Context *ctx); void updateCellsNets(Context *ctx); - LazyTreeItem *nodeFromIndex(const QModelIndex &idx) const; - QModelIndex indexFromNode(LazyTreeItem *node) + Item *nodeFromIndex(const QModelIndex &idx) const; + QModelIndex indexFromNode(Item *node) { - const LazyTreeItem *parent = node->parent(); + const Item *parent = node->parent(); if (parent == nullptr) return QModelIndex(); @@ -411,7 +413,7 @@ class ContextTreeModel : public QAbstractItemModel } QList search(QString text); - boost::optional nodeForIdType(ElementType type, IdString id) const + boost::optional nodeForIdType(ElementType type, IdString id) const { switch (type) { case ElementType::BEL: @@ -441,7 +443,7 @@ class ContextTreeModel : public QAbstractItemModel bool canFetchMore(const QModelIndex &parent) const Q_DECL_OVERRIDE; private: - std::unique_ptr root_; + std::unique_ptr root_; std::unique_ptr bel_root_; std::unique_ptr wire_root_; std::unique_ptr pip_root_; @@ -449,6 +451,8 @@ class ContextTreeModel : public QAbstractItemModel std::unique_ptr net_root_; }; +}; // namespace TreeModel + NEXTPNR_NAMESPACE_END #endif // TREEMODEL_H -- cgit v1.2.3