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/def.h"
#include "../vmisc/literals.h" #include "../vmisc/literals.h"
#include "../vmisc/vcommonsettings.h" #include "../vmisc/vcommonsettings.h"
#include "../vmisc/compatibility.h"
#include "../vlayout/vprintlayout.h" #include "../vlayout/vprintlayout.h"
namespace namespace
@ -335,11 +336,7 @@ void VPTileFactory::DrawRuler(QPainter *painter) const
qreal unitsWidth = 0; qreal unitsWidth = 0;
QFontMetrics fm(fnt); QFontMetrics fm(fnt);
#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0) unitsWidth = TextWidth(fm, units);
unitsWidth = fm.horizontalAdvance(units);
#else
unitsWidth = fm.width(units);
#endif
painter->drawText(QPointF(step*0.5-unitsWidth*0.6, painter->drawText(QPointF(step*0.5-unitsWidth*0.6,
m_drawingAreaHeight - tileStripeWidth + notchHeight+shortNotchHeight), units); 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)); td.setPageSize(QSizeF(m_drawingAreaHeight - UnitConvertor(2, Unit::Cm, Unit::Px), m_drawingAreaWidth));
QFontMetrics metrix = QFontMetrics(td.defaultFont()); QFontMetrics metrix = QFontMetrics(td.defaultFont());
#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0) int maxWidth = TextWidth(metrix, QString().fill('z', 50));
int maxWidth = metrix.horizontalAdvance(QString().fill('z', 50));
#else
int maxWidth = metrix.width(QString().fill('z', 50));
#endif
QString clippedSheetName = metrix.elidedText(sheetName, Qt::ElideMiddle, maxWidth); QString clippedSheetName = metrix.elidedText(sheetName, Qt::ElideMiddle, maxWidth);
td.setHtml(QStringLiteral("<table width='100%' style='color:rgb(%1);'>" 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; QString qsText = tl.m_qsText;
#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0) if (TextWidth(fm, qsText) > dW)
if (fm.horizontalAdvance(qsText) > dW)
#else
if (fm.width(qsText) > dW)
#endif
{ {
qsText = fm.elidedText(qsText, Qt::ElideMiddle, static_cast<int>(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) else if ((tl.m_eAlign & Qt::AlignHCenter) > 0)
{ {
#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0) dX = (dW - TextWidth(fm, qsText))/2;
dX = (dW - fm.horizontalAdvance(qsText))/2;
#else
dX = (dW - fm.width(qsText))/2;
#endif
} }
else if ((tl.m_eAlign & Qt::AlignRight) > 0) else if ((tl.m_eAlign & Qt::AlignRight) > 0)
{ {
#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0) dX = dW - TextWidth(fm, qsText);
dX = dW - fm.horizontalAdvance(qsText);
#else
dX = dW - fm.width(qsText);
#endif
} }
// set up the rotation around top-left corner matrix // set up the rotation around top-left corner matrix

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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