From e68e34095057a9d7d7aedbaf8ad9773913aa4794 Mon Sep 17 00:00:00 2001 From: ctrlaltca Date: Sun, 24 Mar 2024 11:39:46 +0100 Subject: More qt6 porting (#2622) * Port QColor::setNamedColor to QColor::fromString * Port from QVariant::Type to QmetaType::Type * KviCoreActions: port from QAction::parentWidget to qobject_cast + QAction::parent * KviIrcView: port QString::fromUtf16 usage to the non-obsolete char16_t* overload * KviTopicWidget: port QMouseEvent to a non-obsolete overload * Port QMouseEvent from old pos(), x(), y(), to position() * Port obsolete QMessageBox usage to custom buttons * KviApplication: initialize the dbus notify message "replace ID" to 0 * KviIrcConnection: port deprecated QString::count() to QString::size() * KvsObject_*: port QColor::setNamedColor to QColor::fromString * KvsObject_widget: port QWidget::isTopLevel to QWidget::isWindow * KviMainWindow: use QKeyCombination instead of int for shortcuts * QHttp: avoid deprecated methods for QCryptographicHash * ScriptEditorWidget: use the new signature for QMenu::addAction() * KviInputEditor: Extract ACCEL_KEY from header and avoid warning * KviStringConversion: convert old Qt5 font weights to new "css" font weights * syntax fix--- src/kvilib/ext/KviStringConversion.cpp | 58 +++++++++++++++++++++++++++------- 1 file changed, 47 insertions(+), 11 deletions(-) (limited to 'src/kvilib/ext/KviStringConversion.cpp') diff --git a/src/kvilib/ext/KviStringConversion.cpp b/src/kvilib/ext/KviStringConversion.cpp index d16a25007..9400c489a 100644 --- a/src/kvilib/ext/KviStringConversion.cpp +++ b/src/kvilib/ext/KviStringConversion.cpp @@ -35,6 +35,7 @@ #include #include #include +#include QString g_szGlobalDir; QString g_szLocalDir; @@ -219,7 +220,11 @@ namespace KviStringConversion bool fromString(const QString & szValue, QColor & buffer) { +#if (QT_VERSION < QT_VERSION_CHECK(6, 4, 0)) buffer.setNamedColor(szValue); +#else + buffer = QColor::fromString(szValue); +#endif return true; } @@ -238,15 +243,37 @@ namespace KviStringConversion if(font.fixedPitch()) szOptions.append('f'); - szBuffer = QString::asprintf("%s,%d,%d,%d,%s,%s", szFamily.toUtf8().data(), font.pointSize(), font.styleHint(), -#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) - font.weight(), -#else - font.legacyWeight(), -#endif - szOptions.toUtf8().data(), - font.styleName().toUtf8().data() - ); + szBuffer = QString::asprintf("%s,%d,%d,%d,%s,%s", szFamily.toUtf8().data(), font.pointSize(), font.styleHint(), font.weight(), szOptions.toUtf8().data(), font.styleName().toUtf8().data()); + } + + /* Helper function to convert Qt < 6.0 font weight to OpenType font weight */ + static int fromLegacyWeight(int weight) + { + static constexpr std::array weightMap[] = { + { 0, QFont::Thin }, + { 12, QFont::ExtraLight }, + { 25, QFont::Light }, + { 50, QFont::Normal }, + { 57, QFont::Medium }, + { 63, QFont::DemiBold }, + { 75, QFont::Bold }, + { 81, QFont::ExtraBold }, + { 87, QFont::Black }, + }; + + int closestDist = INT_MAX; + int result = -1; + for (auto item: weightMap) { + const int dist = qAbs(item[0] - weight); + if (dist < closestDist) { + result = item[1]; + closestDist = dist; + } else { + break; + } + } + + return result; } bool fromString(const QString & szValue, QFont & buffer) @@ -269,12 +296,21 @@ namespace KviStringConversion if(bOk && (i >= 0)) buffer.setStyleHint((QFont::StyleHint)i); i = weight.toInt(&bOk); - if(bOk && (i >= 0)) + if(bOk && (i >= 0)) { #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) buffer.setWeight(i); #else - buffer.setLegacyWeight(i); + /* + * KVIrc <= 5.2.2 used Qt5 font weights (0 = thinner, 99 = bolder) + * Qt6 introduced opentype (css) font weight (100 = thinner, 900 = bolder) + * if the config is using an old weight, convert it + */ + if(i < 100) { + i = KviStringConversion::fromLegacyWeight(i); + } + buffer.setWeight(QFont::Weight(i)); #endif + } buffer.setBold(options.contains("b")); buffer.setItalic(options.contains("i")); buffer.setUnderline(options.contains("u")); -- cgit v1.3.1-10-gc9f91