diff options
author | Dirk Hohndel <dirk@hohndel.org> | 2020-04-11 11:03:05 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-11 11:03:05 -0700 |
commit | 6d187b5f4a3b51043fa3b53b6c73a7e0ec7f0b53 (patch) | |
tree | 93b26b7381e565ecc0dc6d93e2d35b61a1cdaca9 /desktop-widgets | |
parent | 2990010ccde56acec980e76ef5671137e87af488 (diff) | |
parent | 7dc04b4437c7aa1788f7d85a513a246ce502dea4 (diff) | |
download | subsurface-6d187b5f4a3b51043fa3b53b6c73a7e0ec7f0b53.tar.gz |
Merge pull request #2643 from bstoeger/cylinder4
First steps of cylinder-editing undo
Diffstat (limited to 'desktop-widgets')
-rw-r--r-- | desktop-widgets/mainwindow.cpp | 50 | ||||
-rw-r--r-- | desktop-widgets/mainwindow.h | 3 | ||||
-rw-r--r-- | desktop-widgets/modeldelegates.cpp | 70 | ||||
-rw-r--r-- | desktop-widgets/modeldelegates.h | 9 | ||||
-rw-r--r-- | desktop-widgets/simplewidgets.cpp | 32 | ||||
-rw-r--r-- | desktop-widgets/simplewidgets.h | 7 | ||||
-rw-r--r-- | desktop-widgets/tab-widgets/TabDiveEquipment.cpp | 105 | ||||
-rw-r--r-- | desktop-widgets/tab-widgets/TabDiveEquipment.h | 4 | ||||
-rw-r--r-- | desktop-widgets/tab-widgets/maintab.cpp | 123 | ||||
-rw-r--r-- | desktop-widgets/tab-widgets/maintab.h | 12 |
10 files changed, 79 insertions, 336 deletions
diff --git a/desktop-widgets/mainwindow.cpp b/desktop-widgets/mainwindow.cpp index 8474eee71..1c122a4fd 100644 --- a/desktop-widgets/mainwindow.cpp +++ b/desktop-widgets/mainwindow.cpp @@ -710,6 +710,8 @@ void MainWindow::on_actionPrint_triggered() void MainWindow::disableShortcuts(bool disablePaste) { + undoAction->setEnabled(false); + redoAction->setEnabled(false); ui.actionPreviousDC->setShortcut(QKeySequence()); ui.actionNextDC->setShortcut(QKeySequence()); ui.copy->setShortcut(QKeySequence()); @@ -719,6 +721,8 @@ void MainWindow::disableShortcuts(bool disablePaste) void MainWindow::enableShortcuts() { + undoAction->setEnabled(true); + redoAction->setEnabled(true); ui.actionPreviousDC->setShortcut(Qt::Key_Left); ui.actionNextDC->setShortcut(Qt::Key_Right); ui.copy->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_C)); @@ -918,7 +922,7 @@ void MainWindow::on_actionReplanDive_triggered() divePlannerWidget->setSalinity(current_dive->salinity); DivePlannerPointsModel::instance()->loadFromDive(current_dive); reset_cylinders(&displayed_dive, true); - DivePlannerPointsModel::instance()->cylindersModel()->updateDive(); + DivePlannerPointsModel::instance()->cylindersModel()->updateDive(&displayed_dive); } void MainWindow::on_actionDivePlanner_triggered() @@ -1088,40 +1092,6 @@ void MainWindow::on_actionViewAll_triggered() ui.bottomSplitter->setCollapsible(1,false); } -void MainWindow::enterEditState() -{ - undoAction->setEnabled(false); - redoAction->setEnabled(false); - stateBeforeEdit = state; - if (state == VIEWALL || state == INFO_MAXIMIZED) - return; - toggleCollapsible(true); - beginChangeState(EDIT); - ui.topSplitter->setSizes({ EXPANDED, EXPANDED }); - ui.mainSplitter->setSizes({ EXPANDED, COLLAPSED }); - int appW = qApp->desktop()->size().width(); - QList<int> infoProfileSizes { round_int(appW * 0.3), round_int(appW * 0.7) }; - - QSettings settings; - settings.beginGroup("MainWindow"); - if (settings.value("mainSplitter").isValid()) { - ui.topSplitter->restoreState(settings.value("topSplitter").toByteArray()); - if (ui.topSplitter->sizes().first() == 0 || ui.topSplitter->sizes().last() == 0) - ui.topSplitter->setSizes(infoProfileSizes); - } else { - ui.topSplitter->setSizes(infoProfileSizes); - } -} - -void MainWindow::exitEditState() -{ - undoAction->setEnabled(true); - redoAction->setEnabled(true); - if (stateBeforeEdit == state) - return; - enterState(stateBeforeEdit); -} - void MainWindow::enterState(CurrentState newState) { state = newState; @@ -1141,8 +1111,6 @@ void MainWindow::enterState(CurrentState newState) case PROFILE_MAXIMIZED: on_actionViewProfile_triggered(); break; - case EDIT: - break; } } @@ -1793,20 +1761,18 @@ void MainWindow::editCurrentDive() struct dive *d = current_dive; QString defaultDC(d->dc.model); DivePlannerPointsModel::instance()->clear(); - disableShortcuts(); if (defaultDC == "manually added dive") { + disableShortcuts(); DivePlannerPointsModel::instance()->setPlanMode(DivePlannerPointsModel::ADD); graphics->setAddState(); setApplicationState(ApplicationState::EditDive); DivePlannerPointsModel::instance()->loadFromDive(d); - mainTab->enableEdition(MainTab::MANUALLY_ADDED_DIVE); + mainTab->enableEdition(); } else if (defaultDC == "planned dive") { + disableShortcuts(); DivePlannerPointsModel::instance()->setPlanMode(DivePlannerPointsModel::PLAN); setApplicationState(ApplicationState::EditPlannedDive); DivePlannerPointsModel::instance()->loadFromDive(d); - mainTab->enableEdition(MainTab::MANUALLY_ADDED_DIVE); - } else { - setApplicationState(ApplicationState::EditDive); mainTab->enableEdition(); } } diff --git a/desktop-widgets/mainwindow.h b/desktop-widgets/mainwindow.h index 247bddaf3..1fb20a7ce 100644 --- a/desktop-widgets/mainwindow.h +++ b/desktop-widgets/mainwindow.h @@ -58,7 +58,6 @@ public: INFO_MAXIMIZED, PROFILE_MAXIMIZED, LIST_MAXIMIZED, - EDIT, }; MainWindow(); @@ -81,8 +80,6 @@ public: NotificationWidget *getNotificationWidget(); void enableDisableCloudActions(); void enableDisableOtherDCsActions(); - void enterEditState(); - void exitEditState(); void editDiveSite(dive_site *ds); std::unique_ptr<MainTab> mainTab; diff --git a/desktop-widgets/modeldelegates.cpp b/desktop-widgets/modeldelegates.cpp index ed7278b4c..5480a41db 100644 --- a/desktop-widgets/modeldelegates.cpp +++ b/desktop-widgets/modeldelegates.cpp @@ -38,7 +38,6 @@ QSize DiveListDelegate::sizeHint(const QStyleOptionViewItem&, const QModelIndex& // Gets the index of the model in the currentRow and column. // currCombo is defined below. #define IDX(_XX) mymodel->index(currCombo.currRow, (_XX)) -static bool keyboardFinished = false; StarWidgetsDelegate::StarWidgetsDelegate(QWidget *parent) : QStyledItemDelegate(parent), parentWidget(parent) @@ -86,7 +85,6 @@ ComboBoxDelegate::ComboBoxDelegate(QAbstractItemModel *model, QObject *parent, b { editable = allowEdit; connect(this, &ComboBoxDelegate::closeEditor, this, &ComboBoxDelegate::editorClosed); - connect(this, &ComboBoxDelegate::closeEditor, this, &ComboBoxDelegate::fixTabBehavior); } void ComboBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const @@ -101,14 +99,6 @@ void ComboBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) c->lineEdit()->setSelection(0, c->lineEdit()->text().length()); } -static struct CurrSelected { - QComboBox *comboEditor; - int currRow; - QString activeText; - QAbstractItemModel *model; - bool ignoreSelection; -} currCombo; - QWidget *ComboBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem&, const QModelIndex &index) const { QComboBox *comboDelegate = new QComboBox(parent); @@ -129,7 +119,7 @@ QWidget *ComboBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewI currCombo.comboEditor = comboDelegate; currCombo.currRow = index.row(); currCombo.model = const_cast<QAbstractItemModel *>(index.model()); - keyboardFinished = false; + currCombo.activeText = currCombo.model->data(index).toString(); // Current display of things on Gnome3 looks like shit, so // let`s fix that. @@ -177,16 +167,6 @@ void ComboBoxDelegate::fakeActivation() QStyledItemDelegate::eventFilter(currCombo.comboEditor, &ev); } -// This 'reverts' the model data to what we actually choosed, -// becaus e a TAB is being understood by Qt as 'cancel' while -// we are on a QComboBox ( but not on a QLineEdit. -void ComboBoxDelegate::fixTabBehavior() -{ - if (keyboardFinished) { - setModelData(0, 0, QModelIndex()); - } -} - bool ComboBoxDelegate::eventFilter(QObject *object, QEvent *event) { // Reacts on Key_UP and Key_DOWN to show the QComboBox - list of choices. @@ -200,10 +180,8 @@ bool ComboBoxDelegate::eventFilter(QObject *object, QEvent *event) return true; } } - if (ev->key() == Qt::Key_Tab || ev->key() == Qt::Key_Enter || ev->key() == Qt::Key_Return) { + if (ev->key() == Qt::Key_Tab || ev->key() == Qt::Key_Enter || ev->key() == Qt::Key_Return) currCombo.activeText = currCombo.comboEditor->currentText(); - keyboardFinished = true; - } } else { // the 'Drop Down Menu' part. QKeyEvent *ev = static_cast<QKeyEvent *>(event); if (ev->key() == Qt::Key_Enter || ev->key() == Qt::Key_Return || @@ -230,12 +208,6 @@ void ComboBoxDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionV editor->setGeometry(defaultRect); } -static struct RevertCylinderData { - QString type; - int pressure; - int size; -} currCylinderData; - void TankInfoDelegate::setModelData(QWidget*, QAbstractItemModel*, const QModelIndex&) const { QAbstractItemModel *mymodel = currCombo.model; @@ -254,46 +226,30 @@ void TankInfoDelegate::setModelData(QWidget*, QAbstractItemModel*, const QModelI int tankSize = tanks->data(tanks->index(row, TankInfoModel::ML)).toInt(); int tankPressure = tanks->data(tanks->index(row, TankInfoModel::BAR)).toInt(); - mymodel->setData(IDX(CylindersModel::TYPE), cylinderName, Qt::EditRole); - mymodel->setData(IDX(CylindersModel::WORKINGPRESS), tankPressure, CylindersModel::PASS_IN_ROLE); - mymodel->setData(IDX(CylindersModel::SIZE), tankSize, CylindersModel::PASS_IN_ROLE); + mymodel->setData(IDX(CylindersModel::TYPE), cylinderName, CylindersModel::TEMP_ROLE); + mymodel->setData(IDX(CylindersModel::WORKINGPRESS), tankPressure, CylindersModel::TEMP_ROLE); + mymodel->setData(IDX(CylindersModel::SIZE), tankSize, CylindersModel::TEMP_ROLE); } TankInfoDelegate::TankInfoDelegate(QObject *parent) : ComboBoxDelegate(TankInfoModel::instance(), parent, true) { - connect(this, SIGNAL(closeEditor(QWidget *, QAbstractItemDelegate::EndEditHint)), - this, SLOT(reenableReplot(QWidget *, QAbstractItemDelegate::EndEditHint))); -} - -void TankInfoDelegate::reenableReplot(QWidget*, QAbstractItemDelegate::EndEditHint) -{ - MainWindow::instance()->graphics->setReplot(true); - // FIXME: We need to replot after a cylinder is selected but the replot below overwrites - // the newly selected cylinder. - // MainWindow::instance()->graphics->replot(); } void TankInfoDelegate::editorClosed(QWidget*, QAbstractItemDelegate::EndEditHint hint) { - if (hint == QAbstractItemDelegate::NoHint || - hint == QAbstractItemDelegate::RevertModelCache) { - QAbstractItemModel *mymodel = currCombo.model; - mymodel->setData(IDX(CylindersModel::TYPE), currCylinderData.type, Qt::EditRole); - mymodel->setData(IDX(CylindersModel::WORKINGPRESS), currCylinderData.pressure, CylindersModel::PASS_IN_ROLE); - mymodel->setData(IDX(CylindersModel::SIZE), currCylinderData.size, CylindersModel::PASS_IN_ROLE); - } + QAbstractItemModel *mymodel = currCombo.model; + // Ugly hack: We misuse setData() with COMMIT_ROLE or REVERT_ROLE to commit or + // revert the current row. We send in the type, because we may get multiple + // end events and thus can prevent multiple commits. + if (hint == QAbstractItemDelegate::RevertModelCache) + mymodel->setData(IDX(CylindersModel::TYPE), currCombo.activeText, CylindersModel::REVERT_ROLE); + else + mymodel->setData(IDX(CylindersModel::TYPE), currCombo.activeText, CylindersModel::COMMIT_ROLE); } QWidget *TankInfoDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const { - // ncreate editor needs to be called before because it will populate a few - // things in the currCombo global var. QWidget *delegate = ComboBoxDelegate::createEditor(parent, option, index); - QAbstractItemModel *model = currCombo.model; - int row = index.row(); - currCylinderData.type = model->data(model->index(row, CylindersModel::TYPE)).value<QString>(); - currCylinderData.pressure = model->data(model->index(row, CylindersModel::WORKINGPRESS_INT)).value<int>(); - currCylinderData.size = model->data(model->index(row, CylindersModel::SIZE_INT)).value<int>(); MainWindow::instance()->graphics->setReplot(false); return delegate; } diff --git a/desktop-widgets/modeldelegates.h b/desktop-widgets/modeldelegates.h index 277bf994e..95e9c5fef 100644 --- a/desktop-widgets/modeldelegates.h +++ b/desktop-widgets/modeldelegates.h @@ -44,12 +44,18 @@ slots: void testActivation(const QModelIndex &currIndex); //HACK: try to get rid of this in the future. void fakeActivation(); - void fixTabBehavior(); virtual void editorClosed(QWidget *widget, QAbstractItemDelegate::EndEditHint hint) = 0; private: bool editable; protected: QAbstractItemModel *model; + mutable struct CurrSelected { + QComboBox *comboEditor; + int currRow; + QString activeText; + QAbstractItemModel *model; + bool ignoreSelection; + } currCombo; }; class TankInfoDelegate : public ComboBoxDelegate { @@ -61,7 +67,6 @@ public: public slots: void editorClosed(QWidget *widget, QAbstractItemDelegate::EndEditHint hint); - void reenableReplot(QWidget *widget, QAbstractItemDelegate::EndEditHint hint); }; class TankUseDelegate : public QStyledItemDelegate { diff --git a/desktop-widgets/simplewidgets.cpp b/desktop-widgets/simplewidgets.cpp index fb4d3750b..423f4b68f 100644 --- a/desktop-widgets/simplewidgets.cpp +++ b/desktop-widgets/simplewidgets.cpp @@ -23,7 +23,6 @@ #include "commands/command.h" #include "core/metadata.h" #include "core/tag.h" -#include "core/divelist.h" // for mark_divelist_changed double MinMaxAvgWidget::average() const { @@ -174,38 +173,21 @@ RenumberDialog::RenumberDialog(QWidget *parent) : QDialog(parent), selectedOnly( connect(quit, SIGNAL(activated()), parent, SLOT(close())); } -SetpointDialog *SetpointDialog::instance() -{ - static SetpointDialog *self = new SetpointDialog(MainWindow::instance()); - return self; -} - -void SetpointDialog::setpointData(struct divecomputer *divecomputer, int second) -{ - dc = divecomputer; - time = second < 0 ? 0 : second; -} - void SetpointDialog::buttonClicked(QAbstractButton *button) { - if (ui.buttonBox->buttonRole(button) == QDialogButtonBox::AcceptRole && dc) { - add_event(dc, time, SAMPLE_EVENT_PO2, 0, (int)(1000.0 * ui.spinbox->value()), - QT_TRANSLATE_NOOP("gettextFromC", "SP change")); - invalidate_dive_cache(current_dive); - } - mark_divelist_changed(true); - MainWindow::instance()->graphics->replot(); + if (ui.buttonBox->buttonRole(button) == QDialogButtonBox::AcceptRole) + Command::addEventSetpointChange(d, dcNr, time, pressure_t { (int)(1000.0 * ui.spinbox->value()) }); } -SetpointDialog::SetpointDialog(QWidget *parent) : QDialog(parent), - dc(0), time(0) +SetpointDialog::SetpointDialog(struct dive *dIn, int dcNrIn, int seconds) : QDialog(MainWindow::instance()), + d(dIn), dcNr(dcNrIn), time(seconds < 0 ? 0 : seconds) { ui.setupUi(this); - connect(ui.buttonBox, SIGNAL(clicked(QAbstractButton *)), this, SLOT(buttonClicked(QAbstractButton *))); + connect(ui.buttonBox, &QDialogButtonBox::clicked, this, &SetpointDialog::buttonClicked); QShortcut *close = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_W), this); - connect(close, SIGNAL(activated()), this, SLOT(close())); + connect(close, &QShortcut::activated, this, &QDialog::close); QShortcut *quit = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q), this); - connect(quit, SIGNAL(activated()), parent, SLOT(close())); + connect(quit, &QShortcut::activated, MainWindow::instance(), &QWidget::close); } ShiftTimesDialog *ShiftTimesDialog::instance() diff --git a/desktop-widgets/simplewidgets.h b/desktop-widgets/simplewidgets.h index 99cfc0fa2..421695815 100644 --- a/desktop-widgets/simplewidgets.h +++ b/desktop-widgets/simplewidgets.h @@ -65,16 +65,15 @@ private: class SetpointDialog : public QDialog { Q_OBJECT public: - static SetpointDialog *instance(); - void setpointData(struct divecomputer *divecomputer, int time); + SetpointDialog(struct dive *d, int dcNr, int time); private slots: void buttonClicked(QAbstractButton *button); private: - explicit SetpointDialog(QWidget *parent); Ui::SetpointDialog ui; - struct divecomputer *dc; + struct dive *d; + int dcNr; int time; }; diff --git a/desktop-widgets/tab-widgets/TabDiveEquipment.cpp b/desktop-widgets/tab-widgets/TabDiveEquipment.cpp index 33a8e5a3c..9399d3871 100644 --- a/desktop-widgets/tab-widgets/TabDiveEquipment.cpp +++ b/desktop-widgets/tab-widgets/TabDiveEquipment.cpp @@ -1,18 +1,13 @@ // SPDX-License-Identifier: GPL-2.0 #include "TabDiveEquipment.h" #include "maintab.h" -#include "desktop-widgets/mainwindow.h" // TODO: Only used temporarilly for edit mode changes #include "desktop-widgets/simplewidgets.h" // For isGnome3Session() #include "desktop-widgets/modeldelegates.h" #include "commands/command.h" -#include "profile-widget/profilewidget2.h" #include "qt-models/cylindermodel.h" #include "qt-models/weightmodel.h" -#include "core/subsurface-string.h" -#include "core/divelist.h" - #include <QSettings> #include <QCompleter> @@ -33,9 +28,10 @@ TabDiveEquipment::TabDiveEquipment(QWidget *parent) : TabBase(parent), ui.weights->setModel(weightModel); connect(&diveListNotifier, &DiveListNotifier::divesChanged, this, &TabDiveEquipment::divesChanged); - connect(ui.cylinders, &TableView::itemClicked, cylindersModel, &CylindersModelFiltered::remove); connect(ui.cylinders, &TableView::itemClicked, this, &TabDiveEquipment::editCylinderWidget); connect(ui.weights, &TableView::itemClicked, this, &TabDiveEquipment::editWeightWidget); + connect(cylindersModel->model(), &CylindersModel::divesEdited, this, &TabDiveEquipment::divesEdited); + connect(weightModel, &WeightModel::divesEdited, this, &TabDiveEquipment::divesEdited); // Current display of things on Gnome3 looks like shit, so // let's fix that. @@ -129,7 +125,7 @@ void TabDiveEquipment::toggleTriggeredColumn() void TabDiveEquipment::updateData() { - cylindersModel->updateDive(); + cylindersModel->updateDive(current_dive); weightModel->updateDive(current_dive); suitModel.updateModel(); @@ -153,8 +149,7 @@ void TabDiveEquipment::clear() void TabDiveEquipment::addCylinder_clicked() { - MainWindow::instance()->mainTab->enableEdition(); - cylindersModel->add(); + divesEdited(Command::addCylinder(false)); } void TabDiveEquipment::addWeight_clicked() @@ -164,14 +159,13 @@ void TabDiveEquipment::addWeight_clicked() void TabDiveEquipment::editCylinderWidget(const QModelIndex &index) { - if (cylindersModel->model()->changed && !MainWindow::instance()->mainTab->isEditing()) { - MainWindow::instance()->mainTab->enableEdition(); + if (!index.isValid()) return; - } - if (index.isValid() && index.column() != CylindersModel::REMOVE) { - MainWindow::instance()->mainTab->enableEdition(); + + if (index.column() == CylindersModel::REMOVE) + divesEdited(Command::removeCylinder(cylindersModel->mapToSource(index).row(), false)); + else ui.cylinders->edit(index); - } } void TabDiveEquipment::editWeightWidget(const QModelIndex &index) @@ -185,87 +179,6 @@ void TabDiveEquipment::editWeightWidget(const QModelIndex &index) ui.weights->edit(index); } -// tricky little macro to edit all the selected dives -// loop ove all DIVES and do WHAT. -#define MODIFY_DIVES(DIVES, WHAT) \ - do { \ - for (dive *mydive: DIVES) { \ - invalidate_dive_cache(mydive); \ - WHAT; \ - } \ - mark_divelist_changed(true); \ - } while (0) - -// Get the list of selected dives, but put the current dive at the last position of the vector -static QVector<dive *> getSelectedDivesCurrentLast() -{ - QVector<dive *> res; - struct dive *d; - int i; - for_each_dive (i, d) { - if (d->selected && d != current_dive) - res.append(d); - } - res.append(current_dive); - return res; -} - -// TODO: This is a temporary functions until undo of cylinders is implemented. -// Therefore it is not worth putting it in a header. -extern bool cylinders_equal(const dive *d1, const dive *d2); - -void TabDiveEquipment::acceptChanges() -{ - bool do_replot = false; - - // now check if something has changed and if yes, edit the selected dives that - // were identical with the master dive shown (and mark the divelist as changed) - struct dive *cd = current_dive; - - // Get list of selected dives, but put the current dive last; - // this is required in case the invocation wants to compare things - // to the original value in current_dive like it should - QVector<dive *> selectedDives = getSelectedDivesCurrentLast(); - - if (cylindersModel->model()->changed) { - mark_divelist_changed(true); - MODIFY_DIVES(selectedDives, - // if we started out with the same cylinder description (for multi-edit) or if we do copt & paste - // make sure that we have the same cylinder type and copy the gasmix, but DON'T copy the start - // and end pressures (those are per dive after all) - if (cylinders_equal(mydive, cd) && mydive != cd) - copy_cylinder_types(&displayed_dive, cd); - copy_cylinders(&displayed_dive.cylinders, &cd->cylinders); - ); - /* if cylinders changed we may have changed gas change events - * and sensor idx in samples as well - * - so far this is ONLY supported for a single selected dive */ - struct divecomputer *tdc = ¤t_dive->dc; - struct divecomputer *sdc = &displayed_dive.dc; - while(tdc && sdc) { - free_events(tdc->events); - copy_events(sdc, tdc); - free(tdc->sample); - copy_samples(sdc, tdc); - tdc = tdc->next; - sdc = sdc->next; - } - do_replot = true; - } - - if (do_replot) - MainWindow::instance()->graphics->replot(); - - cylindersModel->model()->changed = false; -} - -void TabDiveEquipment::rejectChanges() -{ - cylindersModel->model()->changed = false; - cylindersModel->updateDive(); - weightModel->updateDive(current_dive); -} - void TabDiveEquipment::divesEdited(int i) { // No warning if only one dive was edited diff --git a/desktop-widgets/tab-widgets/TabDiveEquipment.h b/desktop-widgets/tab-widgets/TabDiveEquipment.h index 28e6235d1..55eb21d1e 100644 --- a/desktop-widgets/tab-widgets/TabDiveEquipment.h +++ b/desktop-widgets/tab-widgets/TabDiveEquipment.h @@ -21,9 +21,6 @@ public: ~TabDiveEquipment(); void updateData() override; void clear() override; - void acceptChanges(); - void rejectChanges(); - void divesEdited(int i); void closeWarning(); private slots: @@ -34,6 +31,7 @@ private slots: void editCylinderWidget(const QModelIndex &index); void editWeightWidget(const QModelIndex &index); void on_suit_editingFinished(); + void divesEdited(int count); private: Ui::TabDiveEquipment ui; diff --git a/desktop-widgets/tab-widgets/maintab.cpp b/desktop-widgets/tab-widgets/maintab.cpp index 252e135a6..7fde2e1b6 100644 --- a/desktop-widgets/tab-widgets/maintab.cpp +++ b/desktop-widgets/tab-widgets/maintab.cpp @@ -49,7 +49,8 @@ struct Completers { }; MainTab::MainTab(QWidget *parent) : QTabWidget(parent), - editMode(NONE), + editMode(false), + ignoreInput(false), lastSelectedDive(true), lastTabSelectedDive(0), lastTabSelectedDiveTrip(0), @@ -206,41 +207,19 @@ void MainTab::displayMessage(QString str) ui.diveNotesMessage->animatedShow(); } -void MainTab::enableEdition(EditMode newEditMode) +void MainTab::enableEdition() { - if (((newEditMode == DIVE || newEditMode == NONE) && current_dive == NULL) || editMode != NONE) + if (current_dive == NULL || editMode) return; - if ((newEditMode == DIVE || newEditMode == NONE) && - current_dive->dc.model && - strcmp(current_dive->dc.model, "manually added dive") == 0) { - // editCurrentDive will call enableEdition with newEditMode == MANUALLY_ADDED_DIVE - // so exit this function here after editCurrentDive() returns - - - - // FIXME : can we get rid of this recursive crap? - - - - MainWindow::instance()->editCurrentDive(); - return; - } ui.editDiveSiteButton->setEnabled(false); MainWindow::instance()->diveList->setEnabled(false); MainWindow::instance()->setEnabledToolbar(false); - MainWindow::instance()->enterEditState(); - ui.tabWidget->setTabEnabled(2, false); - ui.tabWidget->setTabEnabled(3, false); - ui.tabWidget->setTabEnabled(5, false); ui.dateEdit->setEnabled(true); - if (amount_selected > 1) { - displayMessage(tr("Multiple dives are being edited.")); - } else { - displayMessage(tr("This dive is being edited.")); - } - editMode = newEditMode != NONE ? newEditMode : DIVE; + displayMessage(tr("This dive is being edited.")); + + editMode = true; } // This function gets called if a field gets updated by an undo command. @@ -305,7 +284,7 @@ void MainTab::nextInputField(QKeyEvent *event) bool MainTab::isEditing() { - return editMode != NONE; + return editMode; } static bool isHtml(const QString &s) @@ -363,9 +342,8 @@ void MainTab::updateDiveSite(struct dive *d) void MainTab::updateDiveInfo() { ui.location->refreshDiveSiteCache(); - EditMode rememberEM = editMode; // don't execute this while adding / planning a dive - if (editMode == MANUALLY_ADDED_DIVE || MainWindow::instance()->graphics->isPlanner()) + if (editMode || MainWindow::instance()->graphics->isPlanner()) return; // If there is no current dive, disable all widgets except the last, which is the dive site tab. @@ -375,7 +353,7 @@ void MainTab::updateDiveInfo() for (int i = 0; i < extraWidgets.size() - 1; ++i) extraWidgets[i]->setEnabled(enabled); - editMode = IGNORE_MODE; // don't trigger on changes to the widgets + ignoreInput = true; // don't trigger on changes to the widgets for (TabBase *widget: extraWidgets) widget->updateData(); @@ -393,9 +371,6 @@ void MainTab::updateDiveInfo() if (lastSelectedDive && !onDiveSiteTab) lastTabSelectedDive = ui.tabWidget->currentIndex(); ui.tabWidget->setTabText(0, tr("Trip notes")); - ui.tabWidget->setTabEnabled(1, false); - ui.tabWidget->setTabEnabled(2, false); - ui.tabWidget->setTabEnabled(5, false); // Recover the tab selected for last dive trip but only if we're not on the dive site tab if (lastSelectedDive && !onDiveSiteTab) ui.tabWidget->setCurrentIndex(lastTabSelectedDiveTrip); @@ -434,11 +409,6 @@ void MainTab::updateDiveInfo() if (!lastSelectedDive && !onDiveSiteTab) lastTabSelectedDiveTrip = ui.tabWidget->currentIndex(); ui.tabWidget->setTabText(0, tr("Notes")); - ui.tabWidget->setTabEnabled(1, true); - ui.tabWidget->setTabEnabled(2, true); - ui.tabWidget->setTabEnabled(3, true); - ui.tabWidget->setTabEnabled(4, true); - ui.tabWidget->setTabEnabled(5, true); // Recover the tab selected for last dive but only if we're not on the dive site tab if (!lastSelectedDive && !onDiveSiteTab) ui.tabWidget->setCurrentIndex(lastTabSelectedDive); @@ -506,7 +476,7 @@ void MainTab::updateDiveInfo() ui.timeEdit->setTime(QTime(0, 0, 0, 0)); ui.tagWidget->clear(); } - editMode = rememberEM; + ignoreInput = false; if (verbose && current_dive && current_dive->dive_site) qDebug() << "Set the current dive site:" << current_dive->dive_site->uuid; @@ -529,22 +499,17 @@ void MainTab::acceptChanges() if (ui.location->hasFocus()) stealFocus(); - EditMode lastMode = editMode; - editMode = IGNORE_MODE; + ignoreInput = true; ui.dateEdit->setEnabled(true); hideMessage(); - // TODO: This is a temporary hack until the equipment tab is included in the undo system: - // The equipment tab is hardcoded at the first place of the "extra widgets". - ((TabDiveEquipment *)extraWidgets[0])->acceptChanges(); - - if (lastMode == MANUALLY_ADDED_DIVE) { + if (editMode) { MainWindow::instance()->showProfile(); DivePlannerPointsModel::instance()->setPlanMode(DivePlannerPointsModel::NOTHING); Command::editProfile(&displayed_dive); } int scrolledBy = MainWindow::instance()->diveList->verticalScrollBar()->sliderPosition(); - if (lastMode == MANUALLY_ADDED_DIVE) { + if (editMode) { MainWindow::instance()->diveList->reload(); MainWindow::instance()->refreshDisplay(); MainWindow::instance()->graphics->replot(); @@ -554,39 +519,15 @@ void MainTab::acceptChanges() DivePlannerPointsModel::instance()->setPlanMode(DivePlannerPointsModel::NOTHING); MainWindow::instance()->diveList->verticalScrollBar()->setSliderPosition(scrolledBy); MainWindow::instance()->diveList->setFocus(); - MainWindow::instance()->exitEditState(); MainWindow::instance()->setEnabledToolbar(true); ui.editDiveSiteButton->setEnabled(!ui.location->text().isEmpty()); - editMode = NONE; -} - -bool weightsystems_equal(const dive *d1, const dive *d2) -{ - if (d1->weightsystems.nr != d2->weightsystems.nr) - return false; - for (int i = 0; i < d1->weightsystems.nr; ++i) { - if (!same_weightsystem(d1->weightsystems.weightsystems[i], d2->weightsystems.weightsystems[i])) - return false; - } - return true; -} - -bool cylinders_equal(const dive *d1, const dive *d2) -{ - if (d1->cylinders.nr != d2->cylinders.nr) - return false; - for (int i = 0; i < d1->cylinders.nr; ++i) { - if (!same_cylinder(*get_cylinder(d1, i), *get_cylinder(d2, i))) - return false; - } - return true; + ignoreInput = false; + editMode = false; } void MainTab::rejectChanges() { - EditMode lastMode = editMode; - - if (lastMode != NONE && current_dive && !cylinders_equal(current_dive, &displayed_dive)) { + if (editMode && current_dive) { if (QMessageBox::warning(MainWindow::instance(), TITLE_OR_TEXT(tr("Discard the changes?"), tr("You are about to discard your changes.")), QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Discard) != QMessageBox::Discard) { @@ -594,7 +535,7 @@ void MainTab::rejectChanges() } } ui.dateEdit->setEnabled(true); - editMode = NONE; + editMode = false; hideMessage(); // no harm done to call cancelPlan even if we were not PLAN mode... DivePlannerPointsModel::instance()->cancelPlan(); @@ -606,17 +547,9 @@ void MainTab::rejectChanges() clear_dive(&displayed_dive); updateDiveInfo(); - // TODO: This is a temporary hack until the equipment tab is included in the undo system: - // The equipment tab is hardcoded at the first place of the "extra widgets". - ((TabDiveEquipment *)extraWidgets[0])->rejectChanges(); - - // the user could have edited the location and then canceled the edit - // let's get the correct location back in view - MapWidget::instance()->centerOnDiveSite(current_dive ? current_dive->dive_site : nullptr); // show the profile and dive info MainWindow::instance()->graphics->replot(); MainWindow::instance()->setEnabledToolbar(true); - MainWindow::instance()->exitEditState(); ui.editDiveSiteButton->setEnabled(!ui.location->text().isEmpty()); } @@ -632,7 +565,7 @@ void MainTab::divesEdited(int i) void MainTab::on_buddy_editingFinished() { - if (editMode == IGNORE_MODE || !current_dive) + if (ignoreInput || !current_dive) return; divesEdited(Command::editBuddies(stringToList(ui.buddy->toPlainText()), false)); @@ -640,7 +573,7 @@ void MainTab::on_buddy_editingFinished() void MainTab::on_divemaster_editingFinished() { - if (editMode == IGNORE_MODE || !current_dive) + if (ignoreInput || !current_dive) return; divesEdited(Command::editDiveMaster(stringToList(ui.divemaster->toPlainText()), false)); @@ -648,7 +581,7 @@ void MainTab::on_divemaster_editingFinished() void MainTab::on_duration_editingFinished() { - if (editMode == IGNORE_MODE || !current_dive) + if (ignoreInput || !current_dive) return; // Duration editing is special: we only edit the current dive. @@ -657,7 +590,7 @@ void MainTab::on_duration_editingFinished() void MainTab::on_depth_editingFinished() { - if (editMode == IGNORE_MODE || !current_dive) + if (ignoreInput || !current_dive) return; // Depth editing is special: we only edit the current dive. @@ -677,7 +610,7 @@ static void shiftTime(QDateTime &dateTime) void MainTab::on_dateEdit_dateChanged(const QDate &date) { - if (editMode == IGNORE_MODE || !current_dive) + if (ignoreInput || !current_dive) return; QDateTime dateTime = QDateTime::fromMSecsSinceEpoch(1000*current_dive->when, Qt::UTC); dateTime.setTimeSpec(Qt::UTC); @@ -687,7 +620,7 @@ void MainTab::on_dateEdit_dateChanged(const QDate &date) void MainTab::on_timeEdit_timeChanged(const QTime &time) { - if (editMode == IGNORE_MODE || !current_dive) + if (ignoreInput || !current_dive) return; QDateTime dateTime = QDateTime::fromMSecsSinceEpoch(1000*current_dive->when, Qt::UTC); dateTime.setTimeSpec(Qt::UTC); @@ -697,7 +630,7 @@ void MainTab::on_timeEdit_timeChanged(const QTime &time) void MainTab::on_tagWidget_editingFinished() { - if (editMode == IGNORE_MODE || !current_dive) + if (ignoreInput || !current_dive) return; divesEdited(Command::editTags(ui.tagWidget->getBlockStringList(), false)); @@ -705,7 +638,7 @@ void MainTab::on_tagWidget_editingFinished() void MainTab::on_location_diveSiteSelected() { - if (editMode == IGNORE_MODE || !current_dive) + if (ignoreInput || !current_dive) return; struct dive_site *newDs = ui.location->currDiveSite(); @@ -743,7 +676,7 @@ void MainTab::on_notes_editingFinished() void MainTab::on_rating_valueChanged(int value) { - if (editMode == IGNORE_MODE || !current_dive) + if (ignoreInput || !current_dive) return; divesEdited(Command::editRating(value, false)); @@ -760,7 +693,7 @@ void MainTab::escDetected() { // In edit mode, pressing escape cancels the current changes. // In standard mode, remove focus of any active widget to - if (editMode != NONE) + if (editMode) rejectChanges(); else stealFocus(); diff --git a/desktop-widgets/tab-widgets/maintab.h b/desktop-widgets/tab-widgets/maintab.h index 6411f6b3a..49003be4e 100644 --- a/desktop-widgets/tab-widgets/maintab.h +++ b/desktop-widgets/tab-widgets/maintab.h @@ -26,13 +26,6 @@ class TabBase; class MainTab : public QTabWidget { Q_OBJECT public: - enum EditMode { - NONE, - DIVE, - MANUALLY_ADDED_DIVE, - IGNORE_MODE - }; - MainTab(QWidget *parent = 0); ~MainTab(); void clearTabs(); @@ -72,11 +65,12 @@ slots: void closeMessage(); void closeWarning(); void displayMessage(QString str); - void enableEdition(EditMode newEditMode = NONE); + void enableEdition(); void escDetected(void); private: Ui::MainTab ui; - EditMode editMode; + bool editMode; + bool ignoreInput; // When computionally editing fields, we have to ignore changed-signals BuddyCompletionModel buddyModel; DiveMasterCompletionModel diveMasterModel; TagCompletionModel tagModel; |