diff --git a/QMLComponents/boundcontrols/boundcontrolterms.cpp b/QMLComponents/boundcontrols/boundcontrolterms.cpp index 1e69ef8309..bc6b442e20 100644 --- a/QMLComponents/boundcontrols/boundcontrolterms.cpp +++ b/QMLComponents/boundcontrols/boundcontrolterms.cpp @@ -191,8 +191,11 @@ Json::Value BoundControlTerms::makeOption(const Terms& terms, const ListModel::R else if (isSingleRow) optionValue = terms.size() > 0 ? terms[0].asString() : ""; else + { + optionValue = Json::arrayValue; for (const Term& term : terms) optionValue.append(term.asString()); + } result["value"] = optionValue; result["types"] = terms.types(); diff --git a/QMLComponents/components/JASP/Controls/VariablesList.qml b/QMLComponents/components/JASP/Controls/VariablesList.qml index d22cdc9ecc..10dab4eab5 100644 --- a/QMLComponents/components/JASP/Controls/VariablesList.qml +++ b/QMLComponents/components/JASP/Controls/VariablesList.qml @@ -401,7 +401,7 @@ VariablesListBase property bool containsDragItem: variablesList.itemContainingDrag === itemRectangle property bool isVirtual: (typeof model.type !== "undefined") && model.type.includes("virtual") property bool isVariable: (typeof model.type !== "undefined") && model.type.includes("variable") - property string preview: !isVariable ? "" : model.preview + property string preview: !isVariable || (typeof model.preview === "undefined") ? "" : model.preview property bool isLayer: (typeof model.type !== "undefined") && model.type.includes("layer") property bool draggable: variablesList.draggable && model.selectable property string columnType: isVariable && (typeof model.columnType !== "undefined") ? model.columnType : "" diff --git a/QMLComponents/models/listmodel.cpp b/QMLComponents/models/listmodel.cpp index f681ccc799..f078999b90 100644 --- a/QMLComponents/models/listmodel.cpp +++ b/QMLComponents/models/listmodel.cpp @@ -23,6 +23,7 @@ #include "controls/rowcontrols.h" #include "controls/sourceitem.h" #include "log.h" +#include "jsonutilities.h" ListModel::ListModel(JASPListControl* listView) : QAbstractTableModel(listView) @@ -796,8 +797,13 @@ void ListModel::_replaceTerm(int index, const Term &term) Json::Value ListModel::getVariableTypes(bool onlyChanged) const { - if (onlyChanged && _listView->hasMandatoryType()) - return Json::nullValue; + return getVariableTypes(_terms, onlyChanged); +} + +Json::Value ListModel::getVariableTypes(const Terms& terms, bool onlyChanged) const +{ + if (onlyChanged && _listView->hasMandatoryType()) // Don't need to ask for the changed types: this avoids to add the type in formula when it is not necessary + return JsonUtilities::vecToJsonArray(stringvec(terms.size(), columnTypeToString(columnType::unknown))); - return _terms.types(onlyChanged, this); + return terms.types(onlyChanged, this); } diff --git a/QMLComponents/models/listmodel.h b/QMLComponents/models/listmodel.h index 6c2abadbf1..9854cc1a95 100644 --- a/QMLComponents/models/listmodel.h +++ b/QMLComponents/models/listmodel.h @@ -91,6 +91,7 @@ class ListModel : public QAbstractTableModel, public VariableInfoConsumer void setVariableType(int index, columnType type); columnType getVariableType( const QString& name) const; Json::Value getVariableTypes(bool onlyChanged = false) const; + Json::Value getVariableTypes(const Terms& terms, bool onlyChanged = false) const; columnType getVariableRealType(const QString& name) const; QString getVariablePreview( const QString& name) const; QStringList getUsedTypes() const; diff --git a/QMLComponents/rsyntax/formulasource.cpp b/QMLComponents/rsyntax/formulasource.cpp index 60683a014b..db7b530368 100644 --- a/QMLComponents/rsyntax/formulasource.cpp +++ b/QMLComponents/rsyntax/formulasource.cpp @@ -246,7 +246,7 @@ QString FormulaSource::_generateRandomEffectsTerms(const Terms& terms) const result += (hasIntercept ? "1" : "0"); if (filteredTerms.size() > 0) - result += " + " + generateInteractionTerms(filteredTerms, filteredTerms.types(true, componentListModel)); + result += " + " + generateInteractionTerms(filteredTerms, componentListModel->getVariableTypes(filteredTerms, true)); result += (hasCorrelation ? " | " : " || "); result += key; @@ -309,14 +309,14 @@ QString FormulaSource::generateInteractionTerms(const Terms& tterms, const Json: } -QString FormulaSource::_generateSimpleTerms(const Terms &terms, const Json::Value& changedTypes) const +QString FormulaSource::_generateSimpleTerms(const Terms &terms, const Json::Value& types) const { QString result; int i = 0; for (const Term& term : terms) { - Json::Value changedType = changedTypes.size() > i ? changedTypes[i] : Json::nullValue; + Json::Value changedType = types.size() > i ? types[i] : Json::nullValue; if (i > 0) result += " + "; result += FormulaParser::transformToFormulaTerm(term, changedType, FormulaParser::interactionSeparator); i++; diff --git a/QMLComponents/rsyntax/formulasource.h b/QMLComponents/rsyntax/formulasource.h index 05e83fb5c3..605f91cee9 100644 --- a/QMLComponents/rsyntax/formulasource.h +++ b/QMLComponents/rsyntax/formulasource.h @@ -82,7 +82,7 @@ class FormulaSource : public QObject void _addError(const QString& error) const; std::string _controlToOptionName(const QString& name) const; QString _generateRandomEffectsTerms(const Terms& terms) const; - QString _generateSimpleTerms(const Terms& terms, const Json::Value& changedTypes) const; + QString _generateSimpleTerms(const Terms& terms, const Json::Value& types) const; std::pair _onlyTrueTerms(const QString& controlName) const; bool _areTermsInOptions(ListModelAssignedInterface* model, const Json::Value& options, Terms& terms) const; void _addTermsToOptions(ListModelAssignedInterface* model, Json::Value& options, const Terms& terms) const;