From 03fa52da27171be16c6685a55c34a5cb9947aa1a Mon Sep 17 00:00:00 2001 From: Msy-msu <2200456405@qq.com> Date: Mon, 9 Jan 2023 23:22:10 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=9B=E8=A1=8C=E4=BA=86=E9=83=A8=E5=88=86?= =?UTF-8?q?=E9=80=BB=E8=BE=91=E4=B8=8A=E7=9A=84=E6=94=B9=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TcpClient.pro.user | 2 +- datamanager.cpp | 8 +-- login.cpp | 20 +++++++ login.h | 1 + main.cpp | 20 +++++++ mainwidget.cpp | 130 ++++++++++++++++++++++++++++++++++----------- mainwidget.h | 2 + mainwidget.ui | 27 +++++----- protocol.cpp | 1 - tcpclient.cpp | 2 - 10 files changed, 160 insertions(+), 53 deletions(-) diff --git a/TcpClient.pro.user b/TcpClient.pro.user index 15c56b6..117888e 100644 --- a/TcpClient.pro.user +++ b/TcpClient.pro.user @@ -1,6 +1,6 @@ - + EnvironmentId diff --git a/datamanager.cpp b/datamanager.cpp index f03a637..44d65fa 100644 --- a/datamanager.cpp +++ b/datamanager.cpp @@ -160,19 +160,19 @@ void DataManager::send_to_user(QString sendname, QString to_user, QString text) void DataManager::send_file_to_all(QString sendname, QString filename, QByteArray file) { QByteArray data, data_encrypt; - data = protocol->data_maker(Protocol::FILE, sendname, filename, QString(file)); + data = protocol->data_maker(Protocol::FILE, sendname, filename, QString(file.toHex())); data_encrypt = protocol->data_encrypt(data); tcpclient->sendToserver(data_encrypt); - qDebug() << "群发文件" << data; + qDebug() << "群发文件"; } void DataManager::send_file_private(QString sendname, QString filename, QByteArray file, QString name) { QByteArray data, data_encrypt; - data = protocol->data_maker(Protocol::FILE, name, sendname, filename, QString(file)); + data = protocol->data_maker(Protocol::FILE, name, sendname, filename, QString(file.toHex())); data_encrypt = protocol->data_encrypt(data); tcpclient->sendToserver(data_encrypt); - qDebug() << "私发文件" << data; + qDebug() << "私发文件"; } void DataManager::account_cancellation(QString username) diff --git a/login.cpp b/login.cpp index 8f2d1f9..46511b2 100644 --- a/login.cpp +++ b/login.cpp @@ -9,9 +9,11 @@ Login::Login(QWidget *parent) : ui->setupUi(this); this->setWindowIcon(QIcon(":/icon.png")); this->setTabOrder(ui->lineEdit_username,ui->lineEdit_password); + this->setTabOrder(ui->lineEdit_password,ui->btn_signin); this->setWindowFlags(Qt::FramelessWindowHint); //无边框 this->setAttribute(Qt::WA_TranslucentBackground); new_closebutton(ui->close_btn,":/close.png", QSize(30, 30)); + ui->btn_signin->installEventFilter(this); /********datamanager********/ datamanager = DataManager::getInstance(); @@ -267,3 +269,21 @@ void Login::new_closebutton(QPushButton* btn, QString position,QSize size) //设置图标 btn->setIcon(pixmap); } + +bool Login::eventFilter(QObject *target, QEvent *event) +{ + if(target == ui->btn_signin) //可替换 + { + if(event->type() == QEvent::KeyPress)//回车键 + { + QKeyEvent *k = static_cast(event); + + if(k->key() == Qt::Key_Return || k->key() == Qt::Key_Enter) + { + on_btn_signin_clicked(); //替换为需要响应的函数事件,以这里的按钮为例 + return true; + } + } + } + return QWidget::eventFilter(target,event); +} diff --git a/login.h b/login.h index 645228c..90504fd 100644 --- a/login.h +++ b/login.h @@ -63,6 +63,7 @@ private: void new_closebutton(QPushButton* btn, QString position,QSize size); void zoom_up(QPushButton* btn); void zoom_down(QPushButton* btn); + bool eventFilter(QObject *target, QEvent *event); }; diff --git a/main.cpp b/main.cpp index 05aeac6..577b44f 100644 --- a/main.cpp +++ b/main.cpp @@ -1,11 +1,31 @@ #include "login.h" #include +#include int main(int argc, char *argv[]) { QApplication a(argc, argv); Login w; w.show(); + QString document_name("Document"); + QString record_name("ChatRecord"); + QDir dir(QDir::currentPath()); + QFile file("./ChatRecord/memory.txt"); + + if(!dir.exists(document_name)) + { + dir.mkdir(document_name); + qDebug()<name_list->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn); ui->name_list->verticalScrollBar()->setStyleSheet("QScrollBar{width:16px;}"); ui->textEdit->setReadOnly(true); + ui->textEdit->setFocusPolicy(Qt::NoFocus); + //防止QTextBrowser中的内容被重定向 + ui->textEdit->setOpenLinks(false); + ui->textEdit->setOpenExternalLinks(false); + connect(ui->textEdit, &QTextBrowser::anchorClicked, this, [=](const QUrl &link) + { + auto path = link.path(); + qDebug() << path; + path.remove(0, 1); + qDebug() << path; + QFileInfo info(path); + if(info.isDir()) + QDesktopServices::openUrl(link); + else if(info.isFile()) + QDesktopServices::openUrl(QUrl::fromLocalFile(path)); + }); ui->send_btn->setFocus(); ui->send_btn->setDefault(true); ui->msg_edit->installEventFilter(this);//设置完后自动调用其eventFilter函数 @@ -45,7 +61,9 @@ void MainWidget::on_send_text_clicked() if(text.isEmpty()) //群发 { QString currentTime = QTime::currentTime().toString("H:mm A"); - ui->textEdit->append("" + username + " [" + currentTime + "] send a file \"" + filename + "\""); + QString html = "" + filename + ""; + qDebug() << html; + ui->textEdit->append("" + username + " [" + currentTime + "] send a file \"" + html + "\""); this->sendFile(filePath,filename); } else //私发文件 @@ -53,15 +71,29 @@ void MainWidget::on_send_text_clicked() ui->msg_edit->clear(); int n = text.indexOf(' '); QString name = text.mid(1,n-1); - if(name == username) + if(username == name) { QMessageBox::critical(this,"错误","不能自己给自己发送文件!"); + return; + } + + int index = 0; + for (int i = 0; i < ui->name_list->count(); i++) + { + if(name != ui->name_list->item(index)->text()) + index++; + } + if(index >= ui->name_list->count()) + { + QMessageBox::critical(this,"错误","未找到指定用户!"); + return; } else { QString currentTime = QTime::currentTime().toString("H:mm A"); - ui->textEdit->append(QString("" + username + " [" + currentTime + "] send a private file to %1\"" - + filename + "\"").arg(name)); + QString html = "" + filename + ""; + qDebug() << html; + ui->textEdit->append("" + username + " [" + currentTime + "] send " + name + " a private file \"" + html + "\""); this->sendFile(filePath,filename,name); } } @@ -70,7 +102,7 @@ void MainWidget::on_send_text_clicked() void MainWidget::on_save_btn_clicked() { - QFile file("./memory.txt"); + QFile file("./ChatRecord/memory.txt"); if (file.exists()) //文件存在 { QMessageBox::StandardButton reply; @@ -82,8 +114,10 @@ void MainWidget::on_save_btn_clicked() QTextStream stream(&file); //转换为字符串 QString str = ui->textEdit->toPlainText(); - str = "\n\n" + str; - //写入文本流 + QString date = QDate::currentDate().toString("yyyy-MM-dd "); + QString currentTime = QTime::currentTime().toString("H:mm A\n"); + str = "\n\n" + date + currentTime + str; + //加入时间戳并写入 stream << str; //关闭文件 file.close(); @@ -97,7 +131,9 @@ void MainWidget::on_save_btn_clicked() QTextStream stream(&file); //转换为字符串 QString str = ui->textEdit->toPlainText(); - //写入文本流 + QString date = QDate::currentDate().toString("yyyy-MM-dd "); + QString currentTime = QTime::currentTime().toString("H:mm A\n"); + str = date + currentTime + str + "\n"; stream << str; //关闭文件 file.close(); @@ -132,14 +168,17 @@ void MainWidget::on_send_btn_clicked() QString data_text = insert + text.mid(n,-1); QString text_self = QString("私发给%1:%2").arg(to_user).arg(text.mid(n)); qDebug() << text_self; - int index = 0; - for (index = 0; index < ui->name_list->count(); index++) + if(username == to_user) { - if(username ==ui->name_list->item(index)->text()) - { - QMessageBox::critical(this,"错误","不能自己给自己发送消息!"); - return; - } + QMessageBox::critical(this,"错误","不能自己给自己发送文件!"); + return; + } + + int index = 0; + for (int i = 0; i < ui->name_list->count(); i++) + { + if(to_user != ui->name_list->item(index)->text()) + index++; } if(index >= ui->name_list->count()) { @@ -213,6 +252,24 @@ void MainWidget::update_text(QString sendname,QString data) ui->textEdit->append(sendname + " [" + currentTime + "] : " + data); } +void MainWidget::update_file(QString sendname,QString filename,QString file_data) +{ + readFile(filename,QByteArray::fromHex(file_data.toUtf8())); + QString currentTime = QTime::currentTime().toString("H:m A"); + QString html = "" + filename + ""; + QString msg = username + " [" + currentTime + "] : " + QString("收到了一个来自%1的群发文件:").arg(sendname) + html; + ui->textEdit->append(msg); +} + +void MainWidget::update_file_private(QString sendname,QString filename,QString file_data) +{ + readFile(filename,QByteArray::fromHex(file_data.toUtf8())); + QString currentTime = QTime::currentTime().toString("H:m A"); + QString html = "" + filename + ""; + QString msg = username + " [" + currentTime + "] : " + QString("收到了一个来自%1的私发文件:").arg(sendname) + html; + ui->textEdit->append(msg); +} +/* void MainWidget::update_file(QString sendname,QString filename,QString file_data) { QMessageBox::StandardButton response; @@ -248,7 +305,7 @@ void MainWidget::update_file_private(QString sendname,QString filename,QString f QMessageBox::information(this,"消息","未接收该文件!"); } } - +*/ void MainWidget::update_users(int count,QListonline_users) { ui->name_list->clear(); @@ -271,25 +328,23 @@ void MainWidget::sendFile(QString filePath,QString filename,QString name) return; } - //打开文件流,以写的方式打开 + /*打开文件流,以写的方式打开 QDataStream out(&block,QIODevice::WriteOnly); - out.setVersion(QDataStream::Qt_5_8); + 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(username,filename,block); + emit send_file_to_all(username,filename,dataOfFile); } else //私发文件 { - emit send_file_private(username,filename,block,name); + emit send_file_private(username,filename,dataOfFile,name); } QProgressDialog *progressDlg = new QProgressDialog(this); progressDlg->setWindowModality(Qt::WindowModal); @@ -313,21 +368,32 @@ void MainWidget::sendFile(QString filePath,QString filename,QString 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); + /*QString homePath = QDir::homePath(); + QString filePath = QFileDialog::getExistingDirectory(this,tr("Open Directory"),homePath, + QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);*/ + QString filepath = "./Document"; + QDir dir(filepath); + if(dir.exists()) + { + QFile outFile(filepath + "/" +filename); if(!outFile.open(QIODevice::WriteOnly)){ - QMessageBox::warning(this,"","错误: 无法打开文件."); + QMessageBox::warning(this,"错误","接收文件出错!"); return; } outFile.write(data); outFile.close(); - QMessageBox::question(this,"接收文件","接收成功!"); - + } + else + { + dir.mkdir(filepath); + QFile outFile(filepath + "/" +filename); + if(!outFile.open(QIODevice::WriteOnly)){ + QMessageBox::warning(this,"错误","接收文件出错!"); + return; + } + outFile.write(data); + outFile.close(); + } } bool MainWidget::eventFilter(QObject *target, QEvent *event) diff --git a/mainwidget.h b/mainwidget.h index 49d5fba..33caa0f 100644 --- a/mainwidget.h +++ b/mainwidget.h @@ -13,6 +13,8 @@ #include #include #include +#include +#include namespace Ui { class MainWidget; diff --git a/mainwidget.ui b/mainwidget.ui index c792ba4..372029b 100644 --- a/mainwidget.ui +++ b/mainwidget.ui @@ -71,19 +71,6 @@ - - - - 60 - 50 - 361 - 401 - - - - border-radius:20px; - - @@ -242,6 +229,20 @@ QScrollBar::add-page:vertical,QScrollBar::sub-page:vertical + + + + 55 + 51 + 371 + 401 + + + + border-radius: +7px;padding:2px 4px; + + diff --git a/protocol.cpp b/protocol.cpp index e9c7602..6b54f1d 100644 --- a/protocol.cpp +++ b/protocol.cpp @@ -155,7 +155,6 @@ QByteArray Protocol::data_encrypt(QByteArray src) QByteArray Protocol::data_decrypt(QByteArray src) { - qDebug() << QString(src); QByteArray result = XOR_En_Decrypt(src); //qDebug() << result; result = Upper_Lower_En_Decrypt(result); diff --git a/tcpclient.cpp b/tcpclient.cpp index aaa47d4..c017bb6 100644 --- a/tcpclient.cpp +++ b/tcpclient.cpp @@ -35,7 +35,6 @@ void TCPClient::configAndrun() void TCPClient::sendToserver(QByteArray &data) { - qDebug() << data; if (socket.isOpen()) { socket.write(data); @@ -51,7 +50,6 @@ void TCPClient::setServer(QString IP, qint16 port) void TCPClient::connected() { - qDebug() << "1"; emit TCPClientConnected(); qDebug() << "emit TCPClientConnected successfully"; QString addr_t = socket.peerAddress().toString();