From e773033f3bd532ac7444f16d7403864fd8c082f8 Mon Sep 17 00:00:00 2001 From: LordGrimmauld Date: Thu, 7 Mar 2024 11:20:26 +0100 Subject: [PATCH] refactor the model to easily get focused, root, output and workspace --- mainwindow.cpp | 2 +- tree/AbstractTreeNode.h | 10 ++++++++++ tree/SwayTreeModel.cpp | 10 ++-------- tree/SwayTreeModel.h | 2 +- tree/swaytree.h | 30 ++++++++++++++++++++++++++---- 5 files changed, 40 insertions(+), 14 deletions(-) diff --git a/mainwindow.cpp b/mainwindow.cpp index b27bf90..ee2410e 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -20,7 +20,7 @@ MainWindow::MainWindow(QWidget *parent) ui->treeView->expandAll(); ui->treeView->setSelectionMode(QAbstractItemView::ExtendedSelection); - ui->treeView->selectionModel()->select(model->getFocused(), QItemSelectionModel::ClearAndSelect); + ui->treeView->selectionModel()->select(model->findFocusedWindowIndex(), QItemSelectionModel::ClearAndSelect); for (int c = 0; c < model->columnCount(); ++c) ui->treeView->resizeColumnToContents(c); diff --git a/tree/AbstractTreeNode.h b/tree/AbstractTreeNode.h index f352dd4..5054876 100644 --- a/tree/AbstractTreeNode.h +++ b/tree/AbstractTreeNode.h @@ -69,6 +69,16 @@ public: return nullptr; }; + [[nodiscard]] const T *findParentRecursive(std::function test) const { + if (test((const T *) this)) + return (const T*) this; + + if (parent == nullptr) + return nullptr; + + return this->findParentRecursive(test); + }; + void removeChild(int i) { if (i >= 0 && i < childCount()) { children.erase(std::next(children.begin(), i)); diff --git a/tree/SwayTreeModel.cpp b/tree/SwayTreeModel.cpp index a5511b6..65e8901 100644 --- a/tree/SwayTreeModel.cpp +++ b/tree/SwayTreeModel.cpp @@ -4,15 +4,9 @@ #include "SwayTreeModel.h" -QModelIndex SwayTreeModel::getFocused() const { - - std::function test = [](const SwayTreeNode *node) { - return node->node.focused; - }; - const SwayTreeNode *focused = rootItem->findChildRecursive(test); - +QModelIndex SwayTreeModel::findFocusedWindowIndex() const { std::vector indices; - for (const auto *head = focused; head != nullptr; head = head->parentItem()) { + for (const auto *head = getRoot()->findFocused(); head != nullptr; head = head->parentItem()) { indices.insert(indices.cbegin(), head->row()); } diff --git a/tree/SwayTreeModel.h b/tree/SwayTreeModel.h index a0690b3..bb46404 100644 --- a/tree/SwayTreeModel.h +++ b/tree/SwayTreeModel.h @@ -42,7 +42,7 @@ public: }; - [[nodiscard]] QModelIndex getFocused() const; + [[nodiscard]] QModelIndex findFocusedWindowIndex() const; private: diff --git a/tree/swaytree.h b/tree/swaytree.h index eee2907..7915a12 100644 --- a/tree/swaytree.h +++ b/tree/swaytree.h @@ -114,10 +114,6 @@ struct SwayRecord { [[nodiscard]] bool operator==(const SwayRecord &other) const { return this->id == other.id; } - - [[nodiscard]] bool isFocused() const { - return focused; - } }; class SwayTreeNode : public AbstractTreeNode { @@ -153,6 +149,32 @@ public: [[nodiscard]] QVariant headerData(int column) const override; [[nodiscard]] int columnCount() const override { return 1; }; + + [[nodiscard]] const SwayTreeNode *findFocused() const { + std::function focused = [](const SwayTreeNode *testNode) { + return testNode->node.focused; + }; + return this->findChildRecursive(focused); + } + + [[nodiscard]] inline const SwayTreeNode *findRoot() const { + return this->findParentRecursive(matchNodeType(NodeType::root)); + } + + [[nodiscard]] inline const SwayTreeNode *findOutput() const { + return this->findParentRecursive(matchNodeType(NodeType::output)); + } + + [[nodiscard]] inline const SwayTreeNode *findWorkspace() const { + return this->findParentRecursive(matchNodeType(NodeType::workspace)); + } + +private: + [[nodiscard]] static std::function matchNodeType(const NodeType::NodeType type) { + return [type](const SwayTreeNode *testNode) { + return testNode->node.type == type; + }; + } };