mirror of
https://github.com/AyuGram/AyuGramDesktop.git
synced 2025-06-05 06:33:57 +02:00
Removed some duplicated code from chart line view context.
This commit is contained in:
parent
1209bd35c5
commit
788a81df6c
6 changed files with 13 additions and 195 deletions
|
@ -39,23 +39,6 @@ float64 ChartLineViewContext::alpha(int id) const {
|
||||||
return (it == end(_entries)) ? 1. : it->second.alpha;
|
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) {
|
void ChartLineViewContext::tick(crl::time now) {
|
||||||
auto finishedCount = 0;
|
auto finishedCount = 0;
|
||||||
auto idsToRemove = std::vector<int>();
|
auto idsToRemove = std::vector<int>();
|
||||||
|
|
|
@ -7,66 +7,17 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||||
*/
|
*/
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <statistics/statistics_common.h>
|
|
||||||
|
|
||||||
namespace Statistic {
|
namespace Statistic {
|
||||||
|
|
||||||
class ChartLineViewContext final {
|
class ChartLineViewContext final {
|
||||||
public:
|
public:
|
||||||
ChartLineViewContext() = default;
|
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);
|
void setEnabled(int id, bool enabled, crl::time now);
|
||||||
[[nodiscard]] bool isEnabled(int id) const;
|
[[nodiscard]] bool isEnabled(int id) const;
|
||||||
[[nodiscard]] bool isFinished() const;
|
[[nodiscard]] bool isFinished() const;
|
||||||
[[nodiscard]] float64 alpha(int id) 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);
|
void tick(crl::time now);
|
||||||
|
|
||||||
float64 factor = 1.;
|
float64 factor = 1.;
|
||||||
|
@ -79,12 +30,8 @@ private:
|
||||||
};
|
};
|
||||||
|
|
||||||
base::flat_map<int, Entry> _entries;
|
base::flat_map<int, Entry> _entries;
|
||||||
base::flat_map<int, Cache> _caches;
|
|
||||||
base::flat_map<int, Cache> _cachesFooter;
|
|
||||||
bool _isFinished = true;
|
bool _isFinished = true;
|
||||||
|
|
||||||
bool _isFooter = false;
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace Statistic
|
} // namespace Statistic
|
||||||
|
|
|
@ -950,7 +950,7 @@ void ChartWidget::setupChartArea() {
|
||||||
// !_animationController.isFPSSlow()
|
// !_animationController.isFPSSlow()
|
||||||
// || !_animationController.animating());
|
// || !_animationController.animating());
|
||||||
PainterHighQualityEnabler hp(p);
|
PainterHighQualityEnabler hp(p);
|
||||||
_linearChartPainter.main->paint(
|
_linearChartView.main->paint(
|
||||||
p,
|
p,
|
||||||
_chartData,
|
_chartData,
|
||||||
_animationController.currentXIndices(),
|
_animationController.currentXIndices(),
|
||||||
|
@ -1075,8 +1075,7 @@ void ChartWidget::setupFooter() {
|
||||||
// !_animationController.isFPSSlow()
|
// !_animationController.isFPSSlow()
|
||||||
// || !_animationController.animating());
|
// || !_animationController.animating());
|
||||||
PainterHighQualityEnabler hp(p);
|
PainterHighQualityEnabler hp(p);
|
||||||
_animatedChartLines.setCacheFooter(true);
|
_linearChartView.footer->paint(
|
||||||
_linearChartPainter.footer->paint(
|
|
||||||
p,
|
p,
|
||||||
_chartData,
|
_chartData,
|
||||||
{ 0., float64(_chartData.x.size() - 1) },
|
{ 0., float64(_chartData.x.size() - 1) },
|
||||||
|
@ -1085,7 +1084,6 @@ void ChartWidget::setupFooter() {
|
||||||
r,
|
r,
|
||||||
_animatedChartLines,
|
_animatedChartLines,
|
||||||
detailsPaintContext);
|
detailsPaintContext);
|
||||||
_animatedChartLines.setCacheFooter(false);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -1254,8 +1252,8 @@ void ChartWidget::setupFilterButtons() {
|
||||||
void ChartWidget::setChartData(Data::StatisticalChart chartData) {
|
void ChartWidget::setChartData(Data::StatisticalChart chartData) {
|
||||||
_chartData = std::move(chartData);
|
_chartData = std::move(chartData);
|
||||||
|
|
||||||
_linearChartPainter.main = std::make_unique<LinearChartPainter>();
|
_linearChartView.main = std::make_unique<LinearChartView>();
|
||||||
_linearChartPainter.footer = std::make_unique<LinearChartPainter>();
|
_linearChartView.footer = std::make_unique<LinearChartView>();
|
||||||
|
|
||||||
setupDetails();
|
setupDetails();
|
||||||
setupFilterButtons();
|
setupFilterButtons();
|
||||||
|
|
|
@ -20,7 +20,7 @@ namespace Statistic {
|
||||||
class RpMouseWidget;
|
class RpMouseWidget;
|
||||||
class PointDetailsWidget;
|
class PointDetailsWidget;
|
||||||
class ChartLinesFilterWidget;
|
class ChartLinesFilterWidget;
|
||||||
class LinearChartPainter;
|
class LinearChartView;
|
||||||
|
|
||||||
class ChartWidget : public Ui::RpWidget {
|
class ChartWidget : public Ui::RpWidget {
|
||||||
public:
|
public:
|
||||||
|
@ -134,9 +134,9 @@ private:
|
||||||
|
|
||||||
ChartLineViewContext _animatedChartLines;
|
ChartLineViewContext _animatedChartLines;
|
||||||
struct {
|
struct {
|
||||||
std::unique_ptr<LinearChartPainter> main;
|
std::unique_ptr<LinearChartView> main;
|
||||||
std::unique_ptr<LinearChartPainter> footer;
|
std::unique_ptr<LinearChartView> footer;
|
||||||
} _linearChartPainter;
|
} _linearChartView;
|
||||||
|
|
||||||
struct {
|
struct {
|
||||||
base::unique_qptr<PointDetailsWidget> widget;
|
base::unique_qptr<PointDetailsWidget> widget;
|
||||||
|
|
|
@ -54,109 +54,9 @@ void PaintChartLine(
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
void PaintLinearChartView(
|
LinearChartView::LinearChartView() = default;
|
||||||
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(); //
|
|
||||||
|
|
||||||
const auto cacheToken = ChartLineViewContext::CacheToken(
|
void LinearChartView::paint(
|
||||||
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(
|
|
||||||
QPainter &p,
|
QPainter &p,
|
||||||
const Data::StatisticalChart &chartData,
|
const Data::StatisticalChart &chartData,
|
||||||
const Limits &xIndices,
|
const Limits &xIndices,
|
||||||
|
@ -166,7 +66,7 @@ void LinearChartPainter::paint(
|
||||||
ChartLineViewContext &lineViewContext,
|
ChartLineViewContext &lineViewContext,
|
||||||
DetailsPaintContext &detailsPaintContext) {
|
DetailsPaintContext &detailsPaintContext) {
|
||||||
|
|
||||||
const auto cacheToken = LinearChartPainter::CacheToken(
|
const auto cacheToken = LinearChartView::CacheToken(
|
||||||
xIndices,
|
xIndices,
|
||||||
xPercentageLimits,
|
xPercentageLimits,
|
||||||
heightLimits,
|
heightLimits,
|
||||||
|
|
|
@ -19,9 +19,9 @@ struct Limits;
|
||||||
struct DetailsPaintContext;
|
struct DetailsPaintContext;
|
||||||
struct ChartLineViewContext;
|
struct ChartLineViewContext;
|
||||||
|
|
||||||
class LinearChartPainter {
|
class LinearChartView {
|
||||||
public:
|
public:
|
||||||
LinearChartPainter();
|
LinearChartView();
|
||||||
|
|
||||||
void paint(
|
void paint(
|
||||||
QPainter &p,
|
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
|
} // namespace Statistic
|
||||||
|
|
Loading…
Add table
Reference in a new issue