From 40c334c14433fa877b18e1b734c1b4cedfb4cc11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fauconnier?= Date: Mon, 15 Jul 2019 14:07:05 +0200 Subject: [PATCH] reuse last used color for newer text boxes (only if applied globally) + recolor text boxes on background changes (white text becomes black and black text becomes white (like for polygons)) --- src/board/UBBoardController.cpp | 2 +- src/domain/UBGraphicsScene.cpp | 6 ++ src/domain/UBGraphicsTextItem.cpp | 9 +- src/domain/UBGraphicsTextItem.h | 1 + src/domain/UBGraphicsTextItemDelegate.cpp | 107 +++++++++++++++++++++- src/domain/UBGraphicsTextItemDelegate.h | 1 + 6 files changed, 122 insertions(+), 4 deletions(-) diff --git a/src/board/UBBoardController.cpp b/src/board/UBBoardController.cpp index 4be7f66b..001bc509 100644 --- a/src/board/UBBoardController.cpp +++ b/src/board/UBBoardController.cpp @@ -1589,7 +1589,7 @@ void UBBoardController::setActiveDocumentScene(UBDocumentProxy* pDocumentProxy, if(documentChange) { - UBGraphicsTextItem::lastUsedTextColor = QColor(); + UBGraphicsTextItem::lastUsedTextColor = QColor(Qt::black); } if (sceneChange) diff --git a/src/domain/UBGraphicsScene.cpp b/src/domain/UBGraphicsScene.cpp index efc6f1db..13be73d7 100644 --- a/src/domain/UBGraphicsScene.cpp +++ b/src/domain/UBGraphicsScene.cpp @@ -1179,6 +1179,12 @@ void UBGraphicsScene::recolorAllItems() curGroup->setColor(newColor); } } + + if (item->type() == UBGraphicsTextItem::Type) + { + UBGraphicsTextItem *textItem = static_cast(item); + textItem->recolor(); + } } foreach(QGraphicsView* view, views()) diff --git a/src/domain/UBGraphicsTextItem.cpp b/src/domain/UBGraphicsTextItem.cpp index c735fa9c..f3bbbb12 100644 --- a/src/domain/UBGraphicsTextItem.cpp +++ b/src/domain/UBGraphicsTextItem.cpp @@ -41,7 +41,7 @@ #include "core/UBSettings.h" #include "core/memcheck.h" -QColor UBGraphicsTextItem::lastUsedTextColor; +QColor UBGraphicsTextItem::lastUsedTextColor = QColor(Qt::black); UBGraphicsTextItem::UBGraphicsTextItem(QGraphicsItem * parent) : QGraphicsTextItem(parent) @@ -81,6 +81,13 @@ UBGraphicsTextItem::~UBGraphicsTextItem() { } +void UBGraphicsTextItem::recolor() +{ + UBGraphicsTextItemDelegate * del = dynamic_cast(Delegate()); + if (del) + del->recolor(); +} + void UBGraphicsTextItem::setSelected(bool selected) { if(selected){ diff --git a/src/domain/UBGraphicsTextItem.h b/src/domain/UBGraphicsTextItem.h index b7789b28..584a593c 100644 --- a/src/domain/UBGraphicsTextItem.h +++ b/src/domain/UBGraphicsTextItem.h @@ -99,6 +99,7 @@ class UBGraphicsTextItem : public QGraphicsTextItem, public UBItem, public UBRes virtual void setUuid(const QUuid &pUuid); void activateTextEditor(bool activate); void setSelected(bool selected); + void recolor(); QString mTypeTextHereLabel; diff --git a/src/domain/UBGraphicsTextItemDelegate.cpp b/src/domain/UBGraphicsTextItemDelegate.cpp index 49f27c90..a72b32d8 100644 --- a/src/domain/UBGraphicsTextItemDelegate.cpp +++ b/src/domain/UBGraphicsTextItemDelegate.cpp @@ -115,9 +115,21 @@ UBGraphicsTextItemDelegate::UBGraphicsTextItemDelegate(UBGraphicsTextItem* pDele QTextCursor curCursor = delegated()->textCursor(); QTextCharFormat format; QFont font(createDefaultFont()); - font.setPointSize(UBSettings::settings()->fontPointSize()); + 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); @@ -368,12 +380,14 @@ void UBGraphicsTextItemDelegate::pickColor() QColor selectedColor = colorDialog.selectedColor(); delegated()->setDefaultTextColor(selectedColor); QTextCursor curCursor = delegated()->textCursor(); + QTextCharFormat format; format.setForeground(QBrush(selectedColor)); curCursor.mergeCharFormat(format); delegated()->setTextCursor(curCursor); - UBGraphicsTextItem::lastUsedTextColor = selectedColor; + if (!curCursor.hasComplexSelection()) + UBGraphicsTextItem::lastUsedTextColor = selectedColor; delegated()->setSelected(true); delegated()->document()->adjustSize(); @@ -673,6 +687,95 @@ void UBGraphicsTextItemDelegate::ChangeTextSize(qreal factor, textChangeMode cha delegated()->setTextCursor(cursor); } +void UBGraphicsTextItemDelegate::recolor() +{ + QTextCursor cursor = delegated()->textCursor(); + QTextCharFormat textFormat; + + int anchorPos = cursor.anchor(); + int cursorPos = cursor.position(); + + if (0 == anchorPos-cursorPos) + { + // If nothing is selected, then we select all the text + cursor.setPosition (0, QTextCursor::MoveAnchor); + cursor.setPosition (cursor.document()->characterCount()-1, QTextCursor::KeepAnchor); + } + + int startPos = qMin(cursor.anchor(), cursor.position()); + int endPos = qMax(cursor.anchor(), cursor.position()); + + QFont curFont; + QFont nextCharFont; + bool bEndofTheSameBlock; + int iBlockLen; + int iCursorPos = startPos; + QBrush curBrush; + QBrush nextCharBrush; + + cursor.setPosition (startPos, QTextCursor::MoveAnchor); + while(iCursorPos < endPos) + { + bEndofTheSameBlock = false; + iBlockLen = 0; + + // Here we get the point size of the first character + cursor.setPosition (iCursorPos+1, QTextCursor::KeepAnchor); + curBrush = cursor.charFormat().foreground(); + + // Then we position the end cursor to the start cursor position + cursor.setPosition (iCursorPos, QTextCursor::KeepAnchor); + + do + { + cursor.setPosition (iCursorPos+iBlockLen+1, QTextCursor::KeepAnchor); + nextCharBrush = cursor.charFormat().foreground(); + + if (curBrush != nextCharBrush || (iCursorPos+iBlockLen >= endPos)) + { + bEndofTheSameBlock = true; + break; + } + + iBlockLen++; + + }while(!bEndofTheSameBlock); + + + //setting new parameters + if (UBSettings::settings()->isDarkBackground()) + { + if (curBrush.color() == Qt::black) + { + curBrush = QBrush(Qt::white); + } + } + else + { + if (curBrush.color() == Qt::white) + { + curBrush = QBrush(Qt::black); + } + } + + cursor.setPosition (iCursorPos+iBlockLen, QTextCursor::KeepAnchor); + textFormat.setForeground(curBrush); + cursor.mergeCharFormat(textFormat); + + iCursorPos += iBlockLen; + cursor.setPosition (iCursorPos, QTextCursor::MoveAnchor); + + curFont = nextCharFont; + } + + delegated()->setFont(curFont); + //returning initial selection + cursor.setPosition (anchorPos, QTextCursor::MoveAnchor); + cursor.setPosition (cursorPos, QTextCursor::KeepAnchor); + + delegated()->setTextCursor(cursor); +} + void UBGraphicsTextItemDelegate::updateAlighButtonState() { if (!mAlignButton) { diff --git a/src/domain/UBGraphicsTextItemDelegate.h b/src/domain/UBGraphicsTextItemDelegate.h index 0e26ffa5..cc03c08d 100644 --- a/src/domain/UBGraphicsTextItemDelegate.h +++ b/src/domain/UBGraphicsTextItemDelegate.h @@ -112,6 +112,7 @@ class UBGraphicsTextItemDelegate : public UBGraphicsItemDelegate virtual ~UBGraphicsTextItemDelegate(); bool isEditable(); void scaleTextSize(qreal multiplyer); + void recolor(); virtual QVariant itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value); virtual void createControls(); qreal titleBarWidth();