diff options
| author | OmniTroid <davidskoland@gmail.com> | 2017-03-26 01:33:09 +0100 |
|---|---|---|
| committer | OmniTroid <davidskoland@gmail.com> | 2017-03-26 01:33:09 +0100 |
| commit | 2f4e6881e09359a4aa7498f828f82fe395124bc6 (patch) | |
| tree | c7655a0d64e60c61552495ef8a69bb9341745734 | |
| parent | 1bc1ec6d4005ab46314c20c680aca4157d5c2244 (diff) | |
changed server ooc chat log to AOTextArea object
| -rw-r--r-- | aotextarea.cpp | 46 | ||||
| -rw-r--r-- | aotextarea.h | 4 | ||||
| -rw-r--r-- | courtroom.cpp | 41 | ||||
| -rw-r--r-- | courtroom.h | 5 |
4 files changed, 53 insertions, 43 deletions
diff --git a/aotextarea.cpp b/aotextarea.cpp index 1cffd258..18551c1b 100644 --- a/aotextarea.cpp +++ b/aotextarea.cpp @@ -1,6 +1,50 @@ #include "aotextarea.h" -AOTextArea::AOTextArea() +#include <QScrollBar> +#include <QTextCursor> +#include <QRegExp> + +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("<a href=\"" + i_word + "\">" + i_word + "</a> "); + } + 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 b5a2e0df..420bced1 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 101f0e02..fef7d51e 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("<a href=\"" + i_word + "\">" + i_word + "</a> "); - } - 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 eba49db6..d617bd86 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 <QMainWindow> @@ -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; |
