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 <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  QString appendMessage = QString(message).toHtmlEscaped();
56  if (appendMessage.endsWith('\n')) // Remove the trailing new line since appendPlainText seems to add one automatically.
57  appendMessage.chop(1);
58  appendMessage.replace('\n', "<br>");
59 
60  // Process colors
61  if (gConfig.doomseeker.bColorizeServerConsole)
62  appendMessage = Strings::colorizeString(appendMessage);
63  else
64  {
65  static const QRegExp colorCode("\034(\\[[a-zA-Z0-9]*\\]|[a-v+\\-!*])");
66  appendMessage.remove(colorCode);
67  }
68 
69  // Emulate append since we need to force HTML on (append auto detects which fails if &lt; or < is not found).
70  d->consoleOutput->moveCursor(QTextCursor::End);
71  d->consoleOutput->insertHtml("<br>" + appendMessage);
72  d->consoleOutput->moveCursor(QTextCursor::End);
73 }
74 
75 void ServerConsole::forwardMessage()
76 {
77  QString msg = d->consoleInput->text();
78  if (msg[0] == ':')
79  msg.replace(0, 1, "say ");
80 
81  emit messageSent(msg);
82  d->consoleInput->setText("");
83 }
84 
86 {
87  d->consoleInput->setFocus();
88 }