This commit is contained in:
2022-12-09 11:16:11 +08:00
parent fbffe12501
commit 2ec4b1f4d4
168 changed files with 7008 additions and 10696 deletions

41
src/widgets/big_image.cpp Normal file
View File

@@ -0,0 +1,41 @@
#include "big_image.h"
#include <QHBoxLayout>
#include <QtConcurrent>
big_image::big_image(DBlurEffectWidget *parent) :
DBlurEffectWidget(parent),
m_image(new QLabel)
{
// setWindowFlags(this->windowFlags() | Qt::WindowStaysOnTopHint); // 设置图片对话框总在最前
setWindowModality(Qt::ApplicationModal); // 以上无效不如直接使用 模态化对话框
setRadius(0);
setMaskAlpha(60);
setMaskColor(QColor("#000000"));
QHBoxLayout *layout = new QHBoxLayout;
setLayout(layout);
layout->addWidget(m_image);
layout->setMargin(0);
// m_image->setParent(this);
// m_image->setMaximumSize(1360,768);
m_image->setAlignment(Qt::AlignCenter);
}
void big_image::setimage(QPixmap image)
{
m_image->setPixmap(image);
}
void big_image::mousePressEvent(QMouseEvent *)
{
hide();
m_image->clear();
}
void big_image::focusOutEvent(QFocusEvent *)
{
hide();
m_image->clear();
}

27
src/widgets/big_image.h Normal file
View File

@@ -0,0 +1,27 @@
#ifndef BIG_IMAGE_H
#define BIG_IMAGE_H
#include <QMouseEvent>
#include <QLabel>
#include <DBlurEffectWidget>
DWIDGET_USE_NAMESPACE
class big_image : public DBlurEffectWidget
{
Q_OBJECT
public:
explicit big_image(DBlurEffectWidget *parent = nullptr);
QLabel *m_image;
void setimage(QPixmap);
protected:
void mousePressEvent(QMouseEvent *event) override;
void focusOutEvent(QFocusEvent *event) override;
};
#endif // BIG_IMAGE_H

View File

