summaryrefslogtreecommitdiffstats
path: root/Source/WebKit/qt/WidgetSupport/PageClientQt.cpp
blob: 858290f6e96ce6b17bc772f2eb71f5f3dd598eea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 */

#include "config.h"
#include "PageClientQt.h"

#include <QGraphicsScene>
#include <QGraphicsView>

#if defined(Q_WS_X11)
#include <QX11Info>
#endif

#ifdef QT_OPENGL_LIB
#include <QGLWidget>
#endif
#if QT_VERSION >= QT_VERSION_CHECK(5, 4, 0)
#include <QOpenGLWidget>
#endif

#if USE(ACCELERATED_COMPOSITING)
#include "TextureMapper.h"
#include "texmap/TextureMapperLayer.h"
#endif

QWindow* QWebPageClient::ownerWindow() const
{
    QWidget* widget = qobject_cast<QWidget*>(ownerWidget());
    if (!widget)
        return 0;
    if (QWindow *window = widget->windowHandle())
        return window;
    if (const QWidget *nativeParent = widget->nativeParentWidget())
        return nativeParent->windowHandle();
    return 0;
}

namespace WebCore {

void PageClientQWidget::scroll(int dx, int dy, const QRect& rectToScroll)
{
    view->scroll(qreal(dx), qreal(dy), rectToScroll);
}

void PageClientQWidget::update(const QRect & dirtyRect)
{
    view->update(dirtyRect);
}

void PageClientQWidget::repaintViewport()
{
    update(view->rect());
    QMetaObject::invokeMethod(page, "repaintRequested", Qt::QueuedConnection, Q_ARG(QRect, view->rect()));
}

void PageClientQWidget::setInputMethodEnabled(bool enable)
{
    view->setAttribute(Qt::WA_InputMethodEnabled, enable);
}

bool PageClientQWidget::inputMethodEnabled() const
{
    return view->testAttribute(Qt::WA_InputMethodEnabled);
}

void PageClientQWidget::setInputMethodHints(Qt::InputMethodHints hints)
{
    view->setInputMethodHints(hints);
}

PageClientQWidget::~PageClientQWidget()
{
}

#ifndef QT_NO_CURSOR
QCursor PageClientQWidget::cursor() const
{
    return view->cursor();
}

void PageClientQWidget::updateCursor(const QCursor& cursor)
{
    view->setCursor(cursor);
}
#endif

QPalette PageClientQWidget::palette() const
{
    return view->palette();
}

int PageClientQWidget::screenNumber() const
{
#if defined(Q_WS_X11)
    return view->x11Info().screen();
#endif
    return 0;
}

QObject* PageClientQWidget::ownerWidget() const
{
    return view;
}

QRect PageClientQWidget::geometryRelativeToOwnerWidget() const
{
    return view->geometry();
}

QPoint PageClientQWidget::mapToOwnerWindow(const QPoint& point) const
{
    QWidget* widget = qobject_cast<QWidget*>(ownerWidget());
    // Can be false both if ownerWidget() is native or if it doesn't have any native parent.
    if (const QWidget *nativeParent = widget->nativeParentWidget())
        return widget->mapTo(nativeParent, point);

    return point;
}

QObject* PageClientQWidget::pluginParent() const
{
    return view;
}

QStyle* PageClientQWidget::style() const
{
    return view->style();
}

QRectF PageClientQWidget::windowRect() const
{
    return QRectF(view->window()->geometry());
}

void PageClientQWidget::setWidgetVisible(Widget* widget, bool visible)
{
    QWidget* qtWidget = qobject_cast<QWidget*>(widget->platformWidget());
    if (!qtWidget)
        return;
    qtWidget->setVisible(visible);
}

bool PageClientQWidget::isViewVisible()
{
    return view ? view->isVisible() : false;
}

#if !defined(QT_NO_GRAPHICSVIEW)
PageClientQGraphicsWidget::~PageClientQGraphicsWidget()
{
    delete overlay;
}

void PageClientQGraphicsWidget::scroll(int dx, int dy, const QRect& rectToScroll)
{
    view->scroll(qreal(dx), qreal(dy), rectToScroll);
}

void PageClientQGraphicsWidget::update(const QRect& dirtyRect)
{
    view->update(dirtyRect);

    if (overlay)
        overlay->update(QRectF(dirtyRect));
}

void PageClientQGraphicsWidget::repaintViewport()
{
    update(view->boundingRect().toAlignedRect());
    QMetaObject::invokeMethod(page, "repaintRequested", Qt::QueuedConnection, Q_ARG(QRect, view->boundingRect().toAlignedRect()));
}

bool PageClientQGraphicsWidget::makeOpenGLContextCurrentIfAvailable()
{
#if USE(ACCELERATED_COMPOSITING) && USE(TEXTURE_MAPPER_GL)
    QGraphicsView* graphicsView = firstGraphicsView();
    if (graphicsView && graphicsView->viewport()) {
        QWidget *widget = graphicsView->viewport();
#if defined(QT_OPENGL_LIB)
        if (widget->inherits("QGLWidget")) {

            QGLWidget* glWidget = static_cast<QGLWidget*>(widget);
            // The GL context belonging to the QGLWidget viewport must be current when TextureMapper is being created.
            glWidget->makeCurrent();
            return true;
        }
#endif
#if QT_VERSION >= QT_VERSION_CHECK(5, 4, 0)
        if (widget->inherits("QOpenGLWidget")) {
            QOpenGLWidget *qoglWidget = static_cast<QOpenGLWidget*>(widget);
            qoglWidget->makeCurrent();
            return true;
        }
#endif
    }
#endif
    return false;
}

QOpenGLContext* PageClientQGraphicsWidget::openGLContextIfAvailable()
{
#if USE(ACCELERATED_COMPOSITING) && USE(TEXTURE_MAPPER_GL)
    QGraphicsView* graphicsView = firstGraphicsView();
    if (graphicsView && graphicsView->viewport()) {
        QWidget *widget = graphicsView->viewport();
#if defined(QT_OPENGL_LIB)
        if (widget->inherits("QGLWidget")) {
            QGLWidget* glWidget = static_cast<QGLWidget*>(widget);
            return glWidget->context()->contextHandle();
        }
#endif
#if QT_VERSION >= QT_VERSION_CHECK(5, 4, 0)
        if (widget->inherits("QOpenGLWidget")) {
            QOpenGLWidget *qoglWidget = static_cast<QOpenGLWidget*>(widget);
            return qoglWidget->context();
        }
#endif
    }
#endif
    return 0;
}

void PageClientQGraphicsWidget::setInputMethodEnabled(bool enable)
{
    view->setFlag(QGraphicsItem::ItemAcceptsInputMethod, enable);
}

bool PageClientQGraphicsWidget::inputMethodEnabled() const
{
    return view->flags() & QGraphicsItem::ItemAcceptsInputMethod;
}

void PageClientQGraphicsWidget::setInputMethodHints(Qt::InputMethodHints hints)
{
    view->setInputMethodHints(hints);
}

#ifndef QT_NO_CURSOR
QCursor PageClientQGraphicsWidget::cursor() const
{
    return view->cursor();
}

void PageClientQGraphicsWidget::updateCursor(const QCursor& cursor)
{
    view->setCursor(cursor);
}
#endif

QPalette PageClientQGraphicsWidget::palette() const
{
    return view->palette();
}

int PageClientQGraphicsWidget::screenNumber() const
{
#if defined(Q_WS_X11)
    if (QGraphicsView* graphicsView = firstGraphicsView())
        return graphicsView->x11Info().screen();
#endif
    return 0;
}

QObject* PageClientQGraphicsWidget::ownerWidget() const
{
    if (QGraphicsScene* scene = view->scene()) {
        const QList<QGraphicsView*> views = scene->views();
        return views.value(0);
    }
    return 0;
}

QRect PageClientQGraphicsWidget::geometryRelativeToOwnerWidget() const
{
    if (QGraphicsView* graphicsView = firstGraphicsView())
        return graphicsView->mapFromScene(view->boundingRect()).boundingRect();
    return QRect();
}

QPoint PageClientQGraphicsWidget::mapToOwnerWindow(const QPoint& point) const
{
    if (const QGraphicsView* graphicsView = firstGraphicsView()) {
        if (const QWidget *nativeParent = graphicsView->nativeParentWidget())
            return graphicsView->mapTo(nativeParent, graphicsView->mapFromScene(view->mapToScene(point)));
        else
            return graphicsView->mapFromScene(view->mapToScene(point));
    }
    return point;
}

#if USE(TILED_BACKING_STORE)
QRectF PageClientQGraphicsWidget::graphicsItemVisibleRect() const
{
    QGraphicsView* graphicsView = firstGraphicsView();
    if (!graphicsView)
        return QRectF();

    int xOffset = graphicsView->horizontalScrollBar()->value();
    int yOffset = graphicsView->verticalScrollBar()->value();
    return view->mapRectFromScene(QRectF(QPointF(xOffset, yOffset), graphicsView->viewport()->size()));
}
#endif

QObject* PageClientQGraphicsWidget::pluginParent() const
{
    return view;
}

QStyle* PageClientQGraphicsWidget::style() const
{
    return view->style();
}

void PageClientQGraphicsWidget::setWidgetVisible(Widget*, bool)
{
    // Doesn't make sense, does it?
}

QRectF PageClientQGraphicsWidget::windowRect() const
{
    if (!view->scene())
        return QRectF();

    // The sceneRect is a good approximation of the size of the application, independent of the view.
    return view->scene()->sceneRect();
}

QGraphicsView* PageClientQGraphicsWidget::firstGraphicsView() const
{
    if (view->scene() && !view->scene()->views().isEmpty())
        return view->scene()->views().first();
    return 0;
}

bool PageClientQGraphicsWidget::isViewVisible()
{
    return view ? view->isVisible() : false;
}
#endif // QT_NO_GRAPHICSVIEW

} // namespace WebCore