Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reform the download function #211

Merged
merged 2 commits into from
Jan 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions example/QCefViewTest/CefViewWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@
#include <QResizeEvent>
#include <QWindow>

#include "DownloadManager.h"

CefViewWidget::CefViewWidget(const QString url, const QCefSetting* setting, QWidget* parent /* = 0*/)
: QCefView(url, setting, parent)
{}
{
}

CefViewWidget::~CefViewWidget() {}

void
CefViewWidget::onScreenChanged(QScreen* screen)
{
if (!m_cefWindow)
if (!m_pCefWindow)
return;

updateMask();
Expand All @@ -24,8 +27,8 @@ CefViewWidget::onScreenChanged(QScreen* screen)
void
CefViewWidget::onBrowserWindowCreated(QWindow* win)
{
m_cefWindow = win;
if (!m_cefWindow)
m_pCefWindow = win;
if (!m_pCefWindow)
return;

connect(window()->windowHandle(), SIGNAL(screenChanged(QScreen*)), this, SLOT(onScreenChanged(QScreen*)));
Expand All @@ -48,3 +51,20 @@ CefViewWidget::updateMask()
QRegion mask = QRegion(path.toFillPolygon().toPolygon());
setMask(mask);
}

void
CefViewWidget::onNewDownloadItem(const QSharedPointer<QCefDownloadItem>& item, const QString& suggestedName)
{
// keep the item into list or map, and call item->start() to allow the download

DownloadManager::getInstance().AddNewDownloadItem(item);
}

void
CefViewWidget::onUpdateDownloadItem(const QSharedPointer<QCefDownloadItem>& item)
{

// control the download by invoking item->pause(), item->resume(), item->cancel()

DownloadManager::getInstance().UpdateDownloadItem(item);
}
13 changes: 11 additions & 2 deletions example/QCefViewTest/CefViewWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ class CefViewWidget : public QCefView

~CefViewWidget();

signals:
void newDownloadItem(const QSharedPointer<QCefDownloadItem>& item, const QString& suggestedName);
void updateDownloadItem(const QSharedPointer<QCefDownloadItem>& item);

protected slots:
void onScreenChanged(QScreen* screen);

Expand All @@ -29,9 +33,14 @@ protected slots:
void updateMask();

private:
QWindow* m_cefWindow = nullptr;
QWindow* m_pCefWindow = nullptr;

int m_iCornerRadius = 50;

protected:
void onNewDownloadItem(const QSharedPointer<QCefDownloadItem>& item, const QString& suggestedName) override;

int m_cornerRadius = 50;
void onUpdateDownloadItem(const QSharedPointer<QCefDownloadItem>& item) override;
};

#endif // CUSTOMCEFVIEW_H
52 changes: 52 additions & 0 deletions example/QCefViewTest/DownloadManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include "DownloadManager.h"

#include <QDebug>

DownloadManager&
DownloadManager::getInstance()
{
static DownloadManager s_instance;
return s_instance;
}

void
DownloadManager::AddNewDownloadItem(const QSharedPointer<QCefDownloadItem>& item)
{
qDebug() << "DownloadManager::AddNewDownloadItem:"
<< " id: " << item->id() << "\n"
<< " name: " << item->suggestedFileName() << "\n"
<< " path: " << item->fullPath() << "\n"
<< " percent: " << item->percentComplete() << "%, " << item->totalBytes() << "/" << item->receivedBytes()
<< "\n"
<< " canceled: " << item->isCanceled() << "\n"
<< " complete: " << item->isComplete();

m_mapDownloadingItem[item->id()] = item;
item->start("", true);
}

void
DownloadManager::UpdateDownloadItem(const QSharedPointer<QCefDownloadItem>& item)
{
qDebug() << "DownloadManager::UpdateDownloadItem:"
<< " id: " << item->id() << "\n"
<< " name: " << item->suggestedFileName() << "\n"
<< " path: " << item->fullPath() << "\n"
<< " percent: " << item->percentComplete() << "%, " << item->totalBytes() << "/" << item->receivedBytes()
<< "\n"
<< " canceled: " << item->isCanceled() << "\n"
<< " complete: " << item->isComplete();

if (item->isCanceled() || item->isComplete())
m_mapDownloadingItem.remove(item->id());
}

