improved desktop UI
[mardrone] / mardrone / imports / com / nokia / meego / Magnifier.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 Qt Components project.
8 **
9 ** $QT_BEGIN_LICENSE:BSD$
10 ** You may use this file under the terms of the BSD license as follows:
11 **
12 ** "Redistribution and use in source and binary forms, with or without
13 ** modification, are permitted provided that the following conditions are
14 ** met:
15 **   * Redistributions of source code must retain the above copyright
16 **     notice, this list of conditions and the following disclaimer.
17 **   * Redistributions in binary form must reproduce the above copyright
18 **     notice, this list of conditions and the following disclaimer in
19 **     the documentation and/or other materials provided with the
20 **     distribution.
21 **   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
22 **     the names of its contributors may be used to endorse or promote
23 **     products derived from this software without specific prior written
24 **     permission.
25 **
26 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40
41 import QtQuick 1.1
42
43 Item {
44     id: root
45
46     property alias sourceItem: effectSource.sourceItem
47     property real xCenter: 0 // in source item coordinates
48     property real yCenter: 0 // in source item coordinates
49     
50     // Source rect is not as small as it can be as there is drawing problems
51     // with small source rect/texture size.
52     property real _realScaleFactor: 1.25
53     property real _sourceRectMultiplier: 2
54
55     property bool active: false
56
57     visible: active
58     width: 182
59     height: 211
60     z: Number.MAX_VALUE
61
62     function __rootElement() {
63         var ret = parent
64         while (ret.parent) {
65             ret = ret.parent
66         }
67         return ret
68     }
69
70     Component.onCompleted: {
71         sourceItem = parent;
72         declarativeView.setFullViewportMode(root);
73     }
74
75     ShaderEffectSource {
76         id: effectSource
77         sourceRect: Qt.rect(root.xCenter - textureSize.width / 2,
78                             root.yCenter - textureSize.height / 2,
79                             textureSize.width,
80                             textureSize.height);
81         textureSize: Qt.size(root._sourceRectMultiplier * root.width,
82                              root._sourceRectMultiplier * root.height);
83
84         hideSource: false
85         smooth: true
86
87         property real scaleFactor: root._sourceRectMultiplier * root._realScaleFactor
88     }
89
90     Image {
91         id: magnifierFrameImage
92         source: "/usr/share/themes/blanco/meegotouch/images/theme/basement/meegotouch-virtual-keyboard/meegotouch-seattle-magnifier-frame.png"
93     }
94
95     ShaderEffectSource {
96         id: magnifierFrame
97         sourceItem: magnifierFrameImage
98         hideSource: true
99         live: false
100     }
101
102     Image {
103         id: magnifierMaskImage
104         source: "/usr/share/themes/blanco/meegotouch/images/theme/basement/meegotouch-virtual-keyboard/meegotouch-seattle-magnifier-frame-mask.png"
105     }
106
107     ShaderEffectSource {
108         id: magnifierMask
109         sourceItem: magnifierMaskImage
110         hideSource: true
111         live: false
112     }
113
114     ShaderEffectItem {
115         id: magnifier
116         anchors.fill:parent
117         visible: root.visible
118
119         vertexShader: "
120             attribute highp vec4 qt_Vertex;
121             attribute highp vec2 qt_MultiTexCoord0;
122             uniform highp mat4 qt_ModelViewProjectionMatrix;
123             uniform highp float scaleFactor;
124             varying highp vec2 qt_TexCoord0;
125             varying highp vec2 qt_TexCoord1;
126             void main() {
127                 qt_TexCoord0.x = 0.5 - 1. / (2. * scaleFactor) + qt_MultiTexCoord0.x / scaleFactor;
128                 qt_TexCoord0.y = 0.5 - 1. / (2. * scaleFactor) + qt_MultiTexCoord0.y / scaleFactor;
129                 qt_TexCoord1 = qt_MultiTexCoord0;
130                 gl_Position = qt_ModelViewProjectionMatrix * qt_Vertex;
131             }";
132
133         fragmentShader: "
134             varying highp vec2 qt_TexCoord0;
135             varying highp vec2 qt_TexCoord1;
136             uniform lowp sampler2D source;
137             uniform lowp sampler2D frame;
138             uniform lowp sampler2D mask;
139             void main() {
140                 lowp vec4 frame_c = texture2D(frame, qt_TexCoord1);
141                 lowp vec4 mask_c = texture2D(mask, qt_TexCoord1);
142                 lowp vec4 color_c = texture2D(source, qt_TexCoord0);
143                 bool outsideElement=(qt_TexCoord0.s<0. || qt_TexCoord0.s>1. || qt_TexCoord0.t<0. || qt_TexCoord0.t>1.);
144                 bool onGlass=(mask_c.a==1.);
145
146                 if (outsideElement) {
147                     // make white outside the element
148                     color_c=vec4(1.,1.,1.,1.);
149                 } else if (onGlass) {
150                     // blend premultiplied texture with pure white (background)
151                     color_c = color_c + vec4(1.,1.,1.,1.) * (1.-color_c.a);
152                 }
153
154                 if ( qt_TexCoord1.y >= 0.98 ) {
155                     // Top part of item above visible magnifier frame is made
156                     // transparent explicitly to prevent showing of wrongly
157                     // colored pixels, which would otherwise appear sometimes
158                     // when using sourceRect functionality.
159                     gl_FragColor = vec4(0.,0.,0.,0.);
160                 } else {
161                     gl_FragColor = onGlass ? color_c : frame_c;
162                 }
163        }";
164
165         property variant source: effectSource
166         property variant frame: magnifierFrame
167         property variant mask: magnifierMask
168         property real scaleFactor: effectSource.scaleFactor;
169     }
170 }