refactor the model to easily get focused, root, output and workspace
This commit is contained in:
parent
4ba62a63ee
commit
e773033f3b
5 changed files with 40 additions and 14 deletions
|
@ -20,7 +20,7 @@ MainWindow::MainWindow(QWidget *parent)
|
||||||
ui->treeView->expandAll();
|
ui->treeView->expandAll();
|
||||||
|
|
||||||
ui->treeView->setSelectionMode(QAbstractItemView::ExtendedSelection);
|
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)
|
for (int c = 0; c < model->columnCount(); ++c)
|
||||||
ui->treeView->resizeColumnToContents(c);
|
ui->treeView->resizeColumnToContents(c);
|
||||||
|
|
|
@ -69,6 +69,16 @@ public:
|
||||||
return nullptr;
|
return nullptr;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
[[nodiscard]] const T *findParentRecursive(std::function<bool(const T *)> test) const {
|
||||||
|
if (test((const T *) this))
|
||||||
|
return (const T*) this;
|
||||||
|
|
||||||
|
if (parent == nullptr)
|
||||||
|
return nullptr;
|
||||||
|
|
||||||
|
return this->findParentRecursive(test);
|
||||||
|
};
|
||||||
|
|
||||||
void removeChild(int i) {
|
void removeChild(int i) {
|
||||||
if (i >= 0 && i < childCount()) {
|
if (i >= 0 && i < childCount()) {
|
||||||
children.erase(std::next(children.begin(), i));
|
children.erase(std::next(children.begin(), i));
|
||||||
|
|
|
@ -4,15 +4,9 @@
|
||||||
|
|
||||||
#include "SwayTreeModel.h"
|
#include "SwayTreeModel.h"
|
||||||
|
|
||||||
QModelIndex SwayTreeModel::getFocused() const {
|
QModelIndex SwayTreeModel::findFocusedWindowIndex() const {
|
||||||
|
|
||||||
std::function<bool(const SwayTreeNode *)> test = [](const SwayTreeNode *node) {
|
|
||||||
return node->node.focused;
|
|
||||||
};
|
|
||||||
const SwayTreeNode *focused = rootItem->findChildRecursive(test);
|
|
||||||
|
|
||||||
std::vector<int> indices;
|
std::vector<int> 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());
|
indices.insert(indices.cbegin(), head->row());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -42,7 +42,7 @@ public:
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
[[nodiscard]] QModelIndex getFocused() const;
|
[[nodiscard]] QModelIndex findFocusedWindowIndex() const;
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
|
@ -114,10 +114,6 @@ struct SwayRecord {
|
||||||
[[nodiscard]] bool operator==(const SwayRecord &other) const {
|
[[nodiscard]] bool operator==(const SwayRecord &other) const {
|
||||||
return this->id == other.id;
|
return this->id == other.id;
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] bool isFocused() const {
|
|
||||||
return focused;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class SwayTreeNode : public AbstractTreeNode<SwayTreeNode> {
|
class SwayTreeNode : public AbstractTreeNode<SwayTreeNode> {
|
||||||
|
@ -153,6 +149,32 @@ public:
|
||||||
[[nodiscard]] QVariant headerData(int column) const override;
|
[[nodiscard]] QVariant headerData(int column) const override;
|
||||||
|
|
||||||
[[nodiscard]] int columnCount() const override { return 1; };
|
[[nodiscard]] int columnCount() const override { return 1; };
|
||||||
|
|
||||||
|
[[nodiscard]] const SwayTreeNode *findFocused() const {
|
||||||
|
std::function<bool(const SwayTreeNode *)> 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<bool(const SwayTreeNode *)> matchNodeType(const NodeType::NodeType type) {
|
||||||
|
return [type](const SwayTreeNode *testNode) {
|
||||||
|
return testNode->node.type == type;
|
||||||
|
};
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue