[kde-doc-english] [trojita] src/Gui: GUI: try to detect Gnome font preferences as well and cache the result

Jan Kundrát jkt at flaska.net
Thu May 9 11:37:30 UTC 2013


Git commit d97e9ac219837133203491bd860e3432cccd62e0 by Jan Kundrát.
Committed on 09/05/2013 at 13:36.
Pushed by jkt into branch 'master'.

GUI: try to detect Gnome font preferences as well and cache the result

Thanks to Caspar Schutijser (cschutijser on IRC) for reporting a regression
in commit cff3b86 on his system.

M  +55   -9    src/Gui/Util.cpp

http://commits.kde.org/trojita/d97e9ac219837133203491bd860e3432cccd62e0

diff --git a/src/Gui/Util.cpp b/src/Gui/Util.cpp
index d800a49..51a4e5c 100644
--- a/src/Gui/Util.cpp
+++ b/src/Gui/Util.cpp
@@ -25,6 +25,7 @@
 #include <QCursor> // for Util::centerWidgetOnScreen
 #include <QDesktopWidget> // for Util::centerWidgetOnScreen
 #include <QDir>
+#include <QProcess>
 #include <QSettings>
 
 #include "Util.h"
@@ -33,6 +34,16 @@ namespace {
 
 #ifdef Q_WS_X11
 
+bool isRunningKde4()
+{
+    return qgetenv("KDE_SESSION_VERSION") == "4";
+}
+
+bool isRunningGnome()
+{
+    return qgetenv("DESKTOP_SESSION") == "gnome";
+}
+
 /** @short Return full path to the $KDEHOME
 
 Shamelessly stolen from Qt4's src/gui/kernel/qkde.cpp (it's a private class) and adopted to check $KDE_SESSION_VERSION
@@ -128,17 +139,51 @@ QColor tintColor(const QColor &color, const QColor &tintColor)
 /** @short Return the monospace font according to the systemwide settings */
 QFont systemMonospaceFont()
 {
-    QString fontDescription;
-    QFont font;
+    static bool initialized = false;
+    static QFont font;
+
+    if (!initialized) {
 #ifdef Q_WS_X11
-    // This part was shamelessly inspired by Qt4's src/gui/kernel/qapplication_x11.cpp
-    QSettings kdeSettings(::kdeHome() + QLatin1String("/share/config/kdeglobals"), QSettings::IniFormat);
-    QLatin1String confKey("fixed");
-    fontDescription = kdeSettings.value(confKey).toStringList().join(QLatin1String(","));
-    if (fontDescription.isEmpty())
-        fontDescription = kdeSettings.value(confKey).toString();
+        if (isRunningKde4()) {
+            // This part was shamelessly inspired by Qt4's src/gui/kernel/qapplication_x11.cpp
+            QSettings kdeSettings(::kdeHome() + QLatin1String("/share/config/kdeglobals"), QSettings::IniFormat);
+            QLatin1String confKey("fixed");
+            QString fontDescription = kdeSettings.value(confKey).toStringList().join(QLatin1String(","));
+            if (fontDescription.isEmpty())
+                fontDescription = kdeSettings.value(confKey).toString();
+            initialized = !fontDescription.isEmpty() && font.fromString(fontDescription);
+        } else if (isRunningGnome()) {
+            // Under Gnome, we can read the preferred font in yet another format via gconftool-2
+            QByteArray fontDescription;
+            do {
+                QProcess gconf;
+                gconf.start(QLatin1String("gconftool-2"),
+                            QStringList() << QLatin1String("--get") << QLatin1String("/desktop/gnome/interface/monospace_font_name"));
+                if (!gconf.waitForStarted())
+                    break;
+                gconf.closeWriteChannel();
+                if (!gconf.waitForFinished())
+                    break;
+                fontDescription = gconf.readAllStandardOutput();
+            } while (0);
+
+            // This value is apparently supposed to be parsed via the pango_font_description_from_string function. We, of course,
+            // do not link with Pango, so we attempt to do a very crude parsing experiment by hand.
+            // This code does *not* handle many fancy options like specifying the font size in pixels, parsing the bold/italics
+            // options etc. It will also very likely break when the user has specified preefrences for more than one font family.
+            // However, I hope it's better to try to do something than ignoring the problem altogether.
+            int lastSpace = fontDescription.lastIndexOf(' ');
+            bool ok;
+            double size = fontDescription.mid(lastSpace).toDouble(&ok);
+            if (lastSpace > 0 && ok) {
+                font = QFont(fontDescription.left(lastSpace), size);
+                initialized = true;
+            }
+        }
 #endif
-    if (fontDescription.isEmpty() || !font.fromString(fontDescription)) {
+    }
+
+    if (!initialized) {
         // Ok, that failed, let's create some fallback font.
         // The problem is that these names wary acros platforms,
         // but the following works well -- at first, we come up with a made-up name, and then
@@ -146,6 +191,7 @@ QFont systemMonospaceFont()
         font = QFont(QLatin1String("x-trojita-terminus-like-fixed-width"));
         font.setStyleHint(QFont::TypeWriter);
     }
+
     return font;
 }
 


More information about the kde-doc-english mailing list