Add save and revert button to datagrid no implicit saves any more.
This commit is contained in:
parent
66267334c8
commit
ee1a8ba48d
9 changed files with 153 additions and 12 deletions
|
|
@ -244,6 +244,7 @@ void CrudModel::loadData()
|
|||
void CrudModel::loadIntoModel(std::shared_ptr<Pgsql::Result> data)
|
||||
{
|
||||
beginResetModel();
|
||||
bool was_dirty = hasPendingChanges();
|
||||
m_pendingRowList.clear();
|
||||
m_roData = data;
|
||||
initializeColumnList();
|
||||
|
|
@ -253,6 +254,8 @@ void CrudModel::loadIntoModel(std::shared_ptr<Pgsql::Result> data)
|
|||
if (m_wholeTable)
|
||||
appendNewRowInternal();
|
||||
endResetModel();
|
||||
if (was_dirty)
|
||||
emit pendingChangesChanged(false);
|
||||
}
|
||||
|
||||
void CrudModel::initRowMapping()
|
||||
|
|
@ -321,7 +324,10 @@ bool CrudModel::setData(const QModelIndex &index, const QVariant &value, int rol
|
|||
s.clear();
|
||||
val = s;
|
||||
}
|
||||
bool was_dirty = hasPendingChanges();
|
||||
m_pendingRowList.setValue(col, row_mapping.rowKey, val);
|
||||
if (!was_dirty)
|
||||
emit pendingChangesChanged(true);
|
||||
|
||||
emit dataChanged(index, index, QVector<int>() << role);
|
||||
return true;
|
||||
|
|
@ -611,14 +617,27 @@ void CrudModel::initializeColumnList()
|
|||
}
|
||||
}
|
||||
|
||||
bool CrudModel::submit()
|
||||
bool CrudModel::saveChanges()
|
||||
{
|
||||
return savePendingChanges();
|
||||
bool was_dirty = hasPendingChanges();
|
||||
bool ok = savePendingChanges();
|
||||
if (was_dirty != hasPendingChanges())
|
||||
emit pendingChangesChanged(hasPendingChanges());
|
||||
return ok;
|
||||
}
|
||||
|
||||
void CrudModel::revert()
|
||||
void CrudModel::revertChanges()
|
||||
{
|
||||
if (!hasPendingChanges())
|
||||
return;
|
||||
|
||||
m_pendingRowList.clear();
|
||||
for (auto &row_mapping : m_rowMapping)
|
||||
row_mapping.pending = false;
|
||||
|
||||
if (rowCount() > 0)
|
||||
emit dataChanged(index(0, 0), index(rowCount() - 1, columnCount() - 1));
|
||||
emit pendingChangesChanged(false);
|
||||
}
|
||||
|
||||
void CrudModel::appendNewRow()
|
||||
|
|
@ -678,7 +697,10 @@ std::tuple<bool, QString> CrudModel::removeRows(const std::set<IntegerRange<int>
|
|||
// If something goes wrong after this commit we should reload contents of model
|
||||
}
|
||||
// Then from model
|
||||
bool was_dirty = hasPendingChanges();
|
||||
RemoveRangesOfRowsFromModel(row_ranges);
|
||||
if (was_dirty != hasPendingChanges())
|
||||
emit pendingChangesChanged(hasPendingChanges());
|
||||
return { true, "" };
|
||||
}
|
||||
catch (const Pgsql::PgResultError &error)
|
||||
|
|
@ -713,6 +735,8 @@ void CrudModel::RemoveRangeOfRowsFromModel(IntegerRange<int> range)
|
|||
beginRemoveRows(QModelIndex(), range.start(), range.end() - 1);
|
||||
SCOPE_EXIT { endRemoveRows(); };
|
||||
auto first = m_rowMapping.begin() + range.start();
|
||||
for (auto iter = first; iter != first + range.length(); ++iter)
|
||||
m_pendingRowList.m_rows.erase(iter->rowKey);
|
||||
m_rowMapping.erase(first, first + range.length());
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue