summaryrefslogtreecommitdiffstats
path: root/src/declarative/fx/qfxwebview.cpp
blob: eb2f7d238bed46d98fcd031c3cbe5265677d3afe (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
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
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Qt Software Information (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 either Technology Preview License Agreement or the
** Beta Release License Agreement.
**
** 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.0, included in the file LGPL_EXCEPTION.txt in this
** package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
** $QT_END_LICENSE$
**
****************************************************************************/

#include <QDebug>
#include <QPen>
#include <QFile>
#include <QEvent>
#include <QBasicTimer>
#include <QApplication>
#include <QGraphicsSceneMouseEvent>
#include <QtWebKit/QWebPage>
#include <QtWebKit/QWebFrame>

#include "qml.h"
#include "qmlbindablevalue.h"
#include "qmlengine.h"
#include "qmlstate.h"
#include "qfxtransform.h"
#include "qfxscalegrid.h"
#include "qsimplecanvas.h"
#include "qlistmodelinterface.h"

#if defined(QFX_RENDER_OPENGL2)
#include <QtOpenGL/qglframebufferobject.h>
#include <glsave.h>
#endif
#if defined(QFX_RENDER_OPENGL)
#include <gltexture.h>
#endif

#include "qfxwebview.h"
#include <qsimplecanvasfilter.h>
#include <private/qfxitem_p.h>

QT_BEGIN_NAMESPACE
QML_DEFINE_TYPE(QFxWebView,WebView);

static const int MAX_DOUBLECLICK_TIME=500; // XXX need better gesture system

class QFxWebSettings : public QObject {
    Q_OBJECT
    /*
        StandardFont,
        FixedFont,
        SerifFont,
        SansSerifFont,
        CursiveFont,
        FantasyFont

        MinimumFontSize,
        MinimumLogicalFontSize,
        DefaultFontSize,
        DefaultFixedFontSize
    */

    Q_PROPERTY(bool autoLoadImages READ autoLoadImages WRITE setAutoLoadImages)
    Q_PROPERTY(bool javascriptEnabled READ javascriptEnabled WRITE setJavascriptEnabled)
    Q_PROPERTY(bool javaEnabled READ javaEnabled WRITE setJavaEnabled)
    Q_PROPERTY(bool pluginsEnabled READ pluginsEnabled WRITE setPluginsEnabled)
    Q_PROPERTY(bool privateBrowsingEnabled READ privateBrowsingEnabled WRITE setPrivateBrowsingEnabled)
    Q_PROPERTY(bool javascriptCanOpenWindows READ javascriptCanOpenWindows WRITE setJavascriptCanOpenWindows)
    Q_PROPERTY(bool javascriptCanAccessClipboard READ javascriptCanAccessClipboard WRITE setJavascriptCanAccessClipboard)
    Q_PROPERTY(bool developerExtrasEnabled READ developerExtrasEnabled WRITE setDeveloperExtrasEnabled)
    Q_PROPERTY(bool linksIncludedInFocusChain READ linksIncludedInFocusChain WRITE setLinksIncludedInFocusChain)
    Q_PROPERTY(bool zoomTextOnly READ zoomTextOnly WRITE setZoomTextOnly)
    Q_PROPERTY(bool printElementBackgrounds READ printElementBackgrounds WRITE setPrintElementBackgrounds)
    Q_PROPERTY(bool offlineStorageDatabaseEnabled READ offlineStorageDatabaseEnabled WRITE setOfflineStorageDatabaseEnabled)
    Q_PROPERTY(bool offlineWebApplicationCacheEnabled READ offlineWebApplicationCacheEnabled WRITE setOfflineWebApplicationCacheEnabled)
    Q_PROPERTY(bool localStorageDatabaseEnabled READ localStorageDatabaseEnabled WRITE setLocalStorageDatabaseEnabled)

    Q_PROPERTY(QString userStyleSheetUrl READ userStyleSheetUrl WRITE setUserStyleSheetUrl)

public:
    QFxWebSettings() {}

    bool autoLoadImages() const { return s->testAttribute(QWebSettings::AutoLoadImages); }
    void setAutoLoadImages(bool on) { s->setAttribute(QWebSettings::AutoLoadImages, on); }
    bool javascriptEnabled() const { return s->testAttribute(QWebSettings::JavascriptEnabled); }
    void setJavascriptEnabled(bool on) { s->setAttribute(QWebSettings::JavascriptEnabled, on); }
    bool javaEnabled() const { return s->testAttribute(QWebSettings::JavaEnabled); }
    void setJavaEnabled(bool on) { s->setAttribute(QWebSettings::JavaEnabled, on); }
    bool pluginsEnabled() const { return s->testAttribute(QWebSettings::PluginsEnabled); }
    void setPluginsEnabled(bool on) { s->setAttribute(QWebSettings::PluginsEnabled, on); }
    bool privateBrowsingEnabled() const { return s->testAttribute(QWebSettings::PrivateBrowsingEnabled); }
    void setPrivateBrowsingEnabled(bool on) { s->setAttribute(QWebSettings::PrivateBrowsingEnabled, on); }
    bool javascriptCanOpenWindows() const { return s->testAttribute(QWebSettings::JavascriptCanOpenWindows); }
    void setJavascriptCanOpenWindows(bool on) { s->setAttribute(QWebSettings::JavascriptCanOpenWindows, on); }
    bool javascriptCanAccessClipboard() const { return s->testAttribute(QWebSettings::JavascriptCanAccessClipboard); }
    void setJavascriptCanAccessClipboard(bool on) { s->setAttribute(QWebSettings::JavascriptCanAccessClipboard, on); }
    bool developerExtrasEnabled() const { return s->testAttribute(QWebSettings::DeveloperExtrasEnabled); }
    void setDeveloperExtrasEnabled(bool on) { s->setAttribute(QWebSettings::DeveloperExtrasEnabled, on); }
    bool linksIncludedInFocusChain() const { return s->testAttribute(QWebSettings::LinksIncludedInFocusChain); }
    void setLinksIncludedInFocusChain(bool on) { s->setAttribute(QWebSettings::LinksIncludedInFocusChain, on); }
    bool zoomTextOnly() const { return s->testAttribute(QWebSettings::ZoomTextOnly); }
    void setZoomTextOnly(bool on) { s->setAttribute(QWebSettings::ZoomTextOnly, on); }
    bool printElementBackgrounds() const { return s->testAttribute(QWebSettings::PrintElementBackgrounds); }
    void setPrintElementBackgrounds(bool on) { s->setAttribute(QWebSettings::PrintElementBackgrounds, on); }
    bool offlineStorageDatabaseEnabled() const { return s->testAttribute(QWebSettings::OfflineStorageDatabaseEnabled); }
    void setOfflineStorageDatabaseEnabled(bool on) { s->setAttribute(QWebSettings::OfflineStorageDatabaseEnabled, on); }
    bool offlineWebApplicationCacheEnabled() const { return s->testAttribute(QWebSettings::OfflineWebApplicationCacheEnabled); }
    void setOfflineWebApplicationCacheEnabled(bool on) { s->setAttribute(QWebSettings::OfflineWebApplicationCacheEnabled, on); }
    bool localStorageDatabaseEnabled() const { return s->testAttribute(QWebSettings::LocalStorageDatabaseEnabled); }
    void setLocalStorageDatabaseEnabled(bool on) { s->setAttribute(QWebSettings::LocalStorageDatabaseEnabled, on); }

    QString userStyleSheetUrl() const { return s->userStyleSheetUrl(); }
    void setUserStyleSheetUrl(const QString& url) { s->setUserStyleSheetUrl(url); }

    QWebSettings *s;
};


class QFxWebViewPrivate : public QFxItemPrivate
{
    Q_DECLARE_PUBLIC(QFxWebView)

public:
    QFxWebViewPrivate()
      : page(0), idealwidth(0), idealheight(0), interactive(true), lastPress(0), lastRelease(0), mouseX(0), mouseY(0),
            smooth(true), max_imagecache_size(100000), progress(1.0), pending(PendingNone)
    {
    }

    QWebPage *page;

    struct ImageCacheItem {
        ImageCacheItem() : age(0) {}
        ~ImageCacheItem() { }
        int age;
        QRect area;
#if defined(QFX_RENDER_QPAINTER)
        QSimpleCanvasConfig::Image image;
#else
        GLTexture image;
#endif
    };
    QList<ImageCacheItem*> imagecache;
    void dirtyCache(const QRect& dirt)
    {
        for (int i=0; i<imagecache.count(); ) {
            if (imagecache[i]->area.intersects(dirt)) {
                imagecache.removeAt(i);
            } else {
                ++i;
            }
        }
    }
    void clearCache()
    {
        foreach (ImageCacheItem* i, imagecache)
            delete i;
        imagecache.clear();
    }

    int idealwidth;
    int idealheight;
    bool interactive;
    QMouseEvent *lastPress, *lastRelease;
    int mouseX, mouseY;
    bool smooth;
    int max_imagecache_size;
    qreal progress;
    QBasicTimer dcTimer;
    QString statusBarMessage;
    enum { PendingNone, PendingUrl, PendingHtml, PendingContent } pending;
    QUrl pending_url;
    QString pending_string;
    QByteArray pending_data;
    mutable QFxWebSettings settings;
};


/*!
    \qmlclass WebView
    \brief The WebView element 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 element is not set, they will
    dynamically adjust to a size appropriate for the content.
    This width may be large (eg. 980) for typical online web pages.

    If the idealWidth is set, the width will be this amount or larger,
    usually laying out the web content to fit the idealWidth.

    If the idealHeight is set, the height will be this amount or larger.
    Due to WebKit limitations, the height may be more than necessary
    if the idealHeight is changed after the content is loaded.

    \code
    WebView {
        url: "http://www.nokia.com"
        width: 490
        height: 400
        scale: 0.5
        smooth: true
    }
    \endcode

    \image webview.png

    The element includes no scrolling, scaling,
    toolbars, etc., those must be implemented around WebView. See the WebBrowser example
    for a demonstration of this.
*/

/*!
    \internal
    \class QFxWebView
    \brief The QFxWebView class allows you to add web content to a QFxView.

    A WebView renders web content base on a URL.

    \image webview.png

    The element includes no scrolling, scaling,
    toolbars, etc., those must be implemented around WebView. See the WebBrowser example
    for a demonstration of this.

    A QFxWebView object can be instantiated in Qml using the tag \l WebView.
*/

QFxWebView::QFxWebView(QFxItem *parent)
  : QFxItem(*(new QFxWebViewPrivate), parent)
{
    init();
}

QFxWebView::QFxWebView(QFxWebViewPrivate &dd, QFxItem *parent)
  : QFxItem(dd, parent)
{
    init();
}

QFxWebView::~QFxWebView()
{
    Q_D(QFxWebView);
    delete d->page;
}

void QFxWebView::init()
{
    Q_D(QFxWebView);

    setAcceptedMouseButtons(Qt::LeftButton);
    setOptions(HasContents | MouseEvents);
    setFocusable(true);

    d->page = 0;
}

void QFxWebView::componentComplete()
{
    QFxItem::componentComplete();
    Q_D(QFxWebView);
    switch (d->pending) {
        case QFxWebViewPrivate::PendingUrl:
            setUrl(d->pending_url.toString());
            break;
        case QFxWebViewPrivate::PendingHtml:
            setHtml(d->pending_string, d->pending_url);
            break;
        case QFxWebViewPrivate::PendingContent:
            setContent(d->pending_data, d->pending_string, d->pending_url);
            break;
        default:
            break;
    }
    d->pending = QFxWebViewPrivate::PendingNone;
}

/*!
    \qmlproperty real WebView::progress
    This property holds the progress of loading the current URL, from 0 to 1.
*/
/*!
    \property QFxWebView::progress
    \brief the progress of loading the current URL, from 0 to 1.
*/
qreal QFxWebView::progress() const
{
    Q_D(const QFxWebView);
    return d->progress;
}

void QFxWebView::doLoadProgress(int p)
{
    Q_D(QFxWebView);
    if (d->progress == p/100.0)
        return;
    d->progress = p/100.0;
    expandToWebPage();
    emit progressChanged();
}

void QFxWebView::doLoadFinished(bool ok)
{
    // XXX bug 232556 - pages with no title never get this signal
    if (title().isEmpty())
        emit urlChanged();

    if (ok) {
        emit loadFinished();
    } else {
        emit loadFailed();
    }
}

/*!
    \qmlproperty string WebView::url
    This property holds the URL to the page displayed in this item.

    Note that after this property is set, it may take some time
    before the change is notified, as this only happens when
    loading of the URL successfully starts.
*/
/*!
    \property QFxWebView::url
    \brief the URL to the page displayed in this item.

    \sa urlChanged()
*/
/*!
    \fn void QFxWebView::urlChanged()

    Emitted when loading of the URL successfully starts after
    setUrl() is called.
*/
QString QFxWebView::url() const
{
    return page()->mainFrame()->url().toString();
}

void QFxWebView::setUrl(const QString &n)
{
    Q_D(QFxWebView);
    if (n == page()->mainFrame()->url().toString())
        return;

    page()->setViewportSize(QSize(
        d->idealwidth>0 ? d->idealwidth : width(),
        d->idealheight>0 ? d->idealheight : height()));

    QUrl url(n);
    if (url.isRelative())
        url = qmlContext(this)->resolvedUrl(n);

    if (isComponentComplete())
        page()->mainFrame()->load(url);
    else {
        d->pending = d->PendingUrl;
        d->pending_url = url;
    }

    // emit urlChanged() - not until actually loaded
}

/*!
    \qmlproperty int WebView::idealWidth
    This property holds the ideal width for displaying the current URL.
*/
/*!
    \property QFxWebView::idealWidth
    \brief the ideal width for displaying the current URL.
*/
int QFxWebView::idealWidth() const
{
    Q_D(const QFxWebView);
    return d->idealwidth;
}

void QFxWebView::setIdealWidth(int iw)
{
    Q_D(QFxWebView);
    if (d->idealwidth == iw) return;
    d->idealwidth = iw;
    expandToWebPage();
    emit idealWidthChanged();
}

/*!
    \qmlproperty int WebView::idealHeight
    This property holds the ideal height for displaying the current URL.
*/
/*!
    \property QFxWebView::idealHeight
    \brief the ideal height for displaying the current URL.
*/
int QFxWebView::idealHeight() const
{
    Q_D(const QFxWebView);
    return d->idealheight;
}

void QFxWebView::setIdealHeight(int ih)
{
    Q_D(QFxWebView);
    if (d->idealheight == ih) return;
    d->idealheight = ih;
    expandToWebPage();
    emit idealHeightChanged();
}

/*!
  \qmlproperty bool WebView::interactive

  This property holds controls whether the item responds to mouse and
  key events.
*/

/*!
  \property QFxWebView::interactive

  \brief controls whether the item responds to mouse and key events.
*/
bool QFxWebView::interactive() const
{
    Q_D(const QFxWebView);
    return d->interactive;
}

void QFxWebView::setInteractive(bool i)
{
    Q_D(QFxWebView);
    if (d->interactive == i) return;
    d->interactive = i;
    emit interactiveChanged();
}

/*!
    \qmlproperty bool WebView::smooth
    This property holds hints as to whether the item should be drawn anti-aliased.
*/
/*!
    \property QFxWebView::smooth
    \brief hints as to whether the item should be drawn anti-aliased.
*/
bool QFxWebView::smooth() const
{
    Q_D(const QFxWebView);
    return d->smooth;
}

void QFxWebView::setSmooth(bool i)
{
    Q_D(QFxWebView);
    if (d->smooth == i) return;
    d->smooth = i;
    update();
}

void QFxWebView::updateCacheForVisibility()
{
    Q_D(QFxWebView);
    if (!isVisible())
        d->clearCache();
}

void QFxWebView::expandToWebPage()
{
    Q_D(QFxWebView);
    QSize cs = page()->mainFrame()->contentsSize();
    if (cs.width() < d->idealwidth)
        cs.setWidth(d->idealwidth);
    if (cs.height() < d->idealheight)
        cs.setHeight(d->idealheight);
    if (widthValid() && cs.width() < width())
        cs.setWidth(width());
    if (heightValid() && cs.height() < height())
        cs.setHeight(height());
    if (cs != page()->viewportSize()) {
        page()->setViewportSize(cs);
        d->clearCache();
        setImplicitWidth(cs.width());
        setImplicitHeight(cs.height());
    }
}

void QFxWebView::geometryChanged(const QRectF &newGeometry, 
                                 const QRectF &oldGeometry)
{
    if (newGeometry.size() != oldGeometry.size())
        expandToWebPage();
    QFxItem::geometryChanged(newGeometry, oldGeometry);
}

void QFxWebView::paintPage(const QRect& r)
{
    Q_D(QFxWebView);
    d->dirtyCache(r);
    update();
}

/*!
  \qmlproperty int WebView::cacheSize

  This property holds the maximum number of pixels of image cache to
  allow. The default is 0.1 megapixels. The cache will not be larger
  than the (unscaled) size of the WebView.
*/

/*!
  \property QFxWebView::cacheSize

  The maximum number of pixels of image cache to allow. The default
  is 0.1 megapixels. The cache will not be larger than the (unscaled)
  size of the QFxWebView.
*/
int QFxWebView::cacheSize() const
{
    Q_D(const QFxWebView);
    return d->max_imagecache_size;
}

void QFxWebView::setCacheSize(int pixels)
{
    Q_D(QFxWebView);
    if (pixels < d->max_imagecache_size) {
        int cachesize=0;
        for (int i=0; i<d->imagecache.count(); ++i) {
            QRect area = d->imagecache[i]->area;
            cachesize += area.width()*area.height();
        }
        while (d->imagecache.count() && cachesize > pixels) {
            int oldest=-1;
            int age=-1;
            for (int i=0; i<d->imagecache.count(); ++i) {
                int a = d->imagecache[i]->age;
                if (a > age) {
                    oldest = i;
                    age = a;
                }
            }
            cachesize -= d->imagecache[oldest]->area.width()*d->imagecache[oldest]->area.height();
            d->imagecache.removeAt(oldest);
        }
    }
    d->max_imagecache_size = pixels;
}

void QFxWebView::dump(int depth)
{
    QByteArray ba(depth * 4, ' ');
    qWarning() << ba.constData() << "url:" << url();
    QFxItem::dump(depth);
}

#if defined(QFX_RENDER_QPAINTER) 
void QFxWebView::paintContents(QPainter &p)
#elif defined(QFX_RENDER_OPENGL)
void QFxWebView::paintGLContents(GLPainter &p)
#else
#error "What render?"
#endif
{
    Q_D(QFxWebView);
    QWebFrame *frame = page()->mainFrame();
    const QRect content(QPoint(0,0),frame->contentsSize());

    if (content.width() <= 0 || content.height() <= 0)
        return;

#if defined(QFX_RENDER_QPAINTER)
    bool wasAA = p.testRenderHint(QPainter::Antialiasing);
    bool wasSM = p.testRenderHint(QPainter::SmoothPixmapTransform);
    p.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform, d->smooth);
    QRectF clipf = p.clipRegion().boundingRect();
    const QRect clip = p.clipRegion().isEmpty() ? content : clipf.toRect();
#elif defined(QFX_RENDER_OPENGL)
    const QRectF clipf = p.sceneClipRect;
    const QRect clip = mapFromScene(clipf).toRect();
#endif

    QRegion topaint(clip);
    topaint &= content;
    QRegion uncached(content);

    int cachesize=0;
    for (int i=0; i<d->imagecache.count(); ++i) {
        QRect area = d->imagecache[i]->area;
        if (topaint.contains(area)) {
            p.drawImage(area, d->imagecache[i]->image);
            topaint -= area;
            d->imagecache[i]->age=0;
        } else {
            d->imagecache[i]->age++;
        }
        cachesize += area.width()*area.height();
        uncached -= area;
    }

    if (!topaint.isEmpty()) {
        // Find a sensible larger area, otherwise will paint lots of tiny images.
        QRect biggerrect = topaint.boundingRect().adjusted(-64,-64,128,128);
        cachesize += biggerrect.width() * biggerrect.height();
        while (d->imagecache.count() && cachesize > d->max_imagecache_size) {
            int oldest=-1;
            int age=-1;
            for (int i=0; i<d->imagecache.count(); ++i) {
                int a = d->imagecache[i]->age;
                if (a > age) {
                    oldest = i;
                    age = a;
                }
            }
            cachesize -= d->imagecache[oldest]->area.width()*d->imagecache[oldest]->area.height();
            uncached += d->imagecache[oldest]->area;
            d->imagecache.removeAt(oldest);
        }
        const QRegion bigger = QRegion(biggerrect) & uncached;
        const QVector<QRect> rects = bigger.rects();
        foreach (QRect r, rects) {
            QImage img(r.size(),QImage::Format_ARGB32_Premultiplied);
            img.fill(0);
            {
                QPainter qp(&img);
                qp.translate(-r.x(),-r.y());
                frame->render(&qp,r);
            }
            QFxWebViewPrivate::ImageCacheItem *newitem = new QFxWebViewPrivate::ImageCacheItem;
            newitem->area = r;
#if defined(QFX_RENDER_QPAINTER)
            newitem->image = QSimpleCanvasConfig::Image(QSimpleCanvasConfig::toImage(img));
#else
            newitem->image.setImage(img);
#endif
            d->imagecache.append(newitem);
            p.drawImage(r, newitem->image);
        }
    }
#if defined(QFX_RENDER_QPAINTER)
    p.setRenderHints(QPainter::Antialiasing, wasAA);
    p.setRenderHints(QPainter::SmoothPixmapTransform, wasSM);
#endif
}

QString QFxWebView::propertyInfo() const
{
    return page()->mainFrame()->url().toString();
}

static QMouseEvent *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().toPoint(), e->button(), e->buttons(), 0);
    return me;
}


