subsurface/stats/boxseries.h
Berthold Stoeger db69c38245 statistics: refactor QSG memory management
The code was wrong, because it deleted the ChartItems in the
main UI thread, not the render thread. This would delete the
QSG nodes in the UI thread and then crash on mobile.

Therefore refactor this part of the code by adding the
items to be deleted to a list that will be deleted by the
render thread.

As a drop in replacement of std::unique_ptr, implement
a silly ChartItemPtr class, which auto-initializes to null.

This turns the deterministic and easily controlled memory
management into a steaming pile of insanity. Obviously,
this can be made much more elegant, but this has to do for now.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2021-01-20 08:47:18 +01:00

56 lines
1.6 KiB
C++

// SPDX-License-Identifier: GPL-2.0
// A small custom box plot series, which displays quartiles.
// The QtCharts bar series were too inflexible with respect
// to placement of the bars.
#ifndef BOX_SERIES_H
#define BOX_SERIES_H
#include "chartitem.h"
#include "statsseries.h"
#include "statsvariables.h" // for StatsQuartiles
#include <memory>
#include <vector>
struct InformationBox;
class BoxSeries : public StatsSeries {
public:
BoxSeries(StatsView &view, StatsAxis *xAxis, StatsAxis *yAxis,
const QString &variable, const QString &unit, int decimals);
~BoxSeries();
void updatePositions() override;
bool hover(QPointF pos) override;
void unhighlight() override;
// Note: this expects that all items are added with increasing pos
// and that no bar is inside another bar, i.e. lowerBound and upperBound
// are ordered identically.
void append(double lowerBound, double upperBound, const StatsQuartiles &q, const QString &binName);
private:
// Get item under mouse pointer, or -1 if none
int getItemUnderMouse(const QPointF &f);
struct Item {
ChartItemPtr<ChartBoxItem> item;
double lowerBound, upperBound;
StatsQuartiles q;
QString binName;
Item(StatsView &view, BoxSeries *series, double lowerBound, double upperBound, const StatsQuartiles &q, const QString &binName);
~Item();
void updatePosition(BoxSeries *series);
void highlight(bool highlight);
};
QString variable, unit;
int decimals;
std::vector<QString> formatInformation(const Item &item) const;
ChartItemPtr<InformationBox> information;
std::vector<std::unique_ptr<Item>> items;
int highlighted; // -1: no item highlighted
};
#endif