Skip to content

Commit

Permalink
HelpBrowser Widget: use QcWebView
Browse files Browse the repository at this point in the history
  • Loading branch information
elgiano committed Jul 21, 2020
1 parent da4ca3f commit 6d8dd76
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 47 deletions.
4 changes: 3 additions & 1 deletion QtCollider/widgets/QcWebView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

# include "QcWebView.h"
# include "../widgets/web_page.hpp"
# include "../QcWidgetFactory.h"
# include <QWebEnginePage>
# include <QWebEngineSettings>
# include <QWebEngineContextMenuData>
Expand All @@ -35,7 +34,10 @@
# include <QStyle>
# include <QWebEngineCallback>

# ifndef SCIDE_NO_QWIDGETFACTORY
# include "../QcWidgetFactory.h"
QC_DECLARE_QWIDGET_FACTORY(WebView);
# endif

namespace QtCollider {

Expand Down
5 changes: 4 additions & 1 deletion editors/sc-ide/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,14 @@ set(ide_webengine_moc_hdrs
widgets/util/WebSocketClientWrapper.hpp
widgets/util/IDEWebChannelWrapper.hpp
${CMAKE_SOURCE_DIR}/QtCollider/widgets/web_page.hpp
${CMAKE_SOURCE_DIR}/QtCollider/widgets/QcWebView.h
${CMAKE_SOURCE_DIR}/QtCollider/QcCallback.hpp
)
set(ide_webengine_src
widgets/help_browser.cpp
widgets/util/WebSocketTransport.cpp
${CMAKE_SOURCE_DIR}/QtCollider/widgets/web_page.cpp
${CMAKE_SOURCE_DIR}/QtCollider/widgets/QcWebView.cpp
)

if(SC_USE_QTWEBENGINE)
Expand Down Expand Up @@ -377,7 +380,7 @@ endif()

if(SC_USE_QTWEBENGINE)
message(STATUS "IDE: Building with QtWebEngine")
target_compile_definitions(libscide PUBLIC SC_USE_QTWEBENGINE)
target_compile_definitions(libscide PUBLIC SC_USE_QTWEBENGINE SCIDE_NO_QWIDGETFACTORY)
endif()

# Installation
Expand Down
71 changes: 36 additions & 35 deletions editors/sc-ide/widgets/help_browser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,13 @@

namespace ScIDE {

HelpWebPage::HelpWebPage(HelpBrowser* browser): WebPage(browser), mBrowser(browser) {
setDelegateNavigation(true);
connect(this, SIGNAL(navigationRequested(const QUrl&, QWebEnginePage::NavigationType, bool)), browser,
SLOT(onLinkClicked(const QUrl&, QWebEnginePage::NavigationType, bool)));
}

HelpBrowser::HelpBrowser(QWidget* parent): QWidget(parent) {
QRect availableScreenRect = qApp->desktop()->availableGeometry(this);
mSizeHint = QSize(availableScreenRect.width() * 0.4, availableScreenRect.height() * 0.7);

HelpWebPage* webPage = new HelpWebPage(this);
webPage->setDelegateReload(true);

mWebView = new QWebEngineView;
// setPage does not take ownership of webPage; it must be deleted manually later (see below)
mWebView->setPage(webPage);
mWebView = new QtCollider::WebView(this);

mWebView->settings()->setAttribute(QWebEngineSettings::LocalStorageEnabled, true);
mWebView->setContextMenuPolicy(Qt::CustomContextMenu);

Expand All @@ -78,45 +69,55 @@ HelpBrowser::HelpBrowser(QWidget* parent): QWidget(parent) {
mWebView->setStyle(QStyleFactory::create("Fusion"));
# endif

mWebView->installEventFilter(this);

mLoadProgressIndicator = new LoadProgressIndicator;
mLoadProgressIndicator->setIndent(10);

QVBoxLayout* layout = new QVBoxLayout;
layout->setContentsMargins(0, 0, 0, 0);
layout->setSpacing(0);
layout->addWidget(mWebView);
setLayout(layout);

mLoadProgressIndicator = new LoadProgressIndicator;
mLoadProgressIndicator->setIndent(10);
connect(mWebView, SIGNAL(loadStarted()), mLoadProgressIndicator, SLOT(start()));
connect(mWebView, SIGNAL(loadFinished(bool)), mLoadProgressIndicator, SLOT(stop()));
connect(mWebView, SIGNAL(loadFinished(bool)), this, SLOT(onPageLoad()));

connect(mWebView, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(onContextMenuRequest(QPoint)));

connect(webPage->action(QWebEnginePage::Reload), SIGNAL(triggered(bool)), this, SLOT(onReload()));
connect(webPage, SIGNAL(jsConsoleMsg(QString, int, QString)), this, SLOT(onJsConsoleMsg(QString, int, QString)));
mWebView->setOverrideNavigation(true);
connect(mWebView->page(), SIGNAL(navigationRequested(const QUrl&, QWebEnginePage::NavigationType, bool)), this,
SLOT(onLinkClicked(const QUrl&, QWebEnginePage::NavigationType, bool)));
mWebView->setDelegateReload(true);
connect(mWebView->page()->action(QWebEnginePage::Reload), SIGNAL(triggered(bool)), this, SLOT(onReload()));
connect(mWebView, SIGNAL(jsConsoleMsg(QString, int, QString)), this, SLOT(onJsConsoleMsg(QString, int, QString)));

ScProcess* scProcess = Main::scProcess();
connect(scProcess, SIGNAL(response(QString, QString)), this, SLOT(onScResponse(QString, QString)));
connect(scProcess, SIGNAL(finished(int)), mLoadProgressIndicator, SLOT(stop()));
// FIXME: should actually respond to class library shutdown, but we don't have that signal
connect(scProcess, SIGNAL(classLibraryRecompiled()), mLoadProgressIndicator, SLOT(stop()));

// Delete the help browser's page to avoid an assert/crash during shutdown. See QTBUG-56441, QTBUG-50160.
// Note that putting this in the destructor doesn't work.
connect(QApplication::instance(), &QApplication::aboutToQuit, [webPage]() { delete webPage; });

createActions();

mWebView->installEventFilter(this);

applySettings(Main::settings());

setFocusProxy(mWebView);

// Delete the help browser's page to avoid an assert/crash during shutdown. See QTBUG-56441, QTBUG-50160.
// Note that putting this in the destructor doesn't work.
connect(QApplication::instance(), &QApplication::aboutToQuit, [=]() { delete mWebView->page(); });
}

void HelpBrowser::onPageLoad() {
if (mServerPort) {
// add these actions to weview's renderer, to capture shift+enter and possibly other swallowed chortcuts
((OverridingAction*)mActions[EvaluateRegion])->addToWidget(mWebView->focusProxy());
((OverridingAction*)mActions[Evaluate])->addToWidget(mWebView->focusProxy());

// inactive for now, but for the future:
/*if (mServerPort) {
mWebView->page()->runJavaScript(QString("setUpWebChannel(%1)").arg(mServerPort));
}
}*/
}

void HelpBrowser::createActions() {
Expand All @@ -142,9 +143,11 @@ void HelpBrowser::createActions() {
connect(ovrAction, SIGNAL(triggered()), this, SLOT(resetZoom()));
ovrAction->addToWidget(this);

// eval actions <re added to mWebView->focusProxy() in onPageLoad()
mActions[Evaluate] = ovrAction = new OverridingAction(tr("Evaluate as Code"), this);
connect(ovrAction, SIGNAL(triggered()), this, SLOT(evaluateSelection()));
ovrAction->addToWidget(this);
mActions[EvaluateRegion] = ovrAction = new OverridingAction(tr("Evaluate as Code Region"), this);
connect(mActions[EvaluateRegion], &OverridingAction::triggered, this, [=]() { this->evaluateSelection(true); });

// For the sake of display:
mWebView->pageAction(QWebEnginePage::Copy)->setShortcut(QKeySequence::Copy);
Expand All @@ -167,10 +170,10 @@ void HelpBrowser::applySettings(Settings::Manager* settings) {
mActions[ResetZoom]->setShortcut(settings->shortcut("editor-reset-font-size"));

QList<QKeySequence> evalShortcuts;
evalShortcuts.append(settings->shortcut("editor-eval-smart"));
evalShortcuts.append(settings->shortcut("editor-eval-line"));
evalShortcuts.append(QKeySequence(Qt::Key_Enter));
// evalShortcuts.append(QKeySequence(Qt::Key_Enter));
mActions[Evaluate]->setShortcuts(evalShortcuts);
mActions[EvaluateRegion]->setShortcut(settings->shortcut("editor-eval-smart"));

settings->endGroup();

Expand Down Expand Up @@ -248,7 +251,7 @@ void HelpBrowser::findText(const QString& text, bool backwards) {
QWebEnginePage::FindFlags flags;
if (backwards)
flags |= QWebEnginePage::FindBackward;
mWebView->findText(text, flags);
mWebView->findText(text, backwards);
}

bool HelpBrowser::helpBrowserHasFocus() const {
Expand Down Expand Up @@ -284,12 +287,8 @@ bool HelpBrowser::eventFilter(QObject* object, QEvent* event) {
break;
}
case QEvent::ShortcutOverride: {
QKeyEvent* kevent = static_cast<QKeyEvent*>(event);
if (kevent == QKeySequence::Copy || kevent == QKeySequence::Paste) {
kevent->accept();
return true;
}
break;
event->accept();
return true;
}
default:
break;
Expand Down Expand Up @@ -352,7 +351,7 @@ void HelpBrowser::evaluateSelection(bool evaluateRegion) {
}

void HelpBrowser::onJsConsoleMsg(const QString& arg1, int arg2, const QString& arg3) {
qWarning() << "*** ERROR in JavaScript:" << arg1;
qWarning() << "*** JavaScript Message:" << arg1;
qWarning() << "* line:" << arg2;
qWarning() << "* source ID:" << arg3;
}
Expand Down Expand Up @@ -380,6 +379,8 @@ void HelpBrowser::onContextMenuRequest(const QPoint& pos) {

if (!contextData.selectedText().isEmpty())
menu.addAction(mActions[Evaluate]);
else
menu.addAction(mActions[EvaluateRegion]);

menu.addSeparator();

Expand Down
13 changes: 3 additions & 10 deletions editors/sc-ide/widgets/help_browser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include "util/docklet.hpp"
#include "QtCollider/widgets/web_page.hpp"
#include "QtCollider/widgets/QcWebView.h"

#include <QWebEngineView>
#include <QLabel>
Expand Down Expand Up @@ -75,15 +76,6 @@ public slots:
int mDotCount;
};

class HelpWebPage : public QtCollider::WebPage {
Q_OBJECT

public:
HelpWebPage(HelpBrowser* browser);

private:
HelpBrowser* mBrowser;
};

class HelpBrowser : public QWidget {
Q_OBJECT
Expand All @@ -96,6 +88,7 @@ class HelpBrowser : public QWidget {
ZoomOut,
ResetZoom,
Evaluate,
EvaluateRegion,

ActionCount
};
Expand Down Expand Up @@ -148,7 +141,7 @@ private slots:
void sendRequest(const QString& code);
QString symbolUnderCursor();

QWebEngineView* mWebView;
QtCollider::WebView* mWebView;
LoadProgressIndicator* mLoadProgressIndicator;

QSize mSizeHint;
Expand Down

0 comments on commit 6d8dd76

Please sign in to comment.