From 0649884d3c9ceeef5515d621e9867dfc08df59ba Mon Sep 17 00:00:00 2001 From: Crystalwarrior Date: Wed, 25 Sep 2019 04:45:45 +0300 Subject: [PATCH] Fix issue with ao line edit not correctly preserving selection Resolve a segfault with text formatting Implement ability to color text inline by selecting text and using the color dropdown TODO: allow html characters to be used for coloring text, oof --- src/aolineedit.cpp | 6 +++--- src/courtroom.cpp | 25 ++++++++++++++++++++----- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/aolineedit.cpp b/src/aolineedit.cpp index e345de3..56b571f 100644 --- a/src/aolineedit.cpp +++ b/src/aolineedit.cpp @@ -13,8 +13,8 @@ void AOLineEdit::mouseDoubleClickEvent(QMouseEvent *e) void AOLineEdit::focusOutEvent(QFocusEvent *ev) { int start = selectionStart(); - int end = selectionEnd(); + int len = selectionLength(); QLineEdit::focusOutEvent(ev); - if (p_selection && start != -1 && end != -1) - this->setSelection(start, end); + if (p_selection && start != -1 && len != -1) + this->setSelection(start, len); } diff --git a/src/courtroom.cpp b/src/courtroom.cpp index 2ae611a..75ce54d 100644 --- a/src/courtroom.cpp +++ b/src/courtroom.cpp @@ -2156,7 +2156,7 @@ QString Courtroom::filter_ic_text(QString p_text, bool colorize, int pos, int de break; //Prevent it from looping forward for whatever reason } } - else if (f_character == markdown_start || (f_character == markdown_end && ic_color_stack.top() == c)) + else if (f_character == markdown_start || (f_character == markdown_end && !ic_color_stack.empty() && ic_color_stack.top() == c)) { if (colorize) { @@ -2547,8 +2547,8 @@ void Courtroom::chat_tick() { //Clear the stored optimization information // color_rgb_list.at(c); - QString markdown_start = color_markdown_start_list.at(c); - QString markdown_end = color_markdown_end_list.at(c); + QString markdown_start = color_markdown_start_list.at(c).toHtmlEscaped(); + QString markdown_end = color_markdown_end_list.at(c).toHtmlEscaped(); bool markdown_remove = color_markdown_remove_list.at(c); bool color_is_talking = color_markdown_talking_list.at(c); if (markdown_start.isEmpty()) @@ -3846,8 +3846,23 @@ void Courtroom::on_text_color_changed(int p_color) { if (ui_ic_chat_message->selectionStart() != -1) //We have a selection! { - qDebug() << "Setting color to selection" << ui_ic_chat_message->selectionStart() << ui_ic_chat_message->selectionEnd(); - ui_ic_chat_message->end(false); + int c = color_row_to_number.at(p_color); + QString markdown_start = color_markdown_start_list.at(c); + if (markdown_start.isEmpty()) + { + qDebug() << "W: Color list dropdown selected a non-existent markdown start character"; + return; + } + QString markdown_end = color_markdown_end_list.at(c); + if (markdown_end.isEmpty()) + markdown_end = markdown_start; + int start = ui_ic_chat_message->selectionStart(); + int end = ui_ic_chat_message->selectionEnd()+1; + ui_ic_chat_message->setCursorPosition(start); + ui_ic_chat_message->insert(markdown_start); + ui_ic_chat_message->setCursorPosition(end); + ui_ic_chat_message->insert(markdown_end); +// ui_ic_chat_message->end(false); ui_text_color->setCurrentIndex(0); } else