Removed some duplicated code from chart line view context.

This commit is contained in:
23rd 2023-07-27 01:10:24 +03:00 committed by John Preston
parent 1209bd35c5
commit 788a81df6c
6 changed files with 13 additions and 195 deletions

View File

@ -39,23 +39,6 @@ float64 ChartLineViewContext::alpha(int id) const {
return (it == end(_entries)) ? 1. : it->second.alpha;
}
void ChartLineViewContext::setCacheImage(int id, QImage &&image) {
(_isFooter ? _cachesFooter : _caches)[id].image = std::move(image);
}
void ChartLineViewContext::setCacheLastToken(int id, CacheToken token) {
(_isFooter ? _cachesFooter : _caches)[id].lastToken = token;
}
void ChartLineViewContext::setCacheHQ(int id, bool value) {
(_isFooter ? _cachesFooter : _caches)[id].hq = value;
}
const ChartLineViewContext::Cache &ChartLineViewContext::cache(int id) {
[[maybe_unused]] auto unused = (_isFooter ? _cachesFooter : _caches)[id];
return (_isFooter ? _cachesFooter : _caches).find(id)->second;
}
void ChartLineViewContext::tick(crl::time now) {
auto finishedCount = 0;
auto idsToRemove = std::vector<int>();

View File

@ -7,66 +7,17 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#pragma once
#include <statistics/statistics_common.h>
namespace Statistic {
class ChartLineViewContext final {
public:
ChartLineViewContext() = default;
struct CacheToken final {
explicit CacheToken() = default;
explicit CacheToken(
Limits xIndices,
Limits xPercentageLimits,
Limits heightLimits,
QSize rectSize)
: xIndices(std::move(xIndices))
, xPercentageLimits(std::move(xPercentageLimits))
, heightLimits(std::move(heightLimits))
, rectSize(std::move(rectSize)) {
}
bool operator==(const CacheToken &other) const {
return (rectSize == other.rectSize)
&& (xIndices.min == other.xIndices.min)
&& (xIndices.max == other.xIndices.max)
&& (xPercentageLimits.min == other.xPercentageLimits.min)
&& (xPercentageLimits.max == other.xPercentageLimits.max)
&& (heightLimits.min == other.heightLimits.min)
&& (heightLimits.max == other.heightLimits.max);
}
bool operator!=(const CacheToken &other) const {
return !(*this == other);
}
Limits xIndices;
Limits xPercentageLimits;
Limits heightLimits;
QSize rectSize;
};
struct Cache final {
QImage image;
CacheToken lastToken;
bool hq = false;
};
void setEnabled(int id, bool enabled, crl::time now);
[[nodiscard]] bool isEnabled(int id) const;
[[nodiscard]] bool isFinished() const;
[[nodiscard]] float64 alpha(int id) const;
void setCacheFooter(bool value) {
_isFooter = value;
}
void setCacheImage(int id, QImage &&image);
void setCacheLastToken(int id, CacheToken token);
void setCacheHQ(int id, bool value);
[[nodiscard]] const Cache &cache(int id);
void tick(crl::time now);
float64 factor = 1.;
@ -79,12 +30,8 @@ private:
};
base::flat_map<int, Entry> _entries;
base::flat_map<int, Cache> _caches;
base::flat_map<int, Cache> _cachesFooter;
bool _isFinished = true;
bool _isFooter = false;
};
} // namespace Statistic

View File

