Skip to content

Commit

Permalink
Merge pull request #88 from NekoSilverFox/MengJianing
Browse files Browse the repository at this point in the history
Add Search function
  • Loading branch information
nekosilverfox authored Dec 9, 2022
2 parents a8cf276 + 593d689 commit b780d65
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 4 deletions.
2 changes: 2 additions & 0 deletions PolyChatApp/PolyChatApp.pro
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,5 @@ else: unix:!android: target.path = /opt/$${TARGET}/bin

RESOURCES += \
resource.qrc

RC_ICONS = logo_fox.ico
1 change: 0 additions & 1 deletion PolyChatApp/bll_polychat.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ static void initAndShowChatList(QWidget* parent)
chatList->show();
}


}

#endif // BLL_POLYCHAT_H
Binary file added PolyChatApp/logo_fox.ico
Binary file not shown.
2 changes: 1 addition & 1 deletion PolyChatApp/signaltype.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ enum SignalType

};

static const int BORDCAST_TIME_STEP = 3000; // 广播信息的时间间隔
static const int BORDCAST_TIME_STEP = 1000; // 广播信息的时间间隔

#endif // SIGNALTYPE_H
9 changes: 9 additions & 0 deletions PolyChatApp/uil_chatboxwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,15 @@ void ChatBoxWidget::receiveUDPMessage()
dataStream >> localIpAddress_6; // 第6段:发送信号用户ip
dataStream >> msg_7; // 第7段:具体内容 QString

qDebug() << "收到消息:" << SignalType::Msg
<< signalType_1
<< chatName_2
<< chatPort_3
<< localUserName_4
<< localUserGroupNumber_5
<< localIpAddress_6
<< msg_7;

switch (signalType_1) {
case SignalType::Msg:
// 追加聊天记录
Expand Down
45 changes: 44 additions & 1 deletion PolyChatApp/uil_chatlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,28 @@ ChatList::ChatList(QWidget* parent, QString localUserName, QString localUserGrou
});
});


/* 搜索框状态变化 */
connect(ui->leSearch, &QLineEdit::textChanged,
this, [=](){
/* 如果文本框中的内容为空,则显示所有的聊天按钮 */
if (ui->leSearch->text().isEmpty())
{
for (auto i : this->vPair_OChat_BtnChat)
{
i.second->show();
}
return;
}

for (auto i : this->vPair_OChat_BtnChat)
{
QString textOnBtn = i.second->text();
if (isNeedHideBtn(textOnBtn)) i.second->hide();
else i.second->show();
}
});

}

ChatList::~ChatList()
Expand All @@ -82,6 +104,9 @@ void ChatList::addBtnChatInLayout(QToolButton* btn)
{
if (nullptr == btn) return;

/* 根据搜索框中的内容,显示或者隐藏按钮 */
if (isNeedHideBtn(btn->text())) btn->hide();

ui->vLayout->addWidget(btn); // 加到垂直布局中
}

Expand Down Expand Up @@ -291,7 +316,6 @@ bool ChatList::setChatState(QString name, bool state)
}



bool ChatList::updateBtnInvPair(QString name, QToolButton* btn)
{
for (auto i : this->vPair_OChat_BtnChat)
Expand All @@ -306,3 +330,22 @@ bool ChatList::updateBtnInvPair(QString name, QToolButton* btn)
qDebug() << "[ERROR] File to update btn, ChatBox named" << name << "do not exits in local vPair_OChat_BtnChat";
return false;
}


/** 根据搜索框中的内容,并使用正则表达式,判断是否需要隐藏按钮
* @brief isNeedHideBtn
* @param btnText
* @return true 代表需要隐藏
*/
bool ChatList::isNeedHideBtn(QString textOnBtn)
{
/* 如果文本框中的内容为空,则不需要隐藏 */
if (ui->leSearch->text().isEmpty()) return false;

QString strRegExp("\\S*" + ui->leSearch->text() + "\\S*");
QRegularExpression regExp;
regExp.setPattern(strRegExp);

if (!regExp.match(textOnBtn).hasMatch()) return true;
else return false;
}
4 changes: 3 additions & 1 deletion PolyChatApp/uil_chatlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <QWidget>
#include <QUdpSocket>
#include <QToolButton>
#include <QRegularExpression>

namespace Ui {
class ChatList;
Expand All @@ -31,11 +32,12 @@ class ChatList : public QWidget
void addBtnChatInLayout(QToolButton* btn); // 添加按钮对象到 Layout

QToolButton* getNewBtn(QString btn_text, qint16 port, bool isOpen);
qint16 getRandomPort(); //获取一个不重复的随机端口号
qint16 getRandomPort(); //获取一个不重复的随机端口号

bool setChatState(QString name, bool state); // 设置聊天窗口为打开或者关闭
bool updateBtnInvPair(QString name, QToolButton* btn);

bool isNeedHideBtn(QString textOnBtn); // 根据正则表达式,判断是否需要隐藏按钮
private:
Ui::ChatList *ui;

Expand Down

0 comments on commit b780d65

Please sign in to comment.