From 767eaa0a474b3c4cee68866527e11c87d892056d Mon Sep 17 00:00:00 2001 From: mappu Date: Mon, 19 Jun 2017 20:42:51 +1200 Subject: [PATCH] add fallback display for non-printable characters --- qbolt/mainwindow.cpp | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/qbolt/mainwindow.cpp b/qbolt/mainwindow.cpp index 209897e..67d3e33 100644 --- a/qbolt/mainwindow.cpp +++ b/qbolt/mainwindow.cpp @@ -91,7 +91,27 @@ void MainWindow::openDatabase(QString file, bool readOnly) static const QString getDisplayName(const QByteArray &qba) { // FIXME the formatting isn't so great when control characters, etc. are used // A C-style escape display, or the unicode-replacement-character would be preferable - return QString::fromUtf8(qba); + QString ret(QString::fromUtf8(qba)); + + bool allPrintable = true; + for (auto i = ret.begin(), e = ret.end(); i != e; ++i) { + if (! i->isPrint()) { + allPrintable = false; + break; + } + } + + if (allPrintable) { + return ret; // fine + } + + // Some of the characters weren't printable. + // Build up a replacement string + QString replacement; + for (auto i = ret.begin(), e = ret.end(); i != e; ++i) { + replacement += i->isPrint() ? *i : QStringLiteral("\\u{%1}").arg(i->unicode()); + } + return replacement; } void MainWindow::refreshBucketTree(QTreeWidgetItem* itm)