aboutsummaryrefslogtreecommitdiff
path: root/aotextarea.cpp
blob: a61a2486a6b33c8828484c06dcb0524786bb01d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include "aotextarea.h"

#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->append(p_name + ": ");

  QStringList word_list = p_message.split(" ");

  for (QString i_word : word_list)
  {
    if (i_word.startsWith("http"))
    {
      this->insertHtml("<a href=\"" + i_word + "\">" + i_word + "</a> ");
    }
    else
      this->insertPlainText(i_word + " ");
  }

  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());
  }
}