Dive list: add context menu function to renumber dive(s)

No attempt is made to ensure that what the user does is sane. So this can
result in duplicate numbers, non-consecutive numbers, non-monotonous
numbers, whatever floats the users boat.

You can renumber a single dive or all selected dives (with a starting
number given that is applied to the oldest selected dive and then for each
newer selected dive that number is incremented by one).

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Dirk Hohndel 2014-05-20 06:11:32 +09:00
parent 4d14059d7c
commit b303f217a9
7 changed files with 32 additions and 10 deletions

2
dive.h
View file

@ -554,7 +554,7 @@ extern unsigned int dc_airtemp(struct divecomputer *dc);
extern unsigned int dc_watertemp(struct divecomputer *dc); extern unsigned int dc_watertemp(struct divecomputer *dc);
extern struct dive *merge_dives(struct dive *a, struct dive *b, int offset, bool prefer_downloaded); extern struct dive *merge_dives(struct dive *a, struct dive *b, int offset, bool prefer_downloaded);
extern struct dive *try_to_merge(struct dive *a, struct dive *b, bool prefer_downloaded); extern struct dive *try_to_merge(struct dive *a, struct dive *b, bool prefer_downloaded);
extern void renumber_dives(int nr); extern void renumber_dives(int start_nr, bool selected_only);
extern void copy_events(struct dive *s, struct dive *d); extern void copy_events(struct dive *s, struct dive *d);
extern void copy_cylinders(struct dive *s, struct dive *d); extern void copy_cylinders(struct dive *s, struct dive *d);
extern void copy_samples(struct dive *s, struct dive *d); extern void copy_samples(struct dive *s, struct dive *d);

View file

@ -529,6 +529,12 @@ void DiveListView::mergeDives()
MainWindow::instance()->refreshDisplay(); MainWindow::instance()->refreshDisplay();
} }
void DiveListView::renumberDives()
{
RenumberDialog::instance()->renumberOnlySelected();
RenumberDialog::instance()->show();
}
void DiveListView::merge_trip(const QModelIndex &a, int offset) void DiveListView::merge_trip(const QModelIndex &a, int offset)
{ {
int i = a.row() + offset; int i = a.row() + offset;
@ -768,6 +774,7 @@ void DiveListView::contextMenuEvent(QContextMenuEvent *event)
if (amount_selected > 1 && consecutive_selected()) if (amount_selected > 1 && consecutive_selected())
popup.addAction(tr("merge selected dives"), this, SLOT(mergeDives())); popup.addAction(tr("merge selected dives"), this, SLOT(mergeDives()));
if (amount_selected >= 1) { if (amount_selected >= 1) {
popup.addAction(tr("renumber dive(s)"), this, SLOT(renumberDives()));
popup.addAction(tr("save As"), this, SLOT(saveSelectedDivesAs())); popup.addAction(tr("save As"), this, SLOT(saveSelectedDivesAs()));
popup.addAction(tr("export As UDDF"), this, SLOT(exportSelectedDivesAsUDDF())); popup.addAction(tr("export As UDDF"), this, SLOT(exportSelectedDivesAsUDDF()));
popup.addAction(tr("export As CSV"), this, SLOT(exportSelectedDivesAsCSV())); popup.addAction(tr("export As CSV"), this, SLOT(exportSelectedDivesAsCSV()));

View file

@ -48,6 +48,7 @@ slots:
void addToTripAbove(); void addToTripAbove();
void addToTripBelow(); void addToTripBelow();
void mergeDives(); void mergeDives();
void renumberDives();
void saveSelectedDivesAs(); void saveSelectedDivesAs();
void exportSelectedDivesAsUDDF(); void exportSelectedDivesAsUDDF();
void exportSelectedDivesAsCSV(); void exportSelectedDivesAsCSV();

View file

@ -444,6 +444,7 @@ void MainWindow::on_actionAddDive_triggered()
void MainWindow::on_actionRenumber_triggered() void MainWindow::on_actionRenumber_triggered()
{ {
RenumberDialog::instance()->renumberOnlySelected(false);
RenumberDialog::instance()->show(); RenumberDialog::instance()->show();
} }

View file

@ -16,8 +16,9 @@
#include <QDateTime> #include <QDateTime>
#include <QShortcut> #include <QShortcut>
#include "exif.h" #include "exif.h"
#include "../dive.h" #include "dive.h"
#include "../file.h" #include "file.h"
#include "display.h"
#include "mainwindow.h" #include "mainwindow.h"
#include "helpers.h" #include "helpers.h"
@ -117,15 +118,24 @@ RenumberDialog *RenumberDialog::instance()
return self; return self;
} }
void RenumberDialog::renumberOnlySelected(bool selected)
{
if (selected && amount_selected == 1)
ui.groupBox->setTitle(tr("New number"));
else
ui.groupBox->setTitle(tr("New starting number"));
selectedOnly = selected;
}
void RenumberDialog::buttonClicked(QAbstractButton *button) void RenumberDialog::buttonClicked(QAbstractButton *button)
{ {
if (ui.buttonBox->buttonRole(button) == QDialogButtonBox::AcceptRole) { if (ui.buttonBox->buttonRole(button) == QDialogButtonBox::AcceptRole) {
qDebug() << "Renumbering."; qDebug() << "Renumbering.";
renumber_dives(ui.spinBox->value()); renumber_dives(ui.spinBox->value(), selectedOnly);
} }
} }
RenumberDialog::RenumberDialog(QWidget *parent) : QDialog(parent) RenumberDialog::RenumberDialog(QWidget *parent) : QDialog(parent), selectedOnly(false)
{ {
ui.setupUi(this); ui.setupUi(this);
connect(ui.buttonBox, SIGNAL(clicked(QAbstractButton *)), this, SLOT(buttonClicked(QAbstractButton *))); connect(ui.buttonBox, SIGNAL(clicked(QAbstractButton *)), this, SLOT(buttonClicked(QAbstractButton *)));

View file

@ -40,6 +40,7 @@ class RenumberDialog : public QDialog {
Q_OBJECT Q_OBJECT
public: public:
static RenumberDialog *instance(); static RenumberDialog *instance();
void renumberOnlySelected(bool selected = true);
private private
slots: slots:
void buttonClicked(QAbstractButton *button); void buttonClicked(QAbstractButton *button);
@ -47,6 +48,7 @@ slots:
private: private:
explicit RenumberDialog(QWidget *parent); explicit RenumberDialog(QWidget *parent);
Ui::RenumberDialog ui; Ui::RenumberDialog ui;
bool selectedOnly;
}; };
class ShiftTimesDialog : public QDialog { class ShiftTimesDialog : public QDialog {

View file

@ -149,13 +149,14 @@ void parse_argument(const char *arg)
} while (*++p); } while (*++p);
} }
void renumber_dives(int nr) void renumber_dives(int start_nr, bool selected_only)
{ {
int i; int i, nr = start_nr;
struct dive *dive;
for (i = 0; i < dive_table.nr; i++) { for_each_dive (i, dive) {
struct dive *dive = dive_table.dives[i]; if (dive->selected)
dive->number = nr + i; dive->number = nr++;
} }
mark_divelist_changed(true); mark_divelist_changed(true);
} }