Magnifier.qml 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. import QtQuick 1.1
  41. import Qt.labs.shaders 1.0
  42. Item {
  43. id: root
  44. property alias sourceItem: effectSource.sourceItem
  45. property alias xCenter: magnifier.xCenter
  46. property alias yCenter: magnifier.yCenter
  47. property alias active: magnifier.active
  48. property real yAdjustment: 0
  49. visible: magnifier.active
  50. width: 182
  51. height: 211
  52. z: Number.MAX_VALUE
  53. function __rootElement() {
  54. var ret = parent
  55. while (ret.parent) {
  56. ret = ret.parent
  57. }
  58. return ret
  59. }
  60. Component.onCompleted: {
  61. sourceItem = parent;
  62. }
  63. ShaderEffectSource {
  64. id: effectSource
  65. textureSize: Qt.size((sourceItem.width) * scaleFactor, (sourceItem.height) * scaleFactor);
  66. hideOriginal: false
  67. margins: Qt.size(1, 1);
  68. property real scaleFactor: 1.25
  69. filtering: ShaderEffectSource.Linear
  70. }
  71. ShaderEffectSource {
  72. id: magnifierFrame
  73. sourceImage: "/usr/share/themes/blanco/meegotouch/images/theme/basement/meegotouch-virtual-keyboard/meegotouch-seattle-magnifier-frame.png"
  74. filtering: ShaderEffectSource.Linear
  75. }
  76. ShaderEffectSource {
  77. id: magnifierMask
  78. sourceImage: "/usr/share/themes/blanco/meegotouch/images/theme/basement/meegotouch-virtual-keyboard/meegotouch-seattle-magnifier-frame-mask.png"
  79. }
  80. ShaderEffectItem {
  81. id: magnifier
  82. anchors.fill:parent
  83. active: false
  84. vertexShader: "
  85. attribute highp vec4 qt_Vertex;
  86. attribute highp vec2 qt_MultiTexCoord0;
  87. uniform highp mat4 qt_ModelViewProjectionMatrix;
  88. uniform highp float xCenter;
  89. uniform highp float xSize;
  90. uniform highp float yCenter;
  91. uniform highp float ySize;
  92. uniform highp float scaleFactor;
  93. varying highp vec2 qt_TexCoord0;
  94. varying highp vec2 qt_TexCoord1;
  95. void main() {
  96. qt_TexCoord0.x = xCenter - xSize / (2. * scaleFactor) + xSize * qt_MultiTexCoord0.x / scaleFactor;
  97. qt_TexCoord0.y = yCenter - ySize / (2. * scaleFactor) + ySize * qt_MultiTexCoord0.y / scaleFactor;
  98. qt_TexCoord1 = qt_MultiTexCoord0;
  99. gl_Position = qt_ModelViewProjectionMatrix * qt_Vertex;
  100. }";
  101. fragmentShader: "
  102. varying highp vec2 qt_TexCoord0;
  103. varying highp vec2 qt_TexCoord1;
  104. uniform lowp sampler2D source;
  105. uniform lowp sampler2D frame;
  106. uniform lowp sampler2D mask;
  107. void main() {
  108. lowp vec4 frame_c = texture2D(frame, qt_TexCoord1);
  109. lowp vec4 mask_c = texture2D(mask, qt_TexCoord1);
  110. lowp vec4 color_c = texture2D(source, qt_TexCoord0);
  111. bool outsideElement=(qt_TexCoord0.s<0. || qt_TexCoord0.s>1. || qt_TexCoord0.t<0. || qt_TexCoord0.t>1.);
  112. bool onGlass=(mask_c.a==1.);
  113. if (outsideElement) {
  114. // make white outside the element
  115. color_c=vec4(1.,1.,1.,1.);
  116. } else if (onGlass) {
  117. // blend premultiplied texture with pure white (background)
  118. color_c = color_c + vec4(1.,1.,1.,1.) * (1.-color_c.a);
  119. }
  120. gl_FragColor = (onGlass) ? color_c : frame_c;
  121. }";
  122. property real xCenter: 0
  123. property real yCenter: 0.25
  124. property real xSize: width / (root.sourceItem.width)
  125. property real ySize: height / (root.sourceItem.height)
  126. property real scaleFactor: effectSource.scaleFactor;
  127. property variant source: effectSource
  128. property variant frame: magnifierFrame
  129. property variant mask: magnifierMask
  130. }
  131. }