From 026ddb3c564bbb6e2180038ba9046f0f449fd5e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fauconnier?= Date: Tue, 27 Apr 2021 18:28:40 +0200 Subject: [PATCH] fixed some issues where change align state could result on loosing last changes (color, size, content) --- src/domain/UBGraphicsTextItemDelegate.cpp | 25 +++++++++++++---------- src/domain/UBGraphicsTextItemDelegate.h | 2 +- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/domain/UBGraphicsTextItemDelegate.cpp b/src/domain/UBGraphicsTextItemDelegate.cpp index eadd3aa3..a385b3ac 100644 --- a/src/domain/UBGraphicsTextItemDelegate.cpp +++ b/src/domain/UBGraphicsTextItemDelegate.cpp @@ -384,6 +384,7 @@ void UBGraphicsTextItemDelegate::pickColor() format.setForeground(QBrush(selectedColor)); curCursor.mergeCharFormat(format); delegated()->setTextCursor(curCursor); + saveTextCursorFormats(); if (!curCursor.hasSelection() || (curCursor.selectedText().length() == delegated()->toPlainText().length())) { @@ -543,7 +544,7 @@ bool UBGraphicsTextItemDelegate::mouseReleaseEvent(QGraphicsSceneMouseEvent *eve mSelectionData.mButtonIsPressed = false; qDebug() << "Reporting selection of the cursor (mouse release)" << delegated()->textCursor().selection().isEmpty(); qDebug() << QString("Anchor: %1\nposition: %2 (mouse mouse release)").arg(delegated()->textCursor().anchor()).arg(delegated()->textCursor().position()); - updateAlighButtonState(); + updateAlignButtonState(); if (!UBGraphicsItemDelegate::mouseReleaseEvent(event)) { return false; @@ -564,14 +565,10 @@ bool UBGraphicsTextItemDelegate::keyReleaseEvent(QKeyEvent *event) return true; } - switch (event->key()) { - case Qt::Key_Left: - case Qt::Key_Right: - case Qt::Key_Up: - case Qt::Key_Down: - updateAlighButtonState(); - break; - } + // to be sure the save/restore TextCursorFormats mechanism is always up-to-date + // and because if some text is selected, almost any typed key can clear the selection before + // keyReleaseEvent is called, it's safer to call updateAlignButtonState everytime a key is released + updateAlignButtonState(); qDebug() << "Key has been released" << QString::number(event->key(), 16); return true; @@ -685,6 +682,7 @@ void UBGraphicsTextItemDelegate::ChangeTextSize(qreal factor, textChangeMode cha cursor.setPosition (cursorPos, QTextCursor::KeepAnchor); delegated()->setTextCursor(cursor); + saveTextCursorFormats(); } void UBGraphicsTextItemDelegate::recolor() @@ -774,9 +772,10 @@ void UBGraphicsTextItemDelegate::recolor() cursor.setPosition (cursorPos, QTextCursor::KeepAnchor); delegated()->setTextCursor(cursor); + saveTextCursorFormats(); } -void UBGraphicsTextItemDelegate::updateAlighButtonState() +void UBGraphicsTextItemDelegate::updateAlignButtonState() { if (!mAlignButton) { return; @@ -844,7 +843,10 @@ void UBGraphicsTextItemDelegate::restoreTextCursorFormats() QTextCursor tcrsr = delegated()->textCursor(); tcrsr.setPosition(mSelectionData.position); - tcrsr.movePosition(QTextCursor::Left, QTextCursor::KeepAnchor, steps); + if (mSelectionData.position >= mSelectionData.anchor) + tcrsr.movePosition(QTextCursor::Left, QTextCursor::KeepAnchor, steps); + else + tcrsr.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, steps); delegated()->setTextCursor(tcrsr); } @@ -864,6 +866,7 @@ QVariant UBGraphicsTextItemDelegate::itemChange(QGraphicsItem::GraphicsItemChang { c.clearSelection(); delegated()->setTextCursor(c); + saveTextCursorFormats(); } } } diff --git a/src/domain/UBGraphicsTextItemDelegate.h b/src/domain/UBGraphicsTextItemDelegate.h index cc03c08d..76aaf8f3 100644 --- a/src/domain/UBGraphicsTextItemDelegate.h +++ b/src/domain/UBGraphicsTextItemDelegate.h @@ -152,7 +152,7 @@ class UBGraphicsTextItemDelegate : public UBGraphicsItemDelegate private: void customize(QFontDialog &fontDialog); void ChangeTextSize(qreal factor, textChangeMode changeMode); - void updateAlighButtonState(); + void updateAlignButtonState(); bool oneBlockSelection(); void saveTextCursorFormats(); void restoreTextCursorFormats();