/*! Sets the values at the specified \a row to the values of \a record. Returns true if all the values could be set; otherwise returns false. \sa record() */ bool QSqlTableModel::setRecord(int row, const QSqlRecord &record) { Q_D(QSqlTableModel); Q_ASSERT_X(row >= 0, "QSqlTableModel::setRecord()", "Cannot set a record to a row less than 0"); if (row >= rowCount()) return false; bool isOk = true; switch (d->strategy) { case OnFieldChange: case OnRowChange: return d->setRecord(row, record); case OnManualSubmit: { QSqlTableModelPrivate::ModifiedRow &mrow = d->cache[row]; if (mrow.op == QSqlTableModelPrivate::None) { mrow.op = QSqlTableModelPrivate::Update; mrow.rec = d->rec; mrow.primaryValues = d->primaryValues(indexInQuery(createIndex(row, 0)).row()); } for (int i = 0; i < record.count(); ++i) { int idx = mrow.rec.indexOf(record.fieldName(i)); if (idx == -1) isOk = false; else mrow.rec.setValue(idx, record.value(i)); } return isOk; } } return false; }
/*! \reimp */ QVariant QSqlTableModel::data(const QModelIndex &index, int role) const { Q_D(const QSqlTableModel); if (!index.isValid() || (role != Qt::DisplayRole && role != Qt::EditRole)) return QVariant(); QModelIndex item = indexInQuery(index); switch (d->strategy) { case OnFieldChange: case OnRowChange: if (index.row() == d->insertIndex) { QVariant val; if (item.column() < 0 || item.column() >= d->rec.count()) return val; val = d->editBuffer.value(index.column()); if (val.type() == QVariant::Invalid) val = QVariant(d->rec.field(item.column()).type()); return val; } if (d->editIndex == item.row()) { QVariant var = d->editBuffer.value(item.column()); if (var.isValid()) return var; } break; case OnManualSubmit: { const QSqlTableModelPrivate::ModifiedRow row = d->cache.value(index.row()); const QVariant var = row.rec.value(item.column()); if (var.isValid() || row.op == QSqlTableModelPrivate::Insert) return var; break; } } return QSqlQueryModel::data(item, role); }
QVariant TableModel::data(const QModelIndex &index, int role) const { QModelIndex item = indexInQuery(index); //Align integers to the right if ((index.isValid() && role == Qt::TextAlignmentRole) && index.data().type() == QVariant::Int) { return Qt::AlignVCenter + Qt::AlignRight; } //Disable all roles except DisplayRole and EditRole if (!index.isValid() || (role != Qt::DisplayRole && role != Qt::EditRole)) return QVariant(); //Store the index into item to call the sibling of index. //QModelIndex item = indexInQuery(index); //Return cached values for UPDATEd data if(cache_values.contains(index)) { if(cache_values.value(index).type() == QVariant::Int && role == Qt::TextAlignmentRole) return Qt::AlignVCenter + Qt::AlignRight; return cache_values.value(index); } //Return sibling of index return QSqlQueryModel::data(index.sibling(item.row(), index.column()), role); }
/*! Sets the data for the item \a index for the role \a role to \a value. Depending on the edit strategy, the value might be applied to the database at once or cached in the model. Returns true if the value could be set or false on error, for example if \a index is out of bounds. \sa editStrategy(), data(), submit(), submitAll(), revertRow() */ bool QSqlTableModel::setData(const QModelIndex &index, const QVariant &value, int role) { Q_D(QSqlTableModel); if (role != Qt::EditRole) return QSqlQueryModel::setData(index, value, role); if (!index.isValid() || index.column() >= d->rec.count() || index.row() >= rowCount()) return false; bool isOk = true; switch (d->strategy) { case OnFieldChange: { if (index.row() == d->insertIndex) { QSqlTableModelPrivate::setGeneratedValue(d->editBuffer, index.column(), value); return true; } d->clearEditBuffer(); QSqlTableModelPrivate::setGeneratedValue(d->editBuffer, index.column(), value); isOk = updateRowInTable(index.row(), d->editBuffer); if (isOk) select(); emit dataChanged(index, index); break; } case OnRowChange: if (index.row() == d->insertIndex) { QSqlTableModelPrivate::setGeneratedValue(d->editBuffer, index.column(), value); return true; } if (d->editIndex != index.row()) { if (d->editIndex != -1) submit(); d->clearEditBuffer(); } QSqlTableModelPrivate::setGeneratedValue(d->editBuffer, index.column(), value); d->editIndex = index.row(); emit dataChanged(index, index); break; case OnManualSubmit: { QSqlTableModelPrivate::ModifiedRow &row = d->cache[index.row()]; if (row.op == QSqlTableModelPrivate::None) { row.op = QSqlTableModelPrivate::Update; row.rec = d->rec; QSqlTableModelPrivate::clearGenerated(row.rec); row.primaryValues = d->primaryValues(indexInQuery(index).row()); } QSqlTableModelPrivate::setGeneratedValue(row.rec, index.column(), value); emit dataChanged(index, index); break; } } return isOk; }
/*! Returns the header data for the given \a role in the \a section of the header with the specified \a orientation. */ QVariant QSqlQueryModel::headerData(int section, Qt::Orientation orientation, int role) const { Q_D(const QSqlQueryModel); if (orientation == Qt::Horizontal) { QVariant val = d->headers.value(section).value(role); if (role == Qt::DisplayRole && !val.isValid()) val = d->headers.value(section).value(Qt::EditRole); if (val.isValid()) return val; // See if it's an inserted column (iiq.column() != -1) QModelIndex dItem = indexInQuery(createIndex(0, section)); if (role == Qt::DisplayRole && d->rec.count() > section && dItem.column() != -1) return d->rec.fieldName(section); } return QAbstractItemModel::headerData(section, orientation, role); }
QVariant QueryModel::data(const QModelIndex &index, int role) const { //Store the index into item to call the sibling of index. QModelIndex item = indexInQuery(index); //Align integers to the right if ((index.isValid() && role == Qt::TextAlignmentRole) && (index.data().type() != QMetaType::QString)) return (Qt::AlignVCenter + Qt::AlignRight); if(index.isValid() && role == Qt::BackgroundRole && index.column() == pivot_col) return QColor(255, 0, 0, 100); if(index.isValid() && role == Qt::BackgroundRole && index.column() == pivot_cat) return QColor(0, 255, 0, 100); //Disable all roles except DisplayRole if (!index.isValid() || (role != Qt::DisplayRole)) return QVariant(); //Return sibling of index return QSqlQueryModel::data(index.sibling(item.row(), index.column()), role); }
/*! Sets the values at the specified \a row to the values of \a record. Returns true if all the values could be set; otherwise returns false. \sa record() */ bool QSqlTableModel::setRecord(int row, const QSqlRecord &record) { Q_D(QSqlTableModel); Q_ASSERT_X(row >= 0, "QSqlTableModel::setRecord()", "Cannot set a record to a row less than 0"); if (row >= rowCount()) return false; bool isOk = true; switch (d->strategy) { case OnFieldChange: case OnRowChange: return d->setRecord(row, record); case OnManualSubmit: { QSqlTableModelPrivate::ModifiedRow &mrow = d->cache[row]; if (mrow.op == QSqlTableModelPrivate::None) { mrow.op = QSqlTableModelPrivate::Update; mrow.rec = d->rec; QSqlTableModelPrivate::clearGenerated(mrow.rec); mrow.primaryValues = d->primaryValues(indexInQuery(createIndex(row, 0)).row()); } QString fieldName; for (int i = 0; i < record.count(); ++i) { fieldName = record.fieldName(i); if (d->db.driver()->isIdentifierEscaped(fieldName, QSqlDriver::FieldName)) fieldName = d->db.driver()->stripDelimiters(fieldName, QSqlDriver::FieldName); int idx = mrow.rec.indexOf(fieldName); if (idx == -1) { isOk = false; } else { QSqlTableModelPrivate::setGeneratedValue(mrow.rec, idx, record.value(i)); } } if (isOk) emit dataChanged(createIndex(row, 0), createIndex(row, columnCount() - 1)); return isOk; } } return false; }
/*! Removes \a count rows starting at \a row. Since this model does not support hierarchical structures, \a parent must be an invalid model index. Emits the beforeDelete() signal before a row is deleted. When the edit strategy is OnManualSubmit signal emission is delayed until submitAll() is called. Returns true if all rows could be removed; otherwise returns false. Detailed error information can be retrieved using lastError(). \sa removeColumns(), insertRows() */ bool QSqlTableModel::removeRows(int row, int count, const QModelIndex &parent) { Q_D(QSqlTableModel); if (parent.isValid() || row < 0 || count <= 0) return false; int i; switch (d->strategy) { case OnFieldChange: case OnRowChange: for (i = 0; i < count; ++i) { if (row + i == d->insertIndex) d->revertInsertedRow(); else if (!deleteRowFromTable(row + i)) return false; } select(); break; case OnManualSubmit: for (i = 0; i < count; ++i) { int idx = row + i; if (idx >= rowCount()) return false; if (d->cache.value(idx).op == QSqlTableModelPrivate::Insert) { revertRow(idx); // Reverting a row means all the other cache entries have been adjusted downwards // so fake this by adjusting row --row; } else { d->cache[idx].op = QSqlTableModelPrivate::Delete; d->cache[idx].primaryValues = d->primaryValues(indexInQuery(createIndex(idx, 0)).row()); emit headerDataChanged(Qt::Vertical, idx, idx); } } break; } return true; }
/*! Returns the value for the specified \a item and \a role. If \a item is out of bounds or if an error occurred, an invalid QVariant is returned. \sa lastError() */ QVariant QSqlQueryModel::data(const QModelIndex &item, int role) const { Q_D(const QSqlQueryModel); if (!item.isValid()) return QVariant(); QVariant v; if (role & ~(Qt::DisplayRole | Qt::EditRole)) return v; if (!d->rec.isGenerated(item.column())) return v; QModelIndex dItem = indexInQuery(item); if (dItem.row() > d->bottom.row()) const_cast<QSqlQueryModelPrivate *>(d)->prefetch(dItem.row()); if (!d->query.seek(dItem.row())) { d->error = d->query.lastError(); return v; } return d->query.value(dItem.column()); }
/*! \reimp */ QVariant QSqlTableModel::data(const QModelIndex &index, int role) const { Q_D(const QSqlTableModel); 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); switch (d->strategy) { case OnFieldChange: case OnRowChange: if (index.row() == d->insertIndex) { if (item.column() < 0 || item.column() >= d->rec.count()) return QVariant(); return d->editBuffer.value(index.column()); } if (d->editIndex == item.row()) { if (d->editBuffer.isGenerated(item.column())) return d->editBuffer.value(item.column()); } break; case OnManualSubmit: if (d->cache.contains(index.row())) { const QSqlTableModelPrivate::ModifiedRow row = d->cache.value(index.row()); if (row.rec.isGenerated(item.column()) || row.op == QSqlTableModelPrivate::Insert) return row.rec.value(item.column()); } break; } // We need to handle row mapping here, but not column mapping return QSqlQueryModel::data(index.sibling(item.row(), index.column()), role); }