diff options
| author | David Skoland <davidskoland@gmail.com> | 2018-12-26 16:43:08 +0100 |
|---|---|---|
| committer | David Skoland <davidskoland@gmail.com> | 2018-12-26 16:43:08 +0100 |
| commit | 00cfd2750d39795e4c205aee2a33b92b1da2524d (patch) | |
| tree | 839c1129418ca8fefe41e6814917f9dabb6efa4b /src/aotextarea.cpp | |
| parent | 6f1bce5882676ea7affe717a2f5a00b8c3b7fe12 (diff) | |
moved headers into include and cpp files into src + logo into resource
Diffstat (limited to 'src/aotextarea.cpp')
| -rw-r--r-- | src/aotextarea.cpp | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/src/aotextarea.cpp b/src/aotextarea.cpp new file mode 100644 index 00000000..5e146326 --- /dev/null +++ b/src/aotextarea.cpp @@ -0,0 +1,60 @@ +#include "aotextarea.h" + +AOTextArea::AOTextArea(QWidget *p_parent) : QTextBrowser(p_parent) +{ + +} + +void AOTextArea::append_chatmessage(QString p_name, QString p_message, QString p_colour) +{ + 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->append(""); + this->insertHtml("<b><font color=" + p_colour + ">" + p_name.toHtmlEscaped() + "</font></b>: "); + + //cheap workarounds ahoy + p_message += " "; + QString result = p_message.toHtmlEscaped().replace("\n", "<br>").replace(omnis_dank_url_regex, "<a href='\\1'>\\1</a>" ); + + this->insertHtml(result); + + this->auto_scroll(old_cursor, old_scrollbar_value, is_scrolled_down); +} + +void AOTextArea::append_error(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->append(""); + + p_message += " "; + QString result = p_message.replace("\n", "<br>").replace(omnis_dank_url_regex, "<a href='\\1'>\\1</a>" ); + + this->insertHtml("<font color='red'>" + result + "</font>"); + + this->auto_scroll(old_cursor, old_scrollbar_value, is_scrolled_down); +} + +void AOTextArea::auto_scroll(QTextCursor old_cursor, int old_scrollbar_value, bool is_scrolled_down) +{ + 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()); + } +} |
