Removed chart line view context.

This commit is contained in:
23rd 2023-07-27 01:23:57 +03:00 committed by John Preston
parent 788a81df6c
commit 2055cc70d1
7 changed files with 88 additions and 155 deletions

View File

@ -1284,8 +1284,6 @@ PRIVATE
settings/settings_websites.h
statistics/chart_horizontal_lines_data.cpp
statistics/chart_horizontal_lines_data.h
statistics/chart_line_view_context.cpp
statistics/chart_line_view_context.h
statistics/chart_widget.cpp
statistics/chart_widget.h
statistics/linear_chart_view.cpp

View File

@ -1,68 +0,0 @@
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#include "statistics/chart_line_view_context.h"
namespace Statistic {
namespace {
constexpr auto kAlphaDuration = float64(350);
} // namespace
void ChartLineViewContext::setEnabled(int id, bool enabled, crl::time now) {
const auto it = _entries.find(id);
if (it == end(_entries)) {
_entries[id] = Entry{ .enabled = enabled, .startedAt = now };
} else if (it->second.enabled != enabled) {
auto &entry = it->second;
entry.enabled = enabled;
entry.startedAt = now
- kAlphaDuration * (enabled ? entry.alpha : (1. - entry.alpha));
}
_isFinished = false;
}
bool ChartLineViewContext::isFinished() const {
return _isFinished;
}
bool ChartLineViewContext::isEnabled(int id) const {
const auto it = _entries.find(id);
return (it == end(_entries)) ? true : it->second.enabled;
}
float64 ChartLineViewContext::alpha(int id) const {
const auto it = _entries.find(id);
return (it == end(_entries)) ? 1. : it->second.alpha;
}
void ChartLineViewContext::tick(crl::time now) {
auto finishedCount = 0;
auto idsToRemove = std::vector<int>();
for (auto &[id, entry] : _entries) {
if (!entry.startedAt) {
continue;
}
const auto progress = (now - entry.startedAt) / kAlphaDuration;
entry.alpha = std::clamp(
entry.enabled ? progress : (1. - progress),
0.,
1.);
if (entry.alpha == 1.) {
idsToRemove.push_back(id);
}
if (progress >= 1.) {
finishedCount++;
}
}
_isFinished = (finishedCount == _entries.size());
for (const auto &id : idsToRemove) {
_entries.remove(id);
}
}
} // namespace Statistic

View File

@ -1,37 +0,0 @@
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#pragma once
namespace Statistic {
class ChartLineViewContext final {
public:
ChartLineViewContext() = default;
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 tick(crl::time now);
float64 factor = 1.;
private:
struct Entry final {
bool enabled = false;
crl::time startedAt = 0;
float64 alpha = 1.;
};
base::flat_map<int, Entry> _entries;
bool _isFinished = true;
};
} // namespace Statistic

View File

