Add logic for selecting currently active window at startup

This commit is contained in:
LordGrimmauld 2024-03-06 23:40:48 +01:00
parent da05b706e4
commit 4ba62a63ee
6 changed files with 50 additions and 5 deletions

View file

@ -4,6 +4,7 @@
#include "tree/SwayTreeModel.h"
#include <QScreen>
#include <QTimer>
#include <iostream>
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();
}

View file

@ -12,8 +12,7 @@
template<class T>
class AbstractTreeModel : public QAbstractItemModel {
static_assert(std::is_base_of<AbstractTreeNode<T>, 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)

View file

@ -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<T> &child) { children.push_back(std::move(child)); }
[[nodiscard]] T* findChild(std::function<bool(const T &)> test) {
[[nodiscard]] T *findChild(std::function<bool(const T &)> 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<bool(const T *)> 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));

View file

@ -3,3 +3,24 @@
//
#include "SwayTreeModel.h"
QModelIndex SwayTreeModel::getFocused() const {
std::function<bool(const SwayTreeNode *)> test = [](const SwayTreeNode *node) {
return node->node.focused;
};
const SwayTreeNode *focused = rootItem->findChildRecursive(test);
std::vector<int> 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;
}

View file

@ -42,6 +42,9 @@ public:
};
[[nodiscard]] QModelIndex getFocused() const;
private:
SwayTreeNode * rootItem;
};

View file

@ -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<SwayTreeNode> {