Skip to content

Commit

Permalink
Merge pull request #84 from NekoSilverFox/MengJianing
Browse files Browse the repository at this point in the history
Optimize code and structure, update ui
  • Loading branch information
nekosilverfox authored Dec 8, 2022
2 parents 9e80ce3 + 6908398 commit c2feb15
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 23 deletions.
19 changes: 19 additions & 0 deletions PolyChatApp/chatboxwidget.ui
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@
</item>
<item>
<widget class="QComboBox" name="cbxFontSize">
<property name="currentIndex">
<number>3</number>
</property>
<item>
<property name="text">
<string>8</string>
Expand Down Expand Up @@ -410,6 +413,12 @@
<height>0</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>350</width>
<height>16777215</height>
</size>
</property>
<property name="selectionMode">
<enum>QAbstractItemView::NoSelection</enum>
</property>
Expand All @@ -421,6 +430,16 @@
<string>Name</string>
</property>
</column>
<column>
<property name="text">
<string>Group</string>
</property>
</column>
<column>
<property name="text">
<string>IP</string>
</property>
</column>
</widget>
</item>
</layout>
Expand Down
Binary file added PolyChatApp/icons/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added PolyChatApp/icons/logo_fox.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions PolyChatApp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ int main(int argc, char *argv[])
{
QApplication a(argc, argv);

a.setWindowIcon(QIcon(":/icon/icons/logo_fox.png"));

LoginWidget login;
login.show();

Expand Down
4 changes: 4 additions & 0 deletions PolyChatApp/resource.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,9 @@
<file>icons/color-text.png</file>
<file>icons/save.png</file>
<file>icons/user-group.png</file>
<file>icons/logo.png</file>
<file>icons/user-info.png</file>
<file>icons/register-add-friend.png</file>
<file>icons/logo_fox.png</file>
</qresource>
</RCC>
17 changes: 10 additions & 7 deletions PolyChatApp/uil_chatboxwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ void ChatBoxWidget::receiveUDPMessage()
break;

case SignalType::UserJoin:
userJoin(localUserName_4);
userJoin(localUserName_4, localUserGroupNumber_5, localIpAddress_6);
break;

case SignalType::UserLeft:
Expand All @@ -188,14 +188,15 @@ void ChatBoxWidget::receiveUDPMessage()
}

