From d4a1decb73f3d887b12ed345220a3fddd204665a Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kittywhiskers@users.noreply.github.com> Date: Sun, 5 Dec 2021 18:45:22 +0530 Subject: [PATCH] merge bitcoin#16701: Replace functions deprecated in Qt 5.13 --- src/qt/bitcoinamountfield.cpp | 2 +- src/qt/bitcoingui.cpp | 8 ++++---- src/qt/guiutil.cpp | 12 ++++++++++-- src/qt/guiutil.h | 8 ++++++++ src/qt/optionsdialog.cpp | 2 +- src/qt/rpcconsole.cpp | 4 ++-- src/qt/splashscreen.cpp | 16 ++++++++-------- 7 files changed, 34 insertions(+), 18 deletions(-) diff --git a/src/qt/bitcoinamountfield.cpp b/src/qt/bitcoinamountfield.cpp index 58fe832452..eb0578afe6 100644 --- a/src/qt/bitcoinamountfield.cpp +++ b/src/qt/bitcoinamountfield.cpp @@ -117,7 +117,7 @@ public: ensurePolished(); const QFontMetrics fm(fontMetrics()); int h = 0; - int w = fm.width(BitcoinUnits::format(BitcoinUnits::DASH, BitcoinUnits::maxMoney(), false, BitcoinUnits::separatorAlways)); + int w = GUIUtil::TextWidth(fm, BitcoinUnits::format(BitcoinUnits::DASH, BitcoinUnits::maxMoney(), false, BitcoinUnits::separatorAlways)); w += 2; // cursor blinking space w += GUIUtil::dashThemeActive() ? 24 : 0; // counteract padding from css return QSize(w, h); diff --git a/src/qt/bitcoingui.cpp b/src/qt/bitcoingui.cpp index 14c7c86c9c..ceeed5e1b6 100644 --- a/src/qt/bitcoingui.cpp +++ b/src/qt/bitcoingui.cpp @@ -46,7 +46,6 @@ #include #include #include -#include #include #include #include @@ -54,6 +53,7 @@ #include #include #include +#include #include #include #include @@ -89,7 +89,7 @@ BitcoinGUI::BitcoinGUI(interfaces::Node& node, const NetworkStyle* networkStyle, QSettings settings; if (!restoreGeometry(settings.value("MainWindowGeometry").toByteArray())) { // Restore failed (perhaps missing setting), center the window - move(QApplication::desktop()->availableGeometry().center() - frameGeometry().center()); + move(QGuiApplication::primaryScreen()->availableGeometry().center() - frameGeometry().center()); } #ifdef ENABLE_WALLET @@ -1324,7 +1324,7 @@ void BitcoinGUI::updateWidth() continue; } QFontMetrics fm(button->font()); - nWidthWidestButton = std::max(nWidthWidestButton, fm.width(button->text())); + nWidthWidestButton = std::max(nWidthWidestButton, GUIUtil::TextWidth(fm, button->text())); ++nButtonsVisible; } // Add 30 per button as padding and use minimum 980 which is the minimum required to show all tab's contents @@ -1990,7 +1990,7 @@ UnitDisplayStatusBarControl::UnitDisplayStatusBarControl() : const QFontMetrics fm(GUIUtil::getFontNormal()); for (const BitcoinUnits::Unit unit : units) { - max_width = qMax(max_width, fm.width(BitcoinUnits::name(unit))); + max_width = qMax(max_width, GUIUtil::TextWidth(fm, BitcoinUnits::name(unit))); } setMinimumSize(max_width, 0); setAlignment(Qt::AlignRight | Qt::AlignVCenter); diff --git a/src/qt/guiutil.cpp b/src/qt/guiutil.cpp index 0aa06dd1ab..d541e10783 100644 --- a/src/qt/guiutil.cpp +++ b/src/qt/guiutil.cpp @@ -51,7 +51,6 @@ #include #include #include -#include #include #include #include @@ -1832,7 +1831,7 @@ qreal calculateIdealFontSize(int width, const QString& text, QFont font, qreal m while(font_size >= minPointSize) { font.setPointSizeF(font_size); QFontMetrics fm(font); - if (fm.width(text) < width) { + if (TextWidth(fm, text) < width) { break; } font_size -= 0.5; @@ -1872,4 +1871,13 @@ void PolishProgressDialog(QProgressDialog* dialog) #endif } +int TextWidth(const QFontMetrics& fm, const QString& text) +{ +#if (QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)) + return fm.horizontalAdvance(text); +#else + return fm.width(text); +#endif +} + } // namespace GUIUtil diff --git a/src/qt/guiutil.h b/src/qt/guiutil.h index dd6b4a9d89..425fe7791d 100644 --- a/src/qt/guiutil.h +++ b/src/qt/guiutil.h @@ -467,6 +467,14 @@ namespace GUIUtil // Fix known bugs in QProgressDialog class. void PolishProgressDialog(QProgressDialog* dialog); + + /** + * Returns the distance in pixels appropriate for drawing a subsequent character after text. + * + * In Qt 5.12 and before the QFontMetrics::width() is used and it is deprecated since Qt 13.0. + * In Qt 5.11 the QFontMetrics::horizontalAdvance() was introduced. + */ + int TextWidth(const QFontMetrics& fm, const QString& text); } // namespace GUIUtil #endif // BITCOIN_QT_GUIUTIL_H diff --git a/src/qt/optionsdialog.cpp b/src/qt/optionsdialog.cpp index 6f8f4c1a79..ad8cca2fd5 100644 --- a/src/qt/optionsdialog.cpp +++ b/src/qt/optionsdialog.cpp @@ -497,7 +497,7 @@ void OptionsDialog::updateWidth() continue; } QFontMetrics fm(button->font()); - nWidthWidestButton = std::max(nWidthWidestButton, fm.width(button->text())); + nWidthWidestButton = std::max(nWidthWidestButton, GUIUtil::TextWidth(fm, button->text())); ++nButtonsVisible; } // Add 10 per button as padding and use minimum 585 which is what we used in css before diff --git a/src/qt/rpcconsole.cpp b/src/qt/rpcconsole.cpp index 63f87b743f..54195c2ee1 100644 --- a/src/qt/rpcconsole.cpp +++ b/src/qt/rpcconsole.cpp @@ -31,12 +31,12 @@ #include #include -#include #include #include #include #include #include +#include #include #include #include @@ -466,7 +466,7 @@ RPCConsole::RPCConsole(interfaces::Node& node, QWidget* parent, Qt::WindowFlags QSettings settings; if (!restoreGeometry(settings.value("RPCConsoleWindowGeometry").toByteArray())) { // Restore failed (perhaps missing setting), center the window - move(QApplication::desktop()->availableGeometry().center() - frameGeometry().center()); + move(QGuiApplication::primaryScreen()->availableGeometry().center() - frameGeometry().center()); } QChar nonbreaking_hyphen(8209); diff --git a/src/qt/splashscreen.cpp b/src/qt/splashscreen.cpp index 64610f88d4..2e1a479d5a 100644 --- a/src/qt/splashscreen.cpp +++ b/src/qt/splashscreen.cpp @@ -9,22 +9,22 @@ #include -#include -#include #include #include #include #include #include +#include +#include #include #include #include #include #include -#include #include +#include SplashScreen::SplashScreen(interfaces::Node& node, Qt::WindowFlags f, const NetworkStyle *networkStyle) : @@ -89,7 +89,7 @@ SplashScreen::SplashScreen(interfaces::Node& node, Qt::WindowFlags f, const Netw fontBold.setPointSize(50 * fontFactor); pixPaint.setFont(fontBold); QFontMetrics fm = pixPaint.fontMetrics(); - int titleTextWidth = fm.width(titleText); + int titleTextWidth = GUIUtil::TextWidth(fm, titleText); if (titleTextWidth > width * 0.8) { fontFactor = 0.75; } @@ -97,14 +97,14 @@ SplashScreen::SplashScreen(interfaces::Node& node, Qt::WindowFlags f, const Netw fontBold.setPointSize(50 * fontFactor); pixPaint.setFont(fontBold); fm = pixPaint.fontMetrics(); - titleTextWidth = fm.width(titleText); + titleTextWidth = GUIUtil::TextWidth(fm, titleText); int titleTextHeight = fm.height(); pixPaint.drawText((width / 2) - (titleTextWidth / 2), titleTextHeight + paddingTop, titleText); fontNormal.setPointSize(16 * fontFactor); pixPaint.setFont(fontNormal); fm = pixPaint.fontMetrics(); - int versionTextWidth = fm.width(versionText); + int versionTextWidth = GUIUtil::TextWidth(fm, versionText); pixPaint.drawText((width / 2) - (versionTextWidth / 2), titleTextHeight + paddingTop + titleVersionVSpace, versionText); // draw additional text if special network @@ -112,7 +112,7 @@ SplashScreen::SplashScreen(interfaces::Node& node, Qt::WindowFlags f, const Netw fontBold.setPointSize(10 * fontFactor); pixPaint.setFont(fontBold); fm = pixPaint.fontMetrics(); - int titleAddTextWidth = fm.width(titleAddText); + int titleAddTextWidth = GUIUtil::TextWidth(fm, titleAddText); // Draw the badge background with the network-specific color QRect badgeRect = QRect(width - titleAddTextWidth - 20, 5, width, fm.height() + 10); QColor badgeColor = networkStyle->getBadgeColor(); @@ -128,7 +128,7 @@ SplashScreen::SplashScreen(interfaces::Node& node, Qt::WindowFlags f, const Netw QRect r(QPoint(), QSize(width, height)); resize(r.size()); setFixedSize(r.size()); - move(QApplication::desktop()->screenGeometry().center() - r.center()); + move(QGuiApplication::primaryScreen()->geometry().center() - r.center()); subscribeToCoreSignals(); installEventFilter(this);