DownloadManager::DownloadManager() {}

DownloadManager::~DownloadManager()
{
for (auto& item : m_mapDownloadingItem) {
item->cancel();
}
m_mapDownloadingItem.clear();
}
26 changes: 26 additions & 0 deletions example/QCefViewTest/DownloadManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef DOWNLOADMANAGER_H
#define DOWNLOADMANAGER_H
#pragma once

#include <QMap>
#include <QSharedPointer>

#include <QCefDownloadItem.h>

class DownloadManager
{
public:
static DownloadManager& getInstance();

void AddNewDownloadItem(const QSharedPointer<QCefDownloadItem>& item);

void UpdateDownloadItem(const QSharedPointer<QCefDownloadItem>& item);

private:
DownloadManager();
~DownloadManager();

QMap<qint32, QSharedPointer<QCefDownloadItem>> m_mapDownloadingItem;
};

#endif
101 changes: 36 additions & 65 deletions example/QCefViewTest/MainWindow.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "MainWindow.h"
#include "MainWindow.h"

#include <QCoreApplication>
#include <QDebug>
Expand All @@ -18,23 +18,23 @@
MainWindow::MainWindow(QWidget* parent)
: QMainWindow(parent /*, Qt::FramelessWindowHint*/)
{
ui.setupUi(this);
m_ui.setupUi(this);

#ifdef Q_OS_MACOS
this->ui.nativeContainer->setContentsMargins(0, 28, 0, 0);
this->m_ui.nativeContainer->setContentsMargins(0, 28, 0, 0);
#endif

setupWindow();

// setWindowFlags(Qt::FramelessWindowHint);
// setAttribute(Qt::WA_TranslucentBackground);

connect(ui.btn_reCreate, &QPushButton::clicked, this, &MainWindow::onBtnRecreateClicked);
connect(ui.btn_changeColor, &QPushButton::clicked, this, &MainWindow::onBtnChangeColorClicked);
connect(ui.btn_setFocus, &QPushButton::clicked, this, &MainWindow::onBtnSetFocusClicked);
connect(ui.btn_callJSCode, &QPushButton::clicked, this, &MainWindow::onBtnCallJSCodeClicked);
connect(ui.btn_newBrowser, &QPushButton::clicked, this, &MainWindow::onBtnNewBrowserClicked);
connect(ui.btn_quitApp, &QPushButton::clicked, qApp, &QCoreApplication::quit);
connect(m_ui.btn_reCreate, &QPushButton::clicked, this, &MainWindow::onBtnRecreateClicked);
connect(m_ui.btn_changeColor, &QPushButton::clicked, this, &MainWindow::onBtnChangeColorClicked);
connect(m_ui.btn_setFocus, &QPushButton::clicked, this, &MainWindow::onBtnSetFocusClicked);
connect(m_ui.btn_callJSCode, &QPushButton::clicked, this, &MainWindow::onBtnCallJSCodeClicked);
connect(m_ui.btn_newBrowser, &QPushButton::clicked, this, &MainWindow::onBtnNewBrowserClicked);
connect(m_ui.btn_quitApp, &QPushButton::clicked, qApp, &QCoreApplication::quit);

// build the path to the web resource
QDir dir = QCoreApplication::applicationDirPath();
Expand All @@ -49,8 +49,8 @@ MainWindow::MainWindow(QWidget* parent)

createCefView();

rightCefViewWidget = new CefViewWidget("https://www.testufo.com", nullptr, this);
ui.rightCefViewContainer->layout()->addWidget(rightCefViewWidget);
m_pRightCefViewWidget = new CefViewWidget("https://www.testufo.com", nullptr, this);
m_ui.rightCefViewContainer->layout()->addWidget(m_pRightCefViewWidget);
}

MainWindow::~MainWindow() {}
Expand All @@ -71,24 +71,25 @@ MainWindow::createCefView()
// setting.setBackgroundColor(Qt::blue);

// create the QCefView widget and add it to the layout container
leftCefViewWidget = new CefViewWidget(INDEX_URL, &setting);
m_pLeftCefViewWidget = new CefViewWidget("https://www.thinkbroadband.com/download", &setting);
// m_pLeftCefViewWidget = new CefViewWidget("https://codepen.io/tishion/pen/qBKEYXL", &setting);

