Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backward compatibility issue with variable types #5574

Merged
merged 4 commits into from
Jun 20, 2024
Merged
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
59 changes: 42 additions & 17 deletions QMLComponents/boundcontrols/boundcontrolterms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,23 @@ Json::Value BoundControlTerms::_adjustBindingValue(const Json::Value &value) con
return adjustedValue;
}

Json::Value BoundControlTerms::_adjustBindingType(const Json::Value &value) const
{
Json::Value adjustedType = _isValueWithTypes(value) ? value["types"] : Json::arrayValue;
if (adjustedType.isString())
{
std::string type = adjustedType.asString();
adjustedType = Json::arrayValue;
adjustedType.append(type);
}

return adjustedType;
}

void BoundControlTerms::bindTo(const Json::Value &value)
{
Json::Value valuePart = _adjustBindingValue(value);
Json::Value typesPart = _isValueWithTypes(value) ? value["types"] : Json::arrayValue;
Json::Value typesPart = _adjustBindingType(value);

Terms terms;
ListModel::RowControlsValues allControlValues;
Expand Down Expand Up @@ -123,11 +136,24 @@ void BoundControlTerms::bindTo(const Json::Value &value)
if (assignedModel && !assignedModel->checkAllowedTerms(terms))
valuePart = addTermsToOption(Json::Value::null, terms);

bool hasTypes = (typesPart.size() > 0);
if (!hasTypes)
int i = 0;
for (Term& term : terms)
{
for (const Term& term : terms)
if (typesPart.size() > i) // If the type is given, use it
term.setType(columnTypeFromString(typesPart[i].asString(), columnType::unknown));
else
{
if (term.type() == columnType::unknown)
{
// Backward compatibility: the type was not saved before 0.19, so get the real type and check whether it is allowed. If not, take the default
columnType type = _listView->model()->getVariableRealType(term.asQString());
if (type != columnType::unknown && !_listView->isTypeAllowed(type))
type = _listView->defaultType();
term.setType(type);
}
typesPart.append(columnTypeToString(term.type()));
}
i++;
}

Json::Value newValue = Json::objectValue;
Expand All @@ -137,16 +163,6 @@ void BoundControlTerms::bindTo(const Json::Value &value)

_termsModel->initTerms(terms, allControlValues);

// Set the terms types
if (hasTypes)
{
for (int i = 0; i < typesPart.size(); i++)
{
columnType type = columnTypeFromString(typesPart[i].asString(), columnType::unknown);
if (type != columnType::unknown)
_termsModel->setVariableType(i, type);
}
}
}

Json::Value BoundControlTerms::createJson() const
Expand Down Expand Up @@ -192,22 +208,29 @@ Json::Value BoundControlTerms::createJson() const
}
}
valuePart.append(row);
typesPart.append(columnTypeToString(term.type()));
}
}
else if (_isSingleRow)
{
valuePart = (terms.size() > 0) ? terms.at(0).asString() : "";
typesPart = columnTypeToString((terms.size() > 0) ? terms.at(0).type() : columnType::unknown);
}
else
{
valuePart = Json::arrayValue;
for (const Term& term : terms)
{
valuePart.append(term.asString());
typesPart.append(columnTypeToString(term.type()));
}
}



Json::Value result = Json::objectValue;
result["value"] = valuePart;
result["types"] = _getTypes();
result["types"] = typesPart;

return result;
}
Expand Down Expand Up @@ -262,7 +285,7 @@ bool BoundControlTerms::isJsonValid(const Json::Value &optionValue) const
}
}

return valid && typesPart.isArray();
return valid && typesPart.isArray() || typesPart.isString();
}

void BoundControlTerms::resetBoundValue()
Expand Down Expand Up @@ -402,10 +425,12 @@ bool BoundControlTerms::areTermsInOption(const Json::Value &option, Terms &terms
return result;
}

Terms BoundControlTerms::_getValuesFromOptions(const Json::Value& option) const
Terms BoundControlTerms::_getValuesFromOptions(const Json::Value& _option) const
{
Terms result;

Json::Value option = _isValueWithTypes(option) ? _option["value"] : _option;

if (_listView->hasRowComponent() || _listView->containsInteractions())
{
if (!option.isArray())
Expand Down
1 change: 1 addition & 0 deletions QMLComponents/boundcontrols/boundcontrolterms.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class BoundControlTerms : public BoundControlBase
private:
Terms _getValuesFromOptions(const Json::Value& option) const;
Json::Value _adjustBindingValue(const Json::Value &value) const;
Json::Value _adjustBindingType(const Json::Value &value) const;
Json::Value _getTypes() const;
bool _isValueWithTypes(const Json::Value &value) const;

Expand Down
2 changes: 1 addition & 1 deletion QMLComponents/models/listmodelcustomcontrasts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ void ListModelCustomContrasts::getVariablesAndLabels(Terms& variables, QVector<Q
labels = _factors[newVariable.asQString()];
else
{
if (getVariableRealType(newVariable.asQString()) == columnType::scale)
if (newVariable.type() == columnType::scale)
{
if (_scaleFactor == 0)
labels = {"0"};
Expand Down
6 changes: 5 additions & 1 deletion QMLComponents/models/listmodellayersassigned.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,11 @@ void ListModelLayersAssigned::_setTerms()
{
newTerms.add(tr("Layer %1").arg(layer));
for (const QString& variable : variables)
newTerms.add(variable);
{
Term term(variable);
term.setType(columnType::nominal);
newTerms.add(term);
}
layer++;
}

Expand Down
Loading