重写下载按钮逻辑

This commit is contained in:
柚子 2022-12-13 20:32:39 +08:00
parent 922cb6a34f
commit 83ee212484
6 changed files with 52 additions and 47 deletions

View File

@ -51,7 +51,7 @@ void AppIntoPage::setDownloadWidget(DownloadListWidget *w)
{
dw=w;
connect(w, &DownloadListWidget::downloadFinished, [=]() {
isDownloading();
isDownloading(SparkAPI::getServerUrl()+"store"+spk.path()+"/"+info["Filename"].toString());
});
}
void AppIntoPage::openUrl(QUrl url)
@ -144,7 +144,7 @@ void AppIntoPage::openUrl(QUrl url)
else
{
ui->downloadButton->setText(tr("Download"));
isDownloading();
isDownloading(SparkAPI::getServerUrl()+"store"+spk.path()+"/"+info["Filename"].toString());
ui->downloadButton->setEnabled(true);
ui->downloadButton->show();
}
@ -193,24 +193,36 @@ void AppIntoPage::openUrl(QUrl url)
api1->getAppDownloadTimes(url);
}
void AppIntoPage::isDownloading()
void AppIntoPage::isDownloading(QUrl url)
{
switch (dw->isDownloading(SparkAPI::getServerUrl()+"store"+spk.path()+"/"+info["Filename"].toString())) {
case 3:{
ui->downloadButton->setEnabled(true);
ui->downloadButton->setText(tr("Download"));
break;
}
case 1:{
ui->downloadButton->setEnabled(false);
ui->downloadButton->setText(tr("Downloading"));
break;
}
case 2:{
ui->downloadButton->setEnabled(false);
ui->downloadButton->setText(tr("Downloaded"));
break;
}
if(dw->getUrlList().lastIndexOf(url) == -1)
{
ui->downloadButton->setEnabled(true);
return;
}else{
ui->downloadButton->setEnabled(false);
}
if(dw->getDIList()[dw->getUrlList().lastIndexOf(url)]->download == 2)
{
ui->downloadButton->setEnabled(true);
ui->downloadButton->setText(tr("Download"));
}
if(dw->getDIList()[dw->getUrlList().lastIndexOf(url)]->download == 1)
{
ui->downloadButton->setEnabled(true);
ui->downloadButton->setText(tr("Install"));
}
if(dw->getDIList()[dw->getUrlList().lastIndexOf(url)]->isInstall)
{
ui->downloadButton->setEnabled(false);
ui->downloadButton->setText(tr("Installing"));
}
if(dw->getDIList()[dw->getUrlList().lastIndexOf(url)]->download == 3)
{
ui->downloadButton->setEnabled(true);
ui->downloadButton->setText(tr("Reinstall"));
ui->downloadButton->show();
ui->pushButton_3->show();
}
}
@ -292,13 +304,20 @@ AppIntoPage::~AppIntoPage()
void AppIntoPage::on_downloadButton_clicked()
{
if(ui->downloadButton->text() == tr("Install"))
{
dw->getDIList()[dw->getUrlList().lastIndexOf(SparkAPI::getServerUrl()+"store"+spk.path()+"/"+info["Filename"].toString())]->install(0);
isDownloading(SparkAPI::getServerUrl()+"store"+spk.path()+"/"+info["Filename"].toString());
return;
}
emit clickedDownloadBtn();
dw->addItem(info["Name"].toString(),info["Filename"].toString(),info["Pkgname"].toString(),iconpixmap,SparkAPI::getServerUrl()+"store"+spk.path()+"/"+info["Filename"].toString());
if(ui->downloadButton->text() == tr("Reinstall"))
{
dw->getDIList()[dw->allDownload - 1]->reinstall = true;
}
isDownloading();
ui->downloadButton->setEnabled(false);
isDownloading(SparkAPI::getServerUrl()+"store"+spk.path()+"/"+info["Filename"].toString());
}
void AppIntoPage::on_pushButton_3_clicked()

View File

@ -40,7 +40,7 @@ private:
QJsonObject info;
QPixmap iconpixmap;
QUrl spk;
void isDownloading();
void isDownloading(QUrl url);
void sltAppinfoTags(QStringList *tagList);
DownloadListWidget *dw;
Ui::AppIntoPage *ui;

View File

@ -36,6 +36,7 @@ void DownloadItem::setValue(qint64 value)
ui->label_2->setText(QString::number(double(value) / 100) + "% (" + speed + ")");
if(ui->label_2->text().left(4) == "100%")
{
download = 1;
ui->label_2->setText(tr("Download Complete."));
}
}
@ -140,12 +141,14 @@ void DownloadItem::install(int t)
ui->pushButton_install->hide();
Utils::sendNotification("spark-store",tr("Spark Store"),ui->label->text() + " " + tr("Installation complete."));
ui->label_2->setText(tr("Finish"));
download = 3;
ui->pushButton_3->show();
}
else
{
ui->pushButton_install->show();
ui->pushButton_install->setText(tr("Retry"));
download = 1;
Utils::sendNotification("spark-store",tr("Spark Store"),tr("Error happened in dpkg progress , you can try it again."));
ui->label_2->setText(tr("Error happened in dpkg progress , you can try it again"));
ui->pushButton_3->show();
@ -176,6 +179,7 @@ void DownloadItem::on_pushButton_install_clicked()
void DownloadItem::on_pushButton_2_clicked()
{
ui->label_2->setText(tr("Download canceled"));
download = 2;
ui->pushButton_2->setEnabled(false);
ui->progressBar->hide();
close = true;

View File

@ -29,7 +29,7 @@ public:
QString out;
QString pkgName;
bool close;
int download;
void setValue(qint64);
void setMax(qint64);
void setName(QString);

View File

@ -72,16 +72,12 @@ void DownloadListWidget::clearItem()
}
void DownloadListWidget::addItem(QString name,QString fileName,QString pkgName,const QPixmap icon,QString downloadurl)
{
allDownload += 1;
urList.append(downloadurl);
if(dlist.contains(downloadurl))
{
return;
}
if(fileName.isEmpty())
{
return;
}
allDownload += 1;
DownloadItem *di=new DownloadItem(this);
dlist<<downloadurl;
downloaditemlist<<di;
@ -105,6 +101,11 @@ QList<DownloadItem *> DownloadListWidget::getDIList()
return downloaditemlist;
}
QList<QUrl> DownloadListWidget::getUrlList()
{
return urList;
}
void DownloadListWidget::startRequest(QUrl url, QString fileName)
{
ui->listWidget->show();
@ -130,7 +131,6 @@ void DownloadListWidget::httpFinished() // 完成下载
emit downloadFinished();
if(nowDownload < allDownload)
{
// 如果有排队则下载下一个
qDebug() << "切换下一个下载...";
nowDownload += 1;
@ -143,24 +143,6 @@ void DownloadListWidget::httpFinished() // 完成下载
}
}
int DownloadListWidget::isDownloading(QString url)
{
int i = urList.indexOf(QUrl(url),0);
if(i == -1){
return 3;
}else if(i==nowDownload-1 && isdownload)
{
return 1;
}else if(i==nowDownload-1 && !isdownload)
{
return 2;
}else if(i<nowDownload-1)
{
return 2;
}
return 0;
}
void DownloadListWidget::updateDataReadProgress(QString speedInfo, qint64 bytesRead, qint64 totalBytes)
{
if(totalBytes <= 0)

View File

@ -21,10 +21,10 @@ class DownloadListWidget : public DBlurEffectWidget
public:
void addItem(QString name, QString fileName, QString pkgName, const QPixmap icon, QString downloadurl);
int isDownloading(QString url);
int nowDownload = 0;
int allDownload = 0;
QList<DownloadItem *> getDIList();
QList<QUrl> getUrlList();
void m_move(int x, int y);
explicit DownloadListWidget(QWidget *parent = nullptr);
~DownloadListWidget();