2024-03-04 00:27:05 +01:00
|
|
|
//
|
|
|
|
// Created by grimmauld on 03.03.24.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "SwayTreeModel.h"
|
2024-03-06 23:40:48 +01:00
|
|
|
|
2024-03-09 23:44:43 +01:00
|
|
|
QModelIndex SwayTreeModel::findIndexByNode(const SwayTreeNode* node) const {
|
2024-03-06 23:40:48 +01:00
|
|
|
std::vector<int> indices;
|
2024-03-09 23:44:43 +01:00
|
|
|
for (const auto *head = node; head != nullptr; head = head->parentItem()) {
|
2024-03-06 23:40:48 +01:00
|
|
|
indices.insert(indices.cbegin(), head->row());
|
|
|
|
}
|
|
|
|
|
2024-03-09 23:44:43 +01:00
|
|
|
// weirdness, idk ask QT
|
2024-03-06 23:40:48 +01:00
|
|
|
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;
|
|
|
|
}
|
2024-03-08 13:13:28 +01:00
|
|
|
|
|
|
|
std::set<SwayTreeNode *> SwayTreeModel::getSelectedNodes(const QTreeView *treeView) const {
|
|
|
|
std::set<SwayTreeNode *> selectedNodes;
|
2024-03-09 23:44:43 +01:00
|
|
|
if (rootItem == nullptr)
|
|
|
|
return selectedNodes;
|
|
|
|
|
2024-03-08 13:13:28 +01:00
|
|
|
auto indexes = treeView->selectionModel()->selectedIndexes();
|
|
|
|
for (auto i: indexes) {
|
2024-03-09 23:44:43 +01:00
|
|
|
auto node = getSelectedNode(i);
|
|
|
|
if (node != nullptr)
|
|
|
|
selectedNodes.insert(node);
|
2024-03-08 13:13:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return selectedNodes;
|
|
|
|
}
|
2024-03-09 23:44:43 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|