[kde-doc-english] [trojita] src/Gui: GUI: respect KDE's settings for the monospace font in webkit and composer

Jan Kundrát jkt at flaska.net
Wed May 8 23:00:50 UTC 2013


Git commit cff3b867dbf40046ae4db9f0023b708b80eda46d by Jan Kundrát.
Committed on 09/05/2013 at 00:51.
Pushed by jkt into branch 'master'.

GUI: respect KDE's settings for the monospace font in webkit and composer

refs #547

M  +2    -6    src/Gui/ComposeWidget.cpp
M  +10   -1    src/Gui/SimplePartWidget.cpp
M  +57   -0    src/Gui/Util.cpp
M  +3    -0    src/Gui/Util.h

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

diff --git a/src/Gui/ComposeWidget.cpp b/src/Gui/ComposeWidget.cpp
index d3eae5a..2506e1b 100644
--- a/src/Gui/ComposeWidget.cpp
+++ b/src/Gui/ComposeWidget.cpp
@@ -39,6 +39,7 @@
 #include "LineEdit.h"
 #include "OverlayWidget.h"
 #include "ProgressPopUp.h"
+#include "Gui/Util.h"
 #include "Window.h"
 #include "ui_ComposeWidget.h"
 
@@ -104,12 +105,7 @@ ComposeWidget::ComposeWidget(MainWindow *mainWindow, MSA::MSAFactory *msaFactory
     m_recipientListUpdateTimer->setInterval(250);
     connect(m_recipientListUpdateTimer, SIGNAL(timeout()), SLOT(updateRecipientList()));
 
-    // Ask for a fixed-width 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
-    // let the Qt font substitution algorithm do its magic.
-    QFont font(QLatin1String("x-trojita-terminus-like-fixed-width"));
-    font.setStyleHint(QFont::TypeWriter);
-    ui->mailText->setFont(font);
+    ui->mailText->setFont(Gui::Util::systemMonospaceFont());
 
     connect(ui->mailText, SIGNAL(urlsAdded(QList<QUrl>)), SLOT(slotAttachFiles(QList<QUrl>)));
     connect(ui->mailText, SIGNAL(sendRequest()), SLOT(send()));
diff --git a/src/Gui/SimplePartWidget.cpp b/src/Gui/SimplePartWidget.cpp
index 1082c97..1fc4b03 100644
--- a/src/Gui/SimplePartWidget.cpp
+++ b/src/Gui/SimplePartWidget.cpp
@@ -97,6 +97,15 @@ void SimplePartWidget::slotMarkupPlainText() {
         "span.shortquote > blockquote > label {display: none}"
     );
 
+    QFontInfo monospaceInfo(Gui::Util::systemMonospaceFont());
+    QString fontSpecification(QLatin1String("pre{"));
+    if (monospaceInfo.italic())
+        fontSpecification += QLatin1String("font-style: italic; ");
+    if (monospaceInfo.bold())
+        fontSpecification += QLatin1String("font-weight: bold; ");
+    fontSpecification += QString::fromUtf8("font-size: %1px; font-family: \"%2\", monospace }").arg(
+                QString::number(monospaceInfo.pixelSize()), monospaceInfo.family());
+
     QPalette palette = QApplication::palette();
     QString textColors = QString::fromUtf8("body { background-color: %1; color: %2 }"
                                            "a:link { color: %3 } a:visited { color: %4 } a:hover { color: %3 }").arg(
@@ -125,7 +134,7 @@ void SimplePartWidget::slotMarkupPlainText() {
             file.close();
         }
     }
-    QString htmlHeader("<html><head><style type=\"text/css\"><!--" + stylesheet + textColors + "--></style></head><body><pre>");
+    QString htmlHeader("<html><head><style type=\"text/css\"><!--" + stylesheet + textColors + fontSpecification + "--></style></head><body><pre>");
     static QString htmlFooter("\n</pre></body></html>");
 
     QString markup = Composer::Util::plainTextToHtml(page()->mainFrame()->toPlainText(), flowedFormat);
diff --git a/src/Gui/Util.cpp b/src/Gui/Util.cpp
index a1ec6de..d800a49 100644
--- a/src/Gui/Util.cpp
+++ b/src/Gui/Util.cpp
@@ -24,9 +24,41 @@
 #include <QApplication>
 #include <QCursor> // for Util::centerWidgetOnScreen
 #include <QDesktopWidget> // for Util::centerWidgetOnScreen
+#include <QDir>
+#include <QSettings>
 
 #include "Util.h"
 
+namespace {
+
+#ifdef Q_WS_X11
+
+/** @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
+instead of yet another private class.
+*/
+QString kdeHome()
+{
+    static QString kdeHomePath;
+    if (kdeHomePath.isEmpty()) {
+        kdeHomePath = QString::fromLocal8Bit(qgetenv("KDEHOME"));
+        if (kdeHomePath.isEmpty()) {
+            QDir homeDir(QDir::homePath());
+            QString kdeConfDir(QLatin1String("/.kde"));
+            if (qgetenv("KDE_SESSION_VERSION") == "4" && homeDir.exists(QLatin1String(".kde4"))) {
+                kdeConfDir = QLatin1String("/.kde4");
+            }
+            kdeHomePath = QDir::homePath() + kdeConfDir;
+        }
+    }
+    return kdeHomePath;
+}
+
+#endif
+
+}
+
 namespace Gui
 {
 
@@ -92,6 +124,31 @@ QColor tintColor(const QColor &color, const QColor &tintColor)
     return finalColor;
 }
 
+
+/** @short Return the monospace font according to the systemwide settings */
+QFont systemMonospaceFont()
+{
+    QString fontDescription;
+    QFont font;
+#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();
+#endif
+    if (fontDescription.isEmpty() || !font.fromString(fontDescription)) {
+        // 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
+        // let the Qt font substitution algorithm do its magic.
+        font = QFont(QLatin1String("x-trojita-terminus-like-fixed-width"));
+        font.setStyleHint(QFont::TypeWriter);
+    }
+    return font;
+}
+
 } // namespace Util
 
 } // namespace Gui
diff --git a/src/Gui/Util.h b/src/Gui/Util.h
index 9e68c92..6570566 100644
--- a/src/Gui/Util.h
+++ b/src/Gui/Util.h
@@ -27,6 +27,7 @@
 #include <QString>
 
 class QColor;
+class QFont;
 class QWidget;
 
 namespace Gui
@@ -42,6 +43,8 @@ QString pkgDataDir();
 
 QColor tintColor(const QColor &color, const QColor &tintColor);
 
+QFont systemMonospaceFont();
+
 } // namespace Util
 
 } // namespace Gui



More information about the kde-doc-english mailing list