WebKit/qt/Api/qwebview.cpp
changeset 0 4f2f89ce4247
child 2 303757a437d3
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     1 /*
       
     2     Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
       
     3     Copyright (C) 2008 Holger Hans Peter Freyther
       
     4     Copyright (C) 2009 Girish Ramakrishnan <girish@forwardbias.in>
       
     5 
       
     6     This library is free software; you can redistribute it and/or
       
     7     modify it under the terms of the GNU Library General Public
       
     8     License as published by the Free Software Foundation; either
       
     9     version 2 of the License, or (at your option) any later version.
       
    10 
       
    11     This library is distributed in the hope that it will be useful,
       
    12     but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    14     Library General Public License for more details.
       
    15 
       
    16     You should have received a copy of the GNU Library General Public License
       
    17     along with this library; see the file COPYING.LIB.  If not, write to
       
    18     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
       
    19     Boston, MA 02110-1301, USA.
       
    20 */
       
    21 
       
    22 #include "config.h"
       
    23 #include "qwebview.h"
       
    24 
       
    25 #include "Page.h"
       
    26 #include "QWebPageClient.h"
       
    27 #include "Settings.h"
       
    28 #include "qwebframe.h"
       
    29 #include "qwebpage_p.h"
       
    30 #include "qbitmap.h"
       
    31 #include "qevent.h"
       
    32 #include "qpainter.h"
       
    33 #include "qprinter.h"
       
    34 #include "qdir.h"
       
    35 #include "qfile.h"
       
    36 
       
    37 class QWebViewPrivate {
       
    38 public:
       
    39     QWebViewPrivate(QWebView *view)
       
    40         : view(view)
       
    41         , page(0)
       
    42         , renderHints(QPainter::TextAntialiasing)
       
    43     {
       
    44         Q_ASSERT(view);
       
    45     }
       
    46 
       
    47     virtual ~QWebViewPrivate();
       
    48 
       
    49     void _q_pageDestroyed();
       
    50     void detachCurrentPage();
       
    51 
       
    52     QWebView *view;
       
    53     QWebPage *page;
       
    54 
       
    55     QPainter::RenderHints renderHints;
       
    56 };
       
    57 
       
    58 QWebViewPrivate::~QWebViewPrivate()
       
    59 {
       
    60     detachCurrentPage();
       
    61 }
       
    62 
       
    63 void QWebViewPrivate::_q_pageDestroyed()
       
    64 {
       
    65     page = 0;
       
    66     view->setPage(0);
       
    67 }
       
    68 
       
    69 #ifdef Q_WS_MAEMO_5
       
    70 #include "qabstractkineticscroller.h"
       
    71 #include "qapplication.h"
       
    72 
       
    73 // QCoreApplication::sendSpontaneousEvent() is private, hence this friend wrapper
       
    74 bool qt_sendSpontaneousEvent(QObject* receiver, QEvent* ev)
       
    75 {
       
    76     return QCoreApplication::sendSpontaneousEvent(receiver, ev);
       
    77 }
       
    78 
       
    79 class QWebViewKineticScroller : public QObject, public QAbstractKineticScroller {
       
    80 public:
       
    81     QWebViewKineticScroller()
       
    82         : QObject()
       
    83         , QAbstractKineticScroller()
       
    84         , m_view(0)
       
    85         , m_ignoreEvents(false)
       
    86     {
       
    87     }
       
    88 
       
    89     void setWidget(QWebView* widget)
       
    90     {
       
    91         if (m_view) {
       
    92             m_view->removeEventFilter(this);
       
    93             QWebFrame* frame = m_view->page()->mainFrame();
       
    94             frame->setScrollBarPolicy(Qt::Vertical, m_oldVerticalScrollBarPolicy);
       
    95             frame->setScrollBarPolicy(Qt::Horizontal, m_oldHorizontalScrollBarPolicy);
       
    96         }
       
    97 
       
    98         m_view = widget;
       
    99         setParent(m_view);
       
   100         if (m_view) {
       
   101             QWebFrame* frame = m_view->page()->mainFrame();
       
   102             m_oldHorizontalScrollBarPolicy = frame->scrollBarPolicy(Qt::Horizontal);
       
   103             m_oldVerticalScrollBarPolicy = frame->scrollBarPolicy(Qt::Vertical);
       
   104             frame->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff);
       
   105             frame->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAlwaysOff);
       
   106             m_view->installEventFilter(this);
       
   107         }
       
   108     }
       
   109 
       
   110 protected:
       
   111     bool eventFilter(QObject* o, QEvent* ev)
       
   112     {
       
   113         if (!o || m_view != o || m_ignoreEvents || !m_view->isEnabled())
       
   114             return QObject::eventFilter(o, ev);
       
   115 
       
   116         bool res = false;
       
   117 
       
   118         switch (ev->type()) {
       
   119         case QEvent::MouseButtonPress: {
       
   120             // remember the frame where the button was pressed
       
   121             QWebFrame* hitFrame = scrollingFrameAt(static_cast<QMouseEvent*>(ev)->pos());
       
   122             if (hitFrame)
       
   123                 m_frame = hitFrame;
       
   124             // fall through
       
   125         }
       
   126         case QEvent::MouseMove:
       
   127         case QEvent::MouseButtonRelease:
       
   128         case QEvent::MouseButtonDblClick:
       
   129             res = handleMouseEvent(static_cast<QMouseEvent*>(ev));
       
   130             break;
       
   131         default:
       
   132             break;
       
   133         }
       
   134         return res ? true : QObject::eventFilter(o, ev);
       
   135     }
       
   136 
       
   137     void cancelLeftMouseButtonPress(const QPoint& /* globalPressPos */)
       
   138     {
       
   139         QMouseEvent cmem(QEvent::MouseMove, QPoint(-INT_MAX, -INT_MAX), Qt::LeftButton, QApplication::mouseButtons() | Qt::LeftButton, QApplication::keyboardModifiers());
       
   140         sendEvent(m_view, &cmem);
       
   141         QMouseEvent cmer(QEvent::MouseButtonRelease, QPoint(-INT_MAX, -INT_MAX), Qt::LeftButton, QApplication::mouseButtons() & ~Qt::LeftButton, QApplication::keyboardModifiers());
       
   142         sendEvent(m_view, &cmer);
       
   143     }
       
   144 
       
   145     QWebFrame* currentFrame() const
       
   146     {
       
   147         if (m_frame)
       
   148             return m_frame;
       
   149 
       
   150         if (m_view)
       
   151             return m_view->page()->mainFrame();
       
   152 
       
   153         return 0;
       
   154     }
       
   155 
       
   156     // Returns the innermost frame at the given position that can scroll.
       
   157     QWebFrame* scrollingFrameAt(const QPoint& pos) const
       
   158     {
       
   159         QWebFrame* hitFrame = 0;
       
   160         if (m_view) {
       
   161             QWebFrame* frame = m_view->page()->mainFrame();
       
   162             hitFrame = frame->hitTestContent(pos).frame();
       
   163             QSize range = hitFrame->contentsSize() - hitFrame->geometry().size();
       
   164 
       
   165             while (hitFrame && range.width() <= 1 && range.height() <= 1)
       
   166                 hitFrame = hitFrame->parentFrame();
       
   167 
       
   168             return hitFrame;
       
   169         }
       
   170     }
       
   171 
       
   172     QPoint maximumScrollPosition() const
       
   173     {
       
   174         QWebFrame* frame = currentFrame();
       
   175         QSize s = frame ? frame->contentsSize() - frame->geometry().size() : QSize(0, 0);
       
   176         return QPoint(qMax(0, s.width()), qMax(0, s.height()));
       
   177     }
       
   178 
       
   179     QPoint scrollPosition() const
       
   180     {
       
   181         QWebFrame* frame = currentFrame();
       
   182         return frame ? frame->scrollPosition() : QPoint();
       
   183     }
       
   184 
       
   185     QSize viewportSize() const
       
   186     {
       
   187         return m_view ? m_view->page()->viewportSize() : QSize();
       
   188     }
       
   189 
       
   190     void setScrollPosition(const QPoint& point, const QPoint& /* overShootDelta */)
       
   191     {
       
   192         QWebFrame* frame = currentFrame();
       
   193         if (frame)
       
   194             frame->setScrollPosition(point);
       
   195     }
       
   196 
       
   197     void sendEvent(QWidget* w, QEvent* ev)
       
   198     {
       
   199         m_ignoreEvents = true;
       
   200         qt_sendSpontaneousEvent(w, ev);
       
   201         m_ignoreEvents = false;
       
   202     }
       
   203 
       
   204     QWebView* m_view;
       
   205     bool m_ignoreEvents;
       
   206     QPointer<QWebFrame> m_frame;
       
   207     Qt::ScrollBarPolicy m_oldVerticalScrollBarPolicy;
       
   208     Qt::ScrollBarPolicy m_oldHorizontalScrollBarPolicy;
       
   209 };
       
   210 
       
   211 #endif // Q_WS_MAEMO_5
       
   212 
       
   213 
       
   214 /*!
       
   215     \class QWebView
       
   216     \since 4.4
       
   217     \brief The QWebView class provides a widget that is used to view and edit
       
   218     web documents.
       
   219     \ingroup advanced
       
   220 
       
   221     \inmodule QtWebKit
       
   222 
       
   223     QWebView is the main widget component of the QtWebKit web browsing module.
       
   224     It can be used in various applications to display web content live from the
       
   225     Internet.
       
   226 
       
   227     The image below shows QWebView previewed in \QD with a Nokia website.
       
   228 
       
   229     \image qwebview-url.png
       
   230 
       
   231     A web site can be loaded onto QWebView with the load() function. Like all
       
   232     Qt widgets, the show() function must be invoked in order to display
       
   233     QWebView. The snippet below illustrates this:
       
   234 
       
   235     \snippet webkitsnippets/simple/main.cpp Using QWebView
       
   236 
       
   237     Alternatively, setUrl() can also be used to load a web site. If you have
       
   238     the HTML content readily available, you can use setHtml() instead.
       
   239 
       
   240     The loadStarted() signal is emitted when the view begins loading. The
       
   241     loadProgress() signal, on the other hand, is emitted whenever an element of
       
   242     the web view completes loading, such as an embedded image, a script, etc.
       
   243     Finally, the loadFinished() signal is emitted when the view has loaded
       
   244     completely. It's argument - either \c true or \c false - indicates
       
   245     load success or failure.
       
   246 
       
   247     The page() function returns a pointer to the web page object. See
       
   248     \l{Elements of QWebView} for an explanation of how the web page
       
   249     is related to the view. To modify your web view's settings, you can access
       
   250     the QWebSettings object with the settings() function. With QWebSettings,
       
   251     you can change the default fonts, enable or disable features such as
       
   252     JavaScript and plugins.
       
   253 
       
   254     The title of an HTML document can be accessed with the title() property.
       
   255     Additionally, a web site may also specify an icon, which can be accessed
       
   256     using the icon() property. If the title or the icon changes, the corresponding
       
   257     titleChanged() and iconChanged() signals will be emitted. The
       
   258     textSizeMultiplier() property can be used to change the overall size of
       
   259     the text displayed in the web view.
       
   260 
       
   261     If you require a custom context menu, you can implement it by reimplementing
       
   262     \l{QWidget::}{contextMenuEvent()} and populating your QMenu with the actions
       
   263     obtained from pageAction(). More functionality such as reloading the view,
       
   264     copying selected text to the clipboard, or pasting into the view, is also
       
   265     encapsulated within the QAction objects returned by pageAction(). These
       
   266     actions can be programmatically triggered using triggerPageAction().
       
   267     Alternatively, the actions can be added to a toolbar or a menu directly.
       
   268     QWebView maintains the state of the returned actions but allows
       
   269     modification of action properties such as \l{QAction::}{text} or
       
   270     \l{QAction::}{icon}.
       
   271 
       
   272     A QWebView can be printed onto a QPrinter using the print() function.
       
   273     This function is marked as a slot and can be conveniently connected to
       
   274     \l{QPrintPreviewDialog}'s \l{QPrintPreviewDialog::}{paintRequested()}
       
   275     signal.
       
   276 
       
   277     If you want to provide support for web sites that allow the user to open
       
   278     new windows, such as pop-up windows, you can subclass QWebView and
       
   279     reimplement the createWindow() function.
       
   280 
       
   281     \section1 Elements of QWebView
       
   282 
       
   283     QWebView consists of other objects such as QWebFrame and QWebPage. The
       
   284     flowchart below shows these elements are related.
       
   285 
       
   286     \image qwebview-diagram.png
       
   287 
       
   288     \note It is possible to use QWebPage and QWebFrame, without using QWebView,
       
   289     if you do not require QWidget attributes. Nevertheless, QtWebKit depends
       
   290     on QtGui, so you should use a QApplication instead of QCoreApplication.
       
   291 
       
   292     \sa {Previewer Example}, {Web Browser}, {Form Extractor Example},
       
   293     {Google Chat Example}, {Fancy Browser Example}
       
   294 */
       
   295 
       
   296 /*!
       
   297     Constructs an empty QWebView with parent \a parent.
       
   298 
       
   299     \sa load()
       
   300 */
       
   301 QWebView::QWebView(QWidget *parent)
       
   302     : QWidget(parent)
       
   303 {
       
   304     d = new QWebViewPrivate(this);
       
   305 
       
   306 #if !defined(Q_WS_QWS) && !defined(Q_OS_SYMBIAN)
       
   307     setAttribute(Qt::WA_InputMethodEnabled);
       
   308 #endif
       
   309 
       
   310 #if QT_VERSION >= QT_VERSION_CHECK(4, 6, 0)
       
   311     setAttribute(Qt::WA_AcceptTouchEvents);
       
   312 #endif
       
   313 #if defined(Q_WS_MAEMO_5)
       
   314     QAbstractKineticScroller* scroller = new QWebViewKineticScroller();
       
   315     static_cast<QWebViewKineticScroller*>(scroller)->setWidget(this);
       
   316     setProperty("kineticScroller", QVariant::fromValue(scroller));
       
   317 #endif
       
   318     setAcceptDrops(true);
       
   319 
       
   320     setMouseTracking(true);
       
   321     setFocusPolicy(Qt::WheelFocus);
       
   322 }
       
   323 
       
   324 /*!
       
   325     Destroys the web view.
       
   326 */
       
   327 QWebView::~QWebView()
       
   328 {
       
   329     delete d;
       
   330 }
       
   331 
       
   332 /*!
       
   333     Returns a pointer to the underlying web page.
       
   334 
       
   335     \sa setPage()
       
   336 */
       
   337 QWebPage *QWebView::page() const
       
   338 {
       
   339     if (!d->page) {
       
   340         QWebView *that = const_cast<QWebView *>(this);
       
   341         that->setPage(new QWebPage(that));
       
   342     }
       
   343     return d->page;
       
   344 }
       
   345 
       
   346 void QWebViewPrivate::detachCurrentPage()
       
   347 {
       
   348     if (!page)
       
   349         return;
       
   350 
       
   351     if (page) {
       
   352 #if QT_VERSION >= 0x040600
       
   353         page->d->view.clear();
       
   354 #else
       
   355         page->d->view = 0;
       
   356 #endif
       
   357     }
       
   358 
       
   359     // if the page client is the special client constructed for
       
   360     // delegating the responsibilities to a QWidget, we need
       
   361     // to destroy it.
       
   362 
       
   363     if (page->d->client && page->d->client->isQWidgetClient())
       
   364         delete page->d->client;
       
   365 
       
   366     page->d->client = 0;
       
   367 
       
   368     // if the page was created by us, we own it and need to
       
   369     // destroy it as well.
       
   370 
       
   371     if (page->parent() == view)
       
   372         delete page;
       
   373     else
       
   374         page->disconnect(view);
       
   375 
       
   376     page = 0;
       
   377 }
       
   378 
       
   379 /*!
       
   380     Makes \a page the new web page of the web view.
       
   381 
       
   382     The parent QObject of the provided page remains the owner
       
   383     of the object. If the current document is a child of the web
       
   384     view, it will be deleted.
       
   385 
       
   386     \sa page()
       
   387 */
       
   388 void QWebView::setPage(QWebPage* page)
       
   389 {
       
   390     if (d->page == page)
       
   391         return;
       
   392 
       
   393     d->detachCurrentPage();
       
   394     d->page = page;
       
   395 
       
   396     if (d->page) {
       
   397         d->page->setView(this);
       
   398         d->page->setPalette(palette());
       
   399         // #### connect signals
       
   400         QWebFrame *mainFrame = d->page->mainFrame();
       
   401         connect(mainFrame, SIGNAL(titleChanged(QString)),
       
   402                 this, SIGNAL(titleChanged(QString)));
       
   403         connect(mainFrame, SIGNAL(iconChanged()),
       
   404                 this, SIGNAL(iconChanged()));
       
   405         connect(mainFrame, SIGNAL(urlChanged(QUrl)),
       
   406                 this, SIGNAL(urlChanged(QUrl)));
       
   407 
       
   408         connect(d->page, SIGNAL(loadStarted()),
       
   409                 this, SIGNAL(loadStarted()));
       
   410         connect(d->page, SIGNAL(loadProgress(int)),
       
   411                 this, SIGNAL(loadProgress(int)));
       
   412         connect(d->page, SIGNAL(loadFinished(bool)),
       
   413                 this, SIGNAL(loadFinished(bool)));
       
   414         connect(d->page, SIGNAL(statusBarMessage(QString)),
       
   415                 this, SIGNAL(statusBarMessage(QString)));
       
   416         connect(d->page, SIGNAL(linkClicked(QUrl)),
       
   417                 this, SIGNAL(linkClicked(QUrl)));
       
   418 
       
   419         connect(d->page, SIGNAL(microFocusChanged()),
       
   420                 this, SLOT(updateMicroFocus()));
       
   421         connect(d->page, SIGNAL(destroyed()),
       
   422                 this, SLOT(_q_pageDestroyed()));
       
   423     }
       
   424     setAttribute(Qt::WA_OpaquePaintEvent, d->page);
       
   425     update();
       
   426 }
       
   427 
       
   428 /*!
       
   429     Loads the specified \a url and displays it.
       
   430 
       
   431     \note The view remains the same until enough data has arrived to display the new \a url.
       
   432 
       
   433     \sa setUrl(), url(), urlChanged(), QUrl::fromUserInput()
       
   434 */
       
   435 void QWebView::load(const QUrl &url)
       
   436 {
       
   437     page()->mainFrame()->load(url);
       
   438 }
       
   439 
       
   440 /*!
       
   441     \fn void QWebView::load(const QNetworkRequest &request, QNetworkAccessManager::Operation operation, const QByteArray &body)
       
   442 
       
   443     Loads a network request, \a request, using the method specified in \a operation.
       
   444 
       
   445     \a body is optional and is only used for POST operations.
       
   446 
       
   447     \note The view remains the same until enough data has arrived to display the new url.
       
   448 
       
   449     \sa url(), urlChanged()
       
   450 */
       
   451 
       
   452 void QWebView::load(const QNetworkRequest &request,
       
   453                     QNetworkAccessManager::Operation operation,
       
   454                     const QByteArray &body)
       
   455 {
       
   456     page()->mainFrame()->load(request, operation, body);
       
   457 }
       
   458 
       
   459 /*!
       
   460     Sets the content of the web view to the specified \a html.
       
   461 
       
   462     External objects such as stylesheets or images referenced in the HTML
       
   463     document are located relative to \a baseUrl.
       
   464 
       
   465     The \a html is loaded immediately; external objects are loaded asynchronously.
       
   466 
       
   467     When using this method, WebKit assumes that external resources such as
       
   468     JavaScript programs or style sheets are encoded in UTF-8 unless otherwise
       
   469     specified. For example, the encoding of an external script can be specified
       
   470     through the charset attribute of the HTML script tag. Alternatively, the
       
   471     encoding can also be specified by the web server.
       
   472 
       
   473     This is a convenience function equivalent to setContent(html, "text/html", baseUrl).
       
   474 
       
   475     \warning This function works only for HTML, for other mime types (i.e. XHTML, SVG)
       
   476     setContent() should be used instead.
       
   477 
       
   478     \sa load(), setContent(), QWebFrame::toHtml(), QWebFrame::setContent()
       
   479 */
       
   480 void QWebView::setHtml(const QString &html, const QUrl &baseUrl)
       
   481 {
       
   482     page()->mainFrame()->setHtml(html, baseUrl);
       
   483 }
       
   484 
       
   485 /*!
       
   486     Sets the content of the web view to the specified content \a data. If the \a mimeType argument
       
   487     is empty it is currently assumed that the content is HTML but in future versions we may introduce
       
   488     auto-detection.
       
   489 
       
   490     External objects referenced in the content are located relative to \a baseUrl.
       
   491 
       
   492     The \a data is loaded immediately; external objects are loaded asynchronously.
       
   493 
       
   494     \sa load(), setHtml(), QWebFrame::toHtml()
       
   495 */
       
   496 void QWebView::setContent(const QByteArray &data, const QString &mimeType, const QUrl &baseUrl)
       
   497 {
       
   498     page()->mainFrame()->setContent(data, mimeType, baseUrl);
       
   499 }
       
   500 
       
   501 /*!
       
   502     Returns a pointer to the view's history of navigated web pages.
       
   503 
       
   504     It is equivalent to
       
   505 
       
   506     \snippet webkitsnippets/qtwebkit_qwebview_snippet.cpp 0
       
   507 */
       
   508 QWebHistory *QWebView::history() const
       
   509 {
       
   510     return page()->history();
       
   511 }
       
   512 
       
   513 /*!
       
   514     Returns a pointer to the view/page specific settings object.
       
   515 
       
   516     It is equivalent to
       
   517 
       
   518     \snippet webkitsnippets/qtwebkit_qwebview_snippet.cpp 1
       
   519 
       
   520     \sa QWebSettings::globalSettings()
       
   521 */
       
   522 QWebSettings *QWebView::settings() const
       
   523 {
       
   524     return page()->settings();
       
   525 }
       
   526 
       
   527 /*!
       
   528     \property QWebView::title
       
   529     \brief the title of the web page currently viewed
       
   530 
       
   531     By default, this property contains an empty string.
       
   532 
       
   533     \sa titleChanged()
       
   534 */
       
   535 QString QWebView::title() const
       
   536 {
       
   537     if (d->page)
       
   538         return d->page->mainFrame()->title();
       
   539     return QString();
       
   540 }
       
   541 
       
   542 /*!
       
   543     \property QWebView::url
       
   544     \brief the url of the web page currently viewed
       
   545 
       
   546     Setting this property clears the view and loads the URL.
       
   547 
       
   548     By default, this property contains an empty, invalid URL.
       
   549 
       
   550     \sa load(), urlChanged()
       
   551 */
       
   552 
       
   553 void QWebView::setUrl(const QUrl &url)
       
   554 {
       
   555     page()->mainFrame()->setUrl(url);
       
   556 }
       
   557 
       
   558 QUrl QWebView::url() const
       
   559 {
       
   560     if (d->page)
       
   561         return d->page->mainFrame()->url();
       
   562     return QUrl();
       
   563 }
       
   564 
       
   565 /*!
       
   566     \property QWebView::icon
       
   567     \brief the icon associated with the web page currently viewed
       
   568 
       
   569     By default, this property contains a null icon.
       
   570 
       
   571     \sa iconChanged(), QWebSettings::iconForUrl()
       
   572 */
       
   573 QIcon QWebView::icon() const
       
   574 {
       
   575     if (d->page)
       
   576         return d->page->mainFrame()->icon();
       
   577     return QIcon();
       
   578 }
       
   579 
       
   580 /*!
       
   581     \property QWebView::selectedText
       
   582     \brief the text currently selected
       
   583 
       
   584     By default, this property contains an empty string.
       
   585 
       
   586     \sa findText(), selectionChanged()
       
   587 */
       
   588 QString QWebView::selectedText() const
       
   589 {
       
   590     if (d->page)
       
   591         return d->page->selectedText();
       
   592     return QString();
       
   593 }
       
   594 
       
   595 #ifndef QT_NO_ACTION
       
   596 /*!
       
   597     Returns a pointer to a QAction that encapsulates the specified web action \a action.
       
   598 */
       
   599 QAction *QWebView::pageAction(QWebPage::WebAction action) const
       
   600 {
       
   601     return page()->action(action);
       
   602 }
       
   603 #endif
       
   604 
       
   605 /*!
       
   606     Triggers the specified \a action. If it is a checkable action the specified
       
   607     \a checked state is assumed.
       
   608 
       
   609     The following example triggers the copy action and therefore copies any
       
   610     selected text to the clipboard.
       
   611 
       
   612     \snippet webkitsnippets/qtwebkit_qwebview_snippet.cpp 2
       
   613 
       
   614     \sa pageAction()
       
   615 */
       
   616 void QWebView::triggerPageAction(QWebPage::WebAction action, bool checked)
       
   617 {
       
   618     page()->triggerAction(action, checked);
       
   619 }
       
   620 
       
   621 /*!
       
   622     \property QWebView::modified
       
   623     \brief whether the document was modified by the user
       
   624 
       
   625     Parts of HTML documents can be editable for example through the
       
   626     \c{contenteditable} attribute on HTML elements.
       
   627 
       
   628     By default, this property is false.
       
   629 */
       
   630 bool QWebView::isModified() const
       
   631 {
       
   632     if (d->page)
       
   633         return d->page->isModified();
       
   634     return false;
       
   635 }
       
   636 
       
   637 /*
       
   638 Qt::TextInteractionFlags QWebView::textInteractionFlags() const
       
   639 {
       
   640     // ### FIXME (add to page)
       
   641     return Qt::TextInteractionFlags();
       
   642 }
       
   643 */
       
   644 
       
   645 /*
       
   646     \property QWebView::textInteractionFlags
       
   647     \brief how the view should handle user input
       
   648 
       
   649     Specifies how the user can interact with the text on the page.
       
   650 */
       
   651 
       
   652 /*
       
   653 void QWebView::setTextInteractionFlags(Qt::TextInteractionFlags flags)
       
   654 {
       
   655     Q_UNUSED(flags)
       
   656     // ### FIXME (add to page)
       
   657 }
       
   658 */
       
   659 
       
   660 /*!
       
   661     \reimp
       
   662 */
       
   663 QSize QWebView::sizeHint() const
       
   664 {
       
   665     return QSize(800, 600); // ####...
       
   666 }
       
   667 
       
   668 /*!
       
   669     \property QWebView::zoomFactor
       
   670     \since 4.5
       
   671     \brief the zoom factor for the view
       
   672 */
       
   673 
       
   674 void QWebView::setZoomFactor(qreal factor)
       
   675 {
       
   676     page()->mainFrame()->setZoomFactor(factor);
       
   677 }
       
   678 
       
   679 qreal QWebView::zoomFactor() const
       
   680 {
       
   681     return page()->mainFrame()->zoomFactor();
       
   682 }
       
   683 
       
   684 /*!
       
   685   \property QWebView::textSizeMultiplier
       
   686   \brief the scaling factor for all text in the frame
       
   687   \obsolete
       
   688 
       
   689   Use setZoomFactor instead, in combination with the
       
   690   ZoomTextOnly attribute in QWebSettings.
       
   691 
       
   692   \note Setting this property also enables the
       
   693   ZoomTextOnly attribute in QWebSettings.
       
   694 
       
   695   By default, this property contains a value of 1.0.
       
   696 */
       
   697 
       
   698 /*!
       
   699     Sets the value of the multiplier used to scale the text in a Web page to
       
   700     the \a factor specified.
       
   701 */
       
   702 void QWebView::setTextSizeMultiplier(qreal factor)
       
   703 {
       
   704     page()->mainFrame()->setTextSizeMultiplier(factor);
       
   705 }
       
   706 
       
   707 /*!
       
   708     Returns the value of the multiplier used to scale the text in a Web page.
       
   709 */
       
   710 qreal QWebView::textSizeMultiplier() const
       
   711 {
       
   712     return page()->mainFrame()->textSizeMultiplier();
       
   713 }
       
   714 
       
   715 /*!
       
   716     \property QWebView::renderHints
       
   717     \since 4.6
       
   718     \brief the default render hints for the view
       
   719 
       
   720     These hints are used to initialize QPainter before painting the Web page.
       
   721 
       
   722     QPainter::TextAntialiasing is enabled by default.
       
   723 
       
   724     \note This property is not available on Symbian. However, the getter and
       
   725     setter functions can still be used directly.
       
   726 
       
   727     \sa QPainter::renderHints()
       
   728 */
       
   729 
       
   730 /*!
       
   731     \since 4.6
       
   732     Returns the render hints used by the view to render content.
       
   733 
       
   734     \sa QPainter::renderHints()
       
   735 */
       
   736 QPainter::RenderHints QWebView::renderHints() const
       
   737 {
       
   738     return d->renderHints;
       
   739 }
       
   740 
       
   741 /*!
       
   742     \since 4.6
       
   743     Sets the render hints used by the view to the specified \a hints.
       
   744 
       
   745     \sa QPainter::setRenderHints()
       
   746 */
       
   747 void QWebView::setRenderHints(QPainter::RenderHints hints)
       
   748 {
       
   749     if (hints == d->renderHints)
       
   750         return;
       
   751     d->renderHints = hints;
       
   752     update();
       
   753 }
       
   754 
       
   755 /*!
       
   756     \since 4.6
       
   757     If \a enabled is true, enables the specified render \a hint; otherwise
       
   758     disables it.
       
   759 
       
   760     \sa renderHints, QPainter::renderHints()
       
   761 */
       
   762 void QWebView::setRenderHint(QPainter::RenderHint hint, bool enabled)
       
   763 {
       
   764     QPainter::RenderHints oldHints = d->renderHints;
       
   765     if (enabled)
       
   766         d->renderHints |= hint;
       
   767     else
       
   768         d->renderHints &= ~hint;
       
   769     if (oldHints != d->renderHints)
       
   770         update();
       
   771 }
       
   772 
       
   773 
       
   774 /*!
       
   775     Finds the specified string, \a subString, in the page, using the given \a options.
       
   776 
       
   777     If the HighlightAllOccurrences flag is passed, the function will highlight all occurrences
       
   778     that exist in the page. All subsequent calls will extend the highlight, rather than
       
   779     replace it, with occurrences of the new string.
       
   780 
       
   781     If the HighlightAllOccurrences flag is not passed, the function will select an occurrence
       
   782     and all subsequent calls will replace the current occurrence with the next one.
       
   783 
       
   784     To clear the selection, just pass an empty string.
       
   785 
       
   786     Returns true if \a subString was found; otherwise returns false.
       
   787 
       
   788     \sa selectedText(), selectionChanged()
       
   789 */
       
   790 bool QWebView::findText(const QString &subString, QWebPage::FindFlags options)
       
   791 {
       
   792     if (d->page)
       
   793         return d->page->findText(subString, options);
       
   794     return false;
       
   795 }
       
   796 
       
   797 /*! \reimp
       
   798 */
       
   799 bool QWebView::event(QEvent *e)
       
   800 {
       
   801     if (d->page) {
       
   802 #ifndef QT_NO_CONTEXTMENU
       
   803         if (e->type() == QEvent::ContextMenu) {
       
   804             if (!isEnabled())
       
   805                 return false;
       
   806             QContextMenuEvent *event = static_cast<QContextMenuEvent *>(e);
       
   807             if (d->page->swallowContextMenuEvent(event)) {
       
   808                 e->accept();
       
   809                 return true;
       
   810             }
       
   811             d->page->updatePositionDependentActions(event->pos());
       
   812         } else
       
   813 #endif // QT_NO_CONTEXTMENU
       
   814         if (e->type() == QEvent::ShortcutOverride) {
       
   815             d->page->event(e);
       
   816 #ifndef QT_NO_CURSOR
       
   817         } else if (e->type() == QEvent::CursorChange) {
       
   818             // An unsetCursor will set the cursor to Qt::ArrowCursor.
       
   819             // Thus this cursor change might be a QWidget::unsetCursor()
       
   820             // If this is not the case and it came from WebCore, the
       
   821             // QWebPageClient already has set its cursor internally
       
   822             // to Qt::ArrowCursor, so updating the cursor is always
       
   823             // right, as it falls back to the last cursor set by
       
   824             // WebCore.
       
   825             // FIXME: Add a QEvent::CursorUnset or similar to Qt.
       
   826             if (cursor().shape() == Qt::ArrowCursor)
       
   827                 d->page->d->client->resetCursor();
       
   828 #endif
       
   829 #if QT_VERSION >= QT_VERSION_CHECK(4, 6, 0)
       
   830         } else if (e->type() == QEvent::TouchBegin 
       
   831                    || e->type() == QEvent::TouchEnd 
       
   832                    || e->type() == QEvent::TouchUpdate) {
       
   833             d->page->event(e);
       
   834 
       
   835             // Always return true so that we'll receive also TouchUpdate and TouchEnd events
       
   836             return true;
       
   837 #endif
       
   838         } else if (e->type() == QEvent::Leave)
       
   839             d->page->event(e);
       
   840     }
       
   841 
       
   842     return QWidget::event(e);
       
   843 }
       
   844 
       
   845 /*!
       
   846     Prints the main frame to the given \a printer.
       
   847 
       
   848     \sa QWebFrame::print(), QPrintPreviewDialog
       
   849 */
       
   850 void QWebView::print(QPrinter *printer) const
       
   851 {
       
   852 #ifndef QT_NO_PRINTER
       
   853     page()->mainFrame()->print(printer);
       
   854 #endif
       
   855 }
       
   856 
       
   857 /*!
       
   858     Convenience slot that stops loading the document.
       
   859 
       
   860     It is equivalent to
       
   861 
       
   862     \snippet webkitsnippets/qtwebkit_qwebview_snippet.cpp 3
       
   863 
       
   864     \sa reload(), pageAction(), loadFinished()
       
   865 */
       
   866 void QWebView::stop()
       
   867 {
       
   868     if (d->page)
       
   869         d->page->triggerAction(QWebPage::Stop);
       
   870 }
       
   871 
       
   872 /*!
       
   873     Convenience slot that loads the previous document in the list of documents
       
   874     built by navigating links. Does nothing if there is no previous document.
       
   875 
       
   876     It is equivalent to
       
   877 
       
   878     \snippet webkitsnippets/qtwebkit_qwebview_snippet.cpp 4
       
   879 
       
   880     \sa forward(), pageAction()
       
   881 */
       
   882 void QWebView::back()
       
   883 {
       
   884     if (d->page)
       
   885         d->page->triggerAction(QWebPage::Back);
       
   886 }
       
   887 
       
   888 /*!
       
   889     Convenience slot that loads the next document in the list of documents
       
   890     built by navigating links. Does nothing if there is no next document.
       
   891 
       
   892     It is equivalent to
       
   893 
       
   894     \snippet webkitsnippets/qtwebkit_qwebview_snippet.cpp 5
       
   895 
       
   896     \sa back(), pageAction()
       
   897 */
       
   898 void QWebView::forward()
       
   899 {
       
   900     if (d->page)
       
   901         d->page->triggerAction(QWebPage::Forward);
       
   902 }
       
   903 
       
   904 /*!
       
   905     Reloads the current document.
       
   906 
       
   907     \sa stop(), pageAction(), loadStarted()
       
   908 */
       
   909 void QWebView::reload()
       
   910 {
       
   911     if (d->page)
       
   912         d->page->triggerAction(QWebPage::Reload);
       
   913 }
       
   914 
       
   915 /*! \reimp
       
   916 */
       
   917 void QWebView::resizeEvent(QResizeEvent *e)
       
   918 {
       
   919     if (d->page)
       
   920         d->page->setViewportSize(e->size());
       
   921 }
       
   922 
       
   923 /*! \reimp
       
   924 */
       
   925 void QWebView::paintEvent(QPaintEvent *ev)
       
   926 {
       
   927     if (!d->page)
       
   928         return;
       
   929 #ifdef QWEBKIT_TIME_RENDERING
       
   930     QTime time;
       
   931     time.start();
       
   932 #endif
       
   933 
       
   934     QWebFrame *frame = d->page->mainFrame();
       
   935     QPainter p(this);
       
   936     p.setRenderHints(d->renderHints);
       
   937 
       
   938     frame->render(&p, ev->region());
       
   939 
       
   940 #ifdef    QWEBKIT_TIME_RENDERING
       
   941     int elapsed = time.elapsed();
       
   942     qDebug() << "paint event on " << ev->region() << ", took to render =  " << elapsed;
       
   943 #endif
       
   944 }
       
   945 
       
   946 /*!
       
   947     This function is called from the createWindow() method of the associated QWebPage,
       
   948     each time the page wants to create a new window of the given \a type. This might
       
   949     be the result, for example, of a JavaScript request to open a document in a new window.
       
   950 
       
   951     \note If the createWindow() method of the associated page is reimplemented, this
       
   952     method is not called, unless explicitly done so in the reimplementation.
       
   953 
       
   954     \sa QWebPage::createWindow()
       
   955 */
       
   956 QWebView *QWebView::createWindow(QWebPage::WebWindowType type)
       
   957 {
       
   958     Q_UNUSED(type)
       
   959     return 0;
       
   960 }
       
   961 
       
   962 /*! \reimp
       
   963 */
       
   964 void QWebView::mouseMoveEvent(QMouseEvent* ev)
       
   965 {
       
   966     if (d->page) {
       
   967         const bool accepted = ev->isAccepted();
       
   968         d->page->event(ev);
       
   969         ev->setAccepted(accepted);
       
   970     }
       
   971 }
       
   972 
       
   973 /*! \reimp
       
   974 */
       
   975 void QWebView::mousePressEvent(QMouseEvent* ev)
       
   976 {
       
   977     if (d->page) {
       
   978         const bool accepted = ev->isAccepted();
       
   979         d->page->event(ev);
       
   980         ev->setAccepted(accepted);
       
   981     }
       
   982 }
       
   983 
       
   984 /*! \reimp
       
   985 */
       
   986 void QWebView::mouseDoubleClickEvent(QMouseEvent* ev)
       
   987 {
       
   988     if (d->page) {
       
   989         const bool accepted = ev->isAccepted();
       
   990         d->page->event(ev);
       
   991         ev->setAccepted(accepted);
       
   992     }
       
   993 }
       
   994 
       
   995 /*! \reimp
       
   996 */
       
   997 void QWebView::mouseReleaseEvent(QMouseEvent* ev)
       
   998 {
       
   999     if (d->page) {
       
  1000         const bool accepted = ev->isAccepted();
       
  1001         d->page->event(ev);
       
  1002         ev->setAccepted(accepted);
       
  1003     }
       
  1004 }
       
  1005 
       
  1006 #ifndef QT_NO_CONTEXTMENU
       
  1007 /*! \reimp
       
  1008 */
       
  1009 void QWebView::contextMenuEvent(QContextMenuEvent* ev)
       
  1010 {
       
  1011     if (d->page) {
       
  1012         const bool accepted = ev->isAccepted();
       
  1013         d->page->event(ev);
       
  1014         ev->setAccepted(accepted);
       
  1015     }
       
  1016 }
       
  1017 #endif // QT_NO_CONTEXTMENU
       
  1018 
       
  1019 #ifndef QT_NO_WHEELEVENT
       
  1020 /*! \reimp
       
  1021 */
       
  1022 void QWebView::wheelEvent(QWheelEvent* ev)
       
  1023 {
       
  1024     if (d->page) {
       
  1025         const bool accepted = ev->isAccepted();
       
  1026         d->page->event(ev);
       
  1027         ev->setAccepted(accepted);
       
  1028     }
       
  1029 }
       
  1030 #endif // QT_NO_WHEELEVENT
       
  1031 
       
  1032 /*! \reimp
       
  1033 */
       
  1034 void QWebView::keyPressEvent(QKeyEvent* ev)
       
  1035 {
       
  1036     if (d->page)
       
  1037         d->page->event(ev);
       
  1038     if (!ev->isAccepted())
       
  1039         QWidget::keyPressEvent(ev);
       
  1040 }
       
  1041 
       
  1042 /*! \reimp
       
  1043 */
       
  1044 void QWebView::keyReleaseEvent(QKeyEvent* ev)
       
  1045 {
       
  1046     if (d->page)
       
  1047         d->page->event(ev);
       
  1048     if (!ev->isAccepted())
       
  1049         QWidget::keyReleaseEvent(ev);
       
  1050 }
       
  1051 
       
  1052 /*! \reimp
       
  1053 */
       
  1054 void QWebView::focusInEvent(QFocusEvent* ev)
       
  1055 {
       
  1056     if (d->page)
       
  1057         d->page->event(ev);
       
  1058     else
       
  1059         QWidget::focusInEvent(ev);
       
  1060 }
       
  1061 
       
  1062 /*! \reimp
       
  1063 */
       
  1064 void QWebView::focusOutEvent(QFocusEvent* ev)
       
  1065 {
       
  1066     if (d->page)
       
  1067         d->page->event(ev);
       
  1068     else
       
  1069         QWidget::focusOutEvent(ev);
       
  1070 }
       
  1071 
       
  1072 /*! \reimp
       
  1073 */
       
  1074 void QWebView::dragEnterEvent(QDragEnterEvent* ev)
       
  1075 {
       
  1076 #ifndef QT_NO_DRAGANDDROP
       
  1077     if (d->page)
       
  1078         d->page->event(ev);
       
  1079 #endif
       
  1080 }
       
  1081 
       
  1082 /*! \reimp
       
  1083 */
       
  1084 void QWebView::dragLeaveEvent(QDragLeaveEvent* ev)
       
  1085 {
       
  1086 #ifndef QT_NO_DRAGANDDROP
       
  1087     if (d->page)
       
  1088         d->page->event(ev);
       
  1089 #endif
       
  1090 }
       
  1091 
       
  1092 /*! \reimp
       
  1093 */
       
  1094 void QWebView::dragMoveEvent(QDragMoveEvent* ev)
       
  1095 {
       
  1096 #ifndef QT_NO_DRAGANDDROP
       
  1097     if (d->page)
       
  1098         d->page->event(ev);
       
  1099 #endif
       
  1100 }
       
  1101 
       
  1102 /*! \reimp
       
  1103 */
       
  1104 void QWebView::dropEvent(QDropEvent* ev)
       
  1105 {
       
  1106 #ifndef QT_NO_DRAGANDDROP
       
  1107     if (d->page)
       
  1108         d->page->event(ev);
       
  1109 #endif
       
  1110 }
       
  1111 
       
  1112 /*! \reimp
       
  1113 */
       
  1114 bool QWebView::focusNextPrevChild(bool next)
       
  1115 {
       
  1116     if (d->page && d->page->focusNextPrevChild(next))
       
  1117         return true;
       
  1118     return QWidget::focusNextPrevChild(next);
       
  1119 }
       
  1120 
       
  1121 /*!\reimp
       
  1122 */
       
  1123 QVariant QWebView::inputMethodQuery(Qt::InputMethodQuery property) const
       
  1124 {
       
  1125     if (d->page)
       
  1126         return d->page->inputMethodQuery(property);
       
  1127     return QVariant();
       
  1128 }
       
  1129 
       
  1130 /*!\reimp
       
  1131 */
       
  1132 void QWebView::inputMethodEvent(QInputMethodEvent *e)
       
  1133 {
       
  1134     if (d->page)
       
  1135        d->page->event(e);
       
  1136 }
       
  1137 
       
  1138 /*!\reimp
       
  1139 */
       
  1140 void QWebView::changeEvent(QEvent *e)
       
  1141 {
       
  1142     if (d->page && e->type() == QEvent::PaletteChange)
       
  1143         d->page->setPalette(palette());
       
  1144     QWidget::changeEvent(e);
       
  1145 }
       
  1146 
       
  1147 /*!
       
  1148     \fn void QWebView::titleChanged(const QString &title)
       
  1149 
       
  1150     This signal is emitted whenever the \a title of the main frame changes.
       
  1151 
       
  1152     \sa title()
       
  1153 */
       
  1154 
       
  1155 /*!
       
  1156     \fn void QWebView::urlChanged(const QUrl &url)
       
  1157 
       
  1158     This signal is emitted when the \a url of the view changes.
       
  1159 
       
  1160     \sa url(), load()
       
  1161 */
       
  1162 
       
  1163 /*!
       
  1164     \fn void QWebView::statusBarMessage(const QString& text)
       
  1165 
       
  1166     This signal is emitted when the status bar \a text is changed by the page.
       
  1167 */
       
  1168 
       
  1169 /*!
       
  1170     \fn void QWebView::iconChanged()
       
  1171 
       
  1172     This signal is emitted whenever the icon of the page is loaded or changes.
       
  1173 
       
  1174     In order for icons to be loaded, you will need to set an icon database path
       
  1175     using QWebSettings::setIconDatabasePath().
       
  1176 
       
  1177     \sa icon(), QWebSettings::setIconDatabasePath()
       
  1178 */
       
  1179 
       
  1180 /*!
       
  1181     \fn void QWebView::loadStarted()
       
  1182 
       
  1183     This signal is emitted when a new load of the page is started.
       
  1184 
       
  1185     \sa loadProgress(), loadFinished()
       
  1186 */
       
  1187 
       
  1188 /*!
       
  1189     \fn void QWebView::loadFinished(bool ok)
       
  1190 
       
  1191     This signal is emitted when a load of the page is finished.
       
  1192     \a ok will indicate whether the load was successful or any error occurred.
       
  1193 
       
  1194     \sa loadStarted()
       
  1195 */
       
  1196 
       
  1197 /*!
       
  1198     \fn void QWebView::selectionChanged()
       
  1199 
       
  1200     This signal is emitted whenever the selection changes.
       
  1201 
       
  1202     \sa selectedText()
       
  1203 */
       
  1204 
       
  1205 /*!
       
  1206     \fn void QWebView::loadProgress(int progress)
       
  1207 
       
  1208     This signal is emitted every time an element in the web page
       
  1209     completes loading and the overall loading progress advances.
       
  1210 
       
  1211     This signal tracks the progress of all child frames.
       
  1212 
       
  1213     The current value is provided by \a progress and scales from 0 to 100,
       
  1214     which is the default range of QProgressBar.
       
  1215 
       
  1216     \sa loadStarted(), loadFinished()
       
  1217 */
       
  1218 
       
  1219 /*!
       
  1220     \fn void QWebView::linkClicked(const QUrl &url)
       
  1221 
       
  1222     This signal is emitted whenever the user clicks on a link and the page's linkDelegationPolicy
       
  1223     property is set to delegate the link handling for the specified \a url.
       
  1224 
       
  1225     \sa QWebPage::linkDelegationPolicy()
       
  1226 */
       
  1227 
       
  1228 #include "moc_qwebview.cpp"
       
  1229