1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-18 17:40:48 +02:00
vcmi/launcher/lobby/chat_moc.cpp

164 lines
3.7 KiB
C++
Raw Normal View History

2023-09-03 01:05:48 +02:00
#include "chat_moc.h"
#include "ui_chat_moc.h"
Chat::Chat(QWidget *parent) :
QWidget(parent),
ui(new Ui::Chat)
{
ui->setupUi(this);
namesCompleter.setModel(ui->listUsers->model());
namesCompleter.setCompletionMode(QCompleter::InlineCompletion);
ui->messageEdit->setCompleter(&namesCompleter);
for(auto i : {GLOBAL, ROOM})
chatDocuments.push_back(new QTextDocument(this));
2023-09-03 14:47:34 +02:00
setChatId(GLOBAL);
2023-09-03 01:05:48 +02:00
}
Chat::~Chat()
{
delete ui;
}
void Chat::setUsername(const QString & user)
{
username = user;
}
void Chat::setSession(const QString & s)
{
session = s;
2023-09-03 14:47:34 +02:00
on_chatSwitch_clicked();
}
void Chat::setChannel(const QString & channel)
{
static const QMap<QString, ChatId> chatNames{{"global", GLOBAL}, {"room", ROOM}};
setChatId(chatNames.value(channel));
2023-09-03 01:05:48 +02:00
}
void Chat::addUser(const QString & user)
{
ui->listUsers->addItem(new QListWidgetItem("@" + user));
}
void Chat::clearUsers()
{
ui->listUsers->clear();
}
2023-09-03 14:47:34 +02:00
void Chat::chatMessage(const QString & title, const QString & channel, QString body, bool isSystem)
2023-09-03 01:05:48 +02:00
{
const QTextCharFormat regularFormat;
const QString boldHtml = "<b>%1</b>";
const QString colorHtml = "<font color=\"%1\">%2</font>";
bool meMentioned = false;
bool isScrollBarBottom = (ui->chat->verticalScrollBar()->maximum() - ui->chat->verticalScrollBar()->value() < 24);
2023-09-03 14:47:34 +02:00
static const QMap<QString, ChatId> chatNames{{"global", GLOBAL}, {"room", ROOM}};
QTextDocument * doc = ui->chat->document();
if(chatNames.contains(channel))
doc = chatDocuments[chatNames.value(channel)];
QTextCursor curs(doc);
2023-09-03 01:05:48 +02:00
curs.movePosition(QTextCursor::End);
QString titleColor = "Olive";
if(isSystem || title == "System")
titleColor = "ForestGreen";
if(title == username)
titleColor = "Gold";
curs.insertHtml(boldHtml.arg(colorHtml.arg(titleColor, title + ": ")));
QRegularExpression mentionRe("@[\\w\\d]+");
auto subBody = body;
int mem = 0;
for(auto match = mentionRe.match(subBody); match.hasMatch(); match = mentionRe.match(subBody))
{
body.insert(mem + match.capturedEnd(), QChar(-1));
body.insert(mem + match.capturedStart(), QChar(-1));
mem += match.capturedEnd() + 2;
subBody = body.right(body.size() - mem);
}
auto pieces = body.split(QChar(-1));
for(auto & block : pieces)
{
if(block.startsWith("@"))
{
if(block == "@" + username)
{
meMentioned = true;
curs.insertHtml(boldHtml.arg(colorHtml.arg("IndianRed", block)));
}
else
curs.insertHtml(colorHtml.arg("DeepSkyBlue", block));
}
else
{
if(isSystem)
curs.insertHtml(colorHtml.arg("ForestGreen", block));
else
curs.insertText(block, regularFormat);
}
}
curs.insertText("\n", regularFormat);
2023-09-03 14:47:34 +02:00
if(doc == ui->chat->document() && (meMentioned || isScrollBarBottom))
2023-09-03 01:05:48 +02:00
{
ui->chat->ensureCursorVisible();
ui->chat->verticalScrollBar()->setValue(ui->chat->verticalScrollBar()->maximum());
}
}
2023-09-03 14:47:34 +02:00
void Chat::chatMessage(const QString & title, QString body, bool isSystem)
{
chatMessage(title, "", body, isSystem);
}
2023-09-03 01:05:48 +02:00
void Chat::sysMessage(QString body)
{
chatMessage("System", body, true);
}
2023-09-03 14:47:34 +02:00
void Chat::sendMessage()
2023-09-03 01:05:48 +02:00
{
2023-09-03 14:47:34 +02:00
QString msg(ui->messageEdit->text());
2023-09-03 01:05:48 +02:00
ui->messageEdit->clear();
2023-09-03 14:47:34 +02:00
emit messageSent(msg);
}
void Chat::on_messageEdit_returnPressed()
{
sendMessage();
2023-09-03 01:05:48 +02:00
}
void Chat::on_sendButton_clicked()
{
2023-09-03 14:47:34 +02:00
sendMessage();
2023-09-03 01:05:48 +02:00
}
void Chat::on_chatSwitch_clicked()
{
2023-09-03 14:47:34 +02:00
static const QMap<ChatId, QString> chatNames{{GLOBAL, "global"}, {ROOM, "room"}};
2023-09-03 01:05:48 +02:00
if(chatId == GLOBAL && !session.isEmpty())
2023-09-03 14:47:34 +02:00
emit channelSwitch(chatNames[ROOM]);
2023-09-03 01:05:48 +02:00
else
2023-09-03 14:47:34 +02:00
emit channelSwitch(chatNames[GLOBAL]);
2023-09-03 01:05:48 +02:00
}
void Chat::setChatId(ChatId _chatId)
{
static const QMap<ChatId, QString> chatNames{{GLOBAL, "Global"}, {ROOM, "Room"}};
chatId = _chatId;
ui->chatSwitch->setText(chatNames[chatId] + " chat");
ui->chat->setDocument(chatDocuments[chatId]);
}