Skip to content

Commit

Permalink
Replace WtWbKit with QtWebEngine (#2554)
Browse files Browse the repository at this point in the history
  • Loading branch information
ctrlaltca authored Sep 19, 2023
1 parent 05e13b4 commit 681f094
Show file tree
Hide file tree
Showing 10 changed files with 365 additions and 962 deletions.
8 changes: 4 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -471,10 +471,10 @@ endif()

# Qt WebKit support
if(WANT_QTWEBKIT)
find_package(Qt5WebKitWidgets)
if(Qt5WebKitWidgets_FOUND)
list(APPEND qt5_kvirc_modules Qt5::WebKitWidgets)
include_directories(${Qt5WebKitWidgets_INCLUDE_DIRS})
find_package(Qt5WebEngineWidgets)
if(Qt5WebEngineWidgets_FOUND)
list(APPEND qt5_kvirc_modules Qt5::WebEngineWidgets)
include_directories(${Qt5WebEngineWidgets_INCLUDE_DIRS})
set(COMPILE_WEBKIT_SUPPORT 1)
endif()
endif()
Expand Down
126 changes: 53 additions & 73 deletions src/kvirc/ui/KviWebPackageManagementDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,20 @@
#include <QTimer>
#include <QClipboard>
#include <QHBoxLayout>
#include <QWebView>
#include <QWebEngineView>
#include <QVBoxLayout>
#include <QToolBar>
#include <QFile>
#include <QProgressBar>
#include <QDir>
#include <QWebView>
#include <QWebFrame>
#include <QWebElement>
#include <QUrl>
#include <QShowEvent>

KviWebPackageManagementDialog::KviWebPackageManagementDialog(QWidget * pParent)
: QWidget(pParent)
{
setAttribute(Qt::WA_DeleteOnClose);

setMinimumSize(600, 500);
setWindowIcon(QIcon(*(g_pIconManager->getBigIcon(KVI_BIGICON_WWW))));

m_pLayout = new QVBoxLayout(this);
Expand All @@ -83,25 +80,29 @@ KviWebPackageManagementDialog::KviWebPackageManagementDialog(QWidget * pParent)
hbox->addWidget(m_pProgressBar);
hbox->setAlignment(m_pProgressBar, Qt::AlignRight);

m_pWebView = new QWebView(this);
m_pWebView = new QWebEngineView(this);
// we managing the links
m_pWebView->setPage(new KviWebPackagePage());

m_pLayout->addWidget(m_pWebView);
m_pLayout->setStretchFactor(m_pWebView, 10); // must take most space
// disable context menu
m_pWebView->setContextMenuPolicy(Qt::NoContextMenu);
m_pToolBar->addAction(m_pWebView->pageAction(QWebPage::Back));
m_pToolBar->addAction(m_pWebView->pageAction(QWebPage::Forward));
m_pToolBar->addAction(m_pWebView->pageAction(QWebEnginePage::Back));
m_pToolBar->addAction(m_pWebView->pageAction(QWebEnginePage::Forward));

//connect(pWebView,SIGNAL(loadStarted()),this,SLOT(slotLoadStarted()));
connect(m_pWebView, SIGNAL(loadFinished(bool)), this, SLOT(slotLoadFinished(bool)));
connect(m_pWebView, SIGNAL(loadProgress(int)), this, SLOT(slotLoadProgress(int)));
connect(m_pWebView->page(), SIGNAL(linkClicked(const QUrl &)), this, SLOT(slotLinkClicked(const QUrl &)));

// we managing the links
m_pWebView->page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);
}

KviWebPackageManagementDialog::~KviWebPackageManagementDialog()
= default;
{
KviWebPackagePage *tmp = (KviWebPackagePage*) m_pWebView->page();
m_pWebView->setPage(nullptr);
tmp->deleteLater();
}

