Add help menu list view, add toggle to get to help menu

This commit is contained in:
LordGrimmauld 2024-03-07 17:37:18 +01:00
parent e773033f3b
commit 6f1498d65c
10 changed files with 228 additions and 25 deletions

View file

@ -30,6 +30,11 @@ set(PROJECT_SOURCES
tree/AbstractTreeModel.h
tree/SwayTreeModel.h
tree/SwayTreeModel.cpp
Keys/KeyHandler.h
Keys/KeyHandler.cpp
Keys/AbstractKeyListener.h
Keys/HelpKeyListener.h
Keys/CloseHelpKeyListener.h
)
if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)

View file

@ -0,0 +1,32 @@
//
// Created by grimmauld on 07.03.24.
//
#ifndef SWAYMUX_ABSTRACTKEYLISTENER_H
#define SWAYMUX_ABSTRACTKEYLISTENER_H
#include <set>
#include <Qt>
#include <QKeyEvent>
class AbstractKeyListener {
public:
virtual void handleKeyEvent(const QKeyEvent *keyEvent) const = 0;
bool testAndInvoke(const QKeyEvent *keyEvent) const {
if (canAcceptKey(keyEvent->key())) {
handleKeyEvent(keyEvent);
return true;
}
return false;
}
virtual std::string getDescription() = 0;
virtual std::string getKeyText() = 0;
[[nodiscard]] virtual bool canAcceptKey(int key) const = 0;
};
#endif //SWAYMUX_ABSTRACTKEYLISTENER_H

View file

@ -0,0 +1,38 @@
//
// Created by grimmauld on 07.03.24.
//
#ifndef SWAYMUX_CLOSEHELPKEYLISTENER_H
#define SWAYMUX_CLOSEHELPKEYLISTENER_H
#include <QStackedWidget>
#include "AbstractKeyListener.h"
class CloseHelpKeyListener : public AbstractKeyListener {
public:
explicit CloseHelpKeyListener(QWidget *pWidget) : stackPage(pWidget) {}
void handleKeyEvent(const QKeyEvent *keyEvent) const override {
auto* stackWidget = qobject_cast<QStackedWidget*>(stackPage->parent());
if (stackWidget != nullptr) {
stackWidget->setCurrentWidget(stackPage);
}
}
std::string getDescription() override {
return "Close help menu";
}
std::string getKeyText() override {
return "?, ESC, q";
}
[[nodiscard]] bool canAcceptKey(int key) const override {
return key == Qt::Key_Question || key == Qt::Key_Q || key == Qt::Key_Escape;
}
QWidget *stackPage{};
};
#endif //SWAYMUX_CLOSEHELPKEYLISTENER_H

37
Keys/HelpKeyListener.h Normal file
View file

@ -0,0 +1,37 @@
//
// Created by grimmauld on 07.03.24.
//
#ifndef SWAYMUX_HELPKEYLISTENER_H
#define SWAYMUX_HELPKEYLISTENER_H
#include "AbstractKeyListener.h"
class HelpKeyListener : public AbstractKeyListener {
public:
explicit HelpKeyListener(QWidget *pWidget) : stackPage(pWidget) {}
void handleKeyEvent(const QKeyEvent *keyEvent) const override {
auto* stackWidget = qobject_cast<QStackedWidget*>(stackPage->parent());
if (stackWidget != nullptr) {
stackWidget->setCurrentWidget(stackPage);
}
}
std::string getDescription() override {
return "View this help menu";
}
std::string getKeyText() override {
return "?";
}
[[nodiscard]] bool canAcceptKey(int key) const override {
return key == Qt::Key_Question;
}
QWidget *stackPage;
};
#endif //SWAYMUX_HELPKEYLISTENER_H

19
Keys/KeyHandler.cpp Normal file
View file

@ -0,0 +1,19 @@
//
// Created by grimmauld on 07.03.24.
//
#include <QKeyEvent>
#include "KeyHandler.h"
bool KeyHandler::eventFilter(QObject *obj, QEvent *event) {
if (event->type() == QEvent::KeyPress) {
auto *key = dynamic_cast<QKeyEvent *>(event);
return std::any_of(listeners.cbegin(), listeners.cend(),
[key](const auto *listener) {
return listener->testAndInvoke(key);
});
} else {
return QObject::eventFilter(obj, event);
}
}

