Exemple #1
0
/*!
    Inserts \a count empty rows at position \a row. Note that \a
    parent must be invalid, since this model does not support
    parent-child relations.

    Only one row at a time can be inserted when using the
    OnFieldChange or OnRowChange update strategies.

    The primeInsert() signal will be emitted for each new row.
    Connect to it if you want to initialize the new row with default
    values.

    Returns false if the parameters are out of bounds; otherwise
    returns true.

    \sa primeInsert(), insertRecord()
*/
bool QSqlTableModel::insertRows(int row, int count, const QModelIndex &parent)
{
    Q_D(QSqlTableModel);
    if (row < 0 || count <= 0 || row > rowCount() || parent.isValid())
        return false;

    switch (d->strategy) {
    case OnFieldChange:
    case OnRowChange:
        if (count != 1)
            return false;
        beginInsertRows(parent, row, row);
        d->insertIndex = row;
        // ### apply dangling changes...
        d->clearEditBuffer();
        emit primeInsert(row, d->editBuffer);
        break;
    case OnManualSubmit:
        beginInsertRows(parent, row, row + count - 1);
        if (!d->cache.isEmpty()) {
            QMap<int, QSqlTableModelPrivate::ModifiedRow>::Iterator it = d->cache.end();
            while (it != d->cache.begin() && (--it).key() >= row) {
                int oldKey = it.key();
                const QSqlTableModelPrivate::ModifiedRow oldValue = it.value();
                d->cache.erase(it);
                it = d->cache.insert(oldKey + count, oldValue);
            }
        }

        for (int i = 0; i < count; ++i) {
            d->cache[row + i] = QSqlTableModelPrivate::ModifiedRow(QSqlTableModelPrivate::Insert,
                                                                   d->rec);
            emit primeInsert(row + i, d->cache[row + i].rec);
        }
        break;
    }
    endInsertRows();
    return true;
}
Exemple #2
0
void Q3DataBrowser::insert()
{
    QSqlRecord* buf = d->frm.record();
    Q3SqlCursor* cur = d->cur.cursor();
    if (!buf || !cur)
        return;
    bool doIns = true;
    QSql::Confirm conf = QSql::Yes;
    switch (d->dat.mode()) {
    case QSql::Insert:
        if (autoEdit()) {
            if (confirmInsert())
                conf = confirmEdit(QSql::Insert);
            switch (conf) {
            case QSql::Yes:
                insertCurrent();
                break;
            case QSql::No:
                break;
            case QSql::Cancel:
                doIns = false;
                break;
            }
        }
        break;
    default:
        if (autoEdit() && currentEdited()) {
            if (confirmUpdate())
                conf = confirmEdit(QSql::Update);
            switch (conf) {
            case QSql::Yes:
                updateCurrent();
                break;
            case QSql::No:
                break;
            case QSql::Cancel:
                doIns = false;
                break;
            }
        }
        break;
    }
    if (doIns) {
        d->dat.setMode(QSql::Insert);
        sqlCursor()->primeInsert();
        emit primeInsert(d->frm.record());
        readFields();
    }
}
/*!
    Inserts \a count empty rows at position \a row. Note that \a
    parent must be invalid, since this model does not support
    parent-child relations.

    For edit strategies OnFieldChange and OnRowChange, only one row
    may be inserted at a time and the model may not contain other
    cached changes.

    The primeInsert() signal will be emitted for each new row.
    Connect to it if you want to initialize the new row with default
    values.

    Does not submit rows, regardless of edit strategy.

    Returns \c false if the parameters are out of bounds or the row cannot be
    inserted; otherwise returns \c true.

    \sa primeInsert(), insertRecord()
*/
bool QSqlTableModel::insertRows(int row, int count, const QModelIndex &parent)
{
    Q_D(QSqlTableModel);
    if (row < 0 || count <= 0 || row > rowCount() || parent.isValid())
        return false;

    if (d->strategy != OnManualSubmit)
        if (count != 1 || isDirty())
            return false;

    d->busyInsertingRows = true;
    beginInsertRows(parent, row, row + count - 1);

    if (d->strategy != OnManualSubmit)
        d->cache.empty();

    if (!d->cache.isEmpty()) {
        QMap<int, QSqlTableModelPrivate::ModifiedRow>::Iterator it = d->cache.end();
        while (it != d->cache.begin() && (--it).key() >= row) {
            int oldKey = it.key();
            const QSqlTableModelPrivate::ModifiedRow oldValue = it.value();
            d->cache.erase(it);
            it = d->cache.insert(oldKey + count, oldValue);
        }
    }

    for (int i = 0; i < count; ++i) {
        d->cache[row + i] = QSqlTableModelPrivate::ModifiedRow(QSqlTableModelPrivate::Insert,
                                                               d->rec);
        emit primeInsert(row + i, d->cache[row + i].recRef());
    }

    endInsertRows();
    d->busyInsertingRows = false;
    return true;
}