From 2f4e6881e09359a4aa7498f828f82fe395124bc6 Mon Sep 17 00:00:00 2001 From: OmniTroid Date: Sun, 26 Mar 2017 01:33:09 +0100 Subject: [PATCH] changed server ooc chat log to AOTextArea object --- aotextarea.cpp | 46 +++++++++++++++++++++++++++++++++++++++++++++- aotextarea.h | 4 ++-- courtroom.cpp | 41 +++-------------------------------------- courtroom.h | 5 +++-- 4 files changed, 53 insertions(+), 43 deletions(-) diff --git a/aotextarea.cpp b/aotextarea.cpp index 1cffd25..18551c1 100644 --- a/aotextarea.cpp +++ b/aotextarea.cpp @@ -1,6 +1,50 @@ #include "aotextarea.h" -AOTextArea::AOTextArea() +#include +#include +#include + +AOTextArea::AOTextArea(QWidget *p_parent) : QTextBrowser(p_parent) { } + +void AOTextArea::append_chatmessage(QString p_name, QString p_message) +{ + const QTextCursor old_cursor = this->textCursor(); + const int old_scrollbar_value = this->verticalScrollBar()->value(); + const bool is_scrolled_down = old_scrollbar_value == this->verticalScrollBar()->maximum(); + + this->moveCursor(QTextCursor::End); + + this->insertPlainText(p_name + ": "); + + QRegExp split_rx("(\\ |\\n)"); + QStringList word_list = p_message.split(split_rx); + + for (QString i_word : word_list) + { + if (i_word.startsWith("http")) + { + i_word.replace("\r", ""); + this->insertHtml("" + i_word + " "); + } + else + this->insertPlainText(i_word + " "); + } + + this->insertPlainText("\n"); + + if (old_cursor.hasSelection() || !is_scrolled_down) + { + // The user has selected text or scrolled away from the bottom: maintain position. + this->setTextCursor(old_cursor); + this->verticalScrollBar()->setValue(old_scrollbar_value); + } + else + { + // The user hasn't selected any text and the scrollbar is at the bottom: scroll to the bottom. + this->moveCursor(QTextCursor::End); + this->verticalScrollBar()->setValue(this->verticalScrollBar()->maximum()); + } +} diff --git a/aotextarea.h b/aotextarea.h index b5a2e0d..420bced 100644 --- a/aotextarea.h +++ b/aotextarea.h @@ -6,9 +6,9 @@ class AOTextArea : public QTextBrowser { public: - AOTextArea(); + AOTextArea(QWidget *p_parent = nullptr); - append_text(); + void append_chatmessage(QString p_name, QString p_message); }; #endif // AOTEXTAREA_H diff --git a/courtroom.cpp b/courtroom.cpp index 101f0e0..fef7d51 100644 --- a/courtroom.cpp +++ b/courtroom.cpp @@ -83,7 +83,7 @@ Courtroom::Courtroom(AOApplication *p_ao_app) : QMainWindow() ui_ms_chatlog->setOpenExternalLinks(true); ui_ms_chatlog->hide(); - ui_server_chatlog = new QTextBrowser(this); + ui_server_chatlog = new AOTextArea(this); ui_server_chatlog->setReadOnly(true); ui_server_chatlog->setOpenExternalLinks(true); @@ -761,44 +761,9 @@ void Courtroom::append_ms_chatmessage(QString f_message) } } -void Courtroom::append_server_chatmessage(QString f_name, QString f_message) +void Courtroom::append_server_chatmessage(QString p_name, QString p_message) { - const QTextCursor old_cursor = ui_server_chatlog->textCursor(); - const int old_scrollbar_value = ui_server_chatlog->verticalScrollBar()->value(); - const bool is_scrolled_down = old_scrollbar_value == ui_server_chatlog->verticalScrollBar()->maximum(); - - ui_server_chatlog->moveCursor(QTextCursor::End); - - ui_server_chatlog->insertPlainText(f_name + ": "); - - QRegExp split_rx("(\\ |\\n)"); - QStringList word_list = f_message.split(split_rx); - - for (QString i_word : word_list) - { - if (i_word.startsWith("http")) - { - i_word.replace("\r", ""); - ui_server_chatlog->insertHtml("" + i_word + " "); - } - else - ui_server_chatlog->insertPlainText(i_word + " "); - } - - ui_server_chatlog->insertPlainText("\n"); - - if (old_cursor.hasSelection() || !is_scrolled_down) - { - // The user has selected text or scrolled away from the bottom: maintain position. - ui_server_chatlog->setTextCursor(old_cursor); - ui_server_chatlog->verticalScrollBar()->setValue(old_scrollbar_value); - } - else - { - // The user hasn't selected any text and the scrollbar is at the bottom: scroll to the bottom. - ui_server_chatlog->moveCursor(QTextCursor::End); - ui_server_chatlog->verticalScrollBar()->setValue(ui_server_chatlog->verticalScrollBar()->maximum()); - } + ui_server_chatlog->append_chatmessage(p_name, p_message); } void Courtroom::on_chat_return_pressed() diff --git a/courtroom.h b/courtroom.h index eba49db..d617bd8 100644 --- a/courtroom.h +++ b/courtroom.h @@ -13,6 +13,7 @@ #include "aosfxplayer.h" #include "aoblipplayer.h" #include "aoevidencebutton.h" +#include "aotextarea.h" #include "datatypes.h" #include @@ -76,7 +77,7 @@ public: void list_music(); void append_ms_chatmessage(QString f_message); - void append_server_chatmessage(QString f_name, QString f_message); + void append_server_chatmessage(QString p_name, QString p_message); void handle_chatmessage(QStringList *p_contents); void handle_chatmessage_2(); @@ -228,7 +229,7 @@ private: QPlainTextEdit *ui_ic_chatlog; QTextBrowser *ui_ms_chatlog; - QTextBrowser *ui_server_chatlog; + AOTextArea *ui_server_chatlog; QListWidget *ui_mute_list; QListWidget *ui_area_list;