Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 17 additions & 45 deletions companion/src/firmwares/edgetx/yaml_modeldata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "boardjson.h"
#include "modeldata.h"
#include "output_data.h"
#include "sourcenumref.h"
#include "eeprominterface.h"
#include "version.h"
#include "helpers.h"
Expand Down Expand Up @@ -518,60 +519,31 @@ bool convert<TimerData>::decode(const Node& node, TimerData& rhs)

static int32_t YamlReadLimitValue(const YAML::Node& node, int32_t shift = 0)
{
Firmware *firmware = getCurrentFirmware();
std::string val_str = node.as<std::string>();
std::string val = node.as<std::string>();

try {
return std::stoi(val_str) + shift;
} catch(...) {
// Legacy 1-based "GVn" / "-GVn" form
bool neg = !val.empty() && val[0] == '-';
size_t pos = neg ? 1 : 0;
if (val.size() > pos + 2 && val[pos] == 'G' && val[pos + 1] == 'V') {
int gv = 0;
try {
const char* val = val_str.data();
int multiplier = 1;

if (val_str.size() >= 4
&& val[0] == '-') {

multiplier = -1;
val_str = val_str.substr(1);
}

if (val_str.size() >= 3
&& val[0] == 'G'
&& val[1] == 'V') {

int32_t num = 0;

try {
num = std::stoi(val_str.substr(2));
} catch (...) {
throw;
}

if (num <= firmware->getCapability(Gvars))
return ((10000 * multiplier) + (num * multiplier));
else
return 0;

} else {
throw "Invalid value";
}
} catch(...) {
gv = std::stoi(val.substr(pos + 2));
} catch (...) {
throw YAML::TypedBadConversion<int>(node.Mark());
}
if (gv < 1 || gv > getCurrentFirmware()->getCapability(Gvars))
return 0;
int src = RawSource(SOURCE_TYPE_GVAR, gv).toValue();
return neg ? -src : src;
}

int32_t result = YamlSourceNumRefDecode(node);
return SourceNumRef(result).isNumber() ? result + shift : result;
}

static std::string YamlWriteLimitValue(int32_t sval, int32_t shift = 0)
{
if (sval < -10000) {
int n = -sval - 10000;
return std::string("-GV") + std::to_string(n);
} else if (sval > 10000) {
int n = sval - 10000;
return std::string("GV") + std::to_string(n);
}

return std::to_string(sval - shift);
return YamlSourceNumRefEncode(SourceNumRef(sval).isNumber() ? sval - shift : sval);
}

template <>
Expand Down
17 changes: 3 additions & 14 deletions companion/src/firmwares/modeldata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -856,9 +856,9 @@ int ModelData::updateReference()
for (int i = 0; i < CPN_MAX_CHNOUT; i++) {
LimitData *ld = &limitData[i];
if (!ld->isEmpty()) {
updateAdjustRef(ld->min);
updateAdjustRef(ld->max);
updateAdjustRef(ld->offset);
updateSourceNumRef(ld->min);
updateSourceNumRef(ld->max);
updateSourceNumRef(ld->offset);
updateLimitCurveRef(ld->curve);
}
}
Expand Down Expand Up @@ -1167,17 +1167,6 @@ void ModelData::updateLimitCurveRef(CurveReference & crv)
crv.value = src.value;
}

void ModelData::updateAdjustRef(int & value)
{
if (updRefInfo.type != REF_UPD_TYPE_GLOBAL_VARIABLE)
return;

AdjustmentReference adj = AdjustmentReference(value);
updateTypeValueRef<AdjustmentReference, AdjustmentReference::AdjustRefType>(adj, AdjustmentReference::ADJUST_REF_GVAR, 1);
if (value != adj.toValue())
value = adj.toValue();
}

