143 lines
4.6 KiB
C++
143 lines
4.6 KiB
C++
#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();
|
|
m_nameLabel->setBuddy(m_nameEdit);
|
|
|
|
m_descriptionLabel = new QLabel("&Description");
|
|
m_descriptionEdit = new QLineEdit();
|
|
m_descriptionLabel->setBuddy(m_descriptionEdit);
|
|
|
|
m_infoLabel = new QLabel("&Info");
|
|
m_infoEdit = new QLineEdit();
|
|
m_infoLabel->setBuddy(m_infoEdit);
|
|
|
|
m_amountLabel = new QLabel("&Amount");
|
|
m_amountBox = new QSpinBox();
|
|
m_amountBox->setMaximum(1000);
|
|
m_amountLabel->setBuddy(m_amountBox);
|
|
|
|
m_factorLabel = new QLabel("&Factor");
|
|
m_factorBox = new QDoubleSpinBox();
|
|
m_factorBox->setMaximum(1000);
|
|
m_factorLabel->setBuddy(m_factorBox);
|
|
|
|
QGridLayout* layout = new QGridLayout();
|
|
layout->addWidget(m_nameLabel, 0, 0, 1, 1);
|
|
layout->addWidget(m_nameEdit, 0, 1, 1, 1);
|
|
layout->addWidget(m_descriptionLabel, 1, 0, 1, 1);
|
|
layout->addWidget(m_descriptionEdit, 1, 1, 1, 1);
|
|
layout->addWidget(m_infoLabel, 2, 0, 1, 1);
|
|
layout->addWidget(m_infoEdit, 2, 1, 1, 1);
|
|
layout->addWidget(m_amountLabel, 3, 0, 1, 1);
|
|
layout->addWidget(m_amountBox, 3, 1, 1, 1);
|
|
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);
|
|
}
|