2021-01-01 21:30:30 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
#include "scatterseries.h"
|
2021-01-17 21:03:27 +00:00
|
|
|
#include "chartitem.h"
|
2021-01-01 21:30:30 +00:00
|
|
|
#include "informationbox.h"
|
|
|
|
#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:30:30 +00:00
|
|
|
#include "statstranslations.h"
|
|
|
|
#include "statsvariables.h"
|
2021-01-13 14:17:54 +00:00
|
|
|
#include "statsview.h"
|
2021-01-01 21:30:30 +00:00
|
|
|
#include "zvalues.h"
|
|
|
|
#include "core/dive.h"
|
|
|
|
#include "core/divelist.h"
|
|
|
|
#include "core/qthelper.h"
|
2021-01-17 12:34:18 +00:00
|
|
|
#include "core/selection.h"
|
2021-01-01 21:30:30 +00:00
|
|
|
|
2021-01-18 11:35:22 +00:00
|
|
|
ScatterSeries::ScatterSeries(StatsView &view, StatsAxis *xAxis, StatsAxis *yAxis,
|
2021-01-01 21:30:30 +00:00
|
|
|
const StatsVariable &varX, const StatsVariable &varY) :
|
2021-01-18 11:35:22 +00:00
|
|
|
StatsSeries(view, xAxis, yAxis),
|
2021-01-01 21:30:30 +00:00
|
|
|
varX(varX), varY(varY)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ScatterSeries::~ScatterSeries()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:03:27 +00:00
|
|
|
ScatterSeries::Item::Item(StatsView &view, ScatterSeries *series, dive *d, double pos, double value) :
|
2022-10-19 20:35:18 +00:00
|
|
|
item(view.createChartItem<ChartScatterItem>(ChartZValue::Series, d->selected)),
|
2021-01-01 21:30:30 +00:00
|
|
|
d(d),
|
2021-01-31 19:48:12 +00:00
|
|
|
selected(d->selected),
|
2021-01-01 21:30:30 +00:00
|
|
|
pos(pos),
|
|
|
|
value(value)
|
|
|
|
{
|
2021-01-05 11:11:46 +00:00
|
|
|
updatePosition(series);
|
2021-01-01 21:30:30 +00:00
|
|
|
}
|
|
|
|
|
2021-01-05 11:11:46 +00:00
|
|
|
void ScatterSeries::Item::updatePosition(ScatterSeries *series)
|
2021-01-01 21:30:30 +00:00
|
|
|
{
|
2021-01-17 21:03:27 +00:00
|
|
|
item->setPos(series->toScreen(QPointF(pos, value)));
|
2021-01-01 21:30:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScatterSeries::Item::highlight(bool highlight)
|
|
|
|
{
|
2021-01-31 19:48:12 +00:00
|
|
|
ChartScatterItem::Highlight status = d->selected ?
|
|
|
|
ChartScatterItem::Highlight::Selected : ChartScatterItem::Highlight::Unselected;
|
|
|
|
if (highlight)
|
|
|
|
status = ChartScatterItem::Highlight::Highlighted;
|
|
|
|
item->setHighlight(status);
|
2021-01-01 21:30:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScatterSeries::append(dive *d, double pos, double value)
|
|
|
|
{
|
2021-01-17 21:03:27 +00:00
|
|
|
items.emplace_back(view, this, d, pos, value);
|
2021-01-01 21:30:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScatterSeries::updatePositions()
|
|
|
|
{
|
|
|
|
for (Item &item: items)
|
2021-01-05 11:11:46 +00:00
|
|
|
item.updatePosition(this);
|
2021-01-01 21:30:30 +00:00
|
|
|
}
|
|
|
|
|
2021-01-05 11:11:46 +00:00
|
|
|
std::vector<int> ScatterSeries::getItemsUnderMouse(const QPointF &point) const
|
2021-01-01 21:30:30 +00:00
|
|
|
{
|
|
|
|
std::vector<int> res;
|
|
|
|
double x = point.x();
|
|
|
|
|
2021-01-17 21:03:27 +00:00
|
|
|
auto low = std::lower_bound(items.begin(), items.end(), x,
|
|
|
|
[] (const Item &item, double x) { return item.item->getRect().right() < x; });
|
|
|
|
auto high = std::upper_bound(low, items.end(), x,
|
|
|
|
[] (double x, const Item &item) { return x < item.item->getRect().left(); });
|
2021-01-01 21:30:30 +00:00
|
|
|
// Hopefully that narrows it down enough. For discrete scatter plots, we could also partition
|
|
|
|
// by equal x and do a binary search in these partitions. But that's probably not worth it.
|
|
|
|
res.reserve(high - low);
|
|
|
|
for (auto it = low; it < high; ++it) {
|
2021-01-17 21:03:27 +00:00
|
|
|
if (it->item->contains(point))
|
2021-01-01 21:30:30 +00:00
|
|
|
res.push_back(it - items.begin());
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2021-02-01 22:17:04 +00:00
|
|
|
std::vector<int> ScatterSeries::getItemsInRect(const QRectF &rect) const
|
|
|
|
{
|
|
|
|
std::vector<int> res;
|
|
|
|
|
|
|
|
auto low = std::lower_bound(items.begin(), items.end(), rect.left(),
|
|
|
|
[] (const Item &item, double x) { return item.item->getRect().right() < x; });
|
|
|
|
auto high = std::upper_bound(low, items.end(), rect.right(),
|
|
|
|
[] (double x, const Item &item) { return x < item.item->getRect().left(); });
|
|
|
|
// Hopefully that narrows it down enough. For discrete scatter plots, we could also partition
|
|
|
|
// by equal x and do a binary search in these partitions. But that's probably not worth it.
|
|
|
|
res.reserve(high - low);
|
|
|
|
for (auto it = low; it < high; ++it) {
|
|
|
|
if (it->item->inRect(rect))
|
|
|
|
res.push_back(it - items.begin());
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2021-02-08 16:07:37 +00:00
|
|
|
bool ScatterSeries::selectItemsUnderMouse(const QPointF &point, SelectionModifier modifier)
|
2021-01-17 12:34:18 +00:00
|
|
|
{
|
2021-01-31 20:30:51 +00:00
|
|
|
std::vector<int> indices = getItemsUnderMouse(point);
|
2021-02-10 11:15:14 +00:00
|
|
|
std::vector<struct dive *> divesUnderMouse;
|
2021-01-17 12:34:18 +00:00
|
|
|
|
2021-02-10 11:15:14 +00:00
|
|
|
divesUnderMouse.reserve(indices.size());
|
|
|
|
for(int idx: indices)
|
|
|
|
divesUnderMouse.push_back(items[idx].d);
|
2021-01-17 12:34:18 +00:00
|
|
|
|
2021-02-10 11:15:14 +00:00
|
|
|
processSelection(std::move(divesUnderMouse), modifier);
|
2021-02-01 22:17:04 +00:00
|
|
|
return !indices.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ScatterSeries::supportsLassoSelection() const
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-02-08 16:07:37 +00:00
|
|
|
void ScatterSeries::selectItemsInRect(const QRectF &rect, SelectionModifier modifier, const std::vector<dive *> &oldSelection)
|
2021-02-01 22:17:04 +00:00
|
|
|
{
|
|
|
|
std::vector<struct dive *> selected;
|
|
|
|
std::vector<int> indices = getItemsInRect(rect);
|
|
|
|
selected.reserve(oldSelection.size() + indices.size());
|
|
|
|
|
2021-02-08 16:07:37 +00:00
|
|
|
if (modifier.ctrl) {
|
2021-02-01 22:17:04 +00:00
|
|
|
selected = oldSelection;
|
|
|
|
// Ouch - this primitive merging of the selections grows with O(n^2). Fix this.
|
|
|
|
for (int idx: indices) {
|
|
|
|
if (std::find(selected.begin(), selected.end(), items[idx].d) == selected.end())
|
|
|
|
selected.push_back(items[idx].d);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (int idx: indices)
|
|
|
|
selected.push_back(items[idx].d);
|
|
|
|
}
|
|
|
|
|
2022-04-04 16:57:28 +00:00
|
|
|
setSelection(selected, selected.empty() ? nullptr : selected.front(), -1);
|
2021-01-17 12:34:18 +00:00
|
|
|
}
|
|
|
|
|
2021-01-01 21:30:30 +00:00
|
|
|
static QString dataInfo(const StatsVariable &var, const dive *d)
|
|
|
|
{
|
|
|
|
// For "numeric" variables, we display value and unit.
|
|
|
|
// For "discrete" variables, we display all categories the dive belongs to.
|
|
|
|
// There is only one "continuous" variable, the date, for which we don't display anything,
|
|
|
|
// because the date is displayed anyway.
|
|
|
|
QString val;
|
|
|
|
switch (var.type()) {
|
|
|
|
case StatsVariable::Type::Numeric:
|
|
|
|
val = var.valueWithUnit(d);
|
|
|
|
break;
|
|
|
|
case StatsVariable::Type::Discrete:
|
|
|
|
val = var.diveCategories(d);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
return QString("%1: %2").arg(var.name(), val);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Highlight item when hovering over item
|
|
|
|
bool ScatterSeries::hover(QPointF pos)
|
|
|
|
{
|
|
|
|
std::vector<int> newHighlighted = getItemsUnderMouse(pos);
|
|
|
|
|
|
|
|
if (newHighlighted == highlighted) {
|
|
|
|
if (information)
|
|
|
|
information->setPos(pos);
|
|
|
|
return !newHighlighted.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
// This might be overkill: differential unhighlighting / highlighting of items.
|
|
|
|
for (int idx: highlighted) {
|
|
|
|
if (std::find(newHighlighted.begin(), newHighlighted.end(), idx) == newHighlighted.end())
|
|
|
|
items[idx].highlight(false);
|
|
|
|
}
|
|
|
|
for (int idx: newHighlighted) {
|
|
|
|
if (std::find(highlighted.begin(), highlighted.end(), idx) == highlighted.end())
|
|
|
|
items[idx].highlight(true);
|
|
|
|
}
|
|
|
|
highlighted = std::move(newHighlighted);
|
|
|
|
|
|
|
|
if (highlighted.empty()) {
|
2021-01-18 21:29:34 +00:00
|
|
|
information->setVisible(false);
|
2021-01-01 21:30:30 +00:00
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
if (!information)
|
2021-01-13 14:17:54 +00:00
|
|
|
information = view.createChartItem<InformationBox>();
|
2021-01-01 21:30:30 +00:00
|
|
|
|
|
|
|
std::vector<QString> text;
|
|
|
|
text.reserve(highlighted.size() * 5);
|
|
|
|
int show = information->recommendedMaxLines() / 5;
|
|
|
|
int shown = 0;
|
|
|
|
for (int idx: highlighted) {
|
|
|
|
const dive *d = items[idx].d;
|
|
|
|
if (!text.empty())
|
|
|
|
text.push_back(QString(" ")); // Argh. Empty strings are filtered away.
|
2021-01-26 20:57:41 +00:00
|
|
|
text.push_back(StatsTranslations::tr("Dive #%1").arg(d->number));
|
|
|
|
text.push_back(get_dive_date_string(d->when));
|
|
|
|
text.push_back(dataInfo(varX, d));
|
|
|
|
text.push_back(dataInfo(varY, d));
|
2021-01-01 21:30:30 +00:00
|
|
|
if (++shown >= show && shown < (int)highlighted.size()) {
|
|
|
|
text.push_back(" ");
|
|
|
|
text.push_back(StatsTranslations::tr("and %1 more").arg((int)highlighted.size() - shown));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
information->setText(text, pos);
|
2021-01-18 21:29:34 +00:00
|
|
|
information->setVisible(true);
|
2021-01-01 21:30:30 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScatterSeries::unhighlight()
|
|
|
|
{
|
|
|
|
for (int idx: highlighted)
|
|
|
|
items[idx].highlight(false);
|
|
|
|
highlighted.clear();
|
|
|
|
}
|
2021-01-31 19:48:12 +00:00
|
|
|
|
|
|
|
void ScatterSeries::divesSelected(const QVector<dive *> &)
|
|
|
|
{
|
|
|
|
for (Item &item: items) {
|
|
|
|
if (item.selected != item.d->selected) {
|
|
|
|
item.selected = item.d->selected;
|
|
|
|
int idx = &item - &items[0];
|
|
|
|
bool highlight = std::find(highlighted.begin(), highlighted.end(), idx) != highlighted.end();
|
|
|
|
item.highlight(highlight);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|