Use combo box for moving sensor data

Also allow editing sensor on a cylinder with already attached sensor.
This will swap the sensor data with the cylinder that it is taking the
sensor data from, removing the need for adding an extra temporary
cylinder when swapping two sensors.

Signed-off-by: Michael Andreen <michael@andreen.dev>
This commit is contained in:
Michael Andreen 2022-09-02 22:51:19 +02:00 committed by Dirk Hohndel
parent 70e43d13a0
commit a39e69c497
6 changed files with 54 additions and 30 deletions

View file

@ -1,6 +1,7 @@
import: allow import of divesites without UUID import: allow import of divesites without UUID
divelist: do not include planned versions of a dive if there is real data divelist: do not include planned versions of a dive if there is real data
desktop: fix key composition in tag widgets and dive site widget desktop: fix key composition in tag widgets and dive site widget
desktop: use combobox for moving sensor between cylinders
mobile: send log files as attachments for support emails on iOS mobile: send log files as attachments for support emails on iOS
mobile: allow cloud account deletion (Apple app store requirement) mobile: allow cloud account deletion (Apple app store requirement)
mobile: fix listing of local cloud cache directories mobile: fix listing of local cloud cache directories

View file

@ -1361,6 +1361,9 @@ void EditSensors::mapSensors(int toCyl, int fromCyl)
for (int s = 0; s < MAX_SENSORS; ++s) { for (int s = 0; s < MAX_SENSORS; ++s) {
if (dc->sample[i].pressure[s].mbar && dc->sample[i].sensor[s] == fromCyl) if (dc->sample[i].pressure[s].mbar && dc->sample[i].sensor[s] == fromCyl)
dc->sample[i].sensor[s] = toCyl; dc->sample[i].sensor[s] = toCyl;
// In case the cylinder we are moving to has a sensor attached, move it to the other cylinder
else if (dc->sample[i].pressure[s].mbar && dc->sample[i].sensor[s] == toCyl)
dc->sample[i].sensor[s] = fromCyl;
} }
} }
emit diveListNotifier.diveComputerEdited(dc); emit diveListNotifier.diveComputerEdited(dc);

View file

@ -1,6 +1,7 @@
// SPDX-License-Identifier: GPL-2.0 // SPDX-License-Identifier: GPL-2.0
#include "desktop-widgets/modeldelegates.h" #include "desktop-widgets/modeldelegates.h"
#include "core/sample.h"
#include "core/subsurface-string.h" #include "core/subsurface-string.h"
#include "core/gettextfromc.h" #include "core/gettextfromc.h"
#include "desktop-widgets/mainwindow.h" #include "desktop-widgets/mainwindow.h"
@ -281,6 +282,42 @@ void TankUseDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, c
model->setData(index, comboBox->currentIndex()); model->setData(index, comboBox->currentIndex());
} }
SensorDelegate::SensorDelegate(QObject *parent) : QStyledItemDelegate(parent)
{
}
QWidget *SensorDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &index) const
{
QComboBox *comboBox = new QComboBox(parent);
std::vector<int16_t> sensors;
const struct divecomputer *currentdc = get_dive_dc(current_dive, dc_number);
for (int i = 0; i < currentdc->samples; ++i) {
auto &sample = currentdc->sample[i];
for (int s = 0; s < MAX_SENSORS; ++s) {
if (sample.pressure[s].mbar) {
if (std::find(sensors.begin(), sensors.end(), sample.sensor[s]) == sensors.end())
sensors.push_back(sample.sensor[s]);
}
}
}
std::sort(sensors.begin(), sensors.end());
for (auto s : sensors)
comboBox->addItem(QString::number(s));
comboBox->setCurrentIndex(-1);
QString indexString = index.data().toString();
if (!indexString.isEmpty())
comboBox->setCurrentText(indexString);
return comboBox;
}
void SensorDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
QComboBox *comboBox = qobject_cast<QComboBox *>(editor);
model->setData(index, comboBox->currentText());
}
void WSInfoDelegate::editorClosed(QWidget *, QAbstractItemDelegate::EndEditHint hint) void WSInfoDelegate::editorClosed(QWidget *, QAbstractItemDelegate::EndEditHint hint)
{ {
WeightModel *mymodel = qobject_cast<WeightModel *>(currCombo.model); WeightModel *mymodel = qobject_cast<WeightModel *>(currCombo.model);

View file

@ -79,6 +79,15 @@ private:
void setEditorData(QWidget *editor, const QModelIndex &index) const override; void setEditorData(QWidget *editor, const QModelIndex &index) const override;
}; };
class SensorDelegate : public QStyledItemDelegate {
Q_OBJECT
public:
explicit SensorDelegate(QObject *parent = 0);
private:
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
};
class WSInfoDelegate : public ComboBoxDelegate { class WSInfoDelegate : public ComboBoxDelegate {
Q_OBJECT Q_OBJECT
public: public:

View file

@ -61,6 +61,7 @@ TabDiveEquipment::TabDiveEquipment(QWidget *parent) : TabBase(parent),
ui.cylinders->view()->setItemDelegateForColumn(CylindersModel::TYPE, new TankInfoDelegate(this)); ui.cylinders->view()->setItemDelegateForColumn(CylindersModel::TYPE, new TankInfoDelegate(this));
ui.cylinders->view()->setItemDelegateForColumn(CylindersModel::USE, new TankUseDelegate(this)); ui.cylinders->view()->setItemDelegateForColumn(CylindersModel::USE, new TankUseDelegate(this));
ui.cylinders->view()->setItemDelegateForColumn(CylindersModel::SENSORS, new SensorDelegate(this));
ui.weights->view()->setItemDelegateForColumn(WeightModel::TYPE, new WSInfoDelegate(this)); ui.weights->view()->setItemDelegateForColumn(WeightModel::TYPE, new WSInfoDelegate(this));
ui.cylinders->view()->setColumnHidden(CylindersModel::DEPTH, true); ui.cylinders->view()->setColumnHidden(CylindersModel::DEPTH, true);
ui.cylinders->view()->setColumnHidden(CylindersModel::WORKINGPRESS_INT, true); ui.cylinders->view()->setColumnHidden(CylindersModel::WORKINGPRESS_INT, true);

View file

@ -240,23 +240,17 @@ QVariant CylindersModel::data(const QModelIndex &index, int role) const
case SIZE_INT: case SIZE_INT:
return static_cast<int>(cyl->type.size.mliter); return static_cast<int>(cyl->type.size.mliter);
case SENSORS: { case SENSORS: {
std::vector<int16_t> sensors;
const struct divecomputer *currentdc = get_dive_dc(current_dive, dc_number); const struct divecomputer *currentdc = get_dive_dc(current_dive, dc_number);
for (int i = 0; i < currentdc->samples; ++i) { for (int i = 0; i < currentdc->samples; ++i) {
auto &sample = currentdc->sample[i]; auto &sample = currentdc->sample[i];
for (auto s = 0; s < MAX_SENSORS; ++s) { for (int s = 0; s < MAX_SENSORS; ++s) {
if (sample.pressure[s].mbar) { if (sample.pressure[s].mbar) {
if (sample.sensor[s] == index.row()) if (sample.sensor[s] == index.row())
return tr("Sensor attached, can't move another sensor here."); return QString::number(sample.sensor[s]);
else if (std::find(sensors.begin(), sensors.end(), sample.sensor[s]) == sensors.end())
sensors.push_back(sample.sensor[s]);
} }
} }
} }
QStringList sensorStrings; return QString();
for (auto s : sensors)
sensorStrings << QString::number(s);
return tr("Select one of these cylinders: ") + sensorStrings.join(",");
} }
} }
break; break;
@ -477,14 +471,6 @@ bool CylindersModel::setData(const QModelIndex &index, const QVariant &value, in
type = Command::EditCylinderType::TYPE; type = Command::EditCylinderType::TYPE;
break; break;
case SENSORS: { case SENSORS: {
std::vector<int> sensors;
for (auto &sensor : vString.split(",")) {
bool ok = false;
int s = sensor.toInt(&ok);
if (ok && s < MAX_SENSORS)
sensors.push_back(s);
}
bool ok = false; bool ok = false;
int s = vString.toInt(&ok); int s = vString.toInt(&ok);
if (ok) { if (ok) {
@ -553,19 +539,6 @@ Qt::ItemFlags CylindersModel::flags(const QModelIndex &index) const
{ {
if (index.column() == REMOVE || index.column() == USE) if (index.column() == REMOVE || index.column() == USE)
return Qt::ItemIsEnabled; return Qt::ItemIsEnabled;
if (index.column() == SENSORS) {
const struct divecomputer *currentdc = get_dive_dc(current_dive, dc_number);
for (int i = 0; i < currentdc->samples; ++i) {
auto &sample = currentdc->sample[i];
for (auto s = 0; s < MAX_SENSORS; ++s) {
if (sample.pressure[s].mbar) {
if (sample.sensor[s] == index.row())
// Sensor attached, not editable.
return QAbstractItemModel::flags(index);
}
}
}
}
return QAbstractItemModel::flags(index) | Qt::ItemIsEditable; return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
} }