mirror of
https://github.com/subsurface/subsurface.git
synced 2025-02-19 22:16:15 +00:00
statistics: turn axes into QSGNode
Render the labels and the title into a pixmap and render the ticks and the base line using individual QSGNodes. Attempting to render the ticks likewise into the pixmap gave horrible results, because (quite obviously) rendering with QPainter and the QSG shader gives non-matching ticks and grid lines. The memory management had to be changed a bit: The ChartItems were collected in the root QSGNode. However, the axes are added before the first plotting, so this node might not exist. Therefore, store the axes in the StatsView object. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
parent
9b7565e81a
commit
ada5e8a49d
4 changed files with 143 additions and 117 deletions
|
@ -4,6 +4,7 @@
|
||||||
#include "statshelper.h"
|
#include "statshelper.h"
|
||||||
#include "statstranslations.h"
|
#include "statstranslations.h"
|
||||||
#include "statsvariables.h"
|
#include "statsvariables.h"
|
||||||
|
#include "statsview.h"
|
||||||
#include "zvalues.h"
|
#include "zvalues.h"
|
||||||
#include "core/pref.h"
|
#include "core/pref.h"
|
||||||
#include "core/subsurface-time.h"
|
#include "core/subsurface-time.h"
|
||||||
|
@ -23,23 +24,19 @@ static const double axisLabelSpaceVertical = 2.0; // Space between axis or ticks
|
||||||
static const double axisTitleSpaceHorizontal = 2.0; // Space between labels and title
|
static const double axisTitleSpaceHorizontal = 2.0; // Space between labels and title
|
||||||
static const double axisTitleSpaceVertical = 2.0; // Space between labels and title
|
static const double axisTitleSpaceVertical = 2.0; // Space between labels and title
|
||||||
|
|
||||||
StatsAxis::StatsAxis(const QString &titleIn, bool horizontal, bool labelsBetweenTicks) :
|
StatsAxis::StatsAxis(StatsView &view, const QString &title, bool horizontal, bool labelsBetweenTicks) :
|
||||||
horizontal(horizontal), labelsBetweenTicks(labelsBetweenTicks),
|
ChartPixmapItem(view, ChartZValue::Axes),
|
||||||
|
line(view.createChartItem<ChartLineItem>(ChartZValue::Axes, axisColor, axisWidth)),
|
||||||
|
title(title), horizontal(horizontal), labelsBetweenTicks(labelsBetweenTicks),
|
||||||
size(1.0), zeroOnScreen(0.0), min(0.0), max(1.0), labelWidth(0.0)
|
size(1.0), zeroOnScreen(0.0), min(0.0), max(1.0), labelWidth(0.0)
|
||||||
{
|
{
|
||||||
// use a Light version of the application fond for both labels and title
|
// use a Light version of the application font for both labels and title
|
||||||
labelFont = QFont();
|
labelFont = QFont();
|
||||||
labelFont.setWeight(QFont::Light);
|
labelFont.setWeight(QFont::Light);
|
||||||
titleFont = labelFont;
|
titleFont = labelFont;
|
||||||
setPen(QPen(axisColor, axisWidth));
|
QFontMetrics fm(titleFont);
|
||||||
setZValue(ZValues::axes);
|
titleWidth = title.isEmpty() ? 0.0
|
||||||
if (!titleIn.isEmpty()) {
|
: static_cast<double>(fm.size(Qt::TextSingleLine, title).width());
|
||||||
title.reset(new QGraphicsSimpleTextItem(titleIn, this));
|
|
||||||
title->setFont(titleFont);
|
|
||||||
title->setBrush(darkLabelColor);
|
|
||||||
if (!horizontal)
|
|
||||||
title->setRotation(-90.0);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
StatsAxis::~StatsAxis()
|
StatsAxis::~StatsAxis()
|
||||||
|
@ -101,7 +98,7 @@ int StatsAxis::guessNumTicks(const std::vector<QString> &strings) const
|
||||||
|
|
||||||
double StatsAxis::titleSpace() const
|
double StatsAxis::titleSpace() const
|
||||||
{
|
{
|
||||||
if (!title)
|
if (title.isEmpty())
|
||||||
return 0.0;
|
return 0.0;
|
||||||
return horizontal ? QFontMetrics(titleFont).height() + axisTitleSpaceHorizontal
|
return horizontal ? QFontMetrics(titleFont).height() + axisTitleSpaceHorizontal
|
||||||
: QFontMetrics(titleFont).height() + axisTitleSpaceVertical;
|
: QFontMetrics(titleFont).height() + axisTitleSpaceVertical;
|
||||||
|
@ -124,31 +121,14 @@ double StatsAxis::height() const
|
||||||
(labelsBetweenTicks ? 0.0 : axisTickSizeHorizontal);
|
(labelsBetweenTicks ? 0.0 : axisTickSizeHorizontal);
|
||||||
}
|
}
|
||||||
|
|
||||||
StatsAxis::Label::Label(const QString &name, double pos, QGraphicsScene *scene, const QFont &font) :
|
void StatsAxis::addLabel(const QFontMetrics &fm, const QString &label, double pos)
|
||||||
label(createItem<QGraphicsSimpleTextItem>(scene, name)),
|
|
||||||
pos(pos)
|
|
||||||
{
|
{
|
||||||
label->setBrush(QBrush(darkLabelColor));
|
labels.push_back({ label, fm.size(Qt::TextSingleLine, label).width(), pos });
|
||||||
label->setFont(font);
|
|
||||||
label->setZValue(ZValues::axes);
|
|
||||||
}
|
|
||||||
|
|
||||||
void StatsAxis::addLabel(const QString &label, double pos)
|
|
||||||
{
|
|
||||||
labels.emplace_back(label, pos, scene(), labelFont);
|
|
||||||
}
|
|
||||||
|
|
||||||
StatsAxis::Tick::Tick(double pos, QGraphicsScene *scene) :
|
|
||||||
item(createItemPtr<QGraphicsLineItem>(scene)),
|
|
||||||
pos(pos)
|
|
||||||
{
|
|
||||||
item->setPen(QPen(axisColor, axisTickWidth));
|
|
||||||
item->setZValue(ZValues::axes);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void StatsAxis::addTick(double pos)
|
void StatsAxis::addTick(double pos)
|
||||||
{
|
{
|
||||||
ticks.emplace_back(pos, scene());
|
ticks.push_back({ view.createChartItem<ChartLineItem>(ChartZValue::Axes, axisColor, axisTickWidth), pos });
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<double> StatsAxis::ticksPositions() const
|
std::vector<double> StatsAxis::ticksPositions() const
|
||||||
|
@ -178,60 +158,105 @@ double StatsAxis::toValue(double pos) const
|
||||||
void StatsAxis::setSize(double sizeIn)
|
void StatsAxis::setSize(double sizeIn)
|
||||||
{
|
{
|
||||||
size = sizeIn;
|
size = sizeIn;
|
||||||
|
|
||||||
|
labels.clear();
|
||||||
|
ticks.clear();
|
||||||
updateLabels();
|
updateLabels();
|
||||||
|
|
||||||
labelWidth = 0.0;
|
labelWidth = 0.0;
|
||||||
for (const Label &label: labels) {
|
for (const Label &label: labels) {
|
||||||
double w = label.label->boundingRect().width();
|
if (label.width > labelWidth)
|
||||||
if (w > labelWidth)
|
labelWidth = label.width;
|
||||||
labelWidth = w;
|
}
|
||||||
|
|
||||||
|
QFontMetrics fm(labelFont);
|
||||||
|
int fontHeight = fm.height();
|
||||||
|
if (horizontal) {
|
||||||
|
double pixmapWidth = size;
|
||||||
|
double offsetX = 0.0;
|
||||||
|
if (!labels.empty() && !labelsBetweenTicks) {
|
||||||
|
pixmapWidth += labels.front().width / 2.0 + labels.back().width / 2.0;
|
||||||
|
offsetX += labels.front().width / 2.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
double pixmapHeight = fontHeight + titleSpace();
|
||||||
|
double offsetY = -axisWidth / 2.0 - axisLabelSpaceHorizontal -
|
||||||
|
(labelsBetweenTicks ? 0.0 : axisTickSizeHorizontal);
|
||||||
|
|
||||||
|
ChartPixmapItem::resize(QSizeF(pixmapWidth, pixmapHeight)); // Note: this rounds the dimensions up
|
||||||
|
offset = QPointF(round(offsetX), round(offsetY));
|
||||||
|
img->fill(Qt::transparent);
|
||||||
|
|
||||||
|
painter->setPen(QPen(darkLabelColor));
|
||||||
|
painter->setFont(labelFont);
|
||||||
|
for (const Label &label: labels) {
|
||||||
|
double x = (label.pos - min) / (max - min) * size + offset.x() - round(label.width / 2.0);
|
||||||
|
QRectF rect(x, 0.0, label.width, fontHeight);
|
||||||
|
painter->drawText(rect, label.label);
|
||||||
|
}
|
||||||
|
if (!title.isEmpty()) {
|
||||||
|
QRectF rect(offset.x() + round((size - titleWidth) / 2.0),
|
||||||
|
fontHeight + axisTitleSpaceHorizontal,
|
||||||
|
titleWidth, fontHeight);
|
||||||
|
painter->setFont(titleFont);
|
||||||
|
painter->drawText(rect, title);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
double pixmapWidth = labelWidth + titleSpace();
|
||||||
|
double offsetX = pixmapWidth + axisLabelSpaceVertical + (labelsBetweenTicks ? 0.0 : axisTickSizeVertical);
|
||||||
|
|
||||||
|
double pixmapHeight = ceil(size + axisTickWidth);
|
||||||
|
double offsetY = size;
|
||||||
|
if (!labels.empty() && !labelsBetweenTicks) {
|
||||||
|
pixmapHeight += fontHeight;
|
||||||
|
offsetY += fontHeight / 2.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
ChartPixmapItem::resize(QSizeF(pixmapWidth, pixmapHeight)); // Note: this rounds the dimensions up
|
||||||
|
offset = QPointF(round(offsetX), round(offsetY));
|
||||||
|
img->fill(Qt::transparent);
|
||||||
|
|
||||||
|
painter->setPen(QPen(darkLabelColor));
|
||||||
|
painter->setFont(labelFont);
|
||||||
|
for (const Label &label: labels) {
|
||||||
|
double y = (min - label.pos) / (max - min) * size + offset.y() - round(fontHeight / 2.0);
|
||||||
|
QRectF rect(pixmapWidth - label.width, y, label.width, fontHeight);
|
||||||
|
painter->drawText(rect, label.label);
|
||||||
|
}
|
||||||
|
if (!title.isEmpty()) {
|
||||||
|
painter->rotate(-90.0);
|
||||||
|
QRectF rect(round(-(offsetY + titleWidth) / 2.0), 0.0, titleWidth, fontHeight);
|
||||||
|
painter->setFont(titleFont);
|
||||||
|
painter->drawText(rect, title);
|
||||||
|
painter->resetTransform();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void StatsAxis::setPos(QPointF pos)
|
void StatsAxis::setPos(QPointF pos)
|
||||||
{
|
{
|
||||||
|
zeroOnScreen = horizontal ? pos.x() : pos.y();
|
||||||
|
ChartPixmapItem::setPos(pos - offset);
|
||||||
|
|
||||||
if (horizontal) {
|
if (horizontal) {
|
||||||
zeroOnScreen = pos.x();
|
|
||||||
double labelY = pos.y() + axisLabelSpaceHorizontal +
|
|
||||||
(labelsBetweenTicks ? 0.0 : axisTickSizeHorizontal);
|
|
||||||
double y = pos.y();
|
double y = pos.y();
|
||||||
for (Label &label: labels) {
|
for (const Tick &tick: ticks) {
|
||||||
double x = toScreen(label.pos) - label.label->boundingRect().width() / 2.0;
|
|
||||||
label.label->setPos(QPointF(x, labelY));
|
|
||||||
}
|
|
||||||
for (Tick &tick: ticks) {
|
|
||||||
double x = toScreen(tick.pos);
|
double x = toScreen(tick.pos);
|
||||||
tick.item->setLine(x, y, x, y + axisTickSizeHorizontal);
|
tick.item->setLine(QPointF(x, y), QPointF(x, y + axisTickSizeHorizontal));
|
||||||
}
|
}
|
||||||
setLine(zeroOnScreen, y, zeroOnScreen + size, y);
|
line->setLine(QPointF(zeroOnScreen, y), QPointF(zeroOnScreen + size, y));
|
||||||
if (title)
|
|
||||||
title->setPos(zeroOnScreen + (size - title->boundingRect().width()) / 2.0,
|
|
||||||
labelY + QFontMetrics(labelFont).height() + axisTitleSpaceHorizontal);
|
|
||||||
} else {
|
} else {
|
||||||
double fontHeight = QFontMetrics(labelFont).height();
|
|
||||||
zeroOnScreen = pos.y();
|
|
||||||
double x = pos.x();
|
double x = pos.x();
|
||||||
double labelX = x - axisLabelSpaceVertical -
|
for (const Tick &tick: ticks) {
|
||||||
(labelsBetweenTicks ? 0.0 : axisTickSizeVertical);
|
|
||||||
for (Label &label: labels) {
|
|
||||||
double y = toScreen(label.pos) - fontHeight / 2.0;
|
|
||||||
label.label->setPos(QPointF(labelX - label.label->boundingRect().width(), y));
|
|
||||||
}
|
|
||||||
for (Tick &tick: ticks) {
|
|
||||||
double y = toScreen(tick.pos);
|
double y = toScreen(tick.pos);
|
||||||
tick.item->setLine(x, y, x - axisTickSizeVertical, y);
|
tick.item->setLine(QPointF(x, y), QPointF(x - axisTickSizeVertical, y));
|
||||||
}
|
}
|
||||||
// This is very confusing: even though we need the height of the title, the correct
|
line->setLine(QPointF(x, zeroOnScreen), QPointF(x, zeroOnScreen - size));
|
||||||
// size is stored in boundingRect().width(). Presumably because the item is rotated
|
|
||||||
// by -90°. Apparently, the boundingRect is in item-local coordinates?
|
|
||||||
if (title)
|
|
||||||
title->setPos(labelX - labelWidth - QFontMetrics(labelFont).height() - axisTitleSpaceVertical,
|
|
||||||
zeroOnScreen - (size - title->boundingRect().width()) / 2.0);
|
|
||||||
setLine(x, zeroOnScreen, x, zeroOnScreen - size);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ValueAxis::ValueAxis(const QString &title, double min, double max, int decimals, bool horizontal) :
|
ValueAxis::ValueAxis(StatsView &view, const QString &title, double min, double max, int decimals, bool horizontal) :
|
||||||
StatsAxis(title, horizontal, false),
|
StatsAxis(view, title, horizontal, false),
|
||||||
min(min), max(max), decimals(decimals)
|
min(min), max(max), decimals(decimals)
|
||||||
{
|
{
|
||||||
// Avoid degenerate cases
|
// Avoid degenerate cases
|
||||||
|
@ -251,9 +276,6 @@ std::pair<QString, QString> ValueAxis::getFirstLastLabel() const
|
||||||
|
|
||||||
void ValueAxis::updateLabels()
|
void ValueAxis::updateLabels()
|
||||||
{
|
{
|
||||||
labels.clear();
|
|
||||||
ticks.clear();
|
|
||||||
|
|
||||||
QLocale loc;
|
QLocale loc;
|
||||||
auto [minString, maxString] = getFirstLastLabel();
|
auto [minString, maxString] = getFirstLastLabel();
|
||||||
int numTicks = guessNumTicks({ minString, maxString});
|
int numTicks = guessNumTicks({ minString, maxString});
|
||||||
|
@ -283,15 +305,16 @@ void ValueAxis::updateLabels()
|
||||||
double act = actMin;
|
double act = actMin;
|
||||||
labels.reserve(num + 1);
|
labels.reserve(num + 1);
|
||||||
ticks.reserve(num + 1);
|
ticks.reserve(num + 1);
|
||||||
|
QFontMetrics fm(labelFont);
|
||||||
for (int i = 0; i <= num; ++i) {
|
for (int i = 0; i <= num; ++i) {
|
||||||
addLabel(loc.toString(act, 'f', decimals), act);
|
addLabel(fm, loc.toString(act, 'f', decimals), act);
|
||||||
addTick(act);
|
addTick(act);
|
||||||
act += actStep;
|
act += actStep;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
CountAxis::CountAxis(const QString &title, int count, bool horizontal) :
|
CountAxis::CountAxis(StatsView &view, const QString &title, int count, bool horizontal) :
|
||||||
ValueAxis(title, 0.0, (double)count, 0, horizontal),
|
ValueAxis(view, title, 0.0, (double)count, 0, horizontal),
|
||||||
count(count)
|
count(count)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
@ -306,9 +329,6 @@ std::pair<QString, QString> CountAxis::getFirstLastLabel() const
|
||||||
|
|
||||||
void CountAxis::updateLabels()
|
void CountAxis::updateLabels()
|
||||||
{
|
{
|
||||||
labels.clear();
|
|
||||||
ticks.clear();
|
|
||||||
|
|
||||||
QLocale loc;
|
QLocale loc;
|
||||||
QString countString = loc.toString(count);
|
QString countString = loc.toString(count);
|
||||||
int numTicks = guessNumTicks({ countString });
|
int numTicks = guessNumTicks({ countString });
|
||||||
|
@ -345,14 +365,15 @@ void CountAxis::updateLabels()
|
||||||
|
|
||||||
labels.reserve(max + 1);
|
labels.reserve(max + 1);
|
||||||
ticks.reserve(max + 1);
|
ticks.reserve(max + 1);
|
||||||
|
QFontMetrics fm(labelFont);
|
||||||
for (int i = 0; i <= max; i += step) {
|
for (int i = 0; i <= max; i += step) {
|
||||||
addLabel(loc.toString(i), static_cast<double>(i));
|
addLabel(fm, loc.toString(i), static_cast<double>(i));
|
||||||
addTick(static_cast<double>(i));
|
addTick(static_cast<double>(i));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
CategoryAxis::CategoryAxis(const QString &title, const std::vector<QString> &labels, bool horizontal) :
|
CategoryAxis::CategoryAxis(StatsView &view, const QString &title, const std::vector<QString> &labels, bool horizontal) :
|
||||||
StatsAxis(title, horizontal, true),
|
StatsAxis(view, title, horizontal, true),
|
||||||
labelsText(labels)
|
labelsText(labels)
|
||||||
{
|
{
|
||||||
setRange(-0.5, static_cast<double>(labels.size()) + 0.5);
|
setRange(-0.5, static_cast<double>(labels.size()) + 0.5);
|
||||||
|
@ -392,8 +413,6 @@ void CategoryAxis::updateLabels()
|
||||||
|
|
||||||
QString ellipsis = horizontal ? getEllipsis(fm, size_per_label) : QString();
|
QString ellipsis = horizontal ? getEllipsis(fm, size_per_label) : QString();
|
||||||
|
|
||||||
labels.clear();
|
|
||||||
ticks.clear();
|
|
||||||
labels.reserve(labelsText.size());
|
labels.reserve(labelsText.size());
|
||||||
ticks.reserve(labelsText.size() + 1);
|
ticks.reserve(labelsText.size() + 1);
|
||||||
double pos = 0.0;
|
double pos = 0.0;
|
||||||
|
@ -401,18 +420,18 @@ void CategoryAxis::updateLabels()
|
||||||
for (const QString &s: labelsText) {
|
for (const QString &s: labelsText) {
|
||||||
if (horizontal) {
|
if (horizontal) {
|
||||||
double width = static_cast<double>(fm.size(Qt::TextSingleLine, s).width());
|
double width = static_cast<double>(fm.size(Qt::TextSingleLine, s).width());
|
||||||
addLabel(width < size_per_label ? s : ellipsis, pos);
|
addLabel(fm, width < size_per_label ? s : ellipsis, pos);
|
||||||
} else {
|
} else {
|
||||||
if (fontHeight < size_per_label)
|
if (fontHeight < size_per_label)
|
||||||
addLabel(s, pos);
|
addLabel(fm, s, pos);
|
||||||
}
|
}
|
||||||
addTick(pos + 0.5);
|
addTick(pos + 0.5);
|
||||||
pos += 1.0;
|
pos += 1.0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
HistogramAxis::HistogramAxis(const QString &title, std::vector<HistogramAxisEntry> bins, bool horizontal) :
|
HistogramAxis::HistogramAxis(StatsView &view, const QString &title, std::vector<HistogramAxisEntry> bins, bool horizontal) :
|
||||||
StatsAxis(title, horizontal, false),
|
StatsAxis(view, title, horizontal, false),
|
||||||
bin_values(std::move(bins))
|
bin_values(std::move(bins))
|
||||||
{
|
{
|
||||||
if (bin_values.size() < 2) // Less than two makes no sense -> there must be at least one category
|
if (bin_values.size() < 2) // Less than two makes no sense -> there must be at least one category
|
||||||
|
@ -446,9 +465,6 @@ std::pair<QString, QString> HistogramAxis::getFirstLastLabel() const
|
||||||
// There, we obviously want to show the years and not the quarters.
|
// There, we obviously want to show the years and not the quarters.
|
||||||
void HistogramAxis::updateLabels()
|
void HistogramAxis::updateLabels()
|
||||||
{
|
{
|
||||||
labels.clear();
|
|
||||||
ticks.clear();
|
|
||||||
|
|
||||||
if (bin_values.size() < 2) // Less than two makes no sense -> there must be at least one category
|
if (bin_values.size() < 2) // Less than two makes no sense -> there must be at least one category
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
@ -488,9 +504,10 @@ void HistogramAxis::updateLabels()
|
||||||
if (first != 0)
|
if (first != 0)
|
||||||
addTick(bin_values.front().value);
|
addTick(bin_values.front().value);
|
||||||
int last = first;
|
int last = first;
|
||||||
|
QFontMetrics fm(labelFont);
|
||||||
for (int i = first; i < (int)bin_values.size(); i += step) {
|
for (int i = first; i < (int)bin_values.size(); i += step) {
|
||||||
const auto &[name, value, recommended] = bin_values[i];
|
const auto &[name, value, recommended] = bin_values[i];
|
||||||
addLabel(name, value);
|
addLabel(fm, name, value);
|
||||||
addTick(value);
|
addTick(value);
|
||||||
last = i;
|
last = i;
|
||||||
}
|
}
|
||||||
|
@ -617,7 +634,7 @@ static std::vector<HistogramAxisEntry> timeRangeToBins(double from, double to)
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
DateAxis::DateAxis(const QString &title, double from, double to, bool horizontal) :
|
DateAxis::DateAxis(StatsView &view, const QString &title, double from, double to, bool horizontal) :
|
||||||
HistogramAxis(title, timeRangeToBins(from, to), horizontal)
|
HistogramAxis(view, title, timeRangeToBins(from, to), horizontal)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,15 +2,19 @@
|
||||||
#ifndef STATS_AXIS_H
|
#ifndef STATS_AXIS_H
|
||||||
#define STATS_AXIS_H
|
#define STATS_AXIS_H
|
||||||
|
|
||||||
|
#include "chartitem.h"
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <QFont>
|
#include <QFont>
|
||||||
#include <QGraphicsSimpleTextItem>
|
|
||||||
#include <QGraphicsLineItem>
|
|
||||||
|
|
||||||
class QGraphicsScene;
|
class StatsView;
|
||||||
|
class ChartLineItem;
|
||||||
|
class QFontMetrics;
|
||||||
|
|
||||||
class StatsAxis : public QGraphicsLineItem {
|
// The labels and the title of the axis are rendered into a pixmap.
|
||||||
|
// The ticks and the baseline are realized as individual ChartLineItems.
|
||||||
|
class StatsAxis : public ChartPixmapItem {
|
||||||
public:
|
public:
|
||||||
virtual ~StatsAxis();
|
virtual ~StatsAxis();
|
||||||
// Returns minimum and maximum of shown range, not of data points.
|
// Returns minimum and maximum of shown range, not of data points.
|
||||||
|
@ -30,22 +34,25 @@ public:
|
||||||
|
|
||||||
std::vector<double> ticksPositions() const; // Positions in screen coordinates
|
std::vector<double> ticksPositions() const; // Positions in screen coordinates
|
||||||
protected:
|
protected:
|
||||||
StatsAxis(const QString &title, bool horizontal, bool labelsBetweenTicks);
|
StatsAxis(StatsView &view, const QString &title, bool horizontal, bool labelsBetweenTicks);
|
||||||
|
|
||||||
|
std::unique_ptr<ChartLineItem> line;
|
||||||
|
QString title;
|
||||||
|
double titleWidth;
|
||||||
|
|
||||||
struct Label {
|
struct Label {
|
||||||
std::unique_ptr<QGraphicsSimpleTextItem> label;
|
QString label;
|
||||||
|
int width;
|
||||||
double pos;
|
double pos;
|
||||||
Label(const QString &name, double pos, QGraphicsScene *scene, const QFont &font);
|
|
||||||
};
|
};
|
||||||
std::vector<Label> labels;
|
std::vector<Label> labels;
|
||||||
void addLabel(const QString &label, double pos);
|
void addLabel(const QFontMetrics &fm, const QString &label, double pos);
|
||||||
virtual void updateLabels() = 0;
|
virtual void updateLabels() = 0;
|
||||||
virtual std::pair<QString, QString> getFirstLastLabel() const = 0;
|
virtual std::pair<QString, QString> getFirstLastLabel() const = 0;
|
||||||
|
|
||||||
struct Tick {
|
struct Tick {
|
||||||
std::unique_ptr<QGraphicsLineItem> item;
|
std::unique_ptr<ChartLineItem> item;
|
||||||
double pos;
|
double pos;
|
||||||
Tick(double pos, QGraphicsScene *scene);
|
|
||||||
};
|
};
|
||||||
std::vector<Tick> ticks;
|
std::vector<Tick> ticks;
|
||||||
void addTick(double pos);
|
void addTick(double pos);
|
||||||
|
@ -55,9 +62,9 @@ protected:
|
||||||
bool labelsBetweenTicks; // When labels are between ticks, they can be moved closer to the axis
|
bool labelsBetweenTicks; // When labels are between ticks, they can be moved closer to the axis
|
||||||
|
|
||||||
QFont labelFont, titleFont;
|
QFont labelFont, titleFont;
|
||||||
std::unique_ptr<QGraphicsSimpleTextItem> title;
|
|
||||||
double size; // width for horizontal, height for vertical
|
double size; // width for horizontal, height for vertical
|
||||||
double zeroOnScreen;
|
double zeroOnScreen;
|
||||||
|
QPointF offset; // Offset of the label and title pixmap with respect to the (0,0) position.
|
||||||
double min, max;
|
double min, max;
|
||||||
double labelWidth; // Maximum width of labels
|
double labelWidth; // Maximum width of labels
|
||||||
private:
|
private:
|
||||||
|
@ -66,7 +73,7 @@ private:
|
||||||
|
|
||||||
class ValueAxis : public StatsAxis {
|
class ValueAxis : public StatsAxis {
|
||||||
public:
|
public:
|
||||||
ValueAxis(const QString &title, double min, double max, int decimals, bool horizontal);
|
ValueAxis(StatsView &view, const QString &title, double min, double max, int decimals, bool horizontal);
|
||||||
private:
|
private:
|
||||||
double min, max;
|
double min, max;
|
||||||
int decimals;
|
int decimals;
|
||||||
|
@ -76,7 +83,7 @@ private:
|
||||||
|
|
||||||
class CountAxis : public ValueAxis {
|
class CountAxis : public ValueAxis {
|
||||||
public:
|
public:
|
||||||
CountAxis(const QString &title, int count, bool horizontal);
|
CountAxis(StatsView &view, const QString &title, int count, bool horizontal);
|
||||||
private:
|
private:
|
||||||
int count;
|
int count;
|
||||||
void updateLabels() override;
|
void updateLabels() override;
|
||||||
|
@ -85,7 +92,7 @@ private:
|
||||||
|
|
||||||
class CategoryAxis : public StatsAxis {
|
class CategoryAxis : public StatsAxis {
|
||||||
public:
|
public:
|
||||||
CategoryAxis(const QString &title, const std::vector<QString> &labels, bool horizontal);
|
CategoryAxis(StatsView &view, const QString &title, const std::vector<QString> &labels, bool horizontal);
|
||||||
private:
|
private:
|
||||||
std::vector<QString> labelsText;
|
std::vector<QString> labelsText;
|
||||||
void updateLabels();
|
void updateLabels();
|
||||||
|
@ -100,7 +107,7 @@ struct HistogramAxisEntry {
|
||||||
|
|
||||||
class HistogramAxis : public StatsAxis {
|
class HistogramAxis : public StatsAxis {
|
||||||
public:
|
public:
|
||||||
HistogramAxis(const QString &title, std::vector<HistogramAxisEntry> bin_values, bool horizontal);
|
HistogramAxis(StatsView &view, const QString &title, std::vector<HistogramAxisEntry> bin_values, bool horizontal);
|
||||||
private:
|
private:
|
||||||
void updateLabels() override;
|
void updateLabels() override;
|
||||||
std::pair<QString, QString> getFirstLastLabel() const override;
|
std::pair<QString, QString> getFirstLastLabel() const override;
|
||||||
|
@ -110,7 +117,7 @@ private:
|
||||||
|
|
||||||
class DateAxis : public HistogramAxis {
|
class DateAxis : public HistogramAxis {
|
||||||
public:
|
public:
|
||||||
DateAxis(const QString &title, double from, double to, bool horizontal);
|
DateAxis(StatsView &view, const QString &title, double from, double to, bool horizontal);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -38,6 +38,7 @@ StatsView::StatsView(QQuickItem *parent) : QQuickItem(parent),
|
||||||
draggedItem(nullptr),
|
draggedItem(nullptr),
|
||||||
rootNode(nullptr)
|
rootNode(nullptr)
|
||||||
{
|
{
|
||||||
|
chartItems.reset(new std::vector<ChartItem *>[(size_t)ChartZValue::Count]);
|
||||||
setFlag(ItemHasContents, true);
|
setFlag(ItemHasContents, true);
|
||||||
|
|
||||||
connect(&diveListNotifier, &DiveListNotifier::numShownChanged, this, &StatsView::replotIfVisible);
|
connect(&diveListNotifier, &DiveListNotifier::numShownChanged, this, &StatsView::replotIfVisible);
|
||||||
|
@ -89,7 +90,6 @@ public:
|
||||||
QSGImageNode *imageNode; // imageNode to plot QGRaphicsScene on. Remove in due course.
|
QSGImageNode *imageNode; // imageNode to plot QGRaphicsScene on. Remove in due course.
|
||||||
// We entertain one node per Z-level.
|
// We entertain one node per Z-level.
|
||||||
std::array<QSGNode *, (size_t)ChartZValue::Count> zNodes;
|
std::array<QSGNode *, (size_t)ChartZValue::Count> zNodes;
|
||||||
std::array<std::vector<ChartItem *>, (size_t)ChartZValue::Count> items;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
RootNode::RootNode(QQuickWindow *w)
|
RootNode::RootNode(QQuickWindow *w)
|
||||||
|
@ -124,8 +124,8 @@ QSGNode *StatsView::updatePaintNode(QSGNode *oldNode, QQuickItem::UpdatePaintNod
|
||||||
plotAreaChanged(plotRect.size());
|
plotAreaChanged(plotRect.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto &v: n->items) {
|
for (int i = 0; i < (int)ChartZValue::Count; ++i) {
|
||||||
for (ChartItem *item: v) {
|
for (ChartItem *item: chartItems[i]) {
|
||||||
if (item->dirty)
|
if (item->dirty)
|
||||||
item->render();
|
item->render();
|
||||||
}
|
}
|
||||||
|
@ -151,7 +151,7 @@ void StatsView::addQSGNode(QSGNode *node, ChartZValue z)
|
||||||
void StatsView::unregisterChartItem(const ChartItem *item)
|
void StatsView::unregisterChartItem(const ChartItem *item)
|
||||||
{
|
{
|
||||||
int idx = std::clamp((int)item->zValue, 0, (int)ChartZValue::Count - 1);
|
int idx = std::clamp((int)item->zValue, 0, (int)ChartZValue::Count - 1);
|
||||||
std::vector<ChartItem *> &v = rootNode->items[idx];
|
std::vector<ChartItem *> &v = chartItems[idx];
|
||||||
auto it = std::find(v.begin(), v.end(), item);
|
auto it = std::find(v.begin(), v.end(), item);
|
||||||
if (it != v.end())
|
if (it != v.end())
|
||||||
v.erase(it);
|
v.erase(it);
|
||||||
|
@ -160,7 +160,7 @@ void StatsView::unregisterChartItem(const ChartItem *item)
|
||||||
void StatsView::registerChartItem(ChartItem *item)
|
void StatsView::registerChartItem(ChartItem *item)
|
||||||
{
|
{
|
||||||
int idx = std::clamp((int)item->zValue, 0, (int)ChartZValue::Count - 1);
|
int idx = std::clamp((int)item->zValue, 0, (int)ChartZValue::Count - 1);
|
||||||
rootNode->items[idx].push_back(item);
|
chartItems[idx].push_back(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
QQuickWindow *StatsView::w() const
|
QQuickWindow *StatsView::w() const
|
||||||
|
@ -307,8 +307,9 @@ void StatsView::updateTitlePos()
|
||||||
template <typename T, class... Args>
|
template <typename T, class... Args>
|
||||||
T *StatsView::createAxis(const QString &title, Args&&... args)
|
T *StatsView::createAxis(const QString &title, Args&&... args)
|
||||||
{
|
{
|
||||||
T *res = createItem<T>(&scene, title, std::forward<Args>(args)...);
|
std::unique_ptr<T> ptr = createChartItem<T>(title, std::forward<Args>(args)...);
|
||||||
axes.emplace_back(res);
|
T *res = ptr.get();
|
||||||
|
axes.push_back(std::move(ptr));
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -326,8 +327,8 @@ void StatsView::reset()
|
||||||
xAxis = yAxis = nullptr;
|
xAxis = yAxis = nullptr;
|
||||||
draggedItem = nullptr;
|
draggedItem = nullptr;
|
||||||
if (rootNode) {
|
if (rootNode) {
|
||||||
for (auto &v: rootNode->items)
|
for (int i = 0; i < (int)ChartZValue::Count; ++i)
|
||||||
v.clear(); // non-owning pointers
|
chartItems[i].clear(); // non-owning pointers
|
||||||
}
|
}
|
||||||
legend.reset();
|
legend.reset();
|
||||||
series.clear();
|
series.clear();
|
||||||
|
|
|
@ -142,6 +142,7 @@ private:
|
||||||
|
|
||||||
StatsState state;
|
StatsState state;
|
||||||
QFont titleFont;
|
QFont titleFont;
|
||||||
|
std::unique_ptr<std::vector<ChartItem *>[]> chartItems;
|
||||||
std::vector<std::unique_ptr<StatsAxis>> axes;
|
std::vector<std::unique_ptr<StatsAxis>> axes;
|
||||||
std::unique_ptr<StatsGrid> grid;
|
std::unique_ptr<StatsGrid> grid;
|
||||||
std::vector<std::unique_ptr<StatsSeries>> series;
|
std::vector<std::unique_ptr<StatsSeries>> series;
|
||||||
|
|
Loading…
Add table
Reference in a new issue