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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
|
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtDeclarative module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qdeclarativewebview_p.h"
#include "qdeclarativewebview_p_p.h"
#include <private/qdeclarativepainteditem_p_p.h>
#include <qdeclarative.h>
#include <qdeclarativeengine.h>
#include <private/qdeclarativestate_p.h>
#include <QDebug>
#include <QPen>
#include <QFile>
#include <QEvent>
#include <QMouseEvent>
#include <QKeyEvent>
#include <QBasicTimer>
#include <QApplication>
#include <QGraphicsSceneMouseEvent>
#include <QtWebKit/QWebPage>
#include <QtWebKit/QWebFrame>
#include <QtWebKit/QWebElement>
#include <QtWebKit/QWebSettings>
#include <private/qlistmodelinterface_p.h>
QT_BEGIN_NAMESPACE
static const int MAX_DOUBLECLICK_TIME=500; // XXX need better gesture system
class QDeclarativeWebViewPrivate : public QDeclarativePaintedItemPrivate
{
Q_DECLARE_PUBLIC(QDeclarativeWebView)
public:
QDeclarativeWebViewPrivate()
: QDeclarativePaintedItemPrivate(), page(0), preferredwidth(0), preferredheight(0),
progress(1.0), status(QDeclarativeWebView::Null), pending(PendingNone),
newWindowComponent(0), newWindowParent(0),
pressTime(400),
rendering(true)
{
}
void focusChanged(bool);
QUrl url; // page url might be different if it has not loaded yet
QWebPage *page;
int preferredwidth, preferredheight;
qreal progress;
QDeclarativeWebView::Status status;
QString statusText;
enum { PendingNone, PendingUrl, PendingHtml, PendingContent } pending;
QUrl pending_url;
QString pending_string;
QByteArray pending_data;
mutable QDeclarativeWebSettings settings;
QDeclarativeComponent *newWindowComponent;
QDeclarativeItem *newWindowParent;
QBasicTimer pressTimer;
QPoint pressPoint;
int pressTime; // milliseconds before it's a "hold"
static void windowObjects_append(QDeclarativeListProperty<QObject> *prop, QObject *o) {
static_cast<QDeclarativeWebViewPrivate *>(prop->data)->windowObjects.append(o);
static_cast<QDeclarativeWebViewPrivate *>(prop->data)->updateWindowObjects();
}
void updateWindowObjects();
QObjectList windowObjects;
bool rendering;
};
/*!
\qmlclass WebView QDeclarativeWebView
\since 4.7
\brief The WebView item allows you to add web content to a canvas.
\inherits Item
A WebView renders web content based on a URL.
If the width and height of the item is not set, they will
dynamically adjust to a size appropriate for the content.
This width may be large for typical online web pages.
If the preferredWidth is set, the width will be this amount or larger,
usually laying out the web content to fit the preferredWidth.
\qml
import org.webkit 1.0
WebView {
url: "http://www.nokia.com"
width: 490
height: 400
scale: 0.5
smooth: false
smoothCache: true
}
\endqml
\image webview.png
The item includes no scrolling, scaling,
toolbars, etc., those must be implemented around WebView. See the WebBrowser example
for a demonstration of this.
*/
/*!
\internal
\class QDeclarativeWebView
\brief The QDeclarativeWebView class allows you to add web content to a QDeclarativeView.
A WebView renders web content base on a URL.
\image webview.png
The item includes no scrolling, scaling,
toolbars, etc., those must be implemented around WebView. See the WebBrowser example
for a demonstration of this.
A QDeclarativeWebView object can be instantiated in Qml using the tag \l WebView.
*/
QDeclarativeWebView::QDeclarativeWebView(QDeclarativeItem *parent)
: QDeclarativePaintedItem(*(new QDeclarativeWebViewPrivate), parent)
{
init();
}
QDeclarativeWebView::~QDeclarativeWebView()
{
Q_D(QDeclarativeWebView);
delete d->page;
}
void QDeclarativeWebView::init()
{
Q_D(QDeclarativeWebView);
QWebSettings::enablePersistentStorage();
setAcceptHoverEvents(true);
setAcceptedMouseButtons(Qt::LeftButton);
setFlag(QGraphicsItem::ItemHasNoContents, false);
d->page = 0;
}
void QDeclarativeWebView::componentComplete()
{
QDeclarativePaintedItem::componentComplete();
Q_D(QDeclarativeWebView);
switch (d->pending) {
case QDeclarativeWebViewPrivate::PendingUrl:
setUrl(d->pending_url);
break;
case QDeclarativeWebViewPrivate::PendingHtml:
setHtml(d->pending_string, d->pending_url);
break;
case QDeclarativeWebViewPrivate::PendingContent:
setContent(d->pending_data, d->pending_string, d->pending_url);
break;
default:
break;
}
d->pending = QDeclarativeWebViewPrivate::PendingNone;
d->updateWindowObjects();
}
QDeclarativeWebView::Status QDeclarativeWebView::status() const
{
Q_D(const QDeclarativeWebView);
return d->status;
}
/*!
\qmlproperty real WebView::progress
This property holds the progress of loading the current URL, from 0 to 1.
If you just want to know when progress gets to 1, use
WebView::onLoadFinished() or WebView::onLoadFailed() instead.
*/
qreal QDeclarativeWebView::progress() const
{
Q_D(const QDeclarativeWebView);
return d->progress;
}
void QDeclarativeWebView::doLoadStarted()
{
Q_D(QDeclarativeWebView);
if (!d->url.isEmpty()) {
d->status = Loading;
emit statusChanged(d->status);
}
emit loadStarted();
}
void QDeclarativeWebView::doLoadProgress(int p)
{
Q_D(QDeclarativeWebView);
if (d->progress == p/100.0)
return;
d->progress = p/100.0;
emit progressChanged();
}
void QDeclarativeWebView::pageUrlChanged()
{
Q_D(QDeclarativeWebView);
page()->setViewportSize(QSize(
d->preferredwidth>0 ? d->preferredwidth : width(),
d->preferredheight>0 ? d->preferredheight : height()));
expandToWebPage();
if ((d->url.isEmpty() && page()->mainFrame()->url() != QUrl(QLatin1String("about:blank")))
|| (d->url != page()->mainFrame()->url() && !page()->mainFrame()->url().isEmpty()))
{
d->url = page()->mainFrame()->url();
if (d->url == QUrl(QLatin1String("about:blank")))
d->url = QUrl();
emit urlChanged();
}
}
void QDeclarativeWebView::doLoadFinished(bool ok)
{
Q_D(QDeclarativeWebView);
if (title().isEmpty())
pageUrlChanged(); // XXX bug 232556 - pages with no title never get urlChanged()
if (ok) {
d->status = d->url.isEmpty() ? Null : Ready;
emit loadFinished();
} else {
d->status = Error;
emit loadFailed();
}
emit statusChanged(d->status);
}
/*!
\qmlproperty url WebView::url
This property holds the URL to the page displayed in this item. It can be set,
but also can change spontaneously (eg. because of network redirection).
If the url is empty, the page is blank.
The url is always absolute (QML will resolve relative URL strings in the context
of the containing QML document).
*/
QUrl QDeclarativeWebView::url() const
{
Q_D(const QDeclarativeWebView);
return d->url;
}
void QDeclarativeWebView::setUrl(const QUrl &url)
{
Q_D(QDeclarativeWebView);
if (url == d->url)
return;
if (isComponentComplete()) {
d->url = url;
page()->setViewportSize(QSize(
d->preferredwidth>0 ? d->preferredwidth : width(),
d->preferredheight>0 ? d->preferredheight : height()));
QUrl seturl = url;
if (seturl.isEmpty())
seturl = QUrl(QLatin1String("about:blank"));
Q_ASSERT(!seturl.isRelative());
page()->mainFrame()->load(seturl);
emit urlChanged();
} else {
d->pending = d->PendingUrl;
d->pending_url = url;
}
}
/*!
\qmlproperty int WebView::preferredWidth
This property holds the ideal width for displaying the current URL.
*/
int QDeclarativeWebView::preferredWidth() const
{
Q_D(const QDeclarativeWebView);
return d->preferredwidth;
}
void QDeclarativeWebView::setPreferredWidth(int iw)
{
Q_D(QDeclarativeWebView);
if (d->preferredwidth == iw) return;
d->preferredwidth = iw;
//expandToWebPage();
emit preferredWidthChanged();
}
/*!
\qmlproperty int WebView::preferredHeight
This property holds the ideal height for displaying the current URL.
This only affects the area zoomed by heuristicZoom().
*/
int QDeclarativeWebView::preferredHeight() const
{
Q_D(const QDeclarativeWebView);
return d->preferredheight;
}
void QDeclarativeWebView::setPreferredHeight(int ih)
{
Q_D(QDeclarativeWebView);
if (d->preferredheight == ih) return;
d->preferredheight = ih;
emit preferredHeightChanged();
}
/*!
\qmlmethod bool WebView::evaluateJavaScript(string)
Evaluates the \a scriptSource JavaScript inside the context of the
main web frame, and returns the result of the last executed statement.
Note that this JavaScript does \e not have any access to QML objects
except as made available as windowObjects.
*/
QVariant QDeclarativeWebView::evaluateJavaScript(const QString &scriptSource)
{
return this->page()->mainFrame()->evaluateJavaScript(scriptSource);
}
void QDeclarativeWebViewPrivate::focusChanged(bool hasFocus)
{
Q_Q(QDeclarativeWebView);
QFocusEvent e(hasFocus ? QEvent::FocusIn : QEvent::FocusOut);
q->page()->event(&e);
QDeclarativeItemPrivate::focusChanged(hasFocus);
}
void QDeclarativeWebView::initialLayout()
{
// nothing useful to do at this point
}
void QDeclarativeWebView::noteContentsSizeChanged(const QSize&)
{
expandToWebPage();
}
void QDeclarativeWebView::expandToWebPage()
{
Q_D(QDeclarativeWebView);
QSize cs = page()->mainFrame()->contentsSize();
if (cs.width() < d->preferredwidth)
cs.setWidth(d->preferredwidth);
if (cs.height() < d->preferredheight)
cs.setHeight(d->preferredheight);
if (widthValid())
cs.setWidth(width());
if (heightValid())
cs.setHeight(height());
if (cs != page()->viewportSize()) {
page()->setViewportSize(cs);
}
if (cs != contentsSize())
setContentsSize(cs);
}
void QDeclarativeWebView::geometryChanged(const QRectF &newGeometry,
const QRectF &oldGeometry)
{
if (newGeometry.size() != oldGeometry.size())
expandToWebPage();
QDeclarativePaintedItem::geometryChanged(newGeometry, oldGeometry);
}
void QDeclarativeWebView::paintPage(const QRect& r)
{
dirtyCache(r);
update();
}
/*!
\qmlproperty list<object> WebView::javaScriptWindowObjects
This property is a list of object that are available from within
the webview's JavaScript context.
The \a object will be inserted as a child of the frame's window
object, under the name given by the attached property \c WebView.windowObjectName.
\qml
WebView {
javaScriptWindowObjects: Object {
WebView.windowObjectName: "coordinates"
}
}
\endqml
Properties of the object will be exposed as JavaScript properties and slots as
JavaScript methods.
If Javascript is not enabled for this page, then this property does nothing.
*/
QDeclarativeListProperty<QObject> QDeclarativeWebView::javaScriptWindowObjects()
{
Q_D(QDeclarativeWebView);
return QDeclarativeListProperty<QObject>(this, d, &QDeclarativeWebViewPrivate::windowObjects_append);
}
QDeclarativeWebViewAttached *QDeclarativeWebView::qmlAttachedProperties(QObject *o)
{
return new QDeclarativeWebViewAttached(o);
}
void QDeclarativeWebViewPrivate::updateWindowObjects()
{
Q_Q(QDeclarativeWebView);
if (!q->isComponentComplete() || !page)
return;
for (int ii = 0; ii < windowObjects.count(); ++ii) {
QObject *object = windowObjects.at(ii);
QDeclarativeWebViewAttached *attached = static_cast<QDeclarativeWebViewAttached *>(qmlAttachedPropertiesObject<QDeclarativeWebView>(object));
if (attached && !attached->windowObjectName().isEmpty()) {
page->mainFrame()->addToJavaScriptWindowObject(attached->windowObjectName(), object);
}
}
}
bool QDeclarativeWebView::renderingEnabled() const
{
Q_D(const QDeclarativeWebView);
return d->rendering;
}
void QDeclarativeWebView::setRenderingEnabled(bool enabled)
{
Q_D(QDeclarativeWebView);
if (d->rendering == enabled)
return;
d->rendering = enabled;
emit renderingEnabledChanged();
setCacheFrozen(!enabled);
if (enabled)
clearCache();
}
void QDeclarativeWebView::drawContents(QPainter *p, const QRect &r)
{
Q_D(QDeclarativeWebView);
if (d->rendering)
page()->mainFrame()->render(p,r);
}
QMouseEvent *QDeclarativeWebView::sceneMouseEventToMouseEvent(QGraphicsSceneMouseEvent *e)
{
QEvent::Type t;
switch(e->type()) {
default:
case QEvent::GraphicsSceneMousePress:
t = QEvent::MouseButtonPress;
break;
case QEvent::GraphicsSceneMouseRelease:
t = QEvent::MouseButtonRelease;
break;
case QEvent::GraphicsSceneMouseMove:
t = QEvent::MouseMove;
break;
case QGraphicsSceneEvent::GraphicsSceneMouseDoubleClick:
t = QEvent::MouseButtonDblClick;
break;
}
QMouseEvent *me = new QMouseEvent(t, (e->pos()/contentsScale()).toPoint(), e->button(), e->buttons(), 0);
return me;
}
QMouseEvent *QDeclarativeWebView::sceneHoverMoveEventToMouseEvent(QGraphicsSceneHoverEvent *e)
{
QEvent::Type t = QEvent::MouseMove;
QMouseEvent *me = new QMouseEvent(t, (e->pos()/contentsScale()).toPoint(), Qt::NoButton, Qt::NoButton, 0);
return me;
}
/*!
\qmlsignal WebView::onDoubleClick(clickx,clicky)
The WebView does not pass double-click events to the web engine, but rather
emits this signals.
*/
void QDeclarativeWebView::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
{
QMouseEvent *me = sceneMouseEventToMouseEvent(event);
emit doubleClick(me->x(),me->y());
delete me;
}
/*!
\qmlmethod bool WebView::heuristicZoom(clickX,clickY,maxzoom)
Finds a zoom that:
\list
\i shows a whole item
\i includes (\a clickX, \a clickY)
\i fits into the preferredWidth and preferredHeight
\i zooms by no more than \a maxzoom
\i is more than 10% above the current zoom
\endlist
If such a zoom exists, emits zoomTo(zoom,centerX,centerY) and returns true; otherwise,
no signal is emitted and returns false.
*/
bool QDeclarativeWebView::heuristicZoom(int clickX, int clickY, qreal maxzoom)
{
Q_D(QDeclarativeWebView);
if (contentsScale() >= maxzoom/zoomFactor())
return false;
qreal ozf = contentsScale();
QRect showarea = elementAreaAt(clickX, clickY, d->preferredwidth/maxzoom, d->preferredheight/maxzoom);
qreal z = qMin(qreal(d->preferredwidth)/showarea.width(),qreal(d->preferredheight)/showarea.height());
if (z > maxzoom/zoomFactor())
z = maxzoom/zoomFactor();
if (z/ozf > 1.2) {
QRectF r(showarea.left()*z, showarea.top()*z, showarea.width()*z, showarea.height()*z);
emit zoomTo(z,r.x()+r.width()/2, r.y()+r.height()/2);
return true;
} else {
return false;
}
}
/*!
\qmlproperty int WebView::pressGrabTime
The number of milliseconds the user must press before the WebView
starts passing move events through to the web engine (rather than
letting other QML elements such as a Flickable take them).
Defaults to 400ms. Set to 0 to always grab and pass move events to
the web engine.
*/
int QDeclarativeWebView::pressGrabTime() const
{
Q_D(const QDeclarativeWebView);
return d->pressTime;
}
void QDeclarativeWebView::setPressGrabTime(int ms)
{
Q_D(QDeclarativeWebView);
if (d->pressTime == ms)
return;
d->pressTime = ms;
emit pressGrabTimeChanged();
}
void QDeclarativeWebView::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
Q_D(QDeclarativeWebView);
setFocus (true);
QMouseEvent *me = sceneMouseEventToMouseEvent(event);
d->pressPoint = me->pos();
if (d->pressTime) {
d->pressTimer.start(d->pressTime,this);
setKeepMouseGrab(false);
} else {
grabMouse();
setKeepMouseGrab(true);
}
page()->event(me);
event->setAccepted(
/*
It is not correct to send the press event upwards, if it is not accepted by WebKit
e.g. push button does not work, if done so as QGraphicsScene will not send the release event at all to WebKit
Might be a bug in WebKit, though
*/
#if 1 //QT_VERSION <= 0x040500 // XXX see bug 230835
true
#else
me->isAccepted()
#endif
);
delete me;
if (!event->isAccepted()) {
QDeclarativePaintedItem::mousePressEvent(event);
}
}
void QDeclarativeWebView::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
Q_D(QDeclarativeWebView);
QMouseEvent *me = sceneMouseEventToMouseEvent(event);
page()->event(me);
d->pressTimer.stop();
event->setAccepted(
/*
It is not correct to send the press event upwards, if it is not accepted by WebKit
e.g. push button does not work, if done so as QGraphicsScene will not send all the events to WebKit
*/
#if 1 //QT_VERSION <= 0x040500 // XXX see bug 230835
true
#else
me->isAccepted()
#endif
);
delete me;
if (!event->isAccepted()) {
QDeclarativePaintedItem::mouseReleaseEvent(event);
}
setKeepMouseGrab(false);
ungrabMouse();
}
void QDeclarativeWebView::timerEvent(QTimerEvent *event)
{
Q_D(QDeclarativeWebView);
if (event->timerId() == d->pressTimer.timerId()) {
d->pressTimer.stop();
grabMouse();
setKeepMouseGrab(true);
}
}
void QDeclarativeWebView::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
Q_D(QDeclarativeWebView);
QMouseEvent *me = sceneMouseEventToMouseEvent(event);
if (d->pressTimer.isActive()) {
if ((me->pos() - d->pressPoint).manhattanLength() > QApplication::startDragDistance()) {
d->pressTimer.stop();
}
}
if (keepMouseGrab()) {
page()->event(me);
event->setAccepted(
/*
It is not correct to send the press event upwards, if it is not accepted by WebKit
e.g. push button does not work, if done so as QGraphicsScene will not send the release event at all to WebKit
Might be a bug in WebKit, though
*/
#if 1 // QT_VERSION <= 0x040500 // XXX see bug 230835
true
#else
me->isAccepted()
#endif
);
}
delete me;
if (!event->isAccepted())
QDeclarativePaintedItem::mouseMoveEvent(event);
}
void QDeclarativeWebView::hoverMoveEvent (QGraphicsSceneHoverEvent * event)
{
QMouseEvent *me = sceneHoverMoveEventToMouseEvent(event);
page()->event(me);
event->setAccepted(
#if QT_VERSION <= 0x040500 // XXX see bug 230835
true
#else
me->isAccepted()
#endif
);
delete me;
if (!event->isAccepted())
QDeclarativePaintedItem::hoverMoveEvent(event);
}
void QDeclarativeWebView::keyPressEvent(QKeyEvent* event)
{
page()->event(event);
if (!event->isAccepted())
QDeclarativePaintedItem::keyPressEvent(event);
}
void QDeclarativeWebView::keyReleaseEvent(QKeyEvent* event)
{
page()->event(event);
if (!event->isAccepted())
QDeclarativePaintedItem::keyReleaseEvent(event);
}
bool QDeclarativeWebView::sceneEvent(QEvent *event)
{
if (event->type() == QEvent::KeyPress) {
QKeyEvent *k = static_cast<QKeyEvent *>(event);
if (k->key() == Qt::Key_Tab || k->key() == Qt::Key_Backtab) {
if (!(k->modifiers() & (Qt::ControlModifier | Qt::AltModifier))) { //### Add MetaModifier?
page()->event(event);
if (event->isAccepted())
return true;
}
}
}
return QDeclarativePaintedItem::sceneEvent(event);
}
/*!
\qmlproperty action WebView::back
This property holds the action for causing the previous URL in the history to be displayed.
*/
QAction *QDeclarativeWebView::backAction() const
{
return page()->action(QWebPage::Back);
}
/*!
\qmlproperty action WebView::forward
This property holds the action for causing the next URL in the history to be displayed.
*/
QAction *QDeclarativeWebView::forwardAction() const
{
return page()->action(QWebPage::Forward);
}
/*!
\qmlproperty action WebView::reload
This property holds the action for reloading with the current URL
*/
QAction *QDeclarativeWebView::reloadAction() const
{
return page()->action(QWebPage::Reload);
}
/*!
\qmlproperty action WebView::stop
This property holds the action for stopping loading with the current URL
*/
QAction *QDeclarativeWebView::stopAction() const
{
return page()->action(QWebPage::Stop);
}
/*!
\qmlproperty real WebView::title
This property holds the title of the web page currently viewed
By default, this property contains an empty string.
*/
QString QDeclarativeWebView::title() const
{
return page()->mainFrame()->title();
}
/*!
\qmlproperty pixmap WebView::icon
This property holds the icon associated with the web page currently viewed
*/
QPixmap QDeclarativeWebView::icon() const
{
return page()->mainFrame()->icon().pixmap(QSize(256,256));
}
/*!
\qmlproperty real WebView::zoomFactor
This property holds the multiplier used to scale the contents of a Web page.
*/
void QDeclarativeWebView::setZoomFactor(qreal factor)
{
Q_D(QDeclarativeWebView);
if (factor == page()->mainFrame()->zoomFactor())
return;
page()->mainFrame()->setZoomFactor(factor);
page()->setViewportSize(QSize(
d->preferredwidth>0 ? d->preferredwidth*factor : width()*factor,
d->preferredheight>0 ? d->preferredheight*factor : height()*factor));
expandToWebPage();
emit zoomFactorChanged();
}
qreal QDeclarativeWebView::zoomFactor() const
{
return page()->mainFrame()->zoomFactor();
}
/*!
\qmlproperty string WebView::statusText
This property is the current status suggested by the current web page. In a web browser,
such status is often shown in some kind of status bar.
*/
void QDeclarativeWebView::setStatusText(const QString& s)
{
Q_D(QDeclarativeWebView);
d->statusText = s;
emit statusTextChanged();
}
void QDeclarativeWebView::windowObjectCleared()
{
Q_D(QDeclarativeWebView);
d->updateWindowObjects();
}
QString QDeclarativeWebView::statusText() const
{
Q_D(const QDeclarativeWebView);
return d->statusText;
}
QWebPage *QDeclarativeWebView::page() const
{
Q_D(const QDeclarativeWebView);
if (!d->page) {
QDeclarativeWebView *self = const_cast<QDeclarativeWebView*>(this);
QWebPage *wp = new QDeclarativeWebPage(self);
// QML items don't default to having a background,
// even though most we pages will set one anyway.
QPalette pal = QApplication::palette();
pal.setBrush(QPalette::Base, QColor::fromRgbF(0, 0, 0, 0));
wp->setPalette(pal);
wp->setNetworkAccessManager(qmlEngine(this)->networkAccessManager());
self->setPage(wp);
return wp;
}
return d->page;
}
// The QObject interface to settings().
/*!
\qmlproperty string WebView::settings.standardFontFamily
\qmlproperty string WebView::settings.fixedFontFamily
\qmlproperty string WebView::settings.serifFontFamily
\qmlproperty string WebView::settings.sansSerifFontFamily
\qmlproperty string WebView::settings.cursiveFontFamily
\qmlproperty string WebView::settings.fantasyFontFamily
\qmlproperty int WebView::settings.minimumFontSize
\qmlproperty int WebView::settings.minimumLogicalFontSize
\qmlproperty int WebView::settings.defaultFontSize
\qmlproperty int WebView::settings.defaultFixedFontSize
\qmlproperty bool WebView::settings.autoLoadImages
\qmlproperty bool WebView::settings.javascriptEnabled
\qmlproperty bool WebView::settings.javaEnabled
\qmlproperty bool WebView::settings.pluginsEnabled
\qmlproperty bool WebView::settings.privateBrowsingEnabled
\qmlproperty bool WebView::settings.javascriptCanOpenWindows
\qmlproperty bool WebView::settings.javascriptCanAccessClipboard
\qmlproperty bool WebView::settings.developerExtrasEnabled
\qmlproperty bool WebView::settings.linksIncludedInFocusChain
\qmlproperty bool WebView::settings.zoomTextOnly
\qmlproperty bool WebView::settings.printElementBackgrounds
\qmlproperty bool WebView::settings.offlineStorageDatabaseEnabled
\qmlproperty bool WebView::settings.offlineWebApplicationCacheEnabled
\qmlproperty bool WebView::settings.localStorageDatabaseEnabled
\qmlproperty bool WebView::settings.localContentCanAccessRemoteUrls
These properties give access to the settings controlling the web view.
See QWebSettings for details of these properties.
\qml
WebView {
settings.pluginsEnabled: true
settings.standardFontFamily: "Arial"
...
}
\endqml
*/
QDeclarativeWebSettings *QDeclarativeWebView::settingsObject() const
{
Q_D(const QDeclarativeWebView);
d->settings.s = page()->settings();
return &d->settings;
}
void QDeclarativeWebView::setPage(QWebPage *page)
{
Q_D(QDeclarativeWebView);
if (d->page == page)
return;
if (d->page) {
if (d->page->parent() == this) {
delete d->page;
} else {
d->page->disconnect(this);
}
}
d->page = page;
d->page->setViewportSize(QSize(
d->preferredwidth>0 ? d->preferredwidth : width(),
d->preferredheight>0 ? d->preferredheight : height()));
d->page->mainFrame()->setScrollBarPolicy(Qt::Horizontal,Qt::ScrollBarAlwaysOff);
d->page->mainFrame()->setScrollBarPolicy(Qt::Vertical,Qt::ScrollBarAlwaysOff);
connect(d->page,SIGNAL(repaintRequested(QRect)),this,SLOT(paintPage(QRect)));
connect(d->page->mainFrame(),SIGNAL(urlChanged(QUrl)),this,SLOT(pageUrlChanged()));
connect(d->page->mainFrame(), SIGNAL(titleChanged(QString)), this, SIGNAL(titleChanged(QString)));
connect(d->page->mainFrame(), SIGNAL(titleChanged(QString)), this, SIGNAL(iconChanged()));
connect(d->page->mainFrame(), SIGNAL(iconChanged()), this, SIGNAL(iconChanged()));
connect(d->page->mainFrame(), SIGNAL(contentsSizeChanged(QSize)), this, SLOT(noteContentsSizeChanged(QSize)));
connect(d->page->mainFrame(), SIGNAL(initialLayoutCompleted()), this, SLOT(initialLayout()));
connect(d->page,SIGNAL(loadStarted()),this,SLOT(doLoadStarted()));
connect(d->page,SIGNAL(loadProgress(int)),this,SLOT(doLoadProgress(int)));
connect(d->page,SIGNAL(loadFinished(bool)),this,SLOT(doLoadFinished(bool)));
connect(d->page,SIGNAL(statusBarMessage(QString)),this,SLOT(setStatusText(QString)));
connect(d->page->mainFrame(),SIGNAL(javaScriptWindowObjectCleared()),this,SLOT(windowObjectCleared()));
}
/*!
\qmlsignal WebView::onLoadStarted()
This handler is called when the web engine begins loading
a page. Later, WebView::onLoadFinished() or WebView::onLoadFailed()
will be emitted.
*/
/*!
\qmlsignal WebView::onLoadFinished()
This handler is called when the web engine \e successfully
finishes loading a page, including any component content
(WebView::onLoadFailed() will be emitted otherwise).
\sa progress
*/
/*!
\qmlsignal WebView::onLoadFailed()
This handler is called when the web engine fails loading
a page or any component content
(WebView::onLoadFinished() will be emitted on success).
*/
void QDeclarativeWebView::load(const QNetworkRequest &request,
QNetworkAccessManager::Operation operation,
const QByteArray &body)
{
page()->mainFrame()->load(request, operation, body);
}
QString QDeclarativeWebView::html() const
{
return page()->mainFrame()->toHtml();
}
/*!
\qmlproperty string WebView::html
This property holds HTML text set directly
The html property can be set as a string.
\qml
WebView {
html: "<p>This is <b>HTML</b>."
}
\endqml
*/
void QDeclarativeWebView::setHtml(const QString &html, const QUrl &baseUrl)
{
Q_D(QDeclarativeWebView);
page()->setViewportSize(QSize(
d->preferredwidth>0 ? d->preferredwidth : width(),
d->preferredheight>0 ? d->preferredheight : height()));
if (isComponentComplete())
page()->mainFrame()->setHtml(html, baseUrl);
else {
d->pending = d->PendingHtml;
d->pending_url = baseUrl;
d->pending_string = html;
}
emit htmlChanged();
}
void QDeclarativeWebView::setContent(const QByteArray &data, const QString &mimeType, const QUrl &baseUrl)
{
Q_D(QDeclarativeWebView);
page()->setViewportSize(QSize(
d->preferredwidth>0 ? d->preferredwidth : width(),
d->preferredheight>0 ? d->preferredheight : height()));
if (isComponentComplete())
page()->mainFrame()->setContent(data,mimeType,qmlContext(this)->resolvedUrl(baseUrl));
else {
d->pending = d->PendingContent;
d->pending_url = baseUrl;
d->pending_string = mimeType;
d->pending_data = data;
}
}
QWebHistory *QDeclarativeWebView::history() const
{
return page()->history();
}
QWebSettings *QDeclarativeWebView::settings() const
{
return page()->settings();
}
QDeclarativeWebView *QDeclarativeWebView::createWindow(QWebPage::WebWindowType type)
{
Q_D(QDeclarativeWebView);
switch (type) {
case QWebPage::WebBrowserWindow: {
if (!d->newWindowComponent && d->newWindowParent)
qWarning("WebView::newWindowComponent not set - WebView::newWindowParent ignored");
else if (d->newWindowComponent && !d->newWindowParent)
qWarning("WebView::newWindowParent not set - WebView::newWindowComponent ignored");
else if (d->newWindowComponent && d->newWindowParent) {
QDeclarativeWebView *webview = 0;
QDeclarativeContext *windowContext = new QDeclarativeContext(qmlContext(this));
QObject *nobj = d->newWindowComponent->create(windowContext);
if (nobj) {
windowContext->setParent(nobj);
QDeclarativeItem *item = qobject_cast<QDeclarativeItem *>(nobj);
if (!item) {
delete nobj;
} else {
webview = item->findChild<QDeclarativeWebView*>();
if (!webview) {
delete item;
} else {
nobj->setParent(d->newWindowParent);
static_cast<QGraphicsObject*>(item)->setParentItem(d->newWindowParent);
}
}
} else {
delete windowContext;
}
return webview;
}
}
break;
case QWebPage::WebModalDialog: {
// Not supported
}
}
return 0;
}
/*!
\qmlproperty component WebView::newWindowComponent
This property holds the component to use for new windows.
The component must have a WebView somewhere in its structure.
When the web engine requests a new window, it will be an instance of
this component.
The parent of the new window is set by newWindowParent. It must be set.
*/
QDeclarativeComponent *QDeclarativeWebView::newWindowComponent() const
{
Q_D(const QDeclarativeWebView);
return d->newWindowComponent;
}
void QDeclarativeWebView::setNewWindowComponent(QDeclarativeComponent *newWindow)
{
Q_D(QDeclarativeWebView);
if (newWindow == d->newWindowComponent)
return;
d->newWindowComponent = newWindow;
emit newWindowComponentChanged();
}
/*!
\qmlproperty item WebView::newWindowParent
The parent item for new windows.
\sa newWindowComponent
*/
QDeclarativeItem *QDeclarativeWebView::newWindowParent() const
{
Q_D(const QDeclarativeWebView);
return d->newWindowParent;
}
void QDeclarativeWebView::setNewWindowParent(QDeclarativeItem *parent)
{
Q_D(QDeclarativeWebView);
if (parent == d->newWindowParent)
return;
if (d->newWindowParent && parent) {
QList<QGraphicsItem *> children = d->newWindowParent->childItems();
for (int i = 0; i < children.count(); ++i) {
children.at(i)->setParentItem(parent);
}
}
d->newWindowParent = parent;
emit newWindowParentChanged();
}
/*!
Returns the area of the largest element at position (\a x,\a y) that is no larger
than \a maxwidth by \a maxheight pixels.
May return an area larger in the case when no smaller element is at the position.
*/
QRect QDeclarativeWebView::elementAreaAt(int x, int y, int maxwidth, int maxheight) const
{
QWebHitTestResult hit = page()->mainFrame()->hitTestContent(QPoint(x,y));
QRect rv = hit.boundingRect();
QWebElement element = hit.enclosingBlockElement();
if (maxwidth<=0) maxwidth = INT_MAX;
if (maxheight<=0) maxheight = INT_MAX;
while (!element.parent().isNull() && element.geometry().width() <= maxwidth && element.geometry().height() <= maxheight) {
rv = element.geometry();
element = element.parent();
}
return rv;
}
/*!
\internal
\class QDeclarativeWebPage
\brief The QDeclarativeWebPage class is a QWebPage that can create QML plugins.
\sa QDeclarativeWebView
*/
QDeclarativeWebPage::QDeclarativeWebPage(QDeclarativeWebView *parent) :
QWebPage(parent)
{
}
QDeclarativeWebPage::~QDeclarativeWebPage()
{
}
void QDeclarativeWebPage::javaScriptConsoleMessage(const QString& message, int lineNumber, const QString& sourceID)
{
qWarning() << sourceID << ':' << lineNumber << ':' << message;
}
QString QDeclarativeWebPage::chooseFile(QWebFrame *originatingFrame, const QString& oldFile)
{
// Not supported (it's modal)
Q_UNUSED(originatingFrame)
Q_UNUSED(oldFile)
return oldFile;
}
void QDeclarativeWebPage::javaScriptAlert(QWebFrame *originatingFrame, const QString& msg)
{
Q_UNUSED(originatingFrame)
emit viewItem()->alert(msg);
}
bool QDeclarativeWebPage::javaScriptConfirm(QWebFrame *originatingFrame, const QString& msg)
{
// Not supported (it's modal)
Q_UNUSED(originatingFrame)
Q_UNUSED(msg)
return false;
}
bool QDeclarativeWebPage::javaScriptPrompt(QWebFrame *originatingFrame, const QString& msg, const QString& defaultValue, QString* result)
{
// Not supported (it's modal)
Q_UNUSED(originatingFrame)
Q_UNUSED(msg)
Q_UNUSED(defaultValue)
Q_UNUSED(result)
return false;
}
/*
Qt WebKit does not understand non-QWidget plugins, so dummy widgets
are created, parented to a single dummy tool window.
The requirements for QML object plugins are input to the Qt WebKit
non-QWidget plugin support, which will obsolete this kludge.
*/
class QWidget_Dummy_Plugin : public QWidget
{
Q_OBJECT
public:
static QWidget *dummy_shared_parent()
{
static QWidget *dsp = 0;
if (!dsp) {
dsp = new QWidget(0,Qt::Tool);
dsp->setGeometry(-10000,-10000,0,0);
dsp->show();
}
return dsp;
}
QWidget_Dummy_Plugin(const QUrl& url, QDeclarativeWebView *view, const QStringList ¶mNames, const QStringList ¶mValues) :
QWidget(dummy_shared_parent()),
propertyNames(paramNames),
propertyValues(paramValues),
webview(view)
{
QDeclarativeEngine *engine = qmlEngine(webview);
component = new QDeclarativeComponent(engine, url, this);
item = 0;
if (component->isLoading())
connect(component, SIGNAL(statusChanged(QDeclarativeComponent::Status)), this, SLOT(qmlLoaded()));
else
qmlLoaded();
}
public Q_SLOTS:
void qmlLoaded()
{
if (component->isError()) {
// ### Could instead give these errors to the WebView to handle.
qWarning() << component->errors();
return;
}
item = qobject_cast<QDeclarativeItem*>(component->create(qmlContext(webview)));
item->setParent(webview);
QString jsObjName;
for (int i=0; i<propertyNames.count(); ++i) {
if (propertyNames[i] != QLatin1String("type") && propertyNames[i] != QLatin1String("data")) {
item->setProperty(propertyNames[i].toUtf8(),propertyValues[i]);
if (propertyNames[i] == QLatin1String("objectname"))
jsObjName = propertyValues[i];
}
}
if (!jsObjName.isNull()) {
QWebFrame *f = webview->page()->mainFrame();
f->addToJavaScriptWindowObject(jsObjName, item);
}
resizeEvent(0);
delete component;
component = 0;
}
void resizeEvent(QResizeEvent*)
{
if (item) {
item->setX(x());
item->setY(y());
item->setWidth(width());
item->setHeight(height());
}
}
private:
QDeclarativeComponent *component;
QDeclarativeItem *item;
QStringList propertyNames, propertyValues;
QDeclarativeWebView *webview;
};
QDeclarativeWebView *QDeclarativeWebPage::viewItem()
{
return static_cast<QDeclarativeWebView*>(parent());
}
QObject *QDeclarativeWebPage::createPlugin(const QString &, const QUrl &url, const QStringList ¶mNames, const QStringList ¶mValues)
{
QUrl comp = qmlContext(viewItem())->resolvedUrl(url);
return new QWidget_Dummy_Plugin(comp,viewItem(),paramNames,paramValues);
}
QWebPage *QDeclarativeWebPage::createWindow(WebWindowType type)
{
QDeclarativeWebView *newView = viewItem()->createWindow(type);
if (newView)
return newView->page();
return 0;
}
QT_END_NAMESPACE
#include <qdeclarativewebview.moc>
|