QSql*Model: make indexInQuery() virtual
authorMark Brand <mabrand@mabrand.nl>
Thu, 9 Feb 2012 13:53:35 +0000 (14:53 +0100)
committerQt by Nokia <qt-info@nokia.com>
Wed, 15 Feb 2012 01:36:07 +0000 (02:36 +0100)
Qt 5 seems like an excellent opportunity to simplify logic and separate
concerns by making indexInQuery() virtual. Note that this wasn't my
idea, but was mentioned in a helpful comment.

Change-Id: Ie29ead110def45297c32de3ce6d07a8eefb08d8c
Reviewed-by: Yunqiao Yin <charles.yin@nokia.com>
dist/changes-5.0.0
src/sql/models/qsqlquerymodel.h
src/sql/models/qsqltablemodel.cpp

index bb4ceef..884c308 100644 (file)
@@ -213,6 +213,8 @@ information about a particular change.
 
 - QSystemLocale has been removed from the public API.
 
+- QSqlQueryModel::indexInQuery() is now virtual. See note below under QSql.
+
 ****************************************************************************
 *                           General                                        *
 ****************************************************************************
@@ -368,6 +370,10 @@ ignore the rest of the range.
   -For OnManualSubmit, insertRecord() no longer leaves behind an empty
   row if setRecord() fails.
 
+* QSqlQueryModel::indexInQuery() is now virtual. See
+QSqlTableModel::indexInQuery() as example of how to implement in a
+subclass.
+
 ****************************************************************************
 *                          Database Drivers                                *
 ****************************************************************************
index 7db973b..b5e1a7a 100644 (file)
@@ -92,7 +92,7 @@ public:
 protected:
     virtual void queryChange();
 
-    QModelIndex indexInQuery(const QModelIndex &item) const;
+    virtual QModelIndex indexInQuery(const QModelIndex &item) const;
     void setLastError(const QSqlError &error);
     QSqlQueryModel(QSqlQueryModelPrivate &dd, QObject *parent = 0);
 };
index 29f29ba..ab87397 100644 (file)
@@ -384,12 +384,6 @@ QVariant QSqlTableModel::data(const QModelIndex &index, int role) const
     if (!index.isValid() || (role != Qt::DisplayRole && role != Qt::EditRole))
         return QVariant();
 
-    // Problem.. we need to use QSQM::indexInQuery to handle inserted columns
-    // but inserted rows we need to handle
-    // and indexInQuery is not virtual (grrr) so any values we pass to QSQM need
-    // to handle the insertedRows
-    QModelIndex item = indexInQuery(index);
-
     if (d->cache.contains(index.row())) {
         const QSqlTableModelPrivate::ModifiedRow row = d->cache.value(index.row());
 
@@ -397,25 +391,24 @@ QVariant QSqlTableModel::data(const QModelIndex &index, int role) const
         case OnFieldChange:
         case OnRowChange:
             if (row.op() == QSqlTableModelPrivate::Insert) {
-                if (item.column() < 0 || item.column() >= row.rec().count())
+                if (index.column() < 0 || index.column() >= row.rec().count())
                     return QVariant();
-                return row.rec().value(item.column());
+                return row.rec().value(index.column());
             } else if (row.op() == QSqlTableModelPrivate::Update) {
-                if (row.rec().isGenerated(item.column()))
-                    return row.rec().value(item.column());
+                if (row.rec().isGenerated(index.column()))
+                    return row.rec().value(index.column());
             }
             break;
         case OnManualSubmit:
             if (row.op() == QSqlTableModelPrivate::Insert
                 || (row.op() != QSqlTableModelPrivate::None
-                    && row.rec().isGenerated(item.column())))
-                return row.rec().value(item.column());
+                    && row.rec().isGenerated(index.column())))
+                return row.rec().value(index.column());
             break;
         }
     }
 
-    // We need to handle row mapping here, but not column mapping
-    return QSqlQueryModel::data(index.sibling(item.row(), index.column()), role);
+    return QSqlQueryModel::data(index, role);
 }
 
 /*!
@@ -1122,15 +1115,14 @@ int QSqlTableModel::rowCount(const QModelIndex &parent) const
 QModelIndex QSqlTableModel::indexInQuery(const QModelIndex &item) const
 {
     Q_D(const QSqlTableModel);
-    const QModelIndex it = QSqlQueryModel::indexInQuery(item); // this adjusts columns only
     int rowOffset = 0;
     QSqlTableModelPrivate::CacheMap::ConstIterator i = d->cache.constBegin();
-    while (i != d->cache.constEnd() && i.key() <= it.row()) {
+    while (i != d->cache.constEnd() && i.key() <= item.row()) {
         if (i.value().op() == QSqlTableModelPrivate::Insert)
             ++rowOffset;
         ++i;
     }
-    return createIndex(it.row() - rowOffset, it.column(), it.internalPointer());
+    return QSqlQueryModel::indexInQuery(createIndex(item.row() - rowOffset, item.column(), item.internalPointer()));
 }
 
 /*!