serverconsole.cpp
1 //------------------------------------------------------------------------------
2 // serverconsole.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // This program 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
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; 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 "Blzut3" <admin@maniacsvault.net>
22 //------------------------------------------------------------------------------
23 #include "configuration/doomseekerconfig.h"
24 #include "gui/widgets/memorylineedit.h"
25 #include "serverconsole.h"
26 #include "ui_serverconsole.h"
27 #include "strings.h"
28 
29 #include <QRegExp>
30 
31 DClass<ServerConsole> : public Ui::ServerConsole
32 {
33 public:
34  MemoryLineEdit *consoleInput;
35 };
36 
37 DPointered(ServerConsole)
38 
39 ServerConsole::ServerConsole(QWidget *parent, Qt::WindowFlags f) : QWidget(parent, f)
40 {
41  d->setupUi(this);
42 
43  d->consoleInput = new MemoryLineEdit();
44  layout()->addWidget(d->consoleInput);
45 
46  connect(d->consoleInput, SIGNAL(returnPressed()), this, SLOT(forwardMessage()));
47 }
48 
49 ServerConsole::~ServerConsole()
50 {
51 }
52 
53 void ServerConsole::appendMessage(const QString &message)
54 {
55 #if QT_VERSION >= 0x050000
56  QString appendMessage = QString(message).toHtmlEscaped();
57 #else
58  QString appendMessage = Qt::escape(message);
59 #endif
60  if(appendMessage.endsWith('\n')) // Remove the trailing new line since appendPlainText seems to add one automatically.
61  appendMessage.chop(1);
62  appendMessage.replace('\n', "<br>");
63 
64  // Process colors
65  if(gConfig.doomseeker.bColorizeServerConsole)
66  appendMessage = Strings::colorizeString(appendMessage);
67  else
68  {
69  static const QRegExp colorCode("\034(\\[[a-zA-Z0-9]*\\]|[a-v+\\-!*])");
70  appendMessage.remove(colorCode);
71  }
72 
73  // Emulate append since we need to force HTML on (append auto detects which fails if &lt; or < is not found).
74  d->consoleOutput->moveCursor(QTextCursor::End);
75  d->consoleOutput->insertHtml("<br>" + appendMessage);
76  d->consoleOutput->moveCursor(QTextCursor::End);
77 }
78 
79 void ServerConsole::forwardMessage()
80 {
81  QString msg = d->consoleInput->text();
82  if(msg[0] == ':')
83  msg.replace(0, 1, "say ");
84 
85  emit messageSent(msg);
86  d->consoleInput->setText("");
87 }
88 
90 {
91  d->consoleInput->setFocus();
92 }
Stores the input into a history that can be accessed by pressing up and down.
void setFocus()
Sets keyboard focus to the underlying line edit widget.
static QString colorizeString(const QString &str, int def=4)
Definition: strings.cpp:50