mirror of
https://github.com/subsurface/subsurface.git
synced 2025-02-19 22:16:15 +00:00
Cleanup: remove displayed_dive from WeightModel
The WeightModel always acted on the displayed dive. To support undo of weightsystem changes, operate on an arbitrary dive. This is in line with other models, where the updateDive() function resets the model to represent a certain dive. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
parent
936362c102
commit
76a5a38f5e
3 changed files with 25 additions and 22 deletions
|
@ -129,7 +129,7 @@ void TabDiveEquipment::toggleTriggeredColumn()
|
||||||
void TabDiveEquipment::updateData()
|
void TabDiveEquipment::updateData()
|
||||||
{
|
{
|
||||||
cylindersModel->updateDive();
|
cylindersModel->updateDive();
|
||||||
weightModel->updateDive();
|
weightModel->updateDive(current_dive);
|
||||||
suitModel.updateModel();
|
suitModel.updateModel();
|
||||||
|
|
||||||
ui.cylinders->view()->hideColumn(CylindersModel::DEPTH);
|
ui.cylinders->view()->hideColumn(CylindersModel::DEPTH);
|
||||||
|
@ -272,7 +272,7 @@ void TabDiveEquipment::rejectChanges()
|
||||||
cylindersModel->changed = false;
|
cylindersModel->changed = false;
|
||||||
weightModel->changed = false;
|
weightModel->changed = false;
|
||||||
cylindersModel->updateDive();
|
cylindersModel->updateDive();
|
||||||
weightModel->updateDive();
|
weightModel->updateDive(current_dive);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TabDiveEquipment::divesEdited(int i)
|
void TabDiveEquipment::divesEdited(int i)
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
|
|
||||||
WeightModel::WeightModel(QObject *parent) : CleanerTableModel(parent),
|
WeightModel::WeightModel(QObject *parent) : CleanerTableModel(parent),
|
||||||
changed(false),
|
changed(false),
|
||||||
|
d(nullptr),
|
||||||
rows(0)
|
rows(0)
|
||||||
{
|
{
|
||||||
//enum Column {REMOVE, TYPE, WEIGHT};
|
//enum Column {REMOVE, TYPE, WEIGHT};
|
||||||
|
@ -18,33 +19,36 @@ WeightModel::WeightModel(QObject *parent) : CleanerTableModel(parent),
|
||||||
|
|
||||||
weightsystem_t *WeightModel::weightSystemAt(const QModelIndex &index)
|
weightsystem_t *WeightModel::weightSystemAt(const QModelIndex &index)
|
||||||
{
|
{
|
||||||
return &displayed_dive.weightsystems.weightsystems[index.row()];
|
int row = index.row();
|
||||||
|
if (row < 0 || row >= d->weightsystems.nr) {
|
||||||
|
qWarning("WeightModel: Accessing invalid weightsystem %d (of %d)", row, d->weightsystems.nr);
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
return &d->weightsystems.weightsystems[index.row()];
|
||||||
}
|
}
|
||||||
|
|
||||||
void WeightModel::remove(QModelIndex index)
|
void WeightModel::remove(QModelIndex index)
|
||||||
{
|
{
|
||||||
if (index.column() != REMOVE)
|
if (index.column() != REMOVE || !d)
|
||||||
return;
|
return;
|
||||||
beginRemoveRows(QModelIndex(), index.row(), index.row());
|
beginRemoveRows(QModelIndex(), index.row(), index.row());
|
||||||
rows--;
|
rows--;
|
||||||
remove_weightsystem(&displayed_dive, index.row());
|
remove_weightsystem(d, index.row());
|
||||||
changed = true;
|
changed = true;
|
||||||
endRemoveRows();
|
endRemoveRows();
|
||||||
}
|
}
|
||||||
|
|
||||||
void WeightModel::clear()
|
void WeightModel::clear()
|
||||||
{
|
{
|
||||||
beginResetModel();
|
updateDive(nullptr);
|
||||||
rows = 0;
|
|
||||||
endResetModel();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QVariant WeightModel::data(const QModelIndex &index, int role) const
|
QVariant WeightModel::data(const QModelIndex &index, int role) const
|
||||||
{
|
{
|
||||||
if (!index.isValid() || index.row() >= displayed_dive.weightsystems.nr)
|
if (!index.isValid() || index.row() >= d->weightsystems.nr)
|
||||||
return QVariant();
|
return QVariant();
|
||||||
|
|
||||||
const weightsystem_t *ws = &displayed_dive.weightsystems.weightsystems[index.row()];
|
const weightsystem_t *ws = &d->weightsystems.weightsystems[index.row()];
|
||||||
|
|
||||||
switch (role) {
|
switch (role) {
|
||||||
case Qt::FontRole:
|
case Qt::FontRole:
|
||||||
|
@ -81,7 +85,7 @@ QVariant WeightModel::data(const QModelIndex &index, int role) const
|
||||||
// so we only implement the two columns we care about
|
// so we only implement the two columns we care about
|
||||||
void WeightModel::passInData(const QModelIndex &index, const QVariant &value)
|
void WeightModel::passInData(const QModelIndex &index, const QVariant &value)
|
||||||
{
|
{
|
||||||
weightsystem_t *ws = &displayed_dive.weightsystems.weightsystems[index.row()];
|
weightsystem_t *ws = &d->weightsystems.weightsystems[index.row()];
|
||||||
if (index.column() == WEIGHT) {
|
if (index.column() == WEIGHT) {
|
||||||
if (ws->weight.grams != value.toInt()) {
|
if (ws->weight.grams != value.toInt()) {
|
||||||
ws->weight.grams = value.toInt();
|
ws->weight.grams = value.toInt();
|
||||||
|
@ -94,7 +98,7 @@ void WeightModel::passInData(const QModelIndex &index, const QVariant &value)
|
||||||
bool WeightModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
bool WeightModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
||||||
{
|
{
|
||||||
QString vString = value.toString();
|
QString vString = value.toString();
|
||||||
weightsystem_t *ws = &displayed_dive.weightsystems.weightsystems[index.row()];
|
weightsystem_t *ws = &d->weightsystems.weightsystems[index.row()];
|
||||||
switch (index.column()) {
|
switch (index.column()) {
|
||||||
case TYPE:
|
case TYPE:
|
||||||
if (!value.isNull()) {
|
if (!value.isNull()) {
|
||||||
|
@ -147,29 +151,27 @@ void WeightModel::add()
|
||||||
int row = rows;
|
int row = rows;
|
||||||
weightsystem_t ws { {0}, "" };
|
weightsystem_t ws { {0}, "" };
|
||||||
beginInsertRows(QModelIndex(), row, row);
|
beginInsertRows(QModelIndex(), row, row);
|
||||||
add_cloned_weightsystem(&displayed_dive.weightsystems, ws);
|
add_cloned_weightsystem(&d->weightsystems, ws);
|
||||||
rows++;
|
rows++;
|
||||||
changed = true;
|
changed = true;
|
||||||
endInsertRows();
|
endInsertRows();
|
||||||
}
|
}
|
||||||
|
|
||||||
void WeightModel::updateDive()
|
void WeightModel::updateDive(dive *dIn)
|
||||||
{
|
{
|
||||||
beginResetModel();
|
beginResetModel();
|
||||||
rows = displayed_dive.weightsystems.nr;
|
d = dIn;
|
||||||
|
rows = d ? d->weightsystems.nr : 0;
|
||||||
endResetModel();
|
endResetModel();
|
||||||
}
|
}
|
||||||
|
|
||||||
void WeightModel::weightsystemsReset(const QVector<dive *> &dives)
|
void WeightModel::weightsystemsReset(const QVector<dive *> &dives)
|
||||||
{
|
{
|
||||||
// This model only concerns the currently displayed dive. If this is not among the
|
// This model only concerns the currently displayed dive. If this is not among the
|
||||||
// dives that had their cylinders reset, exit.
|
// dives that had their weight reset, exit.
|
||||||
if (!current_dive || std::find(dives.begin(), dives.end(), current_dive) == dives.end())
|
if (!d || std::find(dives.begin(), dives.end(), d) == dives.end())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// Copy the weights from the current dive to the displayed dive.
|
|
||||||
copy_weights(¤t_dive->weightsystems, &displayed_dive.weightsystems);
|
|
||||||
|
|
||||||
// And update the model..
|
// And update the model..
|
||||||
updateDive();
|
updateDive(d);
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,7 +25,7 @@ public:
|
||||||
void passInData(const QModelIndex &index, const QVariant &value);
|
void passInData(const QModelIndex &index, const QVariant &value);
|
||||||
void add();
|
void add();
|
||||||
void clear();
|
void clear();
|
||||||
void updateDive();
|
void updateDive(dive *d);
|
||||||
weightsystem_t *weightSystemAt(const QModelIndex &index);
|
weightsystem_t *weightSystemAt(const QModelIndex &index);
|
||||||
bool changed;
|
bool changed;
|
||||||
|
|
||||||
|
@ -35,6 +35,7 @@ slots:
|
||||||
void weightsystemsReset(const QVector<dive *> &dives);
|
void weightsystemsReset(const QVector<dive *> &dives);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
dive *d;
|
||||||
int rows;
|
int rows;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue