Skip to content

Commit

Permalink
Use combo box for moving sensor data
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
michaelandreen authored and dirkhh committed Sep 3, 2022
1 parent 70e43d1 commit a39e69c
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 30 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import: allow import of divesites without UUID
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: use combobox for moving sensor between cylinders
mobile: send log files as attachments for support emails on iOS
mobile: allow cloud account deletion (Apple app store requirement)
mobile: fix listing of local cloud cache directories
Expand Down
3 changes: 3 additions & 0 deletions commands/command_edit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1361,6 +1361,9 @@ void EditSensors::mapSensors(int toCyl, int fromCyl)
for (int s = 0; s < MAX_SENSORS; ++s) {
if (dc->sample[i].pressure[s].mbar && dc->sample[i].sensor[s] == fromCyl)
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);
Expand Down
37 changes: 37 additions & 0 deletions desktop-widgets/modeldelegates.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: GPL-2.0

#include "desktop-widgets/modeldelegates.h"
#include "core/sample.h"
#include "core/subsurface-string.h"
#include "core/gettextfromc.h"
#include "desktop-widgets/mainwindow.h"
Expand Down Expand Up @@ -281,6 +282,42 @@ void TankUseDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, c
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)
{
WeightModel *mymodel = qobject_cast<WeightModel *>(currCombo.model);
Expand Down
9 changes: 9 additions & 0 deletions desktop-widgets/modeldelegates.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ class TankUseDelegate : public QStyledItemDelegate {
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 {
Q_OBJECT
public:
Expand Down
1 change: 1 addition & 0 deletions desktop-widgets/tab-widgets/TabDiveEquipment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ TabDiveEquipment::TabDiveEquipment(QWidget *parent) : TabBase(parent),

ui.cylinders->view()->setItemDelegateForColumn(CylindersModel::TYPE, new TankInfoDelegate(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.cylinders->view()->setColumnHidden(CylindersModel::DEPTH, true);
ui.cylinders->view()->setColumnHidden(CylindersModel::WORKINGPRESS_INT, true);
Expand Down
33 changes: 3 additions & 30 deletions qt-models/cylindermodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,23 +240,17 @@ QVariant CylindersModel::data(const QModelIndex &index, int role) const
case SIZE_INT:
return static_cast<int>(cyl->type.size.mliter);
case SENSORS: {
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 (auto s = 0; s < MAX_SENSORS; ++s) {
for (int s = 0; s < MAX_SENSORS; ++s) {
if (sample.pressure[s].mbar) {
if (sample.sensor[s] == index.row())
return tr("Sensor attached, can't move another sensor here.");
else if (std::find(sensors.begin(), sensors.end(), sample.sensor[s]) == sensors.end())
sensors.push_back(sample.sensor[s]);
return QString::number(sample.sensor[s]);
}
}
}
QStringList sensorStrings;
for (auto s : sensors)
sensorStrings << QString::number(s);
return tr("Select one of these cylinders: ") + sensorStrings.join(",");
return QString();
}
}
break;
Expand Down Expand Up @@ -477,14 +471,6 @@ bool CylindersModel::setData(const QModelIndex &index, const QVariant &value, in
type = Command::EditCylinderType::TYPE;
break;
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;
int s = vString.toInt(&ok);
if (ok) {
Expand Down Expand Up @@ -553,19 +539,6 @@ Qt::ItemFlags CylindersModel::flags(const QModelIndex &index) const
{
if (index.column() == REMOVE || index.column() == USE)
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;
}

Expand Down

0 comments on commit a39e69c

Please sign in to comment.