aboutsummaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorGravatar Berthold Stoeger <bstoeger@mail.tuwien.ac.at>2019-08-12 18:26:42 +0200
committerGravatar Dirk Hohndel <dirk@hohndel.org>2019-08-22 10:13:40 -0700
commitf25fa2adc5dd37aca17335d8278fd00e39e9b733 (patch)
tree29e7dd254c9ce065871ef9ebbf44b2615a215028 /core
parent0d045f8c1470e011e0beff0ff0b0ec85975272e6 (diff)
downloadsubsurface-f25fa2adc5dd37aca17335d8278fd00e39e9b733.tar.gz
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>
Diffstat (limited to 'core')
-rw-r--r--core/subsurface-qt/CylinderObjectHelper.cpp44
-rw-r--r--core/subsurface-qt/CylinderObjectHelper.h35
-rw-r--r--core/subsurface-qt/DiveObjectHelper.cpp11
-rw-r--r--core/subsurface-qt/DiveObjectHelper.h8
4 files changed, 34 insertions, 64 deletions
diff --git a/core/subsurface-qt/CylinderObjectHelper.cpp b/core/subsurface-qt/CylinderObjectHelper.cpp
index f2216af6f..f8386c402 100644
--- a/core/subsurface-qt/CylinderObjectHelper.cpp
+++ b/core/subsurface-qt/CylinderObjectHelper.cpp
@@ -3,36 +3,16 @@
#include "../qthelper.h"
static QString EMPTY_CYLINDER_STRING = QStringLiteral("");
-CylinderObjectHelper::CylinderObjectHelper(cylinder_t *cylinder) :
- m_cyl(cylinder) {
-}
-
-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);
+CylinderObjectHelper::CylinderObjectHelper(cylinder_t *cylinder)
+{
+ if (!cylinder)
+ return;
+
+ 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);
}
diff --git a/core/subsurface-qt/CylinderObjectHelper.h b/core/subsurface-qt/CylinderObjectHelper.h
index 8453bac01..f7df7f746 100644
--- a/core/subsurface-qt/CylinderObjectHelper.h
+++ b/core/subsurface-qt/CylinderObjectHelper.h
@@ -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
diff --git a/core/subsurface-qt/DiveObjectHelper.cpp b/core/subsurface-qt/DiveObjectHelper.cpp
index 6d625473e..ec4c8d65f 100644
--- a/core/subsurface-qt/DiveObjectHelper.cpp
+++ b/core/subsurface-qt/DiveObjectHelper.cpp
@@ -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;
}
diff --git a/core/subsurface-qt/DiveObjectHelper.h b/core/subsurface-qt/DiveObjectHelper.h
index 7e3073c00..a090811de 100644
--- a/core/subsurface-qt/DiveObjectHelper.h
+++ b/core/subsurface-qt/DiveObjectHelper.h
@@ -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 *)