@@ -0,0 +1,226 @@
#include "downloaditem.h"
#include "ui_downloaditem.h"
#include <QtConcurrent>
#include <QProcess>
bool DownloadItem::isInstall = false;
DownloadItem::DownloadItem(QWidget *parent) :
QWidget(parent),
reinstall(false),
close(false),
ui(new Ui::DownloadItem),
menu_install(new QMenu),
action_dpkg(new QAction),
action_deepin(new QAction),
action_gdebi(new QAction),
output_w(new DDialog),
textbrowser(new QTextBrowser)
{
ui->setupUi(this);
ui->pushButton_install->setEnabled(false);
ui->progressBar->setValue(0);
ui->label_filename->hide();
ui->pushButton_install->hide();
ui->pushButton_3->hide();
ui->widget_spinner->start();
ui->widget_spinner->hide();
action_dpkg->setText(tr("Spark Store App Installer"));
action_deepin->setText(tr("deepin deb installer"));
action_gdebi->setText(tr("gdebi"));
connect(action_dpkg,&QAction::triggered,[=](){DownloadItem::install(0);});
connect(action_deepin,&QAction::triggered,[=](){DownloadItem::install(1);});
connect(action_gdebi,&QAction::triggered,[=](){DownloadItem::install(2);});
// ssinstall 命令存在时再加入该选项
QFile ssinstall("/usr/local/bin/ssinstall");
ssinstall.open(QIODevice::ReadOnly);
if(ssinstall.isOpen())
{
menu_install->addAction(action_dpkg);
}
// QFile deepin("/usr/bin/deepin-deb-installer");
// deepin.open(QIODevice::ReadOnly);
// if(deepin.isOpen())
// {
// menu_install->addAction(action_deepin);
// }
// QFile gdebi("/usr/bin/gdebi");
// gdebi.open(QIODevice::ReadOnly);
// if(gdebi.isOpen())
// {
// menu_install->addAction(action_gdebi);
// }
}
DownloadItem::~DownloadItem()
{
delete ui;
}
void DownloadItem::setValue(qint64 value)
{
ui->progressBar->setValue(qint32(value));
ui->label_2->setText(QString::number(double(value) / 100) + "% (" + speed + ")");
if(ui->label_2->text().left(4) == "100%")
{
ui->label_2->setText(tr("Downloaded. Open APP Upgrade and Install Settings to enable password-free installation"));
}
}
void DownloadItem::setMax(qint64 max)
{
ui->progressBar->setMaximum(qint32(max));
}
void DownloadItem::setName(QString name)
{
ui->label->setText(name);
}
QString DownloadItem::getName()
{
return ui->label_filename->text();
}
void DownloadItem::readyInstall()
{
if(!close)
{
ui->progressBar->hide();
ui->pushButton_install->setEnabled(true);
ui->pushButton_install->show();
ui->pushButton_2->hide();
}
}
void DownloadItem::setFileName(QString fileName)
{
ui->label_filename->setText(fileName);
}
void DownloadItem::seticon(const QPixmap icon)
{
ui->label_3->setPixmap(icon);
}
void DownloadItem::closeDownload()
{
on_pushButton_2_clicked();
}
void DownloadItem::setSpeed(QString s)
{
speed = s;
}
void DownloadItem::install(int t)
{
if(!isInstall)
{
isInstall = true;
ui->pushButton_install->hide();
ui->widget_spinner->show();
qDebug() << "/tmp/spark-store/" + ui->label_filename->text().toUtf8();
ui->label_2->setText(tr("Installing"));
QtConcurrent::run([=]()
{
QProcess installer;
switch(t)
{
case 0:
installer.start("pkexec", QStringList() << "ssinstall" << "/tmp/spark-store/" + ui->label_filename->text().toUtf8());
break;
case 1:
installer.start("deepin-deb-installer", QStringList() << "/tmp/spark-store/" + ui->label_filename->text().toUtf8());
break;
case 2:
installer.start("pkexec", QStringList() << "gdebi" << "-n" << "/tmp/spark-store/" + ui->label_filename->text().toUtf8());
break;
}
bool haveError = false;
bool notRoot = false;
installer.waitForFinished(-1); // 不设置超时
out = installer.readAllStandardOutput();
QStringList everyOut = out.split("\n");
for(int i=0;i<everyOut.size();i++)
{
if(everyOut[i].left(2) == "E:")
{
haveError = true;
}
if(everyOut[i].right(14) == "Not authorized")
{
notRoot = true;
}
}
QProcess isInstall;
isInstall.start("dpkg -s " + pkgName);
isInstall.waitForFinished(180); // 默认超时 3 分钟
int error = QString::fromStdString(isInstall.readAllStandardError().toStdString()).length();
if(error == 0)
{
ui->pushButton_install->hide();
ui->label_2->setText(tr("Finish"));
ui->pushButton_3->show();
}
else
{
ui->pushButton_install->show();
ui->pushButton_install->setText(tr("Retry"));
ui->label_2->setText(tr("Error happened in dpkg progress , you can try it again"));
ui->pushButton_3->show();
}
if(notRoot)
{
ui->label_2->setText(tr("dpkg progress had been abortedyou can retry installation"));
ui->pushButton_install->show();
ui->pushButton_3->hide();
}
ui->widget_spinner->hide();
DownloadItem::isInstall = false;
});
qDebug()<<ui->label_filename->text().toUtf8();
}
}
void DownloadItem::on_pushButton_install_clicked()
{
// 弹出菜单
menu_install->exec(cursor().pos());
}
void DownloadItem::on_pushButton_2_clicked()
{
ui->label_2->setText(tr("Download canceled"));
ui->pushButton_2->setEnabled(false);
ui->progressBar->hide();
close = true;
}
void DownloadItem::on_pushButton_3_clicked()
{
textbrowser->setLineWidth(0);
textbrowser->setText(out);
textbrowser->setMinimumHeight(500);
output_w->setMinimumHeight(600);
output_w->setAttribute(Qt::WA_TranslucentBackground);
output_w->setTitle(ui->label->text());
output_w->layout()->setMargin(20);
output_w->layout()->addWidget(textbrowser);
output_w->show();
}

