limit label width, remove apply/saveSpeciesColors

This commit is contained in:
cawtds
2026-07-31 17:26:28 +02:00
parent ffe1dd901b
commit 542404a08b
4 changed files with 15 additions and 24 deletions

View File

@@ -25,6 +25,7 @@ public:
static constexpr int IconVerticalOffset = -2;
static constexpr int Spacing = 6;
static constexpr int Padding = 10;
static constexpr int MaxLabelWidth = 230;
explicit SpeciesChartView(QWidget *parent = nullptr);
void setSpecies(const QStringList &species);

View File

@@ -5,11 +5,11 @@
#include <QWidget>
class Project;
#if __has_include(<QtCharts>)
#include <QtCharts>
class Project;
namespace Ui {
class WildMonChart;
}
@@ -49,8 +49,6 @@ private:
typedef QMap<QString, Summary> GroupedData;
QMap<QString, GroupedData> speciesToGroupedData;
QMap<QString, QColor> speciesToColor;
QStringList getSpeciesNamesAlphabetical() const;
double getSpeciesFrequency(const QString&, const QString&) const;
@@ -66,8 +64,6 @@ private:
void refreshSpeciesDistributionChart();
void refreshLevelDistributionChart();
void saveSpeciesColors(const QList<QBarSet*> &);
void applySpeciesColors(const QList<QBarSet*> &);
QChart::ChartTheme currentTheme() const;
void updateTheme();
void limitChartAnimation();
@@ -77,8 +73,6 @@ private:
#else
class Project;
class WildMonChart : public QWidget
{
Q_OBJECT

View File

@@ -23,7 +23,8 @@ void SpeciesChartView::setSpecies(const QStringList &species)
{
m_maxTextWidth = std::max(m_maxTextWidth, fm.horizontalAdvance(name));
QPixmap icon = m_project->getSpeciesIcon(speciesPrefix + name);
QPixmap icon;
if (m_project) icon = m_project->getSpeciesIcon(speciesPrefix + name);
m_speciesEntries.push_back({
name,
@@ -35,6 +36,10 @@ void SpeciesChartView::setSpecies(const QStringList &species)
});
}
if (m_maxTextWidth > MaxLabelWidth) {
m_maxTextWidth = MaxLabelWidth;
}
viewport()->update();
}

View File

@@ -78,7 +78,6 @@ void WildMonChart::clearTableData() {
this->tableIndexToGroupName.clear();
this->groupedLevelRanges.clear();
this->speciesToGroupedData.clear();
this->speciesToColor.clear();
setWindowTitle(baseWindowTitle);
const QSignalBlocker blocker1(ui->comboBox_Species);
@@ -303,13 +302,18 @@ QChart* WildMonChart::createSpeciesDistributionChart() {
int maxTextWidth = 0;
for (const QString &category : categories) {
maxTextWidth = std::max(maxTextWidth, fm.horizontalAdvance(category));
if (maxTextWidth > SpeciesChartView::MaxLabelWidth) {
maxTextWidth = SpeciesChartView::MaxLabelWidth;
break;
}
}
int labelWidth = maxTextWidth + SpeciesChartView::IconSize + SpeciesChartView::Spacing;
int leftMargin = labelWidth + 2 * SpeciesChartView::Padding;
chart->setMargins(QMargins(leftMargin, 20, 20, 20));
// X-axis is the % frequency. We're already showing percentages on the bar, so we just display 0/50/100%
// X-axis is the % frequency.
auto axisX = new QValueAxis();
axisX->setRange(0, 100);
axisX->setLabelFormat("%u%%");
@@ -435,20 +439,7 @@ void WildMonChart::updateTheme() {
if (!chart || chart->series().isEmpty())
return;
saveSpeciesColors(static_cast<QAbstractBarSeries*>(chart->series().at(0))->barSets());
chart->setTheme(theme);
applySpeciesColors(static_cast<QAbstractBarSeries*>(chart->series().at(0))->barSets());
}
void WildMonChart::saveSpeciesColors(const QList<QBarSet*> &barSets) {
this->speciesToColor.clear();
for (auto set : barSets)
this->speciesToColor.insert(set->label(), set->color());
}
void WildMonChart::applySpeciesColors(const QList<QBarSet*> &barSets) {
for (auto set : barSets)
set->setColor(this->speciesToColor.value(set->label()));
}
// Turn off the chart animation once it's played, otherwise it replays any time the window changes size.