143 lines
4.5 KiB
C++
143 lines
4.5 KiB
C++
#include "qmacrobindswindow.h"
|
|
#include "./ui_qmacrobindswindow.h"
|
|
|
|
#include "QtImprovements/qsettingsplus.h"
|
|
#include "Widgets/qdialbutton.h"
|
|
#include "Widgets/qmacrobutton.h"
|
|
#include "qmacrokeysmanager.h"
|
|
#include "windowfinderdialogue.h"
|
|
|
|
#include <QCryptographicHash>
|
|
#include <QDir>
|
|
#include <QKeyEvent>
|
|
#include <QSettings>
|
|
#include <QStandardPaths>
|
|
#include <QUrl>
|
|
|
|
#include <QDebug>
|
|
|
|
|
|
QMacroBindsWindow::QMacroBindsWindow(QWidget *parent)
|
|
: QMainWindow(parent)
|
|
, ui(new Ui::QMacroBindsWindow)
|
|
{
|
|
ui->setupUi(this);
|
|
|
|
this->vButtonWidgets.clear();
|
|
for (qint8 row = DEFAULT_ROW_COUNT - 1; row >= 0; row--) {
|
|
QVector<QPushButton *> buttonRow;
|
|
for (qint8 column = 0; column < DEFAULT_COLUMN_COUNT; column++) {
|
|
QMacroButton *button = new QMacroButton(row, column);
|
|
QObject::connect(button,
|
|
&QRatioButton::pressedRowColumn,
|
|
this,
|
|
&QMacroBindsWindow::showKeyBindingWindow);
|
|
ui->gridButtons->addWidget(button, DEFAULT_ROW_COUNT - 1 - row, column);
|
|
buttonRow.append(button);
|
|
}
|
|
this->vButtonWidgets.insert(0, buttonRow);
|
|
}
|
|
|
|
QVector<QPushButton *> dialRow;
|
|
const quint8 dialButtonsCount = 4;
|
|
const quint8 dialColumnCount = 2;
|
|
const qint8 dialRowCount = ceil(dialButtonsCount / (float)dialColumnCount);
|
|
for (qint8 i = 0; i < dialButtonsCount; i++) {
|
|
|
|
const quint8 dialRowNumber = ((dialRowCount - 1 - (i % dialRowCount)) % dialRowCount) * 2;
|
|
const quint8 dialColumnNumber = (dialButtonsCount % 2 == 0) ? (i / dialRowCount) * 2 : i;
|
|
|
|
QDialButton *button = new QDialButton(this->vButtonWidgets.size(), dialRow.size());
|
|
|
|
QObject::connect(button,
|
|
&QRatioButton::pressedRowColumn,
|
|
this,
|
|
&QMacroBindsWindow::showKeyBindingWindow);
|
|
|
|
ui->gridDials->addWidget(button,
|
|
dialRowNumber,
|
|
dialColumnNumber,
|
|
2, 2);
|
|
|
|
dialRow.push_back(button);
|
|
}
|
|
this->vButtonWidgets.push_back(dialRow);
|
|
|
|
this->mkmMacroKeys = new QMacroKeysManager(this);
|
|
QObject::connect(this->mkmMacroKeys,
|
|
&QMacroKeysManager::newDeviceFound,
|
|
this,
|
|
&QMacroBindsWindow::addDeviceToList);
|
|
QObject::connect(this->mkmMacroKeys, &QMacroKeysManager::newButtonState, this, &QMacroBindsWindow::newButtonState);
|
|
QObject::connect(this->mkmMacroKeys, &QMacroKeysManager::newEncoderRotation, this, &QMacroBindsWindow::newEncoderRotation);
|
|
this->mkmMacroKeys->initDevices();
|
|
}
|
|
|
|
QMacroBindsWindow::~QMacroBindsWindow()
|
|
{
|
|
delete ui;
|
|
}
|
|
|
|
|
|
void QMacroBindsWindow::addDeviceToList(const QString &devicePath)
|
|
{
|
|
QSettings deviceNameDatabase = QSettingsPlus::createQSettings(this);
|
|
QString deviceName = deviceNameDatabase.value(devicePath).toString();
|
|
if (deviceName.isEmpty()) {
|
|
const size_t keyCount = deviceNameDatabase.allKeys().size() + 1;
|
|
deviceName = QString("Device %1").arg(keyCount);
|
|
deviceNameDatabase.setValue(devicePath, deviceName);
|
|
}
|
|
ui->comboMacroKeysList->addItem(deviceName);
|
|
ui->comboMacroKeysList->setEnabled(true);
|
|
}
|
|
|
|
void QMacroBindsWindow::deviceNameChanged(const QString &newName)
|
|
{
|
|
QSettings deviceNameDatabase = QSettingsPlus::createQSettings(this);
|
|
QStringList devicePaths = deviceNameDatabase.allKeys();
|
|
const QString &oldName = this->sLastSelectedDevice;
|
|
for (const QString &devicePath : devicePaths) {
|
|
if (deviceNameDatabase.value(devicePath) == oldName) {
|
|
deviceNameDatabase.setValue(devicePath, newName);
|
|
break;
|
|
}
|
|
}
|
|
this->sLastSelectedDevice = newName;
|
|
}
|
|
|
|
void QMacroBindsWindow::newButtonState(const quint8 &row, const quint8 &column, const bool &state)
|
|
{
|
|
const quint8 &availableRows = this->vButtonWidgets.size();
|
|
|
|
if (row < availableRows) {
|
|
if (column < this->vButtonWidgets[row].size()) {
|
|
this->vButtonWidgets[row][column]->setCheckable(true);
|
|
this->vButtonWidgets[row][column]->setChecked(state);
|
|
WindowFinder::sendInputToFrontmostWindow(QKeySequence(tr("Shift+Z")), state ? KeyEventType::PRESS : KeyEventType::RELEASE);
|
|
}
|
|
}
|
|
}
|
|
|
|
void QMacroBindsWindow::newEncoderRotation(const quint8 &column, const EncoderRotation &direction)
|
|
{
|
|
const QVector<QPushButton *> &encoderButtons = this->vButtonWidgets.last();
|
|
if (column < encoderButtons.size()) {
|
|
WindowFinder::sendInputToFrontmostWindow(direction == EncoderRotation::CCW ? QKeySequence(tr("Shift+Y")) : QKeySequence(tr("Shift+X")), KeyEventType::PRESS_AND_RELEASE);
|
|
}
|
|
}
|
|
|
|
|
|
void QMacroBindsWindow::showKeyBindingWindow(const quint8 &row, const quint8 &column)
|
|
{
|
|
this->vButtonWidgets[row][column]->setCheckable(false);
|
|
qDebug() << row << ":" << column;
|
|
}
|
|
|
|
|
|
void QMacroBindsWindow::showWindowFinder()
|
|
{
|
|
this->diagWindowFinder = new WindowFinderDialogue(this);
|
|
this->diagWindowFinder->show();
|
|
}
|