View File

@@ -0,0 +1,66 @@
#ifndef DOWNLOADITEM_H
#define DOWNLOADITEM_H
#include <QWidget>
#include <QTextBrowser>
#include <QMenu>
#include <QAction>
#include <DDialog>
DWIDGET_USE_NAMESPACE
namespace Ui {
class DownloadItem;
}
class DownloadItem : public QWidget
{
Q_OBJECT
public:
explicit DownloadItem(QWidget *parent = nullptr);
~DownloadItem();
int num;
bool free;
static bool isInstall;
bool reinstall;
QString speed;
QString out;
QString pkgName;
bool close;
void setValue(qint64);
void setMax(qint64);
void setName(QString);
QString getName();
void readyInstall();
void setFileName(QString);
void seticon(const QPixmap);
void closeDownload();
void setSpeed(QString);
void install(int);
private:
Ui::DownloadItem *ui;
QMenu *menu_install;
QAction *action_dpkg;
QAction *action_deepin;
QAction *action_gdebi;
DDialog *output_w;
QTextBrowser *textbrowser;
private slots:
void on_pushButton_install_clicked();
void on_pushButton_2_clicked();
void on_pushButton_3_clicked();
signals:
void finished();
};
#endif // DOWNLOADITEM_H

View File

@@ -0,0 +1,294 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>DownloadItem</class>
<widget class="QWidget" name="DownloadItem">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>666</width>
<height>54</height>
</rect>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
<height>54</height>
</size>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<property name="styleSheet">
<string notr="true"/>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="spacing">
<number>2</number>
</property>
<property name="leftMargin">
<number>4</number>
</property>
<property name="topMargin">
<number>4</number>
</property>
<property name="rightMargin">
<number>4</number>
</property>
<property name="bottomMargin">
<number>4</number>
</property>
<item>
<widget class="QLabel" name="label_3">
<property name="maximumSize">
<size>
<width>48</width>
<height>16777215</height>
</size>
</property>
<property name="text">
<string>icon</string>
</property>
<property name="scaledContents">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Fixed</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QLabel" name="label_filename">
<property name="maximumSize">
<size>
<width>0</width>
<height>16777215</height>
</size>
</property>
<property name="text">
<string>TextLabel</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label">
<property name="minimumSize">
<size>
<width>80</width>
<height>0</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
<height>16777215</height>
</size>
</property>
<property name="font">
<font>
<pointsize>13</pointsize>
</font>
</property>
<property name="text">
<string>Name</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Expanding</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QWidget" name="widget" native="true">
<property name="minimumSize">
<size>
<width>280</width>
<height>0</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>500</width>
<height>16777215</height>
</size>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<property name="spacing">
<number>2</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QProgressBar" name="progressBar">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>300</width>
<height>0</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>500</width>
<height>10</height>
</size>
</property>
<property name="value">
<number>24</number>
</property>
<property name="textVisible">
<bool>false</bool>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_2">
<property name="font">
<font>
<pointsize>9</pointsize>
</font>
</property>
<property name="text">
<string>Waiting to download</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_3">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Fixed</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="DSpinner" name="widget_spinner" native="true">
<property name="minimumSize">
<size>
<width>30</width>
<height>0</height>
</size>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_install">
<property name="minimumSize">
<size>
<width>60</width>
<height>0</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>60</width>
<height>16777215</height>
</size>
</property>
<property name="text">
<string>Install</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_2">
<property name="minimumSize">
<size>
<width>60</width>
<height>0</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>60</width>
<height>16777215</height>
</size>
</property>
<property name="text">
<string>Cancel</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_3">
<property name="minimumSize">
<size>
<width>60</width>
<height>0</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>60</width>
<height>16777215</height>
</size>
</property>
<property name="text">
<string>Info</string>
</property>
</widget>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>DSpinner</class>
<extends>QWidget</extends>
<header location="global">dspinner.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>

