Skip to content

Commit c2feb15

Browse files
Merge pull request #84 from NekoSilverFox/MengJianing
Optimize code and structure, update ui
2 parents 9e80ce3 + 6908398 commit c2feb15

File tree

9 files changed

+58
-23
lines changed

9 files changed

+58
-23
lines changed

PolyChatApp/chatboxwidget.ui

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@
8686
</item>
8787
<item>
8888
<widget class="QComboBox" name="cbxFontSize">
89+
<property name="currentIndex">
90+
<number>3</number>
91+
</property>
8992
<item>
9093
<property name="text">
9194
<string>8</string>
@@ -410,6 +413,12 @@
410413
<height>0</height>
411414
</size>
412415
</property>
416+
<property name="maximumSize">
417+
<size>
418+
<width>350</width>
419+
<height>16777215</height>
420+
</size>
421+
</property>
413422
<property name="selectionMode">
414423
<enum>QAbstractItemView::NoSelection</enum>
415424
</property>
@@ -421,6 +430,16 @@
421430
<string>Name</string>
422431
</property>
423432
</column>
433+
<column>
434+
<property name="text">
435+
<string>Group</string>
436+
</property>
437+
</column>
438+
<column>
439+
<property name="text">
440+
<string>IP</string>
441+
</property>
442+
</column>
424443
</widget>
425444
</item>
426445
</layout>

PolyChatApp/icons/logo.png

120 KB
Loading

PolyChatApp/icons/logo_fox.png

87.5 KB
Loading

PolyChatApp/main.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ int main(int argc, char *argv[])
1414
{
1515
QApplication a(argc, argv);
1616

17+
a.setWindowIcon(QIcon(":/icon/icons/logo_fox.png"));
18+
1719
LoginWidget login;
1820
login.show();
1921

PolyChatApp/resource.qrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,9 @@
1111
<file>icons/color-text.png</file>
1212
<file>icons/save.png</file>
1313
<file>icons/user-group.png</file>
14+
<file>icons/logo.png</file>
15+
<file>icons/user-info.png</file>
16+
<file>icons/register-add-friend.png</file>
17+
<file>icons/logo_fox.png</file>
1418
</qresource>
1519
</RCC>

PolyChatApp/uil_chatboxwidget.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ void ChatBoxWidget::receiveUDPMessage()
175175
break;
176176

177177
case SignalType::UserJoin:
178-
userJoin(localUserName_4);
178+
userJoin(localUserName_4, localUserGroupNumber_5, localIpAddress_6);
179179
break;
180180

181181
case SignalType::UserLeft:
@@ -188,14 +188,15 @@ void ChatBoxWidget::receiveUDPMessage()
188188
}
189189

