add searchbar icons
[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         Keys.onRightPressed: {
76             console.log("test");
77         }
78
79         function doZoom(zoom,centerX,centerY)
80         {
81             var tempX = flickable.width/2 - parent.x*zoom;
82             var tempY = flickable.height/2 - parent.y*zoom;
83             if (centerX) {
84                 var sc = zoom*contentsScale;
85                 scaleAnim.to = sc;
86                 flickVX.from = flickable.contentX
87                 flickVX.to = Math.max(0,Math.min(tempX-flickable.width/2,webView.width*sc-flickable.width))
88                 finalX.value = flickVX.to
89                 flickVY.from = flickable.contentY
90                 flickVY.to = Math.max(0,Math.min(tempY-flickable.height/2,webView.height*sc-flickable.height))
91                 finalY.value = flickVY.to
92                 quickZoom.start()
93             }
94         }
95
96        onContentsSizeChanged: {
97            contentsScale = Math.min(1,flickable.width / contentsSize.width)
98        }
99
100        onDoubleClick: {
101             if (!heuristicZoom(clickX,clickY,2.5)) {
102                 var zf = flickable.width / contentsSize.width
103                 if (zf >= contentsScale)
104                     zf = 2.0/zoomFactor // zoom in (else zooming out)
105                 doZoom(zf,clickX*zf,clickY*zf)
106             }
107         }
108
109         SequentialAnimation {
110             id: quickZoom
111
112             PropertyAction {
113                 target: webView
114                 property: "renderingEnabled"
115                 value: false
116             }
117             ParallelAnimation {
118                 NumberAnimation {
119                     id: scaleAnim
120                     target: webView
121                     property: "contentsScale"
122                     easing.type: Easing.Linear
123                     duration: 200
124                 }
125                 NumberAnimation {
126                     id: flickVX
127                     target: flickable
128                     property: "contentX"
129                     easing.type: Easing.Linear
130                     duration: 200
131                     from: 0
132                     to: 0
133                 }
134                 NumberAnimation {
135                     id: flickVY
136                     target: flickable
137                     property: "contentY"
138                     easing.type: Easing.Linear
139                     duration: 200
140                     from: 0
141                     to: 0
142                 }
143             }
144             PropertyAction {
145                 id: finalX
146                 target: flickable
147                 property: "contentX"
148                 value: 0
149             }
150             PropertyAction {
151                 id: finalY
152                 target: flickable
153                 property: "contentY"
154                 value: 0
155             }
156             PropertyAction {
157                 target: webView
158                 property: "renderingEnabled"
159                 value: true
160             }
161         }
162         onZoomTo: doZoom(zoom,centerX,centerY)
163     }
164 }