void KviWebPackageManagementDialog::setPackagePageUrl(const QString & szUrl)
{
Expand All @@ -113,7 +114,7 @@ void KviWebPackageManagementDialog::slotLoadFinished(bool bOk)
{
if(!bOk)
{
qDebug("Error loading page"); // fixme: add warning dialog return;
qDebug("Error loading page"); // fixme: add warning dialog
return;
}

Expand All @@ -123,38 +124,37 @@ void KviWebPackageManagementDialog::slotLoadFinished(bool bOk)
if(!isVisible())
show();

// main frame
QWebFrame * pFrame = m_pWebView->page()->mainFrame();

// search for all item_entry div
QWebElementCollection elementscollection = pFrame->documentElement().findAll("div.item_entry");

// if this is null something is wrong
if(elementscollection.count())
{
foreach(QWebElement element, elementscollection)
{
// web element 'title'
QWebElement eId = element.findFirst("span.item_id");
// string title
QString szId = eId.toPlainText();
// number version
QString szVersion = element.findFirst("span.item_version").toPlainText();
//QString t=szLocalThemesPath+szTitle+"-"+szVersion;
// is the theme installed?

if(packageIsInstalled(szId, szVersion))
m_pWebView->page()->runJavaScript("var items = "" \
Array.from(document.querySelectorAll(\"div.item_entry\")).forEach( \
function(element, index, array) { \
items += index + \"|\" + element.querySelector(\"span.item_id\").innerText + \"|\" + element.querySelector(\"span.item_version\").innerText + \"\\n\" \
})",
[this](const QVariant &items) {
QStringList elementscollection = items.toString().split("\n", Qt::SkipEmptyParts);
foreach(const QString element, elementscollection)
{
// yeah: change the background color for highlight the item
element.findFirst("div.item_identification").setStyleProperty("background", "none repeat scroll 0 0 #3cd543");
// then change the 'install' text into 'uninstall'
QWebElement eDownload = element.findFirst("a.item_download_link");
eDownload.setPlainText("Uninstall");
// why use a c++ var when we can use the web page for store it? :-D
eDownload.setAttribute("Installed", QString("1"));
QStringList tmp = element.split("|");
if(tmp.size() != 3)
continue;

// web element 'title'
QString szIndex = tmp.at(0);
// string title
QString szId = tmp.at(1);
// number version
QString szVersion = tmp.at(2);
// is the theme installed?
if(packageIsInstalled(szId, szVersion))
{
// change the background color to highlight the item
m_pWebView->page()->runJavaScript(QString("var item = document.querySelectorAll(\"div.item_entry\")[%1]; \
item.querySelector(\"div.item_identification\").style.background = \"none repeat scroll 0 0 #3cd543\"; \
item.querySelector(\"a.item_download_link\").innerText = \"Reinstall\";").arg(szIndex));
}

}
}
}
);
}
void KviWebPackageManagementDialog::slotLinkClicked(const QUrl & url)
{
Expand All @@ -163,42 +163,22 @@ void KviWebPackageManagementDialog::slotLinkClicked(const QUrl & url)
return;
QString szScheme = url.scheme();
// it's an ftp link?
if(!KviQString::equalCI(szScheme, "ftp"))
if(KviQString::equalCI(szScheme, "ftp"))
{
// one download at once
m_bBusy = true;

QNetworkRequest req(url);
QNetworkReply * pReply = KviNetworkAccessManager::getInstance()->get(req);
connect(pReply, SIGNAL(finished()), this, SLOT(slotCommandFinished()));
connect(pReply, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(slotDataTransferProgress(qint64, qint64)));

m_pProgressBar->show();
m_pWebView->load(url);
return;
}

// let's search for the 'a' tag
QWebElementCollection elementscollection = m_pWebView->page()->mainFrame()->findAllElements("a.item_download_link");
if(elementscollection.count() == 0)
return;

foreach(QWebElement element, elementscollection)
{
// check the href to find the item
if(!KviQString::equalCI(element.attribute("href"), url.toString()))
continue;

// check the hidden attribute for installed item
if(element.attribute("Installed").isEmpty())
{
// one download at once
m_bBusy = true;

QNetworkRequest req(url);
QNetworkReply * pReply = KviNetworkAccessManager::getInstance()->get(req);
connect(pReply, SIGNAL(finished()), this, SLOT(slotCommandFinished()));
connect(pReply, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(slotDataTransferProgress(qint64, qint64)));

m_pProgressBar->show();
}
else
{
qDebug("uninstall theme"); //to be continued
}
}
m_pProgressBar->show();
m_pWebView->load(url);
}

void KviWebPackageManagementDialog::slotDataTransferProgress(qint64 iDone, qint64 iTotal)
Expand Down
26 changes: 24 additions & 2 deletions src/kvirc/ui/KviWebPackageManagementDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,36 @@
#ifdef COMPILE_WEBKIT_SUPPORT

#include <QWidget>
#include <QWebEnginePage>

class QToolBar;
class QVBoxLayout;
class QWebView;
class QWebEngineView;
class QFile;
class QProgressBar;
class QUrl;

class KviWebPackagePage : public QWebEnginePage
{
Q_OBJECT
public:
KviWebPackagePage(QObject* parent = 0) : QWebEnginePage(parent){}

bool acceptNavigationRequest(const QUrl & url, QWebEnginePage::NavigationType type, bool)
{
if (type == QWebEnginePage::NavigationTypeLinkClicked)
{
emit linkClicked(url);
return false;
}
return true;
}

signals:
void linkClicked(const QUrl&);

};

///
/// \class KviWebPackageManagementDialog
/// \brief The KviWebPackageManagementDialog class
Expand All @@ -63,7 +85,7 @@ class KVIRC_API KviWebPackageManagementDialog : public QWidget
private:
QToolBar * m_pToolBar;
QVBoxLayout * m_pLayout;
QWebView * m_pWebView;
QWebEngineView * m_pWebView;
bool m_bBusy;
QProgressBar * m_pProgressBar;
QString m_szPackagePageUrl;
Expand Down
20 changes: 9 additions & 11 deletions src/modules/help/HelpWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ extern KviPointerList<HelpWidget> * g_pHelpWidgetList;
#include <QShortcut>
#include <QAction>

#define HIGHLIGHT_FLAGS QWebPage::HighlightAllOccurrences

HelpWidget::HelpWidget(QWidget * par, bool bIsStandalone)
: QWidget(par)
{
Expand All @@ -74,7 +72,7 @@ HelpWidget::HelpWidget(QWidget * par, bool bIsStandalone)
m_pLayout->addWidget(m_pToolBar);

// webview
m_pTextBrowser = new QWebView(this);
m_pTextBrowser = new QWebEngineView(this);
m_pTextBrowser->setObjectName("text_browser");
m_pTextBrowser->setStyleSheet("QTextBrowser { background-color:white; color:black; }");
m_pLayout->addWidget(m_pTextBrowser);
Expand All @@ -101,10 +99,10 @@ HelpWidget::HelpWidget(QWidget * par, bool bIsStandalone)
m_pToolBar->addAction(*g_pIconManager->getBigIcon(KVI_BIGICON_HELPINDEX), __tr2qs("Show index"), this, SLOT(showIndex()));

QAction * pAction;
pAction = m_pTextBrowser->pageAction(QWebPage::Back);
pAction = m_pTextBrowser->pageAction(QWebEnginePage::Back);
pAction->setIcon(*g_pIconManager->getBigIcon(KVI_BIGICON_HELPBACK));
m_pToolBar->addAction(pAction);
pAction = m_pTextBrowser->pageAction(QWebPage::Forward);
pAction = m_pTextBrowser->pageAction(QWebEnginePage::Forward);
pAction->setIcon(*g_pIconManager->getBigIcon(KVI_BIGICON_HELPFORWARD));
m_pToolBar->addAction(pAction);

Expand All @@ -120,15 +118,15 @@ HelpWidget::HelpWidget(QWidget * par, bool bIsStandalone)

void HelpWidget::slotCopy()
{
m_pTextBrowser->triggerPageAction(QWebPage::Copy);
m_pTextBrowser->triggerPageAction(QWebEnginePage::Copy);
}

void HelpWidget::slotShowHideFind()
{
if(m_pToolBarHighlight->isVisible())
{
m_pToolBarHighlight->hide();
m_pTextBrowser->findText("", HIGHLIGHT_FLAGS);
m_pTextBrowser->findText("");
}
else
{
Expand All @@ -139,18 +137,18 @@ void HelpWidget::slotShowHideFind()

void HelpWidget::slotLoadFinished(bool)
{
m_pTextBrowser->findText(m_pFindText->text(), HIGHLIGHT_FLAGS);
m_pTextBrowser->findText(m_pFindText->text());
}

void HelpWidget::slotTextChanged(const QString szFind)
{
m_pTextBrowser->findText("", HIGHLIGHT_FLAGS);
m_pTextBrowser->findText(szFind, HIGHLIGHT_FLAGS);
m_pTextBrowser->findText("");
m_pTextBrowser->findText(szFind);
}

void HelpWidget::slotFindPrev()
{
m_pTextBrowser->findText(m_pFindText->text(), QWebPage::FindBackward);
m_pTextBrowser->findText(m_pFindText->text(), QWebEnginePage::FindBackward);
}

void HelpWidget::slotFindNext()
Expand Down
6 changes: 3 additions & 3 deletions src/modules/help/HelpWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#include "kvi_settings.h"

#ifdef COMPILE_WEBKIT_SUPPORT
#include <QtWebKitWidgets/QWebView>
#include <QWebEngineView>
#else
#include <QTextBrowser>
#endif
Expand All @@ -53,7 +53,7 @@ class HelpWidget : public QWidget
QToolBar * m_pToolBar;
QToolBar * m_pToolBarHighlight;
QLineEdit * m_pFindText;
QWebView * m_pTextBrowser;
QWebEngineView * m_pTextBrowser;
#else
QVBoxLayout * m_pLayout;
QToolBar * m_pToolBar;
Expand All @@ -77,7 +77,7 @@ protected slots:
#endif
public:
#ifdef COMPILE_WEBKIT_SUPPORT
QWebView * textBrowser()
QWebEngineView * textBrowser()
{
return m_pTextBrowser;
}
Expand Down
3 changes: 2 additions & 1 deletion src/modules/help/HelpWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ HelpWindow::HelpWindow(const char * name)
: KviWindow(KviWindow::Help, name)
{
g_pHelpWindowList->append(this);
setMinimumSize(600, 500);
m_pSplitter = new KviTalSplitter(Qt::Horizontal, this);
m_pSplitter->setObjectName("main_splitter");
m_pSplitter->setChildrenCollapsible(false);
Expand Down Expand Up @@ -268,7 +269,7 @@ void HelpWindow::startSearch()
}

#ifdef COMPILE_WEBKIT_SUPPORT
QWebView * HelpWindow::textBrowser()
QWebEngineView * HelpWindow::textBrowser()
#else
QTextBrowser * HelpWindow::textBrowser()
#endif
Expand Down
4 changes: 2 additions & 2 deletions src/modules/help/HelpWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

#include <QTabWidget>
#ifdef COMPILE_WEBKIT_SUPPORT
#include <QtWebKitWidgets/QWebView>
#include <QWebEngineView>
#else
class QTextBrowser;
#endif
Expand Down Expand Up @@ -80,7 +80,7 @@ class HelpWindow : public KviWindow

public:
#ifdef COMPILE_WEBKIT_SUPPORT
QWebView * textBrowser();
QWebEngineView * textBrowser();
#else
QTextBrowser * textBrowser();
#endif
Expand Down
Loading

0 comments on commit 681f094

Please sign in to comment.