二.新增下载任务UI
NewTaskDialogUI类
1.新任务的信息
// 新任务的信息
struct newTaskMessage
{
QString url;
QString fileName;
QString path;
int threadNum;
unsigned long limitedSpeed;
QString userName;
QString password;
};
2.任务界面初始化
//下载目录信息
urlLabel = new QLabel(tr("下载链接:")); // URL label
nameLabel = new QCheckBox(tr("自定义文件名:")); // 自定义 checkbox
nameLabel->setCheckState(Qt::CheckState::Unchecked);
addrLabel = new QLabel(tr("下载目录:")); // Address label
errorLabel = new QLabel(tr(" "));
errorText = new QLabel(tr("该链接非法,本软件只支持ftp和http协议"));
errorText->setStyleSheet("color:red;");
errorLabel->hide();
errorText->hide();
//URL输入和保存文件地址选择
urlLineEdit = new QLineEdit(); // URL content
urlLineEdit->setText("https://mirrors.edge.kernel.org/pub/linux/kernel/v5.x/linux-5.10.101.tar.xz");
nameLineEdit = new QLineEdit(); // 自定义 content
nameLineEdit->setEnabled(false);
addrLineEdit = new QLineEdit(); // Address content
addrLineEdit->setText(tr("/home/lwb/"));
searchDirButton = new QPushButton(tr("浏览"));
//多线程信息
multiThreadCheckBox = new QCheckBox; // 多线程复选框
multiThreadCheckBox->setText(tr("多线程"));
multiThreadCheckBox->setCheckState(Qt::CheckState::Checked);
threadNumSpinBox = new QSpinBox; // 多线程数量设置
threadNumSpinBox->setRange(1,16);
threadNumSpinBox->setValue(8);
// 限制速度
int _speed;
_speed = limitedSpeedCheckBox->isChecked() ? limitedSpeedSpinBox->value() : 0;
confirmButton = new QPushButton(tr("确定")); // 确定按钮
confirmButton->setEnabled(true);
cancelButton = new QPushButton(tr("取消")); // 取消按钮
confirmButton->setDefault(true);
3.获取URL中的文件名
// 获取URL中的文件名
QString urlString = urlLineEdit->text();
QString _fileName = "";
int index = urlString.lastIndexOf("/");
if (index >= 0)
_fileName = urlString.right(urlString.length() - index);
// 是否使用自定义文件名
_fileName = nameLabel->isChecked() ? nameLineEdit->text() : _fileName;
4.保存该任务的相关信息
nTaskMessage.url = urlLineEdit->text();
nTaskMessage.path = addrLineEdit->text();
nTaskMessage.fileName = _fileName;
nTaskMessage.threadNum = threadNumSpinBox->value();
nTaskMessage.limitedSpeed = _speed*1024*1024;
nTaskMessage.userName = ftpCheckbox->isChecked() ? ftpUsernameLineEdit->text() : tr("");
nTaskMessage.password = ftpCheckbox->isChecked() ? ftpPasswordLineEdit->text() : tr("");
5.判断文件是否存在
QString orgPath = QDir::cleanPath(nTaskMessage.path + QDir::separator() + nTaskMessage.fileName);
QFile file(orgPath);
if (file.exists()){ // 文件存在
QString dlgTitle="警告";
QString strInfo="文件已存在, 是否覆盖原来的文件?";
QMessageBox::StandardButton result;//返回选择的按钮
result=QMessageBox::question(this, dlgTitle, strInfo,
QMessageBox::Yes|QMessageBox::No);
if (result == QMessageBox::Yes){ // 确定覆盖
// 隐藏对话框
QDialog::accept();
// 添加新的下载任务
emit signalCreateNewTask(nTaskMessage);
} else if (result == QMessageBox::No){ // 不处理
;
}
}else {
// 保存路径不存在,自动创建
QDir dir(nTaskMessage.path);
if (!dir.exists())
dir.mkdir(nTaskMessage.path);
// 隐藏对话框
QDialog::accept();
// 添加新的下载任务
emit signalCreateNewTask(nTaskMessage);
}
}
6.自动检测URL合法性
void NewTaskDialog::urlChanged(QString url) {
if(url != "" && \
(url.startsWith("http:") or url.startsWith("https:") or url.startsWith("ftp:"))){
confirmButton->setEnabled(true);
setUrlErrorUI(false);
if (url.startsWith("ftp:")) // 若协议为ftp
_ftpUI->show();
else
_ftpUI->hide();
}
else{
_ftpUI->hide();
confirmButton->setEnabled(false);
setUrlErrorUI(true);
}
}
7.显示URL错误
void NewTaskDialog::setUrlErrorUI(bool key)
{
if (key) //显示
{
errorText->show();
errorLabel->show();
}else{
errorText->hide();
errorLabel->hide();
}
}
MainWindow类中
// ******** UI初始化 ******** //
initWinUI(); // 主窗口UI
createWinToolsUI(); // 工具栏UI
createAddDialogUI(); // 下载对话框UI
// ******** 信号&槽 ******** //
connect(toolBarUI->exitActionUI, &QAction::triggered, this, &MainWindow::exit); // 按 退出,关闭软件
connect(toolBarUI->newTaskActionUI, &QAction::triggered, this, &MainWindow::newTask); // 按 + ,弹出新建任务对话框
MainWindow::~MainWindow()
{
toolBarUI->deleteLater();
newTaskdiaUI->deleteLater();
}
void MainWindow::initWinUI()
{
// 主窗口初始化
setWindowTitle(tr("CHD_Downloader"));
resize(800, 600);
}
void MainWindow::createAddDialogUI()
{
// 创建"新增下载任务对话框"
newTaskdiaUI = new NewTaskDialog(this);
}
void MainWindow::createWinToolsUI()
{
// 创建"工具栏"
toolBarUI = new toolsbarUI(this);
addToolBar(toolBarUI);
}
//Open "新增下载任务对话框"
void MainWindow::newTask()
{
newTaskdiaUI->open();
}
//退出
void MainWindow::exit()
{
//emit exitApp();//发送exitApp信号
qApp->quit();
}