Skip to content

Commit

Permalink
Encode type recursively.
Browse files Browse the repository at this point in the history
  • Loading branch information
boutinb committed Oct 23, 2024
1 parent b9405c3 commit 331b21e
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 48 deletions.
2 changes: 1 addition & 1 deletion Common/jaspColumnEncoder
29 changes: 0 additions & 29 deletions QMLComponents/boundcontrols/boundcontrolbase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,35 +46,6 @@ Json::Value BoundControlBase::createMeta() const
return meta;
}

bool BoundControlBase::setValueType()
{
bool hasChanges = false;
AnalysisForm* form = _control->form();

if (_control->encodeValue())
{
JASPListControl* listControl = qobject_cast<JASPListControl*>(_control);
if (listControl)
{
Json::Value jsonTypesOrg = form->boundValue(getName() + ".types", _control->getParentKeys());

columnTypeVec types = listControl->valueTypes();
Json::Value jsonTypes(Json::arrayValue);

for (columnType type : types)
jsonTypes.append(columnTypeToString(type));

if (jsonTypes != jsonTypesOrg)
{
form->setBoundValue(getName() + ".types", jsonTypes, Json::nullValue, _control->getParentKeys());
hasChanges = true;
}
}
}

return hasChanges;
}

void BoundControlBase::handleComputedColumn(const Json::Value& value)
{
if (_isColumn && value.isString())
Expand Down
1 change: 0 additions & 1 deletion QMLComponents/boundcontrols/boundcontrolbase.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ class BoundControlBase : public BoundControl

protected:
std::string getName() const;
bool setValueType();
void handleComputedColumn(const Json::Value& value);

Json::Value _getTableValueOption(const Terms& terms, const ListModel::RowControlsValues& componentValuesMap, const std::string& key, bool hasMultipleTerms);
Expand Down
16 changes: 4 additions & 12 deletions QMLComponents/boundcontrols/boundcontrolterms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,17 +307,6 @@ void BoundControlTerms::resetBoundValue()
}
}

Json::Value BoundControlTerms::_getTypes() const
{
columnTypeVec types = _listView->valueTypes();
Json::Value jsonTypes(Json::arrayValue);

for (columnType type : types)
jsonTypes.append(columnTypeToString(type));

return jsonTypes;
}

void BoundControlTerms::setBoundValue(const Json::Value &value, bool emitChanges)
{
Json::Value newValue;
Expand All @@ -329,7 +318,10 @@ void BoundControlTerms::setBoundValue(const Json::Value &value, bool emitChanges
else
{
newValue["value"] = value;
newValue["types"] = _getTypes();
Json::Value types = _listView->valueTypes();
if (_isSingleRow && types.isArray() && types.size() > 0)
types = types[0];
newValue["types"] = types;
}
if (_listView->hasRowComponent() || _listView->containsInteractions())
newValue["optionKey"] = _optionKey;
Expand Down
1 change: 0 additions & 1 deletion QMLComponents/boundcontrols/boundcontrolterms.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ class BoundControlTerms : public BoundControlBase
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;

ListModelAssignedInterface* _termsModel = nullptr;
JASPListControl* _listView = nullptr;
Expand Down
29 changes: 26 additions & 3 deletions QMLComponents/controls/jasplistcontrol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,12 +296,35 @@ std::vector<std::string> JASPListControl::usedVariables() const
else return {};
}

columnTypeVec JASPListControl::valueTypes() const
Json::Value JASPListControl::valueTypes() const
{
columnTypeVec types;
Json::Value types(Json::arrayValue);
std::map<std::string, std::string> variableTypeMap;
static columnType unknownType = columnType::unknown;

// An interaction term has components that can be variables: if the model contains also such variables, the interaction term should get the same types.
// So first check which terms have only 1 component: these terms might be variable names, so keep in a map their types. Use then this map to set the type for interaction terms.
for (const Term& term : model()->terms())
types.push_back(term.type());
if (term.components().size() == 1)
variableTypeMap[term.asString()] = columnTypeToString(term.type());

for (const Term& term : model()->terms())
{
if (term.components().size() == 1)
types.append(columnTypeToString(term.type()));
else
{
Json::Value compTypes(Json::arrayValue);
for (const std::string& component : term.scomponents())
{
if (variableTypeMap.count(component) > 0)
compTypes.append(variableTypeMap[component]);
else
compTypes.append(columnTypeToString(unknownType));
}
types.append(compTypes);
}
}

return types;
}
Expand Down
2 changes: 1 addition & 1 deletion QMLComponents/controls/jasplistcontrol.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class JASPListControl : public JASPControl
bool allowAnalysisOwnComputedColumns() const { return _allowAnalysisOwnComputedColumns; }
bool isTypeAllowed(columnType type) const;
columnType defaultType() const;
columnTypeVec valueTypes() const;
Json::Value valueTypes() const;
const QStringList & columnsTypes() const { return _columnsTypes; }
const QStringList & columnsNames() const { return _columnsNames; }
QAbstractListModel * allowedTypesModel();
Expand Down

0 comments on commit 331b21e

Please sign in to comment.