/* 处理用户加入 */
void ChatBoxWidget::userJoin(QString name)
void ChatBoxWidget::userJoin(QString name, QString groupNumber, QHostAddress ip)
{
if (ui->tbUser->findItems(name, Qt::MatchExactly).isEmpty())
{
/* 更新用户列表 */
QTableWidgetItem* user = new QTableWidgetItem(name);
ui->tbUser->insertRow(0);
ui->tbUser->setItem(0, 0, user);
ui->tbUser->setItem(0, 0, new QTableWidgetItem(name));
ui->tbUser->setItem(0, 1, new QTableWidgetItem(groupNumber));
ui->tbUser->setItem(0, 2, new QTableWidgetItem(ip.toString()));

/* 追加聊天记录 */
ui->msgTextBrowser->setTextColor(Qt::gray);
Expand Down Expand Up @@ -243,10 +244,12 @@ QString ChatBoxWidget::getAndCleanMsg()
void ChatBoxWidget::closeEvent(QCloseEvent* event)
{
emit this->signalClose();
sendUDPSignal(SignalType::UserLeft);



sendUDPSignal(SignalType::UserLeft); // TODO 如果是最后一个用户则应该发出销毁指令
if (1 == ui->tbUser->rowCount())
{
sendUDPSignal(SignalType::ChatDestory);
} // TODO

udpSocketOnPortChatList->close(); // 关闭套接字
udpSocketOnPortChatList->destroyed();
Expand Down
2 changes: 1 addition & 1 deletion PolyChatApp/uil_chatboxwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class ChatBoxWidget : public QWidget

virtual void closeEvent(QCloseEvent*); // 【重写】触发关闭事件

void userJoin(QString name); // 处理用户加入
void userJoin(QString name, QString groupNumber, QHostAddress ip); // 处理用户加入
void userLeft(QString name, QString time); // 处理用户离开

signals:
Expand Down
34 changes: 21 additions & 13 deletions PolyChatApp/uil_chatlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ ChatList::ChatList(QWidget* parent, QString localUserName, QString localUserGrou
ui(new Ui::ChatList)
{
ui->setupUi(this);
this->setWindowTitle("Chat list");
this->setWindowIcon(QIcon(":/icon/icons/logo_fox.png"));


qDebug() << "IN ChatList::ChatList:" << ::localUserName << ::localUserGroupNumber << ::localIpAddress;

/* Init data on UI */
ui->lbName->setText(localUserName);
ui->lbGroupNumber->setText(localUserGroupNumber);
Expand All @@ -35,6 +35,8 @@ ChatList::ChatList(QWidget* parent, QString localUserName, QString localUserGrou
connect(ui->btnNewChat, &QToolButton::clicked,
this, [=](){
AddChat* addChat = new AddChat(nullptr);
addChat->setWindowTitle("Add new chat");
addChat->setWindowIcon(QIcon(":/icon/icons/register-add-friend.png"));
addChat->setAttribute(Qt::WA_DeleteOnClose);
addChat->setWindowModality(Qt::ApplicationModal);
addChat->show();
Expand All @@ -58,8 +60,11 @@ ChatList::ChatList(QWidget* parent, QString localUserName, QString localUserGrou
/* 条件满足,添加新的聊天窗口 */
ChatBoxWidget* chatBoxWidget = new ChatBoxWidget(nullptr, name, port);
chatBoxWidget->setAttribute(Qt::WA_DeleteOnClose);
chatBoxWidget->setWindowIcon(QIcon(":/icon/icons/user-group.png"));
chatBoxWidget->show();

/* 关闭聊天对话框 重置是否打开的数组。(如果接收到窗口关闭信号,就 XXX) */
connect(chatBoxWidget, &ChatBoxWidget::signalClose,
this, [=](){ setChatState(name, false); });
});
});

Expand All @@ -84,7 +89,6 @@ void ChatList::addBtnChatInLayout(QToolButton* btn)
/* 接收和解析 UDP 消息 */
void ChatList::receiveMessage()
{
qDebug() << "ChatList::receiveMessage : Msg get!!!!";
/* 拿到数据报文 */
qint64 size = udpSocket->pendingDatagramSize();
QByteArray byteArrayGetUDP = QByteArray(size, 0);
Expand All @@ -105,7 +109,7 @@ void ChatList::receiveMessage()
QString localUserName_4 ;
QString localUserGroupNumber_5;
QHostAddress localIpAddress_6 ;
QString msg_7;
QString msg_7 ;

QDataStream dataStream(&byteArrayGetUDP, QIODevice::ReadOnly);

Expand Down Expand Up @@ -133,17 +137,21 @@ void ChatList::receiveMessage()
}
break;

case SignalType::ChatDestory: break;
case SignalType::ChatDestory:
/* 移除本地记录及按钮 */
for (int i = 0; i < this->vPair_OChat_BtnChat.size(); i++)
{
if (chatName_2 == this->vPair_OChat_BtnChat.at(i).first->name)
{
this->vPair_OChat_BtnChat.at(i).second->close();
this->vPair_OChat_BtnChat.removeAt(i);
}
}
break;

default:
break;
}

qDebug() << "ChaList receiveMessage signal: SignalType::" << signalType_1
<< chatName_2 << chatPort_3
<< localUserName_4
<< localUserGroupNumber_5
<< localIpAddress_6;
}

/** 查找一个名称的群聊是否已经存在
Expand Down Expand Up @@ -229,10 +237,10 @@ QToolButton* ChatList::getNewBtn(QString btn_text, qint16 port, bool isOpen)
QToolButton* btn = new QToolButton;
// btn->setText(QString("[%1] %2").arg(chatPort).arg(chatName));
btn->setText(btn_text);

btn->setIcon(QIcon(":/icon/icons/user-group.png"));
btn->setAutoRaise(true); // 按钮透明风格
btn->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); // 设置显示图标和文字
btn->setAttribute(Qt::WA_DeleteOnClose);
btn->setFixedSize(220, 50);

/* 按钮添加信号和槽 */
Expand Down
3 changes: 1 addition & 2 deletions PolyChatApp/uil_loginwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ LoginWidget::LoginWidget(QWidget *parent) :
ui->setupUi(this);

this->setWindowTitle("PolyChat Login");
this->setWindowIcon(QIcon(":/icon/icons/user-info.png"));
ui->leUserName->setFocus();

/* User checked button `login` */
Expand All @@ -34,9 +35,7 @@ void LoginWidget::userLogin()
}
this->close();

qDebug() << "IN LoginWidget::userLogin:" << localUserName << localUserGroupNumber << localIpAddress;
DAL::initAndShowChatList(nullptr); // If user login, then show ChatList. After this `ChatList` Widget is main windows
qDebug() << "IN LoginWidget::userLogin222:" << localUserName << localUserGroupNumber << localIpAddress;
}


Expand Down

0 comments on commit c2feb15

Please sign in to comment.