#include "qqchat.h"
#include "ui_qqchat.h"
#include<QDataStream>
#include<QMessageBox>
#include<QDateTime>
#include<QTableWidget>
#include<QComboBox>
#include<QFont>
#include<QColorDialog>
#include<QFileDialog>
#include<QDebug>
QQChat::QQChat(QWidget *parent,QString name) :
QMainWindow(parent),
ui(new Ui::QQChat)
{
ui->setupUi(this);
this->myname=name;
port=9999;
udpSocket=new QUdpSocket(this);
//允许其他服务器连接相同端口和地址|重新连接服务器
udpSocket->bind(port,QUdpSocket::ShareAddress|QUdpSocket::ReuseAddressHint);
//发送按钮
connect(ui->sendBtn_5,&QPushButton::clicked,this,[=](){
sendMsg(Msg);
});
//监听别人发送的数据
connect(udpSocket,&QUdpSocket::readyRead,this,&QQChat::receiveMessage);
//新用户进入
sendMsg(UserEnter);
connect(ui->exitBtn_5,&QPushButton::clicked,this,[=](){
this->close();
});
//字体
connect(ui->fontCbx_5,&QFontComboBox::currentFontChanged,this,[=](const QFont &font){
//设置全局字体
ui->msgTxtEdit_5->setFontFamily(font.toString());
ui->msgTxtEdit_5->setFocus();
});
//字体大小
void (QComboBox:: *sizeBtn)(const QString &text)=&QComboBox::currentTextChanged;
connect(ui->sizeCbx_5,sizeBtn,this,[=](const QString &text){
ui->msgTxtEdit_5->setFontPointSize(text.toDouble());
ui->msgTxtEdit_5->setFocus();
});
//加粗
connect(ui->boldBtn_5,&QToolButton::clicked,this,[=](bool checked){
//是否按下
if(checked){
ui->msgTxtEdit_5->setFontWeight(QFont::Bold);
}else{
ui->msgTxtEdit_5->setFontWeight(QFont::Normal);
}
});
//倾斜
connect(ui->italicTbtn_5,&QToolButton::clicked,this,[=](bool checked){
ui->msgTxtEdit_5->setFontItalic(checked);
ui->msgTxtEdit_5->setFocus();
});
//下划线
connect(ui->underlineTbtn_5,&QToolButton::clicked,this,[=](bool checked){
ui->msgTxtEdit_5->setFontUnderline(checked);
ui->msgTxtEdit_5->setFocus();
});
//清空
connect(ui->clearTbtn_5,&QToolButton::clicked,this,[=](){
ui->msgBrowser_5->clear();
});
//字体颜色
connect(ui->colorTbtn_5,&QToolButton::clicked,this,[=](){
QColor color=QColorDialog::getColor(color,this);
ui->msgTxtEdit_5->setTextColor(color);
});
//保存聊天记录
connect(ui->saveTbtn_5,&QToolButton::clicked,this,[=](){
if(ui->msgBrowser_5->toPlainText().isEmpty()){
QMessageBox::warning(this,"警告","保存内容不能为空");
return;
}
//获取保存文件绝对路径/文件名(窗口标题,文件名,类型)
QString fileName=QFileDialog::getSaveFileName(this,"保存聊天记录","聊天记录","(*.txt)");
if(!fileName.isEmpty()){
QFile file(fileName);
file.open(QIODevice::WriteOnly|QFile::Text);
QTextStream stream(&file);
stream<<ui->msgBrowser_5->toPlainText();
file.close();
}
});
}
QQChat::~QQChat()
{
delete ui;
}
void QQChat::closeEvent(QCloseEvent *)
{
emit closeWidget();
sendMsg(UserLeft);
udpSocket->close();
udpSocket->destroyed();
}
void QQChat::sendMsg(QQChat::msgType type)
{
QByteArray array;
QDataStream stream(&array,QIODevice::WriteOnly);
stream<<type<<this->getName();
switch (type)
{
case Msg:
if(ui->msgTxtEdit_5->toPlainText()=="")
{
QMessageBox::warning(this,"警告","发送的聊天内容不能为空!");
return;
}
stream<<this->getMsg();
break;
case UserEnter:
break;
case UserLeft:
break;
}
//书写报文
udpSocket->writeDatagram(array.data(),array.size(),QHostAddress::Broadcast,this->port);
}
QString QQChat::getName()
{
return myname;
}
QString QQChat::getMsg()
{
QString msg=ui->msgTxtEdit_5->toHtml();
ui->msgTxtEdit_5->clear();
ui->msgTxtEdit_5->setFocus();
return msg;
}
void QQChat::userEnter(QString username)
{
bool IsEmpty=ui->tableWidget_5->findItems(username,Qt::MatchExactly).isEmpty();
if(IsEmpty)
{
QTableWidgetItem *table=new QTableWidgetItem(username);
ui->tableWidget_5->insertRow(0);
ui->tableWidget_5->setItem(0,0,table);
ui->msgBrowser_5->setTextColor(QColor(Qt::gray));
ui->msgBrowser_5->append(username+"已上线");
ui->userNumLbl_5->setText(QString("在线人数:%1").arg(ui->tableWidget_5->rowCount()));
sendMsg(UserEnter);
}
}
void QQChat::userLeft(QString username, QString time)
{
bool isEmpty=ui->tableWidget_5->findItems(username,Qt::MatchExactly).isEmpty();
if(!isEmpty){
int row=ui->tableWidget_5->findItems(username,Qt::MatchExactly).first()->row();
ui->tableWidget_5->removeRow(row);
ui->msgBrowser_5->append(username+"用户于"+time+"离开");
ui->userNumLbl_5->setText(QString("在线人数:%1").arg(ui->tableWidget_5->rowCount()));
}
}
void QQChat::receiveMessage()
{
qint64 size=udpSocket->pendingDatagramSize();
//int mysize= static_cast<int>(size);
QByteArray array=QByteArray(size,0);
udpSocket->readDatagram(array.data(),size);
QDataStream stream(&array,QIODevice::ReadOnly);
int mytype;
QString name,msg;//用户名 聊天内容
QString time=QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
stream>>mytype;
switch (mytype) {
case Msg:
stream>>name>>msg;
ui->msgBrowser_5->setTextColor(QColor(Qt::blue));
ui->msgBrowser_5->append("["+name+"]"+time);
ui->msgBrowser_5->append(msg);
break;
case UserLeft:
stream>>name;
userLeft(name,time);
break;
case UserEnter:
stream>>name;
userEnter(name);
break;
}
}