[calamares] Fit text into sidepane

- Shrink the font in an attempt to fit into the box,
   but only up to 4pt smaller; after that just clip on one line.
This commit is contained in:
Adriaan de Groot 2019-05-06 14:14:20 +02:00
parent 0f30acc13f
commit dc8cfd2fce

View File

@ -109,6 +109,32 @@ ProgressTreeDelegate::paintViewStep( QPainter* painter,
painter->setBrush( QColor( textHighlight ) );
}
painter->fillRect( option.rect, painter->brush().color() );
painter->drawText( textRect, Qt::AlignHCenter | Qt::AlignVCenter | Qt::TextSingleLine, index.data().toString() );
// Draw the text at least once. If it doesn't fit, then shrink the font
// being used by 1 pt on each iteration, up to a maximum of maximumShrink
// times. On each loop, we'll have to blank out the rectangle again, so this
// is an expensive (in terms of drawing operations) thing to do.
//
// (The loop uses <= because the counter is incremented at the start).
static constexpr int const maximumShrink = 4;
int shrinkSteps = 0;
do
{
painter->fillRect( option.rect, painter->brush().color() );
shrinkSteps++;
QRectF boundingBox;
painter->drawText( textRect, Qt::AlignHCenter | Qt::AlignVCenter | Qt::TextSingleLine, index.data().toString(), &boundingBox );
// The extra check here is to avoid the changing-font-size if we're not going to use
// it in the next iteration of the loop anyway.
if ( ( shrinkSteps <= maximumShrink ) && (boundingBox.width() > textRect.width() ) )
{
font.setPointSize( item_fontsize() - shrinkSteps );
painter->setFont( font );
}
else
break; // It fits
}
while ( shrinkSteps <= maximumShrink );
}