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.
*
* \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

View 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;
};

View 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);
}

View 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);