#include "chat.h"
#include "ui_chat.h"
#include <QMessageBox>
int port_test=1;
chat::chat(QWidget *parent) :
QDialog(parent),
ui(new Ui::chat)
{
ui->setupUi(this);//设置 UI设计的界面的控件
m_p_server = NULL;
m_p_serverSocket = NULL;
m_p_clientSocket = NULL;
p_my_dlg = new MainDlg;//主界面
p_my_dlg->show(); //显示主界面
/*
*连接信号
*/
//主窗体中显示本窗体信号
connect(p_my_dlg, SIGNAL(showChatWindow()), this, SLOT(showAndHideSlot()));
connect(p_my_dlg, SIGNAL(sendEnterMessage(QString , QString)),
this,SLOT(enterSlot(QString , QString )));
connect(ui->writeMessageEdit, SIGNAL(textChanged()), this, SLOT(changeButtonStateSlot()));
connect(ui->aboutButton, SIGNAL(clicked()), this, SLOT(createAboutSlot()));
connect(ui->sendButton, SIGNAL(clicked()), this, SLOT(appendMessageSlot()));
}
chat::~chat()
{
delete ui;
}
//本窗体创建时由系统自动调用
void chat::changeEvent(QEvent *e)
{
QDialog::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);//设置本窗体的一些属性
break;
default:
break;
}
}
//显示关闭主窗体,显示本窗体
void chat::showAndHideSlot()
{
delete p_my_dlg;
this->show();
}
//显示关于信息
void chat::createAboutSlot()
{
QMessageBox mess;
mess.setFont(QFont("Sans Serif", 12, 50));
mess.warning(this,
QString::fromLocal8Bit("About"),
QString::fromLocal8Bit("<font size=5>An easy C/S Chatroom based on QT</font>"));
}
//根据选择p_my_dlg->m_bool_server 选择相应操作
void chat::enterSlot(QString name, QString host)
{
port = 22222;//设置Socket端口号
m_bool_server = p_my_dlg->m_bool_server;//选择状态传送
if (m_bool_server)
{
socketServer();
}
else
{
socketClient(host);
}
ui->sendButton->setDisabled(false);//发送按钮不能使用
m_str_userName = name;
ui->onlineMessageList->addItem(name);
}
//如果输入框为空,则发送按钮不能使用
void chat::changeButtonStateSlot()
{
bool boo_dis = ui->writeMessageEdit->toPlainText().isEmpty();
ui->sendButton->setDisabled(boo_dis);
}
//发送信息
void chat::appendMessageSlot()
{
//获取输入框内容,然后清空
QString content = ui->writeMessageEdit->toPlainText();
if (content.isEmpty())
{
QMessageBox::warning(this, "出错", QString::fromLocal8Bit("发送的内容不能为空"));
return;
}
nowDateTime = QDateTime::currentDateTime();//获取时间
//按指定格式拼合字符串
ui->showMessageEdit->append(
QString("\n[%1] %2 %3\n%4")
.arg(nowDateTime.toString("yyyy-MM-dd hh:mm:ss"))
.arg(m_str_userName)
.arg(QString::fromLocal8Bit(" :"))
.arg(content) );
QDataStream out;//流
if (m_bool_server)
{
out.setDevice(m_p_serverSocket);
}
else
{
out.setDevice(m_p_clientSocket);
}
int mark = 0;//普通信息代号
//发送代号+用户名=信息
out << mark;
out << m_str_userName << content;
ui->writeMessageEdit->clear();
}
//创建服务器Socket 监听连接
void chat::socketServer()
{
m_p_server = new QTcpServer(this);//创建TCP 服务器用于监听socket连接
m_p_serverSocket = new QTcpSocket(this);//创建套接字
m_p_server->listen(QHostAddress::Any, port);//监听 socket连接
//设置 有新连接信号,触发函数
connect(m_p_server, SIGNAL(newConnection()), this, SLOT(newConnectionSlot()));
}
//有新的连接时。槽
void chat::newConnectionSlot()
{
m_p_serverSocket = m_p_server->nextPendingConnection();
//有数据来时,
connect(m_p_serverSocket, SIGNAL(readyRead()), this, SLOT(newDataSlot()));
connect(m_p_serverSocket, SIGNAL(disconnected()), this, SLOT(deleNameSlot()));
}
//创建客户端Socket 与服务器连接
void chat::socketClient(QString host)
{
m_p_clientSocket = new QTcpSocket(this);
m_p_clientSocket->connectToHost(host, port);
//当成功连接时,名字list框中加入用户名
connect(m_p_clientSocket, SIGNAL(connected()), this, SLOT(addSlot()));
connect(m_p_clientSocket, SIGNAL(readyRead()), this, SLOT(newDataSlot()));
connect(m_p_clientSocket, SIGNAL(disconnected()), this, SLOT(deleNameSlot()));
}
//在名字list框中加入用户名
void chat::addSlot()
{
QString content;
QDataStream out(m_p_clientSocket);//Qt socket 数据传输的方式
int mark = 11;
//把socket看成流使用
out << mark;//发送代号11+用户名, 告诉服务器,连接成功了,
out << m_str_userName << content;
}
//接收信息
void chat::newDataSlot()
{
int mark;
QString hisName;
QString content;
QDataStream in;//流对象
if (m_bool_server)
{
in.setDevice(m_p_serverSocket);//把服务器的Sokcet当成流
}
else
{
in.setDevice(m_p_clientSocket);//把客户端的Sokcet当成流
}
in >> mark;//读取代号
// 如果代号是11,则是客户端加入。添加用户名
if (mark == 11) //addList
{
in >> hisName >> content;
if (content.isEmpty())
{
ui->onlineMessageList->addItem(hisName);//添加用户名
if (m_bool_server)
{
QString content;
QDataStream out(m_p_serverSocket);//执行相同代号,让客户端也添加用户名
int mark = 11;
out << mark;
out << m_str_userName<< content;
}
}
return;
}
//代号0.普通信息
else if (mark == 0) //message
{
in >> hisName >> content;//读信息
nowDateTime = QDateTime::currentDateTime();//时间
//按照指定格式,拼合 显示信息,
ui->showMessageEdit->append(
QString("\n[%1] %2 %3\n%4")
.arg(nowDateTime.toString("yyyy-MM-dd hh:mm:ss"))
.arg(hisName)
.arg(QString::fromLocal8Bit(" :"))
.arg(content) );
return;
}
}
//断开连接时,删除用户名
void chat::deleNameSlot()
{
ui->onlineMessageList->takeItem(1);
}