Skip to content

Commit

Permalink
优化 代码性能和静态测试错误
Browse files Browse the repository at this point in the history
  • Loading branch information
nekosilverfox committed Feb 24, 2024
1 parent fa8913d commit a333b93
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 37 deletions.
4 changes: 2 additions & 2 deletions App/tcpclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ void TcpClient::connectTcpServerAndOpenFile()
bool isFileOpen = file.open(QIODevice::WriteOnly);
if (!isFileOpen)
{
appendTextBrowser(Qt::red, QString("[ERROR] Can not write file %1 to file: %2").arg(fileName).arg(savePath));
appendTextBrowser(Qt::red, QString("[ERROR] Can not write file %1 to file: %2").arg(fileName, savePath));
return;
}

Expand Down Expand Up @@ -168,7 +168,7 @@ void TcpClient::receiveTcpDataAndSave()
ui->progressBar->update();

ui->textBrowser->setTextColor(Qt::green);
ui->textBrowser->append(QString("[INFO] File %1 received successfully, already saved to the path: %2").arg(fileName).arg(file.fileName()));
ui->textBrowser->append(QString("[INFO] File %1 received successfully, already saved to the path: %2").arg(fileName, file.fileName()));

this->isHeaderReceived = false;
this->bytesReceived = 0;
Expand Down
6 changes: 3 additions & 3 deletions App/tcpserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ TcpServer::TcpServer(QWidget *parent, QString filePath, QHostAddress ip, qint16
ui->progressBar->setValue(0);

/* 如果断开连接 */
connect(tcpSocket, &QTcpSocket::disconnected, [=](){
connect(tcpSocket, &QTcpSocket::disconnected, this, [=](){
ui->lbClientIP->setText("[NONE] NO CONNECT");
ui->lbClientPort->setText("[NONE] NO CONNECT");
appendTextBrowser(Qt::darkYellow, QString("[WARRING] Disconnect with client %1:%2 ").arg(clientIp).arg(clientPort));
Expand All @@ -80,7 +80,7 @@ TcpServer::TcpServer(QWidget *parent, QString filePath, QHostAddress ip, qint16
QString("[INFO] File opened successfully\n"
"Name: %1\n"
"Path: %2\n"
"Size: %3Kb").arg(this->fileName).arg(filePath).arg(fileSize / 1024));
"Size: %3Kb").arg(this->fileName, filePath, QString::number(fileSize / 1024)));
}
else
{
Expand Down Expand Up @@ -121,7 +121,7 @@ TcpServer::TcpServer(QWidget *parent, QString filePath, QHostAddress ip, qint16


/* 点击取消按钮 */
connect(ui->btnCancel, &QPushButton::clicked, [=](){ this->close(); });
connect(ui->btnCancel, &QPushButton::clicked, this, [=](){ this->close(); });
}


Expand Down
45 changes: 21 additions & 24 deletions App/uil_chatboxwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ ChatBoxWidget::ChatBoxWidget(QWidget* parent, QString name, qint16 port)

/* 点击发送按钮发送消息 */
connect(ui->btnSend, &QPushButton::clicked,
[=](){sendUDPSignal(SignalType::Msg);});
this, [=](){sendUDPSignal(SignalType::Msg);});


/* 点击退出按钮,关闭窗口 */
Expand All @@ -60,44 +60,42 @@ ChatBoxWidget::ChatBoxWidget(QWidget* parent, QString name, qint16 port)
//////////////////////////////////////////////// 辅助功能 ////////////////////////////////////////////////
/* 字体 */
connect(ui->cbxFontType, &QFontComboBox::currentFontChanged,
[=](const QFont& font){
ui->msgTextEdit->setCurrentFont(font);
ui->msgTextEdit->setFocus();
this, [=](const QFont& font){
ui->msgTextEdit->setCurrentFont(font);
ui->msgTextEdit->setFocus();
});

/* 字号 */
void(QComboBox::* cbxSingal)(const QString &text) = &QComboBox::currentTextChanged;
connect(ui->cbxFontSize, cbxSingal,
[=](const QString &text){
ui->msgTextEdit->setFontPointSize(text.toDouble());
ui->msgTextEdit->setFocus();
this, [=](const QString &text){
ui->msgTextEdit->setFontPointSize(text.toDouble());
ui->msgTextEdit->setFocus();
});

/* 加粗 */
connect(ui->btnBold, &QToolButton::clicked,
[=](bool isCheck){
if (isCheck) ui->msgTextEdit->setFontWeight(QFont::Bold);
else ui->msgTextEdit->setFontWeight(QFont::Normal);
this, [=](bool isCheck){
if (isCheck) ui->msgTextEdit->setFontWeight(QFont::Bold);
else ui->msgTextEdit->setFontWeight(QFont::Normal);
});

/* 倾斜 */
connect(ui->btnItalic, &QToolButton::clicked,
[=](bool isCheck){
ui->msgTextEdit->setFontItalic(isCheck);
this, [=](bool isCheck){ ui->msgTextEdit->setFontItalic(isCheck);
});


/* 下划线 */
connect(ui->btnUnderLine, &QToolButton::clicked,
[=](bool isCheck){
ui->msgTextEdit->setFontUnderline(isCheck);
this, [=](bool isCheck){ ui->msgTextEdit->setFontUnderline(isCheck);
});

/* 更改颜色 */
connect(ui->btnColor, &QToolButton::clicked,
[=](){
QColor color = QColorDialog::getColor(Qt::black);
ui->msgTextEdit->setTextColor(color);
this, [=](){
QColor color = QColorDialog::getColor(Qt::black);
ui->msgTextEdit->setTextColor(color);
});

/* 清空聊天 */
Expand Down Expand Up @@ -338,7 +336,7 @@ void ChatBoxWidget::receiveUDPMessage()
"[%1] on [%2] send a file, file information:\n"
"Name: %3\n"
"Size: %4Kb\n"
">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>").arg(localUserName_4).arg(time).arg(msg_7).arg(fileSize_8 / 1024));
">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>").arg(localUserName_4, time, msg_7, QString::number(fileSize_8 / 1024)));
#if QT_NO_DEBUG
/* 判断发送方是不是自己,如果是自己的话,就不用再接收了
* 如果是不是自己的话,那么就询问是否接收
Expand All @@ -350,7 +348,7 @@ void ChatBoxWidget::receiveUDPMessage()
"---------------------\n"
"File information:\n"
"Name: %3\n"
"Size: %4Kb").arg(localUserName_4).arg(localUserGroupNumber_5).arg(msg_7).arg(fileSize_8 / 1024),
"Size: %4Kb").arg(localUserName_4, localUserGroupNumber_5, msg_7, QString::number(fileSize_8 / 1024)),
QMessageBox::No | QMessageBox::Yes,
QMessageBox::Yes))
{
Expand Down Expand Up @@ -405,11 +403,11 @@ void ChatBoxWidget::userLeft(QString name, QString time)
if (!ui->tbUser->findItems(name, Qt::MatchExactly).isEmpty())
{
/* 更新用户列表 */
ui->tbUser->removeRow(ui->tbUser->findItems(name, Qt::MatchExactly).first()->row());
ui->tbUser->removeRow(ui->tbUser->findItems(name, Qt::MatchExactly).constFirst()->row());

/* 追加聊天记录 */
ui->msgTextBrowser->setTextColor(Qt::gray);
ui->msgTextBrowser->append(QString("%1 left on %2").arg(name).arg(time));
ui->msgTextBrowser->append(QString("%1 left on %2").arg(name, time));

/* 在线用户更新 */
ui->lbNumberOnlineUse->setText(QString("Number of online user: %1").arg(ui->tbUser->rowCount()));
Expand Down Expand Up @@ -443,16 +441,15 @@ void ChatBoxWidget::closeEvent(QCloseEvent* event)
}

udpSocketOnPortChatList->close(); // 关闭套接字
udpSocketOnPortChatList->destroyed();
emit udpSocketOnPortChatList->destroyed();

udpSocketOnPortChatBox->close();
udpSocketOnPortChatBox->destroyed();
emit udpSocketOnPortChatBox->destroyed();

QWidget::closeEvent(event);
}

void ChatBoxWidget::openURL(const QUrl& url)
{
QString strUrl = url.toString();
QDesktopServices::openUrl(url);
}
28 changes: 20 additions & 8 deletions App/uil_chatlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ bool ChatList::isChatExist(const QString &name)
[&](auto pair) { return name == pair.first->name; });
}


