mirror of
https://github.com/subsurface/subsurface.git
synced 2024-11-30 22:20:21 +00:00
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:
parent
88c6ce988d
commit
235146a95f
6 changed files with 30 additions and 27 deletions
|
@ -1274,6 +1274,23 @@ QString get_gas_string(struct gasmix gas)
|
|||
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)
|
||||
{
|
||||
char *buffer = taglist_get_tagstring(tag_list);
|
||||
|
|
|
@ -27,6 +27,7 @@ QString weight_string(int weight_in_grams);
|
|||
QString distance_string(int distanceInMeters);
|
||||
bool gpsHasChanged(struct dive *dive, struct dive *master, const QString &gps_text, bool *parsed_out = 0);
|
||||
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);
|
||||
QStringList stringToList(const QString &s);
|
||||
void read_hashes();
|
||||
|
|
|
@ -1,15 +1,16 @@
|
|||
// SPDX-License-Identifier: GPL-2.0
|
||||
#include "divehandler.h"
|
||||
#include "profilewidget2.h"
|
||||
#include "core/dive.h"
|
||||
#include "core/gettextfromc.h"
|
||||
#include "core/qthelper.h"
|
||||
#include "qt-models/diveplannermodel.h"
|
||||
#include "qt-models/models.h"
|
||||
|
||||
#include <QMenu>
|
||||
#include <QGraphicsSceneMouseEvent>
|
||||
#include <QSettings>
|
||||
|
||||
DiveHandler::DiveHandler() : QGraphicsEllipseItem()
|
||||
DiveHandler::DiveHandler(const struct dive *d) : dive(d)
|
||||
{
|
||||
setRect(-5, -5, 10, 10);
|
||||
setFlags(ItemIgnoresTransformations | ItemIsSelectable | ItemIsMovable | ItemSendsGeometryChanges);
|
||||
|
@ -32,12 +33,10 @@ void DiveHandler::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
|
|||
DivePlannerPointsModel *plannerModel = DivePlannerPointsModel::instance();
|
||||
QModelIndex index = plannerModel->index(parentIndex(), DivePlannerPointsModel::GAS);
|
||||
if (index.sibling(index.row() + 1, index.column()).isValid()) {
|
||||
GasSelectionModel *model = GasSelectionModel::instance();
|
||||
model->repopulate();
|
||||
int rowCount = model->rowCount();
|
||||
for (int i = 0; i < rowCount; i++) {
|
||||
QStringList gases = get_dive_gas_list(dive);
|
||||
for (int i = 0; i < gases.size(); i++) {
|
||||
QAction *action = new QAction(&m);
|
||||
action->setText(model->data(model->index(i, 0), Qt::DisplayRole).toString());
|
||||
action->setText(gases[i]);
|
||||
action->setData(i);
|
||||
connect(action, &QAction::triggered, this, &DiveHandler::changeGas);
|
||||
m.addAction(action);
|
||||
|
|
|
@ -5,10 +5,12 @@
|
|||
#include <QGraphicsPathItem>
|
||||
#include <QElapsedTimer>
|
||||
|
||||
struct dive;
|
||||
|
||||
class DiveHandler : public QObject, public QGraphicsEllipseItem {
|
||||
Q_OBJECT
|
||||
public:
|
||||
DiveHandler();
|
||||
DiveHandler(const struct dive *d);
|
||||
|
||||
protected:
|
||||
void contextMenuEvent(QGraphicsSceneContextMenuEvent *event);
|
||||
|
@ -26,6 +28,7 @@ slots:
|
|||
void selfRemove();
|
||||
void changeGas();
|
||||
private:
|
||||
const struct dive *dive;
|
||||
QElapsedTimer t;
|
||||
};
|
||||
|
||||
|
|
|
@ -1695,7 +1695,7 @@ void ProfileWidget2::disconnectTemporaryConnections()
|
|||
#ifndef SUBSURFACE_MOBILE
|
||||
void ProfileWidget2::pointInserted(const QModelIndex&, int, int)
|
||||
{
|
||||
DiveHandler *item = new DiveHandler();
|
||||
DiveHandler *item = new DiveHandler(&displayed_dive);
|
||||
scene()->addItem(item);
|
||||
handles << item;
|
||||
|
||||
|
|
|
@ -24,26 +24,9 @@ GasSelectionModel *GasSelectionModel::instance()
|
|||
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()
|
||||
{
|
||||
setStringList(getGasList());
|
||||
setStringList(get_dive_gas_list(&displayed_dive));
|
||||
}
|
||||
|
||||
QVariant GasSelectionModel::data(const QModelIndex &index, int role) const
|
||||
|
|
Loading…
Reference in a new issue