void QFxWebView::timerEvent(QTimerEvent *event)
{
    Q_D(QFxWebView);
    if (event->timerId() ==d->dcTimer.timerId()) {
        d->dcTimer.stop();
        if (d->lastPress) {
            page()->event(d->lastPress);
            delete d->lastPress;
            d->lastPress = 0;
        }
        if (d->lastRelease) {
            page()->event(d->lastRelease);
            delete d->lastRelease;
            d->lastRelease = 0;
        }
    }
}

int QFxWebView::mouseX() const
{
    Q_D(const QFxWebView);
    if (d->lastPress)
        return d->lastPress->x();
    if (d->lastRelease)
        return d->lastRelease->x();
    return d->mouseX;
}

int QFxWebView::mouseY() const
{
    Q_D(const QFxWebView);
    if (d->lastPress)
        return d->lastPress->y();
    if (d->lastRelease)
        return d->lastRelease->y();
    return d->mouseY;
}

void QFxWebView::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
{
    QMouseEvent *me = sceneMouseEventToMouseEvent(event);
    Q_D(QFxWebView);
    d->dcTimer.stop();
    delete d->lastPress;
    delete d->lastRelease;
    d->lastPress = 0;
    d->lastRelease = 0;
    d->mouseX = me->x();
    d->mouseY = me->y();
    emit doubleClick();
    d->mouseX = 0;
    d->mouseY = 0;
    delete me;
}

void QFxWebView::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    Q_D(QFxWebView);
    if (d->interactive) {
        d->lastPress = sceneMouseEventToMouseEvent(event);
        event->setAccepted(true);
    } else {
        event->setAccepted(false);
    }
    if (!event->isAccepted())
        QFxItem::mousePressEvent(event);
}

void QFxWebView::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    Q_D(QFxWebView);
    if (d->interactive) {
        d->lastRelease = sceneMouseEventToMouseEvent(event);
        d->dcTimer.start(MAX_DOUBLECLICK_TIME,this);
        event->setAccepted(true);
    } else {
        event->setAccepted(false);
    }
    if (!event->isAccepted())
        QFxItem::mouseReleaseEvent(event);
}

void QFxWebView::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    Q_D(const QFxWebView);
    if (d->interactive && !d->dcTimer.isActive()) {
        QMouseEvent *me = sceneMouseEventToMouseEvent(event);
        page()->event(me);
        event->setAccepted(
#if QT_VERSION <= 0x040500 // XXX see bug 230835
            true
#else
            me->isAccepted()
#endif
        );
        delete me;
    } else {
        event->setAccepted(false);
    }
    if (!event->isAccepted())
        QFxItem::mouseMoveEvent(event);
}

void QFxWebView::keyPressEvent(QKeyEvent* event)
{
    Q_D(const QFxWebView);
    if (d->interactive)
        page()->event(event);
    if (!event->isAccepted())
        QFxItem::keyPressEvent(event);
}

