diff options
author | Allan Sandfeld Jensen <allan.jensen@theqtcompany.com> | 2016-02-04 13:50:57 +0100 |
---|---|---|
committer | Allan Sandfeld Jensen <allan.jensen@theqtcompany.com> | 2016-02-04 14:13:39 +0100 |
commit | 6722bbbf4ea2ddcdd9194f26a883029ec3afc39a (patch) | |
tree | c3290b82fac21c4990be8472ae6cb4ca043205cf /examples/webenginewidgets | |
parent | 41d69eb0fa2375f0da6ba9b35136f5598be4b3a4 (diff) | |
parent | 82f4d13a13b40d9cb7710f6dd4190175a272a394 (diff) |
Merge branch '5.6' into dev
Change-Id: I0bb971f01ee1e02da768f336680c8ec0254ab2b0
Diffstat (limited to 'examples/webenginewidgets')
15 files changed, 977 insertions, 90 deletions
diff --git a/examples/webenginewidgets/contentmanipulation/doc/images/contentmanipulation-example.png b/examples/webenginewidgets/contentmanipulation/doc/images/contentmanipulation-example.png Binary files differindex 717ac9ddc..ecdbc4c64 100644 --- a/examples/webenginewidgets/contentmanipulation/doc/images/contentmanipulation-example.png +++ b/examples/webenginewidgets/contentmanipulation/doc/images/contentmanipulation-example.png diff --git a/examples/webenginewidgets/contentmanipulation/doc/src/contentmanipulation.qdoc b/examples/webenginewidgets/contentmanipulation/doc/src/contentmanipulation.qdoc index ab5dd06c6..aa94b17da 100644 --- a/examples/webenginewidgets/contentmanipulation/doc/src/contentmanipulation.qdoc +++ b/examples/webenginewidgets/contentmanipulation/doc/src/contentmanipulation.qdoc @@ -29,114 +29,143 @@ \example webenginewidgets/contentmanipulation \title WebEngine Content Manipulation Example \ingroup webengine-widgetexamples - \brief Demonstrates how to use browse web and manipulate content - - \brief The Content Manipulation example shows how to use JQuery with QtWebEngine to - create a web browser with special effects and content - manipulation. + \brief Demonstrates how to load and manipulate web content. \image contentmanipulation-example.png - The application makes use of QWebEnginePage::evaluateJavaScript to - evaluate the jQuery JavaScript code. A QMainWindow with a QWebEngineView - as central widget builds up the browser itself. + \e{Content Manipulation} shows how to use JQuery with \l {Qt WebEngine Widgets} to + create a web browser with special effects and content manipulation. + + In the application, we call QWebEnginePage::runJavaScript() to + execute jQuery JavaScript code. We implement a QMainWindow with a QWebEngineView + as a central widget to build up the browser itself. \include examples-run.qdocinc \section1 MainWindow Class Definition The \c MainWindow class inherits QMainWindow. It implements a number of - slots to perform actions on both the application and on the web content. + slots to perform actions on both the application and on the web content: - \snippet webenginewidgets/contentmanipulation/mainwindow.h 1 + \quotefromfile webenginewidgets/contentmanipulation/mainwindow.h + \skipto class MainWindow : + \printuntil /^\}/ - We also declare a QString that contains the jQuery, a QWebView + We also declare a QString that contains the jQuery, a QWebEngineView that displays the web content, and a QLineEdit that acts as the address bar. \section1 MainWindow Class Implementation - We start by implementing the constructor. - - \snippet webenginewidgets/contentmanipulation/mainwindow.cpp 1 + We start by implementing the constructor. The first part of the constructor sets the value of + \c progress to 0. This value will be used later in the code to visualize the loading of a + web page: - The first part of the constructor sets the value of \c progress to - 0. This value will be used later in the code to visualize the - loading of a webpage. + \quotefromfile webenginewidgets/contentmanipulation/mainwindow.cpp + \skipto MainWindow::MainWindow + \printuntil progress - Next, the jQuery library is loaded using a QFile and reading the file + Next, the jQuery library is loaded by using a QFile and reading the file content. The jQuery library is a JavaScript library that provides different - functions for manipulating HTML. + functions for manipulating HTML: + + \printuntil file.close() + + The second part of the constructor creates a QWebEngineView and connects + slots to the view's signals: + + \printuntil SLOT(finishLoading + + Furthermore, we create a QLineEdit as the browser's address bar. We then set the horizontal + QSizePolicy to fill the available area in the browser at all times. We add the + QLineEdit to a QToolBar together with a set of navigation actions from + QWebEngineView::pageAction(): + + \printuntil addWidget(locationEdit) + + The third part of the constructor implements two QMenu widgets and assigns + a set of actions to them: + + \printuntil removeEmbeddedElements - \snippet webenginewidgets/contentmanipulation/mainwindow.cpp 2 + The last line sets the QWebEngineView as the central widget in the QMainWindow: - The second part of the constructor creates a QWebView and connects - slots to the views signals. Furthermore, we create a QLineEdit as - the browsers address bar. We then set the horizontal QSizePolicy - to fill the available area in the browser at all times. We add the - QLineEdit to a QToolbar together with a set of navigation actions - from QWebView::pageAction. + \printuntil } - \snippet webenginewidgets/contentmanipulation/mainwindow.cpp 3 + When the page is loaded, \c adjustLocation() is triggered by the \c loadFinished() signal in + QWebEngineView to update the address bar: - The third and last part of the constructor implements two QMenus and assigns - a set of actions to them. The last line sets the QWebView as the central - widget in the QMainWindow. + \skipto adjustLocation() + \printuntil } - \snippet webenginewidgets/contentmanipulation/mainwindow.cpp 4 + In \c changeLocation(), we create a QUrl object, and then use it to load the page into the + QWebEngineView. When the new web page has finished loading, \c adjustLocation() will be + run once more to update the address bar: - When the page is loaded, \c adjustLocation() updates the address - bar; \c adjustLocation() is triggered by the \c loadFinished() - signal in QWebView. In \c changeLocation() we create a QUrl - object, and then use it to load the page into the QWebView. When - the new web page has finished loading, \c adjustLocation() will be - run once more to update the address bar. + \printuntil } - \snippet webenginewidgets/contentmanipulation/mainwindow.cpp 5 + The \c adjustTitle() method sets the window title and displays the loading progress: - \c adjustTitle() sets the window title and displays the loading - progress. This slot is triggered by the \c titleChanged() signal - in QWebView. + \printuntil } + \printuntil } - \snippet webenginewidgets/contentmanipulation/mainwindow.cpp 6 + This slot is triggered by the \c titleChanged() signal in QWebEngineView. - When a web page has loaded, \c finishLoading() is triggered by the - \c loadFinished() signal in QWebView. \c finishLoading() then updates the - progress in the title bar and calls \c evaluateJavaScript() - to evaluate the jQuery library. This evaluates the JavaScript against the - current web page. What that means is that the JavaScript can be viewed as - part of the content loaded into the QWebView, and therefore needs to be + When a web page has loaded, the \c finishLoading() method is triggered by the + \c loadFinished() signal in QWebEngineView. The method then updates the + progress in the title bar and calls \c runJavaScript() + to evaluate the jQuery library against the current web page: + + \printuntil } + + This means that the JavaScript can be viewed as a + part of the content loaded into the QWebEngineView, and therefore needs to be loaded every time a new page is loaded. Once the jQuery library is loaded, we can start executing the different jQuery functions in the browser. - The rotateImages() function is then called explicitely to make sure + The \c rotateImages() function is then called explicitly to make sure that the images of the newly loaded page respect the state of the toggle action. - \snippet webenginewidgets/contentmanipulation/mainwindow.cpp 7 - The first jQuery-based function, \c highlightAllLinks(), is designed to highlight all links in the current webpage. The JavaScript code looks for web elements named \e {a}, which is the tag for a hyperlink. For each such element, the background color is set to be yellow by - using CSS. + using CSS: - \snippet webenginewidgets/contentmanipulation/mainwindow.cpp 8 + \printuntil } + \printuntil } + + We append \c undefined after the jQuery call to prevent a possible recursion loop + and crash caused by the way the elements returned by the each iterator elements + reference each other, which causes problems upon converting them to QVariant. The \c rotateImages() function rotates the images on the current - web page. This JavaScript code relies on CSS transforms and + web page. This JavaScript code relies on CSS transforms. It looks up all \e {img} elements and rotates the images 180 degrees - and then back again. + and then back again: - \snippet webenginewidgets/contentmanipulation/mainwindow.cpp 9 + \printuntil runJavaScript(code); + \printuntil } - The remaining four methods remove different elements from the current web - page. \c removeGifImages() removes all GIF images on the page by looking up + The remaining methods remove different elements from the current web + page. The \c removeGifImages() removes all GIF images on the page by looking up the \e {src} attribute of all the elements on the web page. Any element with - a \e {gif} file as its source is removed. \c removeInlineFrames() removes all - \e {iframe} or inline elements. \c removeObjectElements() removes all - \e {object} elements, and \c removeEmbeddedElements() removes any elements - such as plugins embedded on the page using the \e {embed} tag. + a \e {gif} file as its source is removed: + + \printuntil } + + The \c removeInlineFrames() method removes all \e {iframe} or inline elements: + + \printuntil } + + The \c removeObjectElements() method removes all \e {object} elements: + + \printuntil } + + The \c removeEmbeddedElements() method removes any elements using the \e {embed} tag, such as + plugins embedded on the page: + \printuntil } */ diff --git a/examples/webenginewidgets/contentmanipulation/mainwindow.cpp b/examples/webenginewidgets/contentmanipulation/mainwindow.cpp index 545d2c45a..3198f0908 100644 --- a/examples/webenginewidgets/contentmanipulation/mainwindow.cpp +++ b/examples/webenginewidgets/contentmanipulation/mainwindow.cpp @@ -68,8 +68,6 @@ InvokeWrapper<Arg, R, C> invoke(R *receiver, void (C::*memberFun)(Arg)) return wrapper; } -//! [1] - MainWindow::MainWindow(const QUrl& url) { progress = 0; @@ -80,9 +78,7 @@ MainWindow::MainWindow(const QUrl& url) jQuery = file.readAll(); jQuery.append("\nvar qt = { 'jQuery': jQuery.noConflict(true) };"); file.close(); -//! [1] -//! [2] view = new QWebEngineView(this); view->load(url); connect(view, SIGNAL(loadFinished(bool)), SLOT(adjustLocation())); @@ -100,14 +96,12 @@ MainWindow::MainWindow(const QUrl& url) toolBar->addAction(view->pageAction(QWebEnginePage::Reload)); toolBar->addAction(view->pageAction(QWebEnginePage::Stop)); toolBar->addWidget(locationEdit); -//! [2] QMenu *viewMenu = menuBar()->addMenu(tr("&View")); QAction* viewSourceAction = new QAction("Page Source", this); connect(viewSourceAction, SIGNAL(triggered()), SLOT(viewSource())); viewMenu->addAction(viewSourceAction); -//! [3] QMenu *effectMenu = menuBar()->addMenu(tr("&Effect")); effectMenu->addAction("Highlight all links", this, SLOT(highlightAllLinks())); @@ -126,7 +120,6 @@ MainWindow::MainWindow(const QUrl& url) setCentralWidget(view); } -//! [3] void MainWindow::viewSource() { @@ -139,7 +132,6 @@ void MainWindow::viewSource() view->page()->toHtml(invoke(textEdit, &QTextEdit::setPlainText)); } -//! [4] void MainWindow::adjustLocation() { locationEdit->setText(view->url().toString()); @@ -151,9 +143,7 @@ void MainWindow::changeLocation() view->load(url); view->setFocus(); } -//! [4] -//! [5] void MainWindow::adjustTitle() { if (progress <= 0 || progress >= 100) @@ -167,9 +157,7 @@ void MainWindow::setProgress(int p) progress = p; adjustTitle(); } -//! [5] -//! [6] void MainWindow::finishLoading(bool) { progress = 100; @@ -178,36 +166,24 @@ void MainWindow::finishLoading(bool) rotateImages(rotateAction->isChecked()); } -//! [6] -//! [7] void MainWindow::highlightAllLinks() { - // We append '; undefined' after the jQuery call here to prevent a possible recursion loop and crash caused by - // the way the elements returned by the each iterator elements reference each other, which causes problems upon - // converting them to QVariants. QString code = "qt.jQuery('a').each( function () { qt.jQuery(this).css('background-color', 'yellow') } ); undefined"; view->page()->runJavaScript(code); } -//! [7] -//! [8] void MainWindow::rotateImages(bool invert) { QString code; - // We append '; undefined' after each of the jQuery calls here to prevent a possible recursion loop and crash caused by - // the way the elements returned by the each iterator elements reference each other, which causes problems upon - // converting them to QVariants. if (invert) code = "qt.jQuery('img').each( function () { qt.jQuery(this).css('-webkit-transition', '-webkit-transform 2s'); qt.jQuery(this).css('-webkit-transform', 'rotate(180deg)') } ); undefined"; else code = "qt.jQuery('img').each( function () { qt.jQuery(this).css('-webkit-transition', '-webkit-transform 2s'); qt.jQuery(this).css('-webkit-transform', 'rotate(0deg)') } ); undefined"; view->page()->runJavaScript(code); } -//! [8] -//! [9] void MainWindow::removeGifImages() { QString code = "qt.jQuery('[src*=gif]').remove()"; @@ -231,5 +207,3 @@ void MainWindow::removeEmbeddedElements() QString code = "qt.jQuery('embed').remove()"; view->page()->runJavaScript(code); } -//! [9] - diff --git a/examples/webenginewidgets/contentmanipulation/mainwindow.h b/examples/webenginewidgets/contentmanipulation/mainwindow.h index 65a6cc6c2..66512f969 100644 --- a/examples/webenginewidgets/contentmanipulation/mainwindow.h +++ b/examples/webenginewidgets/contentmanipulation/mainwindow.h @@ -55,7 +55,6 @@ class QWebEngineView; class QLineEdit; QT_END_NAMESPACE -//! [1] class MainWindow : public QMainWindow { Q_OBJECT @@ -86,5 +85,4 @@ private: QLineEdit *locationEdit; QAction *rotateAction; int progress; -//! [1] }; diff --git a/examples/webenginewidgets/cookiebrowser/cookiebrowser.pro b/examples/webenginewidgets/cookiebrowser/cookiebrowser.pro new file mode 100644 index 000000000..66ea064ef --- /dev/null +++ b/examples/webenginewidgets/cookiebrowser/cookiebrowser.pro @@ -0,0 +1,19 @@ +QT += core gui webenginewidgets +TARGET = cookiebrowser +TEMPLATE = app +CONFIG += c++11 + +SOURCES += \ + main.cpp\ + mainwindow.cpp + +HEADERS += \ + mainwindow.h + +FORMS += \ + mainwindow.ui \ + cookiedialog.ui \ + cookiewidget.ui + +RESOURCES += \ + cookiebrowser.qrc diff --git a/examples/webenginewidgets/cookiebrowser/cookiebrowser.qrc b/examples/webenginewidgets/cookiebrowser/cookiebrowser.qrc new file mode 100644 index 000000000..8805f2c53 --- /dev/null +++ b/examples/webenginewidgets/cookiebrowser/cookiebrowser.qrc @@ -0,0 +1,5 @@ +<RCC> + <qresource prefix="/"> + <file>view-refresh.png</file> + </qresource> +</RCC> diff --git a/examples/webenginewidgets/cookiebrowser/cookiedialog.ui b/examples/webenginewidgets/cookiebrowser/cookiedialog.ui new file mode 100644 index 000000000..ad6e5cab1 --- /dev/null +++ b/examples/webenginewidgets/cookiebrowser/cookiedialog.ui @@ -0,0 +1,189 @@ +<?xml version="1.0" encoding="UTF-8"?> +<ui version="4.0"> + <class>CookieDialog</class> + <widget class="QDialog" name="CookieDialog"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>400</width> + <height>245</height> + </rect> + </property> + <property name="windowTitle"> + <string>Cookie</string> + </property> + <layout class="QFormLayout" name="formLayout"> + <item row="0" column="0"> + <widget class="QLabel" name="label"> + <property name="text"> + <string>Name</string> + </property> + </widget> + </item> + <item row="0" column="1"> + <widget class="QLineEdit" name="m_nameLineEdit"> + <property name="readOnly"> + <bool>true</bool> + </property> + </widget> + </item> + <item row="1" column="0"> + <widget class="QLabel" name="label_2"> + <property name="text"> + <string>Domain</string> + </property> + </widget> + </item> + <item row="1" column="1"> + <widget class="QLineEdit" name="m_domainLineEdit"> + <property name="readOnly"> + <bool>true</bool> + </property> + </widget> + </item> + <item row="5" column="0"> + <widget class="QLabel" name="label_4"> + <property name="text"> + <string>Path</string> + </property> + </widget> + </item> + <item row="5" column="1"> + <widget class="QLineEdit" name="m_pathLineEdit"> + <property name="readOnly"> + <bool>true</bool> + </property> + </widget> + </item> + <item row="6" column="0"> + <widget class="QLabel" name="label_5"> + <property name="text"> + <string>isHttpOnly</string> + </property> + </widget> + </item> + <item row="6" column="1"> + <widget class="QComboBox" name="m_isHttpOnlyComboBox"/> + </item> + <item row="7" column="0"> + <widget class="QLabel" name="label_3"> + <property name="text"> + <string>isSecure</string> + </property> + </widget> + </item> + <item row="8" column="1"> + <layout class="QHBoxLayout" name="horizontalLayout"> + <item> + <spacer name="horizontalSpacer"> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + <property name="sizeHint" stdset="0"> + <size> + <width>40</width> + <height>20</height> + </size> + </property> + </spacer> + </item> + <item> + <widget class="QPushButton" name="m_addButton"> + <property name="enabled"> + <bool>true</bool> + </property> + <property name="text"> + <string>Add</string> + </property> + </widget> + </item> + <item> + <widget class="QPushButton" name="m_cancelButton"> + <property name="text"> + <string>Cancel</string> + </property> + </widget> + </item> + </layout> + </item> + <item row="7" column="1"> + <widget class="QComboBox" name="m_isSecureComboBox"/> + </item> + <item row="3" column="0"> + <widget class="QLabel" name="label_6"> + <property name="text"> + <string>Value</string> + </property> + </widget> + </item> + <item row="3" column="1"> + <widget class="QLineEdit" name="m_valueLineEdit"> + <property name="readOnly"> + <bool>true</bool> + </property> + </widget> + </item> + <item row="4" column="1"> + <widget class="QDateEdit" name="m_dateEdit"> + <property name="readOnly"> + <bool>true</bool> + </property> + </widget> + </item> + <item row="4" column="0"> + <widget class="QLabel" name="label_7"> + <property name="text"> + <string>Expires</string> + </property> + </widget> + </item> + </layout> + </widget> + <tabstops> + <tabstop>m_nameLineEdit</tabstop> + <tabstop>m_domainLineEdit</tabstop> + <tabstop>m_valueLineEdit</tabstop> + <tabstop>m_dateEdit</tabstop> + <tabstop>m_pathLineEdit</tabstop> + <tabstop>m_isHttpOnlyComboBox</tabstop> + <tabstop>m_isSecureComboBox</tabstop> + <tabstop>m_addButton</tabstop> + <tabstop>m_cancelButton</tabstop> + </tabstops> + <resources/> + <connections> + <connection> + <sender>m_cancelButton</sender> + <signal>clicked()</signal> + <receiver>CookieDialog</receiver> + <slot>reject()</slot> + <hints> + <hint type="sourcelabel"> + <x>350</x> + <y>103</y> + </hint> + <hint type="destinationlabel"> + <x>199</x> + <y>63</y> + </hint> + </hints> + </connection> + <connection> + <sender>m_addButton</sender> + <signal>clicked()</signal> + <receiver>CookieDialog</receiver> + <slot>accept()</slot> + <hints> + <hint type="sourcelabel"> + <x>265</x> + <y>112</y> + </hint> + <hint type="destinationlabel"> + <x>199</x> + <y>63</y> + </hint> + </hints> + </connection> + </connections> +</ui> diff --git a/examples/webenginewidgets/cookiebrowser/cookiewidget.ui b/examples/webenginewidgets/cookiebrowser/cookiewidget.ui new file mode 100644 index 000000000..816d7473e --- /dev/null +++ b/examples/webenginewidgets/cookiebrowser/cookiewidget.ui @@ -0,0 +1,92 @@ +<?xml version="1.0" encoding="UTF-8"?> +<ui version="4.0"> + <class>CookieWidget</class> + <widget class="QWidget" name="CookieWidget"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>300</width> + <height>71</height> + </rect> + </property> + <property name="sizePolicy"> + <sizepolicy hsizetype="Preferred" vsizetype="Preferred"> + <horstretch>0</horstretch> + <verstretch>0</verstretch> + </sizepolicy> + </property> + <property name="minimumSize"> + <size> + <width>300</width> + <height>0</height> + </size> + </property> + <property name="maximumSize"> + <size> + <width>310</width> + <height>16777215</height> + </size> + </property> + <property name="windowTitle"> + <string>Form</string> + </property> + <layout class="QHBoxLayout" name="horizontalLayout" stretch="3,1"> + <item> + <layout class="QFormLayout" name="formLayout"> + <property name="formAlignment"> + <set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set> + </property> + <item row="0" column="0"> + <widget class="QLabel" name="label"> + <property name="text"> + <string>Name:</string> + </property> + </widget> + </item> + <item row="0" column="1"> + <widget class="QLabel" name="m_nameLabel"> + <property name="text"> + <string>Empty</string> + </property> + </widget> + </item> + <item row="1" column="0"> + <widget class="QLabel" name="label_2"> + <property name="text"> + <string>Domain:</string> + </property> + </widget> + </item> + <item row="1" column="1"> + <widget class="QLabel" name="m_domainLabel"> + <property name="text"> + <string>Emtpy</string> + </property> + </widget> + </item> + </layout> + </item> + <item> + <layout class="QVBoxLayout" name="verticalLayout"> + <item> + <widget class="QPushButton" name="m_viewButton"> + <property name="text"> + <string>View</string> + </property> + </widget> + </item> + <item> + <widget class="QPushButton" name="m_deleteButton"> + <property name="text"> + <string>Delete</string> + </property> + </widget> + </item> + </layout> + </item> + </layout> + </widget> + <resources/> + <connections/> +</ui> diff --git a/examples/webenginewidgets/cookiebrowser/doc/images/cookiebrowser.png b/examples/webenginewidgets/cookiebrowser/doc/images/cookiebrowser.png Binary files differnew file mode 100644 index 000000000..4e76ba6d9 --- /dev/null +++ b/examples/webenginewidgets/cookiebrowser/doc/images/cookiebrowser.png diff --git a/examples/webenginewidgets/cookiebrowser/doc/src/cookiebrowser.qdoc b/examples/webenginewidgets/cookiebrowser/doc/src/cookiebrowser.qdoc new file mode 100644 index 000000000..259de0724 --- /dev/null +++ b/examples/webenginewidgets/cookiebrowser/doc/src/cookiebrowser.qdoc @@ -0,0 +1,41 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:FDL$ +** 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 The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Free Documentation License Usage +** Alternatively, this file may be used under the terms of the GNU Free +** Documentation License version 1.3 as published by the Free Software +** Foundation and appearing in the file included in the packaging of +** this file. Please review the following information to ensure +** the GNU Free Documentation License version 1.3 requirements +** will be met: http://www.gnu.org/copyleft/fdl.html. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +/*! + \example webenginewidgets/cookiebrowser + \title WebEngine Cookie Browser Example + \ingroup webengine-widgetexamples + \brief A cookie browser based on Qt WebEngine Widgets + + \image cookiebrowser.png + + \e {Cookie Browser} demonstrates how to use the \l{Qt WebEngine Widgets C++ Classes} + {Qt WebEngine C++ classes} to manage cookies. The browser can be used to view cookie content as + well as delete cookies and add new cookies. + + \include examples-run.qdocinc +*/ diff --git a/examples/webenginewidgets/cookiebrowser/main.cpp b/examples/webenginewidgets/cookiebrowser/main.cpp new file mode 100644 index 000000000..c122eb7c3 --- /dev/null +++ b/examples/webenginewidgets/cookiebrowser/main.cpp @@ -0,0 +1,52 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the demonstration applications of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** 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 The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/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. +** +** As a special exception, The Qt Company gives you certain additional +** rights. These rights are described in The Qt Company 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 "mainwindow.h" +#include <QApplication> +#include <QUrl> + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + MainWindow window(QUrl("http://qt.io")); + window.show(); + return app.exec(); +} diff --git a/examples/webenginewidgets/cookiebrowser/mainwindow.cpp b/examples/webenginewidgets/cookiebrowser/mainwindow.cpp new file mode 100644 index 000000000..4139153c9 --- /dev/null +++ b/examples/webenginewidgets/cookiebrowser/mainwindow.cpp @@ -0,0 +1,192 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the demonstration applications of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** 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 The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/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. +** +** As a special exception, The Qt Company gives you certain additional +** rights. These rights are described in The Qt Company 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 "mainwindow.h" +#include <QWebEngineCookieStore> +#include <QWebEngineProfile> + +CookieDialog::CookieDialog(const QNetworkCookie &cookie, QWidget *parent): QDialog(parent) +{ + setupUi(this); + m_nameLineEdit->setText(cookie.name()); + m_domainLineEdit->setText(cookie.domain()); + m_valueLineEdit->setText(cookie.value()); + m_pathLineEdit->setText(cookie.path()); + m_dateEdit->setDate(cookie.expirationDate().date()); + m_isSecureComboBox->addItem(cookie.isSecure() ? tr("yes") : tr("no")); + m_isHttpOnlyComboBox->addItem(cookie.isHttpOnly() ? tr("yes") : tr("no")); + m_addButton->setVisible(false); + m_cancelButton->setText(tr("Close")); +} + +CookieDialog::CookieDialog(QWidget *parent): QDialog(parent) +{ + setupUi(this); + m_nameLineEdit->setReadOnly(false); + m_domainLineEdit->setReadOnly(false); + m_valueLineEdit->setReadOnly(false); + m_pathLineEdit->setReadOnly(false); + m_dateEdit->setReadOnly(false); + m_dateEdit->setDate(QDateTime::currentDateTime().addYears(1).date()); + m_isSecureComboBox->addItem(tr("no")); + m_isSecureComboBox->addItem(tr("yes")); + m_isHttpOnlyComboBox->addItem(tr("no")); + m_isHttpOnlyComboBox->addItem(tr("yes")); +} + +QNetworkCookie CookieDialog::cookie() +{ + QNetworkCookie cookie; + cookie.setDomain(m_domainLineEdit->text()); + cookie.setName(m_nameLineEdit->text().toLatin1()); + cookie.setValue(m_valueLineEdit->text().toLatin1()); + cookie.setExpirationDate(QDateTime(m_dateEdit->date())); + cookie.setPath(m_pathLineEdit->text()); + cookie.setSecure(m_isSecureComboBox->currentText() == tr("yes")); + cookie.setHttpOnly(m_isHttpOnlyComboBox->currentText() == tr("yes")); + return cookie; +} + +CookieWidget::CookieWidget(const QNetworkCookie &cookie, QWidget *parent): QWidget(parent) +{ + setupUi(this); + setAutoFillBackground(true); + m_nameLabel->setText(cookie.name()); + m_domainLabel->setText(cookie.domain()); + connect(m_viewButton, &QPushButton::clicked, this, &CookieWidget::viewClicked); + connect(m_deleteButton, &QPushButton::clicked, this, &CookieWidget::deleteClicked); +} + +void CookieWidget::setHighlighted(bool enabled) +{ + QPalette p = palette(); + p.setColor(backgroundRole(), enabled ? QColor(0xF0, 0xF8, 0xFF) : Qt::white); + setPalette(p); +} + +MainWindow::MainWindow(const QUrl &url) : + QMainWindow(), + m_store(nullptr), + m_layout(new QVBoxLayout) +{ + setupUi(this); + m_urlLineEdit->setText(url.toString()); + + m_layout->addItem(new QSpacerItem(0,0, QSizePolicy::Minimum, QSizePolicy::Expanding)); + m_layout->setContentsMargins(0, 0, 0, 0); + m_layout->setSpacing(0); + + QWidget *w = new QWidget(); + QPalette p = w->palette(); + p.setColor(widget->backgroundRole(), Qt::white); + w->setPalette(p); + w->setLayout(m_layout); + + m_scrollArea->setWidget(w); + m_scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + m_scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn); + + connect(m_urlButton, &QPushButton::clicked, this, &MainWindow::handleUrlClicked); + connect(m_deleteAllButton, &QPushButton::clicked, this, &MainWindow::handleDeleteAllClicked); + connect(m_newButton, &QPushButton::clicked, this, &MainWindow::handleNewClicked); + + m_store = m_webview->page()->profile()->cookieStore(); + connect(m_store, &QWebEngineCookieStore::cookieAdded, this, &MainWindow::handleCookieAdded); + m_store->loadAllCookies(); + m_webview->load(url); +} + +bool MainWindow::containsCookie(const QNetworkCookie &cookie) +{ + for (auto c: m_cookies) { + if (c.hasSameIdentifier(cookie)) + return true; + } + return false; +} + +void MainWindow::handleCookieAdded(const QNetworkCookie &cookie) +{ + // only new cookies + if (containsCookie(cookie)) + return; + + CookieWidget *widget = new CookieWidget(cookie); + widget->setHighlighted(m_cookies.count() % 2); + m_cookies.append(cookie); + m_layout->insertWidget(0,widget); + + connect(widget, &CookieWidget::deleteClicked, [this, cookie, widget]() { + m_store->deleteCookie(cookie); + delete widget; + m_cookies.removeOne(cookie); + for (int i = 0; i < m_layout->count() - 1; i++) { + // fix background colors + auto widget = qobject_cast<CookieWidget*>(m_layout->itemAt(i)->widget()); + widget->setHighlighted(i % 2); + } + }); + + connect(widget, &CookieWidget::viewClicked, [cookie]() { + CookieDialog dialog(cookie); + dialog.exec(); + }); +} + +void MainWindow::handleDeleteAllClicked() +{ + m_store->deleteAllCookies(); + for (int i = m_layout->count() - 1; i >= 0; i--) + delete m_layout->itemAt(i)->widget(); + m_cookies.clear(); +} + +void MainWindow::handleNewClicked() +{ + CookieDialog dialog; + if (dialog.exec() == QDialog::Accepted) + m_store->setCookie(dialog.cookie()); +} + +void MainWindow::handleUrlClicked() +{ + m_webview->load(QUrl(m_urlLineEdit->text())); +} diff --git a/examples/webenginewidgets/cookiebrowser/mainwindow.h b/examples/webenginewidgets/cookiebrowser/mainwindow.h new file mode 100644 index 000000000..93d0a5a31 --- /dev/null +++ b/examples/webenginewidgets/cookiebrowser/mainwindow.h @@ -0,0 +1,96 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the demonstration applications of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** 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 The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/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. +** +** As a special exception, The Qt Company gives you certain additional +** rights. These rights are described in The Qt Company 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 MAINWINDOW_H +#define MAINWINDOW_H + +#include "ui_mainwindow.h" +#include "ui_cookiewidget.h" +#include "ui_cookiedialog.h" +#include <QNetworkCookie> +#include <QMainWindow> + +QT_BEGIN_NAMESPACE +class QWebEngineCookieStore; +QT_END_NAMESPACE + +class CookieDialog : public QDialog, public Ui_CookieDialog +{ + Q_OBJECT +public: + CookieDialog(const QNetworkCookie &cookie, QWidget *parent = nullptr); + CookieDialog(QWidget *parent = 0); + QNetworkCookie cookie(); +}; + +class CookieWidget : public QWidget, public Ui_CookieWidget +{ + Q_OBJECT +public: + CookieWidget(const QNetworkCookie &cookie, QWidget *parent = nullptr); + void setHighlighted(bool enabled); +signals: + void deleteClicked(); + void viewClicked(); +}; + +class MainWindow : public QMainWindow, public Ui_MainWindow +{ + Q_OBJECT +public: + explicit MainWindow(const QUrl &url); + +private: + bool containsCookie(const QNetworkCookie &cookie); + +private slots: + void handleCookieAdded(const QNetworkCookie &cookie); + void handleDeleteAllClicked(); + void handleNewClicked(); + void handleUrlClicked(); + +private: + QWebEngineCookieStore *m_store; + QVector<QNetworkCookie> m_cookies; + QVBoxLayout *m_layout; +}; + +#endif // MAINWINDOW_H diff --git a/examples/webenginewidgets/cookiebrowser/mainwindow.ui b/examples/webenginewidgets/cookiebrowser/mainwindow.ui new file mode 100644 index 000000000..78d451396 --- /dev/null +++ b/examples/webenginewidgets/cookiebrowser/mainwindow.ui @@ -0,0 +1,200 @@ +<?xml version="1.0" encoding="UTF-8"?> +<ui version="4.0"> + <class>MainWindow</class> + <widget class="QMainWindow" name="MainWindow"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>1400</width> + <height>650</height> + </rect> + </property> + <property name="windowTitle"> + <string>Cookie Manager</string> + </property> + <widget class="QWidget" name="centralWidget"> + <layout class="QHBoxLayout" name="horizontalLayout_2"> + <item> + <widget class="QFrame" name="frame"> + <property name="frameShape"> + <enum>QFrame::StyledPanel</enum> + </property> + <property name="frameShadow"> + <enum>QFrame::Raised</enum> + </property> + <layout class="QVBoxLayout" name="verticalLayout"> + <item> + <widget class="QWidget" name="widget" native="true"> + <layout class="QHBoxLayout" name="horizontalLayout"> + <property name="leftMargin"> + <number>0</number> + </property> + <property name="topMargin"> + <number>0</number> + </property> + <property name="rightMargin"> + <number>0</number> + </property> + <property name="bottomMargin"> + <number>0</number> + </property> + <item> + <widget class="QLineEdit" name="m_urlLineEdit"/> + </item> + <item> + <widget class="QPushButton" name="m_urlButton"> + <property name="text"> + <string/> + </property> + <property name="icon"> + <iconset resource="cookiebrowser.qrc"> + <normaloff>:/view-refresh.png</normaloff>:/view-refresh.png</iconset> + </property> + </widget> + </item> + </layout> + </widget> + </item> + <item> + <widget class="QWebEngineView" name="m_webview" native="true"> + <property name="sizePolicy"> + <sizepolicy hsizetype="Preferred" vsizetype="Expanding"> + <horstretch>0</horstretch> + <verstretch>0</verstretch> + </sizepolicy> + </property> + <property name="minimumSize"> + <size> + <width>0</width> + <height>0</height> + </size> + </property> + </widget> + </item> + </layout> + </widget> + </item> + <item> + <widget class="QFrame" name="frame_2"> + <property name="maximumSize"> + <size> + <width>336</width> + <height>16777215</height> + </size> + </property> + <property name="frameShape"> + <enum>QFrame::StyledPanel</enum> + </property> + <property name="frameShadow"> + <enum>QFrame::Raised</enum> + </property> + <layout class="QVBoxLayout" name="verticalLayout_2"> + <item> + <widget class="QWidget" name="widget_2" native="true"> + <layout class="QHBoxLayout" name="horizontalLayout_3"> + <property name="leftMargin"> + <number>0</number> + </property> + <property name="topMargin"> + <number>0</number> + </property> + <property name="rightMargin"> + <number>0</number> + </property> + <property name="bottomMargin"> + <number>0</number> + </property> + <item> + <widget class="QLabel" name="label"> + <property name="text"> + <string>Cookies:</string> + </property> + </widget> + </item> + <item> + <spacer name="horizontalSpacer"> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + <property name="sizeHint" stdset="0"> + <size> + <width>87</width> + <height>20</height> + </size> + </property> + </spacer> + </item> + <item> + <widget class="QPushButton" name="m_newButton"> + <property name="text"> + <string>New</string> + </property> + </widget> + </item> + <item> + <widget class="QPushButton" name="m_deleteAllButton"> + <property name="text"> + <string>Delete All</string> + </property> + </widget> + </item> + </layout> + </widget> + </item> + <item> + <widget class="QScrollArea" name="m_scrollArea"> + <property name="minimumSize"> + <size> + <width>320</width> + <height>0</height> + </size> + </property> + <property name="maximumSize"> + <size> + <width>320</width> + <height>16777215</height> + </size> + </property> + <property name="widgetResizable"> + <bool>true</bool> + </property> + </widget> + </item> + </layout> + </widget> + </item> + </layout> + </widget> + </widget> + <layoutdefault spacing="6" margin="11"/> + <customwidgets> + <customwidget> + <class>QWebEngineView</class> + <extends>QWidget</extends> + <header>qwebengineview.h</header> + <container>1</container> + </customwidget> + </customwidgets> + <resources> + <include location="cookiebrowser.qrc"/> + </resources> + <connections> + <connection> + <sender>m_urlLineEdit</sender> + <signal>returnPressed()</signal> + <receiver>m_urlButton</receiver> + <slot>click()</slot> + <hints> + <hint type="sourcelabel"> + <x>509</x> + <y>28</y> + </hint> + <hint type="destinationlabel"> + <x>1024</x> + <y>27</y> + </hint> + </hints> + </connection> + </connections> +</ui> diff --git a/examples/webenginewidgets/cookiebrowser/view-refresh.png b/examples/webenginewidgets/cookiebrowser/view-refresh.png Binary files differnew file mode 100644 index 000000000..cab4d02c7 --- /dev/null +++ b/examples/webenginewidgets/cookiebrowser/view-refresh.png |