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.
This commit is contained in:
Mark Liversedge
2021-11-28 15:26:11 +00:00
parent d6d969f819
commit d6274c0fff

View File

@@ -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<int,int> 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