void QFxWebView::keyReleaseEvent(QKeyEvent* event)
{
    Q_D(const QFxWebView);
    if (d->interactive)
        page()->event(event);
    if (!event->isAccepted())
        QFxItem::keyReleaseEvent(event);
}

/*!
    \qmlproperty action WebView::back
    This property holds the action for causing the previous URL in the history to be displayed.
*/
QAction *QFxWebView::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 *QFxWebView::forwardAction() const
{
    return page()->action(QWebPage::Forward);
}

/*!
    \qmlproperty action WebView::reload
    This property holds the action for reloading with the current URL
*/
QAction *QFxWebView::reloadAction() const
{
    return page()->action(QWebPage::Reload);
}

/*!
    \qmlproperty action WebView::stop
    This property holds the action for stopping loading with the current URL
*/
QAction *QFxWebView::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.
*/
/*!
    \property QFxWebView::title
    This property holds the title of the web page currently viewed

    By default, this property contains an empty string.

    \sa titleChanged()
*/
QString QFxWebView::title() const
{
    return page()->mainFrame()->title();
}



/*!
    \qmlproperty pixmap WebView::icon
    This property holds the icon associated with the web page currently viewed
*/
/*!
    \property QFxWebView::icon
    \brief the icon associated with the web page currently viewed

    By default, this property contains a null icon.

    \sa iconChanged(), QWebSettings::iconForUrl()
*/
QPixmap QFxWebView::icon() const
{
    return page()->mainFrame()->icon().pixmap(QSize(256,256));
}


