void Label::setText(const QString& newText)
{
// Get label font
QFont labelFont
= font
();
// Check is font pixel size is initialized, if not initialize it
if (myFontPixelSize <= 0)
{
myFontPixelSize = labelFont.pixelSize();
}
// Get text for testing, remove italic flags and replace line breaks with a new line
testText.
replace(QRegExp("<i[^>]*>", Qt
::CaseInsensitive),
"");
testText.
replace(QRegExp("</i[^>]*>", Qt
::CaseInsensitive),
"");
testText.
replace(QRegExp("<br[^>]*>", Qt
::CaseInsensitive),
"\n");
// Get saved font size
int fontPixelSize = myFontPixelSize;
// If font size if larger than zero, proceed
if (fontPixelSize > 0)
{
// Initialize the 'fit' flag to false
bool fit = false;
// Set test pixel size
labelFont.setPixelSize(fontPixelSize);
// Get alignement flags
int flags = alignment();
// If we support word wrap, add it
if (wordWrap() == true)
{
flags |= Qt::TextWordWrap;
}
// Test until we can fit with regular font pixel size
while (fit == false && fontPixelSize > 0)
{
// Set pixel size
labelFont.setPixelSize(fontPixelSize);
// Font metrics
// Get bounding rect, we need to take into account 2 vertical and 2 horizontal margins
QRect bound
= fm.
boundingRect(0,
0, contentsRect
().
width() - 2 * margin
(),
contentsRect().height() - 2 * margin(), flags, testText);
// Check bounding rect width and height, we need to take into account 2 vertical and 2 horizontal margins
if ((bound.width() <= contentsRect().width() - 2 * margin()) &&
(bound.height() <= contentsRect().height() - 2 * margin()))
{
// We can now fit
fit = true;
}
else
{
// Decrease font pixel size
fontPixelSize--;
}
}
// Set last font
setFont(labelFont);
}
// Set new text
}
void Label::setText(const QString& newText)
{
// Get label font
QFont labelFont = font();
// Check is font pixel size is initialized, if not initialize it
if (myFontPixelSize <= 0)
{
myFontPixelSize = labelFont.pixelSize();
}
// Get text for testing, remove italic flags and replace line breaks with a new line
QString testText = newText;
testText.replace(QRegExp("<i[^>]*>", Qt::CaseInsensitive), "");
testText.replace(QRegExp("</i[^>]*>", Qt::CaseInsensitive), "");
testText.replace(QRegExp("<br[^>]*>", Qt::CaseInsensitive), "\n");
// Get saved font size
int fontPixelSize = myFontPixelSize;
// If font size if larger than zero, proceed
if (fontPixelSize > 0)
{
// Initialize the 'fit' flag to false
bool fit = false;
// Set test pixel size
labelFont.setPixelSize(fontPixelSize);
// Get alignement flags
int flags = alignment();
// If we support word wrap, add it
if (wordWrap() == true)
{
flags |= Qt::TextWordWrap;
}
// Test until we can fit with regular font pixel size
while (fit == false && fontPixelSize > 0)
{
// Set pixel size
labelFont.setPixelSize(fontPixelSize);
// Font metrics
QFontMetrics fm(labelFont);
// Get bounding rect, we need to take into account 2 vertical and 2 horizontal margins
QRect bound = fm.boundingRect(0, 0, contentsRect().width() - 2 * margin(),
contentsRect().height() - 2 * margin(), flags, testText);
// Check bounding rect width and height, we need to take into account 2 vertical and 2 horizontal margins
if ((bound.width() <= contentsRect().width() - 2 * margin()) &&
(bound.height() <= contentsRect().height() - 2 * margin()))
{
// We can now fit
fit = true;
}
else
{
// Decrease font pixel size
fontPixelSize--;
}
}
// Set last font
setFont(labelFont);
}
// Set new text
QLabel::setText(newText);
}
To copy to clipboard, switch view to plain text mode
Bookmarks