demos/declarative/webbrowser/content/FlickableWebView.qml
branchGCC_SURGE
changeset 31 5daf16870df6
parent 30 5dc02b23752f
child 33 3e2da88830cd
equal deleted inserted replaced
27:93b982ccede2 31:5daf16870df6
       
     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 ** 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 Qt 4.7
       
    43 import org.webkit 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 reload: webView.reload
       
    52     property alias forward: webView.forward
       
    53 
       
    54     id: flickable
       
    55     width: parent.width
       
    56     contentWidth: Math.max(parent.width,webView.width)
       
    57     contentHeight: Math.max(parent.height,webView.height)
       
    58     anchors.top: headerSpace.bottom
       
    59     anchors.bottom: parent.top
       
    60     anchors.left: parent.left
       
    61     anchors.right: parent.right
       
    62     pressDelay: 200
       
    63 
       
    64     onWidthChanged : {
       
    65         // Expand (but not above 1:1) if otherwise would be smaller that available width.
       
    66         if (width > webView.width*webView.contentsScale && webView.contentsScale < 1.0)
       
    67             webView.contentsScale = width / webView.width * webView.contentsScale;
       
    68     }
       
    69 
       
    70     WebView {
       
    71         id: webView
       
    72         transformOrigin: Item.TopLeft
       
    73 
       
    74         function fixUrl(url)
       
    75         {
       
    76             if (url == "") return url
       
    77             if (url[0] == "/") return "file://"+url
       
    78             if (url.indexOf(":")<0) {
       
    79                 if (url.indexOf(".")<0 || url.indexOf(" ")>=0) {
       
    80                     // Fall back to a search engine; hard-code Wikipedia
       
    81                     return "http://en.wikipedia.org/w/index.php?search="+url
       
    82                 } else {
       
    83                     return "http://"+url
       
    84                 }
       
    85             }
       
    86             return url
       
    87         }
       
    88 
       
    89         url: fixUrl(webBrowser.urlString)
       
    90         smooth: false // We don't want smooth scaling, since we only scale during (fast) transitions
       
    91         focus: true
       
    92         zoomFactor: 1
       
    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 = zoom;
       
   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/zoomFactor
       
   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/zoomFactor // 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 }