/*!
    \qmlproperty real WebView::textSizeMultiplier
    This property holds multiplier used to scale the text in a Web page
*/
/*!
    Sets the value of the multiplier used to scale the text in a Web page to
    the \a factor specified.
*/
void QFxWebView::setTextSizeMultiplier(qreal factor)
{
    page()->mainFrame()->setTextSizeMultiplier(factor);
}

/*!
    Returns the value of the multiplier used to scale the text in a Web page.
*/
qreal QFxWebView::textSizeMultiplier() const
{
    return page()->mainFrame()->textSizeMultiplier();
}

void QFxWebView::setStatusBarMessage(const QString& s)
{
    Q_D(QFxWebView);
    d->statusBarMessage = s;
    emit statusChanged();
}

QString QFxWebView::status() const
{
    Q_D(const QFxWebView);
    return d->statusBarMessage;
}

QWebPage *QFxWebView::page() const
{
    Q_D(const QFxWebView);

    if (!d->page) {
        QFxWebView *self = const_cast<QFxWebView*>(this);
        QWebPage *wp = new QFxWebPage(self);

        // QML elements 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 QWebSettings WebView::settings

    This property gives access to the settings controlling the web view.

    See QWebSettings for the list of sub-properties.

    \code
        WebView {
            settings.pluginsEnabled: true
            settings.userStyleSheetUrl: "mystyle.css"
            ...
        }
    \endcode
*/
QObject *QFxWebView::settingsObject() const
{
    Q_D(const QFxWebView);
    d->settings.s = page()->settings();
    return &d->settings;
}

void QFxWebView::setPage(QWebPage *page)
{
    Q_D(QFxWebView);
    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->idealwidth>0 ? d->idealwidth : -1,
        d->idealheight>0 ? d->idealheight : -1));
    d->page->mainFrame()->setScrollBarPolicy(Qt::Horizontal,Qt::ScrollBarAlwaysOff);
    d->page->mainFrame()->setScrollBarPolicy(Qt::Vertical,Qt::ScrollBarAlwaysOff);
    connect(this,SIGNAL(visibleChanged()),this,SLOT(updateCacheForVisibility()));
    connect(d->page,SIGNAL(repaintRequested(QRect)),this,SLOT(paintPage(QRect)));
    connect(d->page->mainFrame(),SIGNAL(urlChanged(QUrl)),this,SIGNAL(urlChanged()));
    connect(d->page->mainFrame(), SIGNAL(titleChanged(QString)), this, SIGNAL(titleChanged(QString)));
    connect(d->page->mainFrame(), SIGNAL(iconChanged()), this, SIGNAL(iconChanged()));

    connect(d->page,SIGNAL(loadStarted()),this,SIGNAL(loadStarted()));
    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(setStatusBarMessage(QString)));
}