View File

@@ -0,0 +1,25 @@
#include "smoothlistwidget.h"
#include <QVBoxLayout>
#include <QLabel>
#include <QWheelEvent>
#include <QDebug>
SmoothListWidget::SmoothListWidget(QWidget *parent) : QListWidget(parent)
{
vScrollBar->setOrientation(Qt::Orientation::Vertical); //将滚动条设置为纵向
setVerticalScrollBar(vScrollBar); //设置纵向滚动条
connect(vScrollBar, SIGNAL(valueChanged(int)), this, SLOT(onSliderChanged(int)));
}
void SmoothListWidget::wheelEvent(QWheelEvent *e)
{
//当捕获到事件后,调用相对滚动的槽函数
vScrollBar->scroll(e->angleDelta().y());
}
void SmoothListWidget::onSliderChanged(int p)
{
int startRow = count();
if (p == vScrollBar->maximum())
{
emit reachedBottom(); // 1
}
emit msliderChanged(p); // 2
}

View File

@@ -0,0 +1,24 @@
#ifndef SMOOTHLISTWIDGET_H
#define SMOOTHLISTWIDGET_H
#include <QWidget>
#include <QPushButton>
#include <QListWidget>
#include "smoothscrollbar.h"
class SmoothListWidget: public QListWidget
{
Q_OBJECT
public:
explicit SmoothListWidget(QWidget *parent = nullptr);
SmoothScrollBar* vScrollBar=new SmoothScrollBar(); //纵向滚动条
private:
void wheelEvent(QWheelEvent* e); //捕获鼠标滚轮事件
signals:
void msliderChanged(int p);
void reachedBottom();
private slots:
void onSliderChanged(int p);
};
#endif // SMOOTHLISTWIDGET_H

View File

@@ -0,0 +1,56 @@
#include "smoothscrollbar.h"
#include <QWheelEvent>
#include <QDebug>
SmoothScrollBar::SmoothScrollBar(QWidget* parent):QScrollBar(parent)
{
m_scrollAni=new QPropertyAnimation(this);
m_scrollAni->setTargetObject(this);
m_scrollAni->setPropertyName("value");
m_scrollAni->setEasingCurve(QEasingCurve::OutQuint); //设置动画曲线在Qt文档中有详细的介绍
m_scrollAni->setDuration(500); //设置动画时间,数值越小播放越快
m_targetValue_v=value(); //将m_targetValue_v初始化
}
void SmoothScrollBar::setValue(int value)
{
m_scrollAni->stop();//停止现在的动画,防止出现冲突
m_scrollAni->setStartValue(this->value()); //设置动画滚动的初始值为当前位置
m_scrollAni->setEndValue(value); //设置动画的结束位置为目标值
m_scrollAni->start(); //开始动画
}
void SmoothScrollBar::scrollTop()
{
setValue(-m_targetValue_v); //开始动画
m_targetValue_v=0;
}
void SmoothScrollBar::scroll(int value)
{
//这里推荐评论区中大佬优化的写法
if(m_targetValue_v-value>=0)
{
m_targetValue_v-=value; //将目标值和相对位置进行运算
setValue(m_targetValue_v); //开始动画
}
}
void SmoothScrollBar::mousePressEvent(QMouseEvent *e)
{
//当使用鼠标操作滚动条时不会刷新m_targetValue_v的值因而需要重写事件对其进行刷新。
m_scrollAni->stop();
QScrollBar::mousePressEvent(e);
m_targetValue_v=value();
}
void SmoothScrollBar::mouseReleaseEvent(QMouseEvent *e)
{
m_scrollAni->stop();
QScrollBar::mouseReleaseEvent(e);
m_targetValue_v=value();
}
void SmoothScrollBar::mouseMoveEvent(QMouseEvent *e)
{
m_scrollAni->stop();
QScrollBar::mouseMoveEvent(e);
m_targetValue_v=value();
}

