Skip to content

Commit 2cc1d20

Browse files
Merge pull request #89 from NekoSilverFox/MengJianing
Add file send function
2 parents b780d65 + d8369f2 commit 2cc1d20

File tree

6 files changed

+81
-2
lines changed

6 files changed

+81
-2
lines changed

PolyChatApp/chatboxwidget.ui

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@
6464
<height>380</height>
6565
</size>
6666
</property>
67+
<property name="openExternalLinks">
68+
<bool>false</bool>
69+
</property>
70+
<property name="openLinks">
71+
<bool>false</bool>
72+
</property>
6773
</widget>
6874
</item>
6975
<item>
@@ -87,7 +93,7 @@
8793
<item>
8894
<widget class="QComboBox" name="cbxFontSize">
8995
<property name="currentIndex">
90-
<number>3</number>
96+
<number>0</number>
9197
</property>
9298
<item>
9399
<property name="text">
@@ -129,6 +135,16 @@
129135
<string>22</string>
130136
</property>
131137
</item>
138+
<item>
139+
<property name="text">
140+
<string>24</string>
141+
</property>
142+
</item>
143+
<item>
144+
<property name="text">
145+
<string>28</string>
146+
</property>
147+
</item>
132148
</widget>
133149
</item>
134150
<item>
@@ -229,6 +245,32 @@
229245
</property>
230246
</widget>
231247
</item>
248+
<item>
249+
<widget class="QToolButton" name="btnFileSend">
250+
<property name="toolTip">
251+
<string>Bold</string>
252+
</property>
253+
<property name="text">
254+
<string>B</string>
255+
</property>
256+
<property name="icon">
257+
<iconset resource="resource.qrc">
258+
<normaloff>:/icon/icons/send-file.png</normaloff>:/icon/icons/send-file.png</iconset>
259+
</property>
260+
<property name="iconSize">
261+
<size>
262+
<width>20</width>
263+
<height>20</height>
264+
</size>
265+
</property>
266+
<property name="shortcut">
267+
<string>Ctrl+B</string>
268+
</property>
269+
<property name="checkable">
270+
<bool>false</bool>
271+
</property>
272+
</widget>
273+
</item>
232274
<item>
233275
<widget class="QToolButton" name="btnSave">
234276
<property name="toolTip">
@@ -373,7 +415,7 @@
373415
<property name="minimumSize">
374416
<size>
375417
<width>0</width>
376-
<height>30</height>
418+
<height>0</height>
377419
</size>
378420
</property>
379421
<property name="font">

PolyChatApp/icons/send-file.png

3.72 KB
Loading

PolyChatApp/resource.qrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@
1515
<file>icons/user-info.png</file>
1616
<file>icons/register-add-friend.png</file>
1717
<file>icons/logo_fox.png</file>
18+
<file>icons/send-file.png</file>
1819
</qresource>
1920
</RCC>

PolyChatApp/signaltype.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ enum SignalType
77
ChatExist, // 存在本聊天窗口 (用于广播)
88
ChatDestory, // 当聊天里最后一位用户退出时销毁
99
Msg, // 普通消息
10+
FilePath,
1011
UserJoin, // 用户进入聊天
1112
UserLeft, // 用户离开
1213

PolyChatApp/uil_chatboxwidget.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <QMessageBox>
99
#include <QColorDialog>
1010
#include <QFileDialog>
11+
#include <QDesktopServices>
1112

1213
ChatBoxWidget::ChatBoxWidget(QWidget* parent, QString name, qint16 port)
1314
: QWidget(parent)
@@ -128,6 +129,16 @@ ChatBoxWidget::ChatBoxWidget(QWidget* parent, QString name, qint16 port)
128129
textStream << ui->msgTextBrowser->toPlainText();
129130
file.close();
130131
});
132+
133+
/* 发送文件 */
134+
connect(ui->btnFileSend, &QToolButton::clicked,
135+
this, [=](){
136+
this->lastFilePath = QFileDialog::getOpenFileName(this, "Send file", ".");
137+
if (lastFilePath.isEmpty()) return;
138+
sendUDPSignal(SignalType::FilePath);
139+
});
140+
connect(ui->msgTextBrowser, &QTextBrowser::anchorClicked,
141+
this, &ChatBoxWidget::openURL);
131142
}
132143

133144
ChatBoxWidget::~ChatBoxWidget()
@@ -194,6 +205,13 @@ void ChatBoxWidget::sendUDPSignal(const SignalType type)
194205
port);
195206
break; // END SignalType::Msg
196207

208+
case SignalType::FilePath:
209+
dataStream << this->lastFilePath;
210+
udpSocketOnPortChatBox->writeDatagram(resByteArray,
211+
QHostAddress(QHostAddress::Broadcast),
212+
port);
213+
break; // END SignalType::FilePath
214+
197215
case SignalType::UserJoin:
198216
dataStream << QString("SignalType::UserJoin");
199217
udpSocketOnPortChatBox->writeDatagram(resByteArray,
@@ -268,6 +286,13 @@ void ChatBoxWidget::receiveUDPMessage()
268286
ui->msgTextBrowser->append(msg_7);
269287
break;
270288

289+
case SignalType::FilePath:
290+
// 追加聊天记录
291+
ui->msgTextBrowser->setTextColor(Qt::red);
292+
ui->msgTextBrowser->append(">>> [FILE] " + time);
293+
ui->msgTextBrowser->append(QString("<p><a href=\"%1\" target=\"_blank\">[Name]: %2 [Size]: %3Kb</a></p>").arg(msg_7).arg(QFileInfo(msg_7).fileName()).arg(QFileInfo(msg_7).size()));
294+
break;
295+
271296
case SignalType::UserJoin:
272297
userJoin(localUserName_4, localUserGroupNumber_5, localIpAddress_6);
273298
break;
@@ -356,3 +381,10 @@ void ChatBoxWidget::closeEvent(QCloseEvent* event)
356381

357382
QWidget::closeEvent(event);
358383
}
384+
385+
void ChatBoxWidget::openURL(const QUrl &url)
386+
{
387+
QString strUrl = url.toString();
388+
qDebug() << strUrl;
389+
QDesktopServices::openUrl(url);
390+
}

PolyChatApp/uil_chatboxwidget.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,13 @@ class ChatBoxWidget : public QWidget
3131

3232
private:
3333
void receiveUDPMessage(); // 接收 UDP 消息
34+
void openURL(const QUrl &url);
3435

3536
private:
3637
Ui::ChatBoxWidget* ui;
3738

39+
QString lastFilePath;
40+
3841
QUdpSocket* udpSocketOnPortChatList;
3942
QUdpSocket* udpSocketOnPortChatBox;
4043
QString name;

0 commit comments

Comments
 (0)