25
Keys/KeyHandler.h Normal file
View file

@ -0,0 +1,25 @@
//
// Created by grimmauld on 07.03.24.
//
#ifndef SWAYMUX_KEYHANDLER_H
#define SWAYMUX_KEYHANDLER_H
#include <QObject>
#include "AbstractKeyListener.h"
class KeyHandler : public QObject {
Q_OBJECT
public:
void addListener(AbstractKeyListener *listener) { listeners.push_back(listener); }
protected:
bool eventFilter(QObject *obj, QEvent *event) override;
std::vector<AbstractKeyListener *> listeners;
};
#endif //SWAYMUX_KEYHANDLER_H

View file

@ -1,19 +1,20 @@
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include "tree/PsTreeModel.h"
#include "tree/SwayTreeModel.h"
#include <QScreen>
#include "Keys/HelpKeyListener.h"
#include "Keys/CloseHelpKeyListener.h"
#include <QTimer>
#include <iostream>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow) {
ui->setupUi(this);
model = new SwayTreeModel(ui->treeView);
modelUpdateTimer = new QTimer(this);
connect(modelUpdateTimer, &QTimer::timeout, this, QOverload<>::of(&MainWindow::update));
modelUpdateTimer->start(1000);
// modelUpdateTimer = new QTimer(this);
// connect(modelUpdateTimer, &QTimer::timeout, this, QOverload<>::of(&MainWindow::update));
// modelUpdateTimer->start(1000);
ui->statusbar->showMessage(QString::fromStdString("test test 123"));
ui->treeView->setModel(model);
@ -24,15 +25,28 @@ MainWindow::MainWindow(QWidget *parent)
for (int c = 0; c < model->columnCount(); ++c)
ui->treeView->resizeColumnToContents(c);
swayTreeKeyHandler = new KeyHandler();
swayTreeKeyHandler->addListener(new HelpKeyListener(ui->help_page));
closeHelpKeyHandler = new KeyHandler();
closeHelpKeyHandler->addListener(new CloseHelpKeyListener(ui->tree_page));
ui->stackedWidget->setCurrentIndex(0);
ui->treeView->installEventFilter(swayTreeKeyHandler);
ui->listView->installEventFilter(closeHelpKeyHandler);
}
MainWindow::~MainWindow() {
delete ui;
delete model;
delete modelUpdateTimer;
// delete modelUpdateTimer;
delete swayTreeKeyHandler;
delete closeHelpKeyHandler;
}
void MainWindow::update() {
std::cout << ui->treeView->currentIndex().parent().row() << "\n";
// model->update();
void MainWindow::keyPressEvent(QKeyEvent *event) {
if (event->key() == Qt::Key_W) {
qDebug() << "hi";
}
}

View file

@ -4,6 +4,7 @@
#include <QMainWindow>
#include "tree/PsTreeModel.h"
#include "tree/SwayTreeModel.h"
#include "Keys/KeyHandler.h"
QT_BEGIN_NAMESPACE
namespace Ui {
@ -19,11 +20,13 @@ public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow() override;
void update() ;
void keyPressEvent(QKeyEvent *event) override;
private:
Ui::MainWindow *ui;
SwayTreeModel* model;
QTimer* modelUpdateTimer;
// QTimer* modelUpdateTimer{};
KeyHandler* swayTreeKeyHandler;
KeyHandler* closeHelpKeyHandler;
};
#endif // MAINWINDOW_H

View file

@ -21,16 +21,23 @@
</property>
<widget class="QWidget" name="centralwidget">
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Maximum">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
<height>16777215</height>
</size>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QStackedWidget" name="stackedWidget">
<property name="currentIndex">
<number>0</number>
</property>
<widget class="QWidget" name="tree_page">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
@ -44,6 +51,29 @@
</item>
</layout>
</widget>
<widget class="QWidget" name="help_page">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QListView" name="listView">
<property name="font">
<font>
<pointsize>20</pointsize>
</font>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
</item>
</layout>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>

View file

@ -86,7 +86,7 @@ struct SwayRecord {
explicit SwayRecord(const json &rep) :
id(rep["id"]),
name(rep["name"]),
name(rep["name"].is_null() ? "untitled container" : rep["name"]),
type(NodeType::fromString(rep["type"])),
border(rep["border"]),
current_border_width(rep["current_border_width"]),