2023-05-24 15:30:30 +00:00
|
|
|
/*
|
|
|
|
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/linear_chart_view.h"
|
|
|
|
|
|
|
|
#include "data/data_statistics.h"
|
2023-07-14 04:25:17 +00:00
|
|
|
#include "statistics/chart_line_view_context.h"
|
2023-05-26 13:04:33 +00:00
|
|
|
#include "statistics/statistics_common.h"
|
2023-05-26 13:04:49 +00:00
|
|
|
#include "ui/effects/animation_value_f.h"
|
2023-07-07 08:28:50 +00:00
|
|
|
#include "ui/painter.h"
|
2023-05-24 15:30:30 +00:00
|
|
|
#include "styles/style_boxes.h"
|
2023-07-07 08:28:50 +00:00
|
|
|
#include "styles/style_statistics.h"
|
2023-05-24 15:30:30 +00:00
|
|
|
|
|
|
|
namespace Statistic {
|
2023-07-26 22:03:52 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
void PaintChartLine(
|
|
|
|
QPainter &p,
|
|
|
|
int lineIndex,
|
|
|
|
const Data::StatisticalChart &chartData,
|
|
|
|
const Limits &xIndices,
|
|
|
|
const Limits &xPercentageLimits,
|
|
|
|
const Limits &heightLimits,
|
|
|
|
const QSize &size) {
|
|
|
|
const auto &line = chartData.lines[lineIndex];
|
|
|
|
|
|
|
|
auto chartPoints = QPolygonF();
|
|
|
|
|
|
|
|
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 = size.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 = (1. - yPercentage) * size.height();
|
|
|
|
chartPoints << QPointF(xPoint, yPoint);
|
|
|
|
}
|
|
|
|
p.setPen(QPen(line.color, st::statisticsChartLineWidth));
|
|
|
|
p.setBrush(Qt::NoBrush);
|
|
|
|
p.drawPolyline(chartPoints);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
2023-05-24 15:30:30 +00:00
|
|
|
|
2023-07-26 22:10:24 +00:00
|
|
|
LinearChartView::LinearChartView() = default;
|
2023-07-26 22:03:52 +00:00
|
|
|
|
2023-07-26 22:10:24 +00:00
|
|
|
void LinearChartView::paint(
|
2023-07-26 22:03:52 +00:00
|
|
|
QPainter &p,
|
|
|
|
const Data::StatisticalChart &chartData,
|
|
|
|
const Limits &xIndices,
|
|
|
|
const Limits &xPercentageLimits,
|
|
|
|
const Limits &heightLimits,
|
|
|
|
const QRect &rect,
|
|
|
|
ChartLineViewContext &lineViewContext,
|
|
|
|
DetailsPaintContext &detailsPaintContext) {
|
|
|
|
|
2023-07-26 22:10:24 +00:00
|
|
|
const auto cacheToken = LinearChartView::CacheToken(
|
2023-07-26 22:03:52 +00:00
|
|
|
xIndices,
|
|
|
|
xPercentageLimits,
|
|
|
|
heightLimits,
|
|
|
|
rect.size());
|
|
|
|
|
|
|
|
for (auto i = 0; i < chartData.lines.size(); i++) {
|
|
|
|
const auto &line = chartData.lines[i];
|
|
|
|
p.setOpacity(lineViewContext.alpha(line.id));
|
|
|
|
if (!p.opacity()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (p.opacity() < 1.) {
|
|
|
|
// p.setRenderHint(QPainter::Antialiasing, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
////
|
|
|
|
auto &cache = _caches[line.id];
|
|
|
|
|
|
|
|
const auto isSameToken = (cache.lastToken == cacheToken);
|
|
|
|
if (isSameToken && cache.hq) {
|
|
|
|
p.drawImage(rect.topLeft(), cache.image);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const auto ratio = style::DevicePixelRatio();
|
|
|
|
cache.hq = isSameToken;
|
|
|
|
auto image = QImage();
|
|
|
|
image = QImage(
|
|
|
|
rect.size() * style::DevicePixelRatio() * (isSameToken ? 1. : ratio),
|
|
|
|
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(ratio, ratio);
|
|
|
|
}
|
|
|
|
////
|
|
|
|
|
|
|
|
PaintChartLine(
|
|
|
|
imagePainter,
|
|
|
|
i,
|
|
|
|
chartData,
|
|
|
|
xIndices,
|
|
|
|
xPercentageLimits,
|
|
|
|
heightLimits,
|
|
|
|
rect.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isSameToken) {
|
|
|
|
image = image.scaled(rect.size() * style::DevicePixelRatio(), Qt::IgnoreAspectRatio, Qt::FastTransformation);
|
|
|
|
}
|
|
|
|
p.drawImage(rect.topLeft(), image);
|
|
|
|
cache.lastToken = cacheToken;
|
|
|
|
cache.image = std::move(image);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-24 15:30:30 +00:00
|
|
|
} // namespace Statistic
|