View File

@@ -0,0 +1,27 @@
#ifndef SMOOTHSCROLLBAR_H
#define SMOOTHSCROLLBAR_H
#include <QScrollBar>
#include <QPropertyAnimation>
class SmoothScrollBar : public QScrollBar
{
Q_OBJECT
public:
SmoothScrollBar(QWidget *parent=nullptr);
private:
//这里重写鼠标事件的目的是在手动点击或拖动滚动条时更新m_targetValue_v变量并且在拖动时立即结束滚动的动画。
//这里如果不明白作用,可以先注释掉看看手动拖动滚动条时对动画有什么影响。
void mousePressEvent(QMouseEvent *) override;
void mouseReleaseEvent(QMouseEvent *) override;
void mouseMoveEvent(QMouseEvent *) override;
QPropertyAnimation *m_scrollAni; //用来实现动画
int m_targetValue_v; //用来记录目标位置的变量
public slots:
void scrollTop();
void setValue(int value); //重写的setValue槽函数实现动画效果
void scroll(int value); //新增相对滚动的槽函数value为滚动距离的矢量表示
signals:
};
#endif // SMOOTHSCROLLBAR_H

View File

@@ -0,0 +1,44 @@
#include "webenginepage.h"
#include <QDesktopServices>
WebEnginePage::WebEnginePage(QObject *parent)
: QWebEnginePage(parent)
{
}
WebEnginePage::~WebEnginePage()
{
}
void WebEnginePage::setUrl(const QUrl &url)
{
if (m_currentUrl == url) {
return;
}
m_currentUrl = url;
QWebEnginePage::setUrl(url);
}
QWebEnginePage *WebEnginePage::createWindow(QWebEnginePage::WebWindowType type)
{
Q_UNUSED(type)
WebEnginePage *page = new WebEnginePage(parent());
connect(page, &WebEnginePage::urlChanged, this, &WebEnginePage::slotUrlChanged);
return page;
}
void WebEnginePage::slotUrlChanged(const QUrl &url)
{
if (m_currentUrl == url) {
sender()->deleteLater();
return;
}
qDebug() << Q_FUNC_INFO << m_currentUrl << url;
QDesktopServices::openUrl(url);
sender()->deleteLater();
}

View File

@@ -0,0 +1,26 @@
#ifndef WEBENGINEPAGE_H
#define WEBENGINEPAGE_H
#include <QWebEnginePage>
class WebEnginePage : public QWebEnginePage
{
Q_OBJECT
public:
explicit WebEnginePage(QObject *parent = nullptr);
~WebEnginePage() override;
void setUrl(const QUrl &url);
protected:
QWebEnginePage *createWindow(WebWindowType type) override;
private slots:
void slotUrlChanged(const QUrl &url);
private:
QUrl m_currentUrl;
};
#endif // WEBENGINEPAGE_H

View File

@@ -0,0 +1,10 @@
#include "webengineview.h"
#include "webenginepage.h"
#include <QDesktopServices>
WebEngineView::WebEngineView(QWidget *parent)
: QWebEngineView(parent)
{
setPage(new WebEnginePage(this));
}

View File

@@ -0,0 +1,14 @@
#ifndef WEBENGINEVIEW_H
#define WEBENGINEVIEW_H
#include <QWebEngineView>
class WebEngineView : public QWebEngineView
{
Q_OBJECT
public:
explicit WebEngineView(QWidget *parent = nullptr);
};
#endif // WEBENGINEVIEW_H

View File