/** 聊天已经记录(存在)的情况下,按钮不存在于 this 的 vPair_OChat_BtnChat 中
* @brief ChatList::isBtnChatInVector
* @param name
Expand Down Expand Up @@ -280,7 +281,7 @@ QToolButton* ChatList::getNewBtn(QString btn_text, qint16 port, bool isOpen)
btn->setFixedSize(220, 50);

/* 按钮添加信号和槽 */
connect(btn, &QToolButton::clicked,
connect(btn, &QToolButton::clicked,this,
[=](){
/* 如果窗口已经被打开,就不要再执行 */
if (isChatOpen(btn_text))
Expand Down Expand Up @@ -340,16 +341,27 @@ bool ChatList::setChatState(const QString &name, bool state)

bool ChatList::updateBtnInvPair(const QString &name, QToolButton* btn)
{
for (auto i : this->vPair_OChat_BtnChat)
// for (auto i : this->vPair_OChat_BtnChat)
// {
// if (name == i.first->name)
// {
// i.second = btn;
// return true;
// }
// }
// 使用 std::find_if 算法查找是否存在满足条件的元素
auto it = std::find_if(this->vPair_OChat_BtnChat.begin(), this->vPair_OChat_BtnChat.end(),
[&](const auto &pair) { return name == pair.first->name; });

// 如果找到满足条件的元素,则更新其对应的按钮,并返回 true
// 否则,输出错误信息,并返回 false
if (it != this->vPair_OChat_BtnChat.end())
{
if (name == i.first->name)
{
i.second = btn;
return true;
}
it->second = btn;
return true;
}

qDebug() << "[ERROR] File to update btn, ChatBox named" << name << "do not exits in local vPair_OChat_BtnChat";
qDebug() << "[ERROR] Fail to update btn, ChatBox named" << name << "do not exits in local vPair_OChat_BtnChat";
return false;
}

Expand Down

0 comments on commit a333b93

Please sign in to comment.