Refactor curve reference
This commit is contained in:
@@ -19,45 +19,285 @@
|
||||
*/
|
||||
|
||||
#include "curvereference.h"
|
||||
#include "adjustmentreference.h"
|
||||
#include "helpers.h"
|
||||
#include "modeldata.h"
|
||||
|
||||
#include "helpers.h" // for Helpers::getAdjustmentString()
|
||||
#include "radiodata.h" // for ModelData
|
||||
|
||||
QString CurveReference::toString(const ModelData * model, bool verbose) const
|
||||
const QString CurveReference::toString(const ModelData * model, bool verbose) const
|
||||
{
|
||||
if (value == 0) {
|
||||
return "----";
|
||||
}
|
||||
if (value == 0)
|
||||
return CPN_STR_NONE_ITEM;
|
||||
|
||||
QString ret;
|
||||
unsigned idx = abs(value) - 1;
|
||||
|
||||
switch(type) {
|
||||
case CURVE_REF_DIFF:
|
||||
ret = tr("Diff(%1)").arg(Helpers::getAdjustmentString(value, model));
|
||||
break;
|
||||
|
||||
case CURVE_REF_EXPO:
|
||||
ret = tr("Expo(%1)").arg(Helpers::getAdjustmentString(value, model));
|
||||
ret = AdjustmentReference(value).toString(model);
|
||||
break;
|
||||
|
||||
case CURVE_REF_FUNC:
|
||||
ret = QString("x>0" "x<0" "|x|" "f>0" "f<0" "|f|").mid(3*(value-1), 3);
|
||||
if (verbose)
|
||||
ret = tr("Function(%1)").arg(ret);
|
||||
ret = functionToString(value);
|
||||
break;
|
||||
|
||||
default:
|
||||
case CURVE_REF_CUSTOM:
|
||||
if (model)
|
||||
ret = model->curves[idx].nameToString(idx);
|
||||
else
|
||||
ret = CurveData().nameToString(idx);
|
||||
if (verbose)
|
||||
ret = tr("Curve(%1)").arg(ret);
|
||||
if (value < 0)
|
||||
ret.prepend(CPN_STR_SW_INDICATOR_REV);
|
||||
break;
|
||||
default:
|
||||
return CPN_STR_UNKNOWN_ITEM;
|
||||
}
|
||||
|
||||
if (verbose)
|
||||
ret = tr(qPrintable(QString(typeToString(type) + "(%1)").arg(ret)));
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
const bool CurveReference::isValueNumber() const
|
||||
{
|
||||
return (type == CURVE_REF_DIFF || type == CURVE_REF_EXPO) && AdjustmentReference(value).type == AdjustmentReference::ADJUST_REF_VALUE;
|
||||
}
|
||||
|
||||
// static
|
||||
int CurveReference::getDefaultValue(const CurveRefType type, const bool isGVar)
|
||||
{
|
||||
if (isGVar && (type == CURVE_REF_DIFF || type == CURVE_REF_EXPO))
|
||||
return AdjustmentReference(AdjustmentReference::ADJUST_REF_GVAR, 1).toValue();
|
||||
else if (type == CURVE_REF_FUNC)
|
||||
return 1;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
// static
|
||||
QString CurveReference::typeToString(const CurveRefType type)
|
||||
{
|
||||
const QStringList strl = { "Diff", "Expo" , "Func", "Curve" };
|
||||
int idx = (int)type;
|
||||
|
||||
if (idx < 0 || idx >= strl.count())
|
||||
return CPN_STR_UNKNOWN_ITEM;
|
||||
|
||||
return strl.at(idx);
|
||||
}
|
||||
|
||||
constexpr int MAX_CURVE_REF_FUNC { 6 };
|
||||
|
||||
// static
|
||||
QString CurveReference::functionToString(const int value)
|
||||
{
|
||||
const QStringList strl = { "x>0", "x<0", "|x|", "f>0", "f<0", "|f|" };
|
||||
int idx = value - 1;
|
||||
|
||||
if (idx < 0 || idx >= strl.count())
|
||||
return CPN_STR_UNKNOWN_ITEM;
|
||||
|
||||
return strl.at(idx);
|
||||
}
|
||||
|
||||
/*
|
||||
* CurveReferenceUIManager
|
||||
*/
|
||||
|
||||
constexpr int CURVE_REF_UI_HIDE_DIFF { 0x01 };
|
||||
constexpr int CURVE_REF_UI_HIDE_EXPO { 0x02 };
|
||||
constexpr int CURVE_REF_UI_HIDE_NEGATIVE_CURVES { 0x04 };
|
||||
|
||||
// static
|
||||
bool CurveReferenceUIManager::firsttime { true };
|
||||
int CurveReferenceUIManager::flags{ 0 };
|
||||
bool CurveReferenceUIManager::hasCapabilityGvars{ false };
|
||||
int CurveReferenceUIManager::numCurves{ 0 };
|
||||
|
||||
CurveReferenceUIManager::CurveReferenceUIManager(QComboBox * curveValueCB, CurveReference & curve, const ModelData & model, QObject * parent) :
|
||||
QObject(parent),
|
||||
curveTypeCB(nullptr),
|
||||
curveGVarCB(nullptr),
|
||||
curveValueSB(nullptr),
|
||||
curveValueCB(curveValueCB),
|
||||
curve(curve),
|
||||
model(model),
|
||||
lock(false)
|
||||
{
|
||||
init();
|
||||
}
|
||||
|
||||
CurveReferenceUIManager::CurveReferenceUIManager(QComboBox * curveTypeCB, QCheckBox * curveGVarCB, QSpinBox * curveValueSB, QComboBox * curveValueCB,
|
||||
CurveReference & curve, const ModelData & model, QObject * parent) :
|
||||
QObject(parent),
|
||||
curveTypeCB(curveTypeCB),
|
||||
curveGVarCB(curveGVarCB),
|
||||
curveValueSB(curveValueSB),
|
||||
curveValueCB(curveValueCB),
|
||||
curve(curve),
|
||||
model(model),
|
||||
lock(false)
|
||||
{
|
||||
init();
|
||||
}
|
||||
|
||||
CurveReferenceUIManager::~CurveReferenceUIManager()
|
||||
{
|
||||
}
|
||||
|
||||
void CurveReferenceUIManager::init()
|
||||
{
|
||||
if (firsttime) {
|
||||
firsttime = false;
|
||||
Firmware * fw = getCurrentFirmware();
|
||||
hasCapabilityGvars = fw->getCapability(Gvars);
|
||||
numCurves = fw->getCapability(NumCurves);
|
||||
|
||||
if (!fw->getCapability(HasInputDiff))
|
||||
flags |= (CURVE_REF_UI_HIDE_DIFF | CURVE_REF_UI_HIDE_NEGATIVE_CURVES);
|
||||
|
||||
if (!fw->getCapability(HasMixerExpo))
|
||||
flags |= CURVE_REF_UI_HIDE_EXPO;
|
||||
}
|
||||
|
||||
if (curveTypeCB) {
|
||||
populateTypeCB(curveTypeCB, curve);
|
||||
connect(curveTypeCB, SIGNAL(currentIndexChanged(int)), this, SLOT(typeChanged(int)));
|
||||
}
|
||||
|
||||
if (curveGVarCB)
|
||||
connect(curveGVarCB, SIGNAL(stateChanged(int)), this, SLOT(gvarCBChanged(int)));
|
||||
|
||||
if (curveValueSB) {
|
||||
curveValueSB->setMinimum(-100);
|
||||
curveValueSB->setMaximum(100);
|
||||
connect(curveValueSB, SIGNAL(editingFinished()), this, SLOT(valueSBChanged()));
|
||||
}
|
||||
|
||||
curveValueCB->setSizeAdjustPolicy(QComboBox::AdjustToContents);
|
||||
curveValueCB->setMaxVisibleItems(10);
|
||||
connect(curveValueCB, SIGNAL(currentIndexChanged(int)), this, SLOT(valueCBChanged()));
|
||||
|
||||
update();
|
||||
}
|
||||
|
||||
#define CURVE_REF_UI_GVAR_SHOW (1<<0)
|
||||
#define CURVE_REF_UI_VALUE_SHOW (1<<1)
|
||||
#define CURVE_REF_UI_REF_SHOW (1<<2)
|
||||
|
||||
void CurveReferenceUIManager::update()
|
||||
{
|
||||
lock = true;
|
||||
int widgetsMask = 0;
|
||||
|
||||
if (curve.type == CurveReference::CURVE_REF_DIFF || curve.type == CurveReference::CURVE_REF_EXPO) {
|
||||
if (hasCapabilityGvars)
|
||||
widgetsMask |= CURVE_REF_UI_GVAR_SHOW;
|
||||
if (curve.isValueNumber()) {
|
||||
curveGVarCB->setChecked(false);
|
||||
curveValueSB->setValue(curve.value);
|
||||
widgetsMask |= CURVE_REF_UI_VALUE_SHOW;
|
||||
}
|
||||
else {
|
||||
curveGVarCB->setChecked(true);
|
||||
widgetsMask |= CURVE_REF_UI_REF_SHOW;
|
||||
}
|
||||
}
|
||||
else {
|
||||
widgetsMask |= CURVE_REF_UI_REF_SHOW;
|
||||
}
|
||||
|
||||
if(curveTypeCB) {
|
||||
curveTypeCB->setCurrentIndex(curveTypeCB->findData(curve.type));
|
||||
curveTypeCB->show();
|
||||
}
|
||||
if(curveGVarCB)
|
||||
curveGVarCB->setVisible(widgetsMask & CURVE_REF_UI_GVAR_SHOW);
|
||||
if(curveValueSB)
|
||||
curveValueSB->setVisible(widgetsMask & CURVE_REF_UI_VALUE_SHOW);
|
||||
if(curveValueCB) {
|
||||
if (curve.isValueReference())
|
||||
populateValueCB(curveValueCB, curve, &model);
|
||||
curveValueCB->setVisible(widgetsMask & CURVE_REF_UI_REF_SHOW);
|
||||
}
|
||||
|
||||
lock = false;
|
||||
}
|
||||
|
||||
void CurveReferenceUIManager::gvarCBChanged(int state)
|
||||
{
|
||||
if (!lock) {
|
||||
curve.value = CurveReference::getDefaultValue(curve.type, state);
|
||||
update();
|
||||
}
|
||||
}
|
||||
|
||||
void CurveReferenceUIManager::typeChanged(int value)
|
||||
{
|
||||
if (!lock) {
|
||||
CurveReference::CurveRefType type = (CurveReference::CurveRefType)curveTypeCB->itemData(curveTypeCB->currentIndex()).toInt();
|
||||
curve = CurveReference(type, CurveReference::getDefaultValue(type));
|
||||
update();
|
||||
}
|
||||
}
|
||||
|
||||
void CurveReferenceUIManager::valueSBChanged()
|
||||
{
|
||||
if (!lock) {
|
||||
curve.value = curveValueSB->value();
|
||||
update();
|
||||
}
|
||||
}
|
||||
|
||||
void CurveReferenceUIManager::valueCBChanged()
|
||||
{
|
||||
if (!lock) {
|
||||
curve.value = curveValueCB->itemData(curveValueCB->currentIndex()).toInt();
|
||||
update();
|
||||
}
|
||||
}
|
||||
|
||||
// static
|
||||
void CurveReferenceUIManager::populateTypeCB(QComboBox * cb, const CurveReference & curveRef)
|
||||
{
|
||||
if (cb) {
|
||||
cb->clear();
|
||||
for (int i = 0; i <= CurveReference::MAX_CURVE_REF_TYPE; i++) {
|
||||
if ((curveRef.type == CurveReference::CURVE_REF_DIFF && !(flags & CURVE_REF_UI_HIDE_DIFF)) ||
|
||||
(curveRef.type == CurveReference::CURVE_REF_EXPO && !(flags & CURVE_REF_UI_HIDE_EXPO)) ||
|
||||
(curveRef.type != CurveReference::CURVE_REF_DIFF && curveRef.type != CurveReference::CURVE_REF_EXPO))
|
||||
cb->addItem(CurveReference::typeToString((CurveReference::CurveRefType)i), i);
|
||||
}
|
||||
|
||||
cb->setCurrentIndex(cb->findData((int)curveRef.type));
|
||||
}
|
||||
}
|
||||
|
||||
// static
|
||||
void CurveReferenceUIManager::populateValueCB(QComboBox * cb, const CurveReference & curveRef, const ModelData * model)
|
||||
{
|
||||
if (cb) {
|
||||
cb->clear();
|
||||
|
||||
switch (curveRef.type) {
|
||||
case CurveReference::CURVE_REF_DIFF:
|
||||
case CurveReference::CURVE_REF_EXPO:
|
||||
Helpers::populateGVCB(*cb, curveRef.value, *model);
|
||||
break;
|
||||
case CurveReference::CURVE_REF_FUNC:
|
||||
for (int i = 1; i <= MAX_CURVE_REF_FUNC; i++) {
|
||||
cb->addItem(CurveReference::functionToString(i), i);
|
||||
}
|
||||
break;
|
||||
case CurveReference::CURVE_REF_CUSTOM:
|
||||
// TODO replace with ui datamodel
|
||||
for (int i = ((flags & CURVE_REF_UI_HIDE_NEGATIVE_CURVES) ? 0 : -numCurves); i <= numCurves; i++) {
|
||||
cb->addItem(CurveReference(CurveReference::CURVE_REF_CUSTOM, i).toString(model, false), i);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
cb->setCurrentIndex(cb->findData(curveRef.value));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user