diff --git a/mainwindow.cpp b/mainwindow.cpp index bec4990..b27bf90 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -4,6 +4,7 @@ #include "tree/SwayTreeModel.h" #include #include +#include MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { @@ -18,6 +19,9 @@ MainWindow::MainWindow(QWidget *parent) ui->treeView->setModel(model); ui->treeView->expandAll(); + ui->treeView->setSelectionMode(QAbstractItemView::ExtendedSelection); + ui->treeView->selectionModel()->select(model->getFocused(), QItemSelectionModel::ClearAndSelect); + for (int c = 0; c < model->columnCount(); ++c) ui->treeView->resizeColumnToContents(c); } @@ -29,5 +33,6 @@ MainWindow::~MainWindow() { } void MainWindow::update() { + std::cout << ui->treeView->currentIndex().parent().row() << "\n"; // model->update(); } diff --git a/tree/AbstractTreeModel.h b/tree/AbstractTreeModel.h index 2b84e8d..4c2bcbe 100644 --- a/tree/AbstractTreeModel.h +++ b/tree/AbstractTreeModel.h @@ -12,8 +12,7 @@ template class AbstractTreeModel : public QAbstractItemModel { static_assert(std::is_base_of, T>::value, - "Tree model subject class is not derived from AbstractTreeNode"); - + "Tree node subject class is not derived from AbstractTreeNode"); public: Q_DISABLE_COPY_MOVE(AbstractTreeModel) diff --git a/tree/AbstractTreeNode.h b/tree/AbstractTreeNode.h index fcb3591..f352dd4 100644 --- a/tree/AbstractTreeNode.h +++ b/tree/AbstractTreeNode.h @@ -19,7 +19,7 @@ public: explicit AbstractTreeNode(T *parent = nullptr) : parent(parent) {} - [[nodiscard]] T *parentItem() { return parent; }; + [[nodiscard]] T *parentItem() const { return parent; }; [[nodiscard]] size_t childCount() const { return children.size(); }; @@ -31,7 +31,7 @@ public: [[nodiscard]] virtual int columnCount() const = 0; - [[nodiscard]] int row() { + [[nodiscard]] int row() const { if (parent == nullptr) return 0; const auto it = std::find_if(parent->children.cbegin(), parent->children.cend(), @@ -46,7 +46,7 @@ public: void appendChild(std::unique_ptr &child) { children.push_back(std::move(child)); } - [[nodiscard]] T* findChild(std::function test) { + [[nodiscard]] T *findChild(std::function test) const { for (int i = 0; i < childCount(); ++i) { auto* c = child(i); @@ -56,6 +56,19 @@ public: return nullptr; }; + [[nodiscard]] const T *findChildRecursive(std::function test) const { + if (test((const T *) this)) + return (const T*) this; + + for (int i = 0; i < childCount(); ++i) { + auto *c = child(i); + const T *r = c->findChildRecursive(test); + if (r != nullptr) + return r; + } + return nullptr; + }; + 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 3756352..a5511b6 100644 --- a/tree/SwayTreeModel.cpp +++ b/tree/SwayTreeModel.cpp @@ -3,3 +3,24 @@ // #include "SwayTreeModel.h" + +QModelIndex SwayTreeModel::getFocused() const { + + std::function test = [](const SwayTreeNode *node) { + return node->node.focused; + }; + const SwayTreeNode *focused = rootItem->findChildRecursive(test); + + std::vector indices; + for (const auto *head = focused; head != nullptr; head = head->parentItem()) { + indices.insert(indices.cbegin(), head->row()); + } + + // weirdness, idk... ask QT + QModelIndex idx = this->index(indices.size() < 2 ? 0 : indices[1], 0); + for (int i = 2; i < indices.size(); ++i) { + idx = this->index(indices[i], 0, idx); + } + + return idx; +} diff --git a/tree/SwayTreeModel.h b/tree/SwayTreeModel.h index 6fa5638..a0690b3 100644 --- a/tree/SwayTreeModel.h +++ b/tree/SwayTreeModel.h @@ -42,6 +42,9 @@ public: }; + [[nodiscard]] QModelIndex getFocused() const; + + private: SwayTreeNode * rootItem; }; diff --git a/tree/swaytree.h b/tree/swaytree.h index 40f6ad9..eee2907 100644 --- a/tree/swaytree.h +++ b/tree/swaytree.h @@ -114,6 +114,10 @@ struct SwayRecord { [[nodiscard]] bool operator==(const SwayRecord &other) const { return this->id == other.id; } + + [[nodiscard]] bool isFocused() const { + return focused; + } }; class SwayTreeNode : public AbstractTreeNode {