profile: use unique_ptr to store DiveTextItems

Pure bike-shedding:
The DiveTextItems of the DiveProfileItems were stored as raw
pointers. Instead, store them as unique_ptrs, so that they
don't have to be explicitly deleted.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2021-12-11 18:01:40 +01:00 committed by Dirk Hohndel
parent cf1ad3d831
commit 4458535265
2 changed files with 19 additions and 19 deletions

View file

@ -19,10 +19,13 @@ AbstractProfilePolygonItem::AbstractProfilePolygonItem(const plot_info &pInfo, c
setCacheMode(DeviceCoordinateCache); setCacheMode(DeviceCoordinateCache);
} }
AbstractProfilePolygonItem::~AbstractProfilePolygonItem()
{
}
void AbstractProfilePolygonItem::clear() void AbstractProfilePolygonItem::clear()
{ {
setPolygon(QPolygonF()); setPolygon(QPolygonF());
qDeleteAll(texts);
texts.clear(); texts.clear();
} }
@ -92,7 +95,6 @@ void AbstractProfilePolygonItem::makePolygon(int fromIn, int toIn)
} }
setPolygon(poly); setPolygon(poly);
qDeleteAll(texts);
texts.clear(); texts.clear();
} }
@ -254,10 +256,10 @@ void DiveProfileItem::replot(const dive *d, int from, int to, bool in_planner)
void DiveProfileItem::plot_depth_sample(const struct plot_data &entry, QFlags<Qt::AlignmentFlag> flags, const QColor &color) void DiveProfileItem::plot_depth_sample(const struct plot_data &entry, QFlags<Qt::AlignmentFlag> flags, const QColor &color)
{ {
DiveTextItem *item = new DiveTextItem(dpr, 1.0, flags, this); auto item = std::make_unique<DiveTextItem>(dpr, 1.0, flags, this);
item->set(get_depth_string(entry.depth, true), color); item->set(get_depth_string(entry.depth, true), color);
item->setPos(hAxis.posAtValue(entry.sec), vAxis.posAtValue(entry.depth)); item->setPos(hAxis.posAtValue(entry.sec), vAxis.posAtValue(entry.depth));
texts.append(item); texts.push_back(std::move(item));
} }
DiveHeartrateItem::DiveHeartrateItem(const plot_info &pInfo, const DiveCartesianAxis &hAxis, DiveHeartrateItem::DiveHeartrateItem(const plot_info &pInfo, const DiveCartesianAxis &hAxis,
@ -283,7 +285,6 @@ void DiveHeartrateItem::replot(const dive *, int fromIn, int toIn, bool)
} hist[3] = {}; } hist[3] = {};
std::vector<sec_hr> textItems; std::vector<sec_hr> textItems;
qDeleteAll(texts);
texts.clear(); texts.clear();
// Ignore empty values. a heart rate of 0 would be a bad sign. // Ignore empty values. a heart rate of 0 would be a bad sign.
QPolygonF poly; QPolygonF poly;
@ -334,10 +335,10 @@ void DiveHeartrateItem::createTextItem(int sec, int hr, bool last)
{ {
int flags = last ? Qt::AlignLeft | Qt::AlignBottom : int flags = last ? Qt::AlignLeft | Qt::AlignBottom :
Qt::AlignRight | Qt::AlignBottom; Qt::AlignRight | Qt::AlignBottom;
DiveTextItem *text = new DiveTextItem(dpr, 0.7, flags, this); auto text = std::make_unique<DiveTextItem>(dpr, 0.7, flags, this);
text->set(QString("%1").arg(hr), getColor(HR_TEXT)); text->set(QString("%1").arg(hr), getColor(HR_TEXT));
text->setPos(QPointF(hAxis.posAtValue(sec), vAxis.posAtValue(hr))); text->setPos(QPointF(hAxis.posAtValue(sec), vAxis.posAtValue(hr)));
texts.append(text); texts.push_back(std::move(text));
} }
void DiveHeartrateItem::paint(QPainter *painter, const QStyleOptionGraphicsItem*, QWidget*) void DiveHeartrateItem::paint(QPainter *painter, const QStyleOptionGraphicsItem*, QWidget*)
@ -369,7 +370,6 @@ void DiveTemperatureItem::replot(const dive *, int fromIn, int toIn, bool)
double last = -300.0, last_printed_temp = 0.0, last_valid_temp = 0.0, sec = 0.0; double last = -300.0, last_printed_temp = 0.0, last_valid_temp = 0.0, sec = 0.0;
std::vector<std::pair<int, int>> textItems; std::vector<std::pair<int, int>> textItems;
qDeleteAll(texts);
texts.clear(); texts.clear();
// Ignore empty values. things do not look good with '0' as temperature in kelvin... // Ignore empty values. things do not look good with '0' as temperature in kelvin...
QPolygonF poly; QPolygonF poly;
@ -418,10 +418,10 @@ void DiveTemperatureItem::createTextItem(int sec, int mkelvin, bool last)
int flags = last ? Qt::AlignLeft | Qt::AlignBottom : int flags = last ? Qt::AlignLeft | Qt::AlignBottom :
Qt::AlignRight | Qt::AlignBottom; Qt::AlignRight | Qt::AlignBottom;
DiveTextItem *text = new DiveTextItem(dpr, 0.8, flags, this); auto text = std::make_unique<DiveTextItem>(dpr, 0.8, flags, this);
text->set(get_temperature_string(temp, true), getColor(TEMP_TEXT)); text->set(get_temperature_string(temp, true), getColor(TEMP_TEXT));
text->setPos(QPointF(hAxis.posAtValue(sec), vAxis.posAtValue(mkelvin))); text->setPos(QPointF(hAxis.posAtValue(sec), vAxis.posAtValue(mkelvin)));
texts.append(text); texts.push_back(std::move(text));
} }
void DiveTemperatureItem::paint(QPainter *painter, const QStyleOptionGraphicsItem*, QWidget*) void DiveTemperatureItem::paint(QPainter *painter, const QStyleOptionGraphicsItem*, QWidget*)
@ -517,12 +517,11 @@ void DiveMeanDepthItem::paint(QPainter *painter, const QStyleOptionGraphicsItem*
void DiveMeanDepthItem::createTextItem(double lastSec, double lastMeanDepth) void DiveMeanDepthItem::createTextItem(double lastSec, double lastMeanDepth)
{ {
qDeleteAll(texts);
texts.clear(); texts.clear();
DiveTextItem *text = new DiveTextItem(dpr, diveMeanDepthItemLabelScale, Qt::AlignRight | Qt::AlignVCenter, this); auto text = std::make_unique<DiveTextItem>(dpr, diveMeanDepthItemLabelScale, Qt::AlignRight | Qt::AlignVCenter, this);
text->set(get_depth_string(lrint(lastMeanDepth), true), getColor(TEMP_TEXT)); text->set(get_depth_string(lrint(lastMeanDepth), true), getColor(TEMP_TEXT));
text->setPos(QPointF(hAxis.posAtValue(lastSec) + dpr, vAxis.posAtValue(lastMeanDepth))); text->setPos(QPointF(hAxis.posAtValue(lastSec) + dpr, vAxis.posAtValue(lastMeanDepth)));
texts.append(text); texts.push_back(std::move(text));
} }
void DiveGasPressureItem::replot(const dive *d, int fromIn, int toIn, bool in_planner) void DiveGasPressureItem::replot(const dive *d, int fromIn, int toIn, bool in_planner)
@ -612,7 +611,6 @@ void DiveGasPressureItem::replot(const dive *d, int fromIn, int toIn, bool in_pl
} }
setPolygon(boundingPoly); setPolygon(boundingPoly);
qDeleteAll(texts);
texts.clear(); texts.clear();
// These are offset values used to print the gas labels and pressures on a // These are offset values used to print the gas labels and pressures on a
@ -650,19 +648,19 @@ void DiveGasPressureItem::plotPressureValue(double mbar, double sec, QFlags<Qt::
{ {
const char *unit; const char *unit;
int pressure = get_pressure_units(lrint(mbar), &unit); int pressure = get_pressure_units(lrint(mbar), &unit);
DiveTextItem *text = new DiveTextItem(dpr, 1.0, align, this); auto text = std::make_unique<DiveTextItem>(dpr, 1.0, align, this);
text->set(QString("%1%2").arg(pressure).arg(unit), getColor(PRESSURE_TEXT)); text->set(QString("%1%2").arg(pressure).arg(unit), getColor(PRESSURE_TEXT));
text->setPos(hAxis.posAtValue(sec), vAxis.posAtValue(mbar) + pressure_offset); text->setPos(hAxis.posAtValue(sec), vAxis.posAtValue(mbar) + pressure_offset);
texts.push_back(text); texts.push_back(std::move(text));
} }
void DiveGasPressureItem::plotGasValue(double mbar, double sec, struct gasmix gasmix, QFlags<Qt::AlignmentFlag> align, double gasname_offset) void DiveGasPressureItem::plotGasValue(double mbar, double sec, struct gasmix gasmix, QFlags<Qt::AlignmentFlag> align, double gasname_offset)
{ {
QString gas = get_gas_string(gasmix); QString gas = get_gas_string(gasmix);
DiveTextItem *text = new DiveTextItem(dpr, 1.0, align, this); auto text = std::make_unique<DiveTextItem>(dpr, 1.0, align, this);
text->set(gas, getColor(PRESSURE_TEXT)); text->set(gas, getColor(PRESSURE_TEXT));
text->setPos(hAxis.posAtValue(sec), vAxis.posAtValue(mbar) + gasname_offset); text->setPos(hAxis.posAtValue(sec), vAxis.posAtValue(mbar) + gasname_offset);
texts.push_back(text); texts.push_back(std::move(text));
} }
void DiveGasPressureItem::paint(QPainter *painter, const QStyleOptionGraphicsItem*, QWidget*) void DiveGasPressureItem::paint(QPainter *painter, const QStyleOptionGraphicsItem*, QWidget*)

View file

@ -3,6 +3,7 @@
#define DIVEPROFILEITEM_H #define DIVEPROFILEITEM_H
#include <QGraphicsPolygonItem> #include <QGraphicsPolygonItem>
#include <memory>
#include "divelineitem.h" #include "divelineitem.h"
@ -26,6 +27,7 @@ public:
using DataAccessor = double (*)(const plot_data &data); // The pointer-to-function syntax is hilarious. using DataAccessor = double (*)(const plot_data &data); // The pointer-to-function syntax is hilarious.
AbstractProfilePolygonItem(const plot_info &pInfo, const DiveCartesianAxis &hAxis, const DiveCartesianAxis &vAxis, AbstractProfilePolygonItem(const plot_info &pInfo, const DiveCartesianAxis &hAxis, const DiveCartesianAxis &vAxis,
DataAccessor accessor, double dpr); DataAccessor accessor, double dpr);
~AbstractProfilePolygonItem();
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0) = 0; virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0) = 0;
void clear(); void clear();
@ -44,7 +46,7 @@ protected:
DataAccessor accessor; DataAccessor accessor;
double dpr; double dpr;
int from, to; int from, to;
QList<DiveTextItem *> texts; std::vector<std::unique_ptr<DiveTextItem>> texts;
}; };
class DiveProfileItem : public AbstractProfilePolygonItem { class DiveProfileItem : public AbstractProfilePolygonItem {