@ -950,7 +950,7 @@ void ChartWidget::setupChartArea() {
// !_animationController.isFPSSlow()
// || !_animationController.animating());
PainterHighQualityEnabler hp(p);
_linearChartPainter.main->paint(
_linearChartView.main->paint(
p,
_chartData,
_animationController.currentXIndices(),
@ -1075,8 +1075,7 @@ void ChartWidget::setupFooter() {
// !_animationController.isFPSSlow()
// || !_animationController.animating());
PainterHighQualityEnabler hp(p);
_animatedChartLines.setCacheFooter(true);
_linearChartPainter.footer->paint(
_linearChartView.footer->paint(
p,
_chartData,
{ 0., float64(_chartData.x.size() - 1) },
@ -1085,7 +1084,6 @@ void ChartWidget::setupFooter() {
r,
_animatedChartLines,
detailsPaintContext);
_animatedChartLines.setCacheFooter(false);
}
});
@ -1254,8 +1252,8 @@ void ChartWidget::setupFilterButtons() {
void ChartWidget::setChartData(Data::StatisticalChart chartData) {
_chartData = std::move(chartData);
_linearChartPainter.main = std::make_unique<LinearChartPainter>();
_linearChartPainter.footer = std::make_unique<LinearChartPainter>();
_linearChartView.main = std::make_unique<LinearChartView>();
_linearChartView.footer = std::make_unique<LinearChartView>();
setupDetails();
setupFilterButtons();

View File

@ -20,7 +20,7 @@ namespace Statistic {
class RpMouseWidget;
class PointDetailsWidget;
class ChartLinesFilterWidget;
class LinearChartPainter;
class LinearChartView;
class ChartWidget : public Ui::RpWidget {
public:
@ -134,9 +134,9 @@ private:
ChartLineViewContext _animatedChartLines;
struct {
std::unique_ptr<LinearChartPainter> main;
std::unique_ptr<LinearChartPainter> footer;
} _linearChartPainter;
std::unique_ptr<LinearChartView> main;
std::unique_ptr<LinearChartView> footer;
} _linearChartView;
struct {
base::unique_qptr<PointDetailsWidget> widget;

View File

@ -54,109 +54,9 @@ void PaintChartLine(
} // namespace
void PaintLinearChartView(
QPainter &p,
const Data::StatisticalChart &chartData,
const Limits &xIndices,
const Limits &xPercentageLimits,
const Limits &heightLimits,
const QRect &rect,
ChartLineViewContext &lineViewContext,
DetailsPaintContext &detailsPaintContext) {
const auto currentMinHeight = rect.y(); //
const auto currentMaxHeight = rect.height() + rect.y(); //
LinearChartView::LinearChartView() = default;
const auto cacheToken = ChartLineViewContext::CacheToken(
xIndices,
xPercentageLimits,
heightLimits,
rect.size());
for (const auto &line : chartData.lines) {
p.setOpacity(lineViewContext.alpha(line.id));
if (!p.opacity()) {
continue;
}
const auto additionalP = (chartData.xPercentage.size() < 2)
? 0.
: (chartData.xPercentage.front() * rect.width());
////
const auto &cache = lineViewContext.cache(line.id);
const auto isSameToken = (cache.lastToken == cacheToken);
if (isSameToken && cache.hq) {
p.drawImage(rect.topLeft(), cache.image);
continue;
}
const auto kRatio = lineViewContext.factor;//0.5;
lineViewContext.setCacheHQ(line.id, isSameToken);
auto image = QImage();
image = QImage(
rect.size() * style::DevicePixelRatio() * (isSameToken ? 1. : kRatio),
QImage::Format_ARGB32_Premultiplied);
image.setDevicePixelRatio(style::DevicePixelRatio());
image.fill(Qt::transparent);
// image.fill(Qt::darkRed);
auto imagePainter = QPainter(&image);
imagePainter.setRenderHint(QPainter::Antialiasing, true);
if (isSameToken) {
// PainterHighQualityEnabler hp(imagePainter);
} else {
imagePainter.scale(kRatio, kRatio);
}
////
auto first = true;
auto chartPath = QPainterPath();
const auto localStart = std::max(0, int(xIndices.min));
const auto localEnd = std::min(
int(chartData.xPercentage.size() - 1),
int(xIndices.max));
for (auto i = localStart; i <= localEnd; i++) {
if (line.y[i] < 0) {
continue;
}
const auto xPoint = rect.width()
* ((chartData.xPercentage[i] - xPercentageLimits.min)
/ (xPercentageLimits.max - xPercentageLimits.min));
const auto yPercentage = (line.y[i] - heightLimits.min)
/ float64(heightLimits.max - heightLimits.min);
const auto yPoint = rect.y() + (1. - yPercentage) * rect.height();
if (i == detailsPaintContext.xIndex) {
detailsPaintContext.dots.push_back({
QPointF(xPoint, yPoint),
line.color,
p.opacity(),
});
}
if (first) {
first = false;
chartPath.moveTo(xPoint, yPoint);
}
chartPath.lineTo(xPoint, yPoint);
}
imagePainter.translate(-rect.topLeft());
imagePainter.setPen(QPen(line.color, st::statisticsChartLineWidth));
imagePainter.setBrush(Qt::NoBrush);
imagePainter.drawPath(chartPath);
if (!isSameToken) {
image = image.scaled(rect.size() * style::DevicePixelRatio(), Qt::IgnoreAspectRatio, Qt::FastTransformation);
}
p.drawImage(rect.topLeft(), image);
lineViewContext.setCacheImage(line.id, std::move(image));
lineViewContext.setCacheLastToken(line.id, cacheToken);
}
p.setPen(st::boxTextFg);
p.setOpacity(1.);
}
LinearChartPainter::LinearChartPainter() = default;
void LinearChartPainter::paint(
void LinearChartView::paint(
QPainter &p,
const Data::StatisticalChart &chartData,
const Limits &xIndices,
@ -166,7 +66,7 @@ void LinearChartPainter::paint(
ChartLineViewContext &lineViewContext,
DetailsPaintContext &detailsPaintContext) {
const auto cacheToken = LinearChartPainter::CacheToken(
const auto cacheToken = LinearChartView::CacheToken(
xIndices,
xPercentageLimits,
heightLimits,

View File

@ -19,9 +19,9 @@ struct Limits;
struct DetailsPaintContext;
struct ChartLineViewContext;
class LinearChartPainter {
class LinearChartView {
public:
LinearChartPainter();
LinearChartView();
void paint(
QPainter &p,
@ -77,14 +77,4 @@ private:
};
void PaintLinearChartView(
QPainter &p,
const Data::StatisticalChart &chartData,
const Limits &xIndices,
const Limits &xPercentageLimits,
const Limits &heightLimits,
const QRect &rect,
ChartLineViewContext &lineViewContext,
DetailsPaintContext &detailsPaintContext);
} // namespace Statistic