]> git.lyx.org Git - lyx.git/commitdiff
Fix alignment of screen font preview in prefs (remaining part of #13046)
authorJuergen Spitzmueller <spitz@lyx.org>
Sun, 24 Mar 2024 08:00:41 +0000 (09:00 +0100)
committerJuergen Spitzmueller <spitz@lyx.org>
Sun, 24 Mar 2024 08:00:41 +0000 (09:00 +0100)
src/frontends/qt/GuiFontExample.cpp
src/frontends/qt/GuiFontExample.h
src/frontends/qt/GuiPrefs.cpp
src/frontends/qt/GuiPrefs.h

index 8a4ca13df8f83334ef93397526a8b4158ead5d3e..ee7716d58821ea0444f0d33cbf19e0d15682b0b4 100644 (file)
@@ -4,12 +4,15 @@
  * Licence details can be found in the file COPYING.
  *
  * \author John Levon
+ * \author Jürgen Spitzmüller
  *
  * Full author contact details are available in file CREDITS.
  */
 
 #include <config.h>
 
+#include "support/qstring_helpers.h"
+
 #include "GuiFontExample.h"
 #include "GuiFontMetrics.h"
 
@@ -23,28 +26,41 @@ void GuiFontExample::set(QFont const & font, QString const & text)
 {
        font_ = font;
        text_ = text;
+       lyx::frontend::GuiFontMetrics m(font_);
+       // store width, ascent and descent of the font name
+       string_width_ = m.width(text_);
+       for (auto const c : lyx::fromqstr(text)) {
+               string_ascent_ = std::max(string_ascent_, m.ascent(c));
+               string_descent_ = std::max(string_ascent_, m.descent(c));
+       }
        update();
 }
 
 
 QSize GuiFontExample::sizeHint() const
 {
-       lyx::frontend::GuiFontMetrics m(font_);
-       return QSize(m.width(text_) + 10, m.maxHeight() + 6);
+       return QSize(string_width_ + 10,
+                    string_ascent_ + string_descent_ + 6);
 }
 
 
 void GuiFontExample::paintEvent(QPaintEvent *)
 {
        QPainter p;
-       lyx::frontend::GuiFontMetrics m(font_);
 
        p.begin(this);
        p.setFont(font_);
-       p.drawRect(0, 0, width() - 1, height() - 1);
-       p.drawText(5, 3 + m.maxAscent(), text_);
+       int const h = height() - 1;
+       p.drawRect(0, 0, width() - 1, h);
+       p.drawText(5, (h / 2) + (string_descent_ / 2), text_);
        p.end();
 }
 
 
+int GuiFontExample::minWidth() const
+{
+       return string_width_;
+}
+
+
 //} // namespace lyx
index 57862e93ee1316dfddb467be5ac66f756df7d923..f9b36dff2fa37b31ee03d3fdf6bb4768817616eb 100644 (file)
@@ -28,6 +28,8 @@ public:
        void set(QFont const & font, QString const & text);
 
        QSize sizeHint() const override;
+       
+       int minWidth() const;
 
 protected:
        void paintEvent(QPaintEvent * p) override;
@@ -35,6 +37,9 @@ protected:
 private:
        QFont font_;
        QString text_;
+       int string_ascent_ = 0;
+       int string_descent_ = 0;
+       int string_width_ = 0;
 };
 
 
index a0c28e413f83e573af1bd30ed6a74405201f1fc7..d531dd4cde39ebc30d31141af664cc513d4ad75d 100644 (file)
@@ -931,18 +931,31 @@ void PrefScreenFonts::updateScreenFontSizes(LyXRC const & rc)
 void PrefScreenFonts::selectRoman(const QString & name)
 {
        screenRomanFE->set(QFont(name), name);
+       screenFontsChanged();
 }
 
 
 void PrefScreenFonts::selectSans(const QString & name)
 {
        screenSansFE->set(QFont(name), name);
+       screenFontsChanged();
 }
 
 
 void PrefScreenFonts::selectTypewriter(const QString & name)
 {
        screenTypewriterFE->set(QFont(name), name);
+       screenFontsChanged();
+}
+
+
+void PrefScreenFonts::screenFontsChanged()
+{
+       int w = max(screenRomanFE->minWidth(), screenSansFE->minWidth());
+       w = max(screenTypewriterFE->minWidth(), w);
+       screenRomanFE->setFixedWidth(w);
+       screenSansFE->setFixedWidth(w);
+       screenTypewriterFE->setFixedWidth(w);
 }
 
 
index 767b6a9c5fb5462edb1e7faad2148f9e7e113f3d..21a9d43a4e3b48c8a8ad9212d2da48fadd175618 100644 (file)
@@ -235,6 +235,7 @@ private Q_SLOTS:
        void selectRoman(const QString&);
        void selectSans(const QString&);
        void selectTypewriter(const QString&);
+       void screenFontsChanged();
 
 public Q_SLOTS:
        void updateScreenFontSizes(LyXRC const & rc);