-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmainwindow.cpp
118 lines (105 loc) · 3.28 KB
/
mainwindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
filesever_(new fileserver(this))
{
ui->setupUi(this);
ui->scan_pushButton->setDisabled(true);
QList<QHostAddress> AddressList = QNetworkInterface::allAddresses();
foreach(QHostAddress address, AddressList){
if(address.protocol() == QAbstractSocket::IPv4Protocol && address != QHostAddress::Null && address != QHostAddress::LocalHost)
{
if (address.toString().contains("127.0.")){
continue;
}
ui->address_comboBox->addItem(address.toString());
}
}
connect(&this->udpsever,&UDPMutlticastSever::getclient,this,&MainWindow::gotaddress);
connect(this->filesever_,&fileserver::getconneted,this,&MainWindow::gettcpconnectd);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::gotaddress(QHostAddress addr)
{
this->address.push_back(addr);
ui->clients_listWidget->insertItem(ui->clients_listWidget->currentRow() + 1,addr.toString());
}
void MainWindow::on_scan_pushButton_clicked()
{
this->udpsever.scan();
ui->clients_listWidget->clear();
}
void MainWindow::on_sendfile_pushButton_clicked()
{
filename = QFileDialog::getOpenFileName(this,tr("选择文件"));
if(filename.isNull() || !ui->scan_pushButton->isEnabled())
{
return ;
}
QList<QListWidgetItem *> items = ui->clients_listWidget->selectedItems();
foreach(QListWidgetItem *Item,items)
{
qDebug()<<Item->text();
sendaddress.push_back(QHostAddress(Item->text()));
}
this->udpsever.sendfile(sendaddress);
this->filesever_->sendfile(filename,sendaddress.count());
}
void MainWindow::on_allselet_pushButton_clicked()
{
ui->clients_listWidget->selectAll();
}
void MainWindow::on_listen_pushButton_clicked()
{
QHostAddress addr = QHostAddress(ui->address_comboBox->currentText());
this->udpsever.bind(addr);
this->filesever_->listen(addr);
ui->scan_pushButton->setEnabled(true);
}
void MainWindow::on_clear_pushButton_clicked()
{
ui->clients_listWidget->clear();
}
void MainWindow::gettcpconnectd(tcpclient *client)
{
QList<QListWidgetItem *> items = ui->clients_listWidget->findItems(client->getaddr(),Qt::MatchCaseSensitive);
if(items.isEmpty()){
qDebug()<<"not find";
return;
}
foreach(QListWidgetItem *item,items)
{
item->setBackgroundColor(QColor(Qt::yellow));
}
connect(client,&tcpclient::finishsend,this,&MainWindow::finishtcpsend);
connect(client,&tcpclient::error,this,&MainWindow::clienterror);
}
void MainWindow::finishtcpsend(QString addr)
{
QList<QListWidgetItem *> items = ui->clients_listWidget->findItems(addr,Qt::MatchCaseSensitive);
if(items.isEmpty()){
qDebug()<<"not find";
return;
}
foreach(QListWidgetItem *item,items)
{
item->setBackgroundColor(QColor(Qt::green));
}
}
void MainWindow::clienterror(QString addr)
{
QList<QListWidgetItem *> items = ui->clients_listWidget->findItems(addr,Qt::MatchCaseSensitive);
if(items.isEmpty()){
qDebug()<<"not find";
return;
}
foreach(QListWidgetItem *item,items)
{
item->setBackgroundColor(QColor(Qt::red));
}
}