From: Mark Brand Date: Thu, 7 Jul 2011 09:52:11 +0000 (+0200) Subject: refactor QSqlTableModel::setData() X-Git-Tag: qt-v5.0.0-alpha1~1314 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=f6e3f2468357ceece00ef05a1a17b63524eae1d9;p=profile%2Fivi%2Fqtbase.git refactor QSqlTableModel::setData() -move logic out of switch statement -clear cache more clearly for OnFieldChange -call setValue() in one place instead of two -eliminate extra return path Existing idiosyncracies have been kept for the time being. Change-Id: Ia4c5a5fd3e374b53e3c4d870f2ee9d37b5090917 Reviewed-by: Yunqiao Yin --- diff --git a/src/sql/models/qsqltablemodel.cpp b/src/sql/models/qsqltablemodel.cpp index 79149a0..298cd03 100644 --- a/src/sql/models/qsqltablemodel.cpp +++ b/src/sql/models/qsqltablemodel.cpp @@ -475,51 +475,33 @@ bool QSqlTableModel::setData(const QModelIndex &index, const QVariant &value, in if (!index.isValid() || index.column() >= d->rec.count() || index.row() >= rowCount()) return false; - bool isOk = true; - switch (d->strategy) { - case OnFieldChange: { - QSqlTableModelPrivate::ModifiedRow &row = d->cache[index.row()]; - if (row.op == QSqlTableModelPrivate::Insert) { - row.setValue(index.column(), value); - return true; - } + if (d->strategy == OnFieldChange && d->cache.value(index.row()).op != QSqlTableModelPrivate::Insert) { + d->cache.clear(); + } else if (d->strategy == OnRowChange && !d->cache.isEmpty() && !d->cache.contains(index.row())) { + submit(); + d->cache.clear(); + } + + QSqlTableModelPrivate::ModifiedRow &row = d->cache[index.row()]; + + if (row.op == QSqlTableModelPrivate::None) { row = QSqlTableModelPrivate::ModifiedRow(QSqlTableModelPrivate::Update, d->rec, d->primaryValues(indexInQuery(index).row())); - row.setValue(index.column(), value); + } + + row.setValue(index.column(), value); + + bool isOk = true; + if (d->strategy == OnFieldChange && row.op != QSqlTableModelPrivate::Insert) { isOk = updateRowInTable(index.row(), row.rec); if (isOk) select(); - emit dataChanged(index, index); - break; } - case OnRowChange: { - if (!d->cache.isEmpty() && !d->cache.contains(index.row())) { - submit(); - d->cache.clear(); - } - QSqlTableModelPrivate::ModifiedRow &row = d->cache[index.row()]; - if (row.op == QSqlTableModelPrivate::Insert) { - row.setValue(index.column(), value); - return true; - } else if (row.op == QSqlTableModelPrivate::None) { - row = QSqlTableModelPrivate::ModifiedRow(QSqlTableModelPrivate::Update, - d->rec, - d->primaryValues(indexInQuery(index).row())); - } - row.setValue(index.column(), value); - emit dataChanged(index, index); - break; } - case OnManualSubmit: { - QSqlTableModelPrivate::ModifiedRow &row = d->cache[index.row()]; - if (row.op == QSqlTableModelPrivate::None) { - row = QSqlTableModelPrivate::ModifiedRow(QSqlTableModelPrivate::Update, - d->rec, - d->primaryValues(indexInQuery(index).row())); - } - row.setValue(index.column(), value); - emit dataChanged(index, index); - break; } } + + if (d->strategy == OnManualSubmit || row.op != QSqlTableModelPrivate::Insert) + emit dataChanged(index, index); + return isOk; }