From 22acd9888a66b0f41d6aa2b41556e31db461a27a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fauconnier?= Date: Thu, 10 Feb 2022 08:30:57 +0100 Subject: [PATCH] fixed a bug where by calling createDefaultFont at every UBGraphicsTextItem instanciation, some fonts styles could be overridden --- src/domain/UBGraphicsScene.cpp | 3 ++ src/domain/UBGraphicsTextItem.cpp | 58 +++++++++++++++++++++++ src/domain/UBGraphicsTextItem.h | 2 + src/domain/UBGraphicsTextItemDelegate.cpp | 55 --------------------- src/domain/UBGraphicsTextItemDelegate.h | 1 - 5 files changed, 63 insertions(+), 56 deletions(-) diff --git a/src/domain/UBGraphicsScene.cpp b/src/domain/UBGraphicsScene.cpp index 533b97c1..816ef1ee 100644 --- a/src/domain/UBGraphicsScene.cpp +++ b/src/domain/UBGraphicsScene.cpp @@ -1934,9 +1934,12 @@ UBGraphicsTextItem* UBGraphicsScene::addTextWithFont(const QString& pString, con UBGraphicsTextItem *UBGraphicsScene::addTextHtml(const QString &pString, const QPointF& pTopLeft) { UBGraphicsTextItem *textItem = new UBGraphicsTextItem(); + textItem->setPlainText(""); textItem->setHtml(UBTextTools::cleanHtml(pString)); + textItem->initFontProperties(); + addItem(textItem); textItem->show(); diff --git a/src/domain/UBGraphicsTextItem.cpp b/src/domain/UBGraphicsTextItem.cpp index 5a96dff6..7e850169 100644 --- a/src/domain/UBGraphicsTextItem.cpp +++ b/src/domain/UBGraphicsTextItem.cpp @@ -81,6 +81,64 @@ UBGraphicsTextItem::~UBGraphicsTextItem() { } +void UBGraphicsTextItem::initFontProperties() +{ + QTextCursor curCursor = textCursor(); + QTextCharFormat format; + QFont font(createDefaultFont()); + + font.setPointSize(UBSettings::settings()->fontPointSize()); + format.setFont(font); + if (UBSettings::settings()->isDarkBackground()) + { + if (UBGraphicsTextItem::lastUsedTextColor == Qt::black) + UBGraphicsTextItem::lastUsedTextColor = Qt::white; + } + else + { + if (UBGraphicsTextItem::lastUsedTextColor == Qt::white) + UBGraphicsTextItem::lastUsedTextColor = Qt::black; + } + + setDefaultTextColor(UBGraphicsTextItem::lastUsedTextColor); + format.setForeground(QBrush(UBGraphicsTextItem::lastUsedTextColor)); + curCursor.mergeCharFormat(format); + setTextCursor(curCursor); + setFont(font); + + adjustSize(); + contentsChanged(); +} + +QFont UBGraphicsTextItem::createDefaultFont() +{ + QFont font; + + QString fFamily = UBSettings::settings()->fontFamily(); + if (!fFamily.isEmpty()) + font.setFamily(fFamily); + + QString fStyleName = UBSettings::settings()->fontStyleName(); + if (!fStyleName .isEmpty()) + font.setStyleName(fStyleName); + + bool bold = UBSettings::settings()->isBoldFont(); + if (bold) + font.setWeight(QFont::Bold); + + bool italic = UBSettings::settings()->isItalicFont(); + if (italic) + font.setItalic(true); + + + int pointSize = UBSettings::settings()->fontPointSize(); + if (pointSize > 0) { + font.setPointSize(pointSize); + } + + return font; +} + void UBGraphicsTextItem::recolor() { UBGraphicsTextItemDelegate * del = dynamic_cast(Delegate()); diff --git a/src/domain/UBGraphicsTextItem.h b/src/domain/UBGraphicsTextItem.h index 584a593c..8c51adef 100644 --- a/src/domain/UBGraphicsTextItem.h +++ b/src/domain/UBGraphicsTextItem.h @@ -100,6 +100,8 @@ class UBGraphicsTextItem : public QGraphicsTextItem, public UBItem, public UBRes void activateTextEditor(bool activate); void setSelected(bool selected); void recolor(); + void initFontProperties(); + QFont createDefaultFont(); QString mTypeTextHereLabel; diff --git a/src/domain/UBGraphicsTextItemDelegate.cpp b/src/domain/UBGraphicsTextItemDelegate.cpp index 08f705b0..b925ae49 100644 --- a/src/domain/UBGraphicsTextItemDelegate.cpp +++ b/src/domain/UBGraphicsTextItemDelegate.cpp @@ -110,32 +110,6 @@ UBGraphicsTextItemDelegate::UBGraphicsTextItemDelegate(UBGraphicsTextItem* pDele , delta(5) { delegated()->setData(UBGraphicsItemData::ItemEditable, QVariant(true)); - delegated()->setPlainText(""); - - QTextCursor curCursor = delegated()->textCursor(); - QTextCharFormat format; - QFont font(createDefaultFont()); - - font.setPointSize(UBSettings::settings()->fontPointSize()); - format.setFont(font); - if (UBSettings::settings()->isDarkBackground()) - { - if (UBGraphicsTextItem::lastUsedTextColor == Qt::black) - UBGraphicsTextItem::lastUsedTextColor = Qt::white; - } - else - { - if (UBGraphicsTextItem::lastUsedTextColor == Qt::white) - UBGraphicsTextItem::lastUsedTextColor = Qt::black; - } - delegated()->setDefaultTextColor(UBGraphicsTextItem::lastUsedTextColor); - format.setForeground(QBrush(UBGraphicsTextItem::lastUsedTextColor)); - curCursor.mergeCharFormat(format); - delegated()->setTextCursor(curCursor); - delegated()->setFont(font); - - delegated()->adjustSize(); - delegated()->contentsChanged(); connect(delegated()->document(), SIGNAL(cursorPositionChanged(QTextCursor)), this, SLOT(onCursorPositionChanged(QTextCursor))); connect(delegated()->document(), SIGNAL(modificationChanged(bool)), this, SLOT(onModificationChanged(bool))); @@ -146,35 +120,6 @@ UBGraphicsTextItemDelegate::~UBGraphicsTextItemDelegate() // NOOP } -QFont UBGraphicsTextItemDelegate::createDefaultFont() -{ - QFont font; - - QString fFamily = UBSettings::settings()->fontFamily(); - if (!fFamily.isEmpty()) - font.setFamily(fFamily); - - QString fStyleName = UBSettings::settings()->fontStyleName(); - if (!fStyleName .isEmpty()) - font.setStyleName(fStyleName); - - bool bold = UBSettings::settings()->isBoldFont(); - if (bold) - font.setWeight(QFont::Bold); - - bool italic = UBSettings::settings()->isItalicFont(); - if (italic) - font.setItalic(true); - - - int pointSize = UBSettings::settings()->fontPointSize(); - if (pointSize > 0) { - font.setPointSize(pointSize); - } - - return font; -} - void UBGraphicsTextItemDelegate::createControls() { UBGraphicsItemDelegate::createControls(); diff --git a/src/domain/UBGraphicsTextItemDelegate.h b/src/domain/UBGraphicsTextItemDelegate.h index 76aaf8f3..30c64849 100644 --- a/src/domain/UBGraphicsTextItemDelegate.h +++ b/src/domain/UBGraphicsTextItemDelegate.h @@ -158,7 +158,6 @@ class UBGraphicsTextItemDelegate : public UBGraphicsItemDelegate void restoreTextCursorFormats(); - QFont createDefaultFont(); QAction *mEditableAction; struct selectionData_t { selectionData_t()