@@ -0,0 +1,201 @@
#include "downloadlistwidget.h"
#include "ui_downloadlistwidget.h"
#include <QGraphicsOpacityEffect>
#include <QPropertyAnimation>
#include <QDebug>
DownloadListWidget::DownloadListWidget(QWidget *parent) :
DBlurEffectWidget(parent),
ui(new Ui::DownloadListWidget)
{
ui->setupUi(this);
installEventFilter(this);
this->setAttribute(Qt::WA_Hover,true);
setFocus();
setFixedSize(500,400);
// 计算显示下载速度
download_speed.setInterval(1000);
download_speed.start();
connect(&download_speed,&QTimer::timeout,[=]()
{
if(isdownload && theSpeed == "")
{
size1 = download_size;
double bspeed;
bspeed = size1 - size2;
if(bspeed < 1024)
{
theSpeed = QString::number(bspeed) + "B/s";
}
else if(bspeed < 1024 * 1024)
{
theSpeed = QString::number(0.01 * int(100 * (bspeed / 1024))) + "KB/s";
}
else if(bspeed < 1024 * 1024 * 1024)
{
theSpeed = QString::number(0.01 * int(100 * (bspeed / (1024 * 1024)))) + "MB/s";
}
else
{
theSpeed = QString::number(0.01 * int(100 * (bspeed / (1024 * 1024 * 1024)))) + "GB/s";
}
size2 = download_size;
}
if(isdownload){
downloaditemlist[nowDownload - 1]->setSpeed(theSpeed);
}
});
}
DownloadListWidget::~DownloadListWidget()
{
delete ui;
}
void DownloadListWidget::clearItem()
{
ui->listWidget->vScrollBar->scrollTop();
int n=ui->listWidget->count();
for(int i=0;i<n;i++)
{
QListWidgetItem *item = ui->listWidget->takeItem(0);
QWidget *card = ui->listWidget->itemWidget(item);
delete card;
card = NULL;
delete item;
item = NULL;
}
ui->listWidget->clear();
}
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;
}
DownloadItem *di=new DownloadItem(this);
dlist<<downloadurl;
downloaditemlist<<di;
di->setName(name);
di->setFileName(fileName);
di->pkgName=pkgName;
di->seticon(icon);
QListWidgetItem* pItem = new QListWidgetItem();
pItem->setSizeHint(QSize(240, 50));
ui->listWidget->addItem(pItem);
ui->listWidget->setItemWidget(pItem, di);
if(!isBusy)
{
nowDownload += 1;
startRequest(urList.at(nowDownload - 1), fileName); // 进行链接请求
}
}
void DownloadListWidget::startRequest(QUrl url, QString fileName)
{
ui->listWidget->show();
isBusy = true;
isdownload = true;
downloaditemlist[allDownload - 1]->free = false;
downloadController = new DownloadController(this); // 并发下载,在点击下载按钮的时候才会初始化
connect(downloadController, &DownloadController::downloadProcess, this, &DownloadListWidget::updateDataReadProgress);
connect(downloadController, &DownloadController::downloadFinished, this, &DownloadListWidget::httpFinished);
//connect(downloadController, &DownloadController::errorOccur, this, [=](QString msg){this->sendNotification(msg);});
downloadController->setFilename(fileName);
downloadController->startDownload(url.toString());
}
void DownloadListWidget::httpFinished() // 完成下载
{
isdownload = false;
isBusy = false;
downloaditemlist[nowDownload - 1]->readyInstall();
downloaditemlist[nowDownload - 1]->free = true;
if(nowDownload < allDownload)
{
// 如果有排队则下载下一个
qDebug() << "切换下一个下载...";
nowDownload += 1;
while(downloaditemlist[nowDownload - 1]->close)
{
nowDownload += 1;
}
QString fileName = downloaditemlist[nowDownload - 1]->getName();
startRequest(urList.at(nowDownload-1), fileName);
}
}
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)
{
return;
}
theSpeed = speedInfo;
downloaditemlist[nowDownload - 1]->setMax(10000); // 最大值
downloaditemlist[nowDownload - 1]->setValue(int(bytesRead * 100 / totalBytes) * 100); // 当前值
download_size = bytesRead;
if(downloaditemlist[nowDownload - 1]->close)
{
// 随时检测下载是否被取消
downloadController->disconnect();
downloadController->stopDownload();
downloaditemlist[nowDownload - 1]->closeDownload();
httpFinished();
}
}
void DownloadListWidget::m_move(int x,int y)
{
m_rect.setX(x);
m_rect.setY(y);
move(x,y);
return;
}
bool DownloadListWidget::eventFilter(QObject *watched, QEvent *event)
{
if (Q_NULLPTR == watched) {
return false;
}
if (QEvent::ActivationChange == event->type()) {
if(QApplication::activeWindow() != this){
this->close();
}
}
return QWidget::eventFilter(watched, event);
}
void DownloadListWidget::mouseMoveEvent(QMouseEvent *event)
{
setGeometry(m_rect);
}
void DownloadListWidget::on_pushButton_clicked()
{
QDesktopServices::openUrl(QUrl("file:///tmp/spark-store", QUrl::TolerantMode));
}

