diff --git a/examples/widgets/browser/tabwidget.cpp b/examples/widgets/browser/tabwidget.cpp index 47bcc2261a393a7f1ffdac2cf2eb763e15a71523..3c71b2d53590c2e727c8766a842491308a77894d 100644 --- a/examples/widgets/browser/tabwidget.cpp +++ b/examples/widgets/browser/tabwidget.cpp @@ -473,12 +473,10 @@ WebView *TabWidget::newTab(bool makeCurrent) setCurrentWidget(webView); // webview actions -#if defined(QWEBENGINEPAGE_ACTION) for (int i = 0; i < m_actions.count(); ++i) { WebActionMapper *mapper = m_actions[i]; mapper->addChild(webView->page()->action(mapper->webAction())); } -#endif if (count() == 1) currentChanged(currentIndex()); @@ -801,12 +799,10 @@ QWebEnginePage::WebAction WebActionMapper::webAction() const void WebActionMapper::rootTriggered() { -#if defined(QWEBENGINEPAGE_ACTION) if (m_currentParent) { QAction *gotoAction = m_currentParent->action(m_webAction); gotoAction->trigger(); } -#endif } void WebActionMapper::childChanged() @@ -835,11 +831,9 @@ void WebActionMapper::updateCurrent(QWebEnginePage *currentParent) m_root->setChecked(false); return; } -#if defined(QWEBENGINEPAGE_ACTION) QAction *source = m_currentParent->action(m_webAction); m_root->setChecked(source->isChecked()); m_root->setEnabled(source->isEnabled()); -#endif connect(m_currentParent, SIGNAL(destroyed(QObject*)), this, SLOT(currentDestroyed())); } diff --git a/examples/widgets/browser/webview.cpp b/examples/widgets/browser/webview.cpp index c7069f89c865f8ec570955d360d34bd7483e7f15..18beaf65ebb021cae03c11bf26f1b24a7b9c9d60 100644 --- a/examples/widgets/browser/webview.cpp +++ b/examples/widgets/browser/webview.cpp @@ -258,10 +258,8 @@ void WebView::wheelEvent(QWheelEvent *event) void WebView::openLinkInNewTab() { -#if defined(QWEBENGINEPAGE_ACTION) m_page->m_openInNewTab = true; pageAction(QWebEnginePage::OpenLinkInNewWindow)->trigger(); -#endif } void WebView::setProgress(int progress) diff --git a/lib/widgets/Api/qwebenginepage.cpp b/lib/widgets/Api/qwebenginepage.cpp index d2dee546faa1850d1231882b93575db24fbc8454..6367ddecdc134141e29ae87252fd4def36167620 100644 --- a/lib/widgets/Api/qwebenginepage.cpp +++ b/lib/widgets/Api/qwebenginepage.cpp @@ -1,43 +1,24 @@ -/**************************************************************************** -** -** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). -** Contact: http://www.qt-project.org/legal -** -** This file is part of the QtWebEngine module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and Digia. For licensing terms and -** conditions see http://qt.digia.com/licensing. For further information -** use the contact form at http://qt.digia.com/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 2.1 requirements -** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Digia gives you certain additional -** rights. These rights are described in the Digia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3.0 as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU General Public License version 3.0 requirements will be -** met: http://www.gnu.org/copyleft/gpl.html. -** -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +/* + Copyright (C) 2012, 2013 Digia Plc and/or its subsidiary(-ies). + Copyright (C) 2008, 2009, 2012 Nokia Corporation and/or its subsidiary(-ies) + Copyright (C) 2007 Staikos Computing Services Inc. + Copyright (C) 2007 Apple Inc. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ #include "qwebenginepage.h" #include "qwebenginepage_p.h" @@ -49,8 +30,9 @@ #include "render_widget_host_view_qt_delegate_widget.h" #include "web_contents_adapter.h" -#include <QUrl> +#include <QAction> #include <QLayout> +#include <QUrl> QWebEnginePagePrivate::QWebEnginePagePrivate() : QObjectPrivate(QObjectPrivateVersion) @@ -60,6 +42,7 @@ QWebEnginePagePrivate::QWebEnginePagePrivate() , m_isLoading(false) { history->d_func()->pagePrivate = this; + memset(actions, 0, sizeof(actions)); } QWebEnginePagePrivate::~QWebEnginePagePrivate() @@ -67,6 +50,11 @@ QWebEnginePagePrivate::~QWebEnginePagePrivate() delete history; } +RenderWidgetHostViewQtDelegate *QWebEnginePagePrivate::CreateRenderWidgetHostViewQtDelegate() +{ + return new RenderWidgetHostViewQtDelegateWidget; +} + void QWebEnginePagePrivate::titleChanged(const QString &title) { Q_Q(QWebEnginePage); @@ -88,6 +76,7 @@ void QWebEnginePagePrivate::loadingStateChanged() if (m_isLoading) Q_EMIT q->loadStarted(); } + updateNavigationActions(); } QRectF QWebEnginePagePrivate::viewportRect() const @@ -108,11 +97,60 @@ void QWebEnginePagePrivate::focusContainer() view->setFocus(); } -RenderWidgetHostViewQtDelegate *QWebEnginePagePrivate::CreateRenderWidgetHostViewQtDelegate() +void QWebEnginePagePrivate::updateAction(QWebEnginePage::WebAction action) const { - return new RenderWidgetHostViewQtDelegateWidget; +#ifdef QT_NO_ACTION + Q_UNUSED(action) +#else + QAction *a = actions[action]; + if (!a) + return; + + bool enabled = false; + + switch (action) { + case QWebEnginePage::Back: + enabled = adapter->canGoBack(); + break; + case QWebEnginePage::Forward: + enabled = adapter->canGoForward(); + break; + case QWebEnginePage::Stop: + enabled = adapter->isLoading(); + break; + case QWebEnginePage::Reload: + case QWebEnginePage::ReloadAndBypassCache: + enabled = !adapter->isLoading(); + break; + default: + break; + } + + a->setEnabled(enabled); +#endif // QT_NO_ACTION +} + +void QWebEnginePagePrivate::updateNavigationActions() +{ + updateAction(QWebEnginePage::Back); + updateAction(QWebEnginePage::Forward); + updateAction(QWebEnginePage::Stop); + updateAction(QWebEnginePage::Reload); + updateAction(QWebEnginePage::ReloadAndBypassCache); } +#ifndef QT_NO_ACTION +void QWebEnginePagePrivate::_q_webActionTriggered(bool checked) +{ + Q_Q(QWebEnginePage); + QAction *a = qobject_cast<QAction *>(q->sender()); + if (!a) + return; + QWebEnginePage::WebAction action = static_cast<QWebEnginePage::WebAction>(a->data().toInt()); + q->triggerAction(action, checked); +} +#endif // QT_NO_ACTION + QWebEnginePage::QWebEnginePage(QObject* parent) : QObject(*new QWebEnginePagePrivate, parent) { @@ -139,6 +177,53 @@ QWidget *QWebEnginePage::view() const return d->view; } +#ifndef QT_NO_ACTION +QAction *QWebEnginePage::action(WebAction action) const +{ + Q_D(const QWebEnginePage); + if (action == QWebEnginePage::NoWebAction) + return 0; + if (d->actions[action]) + return d->actions[action]; + + QString text; + QIcon icon; + QStyle *style = d->view ? d->view->style() : qApp->style(); + + switch (action) { + case Back: + text = tr("Back"); + icon = style->standardIcon(QStyle::SP_ArrowBack); + break; + case Forward: + text = tr("Forward"); + icon = style->standardIcon(QStyle::SP_ArrowForward); + break; + case Stop: + text = tr("Stop"); + icon = style->standardIcon(QStyle::SP_BrowserStop); + break; + case Reload: + text = tr("Reload"); + icon = style->standardIcon(QStyle::SP_BrowserReload); + break; + default: + break; + } + + QAction *a = new QAction(const_cast<QWebEnginePage*>(this)); + a->setText(text); + a->setData(action); + a->setIcon(icon); + + connect(a, SIGNAL(triggered(bool)), this, SLOT(_q_webActionTriggered(bool))); + + d->actions[action] = a; + d->updateAction(action); + return a; +} +#endif // QT_NO_ACTION + void QWebEnginePage::triggerAction(WebAction action, bool) { Q_D(QWebEnginePage); diff --git a/lib/widgets/Api/qwebenginepage.h b/lib/widgets/Api/qwebenginepage.h index 698a38d83808d119e9857aac8eb4152217c8d72b..0a664edf8a1c27e52acef043d5d1e9a2bb9836f2 100644 --- a/lib/widgets/Api/qwebenginepage.h +++ b/lib/widgets/Api/qwebenginepage.h @@ -549,6 +549,10 @@ protected: private: Q_DECLARE_PRIVATE(QWebEnginePage); +#ifndef QT_NO_ACTION + Q_PRIVATE_SLOT(d_func(), void _q_webActionTriggered(bool checked)) +#endif + friend class QWebEngineView; friend class QWebEngineViewPrivate; }; diff --git a/lib/widgets/Api/qwebenginepage_p.h b/lib/widgets/Api/qwebenginepage_p.h index c7f4e36969c1b28f375fff75c8e9d7fcec9e79c7..b1987cbd086f0ad1a4e75623b3a8d1289df66764 100644 --- a/lib/widgets/Api/qwebenginepage_p.h +++ b/lib/widgets/Api/qwebenginepage_p.h @@ -70,9 +70,14 @@ public: virtual void loadFinished(bool success) Q_DECL_OVERRIDE; virtual void focusContainer() Q_DECL_OVERRIDE; + void updateAction(QWebEnginePage::WebAction) const; + void updateNavigationActions(); + void _q_webActionTriggered(bool checked); + QScopedPointer<WebContentsAdapter> adapter; QWebEngineHistory *history; QWebEngineView *view; + mutable QAction *actions[QWebEnginePage::WebActionCount]; bool m_isLoading; }; diff --git a/lib/widgets/Api/qwebengineview.cpp b/lib/widgets/Api/qwebengineview.cpp index 7e8ee35261244307df4d8025565558c61daef834..1bf1acf4db5546c0029be7d8dd708c25571c43e7 100644 --- a/lib/widgets/Api/qwebengineview.cpp +++ b/lib/widgets/Api/qwebengineview.cpp @@ -129,6 +129,18 @@ QUrl QWebEngineView::url() const return page()->url(); } +#ifndef QT_NO_ACTION +QAction* QWebEngineView::pageAction(QWebEnginePage::WebAction action) const +{ + return page()->action(action); +} +#endif + +void QWebEngineView::triggerPageAction(QWebEnginePage::WebAction action, bool checked) +{ + page()->triggerAction(action, checked); +} + void QWebEngineView::stop() { page()->triggerAction(QWebEnginePage::Stop); diff --git a/tests/widgets/qwebenginepage/tst_qwebenginepage.cpp b/tests/widgets/qwebenginepage/tst_qwebenginepage.cpp index f80b6ac7370f2869a7c75918e765f755a5bfcefa..0c11c6c16501615ee7f77390b1e7f56c1def1fe6 100644 --- a/tests/widgets/qwebenginepage/tst_qwebenginepage.cpp +++ b/tests/widgets/qwebenginepage/tst_qwebenginepage.cpp @@ -382,9 +382,6 @@ void tst_QWebEnginePage::loadFinished() void tst_QWebEnginePage::actionStates() { -#if !defined(QWEBENGINEPAGE_ACTION) - QSKIP("QWEBENGINEPAGE_ACTION"); -#else QWebEnginePage* page = m_view->page(); page->load(QUrl("qrc:///resources/script.html")); @@ -394,7 +391,6 @@ void tst_QWebEnginePage::actionStates() QTRY_VERIFY(reloadAction->isEnabled()); QTRY_VERIFY(!stopAction->isEnabled()); -#endif } class ConsolePage : public QWebEnginePage @@ -1672,9 +1668,6 @@ void tst_QWebEnginePage::loadCachedPage() void tst_QWebEnginePage::backActionUpdate() { -#if !defined(QWEBENGINEPAGE_ACTION) - QSKIP("QWEBENGINEPAGE_ACTION"); -#else QWebEngineView view; QWebEnginePage *page = view.page(); QAction *action = page->action(QWebEnginePage::Back); @@ -1688,7 +1681,6 @@ void tst_QWebEnginePage::backActionUpdate() QTRY_COMPARE(loadSpy.count(), 2); QVERIFY(action->isEnabled()); -#endif } #if defined(QWEBENGINEFRAME)