Using QDataWidgetMapper in ItemDetailMapper to work with the model data (current index of the TableView).
This commit is contained in:
@ -1,14 +1,30 @@
|
||||
#include "itemdetailmapper.h"
|
||||
|
||||
#include <QAbstractItemModel>
|
||||
#include <QDataWidgetMapper>
|
||||
#include <QGridLayout>
|
||||
#include <QJsonArray>
|
||||
#include <QJsonObject>
|
||||
#include <QLabel>
|
||||
#include <QLineEdit>
|
||||
#include <QPushButton>
|
||||
#include <QSpinBox>
|
||||
#include <QTableView>
|
||||
|
||||
ItemDetailMapper::ItemDetailMapper(QWidget* parent)
|
||||
: QWidget{parent} {
|
||||
/// model mapping
|
||||
m_mapper = std::make_unique<QDataWidgetMapper>(this);
|
||||
m_mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
|
||||
|
||||
/// model mapping buttons
|
||||
m_nextButton = new QPushButton(tr("Ne&xt"));
|
||||
m_previousButton = new QPushButton(tr("&Previous"));
|
||||
|
||||
connect(m_previousButton, &QAbstractButton::clicked, this, &ItemDetailMapper::toPrevious);
|
||||
connect(m_nextButton, &QAbstractButton::clicked, this, &ItemDetailMapper::toNext);
|
||||
|
||||
/// the individual widgets
|
||||
// REFACTOR deduce label names and types from meta data & use a factory
|
||||
m_nameLabel = new QLabel("&Name");
|
||||
m_nameEdit = new QLineEdit();
|
||||
@ -44,5 +60,83 @@ ItemDetailMapper::ItemDetailMapper(QWidget* parent)
|
||||
layout->addWidget(m_factorLabel, 4, 0, 1, 1);
|
||||
layout->addWidget(m_factorBox, 4, 1, 1, 1);
|
||||
|
||||
layout->addWidget(m_previousButton, 5, 0, 1, 1);
|
||||
layout->addWidget(m_nextButton, 5, 1, 1, 1);
|
||||
|
||||
setLayout(layout);
|
||||
}
|
||||
|
||||
void ItemDetailMapper::setModelMappings(QTableView* tableView) {
|
||||
qDebug() << "Setting model...";
|
||||
m_tableView = tableView;
|
||||
m_model = tableView->model();
|
||||
m_selectionModel = tableView->selectionModel();
|
||||
|
||||
m_mapper->setModel(m_model);
|
||||
m_mapper->addMapping(m_nameEdit, 0);
|
||||
m_mapper->addMapping(m_descriptionEdit, 1);
|
||||
m_mapper->addMapping(m_infoEdit, 2);
|
||||
m_mapper->addMapping(m_amountBox, 3);
|
||||
m_mapper->addMapping(m_factorBox, 4);
|
||||
|
||||
m_mapper->setCurrentIndex(m_selectionModel->currentIndex().row());
|
||||
|
||||
connect(m_model, &QAbstractItemModel::rowsInserted, this, &ItemDetailMapper::rowsInserted);
|
||||
connect(m_model, &QAbstractItemModel::rowsRemoved, this, &ItemDetailMapper::rowsRemoved);
|
||||
connect(m_selectionModel, &QItemSelectionModel::currentChanged, this,
|
||||
&ItemDetailMapper::onCurrentIndexChanged);
|
||||
|
||||
updateButtons(m_selectionModel->currentIndex().row());
|
||||
}
|
||||
|
||||
bool ItemDetailMapper::submit() { return m_mapper->submit(); }
|
||||
|
||||
void ItemDetailMapper::revert() { m_mapper->revert(); }
|
||||
|
||||
void ItemDetailMapper::onCurrentIndexChanged(const QModelIndex& current,
|
||||
const QModelIndex& previous) {
|
||||
if (!isEnabled()) {
|
||||
setEnabled(true);
|
||||
}
|
||||
m_mapper->setCurrentModelIndex(current);
|
||||
updateButtons(current.row());
|
||||
}
|
||||
|
||||
void ItemDetailMapper::rowsInserted(const QModelIndex& parent, int start, int end) {
|
||||
updateButtons(m_mapper->currentIndex());
|
||||
}
|
||||
|
||||
void ItemDetailMapper::rowsRemoved(const QModelIndex& parent, int start, int end) {
|
||||
if (m_model->rowCount() == 0) {
|
||||
setEnabled(false);
|
||||
|
||||
m_nameEdit->clear();
|
||||
m_descriptionEdit->clear();
|
||||
m_infoEdit->clear();
|
||||
m_amountBox->clear();
|
||||
m_factorBox->clear();
|
||||
}
|
||||
|
||||
updateButtons(m_mapper->currentIndex());
|
||||
}
|
||||
|
||||
void ItemDetailMapper::toPrevious() {
|
||||
int currentRow = m_selectionModel->currentIndex().row();
|
||||
QModelIndex previousIndex = m_selectionModel->currentIndex().siblingAtRow(currentRow - 1);
|
||||
if (previousIndex.isValid()) {
|
||||
m_selectionModel->setCurrentIndex(previousIndex, QItemSelectionModel::ClearAndSelect);
|
||||
}
|
||||
}
|
||||
|
||||
void ItemDetailMapper::toNext() {
|
||||
int currentRow = m_selectionModel->currentIndex().row();
|
||||
QModelIndex nextIndex = m_selectionModel->currentIndex().siblingAtRow(currentRow + 1);
|
||||
if (nextIndex.isValid()) {
|
||||
m_selectionModel->setCurrentIndex(nextIndex, QItemSelectionModel::ClearAndSelect);
|
||||
}
|
||||
}
|
||||
|
||||
void ItemDetailMapper::updateButtons(int row) {
|
||||
m_previousButton->setEnabled(row > 0);
|
||||
m_nextButton->setEnabled(row < m_model->rowCount() - 1);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user