profile: pass dive to DiveHandler

The DiveHandler shows a context menu where a cylinder can be
chosen. This indirectly accesses the global displayed_dive
variable.

Remove this in a step to make the profile reentrant.

The code was quite ominous: instead of simply generating the
list of cylinders, a global model was reset and then accessed
with Qt's cumbersome model/view API. All this trampling over
global state can be removed by simply making the function
that generates the list globally accessible.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2021-01-09 21:18:37 +01:00 committed by Dirk Hohndel
parent 88c6ce988d
commit 235146a95f
6 changed files with 30 additions and 27 deletions

View file

@ -1274,6 +1274,23 @@ QString get_gas_string(struct gasmix gas)
return result; return result;
} }
QStringList get_dive_gas_list(const struct dive *d)
{
QStringList list;
for (int i = 0; i < d->cylinders.nr; i++) {
const cylinder_t *cyl = get_cylinder(d, i);
/* Check if we have the same gasmix two or more times
* If yes return more verbose string */
int same_gas = same_gasmix_cylinder(cyl, i, d, true);
if (same_gas == -1)
list.push_back(get_gas_string(cyl->gasmix));
else
list.push_back(get_gas_string(cyl->gasmix) + QString(" (%1 %2 ").arg(gettextFromC::tr("cyl.")).arg(i + 1) +
cyl->type.description + ")");
}
return list;
}
QString get_taglist_string(struct tag_entry *tag_list) QString get_taglist_string(struct tag_entry *tag_list)
{ {
char *buffer = taglist_get_tagstring(tag_list); char *buffer = taglist_get_tagstring(tag_list);

View file

@ -27,6 +27,7 @@ QString weight_string(int weight_in_grams);
QString distance_string(int distanceInMeters); QString distance_string(int distanceInMeters);
bool gpsHasChanged(struct dive *dive, struct dive *master, const QString &gps_text, bool *parsed_out = 0); bool gpsHasChanged(struct dive *dive, struct dive *master, const QString &gps_text, bool *parsed_out = 0);
QString get_gas_string(struct gasmix gas); QString get_gas_string(struct gasmix gas);
QStringList get_dive_gas_list(const struct dive *d);
QString get_taglist_string(struct tag_entry *tag_list); QString get_taglist_string(struct tag_entry *tag_list);
QStringList stringToList(const QString &s); QStringList stringToList(const QString &s);
void read_hashes(); void read_hashes();

View file

@ -1,15 +1,16 @@
// SPDX-License-Identifier: GPL-2.0 // SPDX-License-Identifier: GPL-2.0
#include "divehandler.h" #include "divehandler.h"
#include "profilewidget2.h" #include "profilewidget2.h"
#include "core/dive.h"
#include "core/gettextfromc.h" #include "core/gettextfromc.h"
#include "core/qthelper.h"
#include "qt-models/diveplannermodel.h" #include "qt-models/diveplannermodel.h"
#include "qt-models/models.h"
#include <QMenu> #include <QMenu>
#include <QGraphicsSceneMouseEvent> #include <QGraphicsSceneMouseEvent>
#include <QSettings> #include <QSettings>
DiveHandler::DiveHandler() : QGraphicsEllipseItem() DiveHandler::DiveHandler(const struct dive *d) : dive(d)
{ {
setRect(-5, -5, 10, 10); setRect(-5, -5, 10, 10);
setFlags(ItemIgnoresTransformations | ItemIsSelectable | ItemIsMovable | ItemSendsGeometryChanges); setFlags(ItemIgnoresTransformations | ItemIsSelectable | ItemIsMovable | ItemSendsGeometryChanges);
@ -32,12 +33,10 @@ void DiveHandler::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
DivePlannerPointsModel *plannerModel = DivePlannerPointsModel::instance(); DivePlannerPointsModel *plannerModel = DivePlannerPointsModel::instance();
QModelIndex index = plannerModel->index(parentIndex(), DivePlannerPointsModel::GAS); QModelIndex index = plannerModel->index(parentIndex(), DivePlannerPointsModel::GAS);
if (index.sibling(index.row() + 1, index.column()).isValid()) { if (index.sibling(index.row() + 1, index.column()).isValid()) {
GasSelectionModel *model = GasSelectionModel::instance(); QStringList gases = get_dive_gas_list(dive);
model->repopulate(); for (int i = 0; i < gases.size(); i++) {
int rowCount = model->rowCount();
for (int i = 0; i < rowCount; i++) {
QAction *action = new QAction(&m); QAction *action = new QAction(&m);
action->setText(model->data(model->index(i, 0), Qt::DisplayRole).toString()); action->setText(gases[i]);
action->setData(i); action->setData(i);
connect(action, &QAction::triggered, this, &DiveHandler::changeGas); connect(action, &QAction::triggered, this, &DiveHandler::changeGas);
m.addAction(action); m.addAction(action);

View file

@ -5,10 +5,12 @@
#include <QGraphicsPathItem> #include <QGraphicsPathItem>
#include <QElapsedTimer> #include <QElapsedTimer>
struct dive;
class DiveHandler : public QObject, public QGraphicsEllipseItem { class DiveHandler : public QObject, public QGraphicsEllipseItem {
Q_OBJECT Q_OBJECT
public: public:
DiveHandler(); DiveHandler(const struct dive *d);
protected: protected:
void contextMenuEvent(QGraphicsSceneContextMenuEvent *event); void contextMenuEvent(QGraphicsSceneContextMenuEvent *event);
@ -26,6 +28,7 @@ slots:
void selfRemove(); void selfRemove();
void changeGas(); void changeGas();
private: private:
const struct dive *dive;
QElapsedTimer t; QElapsedTimer t;
}; };

View file

@ -1695,7 +1695,7 @@ void ProfileWidget2::disconnectTemporaryConnections()
#ifndef SUBSURFACE_MOBILE #ifndef SUBSURFACE_MOBILE
void ProfileWidget2::pointInserted(const QModelIndex&, int, int) void ProfileWidget2::pointInserted(const QModelIndex&, int, int)
{ {
DiveHandler *item = new DiveHandler(); DiveHandler *item = new DiveHandler(&displayed_dive);
scene()->addItem(item); scene()->addItem(item);
handles << item; handles << item;

View file

@ -24,26 +24,9 @@ GasSelectionModel *GasSelectionModel::instance()
return &self; return &self;
} }
static QStringList getGasList()
{
QStringList list;
for (int i = 0; i < displayed_dive.cylinders.nr; i++) {
const cylinder_t *cyl = get_cylinder(&displayed_dive, i);
/* Check if we have the same gasmix two or more times
* If yes return more verbose string */
int same_gas = same_gasmix_cylinder(cyl, i, &displayed_dive, true);
if (same_gas == -1)
list.push_back(get_gas_string(cyl->gasmix));
else
list.push_back(get_gas_string(cyl->gasmix) + QString(" (%1 %2 ").arg(GasSelectionModel::tr("cyl.")).arg(i + 1) +
cyl->type.description + ")");
}
return list;
}
void GasSelectionModel::repopulate() void GasSelectionModel::repopulate()
{ {
setStringList(getGasList()); setStringList(get_dive_gas_list(&displayed_dive));
} }
QVariant GasSelectionModel::data(const QModelIndex &index, int role) const QVariant GasSelectionModel::data(const QModelIndex &index, int role) const