View File

@@ -0,0 +1,56 @@
#ifndef DOWNLOADLISTWIDGET_H
#define DOWNLOADLISTWIDGET_H
#include <QWidget>
#include <QTimer>
#include <DBlurEffectWidget>
#include <QNetworkAccessManager>
#include <QDesktopServices>
#include "widgets/common/downloaditem.h"
#include "backend/sparkapi.h"
#include "backend/downloadworker.h"
DWIDGET_USE_NAMESPACE
namespace Ui {
class DownloadListWidget;
}
class DownloadListWidget : public DBlurEffectWidget
{
Q_OBJECT
public:
void addItem(QString name, QString fileName, QString pkgName, const QPixmap icon, QString downloadurl);
int isDownloading(QString url);
void m_move(int x, int y);
explicit DownloadListWidget(QWidget *parent = nullptr);
~DownloadListWidget();
private:
int nowDownload = 0;
int allDownload = 0;
int isdownload = false;
bool isBusy = false;
QStringList dlist;
QList<QUrl> urList;
QList<DownloadItem *> downloaditemlist;
DownloadController *downloadController;
int nowdownload = 0;
QString theSpeed;
QTimer download_speed;
long download_size = 0;
long size1 = 0;
long size2 = 0;
void startRequest(QUrl url, QString fileName);
void httpFinished();
void updateDataReadProgress(QString speedInfo, qint64 bytesRead, qint64 totalBytes);
void clearItem();
QRect m_rect;
Ui::DownloadListWidget *ui;
private slots:
bool eventFilter(QObject *, QEvent *);
void mouseMoveEvent(QMouseEvent *event);
void on_pushButton_clicked();
};
#endif // DOWNLOADLISTWIDGET_H

View File

@@ -0,0 +1,75 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>DownloadListWidget</class>
<widget class="QWidget" name="DownloadListWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>547</width>
<height>416</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="SmoothListWidget" name="listWidget">
<property name="minimumSize">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
<property name="styleSheet">
<string notr="true">QListView,QScrollArea,QWidget#viewport,QWidget#scrollAreaWidgetContents
{
background-color:transparent;
margin:0px;
}
/**列表项选中*/
QListWidget::item::selected
{
background-color:transparent;
}</string>
</property>
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="verticalScrollBarPolicy">
<enum>Qt::ScrollBarAsNeeded</enum>
</property>
<property name="horizontalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOff</enum>
</property>
<property name="autoScroll">
<bool>true</bool>
</property>
<property name="autoScrollMargin">
<number>10</number>
</property>
<property name="resizeMode">
<enum>QListView::Fixed</enum>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>Open download directory</string>
</property>
</widget>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>SmoothListWidget</class>
<extends>QListWidget</extends>
<header>widgets/common/smoothlistwidget.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>