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

View file

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

View file

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

View file

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

View file

@ -133,6 +133,7 @@ QString TemplateLayout::generate()
Grantlee::Engine engine(this); Grantlee::Engine engine(this);
Grantlee::registerMetaType<template_options>(); Grantlee::registerMetaType<template_options>();
Grantlee::registerMetaType<print_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<>, // 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 // 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<YearInfo>();
Grantlee::registerMetaType<template_options>(); Grantlee::registerMetaType<template_options>();
Grantlee::registerMetaType<print_options>(); Grantlee::registerMetaType<print_options>();
Grantlee::registerMetaType<CylinderObjectHelper>(); // TODO: Remove when grantlee supports Q_GADGET
QVariantList years; QVariantList years;

View file

@ -9,6 +9,7 @@
#include "core/statistics.h" #include "core/statistics.h"
#include "core/qthelper.h" #include "core/qthelper.h"
#include "core/subsurface-qt/DiveObjectHelper.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); int getTotalWork(print_options *printOptions);
void find_all_templates(); void find_all_templates();
@ -120,4 +121,22 @@ if (property == "year") {
} }
GRANTLEE_END_LOOKUP 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 #endif