diff options
-rw-r--r-- | commands/command.cpp | 5 | ||||
-rw-r--r-- | commands/command.h | 1 | ||||
-rw-r--r-- | commands/command_edit.cpp | 32 | ||||
-rw-r--r-- | commands/command_edit.h | 9 | ||||
-rw-r--r-- | core/subsurface-qt/DiveListNotifier.h | 2 | ||||
-rw-r--r-- | desktop-widgets/tab-widgets/TabDiveEquipment.cpp | 3 | ||||
-rw-r--r-- | qt-models/weightmodel.cpp | 35 | ||||
-rw-r--r-- | qt-models/weightmodel.h | 3 |
8 files changed, 76 insertions, 14 deletions
diff --git a/commands/command.cpp b/commands/command.cpp index 47595674f..8f3dcc703 100644 --- a/commands/command.cpp +++ b/commands/command.cpp @@ -268,6 +268,11 @@ void editProfile(dive *d) execute(new ReplanDive(d, true)); } +int addWeight(bool currentDiveOnly) +{ + return execute_edit(new AddWeight(currentDiveOnly)); +} + // Trip editing related commands void editTripLocation(dive_trip *trip, const QString &s) { diff --git a/commands/command.h b/commands/command.h index 6446b32fd..85a37534e 100644 --- a/commands/command.h +++ b/commands/command.h @@ -81,6 +81,7 @@ int editDiveMaster(const QStringList &newList, bool currentDiveOnly); void pasteDives(const dive *d, dive_components what); void replanDive(dive *d); // dive computer(s) and cylinder(s) will be reset! void editProfile(dive *d); // dive computer(s) and cylinder(s) will be reset! +int addWeight(bool currentDiveOnly); // 5) Trip editing commands diff --git a/commands/command_edit.cpp b/commands/command_edit.cpp index 5e02ed80e..541eac61a 100644 --- a/commands/command_edit.cpp +++ b/commands/command_edit.cpp @@ -953,4 +953,36 @@ void ReplanDive::redo() undo(); } +// ***** Add Weight ***** +AddWeight::AddWeight(bool currentDiveOnly) : + EditDivesBase(currentDiveOnly) +{ + //: remove the part in parentheses for %n = 1 + setText(tr("Add weight (%n dive(s))", "", dives.size())); +} + +bool AddWeight::workToBeDone() +{ + return true; +} + +void AddWeight::undo() +{ + for (dive *d: dives) { + if (d->weightsystems.nr <= 0) + continue; + remove_weightsystem(d, d->weightsystems.nr - 1); + emit diveListNotifier.weightRemoved(d, d->weightsystems.nr); + } +} + +void AddWeight::redo() +{ + weightsystem_t ws { {0}, "" }; + for (dive *d: dives) { + add_cloned_weightsystem(&d->weightsystems, ws); + emit diveListNotifier.weightAdded(d, d->weightsystems.nr - 1); + } +} + } // namespace Command diff --git a/commands/command_edit.h b/commands/command_edit.h index c3acf9d48..ecf745070 100644 --- a/commands/command_edit.h +++ b/commands/command_edit.h @@ -330,6 +330,15 @@ private: bool workToBeDone() override; }; +class AddWeight : public EditDivesBase { +public: + AddWeight(bool currentDiveOnly); +private: + void undo() override; + void redo() override; + bool workToBeDone() override; +}; + } // namespace Command #endif diff --git a/core/subsurface-qt/DiveListNotifier.h b/core/subsurface-qt/DiveListNotifier.h index b82c0ac05..8c681948e 100644 --- a/core/subsurface-qt/DiveListNotifier.h +++ b/core/subsurface-qt/DiveListNotifier.h @@ -86,6 +86,8 @@ signals: void cylindersReset(const QVector<dive *> &dives); void weightsystemsReset(const QVector<dive *> &dives); + void weightAdded(dive *d, int pos); + void weightRemoved(dive *d, int pos); // Trip edited signal void tripChanged(dive_trip *trip, TripField field); diff --git a/desktop-widgets/tab-widgets/TabDiveEquipment.cpp b/desktop-widgets/tab-widgets/TabDiveEquipment.cpp index 110472e56..1ea57e703 100644 --- a/desktop-widgets/tab-widgets/TabDiveEquipment.cpp +++ b/desktop-widgets/tab-widgets/TabDiveEquipment.cpp @@ -158,8 +158,7 @@ void TabDiveEquipment::addCylinder_clicked() void TabDiveEquipment::addWeight_clicked() { - MainWindow::instance()->mainTab->enableEdition(); - weightModel->add(); + divesEdited(Command::addWeight(false)); } void TabDiveEquipment::editCylinderWidget(const QModelIndex &index) diff --git a/qt-models/weightmodel.cpp b/qt-models/weightmodel.cpp index dafd9d277..ceac7617c 100644 --- a/qt-models/weightmodel.cpp +++ b/qt-models/weightmodel.cpp @@ -15,6 +15,8 @@ WeightModel::WeightModel(QObject *parent) : CleanerTableModel(parent), //enum Column {REMOVE, TYPE, WEIGHT}; setHeaderDataStrings(QStringList() << tr("") << tr("Type") << tr("Weight")); connect(&diveListNotifier, &DiveListNotifier::weightsystemsReset, this, &WeightModel::weightsystemsReset); + connect(&diveListNotifier, &DiveListNotifier::weightAdded, this, &WeightModel::weightAdded); + connect(&diveListNotifier, &DiveListNotifier::weightRemoved, this, &WeightModel::weightRemoved); } weightsystem_t *WeightModel::weightSystemAt(const QModelIndex &index) @@ -146,17 +148,6 @@ int WeightModel::rowCount(const QModelIndex&) const return rows; } -void WeightModel::add() -{ - int row = rows; - weightsystem_t ws { {0}, "" }; - beginInsertRows(QModelIndex(), row, row); - add_cloned_weightsystem(&d->weightsystems, ws); - rows++; - changed = true; - endInsertRows(); -} - void WeightModel::updateDive(dive *dIn) { beginResetModel(); @@ -175,3 +166,25 @@ void WeightModel::weightsystemsReset(const QVector<dive *> &dives) // And update the model.. updateDive(d); } + +void WeightModel::weightAdded(struct dive *changed, int pos) +{ + if (d != changed) + return; + + // The last row was already inserted by the undo command. Just inform the model. + beginInsertRows(QModelIndex(), pos, pos); + rows++; + endInsertRows(); +} + +void WeightModel::weightRemoved(struct dive *changed, int pos) +{ + if (d != changed) + return; + + // The row was already deleted by the undo command. Just inform the model. + beginRemoveRows(QModelIndex(), pos, pos); + rows--; + endRemoveRows(); +} diff --git a/qt-models/weightmodel.h b/qt-models/weightmodel.h index 444d8611e..bb2c63778 100644 --- a/qt-models/weightmodel.h +++ b/qt-models/weightmodel.h @@ -23,7 +23,6 @@ public: bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; void passInData(const QModelIndex &index, const QVariant &value); - void add(); void clear(); void updateDive(dive *d); weightsystem_t *weightSystemAt(const QModelIndex &index); @@ -33,6 +32,8 @@ public slots: void remove(QModelIndex index); void weightsystemsReset(const QVector<dive *> &dives); + void weightAdded(dive *d, int pos); + void weightRemoved(dive *d, int pos); private: dive *d; |