add some file
[mdictionary] / src / mdictionary / qml / FlickableWebView.qml
1 /****************************************************************************
2 **
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** Commercial Usage
11 ** Licensees holding valid Qt Commercial licenses may use this file in
12 ** accordance with the Qt Commercial License Agreement provided with the
13 ** Software or, alternatively, in accordance with the terms contained in
14 ** a written agreement between you and Nokia.
15 **
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file.  Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23 **
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27 **
28 ** GNU General Public License Usage
29 ** Alternatively, this file may be used under the terms of the GNU
30 ** General Public License version 3.0 as published by the Free Software
31 ** Foundation and appearing in the file LICENSE.GPL included in the
32 ** packaging of this file.  Please review the following information to
33 ** ensure the GNU General Public License version 3.0 requirements will be
34 ** met: http://www.gnu.org/copyleft/gpl.html.
35 **
36 ** If you have questions regarding the use of this file, please contact
37 ** Nokia at qt-info@nokia.com.
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 import Qt 4.7
43 import QtWebKit 1.0
44
45 Flickable {
46     id: flickable
47     width: parent.width
48     contentWidth: Math.max(parent.width,webView.width)
49     contentHeight: Math.max(parent.height,webView.height)
50     anchors.top: headerSpace.bottom
51     anchors.bottom: parent.top
52     anchors.left: parent.left
53     anchors.right: parent.right
54     pressDelay: 200
55     onWidthChanged : {
56         if (width > webView.width*webView.contentsScale && webView.contentsScale < 1.0)
57             webView.contentsScale = width / webView.width * webView.contentsScale;
58     }
59
60     property alias url: webView.url
61
62     function doZoom(variable)   { webView.doZoom(variable,1,1) }
63
64     WebView {
65         id: webView
66         transformOrigin: Item.TopLeft
67         url: flickable.url
68         smooth: false
69         focus: true
70         preferredWidth: flickable.width
71         preferredHeight: flickable.height
72         contentsScale: 1
73         pressGrabTime: 0
74
75         function doZoom(zoom,centerX,centerY)
76         {
77             var tempX = flickable.width/2 - parent.x*zoom;
78             var tempY = flickable.height/2 - parent.y*zoom;
79             if (centerX) {
80                 var sc = zoom*contentsScale;
81                 scaleAnim.to = sc;
82                 flickVX.from = flickable.contentX
83                 flickVX.to = Math.max(0,Math.min(tempX-flickable.width/2,webView.width*sc-flickable.width))
84                 finalX.value = flickVX.to
85                 flickVY.from = flickable.contentY
86                 flickVY.to = Math.max(0,Math.min(tempY-flickable.height/2,webView.height*sc-flickable.height))
87                 finalY.value = flickVY.to
88                 quickZoom.start()
89             }
90         }
91
92        onContentsSizeChanged: {
93            contentsScale = Math.min(1,flickable.width / contentsSize.width)
94        }
95
96        onDoubleClick: {
97             if (!heuristicZoom(clickX,clickY,2.5)) {
98                 var zf = flickable.width / contentsSize.width
99                 if (zf >= contentsScale)
100                     zf = 2.0/zoomFactor // zoom in (else zooming out)
101                 doZoom(zf,clickX*zf,clickY*zf)
102             }
103         }
104
105         SequentialAnimation {
106             id: quickZoom
107
108             PropertyAction {
109                 target: webView
110                 property: "renderingEnabled"
111                 value: false
112             }
113             ParallelAnimation {
114                 NumberAnimation {
115                     id: scaleAnim
116                     target: webView
117                     property: "contentsScale"
118                     easing.type: Easing.Linear
119                     duration: 200
120                 }
121                 NumberAnimation {
122                     id: flickVX
123                     target: flickable
124                     property: "contentX"
125                     easing.type: Easing.Linear
126                     duration: 200
127                     from: 0
128                     to: 0
129                 }
130                 NumberAnimation {
131                     id: flickVY
132                     target: flickable
133                     property: "contentY"
134                     easing.type: Easing.Linear
135                     duration: 200
136                     from: 0
137                     to: 0
138                 }
139             }
140             PropertyAction {
141                 id: finalX
142                 target: flickable
143                 property: "contentX"
144                 value: 0
145             }
146             PropertyAction {
147                 id: finalY
148                 target: flickable
149                 property: "contentY"
150                 value: 0
151             }
152             PropertyAction {
153                 target: webView
154                 property: "renderingEnabled"
155                 value: true
156             }
157         }
158         onZoomTo: doZoom(zoom,centerX,centerY)
159     }
160 }