void QFxWebView::load(const QNetworkRequest &request,
          QNetworkAccessManager::Operation operation,
          const QByteArray &body)
{
    page()->mainFrame()->load(request, operation, body);
}

QString QFxWebView::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.

    \code
    WebView {
        html: "<p>This is <b>HTML</b>."
    }
    \endcode
*/
void QFxWebView::setHtml(const QString &html, const QUrl &baseUrl)
{
    Q_D(QFxWebView);
    page()->setViewportSize(QSize(
        d->idealwidth>0 ? d->idealwidth : width(),
        d->idealheight>0 ? d->idealheight : height()));
    if (isComponentComplete())
        page()->mainFrame()->setHtml(html, baseUrl);
    else {
        d->pending = d->PendingHtml;
        d->pending_url = baseUrl;
        d->pending_string = html;
    }
}

void QFxWebView::setContent(const QByteArray &data, const QString &mimeType, const QUrl &baseUrl)
{
    Q_D(QFxWebView);
    page()->setViewportSize(QSize(
        d->idealwidth>0 ? d->idealwidth : width(),
        d->idealheight>0 ? d->idealheight : height()));

    if (isComponentComplete())
        page()->mainFrame()->setContent(data,mimeType,baseUrl);
    else {
        d->pending = d->PendingContent;
        d->pending_url = baseUrl;
        d->pending_string = mimeType;
        d->pending_data = data;
    }
}

