-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
240 lines (206 loc) · 6.59 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
234
235
236
237
238
239
240
#include "mainwindow.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QSplitter>
#include <QTextEdit>
#include <QPushButton>
#include <QTextStream>
#include <QCoreApplication>
#include <QMessageBox>
#include <QMenu>
#include <QMenuBar>
#include <QInputDialog>
#include <QLineEdit>
#include <QTimer>
#include <QDesktopServices>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent),
chatView_(0),
chatBox_(0),
sendBtn_(0),
host_("192.168.10.118"),
port_("12345"),
connected_(false)
{
init();
}
void MainWindow::init()
{
QCoreApplication::setApplicationName("Project Chat Athens");
QCoreApplication::setApplicationVersion("1.0");
setWindowIcon(QIcon("icon.png"));
QWidget* centralw= new QWidget();
QVBoxLayout* vlayout = new QVBoxLayout();
chatView_ = new QTextEdit(this);
vlayout->addWidget(chatView_);
chatView_->setReadOnly(true);
chatView_->setStyleSheet("background: url(background.jpg) no-repeat bottom;");
QWidget *textzone = new QWidget();
QHBoxLayout* hlayout = new QHBoxLayout;
chatBox_ = new MyTextEdit();
hlayout->addWidget(chatBox_);
sendBtn_ = new QPushButton("Send",this);
hlayout->addWidget(sendBtn_,Qt::AlignRight);
textzone->setLayout(hlayout);
textzone->setFixedHeight(150);
chatView_->setText("Welcome to the chat of the future");
vlayout->addWidget(textzone);
centralw->setLayout(vlayout);
setCentralWidget(centralw);
connect(sendBtn_,SIGNAL(clicked()),this,SLOT(slotBtnSend()));
connect(chatBox_,SIGNAL(signalNewText(const QString&)),this,SLOT(slotSend(const QString&)));
connect(this,SIGNAL(signalNewText(const QString&)),this,SLOT(slotAddText(const QString&)));
connect(this, SIGNAL(signalNewText(const QString&)),this,SLOT(slotSend(const QString&)));
connect(&protocolhandler_,SIGNAL(connected()),this,SLOT(slotConnected()));
connect(&protocolhandler_, SIGNAL(NewMessage(QString)),this,SLOT(slotAddText(QString)));
sendBtn_->setFixedSize(70,50);
initMenu();
}
void MainWindow::initMenu()
{
QMenu* menu = menuBar()->addMenu("&Settings");
QAction* actionSettings = new QAction("Server settings", this);
menu->addAction(actionSettings);
menu = menuBar()->addMenu("&Help");
QAction* actionAbout = new QAction(QString("About ")+QCoreApplication::applicationName(),this);
menu->addAction(actionAbout);
QAction * actionContact = new QAction(QString("Contact me"),this);
menu->addAction(actionContact);
connect(actionSettings, SIGNAL(triggered()), this, SLOT(slotSettings()));
connect(actionAbout, SIGNAL(triggered()), this, SLOT(slotAbout()));
connect(actionContact,SIGNAL(triggered()),this,SLOT(slotContact()));
}
MainWindow::~MainWindow()
{
}
void MainWindow::slotAddText(const QString& text)
{
if (chatView_)
{
if (text.contains(":"))
{
chatView_->append("");
QStringList list = text.split(":");
QString name = list[0];
list.pop_front();
QString rest = list.join(":");
chatView_->moveCursor( QTextCursor::End );
chatView_->moveCursor(QTextCursor::Down);
chatView_->moveCursor(QTextCursor::StartOfLine);
QTextCursor cursor( chatView_->textCursor() );
QTextCharFormat formatName;
formatName.setFontWeight( QFont::DemiBold );
formatName.setForeground( QBrush( QColor( "green" ) ) );
QTextCharFormat formatmsg;
formatmsg.setFontWeight(QFont::Normal);
formatmsg.setForeground(QBrush(QColor("black")));
cursor.setCharFormat( formatName );
cursor.insertText( name );
cursor.setCharFormat(formatmsg);
cursor.insertText(rest);
}
else
chatView_->append(text);
}
}
void MainWindow::slotSend(const QString& text)
{
if(text.size()>0 && text != "\n")
buffer_.append(text);
if(connected_ && buffer_.size() > 0)
{
foreach(QString string, buffer_)
{
protocolhandler_.sendMessage(string);
}
buffer_.clear();
}
}
void MainWindow::slotBtnSend()
{
QString text= chatBox_->toPlainText();
chatBox_->clear();
emit signalNewText(text);
}
void MainWindow::connectToServer()
{
slotAddText("Connecting to server...");
protocolhandler_.connectToServer();
}
void MainWindow::connectDefaults()
{
protocolhandler_.setHostAndPort(host_,port_);
connectToServer();
emit signalNewText("/nick loki");
emit signalNewText("/join #public");
}
void MainWindow::disconnectFromServer()
{
slotAddText("Disconecting...");
protocolhandler_.disconnectFromServer();
slotAddText("Disconnected");
connected_ = false;
}
void MainWindow::slotIncomingText()
{
QString line = protocolhandler_.getTextMsg();
slotAddText(line);
}
void MainWindow::keyPressEvent(QKeyEvent *e)
{
if (e->key() == Qt::Key_Escape)
{
disconnectFromServer();
close();
}
}
void MainWindow::slotSettings()
{
bool ok;
QString text = QInputDialog::getText(this, tr("Enter server address"),
tr("Server Address:"), QLineEdit::Normal,
host_, &ok);
if (ok && !text.isEmpty())
{
host_ = text;
ok = false;
text.clear();
text = QInputDialog::getText(this, tr("Enter server port"),
tr("Port:"), QLineEdit::Normal,
port_, &ok);
if (ok && !text.isEmpty())
{
port_ = text;
disconnectFromServer();
connectDefaults();
}
}
}
void MainWindow::slotAbout()
{
QMessageBox about(this);
QString s;
about.setText(QCoreApplication::applicationName() + " v" + QCoreApplication::applicationVersion()
+ " Author: Loik Le Devehat");
QTextStream ss(&s);
ss<<"This application is a graphical user interface for the athens week c++ course project\n";
ss<<"Gui made with Qt 5.2";
about.setTextFormat(Qt::RichText);
about.setInformativeText(s);
about.setIcon(QMessageBox::Information);
about.adjustSize();
about.exec();
}
void MainWindow::slotConnected()
{
slotAddText("Connection successfull");
connected_ = true;
//send what has been kept when not connected
int size = buffer_.size();
for (int i=0; i<size; i++)
slotSend(""); // flush the buffer
}
void MainWindow::slotContact()
{
QDesktopServices::openUrl(QUrl("http://tinyurl.com/2fcpre6"));
}