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