Skip to content

Commit

Permalink
Fix Summary Chart animations stopping prematurely
Browse files Browse the repository at this point in the history
  • Loading branch information
GriffinRichards committed Oct 8, 2024
1 parent d369806 commit a31a014
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
2 changes: 1 addition & 1 deletion include/ui/wildmonchart.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public slots:
void applySpeciesColors(const QList<QBarSet*> &);
QChart::ChartTheme currentTheme() const;
void updateTheme();
void limitChartAnimation(QChart*);
void limitChartAnimation();

void showHelpDialog();
};
Expand Down
28 changes: 21 additions & 7 deletions src/ui/wildmonchart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ WildMonChart::WildMonChart(QWidget *parent, const EncounterTableModel *table) :
connect(ui->comboBox_Species, &QComboBox::currentTextChanged, this, &WildMonChart::refreshLevelDistributionChart);
connect(ui->comboBox_Group, &QComboBox::currentTextChanged, this, &WildMonChart::refreshLevelDistributionChart);

connect(ui->tabWidget, &QTabWidget::currentChanged, this, &WildMonChart::limitChartAnimation);

// Set up Theme combo box
for (auto i : themes)
ui->comboBox_Theme->addItem(i.first, i.second);
Expand Down Expand Up @@ -176,14 +178,16 @@ void WildMonChart::refreshSpeciesDistributionChart() {
if (ui->chartView_SpeciesDistribution->chart())
ui->chartView_SpeciesDistribution->chart()->deleteLater();
ui->chartView_SpeciesDistribution->setChart(createSpeciesDistributionChart());
limitChartAnimation(ui->chartView_SpeciesDistribution->chart());
if (ui->tabWidget->currentWidget() == ui->tabSpecies)
limitChartAnimation();
}

void WildMonChart::refreshLevelDistributionChart() {
if (ui->chartView_LevelDistribution->chart())
ui->chartView_LevelDistribution->chart()->deleteLater();
ui->chartView_LevelDistribution->setChart(createLevelDistributionChart());
limitChartAnimation(ui->chartView_LevelDistribution->chart());
if (ui->tabWidget->currentWidget() == ui->tabLevels)
limitChartAnimation();
}

QStringList WildMonChart::getSpeciesNamesAlphabetical() const {
Expand Down Expand Up @@ -408,17 +412,27 @@ void WildMonChart::applySpeciesColors(const QList<QBarSet*> &barSets) {
set->setColor(this->speciesToColor.value(set->label()));
}

// Turn off the animation once it's played, otherwise it replays any time the window changes size.
void WildMonChart::limitChartAnimation(QChart *chart) {
// Turn off the chart animation once it's played, otherwise it replays any time the window changes size.
// The animation only begins when it's first displayed, so we'll only ever consider the chart for the current tab,
// and when the tab changes we'll call this again.
void WildMonChart::limitChartAnimation() {
// Chart may be destroyed before the animation finishes
QPointer<QChart> safeChart = chart;
QPointer<QChart> chart;
if (ui->tabWidget->currentWidget() == ui->tabSpecies) {
chart = ui->chartView_SpeciesDistribution->chart();
} else if (ui->tabWidget->currentWidget() == ui->tabLevels) {
chart = ui->chartView_LevelDistribution->chart();
}

if (!chart || chart->animationOptions() == QChart::NoAnimation)
return;

// QChart has no signal for when the animation is finished, so we use a timer to stop the animation.
// It is technically possible to get the chart to freeze mid-animation by resizing the window after
// the timer starts but before it finishes, but 1. animations are short so this is difficult to do,
// and 2. the issue resolves if the window is resized afterwards, so it's probably fine.
QTimer::singleShot(chart->animationDuration() + 500, [safeChart] {
if (safeChart) safeChart->setAnimationOptions(QChart::NoAnimation);
QTimer::singleShot(chart->animationDuration(), Qt::PreciseTimer, [chart] {
if (chart) chart->setAnimationOptions(QChart::NoAnimation);
});
}

Expand Down

0 comments on commit a31a014

Please sign in to comment.