2 Copyright (C) 2007 Trolltech ASA
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
22 #include "qwebframe.h"
35 \brief The QWebView class provides a widget that is used to view and edit web documents.
37 QWebView is the main widget component of the QtWebKit web browsing module.
41 Constructs an empty QWebView with parent \a parent.
43 QWebView::QWebView(QWidget *parent)
46 d = new QWebViewPrivate;
49 QPalette pal = palette();
50 pal.setBrush(QPalette::Background, Qt::white);
52 setAttribute(Qt::WA_OpaquePaintEvent);
57 setMouseTracking(true);
58 setFocusPolicy(Qt::WheelFocus);
66 if (d->page && d->page->parent() == this)
72 Returns a pointer to the underlying web page.
76 QWebPage *QWebView::page() const
79 QWebView *that = const_cast<QWebView *>(this);
80 that->setPage(new QWebPage(that));
86 Makes \a page the new web page of the web view.
88 The parent QObject of the provided page remains the owner
89 of the object. If the current document is a child of the web
90 view, then it is deleted.
94 void QWebView::setPage(QWebPage *page)
99 if (d->page->parent() == this) {
102 d->page->disconnect(this);
107 d->page->setView(this);
108 // #### connect signals
109 QWebFrame *mainFrame = d->page->mainFrame();
110 connect(mainFrame, SIGNAL(loadStarted()),
111 this, SIGNAL(loadStarted()));
112 connect(mainFrame, SIGNAL(loadFinished()),
113 this, SIGNAL(loadFinished()));
114 connect(mainFrame, SIGNAL(titleChanged(const QString&)),
115 this, SIGNAL(titleChanged(const QString&)));
116 connect(mainFrame, SIGNAL(iconLoaded()),
117 this, SIGNAL(iconLoaded()));
118 connect(mainFrame, SIGNAL(urlChanged(const QUrl &)),
119 this, SIGNAL(urlChanged(const QUrl &)));
121 connect(d->page, SIGNAL(loadProgressChanged(int)),
122 this, SIGNAL(loadProgressChanged(int)));
123 connect(d->page, SIGNAL(statusBarTextChanged(const QString &)),
124 this, SIGNAL(statusBarTextChanged(const QString &)));
130 Downloads the specified \a url and displays it.
132 void QWebView::load(const QUrl &url)
134 page()->mainFrame()->load(url);
137 #if QT_VERSION < 0x040400
138 void QWebView::load(const QWebNetworkRequest &request)
140 void QWebView::load(const QNetworkRequest &request,
141 QNetworkAccessManager::Operation operation,
142 const QByteArray &body)
145 page()->mainFrame()->load(request
146 #if QT_VERSION >= 0x040400
153 Sets the content of the web view to the specified \a html.
155 External objects referenced in the HTML document are located relative to \a baseUrl.
157 void QWebView::setHtml(const QString &html, const QUrl &baseUrl)
159 page()->mainFrame()->setHtml(html, baseUrl);
163 Sets the content of the web view to the specified \a html.
165 External objects referenced in the HTML document are located relative to \a baseUrl.
167 void QWebView::setHtml(const QByteArray &html, const QUrl &baseUrl)
169 page()->mainFrame()->setHtml(html, baseUrl);
173 Sets the content of the web view to the specified content \a data. If the \a mimeType argument
174 is empty it is assumed that the content is HTML.
176 External objects referenced in the HTML document are located relative to \a baseUrl.
178 void QWebView::setContent(const QByteArray &data, const QString &mimeType, const QUrl &baseUrl)
180 page()->mainFrame()->setContent(data, mimeType, baseUrl);
184 Returns a pointer to the view's history of navigated web pages.
189 view->page()->history();
192 QWebHistory *QWebView::history() const
194 return page()->history();
198 Returns a pointer to the view/page specific settings object.
203 view->page()->settings();
206 QWebSettings *QWebView::settings() const
208 return page()->settings();
212 \property QWebView::title
213 \brief the title of the web page currently viewed.
215 QString QWebView::title() const
218 return d->page->mainFrame()->title();
223 \property QWebView::url
224 \brief the url of the web page currently viewed.
226 QUrl QWebView::url() const
229 return d->page->mainFrame()->url();
234 \property QWebView::icon
235 \brief the icon associated with the web page currently viewed.
237 QPixmap QWebView::icon() const
240 return d->page->mainFrame()->icon();
245 \property QWebView::selectedText
246 \brief the text currently selected.
248 QString QWebView::selectedText() const
251 return d->page->selectedText();
256 Returns a pointer to a QAction that encapsulates the specified web action \a action.
258 QAction *QWebView::action(QWebPage::WebAction action) const
260 return page()->action(action);
264 Triggers the specified \a action. If it is a checkable action the specified \a checked state is assumed.
266 The following example triggers the copy action and therefore copies any selected text to the clipboard.
269 view->triggerAction(QWebPage::Copy);
272 void QWebView::triggerAction(QWebPage::WebAction action, bool checked)
274 page()->triggerAction(action, checked);
278 \property QWebView::modified
279 \brief Indicates whether the document was modified by the user or not.
281 Parts of HTML documents can be editable for example through the \c{contenteditable} attribute on
284 bool QWebView::isModified() const
287 return d->page->isModified();
291 Qt::TextInteractionFlags QWebView::textInteractionFlags() const
293 // ### FIXME (add to page)
294 return Qt::TextInteractionFlags();
298 \property QWebView::textInteractionFlags
300 Specifies how the view should interact with user input.
303 void QWebView::setTextInteractionFlags(Qt::TextInteractionFlags flags)
306 // ### FIXME (add to page)
312 QSize QWebView::sizeHint() const
314 return QSize(800, 600); // ####...
318 Convenience slot that stops loading the document.
323 view->page()->triggerAction(QWebPage::Stop);
326 void QWebView::stop()
329 d->page->triggerAction(QWebPage::Stop);
333 Convenience slot that loads the previous document in the list of
334 documents built by navigating links. Does nothing if there is no
340 view->page()->triggerAction(QWebPage::GoBack);
343 void QWebView::backward()
346 d->page->triggerAction(QWebPage::GoBack);
350 Convenience slot that loads the next document in the list of
351 documents built by navigating links. Does nothing if there is no
357 view->page()->triggerAction(QWebPage::GoForward);
360 void QWebView::forward()
363 d->page->triggerAction(QWebPage::GoForward);
367 Reloads the current document.
369 void QWebView::reload()
372 d->page->triggerAction(QWebPage::Reload);
377 void QWebView::resizeEvent(QResizeEvent *e)
380 d->page->setViewportSize(e->size());
385 void QWebView::paintEvent(QPaintEvent *ev)
389 #ifdef QWEBKIT_TIME_RENDERING
394 QWebFrame *frame = d->page->mainFrame();
397 frame->render(&p, ev->region());
399 #ifdef QWEBKIT_TIME_RENDERING
400 int elapsed = time.elapsed();
401 qDebug()<<"paint event on "<<ev->region()<<", took to render = "<<elapsed;
406 This function is called whenever WebKit wants to create a new window, for example as a result of
407 a JavaScript request to open a document in a new window.
409 QWebView *QWebView::createWindow()
416 void QWebView::mouseMoveEvent(QMouseEvent* ev)
424 void QWebView::mousePressEvent(QMouseEvent* ev)
432 void QWebView::mouseDoubleClickEvent(QMouseEvent* ev)
440 void QWebView::mouseReleaseEvent(QMouseEvent* ev)
448 void QWebView::contextMenuEvent(QContextMenuEvent* ev)
456 void QWebView::wheelEvent(QWheelEvent* ev)
461 if (!ev->isAccepted())
462 return QWidget::wheelEvent(ev);
467 void QWebView::keyPressEvent(QKeyEvent* ev)
471 if (!ev->isAccepted())
472 QWidget::keyPressEvent(ev);
477 void QWebView::keyReleaseEvent(QKeyEvent* ev)
481 if (!ev->isAccepted())
482 QWidget::keyReleaseEvent(ev);
487 void QWebView::focusInEvent(QFocusEvent* ev)
491 QWidget::focusInEvent(ev);
496 void QWebView::focusOutEvent(QFocusEvent* ev)
498 QWidget::focusOutEvent(ev);
505 void QWebView::dragEnterEvent(QDragEnterEvent* ev)
507 #ifndef QT_NO_DRAGANDDROP
515 void QWebView::dragLeaveEvent(QDragLeaveEvent* ev)
517 #ifndef QT_NO_DRAGANDDROP
525 void QWebView::dragMoveEvent(QDragMoveEvent* ev)
527 #ifndef QT_NO_DRAGANDDROP
535 void QWebView::dropEvent(QDropEvent* ev)
537 #ifndef QT_NO_DRAGANDDROP
545 bool QWebView::focusNextPrevChild(bool next)
547 if (d->page && d->page->focusNextPrevChild(next))
549 return QWidget::focusNextPrevChild(next);
553 \fn void QWebView::titleChanged(const QString &title)
555 This signal is emitted whenever the \a title of the main frame changes.
561 \fn void QWebView::urlChanged(const QUrl &url)
563 This signal is emitted whenever the \a url of the main frame changes.
569 \fn void QWebView::statusBarTextChanged(const QString& text)
571 This signal is emitted when the statusbar \a text is changed by the page.
575 \fn void QWebView::iconLoaded()
577 This signal is emitted whenever the icon of the page is loaded or changes.
581 \fn void QWebView::loadStarted()
583 This signal is emitted when a new load of the frame is started.
587 \fn void QWebView::loadFinished()
589 This signal is emitted when a load of the frame is finished.
593 \fn void QWebView::selectionChanged()
595 This signal is emitted whenever the selection changes.
599 \fn void QWebView::loadProgressChanged(int progress)
601 This signal is emitted when the global progress status changes.
602 The current value is provided by \a progress in percent.
603 It accumulates changes from all the child frames.