1
0
Fork 0
mirror of https://github.com/opentx/opentx.git synced 2025-07-15 20:35:17 +03:00

Add support for rawsource and rawswitch in autocombobox

This commit is contained in:
elecpower 2021-03-17 21:40:13 +11:00
parent aac6c74e59
commit 950fa3d105
3 changed files with 67 additions and 13 deletions

View file

@ -22,6 +22,8 @@
#include <QComboBox> #include <QComboBox>
#include "genericpanel.h" #include "genericpanel.h"
#include "rawsource.h"
#include "rawswitch.h"
class AutoComboBox: public QComboBox class AutoComboBox: public QComboBox
{ {
@ -34,7 +36,9 @@ class AutoComboBox: public QComboBox
panel(nullptr), panel(nullptr),
next(0), next(0),
lock(false), lock(false),
hasModel(false) hasModel(false),
rawSource(nullptr),
rawSwitch(nullptr)
{ {
connect(this, SIGNAL(currentIndexChanged(int)), this, SLOT(onCurrentIndexChanged(int))); connect(this, SIGNAL(currentIndexChanged(int)), this, SLOT(onCurrentIndexChanged(int)));
} }
@ -77,6 +81,8 @@ class AutoComboBox: public QComboBox
void setField(unsigned int & field, GenericPanel * panel = nullptr) void setField(unsigned int & field, GenericPanel * panel = nullptr)
{ {
this->field = (int *)&field; this->field = (int *)&field;
this->rawSource = nullptr;
this->rawSwitch = nullptr;
this->panel = panel; this->panel = panel;
updateValue(); updateValue();
} }
@ -84,6 +90,26 @@ class AutoComboBox: public QComboBox
void setField(int & field, GenericPanel * panel = nullptr) void setField(int & field, GenericPanel * panel = nullptr)
{ {
this->field = &field; this->field = &field;
this->rawSource = nullptr;
this->rawSwitch = nullptr;
this->panel = panel;
updateValue();
}
void setField(RawSource & field, GenericPanel * panel = nullptr)
{
this->rawSource = &field;
this->rawSwitch = nullptr;
this->field = nullptr;
this->panel = panel;
updateValue();
}
void setField(RawSwitch & field, GenericPanel * panel = nullptr)
{
this->rawSwitch = &field;
this->rawSource = nullptr;
this->field = nullptr;
this->panel = panel; this->panel = panel;
updateValue(); updateValue();
} }
@ -108,10 +134,18 @@ class AutoComboBox: public QComboBox
void updateValue() void updateValue()
{ {
if (!field) if (!field && !rawSource && !rawSwitch)
return; return;
lock = true; lock = true;
if (field)
setCurrentIndex(findData(*field)); setCurrentIndex(findData(*field));
else if (rawSource)
setCurrentIndex(findData(rawSource->toValue()));
else if (rawSwitch)
setCurrentIndex(findData(rawSwitch->toValue()));
lock = false; lock = false;
} }
@ -123,16 +157,30 @@ class AutoComboBox: public QComboBox
{ {
if (panel && panel->lock) if (panel && panel->lock)
return; return;
if (index > -1) { if (lock || index < 0)
const int val = itemData(index).toInt(); return;
if (field && !lock) {
bool ok;
const int val = itemData(index).toInt(&ok);
if (!ok)
return;
if (field && *field != val) {
*field = val; *field = val;
}
else if (rawSource && rawSource->toValue() != val) {
*rawSource = RawSource(val);
}
else if (rawSwitch && rawSwitch->toValue() != val) {
*rawSwitch = RawSwitch(val);
}
else
return;
emit currentDataChanged(val); emit currentDataChanged(val);
if (panel) if (panel)
emit panel->modified(); emit panel->modified();
} }
}
}
protected: protected:
int *field = nullptr; int *field = nullptr;
@ -140,4 +188,6 @@ class AutoComboBox: public QComboBox
int next = 0; int next = 0;
bool lock = false; bool lock = false;
bool hasModel = false; bool hasModel = false;
RawSource *rawSource = nullptr;
RawSwitch *rawSwitch = nullptr;
}; };

View file

@ -76,7 +76,9 @@ class AutoLineEdit: public QLineEdit
protected slots: protected slots:
void onEdited() void onEdited()
{ {
if ((panel && panel->lock) || lock) if (panel && panel->lock)
return;
if (lock)
return; return;
if (field) if (field)

View file

@ -77,7 +77,9 @@ class AutoTimeEdit: public QTimeEdit
protected slots: protected slots:
void onTimeChanged(QTime time) void onTimeChanged(QTime time)
{ {
if ((panel && panel->lock) || !field || lock) if (panel && panel->lock)
return;
if (!field || lock)
return; return;
unsigned int val = time.hour() * 3600 + time.minute() * 60 + time.second(); unsigned int val = time.hour() * 3600 + time.minute() * 60 + time.second();