190190
/* 处理用户加入 */
191-
void ChatBoxWidget::userJoin(QString name)
191+
void ChatBoxWidget::userJoin(QString name, QString groupNumber, QHostAddress ip)
192192
{
193193
if (ui->tbUser->findItems(name, Qt::MatchExactly).isEmpty())
194194
{
195195
/* 更新用户列表 */
196-
QTableWidgetItem* user = new QTableWidgetItem(name);
197196
ui->tbUser->insertRow(0);
198-
ui->tbUser->setItem(0, 0, user);
197+
ui->tbUser->setItem(0, 0, new QTableWidgetItem(name));
198+
ui->tbUser->setItem(0, 1, new QTableWidgetItem(groupNumber));
199+
ui->tbUser->setItem(0, 2, new QTableWidgetItem(ip.toString()));
199200

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

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

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

PolyChatApp/uil_chatboxwidget.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class ChatBoxWidget : public QWidget
2323

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

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

2929
signals:

PolyChatApp/uil_chatlist.cpp

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ ChatList::ChatList(QWidget* parent, QString localUserName, QString localUserGrou
1414
ui(new Ui::ChatList)
1515
{
1616
ui->setupUi(this);
17+
this->setWindowTitle("Chat list");
18+
this->setWindowIcon(QIcon(":/icon/icons/logo_fox.png"));
1719

1820

19-
qDebug() << "IN ChatList::ChatList:" << ::localUserName << ::localUserGroupNumber << ::localIpAddress;
20-
2121
/* Init data on UI */
2222
ui->lbName->setText(localUserName);
2323
ui->lbGroupNumber->setText(localUserGroupNumber);
@@ -35,6 +35,8 @@ ChatList::ChatList(QWidget* parent, QString localUserName, QString localUserGrou
3535
connect(ui->btnNewChat, &QToolButton::clicked,
3636
this, [=](){
3737
AddChat* addChat = new AddChat(nullptr);
38+
addChat->setWindowTitle("Add new chat");
39+
addChat->setWindowIcon(QIcon(":/icon/icons/register-add-friend.png"));
3840
addChat->setAttribute(Qt::WA_DeleteOnClose);
3941
addChat->setWindowModality(Qt::ApplicationModal);
4042
addChat->show();
@@ -58,8 +60,11 @@ ChatList::ChatList(QWidget* parent, QString localUserName, QString localUserGrou
5860
/* 条件满足,添加新的聊天窗口 */
5961
ChatBoxWidget* chatBoxWidget = new ChatBoxWidget(nullptr, name, port);
6062
chatBoxWidget->setAttribute(Qt::WA_DeleteOnClose);
63+
chatBoxWidget->setWindowIcon(QIcon(":/icon/icons/user-group.png"));
6164
chatBoxWidget->show();
62-
65+
/* 关闭聊天对话框 重置是否打开的数组。(如果接收到窗口关闭信号,就 XXX) */
66+
connect(chatBoxWidget, &ChatBoxWidget::signalClose,
67+
this, [=](){ setChatState(name, false); });
6368
});
6469
});
6570

@@ -84,7 +89,6 @@ void ChatList::addBtnChatInLayout(QToolButton* btn)
8489
/* 接收和解析 UDP 消息 */
8590
void ChatList::receiveMessage()
8691
{
87-
qDebug() << "ChatList::receiveMessage : Msg get!!!!";
8892
/* 拿到数据报文 */
8993
qint64 size = udpSocket->pendingDatagramSize();
9094
QByteArray byteArrayGetUDP = QByteArray(size, 0);
@@ -105,7 +109,7 @@ void ChatList::receiveMessage()
105109
QString localUserName_4 ;
106110
QString localUserGroupNumber_5;
107111
QHostAddress localIpAddress_6 ;
108-
QString msg_7;
112+
QString msg_7 ;
109113

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

@@ -133,17 +137,21 @@ void ChatList::receiveMessage()
133137
}
134138
break;
135139

136-
case SignalType::ChatDestory: break;
140+
case SignalType::ChatDestory:
141+
/* 移除本地记录及按钮 */
142+
for (int i = 0; i < this->vPair_OChat_BtnChat.size(); i++)
143+
{
144+
if (chatName_2 == this->vPair_OChat_BtnChat.at(i).first->name)
145+
{
146+
this->vPair_OChat_BtnChat.at(i).second->close();
147+
this->vPair_OChat_BtnChat.removeAt(i);
148+
}
149+
}
150+
break;
137151

138152
default:
139153
break;
140154
}
141-
142-
qDebug() << "ChaList receiveMessage signal: SignalType::" << signalType_1
143-
<< chatName_2 << chatPort_3
144-
<< localUserName_4
145-
<< localUserGroupNumber_5
146-
<< localIpAddress_6;
147155
}
148156

149157
/** 查找一个名称的群聊是否已经存在
@@ -229,10 +237,10 @@ QToolButton* ChatList::getNewBtn(QString btn_text, qint16 port, bool isOpen)
229237
QToolButton* btn = new QToolButton;
230238
// btn->setText(QString("[%1] %2").arg(chatPort).arg(chatName));
231239
btn->setText(btn_text);
232-
233240
btn->setIcon(QIcon(":/icon/icons/user-group.png"));
234241
btn->setAutoRaise(true); // 按钮透明风格
235242
btn->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); // 设置显示图标和文字
243+
btn->setAttribute(Qt::WA_DeleteOnClose);
236244
btn->setFixedSize(220, 50);
237245

238246
/* 按钮添加信号和槽 */

PolyChatApp/uil_loginwidget.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ LoginWidget::LoginWidget(QWidget *parent) :
1111
ui->setupUi(this);
1212

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

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

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

4241

0 commit comments

Comments
 (0)