Cleanup: use getDiveSelection() for shifting time

There are two cases where dive-times are shifted: in an explicit
dialog and when editing the date/time of a dive.

In each of these cases, the selected dives were collected manually.
Instead use the getDiveSelection() function. Since this returns
a std::vector, change the argument of Command::ShiftTime() to
such a std::vector.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2020-01-03 16:04:54 +01:00 committed by bstoeger
parent 62adc24d15
commit c495a49b1b
6 changed files with 14 additions and 30 deletions

View file

@ -24,7 +24,7 @@ void deleteDive(const QVector<struct dive*> &divesToDelete)
execute(new DeleteDive(divesToDelete));
}
void shiftTime(const QVector<dive *> &changedDives, int amount)
void shiftTime(const std::vector<dive *> &changedDives, int amount)
{
execute(new ShiftTime(changedDives, amount));
}

View file

@ -28,7 +28,7 @@ void addDive(dive *d, bool autogroup, bool newNumber);
void importDives(struct dive_table *dives, struct trip_table *trips,
struct dive_site_table *sites, int flags, const QString &source); // The tables are consumed!
void deleteDive(const QVector<struct dive*> &divesToDelete);
void shiftTime(const QVector<dive *> &changedDives, int amount);
void shiftTime(const std::vector<dive *> &changedDives, int amount);
void renumberDives(const QVector<QPair<dive *, int>> &divesToRenumber);
void removeDivesFromTrip(const QVector<dive *> &divesToRemove);
void removeAutogenTrips();

View file

@ -579,10 +579,10 @@ void DeleteDive::redoit()
}
ShiftTime::ShiftTime(const QVector<dive *> &changedDives, int amount)
ShiftTime::ShiftTime(std::vector<dive *> changedDives, int amount)
: diveList(changedDives), timeChanged(amount)
{
setText(tr("shift time of %n dives", "", changedDives.count()));
setText(tr("shift time of %n dives", "", changedDives.size()));
}
void ShiftTime::redoit()
@ -601,11 +601,12 @@ void ShiftTime::redoit()
sort_dive_table(&trip->dives); // Keep the trip-table in order
// Send signals
emit diveListNotifier.divesTimeChanged(timeChanged, diveList);
emit diveListNotifier.divesChanged(diveList, DiveField::DATETIME);
QVector<dive *> dives = QVector<dive *>::fromStdVector(diveList);
emit diveListNotifier.divesTimeChanged(timeChanged, dives);
emit diveListNotifier.divesChanged(dives, DiveField::DATETIME);
// Select the changed dives
setSelection(std::vector<dive *>(diveList.begin(), diveList.end()), diveList[0]);
setSelection(diveList, diveList[0]);
// Negate the time-shift so that the next call does the reverse
timeChanged = -timeChanged;
@ -613,7 +614,7 @@ void ShiftTime::redoit()
bool ShiftTime::workToBeDone()
{
return !diveList.isEmpty();
return !diveList.empty();
}
void ShiftTime::undoit()

View file

@ -133,14 +133,14 @@ private:
class ShiftTime : public DiveListBase {
public:
ShiftTime(const QVector<dive *> &changedDives, int amount);
ShiftTime(std::vector<dive *> changedDives, int amount);
private:
void undoit() override;
void redoit() override;
bool workToBeDone() override;
// For redo and undo
QVector<dive *> diveList;
std::vector<dive *> diveList;
int timeChanged;
};

View file

@ -222,17 +222,8 @@ void ShiftTimesDialog::buttonClicked(QAbstractButton *button)
amount = ui.timeEdit->time().hour() * 3600 + ui.timeEdit->time().minute() * 60;
if (ui.backwards->isChecked())
amount *= -1;
if (amount != 0) {
// DANGER, DANGER - this could get our dive_table unsorted...
int i;
struct dive *d;
QVector<dive *> affectedDives;
for_each_dive (i, d) {
if (d->selected)
affectedDives.append(d);
}
Command::shiftTime(affectedDives, amount);
}
if (amount != 0)
Command::shiftTime(getDiveSelection(), amount);
}
}

View file

@ -677,18 +677,10 @@ void MainTab::on_depth_editingFinished()
// all dives are shifted by an offset.
static void shiftTime(QDateTime &dateTime)
{
QVector<dive *> dives;
struct dive *d;
int i;
for_each_dive (i, d) {
if (d->selected)
dives.append(d);
}
timestamp_t when = dateTime.toTime_t();
if (current_dive && current_dive->when != when) {
timestamp_t offset = when - current_dive->when;
Command::shiftTime(dives, (int)offset);
Command::shiftTime(getDiveSelection(), (int)offset);
}
}