devices: connect DiveComputerModel to undo-command

Instead of modifying the device table directly, call the undo
commands. Moreover, don't keep our own copy in the mode - show
the original version. Connect to the appropriate signals.

This means that the calls from the DiveComputerManagement
dialog have to be removed, since this mode of editing is
not supported. The whole dialog will be removed in a future
commit.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2020-10-21 23:10:08 +02:00 committed by Dirk Hohndel
parent 4467477389
commit c6188bbe47
4 changed files with 36 additions and 41 deletions

View file

@ -1,3 +1,4 @@
- desktop: make device management undoable
- desktop: fix crash on drag&drop into tag widgets [#3030] - desktop: fix crash on drag&drop into tag widgets [#3030]
- macOS: fix issue with HID dive computers like Suunto Eon Core/Steel [#2999] - macOS: fix issue with HID dive computers like Suunto Eon Core/Steel [#2999]
- windows: add missing modern Vista theme - windows: add missing modern Vista theme

View file

@ -36,22 +36,10 @@ DiveComputerManagementDialog *DiveComputerManagementDialog::instance()
void DiveComputerManagementDialog::tryRemove(const QModelIndex &index) void DiveComputerManagementDialog::tryRemove(const QModelIndex &index)
{ {
if (index.column() != DiveComputerModel::REMOVE)
return;
QMessageBox::StandardButton response = QMessageBox::question(
this, TITLE_OR_TEXT(
tr("Remove the selected dive computer?"),
tr("Are you sure that you want to \n remove the selected dive computer?")),
QMessageBox::Ok | QMessageBox::Cancel);
if (response == QMessageBox::Ok)
model->remove(index);
} }
void DiveComputerManagementDialog::accept() void DiveComputerManagementDialog::accept()
{ {
model->keepWorkingList();
hide(); hide();
close(); close();
} }

View file

@ -1,13 +1,16 @@
// SPDX-License-Identifier: GPL-2.0 // SPDX-License-Identifier: GPL-2.0
#include "qt-models/divecomputermodel.h" #include "qt-models/divecomputermodel.h"
#include "commands/command.h"
#include "core/dive.h" #include "core/dive.h"
#include "core/divelist.h" #include "core/divelist.h"
#include "core/subsurface-qt/divelistnotifier.h" #include "core/subsurface-qt/divelistnotifier.h"
DiveComputerModel::DiveComputerModel(QObject *parent) : CleanerTableModel(parent), DiveComputerModel::DiveComputerModel(QObject *parent) : CleanerTableModel(parent)
dcs(device_table.devices)
{ {
connect(&diveListNotifier, &DiveListNotifier::dataReset, this, &DiveComputerModel::update); connect(&diveListNotifier, &DiveListNotifier::dataReset, this, &DiveComputerModel::update);
connect(&diveListNotifier, &DiveListNotifier::deviceAdded, this, &DiveComputerModel::deviceAdded);
connect(&diveListNotifier, &DiveListNotifier::deviceDeleted, this, &DiveComputerModel::deviceDeleted);
connect(&diveListNotifier, &DiveListNotifier::deviceEdited, this, &DiveComputerModel::deviceEdited);
setHeaderDataStrings(QStringList() << "" << tr("Model") << tr("Device ID") << tr("Nickname")); setHeaderDataStrings(QStringList() << "" << tr("Model") << tr("Device ID") << tr("Nickname"));
update(); update();
} }
@ -15,15 +18,15 @@ DiveComputerModel::DiveComputerModel(QObject *parent) : CleanerTableModel(parent
void DiveComputerModel::update() void DiveComputerModel::update()
{ {
beginResetModel(); beginResetModel();
dcs = device_table.devices;
endResetModel(); endResetModel();
} }
QVariant DiveComputerModel::data(const QModelIndex &index, int role) const QVariant DiveComputerModel::data(const QModelIndex &index, int role) const
{ {
if (index.row() < 0 || index.row() >= (int)dcs.size()) const device *dev = get_device(&device_table, index.row());
if (dev == nullptr)
return QVariant(); return QVariant();
const device &node = dcs[index.row()]; const device &node = *dev;
if (role == Qt::DisplayRole || role == Qt::EditRole) { if (role == Qt::DisplayRole || role == Qt::EditRole) {
switch (index.column()) { switch (index.column()) {
@ -51,7 +54,24 @@ QVariant DiveComputerModel::data(const QModelIndex &index, int role) const
int DiveComputerModel::rowCount(const QModelIndex&) const int DiveComputerModel::rowCount(const QModelIndex&) const
{ {
return dcs.size(); return (int)device_table.devices.size();
}
void DiveComputerModel::deviceAdded(int idx)
{
beginInsertRows(QModelIndex(), idx, idx);
endInsertRows();
}
void DiveComputerModel::deviceDeleted(int idx)
{
beginRemoveRows(QModelIndex(), idx, idx);
endRemoveRows();
}
void DiveComputerModel::deviceEdited(int idx)
{
dataChanged(index(idx, REMOVE), index(idx, NICKNAME));
} }
Qt::ItemFlags DiveComputerModel::flags(const QModelIndex &index) const Qt::ItemFlags DiveComputerModel::flags(const QModelIndex &index) const
@ -65,29 +85,16 @@ Qt::ItemFlags DiveComputerModel::flags(const QModelIndex &index) const
bool DiveComputerModel::setData(const QModelIndex &index, const QVariant &value, int) bool DiveComputerModel::setData(const QModelIndex &index, const QVariant &value, int)
{ {
// We should test if the role == Qt::EditRole // We should test if the role == Qt::EditRole
if (index.row() < 0 || index.row() >= (int)dcs.size()) Command::editDeviceNickname(index.row(), value.toString());
return false;
device &node = dcs[index.row()];
node.nickName = value.toString().toStdString();
emit dataChanged(index, index);
return true; return true;
} }
void DiveComputerModel::remove(const QModelIndex &index) void DiveComputerModel::remove(const QModelIndex &index)
{ {
if (index.row() < 0 || index.row() >= (int)dcs.size()) int row = index.row();
if (row < 0 || row >= (int)device_table.devices.size())
return; return;
beginRemoveRows(QModelIndex(), index.row(), index.row()); Command::removeDevice(index.row());
dcs.erase(dcs.begin() + index.row());
endRemoveRows();
}
void DiveComputerModel::keepWorkingList()
{
if (device_table.devices != dcs)
mark_divelist_changed(true);
device_table.devices = dcs;
} }
// Convenience function to access alternative columns // Convenience function to access alternative columns
@ -129,5 +136,5 @@ void DiveComputerSortedModel::remove(const QModelIndex &index)
int row = mapToSource(index).row(); int row = mapToSource(index).row();
if (row < 0 || row >= (int)device_table.devices.size()) if (row < 0 || row >= (int)device_table.devices.size())
return; return;
device_table.devices.erase(device_table.devices.begin() + row); Command::removeDevice(row);
} }

View file

@ -20,15 +20,14 @@ public:
int rowCount(const QModelIndex &parent = QModelIndex()) const override; int rowCount(const QModelIndex &parent = QModelIndex()) const override;
Qt::ItemFlags flags(const QModelIndex &index) const override; Qt::ItemFlags flags(const QModelIndex &index) const override;
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
void keepWorkingList();
public private
slots: slots:
void remove(const QModelIndex &index); void remove(const QModelIndex &index);
void update(); void update();
void deviceAdded(int idx);
private: void deviceDeleted(int idx);
std::vector<device> dcs; void deviceEdited(int idx);
}; };
class DiveComputerSortedModel : public QSortFilterProxyModel { class DiveComputerSortedModel : public QSortFilterProxyModel {