Skip to content

Commit

Permalink
Fix Variables List with layers for contingency table analysis
Browse files Browse the repository at this point in the history
Fixes jasp-stats/INTERNAL-jasp#2683

I'm not sure if a Layer Variables List with several layers has ever worked properly. It was at least not possible to load a JASP file with a contingency table analysis with several layers.
To reproduce the problem, you can also just duplicate the analysis.
  • Loading branch information
boutinb committed Nov 13, 2024
1 parent e6abf6d commit 6677fbd
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 117 deletions.
35 changes: 31 additions & 4 deletions QMLComponents/models/listmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -454,15 +454,42 @@ int ListModel::rowCount(const QModelIndex &) const
return int(terms().size());
}

Terms ListModel::termsFromIndexes(const QList<int> &indexes) const
{
Terms result;

for (int index : indexes)
if (size_t(index) < terms().size())
result.add(terms().at(index));

return result;
}

QList<int> ListModel::indexesFromTerms(const Terms & termsIn) const
{
std::set<int> result;
std::map<Term, int> termToIndex;

for(size_t t=0; t<terms().size(); t++)
termToIndex[terms().at(t)] = t;

for(const Term & term : termsIn)
if(termToIndex.count(term))
result.insert(termToIndex[term]);

return tql(result);
}



QVariant ListModel::data(const QModelIndex &index, int role) const
{
int row = index.row();
const Terms& myTerms = terms();
size_t row_t = size_t(row);
if (row_t >= myTerms.size())
Terms terms = termsFromIndexes({row});
if (terms.size() == 0)
return QVariant();

const Term& term = myTerms.at(row_t);
const Term& term = terms[0];

switch (role)
{
Expand Down
3 changes: 3 additions & 0 deletions QMLComponents/models/listmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ class ListModel : public QAbstractTableModel, public VariableInfoConsumer

Terms checkTermsTypes(const Terms& terms) const;
Terms checkTermsTypes(const std::vector<Term>& terms) const;
virtual Terms termsFromIndexes( const QList<int>& indexes) const;
virtual QList<int> indexesFromTerms( const Terms & terms) const;


Q_INVOKABLE int searchTermWith(QString searchString);
Q_INVOKABLE void selectItem(int _index, bool _select);
Expand Down
26 changes: 0 additions & 26 deletions QMLComponents/models/listmodeldraggable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,32 +36,6 @@ bool ListModelDraggable::keepTerms() const
return variablesList ? variablesList->keepVariablesWhenMoved() : false;
}

Terms ListModelDraggable::termsFromIndexes(const QList<int> &indexes) const
{
Terms result;

for (int index : indexes)
if (size_t(index) < terms().size())
result.add(terms().at(index));

return result;
}

QList<int> ListModelDraggable::indexesFromTerms(const Terms & termsIn) const
{
std::set<int> result;
std::map<Term, int> termToIndex;

for(size_t t=0; t<terms().size(); t++)
termToIndex[terms().at(t)] = t;

for(const Term & term : termsIn)
if(termToIndex.count(term))
result.insert(termToIndex[term]);

return tql(result);
}

void ListModelDraggable::removeTerms(const QList<int> &indices)
{
if(!indices.count())
Expand Down
2 changes: 0 additions & 2 deletions QMLComponents/models/listmodeldraggable.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,10 @@ class ListModelDraggable : public ListModel

void setDropMode(JASPControl::DropMode dropMode) { _dropMode = dropMode; }

virtual QList<int> indexesFromTerms( const Terms & terms) const;
virtual Terms canAddTerms( const Terms & terms) const;
virtual Terms addTerms( const Terms & terms, int dropItemIndex = -1, const RowControlsValues& rowValues = RowControlsValues());
virtual void moveTerms( const QList<int>& indexes, int dropItemIndex = -1);
virtual void removeTerms( const QList<int>& indexes);
virtual Terms termsFromIndexes( const QList<int>& indexes) const;

protected:
JASPControl::DropMode _dropMode = JASPControl::DropMode::DropNone;
Expand Down
Loading

0 comments on commit 6677fbd

Please sign in to comment.