diff --git a/app/widget/viewer/viewertexteditor.cpp b/app/widget/viewer/viewertexteditor.cpp index 488c6b6bc..7eeab3787 100644 --- a/app/widget/viewer/viewertexteditor.cpp +++ b/app/widget/viewer/viewertexteditor.cpp @@ -213,11 +213,16 @@ void ViewerTextEditor::FormatChanged(const QTextCharFormat &f) void ViewerTextEditor::SetFamily(const QString &s) { + ViewerTextEditorToolBar *toolbar = static_cast(sender()); + QTextCharFormat f; f.setFontFamily(s); #if QT_VERSION >= QT_VERSION_CHECK(5, 13, 0) f.setFontFamilies({s}); #endif + + ApplyStyle(&f, s, toolbar->GetFontStyleName()); + MergeCharFormat(f); } @@ -227,12 +232,7 @@ void ViewerTextEditor::SetStyle(const QString &s) QTextCharFormat f; - // NOTE: Windows appears to require setting weight and italic manually instead of just the style name - QFontDatabase fd; - f.setFontWeight(fd.weight(toolbar->GetFontFamily(), s)); - f.setFontItalic(fd.italic(toolbar->GetFontFamily(), s)); - - f.setFontStyleName(s); + ApplyStyle(&f, toolbar->GetFontFamily(), s); MergeCharFormat(f); } @@ -274,6 +274,17 @@ void ViewerTextEditor::MergeCharFormat(const QTextCharFormat &fmt) block_update_toolbar_signal_ = false; } +void ViewerTextEditor::ApplyStyle(QTextCharFormat *format, const QString &family, const QString &style) +{ + // NOTE: Windows appears to require setting weight and italic manually, while macOS and Linux are + // perfectly fine with just the style name + QFontDatabase fd; + format->setFontWeight(fd.weight(family, style)); + format->setFontItalic(fd.italic(family, style)); + + format->setFontStyleName(style); +} + void ViewerTextEditor::SetLineHeight(qreal i) { QTextBlockFormat f = this->textCursor().blockFormat(); @@ -322,8 +333,7 @@ ViewerTextEditorToolBar::ViewerTextEditorToolBar(QWidget *parent) : int advanced_slider_width = QtUtils::QFontMetricsWidth(fontMetrics(), QStringLiteral("9999.9%")); font_combo_ = new QFontComboBox(); - connect(font_combo_, &QFontComboBox::currentTextChanged, this, &ViewerTextEditorToolBar::FamilyChanged); - connect(font_combo_, &QFontComboBox::currentTextChanged, this, &ViewerTextEditorToolBar::UpdateFontStyleList); + connect(font_combo_, &QFontComboBox::currentTextChanged, this, &ViewerTextEditorToolBar::UpdateFontStyleListAndEmitFamilyChanged); basic_layout->addWidget(font_combo_); font_sz_slider_ = new FloatSlider(); @@ -483,6 +493,13 @@ void ViewerTextEditorToolBar::UpdateFontStyleList(const QString &family) style_combo_->blockSignals(false); } +void ViewerTextEditorToolBar::UpdateFontStyleListAndEmitFamilyChanged(const QString &family) +{ + // Ensures correct ordering of commands + UpdateFontStyleList(family); + emit FamilyChanged(family); +} + void ViewerTextEditorToolBar::mousePressEvent(QMouseEvent *event) { QWidget::mousePressEvent(event); diff --git a/app/widget/viewer/viewertexteditor.h b/app/widget/viewer/viewertexteditor.h index 3cc4153e9..d1faae7be 100644 --- a/app/widget/viewer/viewertexteditor.h +++ b/app/widget/viewer/viewertexteditor.h @@ -43,6 +43,11 @@ public: return font_combo_->currentText(); } + QString GetFontStyleName() const + { + return style_combo_->currentText(); + } + public slots: void SetFontFamily(QString s) { @@ -124,6 +129,8 @@ private: private slots: void UpdateFontStyleList(const QString &family); + void UpdateFontStyleListAndEmitFamilyChanged(const QString &family); + }; class ViewerTextEditor : public QTextEdit @@ -146,6 +153,8 @@ private: void MergeCharFormat(const QTextCharFormat &fmt); + void ApplyStyle(QTextCharFormat *format, const QString &family, const QString &style); + QVector toolbars_; QImage dpi_force_;