fixed some issues where change align state could result on loosing last changes (color, size, content)

preferencesAboutTextFull
Clément Fauconnier 4 years ago
parent f3f07f9930
commit 026ddb3c56
  1. 25
      src/domain/UBGraphicsTextItemDelegate.cpp
  2. 2
      src/domain/UBGraphicsTextItemDelegate.h

@ -384,6 +384,7 @@ void UBGraphicsTextItemDelegate::pickColor()
format.setForeground(QBrush(selectedColor)); format.setForeground(QBrush(selectedColor));
curCursor.mergeCharFormat(format); curCursor.mergeCharFormat(format);
delegated()->setTextCursor(curCursor); delegated()->setTextCursor(curCursor);
saveTextCursorFormats();
if (!curCursor.hasSelection() || (curCursor.selectedText().length() == delegated()->toPlainText().length())) if (!curCursor.hasSelection() || (curCursor.selectedText().length() == delegated()->toPlainText().length()))
{ {
@ -543,7 +544,7 @@ bool UBGraphicsTextItemDelegate::mouseReleaseEvent(QGraphicsSceneMouseEvent *eve
mSelectionData.mButtonIsPressed = false; mSelectionData.mButtonIsPressed = false;
qDebug() << "Reporting selection of the cursor (mouse release)" << delegated()->textCursor().selection().isEmpty(); 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()); qDebug() << QString("Anchor: %1\nposition: %2 (mouse mouse release)").arg(delegated()->textCursor().anchor()).arg(delegated()->textCursor().position());
updateAlighButtonState(); updateAlignButtonState();
if (!UBGraphicsItemDelegate::mouseReleaseEvent(event)) { if (!UBGraphicsItemDelegate::mouseReleaseEvent(event)) {
return false; return false;
@ -564,14 +565,10 @@ bool UBGraphicsTextItemDelegate::keyReleaseEvent(QKeyEvent *event)
return true; return true;
} }
switch (event->key()) { // to be sure the save/restore TextCursorFormats mechanism is always up-to-date
case Qt::Key_Left: // and because if some text is selected, almost any typed key can clear the selection before
case Qt::Key_Right: // keyReleaseEvent is called, it's safer to call updateAlignButtonState everytime a key is released
case Qt::Key_Up: updateAlignButtonState();
case Qt::Key_Down:
updateAlighButtonState();
break;
}
qDebug() << "Key has been released" << QString::number(event->key(), 16); qDebug() << "Key has been released" << QString::number(event->key(), 16);
return true; return true;
@ -685,6 +682,7 @@ void UBGraphicsTextItemDelegate::ChangeTextSize(qreal factor, textChangeMode cha
cursor.setPosition (cursorPos, QTextCursor::KeepAnchor); cursor.setPosition (cursorPos, QTextCursor::KeepAnchor);
delegated()->setTextCursor(cursor); delegated()->setTextCursor(cursor);
saveTextCursorFormats();
} }
void UBGraphicsTextItemDelegate::recolor() void UBGraphicsTextItemDelegate::recolor()
@ -774,9 +772,10 @@ void UBGraphicsTextItemDelegate::recolor()
cursor.setPosition (cursorPos, QTextCursor::KeepAnchor); cursor.setPosition (cursorPos, QTextCursor::KeepAnchor);
delegated()->setTextCursor(cursor); delegated()->setTextCursor(cursor);
saveTextCursorFormats();
} }
void UBGraphicsTextItemDelegate::updateAlighButtonState() void UBGraphicsTextItemDelegate::updateAlignButtonState()
{ {
if (!mAlignButton) { if (!mAlignButton) {
return; return;
@ -844,7 +843,10 @@ void UBGraphicsTextItemDelegate::restoreTextCursorFormats()
QTextCursor tcrsr = delegated()->textCursor(); QTextCursor tcrsr = delegated()->textCursor();
tcrsr.setPosition(mSelectionData.position); 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); delegated()->setTextCursor(tcrsr);
} }
@ -864,6 +866,7 @@ QVariant UBGraphicsTextItemDelegate::itemChange(QGraphicsItem::GraphicsItemChang
{ {
c.clearSelection(); c.clearSelection();
delegated()->setTextCursor(c); delegated()->setTextCursor(c);
saveTextCursorFormats();
} }
} }
} }

@ -152,7 +152,7 @@ class UBGraphicsTextItemDelegate : public UBGraphicsItemDelegate
private: private:
void customize(QFontDialog &fontDialog); void customize(QFontDialog &fontDialog);
void ChangeTextSize(qreal factor, textChangeMode changeMode); void ChangeTextSize(qreal factor, textChangeMode changeMode);
void updateAlighButtonState(); void updateAlignButtonState();
bool oneBlockSelection(); bool oneBlockSelection();
void saveTextCursorFormats(); void saveTextCursorFormats();
void restoreTextCursorFormats(); void restoreTextCursorFormats();

Loading…
Cancel
Save