// this site is for test web events
// leftCefViewContainer = new CefViewWidget("http://xcal1.vodafone.co.uk/", &setting, this);
// m_pLeftCefViewWidget = new CefViewWidget("http://xcal1.vodafone.co.uk/", &setting, this);

//
// leftCefViewContainer = new CefViewWidget("https://mdn.dev/", &setting, this);
// m_pLeftCefViewWidget = new CefViewWidget("https://mdn.dev/", &setting, this);

// this site is for test OSR performance
// rightCefViewWidget = new CefViewWidget("https://www.testufo.com", &setting, this);

// this site is test for input devices
// leftCefViewContainer = new CefViewWidget("https://devicetests.com", &setting);
// leftCefViewWidget = new CefViewWidget("https://devicetests.com", &setting);

ui.leftCefViewContainer->layout()->addWidget(leftCefViewWidget);
m_ui.leftCefViewContainer->layout()->addWidget(m_pLeftCefViewWidget);

// allow show context menu for both OSR and NCW mode
leftCefViewWidget->setContextMenuPolicy(Qt::DefaultContextMenu);
m_pLeftCefViewWidget->setContextMenuPolicy(Qt::DefaultContextMenu);

// all the following values will disable the context menu for both NCW and OSR mode
// cefViewWidget->setContextMenuPolicy(Qt::NoContextMenu);
Expand All @@ -97,28 +98,26 @@ MainWindow::createCefView()
// cefViewWidget->setContextMenuPolicy(Qt::PreventContextMenu);

// connect the invokeMethod to the slot
connect(leftCefViewWidget, &QCefView::invokeMethod, this, &MainWindow::onInvokeMethod);
connect(m_pLeftCefViewWidget, &QCefView::invokeMethod, this, &MainWindow::onInvokeMethod);

// connect the cefQueryRequest to the slot
connect(leftCefViewWidget, &QCefView::cefQueryRequest, this, &MainWindow::onQCefQueryRequest);
connect(m_pLeftCefViewWidget, &QCefView::cefQueryRequest, this, &MainWindow::onQCefQueryRequest);

connect(leftCefViewWidget, &QCefView::draggableRegionChanged, this, &MainWindow::onDraggableRegionChanged);
connect(m_pLeftCefViewWidget, &QCefView::draggableRegionChanged, this, &MainWindow::onDraggableRegionChanged);

connect(leftCefViewWidget, &QCefView::reportJavascriptResult, this, &MainWindow::onJavascriptResult);
connect(m_pLeftCefViewWidget, &QCefView::reportJavascriptResult, this, &MainWindow::onJavascriptResult);

connect(leftCefViewWidget, &QCefView::loadStart, this, &MainWindow::onLoadStart);
connect(leftCefViewWidget, &QCefView::loadEnd, this, &MainWindow::onLoadEnd);
connect(leftCefViewWidget, &QCefView::loadError, this, &MainWindow::onLoadError);
connect(leftCefViewWidget, &QCefView::newDownloadItem, this, &MainWindow::onNewDownloadItem);
connect(leftCefViewWidget, &QCefView::updateDownloadItem, this, &MainWindow::onUpdateDownloadItem);
connect(m_pLeftCefViewWidget, &QCefView::loadStart, this, &MainWindow::onLoadStart);
connect(m_pLeftCefViewWidget, &QCefView::loadEnd, this, &MainWindow::onLoadEnd);
connect(m_pLeftCefViewWidget, &QCefView::loadError, this, &MainWindow::onLoadError);
//*/
}

void
MainWindow::onDraggableRegionChanged(const QRegion& draggableRegion, const QRegion& nonDraggableRegion)
{
draggableRegion_ = draggableRegion;
nonDraggableRegion_ = nonDraggableRegion;
m_draggableRegion = draggableRegion;
m_nonDraggableRegion = nonDraggableRegion;
}

