From cad3bb9418020ff9583d25d688b397afd88f6fea Mon Sep 17 00:00:00 2001 From: Phil Taylor Date: Fri, 10 Feb 2023 00:27:01 +0000 Subject: [PATCH] Various (I hope) improvements! --- controllersetup.cpp | 219 +++++++++++++++++++++++++++----------------- controllersetup.h | 6 +- wfmain.cpp | 15 ++- 3 files changed, 150 insertions(+), 90 deletions(-) diff --git a/controllersetup.cpp b/controllersetup.cpp index 4d161ba..5fb65f4 100644 --- a/controllersetup.cpp +++ b/controllersetup.cpp @@ -13,27 +13,59 @@ controllerSetup::controllerSetup(QWidget* parent) : textItem = scene->addText("No USB controller found"); textItem->setDefaultTextColor(Qt::gray); this->resize(this->sizeHint()); - - connect(&onEvent, SIGNAL(currentIndexChanged(int)), this, SLOT(onEventIndexChanged(int))); - connect(&offEvent, SIGNAL(currentIndexChanged(int)), this, SLOT(offEventIndexChanged(int))); - connect(&knobEvent, SIGNAL(currentIndexChanged(int)), this, SLOT(knobEventIndexChanged(int))); } controllerSetup::~controllerSetup() { + // Remove any existing button text: + for (QGraphicsItem* item : scene->items()) + { + QGraphicsTextItem* txt = qgraphicsitem_cast(item); + if (!txt || txt == textItem) + continue; + scene->removeItem(txt); + delete txt; + } + + if (bgImage != Q_NULLPTR) { + scene->removeItem(bgImage); + delete bgImage; + bgImage = Q_NULLPTR; + } + if (onEventProxy != Q_NULLPTR) { + scene->removeItem(onEventProxy); + onEventProxy = Q_NULLPTR; + delete onEvent; + onEvent = Q_NULLPTR; + } + if (offEventProxy != Q_NULLPTR) { + scene->removeItem(offEventProxy); + offEventProxy = Q_NULLPTR; + delete offEvent; + offEvent = Q_NULLPTR; + } + if (knobEventProxy != Q_NULLPTR) { + scene->removeItem(knobEventProxy); + knobEventProxy = Q_NULLPTR; + delete knobEvent; + knobEvent = Q_NULLPTR; + } + delete textItem; delete scene; delete ui; - - if (bgImage != Q_NULLPTR) { - delete bgImage; - } } void controllerSetup::mousePressed(QPoint p) { // Receive mouse event from the scene qDebug() << "Looking for button Point x=" << p.x() << " y=" << p.y(); + if (onEvent == Q_NULLPTR|| offEvent == Q_NULLPTR|| knobEvent == Q_NULLPTR) + { + qInfo(logUsbControl()) << "Event missing, cannot continue..."; + return; + } + bool found = false; for (BUTTON& b : *buttons) { @@ -42,29 +74,23 @@ void controllerSetup::mousePressed(QPoint p) found = true; currentButton = &b; qDebug() << "Button" << currentButton->num << "On Event" << currentButton->onCommand->text << "Off Event" << currentButton->offCommand->text; - // 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); - onEvent.setCurrentIndex(onEvent.findData(currentButton->onCommand->index)); - onEvent.show(); - onEvent.blockSignals(false); + onEvent->blockSignals(true); + onEvent->move(p); + onEvent->setCurrentIndex(onEvent->findData(currentButton->onCommand->index)); + onEvent->show(); + onEvent->blockSignals(false); p.setY(p.y() + 40); - offEvent.blockSignals(true); - offEvent.move(p); - offEvent.setCurrentIndex(offEvent.findData(currentButton->offCommand->index)); - offEvent.show(); - offEvent.blockSignals(false); - knobEvent.hide(); + offEvent->blockSignals(true); + offEvent->move(p); + offEvent->setCurrentIndex(offEvent->findData(currentButton->offCommand->index)); + offEvent->show(); + offEvent->blockSignals(false); + knobEvent->hide(); break; } } + if (!found) { for (KNOB& k : *knobs) { @@ -73,32 +99,29 @@ void controllerSetup::mousePressed(QPoint 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(); + knobEvent->blockSignals(true); + knobEvent->move(p); + knobEvent->setCurrentIndex(knobEvent->findData(currentKnob->command->index)); + knobEvent->show(); + knobEvent->blockSignals(false); + onEvent->hide(); + offEvent->hide(); break; } } } if (!found) { - onEvent.hide(); - offEvent.hide(); - knobEvent.hide(); + onEvent->hide(); + offEvent->hide(); + knobEvent->hide(); } } void controllerSetup::onEventIndexChanged(int index) { Q_UNUSED(index); - if (currentButton != Q_NULLPTR && onEvent.currentData().toInt() < commands->size()) { - currentButton->onCommand = &commands->at(onEvent.currentData().toInt()); + if (currentButton != Q_NULLPTR && onEvent->currentData().toInt() < commands->size()) { + currentButton->onCommand = &commands->at(onEvent->currentData().toInt()); currentButton->onText->setPlainText(currentButton->onCommand->text); } } @@ -106,16 +129,16 @@ void controllerSetup::onEventIndexChanged(int index) { void controllerSetup::offEventIndexChanged(int index) { Q_UNUSED(index); - if (currentButton != Q_NULLPTR && offEvent.currentData().toInt() < commands->size()) { - currentButton->offCommand = &commands->at(offEvent.currentData().toInt()); + if (currentButton != Q_NULLPTR && offEvent->currentData().toInt() < commands->size()) { + currentButton->offCommand = &commands->at(offEvent->currentData().toInt()); currentButton->offText->setPlainText(currentButton->offCommand->text); } } void controllerSetup::knobEventIndexChanged(int index) { Q_UNUSED(index); - if (currentKnob != Q_NULLPTR && knobEvent.currentData().toInt() < commands->size()) { - currentKnob->command = &commands->at(knobEvent.currentData().toInt()); + if (currentKnob != Q_NULLPTR && knobEvent->currentData().toInt() < commands->size()) { + currentKnob->command = &commands->at(knobEvent->currentData().toInt()); currentKnob->text->setPlainText(currentKnob->command->text); } } @@ -134,25 +157,33 @@ void controllerSetup::newDevice(unsigned char devType, QVector