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
|
|
@ -1,9 +1,13 @@
|
|||
#include "DataGridWidget.h"
|
||||
#include "CrudModel.h"
|
||||
#include "CustomDataRole.h"
|
||||
#include "ResultTableModelUtil.h"
|
||||
#include "util/PgLabItemDelegate.h"
|
||||
#include <QAction>
|
||||
#include <QSortFilterProxyModel>
|
||||
#include <QStyle>
|
||||
#include <QTableView>
|
||||
#include <QToolBar>
|
||||
#include <QVBoxLayout>
|
||||
|
||||
|
||||
|
|
@ -13,6 +17,25 @@ DataGridWidget::DataGridWidget(QWidget *parent)
|
|||
auto layout = new QVBoxLayout(this);
|
||||
layout->setContentsMargins(0, 0, 0, 0);
|
||||
|
||||
m_toolbar = new QToolBar(this);
|
||||
m_toolbar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
|
||||
layout->addWidget(m_toolbar);
|
||||
|
||||
m_actSave = new QAction(style()->standardIcon(QStyle::SP_DialogSaveButton), tr("Save"), this);
|
||||
m_actSave->setShortcut(QKeySequence::Save);
|
||||
m_actSave->setShortcutContext(Qt::WidgetWithChildrenShortcut);
|
||||
m_actSave->setToolTip(tr("Save all pending changes to the database (Ctrl+S)"));
|
||||
m_actSave->setEnabled(false);
|
||||
addAction(m_actSave);
|
||||
m_toolbar->addAction(m_actSave);
|
||||
connect(m_actSave, &QAction::triggered, this, &DataGridWidget::onSave);
|
||||
|
||||
m_actRevert = new QAction(style()->standardIcon(QStyle::SP_DialogResetButton), tr("Revert"), this);
|
||||
m_actRevert->setToolTip(tr("Discard all pending changes"));
|
||||
m_actRevert->setEnabled(false);
|
||||
m_toolbar->addAction(m_actRevert);
|
||||
connect(m_actRevert, &QAction::triggered, this, &DataGridWidget::onRevert);
|
||||
|
||||
m_view = new QTableView(this);
|
||||
layout->addWidget(m_view);
|
||||
|
||||
|
|
@ -27,8 +50,52 @@ void DataGridWidget::setSourceModel(QAbstractItemModel *model)
|
|||
m_proxy = new QSortFilterProxyModel(this);
|
||||
m_proxy->setSortRole(CustomSortRole);
|
||||
}
|
||||
disconnect(m_pendingChangesConnection);
|
||||
m_proxy->setSourceModel(model);
|
||||
m_view->setModel(m_proxy);
|
||||
m_view->setSortingEnabled(true);
|
||||
m_proxy->sort(0, Qt::AscendingOrder);
|
||||
|
||||
auto crud = qobject_cast<CrudModel *>(model);
|
||||
m_toolbar->setVisible(crud != nullptr);
|
||||
if (crud) {
|
||||
m_pendingChangesConnection = connect(crud, &CrudModel::pendingChangesChanged, this, &DataGridWidget::onPendingChangesChanged);
|
||||
onPendingChangesChanged(crud->hasPendingChanges());
|
||||
}
|
||||
else {
|
||||
onPendingChangesChanged(false);
|
||||
}
|
||||
}
|
||||
|
||||
void DataGridWidget::onPendingChangesChanged(bool hasChanges)
|
||||
{
|
||||
m_actSave->setEnabled(hasChanges);
|
||||
m_actRevert->setEnabled(hasChanges);
|
||||
}
|
||||
|
||||
void DataGridWidget::onSave()
|
||||
{
|
||||
auto crud = qobject_cast<CrudModel *>(m_proxy->sourceModel());
|
||||
if (!crud)
|
||||
return;
|
||||
|
||||
commitOpenEditor();
|
||||
crud->saveChanges();
|
||||
}
|
||||
|
||||
void DataGridWidget::onRevert()
|
||||
{
|
||||
auto crud = qobject_cast<CrudModel *>(m_proxy->sourceModel());
|
||||
if (!crud)
|
||||
return;
|
||||
|
||||
commitOpenEditor();
|
||||
crud->revertChanges();
|
||||
}
|
||||
|
||||
void DataGridWidget::commitOpenEditor()
|
||||
{
|
||||
// Moving focus away from the editor makes the delegate commit its value, a toolbar click does not take focus
|
||||
if (m_view->indexWidget(m_view->currentIndex()))
|
||||
m_view->setFocus();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue