From 0e99527e8c0868f63c7319bf7ac7b56f23af9935 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82?= Date: Sat, 13 Mar 2021 14:47:02 +0100 Subject: [PATCH] Fixed deprecated method QFontMetrics::width (#2509) Co-authored-by: pajlada --- src/messages/MessageElement.cpp | 14 ++++++++------ src/messages/layouts/MessageLayoutContainer.cpp | 4 ++-- src/messages/layouts/MessageLayoutElement.cpp | 6 +++--- src/widgets/Label.cpp | 5 +++-- src/widgets/helper/NotebookTab.cpp | 6 +++--- 5 files changed, 19 insertions(+), 16 deletions(-) diff --git a/src/messages/MessageElement.cpp b/src/messages/MessageElement.cpp index 211404a42..1f5fa0a3d 100644 --- a/src/messages/MessageElement.cpp +++ b/src/messages/MessageElement.cpp @@ -320,7 +320,7 @@ void TextElement::addToContainer(MessageLayoutContainer &container, // fourtf: add again // if (word.width == -1) { - word.width = metrics.width(word.text); + word.width = metrics.horizontalAdvance(word.text); // } // see if the text fits in the current line @@ -357,8 +357,9 @@ void TextElement::addToContainer(MessageLayoutContainer &container, auto isSurrogate = text.size() > i + 1 && QChar::isHighSurrogate(text[i].unicode()); - auto charWidth = isSurrogate ? metrics.width(text.mid(i, 2)) - : metrics.width(text[i]); + auto charWidth = isSurrogate + ? metrics.horizontalAdvance(text.mid(i, 2)) + : metrics.horizontalAdvance(text[i]); if (!container.fitsInLine(width + charWidth)) { @@ -585,7 +586,7 @@ void IrcTextElement::addToContainer(MessageLayoutContainer &container, // fourtf: add again // if (word.width == -1) { - word.width = metrics.width(word.text); + word.width = metrics.horizontalAdvance(word.text); // } // see if the text fits in the current line @@ -632,8 +633,9 @@ void IrcTextElement::addToContainer(MessageLayoutContainer &container, auto isSurrogate = text.size() > i + 1 && QChar::isHighSurrogate(text[i].unicode()); - auto charWidth = isSurrogate ? metrics.width(text.mid(i, 2)) - : metrics.width(text[i]); + auto charWidth = isSurrogate + ? metrics.horizontalAdvance(text.mid(i, 2)) + : metrics.horizontalAdvance(text[i]); if (!container.fitsInLine(width + charWidth)) { diff --git a/src/messages/layouts/MessageLayoutContainer.cpp b/src/messages/layouts/MessageLayoutContainer.cpp index 2a69431cd..6c0096b50 100644 --- a/src/messages/layouts/MessageLayoutContainer.cpp +++ b/src/messages/layouts/MessageLayoutContainer.cpp @@ -43,8 +43,8 @@ void MessageLayoutContainer::begin(int width, float scale, MessageFlags flags) auto mediumFontMetrics = getApp()->fonts->getFontMetrics(FontStyle::ChatMedium, scale); this->textLineHeight_ = mediumFontMetrics.height(); - this->spaceWidth_ = mediumFontMetrics.width(' '); - this->dotdotdotWidth_ = mediumFontMetrics.width("..."); + this->spaceWidth_ = mediumFontMetrics.horizontalAdvance(' '); + this->dotdotdotWidth_ = mediumFontMetrics.horizontalAdvance("..."); this->canAddMessages_ = true; this->isCollapsed_ = false; } diff --git a/src/messages/layouts/MessageLayoutElement.cpp b/src/messages/layouts/MessageLayoutElement.cpp index 34e9dcbb4..3eab41f41 100644 --- a/src/messages/layouts/MessageLayoutElement.cpp +++ b/src/messages/layouts/MessageLayoutElement.cpp @@ -270,7 +270,7 @@ int TextLayoutElement::getMouseOverIndex(const QPoint &abs) const for (auto i = 0; i < this->getText().size(); i++) { auto &&text = this->getText(); - auto width = metrics.width(this->getText()[i]); + auto width = metrics.horizontalAdvance(this->getText()[i]); if (x + width > abs.x()) { @@ -309,7 +309,7 @@ int TextLayoutElement::getXFromIndex(int index) int x = 0; for (int i = 0; i < index; i++) { - x += metrics.width(this->getText()[i]); + x += metrics.horizontalAdvance(this->getText()[i]); } return x + this->getRect().left(); } @@ -429,7 +429,7 @@ void MultiColorTextLayoutElement::paint(QPainter &painter) this->getRect().y(), 10000, 10000), segment.text, QTextOption(Qt::AlignLeft | Qt::AlignTop)); - xOffset += metrics.width(segment.text); + xOffset += metrics.horizontalAdvance(segment.text); } } diff --git a/src/widgets/Label.cpp b/src/widgets/Label.cpp index 5b2ab1891..e61a32b60 100644 --- a/src/widgets/Label.cpp +++ b/src/widgets/Label.cpp @@ -106,7 +106,7 @@ void Label::paintEvent(QPaintEvent *) // draw text QRect textRect(offset, 0, this->width() - offset - offset, this->height()); - int width = metrics.width(this->text_); + int width = metrics.horizontalAdvance(this->text_); Qt::Alignment alignment = !this->centered_ || width > textRect.width() ? Qt::AlignLeft | Qt::AlignVCenter : Qt::AlignCenter; @@ -128,7 +128,8 @@ void Label::updateSize() QFontMetrics metrics = getFonts()->getFontMetrics(this->fontStyle_, this->scale()); - int width = metrics.width(this->text_) + (2 * this->getOffset()); + int width = + metrics.horizontalAdvance(this->text_) + (2 * this->getOffset()); int height = metrics.height(); this->preferedSize_ = QSize(width, height); diff --git a/src/widgets/helper/NotebookTab.cpp b/src/widgets/helper/NotebookTab.cpp index 5aa42eedc..56cba6732 100644 --- a/src/widgets/helper/NotebookTab.cpp +++ b/src/widgets/helper/NotebookTab.cpp @@ -119,11 +119,11 @@ int NotebookTab::normalTabWidth() if (this->hasXButton()) { - width = (metrics.width(this->getTitle()) + int(32 * scale)); + width = (metrics.horizontalAdvance(this->getTitle()) + int(32 * scale)); } else { - width = (metrics.width(this->getTitle()) + int(16 * scale)); + width = (metrics.horizontalAdvance(this->getTitle()) + int(16 * scale)); } if (this->height() > 150 * scale) @@ -390,7 +390,7 @@ void NotebookTab::paintEvent(QPaintEvent *) textRect.setRight(textRect.right() - this->height() / 2); } - int width = metrics.width(this->getTitle()); + int width = metrics.horizontalAdvance(this->getTitle()); Qt::Alignment alignment = width > textRect.width() ? Qt::AlignLeft | Qt::AlignVCenter : Qt::AlignHCenter | Qt::AlignVCenter;