From fafbc9f51b3a7180750f008bc41d7e989f8c23ee Mon Sep 17 00:00:00 2001 From: Casper van Donderen Date: Thu, 19 May 2011 11:12:01 +0200 Subject: Add TextBalloon QML Scene Grpah example with documentation. I moved an old example over to a subfolder. Reviewed-by: Gunnar Sletta --- doc/src/declarative/example-textballoons.qdoc | 104 +++++++++++++++++++ doc/src/declarative/examples.qdoc | 4 + .../images/declarative-textballoons_example.png | Bin 0 -> 29663 bytes examples/declarative/painteditem/main.cpp | 89 ----------------- examples/declarative/painteditem/myfile.qml | 57 ----------- examples/declarative/painteditem/painteditem.pro | 14 --- examples/declarative/painteditem/smile/main.cpp | 85 ++++++++++++++++ examples/declarative/painteditem/smile/smile.pro | 14 +++ examples/declarative/painteditem/smile/smile.qml | 57 +++++++++++ .../textballoons/textballoonplugin/plugin.h | 56 +++++++++++ .../textballoons/textballoonplugin/qmldir | 1 + .../textballoons/textballoonplugin/textballoon.cpp | 92 +++++++++++++++++ .../textballoons/textballoonplugin/textballoon.h | 68 +++++++++++++ .../painteditem/textballoons/textballoons.pro | 23 +++++ .../painteditem/textballoons/textballoons.qml | 110 +++++++++++++++++++++ 15 files changed, 614 insertions(+), 160 deletions(-) create mode 100644 doc/src/declarative/example-textballoons.qdoc create mode 100644 doc/src/images/declarative-textballoons_example.png delete mode 100644 examples/declarative/painteditem/main.cpp delete mode 100644 examples/declarative/painteditem/myfile.qml delete mode 100644 examples/declarative/painteditem/painteditem.pro create mode 100644 examples/declarative/painteditem/smile/main.cpp create mode 100644 examples/declarative/painteditem/smile/smile.pro create mode 100644 examples/declarative/painteditem/smile/smile.qml create mode 100644 examples/declarative/painteditem/textballoons/textballoonplugin/plugin.h create mode 100644 examples/declarative/painteditem/textballoons/textballoonplugin/qmldir create mode 100644 examples/declarative/painteditem/textballoons/textballoonplugin/textballoon.cpp create mode 100644 examples/declarative/painteditem/textballoons/textballoonplugin/textballoon.h create mode 100644 examples/declarative/painteditem/textballoons/textballoons.pro create mode 100644 examples/declarative/painteditem/textballoons/textballoons.qml diff --git a/doc/src/declarative/example-textballoons.qdoc b/doc/src/declarative/example-textballoons.qdoc new file mode 100644 index 0000000000..90de999265 --- /dev/null +++ b/doc/src/declarative/example-textballoons.qdoc @@ -0,0 +1,104 @@ +/**************************************************************************** +** +** 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 documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:FDL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Free Documentation License +** Alternatively, this file may be used under the terms of the GNU Free +** Documentation License version 1.3 as published by the Free Software +** Foundation and appearing in the file included in the packaging of this +** file. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +/*! + \title Scenegraph Painted Item Example + \example declarative/painteditem/textballoons + + The Painted Item example shows how to use the QML Scene Graph framework to + implement custom scenegraph items using QPainter. + + \image declarative-textballoons_example.png + + The QSGPaintedItem class is a class derived from QSGItem for implementing + custom QML Scene Graph items using the QPainter interfaces. + + The example consists of an item class, a plugin class and a QML file + to use this plugin. The \c TextBalloon class represents the individual + text balloons extending QSGPaintedItem, the \c TextBalloonPlugin class + represents the skeleton code for a QtQuick plugin and the + \c textballoons.qml file is used to load the plugin and display the text + balloons. + + We will focus on the \c TextBalloon class first and continue with the + \c textballoons.qml file. For an example on how to implement a QtQuick + plugin please look at \l{declarative/tutorials/extending/chapter6-plugins} + {Writing an Extension Plugin} + + \section1 TextBalloon Class Declaration + + The \c TextBalloon class inherits from QSGPaintedItem. QSGPaintedItem class + is the base class for all QPainter based items in the QML Scene Graph + framework. + + \snippet examples/declarative/painteditem/textballoons/textballoonplugin/textballoon.h 0 + + To implement a QSGPaintedItem you must implement QSGPaintedIem's pure + virtual function \l {QSGPaintedItem::}{paint()} which implements the + painting of the element. + + \section1 TextBalloon Class Definition + + We have to be sure to initialize the rightAligned property for a + TextBalloon item. + + \snippet examples/declarative/painteditem/textballoons/textballoonplugin/textballoon.cpp 0 + + Then we implement the \c paint() function which is automatically called by + the Scenegraph framework to paint the contents of the item. The function + paints the item in local coordinates. + + \snippet examples/declarative/painteditem/textballoons/textballoonplugin/textballoon.cpp 1 + + We start with setting the pen and brush on the item to define the look of + the item. After that we start drawing. Note that the \l {QSGPaintedItem::}{boundingRect()} + item is called to draw depending on the size of the item. The rectangle + returned by the \l {QSGPaintedItem::}{boundingRect()} function is the size + of the item as defined in the QML file. + + \section1 textballoons.qml file + + The Interface consists of two main parts. The scrollable area with the + textballoons and the controls button to add new balloons. + + \section2 BalloonView + + \snippet examples/declarative/painteditem/textballoons/textballoons.qml 0 + + The balloonModel contains two elements at application start which will be + displayed by the balloonView. The balloonView alernates the TextBalloon + delegate items between left-aligned and right-aligned. + + \section2 Controls + + \snippet examples/declarative/painteditem/textballoons/textballoons.qml 1 + + The controls part of the UI contains a rectangle with a MouseArea which + changes color when the mouse hovers over it. This control 'button' adds + a new element to the end of the model with a random width. + + */ diff --git a/doc/src/declarative/examples.qdoc b/doc/src/declarative/examples.qdoc index b7420e011b..e230d9f951 100644 --- a/doc/src/declarative/examples.qdoc +++ b/doc/src/declarative/examples.qdoc @@ -229,6 +229,10 @@ The examples can be found in Qt's \c examples/declarative directory. \o \l{declarative/cppextensions/networkaccessmanagerfactory}{Network access manager factory} \endlist +\section2 Scenegraph +\list +\o \l{declarative/painteditem/textballoons}{Painted Item} +\endlist \section1 Labs diff --git a/doc/src/images/declarative-textballoons_example.png b/doc/src/images/declarative-textballoons_example.png new file mode 100644 index 0000000000..d572de597c Binary files /dev/null and b/doc/src/images/declarative-textballoons_example.png differ diff --git a/examples/declarative/painteditem/main.cpp b/examples/declarative/painteditem/main.cpp deleted file mode 100644 index 85028600be..0000000000 --- a/examples/declarative/painteditem/main.cpp +++ /dev/null @@ -1,89 +0,0 @@ -/**************************************************************************** -** -** 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 -#include -#include -#include - -class MyPaintItem : public QSGPaintedItem -{ - Q_OBJECT -public: - MyPaintItem() : QSGPaintedItem() - { - setAntialiasing(true); - } - - virtual void paint(QPainter *p) - { - QRectF rect(0, 0, width(), height()); - rect.adjust(10, 10, -10, -10); - p->setPen(QPen(Qt::black, 20)); - p->setBrush(Qt::yellow); - p->drawEllipse(rect); - p->setPen(Qt::black); - p->setFont(QFont(QLatin1String("Times"), qRound(rect.height() / 2))); - p->drawText(rect, Qt::AlignCenter, QLatin1String(":-)")); - } -}; - -int main(int argc, char ** argv) -{ -#ifdef Q_WS_X11 - QApplication::setAttribute(Qt::AA_X11InitThreads); -#endif - - QApplication app(argc, argv); - - qmlRegisterType("MyModule", 1, 0, "MyPaintItem"); - - QGLFormat f = QGLFormat::defaultFormat(); - f.setSampleBuffers(true); - QSGView view(f); - view.setResizeMode(QSGView::SizeRootObjectToView); - view.setSource(QUrl::fromLocalFile("myfile.qml")); - view.show(); - view.raise(); - - return app.exec(); -} - -#include "main.moc" diff --git a/examples/declarative/painteditem/myfile.qml b/examples/declarative/painteditem/myfile.qml deleted file mode 100644 index bc4bd2664b..0000000000 --- a/examples/declarative/painteditem/myfile.qml +++ /dev/null @@ -1,57 +0,0 @@ -/**************************************************************************** -** -** 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 MyModule 1.0 - -Rectangle { - width: 480 - height: 480 - gradient: Gradient { - GradientStop { position: 0.0; color: "#00249a" } - GradientStop { position: 0.7; color: "#ffd94f" } - GradientStop { position: 1.0; color: "#ffa322" } - } - MyPaintItem { - anchors.fill: parent - anchors.margins: 10 - smooth: true - } -} diff --git a/examples/declarative/painteditem/painteditem.pro b/examples/declarative/painteditem/painteditem.pro deleted file mode 100644 index 5d7b9df074..0000000000 --- a/examples/declarative/painteditem/painteditem.pro +++ /dev/null @@ -1,14 +0,0 @@ -TEMPLATE = app -TARGET = painteditem - -QT += declarative - -macx: CONFIG -= app_bundle - -SOURCES += main.cpp - -CONFIG += console - -symbian { - TARGET.EPOCHEAPSIZE = 0x20000 0x5000000 -} diff --git a/examples/declarative/painteditem/smile/main.cpp b/examples/declarative/painteditem/smile/main.cpp new file mode 100644 index 0000000000..e26c1b47e7 --- /dev/null +++ b/examples/declarative/painteditem/smile/main.cpp @@ -0,0 +1,85 @@ +/**************************************************************************** +** +** 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 +#include +#include +#include + +class MyPaintItem : public QSGPaintedItem +{ + Q_OBJECT +public: + MyPaintItem() : QSGPaintedItem() + { + setAntialiasing(true); + } + + virtual void paint(QPainter *p) + { + QRectF rect(0, 0, width(), height()); + rect.adjust(10, 10, -10, -10); + p->setPen(QPen(Qt::black, 20)); + p->setBrush(Qt::yellow); + p->drawEllipse(rect); + p->setPen(Qt::black); + p->setFont(QFont(QLatin1String("Times"), qRound(rect.height() / 2))); + p->drawText(rect, Qt::AlignCenter, QLatin1String(":-)")); + } +}; + +int main(int argc, char ** argv) +{ + QApplication app(argc, argv); + + qmlRegisterType("MyModule", 1, 0, "MyPaintItem"); + + QGLFormat f = QGLFormat::defaultFormat(); + f.setSampleBuffers(true); + QSGView view(f); + view.setResizeMode(QSGView::SizeRootObjectToView); + view.setSource(QUrl::fromLocalFile("smile.qml")); + view.show(); + view.raise(); + + return app.exec(); +} + +#include "main.moc" diff --git a/examples/declarative/painteditem/smile/smile.pro b/examples/declarative/painteditem/smile/smile.pro new file mode 100644 index 0000000000..5d7b9df074 --- /dev/null +++ b/examples/declarative/painteditem/smile/smile.pro @@ -0,0 +1,14 @@ +TEMPLATE = app +TARGET = painteditem + +QT += declarative + +macx: CONFIG -= app_bundle + +SOURCES += main.cpp + +CONFIG += console + +symbian { + TARGET.EPOCHEAPSIZE = 0x20000 0x5000000 +} diff --git a/examples/declarative/painteditem/smile/smile.qml b/examples/declarative/painteditem/smile/smile.qml new file mode 100644 index 0000000000..bc4bd2664b --- /dev/null +++ b/examples/declarative/painteditem/smile/smile.qml @@ -0,0 +1,57 @@ +/**************************************************************************** +** +** 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 MyModule 1.0 + +Rectangle { + width: 480 + height: 480 + gradient: Gradient { + GradientStop { position: 0.0; color: "#00249a" } + GradientStop { position: 0.7; color: "#ffd94f" } + GradientStop { position: 1.0; color: "#ffa322" } + } + MyPaintItem { + anchors.fill: parent + anchors.margins: 10 + smooth: true + } +} diff --git a/examples/declarative/painteditem/textballoons/textballoonplugin/plugin.h b/examples/declarative/painteditem/textballoons/textballoonplugin/plugin.h new file mode 100644 index 0000000000..ec519a5de8 --- /dev/null +++ b/examples/declarative/painteditem/textballoons/textballoonplugin/plugin.h @@ -0,0 +1,56 @@ +/**************************************************************************** +** +** 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 demonstration applications of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial Usage +** Licensees holding valid Qt Commercial licenses may use this file in +** accordance with the Qt Commercial License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Nokia. +** +** GNU Lesser General Public License Usage +** Alternatively, 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. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include + +#include "textballoon.h" + +class TextBalloonPlugin : public QDeclarativeExtensionPlugin +{ + Q_OBJECT +public: + void registerTypes(const char *uri) + { + qmlRegisterType(uri, 1, 0, "TextBalloon"); + } +}; + +Q_EXPORT_PLUGIN2(qmltextballoonplugin, TextBalloonPlugin); diff --git a/examples/declarative/painteditem/textballoons/textballoonplugin/qmldir b/examples/declarative/painteditem/textballoons/textballoonplugin/qmldir new file mode 100644 index 0000000000..e8a08ae9d3 --- /dev/null +++ b/examples/declarative/painteditem/textballoons/textballoonplugin/qmldir @@ -0,0 +1 @@ +plugin qmltextballoonplugin diff --git a/examples/declarative/painteditem/textballoons/textballoonplugin/textballoon.cpp b/examples/declarative/painteditem/textballoons/textballoonplugin/textballoon.cpp new file mode 100644 index 0000000000..0fc56c7a74 --- /dev/null +++ b/examples/declarative/painteditem/textballoons/textballoonplugin/textballoon.cpp @@ -0,0 +1,92 @@ +/**************************************************************************** +** +** 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 demonstration applications of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial Usage +** Licensees holding valid Qt Commercial licenses may use this file in +** accordance with the Qt Commercial License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Nokia. +** +** GNU Lesser General Public License Usage +** Alternatively, 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. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "textballoon.h" + +//! [0] +TextBalloon::TextBalloon(QSGItem *parent) + : QSGPaintedItem(parent) + , rightAligned(false) +{ +} +//! [0] + +//! [1] +void TextBalloon::paint(QPainter *painter) +{ + QBrush brush(QColor("#007430")); + + painter->setBrush(brush); + painter->setPen(Qt::NoPen); + painter->setRenderHint(QPainter::Antialiasing); + + painter->drawRoundedRect(0, 0, boundingRect().width(), boundingRect().height() - 10, 10, 10); + + if (rightAligned) + { + const QPointF points[3] = { + QPointF(boundingRect().width() - 10.0, boundingRect().height() - 10.0), + QPointF(boundingRect().width() - 20.0, boundingRect().height()), + QPointF(boundingRect().width() - 30.0, boundingRect().height() - 10.0), + }; + painter->drawConvexPolygon(points, 3); + } + else + { + const QPointF points[3] = { + QPointF(10.0, boundingRect().height() - 10.0), + QPointF(20.0, boundingRect().height()), + QPointF(30.0, boundingRect().height() - 10.0), + }; + painter->drawConvexPolygon(points, 3); + } +} +//! [1] + +bool TextBalloon::isRightAligned() +{ + return this->rightAligned; +} + +void TextBalloon::setRightAligned(bool rightAligned) +{ + this->rightAligned = rightAligned; +} diff --git a/examples/declarative/painteditem/textballoons/textballoonplugin/textballoon.h b/examples/declarative/painteditem/textballoons/textballoonplugin/textballoon.h new file mode 100644 index 0000000000..1b630f4011 --- /dev/null +++ b/examples/declarative/painteditem/textballoons/textballoonplugin/textballoon.h @@ -0,0 +1,68 @@ +/**************************************************************************** +** +** 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 demonstration applications of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial Usage +** Licensees holding valid Qt Commercial licenses may use this file in +** accordance with the Qt Commercial License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Nokia. +** +** GNU Lesser General Public License Usage +** Alternatively, 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. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef TEXTBALLOON_H +#define TEXTBALLOON_H + +#include + +//! [0] +class TextBalloon : public QSGPaintedItem +{ + Q_OBJECT + Q_PROPERTY(bool rightAligned READ isRightAligned WRITE setRightAligned NOTIFY rightAlignedChanged) + + public: + TextBalloon(QSGItem *parent = 0); + void paint(QPainter *painter); + + bool isRightAligned(); + void setRightAligned(bool rightAligned); + + private: + bool rightAligned; + + signals: + void rightAlignedChanged(); +}; +//! [0] + +#endif diff --git a/examples/declarative/painteditem/textballoons/textballoons.pro b/examples/declarative/painteditem/textballoons/textballoons.pro new file mode 100644 index 0000000000..186581f9d5 --- /dev/null +++ b/examples/declarative/painteditem/textballoons/textballoons.pro @@ -0,0 +1,23 @@ +TEMPLATE = lib +CONFIG += qt plugin +QT += declarative + +TARGET = qmltextballoonplugin + +HEADERS += textballoonplugin/plugin.h \ + textballoonplugin/textballoon.h + +SOURCES += textballoonplugin/textballoon.cpp + +DESTDIR = textballoonplugin + +qdeclarativesources.files += \ + textballoonplugin/qmldir + +qdeclarativesources.path += $$[QT_INSTALL_EXAMPLES]/qtdeclarative/declarative/painteditem/textballoons/textballoonplugin + +sources.files = textballoons.qml +sources.path += $$[QT_INSTALL_EXAMPLES]/qtdeclarative/declarative/painteditem/textballoons +target.path += $$[QT_INSTALL_EXAMPLES]/qtdeclarative/declarative/painteditem/textballoons/textballoonplugin + +INSTALLS = qdeclarativesources sources target diff --git a/examples/declarative/painteditem/textballoons/textballoons.qml b/examples/declarative/painteditem/textballoons/textballoons.qml new file mode 100644 index 0000000000..b786a2c601 --- /dev/null +++ b/examples/declarative/painteditem/textballoons/textballoons.qml @@ -0,0 +1,110 @@ +/**************************************************************************** +** +** 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 demonstration applications of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial Usage +** Licensees holding valid Qt Commercial licenses may use this file in +** accordance with the Qt Commercial License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Nokia. +** +** GNU Lesser General Public License Usage +** Alternatively, 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. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import QtQuick 2.0 +import TextBalloonPlugin 1.0 + +Item { + height: 480 + width: 640 + + //! [0] + ListModel { + id: balloonModel + ListElement { + balloonWidth: 200 + } + ListElement { + balloonWidth: 350 + } + } + + ListView { + anchors.bottom: controls.top + anchors.bottomMargin: 2 + anchors.top: parent.top + id: balloonView + delegate: TextBalloon { + anchors.right: index % 2 == 0 ? undefined : parent.right + height: 60 + rightAligned: index % 2 == 0 ? false : true + width: balloonWidth + } + model: balloonModel + spacing: 5 + width: parent.width + } + //! [0] + + //! [1] + Rectangle { + id: controls + + anchors.bottom: parent.bottom + anchors.left: parent.left + anchors.margins: 1 + anchors.right: parent.right + border.width: 2 + color: "white" + height: parent.height * 0.15 + + Text { + anchors.centerIn: parent + text: "Add another balloon" + } + + MouseArea { + anchors.fill: parent + hoverEnabled: true + onClicked: { + balloonModel.append({"balloonWidth": Math.floor(Math.random() * 300 + 100)}) + balloonView.positionViewAtIndex(balloonView.count -1, ListView.End) + } + onEntered: { + parent.color = "#8ac953" + } + onExited: { + parent.color = "white" + } + } + } + //! [1] +} -- cgit v1.2.3