void
Expand Down Expand Up @@ -166,7 +165,7 @@ MainWindow::onQCefQueryRequest(int browserId, int64_t frameId, const QCefQuery&

QString response = query.request().toUpper();
query.setResponseResult(true, response);
leftCefViewWidget->responseQCefQuery(query);
m_pLeftCefViewWidget->responseQCefQuery(query);
}

void
Expand Down Expand Up @@ -214,40 +213,12 @@ MainWindow::onLoadError(int browserId,
<< ", errorCode:" << errorCode;
}

void
MainWindow::onNewDownloadItem(QCefDownloadItemPointer item)
{
qDebug() << "onNewDownloadItem:"
<< " id: " << item->id() << "\n"
<< " name: " << item->suggestedFileName() << "\n"
<< " path: " << item->fullPath() << "\n"
<< " percent: " << item->percentComplete() << "%, " << item->totalBytes() << "/" << item->receivedBytes()
<< "\n"
<< " canceled: " << item->isCanceled() << "\n"
<< " complete: " << item->isComplete();

item->start("", true);
}

void
MainWindow::onUpdateDownloadItem(QCefDownloadItemPointer item)
{
qDebug() << "onNewDownloadItem:"
<< " id: " << item->id() << "\n"
<< " name: " << item->suggestedFileName() << "\n"
<< " path: " << item->fullPath() << "\n"
<< " percent: " << item->percentComplete() << "%, " << item->totalBytes() << "/" << item->receivedBytes()
<< "\n"
<< " canceled: " << item->isCanceled() << "\n"
<< " complete: " << item->isComplete();
}

void
MainWindow::onBtnRecreateClicked()
{
if (leftCefViewWidget) {
leftCefViewWidget->deleteLater();
leftCefViewWidget = nullptr;
if (m_pLeftCefViewWidget) {
m_pLeftCefViewWidget->deleteLater();
m_pLeftCefViewWidget = nullptr;
}

createCefView();
Expand All @@ -256,7 +227,7 @@ MainWindow::onBtnRecreateClicked()
void
MainWindow::onBtnChangeColorClicked()
{
if (leftCefViewWidget) {
if (m_pLeftCefViewWidget) {
// create a random color
QColor color(QRandomGenerator::global()->generate());

Expand All @@ -265,7 +236,7 @@ MainWindow::onBtnChangeColorClicked()
event.arguments().append(QVariant::fromValue(color.name(QColor::HexArgb)));

// broadcast the event to all frames in all browsers created by this QCefView widget
leftCefViewWidget->broadcastEvent(event);
m_pLeftCefViewWidget->broadcastEvent(event);
}
}

Expand All @@ -274,14 +245,14 @@ MainWindow::onBtnCallJSCodeClicked()
{
int64_t context = 1000;
QString code = "alert('hello QCefView'); return {k1: 'str', k2: true, k3: 100};";
leftCefViewWidget->executeJavascriptWithResult(QCefView::MainFrameID, code, "", context);
m_pLeftCefViewWidget->executeJavascriptWithResult(QCefView::MainFrameID, code, "", context);
}

void
MainWindow::onBtnSetFocusClicked()
{
if (leftCefViewWidget) {
leftCefViewWidget->setFocus();
if (m_pLeftCefViewWidget) {
m_pLeftCefViewWidget->setFocus();
}
}

Expand Down
14 changes: 5 additions & 9 deletions example/QCefViewTest/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ protected slots:
const QString& errorMsg,
const QString& failedUrl);

void onNewDownloadItem(QCefDownloadItemPointer item);

void onUpdateDownloadItem(QCefDownloadItemPointer item);

// ui slots
protected slots:
void onBtnRecreateClicked();
Expand All @@ -60,12 +56,12 @@ protected slots:
void onBtnNewBrowserClicked();

private:
Ui::MainWindow ui;
Ui::MainWindow m_ui;

CefViewWidget* leftCefViewWidget = nullptr;
CefViewWidget* rightCefViewWidget = nullptr;
QRegion draggableRegion_;
QRegion nonDraggableRegion_;
CefViewWidget* m_pLeftCefViewWidget = nullptr;
CefViewWidget* m_pRightCefViewWidget = nullptr;
QRegion m_draggableRegion;
QRegion m_nonDraggableRegion;
};

#endif // QCEFVIEWTEST_H
Loading