QWebHistory *QFxWebView::history() const
{
    return page()->history();
}

QWebSettings *QFxWebView::settings() const
{
    return page()->settings();
}

/*!
    \internal
    \class QFxWebPage
    \brief The QFxWebPage class is a QWebPage that can create QML plugins.

    \sa QFxWebView
*/
QFxWebPage::QFxWebPage(QFxWebView *parent) :
    QWebPage(parent)
{
}

QFxWebPage::~QFxWebPage()
{
}

/*
    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, QFxWebView *view, const QStringList &paramNames, const QStringList &paramValues) :
        QWidget(dummy_shared_parent()),
        propertyNames(paramNames),
        propertyValues(paramValues),
        webview(view)
    {
        QmlEngine *engine = qmlEngine(webview);
        component = new QmlComponent(engine, url, this);
        item = 0;
        if (component->isReady())
            qmlLoaded();
        else
            connect(component, SIGNAL(statusChanged(QmlComponent::Status)), this, SLOT(qmlLoaded()));
    }

public Q_SLOTS:
    void qmlLoaded()
    {
        item = qobject_cast<QFxItem*>(component->create(qmlContext(webview)));
        item->setParent(webview);
        for (int i=0; i<propertyNames.count(); ++i) {
            if (propertyNames[i] != QLatin1String("type") && propertyNames[i] != QLatin1String("data")) {
                item->setProperty(propertyNames[i].toLatin1(),propertyValues[i]);
            }
        }
        resizeEvent(0);
        delete component;
        component = 0;
    }
    void resizeEvent(QResizeEvent*)
    {
        if (item) {
            item->setX(x());
            item->setY(y());
            item->setWidth(width());
            item->setHeight(height());
        }
    }

private:
    QmlComponent *component;
    QFxItem *item;
    QStringList propertyNames, propertyValues;
    QFxWebView *webview;
};

QFxWebView *QFxWebPage::view()
{
    return static_cast<QFxWebView*>(parent());
}

QObject *QFxWebPage::createPlugin(const QString &, const QUrl &url, const QStringList &paramNames, const QStringList &paramValues)
{
    QUrl comp = qmlContext(view())->resolvedUri(url.toString());
    return new QWidget_Dummy_Plugin(comp,view(),paramNames,paramValues);
}

#include "qfxwebview.moc"

QT_END_NAMESPACE