calc result: remove wordwrap, instead decrea^C/increase font size as necessary

This commit is contained in:
Albert S. 2019-06-01 12:17:28 +02:00
parent 8484e88a27
commit 45abb2bf27
1 changed files with 23 additions and 2 deletions

View File

@ -38,7 +38,6 @@ Window::Window(const QVector<EntryConfig> &configs)
calculationResultLabel.setFont(font);
calculationResultLabel.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
calculationResultLabel.setAlignment(Qt::AlignCenter);
calculationResultLabel.setWordWrap(true);
calculationResultLabel.setContextMenuPolicy(Qt::ContextMenuPolicy::CustomContextMenu);
connect(&calculationResultLabel, &QLabel::customContextMenuRequested, this, &Window::showCalculationResultContextMenu);
@ -164,8 +163,30 @@ void Window::addCalcResult(const QString &expression)
{
clearGrid();
currentCalculationResult = calcEngine.evaluate(expression);
calculationResultLabel.setText(expression + ": " + currentCalculationResult);
QString labelText = expression + ": " + currentCalculationResult;
calculationResultLabel.setText(labelText);
calculationResultLabel.setVisible(true);
QFont currentFont = calculationResultLabel.font();
int calculatedPointSize = currentFont.pointSize();
QFontMetrics fm(currentFont);
int contentWidth = calculationResultLabel.contentsRect().width() - calculationResultLabel.margin();
while(calculatedPointSize < 48 && fm.boundingRect(labelText).width() < contentWidth)
{
calculatedPointSize += 1;
currentFont.setPointSize(calculatedPointSize);
fm = QFontMetrics(currentFont);
}
while(fm.boundingRect(labelText).width() >= contentWidth)
{
calculatedPointSize -= 1;
currentFont.setPointSize(calculatedPointSize);
fm = QFontMetrics(currentFont);
}
calculationResultLabel.setFont(currentFont);
grid->addWidget(&calculationResultLabel, 0, 0);
}