WebKit2/UIProcess/API/qt/qgraphicswkview.cpp
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     1 /*
       
     2  * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
       
     3  *
       
     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.
       
     8  *
       
     9  * This program 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.
       
    13  *
       
    14  * You should have received a copy of the GNU Library General Public License
       
    15  * along with this program; 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.
       
    18  *
       
    19  */
       
    20 
       
    21 #include "qgraphicswkview.h"
       
    22 
       
    23 #include "ChunkedUpdateDrawingAreaProxy.h"
       
    24 #include "IntSize.h"
       
    25 #include "RunLoop.h"
       
    26 #include "WKAPICast.h"
       
    27 #include "WebPageNamespace.h"
       
    28 #include "qwkpage.h"
       
    29 #include "qwkpage_p.h"
       
    30 #include <QGraphicsSceneMouseEvent>
       
    31 #include <QGraphicsView>
       
    32 #include <QPainter>
       
    33 #include <QScrollBar>
       
    34 #include <QStyleOptionGraphicsItem>
       
    35 #include <QtDebug>
       
    36 #include <WebCore/PlatformString.h>
       
    37 #include <WebKit2/WKRetainPtr.h>
       
    38 #include <wtf/RefPtr.h>
       
    39 
       
    40 using namespace WebKit;
       
    41 using namespace WebCore;
       
    42 
       
    43 struct QGraphicsWKViewPrivate {
       
    44     QGraphicsWKViewPrivate(QGraphicsWKView* view);
       
    45     WKPageRef pageRef() const { return page->pageRef(); }
       
    46 
       
    47     QGraphicsWKView* q;
       
    48     QWKPage* page;
       
    49 };
       
    50 
       
    51 QGraphicsWKView::QGraphicsWKView(WKPageNamespaceRef pageNamespaceRef, BackingStoreType backingStoreType, QGraphicsItem* parent)
       
    52     : QGraphicsWidget(parent)
       
    53     , d(new QGraphicsWKViewPrivate(this))
       
    54 {
       
    55     setFocusPolicy(Qt::StrongFocus);
       
    56     setAcceptHoverEvents(true);
       
    57     DrawingAreaProxy* drawingAreaProxy = new ChunkedUpdateDrawingAreaProxy(this);
       
    58 
       
    59     d->page = new QWKPage(pageNamespaceRef);
       
    60     d->page->d->init(size().toSize(), drawingAreaProxy);
       
    61     connect(d->page, SIGNAL(titleChanged(QString)), this, SIGNAL(titleChanged(QString)));
       
    62     connect(d->page, SIGNAL(loadStarted()), this, SIGNAL(loadStarted()));
       
    63     connect(d->page, SIGNAL(loadFinished(bool)), this, SIGNAL(loadFinished(bool)));
       
    64     connect(d->page, SIGNAL(loadProgress(int)), this, SIGNAL(loadProgress(int)));
       
    65     connect(d->page, SIGNAL(initialLayoutCompleted()), this, SIGNAL(initialLayoutCompleted()));
       
    66     connect(d->page, SIGNAL(urlChanged(const QUrl&)), this, SIGNAL(urlChanged(const QUrl&)));
       
    67 }
       
    68 
       
    69 QGraphicsWKView::~QGraphicsWKView()
       
    70 {
       
    71     delete d->page;
       
    72     delete d;
       
    73 }
       
    74 
       
    75 QWKPage* QGraphicsWKView::page() const
       
    76 {
       
    77     return d->page;
       
    78 }
       
    79 
       
    80 void QGraphicsWKView::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget*)
       
    81 {
       
    82     page()->d->paint(painter, option->exposedRect.toAlignedRect());
       
    83 }
       
    84 
       
    85 void QGraphicsWKView::setGeometry(const QRectF& rect)
       
    86 {
       
    87     QGraphicsWidget::setGeometry(rect);
       
    88 
       
    89     // NOTE: call geometry() as setGeometry ensures that
       
    90     // the geometry is within legal bounds (minimumSize, maximumSize)
       
    91     page()->setViewportSize(geometry().size().toSize());
       
    92 }
       
    93 
       
    94 void QGraphicsWKView::load(const QUrl& url)
       
    95 {
       
    96     page()->load(url);
       
    97 }
       
    98 
       
    99 void QGraphicsWKView::setUrl(const QUrl& url)
       
   100 {
       
   101     page()->setUrl(url);
       
   102 }
       
   103 
       
   104 QUrl QGraphicsWKView::url() const
       
   105 {
       
   106     return page()->url();
       
   107 }
       
   108 
       
   109 QString QGraphicsWKView::title() const
       
   110 {
       
   111     return page()->title();
       
   112 }
       
   113 
       
   114 void QGraphicsWKView::triggerPageAction(QWKPage::WebAction action, bool checked)
       
   115 {
       
   116     page()->triggerAction(action, checked);
       
   117 }
       
   118 
       
   119 void QGraphicsWKView::back()
       
   120 {
       
   121     page()->triggerAction(QWKPage::Back);
       
   122 }
       
   123 
       
   124 void QGraphicsWKView::forward()
       
   125 {
       
   126     page()->triggerAction(QWKPage::Forward);
       
   127 }
       
   128 
       
   129 void QGraphicsWKView::reload()
       
   130 {
       
   131     page()->triggerAction(QWKPage::Reload);
       
   132 }
       
   133 
       
   134 void QGraphicsWKView::stop()
       
   135 {
       
   136     page()->triggerAction(QWKPage::Stop);
       
   137 }
       
   138 
       
   139 /*! \reimp
       
   140 */
       
   141 QVariant QGraphicsWKView::itemChange(GraphicsItemChange change, const QVariant& value)
       
   142 {
       
   143     // Here so that it can be reimplemented without breaking ABI.
       
   144     return QGraphicsWidget::itemChange(change, value);
       
   145 }
       
   146 
       
   147 /*! \reimp
       
   148 */
       
   149 bool QGraphicsWKView::event(QEvent* event)
       
   150 {
       
   151     // Here so that it can be reimplemented without breaking ABI.
       
   152     return QGraphicsWidget::event(event);
       
   153 }
       
   154 
       
   155 /*! \reimp
       
   156 */
       
   157 QSizeF QGraphicsWKView::sizeHint(Qt::SizeHint which, const QSizeF& constraint) const
       
   158 {
       
   159     if (which == Qt::PreferredSize)
       
   160         return QSizeF(800, 600);
       
   161     return QGraphicsWidget::sizeHint(which, constraint);
       
   162 }
       
   163 
       
   164 /*! \reimp
       
   165 */
       
   166 QVariant QGraphicsWKView::inputMethodQuery(Qt::InputMethodQuery query) const
       
   167 {
       
   168     // implement
       
   169     return QVariant();
       
   170 }
       
   171 
       
   172 /*! \reimp
       
   173 */
       
   174 void QGraphicsWKView::keyPressEvent(QKeyEvent* ev)
       
   175 {
       
   176     page()->d->keyPressEvent(ev);
       
   177 }
       
   178 
       
   179 /*! \reimp
       
   180 */
       
   181 void QGraphicsWKView::keyReleaseEvent(QKeyEvent* ev)
       
   182 {
       
   183     page()->d->keyReleaseEvent(ev);
       
   184 }
       
   185 
       
   186 void QGraphicsWKView::hoverMoveEvent(QGraphicsSceneHoverEvent* ev)
       
   187 {
       
   188     QGraphicsSceneMouseEvent me(QEvent::GraphicsSceneMouseMove);
       
   189     me.setPos(ev->pos());
       
   190     me.setScreenPos(ev->screenPos());
       
   191 
       
   192     page()->d->mouseMoveEvent(&me);
       
   193 
       
   194     if (!ev->isAccepted())
       
   195         QGraphicsItem::hoverMoveEvent(ev);
       
   196 }
       
   197 
       
   198 void QGraphicsWKView::mouseMoveEvent(QGraphicsSceneMouseEvent* ev)
       
   199 {
       
   200     page()->d->mouseMoveEvent(ev);
       
   201     if (!ev->isAccepted())
       
   202         QGraphicsItem::mouseMoveEvent(ev);
       
   203 }
       
   204 
       
   205 void QGraphicsWKView::mousePressEvent(QGraphicsSceneMouseEvent* ev)
       
   206 {
       
   207     page()->d->mousePressEvent(ev);
       
   208     if (!ev->isAccepted())
       
   209         QGraphicsItem::mousePressEvent(ev);
       
   210 }
       
   211 
       
   212 void QGraphicsWKView::mouseReleaseEvent(QGraphicsSceneMouseEvent* ev)
       
   213 {
       
   214     page()->d->mouseReleaseEvent(ev);
       
   215     if (!ev->isAccepted())
       
   216         QGraphicsItem::mouseReleaseEvent(ev);
       
   217 }
       
   218 
       
   219 void QGraphicsWKView::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* ev)
       
   220 {
       
   221     page()->d->mouseDoubleClickEvent(ev);
       
   222     if (!ev->isAccepted())
       
   223         QGraphicsItem::mouseReleaseEvent(ev);
       
   224 }
       
   225 
       
   226 void QGraphicsWKView::wheelEvent(QGraphicsSceneWheelEvent* ev)
       
   227 {
       
   228     page()->d->wheelEvent(ev);
       
   229     if (!ev->isAccepted())
       
   230         QGraphicsItem::wheelEvent(ev);
       
   231 }
       
   232 
       
   233 QGraphicsWKViewPrivate::QGraphicsWKViewPrivate(QGraphicsWKView* view)
       
   234     : q(view)
       
   235 {
       
   236 }
       
   237 
       
   238 QRectF QGraphicsWKView::visibleRect() const
       
   239 {
       
   240     if (!scene())
       
   241         return QRectF();
       
   242 
       
   243     QList<QGraphicsView*> views = scene()->views();
       
   244     if (views.isEmpty())
       
   245         return QRectF();
       
   246 
       
   247     QGraphicsView* graphicsView = views.at(0);
       
   248     int xOffset = graphicsView->horizontalScrollBar()->value();
       
   249     int yOffset = graphicsView->verticalScrollBar()->value();
       
   250     return mapRectFromScene(QRectF(QPointF(xOffset, yOffset), graphicsView->viewport()->size()));
       
   251 }
       
   252 
       
   253 #include "moc_qgraphicswkview.cpp"