Initial window implementation
authorAlan Alpert <alan.alpert@nokia.com>
Fri, 18 Nov 2011 09:32:56 +0000 (19:32 +1000)
committerQt by Nokia <qt-info@nokia.com>
Tue, 29 Nov 2011 02:29:53 +0000 (03:29 +0100)
Includes adding a color property on QQuickCanvas. Note that most Window
related properties come from the QWindow inheritance.

Task-number: QTBUG-19799
Change-Id: I00f6c90a1e2a5c85d787793d6edac2cd7d5309ab
Reviewed-by: Alan Alpert <alan.alpert@nokia.com>
13 files changed:
examples/declarative/window/Window.qml [new file with mode: 0644]
examples/declarative/window/standalone.qml [new file with mode: 0644]
examples/declarative/window/window.cpp [new file with mode: 0644]
examples/declarative/window/window.pro [new file with mode: 0644]
src/declarative/items/items.pri
src/declarative/items/qquickcanvas.cpp
src/declarative/items/qquickcanvas.h
src/declarative/items/qquickcanvas_p.h
src/declarative/items/qquickwindowmodule.cpp [new file with mode: 0644]
src/declarative/items/qquickwindowmodule_p.h [new file with mode: 0644]
src/declarative/qtquick2.cpp
tests/auto/declarative/qquickcanvas/data/window.qml [new file with mode: 0644]
tests/auto/declarative/qquickcanvas/tst_qquickcanvas.cpp

diff --git a/examples/declarative/window/Window.qml b/examples/declarative/window/Window.qml
new file mode 100644 (file)
index 0000000..33efd96
--- /dev/null
@@ -0,0 +1,58 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** 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."
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+import QtQuick 2.0
+import QtQuick.Window 2.0 as Window
+
+Window.Window {
+    width: 640
+    height: 480
+    visible: true //It's false by default
+    property Component self
+    Component.onCompleted: self = Qt.createComponent("Window.qml")
+    Text{
+        text: "Hello World!"
+        anchors.centerIn: parent
+    }
+    MouseArea{
+        anchors.fill: parent
+        onClicked: self.createObject();
+    }
+}
diff --git a/examples/declarative/window/standalone.qml b/examples/declarative/window/standalone.qml
new file mode 100644 (file)
index 0000000..d49e9ae
--- /dev/null
@@ -0,0 +1,71 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** 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."
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+import QtQuick 2.0
+import QtQuick.Window 2.0 as Window
+
+Item {
+    width: 640
+    height: 480
+    Text {
+        anchors.centerIn: parent
+        text: "First Window"
+    }
+    MouseArea {
+        anchors.fill: parent
+        onClicked: Qt.quit()
+    }
+    Window.Window {
+        width: 640
+        height: 480
+        x: 640
+        y: 480
+        visible: true
+        color: "green"
+        Text {
+            anchors.centerIn: parent
+            text: "Second Window"
+        }
+        MouseArea{
+            anchors.fill: parent
+            onClicked: Qt.quit()
+        }
+    }
+}
diff --git a/examples/declarative/window/window.cpp b/examples/declarative/window/window.cpp
new file mode 100644 (file)
index 0000000..56f1c09
--- /dev/null
@@ -0,0 +1,58 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** 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."
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include <QtGui/QGuiApplication>
+#include <QtDeclarative/QDeclarativeEngine>
+#include <QtDeclarative/QDeclarativeComponent>
+#include <QtCore/QUrl>
+#include <QDebug>
+
+int main(int argc, char* argv[])
+{
+    QGuiApplication app(argc, argv);
+    QDeclarativeEngine engine;
+    QDeclarativeComponent component(&engine);
+    component.loadUrl(QUrl::fromLocalFile("Window.qml"));
+    if ( component.isReady() )
+        component.create();
+    else
+        qWarning() << component.errorString();
+    return app.exec();
+}
diff --git a/examples/declarative/window/window.pro b/examples/declarative/window/window.pro
new file mode 100644 (file)
index 0000000..74804bd
--- /dev/null
@@ -0,0 +1,5 @@
+TEMPLATE = app
+CONFIG += qt
+QT += declarative
+
+SOURCES += window.cpp
index f83c65c..308706f 100644 (file)
@@ -66,7 +66,8 @@ HEADERS += \
     $$PWD/qquickdroparea_p.h \
     $$PWD/qquickmultipointtoucharea_p.h \
     $$PWD/qquickitemview_p.h \
