305 lines
8.8 KiB
C++
305 lines
8.8 KiB
C++
#include "mainwidget.h"
|
|
#include "ui_mainwidget.h"
|
|
|
|
MainWidget::MainWidget(QString username,QWidget *parent) :
|
|
QWidget(parent),
|
|
ui(new Ui::MainWidget)
|
|
{
|
|
ui->setupUi(this);
|
|
this->setWindowIcon(QIcon(":/icon.png"));
|
|
this->setWindowTitle("TCP聊天室");
|
|
ui->name_list->setEditTriggers(QAbstractItemView::NoEditTriggers); //不允许编辑
|
|
ui->name_list->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
|
|
ui->name_list->verticalScrollBar()->setStyleSheet("QScrollBar{width:16px;}");
|
|
ui->textEdit->setReadOnly(true);
|
|
ui->send_btn->setFocus();
|
|
ui->send_btn->setDefault(true);
|
|
ui->msg_edit->installEventFilter(this);//设置完后自动调用其eventFilter函数
|
|
this->username = username;
|
|
}
|
|
|
|
MainWidget::~MainWidget()
|
|
{
|
|
delete ui;
|
|
}
|
|
|
|
void MainWidget::on_send_text_clicked()
|
|
{
|
|
QString homePath = QDir::homePath();
|
|
QString filePath = QFileDialog::getOpenFileName(this, tr("Open a file"), homePath, tr("All files (*.*)"), NULL, QFileDialog::DontResolveSymlinks);
|
|
|
|
QString filename = filePath.section("/", -1);
|
|
|
|
if (filename.isEmpty())
|
|
return;
|
|
|
|
QMessageBox::StandardButton reply;
|
|
reply = QMessageBox::question(this, "发送文件", tr("Do you want to send \"%1\"?").arg(filename));
|
|
|
|
if (reply == QMessageBox::No)
|
|
return;
|
|
else
|
|
{
|
|
QString currentTime = QTime::currentTime().toString("H:mm A");
|
|
ui->textEdit->append("<font color=\"MediumBlue\"><b>" + username + " [" + currentTime + "] send a file \"" + filename + "\"</b></font>");
|
|
|
|
QString name = ui->msg_edit->text();
|
|
if(name.isEmpty()) //群发
|
|
{
|
|
this->sendFile(filePath,filename);
|
|
}
|
|
else //私发文件
|
|
{
|
|
this->sendFile(filePath,filename,name);
|
|
}
|
|
QProgressDialog *progressDlg = new QProgressDialog(this);
|
|
progressDlg->setWindowModality(Qt::WindowModal);
|
|
progressDlg->setMinimumDuration(0);
|
|
progressDlg->setAttribute(Qt::WA_DeleteOnClose, true);
|
|
progressDlg->setWindowTitle(tr("上传文件"));
|
|
progressDlg->setLabelText(tr("正在上传......"));
|
|
progressDlg->setCancelButtonText(tr("取消"));
|
|
progressDlg->setRange(0, 8000);
|
|
for (int i = 1; i < 8000; i++)
|
|
{
|
|
progressDlg->setValue(i);
|
|
if (progressDlg->wasCanceled())
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
progressDlg->close();
|
|
}
|
|
}
|
|
|
|
void MainWidget::on_save_btn_clicked()
|
|
{
|
|
QFile file("memory.txt");
|
|
if (file.exists()) //文件存在
|
|
{
|
|
QMessageBox::StandardButton reply;
|
|
reply = QMessageBox::question(this, "提示", tr("是否覆盖之前的聊天记录?"));
|
|
if (reply == QMessageBox::No)
|
|
{
|
|
if (file.open(QIODevice::WriteOnly | QIODevice::Append))
|
|
{
|
|
QTextStream stream(&file);
|
|
//转换为字符串
|
|
QString str = ui->textEdit->toPlainText();
|
|
str = "\n\n" + str;
|
|
//写入文本流
|
|
stream << str;
|
|
//关闭文件
|
|
file.close();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (file.open(QIODevice::WriteOnly | QIODevice::Text))
|
|
{
|
|
QTextStream stream(&file);
|
|
//转换为字符串
|
|
QString str = ui->textEdit->toPlainText();
|
|
//写入文本流
|
|
stream << str;
|
|
//关闭文件
|
|
file.close();
|
|
}
|
|
}
|
|
}
|
|
|
|
else
|
|
{
|
|
QMessageBox::warning(this, "提示", tr("打开文件失败,已为您自动创建文件!"));
|
|
file.open(QIODevice::WriteOnly | QIODevice::Append);
|
|
}
|
|
}
|
|
|
|
void MainWidget::on_send_btn_clicked()
|
|
{
|
|
QString text;
|
|
text = ui->msg_edit->text();
|
|
if(text.isEmpty())
|
|
{
|
|
QMessageBox::warning(this, "发送失败", "输入不能为空!");
|
|
return;
|
|
}
|
|
ui->msg_edit->clear();
|
|
QString currentTime = QTime::currentTime().toString("H:m A");
|
|
ui->textEdit->append("<font color=\"MediumBlue\"><b>" + username + " [" + currentTime + "] :</b> " + text + "</font>");
|
|
if(text[0] == '@')
|
|
{
|
|
int n = text.indexOf(' ');
|
|
QString to_user = text.mid(1,n-1);
|
|
QString insert = "tell you quietly:";
|
|
text = insert + text.mid(n,-1);
|
|
emit send_to_user(username,to_user,text);
|
|
}
|
|
else
|
|
{
|
|
emit send_text(username,text);
|
|
}
|
|
}
|
|
|
|
|
|
void MainWidget::on_name_list_itemDoubleClicked(QListWidgetItem *item)
|
|
{
|
|
QString client_name = ui->name_list->currentItem()->text();
|
|
QString a = "@";
|
|
a = a + client_name + " ";
|
|
ui->msg_edit->clear();
|
|
ui->msg_edit->setText(a);
|
|
}
|
|
|
|
|
|
void MainWidget::on_exit_btn_clicked()
|
|
{
|
|
QMessageBox::StandardButton response;
|
|
response = QMessageBox::question(this, "退出", tr("是否退出软件?"));
|
|
|
|
if(response == QMessageBox::Yes)
|
|
{
|
|
emit exit_client();
|
|
this->close();
|
|
}
|
|
else
|
|
{
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
void MainWidget::on_stop_clicked()
|
|
{
|
|
QMessageBox::StandardButton response;
|
|
response = QMessageBox::question(this, "注销", tr("是否退出软件并注销账户?"));
|
|
|
|
if(response == QMessageBox::Yes)
|
|
{
|
|
emit exit_client();
|
|
emit account_cancellation(username);
|
|
this->close();
|
|
}
|
|
else
|
|
{
|
|
return;
|
|
}
|
|
}
|
|
|
|
void MainWidget::update_text(QString sendname,QString data)
|
|
{
|
|
QString currentTime = QTime::currentTime().toString("H:m A");
|
|
ui->textEdit->append(sendname + " [" + currentTime + "] : " + data);
|
|
}
|
|
|
|
void MainWidget::update_file(QString sendname,QString filename,QString file_data)
|
|
{
|
|
QMessageBox::StandardButton response;
|
|
QString content = QString("你收到了一个来自%1的群发文件%2,是否接收?").arg(sendname).arg(filename);
|
|
response = QMessageBox::question(this,"提示",content);
|
|
if(response == QMessageBox::Yes)
|
|
{
|
|
readFile(filename,file_data.toUtf8());
|
|
}
|
|
else
|
|
{
|
|
QMessageBox::information(this,"消息","未接收该文件!");
|
|
}
|
|
}
|
|
|
|
void MainWidget::update_file_private(QString sendname,QString filename,QString file_data)
|
|
{
|
|
QMessageBox::StandardButton response;
|
|
QString content = QString("你收到了%1私发给你的文件%2,是否接收?").arg(sendname).arg(filename);
|
|
response = QMessageBox::question(this,"提示",content);
|
|
if(response == QMessageBox::Yes)
|
|
{
|
|
readFile(filename,file_data.toUtf8());
|
|
}
|
|
else
|
|
{
|
|
QMessageBox::information(this,"消息","未接收该文件!");
|
|
}
|
|
}
|
|
|
|
void MainWidget::update_users(int count,QList<QString>online_users)
|
|
{
|
|
ui->name_list->clear();
|
|
QList<QString>::iterator it = online_users.begin();
|
|
for(; it!=online_users.end(); ++it)
|
|
{
|
|
ui->name_list->addItem(*it);
|
|
}
|
|
|
|
}
|
|
|
|
bool MainWidget::eventFilter(QObject *target, QEvent *event)
|
|
{
|
|
if(target == ui->msg_edit) //可替换
|
|
{
|
|
if(event->type() == QEvent::KeyPress)//回车键
|
|
{
|
|
QKeyEvent *k = static_cast<QKeyEvent *>(event);
|
|
|
|
if(k->key() == Qt::Key_Return || k->key() == Qt::Key_Enter)
|
|
{
|
|
on_send_btn_clicked(); //替换为需要响应的函数事件,以这里的按钮为例
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
return QWidget::eventFilter(target,event);
|
|
}
|
|
|
|
void MainWidget::sendFile(QString filePath,QString filename,QString name)
|
|
{
|
|
//发送的数据存入block中
|
|
QByteArray block;
|
|
QFile file(filePath);
|
|
//打开文件,定义错误提醒
|
|
if(!file.open(QIODevice::ReadOnly)){
|
|
QMessageBox::warning(this,"错误","无法打开文件!");
|
|
return;
|
|
}
|
|
//打开文件流,以写的方式打开
|
|
QDataStream out(&block,QIODevice::WriteOnly);
|
|
out.setVersion(QDataStream::Qt_5_8);
|
|
|
|
//将数据读取到QByteArray
|
|
QByteArray dataOfFile = file.readAll();
|
|
//关闭文件
|
|
file.close();
|
|
//放入block
|
|
block.append(dataOfFile);
|
|
|
|
if(name.isEmpty()) //群发文件
|
|
{
|
|
emit send_file_to_all(name,filename,block);
|
|
}
|
|
|
|
else //私发文件
|
|
{
|
|
emit send_file_private(name,filename,block,name);
|
|
}
|
|
}
|
|
|
|
void MainWidget::readFile(QString filename,QByteArray data)
|
|
{
|
|
|
|
QString homePath = QDir::homePath();
|
|
QString filePath = QFileDialog::getExistingDirectory(this,
|
|
tr("Open Directory"),
|
|
homePath,
|
|
QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
|
|
QFile outFile(filePath + "/" +filename);
|
|
if(!outFile.open(QIODevice::WriteOnly)){
|
|
QMessageBox::warning(this,"","错误: 无法打开文件.");
|
|
return;
|
|
}
|
|
outFile.write(data);
|
|
outFile.close();
|
|
|
|
}
|
|
|
|
|