Skip to content

Commit

Permalink
Merge branch 'EdgeTX:main' into updated_SE_translations
Browse files Browse the repository at this point in the history
  • Loading branch information
ulfhedlund authored Apr 23, 2023
2 parents 4dbb490 + 8d86fc8 commit 4225398
Show file tree
Hide file tree
Showing 11 changed files with 513 additions and 270 deletions.
15 changes: 9 additions & 6 deletions companion/src/apppreferencesdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,13 @@ void AppPreferencesDialog::accept()
if (ui->joystickChkB ->isChecked()) {
g.jsSupport(ui->joystickChkB ->isChecked());
// Don't overwrite selected joystick if not connected. Avoid surprising the user.
if (ui->joystickCB->isEnabled())
g.jsCtrl(ui->joystickCB ->currentIndex());
if (ui->joystickCB->isEnabled()) {
profile.jsName(ui->joystickCB->currentText());
g.loadNamedJS();
}
}
else {
g.jsSupport(false);
g.jsCtrl(0);
}

// Updates tab
Expand Down Expand Up @@ -287,7 +288,8 @@ void AppPreferencesDialog::initSettings()
}
ui->joystickCB->clear();
ui->joystickCB->insertItems(0, joystickNames);
ui->joystickCB->setCurrentIndex(g.jsCtrl());
int stick = joystick->findCurrent(g.currentProfile().jsName());
ui->joystickCB->setCurrentIndex(stick);
}
else {
ui->joystickCB->clear();
Expand Down Expand Up @@ -614,8 +616,9 @@ void AppPreferencesDialog::on_joystickChkB_clicked() {
}

void AppPreferencesDialog::on_joystickcalButton_clicked() {
joystickDialog * jd=new joystickDialog(this, ui->joystickCB->currentIndex());
jd->exec();
g.currentProfile().jsName(ui->joystickCB->currentText());
joystickDialog * jd = new joystickDialog(this);
jd->exec();
}
#endif

Expand Down
10 changes: 10 additions & 0 deletions companion/src/simulation/joystick.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,13 @@ int Joystick::getAxisValue(int axis)
} else
return 0;
}

int Joystick::findCurrent(QString jsName)
{
for (int i = 0; i < joystickNames.size(); i += 1) {
if (joystickNames[i] == jsName) {
return i;
}
}
return 0;
}
2 changes: 2 additions & 0 deletions companion/src/simulation/joystick.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ class Joystick : public QObject
}
int getAxisValue(int);

int findCurrent(QString jsName);

private:
QMap<int, Sint16> axes;
QMap<int, Uint8> buttons;
Expand Down
141 changes: 86 additions & 55 deletions companion/src/simulation/joystickdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,65 +25,106 @@
#include "boards.h"
#include "constants.h"

joystickDialog::joystickDialog(QWidget *parent, int stick) :
joystickDialog::joystickDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::joystickDialog),
step(0),
numAxes(0),
numButtons(0),
started(false)
{
ui->setupUi(this);

int i;
char s[20];

for (i = 0; i < MAX_JS_AXES; i += 1) {
jscal[i][0] = 32767;
jscal[i][1] = 0;
jscal[i][2] = -32767;
jscal[i][2] = -32768;
}

ui->backButton->setEnabled(false);

ui->joystickChkB->setChecked(g.jsSupport());

if (loadJoysticks()) {
joystickSetEnabled(ui->joystickChkB->isChecked());
joystickOpen(ui->joystickCB->currentIndex());
}
else {
joystickSetEnabled(false);
}
loadStep();

connect(joystick, SIGNAL(axisValueChanged(int, int)), this, SLOT(onjoystickAxisValueChanged(int, int)));
connect(joystick, SIGNAL(buttonValueChanged(int, bool)), this, SLOT(onjoystickButtonValueChanged(int, bool)));
connect(ui->joystickCB, SIGNAL(currentIndexChanged(int)), this, SLOT(joystickOpen(int)));
connect(ui->joystickChkB, SIGNAL(toggled(bool)), this, SLOT(joystickSetEnabled(bool)));
}