@ -479,11 +479,11 @@ ChartWidget::ChartAnimationController::ChartAnimationController(
void ChartWidget::ChartAnimationController::setXPercentageLimits(
Data::StatisticalChart &chartData,
Limits xPercentageLimits,
const ChartLineViewContext &chartLinesViewContext,
const std::unique_ptr<LinearChartView> &linearChartView,
crl::time now) {
if ((_animationValueXMin.to() == xPercentageLimits.min)
&& (_animationValueXMax.to() == xPercentageLimits.max)
&& chartLinesViewContext.isFinished()) {
&& linearChartView->isFinished()) {
return;
}
start();
@ -505,7 +505,7 @@ void ChartWidget::ChartAnimationController::setXPercentageLimits(
auto minValueFull = std::numeric_limits<int>::max();
auto maxValueFull = 0;
for (auto &l : chartData.lines) {
if (!chartLinesViewContext.isEnabled(l.id)) {
if (!linearChartView->isEnabled(l.id)) {
continue;
}
const auto lineMax = l.segmentTree.rMaxQ(startXIndex, endXIndex);
@ -521,7 +521,7 @@ void ChartWidget::ChartAnimationController::setXPercentageLimits(
if (!_previousFullHeightLimits.max) {
_previousFullHeightLimits = _finalHeightLimits;
}
if (!chartLinesViewContext.isFinished()) {
if (!linearChartView->isFinished()) {
_animationValueFooterHeightMin = anim::value(
_animationValueFooterHeightMin.current(),
minValueFull);
@ -565,7 +565,7 @@ void ChartWidget::ChartAnimationController::setXPercentageLimits(
_dtHeight.currentAlpha = 0.;
_addHorizontalLineRequests.fire({});
}
_dtHeight.speed = (!chartLinesViewContext.isFinished())
_dtHeight.speed = (!linearChartView->isFinished())
? kDtHeightSpeedFilter
: (k > kDtHeightSpeedThreshold1)
? kDtHeightSpeed1
@ -611,7 +611,7 @@ void ChartWidget::ChartAnimationController::tick(
crl::time now,
std::vector<ChartHorizontalLinesData> &horizontalLines,
std::vector<BottomCaptionLineData> &dateLines,
ChartLineViewContext &chartLinesViewContext) {
const std::unique_ptr<LinearChartView> &linearChartView) {
if (!_animation.animating()) {
return;
}
@ -662,7 +662,7 @@ void ChartWidget::ChartAnimationController::tick(
const auto footerMinFinished = isFinished(_animationValueFooterHeightMin);
const auto footerMaxFinished = isFinished(_animationValueFooterHeightMax);
chartLinesViewContext.tick(now);
linearChartView->tick(now);
if (xFinished
&& yFinished
@ -670,7 +670,7 @@ void ChartWidget::ChartAnimationController::tick(
&& bottomLineAlphaFinished
&& footerMinFinished
&& footerMaxFinished
&& chartLinesViewContext.isFinished()) {
&& linearChartView->isFinished()) {
const auto &lines = horizontalLines.back().lines;
if ((_finalHeightLimits.min == _animationValueHeightMin.to())
&& _finalHeightLimits.max == _animationValueHeightMax.to()) {
@ -822,7 +822,7 @@ ChartWidget::ChartWidget(not_null<Ui::RpWidget*> parent)
, _animationController([=] {
_chartArea->update();
if (_animationController.footerAnimating()
|| !_animatedChartLines.isFinished()) {
|| !_linearChartView.main->isFinished()) {
_footer->update();
}
}) {
@ -900,7 +900,7 @@ void ChartWidget::setupChartArea() {
now,
_horizontalLines,
_bottomLine.dates,
_animatedChartLines);
_linearChartView.main);
const auto chartRect = chartAreaRect();
@ -934,7 +934,7 @@ void ChartWidget::setupChartArea() {
for (const auto &line : _chartData.lines) {
_details.widget->setLineAlpha(
line.id,
_animatedChartLines.alpha(line.id));
_linearChartView.main->alpha(line.id));
}
}
}
@ -957,7 +957,6 @@ void ChartWidget::setupChartArea() {
_animationController.currentXLimits(),
_animationController.currentHeightLimits(),
chartRect,
_animatedChartLines,
detailsPaintContext);
}
@ -1082,7 +1081,6 @@ void ChartWidget::setupFooter() {
fullXLimits,
_animationController.currentFooterHeightLimits(),
r,
_animatedChartLines,
detailsPaintContext);
}
});
@ -1099,7 +1097,7 @@ void ChartWidget::setupFooter() {
_animationController.setXPercentageLimits(
_chartData,
xPercentageLimits,
_animatedChartLines,
_linearChartView.main,
now);
updateChartFullWidth(_chartArea->width());
updateBottomDates();
@ -1194,26 +1192,6 @@ void ChartWidget::setupFilterButtons() {
}
_filterButtons = base::make_unique_q<ChartLinesFilterWidget>(this);
const auto asd = Ui::CreateChild<Ui::AbstractButton>(_filterButtons.get());
asd->paintRequest(
) | rpl::start_with_next([=](QRect r) {
auto p = QPainter(asd);
p.setOpacity(0.3);
p.fillRect(r, Qt::darkRed);
p.setOpacity(1.0);
p.setFont(st::statisticsDetailsBottomCaptionStyle.font);
p.setPen(st::boxTextFg);
p.drawText(asd->rect(), QString::number(_animatedChartLines.factor * 100) + "%", style::al_center);
}, asd->lifetime());
asd->setClickedCallback([=] {
_animatedChartLines.factor -= 0.1;
if (_animatedChartLines.factor <= 0) {
_animatedChartLines.factor = 1.0;
}
asd->update();
});
asd->resize(50, 50);
sizeValue(
) | rpl::filter([](const QSize &s) {
return s.width() > 0;
@ -1232,19 +1210,17 @@ void ChartWidget::setupFilterButtons() {
_filterButtons->fillButtons(texts, colors, ids, s.width());
resizeHeight();
asd->raise();
asd->moveToRight(0, 0);
}, _filterButtons->lifetime());
_filterButtons->buttonEnabledChanges(
) | rpl::start_with_next([=](const ChartLinesFilterWidget::Entry &e) {
const auto now = crl::now();
_animatedChartLines.setEnabled(e.id, e.enabled, now);
_linearChartView.main->setEnabled(e.id, e.enabled, now);
_animationController.setXPercentageLimits(
_chartData,
_animationController.currentXLimits(),
_animatedChartLines,
_linearChartView.main,
now);
}, _filterButtons->lifetime());
}
@ -1261,7 +1237,7 @@ void ChartWidget::setChartData(Data::StatisticalChart chartData) {
_animationController.setXPercentageLimits(
_chartData,
{ _chartData.xPercentage.front(), _chartData.xPercentage.back() },
_animatedChartLines,
_linearChartView.main,
0);
updateChartFullWidth(_chartArea->width());
updateBottomDates();

View File

@ -9,7 +9,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "data/data_statistics.h"
#include "statistics/chart_horizontal_lines_data.h"
#include "statistics/chart_line_view_context.h"
#include "statistics/statistics_common.h"
#include "ui/effects/animation_value.h"
#include "ui/effects/animations.h"
@ -50,7 +49,7 @@ private:
void setXPercentageLimits(
Data::StatisticalChart &chartData,
Limits xPercentageLimits,
const ChartLineViewContext &chartLinesViewContext,
const std::unique_ptr<LinearChartView> &linearChartView,
crl::time now);
void start();
void finish();
@ -60,7 +59,7 @@ private:
crl::time now,
std::vector<ChartHorizontalLinesData> &horizontalLines,
std::vector<BottomCaptionLineData> &dateLines,
ChartLineViewContext &chartLinesViewContext);
const std::unique_ptr<LinearChartView> &linearChartView);
[[nodiscard]] Limits currentXLimits() const;
[[nodiscard]] Limits currentXIndices() const;
@ -132,7 +131,6 @@ private:
base::unique_qptr<ChartLinesFilterWidget> _filterButtons;
Data::StatisticalChart _chartData;
ChartLineViewContext _animatedChartLines;
struct {
std::unique_ptr<LinearChartView> main;
std::unique_ptr<LinearChartView> footer;

View File

@ -8,7 +8,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "statistics/linear_chart_view.h"
#include "data/data_statistics.h"
#include "statistics/chart_line_view_context.h"
#include "statistics/statistics_common.h"
#include "ui/effects/animation_value_f.h"
#include "ui/painter.h"
@ -18,6 +17,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
namespace Statistic {
namespace {
constexpr auto kAlphaDuration = float64(350);
void PaintChartLine(
QPainter &p,
int lineIndex,
@ -63,7 +64,6 @@ void LinearChartView::paint(
const Limits &xPercentageLimits,
const Limits &heightLimits,
const QRect &rect,
ChartLineViewContext &lineViewContext,
DetailsPaintContext &detailsPaintContext) {
const auto cacheToken = LinearChartView::CacheToken(
@ -74,7 +74,7 @@ void LinearChartView::paint(
for (auto i = 0; i < chartData.lines.size(); i++) {
const auto &line = chartData.lines[i];
p.setOpacity(lineViewContext.alpha(line.id));
p.setOpacity(alpha(line.id));
if (!p.opacity()) {
continue;
}
@ -128,4 +128,56 @@ void LinearChartView::paint(
}
}
void LinearChartView::setEnabled(int id, bool enabled, crl::time now) {
const auto it = _entries.find(id);
if (it == end(_entries)) {
_entries[id] = Entry{ .enabled = enabled, .startedAt = now };
} else if (it->second.enabled != enabled) {
auto &entry = it->second;
entry.enabled = enabled;
entry.startedAt = now
- kAlphaDuration * (enabled ? entry.alpha : (1. - entry.alpha));
}
_isFinished = false;
}
bool LinearChartView::isFinished() const {
return _isFinished;
}
bool LinearChartView::isEnabled(int id) const {
const auto it = _entries.find(id);
return (it == end(_entries)) ? true : it->second.enabled;
}
float64 LinearChartView::alpha(int id) const {
const auto it = _entries.find(id);
return (it == end(_entries)) ? 1. : it->second.alpha;
}
void LinearChartView::tick(crl::time now) {
auto finishedCount = 0;
auto idsToRemove = std::vector<int>();
for (auto &[id, entry] : _entries) {
if (!entry.startedAt) {
continue;
}
const auto progress = (now - entry.startedAt) / kAlphaDuration;
entry.alpha = std::clamp(
entry.enabled ? progress : (1. - progress),
0.,
1.);
if (entry.alpha == 1.) {
idsToRemove.push_back(id);
}
if (progress >= 1.) {
finishedCount++;
}
}
_isFinished = (finishedCount == _entries.size());
for (const auto &id : idsToRemove) {
_entries.remove(id);
}
}
} // namespace Statistic

View File

@ -17,7 +17,6 @@ namespace Statistic {
struct Limits;
struct DetailsPaintContext;
struct ChartLineViewContext;
class LinearChartView {
public:
@ -30,9 +29,15 @@ public:
const Limits &xPercentageLimits,
const Limits &heightLimits,
const QRect &rect,
ChartLineViewContext &lineViewContext,
DetailsPaintContext &detailsPaintContext);
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 tick(crl::time now);
private:
struct CacheToken final {
explicit CacheToken() = default;
@ -75,6 +80,15 @@ private:
base::flat_map<int, Cache> _caches;
struct Entry final {
bool enabled = false;
crl::time startedAt = 0;
float64 alpha = 1.;
};
base::flat_map<int, Entry> _entries;
bool _isFinished = true;
};
} // namespace Statistic