summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorSamuel Rødal <samuel.rodal@nokia.com>2011-03-15 21:06:50 +0100
committerSamuel Rødal <samuel.rodal@nokia.com>2011-03-15 21:06:50 +0100
commit9bfeb0a794c605fa5ac8b49b9ec0c3065298f83f (patch)
tree0d97b1d6fc2762d7b0b4974190d2a9241455f3e9 /examples
parent335186374222d06af94f8c67fe580d3ffa55d0c6 (diff)
Better layouting system in QML compositor.
Diffstat (limited to 'examples')
-rw-r--r--examples/qml-compositor/main.cpp44
-rw-r--r--examples/qml-compositor/qml/QmlCompositor/ShaderEffect.qml57
-rw-r--r--examples/qml-compositor/qml/QmlCompositor/Window.qml43
-rw-r--r--examples/qml-compositor/qml/QmlCompositor/compositor.js85
-rw-r--r--examples/qml-compositor/qml/QmlCompositor/main.qml36
5 files changed, 237 insertions, 28 deletions
diff --git a/examples/qml-compositor/main.cpp b/examples/qml-compositor/main.cpp
index 3a3758c87..0e6f73eb3 100644
--- a/examples/qml-compositor/main.cpp
+++ b/examples/qml-compositor/main.cpp
@@ -150,6 +150,9 @@ public:
void keyPressEvent(QKeyEvent *event);
void keyReleaseEvent(QKeyEvent *event);
+public slots:
+ void takeFocus();
+
private slots:
void surfaceMapped(const QRect &rect);
@@ -168,6 +171,9 @@ WindowItem::WindowItem(WaylandSurface *surface, QSGItem *parent)
, m_surface(surface)
, m_textureProvider(new WaylandSurfaceTextureProvider(surface))
{
+ setWidth(surface->geometry().width());
+ setHeight(surface->geometry().height());
+
setSmooth(true);
setFlag(ItemHasContents);
setAcceptedMouseButtons(Qt::LeftButton | Qt::RightButton);
@@ -182,29 +188,38 @@ WindowItem::~WindowItem()
void WindowItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
- setFocus(true);
- m_surface->setInputFocus();
- m_surface->sendMousePressEvent(toSurface(event->pos()), event->button());
+ if (hasFocus())
+ m_surface->sendMousePressEvent(toSurface(event->pos()), event->button());
}
void WindowItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
- m_surface->sendMouseMoveEvent(toSurface(event->pos()));
+ if (hasFocus())
+ m_surface->sendMouseMoveEvent(toSurface(event->pos()));
}
void WindowItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
- m_surface->sendMouseReleaseEvent(toSurface(event->pos()), event->button());
+ if (hasFocus())
+ m_surface->sendMouseReleaseEvent(toSurface(event->pos()), event->button());
}
void WindowItem::keyPressEvent(QKeyEvent *event)
{
- m_surface->sendKeyPressEvent(event->nativeScanCode());
+ if (hasFocus())
+ m_surface->sendKeyPressEvent(event->nativeScanCode());
}
void WindowItem::keyReleaseEvent(QKeyEvent *event)
{
- m_surface->sendKeyReleaseEvent(event->nativeScanCode());
+ if (hasFocus())
+ m_surface->sendKeyReleaseEvent(event->nativeScanCode());
+}
+
+void WindowItem::takeFocus()
+{
+ setFocus(true);
+ m_surface->setInputFocus();
}
QPoint WindowItem::toSurface(const QPointF &pos) const
@@ -263,6 +278,15 @@ private slots:
void surfaceMapped(const QRect &rect) {
WaylandSurface *surface = qobject_cast<WaylandSurface *>(sender());
surface->setGeometry(rect);
+
+ if (!m_windowMap.contains(surface)) {
+ WindowItem *item = new WindowItem(surface, rootObject());
+ connect(surface, SIGNAL(destroyed(QObject *)), this, SLOT(surfaceDestroyed(QObject *)));
+ emit windowAdded(QVariant::fromValue(static_cast<QSGItem *>(item)));
+ m_windowMap[surface] = item;
+
+ item->takeFocus();
+ }
}
void surfaceDestroyed(QObject *object) {
@@ -274,13 +298,7 @@ private slots:
protected:
void surfaceCreated(WaylandSurface *surface) {
- WindowItem *item = new WindowItem(surface, rootObject());
- item->setFocus(true);
- surface->setInputFocus();
connect(surface, SIGNAL(mapped(const QRect &)), this, SLOT(surfaceMapped(const QRect &)));
- connect(surface, SIGNAL(destroyed(QObject *)), this, SLOT(surfaceDestroyed(QObject *)));
- emit windowAdded(QVariant::fromValue(static_cast<QSGItem *>(item)));
- m_windowMap[surface] = item;
}
void paintEvent(QPaintEvent *event) {
diff --git a/examples/qml-compositor/qml/QmlCompositor/ShaderEffect.qml b/examples/qml-compositor/qml/QmlCompositor/ShaderEffect.qml
new file mode 100644
index 000000000..e033095d2
--- /dev/null
+++ b/examples/qml-compositor/qml/QmlCompositor/ShaderEffect.qml
@@ -0,0 +1,57 @@
+/****************************************************************************
+**
+** This file is part of QtCompositor**
+**
+** Copyright © 2010 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+**
+** Contact: Nokia Corporation qt-info@nokia.com
+**
+** You may use this file under the terms of the BSD license as follows:
+**
+** Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+**
+** Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+**
+** Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in the
+** documentation and/or other materials provided with the distribution.
+**
+** Neither the name of Nokia Corporation and its Subsidiary(-ies) nor the
+** names of its contributors may be used to endorse or promote products
+** derived from this software without specific prior written permission.
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+**
+****************************************************************************/
+
+import QtQuick 2.0
+
+ShaderEffectItem {
+ property variant source: null;
+ property color color: "#ffffff"
+
+ fragmentShader: "
+ uniform sampler2D source;
+ uniform float qt_Opacity;
+ uniform vec4 color;
+ varying highp vec2 qt_TexCoord0;
+ void main() {
+ vec4 sourceColor = texture2D(source, qt_TexCoord0);
+ gl_FragColor = qt_Opacity * vec4(color.rgb * dot(sourceColor.rgb, vec3(11, 16, 5) * (1. / 32.)), sourceColor.a);
+ }
+ "
+}
diff --git a/examples/qml-compositor/qml/QmlCompositor/Window.qml b/examples/qml-compositor/qml/QmlCompositor/Window.qml
index b486b1624..28772f2c6 100644
--- a/examples/qml-compositor/qml/QmlCompositor/Window.qml
+++ b/examples/qml-compositor/qml/QmlCompositor/Window.qml
@@ -45,14 +45,49 @@ Rectangle {
x: -400;
y: 0;
+ opacity: 0
+
+ property variant child: null;
+ property bool animationsEnabled: false;
Behavior on x {
- enabled: true
- NumberAnimation { easing.type: Easing.InCubic; duration: 1000; }
+ enabled: container.animationsEnabled;
+ NumberAnimation { easing.type: Easing.InCubic; duration: 200; }
}
Behavior on y {
- enabled: true
- NumberAnimation { easing.type: Easing.InQuad; duration: 1000; }
+ enabled: container.animationsEnabled;
+ NumberAnimation { easing.type: Easing.InQuad; duration: 200; }
+ }
+
+ Behavior on scale {
+ enabled: container.animationsEnabled;
+ NumberAnimation { easing.type: Easing.InQuad; duration: 200; }
+ }
+
+ Behavior on opacity {
+ enabled: true;
+ NumberAnimation { easing.type: Easing.Linear; duration: 250; }
+ }
+
+ MouseArea {
+ anchors.fill: { if (child == null) parent; else child; }
+ z: 1
+ enabled: { if (child == null) true; else !child.focus; }
+ onClicked: {
+ child.takeFocus();
+ }
+ }
+
+ ShaderEffect {
+ source: child
+ anchors.fill: child
+ opacity: { if (child && child.focus) 0.0; else 0.8; }
+ z: 1
+
+ Behavior on opacity {
+ enabled: true;
+ NumberAnimation { easing.type: Easing.Linear; duration: 200; }
+ }
}
}
diff --git a/examples/qml-compositor/qml/QmlCompositor/compositor.js b/examples/qml-compositor/qml/QmlCompositor/compositor.js
new file mode 100644
index 000000000..d74d790c6
--- /dev/null
+++ b/examples/qml-compositor/qml/QmlCompositor/compositor.js
@@ -0,0 +1,85 @@
+/****************************************************************************
+**
+** This file is part of QtCompositor**
+**
+** Copyright © 2010 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+**
+** Contact: Nokia Corporation qt-info@nokia.com
+**
+** You may use this file under the terms of the BSD license as follows:
+**
+** Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+**
+** Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+**
+** Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in the
+** documentation and/or other materials provided with the distribution.
+**
+** Neither the name of Nokia Corporation and its Subsidiary(-ies) nor the
+** names of its contributors may be used to endorse or promote products
+** derived from this software without specific prior written permission.
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+**
+****************************************************************************/
+
+var windowList = null;
+
+function relayout() {
+ if (windowList.length == 0)
+ return;
+
+ var dim = Math.ceil(Math.sqrt(windowList.length));
+ var w = root.width / dim;
+ var h = root.height / dim;
+
+ var cols = dim;
+ var rows = Math.floor(windowList.length / cols);
+ var i;
+ var ix = 0;
+ var iy = 0;
+ var lastDim = 1;
+
+ for (i = 0; i < windowList.length; ++i) {
+ if (i > 0) {
+ var currentDim = Math.ceil(Math.sqrt(i + 1));
+ if (currentDim == lastDim) {
+ if (iy < currentDim - 1) {
+ ++iy;
+ if (iy == currentDim - 1)
+ ix = 0;
+ } else {
+ ++ix;
+ }
+ } else {
+ iy = 0;
+ ix = currentDim - 1;
+ }
+ lastDim = currentDim;
+ }
+
+ var cx = (ix + 0.5) * w;
+ var cy = (iy + 0.5) * h;
+
+ windowList[i].scale = 0.98 * Math.min(w / windowList[i].width, h / windowList[i].height);
+
+ windowList[i].x = (cx - windowList[i].width / 2);
+ windowList[i].y = (cy - windowList[i].height / 2);
+ }
+}
+
diff --git a/examples/qml-compositor/qml/QmlCompositor/main.qml b/examples/qml-compositor/qml/QmlCompositor/main.qml
index 80ed39e2d..ece3e506a 100644
--- a/examples/qml-compositor/qml/QmlCompositor/main.qml
+++ b/examples/qml-compositor/qml/QmlCompositor/main.qml
@@ -39,12 +39,13 @@
****************************************************************************/
import QtQuick 2.0
+import "compositor.js" as CompositorLogic
Rectangle {
id: root
- width: 800
- height: 640
+ width: 1024
+ height: 768
Image {
id: background
@@ -59,19 +60,32 @@ Rectangle {
var windowContainer = windowComponent.createObject(root);
window.parent = windowContainer;
+ windowContainer.width = window.width;
+ windowContainer.height = window.height;
+ windowContainer.child = window;
- windowContainer.x = spawnX;
- windowContainer.y = spawnY;
+ if (CompositorLogic.windowList == null)
+ CompositorLogic.windowList = new Array(0);
- spawnX -= 40;
- spawnY += 40;
+ CompositorLogic.windowList.push(windowContainer);
+ CompositorLogic.relayout();
+
+ windowContainer.opacity = 1
+ windowContainer.animationsEnabled = true;
}
function windowDestroyed(window) {
- var windowContainer = window.parent;
- windowContainer.destroy();
- }
+ var i;
+ for (i = 0; i < CompositorLogic.windowList.length; ++i) {
+ if (CompositorLogic.windowList[i].child == window)
+ break;
+ }
+
+ var container = CompositorLogic.windowList[i];
- property int spawnX: width - 400;
- property int spawnY: 20;
+ CompositorLogic.windowList.splice(i, 1);
+ CompositorLogic.relayout();
+
+ container.destroy();
+ }
}