-    $$PWD/qquickitemview_p_p.h
+    $$PWD/qquickitemview_p_p.h \
+    $$PWD/qquickwindowmodule_p.h
 
 SOURCES += \
     $$PWD/qquickevents.cpp \
@@ -112,7 +113,8 @@ SOURCES += \
     $$PWD/qquickdrag.cpp \
     $$PWD/qquickdroparea.cpp \
     $$PWD/qquickmultipointtoucharea.cpp \
-    $$PWD/qquickitemview.cpp
+    $$PWD/qquickitemview.cpp \
+    $$PWD/qquickwindowmodule.cpp
 
 SOURCES += \
     $$PWD/qquickshadereffect.cpp \
index c6969b5..8022af9 100644 (file)
@@ -532,6 +532,23 @@ void QQuickCanvasPrivate::init(QQuickCanvas *c)
     q->setFormat(context->defaultSurfaceFormat());
 }
 
+QDeclarativeListProperty<QObject> QQuickCanvasPrivate::data()
+{
+    initRootItem();
+    return QQuickItemPrivate::get(rootItem)->data();
+}
+
+void QQuickCanvasPrivate::initRootItem()
+{
+    Q_Q(QQuickCanvas);
+    q->connect(q, SIGNAL(widthChanged(int)),
+            rootItem, SLOT(setWidth(int)));
+    q->connect(q, SIGNAL(heightChanged(int)),
+            rootItem, SLOT(setHeight(int)));
+    rootItem->setWidth(q->width());
+    rootItem->setHeight(q->height());
+}
+
 void QQuickCanvasPrivate::transformTouchPoints(QList<QTouchEvent::TouchPoint> &touchPoints, const QTransform &transform)
 {
     for (int i=0; i<touchPoints.count(); i++) {
@@ -815,6 +832,14 @@ void QQuickCanvasPrivate::cleanup(QSGNode *n)
 
 
 /*!
+    \qmlclass Window QQuickCanvas
+    \inqmlmodule QtQuick.Window 2
+    \brief The Window object creates a new top-level window.
+
+    The Window object creates a new top-level window for a QtQuick scene. It automatically sets up the
+    window for use with QtQuick 2.0 graphical elements.
+*/
+/*!
     \class QQuickCanvas
     \since QtQuick 2.0
     \brief The QQuickCanvas class provides the canvas for displaying a graphical QML scene
@@ -894,6 +919,14 @@ QQuickItem *QQuickCanvas::mouseGrabberItem() const
 }
 
 
+/*!
+    \qmlproperty color QtQuick2.Window::Window::color
+
+    The background color for the window.
+
+    Setting this property is more efficient than using a separate Rectangle.
+*/
+
 bool QQuickCanvasPrivate::clearHover()
 {
     if (hoverItems.isEmpty())
@@ -2131,7 +2164,10 @@ QSGTexture *QQuickCanvas::createTextureFromId(uint id, const QSize &size, Create
 
 void QQuickCanvas::setClearColor(const QColor &color)
 {
+    if (color == d_func()->clearColor)
+        return;
     d_func()->clearColor = color;
+    emit clearColorChanged(color);
 }
 
 
index a480439..6f9d40d 100644 (file)
@@ -63,8 +63,11 @@ class QInputMethodEvent;
 
 class Q_DECLARATIVE_EXPORT QQuickCanvas : public QWindow
 {
-Q_OBJECT
-Q_DECLARE_PRIVATE(QQuickCanvas)
+    Q_OBJECT
+    Q_PRIVATE_PROPERTY(QQuickCanvas::d_func(), QDeclarativeListProperty<QObject> data READ data DESIGNABLE false)
+    Q_PROPERTY(QColor color READ clearColor WRITE setClearColor NOTIFY clearColorChanged)
+    Q_CLASSINFO("DefaultProperty", "data")
+    Q_DECLARE_PRIVATE(QQuickCanvas)
 public:
     enum CreateTextureOption {
         TextureHasAlphaChannel  = 0x0001,
@@ -114,6 +117,7 @@ Q_SIGNALS:
     void sceneGraphInitialized();
     void beforeRendering();
     void afterRendering();
+    void clearColorChanged(const QColor &);
 
 protected:
     QQuickCanvas(QQuickCanvasPrivate &dd, QWindow *parent = 0);
index fdfe091..13ca288 100644 (file)
 QT_BEGIN_NAMESPACE
 
 //Make it easy to identify and customize the root item if needed
+
 class QQuickRootItem : public QQuickItem
 {
     Q_OBJECT
 public:
     QQuickRootItem();
+public Q_SLOTS:
+    void setWidth(int w) {QQuickItem::setWidth(qreal(w));}
+    void setHeight(int h) {QQuickItem::setHeight(qreal(h));}
 };
 
+class QQuickItemPrivate;
 class QQuickCanvasPrivate;
 
 class QTouchEvent;
@@ -97,8 +102,10 @@ public:
     virtual ~QQuickCanvasPrivate();
 
     void init(QQuickCanvas *);
+    void initRootItem();//Currently only used if items added in QML
 
     QQuickRootItem *rootItem;
+    QDeclarativeListProperty<QObject> data();
 
     QQuickItem *activeFocusItem;
     QQuickItem *mouseGrabberItem;
@@ -175,6 +182,7 @@ public:
     QHash<int, QQuickItem *> itemForTouchPointId;
 
     mutable QQuickCanvasIncubationController *incubationController;
+
 private:
     static void cleanupNodesOnShutdown(QQuickItem *);
 };
diff --git a/src/declarative/items/qquickwindowmodule.cpp b/src/declarative/items/qquickwindowmodule.cpp
new file mode 100644 (file)
index 0000000..7a7f28c
--- /dev/null
@@ -0,0 +1,55 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the Declarative module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** GNU Lesser General Public License Usage
+** This file may be used under the terms of the GNU Lesser General Public
+** License version 2.1 as published by the Free Software Foundation and
+** appearing in the file LICENSE.LGPL included in the packaging of this
+** file. Please review the following information to ensure the GNU Lesser
+** General Public License version 2.1 requirements will be met:
+** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU General
+** Public License version 3.0 as published by the Free Software Foundation
+** and appearing in the file LICENSE.GPL included in the packaging of this
+** file. Please review the following information to ensure the GNU General
+** Public License version 3.0 requirements will be met:
+** http://www.gnu.org/copyleft/gpl.html.
+**
+** Other Usage
+** Alternatively, this file may be used in accordance with the terms and
+** conditions contained in a signed written agreement between you and Nokia.
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "qquickwindowmodule_p.h"
+#include <QQuickCanvas>
+
+QT_BEGIN_NAMESPACE
+
+void QQuickWindowModule::defineModule()
+{
+    const char* uri = "QtQuick.Window";
+
+    qmlRegisterType<QQuickCanvas>(uri, 2, 0, "Window");
+}
+
+QT_END_NAMESPACE
+
diff --git a/src/declarative/items/qquickwindowmodule_p.h b/src/declarative/items/qquickwindowmodule_p.h
new file mode 100644 (file)
index 0000000..ead1594
--- /dev/null
@@ -0,0 +1,63 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the Declarative module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** GNU Lesser General Public License Usage
+** This file may be used under the terms of the GNU Lesser General Public
+** License version 2.1 as published by the Free Software Foundation and
+** appearing in the file LICENSE.LGPL included in the packaging of this
+** file. Please review the following information to ensure the GNU Lesser
+** General Public License version 2.1 requirements will be met:
+** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU General
+** Public License version 3.0 as published by the Free Software Foundation
+** and appearing in the file LICENSE.GPL included in the packaging of this
+** file. Please review the following information to ensure the GNU General
+** Public License version 3.0 requirements will be met:
+** http://www.gnu.org/copyleft/gpl.html.
+**
+** Other Usage
+** Alternatively, this file may be used in accordance with the terms and
+** conditions contained in a signed written agreement between you and Nokia.
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QQUICKWINDOWMODULE_H
+#define QQUICKWINDOWMODULE_H
+
+#include <qdeclarative.h>
+
+QT_BEGIN_HEADER
+
+QT_BEGIN_NAMESPACE
+
+QT_MODULE(Declarative)
+
+class QQuickWindowModule
+{
+public:
+    static void defineModule();
+};
+
+QT_END_NAMESPACE
+
+QT_END_HEADER
+
+#endif
index 72e3251..ff49641 100644 (file)
@@ -45,6 +45,7 @@
 #include <private/qdeclarativevaluetype_p.h>
 #include <private/qquickitemsmodule_p.h>
 #include <private/qquickparticlesmodule_p.h>
+#include <private/qquickwindowmodule_p.h>
 
 #include <private/qdeclarativeenginedebugservice_p.h>
 #include <private/qdeclarativedebugstatesdelegate_p.h>
@@ -179,6 +180,7 @@ void QDeclarativeQtQuick2Module::defineModule()
     QDeclarativeEnginePrivate::defineModule();
     QQuickItemsModule::defineModule();
     QQuickParticlesModule::defineModule();
+    QQuickWindowModule::defineModule();
     QDeclarativeValueTypeFactory::registerValueTypes();
 
     if (QDeclarativeEngineDebugService::isDebuggingEnabled()) {
diff --git a/tests/auto/declarative/qquickcanvas/data/window.qml b/tests/auto/declarative/qquickcanvas/data/window.qml
new file mode 100644 (file)
index 0000000..d79d516
--- /dev/null
@@ -0,0 +1,9 @@
+import QtQuick 2.0
+import QtQuick.Window 2.0 as Window
+
+Window.Window {
+    color: "#00FF00"
+    Item {
+        objectName: "item"
+    }
+}
index 95fcd82..829d666 100644 (file)
 #include <QTouchEvent>
 #include <QtDeclarative/QQuickItem>
 #include <QtDeclarative/QQuickCanvas>
+#include <QtDeclarative/QDeclarativeEngine>
+#include <QtDeclarative/QDeclarativeComponent>
 #include <QtDeclarative/private/qquickrectangle_p.h>
 #include <QtGui/QWindowSystemInterface>
+#include "../shared/util.h"
 
 struct TouchEventData {
     QEvent::Type type;
@@ -197,6 +200,9 @@ private slots:
 
     void clearCanvas();
     void mouseFiltering();
+
+    void qmlCreation();
+    void clearColor();
 };
 
 tst_qquickcanvas::tst_qquickcanvas()
@@ -516,6 +522,35 @@ void tst_qquickcanvas::mouseFiltering()
     QCOMPARE(topItem->mousePressId, 3);
 }
 
+void tst_qquickcanvas::qmlCreation()
+{
+    QDeclarativeEngine engine;
+    QDeclarativeComponent component(&engine);
+    component.loadUrl(TESTDATA("window.qml"));
+    QObject* created = component.create();
+    QVERIFY(created);
+
+    QQuickCanvas* canvas = qobject_cast<QQuickCanvas*>(created);
+    QVERIFY(canvas);
+    QCOMPARE(canvas->clearColor(), QColor(Qt::green));
+
+    QQuickItem* item = canvas->findChild<QQuickItem*>("item");
+    QVERIFY(item);
+    QCOMPARE(item->canvas(), canvas);
+}
+
+void tst_qquickcanvas::clearColor()
+{
+    //### Can we examine rendering to make sure it is really blue?
+    QQuickCanvas *canvas = new QQuickCanvas;
+    canvas->resize(250, 250);
+    canvas->move(100, 100);
+    canvas->setClearColor(Qt::blue);
+    canvas->show();
+    QTest::qWaitForWindowShown(canvas);
+    QCOMPARE(canvas->clearColor(), QColor(Qt::blue));
+    delete canvas;
+}
 
 QTEST_MAIN(tst_qquickcanvas)