void ModelData::updateAssignFunc(CustomFunctionData * cfd)
{
const int invalidateRef = -1;
Expand Down
1 change: 0 additions & 1 deletion companion/src/firmwares/modeldata.h
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,6 @@ class ModelData {
void updateTypeIndexRef(R & curref, const T type, const int idxAdj = 0, const bool defClear = true, const int defType = 0, const int defIndex = 0);
template <class R, typename T>
void updateTypeValueRef(R & curref, const T type, const int idxAdj = 0, const bool defClear = true, const int defType = 0, const int defValue = 0);
void updateAdjustRef(int & adj);
void updateAssignFunc(CustomFunctionData * cfd);
void updateCurveRef(CurveReference & crv);
void updateDestCh(MixData * md);
Expand Down
7 changes: 7 additions & 0 deletions companion/src/firmwares/output_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "output_data.h"
#include "radiodata.h"
#include "radiodataconversionstate.h"
#include "sourcenumref.h"

void LimitData::clear()
{
Expand All @@ -38,11 +39,15 @@ bool LimitData::isEmpty() const

QString LimitData::minToString() const
{
if (SourceNumRef(min).isSource())
return RawSource(min).toString();
return QString::number((qreal)min / 10);
}

QString LimitData::maxToString() const
{
if (SourceNumRef(max).isSource())
return RawSource(max).toString();
return QString::number((qreal)max / 10);
}

Expand All @@ -58,5 +63,7 @@ QString LimitData::nameToString(int index) const

QString LimitData::offsetToString() const
{
if (SourceNumRef(offset).isSource())
return RawSource(offset).toString();
return QString::number((qreal)offset / 10, 'f', 1);
}
128 changes: 100 additions & 28 deletions companion/src/modeledit/channels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,20 @@
#include "filtereditemmodels.h"
#include "curveimagewidget.h"
#include "namevalidator.h"
#include "sourcenumref.h"

LimitsGroup::LimitsGroup(Firmware * firmware, TableLayout * tableLayout, int row, int col, int & value, const ModelData & model, GeneralSettings & generalSettings,
int min, int max, int deflt, FilteredItemModel * gvarModel, ModelPanel * panel):
LimitsGroup::LimitsGroup(Firmware * firmware, TableLayout * tableLayout, int row, int col, int & value, GeneralSettings & generalSettings,
int min, int max, int deflt, FilteredItemModel * sourceModel, ModelPanel * panel):
QObject(panel),
firmware(firmware),
spinbox(new QDoubleSpinBox()),
value(value)
chkSource(new QCheckBox(tr("SRC"))),
cboSource(new QComboBox()),
value(value),
mini(min),
maxi(max),
deflt(deflt),
lock(true)
{
spinbox->setProperty("index", row);
spinbox->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
Expand All @@ -48,45 +56,107 @@ LimitsGroup::LimitsGroup(Firmware * firmware, TableLayout * tableLayout, int row
spinbox->setSingleStep(displayStep);
spinbox->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);

chkSource->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
cboSource->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
cboSource->setSizeAdjustPolicy(QComboBox::AdjustToContents);
cboSource->setMaxVisibleItems(10);
cboSource->setModel(sourceModel);

QHBoxLayout *horizontalLayout = new QHBoxLayout();
gv = new QCheckBox(tr("GV"));
gv->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
horizontalLayout->addWidget(gv);
QComboBox *cb = new QComboBox();
cb->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
horizontalLayout->addWidget(cb);
horizontalLayout->addWidget(chkSource);
horizontalLayout->addWidget(cboSource);
horizontalLayout->addWidget(spinbox);
tableLayout->addLayout(row, col, horizontalLayout);
gvarGroup = new GVarGroup(gv, spinbox, cb, value, model, deflt, min, max, displayStep, gvarModel);
QObject::connect(gvarGroup, &GVarGroup::valueChanged, panel, &ModelPanel::modified);

connect(chkSource, &QCheckBox::stateChanged, this, &LimitsGroup::chkSourceChanged);
connect(cboSource, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &LimitsGroup::cboSourceChanged);
connect(spinbox, &QDoubleSpinBox::editingFinished, this, &LimitsGroup::spinboxChanged);
connect(this, &LimitsGroup::valueChanged, panel, &ModelPanel::modified);

setValue(value);
lock = false;
}

void LimitsGroup::chkSourceChanged(int state)
{
if (lock)
return;

if (state) {
int idx = cboSource->findData(RawSource(SOURCE_TYPE_GVAR, 1).toValue());
cboSource->setCurrentIndex(idx >= 0 ? idx : Helpers::getFirstPosValueIndex(cboSource));
value = cboSource->currentData().toInt();
}
else {
value = deflt;
}

setValue(value);
emit valueChanged();
}

void LimitsGroup::cboSourceChanged(int index)
{
if (lock)
return;

value = cboSource->itemData(index).toInt();
emit valueChanged();
}

LimitsGroup::~LimitsGroup()
void LimitsGroup::spinboxChanged()
{
delete gvarGroup;
if (lock)
return;

value = round(spinbox->value() / displayStep);
emit valueChanged();
}

void LimitsGroup::setValue(int val)
{
gvarGroup->setWeight(val);
bool wasLocked = lock;
lock = true;

spinbox->setMinimum(mini * displayStep);
spinbox->setMaximum(maxi * displayStep);

if (SourceNumRef(val).isSource()) {
chkSource->setChecked(true);
int idx = cboSource->findData(val);
if (idx < 0)
idx = Helpers::getFirstPosValueIndex(cboSource);
cboSource->setCurrentIndex(idx);
spinbox->setValue(deflt * displayStep);
spinbox->hide();
cboSource->show();
}
else {
chkSource->setChecked(false);
spinbox->setValue(val * displayStep);
cboSource->hide();
spinbox->show();
}

lock = wasLocked;
}

void LimitsGroup::updateMinMax(int max)
{
if (spinbox->maximum() == 0) {
spinbox->setMinimum(-max * displayStep);
gvarGroup->setMinimum(-max);
if (!gv->isChecked() && value < -max) {
bool isNumber = SourceNumRef(value).isNumber();

if (maxi == 0) {
mini = -max;
if (isNumber && value < -max)
value = -max;
}
}
if (spinbox->minimum() == 0) {
spinbox->setMaximum(max * displayStep);
gvarGroup->setMaximum(max);
if (!gv->isChecked() && value > max) {
if (mini == 0) {
maxi = max;
if (isNumber && value > max)
value = max;
}
}

setValue(value);
}

ChannelsPanel::ChannelsPanel(QWidget * parent, ModelData & model, GeneralSettings & generalSettings, Firmware * firmware, CompoundItemModelFactory * sharedItemModels):
Expand All @@ -103,7 +173,9 @@ ChannelsPanel::ChannelsPanel(QWidget * parent, ModelData & model, GeneralSetting
int crvid = dialogFilteredItemModels->registerItemModel(new FilteredItemModel(sharedItemModels->getItemModel(AbstractItemModel::IMID_Curve)), "Curve");
connectItemModelEvents(dialogFilteredItemModels->getItemModel(crvid));

int gvid = dialogFilteredItemModels->registerItemModel(new FilteredItemModel(sharedItemModels->getItemModel(AbstractItemModel::IMID_GVarRef)), "GVarRef");
int srcid = dialogFilteredItemModels->registerItemModel(new FilteredItemModel(sharedItemModels->getItemModel(AbstractItemModel::IMID_RawSource),
(RawSource::AllSourceGroups & ~RawSource::NoneGroup & ~RawSource::ScriptsGroup)),
"LimitSource");
curveRefFilteredItemModels = new CurveRefFilteredFactory(sharedItemModels, 0);

QStringList headerLabels;
Expand Down Expand Up @@ -143,13 +215,13 @@ ChannelsPanel::ChannelsPanel(QWidget * parent, ModelData & model, GeneralSetting
}

// Channel offset
chnOffset[i] = new LimitsGroup(firmware, tableLayout, i, col++, model.limitData[i].offset, model, generalSettings, -1000, 1000, 0, dialogFilteredItemModels->getItemModel(gvid), this);
chnOffset[i] = new LimitsGroup(firmware, tableLayout, i, col++, model.limitData[i].offset, generalSettings, -1000, 1000, 0, dialogFilteredItemModels->getItemModel(srcid), this);

// Channel min
chnMin[i] = new LimitsGroup(firmware, tableLayout, i, col++, model.limitData[i].min, model, generalSettings, -model.getChannelsMax() * 10, 0, -1000, dialogFilteredItemModels->getItemModel(gvid), this);
chnMin[i] = new LimitsGroup(firmware, tableLayout, i, col++, model.limitData[i].min, generalSettings, -model.getChannelsMax() * 10, 0, -1000, dialogFilteredItemModels->getItemModel(srcid), this);

// Channel max
chnMax[i] = new LimitsGroup(firmware, tableLayout, i, col++, model.limitData[i].max, model, generalSettings, 0, model.getChannelsMax() * 10, 1000, dialogFilteredItemModels->getItemModel(gvid), this);
chnMax[i] = new LimitsGroup(firmware, tableLayout, i, col++, model.limitData[i].max, generalSettings, 0, model.getChannelsMax() * 10, 1000, dialogFilteredItemModels->getItemModel(srcid), this);

// Channel inversion
invCB[i] = new QComboBox(this);
Expand Down
27 changes: 18 additions & 9 deletions companion/src/modeledit/channels.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,36 @@ class CurveImageWidget;

constexpr char MIMETYPE_CHANNEL[] = "application/x-companion-channel";

class GVarGroup;

class LimitsGroup
class LimitsGroup : public QObject
{
Q_DECLARE_TR_FUNCTIONS(LimitsGroup)
Q_OBJECT

public:
LimitsGroup(Firmware * firmware, TableLayout * tableLayout, int row, int col, int & value, const ModelData & model, GeneralSettings & generalSettings,
int min, int max, int deflt, FilteredItemModel * gvarModel, ModelPanel * panel = nullptr);
~LimitsGroup();
LimitsGroup(Firmware * firmware, TableLayout * tableLayout, int row, int col, int & value, GeneralSettings & generalSettings,
int min, int max, int deflt, FilteredItemModel * sourceModel, ModelPanel * panel = nullptr);

void setValue(int val);
void updateMinMax(int max);

signals:
void valueChanged();

protected slots:
void chkSourceChanged(int state);
void cboSourceChanged(int index);
void spinboxChanged();

protected:
Firmware *firmware;
QDoubleSpinBox *spinbox;
GVarGroup *gvarGroup;
QCheckBox *chkSource;
QComboBox *cboSource;
int &value;
int mini;
int maxi;
int deflt;
double displayStep;
QCheckBox *gv;
bool lock;
};

class ChannelsPanel : public ModelPanel
Expand Down
6 changes: 2 additions & 4 deletions companion/src/print/modelprinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -692,10 +692,8 @@ QString ModelPrinter::printGlobalVarPopup(int idx)
QString ModelPrinter::printOutputValueGVar(int val)
{
QString result = "";
if (abs(val) > 10000) {
if (val < 0)
result = "-";
result.append(RawSource(SOURCE_TYPE_GVAR, (abs(val)-10001) + 1).toString(&model));
if (SourceNumRef(val).isSource()) {
result = RawSource(val).toString(&model);
}
else {
if (val >= 0)
Expand Down
Loading
Loading