From d6274c0fffe9ff4d882882bb3dfeb8d3308b8765 Mon Sep 17 00:00:00 2001 From: Mark Liversedge Date: Sun, 28 Nov 2021 15:26:11 +0000 Subject: [PATCH] Cache pixelsize calculation in pixelSizeForFont .. train view resize performance was terrible because each time a telemetry item was resized it would recalculate the right pixel size for the font. this was only a problem in hidpi displays, but these are becoming more and more popular. --- src/Gui/Colors.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Gui/Colors.cpp b/src/Gui/Colors.cpp index 2628f768b..c537577af 100644 --- a/src/Gui/Colors.cpp +++ b/src/Gui/Colors.cpp @@ -42,12 +42,20 @@ QFont baseFont; // base font scaled to display (before user scal // find the right pixelSize for font and height int pixelSizeForFont(QFont &font, int height) { + static QMap maps; // cache as expensive to calculate + + int pixelsize = maps.value(height, 0); + if (pixelsize) return pixelsize; + QFont with = font; - int pixelsize=6; + pixelsize=6; do { with.setPixelSize(pixelsize+1); QFontMetrics fm(with); - if (fm.tightBoundingRect("Fy").height() > height) return pixelsize; + if (fm.tightBoundingRect("Fy").height() > height) { + maps.insert(height, pixelsize); + return pixelsize; + } else pixelsize++; } while (pixelsize<200); // should never loop that much