-
Notifications
You must be signed in to change notification settings - Fork 0
/
GitChat.cpp
107 lines (89 loc) · 2.8 KB
/
GitChat.cpp
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include "GitChat.h"
#include "Git.h"
#include "ListViewDelegate.h"
#include <QScrollBar>
#include <QMessageBox>
#include <chrono>
#include "./ui_GitChat.h"
using namespace std::literals;
GitChat::GitChat(Git& git)
: QMainWindow(nullptr)
, m_git(git)
, ui(new Ui::GitChat)
{
ui->setupUi(this);
qRegisterMetaType<MessageType>();
ui->listView->setResizeMode(QListView::Adjust);
ui->listView->setWordWrap(true);
ui->listView->setModel(&m_model);
ui->listView->setMinimumSize(200,350);
ui->listView->setItemDelegate(new ListViewDelegate());
connect(this, &GitChat::SignalAddMessages, this, &GitChat::AddMessages);
connect(this, &GitChat::SignalGitThreadError, this, &GitChat::GitThreadError, Qt::ConnectionType::BlockingQueuedConnection);
StartGitListeningThread();
}
GitChat::~GitChat()
{
m_running = false;
if(m_gitThread.joinable())
m_gitThread.join();
delete ui;
}
void GitChat::StartGitListeningThread()
{
m_running = true;
m_gitThread = std::thread{[this]{
try {
while(m_running)
{
auto messages = this->m_git.GetNewMessages();
emit SignalAddMessages(std::move(messages));
std::this_thread::sleep_for(4s);
}
} catch (const std::exception& err) {
emit SignalGitThreadError(QString{"Error: "} + err.what());
}
}};
}
void GitChat::AddMessages(std::queue<std::pair<QString, QString>> messages)
{
while(!messages.empty())
{
auto& message = messages.front();
if(m_git.GetLogin() == message.first)
AddOutgoingMessage(message.first, message.second);
else
AddIncomingMessage(message.first, message.second);
messages.pop();
}
ui->listView->scrollToBottom();
}
void GitChat::GitThreadError(const QString& message)
{
QMessageBox msgBox;
msgBox.setFixedSize(500,200);
msgBox.critical(nullptr, "Error: ", message);
std::terminate();
}
void GitChat::AddIncomingMessage(const QString& author, const QString& message)
{
auto item = std::make_unique<QStandardItem>(QString{"\""} + author + QString{"\""} + QString{": "} + message);
item->setData("Incoming", Qt::UserRole + 1);
m_model.appendRow(item.release());
}
void GitChat::AddOutgoingMessage(const QString& author, const QString& message)
{
auto item = std::make_unique<QStandardItem>(QString{"\""} + author + QString{"\""} + QString{": "} + message);
item->setData("Outgoing", Qt::UserRole + 1);
m_model.appendRow(item.release());
}
void GitChat::on_lineEdit_returnPressed()
{
m_git.PushMessage(m_git.GetLogin(), ui->lineEdit->text());
AddOutgoingMessage(m_git.GetLogin(), ui->lineEdit->text());
ui->lineEdit->clear();
}
void GitChat::on_GitChat_destroyed()
{
std::exit(0);
}