-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
233 lines (196 loc) · 8.02 KB
/
mainwindow.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->chatTimeout = true;
this->connected = false;
this->currColorId = 2; // czarny
this->lastColorButton = ui->blackPushButton;
this->graphicsScene = new QGraphicsScene();
ui->graphicsView->setScene(graphicsScene);
this->currentPen.setColor(QColor("black"));
connect(ui->graphicsView,SIGNAL(drawPoint(QPoint)),this,SLOT(drawPoint(QPoint)));
connect(ui->graphicsView,SIGNAL(drawLineTo(QPoint)),this,SLOT(drawLineTo(QPoint)));
connect(ui->clearPushButton,SIGNAL(clicked()),this,SLOT(clearView()));
connect(ui->redPushButton,SIGNAL(clicked()),this,SLOT(switchColor()));
connect(ui->greenPushButton,SIGNAL(clicked()),this,SLOT(switchColor()));
connect(ui->blackPushButton,SIGNAL(clicked()),this,SLOT(switchColor()));
connect(ui->sendMessagePushButton,SIGNAL(clicked()),this,SLOT(sendMessage()));
connect(ui->chatLineEdit,SIGNAL(returnPressed()),this,SLOT(sendMessage()));
this->socket = new KalSocket();
this->chatTimer = new QTimer();
connect(socket,SIGNAL(connected()),this,SLOT(connectionSuccess()));
connect(socket,SIGNAL(error(QAbstractSocket::SocketError)),
this,SLOT(displayError(QAbstractSocket::SocketError)));
connect(socket,SIGNAL(pointsReceived(int,int,int,KalSocket::DrawType)),
this,SLOT(pointsReceived(int,int,int,KalSocket::DrawType)));
connect(socket,SIGNAL(clear()),graphicsScene,SLOT(clear()));
connect(socket,SIGNAL(someoneLoggedIn(QString)),this,SLOT(someoneLoggedIn(QString)));
connect(socket,SIGNAL(chatMessage(QString,QString)),this,SLOT(addMessage(QString,QString)));
connect(socket,SIGNAL(nicknames(QList<QString>)),this,SLOT(getNicknames(QList<QString>)));
connect(socket,SIGNAL(logout(QString)),this,SLOT(someoneLoggedOut(QString)));
connect(socket,SIGNAL(drawStart(QString)),this,SLOT(drawStart(QString)));
connect(socket,SIGNAL(gotSettings(ServerSettings)),this,SLOT(getSettings(ServerSettings)));
connect(socket,SIGNAL(errorMessage(QString)),this,SLOT(showMessage(QString)));
connect(ui->wantDrawCheckBox,SIGNAL(toggled(bool)),this,SLOT(wantDrawToggled(bool)));
// actiony z menu: Polaczenie
connect(ui->actionPo_cz,SIGNAL(triggered()),this,SLOT(connectWindowExec())); // laczymy sie
connect(ui->actionRoz_cz,SIGNAL(triggered()),this,SLOT(disconnect())); // rozlaczamy sie
connect(ui->actionZako_cz,SIGNAL(triggered()),this,SLOT(close())); // koniec programu
// timer
connect(chatTimer,SIGNAL(timeout()),this,SLOT(timeout()));
connect(ui->progressBar,SIGNAL(full()),this,SLOT(drawTimeout()));
// inne
this->disableActions(false); // Polacz - aktywne, Rozlacz - nieaktywne
}
MainWindow::~MainWindow()
{
delete socket;
delete chatTimer;
delete graphicsScene;
delete ui;
}
void MainWindow::getSettings(ServerSettings settings) {
this->serverSettings = settings;
}
void MainWindow::drawPoint(QPoint point) {
this->previousPoint = point;
this->graphicsScene->addEllipse(
point.x(),point.y(),1,1,currentPen,QBrush(currentPen.color(),Qt::SolidPattern));
if(!sender()->objectName().isNull()) {
this->socket->runCommand(new DrawCommand(DrawCommand::POINT,point.x(),point.y(),this->currColorId));
}
}
void MainWindow::drawLineTo(QPoint point) {
this->graphicsScene->addLine(this->previousPoint.x(),this->previousPoint.y(),
point.x(),point.y(),this->currentPen);
this->previousPoint = point;
if(!sender()->objectName().isNull()) {
this->socket->runCommand(new DrawCommand(DrawCommand::LINE,point.x(),point.y(),this->currColorId));
}
}
void MainWindow::clearView() {
this->socket->runCommand(new ClearCommand());
this->graphicsScene->clear();
}
void MainWindow::switchColor() {
this->lastColorButton->setEnabled(true);
this->lastColorButton = dynamic_cast<QPushButton*>(this->sender());
this->lastColorButton->setEnabled(false);
if(sender()->objectName().startsWith("red")) {
this->currentPen.setColor(QColor("red"));
this->currColorId = 0;
}
else if(sender()->objectName().startsWith("green")) {
this->currentPen.setColor(QColor("green"));
this->currColorId = 1;
}
else {
this->currentPen.setColor(QColor("black"));
this->currColorId = 2;
}
}
void MainWindow::switchColor(int color) {
this->currColorId = color;
if(color == 0) {
this->currentPen.setColor(QColor("red"));
}
else if(color == 1) {
this->currentPen.setColor(QColor("green"));
}
else this->currentPen.setColor(QColor("black"));
}
void MainWindow::displayError(QAbstractSocket::SocketError) {
if(this->connected) ui->loginInfoLabel->setText(tr("Nie jestes zalogowany."));
this->connected = false;
this->showMessage("Wystapil blad polaczenia!");
}
void MainWindow::pointsReceived(int x, int y, int color, KalSocket::DrawType type) {
this->switchColor(color);
if(type == KalSocket::POINT) this->drawPoint(QPoint(x,y));
else this->drawLineTo(QPoint(x,y));
}
void MainWindow::connectWindowExec() {
LoginDialog *loginDialog = new LoginDialog(this);
connect(loginDialog,SIGNAL(finished(QTrio<QString,QHostAddress,int>)),
this,SLOT(connectToHost(QTrio<QString,QHostAddress,int>)));
loginDialog->exec();
}
void MainWindow::connectToHost(QTrio<QString,QHostAddress,int> trio) {
this->socket->connectToHost(trio.second,trio.third);
this->loginName = trio.first;
}
void MainWindow::connectionSuccess() {
this->connected = true;
this->socket->setNickname(this->loginName);
this->socket->runCommand(new LoginCommand(this->loginName));
ui->loginInfoLabel->setText(tr("Jestes zalogowany jako: ") + this->loginName);
this->disableActions(true);
}
void MainWindow::someoneLoggedIn(QString login) {
ui->usersListWidget->addItem(login);
}
void MainWindow::sendMessage() {
if(this->chatTimeout) {
QString message = ui->chatLineEdit->text();
ui->chatLineEdit->clear();
this->socket->runCommand(new SendMessageCommand(message));
this->addMessage(this->loginName,message);
this->chatTimeout = false;
chatTimer->setInterval(2000);
chatTimer->start();
}
}
void MainWindow::timeout() {
this->chatTimeout = true;
}
void MainWindow::addMessage(QString nickname, QString message) {
ui->chatTextBrowser->setHtml(ui->chatTextBrowser->toHtml()
+ nickname + ": " + message + "<br/>");
ui->chatTextBrowser->verticalScrollBar()->setValue(ui->chatTextBrowser->verticalScrollBar()->maximum());
}
void MainWindow::wantDrawToggled(bool toggle) {
this->socket->runCommand(new WantDrawCommand(toggle));
}
void MainWindow::getNicknames(QList<QString> list) {
this->nicknames = list;
QString nickname;
ui->usersListWidget->clear();
foreach(nickname,nicknames) {
ui->usersListWidget->addItem(nickname);
}
}
void MainWindow::someoneLoggedOut(QString nickname) {
// usuwanie z listy nickow
ui->usersListWidget->removeItemByString(nickname);
}
void MainWindow::disconnect() {
this->socket->close();
this->disableActions(false);
}
void MainWindow::disableActions(bool actionFlag) {
ui->actionPo_cz->setEnabled(1-actionFlag);
ui->actionRoz_cz->setEnabled(actionFlag);
}
void MainWindow::drawStart(QString word) {
this->clearView();
ui->graphicsView->setInteractive(true);
ui->passwordLabel->setText(word);
// reszta spraw obslugi otrzymania slowa, ustawienie timera, etc.
ui->progressBar->setInterval(serverSettings.getDrawInterval());
ui->progressBar->start();
}
void MainWindow::drawTimeout() {
ui->graphicsView->setInteractive(false);
}
void MainWindow::showMessage(QString message, QMessageBox::Icon icon) const {
QMessageBox msgBox;
msgBox.setWindowTitle(tr("Wystapil blad"));
msgBox.setText(message);
msgBox.setIcon(icon);
msgBox.exec();
}