Skip to content

Commit ba90a23

Browse files
authored
Merge pull request #118 from Martian-Technologies/ciuicn-keybinds
Ciuicn keybinds
2 parents 8cff430 + ab7d9f9 commit ba90a23

File tree

6 files changed

+99
-39
lines changed

6 files changed

+99
-39
lines changed

src/gui/circuitViewWidget.cpp

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717
#include "circuitViewWidget.h"
1818
#include "backend/backend.h"
1919

20-
CircuitViewWidget::CircuitViewWidget(QWidget* parent, Ui::CircuitViewUi* ui, CircuitFileManager* fileManager) :
21-
QWidget(parent), mouseControls(false), circuitSelector(ui->CircuitSelector), evaluatorSelector(ui->EvaluatorSelector), fileManager(fileManager) {
20+
CircuitViewWidget::CircuitViewWidget(QWidget* parent, Ui::CircuitViewUi* ui, CircuitFileManager* fileManager, KeybindManager* keybindManager) :
21+
QWidget(parent), mouseControls(false), circuitSelector(ui->CircuitSelector), evaluatorSelector(ui->EvaluatorSelector), fileManager(fileManager), keybindManager(keybindManager) {
2222

2323
// create circuitView
2424
renderer = std::make_unique<QtRenderer>();
2525
circuitView = std::make_unique<CircuitView>(renderer.get());
26-
26+
2727
// qt settings
2828
setFocusPolicy(Qt::StrongFocus);
2929
grabGesture(Qt::PinchGesture);
@@ -46,9 +46,25 @@ CircuitViewWidget::CircuitViewWidget(QWidget* parent, Ui::CircuitViewUi* ui, Cir
4646
renderer->resize(w, h);
4747
renderer->initializeTileSet(":logicTiles.png");
4848

49+
// create keybind shortcuts and connect them
50+
connect(keybindManager->createShortcut("Save", this), &QShortcut::activated, this, &CircuitViewWidget::save);
51+
connect(keybindManager->createShortcut("Undo", this), &QShortcut::activated, this, [this]() {
52+
circuitView->getCircuit()->undo();
53+
});
54+
connect(keybindManager->createShortcut("Redo", this), &QShortcut::activated, this, [this]() {
55+
circuitView->getCircuit()->redo();
56+
});
57+
connect(keybindManager->createShortcut("BlockRotateCCW", this), &QShortcut::activated, this, [this]() {
58+
circuitView->getEventRegister().doEvent(Event("tool rotate block ccw"));
59+
});
60+
connect(keybindManager->createShortcut("BlockRotateCW", this), &QShortcut::activated, this, [this]() {
61+
circuitView->getEventRegister().doEvent(Event("tool rotate block cw"));
62+
});
63+
connect(keybindManager->createShortcut("ToggleInteractive", this), &QShortcut::activated, this, [this]() {
64+
circuitView->toggleInteractive();
65+
});
66+
4967
// connect buttons and actions
50-
QShortcut* saveShortcut = new QShortcut(QKeySequence("Ctrl+S"), this);
51-
connect(saveShortcut, &QShortcut::activated, this, &CircuitViewWidget::save);
5268
connect(ui->StartSim, &QPushButton::clicked, this, &CircuitViewWidget::setSimState);
5369
connect(ui->UseSpeed, &QCheckBox::checkStateChanged, this, &CircuitViewWidget::simUseSpeed);
5470
connect(ui->Speed, &QDoubleSpinBox::valueChanged, this, &CircuitViewWidget::setSimSpeed);
@@ -229,29 +245,6 @@ void CircuitViewWidget::wheelEvent(QWheelEvent* event) {
229245
}
230246
}
231247

232-
void CircuitViewWidget::keyPressEvent(QKeyEvent* event) {
233-
if (/*event->modifiers() & Qt::MetaModifier && */event->key() == Qt::Key_Z) {
234-
circuitView->getCircuit()->undo();
235-
event->accept();
236-
} else if (/*event->modifiers() & Qt::MetaModifier && */event->key() == Qt::Key_Y) {
237-
circuitView->getCircuit()->redo();
238-
event->accept();
239-
} else if (event->key() == Qt::Key_Q) {
240-
if (circuitView->getEventRegister().doEvent(Event("tool rotate block ccw"))) {
241-
event->accept();
242-
}
243-
} else if (event->key() == Qt::Key_E) {
244-
if (circuitView->getEventRegister().doEvent(Event("tool rotate block cw"))) {
245-
event->accept();
246-
}
247-
} else if (event->key() == Qt::Key_I) {
248-
circuitView->toggleInteractive();
249-
event->accept();
250-
}
251-
}
252-
253-
void CircuitViewWidget::keyReleaseEvent(QKeyEvent* event) { }
254-
255248
void CircuitViewWidget::mousePressEvent(QMouseEvent* event) {
256249
if (event->button() == Qt::LeftButton) {
257250
if (QGuiApplication::keyboardModifiers().testFlag(Qt::AltModifier)) {
@@ -295,13 +288,14 @@ void CircuitViewWidget::leaveEvent(QEvent* event) {
295288

296289
// save current circuit view widget we are viewing. Right now only works if it is the only widget in application.
297290
void CircuitViewWidget::save() {
298-
std::cout << "Trying to save\n";
299-
if (fileManager) {
300-
QString filePath = QFileDialog::getSaveFileName(this, "Save Circuit", "", "Circuit Files (*.cir);;All Files (*)");
301-
if (!filePath.isEmpty()) {
302-
fileManager->saveToFile(filePath.toStdString(), circuitView->getCircuit());
303-
}
304-
}
291+
logInfo("Trying to save Circuit");
292+
if (fileManager) {
293+
QString filePath = QFileDialog::getSaveFileName(this, "Save Circuit", "", "Circuit Files (*.cir);;All Files (*)");
294+
if (!filePath.isEmpty()) {
295+
fileManager->saveToFile(filePath.toStdString(), circuitView->getCircuit());
296+
logInfo("Successfully saved Circuit to: " + filePath.toStdString());
297+
}
298+
}
305299
}
306300

307301
// for drag and drop load directly onto this circuit view widget

src/gui/circuitViewWidget.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ class QTimer;
1212
#include "computerAPI/circuits/circuitFileManager.h"
1313
#include "circuitView/renderer/qtRenderer.h"
1414
#include "circuitView/circuitView.h"
15+
#include "keybinds/keybindManager.h"
1516
#include "ui_circuitViewUi.h"
1617
#include "util/vec2.h"
1718

1819
class CircuitViewWidget : public QWidget {
1920
Q_OBJECT
2021
public:
21-
CircuitViewWidget(QWidget* parent, Ui::CircuitViewUi* ui, CircuitFileManager* fileManager);
22+
CircuitViewWidget(QWidget* parent, Ui::CircuitViewUi* ui, CircuitFileManager* fileManager, KeybindManager* keybindManager);
2223

2324
// setup
2425
inline CircuitView* getCircuitView() { return circuitView.get(); }
@@ -37,8 +38,6 @@ class CircuitViewWidget : public QWidget {
3738
void paintEvent(QPaintEvent* event) override;
3839
void resizeEvent(QResizeEvent* event) override;
3940
void wheelEvent(QWheelEvent* event) override;
40-
void keyPressEvent(QKeyEvent* event) override;
41-
void keyReleaseEvent(QKeyEvent* event) override;
4241
void mousePressEvent(QMouseEvent* event) override;
4342
void mouseReleaseEvent(QMouseEvent* event) override;
4443
void mouseMoveEvent(QMouseEvent* event) override;
@@ -58,6 +57,8 @@ class CircuitViewWidget : public QWidget {
5857
std::unique_ptr<QtRenderer> renderer;
5958
CircuitFileManager* fileManager;
6059

60+
KeybindManager* keybindManager;
61+
6162
// update loop
6263
QTimer* updateLoopTimer;
6364
const float updateInterval = 0.008f;

src/gui/mainWindow.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ MainWindow::MainWindow(KDDockWidgets::MainWindowOptions options)
2525
setWindowTitle(tr("Gatality"));
2626
setWindowIcon(QIcon(":/gateIcon.ico"));
2727

28+
// set default keybinds
29+
keybindManager.setKeybind("Save", "Ctrl+S");
30+
keybindManager.setKeybind("Undo", "Z");
31+
keybindManager.setKeybind("Redo", "Y");
32+
keybindManager.setKeybind("BlockRotateCCW", "Q");
33+
keybindManager.setKeybind("BlockRotateCW", "E");
34+
keybindManager.setKeybind("ToggleInteractive", "I");
35+
2836
// create default circuit and evaluator
2937
logInfo("Creating default circuitViewWidget");
3038
circuit_id_t id = backend.createCircuit();
@@ -303,7 +311,7 @@ CircuitViewWidget* MainWindow::openNewCircuitViewWindow() {
303311
QWidget* w = new QWidget();
304312
Ui::CircuitViewUi* circuitViewUi = new Ui::CircuitViewUi();
305313
circuitViewUi->setupUi(w);
306-
CircuitViewWidget* circuitViewWidget = new CircuitViewWidget(w, circuitViewUi, &circuitFileManager);
314+
CircuitViewWidget* circuitViewWidget = new CircuitViewWidget(w, circuitViewUi, &circuitFileManager, &keybindManager);
307315
backend.linkCircuitView(circuitViewWidget->getCircuitView());
308316
circuitViews.push_back(circuitViewWidget);
309317
circuitViewUi->verticalLayout_2->addWidget(circuitViewWidget);

src/gui/mainWindow.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ class QGraphicsScene;
88
#include <kddockwidgets/DockWidget.h>
99

1010
#include "computerAPI/circuits/circuitFileManager.h"
11+
#include "circuitView/renderer/qtRenderer.h"
12+
#include "keybinds/keybindManager.h"
1113
#include "backend/backend.h"
1214

1315
namespace Ui {
@@ -48,6 +50,7 @@ class MainWindow : public KDDockWidgets::QtWidgets::MainWindow {
4850
QMenu* loadIntoSubMenu;
4951
QMenu* loadMergedSubMenu;
5052
Backend backend;
53+
KeybindManager keybindManager;
5154
std::vector<CircuitViewWidget*> circuitViews;
5255
std::unordered_map<QWidget*, CircuitViewWidget*> activeWidgets;
5356
CircuitFileManager circuitFileManager;

src/keybinds/keybindManager.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include "keybindManager.h"
2+
3+
void KeybindManager::setKeybind(const std::string& bindName, const std::string& keyString) {
4+
QKeySequence keySequence = QKeySequence(QString::fromStdString(keyString));
5+
auto keybind_itr = keybinds.find(bindName);
6+
if(keybind_itr == keybinds.end()) {
7+
// add a new keybind if it doesn't exist
8+
keybinds.insert({bindName, keySequence});
9+
} else {
10+
// change the old keybind if it already exists
11+
keybind_itr->second = keySequence;
12+
// update this keybind's QShortcuts to use the new key sequence
13+
auto shortcut_itr = shortcuts.find(bindName);
14+
if(shortcut_itr != shortcuts.end()) {
15+
for(auto shortcut : shortcut_itr->second) {
16+
shortcut->setKey(keySequence);
17+
}
18+
}
19+
}
20+
}
21+
22+
QShortcut* KeybindManager::createShortcut(const std::string& bindName, QWidget* parent) {
23+
// try to find this keybind's key sequence
24+
QKeySequence keySequence;
25+
auto keybind_itr = keybinds.find(bindName);
26+
if(keybind_itr == keybinds.end()) {
27+
logWarning("No keybind found matching bindname: " + bindName);
28+
return nullptr;
29+
}
30+
keySequence = keybind_itr->second;
31+
// create a new QShortcut that is activated by this keybind's key sequence
32+
QShortcut* shortcut = new QShortcut(keySequence, parent);
33+
shortcuts[bindName].push_back(shortcut);
34+
return shortcut;
35+
}

src/keybinds/keybindManager.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#ifndef keybindManager_h
2+
#define keybindManager_h
3+
4+
#include <QKeySequence>
5+
#include <QShortcut>
6+
#include <QWidget>
7+
8+
class KeybindManager {
9+
public:
10+
// give a keybind a name and set/reset the key sequence that triggers it
11+
void setKeybind(const std::string& bindName, const std::string& keyString);
12+
// create a QShortcut that is activated by its keybind's key sequence
13+
QShortcut* createShortcut(const std::string& bindName, QWidget* parent);
14+
private:
15+
std::unordered_map<std::string, QKeySequence> keybinds;
16+
std::unordered_map<std::string, std::vector<QShortcut*>> shortcuts;
17+
};
18+
19+
#endif /* keybindManager_h */

0 commit comments

Comments
 (0)