serverconsole.cpp
1 //------------------------------------------------------------------------------
2 // serverconsole.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301 USA
19 //
20 //------------------------------------------------------------------------------
21 // Copyright (C) 2009 Braden "Blzut3" Obrzut <admin@maniacsvault.net>
22 //------------------------------------------------------------------------------
23 #include "configuration/doomseekerconfig.h"
24 #include "gui/widgets/memorylineedit.h"
25 #include "serverconsole.h"
26 #include "strings.hpp"
27 #include "ui_serverconsole.h"
28 
29 #include <QRegularExpression>
30 #include <QTimer>
31 
41 static const int BUFFER_FLUSH_GRACE_MSECS = 100;
42 
43 DClass<ServerConsole> : public Ui::ServerConsole
44 {
45 public:
46  MemoryLineEdit *consoleInput;
47  QString buffer;
48  QTimer bufferFlushTimer;
49 };
50 
51 DPointeredNoCopy(ServerConsole)
52 
53 ServerConsole::ServerConsole(QWidget *parent, Qt::WindowFlags f) : QWidget(parent, f)
54 {
55  d->setupUi(this);
56 
57  d->consoleInput = new MemoryLineEdit();
58  layout()->addWidget(d->consoleInput);
59 
60  connect(d->consoleInput, &QLineEdit::returnPressed,
61  this, &ServerConsole::forwardMessage);
62  connect(&d->bufferFlushTimer, &QTimer::timeout,
63  this, &ServerConsole::flushBuffer);
64  d->bufferFlushTimer.setSingleShot(true);
65 }
66 
67 ServerConsole::~ServerConsole() = default;
68 
69 void ServerConsole::appendMessage(const QString &message)
70 {
71  QString appendMessage = QString(message).toHtmlEscaped();
72  if (!appendMessage.endsWith('\n'))
73  appendMessage += '\n';
74  appendMessage.replace('\n', "<br>");
75 
76  // Process colors
77  if (gConfig.doomseeker.bColorizeServerConsole)
78  appendMessage = Strings::colorizeString(appendMessage);
79  else
80  {
81  static const QRegularExpression colorCode("\034(\\[[a-zA-Z0-9]*\\]|[a-v+\\-!*])");
82  appendMessage.remove(colorCode);
83  }
84 
85  d->buffer += appendMessage;
86  if (!d->bufferFlushTimer.isActive())
87  {
88  d->bufferFlushTimer.start(BUFFER_FLUSH_GRACE_MSECS);
89  }
90 }
91 
92 void ServerConsole::flushBuffer()
93 {
94  // Emulate append since we need to force HTML on (append auto detects which fails if &lt; or < is not found).
95  d->consoleOutput->moveCursor(QTextCursor::End);
96  d->consoleOutput->insertHtml(d->buffer);
97  d->consoleOutput->moveCursor(QTextCursor::End);
98  d->buffer.clear();
99 }
100 
101 void ServerConsole::forwardMessage()
102 {
103  QString msg = d->consoleInput->text();
104  if (msg[0] == ':')
105  msg.replace(0, 1, "say ");
106 
107  emit messageSent(msg);
108  d->consoleInput->setText("");
109 }
110 
112 {
113  d->consoleInput->setFocus();
114 }