Cleanup: turn CylinderObjectHelper into value type

CylinderObjectHelper is used for structured formatting of cylinder
values in grantlee types. Instead of keeping a reference to a
cylinder, turn it into a value type containing the formatted strings.

This should be distinctly safer, as we don't risk having stale
references flying around. Moreover, we don't have to use pointers
but can use containers containing plain CylinderObjectHelper. Thus,
no explicit memory management is needed, making the code distinctly
easier to understand.

Sadly, currently grantlee does not support Q_GADGET based Q_PROPERTY.
Therefore a GRANTLEE_*_LOOKUP block has to be added. This can be
removed in due course, as a patch to remedy this issue is in current
grantlee master.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2019-08-12 18:26:42 +02:00 committed by Dirk Hohndel
parent 0d045f8c14
commit f25fa2adc5
6 changed files with 54 additions and 63 deletions

View file

@ -3,36 +3,16 @@
#include "../qthelper.h"
static QString EMPTY_CYLINDER_STRING = QStringLiteral("");
CylinderObjectHelper::CylinderObjectHelper(cylinder_t *cylinder) :
m_cyl(cylinder) {
}
CylinderObjectHelper::CylinderObjectHelper(cylinder_t *cylinder)
{
if (!cylinder)
return;
CylinderObjectHelper::~CylinderObjectHelper() {
}
QString CylinderObjectHelper::description() const {
if (!m_cyl->type.description)
return QString(EMPTY_CYLINDER_STRING);
else
return QString(m_cyl->type.description);
}
QString CylinderObjectHelper::size() const {
return get_volume_string(m_cyl->type.size, true);
}
QString CylinderObjectHelper::workingPressure() const {
return get_pressure_string(m_cyl->type.workingpressure, true);
}
QString CylinderObjectHelper::startPressure() const {
return get_pressure_string(m_cyl->start, true);
}
QString CylinderObjectHelper::endPressure() const {
return get_pressure_string(m_cyl->end, true);
}
QString CylinderObjectHelper::gasMix() const {
return get_gas_string(m_cyl->gasmix);
description = cylinder->type.description ? cylinder->type.description:
EMPTY_CYLINDER_STRING;
size = get_volume_string(cylinder->type.size, true);
workingPressure = get_pressure_string(cylinder->type.workingpressure, true);
startPressure = get_pressure_string(cylinder->start, true);
endPressure = get_pressure_string(cylinder->end, true);
gasMix = get_gas_string(cylinder->gasmix);
}

View file

@ -6,27 +6,24 @@
#include <QObject>
#include <QString>
class CylinderObjectHelper : public QObject {
Q_OBJECT
Q_PROPERTY(QString description READ description CONSTANT)
Q_PROPERTY(QString size READ size CONSTANT)
Q_PROPERTY(QString workingPressure READ workingPressure CONSTANT)
Q_PROPERTY(QString startPressure READ startPressure CONSTANT)
Q_PROPERTY(QString endPressure READ endPressure CONSTANT)
Q_PROPERTY(QString gasMix READ gasMix CONSTANT)
class CylinderObjectHelper {
Q_GADGET
Q_PROPERTY(QString description MEMBER description CONSTANT)
Q_PROPERTY(QString size MEMBER size CONSTANT)
Q_PROPERTY(QString workingPressure MEMBER workingPressure CONSTANT)
Q_PROPERTY(QString startPressure MEMBER startPressure CONSTANT)
Q_PROPERTY(QString endPressure MEMBER endPressure CONSTANT)
Q_PROPERTY(QString gasMix MEMBER gasMix CONSTANT)
public:
CylinderObjectHelper(cylinder_t *cylinder = NULL);
~CylinderObjectHelper();
QString description() const;
QString size() const;
QString workingPressure() const;
QString startPressure() const;
QString endPressure() const;
QString gasMix() const;
private:
cylinder_t *m_cyl;
QString description;
QString size;
QString workingPressure;
QString startPressure;
QString endPressure;
QString gasMix;
};
Q_DECLARE_METATYPE(CylinderObjectHelper *)
Q_DECLARE_METATYPE(CylinderObjectHelper)
#endif

View file

@ -60,20 +60,13 @@ DiveObjectHelper::DiveObjectHelper(struct dive *d) :
{
if (!m_dive)
qWarning("Creating DiveObjectHelper from NULL dive");
m_cyls.clear();
for (int i = 0; i < MAX_CYLINDERS; i++) {
//Don't add blank cylinders, only those that have been defined.
if (m_dive->cylinder[i].type.description)
m_cyls.append(new CylinderObjectHelper(&m_dive->cylinder[i]));
m_cyls.append(CylinderObjectHelper(&m_dive->cylinder[i]));
}
}
DiveObjectHelper::~DiveObjectHelper()
{
while (!m_cyls.isEmpty())
delete m_cyls.takeFirst();
}
int DiveObjectHelper::number() const
{
return m_dive->number;
@ -316,7 +309,7 @@ QString DiveObjectHelper::cylinder(int idx) const
return getFormattedCylinder(m_dive, idx);
}
QList<CylinderObjectHelper*> DiveObjectHelper::cylinderObjects() const
QVector<CylinderObjectHelper> DiveObjectHelper::cylinderObjects() const
{
return m_cyls;
}

View file

@ -6,6 +6,7 @@
#include <QObject>
#include <QString>
#include <QStringList>
#include <QVector>
#include <QVariant>
class DiveObjectHelper : public QObject {
@ -38,7 +39,7 @@ class DiveObjectHelper : public QObject {
Q_PROPERTY(QString suit READ suit CONSTANT)
Q_PROPERTY(QStringList cylinderList READ cylinderList CONSTANT)
Q_PROPERTY(QStringList cylinders READ cylinders CONSTANT)
Q_PROPERTY(QList<CylinderObjectHelper*> cylinderObjects READ cylinderObjects CONSTANT)
Q_PROPERTY(QVector<CylinderObjectHelper> cylinderObjects READ cylinderObjects CONSTANT)
Q_PROPERTY(QString tripId READ tripId CONSTANT)
Q_PROPERTY(int tripNrDives READ tripNrDives CONSTANT)
Q_PROPERTY(int maxcns READ maxcns CONSTANT)
@ -52,7 +53,6 @@ class DiveObjectHelper : public QObject {
Q_PROPERTY(QString fullTextNoNotes READ fullTextNoNotes CONSTANT)
public:
DiveObjectHelper(struct dive *dive);
~DiveObjectHelper();
int number() const;
int id() const;
struct dive *getDive() const;
@ -84,7 +84,7 @@ public:
QStringList cylinderList() const;
QStringList cylinders() const;
QString cylinder(int idx) const;
QList<CylinderObjectHelper*> cylinderObjects() const;
QVector<CylinderObjectHelper> cylinderObjects() const;
QString tripId() const;
int tripNrDives() const;
int maxcns() const;
@ -99,7 +99,7 @@ public:
private:
struct dive *m_dive;
QList<CylinderObjectHelper*> m_cyls;
QVector<CylinderObjectHelper> m_cyls;
};
Q_DECLARE_METATYPE(DiveObjectHelper *)

View file

@ -133,6 +133,7 @@ QString TemplateLayout::generate()
Grantlee::Engine engine(this);
Grantlee::registerMetaType<template_options>();
Grantlee::registerMetaType<print_options>();
Grantlee::registerMetaType<CylinderObjectHelper>(); // TODO: Remove when grantlee supports Q_GADGET
// Note: Currently, this should not be transformed into a QVector<> or std::vector<>,
// as diveList contains pointers to elements in this list. But vectors might relocate
@ -196,6 +197,7 @@ QString TemplateLayout::generateStatistics()
Grantlee::registerMetaType<YearInfo>();
Grantlee::registerMetaType<template_options>();
Grantlee::registerMetaType<print_options>();
Grantlee::registerMetaType<CylinderObjectHelper>(); // TODO: Remove when grantlee supports Q_GADGET
QVariantList years;

View file

@ -9,6 +9,7 @@
#include "core/statistics.h"
#include "core/qthelper.h"
#include "core/subsurface-qt/DiveObjectHelper.h"
#include "core/subsurface-qt/CylinderObjectHelper.h" // TODO: remove once grantlee supports Q_GADGET objects
int getTotalWork(print_options *printOptions);
void find_all_templates();
@ -120,4 +121,22 @@ if (property == "year") {
}
GRANTLEE_END_LOOKUP
// TODO: This is currently needed because our grantlee version
// doesn't support Q_GADGET based classes. A patch to fix this
// exists. Remove in due course.
GRANTLEE_BEGIN_LOOKUP(CylinderObjectHelper)
if (property == "description") {
return object.description;
} else if (property == "size") {
return object.size;
} else if (property == "workingPressure") {
return object.workingPressure;
} else if (property == "startPressure") {
return object.startPressure;
} else if (property == "endPressure") {
return object.endPressure;
} else if (property == "gasMix") {
return object.gasMix;
}
GRANTLEE_END_LOOKUP
#endif