#include "controllersetup.h" #include "ui_controllersetup.h" #include "logcategories.h" controllerSetup::controllerSetup(QWidget* parent) : QDialog(parent), ui(new Ui::controllerSetup) { ui->setupUi(this); ui->tabWidget->clear(); ui->tabWidget->hide(); noControllersText = new QLabel("No USB controller found"); noControllersText->setStyleSheet("QLabel { color : gray; }"); ui->hboxLayout->addWidget(noControllersText); this->resize(this->sizeHint()); } controllerSetup::~controllerSetup() { delete ui; } void controllerSetup::hideEvent(QHideEvent *event) { qDebug(logUsbControl()) << "Controller window hideEvent()"; updateDialog->hide(); } void controllerSetup::on_tabWidget_currentChanged(int index) { if (updateDialog != Q_NULLPTR) updateDialog->hide(); } void controllerSetup::init() { updateDialog = new QDialog(this); // Not sure if I like it Frameless or not? updateDialog->setWindowFlags(Qt::FramelessWindowHint | Qt::Dialog); QGridLayout* udLayout = new QGridLayout; updateDialog->setLayout(udLayout); updateDialog->setBaseSize(1, 1); onLabel = new QLabel("On"); udLayout->addWidget(onLabel,0,0); onEvent = new QComboBox(); udLayout->addWidget(onEvent,0,1); onLabel->setBuddy(onEvent); offLabel = new QLabel("Off"); udLayout->addWidget(offLabel,1,0); offEvent = new QComboBox(); udLayout->addWidget(offEvent,1,1); offLabel->setBuddy(offEvent); knobLabel = new QLabel("Knob"); udLayout->addWidget(knobLabel,2,0); knobEvent = new QComboBox(); udLayout->addWidget(knobEvent,2,1); knobLabel->setBuddy(knobEvent); buttonLatch = new QCheckBox(); buttonLatch->setText("Toggle"); udLayout->addWidget(buttonLatch,3,0); QHBoxLayout* colorLayout = new QHBoxLayout(); udLayout->addLayout(colorLayout,3,1); buttonOnColor = new QPushButton("Color"); colorLayout->addWidget(buttonOnColor); buttonOffColor = new QPushButton("Pressed"); colorLayout->addWidget(buttonOffColor); buttonIcon = new QPushButton("Icon"); udLayout->addWidget(buttonIcon,4,0); iconLabel = new QLabel(""); udLayout->addWidget(iconLabel,4,1); udLayout->setAlignment(iconLabel,Qt::AlignCenter); udLayout->setSizeConstraint(QLayout::SetFixedSize); updateDialog->hide(); connect(offEvent, SIGNAL(currentIndexChanged(int)), this, SLOT(offEventIndexChanged(int))); connect(onEvent, SIGNAL(currentIndexChanged(int)), this, SLOT(onEventIndexChanged(int))); connect(knobEvent, SIGNAL(currentIndexChanged(int)), this, SLOT(knobEventIndexChanged(int))); connect(buttonOnColor, SIGNAL(clicked()), this, SLOT(buttonOnColorClicked())); connect(buttonOffColor, SIGNAL(clicked()), this, SLOT(buttonOffColorClicked())); connect(buttonIcon, SIGNAL(clicked()), this, SLOT(buttonIconClicked())); connect(buttonLatch, SIGNAL(stateChanged(int)), this, SLOT(latchStateChanged(int))); } void controllerSetup::mousePressed(controllerScene* scene, QPoint p) { Q_UNUSED (scene) // We might want it in the future? // Receive mouse event from the scene qDebug() << "Looking for knob or button at Point x=" << p.x() << " y=" << p.y(); QPoint gp = this->mapToGlobal(p); // Did the user click on a button? auto b = std::find_if(buttons->begin(), buttons->end(), [p, this](BUTTON& b) { return (b.parent != Q_NULLPTR && b.pos.contains(p) && b.page == b.parent->currentPage && ui->tabWidget->currentWidget()->objectName() == b.path ); }); if (b != buttons->end()) { currentButton = b; currentKnob = Q_NULLPTR; qDebug() << "Button" << currentButton->num << "On Event" << currentButton->onCommand->text << "Off Event" << currentButton->offCommand->text; updateDialog->setWindowTitle(QString("Update button %0").arg(b->num)); onEvent->blockSignals(true); onEvent->setCurrentIndex(onEvent->findData(currentButton->onCommand->index)); onEvent->show(); onLabel->show(); onEvent->blockSignals(false); offEvent->blockSignals(true); offEvent->setCurrentIndex(offEvent->findData(currentButton->offCommand->index)); offEvent->show(); offLabel->show(); offEvent->blockSignals(false); knobEvent->hide(); knobLabel->hide(); buttonLatch->blockSignals(true); buttonLatch->setChecked(currentButton->toggle); buttonLatch->blockSignals(false); buttonLatch->show(); switch (currentButton->parent->type.model) { case StreamDeckMini: case StreamDeckMiniV2: case StreamDeckOriginal: case StreamDeckOriginalV2: case StreamDeckOriginalMK2: case StreamDeckXL: case StreamDeckXLV2: case StreamDeckPlus: buttonOnColor->setStyleSheet(QString("background-color: %1").arg(currentButton->backgroundOn.name(QColor::HexArgb))); buttonOffColor->setStyleSheet(QString("background-color: %1").arg(currentButton->backgroundOff.name(QColor::HexArgb))); buttonOnColor->show(); buttonOffColor->show(); buttonIcon->show(); iconLabel->setText(currentButton->iconName); iconLabel->show(); break; default: buttonOnColor->hide(); buttonOffColor->hide(); buttonIcon->hide(); iconLabel->hide(); break; } updateDialog->show(); updateDialog->move(gp); updateDialog->adjustSize(); updateDialog->raise(); } else { // It wasn't a button so was it a knob? auto k = std::find_if(knobs->begin(), knobs->end(), [p, this](KNOB& k) { return (k.parent != Q_NULLPTR && k.pos.contains(p) && k.page == k.parent->currentPage && ui->tabWidget->currentWidget()->objectName() == k.path ); }); if (k != knobs->end()) { currentKnob = k; currentButton = Q_NULLPTR; qDebug() << "Knob" << currentKnob->num << "Event" << currentKnob->command->text; updateDialog->setWindowTitle(QString("Update knob %0").arg(k->num)); knobEvent->blockSignals(true); knobEvent->setCurrentIndex(knobEvent->findData(currentKnob->command->index)); knobEvent->show(); knobLabel->show(); knobEvent->blockSignals(false); onEvent->hide(); offEvent->hide(); onLabel->hide(); offLabel->hide(); buttonLatch->hide(); buttonOnColor->hide(); buttonOffColor->hide(); buttonIcon->hide(); iconLabel->hide(); updateDialog->show(); updateDialog->move(gp); updateDialog->adjustSize(); updateDialog->raise(); } else { // It wasn't either so hide the updateDialog(); updateDialog->hide(); currentButton = Q_NULLPTR; currentKnob = Q_NULLPTR; } } } void controllerSetup::onEventIndexChanged(int index) { Q_UNUSED(index); // If command is changed, delete current command and deep copy the new command if (currentButton != Q_NULLPTR && onEvent->currentData().toInt() < commands->size()) { QMutexLocker locker(mutex); if (currentButton->onCommand) delete currentButton->onCommand; currentButton->onCommand = new COMMAND(commands->at(onEvent->currentData().toInt())); currentButton->onText->setPlainText(currentButton->onCommand->text); currentButton->onText->setPos(currentButton->pos.center().x() - currentButton->onText->boundingRect().width() / 2, (currentButton->pos.center().y() - currentButton->onText->boundingRect().height() / 2)-6); // Signal that any button programming on the device should be completed. emit sendRequest(currentButton->parent,usbFeatureType::featureButton,currentButton->num,currentButton->onCommand->text,Q_NULLPTR,¤tButton->backgroundOn); } } void controllerSetup::offEventIndexChanged(int index) { Q_UNUSED(index); // If command is changed, delete current command and deep copy the new command if (currentButton != Q_NULLPTR && offEvent->currentData().toInt() < commands->size()) { QMutexLocker locker(mutex); if (currentButton->offCommand) delete currentButton->offCommand; currentButton->offCommand = new COMMAND(commands->at(offEvent->currentData().toInt())); currentButton->offText->setPlainText(currentButton->offCommand->text); currentButton->offText->setPos(currentButton->pos.center().x() - currentButton->offText->boundingRect().width() / 2, (currentButton->pos.center().y() - currentButton->offText->boundingRect().height() / 2)+6); } } void controllerSetup::knobEventIndexChanged(int index) { Q_UNUSED(index); // If command is changed, delete current command and deep copy the new command if (currentKnob != Q_NULLPTR && knobEvent->currentData().toInt() < commands->size()) { QMutexLocker locker(mutex); if (currentKnob->command) delete currentKnob->command; currentKnob->command = new COMMAND(commands->at(knobEvent->currentData().toInt())); currentKnob->text->setPlainText(currentKnob->command->text); currentKnob->text->setPos(currentKnob->pos.center().x() - currentKnob->text->boundingRect().width() / 2, (currentKnob->pos.center().y() - currentKnob->text->boundingRect().height() / 2)); } } void controllerSetup::buttonOnColorClicked() { QColorDialog::ColorDialogOptions options; options.setFlag(QColorDialog::ShowAlphaChannel, false); options.setFlag(QColorDialog::DontUseNativeDialog, false); QColor selColor = QColorDialog::getColor(currentButton->backgroundOn, this, "Select Color to use for unpressed button", options); if(selColor.isValid() && currentButton != Q_NULLPTR) { QMutexLocker locker(mutex); currentButton->backgroundOn = selColor; buttonOnColor->setStyleSheet(QString("background-color: %1").arg(currentButton->backgroundOn.name(QColor::HexArgb))); emit sendRequest(currentButton->parent,usbFeatureType::featureButton,currentButton->num,currentButton->onCommand->text,currentButton->icon,¤tButton->backgroundOn); } } void controllerSetup::buttonOffColorClicked() { QColorDialog::ColorDialogOptions options; options.setFlag(QColorDialog::ShowAlphaChannel, false); options.setFlag(QColorDialog::DontUseNativeDialog, false); QColor selColor = QColorDialog::getColor(currentButton->backgroundOff, this, "Select Color to use for pressed button", options); if(selColor.isValid() && currentButton != Q_NULLPTR) { QMutexLocker locker(mutex); currentButton->backgroundOff = selColor; buttonOffColor->setStyleSheet(QString("background-color: %1").arg(currentButton->backgroundOff.name(QColor::HexArgb))); } } void controllerSetup::buttonIconClicked() { QString file = QFileDialog::getOpenFileName(this,"Select Icon Filename",".","Images (*png *.jpg)"); if (!file.isEmpty()) { QFileInfo info = QFileInfo(file); currentButton->iconName = info.fileName(); iconLabel->setText(currentButton->iconName); QImage image; image.load(file); if (currentButton->icon != Q_NULLPTR) delete currentButton->icon; currentButton->icon = new QImage(image.scaled(currentButton->parent->type.iconSize,currentButton->parent->type.iconSize)); emit sendRequest(currentButton->parent,usbFeatureType::featureButton,currentButton->num,currentButton->onCommand->text,currentButton->icon, ¤tButton->backgroundOn); } } void controllerSetup::latchStateChanged(int state) { if (currentButton != Q_NULLPTR) { QMutexLocker locker(mutex); currentButton->toggle=(int)state; } } void controllerSetup::removeDevice(USBDEVICE* dev) { QMutexLocker locker(mutex); int remove = -1; for (int i = 0; i < ui->tabWidget->count(); i++) { auto widget = ui->tabWidget->widget(i); if (widget->objectName() == dev->path) { qDeleteAll(widget->findChildren("", Qt::FindDirectChildrenOnly)); remove = i; } } if (remove != -1) { qInfo(logUsbControl()) << "Removing tab" << dev->product; ui->tabWidget->removeTab(remove); } if (ui->tabWidget->count() == 0) { ui->tabWidget->hide(); noControllersText->show(); this->adjustSize(); } } void controllerSetup::newDevice(USBDEVICE* dev, CONTROLLER* cntrl, QVector