diff --git a/example/example.pro b/example/example.pro index 7c76bb9a767113aac039d579960cf1af60ae5137..33a2f6182ca9f2626534b4987ba5400970a488ff 100644 --- a/example/example.pro +++ b/example/example.pro @@ -1,7 +1,8 @@ TEMPLATE = app TARGET = example -SOURCES = main.cpp +HEADERS = quickwindow.h widgetwindow.h +SOURCES = quickwindow.cpp widgetwindow.cpp main.cpp INCLUDEPATH += ../lib @@ -10,4 +11,4 @@ LIBPATH = $$getOutDir()/$$getConfigDir()/lib LIBS += -L$$LIBPATH -lblinq QMAKE_RPATHDIR += $$LIBPATH -QT += widgets +QT += widgets quick diff --git a/example/main.cpp b/example/main.cpp index 3d068303bc1298b7f760d9f44c8a6971ed0d9df8..edd75f3ec0f70cbd3b76d7b2db51aff78eab4787 100644 --- a/example/main.cpp +++ b/example/main.cpp @@ -38,16 +38,35 @@ ** $QT_END_LICENSE$ ** ****************************************************************************/ -#include <QtGui> -#include <QtWidgets> #include <blinqapplication.h> +#include "quickwindow.h" +#include "widgetwindow.h" -int main(int argc, char **argv) +int mainWidget(int argc, char **argv) +{ + BlinqApplication app(argc, argv); + + WidgetWindow window; + window.show(); + + return app.exec(); +} + +int mainQuick(int argc, char **argv) { - printf("main called\n"); BlinqApplication app(argc, argv); + QuickWindow window; + window.show(); + return app.exec(); } +int main(int argc, char **argv) +{ + if (qgetenv("QQUICKWEBENGINE").isNull()) + return mainWidget(argc, argv); + else + return mainQuick(argc, argv); +} diff --git a/example/quickwindow.cpp b/example/quickwindow.cpp new file mode 100644 index 0000000000000000000000000000000000000000..92cabb9eac10f8feffa7fc31e8a122e4f5fce06e --- /dev/null +++ b/example/quickwindow.cpp @@ -0,0 +1,49 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +#include "quickwindow.h" + +QuickWindow::QuickWindow() +{ + setSource(QUrl("example/quickwindow.qml")); + setResizeMode(QQuickView::SizeRootObjectToView); + setTitle("QQuick Example"); +} diff --git a/example/quickwindow.h b/example/quickwindow.h new file mode 100644 index 0000000000000000000000000000000000000000..7218d084eb3cd354b6513eb88fa2549ffac2ca83 --- /dev/null +++ b/example/quickwindow.h @@ -0,0 +1,55 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +#ifndef QUICKWINDOW_H +#define QUICKWINDOW_H + +#include <QQuickView> + +class QWebContentsView; + +class QuickWindow : public QQuickView { + Q_OBJECT +public: + QuickWindow(); +}; + +#endif // QUICKWINDOW_H diff --git a/lib/browser_window.qml b/example/quickwindow.qml similarity index 80% rename from lib/browser_window.qml rename to example/quickwindow.qml index 8f3c91cf1ace1c6224afbdc40c50f0531ac8c2fd..b424d959cd86911cd8bd0bdc34a9afda3cd1f58d 100644 --- a/lib/browser_window.qml +++ b/example/quickwindow.qml @@ -1,15 +1,11 @@ import QtQuick 2.0 +import QtWebEngine 1.0 Item { id: browserWindow height: 480 width: 320 - signal goBack - signal goForward - signal reload - signal load(string url) - Rectangle { id: navigationBar color: "grey" @@ -29,7 +25,7 @@ Item { MouseArea { anchors.fill: parent onClicked: { - browserWindow.goBack() + webContentsView.goBack() } } } @@ -44,7 +40,7 @@ Item { MouseArea { anchors.fill: parent onClicked: { - browserWindow.goForward() + webContentsView.goForward() } } } @@ -59,7 +55,7 @@ Item { MouseArea { anchors.fill: parent onClicked: { - browserWindow.reload() + webContentsView.reload() } } } @@ -71,33 +67,32 @@ Item { anchors.left: reloadButton.right anchors.right: parent.right - text: "www.google.com" cursorVisible: true persistentSelection: true selectByMouse: true onAccepted: { - browserWindow.load(addressBar.text) + webContentsView.url = text } } } - Rectangle { - id: viewContainer - objectName: "viewContainer" + WebContentsView { + id: webContentsView focus: true - color: "blue" anchors.top: navigationBar.bottom anchors.bottom: parent.bottom anchors.left: parent.left anchors.right: parent.right Binding { - target: viewContainer.children[0] + target: webContentsView.children[0] property: 'anchors.fill' - value: viewContainer - when: viewContainer.children.length > 0 + value: webContentsView + when: webContentsView.children.length > 0 } + + onUrlChanged: addressBar.text = url } Text { diff --git a/example/widgetwindow.cpp b/example/widgetwindow.cpp new file mode 100644 index 0000000000000000000000000000000000000000..89982e509bf8ff8eab8df9c0d4a9fcb31368fd2c --- /dev/null +++ b/example/widgetwindow.cpp @@ -0,0 +1,102 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +#include "widgetwindow.h" + +#include "qwebcontentsview.h" + +WidgetWindow::WidgetWindow() +: m_webView(new QWebContentsView) +, addressLineEdit(0) +{ + // Use oxygen as a fallback. + if (QIcon::themeName().isEmpty()) + QIcon::setThemeName("oxygen"); + + setGeometry(0, 0, 1024, 768); + + QVBoxLayout* layout = new QVBoxLayout; + + // Create a widget based address bar. + QHBoxLayout* addressBar = new QHBoxLayout; + + QToolButton* backButton = new QToolButton; + backButton->setIcon(QIcon::fromTheme("go-previous")); + addressBar->addWidget(backButton); + + QToolButton* forwardButton = new QToolButton; + forwardButton->setIcon(QIcon::fromTheme("go-next")); + addressBar->addWidget(forwardButton); + + QToolButton* reloadButton = new QToolButton; + reloadButton->setIcon(QIcon::fromTheme("view-refresh")); + addressBar->addWidget(reloadButton); + + addressLineEdit = new QLineEdit; + addressBar->addWidget(addressLineEdit); + + layout->addLayout(addressBar); + layout->addWidget(m_webView.data()); + + setLayout(layout); + + connect(addressLineEdit, SIGNAL(returnPressed()), SLOT(loadAddressFromAddressBar())); + connect(backButton, SIGNAL(clicked()), m_webView.data(), SLOT(back())); + connect(forwardButton, SIGNAL(clicked()), m_webView.data(), SLOT(forward())); + connect(reloadButton, SIGNAL(clicked()), m_webView.data(), SLOT(reload())); + connect(m_webView.data(), SIGNAL(titleChanged(const QString&)), SLOT(setWindowTitle(const QString&))); + connect(m_webView.data(), SIGNAL(urlChanged(const QUrl&)), SLOT(setAddressBarUrl(const QUrl&))); +} + +WidgetWindow::~WidgetWindow() +{ +} + +void WidgetWindow::loadAddressFromAddressBar() +{ + m_webView->load(addressLineEdit->text()); +} + +void WidgetWindow::setAddressBarUrl(const QUrl& url) +{ + addressLineEdit->setText(url.toString()); +} + diff --git a/example/widgetwindow.h b/example/widgetwindow.h new file mode 100644 index 0000000000000000000000000000000000000000..e579de27129e159b676f62a154a3cddd2ae36e1c --- /dev/null +++ b/example/widgetwindow.h @@ -0,0 +1,65 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +#ifndef WIDGETWINDOW_H +#define WIDGETWINDOW_H + +#include <QtWidgets> +#include <QScopedPointer> + +class QWebContentsView; + +class WidgetWindow : public QWidget { + Q_OBJECT +public: + WidgetWindow(); + ~WidgetWindow(); + +private Q_SLOTS: + void loadAddressFromAddressBar(); + void setAddressBarUrl(const QUrl& url); + +private: + QLineEdit* addressLineEdit; + QScopedPointer<QWebContentsView> m_webView; +}; + +#endif // WIDGETWINDOW_H diff --git a/lib/blinqapplication.cpp b/lib/blinqapplication.cpp index 49e6c8c4e74696cf68c08bcc5b09c92f6c83ab38..ae1efb728f30f611c72bec4d4d78cfa2a8d20b5f 100644 --- a/lib/blinqapplication.cpp +++ b/lib/blinqapplication.cpp @@ -58,6 +58,7 @@ #include "content_browser_client_qt.h" #include "web_contents_view_qt.h" +#include "qquickwebcontentsview.h" namespace { @@ -252,6 +253,9 @@ BlinqApplication::BlinqApplication(int &argc, char **argv) } base::ThreadRestrictions::SetIOAllowed(true); + + // FIXME: Do a proper plugin. + qmlRegisterType<QQuickWebContentsView>("QtWebEngine", 1, 0, "WebContentsView"); } int BlinqApplication::exec() diff --git a/lib/content_browser_client_qt.cpp b/lib/content_browser_client_qt.cpp index 0470d6700acaf4638ed8c7108f3a627a66f01863..65ef0f75cd2f8da6dfaa96759627a4db0f53092f 100644 --- a/lib/content_browser_client_qt.cpp +++ b/lib/content_browser_client_qt.cpp @@ -12,25 +12,9 @@ #include "content/public/browser/browser_main_parts.h" #include "net/base/net_module.h" #include "net/base/net_util.h" - #include "browser_context_qt.h" #include "web_contents_view_qt.h" -static GURL GetStartupURL() { - CommandLine* command_line = CommandLine::ForCurrentProcess(); - const CommandLine::StringVector& args = command_line->GetArgs(); - - if (args.empty()) - return GURL("http://www.google.com/"); - - GURL url(args[0]); - if (url.is_valid() && url.has_scheme()) - return url; - - return net::FilePathToFileURL(base::FilePath(args[0])); -} - - class BrowserMainPartsQt : public content::BrowserMainParts { public: @@ -48,8 +32,6 @@ public: m_browserContext.reset(new content::ShellBrowserContext(false)); m_offTheRecordBrowserContext.reset(new content::ShellBrowserContext(true)); - content::Shell::CreateNewWindow(m_browserContext.get(), GetStartupURL(), NULL, MSG_ROUTING_NONE, gfx::Size()); - if (m_parameters.ui_task) { m_parameters.ui_task->Run(); delete m_parameters.ui_task; @@ -66,6 +48,10 @@ public: m_offTheRecordBrowserContext.reset(); } + content::ShellBrowserContext* browser_context() const { + return m_browserContext.get(); + } + private: scoped_ptr<content::ShellBrowserContext> m_browserContext; scoped_ptr<content::ShellBrowserContext> m_offTheRecordBrowserContext; @@ -89,14 +75,15 @@ content::WebContentsViewPort* ContentBrowserClientQt::OverrideCreateWebContentsV content::BrowserMainParts *ContentBrowserClientQt::CreateBrowserMainParts(const content::MainFunctionParams ¶meters) { m_browserMainParts = new BrowserMainPartsQt(parameters); - m_browser_context = new BrowserContextQt(); + // FIXME: We don't seem to need it yet, the ShellBrowserContext was being used. + // m_browser_context = new BrowserContextQt(); return m_browserMainParts; } -BrowserContextQt* ContentBrowserClientQt::browser_context() { - - return m_browser_context; +content::ShellBrowserContext* ContentBrowserClientQt::browser_context() { + return m_browserMainParts->browser_context(); + // return m_browser_context; } net::URLRequestContextGetter* ContentBrowserClientQt::CreateRequestContext(content::BrowserContext* content_browser_context, content::ProtocolHandlerMap* protocol_handlers) diff --git a/lib/content_browser_client_qt.h b/lib/content_browser_client_qt.h index 87af56ddf5f0543fdaa634b71c4cfbe74283bab4..0e695dd3452937650e5f125a0b87699913a8ab3e 100644 --- a/lib/content_browser_client_qt.h +++ b/lib/content_browser_client_qt.h @@ -11,6 +11,7 @@ namespace content { class BrowserMainParts; class RenderProcessHost; class RenderViewHostDelegateView; +class ShellBrowserContext; class ShellBrowserMainParts; class WebContentsViewPort; class WebContents; @@ -27,7 +28,7 @@ public: virtual content::BrowserMainParts* CreateBrowserMainParts(const content::MainFunctionParams& parameters) /*Q_DECL_OVERRIDE*/; - BrowserContextQt *browser_context(); + content::ShellBrowserContext *browser_context(); net::URLRequestContextGetter *CreateRequestContext(content::BrowserContext *content_browser_context, content::ProtocolHandlerMap *protocol_handlers); private: diff --git a/lib/lib.pro b/lib/lib.pro index 832dddca3243f3d23aed8174595acdae706d5999..f47717e735c0f2d93c89bbee86821e1de36ca59a 100644 --- a/lib/lib.pro +++ b/lib/lib.pro @@ -22,10 +22,11 @@ SOURCES = \ backing_store_qt.cpp \ blinqapplication.cpp \ content_browser_client_qt.cpp \ + qquickwebcontentsview.cpp \ + qwebcontentsview.cpp \ render_widget_host_view_qt.cpp \ resource_context_qt.cpp \ shell_qt.cpp \ - signal_connector.cpp \ web_event_factory.cpp \ native_view_qt.cpp @@ -34,10 +35,11 @@ HEADERS = \ blinqapplication.h \ browser_context_qt.h \ content_browser_client_qt.h \ + native_view_container_qt.h \ + native_view_qt.h \ + qquickwebcontentsview.h \ + qwebcontentsview.h \ render_widget_host_view_qt.h \ resource_context_qt.h \ - web_event_factory.h \ - signal_connector.h \ - native_view_container_qt.h \ - native_view_qt.h + web_event_factory.h diff --git a/lib/signal_connector.cpp b/lib/qquickwebcontentsview.cpp similarity index 53% rename from lib/signal_connector.cpp rename to lib/qquickwebcontentsview.cpp index b2b2fdfa9c75156c6ff2fe9a807d3a1bdd297ffe..0fdf9dd11e683b1323632108581628db2eed373b 100644 --- a/lib/signal_connector.cpp +++ b/lib/qquickwebcontentsview.cpp @@ -39,72 +39,76 @@ ** ****************************************************************************/ -#include "signal_connector.h" +#include "qquickwebcontentsview.h" +// Needed to get access to content::GetContentClient() +#define CONTENT_IMPLEMENTATION + +#include "content/public/browser/web_contents.h" #include "content/shell/shell.h" -#include <QObject> -#include <QQuickView> -#include <QQuickItem> -#include <QLineEdit> -#include <QToolButton> -#include <QDebug> +#include "content/shell/shell_browser_context.h" +#include "content_browser_client_qt.h" -SignalConnector::SignalConnector(content::Shell* shell, QQuickView* window) - : m_shell(shell) - , m_window(window) -{ - setParent(window); +#include <QWidget> +#include <QUrl> - QQuickItem* rootItem = window->rootObject(); - connect(rootItem, SIGNAL(load(QString)), this, SLOT(load(QString))); - QObject::connect(rootItem, SIGNAL(reload()), this, SLOT(reload())); - QObject::connect(rootItem, SIGNAL(goForward()), this, SLOT(goForward())); - QObject::connect(rootItem, SIGNAL(goBack()), this, SLOT(goBack())); +namespace content { +QQuickWebContentsView* gQuickView = 0; } -SignalConnector::SignalConnector(content::Shell* shell, QWidget* window) - : m_shell(shell) - , m_widget(window) +class QQuickWebContentsViewPrivate +{ +public: + scoped_ptr<content::Shell> shell; +}; + +QQuickWebContentsView::QQuickWebContentsView() { - setParent(window); + d.reset(new QQuickWebContentsViewPrivate); - m_addressLineEdit = m_widget->findChild<QLineEdit*>("AddressLineEdit"); - m_backButton = m_widget->findChild<QToolButton*>("BackButton"); - m_forwardButton = m_widget->findChild<QToolButton*>("ForwardButton"); - m_reloadButton = m_widget->findChild<QToolButton*>("ReloadButton"); + // Cheap hack to allow getting signals from shell_qt.cpp. + Q_ASSERT(!content::gQuickView); + content::gQuickView = this; - connect(m_addressLineEdit, SIGNAL(returnPressed()), this, SLOT(loadAddressFromAddressBar())); - connect(m_backButton, SIGNAL(clicked()), this, SLOT(goBack())); - connect(m_forwardButton, SIGNAL(clicked()), this, SLOT(goForward())); - connect(m_reloadButton, SIGNAL(clicked()), this, SLOT(reload())); + content::BrowserContext* browser_context = static_cast<ContentBrowserClientQt*>(content::GetContentClient()->browser())->browser_context(); + d->shell.reset(content::Shell::CreateNewWindow(browser_context, + GURL(std::string("http://qt-project.org/")), + NULL, + MSG_ROUTING_NONE, + gfx::Size())); } -void SignalConnector::loadAddressFromAddressBar() +QQuickWebContentsView::~QQuickWebContentsView() { - load(m_addressLineEdit->text()); } -void SignalConnector::load(const QString& url) const +QUrl QQuickWebContentsView::url() const { - GURL gurl(url.toStdString()); - if (!gurl.has_scheme()) - gurl = GURL(std::string("http://") + url.toStdString()); - m_shell->LoadURL(gurl); + GURL gurl = d->shell->web_contents()->GetActiveURL(); + return QUrl(QString::fromStdString(gurl.spec())); } -void SignalConnector::goBack() const +void QQuickWebContentsView::setUrl(const QUrl& url) { - m_shell->GoBackOrForward(-1); + QString urlString = url.toString(); + GURL gurl(urlString.toStdString()); + if (!gurl.has_scheme()) + gurl = GURL(std::string("http://") + urlString.toStdString()); + d->shell->LoadURL(gurl); } -void SignalConnector::goForward() const +void QQuickWebContentsView::goBack() { - m_shell->GoBackOrForward(1); + d->shell->GoBackOrForward(-1); } -void SignalConnector::reload() const +void QQuickWebContentsView::goForward() { - m_shell->Reload(); + d->shell->GoBackOrForward(1); } +void QQuickWebContentsView::reload() +{ + d->shell->Reload(); +} diff --git a/lib/qquickwebcontentsview.h b/lib/qquickwebcontentsview.h new file mode 100644 index 0000000000000000000000000000000000000000..030365c292a0b444497c13468991e28fc98bf24b --- /dev/null +++ b/lib/qquickwebcontentsview.h @@ -0,0 +1,81 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +#ifndef QQUICKWEBCONTESTSVIEW_H +#define QQUICKWEBCONTESTSVIEW_H + +#include <QQuickItem> +#include <QScopedPointer> + +namespace content { + class Shell; +} + +class QQuickWebContentsViewPrivate; + +class Q_DECL_EXPORT QQuickWebContentsView : public QQuickItem { + Q_OBJECT + Q_PROPERTY(QUrl url READ url WRITE setUrl NOTIFY urlChanged) +public: + QQuickWebContentsView(); + ~QQuickWebContentsView(); + + QUrl url() const; + void setUrl(const QUrl&); + +public Q_SLOTS: + void goBack(); + void goForward(); + void reload(); + +Q_SIGNALS: + void titleChanged(); + void urlChanged(); + +private: + QScopedPointer<QQuickWebContentsViewPrivate> d; + + friend class content::Shell; +}; + +QML_DECLARE_TYPE(QQuickWebContentsView) + +#endif // QQUICKWEBCONTESTSVIEW_H diff --git a/lib/qwebcontentsview.cpp b/lib/qwebcontentsview.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ea7a801af2b04bded680d6befcef2b5827876ac1 --- /dev/null +++ b/lib/qwebcontentsview.cpp @@ -0,0 +1,107 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +#include "qwebcontentsview.h" + +// Needed to get access to content::GetContentClient() +#define CONTENT_IMPLEMENTATION + +#include "content/shell/shell.h" +#include "content/shell/shell_browser_context.h" + +#include "content_browser_client_qt.h" + +#include <QWidget> +#include <QUrl> + +namespace content { +QWebContentsView* gWidgetView = 0; +} + +class QWebContentsViewPrivate +{ +public: + scoped_ptr<content::Shell> shell; +}; + +QWebContentsView::QWebContentsView() +{ + d.reset(new QWebContentsViewPrivate); + + // Cheap hack to allow getting signals from shell_qt.cpp. + Q_ASSERT(!content::gWidgetView); + content::gWidgetView = this; + + content::BrowserContext* browser_context = static_cast<ContentBrowserClientQt*>(content::GetContentClient()->browser())->browser_context(); + d->shell.reset(content::Shell::CreateNewWindow(browser_context, + GURL(std::string("http://qt-project.org/")), + NULL, + MSG_ROUTING_NONE, + gfx::Size())); +} + +QWebContentsView::~QWebContentsView() +{ +} + +void QWebContentsView::load(const QUrl& url) +{ + QString urlString = url.toString(); + GURL gurl(urlString.toStdString()); + if (!gurl.has_scheme()) + gurl = GURL(std::string("http://") + urlString.toStdString()); + d->shell->LoadURL(gurl); +} + +void QWebContentsView::back() +{ + d->shell->GoBackOrForward(-1); +} + +void QWebContentsView::forward() +{ + d->shell->GoBackOrForward(1); +} + +void QWebContentsView::reload() +{ + d->shell->Reload(); +} diff --git a/lib/signal_connector.h b/lib/qwebcontentsview.h similarity index 75% rename from lib/signal_connector.h rename to lib/qwebcontentsview.h index b1444163a14106772af0880a68adae0826644c0d..077c3cde6503bc03354a2169e42e6f47afb0a1da 100644 --- a/lib/signal_connector.h +++ b/lib/qwebcontentsview.h @@ -39,42 +39,39 @@ ** ****************************************************************************/ -#ifndef QT_SIGNAL_CONNECTOR_H -#define QT_SIGNAL_CONNECTOR_H +#ifndef QWEBCONTESTSVIEW_H +#define QWEBCONTESTSVIEW_H -#include <QObject> - -class QQuickView; -class QToolButton; -class QLineEdit; +#include <QWidget> +#include <QScopedPointer> namespace content { - class Shell; + class Shell; } -class SignalConnector : public QObject -{ - Q_OBJECT +class QWebContentsViewPrivate; + +class Q_DECL_EXPORT QWebContentsView : public QWidget { + Q_OBJECT public: - SignalConnector(content::Shell* shell, QQuickView* window); - SignalConnector(content::Shell* shell, QWidget* window); + QWebContentsView(); + ~QWebContentsView(); + + void load(const QUrl& url); public Q_SLOTS: - void loadAddressFromAddressBar(); - void load(const QString& url) const; - void goBack() const; - void goForward() const; - void reload() const; + void back(); + void forward(); + void reload(); + +Q_SIGNALS: + void titleChanged(const QString& title); + void urlChanged(const QUrl& url); private: - content::Shell* m_shell; - QQuickView* m_window; - QWidget* m_widget; + QScopedPointer<QWebContentsViewPrivate> d; - QLineEdit* m_addressLineEdit; - QToolButton* m_forwardButton; - QToolButton* m_backButton; - QToolButton* m_reloadButton; + friend class content::Shell; }; -#endif +#endif // QWEBCONTESTSVIEW_H diff --git a/lib/shell_qt.cpp b/lib/shell_qt.cpp index 54ee5cf491d206dcf1ccb881d29e84b14247d067..d1da44938da4ca9ab2ee43753c4df88dfe9db0cf 100644 --- a/lib/shell_qt.cpp +++ b/lib/shell_qt.cpp @@ -16,22 +16,20 @@ #include "content/public/common/renderer_preferences.h" #include "content/shell/shell_browser_context.h" #include "content/shell/shell_content_browser_client.h" -#include "signal_connector.h" +#include "qquickwebcontentsview.h" +#include "qwebcontentsview.h" #include "web_contents_view_qt.h" #include <QApplication> -#include <QHBoxLayout> #include <QVBoxLayout> -#include <QLineEdit> -#include <QToolButton> -#include <QQuickView> -#include <QWindow> - -static bool isWidgets = false; +#include <QUrl> namespace content { +extern QWebContentsView* gWidgetView; +extern QQuickWebContentsView* gQuickView; + void Shell::PlatformInitialize(const gfx::Size& default_window_size) { } @@ -50,11 +48,10 @@ void Shell::PlatformSetAddressBarURL(const GURL& url) return; fprintf(stderr, "Set Address to: %s\n", url.spec().c_str()); - - if (isWidgets) { - QLineEdit* addressLine = reinterpret_cast<QWidget*>(window_)->findChild<QLineEdit*>("AddressLineEdit"); - addressLine->setText(QString::fromStdString(url.spec())); - } + if (gWidgetView) + gWidgetView->urlChanged(QUrl(QString::fromStdString(url.spec()))); + else if (gQuickView) + gQuickView->urlChanged(); } @@ -69,71 +66,13 @@ void Shell::PlatformCreateWindow(int width, int height) { if (headless_) return; - if (!window_) { - if (qgetenv("QQUICKWEBENGINE").isNull()) { - fprintf(stderr, "Starting Widgets example...\n"); - isWidgets = true; - QWidget* window = new QWidget; - window_ = reinterpret_cast<gfx::NativeWindow>(window); - - window->setGeometry(100,100, width, height); - - QVBoxLayout* layout = new QVBoxLayout; - - // Create a widget based address bar. - QHBoxLayout* addressBar = new QHBoxLayout; - - int buttonWidth = 26; - QToolButton* backButton = new QToolButton; - backButton->setIcon(QIcon::fromTheme("go-previous")); - backButton->setObjectName("BackButton"); - addressBar->addWidget(backButton); - - QToolButton* forwardButton = new QToolButton; - forwardButton->setIcon(QIcon::fromTheme("go-next")); - forwardButton->setObjectName("ForwardButton"); - addressBar->addWidget(forwardButton); - - QToolButton* reloadButton = new QToolButton; - reloadButton->setIcon(QIcon::fromTheme("view-refresh")); - reloadButton->setObjectName("ReloadButton"); - addressBar->addWidget(reloadButton); - - QLineEdit* lineEdit = new QLineEdit; - lineEdit->setObjectName("AddressLineEdit"); - addressBar->addWidget(lineEdit); - - layout->addLayout(addressBar); - - window->setLayout(layout); - window->show(); - - SignalConnector* signalConnector = new SignalConnector(this, window); - } else { - fprintf(stderr, "Starting QQuick2 example...\n"); - // Use oxygen as a fallback. - if (QIcon::themeName().isEmpty()) - QIcon::setThemeName("oxygen"); - - QQuickView* window = new QQuickView; - window_ = reinterpret_cast<gfx::NativeWindow>(window); - - window->setGeometry(100,100, width, height); - - window->setSource(QUrl("lib/browser_window.qml")); - window->setResizeMode(QQuickView::SizeRootObjectToView); - window->setTitle("QQuick Example"); - - window->show(); - - // SignalConnector will act as a proxy for the QObject signals received from - // m_window. m_window will take ownership of the SignalConnector. - // The SignalConnector will search the children list of m_window - // for back/forward/reload buttons and for the address line edit. - // Therefore the layout must be set and completed before the SignalConnector - // is created. - SignalConnector* signalConnector = new SignalConnector(this, window); - } + if (gWidgetView) { + // The layout is used in PlatformSetContents. + QVBoxLayout* layout = new QVBoxLayout; + gWidgetView->setLayout(layout); + window_ = reinterpret_cast<gfx::NativeWindow>(gWidgetView); + } else if (gQuickView) { + window_ = reinterpret_cast<gfx::NativeWindow>(gQuickView); } } @@ -148,27 +87,15 @@ void Shell::PlatformSetContents() rendererPrefs->caret_blink_interval = static_cast<double>(qApp->cursorFlashTime())/2000; web_contents_->GetRenderViewHost()->SyncRendererPrefs(); - if (isWidgets) { + if (gWidgetView) { WebContentsViewQt* content_view = static_cast<WebContentsViewQt*>(web_contents_->GetView()); - QVBoxLayout* layout = qobject_cast<QVBoxLayout*>(reinterpret_cast<QWidget*>(window_)->layout()); + QVBoxLayout* layout = qobject_cast<QVBoxLayout*>(gWidgetView->layout()); if (layout) layout->addLayout(content_view->windowContainer()->widget()); - } else { - QQuickView* view = reinterpret_cast<QQuickView*>(window_); - if (view->status() != QQuickView::Ready) - fprintf(stderr, "VIEW NOT READY!!!!\n"); - - QQuickItem* rootItem = view->rootObject(); - - QQuickItem* viewContainer = rootItem->findChild<QQuickItem*>("viewContainer"); - if (!viewContainer) - return; - + } else if (gQuickView) { WebContentsViewQt* content_view = static_cast<WebContentsViewQt*>(web_contents_->GetView()); QQuickItem* windowContainer = content_view->windowContainer()->qQuickItem(); - windowContainer->setParentItem(viewContainer); - windowContainer->setWidth(100); - windowContainer->setHeight(100); + windowContainer->setParentItem(gQuickView); } } @@ -233,9 +160,11 @@ void Shell::PlatformSetTitle(const string16& title) if (headless_) return; - // std::string title_utf8 = UTF16ToUTF8(title); - // if (window_) - // reinterpret_cast<QWidget*>(window_)->setWindowTitle(QString::fromStdString(title_utf8)); + if (gWidgetView) { + std::string title_utf8 = UTF16ToUTF8(title); + gWidgetView->titleChanged(QString::fromStdString(title_utf8)); + } else if (gQuickView) + gQuickView->titleChanged(); } } // namespace content