Enable Multi Dive Editing.

This patch enables multi dive editing on the selected
dives. It's a bit of big patch where I reworked how it
worked since it was written just for a single dive.
this means that this can introduce bugs - I'v tested it
quite a bit but a thing could slip thru my fingers. :)

How this is supposed to work:

Select a few dives that you want ( one or more ) then
click on the field that you wanna edit / multi edit, and
press 'accept'. *only* the edited field will be modified
thru all dives.

Next patch - I'll change the bg color of the edited fields.A

Signed-off-by: Tomaz Canabrava <tcanabrava@kde.org>
This commit is contained in:
Tomaz Canabrava 2013-08-16 13:31:52 -03:00
parent cbbc7d99ea
commit 225b13a22d
2 changed files with 162 additions and 56 deletions

View file

@ -24,7 +24,6 @@ MainTab::MainTab(QWidget *parent) : QTabWidget(parent),
ui(new Ui::MainTab()), ui(new Ui::MainTab()),
weightModel(new WeightModel()), weightModel(new WeightModel()),
cylindersModel(new CylindersModel()), cylindersModel(new CylindersModel()),
currentDive(0),
editMode(NONE) editMode(NONE)
{ {
ui->setupUi(this); ui->setupUi(this);
@ -140,7 +139,7 @@ void MainTab::equipmentPlusUpdate()
void MainTab::enableEdition() void MainTab::enableEdition()
{ {
if (ui->editAccept->isVisible() || !currentDive) if (ui->editAccept->isVisible() || !selected_dive)
return; return;
ui->editAccept->setChecked(true); ui->editAccept->setChecked(true);
@ -200,6 +199,7 @@ void MainTab::clearStats()
void MainTab::updateDiveInfo(int dive) void MainTab::updateDiveInfo(int dive)
{ {
editMode = NONE;
// This method updates ALL tabs whenever a new dive or trip is // This method updates ALL tabs whenever a new dive or trip is
// selected. // selected.
// If exactly one trip has been selected, we show the location / notes // If exactly one trip has been selected, we show the location / notes
@ -212,7 +212,7 @@ void MainTab::updateDiveInfo(int dive)
process_selected_dives(); process_selected_dives();
process_all_dives(d, &prevd); process_all_dives(d, &prevd);
currentDive = d;
UPDATE_TEXT(d, notes); UPDATE_TEXT(d, notes);
UPDATE_TEXT(d, location); UPDATE_TEXT(d, location);
UPDATE_TEXT(d, suit); UPDATE_TEXT(d, suit);
@ -379,24 +379,39 @@ void MainTab::on_editAccept_clicked(bool edit)
mainWindow()->dive_list()->setEnabled(!edit); mainWindow()->dive_list()->setEnabled(!edit);
if (edit) { if (edit) {
// We may be editing one or more dives here. backup everything.
notesBackup.clear();
if (mainWindow() && mainWindow()->dive_list()->selectedTrips.count() == 1) { if (mainWindow() && mainWindow()->dive_list()->selectedTrips.count() == 1) {
// we are editing trip location and notes // we are editing trip location and notes
ui->diveNotesMessage->setText(tr("This trip is being edited. Select Save or Undo when ready.")); ui->diveNotesMessage->setText(tr("This trip is being edited. Select Save or Undo when ready."));
ui->diveNotesMessage->animatedShow(); ui->diveNotesMessage->animatedShow();
notesBackup.notes = ui->notes->toPlainText(); notesBackup[NULL].notes = ui->notes->toPlainText();
notesBackup.location = ui->location->text(); notesBackup[NULL].location = ui->location->text();
editMode = TRIP; editMode = TRIP;
} else { } else {
ui->diveNotesMessage->setText(tr("This dive is being edited. Select Save or Undo when ready.")); ui->diveNotesMessage->setText(tr("This dive is being edited. Select Save or Undo when ready."));
ui->diveNotesMessage->animatedShow(); ui->diveNotesMessage->animatedShow();
notesBackup.buddy = ui->buddy->text();
notesBackup.suit = ui->suit->text(); // We may be editing one or more dives here. backup everything.
notesBackup.notes = ui->notes->toPlainText(); struct dive *mydive;
notesBackup.divemaster = ui->divemaster->text(); for (int i = 0; i < dive_table.nr; i++) {
notesBackup.location = ui->location->text(); mydive = get_dive(i);
notesBackup.rating = ui->rating->currentStars(); if (!mydive)
notesBackup.visibility = ui->visibility->currentStars(); continue;
editMode = DIVE; if (!mydive->selected)
continue;
notesBackup[mydive].buddy = QString(mydive->buddy);
notesBackup[mydive].suit = QString(mydive->suit);
notesBackup[mydive].notes = QString(mydive->notes);
notesBackup[mydive].divemaster = QString(mydive->divemaster);
notesBackup[mydive].location = QString(mydive->location);
notesBackup[mydive].rating = mydive->rating;
notesBackup[mydive].visibility = mydive->visibility;
}
editMode = DIVE;
} }
} else { } else {
ui->diveNotesMessage->animatedHide(); ui->diveNotesMessage->animatedHide();
@ -404,38 +419,72 @@ void MainTab::on_editAccept_clicked(bool edit)
ui->editReset->hide(); ui->editReset->hide();
/* now figure out if things have changed */ /* now figure out if things have changed */
if (mainWindow() && mainWindow()->dive_list()->selectedTrips.count() == 1) { if (mainWindow() && mainWindow()->dive_list()->selectedTrips.count() == 1) {
if (notesBackup.notes != ui->notes->toPlainText() || if (notesBackup[NULL].notes != ui->notes->toPlainText() ||
notesBackup.location != ui->location->text()) notesBackup[NULL].location != ui->location->text())
mark_divelist_changed(TRUE); mark_divelist_changed(TRUE);
} else { } else {
if (notesBackup.buddy != ui->buddy->text() || struct dive *curr = current_dive;
notesBackup.suit != ui->suit->text() || if (notesBackup[curr].buddy != ui->buddy->text() ||
notesBackup.notes != ui->notes->toPlainText() || notesBackup[curr].suit != ui->suit->text() ||
notesBackup.divemaster != ui->divemaster->text() || notesBackup[curr].notes != ui->notes->toPlainText() ||
notesBackup.location != ui->location->text() || notesBackup[curr].divemaster != ui->divemaster->text() ||
notesBackup.visibility != ui->visibility->currentStars() || notesBackup[curr].location != ui->location->text() ||
notesBackup.rating != ui->rating->currentStars()) notesBackup[curr].rating != ui->visibility->currentStars() ||
notesBackup[curr].visibility != ui->rating->currentStars())
mark_divelist_changed(TRUE); mark_divelist_changed(TRUE);
if (notesBackup.location != ui->location->text()) if (notesBackup[curr].location != ui->location->text())
mainWindow()->globe()->reload(); mainWindow()->globe()->reload();
} }
editMode = NONE; editMode = NONE;
} }
} }
#define EDIT_TEXT2(what, text) \
textByteArray = text.toLocal8Bit(); \
free(what);\
what = strdup(textByteArray.data());
#define EDIT_TEXT(what, text) \
QByteArray textByteArray = text.toLocal8Bit(); \
free(what);\
what = strdup(textByteArray.data());
void MainTab::on_editReset_clicked() void MainTab::on_editReset_clicked()
{ {
if (!ui->editAccept->isChecked()) if (!ui->editAccept->isChecked())
return; return;
ui->notes->setText(notesBackup.notes); if (mainWindow() && mainWindow()->dive_list()->selectedTrips.count() == 1){
ui->location->setText(notesBackup.location); ui->notes->setText(notesBackup[NULL].notes );
if (mainWindow() && mainWindow()->dive_list()->selectedTrips.count() != 1) { ui->location->setText(notesBackup[NULL].location);
ui->buddy->setText(notesBackup.buddy); }else{
ui->suit->setText(notesBackup.suit); struct dive *curr = current_dive;
ui->divemaster->setText(notesBackup.divemaster); ui->notes->setText(notesBackup[curr].notes );
ui->rating->setCurrentStars(notesBackup.rating); ui->location->setText(notesBackup[curr].location);
ui->visibility->setCurrentStars(notesBackup.visibility); ui->buddy->setText(notesBackup[curr].buddy);
ui->suit->setText(notesBackup[curr].suit);
ui->divemaster->setText(notesBackup[curr].divemaster);
ui->rating->setCurrentStars(notesBackup[curr].rating);
ui->visibility->setCurrentStars(notesBackup[curr].visibility);
struct dive *mydive;
for (int i = 0; i < dive_table.nr; i++) {
mydive = get_dive(i);
if (!mydive)
continue;
if (!mydive->selected)
continue;
QByteArray textByteArray;
EDIT_TEXT2(mydive->buddy, notesBackup[mydive].buddy);
EDIT_TEXT2(mydive->suit, notesBackup[mydive].suit);
EDIT_TEXT2(mydive->notes, notesBackup[mydive].notes);
EDIT_TEXT2(mydive->divemaster, notesBackup[mydive].divemaster);
EDIT_TEXT2(mydive->location, notesBackup[mydive].location);
mydive->rating = notesBackup[mydive].rating;
mydive->visibility = notesBackup[mydive].visibility;
}
} }
ui->editAccept->setChecked(false); ui->editAccept->setChecked(false);
ui->diveNotesMessage->animatedHide(); ui->diveNotesMessage->animatedHide();
@ -451,58 +500,98 @@ void MainTab::on_editReset_clicked()
ui->editAccept->hide(); ui->editAccept->hide();
ui->editReset->hide(); ui->editReset->hide();
notesBackup.clear();
editMode = NONE; editMode = NONE;
} }
#undef EDIT_TEXT2
#define EDIT_TEXT(what, text) \
QByteArray textByteArray = text.toLocal8Bit(); \
free(what);\
what = strdup(textByteArray.data());
void MainTab::on_buddy_textChanged(const QString& text) void MainTab::on_buddy_textChanged(const QString& text)
{ {
if (!currentDive) if (editMode == NONE)
return; return;
EDIT_TEXT(currentDive->buddy, text); struct dive *mydive;
for (int i = 0; i < dive_table.nr; i++) {
mydive = get_dive(i);
if (!mydive)
continue;
if (!mydive->selected)
continue;
EDIT_TEXT(mydive->buddy, text);
}
} }
void MainTab::on_divemaster_textChanged(const QString& text) void MainTab::on_divemaster_textChanged(const QString& text)
{ {
if (!currentDive) if (editMode == NONE)
return; return;
EDIT_TEXT(currentDive->divemaster, text); struct dive *mydive;
for (int i = 0; i < dive_table.nr; i++) {
mydive = get_dive(i);
if (!mydive)
continue;
if (!mydive->selected)
continue;
EDIT_TEXT(mydive->divemaster, text);
}
} }
void MainTab::on_location_textChanged(const QString& text) void MainTab::on_location_textChanged(const QString& text)
{ {
if (editMode == NONE)
return;
if (editMode == TRIP && mainWindow() && mainWindow()->dive_list()->selectedTrips.count() == 1) { if (editMode == TRIP && mainWindow() && mainWindow()->dive_list()->selectedTrips.count() == 1) {
// we are editing a trip // we are editing a trip
dive_trip_t *currentTrip = *mainWindow()->dive_list()->selectedTrips.begin(); dive_trip_t *currentTrip = *mainWindow()->dive_list()->selectedTrips.begin();
EDIT_TEXT(currentTrip->location, text); EDIT_TEXT(currentTrip->location, text);
} else if (editMode == DIVE){ } else if (editMode == DIVE){
if (!currentDive) struct dive *mydive;
return; for (int i = 0; i < dive_table.nr; i++) {
EDIT_TEXT(currentDive->location, text); mydive = get_dive(i);
if (!mydive)
continue;
if (!mydive->selected)
continue;
EDIT_TEXT(mydive->location, text);
}
} }
} }
void MainTab::on_suit_textChanged(const QString& text) void MainTab::on_suit_textChanged(const QString& text)
{ {
if (!currentDive) if (editMode == NONE)
return; return;
EDIT_TEXT(currentDive->suit, text); struct dive *mydive;
for (int i = 0; i < dive_table.nr; i++) {
mydive = get_dive(i);
if (!mydive)
continue;
if (!mydive->selected)
continue;
EDIT_TEXT(mydive->suit, text);
}
} }
void MainTab::on_notes_textChanged() void MainTab::on_notes_textChanged()
{ {
if (editMode == NONE)
return;
if (editMode == TRIP && mainWindow() && mainWindow()->dive_list()->selectedTrips.count() == 1) { if (editMode == TRIP && mainWindow() && mainWindow()->dive_list()->selectedTrips.count() == 1) {
// we are editing a trip // we are editing a trip
dive_trip_t *currentTrip = *mainWindow()->dive_list()->selectedTrips.begin(); dive_trip_t *currentTrip = *mainWindow()->dive_list()->selectedTrips.begin();
EDIT_TEXT(currentTrip->notes, ui->notes->toPlainText()); EDIT_TEXT(currentTrip->notes, ui->notes->toPlainText());
} else if (editMode == DIVE) { } else if (editMode == DIVE) {
if (!currentDive) struct dive *mydive;
return; for (int i = 0; i < dive_table.nr; i++) {
EDIT_TEXT(currentDive->notes, ui->notes->toPlainText()); mydive = get_dive(i);
if (!mydive)
continue;
if (!mydive->selected)
continue;
EDIT_TEXT(mydive->notes, ui->notes->toPlainText());
}
} }
} }
@ -510,16 +599,33 @@ void MainTab::on_notes_textChanged()
void MainTab::on_rating_valueChanged(int value) void MainTab::on_rating_valueChanged(int value)
{ {
if (!currentDive) if (editMode == NONE)
return; return;
currentDive->rating = value; struct dive *mydive;
for (int i = 0; i < dive_table.nr; i++) {
mydive = get_dive(i);
if (!mydive)
continue;
if (!mydive->selected)
continue;
mydive->rating = value;
}
} }
void MainTab::on_visibility_valueChanged(int value) void MainTab::on_visibility_valueChanged(int value)
{ {
if (!currentDive) if (editMode == NONE)
return; return;
currentDive->visibility = value; struct dive *mydive;
for (int i = 0; i < dive_table.nr; i++) {
mydive = get_dive(i);
if (!mydive)
continue;
if (!mydive->selected)
continue;
mydive->visibility = value;
}
} }
void MainTab::hideEvent(QHideEvent* event) void MainTab::hideEvent(QHideEvent* event)

View file

@ -9,11 +9,12 @@
#include <QTabWidget> #include <QTabWidget>
#include <QDialog> #include <QDialog>
#include <QMap>
#include "models.h" #include "models.h"
class QCompleter; class QCompleter;
struct dive;
namespace Ui namespace Ui
{ {
class MainTab; class MainTab;
@ -76,8 +77,7 @@ private:
Ui::MainTab *ui; Ui::MainTab *ui;
WeightModel *weightModel; WeightModel *weightModel;
CylindersModel *cylindersModel; CylindersModel *cylindersModel;
NotesBackup notesBackup; QMap<dive*, NotesBackup> notesBackup;
struct dive* currentDive;
QPushButton *addCylinder; QPushButton *addCylinder;
QPushButton *addWeight; QPushButton *addWeight;
enum { NONE, DIVE, TRIP } editMode; enum { NONE, DIVE, TRIP } editMode;