From 7291a244161ddf77308413b5abad9def68e4415a Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Tue, 21 Feb 2017 09:10:47 +0100 Subject: QErrorMessage: use QStringBuilder Extract Method msgType2i18nString() and use it to build 'rich' in a single QStringBuilder expression. Replace the switch over QtMsgType with an array of QT_TRANSLATE_NOOP'ed strings. That introduces a dependency on the order and amount of enum values, so add static and dynamic asserts to catch any change. Saves memory allocations, as well as nearly 300B in text size on GCC 7 optimized Linux AMD64 builds. Change-Id: I48cc916cba283e482a90ca4ae28aa17b26a4e5ab Reviewed-by: David Faure --- src/widgets/dialogs/qerrormessage.cpp | 45 +++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 21 deletions(-) (limited to 'src') diff --git a/src/widgets/dialogs/qerrormessage.cpp b/src/widgets/dialogs/qerrormessage.cpp index 8200135abe..4ec4da6e1a 100644 --- a/src/widgets/dialogs/qerrormessage.cpp +++ b/src/widgets/dialogs/qerrormessage.cpp @@ -161,32 +161,35 @@ static void deleteStaticcQErrorMessage() // post-routine static bool metFatal = false; +static QString msgType2i18nString(QtMsgType t) +{ + Q_STATIC_ASSERT(QtDebugMsg == 0); + Q_STATIC_ASSERT(QtWarningMsg == 1); + Q_STATIC_ASSERT(QtCriticalMsg == 2); + Q_STATIC_ASSERT(QtFatalMsg == 3); + Q_STATIC_ASSERT(QtInfoMsg == 4); + + // adjust the array below if any of the above fire... + + const char * const messages[] = { + QT_TRANSLATE_NOOP("QErrorMessage", "Debug Message:"), + QT_TRANSLATE_NOOP("QErrorMessage", "Warning:"), + QT_TRANSLATE_NOOP("QErrorMessage", "Critical Error:"), + QT_TRANSLATE_NOOP("QErrorMessage", "Fatal Error:"), + QT_TRANSLATE_NOOP("QErrorMessage", "Information:"), + }; + Q_ASSERT(size_t(t) < sizeof messages / sizeof *messages); + + return QCoreApplication::translate("QErrorMessage", messages[t]); +} + static void jump(QtMsgType t, const QMessageLogContext & /*context*/, const QString &m) { if (!qtMessageHandler) return; - QString rich; - - switch (t) { - case QtDebugMsg: - rich = QErrorMessage::tr("Debug Message:"); - break; - case QtWarningMsg: - rich = QErrorMessage::tr("Warning:"); - break; - case QtCriticalMsg: - rich = QErrorMessage::tr("Critical Error:"); - break; - case QtFatalMsg: - rich = QErrorMessage::tr("Fatal Error:"); - break; - case QtInfoMsg: - rich = QErrorMessage::tr("Information:"); - break; - } - rich = QString::fromLatin1("

%1

").arg(rich); - rich += Qt::convertFromPlainText(m, Qt::WhiteSpaceNormal); + QString rich = QLatin1String("

") + msgType2i18nString(t) + QLatin1String("

") + + Qt::convertFromPlainText(m, Qt::WhiteSpaceNormal); // ### work around text engine quirk if (rich.endsWith(QLatin1String("

"))) -- cgit v1.2.3