add joysick support and state feedback

This commit is contained in:
2022-02-01 19:54:07 +01:00
parent 840cec975d
commit 1f36522492
21 changed files with 2940 additions and 32 deletions

View File

@ -3,6 +3,7 @@
#include "ui_relayscrollbox.h"
#include "../items/train.h"
#include "../items/turnout.h"
#include "../trainjs.h"
ItemScrollBox::ItemScrollBox(QWidget *parent) :
QWidget(parent),
@ -37,6 +38,16 @@ void ItemScrollBox::addItem(std::weak_ptr<Item> item)
widgets_.back()->setShortcuts(QKeySequence(Qt::Key_R), QKeySequence(Qt::Key_F), QKeySequence(Qt::Key_V));
else if(train->getTrainId() == 4)
widgets_.back()->setShortcuts(QKeySequence(Qt::Key_T), QKeySequence(Qt::Key_G), QKeySequence(Qt::Key_B));
std::vector<std::shared_ptr<TrainJs>> joysticks = TrainJs::getJsDevices();
for(auto joystick: joysticks)
{
if(!joystick->itemIsSet())
{
joystick->setItem(item);
break;
}
}
}
else if(turnout)
{

View File

@ -14,21 +14,23 @@ ItemWidget::ItemWidget(std::weak_ptr<Item> item, QWidget *parent) :
{
ui->setupUi(this);
if(auto workingRelay = item_.lock())
if(auto workingItem = item_.lock())
{
ui->label->setText(workingRelay->getName());
ui->label->setText(workingItem->getName());
connect(ui->slider, &QSlider::valueChanged, this, &ItemWidget::moveToValue);
connect(ui->slider, &QSlider::valueChanged, this, &ItemWidget::setValue);
connect(ui->checkBox_f1, &QCheckBox::stateChanged, this, &ItemWidget::f1);
connect(ui->checkBox_f2, &QCheckBox::stateChanged, this, &ItemWidget::f2);
connect(ui->checkBox_f3, &QCheckBox::stateChanged, this, &ItemWidget::f3);
connect(ui->checkBox_f4, &QCheckBox::stateChanged, this, &ItemWidget::f4);
connect(ui->pushButton_reverse, &QPushButton::clicked, this, &ItemWidget::reverse);
connect(ui->radioButton_left, &QRadioButton::clicked, this, [this](){moveToValue(0);});
connect(ui->radioButton_right, &QRadioButton::clicked, this, [this](){moveToValue(1);});
connect(ui->radioButton_left, &QRadioButton::clicked, this, [this](){setValue(0);});
connect(ui->radioButton_right, &QRadioButton::clicked, this, [this](){setValue(1);});
Train* train = dynamic_cast<Train*>(workingRelay.get());
Turnout* turnout = dynamic_cast<Turnout*>(workingRelay.get());
connect(workingItem.get(), &Item::valueChanged, this, &ItemWidget::moveToValue);
Train* train = dynamic_cast<Train*>(workingItem.get());
Turnout* turnout = dynamic_cast<Turnout*>(workingItem.get());
if(turnout)
{
@ -69,7 +71,16 @@ void ItemWidget::deleteItem()
}
}
void ItemWidget::moveToValue(int value)
void ItemWidget::setValue(uint8_t value)
{
moveToValue(value);
if(auto workingItem = item_.lock())
workingItem->setValue(value);
else
disable();
}
void ItemWidget::moveToValue(uint8_t value)
{
ui->slider->blockSignals(true);
ui->radioButton_left->blockSignals(true);
@ -79,9 +90,6 @@ void ItemWidget::moveToValue(int value)
ui->slider->setValue(value);
ui->radioButton_left->setChecked(!value);
ui->radioButton_right->setChecked(value);
if(auto workingItem = item_.lock())
workingItem->setValue(value);
else disable();
ui->slider->blockSignals(false);
ui->radioButton_left->blockSignals(false);
@ -127,7 +135,7 @@ void ItemWidget::reverse()
else
{
qDebug()<<"!((bool)workingItem->getValue()) "<<!((bool)workingItem->getValue());
moveToValue(!((bool)workingItem->getValue()));
setValue(!((bool)workingItem->getValue()));
}
}
else disable();
@ -158,17 +166,17 @@ bool ItemWidget::controles(const ItemData& relay)
void ItemWidget::stepUp()
{
moveToValue(ui->slider->value()+1);
setValue(ui->slider->value()+1);
}
void ItemWidget::stepDown()
{
if(ui->slider->value() == 0)
{
moveToValue(0);
setValue(0);
return;
}
moveToValue(ui->slider->value()-1);
setValue(ui->slider->value()-1);
}
void ItemWidget::setShortcuts(QKeySequence up, QKeySequence down, QKeySequence rev)

View File

@ -26,7 +26,8 @@ signals:
void deleteRequest(const ItemData& item);
private slots:
void moveToValue(int value);
void setValue(uint8_t value);
void moveToValue(uint8_t value);
void deleteItem();
void stepUp();