Skip to content

Commit 90ca287

Browse files
authored
Save changed TextField when adding a new row (#6054)
* Save changed TextField when adding a new row Fixes jasp-stats/jasp-issues#3812 * Optimize also when removing rows
1 parent 31c5b1d commit 90ca287

File tree

3 files changed

+37
-23
lines changed

3 files changed

+37
-23
lines changed

QMLComponents/models/listmodel.cpp

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -190,24 +190,30 @@ void ListModel::setRowComponent(QQmlComponent* rowComponent)
190190
_rowComponent = rowComponent;
191191
}
192192

193-
void ListModel::setUpRowControls()
193+
void ListModel::setUpRowControls(int startRow, bool onlyRemove)
194194
{
195195
if (_rowComponent == nullptr)
196196
return;
197197

198-
int row = 0;
199-
for (const Term& term : terms())
198+
if (!onlyRemove)
200199
{
201-
if (!_rowControlsMap.contains(term.value()))
200+
int row = 0;
201+
for (const Term& term : terms())
202202
{
203-
RowControls* rowControls = new RowControls(this, _rowComponent);
204-
_rowControlsMap[term.value()] = rowControls;
205-
rowControls->initValues(row, term, _rowControlsValues[term.value()]);
206-
}
207-
else
208-
_rowControlsMap[term.value()]->resetValues(row, term, _rowControlsValues[term.value()]);
203+
if (startRow <= row)
204+
{
205+
if (!_rowControlsMap.contains(term.value()))
206+
{
207+
RowControls* rowControls = new RowControls(this, _rowComponent);
208+
_rowControlsMap[term.value()] = rowControls;
209+
rowControls->initValues(row, term, _rowControlsValues[term.value()]);
210+
}
211+
else
212+
_rowControlsMap[term.value()]->resetValues(row, term, _rowControlsValues[term.value()]);
213+
}
209214

210-
row++;
215+
row++;
216+
}
211217
}
212218

213219
// Disconnect and delete all controls that are not used anymore
@@ -785,37 +791,40 @@ void ListModel::_setTerms(const Terms &terms)
785791
void ListModel::_removeTerms(const Terms &terms)
786792
{
787793
_terms.remove(terms);
788-
setUpRowControls();
794+
setUpRowControls(0, true);
789795
}
790796

791797
void ListModel::_removeTerm(int index)
792798
{
793799
_terms.remove(size_t(index));
794-
setUpRowControls();
800+
setUpRowControls(0, true);
795801
}
796802

797803
void ListModel::_removeTerm(const Term &term)
798804
{
799805
_terms.remove(term);
800-
setUpRowControls();
806+
setUpRowControls(0, true);
801807
}
802808

803809
void ListModel::_removeLastTerm()
804810
{
805811
if (_terms.size() > 0)
806812
_terms.remove(_terms.size() - 1);
813+
setUpRowControls(0, true);
807814
}
808815

809816
void ListModel::_addTerms(const Terms &terms)
810817
{
818+
int i = _terms.size();
811819
_terms.add(checkTermsTypes(terms));
812-
setUpRowControls();
820+
setUpRowControls(i);
813821
}
814822

815823
void ListModel::_addTerm(const Term &term, bool isUnique)
816824
{
825+
int i = _terms.size();
817826
_terms.add(_checkTermType(term), isUnique);
818-
setUpRowControls();
827+
setUpRowControls(i);
819828
}
820829

821830
void ListModel::_replaceTerm(int index, const Term &term)

QMLComponents/models/listmodel.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class ListModel : public QAbstractTableModel, public VariableInfoConsumer
7676
Terms getSourceTerms();
7777
void setColumnsUsedForLabels(const QStringList& columns) { _columnsUsedForLabels = columns; }
7878
void setRowComponent(QQmlComponent* rowComponents);
79-
virtual void setUpRowControls();
79+
virtual void setUpRowControls(int startRow = 0, bool onlyRemove = false);
8080
const RowControlMap & getAllRowControls() const { return _rowControlsMap; }
8181
Terms::RelatedValuesPerTerm getTermsWithComponentValues() const;
8282
RowControls* getRowControls(const QString& key) const { return _rowControlsMap.value(key); }

QMLComponents/models/listmodeltermsassigned.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,17 +102,22 @@ Terms ListModelTermsAssigned::addTerms(const Terms& termsToAdd, int dropItemInde
102102
// in a TabView, if the user changes the title of a Tab and clicks direclty the '+' button to add another tab, adding a new tab will be done first, and will add a new
103103
// term to the model: if the model of the TabView is reset, the TextField controls that handle the titles of the Tabs are destroyed and recreated. As the TextField control
104104
// that was used to change the title is destroyed, the signal that changes this title is not received, and the title gets back its old value.
105+
if (dropItemIndex < 0 || dropItemIndex > terms().size())
106+
dropItemIndex = terms().size();
107+
108+
beginInsertRows(QModelIndex(), dropItemIndex, dropItemIndex + termsToAdd.size() - 1);
105109
newTerms = terms();
106-
if (dropItemIndex >= 0 && dropItemIndex < terms().size())
110+
if (dropItemIndex < terms().size())
111+
{
107112
newTerms.insert(dropItemIndex, termsToAdd);
113+
_setTerms(newTerms);
114+
}
108115
else
109116
{
110-
dropItemIndex = terms().size();
111117
newTerms.add(termsToAdd);
118+
_addTerms(termsToAdd);
112119
}
113120

114-
beginInsertRows(QModelIndex(), dropItemIndex, dropItemIndex + termsToAdd.size() - 1);
115-
_setTerms(newTerms);
116121
endInsertRows();
117122

118123
if (maxRows > 0 && newTerms.size() > maxRows)
@@ -136,7 +141,7 @@ void ListModelTermsAssigned::removeTerm(int index)
136141
{
137142
if (index < 0 || index >= rowCount()) return;
138143

139-
beginResetModel();
144+
beginRemoveRows(QModelIndex(), index, index);
140145

141146
const Term& term = terms().at(size_t(index));
142147

@@ -153,7 +158,7 @@ void ListModelTermsAssigned::removeTerm(int index)
153158
}
154159
_removeTerm(term);
155160

156-
endResetModel();
161+
endRemoveRows();
157162
}
158163

159164
void ListModelTermsAssigned::changeTerm(int index, const Term& term)

0 commit comments

Comments
 (0)