Fix alignment of screen font preview in prefs (remaining part of #13046)

This commit is contained in:
Juergen Spitzmueller 2024-03-24 09:00:41 +01:00
parent 58aca89003
commit 60cffcd9b7
4 changed files with 40 additions and 5 deletions

View File

@ -4,12 +4,15 @@
* Licence details can be found in the file COPYING. * Licence details can be found in the file COPYING.
* *
* \author John Levon * \author John Levon
* \author Jürgen Spitzmüller
* *
* Full author contact details are available in file CREDITS. * Full author contact details are available in file CREDITS.
*/ */
#include <config.h> #include <config.h>
#include "support/qstring_helpers.h"
#include "GuiFontExample.h" #include "GuiFontExample.h"
#include "GuiFontMetrics.h" #include "GuiFontMetrics.h"
@ -23,28 +26,41 @@ void GuiFontExample::set(QFont const & font, QString const & text)
{ {
font_ = font; font_ = font;
text_ = text; 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(); update();
} }
QSize GuiFontExample::sizeHint() const QSize GuiFontExample::sizeHint() const
{ {
lyx::frontend::GuiFontMetrics m(font_); return QSize(string_width_ + 10,
return QSize(m.width(text_) + 10, m.maxHeight() + 6); string_ascent_ + string_descent_ + 6);
} }
void GuiFontExample::paintEvent(QPaintEvent *) void GuiFontExample::paintEvent(QPaintEvent *)
{ {
QPainter p; QPainter p;
lyx::frontend::GuiFontMetrics m(font_);
p.begin(this); p.begin(this);
p.setFont(font_); p.setFont(font_);
p.drawRect(0, 0, width() - 1, height() - 1); int const h = height() - 1;
p.drawText(5, 3 + m.maxAscent(), text_); p.drawRect(0, 0, width() - 1, h);
p.drawText(5, (h / 2) + (string_descent_ / 2), text_);
p.end(); p.end();
} }
int GuiFontExample::minWidth() const
{
return string_width_;
}
//} // namespace lyx //} // namespace lyx

View File

@ -29,12 +29,17 @@ public:
QSize sizeHint() const override; QSize sizeHint() const override;
int minWidth() const;
protected: protected:
void paintEvent(QPaintEvent * p) override; void paintEvent(QPaintEvent * p) override;
private: private:
QFont font_; QFont font_;
QString text_; QString text_;
int string_ascent_ = 0;
int string_descent_ = 0;
int string_width_ = 0;
}; };

View File

@ -931,18 +931,31 @@ void PrefScreenFonts::updateScreenFontSizes(LyXRC const & rc)
void PrefScreenFonts::selectRoman(const QString & name) void PrefScreenFonts::selectRoman(const QString & name)
{ {
screenRomanFE->set(QFont(name), name); screenRomanFE->set(QFont(name), name);
screenFontsChanged();
} }
void PrefScreenFonts::selectSans(const QString & name) void PrefScreenFonts::selectSans(const QString & name)
{ {
screenSansFE->set(QFont(name), name); screenSansFE->set(QFont(name), name);
screenFontsChanged();
} }
void PrefScreenFonts::selectTypewriter(const QString & name) void PrefScreenFonts::selectTypewriter(const QString & name)
{ {
screenTypewriterFE->set(QFont(name), 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);
} }

View File

@ -235,6 +235,7 @@ private Q_SLOTS:
void selectRoman(const QString&); void selectRoman(const QString&);
void selectSans(const QString&); void selectSans(const QString&);
void selectTypewriter(const QString&); void selectTypewriter(const QString&);
void screenFontsChanged();
public Q_SLOTS: public Q_SLOTS:
void updateScreenFontSizes(LyXRC const & rc); void updateScreenFontSizes(LyXRC const & rc);