233 lines
6.5 KiB
C++
233 lines
6.5 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>");
|
|
emit send_text(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::sendFile(QString filePath,QString filename,QString name)
|
|
{
|
|
//通过fileTcpSocket发送文件
|
|
|
|
//发送的数据存入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);
|
|
QString command = filename+"\n";
|
|
out << command;
|
|
//将数据读取到QByteArray
|
|
QByteArray dataOfFile = file.readAll();
|
|
//关闭文件
|
|
file.close();
|
|
//放入block
|
|
block.append(dataOfFile);
|
|
|
|
if(name.isEmpty()) //群发文件
|
|
{
|
|
emit send_file_to_all(block);
|
|
}
|
|
|
|
else //私发文件
|
|
{
|
|
emit send_file_private(block,name);
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
|
|
|