aboutsummaryrefslogtreecommitdiff
path: root/src/aotextarea.cpp
blob: a42590391319b9e1c9c9e2ecf59d016ad363af5e (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include "aotextarea.h"

AOTextArea::AOTextArea(QWidget *p_parent, int p_log_length) : QTextBrowser(p_parent)
{
  this->document()->setMaximumBlockCount(p_log_length);
}

void AOTextArea::append_linked(QString p_message)
{
  QString result = p_message.toHtmlEscaped()
                       .replace("\n", "<br>")
                       .replace(url_parser_regex, "<a href='\\1'>\\1</a>");
  this->insertHtml(result);
}

void AOTextArea::append_chatmessage(QString p_name, QString p_message,
                                    QString p_name_colour, QString p_color)
{
  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("");
  if (!p_name.isEmpty()) {
    this->insertHtml("<b><font color=" + p_name_colour + ">" + p_name.toHtmlEscaped() +
                     "</font></b>:&nbsp;");

    // cheap workarounds ahoy
    p_message += " ";
  }

  QString result = p_message.toHtmlEscaped()
                       .replace("\n", "<br>")
                       .replace(url_parser_regex, "<a href='\\1'>\\1</a>");

  if (!p_color.isEmpty()) {
    result = "<font color=" + p_color + ">" + result + "</font>";
  }

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