From a9c512c1f09b3b720fb1aa0d1c10e7ca48fa402c Mon Sep 17 00:00:00 2001 From: csoler Date: Sun, 29 Jan 2023 20:53:25 +0100 Subject: [PATCH] re-created PR 2382 (#Phenom) to match new layout based on submodules --- retroshare-gui/src/gui/chat/ChatWidget.cpp | 44 ++++++++++++++++------ 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/retroshare-gui/src/gui/chat/ChatWidget.cpp b/retroshare-gui/src/gui/chat/ChatWidget.cpp index ab198fa33..efe1af9a0 100644 --- a/retroshare-gui/src/gui/chat/ChatWidget.cpp +++ b/retroshare-gui/src/gui/chat/ChatWidget.cpp @@ -1535,23 +1535,43 @@ void ChatWidget::chooseFont() //Use NULL as parent as with this QFontDialog don't take care of title nether options. QFont font = misc::getFont(&ok, currentFont, nullptr, tr("Choose your font.")); - QTextCursor cursor = ui->chatTextEdit->textCursor(); if (ok) { + QTextCursor cursor = ui->chatTextEdit->textCursor(); + if (cursor.selection().isEmpty()){ currentFont = font; setFont(); } else { - //Merge Format doesn't works for only selection so add a new block. - QString text = "

" + cursor.selectedText().toHtmlEscaped() + "

"; - cursor.insertHtml(text); + // Merge Format doesn't works for only selection. + // and charFormat() get format for last char. + QTextCursor selCurs = cursor; + QTextCursor lastCurs = cursor; + int pos = cursor.selectionStart(); + lastCurs.setPosition(pos); + do + { + // Get format block in selection iterating char one by one + selCurs.setPosition(++pos); + if (selCurs.charFormat() != lastCurs.charFormat()) + { + // New char format, format last block. + QTextCharFormat charFormat = lastCurs.charFormat(); + charFormat.setFont(font); + lastCurs.setCharFormat(charFormat); + // Last block formated, start it to current char. + lastCurs.setPosition(pos-1); + } + // Add current char. + lastCurs.setPosition(pos, QTextCursor::KeepAnchor); + } while (pos < cursor.selectionEnd()); + + // Now format last block + if (lastCurs.selectionStart() != lastCurs.selectionEnd()) + { + QTextCharFormat charFormat = lastCurs.charFormat(); + charFormat.setFont(font); + lastCurs.setCharFormat(charFormat); + } } } }