joystickDialog::~joystickDialog()
{
delete ui;
}

void joystickDialog::loadGrid()
{
int i;
char s[20];

QGridLayout *grid = findChild<QGridLayout*>("gridLayout");

QLayoutItem* item;
while ((item = grid->takeAt(0)) != NULL)
{
delete item->widget();
delete item;
}

memset(sliders, 0, sizeof(sliders));
memset(sticks, 0, sizeof(sliders));
memset(invert, 0, sizeof(sliders));

int row = 0;
int col = 0;
if (grid) {
for (i = 0; i < MAX_JS_AXES; i += 1, row += 1) {
for (i = 0; i < numAxes; i += 1, row += 1) {
col = (i & 1) * 4;
sprintf(s, "Ch%d", i + 1);
QLabel *l = new QLabel(s);
grid->addWidget(l, row/2, col+0, 1, 1);
QSlider *s = new QSlider(Qt::Horizontal);
s->setMinimum(-32767);
s->setMaximum(32767);
sliders[row] = s;
sliders[i] = s;
grid->addWidget(s, row/2, col+1, 1, 1);
QCheckBox *c = new QCheckBox("");
invert[row] = c;
invert[i] = c;
grid->addWidget(c, row/2, col+2, 1, 1);
QComboBox *d = new QComboBox();
populateSourceCombo(d);
sticks[row] = d;
sticks[i] = d;
grid->addWidget(d, row/2, col+3, 1, 1);
}
if (row & 1) row += 1;
for (i = 0; i < MAX_JS_BUTTONS; i += 1, row += 1) {
for (i = 0; i < numButtons; i += 1, row += 1) {
col = (i & 1) * 4;
sprintf(s, "Btn%d", i + 1);
QLabel *l = new QLabel(s);
grid->addWidget(l, row/2, col+0, 1, 1);
QSlider *s = new QSlider(Qt::Horizontal);
s->setMinimum(0);
s->setMaximum(1);
sliders[row] = s;
sliders[i+numAxes] = s;
grid->addWidget(s, row/2, col+1, 1, 1);
QComboBox *d = new QComboBox();
populateButtonCombo(d);
sticks[row] = d;
sticks[i+numAxes] = d;
grid->addWidget(d, row/2, col+3, 1, 1);
}
}

for (int i = 0; i < MAX_JS_AXES; ++i) {
for (int i = 0; i < numAxes; ++i) {
if (g.joystick[i].existsOnDisk()) {
jscal[i][0] = g.joystick[i].stick_min();
jscal[i][1] = g.joystick[i].stick_med();
Expand All @@ -95,37 +136,11 @@ joystickDialog::joystickDialog(QWidget *parent, int stick) :
}
}

for (int i = 0; i < MAX_JS_BUTTONS; ++i) {
for (int i = 0; i < numButtons; ++i) {
if (g.jsButton[i].existsOnDisk()) {
sticks[i+MAX_JS_AXES]->setCurrentIndex(sticks[i+MAX_JS_AXES]->findData(g.jsButton[i].button_idx()));
sticks[i+numAxes]->setCurrentIndex(sticks[i+numAxes]->findData(g.jsButton[i].button_idx()));
}
}

ui->backButton->setEnabled(false);

ui->joystickChkB->setChecked(g.jsSupport() || stick > -1);

if (stick < 0)
stick = g.jsCtrl();

if (loadJoysticks(stick)) {
joystickSetEnabled(ui->joystickChkB->isChecked());
joystickOpen(ui->joystickCB->currentIndex());
}
else {
joystickSetEnabled(false);
}
loadStep();

connect(joystick, SIGNAL(axisValueChanged(int, int)), this, SLOT(onjoystickAxisValueChanged(int, int)));
connect(joystick, SIGNAL(buttonValueChanged(int, bool)), this, SLOT(onjoystickButtonValueChanged(int, bool)));
connect(ui->joystickCB, SIGNAL(currentIndexChanged(int)), this, SLOT(joystickOpen(int)));
connect(ui->joystickChkB, SIGNAL(toggled(bool)), this, SLOT(joystickSetEnabled(bool)));
}

joystickDialog::~joystickDialog()
{
delete ui;
}

void joystickDialog::populateSourceCombo(QComboBox * cb)
Expand Down Expand Up @@ -194,16 +209,16 @@ void joystickDialog::populateButtonCombo(QComboBox * cb)
for (i = 0; i < ttlTrims; i += 1) {
wname = RawSource(RawSourceType::SOURCE_TYPE_TRIM, i).toString(nullptr, &radioSettings);
if ((i == 0) || (i == 3)) {
cb->addItem(wname + " Left", i + ttlSwitches | JS_BUTTON_3POS_DN);
cb->addItem(wname + " Right", i + ttlSwitches | JS_BUTTON_3POS_UP);
cb->addItem(wname + " Left", (i + ttlSwitches) | JS_BUTTON_3POS_DN);
cb->addItem(wname + " Right", (i + ttlSwitches) | JS_BUTTON_3POS_UP);
} else {
cb->addItem(wname + " Down", i + ttlSwitches | JS_BUTTON_3POS_DN);
cb->addItem(wname + " Up", i + ttlSwitches | JS_BUTTON_3POS_UP);
cb->addItem(wname + " Down", (i + ttlSwitches) | JS_BUTTON_3POS_DN);
cb->addItem(wname + " Up", (i + ttlSwitches) | JS_BUTTON_3POS_UP);
}
}
}

bool joystickDialog::loadJoysticks(int stick)
bool joystickDialog::loadJoysticks()
{
QStringList joystickNames;
bool found = false;
Expand All @@ -219,7 +234,8 @@ bool joystickDialog::loadJoysticks(int stick)
joystick->close();
}
ui->joystickCB->insertItems(0, joystickNames);
if (found && stick < joystickNames.size()) {
if (found) {
int stick = joystick->findCurrent(g.currentProfile().jsName());
ui->joystickCB->setCurrentIndex(stick);
}
else if (!found) {
Expand All @@ -230,12 +246,16 @@ bool joystickDialog::loadJoysticks(int stick)

void joystickDialog::joystickOpen(int stick)
{
numAxes = 0;
numButtons = 0;

if (stick < 0)
return;

joystick = new Joystick(this, 1, false, 0);
if (joystick && joystick->open(stick)) {
numAxes = std::min(joystick->numAxes, MAX_JS_AXES);
numButtons = std::min(joystick->numButtons, MAX_JS_BUTTONS);
for (int j=0; j<numAxes; j++) {
joystick->sensitivities[j] = 0;
joystick->deadzones[j] = 20;
Expand All @@ -244,6 +264,9 @@ void joystickDialog::joystickOpen(int stick)
else {
QMessageBox::critical(this, CPN_STR_TTL_ERROR, tr("Cannot open joystick."));
}
g.currentProfile().jsName(ui->joystickCB->currentText());
g.loadNamedJS();
loadGrid();
}

void joystickDialog::joystickSetEnabled(bool enable)
Expand All @@ -256,7 +279,7 @@ void joystickDialog::joystickSetEnabled(bool enable)

void joystickDialog::onjoystickAxisValueChanged(int axis, int value)
{
if (axis >= MAX_JS_AXES)
if (axis >= numAxes)
return;

if (started) {
Expand All @@ -274,10 +297,10 @@ void joystickDialog::onjoystickAxisValueChanged(int axis, int value)

void joystickDialog::onjoystickButtonValueChanged(int button, bool state)
{
if (button >= MAX_JS_BUTTONS)
if (button >= numButtons)
return;

sliders[button + MAX_JS_AXES]->setValue(state);
sliders[button + numAxes]->setValue(state);
}

void joystickDialog::loadStep()
Expand All @@ -290,6 +313,10 @@ void joystickDialog::loadStep()
break;
case 1:
started = true;
for (int i=0; i < numAxes; i++) {
jscal[i][0] = 0;
jscal[i][2] = 0;
}
ui->howtoLabel->setText(tr("Move sticks and pots in every direction making full movement\nPress Next when finished"));
ui->nextButton->setText(tr("Next"));
ui->backButton->setDisabled(true);
Expand Down Expand Up @@ -345,12 +372,12 @@ void joystickDialog::on_cancelButton_clicked()
void joystickDialog::on_okButton_clicked()
{
g.jsSupport(ui->joystickChkB->isChecked());
g.jsCtrl(ui->joystickCB->currentIndex());
g.currentProfile().jsName(ui->joystickCB->currentText());

if (joystick)
joystick->close();

if (!g.jsSupport() || g.jsCtrl() < 0) {
if (!g.jsSupport()) {
this->accept();
return;
}
Expand All @@ -362,7 +389,9 @@ void joystickDialog::on_okButton_clicked()
return;
}

for (int i = 0; i < MAX_JS_AXES; ++i) {
g.clearJSData();

for (int i = 0; i < numAxes; ++i) {
auto stick = sticks[i]->currentData().toInt();
if (stick < 0) {
g.joystick[i].stick_axe(-1);
Expand All @@ -373,20 +402,22 @@ void joystickDialog::on_okButton_clicked()
g.joystick[i].stick_med(jscal[i][1]);
g.joystick[i].stick_min(jscal[i][0]);
g.joystick[i].stick_inv(invert[i]->isChecked() );
qDebug() << "joystick mapping " << sticks[i]->objectName() << "stick:" << i << "axe:" << stick;
qDebug() << "joystick mapping " << sticks[i]->currentText() << "stick:" << i << "axe:" << stick << jscal[i][0] << jscal[i][1] << jscal[i][2];
}
}

for (int i = 0; i < MAX_JS_BUTTONS; ++i) {
auto btn = sticks[i+MAX_JS_AXES]->currentData().toInt();
for (int i = 0; i < numButtons; ++i) {
auto btn = sticks[i+numAxes]->currentData().toInt();
if (btn < 0) {
g.jsButton[i].button_idx(-1);
}
else {
g.jsButton[i].button_idx(btn);
qDebug() << "joystick button mapping " << sticks[i+MAX_JS_AXES]->objectName() << "stick:" << i << "idx:" << btn;
qDebug() << "joystick button mapping " << sticks[i+numAxes]->currentText() << "button:" << i << "idx:" << btn;
}
}

g.saveNamedJS();

this->accept();
}
7 changes: 5 additions & 2 deletions companion/src/simulation/joystickdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class joystickDialog : public QDialog
Q_OBJECT

public:
explicit joystickDialog(QWidget *parent = 0, int stick=-1);
explicit joystickDialog(QWidget *parent = 0);
~joystickDialog();
Joystick *joystick;

Expand All @@ -58,12 +58,15 @@ class joystickDialog : public QDialog
QSlider * sliders[MAX_JS_AXES + MAX_JS_BUTTONS];
int step;
int numAxes;
int numButtons;
bool started;

void loadGrid();

private slots:
void populateSourceCombo(QComboBox * cb);
void populateButtonCombo(QComboBox * cb);
bool loadJoysticks(int stick = -1);
bool loadJoysticks();
void joystickOpen(int stick);
void joystickSetEnabled(bool enable);
void onjoystickAxisValueChanged(int axis, int value);
Expand Down
Loading

0 comments on commit 4225398

Please sign in to comment.