-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmainwindow.cpp
118 lines (110 loc) · 3.16 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"
#include "constant.h"
#include <qmessagebox.h>
#include "Downloader.h"
#include "downloadinfdialog.h"
#include "DownloadDeledate.h"
#include <QStandardItem>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
InitConfig();
model = new QStandardItemModel(this);
ui->DownloadList->setModel(model);
DownloadDeledate * deledate = new DownloadDeledate(this);
ui->DownloadList->setItemDelegate(deledate);
ReadDownloadList();
select_downloader = NULL;
setting_window = NULL;
}
void MainWindow::InitConfig()
{
config->Read();
if(config->Get("default_path")==QJsonValue::Undefined)
{
config->Set("default_path","c:/download");
}
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_SettingAction_triggered()
{
if(setting_window==NULL)
{
setting_window = new SettingWindow;
}
setting_window -> show();
}
void MainWindow::Download()
{
QStringList url_list = ui->UrlTextEdit->toPlainText().split("\n");
for(auto &url:url_list)
{
if(url.length()==0)
{
continue;
}
if(url.mid(0,4)=="http")
{
HttpDownloader * downloader = new HttpDownloader;
downloader->file_path = config->Get("default_path").toString();
downloader->url = url;
downloader->GetFileNameFromUrl();
DownloadInfDialog dialog(downloader,this);
if(dialog.exec()==QDialog::Accepted)
{
downloader->Start();
QVariant variant;
variant.setValue(downloader);
QStandardItem *item = new QStandardItem();
item->setData(variant);
model->appendRow(item);
}
}
else
{
QMessageBox box(QMessageBox::Critical,"错误","无法解析链接:"+url);
box.exec();
}
}
}
void MainWindow::on_DownloadButton_clicked()
{
Download();
}
void MainWindow::ReadDownloadList()
{
}
void MainWindow::SaveDownloadList()
{
}
void MainWindow::on_DownloadList_clicked (const QModelIndex &index)
{
QVariant var = index.data(Qt::UserRole+1);
Downloader* item_data = var.value<Downloader*>();//拿到原始数据,和放进去的时候必须对应
if(select_downloader!=NULL)
{
disconnect(select_downloader,SIGNAL(PercentageChange(float)),this,SLOT(SetProgressBar(float)));
disconnect(select_downloader,SIGNAL(StatusChange()),this,SLOT(ChangeStatus()));
}
select_downloader = item_data;
connect(select_downloader,SIGNAL(PercentageChange(float)),this,SLOT(SetProgressBar(float)));
connect(select_downloader,SIGNAL(StatusChange()),this,SLOT(ChangeStatus()));
ui->FileName->setText(item_data->file_name);
ui->URL->setText(item_data->url);
ui->SavePath->setText(item_data->file_path);
ui->Status->setText(item_data->StatusString());
}
void MainWindow::SetProgressBar(float percentage)
{
ui->ProgressBar->setValue(percentage);
}
void MainWindow::ChangeStatus()
{
ui->Status->setText(select_downloader->StatusString());
}