Initial import from the monolithic Qt.
[profile/ivi/qtdeclarative.git] / demos / declarative / webbrowser / content / FlickableWebView.qml
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 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 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
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 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 import QtQuick 1.0
43 import QtWebKit 1.0
44
45 Flickable {
46     property alias title: webView.title
47     property alias icon: webView.icon
48     property alias progress: webView.progress
49     property alias url: webView.url
50     property alias back: webView.back
51     property alias stop: webView.stop
52     property alias reload: webView.reload
53     property alias forward: webView.forward
54
55     id: flickable
56     width: parent.width
57     contentWidth: Math.max(parent.width,webView.width)
58     contentHeight: Math.max(parent.height,webView.height)
59     anchors.top: headerSpace.bottom
60     anchors.bottom: parent.top
61     anchors.left: parent.left
62     anchors.right: parent.right
63     pressDelay: 200
64
65     onWidthChanged : {
66         // Expand (but not above 1:1) if otherwise would be smaller that available width.
67         if (width > webView.width*webView.contentsScale && webView.contentsScale < 1.0)
68             webView.contentsScale = width / webView.width * webView.contentsScale;
69     }
70
71     WebView {
72         id: webView
73         transformOrigin: Item.TopLeft
74
75         function fixUrl(url)
76         {
77             if (url == "") return url
78             if (url[0] == "/") return "file://"+url
79             if (url.indexOf(":")<0) {
80                 if (url.indexOf(".")<0 || url.indexOf(" ")>=0) {
81                     // Fall back to a search engine; hard-code Wikipedia
82                     return "http://en.wikipedia.org/w/index.php?search="+url
83                 } else {
84                     return "http://"+url
85                 }
86             }
87             return url
88         }
89
90         url: fixUrl(webBrowser.urlString)
91         smooth: false // We don't want smooth scaling, since we only scale during (fast) transitions
92         focus: true
93
94         onAlert: console.log(message)
95
96         function doZoom(zoom,centerX,centerY)
97         {
98             if (centerX) {
99                 var sc = zoom*contentsScale;
100                 scaleAnim.to = sc;
101                 flickVX.from = flickable.contentX
102                 flickVX.to = Math.max(0,Math.min(centerX-flickable.width/2,webView.width*sc-flickable.width))
103                 finalX.value = flickVX.to
104                 flickVY.from = flickable.contentY
105                 flickVY.to = Math.max(0,Math.min(centerY-flickable.height/2,webView.height*sc-flickable.height))
106                 finalY.value = flickVY.to
107                 quickZoom.start()
108             }
109         }
110
111         Keys.onLeftPressed: webView.contentsScale -= 0.1
112         Keys.onRightPressed: webView.contentsScale += 0.1
113
114         preferredWidth: flickable.width
115         preferredHeight: flickable.height
116         contentsScale: 1
117         onContentsSizeChanged: {
118             // zoom out
119             contentsScale = Math.min(1,flickable.width / contentsSize.width)
120         }
121         onUrlChanged: {
122             // got to topleft
123             flickable.contentX = 0
124             flickable.contentY = 0
125             if (url != null) { header.editUrl = url.toString(); }
126         }
127         onDoubleClick: {
128                         if (!heuristicZoom(clickX,clickY,2.5)) {
129                             var zf = flickable.width / contentsSize.width
130                             if (zf >= contentsScale)
131                                 zf = 2.0*contentsScale // zoom in (else zooming out)
132                             doZoom(zf,clickX*zf,clickY*zf)
133                          }
134                        }
135
136         SequentialAnimation {
137             id: quickZoom
138
139             PropertyAction {
140                 target: webView
141                 property: "renderingEnabled"
142                 value: false
143             }
144             ParallelAnimation {
145                 NumberAnimation {
146                     id: scaleAnim
147                     target: webView
148                     property: "contentsScale"
149                     // the to property is set before calling
150                     easing.type: Easing.Linear
151                     duration: 200
152                 }
153                 NumberAnimation {
154                     id: flickVX
155                     target: flickable
156                     property: "contentX"
157                     easing.type: Easing.Linear
158                     duration: 200
159                     from: 0 // set before calling
160                     to: 0 // set before calling
161                 }
162                 NumberAnimation {
163                     id: flickVY
164                     target: flickable
165                     property: "contentY"
166                     easing.type: Easing.Linear
167                     duration: 200
168                     from: 0 // set before calling
169                     to: 0 // set before calling
170                 }
171             }
172             // Have to set the contentXY, since the above 2
173             // size changes may have started a correction if
174             // contentsScale < 1.0.
175             PropertyAction {
176                 id: finalX
177                 target: flickable
178                 property: "contentX"
179                 value: 0 // set before calling
180             }
181             PropertyAction {
182                 id: finalY
183                 target: flickable
184                 property: "contentY"
185                 value: 0 // set before calling
186             }
187             PropertyAction {
188                 target: webView
189                 property: "renderingEnabled"
190                 value: true
191             }
192         }
193         onZoomTo: doZoom(zoom,centerX,centerY)
194     }
195 }