swaymux/tree/SwayTreeModel.cpp

57 lines
1.4 KiB
C++

//
// Created by grimmauld on 03.03.24.
//
#include "SwayTreeModel.h"
QModelIndex SwayTreeModel::findIndexByNode(const SwayTreeNode* node) const {
std::vector<int> indices;
for (const auto *head = node; 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;
}
std::set<SwayTreeNode *> SwayTreeModel::getSelectedNodes(const QTreeView *treeView) const {
std::set<SwayTreeNode *> selectedNodes;
if (rootItem == nullptr)
return selectedNodes;
auto indexes = treeView->selectionModel()->selectedIndexes();
for (auto i: indexes) {
auto node = getSelectedNode(i);
if (node != nullptr)
selectedNodes.insert(node);
}
return selectedNodes;
}
SwayTreeNode *SwayTreeModel::getSelectedNode(const QModelIndex &i) const {
if (rootItem == nullptr)
return nullptr;
std::vector<int> indices;
for (auto j = i; j.isValid(); j = j.parent()) {
indices.insert(indices.cbegin(), j.row());
}
auto *head = rootItem;
for (auto j: indices) {
head = head->child(j);
if (head == nullptr)
return nullptr;
}
return head;
}