2021-01-01 21:23:29 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
#include "boxseries.h"
|
|
|
|
#include "informationbox.h"
|
2021-01-17 20:07:57 +00:00
|
|
|
#include "statsaxis.h"
|
2021-01-01 21:23:29 +00:00
|
|
|
#include "statscolors.h"
|
statistics: convert chart to QQuickItem
It turns out that the wrong base class was used for the chart.
QQuickWidget can only be used on desktop, not in a mobile UI.
Therefore, turn this into a QQuickItem and move the container
QQuickWidget into desktop-only code.
Currently, this code is insane: The chart is rendered onto a
QGraphicsScene (as it was before), which is then rendered into
a QImage, which is transformed into a QSGTexture, which is then
projected onto the device. This is performed on every mouse
move event, since these events in general change the position
of the info-box.
The plan is to slowly convert elements such as the info-box into
QQuickItems. Browsing the QtQuick documentation, this will
not be much fun.
Also note that the rendering currently tears, flickers and has
antialiasing artifacts, most likely owing to integer (QImage)
to floating point (QGraphicsScene, QQuickItem) conversion
problems. The data flow is
QGraphicsScene (float) -> QImage (int) -> QQuickItem (float).
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2021-01-07 13:38:37 +00:00
|
|
|
#include "statshelper.h"
|
2021-01-01 21:23:29 +00:00
|
|
|
#include "statstranslations.h"
|
2021-01-13 14:17:54 +00:00
|
|
|
#include "statsview.h"
|
2021-01-01 21:23:29 +00:00
|
|
|
#include "zvalues.h"
|
|
|
|
|
|
|
|
#include <QLocale>
|
|
|
|
|
|
|
|
// Constants that control the bar layout
|
|
|
|
static const double boxWidth = 0.8; // 1.0 = full width of category
|
2021-01-17 20:07:57 +00:00
|
|
|
static const int boxBorderWidth = 2.0;
|
2021-01-01 21:23:29 +00:00
|
|
|
|
2021-01-18 11:35:22 +00:00
|
|
|
BoxSeries::BoxSeries(StatsView &view, StatsAxis *xAxis, StatsAxis *yAxis,
|
2021-01-01 21:23:29 +00:00
|
|
|
const QString &variable, const QString &unit, int decimals) :
|
2021-01-18 11:35:22 +00:00
|
|
|
StatsSeries(view, xAxis, yAxis),
|
2021-01-01 21:23:29 +00:00
|
|
|
variable(variable), unit(unit), decimals(decimals), highlighted(-1)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
BoxSeries::~BoxSeries()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-01-17 20:07:57 +00:00
|
|
|
BoxSeries::Item::Item(StatsView &view, BoxSeries *series, double lowerBound, double upperBound,
|
2021-01-01 21:23:29 +00:00
|
|
|
const StatsQuartiles &q, const QString &binName) :
|
|
|
|
lowerBound(lowerBound), upperBound(upperBound), q(q),
|
|
|
|
binName(binName)
|
|
|
|
{
|
2021-01-17 20:07:57 +00:00
|
|
|
item = view.createChartItem<ChartBoxItem>(ChartZValue::Series, boxBorderWidth);
|
2021-01-01 21:23:29 +00:00
|
|
|
highlight(false);
|
2021-01-05 11:11:46 +00:00
|
|
|
updatePosition(series);
|
2021-01-01 21:23:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BoxSeries::Item::~Item()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void BoxSeries::Item::highlight(bool highlight)
|
|
|
|
{
|
2021-01-17 20:07:57 +00:00
|
|
|
if (highlight)
|
|
|
|
item->setColor(highlightedColor, highlightedBorderColor);
|
|
|
|
else
|
|
|
|
item->setColor(fillColor, ::borderColor);
|
2021-01-01 21:23:29 +00:00
|
|
|
}
|
|
|
|
|
2021-01-05 11:11:46 +00:00
|
|
|
void BoxSeries::Item::updatePosition(BoxSeries *series)
|
2021-01-01 21:23:29 +00:00
|
|
|
{
|
2021-01-17 20:07:57 +00:00
|
|
|
StatsAxis *xAxis = series->xAxis;
|
|
|
|
StatsAxis *yAxis = series->yAxis;
|
|
|
|
if (!xAxis || !yAxis)
|
|
|
|
return;
|
|
|
|
|
2021-01-01 21:23:29 +00:00
|
|
|
double delta = (upperBound - lowerBound) * boxWidth;
|
|
|
|
double from = (lowerBound + upperBound - delta) / 2.0;
|
|
|
|
double to = (lowerBound + upperBound + delta) / 2.0;
|
|
|
|
|
2021-01-17 20:07:57 +00:00
|
|
|
double fromScreen = xAxis->toScreen(from);
|
|
|
|
double toScreen = xAxis->toScreen(to);
|
|
|
|
double q1 = yAxis->toScreen(q.q1);
|
|
|
|
double q3 = yAxis->toScreen(q.q3);
|
|
|
|
QRectF rect(fromScreen, q3, toScreen - fromScreen, q1 - q3);
|
|
|
|
item->setBox(rect, yAxis->toScreen(q.min), yAxis->toScreen(q.max), yAxis->toScreen(q.q2));
|
2021-01-01 21:23:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BoxSeries::append(double lowerBound, double upperBound, const StatsQuartiles &q, const QString &binName)
|
|
|
|
{
|
2021-01-17 20:07:57 +00:00
|
|
|
items.emplace_back(new Item(view, this, lowerBound, upperBound, q, binName));
|
2021-01-01 21:23:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BoxSeries::updatePositions()
|
|
|
|
{
|
|
|
|
for (auto &item: items)
|
2021-01-05 11:11:46 +00:00
|
|
|
item->updatePosition(this);
|
2021-01-01 21:23:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Attention: this supposes that items are sorted by position and no box is inside another box!
|
|
|
|
int BoxSeries::getItemUnderMouse(const QPointF &point)
|
|
|
|
{
|
|
|
|
// Search the first item whose "end" position is greater than the cursor position.
|
|
|
|
auto it = std::lower_bound(items.begin(), items.end(), point.x(),
|
2021-01-17 20:07:57 +00:00
|
|
|
[] (const std::unique_ptr<Item> &item, double x) { return item->item->getRect().right() < x; });
|
|
|
|
return it != items.end() && (*it)->item->getRect().contains(point) ? it - items.begin() : -1;
|
2021-01-01 21:23:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static QString infoItem(const QString &name, const QString &unit, int decimals, double value)
|
|
|
|
{
|
|
|
|
QLocale loc;
|
|
|
|
QString formattedValue = loc.toString(value, 'f', decimals);
|
|
|
|
return unit.isEmpty() ? QStringLiteral(" %1: %2").arg(name, formattedValue)
|
|
|
|
: QStringLiteral(" %1: %2 %3").arg(name, formattedValue, unit);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<QString> BoxSeries::formatInformation(const Item &item) const
|
|
|
|
{
|
|
|
|
QLocale loc;
|
|
|
|
return {
|
2021-01-10 22:11:50 +00:00
|
|
|
StatsTranslations::tr("%1 (%2 dives)").arg(item.binName, loc.toString(item.q.count)),
|
2021-01-01 21:23:29 +00:00
|
|
|
QStringLiteral("%1:").arg(variable),
|
|
|
|
infoItem(StatsTranslations::tr("min"), unit, decimals, item.q.min),
|
|
|
|
infoItem(StatsTranslations::tr("Q1"), unit, decimals, item.q.q1),
|
2021-01-10 22:11:50 +00:00
|
|
|
infoItem(StatsTranslations::tr("median"), unit, decimals, item.q.q2),
|
2021-01-01 21:23:29 +00:00
|
|
|
infoItem(StatsTranslations::tr("Q3"), unit, decimals, item.q.q3),
|
|
|
|
infoItem(StatsTranslations::tr("max"), unit, decimals, item.q.max)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Highlight item when hovering over item
|
|
|
|
bool BoxSeries::hover(QPointF pos)
|
|
|
|
{
|
|
|
|
int index = getItemUnderMouse(pos);
|
|
|
|
if (index == highlighted) {
|
|
|
|
if (information)
|
|
|
|
information->setPos(pos);
|
|
|
|
return index >= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
unhighlight();
|
|
|
|
highlighted = index;
|
|
|
|
|
|
|
|
// Highlight new item (if any)
|
|
|
|
if (highlighted >= 0 && highlighted < (int)items.size()) {
|
|
|
|
Item &item = *items[highlighted];
|
|
|
|
item.highlight(true);
|
|
|
|
if (!information)
|
2021-01-13 14:17:54 +00:00
|
|
|
information = view.createChartItem<InformationBox>();
|
2021-01-01 21:23:29 +00:00
|
|
|
information->setText(formatInformation(item), pos);
|
2021-01-18 21:29:34 +00:00
|
|
|
information->setVisible(true);
|
2021-01-01 21:23:29 +00:00
|
|
|
} else {
|
2021-01-18 21:29:34 +00:00
|
|
|
information->setVisible(false);
|
2021-01-01 21:23:29 +00:00
|
|
|
}
|
|
|
|
return highlighted >= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BoxSeries::unhighlight()
|
|
|
|
{
|
|
|
|
if (highlighted >= 0 && highlighted < (int)items.size())
|
|
|
|
items[highlighted]->highlight(false);
|
|
|
|
highlighted = -1;
|
|
|
|
}
|