wfview/controllersetup.cpp

271 wiersze
8.6 KiB
C++
Czysty Zwykły widok Historia

2022-10-22 19:55:08 +00:00
#include "controllersetup.h"
#include "ui_controllersetup.h"
#include "logcategories.h"
2022-10-22 19:55:08 +00:00
controllerSetup::controllerSetup(QWidget* parent) :
QDialog(parent),
2022-10-22 19:55:08 +00:00
ui(new Ui::controllerSetup)
{
ui->setupUi(this);
2022-10-22 19:55:08 +00:00
scene = new controllerScene();
2022-04-22 10:11:21 +00:00
connect(scene, SIGNAL(mousePressed(QPoint)), this, SLOT(mousePressed(QPoint)));
ui->graphicsView->setScene(scene);
textItem = scene->addText("No USB controller found");
textItem->setDefaultTextColor(Qt::gray);
this->resize(this->sizeHint());
2022-04-22 10:11:21 +00:00
connect(&onEvent, SIGNAL(currentIndexChanged(int)), this, SLOT(onEventIndexChanged(int)));
connect(&offEvent, SIGNAL(currentIndexChanged(int)), this, SLOT(offEventIndexChanged(int)));
2023-02-09 13:21:51 +00:00
connect(&knobEvent, SIGNAL(currentIndexChanged(int)), this, SLOT(knobEventIndexChanged(int)));
}
2022-10-22 19:55:08 +00:00
controllerSetup::~controllerSetup()
{
delete textItem;
delete scene;
delete ui;
if (bgImage != Q_NULLPTR) {
delete bgImage;
}
}
2022-10-22 19:55:08 +00:00
void controllerSetup::mousePressed(QPoint p)
2022-04-22 10:11:21 +00:00
{
// Receive mouse event from the scene
qDebug() << "Looking for button Point x=" << p.x() << " y=" << p.y();
bool found = false;
for (BUTTON& b : *buttons)
{
2022-09-18 20:00:44 +00:00
if (b.dev == currentDevice && b.pos.contains(p))
2022-04-22 10:11:21 +00:00
{
found = true;
currentButton = &b;
2022-05-12 23:11:34 +00:00
qDebug() << "Button" << currentButton->num << "On Event" << currentButton->onCommand->text << "Off Event" << currentButton->offCommand->text;
2022-04-22 10:11:21 +00:00
// Add off event first so it doesn't obscure on event.
if (offEventProxy == Q_NULLPTR) {
offEventProxy = scene->addWidget(&offEvent);
}
if (onEventProxy == Q_NULLPTR) {
onEventProxy = scene->addWidget(&onEvent);
}
onEvent.blockSignals(true);
onEvent.move(p);
2023-02-09 13:21:51 +00:00
onEvent.setCurrentIndex(onEvent.findData(currentButton->onCommand->index));
2022-04-22 10:11:21 +00:00
onEvent.show();
onEvent.blockSignals(false);
p.setY(p.y() + 40);
offEvent.blockSignals(true);
offEvent.move(p);
2023-02-09 13:21:51 +00:00
offEvent.setCurrentIndex(offEvent.findData(currentButton->offCommand->index));
2022-04-22 10:11:21 +00:00
offEvent.show();
offEvent.blockSignals(false);
2023-02-09 13:21:51 +00:00
knobEvent.hide();
break;
}
}
if (!found) {
for (KNOB& k : *knobs)
{
if (k.dev == currentDevice && k.pos.contains(p))
{
found = true;
currentKnob = &k;
qDebug() << "Knob" << currentKnob->num << "Event" << currentKnob->command->text;
if (knobEventProxy == Q_NULLPTR) {
knobEventProxy = scene->addWidget(&knobEvent);
}
knobEvent.blockSignals(true);
knobEvent.move(p);
knobEvent.setCurrentIndex(knobEvent.findData(currentKnob->command->index));
knobEvent.show();
knobEvent.blockSignals(false);
onEvent.hide();
offEvent.hide();
break;
}
2022-04-22 10:11:21 +00:00
}
}
if (!found) {
onEvent.hide();
offEvent.hide();
2023-02-09 13:21:51 +00:00
knobEvent.hide();
2022-04-22 10:11:21 +00:00
}
}
2022-10-22 19:55:08 +00:00
void controllerSetup::onEventIndexChanged(int index) {
2023-02-09 13:21:51 +00:00
Q_UNUSED(index);
if (currentButton != Q_NULLPTR && onEvent.currentData().toInt() < commands->size()) {
currentButton->onCommand = &commands->at(onEvent.currentData().toInt());
2022-04-27 11:56:54 +00:00
currentButton->onText->setPlainText(currentButton->onCommand->text);
2022-04-22 10:11:21 +00:00
}
}
2022-10-22 19:55:08 +00:00
void controllerSetup::offEventIndexChanged(int index) {
2023-02-09 13:21:51 +00:00
Q_UNUSED(index);
if (currentButton != Q_NULLPTR && offEvent.currentData().toInt() < commands->size()) {
currentButton->offCommand = &commands->at(offEvent.currentData().toInt());
2022-04-27 11:56:54 +00:00
currentButton->offText->setPlainText(currentButton->offCommand->text);
2022-04-22 10:11:21 +00:00
}
}
2023-02-09 13:21:51 +00:00
void controllerSetup::knobEventIndexChanged(int index) {
Q_UNUSED(index);
if (currentKnob != Q_NULLPTR && knobEvent.currentData().toInt() < commands->size()) {
currentKnob->command = &commands->at(knobEvent.currentData().toInt());
currentKnob->text->setPlainText(currentKnob->command->text);
}
}
2022-04-22 10:11:21 +00:00
2023-02-09 13:21:51 +00:00
void controllerSetup::newDevice(unsigned char devType, QVector<BUTTON>* but, QVector<KNOB>* kb, QVector<COMMAND>* cmd)
{
2022-04-22 10:11:21 +00:00
buttons = but;
2023-02-09 13:21:51 +00:00
knobs = kb;
2022-04-25 16:40:41 +00:00
commands = cmd;
// Remove any existing button text:
for (QGraphicsItem* item : scene->items())
{
QGraphicsTextItem* txt = qgraphicsitem_cast<QGraphicsTextItem*>(item);
if (!txt || txt==textItem)
continue;
scene->removeItem(txt);
}
if (bgImage != Q_NULLPTR) {
scene->removeItem(bgImage);
delete bgImage;
bgImage = Q_NULLPTR;
2023-02-09 13:21:51 +00:00
if (onEventProxy != Q_NULLPTR) {
2022-04-22 10:11:21 +00:00
scene->removeItem(onEventProxy);
2023-02-09 13:21:51 +00:00
onEventProxy = Q_NULLPTR;
}
if (offEventProxy != Q_NULLPTR) {
2022-04-22 10:11:21 +00:00
scene->removeItem(offEventProxy);
2023-02-09 13:21:51 +00:00
offEventProxy = Q_NULLPTR;
}
if (knobEventProxy != Q_NULLPTR) {
scene->removeItem(knobEventProxy);
knobEventProxy = Q_NULLPTR;
}
}
QImage image;
switch (devType) {
case shuttleXpress:
2023-01-25 21:37:12 +00:00
image.load(":/resources/shuttlexpress.png");
2022-05-17 07:53:24 +00:00
deviceName = "shuttleXpress";
break;
case shuttlePro2:
2023-01-25 21:37:12 +00:00
image.load(":/resources/shuttlepro.png");
2022-05-17 07:53:24 +00:00
deviceName = "shuttlePro2";
break;
case RC28:
image.load(":/resources/rc28.png");
2022-05-17 07:53:24 +00:00
deviceName = "RC28";
break;
2022-10-22 19:55:08 +00:00
case xBoxGamepad:
2022-10-22 22:23:30 +00:00
image.load(":/resources/xbox.png");
2022-10-22 19:55:08 +00:00
deviceName = "XBox";
break;
2023-02-07 18:41:02 +00:00
case eCoderPlus:
image.load(":/resources/ecoder.png");
deviceName = "eCoderPlus";
break;
default:
textItem->show();
ui->graphicsView->setSceneRect(scene->itemsBoundingRect());
this->adjustSize();
return;
}
textItem->hide();
bgImage = new QGraphicsPixmapItem(QPixmap::fromImage(image));
scene->addItem(bgImage);
2022-10-22 22:43:49 +00:00
ui->graphicsView->setMinimumSize(bgImage->boundingRect().width() + 100, bgImage->boundingRect().height() + 2);
2022-04-22 10:11:21 +00:00
currentDevice = devType;
2022-04-25 16:40:41 +00:00
onEvent.blockSignals(true);
offEvent.blockSignals(true);
2023-02-09 13:21:51 +00:00
knobEvent.blockSignals(true);
2022-12-29 00:06:03 +00:00
onEvent.clear();
offEvent.clear();
2023-02-09 13:21:51 +00:00
knobEvent.clear();
2022-12-29 00:06:03 +00:00
onEvent.setMaxVisibleItems(5);
offEvent.setMaxVisibleItems(5);
2023-02-09 13:21:51 +00:00
knobEvent.setMaxVisibleItems(5);
onEvent.view()->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
offEvent.view()->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
2023-02-09 13:21:51 +00:00
knobEvent.view()->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
2022-12-29 00:06:03 +00:00
onEvent.setStyleSheet("combobox-popup: 0;");
offEvent.setStyleSheet("combobox-popup: 0;");
2023-02-09 13:21:51 +00:00
knobEvent.setStyleSheet("combobox-popup: 0;");
2022-04-25 16:40:41 +00:00
for (COMMAND &c : *commands) {
2023-02-09 13:21:51 +00:00
if (c.cmdType == commandButton || c.text == "None") {
onEvent.addItem(c.text,c.index);
offEvent.addItem(c.text,c.index);
}
if (c.cmdType == commandKnob || c.text == "None") {
knobEvent.addItem(c.text,c.index);
}
2022-04-25 16:40:41 +00:00
}
2023-02-09 13:21:51 +00:00
2022-04-25 16:40:41 +00:00
onEvent.blockSignals(false);
offEvent.blockSignals(false);
2023-02-09 13:21:51 +00:00
knobEvent.blockSignals(false);
2022-04-25 16:40:41 +00:00
2022-04-22 10:11:21 +00:00
// Set button text
for (BUTTON& b : *buttons)
{
2022-09-18 20:00:44 +00:00
if (b.dev == currentDevice) {
b.onText = new QGraphicsTextItem(b.onCommand->text);
b.onText->setDefaultTextColor(b.textColour);
scene->addItem(b.onText);
b.onText->setPos(b.pos.x(), b.pos.y());
b.offText = new QGraphicsTextItem(b.offCommand->text);
b.offText->setDefaultTextColor(b.textColour);
scene->addItem(b.offText);
b.offText->setPos(b.pos.x(), b.pos.y() + 10);
}
2022-04-22 10:11:21 +00:00
}
2023-02-09 13:21:51 +00:00
// Set knob text
for (KNOB& k : *knobs)
{
if (k.dev == currentDevice) {
k.text = new QGraphicsTextItem(k.command->text);
k.text->setDefaultTextColor(k.textColour);
scene->addItem(k.text);
k.text->setPos(k.pos.x(), k.pos.y());
}
}
ui->graphicsView->setSceneRect(scene->itemsBoundingRect());
ui->graphicsView->resize(ui->graphicsView->sizeHint());
//this->resize(this->sizeHint());
this->adjustSize();
}
void controllerSetup::receiveSensitivity(int val)
{
ui->sensitivitySlider->blockSignals(true);
ui->sensitivitySlider->setValue(val);
ui->sensitivitySlider->blockSignals(false);
}
void controllerSetup::on_sensitivitySlider_valueChanged(int val)
{
emit sendSensitivity(val);
2023-02-07 18:41:02 +00:00
}