[LyX features/biginset] Fix alignment of screen font preview in prefs (remaining part of #13046)

Juergen Spitzmueller spitz at lyx.org
Fri Apr 5 13:41:45 UTC 2024


commit 60cffcd9b7cc768d3a3cbfa97beebefb426350e1
Author: Juergen Spitzmueller <spitz at lyx.org>
Date:   Sun Mar 24 09:00:41 2024 +0100

    Fix alignment of screen font preview in prefs (remaining part of #13046)
---
 src/frontends/qt/GuiFontExample.cpp | 26 +++++++++++++++++++++-----
 src/frontends/qt/GuiFontExample.h   |  5 +++++
 src/frontends/qt/GuiPrefs.cpp       | 13 +++++++++++++
 src/frontends/qt/GuiPrefs.h         |  1 +
 4 files changed, 40 insertions(+), 5 deletions(-)

diff --git a/src/frontends/qt/GuiFontExample.cpp b/src/frontends/qt/GuiFontExample.cpp
index 8a4ca13df8..ee7716d588 100644
--- a/src/frontends/qt/GuiFontExample.cpp
+++ b/src/frontends/qt/GuiFontExample.cpp
@@ -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
diff --git a/src/frontends/qt/GuiFontExample.h b/src/frontends/qt/GuiFontExample.h
index 57862e93ee..f9b36dff2f 100644
--- a/src/frontends/qt/GuiFontExample.h
+++ b/src/frontends/qt/GuiFontExample.h
@@ -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;
 };
 
 
diff --git a/src/frontends/qt/GuiPrefs.cpp b/src/frontends/qt/GuiPrefs.cpp
index a0c28e413f..d531dd4cde 100644
--- a/src/frontends/qt/GuiPrefs.cpp
+++ b/src/frontends/qt/GuiPrefs.cpp
@@ -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);
 }
 
 
diff --git a/src/frontends/qt/GuiPrefs.h b/src/frontends/qt/GuiPrefs.h
index 767b6a9c5f..21a9d43a4e 100644
--- a/src/frontends/qt/GuiPrefs.h
+++ b/src/frontends/qt/GuiPrefs.h
@@ -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);


More information about the lyx-cvs mailing list