Profile: don't copy plot data for tank-bar

The whole plot info data was copied only so that the time of the
last item could be determined later. Instead, simply store the
timestamp.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2019-10-27 18:08:07 +01:00 committed by Dirk Hohndel
parent bc11097ded
commit a7067937b0
2 changed files with 17 additions and 18 deletions

View file

@ -7,9 +7,7 @@
TankItem::TankItem(QObject *parent) : TankItem::TankItem(QObject *parent) :
QObject(parent), QObject(parent),
QGraphicsRectItem(), plotEndTime(-1)
pInfoEntry(0),
pInfoNr(0)
{ {
height = 3; height = 3;
QColor red(PERSIANRED1); QColor red(PERSIANRED1);
@ -35,14 +33,19 @@ TankItem::TankItem(QObject *parent) :
void TankItem::setData(DivePlotDataModel *model, struct plot_info *plotInfo, struct dive *d) void TankItem::setData(DivePlotDataModel *model, struct plot_info *plotInfo, struct dive *d)
{ {
free(pInfoEntry); // If there is nothing to plot, quit early.
// the plotInfo and dive structures passed in could become invalid before we stop using them, if (plotInfo->nr <= 0) {
// so copy the data that we need plotEndTime = -1;
int size = plotInfo->nr * sizeof(plotInfo->entry[0]); return;
pInfoEntry = (struct plot_data *)malloc(size); }
pInfoNr = plotInfo->nr;
memcpy(pInfoEntry, plotInfo->entry, size); // Find correct end of the dive plot for correct end of the tankbar.
struct plot_data *last_entry = &plotInfo->entry[plotInfo->nr - 1];
plotEndTime = last_entry->sec;
// Stay informed of changes to the tanks.
connect(model, SIGNAL(dataChanged(QModelIndex, QModelIndex)), this, SLOT(modelDataChanged(QModelIndex, QModelIndex)), Qt::UniqueConnection); connect(model, SIGNAL(dataChanged(QModelIndex, QModelIndex)), this, SLOT(modelDataChanged(QModelIndex, QModelIndex)), Qt::UniqueConnection);
modelDataChanged(); modelDataChanged();
} }
@ -77,16 +80,13 @@ void TankItem::createBar(int startTime, int stopTime, struct gasmix gas)
void TankItem::modelDataChanged(const QModelIndex&, const QModelIndex&) void TankItem::modelDataChanged(const QModelIndex&, const QModelIndex&)
{ {
// We don't have enougth data to calculate things, quit. // We don't have enougth data to calculate things, quit.
if (!pInfoEntry || !pInfoNr) if (plotEndTime < 0)
return; return;
// remove the old rectangles // remove the old rectangles
qDeleteAll(rects); qDeleteAll(rects);
rects.clear(); rects.clear();
// Find correct end of the dive plot for correct end of the tankbar
struct plot_data *last_entry = &pInfoEntry[pInfoNr-1];
// get the information directly from the displayed_dive (the dc always exists) // get the information directly from the displayed_dive (the dc always exists)
struct divecomputer *dc = get_dive_dc(&displayed_dive, dc_number); struct divecomputer *dc = get_dive_dc(&displayed_dive, dc_number);
@ -97,13 +97,13 @@ void TankItem::modelDataChanged(const QModelIndex&, const QModelIndex&)
// work through all the gas changes and add the rectangle for each gas while it was used // work through all the gas changes and add the rectangle for each gas while it was used
const struct event *ev = get_next_event(dc->events, "gaschange"); const struct event *ev = get_next_event(dc->events, "gaschange");
while (ev && (int)ev->time.seconds < last_entry->sec) { while (ev && (int)ev->time.seconds < plotEndTime) {
createBar(startTime, ev->time.seconds, gasmix); createBar(startTime, ev->time.seconds, gasmix);
startTime = ev->time.seconds; startTime = ev->time.seconds;
gasmix = get_gasmix_from_event(&displayed_dive, ev); gasmix = get_gasmix_from_event(&displayed_dive, ev);
ev = get_next_event(ev->next, "gaschange"); ev = get_next_event(ev->next, "gaschange");
} }
createBar(startTime, last_entry->sec, gasmix); createBar(startTime, plotEndTime, gasmix);
} }
void TankItem::setHorizontalAxis(DiveCartesianAxis *horizontal) void TankItem::setHorizontalAxis(DiveCartesianAxis *horizontal)

View file

@ -26,8 +26,7 @@ public slots:
private: private:
void createBar(int startTime, int stopTime, struct gasmix gas); void createBar(int startTime, int stopTime, struct gasmix gas);
DiveCartesianAxis *hAxis; DiveCartesianAxis *hAxis;
struct plot_data *pInfoEntry; int plotEndTime;
int pInfoNr;
qreal height; qreal height;
QBrush air, nitrox, oxygen, trimix; QBrush air, nitrox, oxygen, trimix;
QList<QGraphicsRectItem *> rects; QList<QGraphicsRectItem *> rects;