Added support of chart titles to Data and API classes for statistics.

This commit is contained in:
23rd 2023-08-24 23:01:39 +03:00 committed by John Preston
parent 1dc57afbe1
commit 160794b26c
4 changed files with 92 additions and 2 deletions

View File

@ -192,6 +192,51 @@ void RpMouseWidget::mouseReleaseEvent(QMouseEvent *e) {
_mouseStateChanged.fire({ e->pos(), QEvent::MouseButtonRelease });
}
class ChartWidget::Header final : public RpWidget {
public:
using RpWidget::RpWidget;
void setTitle(QString title);
void setRightInfo(QString rightInfo);
protected:
void paintEvent(QPaintEvent *e) override;
private:
Ui::Text::String _title;
Ui::Text::String _rightInfo;
int _titleWidth = 0;
};
void ChartWidget::Header::setTitle(QString title) {
_titleWidth = st::statisticsHeaderTitleTextStyle.font->width(title);
_title.setText(st::statisticsHeaderTitleTextStyle, std::move(title));
}
void ChartWidget::Header::setRightInfo(QString rightInfo) {
_rightInfo.setText(
st::statisticsHeaderDatesTextStyle,
std::move(rightInfo));
}
void ChartWidget::Header::paintEvent(QPaintEvent *e) {
auto p = Painter(this);
p.setPen(st::boxTextFg);
const auto top = (height()
- st::statisticsHeaderTitleTextStyle.font->height) / 2;
_title.drawLeftElided(p, 0, top, width(), width());
_rightInfo.drawRightElided(
p,
0,
top,
width() - _titleWidth,
width(),
1,
style::al_right);
}
class ChartWidget::Footer final : public RpMouseWidget {
public:
using PaintCallback = Fn<void(QPainter &, const QRect &)>;
@ -823,6 +868,7 @@ bool ChartWidget::ChartAnimationController::isFPSSlow() const {
ChartWidget::ChartWidget(not_null<Ui::RpWidget*> parent)
: Ui::RpWidget(parent)
, _chartArea(base::make_unique_q<RpMouseWidget>(this))
, _header(std::make_unique<Header>(this))
, _footer(std::make_unique<Footer>(this))
, _animationController([=] {
_chartArea->update();
@ -857,8 +903,14 @@ int ChartWidget::resizeGetHeight(int newWidth) {
const auto resultHeight = st::statisticsChartHeight
+ st::statisticsChartFooterHeight
+ st::statisticsChartFooterSkip
+ filtersHeight;
+ filtersHeight
+ st::statisticsChartHeaderHeight;
{
_header->setGeometry(
0,
0,
newWidth,
st::statisticsChartHeaderHeight);
_footer->setGeometry(
0,
resultHeight - st::statisticsChartFooterHeight - filtersHeight,
@ -870,7 +922,7 @@ int ChartWidget::resizeGetHeight(int newWidth) {
}
_chartArea->setGeometry(
0,
0,
st::statisticsChartHeaderHeight,
newWidth,
resultHeight
- st::statisticsChartFooterHeight
@ -1070,6 +1122,19 @@ void ChartWidget::updateBottomDates() {
_animationController.restartBottomLineAlpha();
}
void ChartWidget::updateHeader() {
if (!_chartData) {
return;
}
const auto indices = _animationController.currentXIndices();
_header->setRightInfo(_chartData.getDayString(indices.min)
+ ' '
+ QChar(8212)
+ ' '
+ _chartData.getDayString(indices.max));
_header->update();
}
void ChartWidget::setupFooter() {
_footer->setPaintChartCallback([=, fullXLimits = Limits{ 0., 1. }](
QPainter &p,
@ -1108,6 +1173,7 @@ void ChartWidget::setupFooter() {
now);
updateChartFullWidth(_chartArea->width());
updateBottomDates();
updateHeader();
if ((now - _lastHeightLimitsChanged) < kHeightLimitsUpdateTimeout) {
return;
}
@ -1233,6 +1299,7 @@ void ChartWidget::setChartData(Data::StatisticalChart chartData) {
_linearChartView,
0);
updateChartFullWidth(_chartArea->width());
updateHeader();
updateBottomDates();
_animationController.finish();
addHorizontalLine(_animationController.finalHeightLimits(), false);
@ -1240,6 +1307,15 @@ void ChartWidget::setChartData(Data::StatisticalChart chartData) {
_footer->update();
}
void ChartWidget::setTitle(rpl::producer<QString> &&title) {
std::move(
title
) | rpl::start_with_next([=](QString t) {
_header->setTitle(std::move(t));
_header->update();
}, _header->lifetime());
}
void ChartWidget::setZoomedChartData(Data::StatisticalChart chartData) {
_zoomedChartWidget = base::make_unique_q<ChartWidget>(
dynamic_cast<Ui::RpWidget*>(parentWidget()));

View File

@ -26,6 +26,7 @@ public:
ChartWidget(not_null<Ui::RpWidget*> parent);
void setChartData(Data::StatisticalChart chartData);
void setTitle(rpl::producer<QString> &&title);
void setZoomedChartData(Data::StatisticalChart chartData);
void addHorizontalLine(Limits newHeight, bool animated);
@ -46,6 +47,7 @@ protected:
int resizeGetHeight(int newWidth) override;
private:
class Header;
class Footer;
class ChartAnimationController final {
@ -128,10 +130,12 @@ private:
void setupFilterButtons();
void updateBottomDates();
void updateHeader();
void updateChartFullWidth(int w);
const base::unique_qptr<RpMouseWidget> _chartArea;
const std::unique_ptr<Header> _header;
const std::unique_ptr<Footer> _footer;
base::unique_qptr<ChartLinesFilterWidget> _filterButtons;
Data::StatisticalChart _chartData;

View File

@ -40,3 +40,11 @@ statisticsDetailsPopupStyle: TextStyle(defaultTextStyle) {
statisticsDetailsBottomCaptionStyle: TextStyle(defaultTextStyle) {
font: font(10px);
}
statisticsChartHeaderHeight: 20px;
statisticsHeaderTitleTextStyle: TextStyle(defaultTextStyle) {
font: font(12px semibold);
}
statisticsHeaderDatesTextStyle: TextStyle(defaultTextStyle) {
font: font(11px semibold);
}

View File

@ -55,6 +55,8 @@ void StatisticsBox(not_null<Ui::GenericBox*> box, not_null<PeerData*> peer) {
) | rpl::start_with_done([=] {
if (const auto stats = api->channelStats()) {
chartWidget->setChartData(stats.memberCountGraph.chart);
processZoom(chartWidget, stats.memberCountGraph.zoomToken);
chartWidget->setTitle(tr::lng_chart_title_member_count());
}
}, chartWidget->lifetime());
box->setTitle(tr::lng_stats_title());