Refactoring. Use TextWidth to simplify code.

This commit is contained in:
Roman Telezhynskyi 2022-02-19 21:26:30 +02:00
parent 8837bec33d
commit 19f95c8e54
8 changed files with 18 additions and 65 deletions

View File

@ -9,6 +9,7 @@
#include "../vmisc/def.h"
#include "../vmisc/literals.h"
#include "../vmisc/vcommonsettings.h"
#include "../vmisc/compatibility.h"
#include "../vlayout/vprintlayout.h"
namespace
@ -335,11 +336,7 @@ void VPTileFactory::DrawRuler(QPainter *painter) const
qreal unitsWidth = 0;
QFontMetrics fm(fnt);
#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
unitsWidth = fm.horizontalAdvance(units);
#else
unitsWidth = fm.width(units);
#endif
unitsWidth = TextWidth(fm, units);
painter->drawText(QPointF(step*0.5-unitsWidth*0.6,
m_drawingAreaHeight - tileStripeWidth + notchHeight+shortNotchHeight), units);
}
@ -623,11 +620,7 @@ void VPTileFactory::DrawTextInformation(QPainter *painter, int row, int col, int
td.setPageSize(QSizeF(m_drawingAreaHeight - UnitConvertor(2, Unit::Cm, Unit::Px), m_drawingAreaWidth));
QFontMetrics metrix = QFontMetrics(td.defaultFont());
#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
int maxWidth = metrix.horizontalAdvance(QString().fill('z', 50));
#else
int maxWidth = metrix.width(QString().fill('z', 50));
#endif
int maxWidth = TextWidth(metrix, QString().fill('z', 50));
QString clippedSheetName = metrix.elidedText(sheetName, Qt::ElideMiddle, maxWidth);
td.setHtml(QStringLiteral("<table width='100%' style='color:rgb(%1);'>"

View File

@ -1529,11 +1529,7 @@ void VLayoutPiece::CreateLabelStrings(QGraphicsItem *parent, const QVector<QPoin
}
QString qsText = tl.m_qsText;
#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
if (fm.horizontalAdvance(qsText) > dW)
#else
if (fm.width(qsText) > dW)
#endif
if (TextWidth(fm, qsText) > dW)
{
qsText = fm.elidedText(qsText, Qt::ElideMiddle, static_cast<int>(dW));
}
@ -1545,19 +1541,11 @@ void VLayoutPiece::CreateLabelStrings(QGraphicsItem *parent, const QVector<QPoin
}
else if ((tl.m_eAlign & Qt::AlignHCenter) > 0)
{
#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
dX = (dW - fm.horizontalAdvance(qsText))/2;
#else
dX = (dW - fm.width(qsText))/2;
#endif
dX = (dW - TextWidth(fm, qsText))/2;
}
else if ((tl.m_eAlign & Qt::AlignRight) > 0)
{
#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
dX = dW - fm.horizontalAdvance(qsText);
#else
dX = dW - fm.width(qsText);
#endif
dX = dW - TextWidth(fm, qsText);
}
// set up the rotation around top-left corner matrix

View File

@ -48,6 +48,7 @@
#include "../vmisc/vmath.h"
#include "../vmisc/def.h"
#include "../vmisc/vabstractvalapplication.h"
#include "../vmisc/compatibility.h"
#include "../ifc/exception/vexception.h"
namespace
@ -494,11 +495,7 @@ void VPoster::Ruler(QVector<QGraphicsItem *> &data, QGraphicsItem *parent, QRect
qreal unitsWidth = 0;
QFontMetrics fm(fnt);
#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
unitsWidth = fm.horizontalAdvance(units->toPlainText());
#else
unitsWidth = fm.width(units->toPlainText());
#endif
unitsWidth = TextWidth(fm, units->toPlainText());
units->setPos(rec.x() + step*0.5-unitsWidth*0.7,
rec.y() + rec.height()-static_cast<int>(allowance)-shortNotchHeight);
units->setFont(fnt);

View File

@ -552,11 +552,7 @@ void VTextManager::FitFontSize(qreal fW, qreal fH)
fnt.setBold(tl.m_bold);
fnt.setItalic(tl.m_italic);
QFontMetrics fm(fnt);
#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
const int iTW = fm.horizontalAdvance(tl.m_qsText);
#else
const int iTW = fm.width(tl.m_qsText);
#endif
const int iTW = TextWidth(fm, tl.m_qsText);
if (iTW > iMaxLen)
{
iMaxLen = iTW;
@ -575,11 +571,7 @@ void VTextManager::FitFontSize(qreal fW, qreal fH)
--iFS;
fnt.setPixelSize(iFS + maxLine.m_iFontSize);
QFontMetrics fm(fnt);
#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
lineLength = fm.horizontalAdvance(maxLine.m_qsText);
#else
lineLength = fm.width(maxLine.m_qsText);
#endif
lineLength = TextWidth(fm, maxLine.m_qsText);
}
while (lineLength > fW && iFS > MIN_FONT_SIZE);
}

View File

@ -19,6 +19,7 @@
*************************************************************************/
#include "vtextproperty.h"
#include "../vproperty_p.h"
#include "../vmisc/compatibility.h"
#include <QPlainTextEdit>
#include <QTextEdit>
@ -39,11 +40,7 @@ void SetTabStopDistance(QPlainTextEdit *edit, int tabWidthChar)
#else
// compute the size of a char in double-precision
static constexpr int bigNumber = 1000; // arbitrary big number.
#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
const int many_char_width = fontMetrics.horizontalAdvance(testString.repeated(bigNumber));
#else
const int many_char_width = fontMetrics.width(testString.repeated(bigNumber));
#endif
const int many_char_width = TextWidth(fontMetrics, testString.repeated(bigNumber));
const double singleCharWidthDouble = many_char_width / double(bigNumber);
// set the tab stop with double precision
edit->setTabStopDistance(tabWidthChar * singleCharWidthDouble);

View File

@ -566,11 +566,7 @@ void SetTabStopDistance(QPlainTextEdit *edit, int tabWidthChar)
#else
// compute the size of a char in double-precision
static constexpr int bigNumber = 1000; // arbitrary big number.
#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
const int many_char_width = fontMetrics.horizontalAdvance(testString.repeated(bigNumber));
#else
const int many_char_width = fontMetrics.width(testString.repeated(bigNumber));
#endif
const int many_char_width = TextWidth(fontMetrics, testString.repeated(bigNumber));
const double singleCharWidthDouble = many_char_width / double(bigNumber);
// set the tab stop with double precision
edit->setTabStopDistance(tabWidthChar * singleCharWidthDouble);

View File

@ -29,6 +29,7 @@
#include "fancytabbar.h"
#include "stylehelper.h"
#include "../vmisc/compatibility.h"
#include <QMouseEvent>
#include <QPainter>
@ -92,11 +93,7 @@ QSize FancyTabBar::TabSizeHint(bool minimum) const
{
sentence = sentence.isEmpty() ? sentence = word : sentence + QLatin1Char(' ') + word;
#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
const int width = fm.horizontalAdvance(sentence);
#else
const int width = fm.width(sentence);
#endif
const int width = TextWidth(fm, sentence);
if (maxLabelwidth < width)
{
maxLabelwidth = width;
@ -106,11 +103,7 @@ QSize FancyTabBar::TabSizeHint(bool minimum) const
}
else
{
#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
const int width = fm.horizontalAdvance(tabText);
#else
const int width = fm.width(tabText);
#endif
const int width = TextWidth(fm, tabText);
if (width > maxLabelwidth)
{
maxLabelwidth = width;

View File

@ -42,6 +42,7 @@
#include "../vmisc/def.h"
#include "../vmisc/vmath.h"
#include "../vmisc/literals.h"
#include "../vmisc/compatibility.h"
#include "vtextgraphicsitem.h"
const qreal resizeSquare = (3./*mm*/ / 25.4) * PrintDPI;
@ -177,11 +178,7 @@ void VTextGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem
break;
}
#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
if (fm.horizontalAdvance(qsText) > iW)
#else
if (fm.width(qsText) > iW)
#endif
if (TextWidth(fm, qsText) > iW)
{
qsText = fm.elidedText(qsText, Qt::ElideMiddle, iW);
}