diff options
Diffstat (limited to 'examples/graphicsview')
100 files changed, 6619 insertions, 0 deletions
diff --git a/examples/graphicsview/README b/examples/graphicsview/README new file mode 100644 index 0000000000..6c38c18683 --- /dev/null +++ b/examples/graphicsview/README @@ -0,0 +1,40 @@ +Qt is provided with a comprehensive canvas through the GraphicsView +classes. + +These examples demonstrate the fundamental aspects of canvas programming +with Qt. + + +The example launcher provided with Qt can be used to explore each of the +examples in this directory. + +Documentation for these examples can be found via the Tutorial and Examples +link in the main Qt documentation. + + +Finding the Qt Examples and Demos launcher +========================================== + +On Windows: + +The launcher can be accessed via the Windows Start menu. Select the menu +entry entitled "Qt Examples and Demos" entry in the submenu containing +the Qt tools. + +On Mac OS X: + +For the binary distribution, the qtdemo executable is installed in the +/Developer/Applications/Qt directory. For the source distribution, it is +installed alongside the other Qt tools on the path specified when Qt is +configured. + +On Unix/Linux: + +The qtdemo executable is installed alongside the other Qt tools on the path +specified when Qt is configured. + +On all platforms: + +The source code for the launcher can be found in the demos/qtdemo directory +in the Qt package. This example is built at the same time as the Qt libraries, +tools, examples, and demonstrations. diff --git a/examples/graphicsview/anchorlayout/anchorlayout.pro b/examples/graphicsview/anchorlayout/anchorlayout.pro new file mode 100644 index 0000000000..52cb13b241 --- /dev/null +++ b/examples/graphicsview/anchorlayout/anchorlayout.pro @@ -0,0 +1,9 @@ +SOURCES = main.cpp + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/graphicsview/anchorlayout +sources.files = $$SOURCES $$HEADERS $$RESOURCES anchorlayout.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/graphicsview/anchorlayout +INSTALLS += target sources + +TARGET = anchorlayout diff --git a/examples/graphicsview/anchorlayout/main.cpp b/examples/graphicsview/anchorlayout/main.cpp new file mode 100644 index 0000000000..c31afd214d --- /dev/null +++ b/examples/graphicsview/anchorlayout/main.cpp @@ -0,0 +1,128 @@ +/**************************************************************************** +** +** 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 <QGraphicsWidget> +#include <QGraphicsProxyWidget> +#include <QGraphicsAnchorLayout> +#include <QtGui> + +static QGraphicsProxyWidget *createItem(const QSizeF &minimum = QSizeF(100.0, 100.0), + const QSizeF &preferred = QSize(150.0, 100.0), + const QSizeF &maximum = QSizeF(200.0, 100.0), + const QString &name = "0") +{ + QGraphicsProxyWidget *w = new QGraphicsProxyWidget; + w->setWidget(new QPushButton(name)); + w->setData(0, name); + w->setMinimumSize(minimum); + w->setPreferredSize(preferred); + w->setMaximumSize(maximum); + + w->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred); + return w; +} + +int main(int argc, char **argv) +{ + QApplication app(argc, argv); + + QGraphicsScene scene; + scene.setSceneRect(0, 0, 800, 480); + + QSizeF minSize(30, 100); + QSizeF prefSize(210, 100); + QSizeF maxSize(300, 100); + + QGraphicsProxyWidget *a = createItem(minSize, prefSize, maxSize, "A"); + QGraphicsProxyWidget *b = createItem(minSize, prefSize, maxSize, "B"); + QGraphicsProxyWidget *c = createItem(minSize, prefSize, maxSize, "C"); + QGraphicsProxyWidget *d = createItem(minSize, prefSize, maxSize, "D"); + QGraphicsProxyWidget *e = createItem(minSize, prefSize, maxSize, "E"); + QGraphicsProxyWidget *f = createItem(QSizeF(30, 50), QSizeF(150, 50), maxSize, "F (overflow)"); + QGraphicsProxyWidget *g = createItem(QSizeF(30, 50), QSizeF(30, 100), maxSize, "G (overflow)"); + + QGraphicsAnchorLayout *l = new QGraphicsAnchorLayout; + l->setSpacing(0); + + QGraphicsWidget *w = new QGraphicsWidget(0, Qt::Window); + w->setPos(20, 20); + w->setLayout(l); + + // vertical + QGraphicsAnchor *anchor = l->addAnchor(a, Qt::AnchorTop, l, Qt::AnchorTop); + anchor = l->addAnchor(b, Qt::AnchorTop, l, Qt::AnchorTop); + + anchor = l->addAnchor(c, Qt::AnchorTop, a, Qt::AnchorBottom); + anchor = l->addAnchor(c, Qt::AnchorTop, b, Qt::AnchorBottom); + anchor = l->addAnchor(c, Qt::AnchorBottom, d, Qt::AnchorTop); + anchor = l->addAnchor(c, Qt::AnchorBottom, e, Qt::AnchorTop); + + anchor = l->addAnchor(d, Qt::AnchorBottom, l, Qt::AnchorBottom); + anchor = l->addAnchor(e, Qt::AnchorBottom, l, Qt::AnchorBottom); + + anchor = l->addAnchor(c, Qt::AnchorTop, f, Qt::AnchorTop); + anchor = l->addAnchor(c, Qt::AnchorVerticalCenter, f, Qt::AnchorBottom); + anchor = l->addAnchor(f, Qt::AnchorBottom, g, Qt::AnchorTop); + anchor = l->addAnchor(c, Qt::AnchorBottom, g, Qt::AnchorBottom); + + // horizontal + anchor = l->addAnchor(l, Qt::AnchorLeft, a, Qt::AnchorLeft); + anchor = l->addAnchor(l, Qt::AnchorLeft, d, Qt::AnchorLeft); + anchor = l->addAnchor(a, Qt::AnchorRight, b, Qt::AnchorLeft); + + anchor = l->addAnchor(a, Qt::AnchorRight, c, Qt::AnchorLeft); + anchor = l->addAnchor(c, Qt::AnchorRight, e, Qt::AnchorLeft); + + anchor = l->addAnchor(b, Qt::AnchorRight, l, Qt::AnchorRight); + anchor = l->addAnchor(e, Qt::AnchorRight, l, Qt::AnchorRight); + anchor = l->addAnchor(d, Qt::AnchorRight, e, Qt::AnchorLeft); + + anchor = l->addAnchor(l, Qt::AnchorLeft, f, Qt::AnchorLeft); + anchor = l->addAnchor(l, Qt::AnchorLeft, g, Qt::AnchorLeft); + anchor = l->addAnchor(f, Qt::AnchorRight, g, Qt::AnchorRight); + + + scene.addItem(w); + scene.setBackgroundBrush(Qt::darkGreen); + QGraphicsView view(&scene); + view.show(); + + return app.exec(); +} diff --git a/examples/graphicsview/basicgraphicslayouts/basicgraphicslayouts.pro b/examples/graphicsview/basicgraphicslayouts/basicgraphicslayouts.pro new file mode 100644 index 0000000000..f23bb7544d --- /dev/null +++ b/examples/graphicsview/basicgraphicslayouts/basicgraphicslayouts.pro @@ -0,0 +1,17 @@ +HEADERS = layoutitem.h \ + window.h +SOURCES = layoutitem.cpp \ + main.cpp \ + window.cpp +RESOURCES = basicgraphicslayouts.qrc + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/graphicsview/basicgraphicslayouts +sources.files = $$SOURCES $$HEADERS $$RESOURCES basicgraphicslayouts.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/graphicsview/basicgraphicslayouts +INSTALLS += target sources + +symbian { + TARGET.UID3 = 0xA000A645 + include($$QT_SOURCE_TREE/examples/symbianpkgrules.pri) +} diff --git a/examples/graphicsview/basicgraphicslayouts/basicgraphicslayouts.qrc b/examples/graphicsview/basicgraphicslayouts/basicgraphicslayouts.qrc new file mode 100644 index 0000000000..fbe2efa572 --- /dev/null +++ b/examples/graphicsview/basicgraphicslayouts/basicgraphicslayouts.qrc @@ -0,0 +1,5 @@ +<RCC> + <qresource> + <file>images/block.png</file> + </qresource> +</RCC> diff --git a/examples/graphicsview/basicgraphicslayouts/images/block.png b/examples/graphicsview/basicgraphicslayouts/images/block.png Binary files differnew file mode 100644 index 0000000000..4c72a3f508 --- /dev/null +++ b/examples/graphicsview/basicgraphicslayouts/images/block.png diff --git a/examples/graphicsview/basicgraphicslayouts/layoutitem.cpp b/examples/graphicsview/basicgraphicslayouts/layoutitem.cpp new file mode 100644 index 0000000000..0b1a10dd50 --- /dev/null +++ b/examples/graphicsview/basicgraphicslayouts/layoutitem.cpp @@ -0,0 +1,130 @@ +/**************************************************************************** +** +** 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 "layoutitem.h" + +//! [0] +LayoutItem::LayoutItem(QGraphicsItem *parent/* = 0*/) + : QGraphicsLayoutItem(), QGraphicsItem(parent) +{ + m_pix = new QPixmap(QLatin1String(":/images/block.png")); + setGraphicsItem(this); +} +//! [0] + +LayoutItem::~LayoutItem() +{ + delete m_pix; +} + +//! [1] +void LayoutItem::paint(QPainter *painter, + const QStyleOptionGraphicsItem *option, QWidget *widget /*= 0*/) +{ + Q_UNUSED(widget); + Q_UNUSED(option); + + QRectF frame(QPointF(0,0), geometry().size()); + qreal w = m_pix->width(); + qreal h = m_pix->height(); + QGradientStops stops; +//! [1] + +//! [2] + // paint a background rect (with gradient) + QLinearGradient gradient(frame.topLeft(), frame.topLeft() + QPointF(200,200)); + stops << QGradientStop(0.0, QColor(60, 60, 60)); + stops << QGradientStop(frame.height()/2/frame.height(), QColor(102, 176, 54)); + + //stops << QGradientStop(((frame.height() + h)/2 )/frame.height(), QColor(157, 195, 55)); + stops << QGradientStop(1.0, QColor(215, 215, 215)); + gradient.setStops(stops); + painter->setBrush(QBrush(gradient)); + painter->drawRoundedRect(frame, 10.0, 10.0); + + // paint a rect around the pixmap (with gradient) + QPointF pixpos = frame.center() - (QPointF(w, h)/2); + QRectF innerFrame(pixpos, QSizeF(w, h)); + innerFrame.adjust(-4, -4, +4, +4); + gradient.setStart(innerFrame.topLeft()); + gradient.setFinalStop(innerFrame.bottomRight()); + stops.clear(); + stops << QGradientStop(0.0, QColor(215, 255, 200)); + stops << QGradientStop(0.5, QColor(102, 176, 54)); + stops << QGradientStop(1.0, QColor(0, 0, 0)); + gradient.setStops(stops); + painter->setBrush(QBrush(gradient)); + painter->drawRoundedRect(innerFrame, 10.0, 10.0); + painter->drawPixmap(pixpos, *m_pix); +} +//! [2] + +//! [3] +QRectF LayoutItem::boundingRect() const +{ + return QRectF(QPointF(0,0), geometry().size()); +} +//! [3] + +//! [4] +void LayoutItem::setGeometry(const QRectF &geom) +{ + prepareGeometryChange(); + QGraphicsLayoutItem::setGeometry(geom); + setPos(geom.topLeft()); +} +//! [4] + +//! [5] +QSizeF LayoutItem::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const +{ + switch (which) { + case Qt::MinimumSize: + case Qt::PreferredSize: + // Do not allow a size smaller than the pixmap with two frames around it. + return m_pix->size() + QSize(12, 12); + case Qt::MaximumSize: + return QSizeF(1000,1000); + default: + break; + } + return constraint; +} +//! [5] diff --git a/examples/graphicsview/basicgraphicslayouts/layoutitem.h b/examples/graphicsview/basicgraphicslayouts/layoutitem.h new file mode 100644 index 0000000000..448e9bac3f --- /dev/null +++ b/examples/graphicsview/basicgraphicslayouts/layoutitem.h @@ -0,0 +1,65 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +#ifndef LAYOUTITEM_H +#define LAYOUTITEM_H +#include <QtGui> + +//! [0] +class LayoutItem : public QGraphicsLayoutItem, public QGraphicsItem +{ +public: + LayoutItem(QGraphicsItem *parent = 0); + ~LayoutItem(); + // Inherited from QGraphicsLayoutItem + void setGeometry(const QRectF &geom); + QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint = QSizeF()) const; + + // Inherited from QGraphicsItem + QRectF boundingRect() const; + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, + QWidget *widget = 0); + +private: + QPixmap *m_pix; +}; +//! [0] + +#endif diff --git a/examples/graphicsview/basicgraphicslayouts/main.cpp b/examples/graphicsview/basicgraphicslayouts/main.cpp new file mode 100644 index 0000000000..57448a58d7 --- /dev/null +++ b/examples/graphicsview/basicgraphicslayouts/main.cpp @@ -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> + +#include "window.h" + +int main(int argc, char **argv) +{ + QApplication app(argc, argv); + + QGraphicsScene scene; + + Window *window = new Window; + scene.addItem(window); + QGraphicsView view(&scene); + view.resize(600, 600); + view.show(); + + return app.exec(); +} diff --git a/examples/graphicsview/basicgraphicslayouts/window.cpp b/examples/graphicsview/basicgraphicslayouts/window.cpp new file mode 100644 index 0000000000..17a456e240 --- /dev/null +++ b/examples/graphicsview/basicgraphicslayouts/window.cpp @@ -0,0 +1,90 @@ +/**************************************************************************** +** +** 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 "window.h" +#include "layoutitem.h" + +Window::Window(QGraphicsWidget *parent) : QGraphicsWidget(parent, Qt::Window) +{ +//! [0] + QGraphicsLinearLayout *windowLayout = new QGraphicsLinearLayout(Qt::Vertical); + QGraphicsLinearLayout *linear = new QGraphicsLinearLayout(windowLayout); + LayoutItem *item = new LayoutItem; + linear->addItem(item); + linear->setStretchFactor(item, 1); +//! [0] + +//! [1] + item = new LayoutItem; + linear->addItem(item); + linear->setStretchFactor(item, 3); + windowLayout->addItem(linear); +//! [1] + +//! [2] + QGraphicsGridLayout *grid = new QGraphicsGridLayout(windowLayout); + item = new LayoutItem; + grid->addItem(item, 0, 0, 4, 1); + item = new LayoutItem; + item->setMaximumHeight(item->minimumHeight()); + grid->addItem(item, 0, 1, 2, 1, Qt::AlignVCenter); + item = new LayoutItem; + item->setMaximumHeight(item->minimumHeight()); + grid->addItem(item, 2, 1, 2, 1, Qt::AlignVCenter); + item = new LayoutItem; + grid->addItem(item, 0, 2); + item = new LayoutItem; + grid->addItem(item, 1, 2); + item = new LayoutItem; + grid->addItem(item, 2, 2); + item = new LayoutItem; + grid->addItem(item, 3, 2); + windowLayout->addItem(grid); +//! [2] + +//! [3] + setLayout(windowLayout); + setWindowTitle(tr("Basic Graphics Layouts Example")); +//! [3] + +} + + + diff --git a/examples/graphicsview/basicgraphicslayouts/window.h b/examples/graphicsview/basicgraphicslayouts/window.h new file mode 100644 index 0000000000..b2166f0552 --- /dev/null +++ b/examples/graphicsview/basicgraphicslayouts/window.h @@ -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$ +** +****************************************************************************/ + +#ifndef WINDOW_H +#define WINDOW_H + +#include <QtGui/QGraphicsWidget> + +//! [0] +class Window : public QGraphicsWidget { + Q_OBJECT + +public: + Window(QGraphicsWidget *parent = 0); + +}; +//! [0] + +#endif //WINDOW_H + diff --git a/examples/graphicsview/collidingmice/collidingmice.pro b/examples/graphicsview/collidingmice/collidingmice.pro new file mode 100644 index 0000000000..c88cf5306f --- /dev/null +++ b/examples/graphicsview/collidingmice/collidingmice.pro @@ -0,0 +1,19 @@ +HEADERS += \ + mouse.h +SOURCES += \ + main.cpp \ + mouse.cpp + +RESOURCES += \ + mice.qrc + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/graphicsview/collidingmice +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS collidingmice.pro images +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/graphicsview/collidingmice +INSTALLS += target sources + +symbian { + TARGET.UID3 = 0xA000A643 + include($$QT_SOURCE_TREE/examples/symbianpkgrules.pri) +} diff --git a/examples/graphicsview/collidingmice/images/cheese.jpg b/examples/graphicsview/collidingmice/images/cheese.jpg Binary files differnew file mode 100644 index 0000000000..dea5795fd0 --- /dev/null +++ b/examples/graphicsview/collidingmice/images/cheese.jpg diff --git a/examples/graphicsview/collidingmice/main.cpp b/examples/graphicsview/collidingmice/main.cpp new file mode 100644 index 0000000000..2970a0079f --- /dev/null +++ b/examples/graphicsview/collidingmice/main.cpp @@ -0,0 +1,91 @@ +/**************************************************************************** +** +** 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 "mouse.h" + +#include <QtGui> + +#include <math.h> + +static const int MouseCount = 7; + +//! [0] +int main(int argc, char **argv) +{ + QApplication app(argc, argv); + qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); +//! [0] + +//! [1] + QGraphicsScene scene; + scene.setSceneRect(-300, -300, 600, 600); +//! [1] //! [2] + scene.setItemIndexMethod(QGraphicsScene::NoIndex); +//! [2] + +//! [3] + for (int i = 0; i < MouseCount; ++i) { + Mouse *mouse = new Mouse; + mouse->setPos(::sin((i * 6.28) / MouseCount) * 200, + ::cos((i * 6.28) / MouseCount) * 200); + scene.addItem(mouse); + } +//! [3] + +//! [4] + QGraphicsView view(&scene); + view.setRenderHint(QPainter::Antialiasing); + view.setBackgroundBrush(QPixmap(":/images/cheese.jpg")); +//! [4] //! [5] + view.setCacheMode(QGraphicsView::CacheBackground); + view.setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate); + view.setDragMode(QGraphicsView::ScrollHandDrag); +//! [5] //! [6] + view.setWindowTitle(QT_TRANSLATE_NOOP(QGraphicsView, "Colliding Mice")); + view.resize(400, 300); + view.show(); + + QTimer timer; + QObject::connect(&timer, SIGNAL(timeout()), &scene, SLOT(advance())); + timer.start(1000 / 33); + + return app.exec(); +} +//! [6] diff --git a/examples/graphicsview/collidingmice/mice.qrc b/examples/graphicsview/collidingmice/mice.qrc new file mode 100644 index 0000000000..accdb4d0a6 --- /dev/null +++ b/examples/graphicsview/collidingmice/mice.qrc @@ -0,0 +1,5 @@ +<RCC> + <qresource prefix="/" > + <file>images/cheese.jpg</file> + </qresource> +</RCC> diff --git a/examples/graphicsview/collidingmice/mouse.cpp b/examples/graphicsview/collidingmice/mouse.cpp new file mode 100644 index 0000000000..4e89454f8d --- /dev/null +++ b/examples/graphicsview/collidingmice/mouse.cpp @@ -0,0 +1,200 @@ +/**************************************************************************** +** +** 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 "mouse.h" + +#include <QGraphicsScene> +#include <QPainter> +#include <QStyleOption> + +#include <math.h> + +static const double Pi = 3.14159265358979323846264338327950288419717; +static double TwoPi = 2.0 * Pi; + +static qreal normalizeAngle(qreal angle) +{ + while (angle < 0) + angle += TwoPi; + while (angle > TwoPi) + angle -= TwoPi; + return angle; +} + +//! [0] +Mouse::Mouse() + : angle(0), speed(0), mouseEyeDirection(0), + color(qrand() % 256, qrand() % 256, qrand() % 256) +{ + setRotation(qrand() % (360 * 16)); +} +//! [0] + +//! [1] +QRectF Mouse::boundingRect() const +{ + qreal adjust = 0.5; + return QRectF(-18 - adjust, -22 - adjust, + 36 + adjust, 60 + adjust); +} +//! [1] + +//! [2] +QPainterPath Mouse::shape() const +{ + QPainterPath path; + path.addRect(-10, -20, 20, 40); + return path; +} +//! [2] + +//! [3] +void Mouse::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) +{ + // Body + painter->setBrush(color); + painter->drawEllipse(-10, -20, 20, 40); + + // Eyes + painter->setBrush(Qt::white); + painter->drawEllipse(-10, -17, 8, 8); + painter->drawEllipse(2, -17, 8, 8); + + // Nose + painter->setBrush(Qt::black); + painter->drawEllipse(QRectF(-2, -22, 4, 4)); + + // Pupils + painter->drawEllipse(QRectF(-8.0 + mouseEyeDirection, -17, 4, 4)); + painter->drawEllipse(QRectF(4.0 + mouseEyeDirection, -17, 4, 4)); + + // Ears + painter->setBrush(scene()->collidingItems(this).isEmpty() ? Qt::darkYellow : Qt::red); + painter->drawEllipse(-17, -12, 16, 16); + painter->drawEllipse(1, -12, 16, 16); + + // Tail + QPainterPath path(QPointF(0, 20)); + path.cubicTo(-5, 22, -5, 22, 0, 25); + path.cubicTo(5, 27, 5, 32, 0, 30); + path.cubicTo(-5, 32, -5, 42, 0, 35); + painter->setBrush(Qt::NoBrush); + painter->drawPath(path); +} +//! [3] + +//! [4] +void Mouse::advance(int step) +{ + if (!step) + return; +//! [4] + // Don't move too far away +//! [5] + QLineF lineToCenter(QPointF(0, 0), mapFromScene(0, 0)); + if (lineToCenter.length() > 150) { + qreal angleToCenter = ::acos(lineToCenter.dx() / lineToCenter.length()); + if (lineToCenter.dy() < 0) + angleToCenter = TwoPi - angleToCenter; + angleToCenter = normalizeAngle((Pi - angleToCenter) + Pi / 2); + + if (angleToCenter < Pi && angleToCenter > Pi / 4) { + // Rotate left + angle += (angle < -Pi / 2) ? 0.25 : -0.25; + } else if (angleToCenter >= Pi && angleToCenter < (Pi + Pi / 2 + Pi / 4)) { + // Rotate right + angle += (angle < Pi / 2) ? 0.25 : -0.25; + } + } else if (::sin(angle) < 0) { + angle += 0.25; + } else if (::sin(angle) > 0) { + angle -= 0.25; +//! [5] //! [6] + } +//! [6] + + // Try not to crash with any other mice +//! [7] + QList<QGraphicsItem *> dangerMice = scene()->items(QPolygonF() + << mapToScene(0, 0) + << mapToScene(-30, -50) + << mapToScene(30, -50)); + foreach (QGraphicsItem *item, dangerMice) { + if (item == this) + continue; + + QLineF lineToMouse(QPointF(0, 0), mapFromItem(item, 0, 0)); + qreal angleToMouse = ::acos(lineToMouse.dx() / lineToMouse.length()); + if (lineToMouse.dy() < 0) + angleToMouse = TwoPi - angleToMouse; + angleToMouse = normalizeAngle((Pi - angleToMouse) + Pi / 2); + + if (angleToMouse >= 0 && angleToMouse < Pi / 2) { + // Rotate right + angle += 0.5; + } else if (angleToMouse <= TwoPi && angleToMouse > (TwoPi - Pi / 2)) { + // Rotate left + angle -= 0.5; +//! [7] //! [8] + } +//! [8] //! [9] + } +//! [9] + + // Add some random movement +//! [10] + if (dangerMice.size() > 1 && (qrand() % 10) == 0) { + if (qrand() % 1) + angle += (qrand() % 100) / 500.0; + else + angle -= (qrand() % 100) / 500.0; + } +//! [10] + +//! [11] + speed += (-50 + qrand() % 100) / 100.0; + + qreal dx = ::sin(angle) * 10; + mouseEyeDirection = (qAbs(dx / 5) < 1) ? 0 : dx / 5; + + setRotation(rotation() + dx); + setPos(mapToParent(0, -(3 + sin(speed) * 3))); +} +//! [11] diff --git a/examples/graphicsview/collidingmice/mouse.h b/examples/graphicsview/collidingmice/mouse.h new file mode 100644 index 0000000000..430e741d5b --- /dev/null +++ b/examples/graphicsview/collidingmice/mouse.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 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$ +** +****************************************************************************/ + +#ifndef MOUSE_H +#define MOUSE_H + +#include <QGraphicsItem> + +//! [0] +class Mouse : public QGraphicsItem +{ +public: + Mouse(); + + QRectF boundingRect() const; + QPainterPath shape() const; + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, + QWidget *widget); + +protected: + void advance(int step); + +private: + qreal angle; + qreal speed; + qreal mouseEyeDirection; + QColor color; +}; +//! [0] + +#endif diff --git a/examples/graphicsview/diagramscene/arrow.cpp b/examples/graphicsview/diagramscene/arrow.cpp new file mode 100644 index 0000000000..188928d1d8 --- /dev/null +++ b/examples/graphicsview/diagramscene/arrow.cpp @@ -0,0 +1,146 @@ +/**************************************************************************** +** +** 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> + +#include "arrow.h" +#include <math.h> + +const qreal Pi = 3.14; + +//! [0] +Arrow::Arrow(DiagramItem *startItem, DiagramItem *endItem, + QGraphicsItem *parent, QGraphicsScene *scene) + : QGraphicsLineItem(parent, scene) +{ + myStartItem = startItem; + myEndItem = endItem; + setFlag(QGraphicsItem::ItemIsSelectable, true); + myColor = Qt::black; + setPen(QPen(myColor, 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)); +} +//! [0] + +//! [1] +QRectF Arrow::boundingRect() const +{ + qreal extra = (pen().width() + 20) / 2.0; + + return QRectF(line().p1(), QSizeF(line().p2().x() - line().p1().x(), + line().p2().y() - line().p1().y())) + .normalized() + .adjusted(-extra, -extra, extra, extra); +} +//! [1] + +//! [2] +QPainterPath Arrow::shape() const +{ + QPainterPath path = QGraphicsLineItem::shape(); + path.addPolygon(arrowHead); + return path; +} +//! [2] + +//! [3] +void Arrow::updatePosition() +{ + QLineF line(mapFromItem(myStartItem, 0, 0), mapFromItem(myEndItem, 0, 0)); + setLine(line); +} +//! [3] + +//! [4] +void Arrow::paint(QPainter *painter, const QStyleOptionGraphicsItem *, + QWidget *) +{ + if (myStartItem->collidesWithItem(myEndItem)) + return; + + QPen myPen = pen(); + myPen.setColor(myColor); + qreal arrowSize = 20; + painter->setPen(myPen); + painter->setBrush(myColor); +//! [4] //! [5] + + QLineF centerLine(myStartItem->pos(), myEndItem->pos()); + QPolygonF endPolygon = myEndItem->polygon(); + QPointF p1 = endPolygon.first() + myEndItem->pos(); + QPointF p2; + QPointF intersectPoint; + QLineF polyLine; + for (int i = 1; i < endPolygon.count(); ++i) { + p2 = endPolygon.at(i) + myEndItem->pos(); + polyLine = QLineF(p1, p2); + QLineF::IntersectType intersectType = + polyLine.intersect(centerLine, &intersectPoint); + if (intersectType == QLineF::BoundedIntersection) + break; + p1 = p2; + } + + setLine(QLineF(intersectPoint, myStartItem->pos())); +//! [5] //! [6] + + double angle = ::acos(line().dx() / line().length()); + if (line().dy() >= 0) + angle = (Pi * 2) - angle; + + QPointF arrowP1 = line().p1() + QPointF(sin(angle + Pi / 3) * arrowSize, + cos(angle + Pi / 3) * arrowSize); + QPointF arrowP2 = line().p1() + QPointF(sin(angle + Pi - Pi / 3) * arrowSize, + cos(angle + Pi - Pi / 3) * arrowSize); + + arrowHead.clear(); + arrowHead << line().p1() << arrowP1 << arrowP2; +//! [6] //! [7] + painter->drawLine(line()); + painter->drawPolygon(arrowHead); + if (isSelected()) { + painter->setPen(QPen(myColor, 1, Qt::DashLine)); + QLineF myLine = line(); + myLine.translate(0, 4.0); + painter->drawLine(myLine); + myLine.translate(0,-8.0); + painter->drawLine(myLine); + } +} +//! [7] diff --git a/examples/graphicsview/diagramscene/arrow.h b/examples/graphicsview/diagramscene/arrow.h new file mode 100644 index 0000000000..cc6ff9732d --- /dev/null +++ b/examples/graphicsview/diagramscene/arrow.h @@ -0,0 +1,91 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +#ifndef ARROW_H +#define ARROW_H + +#include <QGraphicsLineItem> + +#include "diagramitem.h" + +QT_BEGIN_NAMESPACE +class QGraphicsPolygonItem; +class QGraphicsLineItem; +class QGraphicsScene; +class QRectF; +class QGraphicsSceneMouseEvent; +class QPainterPath; +QT_END_NAMESPACE + +//! [0] +class Arrow : public QGraphicsLineItem +{ +public: + enum { Type = UserType + 4 }; + + Arrow(DiagramItem *startItem, DiagramItem *endItem, + QGraphicsItem *parent = 0, QGraphicsScene *scene = 0); + + int type() const + { return Type; } + QRectF boundingRect() const; + QPainterPath shape() const; + void setColor(const QColor &color) + { myColor = color; } + DiagramItem *startItem() const + { return myStartItem; } + DiagramItem *endItem() const + { return myEndItem; } + + void updatePosition(); + +protected: + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, + QWidget *widget = 0); + +private: + DiagramItem *myStartItem; + DiagramItem *myEndItem; + QColor myColor; + QPolygonF arrowHead; +}; +//! [0] + +#endif diff --git a/examples/graphicsview/diagramscene/diagramitem.cpp b/examples/graphicsview/diagramscene/diagramitem.cpp new file mode 100644 index 0000000000..cd7ce0c6e1 --- /dev/null +++ b/examples/graphicsview/diagramscene/diagramitem.cpp @@ -0,0 +1,152 @@ +/**************************************************************************** +** +** 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> + +#include "diagramitem.h" +#include "arrow.h" + +//! [0] +DiagramItem::DiagramItem(DiagramType diagramType, QMenu *contextMenu, + QGraphicsItem *parent, QGraphicsScene *scene) + : QGraphicsPolygonItem(parent, scene) +{ + myDiagramType = diagramType; + myContextMenu = contextMenu; + + QPainterPath path; + switch (myDiagramType) { + case StartEnd: + path.moveTo(200, 50); + path.arcTo(150, 0, 50, 50, 0, 90); + path.arcTo(50, 0, 50, 50, 90, 90); + path.arcTo(50, 50, 50, 50, 180, 90); + path.arcTo(150, 50, 50, 50, 270, 90); + path.lineTo(200, 25); + myPolygon = path.toFillPolygon(); + break; + case Conditional: + myPolygon << QPointF(-100, 0) << QPointF(0, 100) + << QPointF(100, 0) << QPointF(0, -100) + << QPointF(-100, 0); + break; + case Step: + myPolygon << QPointF(-100, -100) << QPointF(100, -100) + << QPointF(100, 100) << QPointF(-100, 100) + << QPointF(-100, -100); + break; + default: + myPolygon << QPointF(-120, -80) << QPointF(-70, 80) + << QPointF(120, 80) << QPointF(70, -80) + << QPointF(-120, -80); + break; + } + setPolygon(myPolygon); + setFlag(QGraphicsItem::ItemIsMovable, true); + setFlag(QGraphicsItem::ItemIsSelectable, true); + setFlag(QGraphicsItem::ItemSendsGeometryChanges, true); +} +//! [0] + +//! [1] +void DiagramItem::removeArrow(Arrow *arrow) +{ + int index = arrows.indexOf(arrow); + + if (index != -1) + arrows.removeAt(index); +} +//! [1] + +//! [2] +void DiagramItem::removeArrows() +{ + foreach (Arrow *arrow, arrows) { + arrow->startItem()->removeArrow(arrow); + arrow->endItem()->removeArrow(arrow); + scene()->removeItem(arrow); + delete arrow; + } +} +//! [2] + +//! [3] +void DiagramItem::addArrow(Arrow *arrow) +{ + arrows.append(arrow); +} +//! [3] + +//! [4] +QPixmap DiagramItem::image() const +{ + QPixmap pixmap(250, 250); + pixmap.fill(Qt::transparent); + QPainter painter(&pixmap); + painter.setPen(QPen(Qt::black, 8)); + painter.translate(125, 125); + painter.drawPolyline(myPolygon); + + return pixmap; +} +//! [4] + +//! [5] +void DiagramItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) +{ + scene()->clearSelection(); + setSelected(true); + myContextMenu->exec(event->screenPos()); +} +//! [5] + +//! [6] +QVariant DiagramItem::itemChange(GraphicsItemChange change, + const QVariant &value) +{ + if (change == QGraphicsItem::ItemPositionChange) { + foreach (Arrow *arrow, arrows) { + arrow->updatePosition(); + } + } + + return value; +} +//! [6] diff --git a/examples/graphicsview/diagramscene/diagramitem.h b/examples/graphicsview/diagramscene/diagramitem.h new file mode 100644 index 0000000000..44a8b6dad5 --- /dev/null +++ b/examples/graphicsview/diagramscene/diagramitem.h @@ -0,0 +1,96 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +#ifndef DIAGRAMITEM_H +#define DIAGRAMITEM_H + +#include <QGraphicsPixmapItem> +#include <QList> + +QT_BEGIN_NAMESPACE +class QPixmap; +class QGraphicsItem; +class QGraphicsScene; +class QTextEdit; +class QGraphicsSceneMouseEvent; +class QMenu; +class QGraphicsSceneContextMenuEvent; +class QPainter; +class QStyleOptionGraphicsItem; +class QWidget; +class QPolygonF; +QT_END_NAMESPACE + +class Arrow; + +//! [0] +class DiagramItem : public QGraphicsPolygonItem +{ +public: + enum { Type = UserType + 15 }; + enum DiagramType { Step, Conditional, StartEnd, Io }; + + DiagramItem(DiagramType diagramType, QMenu *contextMenu, + QGraphicsItem *parent = 0, QGraphicsScene *scene = 0); + + void removeArrow(Arrow *arrow); + void removeArrows(); + DiagramType diagramType() const + { return myDiagramType; } + QPolygonF polygon() const + { return myPolygon; } + void addArrow(Arrow *arrow); + QPixmap image() const; + int type() const + { return Type;} + +protected: + void contextMenuEvent(QGraphicsSceneContextMenuEvent *event); + QVariant itemChange(GraphicsItemChange change, const QVariant &value); + +private: + DiagramType myDiagramType; + QPolygonF myPolygon; + QMenu *myContextMenu; + QList<Arrow *> arrows; +}; +//! [0] + +#endif diff --git a/examples/graphicsview/diagramscene/diagramscene.cpp b/examples/graphicsview/diagramscene/diagramscene.cpp new file mode 100644 index 0000000000..7592224db7 --- /dev/null +++ b/examples/graphicsview/diagramscene/diagramscene.cpp @@ -0,0 +1,240 @@ +/**************************************************************************** +** +** 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> + +#include "diagramscene.h" +#include "arrow.h" + +//! [0] +DiagramScene::DiagramScene(QMenu *itemMenu, QObject *parent) + : QGraphicsScene(parent) +{ + myItemMenu = itemMenu; + myMode = MoveItem; + myItemType = DiagramItem::Step; + line = 0; + textItem = 0; + myItemColor = Qt::white; + myTextColor = Qt::black; + myLineColor = Qt::black; +} +//! [0] + +//! [1] +void DiagramScene::setLineColor(const QColor &color) +{ + myLineColor = color; + if (isItemChange(Arrow::Type)) { + Arrow *item = + qgraphicsitem_cast<Arrow *>(selectedItems().first()); + item->setColor(myLineColor); + update(); + } +} +//! [1] + +//! [2] +void DiagramScene::setTextColor(const QColor &color) +{ + myTextColor = color; + if (isItemChange(DiagramTextItem::Type)) { + DiagramTextItem *item = + qgraphicsitem_cast<DiagramTextItem *>(selectedItems().first()); + item->setDefaultTextColor(myTextColor); + } +} +//! [2] + +//! [3] +void DiagramScene::setItemColor(const QColor &color) +{ + myItemColor = color; + if (isItemChange(DiagramItem::Type)) { + DiagramItem *item = + qgraphicsitem_cast<DiagramItem *>(selectedItems().first()); + item->setBrush(myItemColor); + } +} +//! [3] + +//! [4] +void DiagramScene::setFont(const QFont &font) +{ + myFont = font; + + if (isItemChange(DiagramTextItem::Type)) { + QGraphicsTextItem *item = + qgraphicsitem_cast<DiagramTextItem *>(selectedItems().first()); + //At this point the selection can change so the first selected item might not be a DiagramTextItem + if (item) + item->setFont(myFont); + } +} +//! [4] + +void DiagramScene::setMode(Mode mode) +{ + myMode = mode; +} + +void DiagramScene::setItemType(DiagramItem::DiagramType type) +{ + myItemType = type; +} + +//! [5] +void DiagramScene::editorLostFocus(DiagramTextItem *item) +{ + QTextCursor cursor = item->textCursor(); + cursor.clearSelection(); + item->setTextCursor(cursor); + + if (item->toPlainText().isEmpty()) { + removeItem(item); + item->deleteLater(); + } +} +//! [5] + +//! [6] +void DiagramScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent) +{ + if (mouseEvent->button() != Qt::LeftButton) + return; + + DiagramItem *item; + switch (myMode) { + case InsertItem: + item = new DiagramItem(myItemType, myItemMenu); + item->setBrush(myItemColor); + addItem(item); + item->setPos(mouseEvent->scenePos()); + emit itemInserted(item); + break; +//! [6] //! [7] + case InsertLine: + line = new QGraphicsLineItem(QLineF(mouseEvent->scenePos(), + mouseEvent->scenePos())); + line->setPen(QPen(myLineColor, 2)); + addItem(line); + break; +//! [7] //! [8] + case InsertText: + textItem = new DiagramTextItem(); + textItem->setFont(myFont); + textItem->setTextInteractionFlags(Qt::TextEditorInteraction); + textItem->setZValue(1000.0); + connect(textItem, SIGNAL(lostFocus(DiagramTextItem*)), + this, SLOT(editorLostFocus(DiagramTextItem*))); + connect(textItem, SIGNAL(selectedChange(QGraphicsItem*)), + this, SIGNAL(itemSelected(QGraphicsItem*))); + addItem(textItem); + textItem->setDefaultTextColor(myTextColor); + textItem->setPos(mouseEvent->scenePos()); + emit textInserted(textItem); +//! [8] //! [9] + default: + ; + } + QGraphicsScene::mousePressEvent(mouseEvent); +} +//! [9] + +//! [10] +void DiagramScene::mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent) +{ + if (myMode == InsertLine && line != 0) { + QLineF newLine(line->line().p1(), mouseEvent->scenePos()); + line->setLine(newLine); + } else if (myMode == MoveItem) { + QGraphicsScene::mouseMoveEvent(mouseEvent); + } +} +//! [10] + +//! [11] +void DiagramScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent) +{ + if (line != 0 && myMode == InsertLine) { + QList<QGraphicsItem *> startItems = items(line->line().p1()); + if (startItems.count() && startItems.first() == line) + startItems.removeFirst(); + QList<QGraphicsItem *> endItems = items(line->line().p2()); + if (endItems.count() && endItems.first() == line) + endItems.removeFirst(); + + removeItem(line); + delete line; +//! [11] //! [12] + + if (startItems.count() > 0 && endItems.count() > 0 && + startItems.first()->type() == DiagramItem::Type && + endItems.first()->type() == DiagramItem::Type && + startItems.first() != endItems.first()) { + DiagramItem *startItem = + qgraphicsitem_cast<DiagramItem *>(startItems.first()); + DiagramItem *endItem = + qgraphicsitem_cast<DiagramItem *>(endItems.first()); + Arrow *arrow = new Arrow(startItem, endItem); + arrow->setColor(myLineColor); + startItem->addArrow(arrow); + endItem->addArrow(arrow); + arrow->setZValue(-1000.0); + addItem(arrow); + arrow->updatePosition(); + } + } +//! [12] //! [13] + line = 0; + QGraphicsScene::mouseReleaseEvent(mouseEvent); +} +//! [13] + +//! [14] +bool DiagramScene::isItemChange(int type) +{ + foreach (QGraphicsItem *item, selectedItems()) { + if (item->type() == type) + return true; + } + return false; +} +//! [14] diff --git a/examples/graphicsview/diagramscene/diagramscene.h b/examples/graphicsview/diagramscene/diagramscene.h new file mode 100644 index 0000000000..4ebe1d2a71 --- /dev/null +++ b/examples/graphicsview/diagramscene/diagramscene.h @@ -0,0 +1,112 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +#ifndef DIAGRAMSCENE_H +#define DIAGRAMSCENE_H + +#include <QGraphicsScene> +#include "diagramitem.h" +#include "diagramtextitem.h" + +QT_BEGIN_NAMESPACE +class QGraphicsSceneMouseEvent; +class QMenu; +class QPointF; +class QGraphicsLineItem; +class QFont; +class QGraphicsTextItem; +class QColor; +QT_END_NAMESPACE + +//! [0] +class DiagramScene : public QGraphicsScene +{ + Q_OBJECT + +public: + enum Mode { InsertItem, InsertLine, InsertText, MoveItem }; + + DiagramScene(QMenu *itemMenu, QObject *parent = 0); + QFont font() const + { return myFont; } + QColor textColor() const + { return myTextColor; } + QColor itemColor() const + { return myItemColor; } + QColor lineColor() const + { return myLineColor; } + void setLineColor(const QColor &color); + void setTextColor(const QColor &color); + void setItemColor(const QColor &color); + void setFont(const QFont &font); + +public slots: + void setMode(Mode mode); + void setItemType(DiagramItem::DiagramType type); + void editorLostFocus(DiagramTextItem *item); + +signals: + void itemInserted(DiagramItem *item); + void textInserted(QGraphicsTextItem *item); + void itemSelected(QGraphicsItem *item); + +protected: + void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent); + void mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent); + void mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent); + +private: + bool isItemChange(int type); + + DiagramItem::DiagramType myItemType; + QMenu *myItemMenu; + Mode myMode; + bool leftButtonDown; + QPointF startPoint; + QGraphicsLineItem *line; + QFont myFont; + DiagramTextItem *textItem; + QColor myTextColor; + QColor myItemColor; + QColor myLineColor; +}; +//! [0] + +#endif diff --git a/examples/graphicsview/diagramscene/diagramscene.pro b/examples/graphicsview/diagramscene/diagramscene.pro new file mode 100644 index 0000000000..02660b1427 --- /dev/null +++ b/examples/graphicsview/diagramscene/diagramscene.pro @@ -0,0 +1,22 @@ +HEADERS = mainwindow.h \ + diagramitem.h \ + diagramscene.h \ + arrow.h \ + diagramtextitem.h +SOURCES = mainwindow.cpp \ + diagramitem.cpp \ + main.cpp \ + arrow.cpp \ + diagramtextitem.cpp \ + diagramscene.cpp +RESOURCES = diagramscene.qrc + + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/graphicsview/diagramscene +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS diagramscene.pro images +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/graphicsview/diagramscene +INSTALLS += target sources + +symbian: include($$QT_SOURCE_TREE/examples/symbianpkgrules.pri) + diff --git a/examples/graphicsview/diagramscene/diagramscene.qrc b/examples/graphicsview/diagramscene/diagramscene.qrc new file mode 100644 index 0000000000..a11158402f --- /dev/null +++ b/examples/graphicsview/diagramscene/diagramscene.qrc @@ -0,0 +1,20 @@ +<!DOCTYPE RCC><RCC version="1.0"> +<qresource> + <file>images/pointer.png</file> + <file>images/linepointer.png</file> + <file>images/textpointer.png</file> + <file>images/bold.png</file> + <file>images/italic.png</file> + <file>images/underline.png</file> + <file>images/floodfill.png</file> + <file>images/bringtofront.png</file> + <file>images/delete.png</file> + <file>images/sendtoback.png</file> + <file>images/linecolor.png</file> + <file>images/background1.png</file> + <file>images/background2.png</file> + <file>images/background3.png</file> + <file>images/background4.png</file> +</qresource> +</RCC> + diff --git a/examples/graphicsview/diagramscene/diagramtextitem.cpp b/examples/graphicsview/diagramscene/diagramtextitem.cpp new file mode 100644 index 0000000000..588603eda5 --- /dev/null +++ b/examples/graphicsview/diagramscene/diagramtextitem.cpp @@ -0,0 +1,81 @@ +/**************************************************************************** +** +** 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> + +#include "diagramtextitem.h" +#include "diagramscene.h" + +//! [0] +DiagramTextItem::DiagramTextItem(QGraphicsItem *parent, QGraphicsScene *scene) + : QGraphicsTextItem(parent, scene) +{ + setFlag(QGraphicsItem::ItemIsMovable); + setFlag(QGraphicsItem::ItemIsSelectable); +} +//! [0] + +//! [1] +QVariant DiagramTextItem::itemChange(GraphicsItemChange change, + const QVariant &value) +{ + if (change == QGraphicsItem::ItemSelectedHasChanged) + emit selectedChange(this); + return value; +} +//! [1] + +//! [2] +void DiagramTextItem::focusOutEvent(QFocusEvent *event) +{ + setTextInteractionFlags(Qt::NoTextInteraction); + emit lostFocus(this); + QGraphicsTextItem::focusOutEvent(event); +} +//! [2] + +//! [5] +void DiagramTextItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) +{ + if (textInteractionFlags() == Qt::NoTextInteraction) + setTextInteractionFlags(Qt::TextEditorInteraction); + QGraphicsTextItem::mouseDoubleClickEvent(event); +} +//! [5] diff --git a/examples/graphicsview/diagramscene/diagramtextitem.h b/examples/graphicsview/diagramscene/diagramtextitem.h new file mode 100644 index 0000000000..5e3505f6f0 --- /dev/null +++ b/examples/graphicsview/diagramscene/diagramtextitem.h @@ -0,0 +1,78 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +#ifndef DIAGRAMTEXTITEM_H +#define DIAGRAMTEXTITEM_H + +#include <QGraphicsTextItem> +#include <QPen> + +QT_BEGIN_NAMESPACE +class QFocusEvent; +class QGraphicsItem; +class QGraphicsScene; +class QGraphicsSceneMouseEvent; +QT_END_NAMESPACE + +//! [0] +class DiagramTextItem : public QGraphicsTextItem +{ + Q_OBJECT + +public: + enum { Type = UserType + 3 }; + + DiagramTextItem(QGraphicsItem *parent = 0, QGraphicsScene *scene = 0); + + int type() const + { return Type; } + +signals: + void lostFocus(DiagramTextItem *item); + void selectedChange(QGraphicsItem *item); + +protected: + QVariant itemChange(GraphicsItemChange change, const QVariant &value); + void focusOutEvent(QFocusEvent *event); + void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event); +}; +//! [0] + +#endif diff --git a/examples/graphicsview/diagramscene/images/background1.png b/examples/graphicsview/diagramscene/images/background1.png Binary files differnew file mode 100644 index 0000000000..0f93c6bf42 --- /dev/null +++ b/examples/graphicsview/diagramscene/images/background1.png diff --git a/examples/graphicsview/diagramscene/images/background2.png b/examples/graphicsview/diagramscene/images/background2.png Binary files differnew file mode 100644 index 0000000000..1e293db67a --- /dev/null +++ b/examples/graphicsview/diagramscene/images/background2.png diff --git a/examples/graphicsview/diagramscene/images/background3.png b/examples/graphicsview/diagramscene/images/background3.png Binary files differnew file mode 100644 index 0000000000..3db4f8ea5f --- /dev/null +++ b/examples/graphicsview/diagramscene/images/background3.png diff --git a/examples/graphicsview/diagramscene/images/background4.png b/examples/graphicsview/diagramscene/images/background4.png Binary files differnew file mode 100644 index 0000000000..9c1f3bfd77 --- /dev/null +++ b/examples/graphicsview/diagramscene/images/background4.png diff --git a/examples/graphicsview/diagramscene/images/bold.png b/examples/graphicsview/diagramscene/images/bold.png Binary files differnew file mode 100644 index 0000000000..986e65e250 --- /dev/null +++ b/examples/graphicsview/diagramscene/images/bold.png diff --git a/examples/graphicsview/diagramscene/images/bringtofront.png b/examples/graphicsview/diagramscene/images/bringtofront.png Binary files differnew file mode 100644 index 0000000000..bda27578a2 --- /dev/null +++ b/examples/graphicsview/diagramscene/images/bringtofront.png diff --git a/examples/graphicsview/diagramscene/images/delete.png b/examples/graphicsview/diagramscene/images/delete.png Binary files differnew file mode 100644 index 0000000000..df2a147d24 --- /dev/null +++ b/examples/graphicsview/diagramscene/images/delete.png diff --git a/examples/graphicsview/diagramscene/images/floodfill.png b/examples/graphicsview/diagramscene/images/floodfill.png Binary files differnew file mode 100644 index 0000000000..54c0dae237 --- /dev/null +++ b/examples/graphicsview/diagramscene/images/floodfill.png diff --git a/examples/graphicsview/diagramscene/images/italic.png b/examples/graphicsview/diagramscene/images/italic.png Binary files differnew file mode 100644 index 0000000000..9a438b57ad --- /dev/null +++ b/examples/graphicsview/diagramscene/images/italic.png diff --git a/examples/graphicsview/diagramscene/images/linecolor.png b/examples/graphicsview/diagramscene/images/linecolor.png Binary files differnew file mode 100644 index 0000000000..98a821f27e --- /dev/null +++ b/examples/graphicsview/diagramscene/images/linecolor.png diff --git a/examples/graphicsview/diagramscene/images/linepointer.png b/examples/graphicsview/diagramscene/images/linepointer.png Binary files differnew file mode 100644 index 0000000000..66933d43b3 --- /dev/null +++ b/examples/graphicsview/diagramscene/images/linepointer.png diff --git a/examples/graphicsview/diagramscene/images/pointer.png b/examples/graphicsview/diagramscene/images/pointer.png Binary files differnew file mode 100644 index 0000000000..0b0b0aa69d --- /dev/null +++ b/examples/graphicsview/diagramscene/images/pointer.png diff --git a/examples/graphicsview/diagramscene/images/sendtoback.png b/examples/graphicsview/diagramscene/images/sendtoback.png Binary files differnew file mode 100644 index 0000000000..5aa3b0a243 --- /dev/null +++ b/examples/graphicsview/diagramscene/images/sendtoback.png diff --git a/examples/graphicsview/diagramscene/images/textpointer.png b/examples/graphicsview/diagramscene/images/textpointer.png Binary files differnew file mode 100644 index 0000000000..b25832cada --- /dev/null +++ b/examples/graphicsview/diagramscene/images/textpointer.png diff --git a/examples/graphicsview/diagramscene/images/underline.png b/examples/graphicsview/diagramscene/images/underline.png Binary files differnew file mode 100644 index 0000000000..9b8209f525 --- /dev/null +++ b/examples/graphicsview/diagramscene/images/underline.png diff --git a/examples/graphicsview/diagramscene/main.cpp b/examples/graphicsview/diagramscene/main.cpp new file mode 100644 index 0000000000..fcd027cb44 --- /dev/null +++ b/examples/graphicsview/diagramscene/main.cpp @@ -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 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> + +#include "mainwindow.h" + +int main(int argv, char *args[]) +{ + Q_INIT_RESOURCE(diagramscene); + + QApplication app(argv, args); + MainWindow mainWindow; + mainWindow.setGeometry(100, 100, 800, 500); + mainWindow.show(); + + return app.exec(); +} diff --git a/examples/graphicsview/diagramscene/mainwindow.cpp b/examples/graphicsview/diagramscene/mainwindow.cpp new file mode 100644 index 0000000000..87eb33e7ee --- /dev/null +++ b/examples/graphicsview/diagramscene/mainwindow.cpp @@ -0,0 +1,662 @@ +/**************************************************************************** +** +** 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> +#include <QLabel> + +#include "mainwindow.h" +#include "diagramitem.h" +#include "diagramscene.h" +#include "diagramtextitem.h" +#include "arrow.h" + +const int InsertTextButton = 10; + +//! [0] +MainWindow::MainWindow() +{ + createActions(); + createToolBox(); + createMenus(); + + scene = new DiagramScene(itemMenu, this); + scene->setSceneRect(QRectF(0, 0, 5000, 5000)); + connect(scene, SIGNAL(itemInserted(DiagramItem*)), + this, SLOT(itemInserted(DiagramItem*))); + connect(scene, SIGNAL(textInserted(QGraphicsTextItem*)), + this, SLOT(textInserted(QGraphicsTextItem*))); + connect(scene, SIGNAL(itemSelected(QGraphicsItem*)), + this, SLOT(itemSelected(QGraphicsItem*))); + createToolbars(); + + QHBoxLayout *layout = new QHBoxLayout; + layout->addWidget(toolBox); + view = new QGraphicsView(scene); + layout->addWidget(view); + + QWidget *widget = new QWidget; + widget->setLayout(layout); + + setCentralWidget(widget); + setWindowTitle(tr("Diagramscene")); + setUnifiedTitleAndToolBarOnMac(true); +} +//! [0] + +//! [1] +void MainWindow::backgroundButtonGroupClicked(QAbstractButton *button) +{ + QList<QAbstractButton *> buttons = backgroundButtonGroup->buttons(); + foreach (QAbstractButton *myButton, buttons) { + if (myButton != button) + button->setChecked(false); + } + QString text = button->text(); + if (text == tr("Blue Grid")) + scene->setBackgroundBrush(QPixmap(":/images/background1.png")); + else if (text == tr("White Grid")) + scene->setBackgroundBrush(QPixmap(":/images/background2.png")); + else if (text == tr("Gray Grid")) + scene->setBackgroundBrush(QPixmap(":/images/background3.png")); + else + scene->setBackgroundBrush(QPixmap(":/images/background4.png")); + + scene->update(); + view->update(); +} +//! [1] + +//! [2] +void MainWindow::buttonGroupClicked(int id) +{ + QList<QAbstractButton *> buttons = buttonGroup->buttons(); + foreach (QAbstractButton *button, buttons) { + if (buttonGroup->button(id) != button) + button->setChecked(false); + } + if (id == InsertTextButton) { + scene->setMode(DiagramScene::InsertText); + } else { + scene->setItemType(DiagramItem::DiagramType(id)); + scene->setMode(DiagramScene::InsertItem); + } +} +//! [2] + +//! [3] +void MainWindow::deleteItem() +{ + foreach (QGraphicsItem *item, scene->selectedItems()) { + if (item->type() == Arrow::Type) { + scene->removeItem(item); + Arrow *arrow = qgraphicsitem_cast<Arrow *>(item); + arrow->startItem()->removeArrow(arrow); + arrow->endItem()->removeArrow(arrow); + delete item; + } + } + + foreach (QGraphicsItem *item, scene->selectedItems()) { + if (item->type() == DiagramItem::Type) { + qgraphicsitem_cast<DiagramItem *>(item)->removeArrows(); + } + scene->removeItem(item); + delete item; + } +} +//! [3] + +//! [4] +void MainWindow::pointerGroupClicked(int) +{ + scene->setMode(DiagramScene::Mode(pointerTypeGroup->checkedId())); +} +//! [4] + +//! [5] +void MainWindow::bringToFront() +{ + if (scene->selectedItems().isEmpty()) + return; + + QGraphicsItem *selectedItem = scene->selectedItems().first(); + QList<QGraphicsItem *> overlapItems = selectedItem->collidingItems(); + + qreal zValue = 0; + foreach (QGraphicsItem *item, overlapItems) { + if (item->zValue() >= zValue && + item->type() == DiagramItem::Type) + zValue = item->zValue() + 0.1; + } + selectedItem->setZValue(zValue); +} +//! [5] + +//! [6] +void MainWindow::sendToBack() +{ + if (scene->selectedItems().isEmpty()) + return; + + QGraphicsItem *selectedItem = scene->selectedItems().first(); + QList<QGraphicsItem *> overlapItems = selectedItem->collidingItems(); + + qreal zValue = 0; + foreach (QGraphicsItem *item, overlapItems) { + if (item->zValue() <= zValue && + item->type() == DiagramItem::Type) + zValue = item->zValue() - 0.1; + } + selectedItem->setZValue(zValue); +} +//! [6] + +//! [7] +void MainWindow::itemInserted(DiagramItem *item) +{ + pointerTypeGroup->button(int(DiagramScene::MoveItem))->setChecked(true); + scene->setMode(DiagramScene::Mode(pointerTypeGroup->checkedId())); + buttonGroup->button(int(item->diagramType()))->setChecked(false); +} +//! [7] + +//! [8] +void MainWindow::textInserted(QGraphicsTextItem *) +{ + buttonGroup->button(InsertTextButton)->setChecked(false); + scene->setMode(DiagramScene::Mode(pointerTypeGroup->checkedId())); +} +//! [8] + +//! [9] +void MainWindow::currentFontChanged(const QFont &) +{ + handleFontChange(); +} +//! [9] + +//! [10] +void MainWindow::fontSizeChanged(const QString &) +{ + handleFontChange(); +} +//! [10] + +//! [11] +void MainWindow::sceneScaleChanged(const QString &scale) +{ + double newScale = scale.left(scale.indexOf(tr("%"))).toDouble() / 100.0; + QMatrix oldMatrix = view->matrix(); + view->resetMatrix(); + view->translate(oldMatrix.dx(), oldMatrix.dy()); + view->scale(newScale, newScale); +} +//! [11] + +//! [12] +void MainWindow::textColorChanged() +{ + textAction = qobject_cast<QAction *>(sender()); + fontColorToolButton->setIcon(createColorToolButtonIcon( + ":/images/textpointer.png", + qvariant_cast<QColor>(textAction->data()))); + textButtonTriggered(); +} +//! [12] + +//! [13] +void MainWindow::itemColorChanged() +{ + fillAction = qobject_cast<QAction *>(sender()); + fillColorToolButton->setIcon(createColorToolButtonIcon( + ":/images/floodfill.png", + qvariant_cast<QColor>(fillAction->data()))); + fillButtonTriggered(); +} +//! [13] + +//! [14] +void MainWindow::lineColorChanged() +{ + lineAction = qobject_cast<QAction *>(sender()); + lineColorToolButton->setIcon(createColorToolButtonIcon( + ":/images/linecolor.png", + qvariant_cast<QColor>(lineAction->data()))); + lineButtonTriggered(); +} +//! [14] + +//! [15] +void MainWindow::textButtonTriggered() +{ + scene->setTextColor(qvariant_cast<QColor>(textAction->data())); +} +//! [15] + +//! [16] +void MainWindow::fillButtonTriggered() +{ + scene->setItemColor(qvariant_cast<QColor>(fillAction->data())); +} +//! [16] + +//! [17] +void MainWindow::lineButtonTriggered() +{ + scene->setLineColor(qvariant_cast<QColor>(lineAction->data())); +} +//! [17] + +//! [18] +void MainWindow::handleFontChange() +{ + QFont font = fontCombo->currentFont(); + font.setPointSize(fontSizeCombo->currentText().toInt()); + font.setWeight(boldAction->isChecked() ? QFont::Bold : QFont::Normal); + font.setItalic(italicAction->isChecked()); + font.setUnderline(underlineAction->isChecked()); + + scene->setFont(font); +} +//! [18] + +//! [19] +void MainWindow::itemSelected(QGraphicsItem *item) +{ + DiagramTextItem *textItem = + qgraphicsitem_cast<DiagramTextItem *>(item); + + QFont font = textItem->font(); + QColor color = textItem->defaultTextColor(); + fontCombo->setCurrentFont(font); + fontSizeCombo->setEditText(QString().setNum(font.pointSize())); + boldAction->setChecked(font.weight() == QFont::Bold); + italicAction->setChecked(font.italic()); + underlineAction->setChecked(font.underline()); +} +//! [19] + +//! [20] +void MainWindow::about() +{ + QMessageBox::about(this, tr("About Diagram Scene"), + tr("The <b>Diagram Scene</b> example shows " + "use of the graphics framework.")); +} +//! [20] + +//! [21] +void MainWindow::createToolBox() +{ + buttonGroup = new QButtonGroup(this); + buttonGroup->setExclusive(false); + connect(buttonGroup, SIGNAL(buttonClicked(int)), + this, SLOT(buttonGroupClicked(int))); + QGridLayout *layout = new QGridLayout; + layout->addWidget(createCellWidget(tr("Conditional"), + DiagramItem::Conditional), 0, 0); + layout->addWidget(createCellWidget(tr("Process"), + DiagramItem::Step),0, 1); + layout->addWidget(createCellWidget(tr("Input/Output"), + DiagramItem::Io), 1, 0); +//! [21] + + QToolButton *textButton = new QToolButton; + textButton->setCheckable(true); + buttonGroup->addButton(textButton, InsertTextButton); + textButton->setIcon(QIcon(QPixmap(":/images/textpointer.png") + .scaled(30, 30))); + textButton->setIconSize(QSize(50, 50)); + QGridLayout *textLayout = new QGridLayout; + textLayout->addWidget(textButton, 0, 0, Qt::AlignHCenter); + textLayout->addWidget(new QLabel(tr("Text")), 1, 0, Qt::AlignCenter); + QWidget *textWidget = new QWidget; + textWidget->setLayout(textLayout); + layout->addWidget(textWidget, 1, 1); + + layout->setRowStretch(3, 10); + layout->setColumnStretch(2, 10); + + QWidget *itemWidget = new QWidget; + itemWidget->setLayout(layout); + + backgroundButtonGroup = new QButtonGroup(this); + connect(backgroundButtonGroup, SIGNAL(buttonClicked(QAbstractButton*)), + this, SLOT(backgroundButtonGroupClicked(QAbstractButton*))); + + QGridLayout *backgroundLayout = new QGridLayout; + backgroundLayout->addWidget(createBackgroundCellWidget(tr("Blue Grid"), + ":/images/background1.png"), 0, 0); + backgroundLayout->addWidget(createBackgroundCellWidget(tr("White Grid"), + ":/images/background2.png"), 0, 1); + backgroundLayout->addWidget(createBackgroundCellWidget(tr("Gray Grid"), + ":/images/background3.png"), 1, 0); + backgroundLayout->addWidget(createBackgroundCellWidget(tr("No Grid"), + ":/images/background4.png"), 1, 1); + + backgroundLayout->setRowStretch(2, 10); + backgroundLayout->setColumnStretch(2, 10); + + QWidget *backgroundWidget = new QWidget; + backgroundWidget->setLayout(backgroundLayout); + + +//! [22] + toolBox = new QToolBox; + toolBox->setSizePolicy(QSizePolicy(QSizePolicy::Maximum, QSizePolicy::Ignored)); + toolBox->setMinimumWidth(itemWidget->sizeHint().width()); + toolBox->addItem(itemWidget, tr("Basic Flowchart Shapes")); + toolBox->addItem(backgroundWidget, tr("Backgrounds")); +} +//! [22] + +//! [23] +void MainWindow::createActions() +{ + toFrontAction = new QAction(QIcon(":/images/bringtofront.png"), + tr("Bring to &Front"), this); + toFrontAction->setShortcut(tr("Ctrl+F")); + toFrontAction->setStatusTip(tr("Bring item to front")); + connect(toFrontAction, SIGNAL(triggered()), + this, SLOT(bringToFront())); +//! [23] + + sendBackAction = new QAction(QIcon(":/images/sendtoback.png"), + tr("Send to &Back"), this); + sendBackAction->setShortcut(tr("Ctrl+B")); + sendBackAction->setStatusTip(tr("Send item to back")); + connect(sendBackAction, SIGNAL(triggered()), + this, SLOT(sendToBack())); + + deleteAction = new QAction(QIcon(":/images/delete.png"), + tr("&Delete"), this); + deleteAction->setShortcut(tr("Delete")); + deleteAction->setStatusTip(tr("Delete item from diagram")); + connect(deleteAction, SIGNAL(triggered()), + this, SLOT(deleteItem())); + + exitAction = new QAction(tr("E&xit"), this); + exitAction->setShortcuts(QKeySequence::Quit); + exitAction->setStatusTip(tr("Quit Scenediagram example")); + connect(exitAction, SIGNAL(triggered()), this, SLOT(close())); + + boldAction = new QAction(tr("Bold"), this); + boldAction->setCheckable(true); + QPixmap pixmap(":/images/bold.png"); + boldAction->setIcon(QIcon(pixmap)); + boldAction->setShortcut(tr("Ctrl+B")); + connect(boldAction, SIGNAL(triggered()), + this, SLOT(handleFontChange())); + + italicAction = new QAction(QIcon(":/images/italic.png"), + tr("Italic"), this); + italicAction->setCheckable(true); + italicAction->setShortcut(tr("Ctrl+I")); + connect(italicAction, SIGNAL(triggered()), + this, SLOT(handleFontChange())); + + underlineAction = new QAction(QIcon(":/images/underline.png"), + tr("Underline"), this); + underlineAction->setCheckable(true); + underlineAction->setShortcut(tr("Ctrl+U")); + connect(underlineAction, SIGNAL(triggered()), + this, SLOT(handleFontChange())); + + aboutAction = new QAction(tr("A&bout"), this); + aboutAction->setShortcut(tr("Ctrl+B")); + connect(aboutAction, SIGNAL(triggered()), + this, SLOT(about())); +} + +//! [24] +void MainWindow::createMenus() +{ + fileMenu = menuBar()->addMenu(tr("&File")); + fileMenu->addAction(exitAction); + + itemMenu = menuBar()->addMenu(tr("&Item")); + itemMenu->addAction(deleteAction); + itemMenu->addSeparator(); + itemMenu->addAction(toFrontAction); + itemMenu->addAction(sendBackAction); + + aboutMenu = menuBar()->addMenu(tr("&Help")); + aboutMenu->addAction(aboutAction); +} +//! [24] + +//! [25] +void MainWindow::createToolbars() +{ +//! [25] + editToolBar = addToolBar(tr("Edit")); + editToolBar->addAction(deleteAction); + editToolBar->addAction(toFrontAction); + editToolBar->addAction(sendBackAction); + + fontCombo = new QFontComboBox(); + connect(fontCombo, SIGNAL(currentFontChanged(QFont)), + this, SLOT(currentFontChanged(QFont))); + + fontSizeCombo = new QComboBox; + fontSizeCombo->setEditable(true); + for (int i = 8; i < 30; i = i + 2) + fontSizeCombo->addItem(QString().setNum(i)); + QIntValidator *validator = new QIntValidator(2, 64, this); + fontSizeCombo->setValidator(validator); + connect(fontSizeCombo, SIGNAL(currentIndexChanged(QString)), + this, SLOT(fontSizeChanged(QString))); + + fontColorToolButton = new QToolButton; + fontColorToolButton->setPopupMode(QToolButton::MenuButtonPopup); + fontColorToolButton->setMenu(createColorMenu(SLOT(textColorChanged()), + Qt::black)); + textAction = fontColorToolButton->menu()->defaultAction(); + fontColorToolButton->setIcon(createColorToolButtonIcon( + ":/images/textpointer.png", Qt::black)); + fontColorToolButton->setAutoFillBackground(true); + connect(fontColorToolButton, SIGNAL(clicked()), + this, SLOT(textButtonTriggered())); + +//! [26] + fillColorToolButton = new QToolButton; + fillColorToolButton->setPopupMode(QToolButton::MenuButtonPopup); + fillColorToolButton->setMenu(createColorMenu(SLOT(itemColorChanged()), + Qt::white)); + fillAction = fillColorToolButton->menu()->defaultAction(); + fillColorToolButton->setIcon(createColorToolButtonIcon( + ":/images/floodfill.png", Qt::white)); + connect(fillColorToolButton, SIGNAL(clicked()), + this, SLOT(fillButtonTriggered())); +//! [26] + + lineColorToolButton = new QToolButton; + lineColorToolButton->setPopupMode(QToolButton::MenuButtonPopup); + lineColorToolButton->setMenu(createColorMenu(SLOT(lineColorChanged()), + Qt::black)); + lineAction = lineColorToolButton->menu()->defaultAction(); + lineColorToolButton->setIcon(createColorToolButtonIcon( + ":/images/linecolor.png", Qt::black)); + connect(lineColorToolButton, SIGNAL(clicked()), + this, SLOT(lineButtonTriggered())); + + textToolBar = addToolBar(tr("Font")); + textToolBar->addWidget(fontCombo); + textToolBar->addWidget(fontSizeCombo); + textToolBar->addAction(boldAction); + textToolBar->addAction(italicAction); + textToolBar->addAction(underlineAction); + + colorToolBar = addToolBar(tr("Color")); + colorToolBar->addWidget(fontColorToolButton); + colorToolBar->addWidget(fillColorToolButton); + colorToolBar->addWidget(lineColorToolButton); + + QToolButton *pointerButton = new QToolButton; + pointerButton->setCheckable(true); + pointerButton->setChecked(true); + pointerButton->setIcon(QIcon(":/images/pointer.png")); + QToolButton *linePointerButton = new QToolButton; + linePointerButton->setCheckable(true); + linePointerButton->setIcon(QIcon(":/images/linepointer.png")); + + pointerTypeGroup = new QButtonGroup(this); + pointerTypeGroup->addButton(pointerButton, int(DiagramScene::MoveItem)); + pointerTypeGroup->addButton(linePointerButton, + int(DiagramScene::InsertLine)); + connect(pointerTypeGroup, SIGNAL(buttonClicked(int)), + this, SLOT(pointerGroupClicked(int))); + + sceneScaleCombo = new QComboBox; + QStringList scales; + scales << tr("50%") << tr("75%") << tr("100%") << tr("125%") << tr("150%"); + sceneScaleCombo->addItems(scales); + sceneScaleCombo->setCurrentIndex(2); + connect(sceneScaleCombo, SIGNAL(currentIndexChanged(QString)), + this, SLOT(sceneScaleChanged(QString))); + + pointerToolbar = addToolBar(tr("Pointer type")); + pointerToolbar->addWidget(pointerButton); + pointerToolbar->addWidget(linePointerButton); + pointerToolbar->addWidget(sceneScaleCombo); +//! [27] +} +//! [27] + +//! [28] +QWidget *MainWindow::createBackgroundCellWidget(const QString &text, + const QString &image) +{ + QToolButton *button = new QToolButton; + button->setText(text); + button->setIcon(QIcon(image)); + button->setIconSize(QSize(50, 50)); + button->setCheckable(true); + backgroundButtonGroup->addButton(button); + + QGridLayout *layout = new QGridLayout; + layout->addWidget(button, 0, 0, Qt::AlignHCenter); + layout->addWidget(new QLabel(text), 1, 0, Qt::AlignCenter); + + QWidget *widget = new QWidget; + widget->setLayout(layout); + + return widget; +} +//! [28] + +//! [29] +QWidget *MainWindow::createCellWidget(const QString &text, + DiagramItem::DiagramType type) +{ + + DiagramItem item(type, itemMenu); + QIcon icon(item.image()); + + QToolButton *button = new QToolButton; + button->setIcon(icon); + button->setIconSize(QSize(50, 50)); + button->setCheckable(true); + buttonGroup->addButton(button, int(type)); + + QGridLayout *layout = new QGridLayout; + layout->addWidget(button, 0, 0, Qt::AlignHCenter); + layout->addWidget(new QLabel(text), 1, 0, Qt::AlignCenter); + + QWidget *widget = new QWidget; + widget->setLayout(layout); + + return widget; +} +//! [29] + +//! [30] +QMenu *MainWindow::createColorMenu(const char *slot, QColor defaultColor) +{ + QList<QColor> colors; + colors << Qt::black << Qt::white << Qt::red << Qt::blue << Qt::yellow; + QStringList names; + names << tr("black") << tr("white") << tr("red") << tr("blue") + << tr("yellow"); + + QMenu *colorMenu = new QMenu(this); + for (int i = 0; i < colors.count(); ++i) { + QAction *action = new QAction(names.at(i), this); + action->setData(colors.at(i)); + action->setIcon(createColorIcon(colors.at(i))); + connect(action, SIGNAL(triggered()), + this, slot); + colorMenu->addAction(action); + if (colors.at(i) == defaultColor) { + colorMenu->setDefaultAction(action); + } + } + return colorMenu; +} +//! [30] + +//! [31] +QIcon MainWindow::createColorToolButtonIcon(const QString &imageFile, + QColor color) +{ + QPixmap pixmap(50, 80); + pixmap.fill(Qt::transparent); + QPainter painter(&pixmap); + QPixmap image(imageFile); + QRect target(0, 0, 50, 60); + QRect source(0, 0, 42, 42); + painter.fillRect(QRect(0, 60, 50, 80), color); + painter.drawPixmap(target, image, source); + + return QIcon(pixmap); +} +//! [31] + +//! [32] +QIcon MainWindow::createColorIcon(QColor color) +{ + QPixmap pixmap(20, 20); + QPainter painter(&pixmap); + painter.setPen(Qt::NoPen); + painter.fillRect(QRect(0, 0, 20, 20), color); + + return QIcon(pixmap); +} +//! [32] diff --git a/examples/graphicsview/diagramscene/mainwindow.h b/examples/graphicsview/diagramscene/mainwindow.h new file mode 100644 index 0000000000..ad76395a69 --- /dev/null +++ b/examples/graphicsview/diagramscene/mainwindow.h @@ -0,0 +1,150 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include <QMainWindow> + +#include "diagramitem.h" + +class DiagramScene; + +QT_BEGIN_NAMESPACE +class QAction; +class QToolBox; +class QSpinBox; +class QComboBox; +class QFontComboBox; +class QButtonGroup; +class QLineEdit; +class QGraphicsTextItem; +class QFont; +class QToolButton; +class QAbstractButton; +class QGraphicsView; +QT_END_NAMESPACE + +//! [0] +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + MainWindow(); + +private slots: + void backgroundButtonGroupClicked(QAbstractButton *button); + void buttonGroupClicked(int id); + void deleteItem(); + void pointerGroupClicked(int id); + void bringToFront(); + void sendToBack(); + void itemInserted(DiagramItem *item); + void textInserted(QGraphicsTextItem *item); + void currentFontChanged(const QFont &font); + void fontSizeChanged(const QString &size); + void sceneScaleChanged(const QString &scale); + void textColorChanged(); + void itemColorChanged(); + void lineColorChanged(); + void textButtonTriggered(); + void fillButtonTriggered(); + void lineButtonTriggered(); + void handleFontChange(); + void itemSelected(QGraphicsItem *item); + void about(); + +private: + void createToolBox(); + void createActions(); + void createMenus(); + void createToolbars(); + QWidget *createBackgroundCellWidget(const QString &text, + const QString &image); + QWidget *createCellWidget(const QString &text, + DiagramItem::DiagramType type); + QMenu *createColorMenu(const char *slot, QColor defaultColor); + QIcon createColorToolButtonIcon(const QString &image, QColor color); + QIcon createColorIcon(QColor color); + + DiagramScene *scene; + QGraphicsView *view; + + QAction *exitAction; + QAction *addAction; + QAction *deleteAction; + + QAction *toFrontAction; + QAction *sendBackAction; + QAction *aboutAction; + + QMenu *fileMenu; + QMenu *itemMenu; + QMenu *aboutMenu; + + QToolBar *textToolBar; + QToolBar *editToolBar; + QToolBar *colorToolBar; + QToolBar *pointerToolbar; + + QComboBox *sceneScaleCombo; + QComboBox *itemColorCombo; + QComboBox *textColorCombo; + QComboBox *fontSizeCombo; + QFontComboBox *fontCombo; + + QToolBox *toolBox; + QButtonGroup *buttonGroup; + QButtonGroup *pointerTypeGroup; + QButtonGroup *backgroundButtonGroup; + QToolButton *fontColorToolButton; + QToolButton *fillColorToolButton; + QToolButton *lineColorToolButton; + QAction *boldAction; + QAction *underlineAction; + QAction *italicAction; + QAction *textAction; + QAction *fillAction; + QAction *lineAction; +}; +//! [0] + +#endif diff --git a/examples/graphicsview/dragdroprobot/coloritem.cpp b/examples/graphicsview/dragdroprobot/coloritem.cpp new file mode 100644 index 0000000000..81e8f5607b --- /dev/null +++ b/examples/graphicsview/dragdroprobot/coloritem.cpp @@ -0,0 +1,142 @@ +/**************************************************************************** +** +** 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> + +#include "coloritem.h" + +//! [0] +ColorItem::ColorItem() + : color(qrand() % 256, qrand() % 256, qrand() % 256) +{ + setToolTip(QString("QColor(%1, %2, %3)\n%4") + .arg(color.red()).arg(color.green()).arg(color.blue()) + .arg("Click and drag this color onto the robot!")); + setCursor(Qt::OpenHandCursor); + setAcceptedMouseButtons(Qt::LeftButton); +} +//! [0] + +//! [1] +QRectF ColorItem::boundingRect() const +{ + return QRectF(-15.5, -15.5, 34, 34); +} +//! [1] + +//! [2] +void ColorItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) +{ + Q_UNUSED(option); + Q_UNUSED(widget); + painter->setPen(Qt::NoPen); + painter->setBrush(Qt::darkGray); + painter->drawEllipse(-12, -12, 30, 30); + painter->setPen(QPen(Qt::black, 1)); + painter->setBrush(QBrush(color)); + painter->drawEllipse(-15, -15, 30, 30); +} +//! [2] + +//! [3] +void ColorItem::mousePressEvent(QGraphicsSceneMouseEvent *) +{ + setCursor(Qt::ClosedHandCursor); +} +//! [3] + +//! [5] +void ColorItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) +{ + if (QLineF(event->screenPos(), event->buttonDownScreenPos(Qt::LeftButton)) + .length() < QApplication::startDragDistance()) { + return; + } + + QDrag *drag = new QDrag(event->widget()); + QMimeData *mime = new QMimeData; + drag->setMimeData(mime); +//! [5] + +//! [6] + static int n = 0; + if (n++ > 2 && (qrand() % 3) == 0) { + QImage image(":/images/head.png"); + mime->setImageData(image); + + drag->setPixmap(QPixmap::fromImage(image).scaled(30, 40)); + drag->setHotSpot(QPoint(15, 30)); +//! [6] +//! [7] + } else { + mime->setColorData(color); + mime->setText(QString("#%1%2%3") + .arg(color.red(), 2, 16, QLatin1Char('0')) + .arg(color.green(), 2, 16, QLatin1Char('0')) + .arg(color.blue(), 2, 16, QLatin1Char('0'))); + + QPixmap pixmap(34, 34); + pixmap.fill(Qt::white); + + QPainter painter(&pixmap); + painter.translate(15, 15); + painter.setRenderHint(QPainter::Antialiasing); + paint(&painter, 0, 0); + painter.end(); + + pixmap.setMask(pixmap.createHeuristicMask()); + + drag->setPixmap(pixmap); + drag->setHotSpot(QPoint(15, 20)); + } +//! [7] + +//! [8] + drag->exec(); + setCursor(Qt::OpenHandCursor); +} +//! [8] + +//! [4] +void ColorItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *) +{ + setCursor(Qt::OpenHandCursor); +} +//! [4] diff --git a/examples/graphicsview/dragdroprobot/coloritem.h b/examples/graphicsview/dragdroprobot/coloritem.h new file mode 100644 index 0000000000..e5874f910d --- /dev/null +++ b/examples/graphicsview/dragdroprobot/coloritem.h @@ -0,0 +1,65 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +#ifndef COLORITEM_H +#define COLORITEM_H + +#include <QGraphicsItem> + +//! [0] +class ColorItem : public QGraphicsItem +{ +public: + ColorItem(); + + QRectF boundingRect() const; + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); + +protected: + void mousePressEvent(QGraphicsSceneMouseEvent *event); + void mouseMoveEvent(QGraphicsSceneMouseEvent *event); + void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); + +private: + QColor color; +}; +//! [0] + +#endif diff --git a/examples/graphicsview/dragdroprobot/dragdroprobot.pro b/examples/graphicsview/dragdroprobot/dragdroprobot.pro new file mode 100644 index 0000000000..736958baa1 --- /dev/null +++ b/examples/graphicsview/dragdroprobot/dragdroprobot.pro @@ -0,0 +1,20 @@ +HEADERS += \ + coloritem.h \ + robot.h + +SOURCES += \ + coloritem.cpp \ + main.cpp \ + robot.cpp + +RESOURCES += \ + robot.qrc + + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/graphicsview/dragdroprobot +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS dragdroprobot.pro images +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/graphicsview/dragdroprobot +INSTALLS += target sources + +symbian: include($$QT_SOURCE_TREE/examples/symbianpkgrules.pri) diff --git a/examples/graphicsview/dragdroprobot/images/head.png b/examples/graphicsview/dragdroprobot/images/head.png Binary files differnew file mode 100644 index 0000000000..1e520e09b8 --- /dev/null +++ b/examples/graphicsview/dragdroprobot/images/head.png diff --git a/examples/graphicsview/dragdroprobot/main.cpp b/examples/graphicsview/dragdroprobot/main.cpp new file mode 100644 index 0000000000..315d2b6917 --- /dev/null +++ b/examples/graphicsview/dragdroprobot/main.cpp @@ -0,0 +1,81 @@ +/**************************************************************************** +** +** 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> + +#include "coloritem.h" +#include "robot.h" + +#include <math.h> + +//! [0] +int main(int argc, char **argv) +{ + QApplication app(argc, argv); + + qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); +//! [0] +//! [1] + QGraphicsScene scene(-200, -200, 400, 400); + + for (int i = 0; i < 10; ++i) { + ColorItem *item = new ColorItem; + item->setPos(::sin((i * 6.28) / 10.0) * 150, + ::cos((i * 6.28) / 10.0) * 150); + + scene.addItem(item); + } + + Robot *robot = new Robot; + robot->scale(1.2, 1.2); + robot->setPos(0, -20); + scene.addItem(robot); +//! [1] +//! [2] + QGraphicsView view(&scene); + view.setRenderHint(QPainter::Antialiasing); + view.setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate); + view.setBackgroundBrush(QColor(230, 200, 167)); + view.setWindowTitle("Drag and Drop Robot"); + view.show(); + + return app.exec(); +} +//! [2] diff --git a/examples/graphicsview/dragdroprobot/robot.cpp b/examples/graphicsview/dragdroprobot/robot.cpp new file mode 100644 index 0000000000..9494e7af7f --- /dev/null +++ b/examples/graphicsview/dragdroprobot/robot.cpp @@ -0,0 +1,307 @@ +/**************************************************************************** +** +** 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> + +#include "robot.h" + +//! [0] +RobotPart::RobotPart(QGraphicsItem *parent) + : QGraphicsObject(parent), color(Qt::lightGray), dragOver(false) +{ + setAcceptDrops(true); +} +//! [0] + +//! [1] +void RobotPart::dragEnterEvent(QGraphicsSceneDragDropEvent *event) +{ + if (event->mimeData()->hasColor()) { + event->setAccepted(true); + dragOver = true; + update(); + } else { + event->setAccepted(false); + } +} +//! [1] + +//! [2] +void RobotPart::dragLeaveEvent(QGraphicsSceneDragDropEvent *event) +{ + Q_UNUSED(event); + dragOver = false; + update(); +} +//! [2] + +//! [3] +void RobotPart::dropEvent(QGraphicsSceneDragDropEvent *event) +{ + dragOver = false; + if (event->mimeData()->hasColor()) + color = qvariant_cast<QColor>(event->mimeData()->colorData()); + update(); +} +//! [3] + +//! [4] +RobotHead::RobotHead(QGraphicsItem *parent) + : RobotPart(parent) +{ +} +//! [4] + +//! [5] +QRectF RobotHead::boundingRect() const +{ + return QRectF(-15, -50, 30, 50); +} +//! [5] + +//! [6] +void RobotHead::paint(QPainter *painter, + const QStyleOptionGraphicsItem *option, QWidget *widget) +{ + Q_UNUSED(option); + Q_UNUSED(widget); + if (pixmap.isNull()) { + painter->setBrush(dragOver ? color.light(130) : color); + painter->drawRoundedRect(-10, -30, 20, 30, 25, 25, Qt::RelativeSize); + painter->setBrush(Qt::white); + painter->drawEllipse(-7, -3 - 20, 7, 7); + painter->drawEllipse(0, -3 - 20, 7, 7); + painter->setBrush(Qt::black); + painter->drawEllipse(-5, -1 - 20, 2, 2); + painter->drawEllipse(2, -1 - 20, 2, 2); + painter->setPen(QPen(Qt::black, 2)); + painter->setBrush(Qt::NoBrush); + painter->drawArc(-6, -2 - 20, 12, 15, 190 * 16, 160 * 16); + } else { + painter->scale(.2272, .2824); + painter->drawPixmap(QPointF(-15 * 4.4, -50 * 3.54), pixmap); + } +} +//! [6] + +//! [7] +void RobotHead::dragEnterEvent(QGraphicsSceneDragDropEvent *event) +{ + if (event->mimeData()->hasImage()) { + event->setAccepted(true); + dragOver = true; + update(); + } else { + RobotPart::dragEnterEvent(event); + } +} +//! [7] + +//! [8] +void RobotHead::dropEvent(QGraphicsSceneDragDropEvent *event) +{ + if (event->mimeData()->hasImage()) { + dragOver = false; + pixmap = qvariant_cast<QPixmap>(event->mimeData()->imageData()); + update(); + } else { + RobotPart::dropEvent(event); + } +} +//! [8] + +RobotTorso::RobotTorso(QGraphicsItem *parent) + : RobotPart(parent) +{ +} + +QRectF RobotTorso::boundingRect() const +{ + return QRectF(-30, -20, 60, 60); +} + +void RobotTorso::paint(QPainter *painter, + const QStyleOptionGraphicsItem *option, QWidget *widget) +{ + Q_UNUSED(option); + Q_UNUSED(widget); + + painter->setBrush(dragOver ? color.light(130) : color); + painter->drawRoundedRect(-20, -20, 40, 60, 25, 25, Qt::RelativeSize); + painter->drawEllipse(-25, -20, 20, 20); + painter->drawEllipse(5, -20, 20, 20); + painter->drawEllipse(-20, 22, 20, 20); + painter->drawEllipse(0, 22, 20, 20); +} + +RobotLimb::RobotLimb(QGraphicsItem *parent) + : RobotPart(parent) +{ +} + +QRectF RobotLimb::boundingRect() const +{ + return QRectF(-5, -5, 40, 10); +} + +void RobotLimb::paint(QPainter *painter, + const QStyleOptionGraphicsItem *option, QWidget *widget) +{ + Q_UNUSED(option); + Q_UNUSED(widget); + + painter->setBrush(dragOver ? color.light(130) : color); + painter->drawRoundedRect(boundingRect(), 50, 50, Qt::RelativeSize); + painter->drawEllipse(-5, -5, 10, 10); +} + +//! [10] +Robot::Robot(QGraphicsItem *parent) + : RobotPart(parent) +{ + setFlag(ItemHasNoContents); + + QGraphicsObject *torsoItem = new RobotTorso(this); + QGraphicsObject *headItem = new RobotHead(torsoItem); + QGraphicsObject *upperLeftArmItem = new RobotLimb(torsoItem); + QGraphicsObject *lowerLeftArmItem = new RobotLimb(upperLeftArmItem); + QGraphicsObject *upperRightArmItem = new RobotLimb(torsoItem); + QGraphicsObject *lowerRightArmItem = new RobotLimb(upperRightArmItem); + QGraphicsObject *upperRightLegItem = new RobotLimb(torsoItem); + QGraphicsObject *lowerRightLegItem = new RobotLimb(upperRightLegItem); + QGraphicsObject *upperLeftLegItem = new RobotLimb(torsoItem); + QGraphicsObject *lowerLeftLegItem = new RobotLimb(upperLeftLegItem); +//! [10] + +//! [11] + headItem->setPos(0, -18); + upperLeftArmItem->setPos(-15, -10); + lowerLeftArmItem->setPos(30, 0); + upperRightArmItem->setPos(15, -10); + lowerRightArmItem->setPos(30, 0); + upperRightLegItem->setPos(10, 32); + lowerRightLegItem->setPos(30, 0); + upperLeftLegItem->setPos(-10, 32); + lowerLeftLegItem->setPos(30, 0); +//! [11] + +//! [12] + QParallelAnimationGroup *animation = new QParallelAnimationGroup(this); + + QPropertyAnimation *headAnimation = new QPropertyAnimation(headItem, "rotation"); + headAnimation->setStartValue(20); + headAnimation->setEndValue(-20); + QPropertyAnimation *headScaleAnimation = new QPropertyAnimation(headItem, "scale"); + headScaleAnimation->setEndValue(1.1); + animation->addAnimation(headAnimation); + animation->addAnimation(headScaleAnimation); +//! [12] + + QPropertyAnimation *upperLeftArmAnimation = new QPropertyAnimation(upperLeftArmItem, "rotation"); + upperLeftArmAnimation->setStartValue(190); + upperLeftArmAnimation->setEndValue(180); + animation->addAnimation(upperLeftArmAnimation); + + QPropertyAnimation *lowerLeftArmAnimation = new QPropertyAnimation(lowerLeftArmItem, "rotation"); + lowerLeftArmAnimation->setStartValue(50); + lowerLeftArmAnimation->setEndValue(10); + animation->addAnimation(lowerLeftArmAnimation); + + QPropertyAnimation *upperRightArmAnimation = new QPropertyAnimation(upperRightArmItem, "rotation"); + upperRightArmAnimation->setStartValue(300); + upperRightArmAnimation->setEndValue(310); + animation->addAnimation(upperRightArmAnimation); + + QPropertyAnimation *lowerRightArmAnimation = new QPropertyAnimation(lowerRightArmItem, "rotation"); + lowerRightArmAnimation->setStartValue(0); + lowerRightArmAnimation->setEndValue(-70); + animation->addAnimation(lowerRightArmAnimation); + + QPropertyAnimation *upperLeftLegAnimation = new QPropertyAnimation(upperLeftLegItem, "rotation"); + upperLeftLegAnimation->setStartValue(150); + upperLeftLegAnimation->setEndValue(80); + animation->addAnimation(upperLeftLegAnimation); + + QPropertyAnimation *lowerLeftLegAnimation = new QPropertyAnimation(lowerLeftLegItem, "rotation"); + lowerLeftLegAnimation->setStartValue(70); + lowerLeftLegAnimation->setEndValue(10); + animation->addAnimation(lowerLeftLegAnimation); + + QPropertyAnimation *upperRightLegAnimation = new QPropertyAnimation(upperRightLegItem, "rotation"); + upperRightLegAnimation->setStartValue(40); + upperRightLegAnimation->setEndValue(120); + animation->addAnimation(upperRightLegAnimation); + + QPropertyAnimation *lowerRightLegAnimation = new QPropertyAnimation(lowerRightLegItem, "rotation"); + lowerRightLegAnimation->setStartValue(10); + lowerRightLegAnimation->setEndValue(50); + animation->addAnimation(lowerRightLegAnimation); + + QPropertyAnimation *torsoAnimation = new QPropertyAnimation(torsoItem, "rotation"); + torsoAnimation->setStartValue(5); + torsoAnimation->setEndValue(-20); + animation->addAnimation(torsoAnimation); + +//! [13] + for (int i = 0; i < animation->animationCount(); ++i) { + QPropertyAnimation *anim = qobject_cast<QPropertyAnimation *>(animation->animationAt(i)); + anim->setEasingCurve(QEasingCurve::SineCurve); + anim->setDuration(2000); + } + + animation->setLoopCount(-1); + animation->start(); +//! [13] +} + +//! [9] +QRectF Robot::boundingRect() const +{ + return QRectF(); +} + +void Robot::paint(QPainter *painter, + const QStyleOptionGraphicsItem *option, QWidget *widget) +{ + Q_UNUSED(painter); + Q_UNUSED(option); + Q_UNUSED(widget); +} +//! [9] diff --git a/examples/graphicsview/dragdroprobot/robot.h b/examples/graphicsview/dragdroprobot/robot.h new file mode 100644 index 0000000000..e65f39426b --- /dev/null +++ b/examples/graphicsview/dragdroprobot/robot.h @@ -0,0 +1,118 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +#ifndef ROBOT_H +#define ROBOT_H + +#include <QGraphicsItem> + +QT_BEGIN_NAMESPACE +class QGraphicsSceneMouseEvent; +class QParallelAnimationGroup; +QT_END_NAMESPACE + +//! [0] +class RobotPart : public QGraphicsObject +{ +public: + RobotPart(QGraphicsItem *parent = 0); + +protected: + void dragEnterEvent(QGraphicsSceneDragDropEvent *event); + void dragLeaveEvent(QGraphicsSceneDragDropEvent *event); + void dropEvent(QGraphicsSceneDragDropEvent *event); + + QColor color; + bool dragOver; +}; +//! [0] + +//! [1] +class RobotHead : public RobotPart +{ +public: + RobotHead(QGraphicsItem *parent = 0); + + QRectF boundingRect() const; + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); + +protected: + void dragEnterEvent(QGraphicsSceneDragDropEvent *event); + void dropEvent(QGraphicsSceneDragDropEvent *event); + +private: + QPixmap pixmap; +}; +//! [1] + +//! [2] +class RobotTorso : public RobotPart +{ +public: + RobotTorso(QGraphicsItem *parent = 0); + + QRectF boundingRect() const; + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); +}; +//! [2] + +//! [3] +class RobotLimb : public RobotPart +{ +public: + RobotLimb(QGraphicsItem *parent = 0); + + QRectF boundingRect() const; + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); +}; +//! [3] + +//! [4] +class Robot : public RobotPart +{ +public: + Robot(QGraphicsItem *parent = 0); + + QRectF boundingRect() const; + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); +}; +//! [4] + +#endif diff --git a/examples/graphicsview/dragdroprobot/robot.qrc b/examples/graphicsview/dragdroprobot/robot.qrc new file mode 100644 index 0000000000..b0969d2a64 --- /dev/null +++ b/examples/graphicsview/dragdroprobot/robot.qrc @@ -0,0 +1,5 @@ +<RCC> + <qresource prefix="/" > + <file>images/head.png</file> + </qresource> +</RCC> diff --git a/examples/graphicsview/elasticnodes/edge.cpp b/examples/graphicsview/elasticnodes/edge.cpp new file mode 100644 index 0000000000..2b5cae5e1b --- /dev/null +++ b/examples/graphicsview/elasticnodes/edge.cpp @@ -0,0 +1,149 @@ +/**************************************************************************** +** +** 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 <QPainter> + +#include "edge.h" +#include "node.h" + +#include <math.h> + +static const double Pi = 3.14159265358979323846264338327950288419717; +static double TwoPi = 2.0 * Pi; + +//! [0] +Edge::Edge(Node *sourceNode, Node *destNode) + : arrowSize(10) +{ + setAcceptedMouseButtons(0); + source = sourceNode; + dest = destNode; + source->addEdge(this); + dest->addEdge(this); + adjust(); +} +//! [0] + +//! [1] +Node *Edge::sourceNode() const +{ + return source; +} + +Node *Edge::destNode() const +{ + return dest; +} +//! [1] + +//! [2] +void Edge::adjust() +{ + if (!source || !dest) + return; + + QLineF line(mapFromItem(source, 0, 0), mapFromItem(dest, 0, 0)); + qreal length = line.length(); + + prepareGeometryChange(); + + if (length > qreal(20.)) { + QPointF edgeOffset((line.dx() * 10) / length, (line.dy() * 10) / length); + sourcePoint = line.p1() + edgeOffset; + destPoint = line.p2() - edgeOffset; + } else { + sourcePoint = destPoint = line.p1(); + } +} +//! [2] + +//! [3] +QRectF Edge::boundingRect() const +{ + if (!source || !dest) + return QRectF(); + + qreal penWidth = 1; + qreal extra = (penWidth + arrowSize) / 2.0; + + return QRectF(sourcePoint, QSizeF(destPoint.x() - sourcePoint.x(), + destPoint.y() - sourcePoint.y())) + .normalized() + .adjusted(-extra, -extra, extra, extra); +} +//! [3] + +//! [4] +void Edge::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) +{ + if (!source || !dest) + return; + + QLineF line(sourcePoint, destPoint); + if (qFuzzyCompare(line.length(), qreal(0.))) + return; +//! [4] + +//! [5] + // Draw the line itself + painter->setPen(QPen(Qt::black, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)); + painter->drawLine(line); +//! [5] + +//! [6] + // Draw the arrows + double angle = ::acos(line.dx() / line.length()); + if (line.dy() >= 0) + angle = TwoPi - angle; + + QPointF sourceArrowP1 = sourcePoint + QPointF(sin(angle + Pi / 3) * arrowSize, + cos(angle + Pi / 3) * arrowSize); + QPointF sourceArrowP2 = sourcePoint + QPointF(sin(angle + Pi - Pi / 3) * arrowSize, + cos(angle + Pi - Pi / 3) * arrowSize); + QPointF destArrowP1 = destPoint + QPointF(sin(angle - Pi / 3) * arrowSize, + cos(angle - Pi / 3) * arrowSize); + QPointF destArrowP2 = destPoint + QPointF(sin(angle - Pi + Pi / 3) * arrowSize, + cos(angle - Pi + Pi / 3) * arrowSize); + + painter->setBrush(Qt::black); + painter->drawPolygon(QPolygonF() << line.p1() << sourceArrowP1 << sourceArrowP2); + painter->drawPolygon(QPolygonF() << line.p2() << destArrowP1 << destArrowP2); +} +//! [6] diff --git a/examples/graphicsview/elasticnodes/edge.h b/examples/graphicsview/elasticnodes/edge.h new file mode 100644 index 0000000000..8f73edd787 --- /dev/null +++ b/examples/graphicsview/elasticnodes/edge.h @@ -0,0 +1,75 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +#ifndef EDGE_H +#define EDGE_H + +#include <QGraphicsItem> + +class Node; + +//! [0] +class Edge : public QGraphicsItem +{ +public: + Edge(Node *sourceNode, Node *destNode); + + Node *sourceNode() const; + Node *destNode() const; + + void adjust(); + + enum { Type = UserType + 2 }; + int type() const { return Type; } + +protected: + QRectF boundingRect() const; + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); + +private: + Node *source, *dest; + + QPointF sourcePoint; + QPointF destPoint; + qreal arrowSize; +}; +//! [0] + +#endif diff --git a/examples/graphicsview/elasticnodes/elasticnodes.pro b/examples/graphicsview/elasticnodes/elasticnodes.pro new file mode 100644 index 0000000000..cb7cc1b928 --- /dev/null +++ b/examples/graphicsview/elasticnodes/elasticnodes.pro @@ -0,0 +1,23 @@ +HEADERS += \ + edge.h \ + node.h \ + graphwidget.h + +SOURCES += \ + edge.cpp \ + main.cpp \ + node.cpp \ + graphwidget.cpp + +TARGET.EPOCHEAPSIZE = 0x200000 0xA00000 + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/graphicsview/elasticnodes +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS elasticnodes.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/graphicsview/elasticnodes +INSTALLS += target sources + +symbian { + TARGET.UID3 = 0xA000A642 + include($$QT_SOURCE_TREE/examples/symbianpkgrules.pri) +} diff --git a/examples/graphicsview/elasticnodes/graphwidget.cpp b/examples/graphicsview/elasticnodes/graphwidget.cpp new file mode 100644 index 0000000000..c875b6551d --- /dev/null +++ b/examples/graphicsview/elasticnodes/graphwidget.cpp @@ -0,0 +1,235 @@ +/**************************************************************************** +** +** 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 "graphwidget.h" +#include "edge.h" +#include "node.h" + +#include <QtGui> + +#include <math.h> + +//! [0] +GraphWidget::GraphWidget(QWidget *parent) + : QGraphicsView(parent), timerId(0) +{ + QGraphicsScene *scene = new QGraphicsScene(this); + scene->setItemIndexMethod(QGraphicsScene::NoIndex); + scene->setSceneRect(-200, -200, 400, 400); + setScene(scene); + setCacheMode(CacheBackground); + setViewportUpdateMode(BoundingRectViewportUpdate); + setRenderHint(QPainter::Antialiasing); + setTransformationAnchor(AnchorUnderMouse); + scale(qreal(0.8), qreal(0.8)); + setMinimumSize(400, 400); + setWindowTitle(tr("Elastic Nodes")); +//! [0] + +//! [1] + Node *node1 = new Node(this); + Node *node2 = new Node(this); + Node *node3 = new Node(this); + Node *node4 = new Node(this); + centerNode = new Node(this); + Node *node6 = new Node(this); + Node *node7 = new Node(this); + Node *node8 = new Node(this); + Node *node9 = new Node(this); + scene->addItem(node1); + scene->addItem(node2); + scene->addItem(node3); + scene->addItem(node4); + scene->addItem(centerNode); + scene->addItem(node6); + scene->addItem(node7); + scene->addItem(node8); + scene->addItem(node9); + scene->addItem(new Edge(node1, node2)); + scene->addItem(new Edge(node2, node3)); + scene->addItem(new Edge(node2, centerNode)); + scene->addItem(new Edge(node3, node6)); + scene->addItem(new Edge(node4, node1)); + scene->addItem(new Edge(node4, centerNode)); + scene->addItem(new Edge(centerNode, node6)); + scene->addItem(new Edge(centerNode, node8)); + scene->addItem(new Edge(node6, node9)); + scene->addItem(new Edge(node7, node4)); + scene->addItem(new Edge(node8, node7)); + scene->addItem(new Edge(node9, node8)); + + node1->setPos(-50, -50); + node2->setPos(0, -50); + node3->setPos(50, -50); + node4->setPos(-50, 0); + centerNode->setPos(0, 0); + node6->setPos(50, 0); + node7->setPos(-50, 50); + node8->setPos(0, 50); + node9->setPos(50, 50); +} +//! [1] + +//! [2] +void GraphWidget::itemMoved() +{ + if (!timerId) + timerId = startTimer(1000 / 25); +} +//! [2] + +//! [3] +void GraphWidget::keyPressEvent(QKeyEvent *event) +{ + switch (event->key()) { + case Qt::Key_Up: + centerNode->moveBy(0, -20); + break; + case Qt::Key_Down: + centerNode->moveBy(0, 20); + break; + case Qt::Key_Left: + centerNode->moveBy(-20, 0); + break; + case Qt::Key_Right: + centerNode->moveBy(20, 0); + break; + case Qt::Key_Plus: + scaleView(qreal(1.2)); + break; + case Qt::Key_Minus: + scaleView(1 / qreal(1.2)); + break; + case Qt::Key_Space: + case Qt::Key_Enter: + foreach (QGraphicsItem *item, scene()->items()) { + if (qgraphicsitem_cast<Node *>(item)) + item->setPos(-150 + qrand() % 300, -150 + qrand() % 300); + } + break; + default: + QGraphicsView::keyPressEvent(event); + } +} +//! [3] + +//! [4] +void GraphWidget::timerEvent(QTimerEvent *event) +{ + Q_UNUSED(event); + + QList<Node *> nodes; + foreach (QGraphicsItem *item, scene()->items()) { + if (Node *node = qgraphicsitem_cast<Node *>(item)) + nodes << node; + } + + foreach (Node *node, nodes) + node->calculateForces(); + + bool itemsMoved = false; + foreach (Node *node, nodes) { + if (node->advance()) + itemsMoved = true; + } + + if (!itemsMoved) { + killTimer(timerId); + timerId = 0; + } +} +//! [4] + +//! [5] +void GraphWidget::wheelEvent(QWheelEvent *event) +{ + scaleView(pow((double)2, -event->delta() / 240.0)); +} +//! [5] + +//! [6] +void GraphWidget::drawBackground(QPainter *painter, const QRectF &rect) +{ + Q_UNUSED(rect); + + // Shadow + QRectF sceneRect = this->sceneRect(); + QRectF rightShadow(sceneRect.right(), sceneRect.top() + 5, 5, sceneRect.height()); + QRectF bottomShadow(sceneRect.left() + 5, sceneRect.bottom(), sceneRect.width(), 5); + if (rightShadow.intersects(rect) || rightShadow.contains(rect)) + painter->fillRect(rightShadow, Qt::darkGray); + if (bottomShadow.intersects(rect) || bottomShadow.contains(rect)) + painter->fillRect(bottomShadow, Qt::darkGray); + + // Fill + QLinearGradient gradient(sceneRect.topLeft(), sceneRect.bottomRight()); + gradient.setColorAt(0, Qt::white); + gradient.setColorAt(1, Qt::lightGray); + painter->fillRect(rect.intersect(sceneRect), gradient); + painter->setBrush(Qt::NoBrush); + painter->drawRect(sceneRect); + + // Text + QRectF textRect(sceneRect.left() + 4, sceneRect.top() + 4, + sceneRect.width() - 4, sceneRect.height() - 4); + QString message(tr("Click and drag the nodes around, and zoom with the mouse " + "wheel or the '+' and '-' keys")); + + QFont font = painter->font(); + font.setBold(true); + font.setPointSize(14); + painter->setFont(font); + painter->setPen(Qt::lightGray); + painter->drawText(textRect.translated(2, 2), message); + painter->setPen(Qt::black); + painter->drawText(textRect, message); +} +//! [6] + +//! [7] +void GraphWidget::scaleView(qreal scaleFactor) +{ + qreal factor = transform().scale(scaleFactor, scaleFactor).mapRect(QRectF(0, 0, 1, 1)).width(); + if (factor < 0.07 || factor > 100) + return; + + scale(scaleFactor, scaleFactor); +} +//! [7] diff --git a/examples/graphicsview/elasticnodes/graphwidget.h b/examples/graphicsview/elasticnodes/graphwidget.h new file mode 100644 index 0000000000..764bb3f0b9 --- /dev/null +++ b/examples/graphicsview/elasticnodes/graphwidget.h @@ -0,0 +1,72 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +#ifndef GRAPHWIDGET_H +#define GRAPHWIDGET_H + +#include <QtGui/QGraphicsView> + +class Node; + +//! [0] +class GraphWidget : public QGraphicsView +{ + Q_OBJECT + +public: + GraphWidget(QWidget *parent = 0); + + void itemMoved(); + +protected: + void keyPressEvent(QKeyEvent *event); + void timerEvent(QTimerEvent *event); + void wheelEvent(QWheelEvent *event); + void drawBackground(QPainter *painter, const QRectF &rect); + + void scaleView(qreal scaleFactor); + +private: + int timerId; + Node *centerNode; +}; +//! [0] + +#endif diff --git a/examples/graphicsview/elasticnodes/main.cpp b/examples/graphicsview/elasticnodes/main.cpp new file mode 100644 index 0000000000..ab7e7cf7c7 --- /dev/null +++ b/examples/graphicsview/elasticnodes/main.cpp @@ -0,0 +1,53 @@ +/**************************************************************************** +** +** 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> + +#include "graphwidget.h" + +int main(int argc, char **argv) +{ + QApplication app(argc, argv); + qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); + + GraphWidget widget; + widget.show(); + return app.exec(); +} diff --git a/examples/graphicsview/elasticnodes/node.cpp b/examples/graphicsview/elasticnodes/node.cpp new file mode 100644 index 0000000000..8d1daddc12 --- /dev/null +++ b/examples/graphicsview/elasticnodes/node.cpp @@ -0,0 +1,211 @@ +/**************************************************************************** +** +** 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 <QGraphicsScene> +#include <QGraphicsSceneMouseEvent> +#include <QPainter> +#include <QStyleOption> + +#include "edge.h" +#include "node.h" +#include "graphwidget.h" + +//! [0] +Node::Node(GraphWidget *graphWidget) + : graph(graphWidget) +{ + setFlag(ItemIsMovable); + setFlag(ItemSendsGeometryChanges); + setCacheMode(DeviceCoordinateCache); + setZValue(-1); +} +//! [0] + +//! [1] +void Node::addEdge(Edge *edge) +{ + edgeList << edge; + edge->adjust(); +} + +QList<Edge *> Node::edges() const +{ + return edgeList; +} +//! [1] + +//! [2] +void Node::calculateForces() +{ + if (!scene() || scene()->mouseGrabberItem() == this) { + newPos = pos(); + return; + } +//! [2] + +//! [3] + // Sum up all forces pushing this item away + qreal xvel = 0; + qreal yvel = 0; + foreach (QGraphicsItem *item, scene()->items()) { + Node *node = qgraphicsitem_cast<Node *>(item); + if (!node) + continue; + + QPointF vec = mapToItem(node, 0, 0); + qreal dx = vec.x(); + qreal dy = vec.y(); + double l = 2.0 * (dx * dx + dy * dy); + if (l > 0) { + xvel += (dx * 150.0) / l; + yvel += (dy * 150.0) / l; + } + } +//! [3] + +//! [4] + // Now subtract all forces pulling items together + double weight = (edgeList.size() + 1) * 10; + foreach (Edge *edge, edgeList) { + QPointF vec; + if (edge->sourceNode() == this) + vec = mapToItem(edge->destNode(), 0, 0); + else + vec = mapToItem(edge->sourceNode(), 0, 0); + xvel -= vec.x() / weight; + yvel -= vec.y() / weight; + } +//! [4] + +//! [5] + if (qAbs(xvel) < 0.1 && qAbs(yvel) < 0.1) + xvel = yvel = 0; +//! [5] + +//! [6] + QRectF sceneRect = scene()->sceneRect(); + newPos = pos() + QPointF(xvel, yvel); + newPos.setX(qMin(qMax(newPos.x(), sceneRect.left() + 10), sceneRect.right() - 10)); + newPos.setY(qMin(qMax(newPos.y(), sceneRect.top() + 10), sceneRect.bottom() - 10)); +} +//! [6] + +//! [7] +bool Node::advance() +{ + if (newPos == pos()) + return false; + + setPos(newPos); + return true; +} +//! [7] + +//! [8] +QRectF Node::boundingRect() const +{ + qreal adjust = 2; + return QRectF(-10 - adjust, -10 - adjust, + 23 + adjust, 23 + adjust); +} +//! [8] + +//! [9] +QPainterPath Node::shape() const +{ + QPainterPath path; + path.addEllipse(-10, -10, 20, 20); + return path; +} +//! [9] + +//! [10] +void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *) +{ + painter->setPen(Qt::NoPen); + painter->setBrush(Qt::darkGray); + painter->drawEllipse(-7, -7, 20, 20); + + QRadialGradient gradient(-3, -3, 10); + if (option->state & QStyle::State_Sunken) { + gradient.setCenter(3, 3); + gradient.setFocalPoint(3, 3); + gradient.setColorAt(1, QColor(Qt::yellow).light(120)); + gradient.setColorAt(0, QColor(Qt::darkYellow).light(120)); + } else { + gradient.setColorAt(0, Qt::yellow); + gradient.setColorAt(1, Qt::darkYellow); + } + painter->setBrush(gradient); + painter->setPen(QPen(Qt::black, 0)); + painter->drawEllipse(-10, -10, 20, 20); +} +//! [10] + +//! [11] +QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value) +{ + switch (change) { + case ItemPositionHasChanged: + foreach (Edge *edge, edgeList) + edge->adjust(); + graph->itemMoved(); + break; + default: + break; + }; + + return QGraphicsItem::itemChange(change, value); +} +//! [11] + +//! [12] +void Node::mousePressEvent(QGraphicsSceneMouseEvent *event) +{ + update(); + QGraphicsItem::mousePressEvent(event); +} + +void Node::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) +{ + update(); + QGraphicsItem::mouseReleaseEvent(event); +} +//! [12] diff --git a/examples/graphicsview/elasticnodes/node.h b/examples/graphicsview/elasticnodes/node.h new file mode 100644 index 0000000000..53af7182d0 --- /dev/null +++ b/examples/graphicsview/elasticnodes/node.h @@ -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$ +** +****************************************************************************/ + +#ifndef NODE_H +#define NODE_H + +#include <QGraphicsItem> +#include <QList> + +class Edge; +class GraphWidget; +QT_BEGIN_NAMESPACE +class QGraphicsSceneMouseEvent; +QT_END_NAMESPACE + +//! [0] +class Node : public QGraphicsItem +{ +public: + Node(GraphWidget *graphWidget); + + void addEdge(Edge *edge); + QList<Edge *> edges() const; + + enum { Type = UserType + 1 }; + int type() const { return Type; } + + void calculateForces(); + bool advance(); + + QRectF boundingRect() const; + QPainterPath shape() const; + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); + +protected: + QVariant itemChange(GraphicsItemChange change, const QVariant &value); + + void mousePressEvent(QGraphicsSceneMouseEvent *event); + void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); + +private: + QList<Edge *> edgeList; + QPointF newPos; + GraphWidget *graph; +}; +//! [0] + +#endif diff --git a/examples/graphicsview/flowlayout/flowlayout.cpp b/examples/graphicsview/flowlayout/flowlayout.cpp new file mode 100644 index 0000000000..0531e4cce5 --- /dev/null +++ b/examples/graphicsview/flowlayout/flowlayout.cpp @@ -0,0 +1,213 @@ +/**************************************************************************** +** +** 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 "flowlayout.h" +#include <QtGui/qwidget.h> +#include <QtCore/qmath.h> + +FlowLayout::FlowLayout() +{ + m_spacing[0] = 6; + m_spacing[1] = 6; + QSizePolicy sp = sizePolicy(); + sp.setHeightForWidth(true); + setSizePolicy(sp); +} + +void FlowLayout::insertItem(int index, QGraphicsLayoutItem *item) +{ + item->setParentLayoutItem(this); + if (uint(index) > uint(m_items.count())) + index = m_items.count(); + m_items.insert(index, item); + invalidate(); +} + +int FlowLayout::count() const +{ + return m_items.count(); +} + +QGraphicsLayoutItem *FlowLayout::itemAt(int index) const +{ + return m_items.value(index); +} + +void FlowLayout::removeAt(int index) +{ + m_items.removeAt(index); + invalidate(); +} + +qreal FlowLayout::spacing(Qt::Orientation o) const +{ + return m_spacing[int(o) - 1]; +} + +void FlowLayout::setSpacing(Qt::Orientations o, qreal spacing) +{ + if (o & Qt::Horizontal) + m_spacing[0] = spacing; + if (o & Qt::Vertical) + m_spacing[1] = spacing; +} + +void FlowLayout::setGeometry(const QRectF &geom) +{ + QGraphicsLayout::setGeometry(geom); + doLayout(geom, true); +} + +qreal FlowLayout::doLayout(const QRectF &geom, bool applyNewGeometry) const +{ + qreal left, top, right, bottom; + getContentsMargins(&left, &top, &right, &bottom); + const qreal maxw = geom.width() - left - right; + + qreal x = 0; + qreal y = 0; + qreal maxRowHeight = 0; + QSizeF pref; + for (int i = 0; i < m_items.count(); ++i) { + QGraphicsLayoutItem *item = m_items.at(i); + pref = item->effectiveSizeHint(Qt::PreferredSize); + maxRowHeight = qMax(maxRowHeight, pref.height()); + + qreal next_x; + next_x = x + pref.width(); + if (next_x > maxw) { + if (x == 0) { + pref.setWidth(maxw); + } else { + x = 0; + next_x = pref.width(); + } + y += maxRowHeight + spacing(Qt::Vertical); + maxRowHeight = 0; + } + + if (applyNewGeometry) + item->setGeometry(QRectF(QPointF(left + x, top + y), pref)); + x = next_x + spacing(Qt::Horizontal); + } + maxRowHeight = qMax(maxRowHeight, pref.height()); + return top + y + maxRowHeight + bottom; +} + +QSizeF FlowLayout::minSize(const QSizeF &constraint) const +{ + QSizeF size(0, 0); + qreal left, top, right, bottom; + getContentsMargins(&left, &top, &right, &bottom); + if (constraint.width() >= 0) { // height for width + const qreal height = doLayout(QRectF(QPointF(0,0), constraint), false); + size = QSizeF(constraint.width(), height); + } else if (constraint.height() >= 0) { // width for height? + // not supported + } else { + QGraphicsLayoutItem *item; + foreach (item, m_items) + size = size.expandedTo(item->effectiveSizeHint(Qt::MinimumSize)); + size += QSize(left + right, top + bottom); + } + return size; +} + +QSizeF FlowLayout::prefSize() const +{ + qreal left, right; + getContentsMargins(&left, 0, &right, 0); + + QGraphicsLayoutItem *item; + qreal maxh = 0; + qreal totalWidth = 0; + foreach (item, m_items) { + if (totalWidth > 0) + totalWidth += spacing(Qt::Horizontal); + QSizeF pref = item->effectiveSizeHint(Qt::PreferredSize); + totalWidth += pref.width(); + maxh = qMax(maxh, pref.height()); + } + maxh += spacing(Qt::Vertical); + + const qreal goldenAspectRatio = 1.61803399; + qreal w = qSqrt(totalWidth * maxh * goldenAspectRatio) + left + right; + + return minSize(QSizeF(w, -1)); +} + +QSizeF FlowLayout::maxSize() const +{ + QGraphicsLayoutItem *item; + qreal totalWidth = 0; + qreal totalHeight = 0; + foreach (item, m_items) { + if (totalWidth > 0) + totalWidth += spacing(Qt::Horizontal); + if (totalHeight > 0) + totalHeight += spacing(Qt::Vertical); + QSizeF pref = item->effectiveSizeHint(Qt::PreferredSize); + totalWidth += pref.width(); + totalHeight += pref.height(); + } + + qreal left, top, right, bottom; + getContentsMargins(&left, &top, &right, &bottom); + return QSizeF(left + totalWidth + right, top + totalHeight + bottom); +} + +QSizeF FlowLayout::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const +{ + QSizeF sh = constraint; + switch (which) { + case Qt::PreferredSize: + sh = prefSize(); + break; + case Qt::MinimumSize: + sh = minSize(constraint); + break; + case Qt::MaximumSize: + sh = maxSize(); + break; + default: + break; + } + return sh; +} diff --git a/examples/graphicsview/flowlayout/flowlayout.h b/examples/graphicsview/flowlayout/flowlayout.h new file mode 100644 index 0000000000..aff3382c01 --- /dev/null +++ b/examples/graphicsview/flowlayout/flowlayout.h @@ -0,0 +1,76 @@ +/**************************************************************************** +** +** 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/qgraphicslayout.h> + +class FlowLayout : public QGraphicsLayout +{ +public: + FlowLayout(); + inline void addItem(QGraphicsLayoutItem *item); + void insertItem(int index, QGraphicsLayoutItem *item); + void setSpacing(Qt::Orientations o, qreal spacing); + qreal spacing(Qt::Orientation o) const; + + // inherited functions + void setGeometry(const QRectF &geom); + + int count() const; + QGraphicsLayoutItem *itemAt(int index) const; + void removeAt(int index); + +protected: + QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint = QSizeF()) const; + +private: + qreal doLayout(const QRectF &geom, bool applyNewGeometry) const; + QSizeF minSize(const QSizeF &constraint) const; + QSizeF prefSize() const; + QSizeF maxSize() const; + + QList<QGraphicsLayoutItem*> m_items; + qreal m_spacing[2]; +}; + + +inline void FlowLayout::addItem(QGraphicsLayoutItem *item) +{ + insertItem(-1, item); +} diff --git a/examples/graphicsview/flowlayout/flowlayout.pro b/examples/graphicsview/flowlayout/flowlayout.pro new file mode 100644 index 0000000000..ce3536774b --- /dev/null +++ b/examples/graphicsview/flowlayout/flowlayout.pro @@ -0,0 +1,10 @@ +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +QMAKE_PROJECT_NAME = flowlayout_graphicsview + +# Input +HEADERS += flowlayout.h window.h +SOURCES += flowlayout.cpp main.cpp window.cpp diff --git a/examples/graphicsview/flowlayout/main.cpp b/examples/graphicsview/flowlayout/main.cpp new file mode 100644 index 0000000000..cee08d76ac --- /dev/null +++ b/examples/graphicsview/flowlayout/main.cpp @@ -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 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> +#include "window.h" + +int main(int argc, char **argv) +{ + QApplication app(argc, argv); + + QGraphicsScene scene; + QGraphicsView *view = new QGraphicsView(&scene); + Window *w = new Window; + scene.addItem(w); + view->resize(400, 300); + view->show(); + return app.exec(); +} diff --git a/examples/graphicsview/flowlayout/window.cpp b/examples/graphicsview/flowlayout/window.cpp new file mode 100644 index 0000000000..4abe2a55c2 --- /dev/null +++ b/examples/graphicsview/flowlayout/window.cpp @@ -0,0 +1,62 @@ +/**************************************************************************** +** +** 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/qgraphicsproxywidget.h> +#include <QtGui/qlabel.h> +#include "flowlayout.h" +#include "window.h" + +Window::Window() +: QGraphicsWidget(0, Qt::Window) +{ + FlowLayout *lay = new FlowLayout; + QLatin1String wiseWords("I am not bothered by the fact that I am unknown." + " I am bothered when I do not know others. (Confucius)"); + QString sentence(wiseWords); + QStringList words = sentence.split(QLatin1Char(' '), QString::SkipEmptyParts); + for (int i = 0; i < words.count(); ++i) { + QGraphicsProxyWidget *proxy = new QGraphicsProxyWidget(this); + QLabel *label = new QLabel(words.at(i)); + label->setFrameStyle(QFrame::Box | QFrame::Plain); + proxy->setWidget(label); + lay->addItem(proxy); + } + setLayout(lay); +} diff --git a/examples/graphicsview/flowlayout/window.h b/examples/graphicsview/flowlayout/window.h new file mode 100644 index 0000000000..c338a14236 --- /dev/null +++ b/examples/graphicsview/flowlayout/window.h @@ -0,0 +1,47 @@ +/**************************************************************************** +** +** 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/qgraphicswidget.h> + +class Window : public QGraphicsWidget { + Q_OBJECT +public: + Window(); +}; diff --git a/examples/graphicsview/graphicsview.pro b/examples/graphicsview/graphicsview.pro new file mode 100644 index 0000000000..5e49855104 --- /dev/null +++ b/examples/graphicsview/graphicsview.pro @@ -0,0 +1,25 @@ +TEMPLATE = subdirs +SUBDIRS = \ + elasticnodes \ + collidingmice \ + padnavigator \ + basicgraphicslayouts + +!symbian: SUBDIRS += \ + diagramscene \ + dragdroprobot \ + flowlayout \ + anchorlayout \ + simpleanchorlayout \ + weatheranchorlayout + +contains(QT_CONFIG, qt3support):SUBDIRS += portedcanvas portedasteroids +contains(DEFINES, QT_NO_CURSOR)|contains(DEFINES, QT_NO_DRAGANDDROP): SUBDIRS -= dragdroprobot + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/graphicsview +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS graphicsview.pro README +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/graphicsview +INSTALLS += target sources + +symbian: include($$QT_SOURCE_TREE/examples/symbianpkgrules.pri) diff --git a/examples/graphicsview/padnavigator/flippablepad.cpp b/examples/graphicsview/padnavigator/flippablepad.cpp new file mode 100644 index 0000000000..ada44dad8f --- /dev/null +++ b/examples/graphicsview/padnavigator/flippablepad.cpp @@ -0,0 +1,98 @@ +/**************************************************************************** +** +** 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 "flippablepad.h" + +#include <QtGui/QtGui> + +//! [0] +static QRectF boundsFromSize(const QSize &size) +{ + return QRectF((-size.width() / 2.0) * 150, (-size.height() / 2.0) * 150, + size.width() * 150, size.height() * 150); +} +//! [0] + +//! [1] +static QPointF posForLocation(int column, int row, const QSize &size) +{ + return QPointF(column * 150, row * 150) + - QPointF((size.width() - 1) * 75, (size.height() - 1) * 75); +} +//! [1] + +//! [2] +FlippablePad::FlippablePad(const QSize &size, QGraphicsItem *parent) + : RoundRectItem(boundsFromSize(size), QColor(226, 255, 92, 64), parent) +{ +//! [2] +//! [3] + int numIcons = size.width() * size.height(); + QList<QPixmap> pixmaps; + QDirIterator it(":/images", QStringList() << "*.png"); + while (it.hasNext() && pixmaps.size() < numIcons) + pixmaps << it.next(); +//! [3] + +//! [4] + const QRectF iconRect(-54, -54, 108, 108); + const QColor iconColor(214, 240, 110, 128); + iconGrid.resize(size.height()); + int n = 0; + + for (int y = 0; y < size.height(); ++y) { + iconGrid[y].resize(size.width()); + for (int x = 0; x < size.width(); ++x) { + RoundRectItem *rect = new RoundRectItem(iconRect, iconColor, this); + rect->setZValue(1); + rect->setPos(posForLocation(x, y, size)); + rect->setPixmap(pixmaps.at(n++ % pixmaps.size())); + iconGrid[y][x] = rect; + } + } +} +//! [4] + +//! [5] +RoundRectItem *FlippablePad::iconAt(int column, int row) const +{ + return iconGrid[row][column]; +} +//! [5] diff --git a/examples/graphicsview/padnavigator/flippablepad.h b/examples/graphicsview/padnavigator/flippablepad.h new file mode 100644 index 0000000000..0114b43a75 --- /dev/null +++ b/examples/graphicsview/padnavigator/flippablepad.h @@ -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 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$ +** +****************************************************************************/ + +#ifndef FLIPPABLEPAD_H +#define FLIPPABLEPAD_H + +#include "roundrectitem.h" + +#include <QGraphicsObject> +#include <QLinearGradient> +#include <QVector> + +//! [0] +class FlippablePad : public RoundRectItem +{ +public: + FlippablePad(const QSize &size, QGraphicsItem *parent = 0); + + RoundRectItem *iconAt(int column, int row) const; + +private: + QVector<QVector<RoundRectItem *> > iconGrid; +}; +//! [0] + +#endif // FLIPPABLEPAD_H diff --git a/examples/graphicsview/padnavigator/form.ui b/examples/graphicsview/padnavigator/form.ui new file mode 100644 index 0000000000..fc7d12321e --- /dev/null +++ b/examples/graphicsview/padnavigator/form.ui @@ -0,0 +1,208 @@ +<ui version="4.0" > + <class>Form</class> + <widget class="QWidget" name="Form" > + <property name="geometry" > + <rect> + <x>0</x> + <y>0</y> + <width>378</width> + <height>385</height> + </rect> + </property> + <property name="windowTitle" > + <string>BackSide</string> + </property> + <layout class="QVBoxLayout" name="verticalLayout_2" > + <item> + <widget class="QGroupBox" name="groupBox" > + <property name="title" > + <string>Settings</string> + </property> + <property name="flat" > + <bool>true</bool> + </property> + <property name="checkable" > + <bool>true</bool> + </property> + <layout class="QGridLayout" name="gridLayout" > + <item row="0" column="0" > + <widget class="QLabel" name="label" > + <property name="text" > + <string>Title:</string> + </property> + </widget> + </item> + <item row="0" column="1" > + <widget class="QLineEdit" name="hostName" > + <property name="text" > + <string>Pad Navigator Example</string> + </property> + </widget> + </item> + <item row="1" column="0" > + <widget class="QLabel" name="label_2" > + <property name="text" > + <string>Modified:</string> + </property> + </widget> + </item> + <item row="2" column="0" > + <widget class="QLabel" name="label_3" > + <property name="text" > + <string>Extent</string> + </property> + </widget> + </item> + <item row="2" column="1" > + <layout class="QHBoxLayout" name="horizontalLayout" > + <item> + <widget class="QSlider" name="horizontalSlider" > + <property name="value" > + <number>42</number> + </property> + <property name="orientation" > + <enum>Qt::Horizontal</enum> + </property> + </widget> + </item> + <item> + <widget class="QSpinBox" name="spinBox" > + <property name="value" > + <number>42</number> + </property> + </widget> + </item> + </layout> + </item> + <item row="1" column="1" > + <widget class="QDateTimeEdit" name="dateTimeEdit" /> + </item> + </layout> + </widget> + </item> + <item> + <widget class="QGroupBox" name="groupBox_2" > + <property name="title" > + <string>Other input</string> + </property> + <property name="flat" > + <bool>true</bool> + </property> + <property name="checkable" > + <bool>true</bool> + </property> + <layout class="QHBoxLayout" name="horizontalLayout_2" > + <item> + <widget class="QTreeWidget" name="treeWidget" > + <column> + <property name="text" > + <string>Widgets On Graphics View</string> + </property> + </column> + <item> + <property name="text" > + <string>QGraphicsProxyWidget</string> + </property> + <item> + <property name="text" > + <string>QGraphicsWidget</string> + </property> + <item> + <property name="text" > + <string>QObject</string> + </property> + </item> + <item> + <property name="text" > + <string>QGraphicsItem</string> + </property> + </item> + <item> + <property name="text" > + <string>QGraphicsLayoutItem</string> + </property> + </item> + </item> + </item> + <item> + <property name="text" > + <string>QGraphicsGridLayout</string> + </property> + <item> + <property name="text" > + <string>QGraphicsLayout</string> + </property> + <item> + <property name="text" > + <string>QGraphicsLayoutItem</string> + </property> + </item> + </item> + </item> + <item> + <property name="text" > + <string>QGraphicsLinearLayout</string> + </property> + <item> + <property name="text" > + <string>QGraphicsLayout</string> + </property> + <item> + <property name="text" > + <string>QGraphicsLayoutItem</string> + </property> + </item> + </item> + </item> + </widget> + </item> + </layout> + </widget> + </item> + </layout> + </widget> + <tabstops> + <tabstop>groupBox</tabstop> + <tabstop>hostName</tabstop> + <tabstop>dateTimeEdit</tabstop> + <tabstop>horizontalSlider</tabstop> + <tabstop>spinBox</tabstop> + <tabstop>groupBox_2</tabstop> + <tabstop>treeWidget</tabstop> + </tabstops> + <resources/> + <connections> + <connection> + <sender>horizontalSlider</sender> + <signal>valueChanged(int)</signal> + <receiver>spinBox</receiver> + <slot>setValue(int)</slot> + <hints> + <hint type="sourcelabel" > + <x>184</x> + <y>125</y> + </hint> + <hint type="destinationlabel" > + <x>275</x> + <y>127</y> + </hint> + </hints> + </connection> + <connection> + <sender>spinBox</sender> + <signal>valueChanged(int)</signal> + <receiver>horizontalSlider</receiver> + <slot>setValue(int)</slot> + <hints> + <hint type="sourcelabel" > + <x>272</x> + <y>114</y> + </hint> + <hint type="destinationlabel" > + <x>190</x> + <y>126</y> + </hint> + </hints> + </connection> + </connections> +</ui> diff --git a/examples/graphicsview/padnavigator/images/artsfftscope.png b/examples/graphicsview/padnavigator/images/artsfftscope.png Binary files differnew file mode 100644 index 0000000000..4db003f8d2 --- /dev/null +++ b/examples/graphicsview/padnavigator/images/artsfftscope.png diff --git a/examples/graphicsview/padnavigator/images/blue_angle_swirl.jpg b/examples/graphicsview/padnavigator/images/blue_angle_swirl.jpg Binary files differnew file mode 100644 index 0000000000..5bf0debdbd --- /dev/null +++ b/examples/graphicsview/padnavigator/images/blue_angle_swirl.jpg diff --git a/examples/graphicsview/padnavigator/images/kontact_contacts.png b/examples/graphicsview/padnavigator/images/kontact_contacts.png Binary files differnew file mode 100644 index 0000000000..6fb4cc8f38 --- /dev/null +++ b/examples/graphicsview/padnavigator/images/kontact_contacts.png diff --git a/examples/graphicsview/padnavigator/images/kontact_journal.png b/examples/graphicsview/padnavigator/images/kontact_journal.png Binary files differnew file mode 100644 index 0000000000..b1fedb6982 --- /dev/null +++ b/examples/graphicsview/padnavigator/images/kontact_journal.png diff --git a/examples/graphicsview/padnavigator/images/kontact_mail.png b/examples/graphicsview/padnavigator/images/kontact_mail.png Binary files differnew file mode 100644 index 0000000000..672f8fa3ab --- /dev/null +++ b/examples/graphicsview/padnavigator/images/kontact_mail.png diff --git a/examples/graphicsview/padnavigator/images/kontact_notes.png b/examples/graphicsview/padnavigator/images/kontact_notes.png Binary files differnew file mode 100644 index 0000000000..229bf73ac6 --- /dev/null +++ b/examples/graphicsview/padnavigator/images/kontact_notes.png diff --git a/examples/graphicsview/padnavigator/images/kopeteavailable.png b/examples/graphicsview/padnavigator/images/kopeteavailable.png Binary files differnew file mode 100644 index 0000000000..2eaf41afec --- /dev/null +++ b/examples/graphicsview/padnavigator/images/kopeteavailable.png diff --git a/examples/graphicsview/padnavigator/images/metacontact_online.png b/examples/graphicsview/padnavigator/images/metacontact_online.png Binary files differnew file mode 100644 index 0000000000..6a398dd10f --- /dev/null +++ b/examples/graphicsview/padnavigator/images/metacontact_online.png diff --git a/examples/graphicsview/padnavigator/images/minitools.png b/examples/graphicsview/padnavigator/images/minitools.png Binary files differnew file mode 100644 index 0000000000..0248c9d50a --- /dev/null +++ b/examples/graphicsview/padnavigator/images/minitools.png diff --git a/examples/graphicsview/padnavigator/main.cpp b/examples/graphicsview/padnavigator/main.cpp new file mode 100644 index 0000000000..8f4a681743 --- /dev/null +++ b/examples/graphicsview/padnavigator/main.cpp @@ -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 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 "padnavigator.h" + +#include <QtGui/QtGui> + +//! [0] +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + Q_INIT_RESOURCE(padnavigator); + + PadNavigator navigator(QSize(3, 3)); + navigator.show(); + + return app.exec(); +} +//! [0] diff --git a/examples/graphicsview/padnavigator/padnavigator.cpp b/examples/graphicsview/padnavigator/padnavigator.cpp new file mode 100644 index 0000000000..3e57dcfbe6 --- /dev/null +++ b/examples/graphicsview/padnavigator/padnavigator.cpp @@ -0,0 +1,306 @@ +/**************************************************************************** +** +** 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 "flippablepad.h" +#include "padnavigator.h" +#include "splashitem.h" + +#include <QtGui/QtGui> +#ifndef QT_NO_OPENGL +#include <QtOpenGL/QtOpenGL> +#endif + +//! [0] +PadNavigator::PadNavigator(const QSize &size, QWidget *parent) + : QGraphicsView(parent) +{ +//! [0] +//! [1] + // Splash item + SplashItem *splash = new SplashItem; + splash->setZValue(1); +//! [1] + +//! [2] + // Pad item + FlippablePad *pad = new FlippablePad(size); + QGraphicsRotation *flipRotation = new QGraphicsRotation(pad); + QGraphicsRotation *xRotation = new QGraphicsRotation(pad); + QGraphicsRotation *yRotation = new QGraphicsRotation(pad); + flipRotation->setAxis(Qt::YAxis); + xRotation->setAxis(Qt::YAxis); + yRotation->setAxis(Qt::XAxis); + pad->setTransformations(QList<QGraphicsTransform *>() + << flipRotation + << xRotation << yRotation); +//! [2] + +//! [3] + // Back (proxy widget) item + QGraphicsProxyWidget *backItem = new QGraphicsProxyWidget(pad); + QWidget *widget = new QWidget; + form.setupUi(widget); + form.hostName->setFocus(); + backItem->setWidget(widget); + backItem->setVisible(false); + backItem->setFocus(); + backItem->setCacheMode(QGraphicsItem::ItemCoordinateCache); + const QRectF r = backItem->rect(); + backItem->setTransform(QTransform() + .rotate(180, Qt::YAxis) + .translate(-r.width()/2, -r.height()/2)); +//! [3] + +//! [4] + // Selection item + RoundRectItem *selectionItem = new RoundRectItem(QRectF(-60, -60, 120, 120), + Qt::gray, pad); + selectionItem->setZValue(0.5); +//! [4] + +//! [5] + // Splash animations + QPropertyAnimation *smoothSplashMove = new QPropertyAnimation(splash, "y"); + QPropertyAnimation *smoothSplashOpacity = new QPropertyAnimation(splash, "opacity"); + smoothSplashMove->setEasingCurve(QEasingCurve::InQuad); + smoothSplashMove->setDuration(250); + smoothSplashOpacity->setDuration(250); +//! [5] + +//! [6] + // Selection animation + QPropertyAnimation *smoothXSelection = new QPropertyAnimation(selectionItem, "x"); + QPropertyAnimation *smoothYSelection = new QPropertyAnimation(selectionItem, "y"); + QPropertyAnimation *smoothXRotation = new QPropertyAnimation(xRotation, "angle"); + QPropertyAnimation *smoothYRotation = new QPropertyAnimation(yRotation, "angle"); + smoothXSelection->setDuration(125); + smoothYSelection->setDuration(125); + smoothXRotation->setDuration(125); + smoothYRotation->setDuration(125); + smoothXSelection->setEasingCurve(QEasingCurve::InOutQuad); + smoothYSelection->setEasingCurve(QEasingCurve::InOutQuad); + smoothXRotation->setEasingCurve(QEasingCurve::InOutQuad); + smoothYRotation->setEasingCurve(QEasingCurve::InOutQuad); +//! [6] + +//! [7] + // Flip animation setup + QPropertyAnimation *smoothFlipRotation = new QPropertyAnimation(flipRotation, "angle"); + QPropertyAnimation *smoothFlipScale = new QPropertyAnimation(pad, "scale"); + QPropertyAnimation *smoothFlipXRotation = new QPropertyAnimation(xRotation, "angle"); + QPropertyAnimation *smoothFlipYRotation = new QPropertyAnimation(yRotation, "angle"); + QParallelAnimationGroup *flipAnimation = new QParallelAnimationGroup(this); + smoothFlipScale->setDuration(500); + smoothFlipRotation->setDuration(500); + smoothFlipXRotation->setDuration(500); + smoothFlipYRotation->setDuration(500); + smoothFlipScale->setEasingCurve(QEasingCurve::InOutQuad); + smoothFlipRotation->setEasingCurve(QEasingCurve::InOutQuad); + smoothFlipXRotation->setEasingCurve(QEasingCurve::InOutQuad); + smoothFlipYRotation->setEasingCurve(QEasingCurve::InOutQuad); + smoothFlipScale->setKeyValueAt(0, qvariant_cast<qreal>(1.0)); + smoothFlipScale->setKeyValueAt(0.5, qvariant_cast<qreal>(0.7)); + smoothFlipScale->setKeyValueAt(1, qvariant_cast<qreal>(1.0)); + flipAnimation->addAnimation(smoothFlipRotation); + flipAnimation->addAnimation(smoothFlipScale); + flipAnimation->addAnimation(smoothFlipXRotation); + flipAnimation->addAnimation(smoothFlipYRotation); +//! [7] + +//! [8] + // Flip animation delayed property assignment + QSequentialAnimationGroup *setVariablesSequence = new QSequentialAnimationGroup; + QPropertyAnimation *setFillAnimation = new QPropertyAnimation(pad, "fill"); + QPropertyAnimation *setBackItemVisibleAnimation = new QPropertyAnimation(backItem, "visible"); + QPropertyAnimation *setSelectionItemVisibleAnimation = new QPropertyAnimation(selectionItem, "visible"); + setFillAnimation->setDuration(0); + setBackItemVisibleAnimation->setDuration(0); + setSelectionItemVisibleAnimation->setDuration(0); + setVariablesSequence->addPause(250); + setVariablesSequence->addAnimation(setBackItemVisibleAnimation); + setVariablesSequence->addAnimation(setSelectionItemVisibleAnimation); + setVariablesSequence->addAnimation(setFillAnimation); + flipAnimation->addAnimation(setVariablesSequence); +//! [8] + +//! [9] + // Build the state machine + QStateMachine *stateMachine = new QStateMachine(this); + QState *splashState = new QState(stateMachine); + QState *frontState = new QState(stateMachine); + QHistoryState *historyState = new QHistoryState(frontState); + QState *backState = new QState(stateMachine); +//! [9] +//! [10] + frontState->assignProperty(pad, "fill", false); + frontState->assignProperty(splash, "opacity", 0.0); + frontState->assignProperty(backItem, "visible", false); + frontState->assignProperty(flipRotation, "angle", qvariant_cast<qreal>(0.0)); + frontState->assignProperty(selectionItem, "visible", true); + backState->assignProperty(pad, "fill", true); + backState->assignProperty(backItem, "visible", true); + backState->assignProperty(xRotation, "angle", qvariant_cast<qreal>(0.0)); + backState->assignProperty(yRotation, "angle", qvariant_cast<qreal>(0.0)); + backState->assignProperty(flipRotation, "angle", qvariant_cast<qreal>(180.0)); + backState->assignProperty(selectionItem, "visible", false); + stateMachine->addDefaultAnimation(smoothXRotation); + stateMachine->addDefaultAnimation(smoothYRotation); + stateMachine->addDefaultAnimation(smoothXSelection); + stateMachine->addDefaultAnimation(smoothYSelection); + stateMachine->setInitialState(splashState); +//! [10] + +//! [11] + // Transitions + QEventTransition *anyKeyTransition = new QEventTransition(this, QEvent::KeyPress, splashState); + anyKeyTransition->setTargetState(frontState); + anyKeyTransition->addAnimation(smoothSplashMove); + anyKeyTransition->addAnimation(smoothSplashOpacity); +//! [11] + +//! [12] + QKeyEventTransition *enterTransition = new QKeyEventTransition(this, QEvent::KeyPress, + Qt::Key_Enter, backState); + QKeyEventTransition *returnTransition = new QKeyEventTransition(this, QEvent::KeyPress, + Qt::Key_Return, backState); + QKeyEventTransition *backEnterTransition = new QKeyEventTransition(this, QEvent::KeyPress, + Qt::Key_Enter, frontState); + QKeyEventTransition *backReturnTransition = new QKeyEventTransition(this, QEvent::KeyPress, + Qt::Key_Return, frontState); + enterTransition->setTargetState(historyState); + returnTransition->setTargetState(historyState); + backEnterTransition->setTargetState(backState); + backReturnTransition->setTargetState(backState); + enterTransition->addAnimation(flipAnimation); + returnTransition->addAnimation(flipAnimation); + backEnterTransition->addAnimation(flipAnimation); + backReturnTransition->addAnimation(flipAnimation); +//! [12] + +//! [13] + // Create substates for each icon; store in temporary grid. + int columns = size.width(); + int rows = size.height(); + QVector< QVector< QState * > > stateGrid; + stateGrid.resize(rows); + for (int y = 0; y < rows; ++y) { + stateGrid[y].resize(columns); + for (int x = 0; x < columns; ++x) + stateGrid[y][x] = new QState(frontState); + } + frontState->setInitialState(stateGrid[0][0]); + selectionItem->setPos(pad->iconAt(0, 0)->pos()); +//! [13] + +//! [14] + // Enable key navigation using state transitions + for (int y = 0; y < rows; ++y) { + for (int x = 0; x < columns; ++x) { + QState *state = stateGrid[y][x]; + QKeyEventTransition *rightTransition = new QKeyEventTransition(this, QEvent::KeyPress, + Qt::Key_Right, state); + QKeyEventTransition *leftTransition = new QKeyEventTransition(this, QEvent::KeyPress, + Qt::Key_Left, state); + QKeyEventTransition *downTransition = new QKeyEventTransition(this, QEvent::KeyPress, + Qt::Key_Down, state); + QKeyEventTransition *upTransition = new QKeyEventTransition(this, QEvent::KeyPress, + Qt::Key_Up, state); + rightTransition->setTargetState(stateGrid[y][(x + 1) % columns]); + leftTransition->setTargetState(stateGrid[y][((x - 1) + columns) % columns]); + downTransition->setTargetState(stateGrid[(y + 1) % rows][x]); + upTransition->setTargetState(stateGrid[((y - 1) + rows) % rows][x]); +//! [14] +//! [15] + RoundRectItem *icon = pad->iconAt(x, y); + state->assignProperty(xRotation, "angle", -icon->x() / 6.0); + state->assignProperty(yRotation, "angle", icon->y() / 6.0); + state->assignProperty(selectionItem, "x", icon->x()); + state->assignProperty(selectionItem, "y", icon->y()); + frontState->assignProperty(icon, "visible", true); + backState->assignProperty(icon, "visible", false); + + QPropertyAnimation *setIconVisibleAnimation = new QPropertyAnimation(icon, "visible"); + setIconVisibleAnimation->setDuration(0); + setVariablesSequence->addAnimation(setIconVisibleAnimation); + } + } +//! [15] + +//! [16] + // Scene + QGraphicsScene *scene = new QGraphicsScene(this); + scene->setBackgroundBrush(QPixmap(":/images/blue_angle_swirl.jpg")); + scene->setItemIndexMethod(QGraphicsScene::NoIndex); + scene->addItem(pad); + scene->setSceneRect(scene->itemsBoundingRect()); + setScene(scene); +//! [16] + +//! [17] + // Adjust splash item to scene contents + const QRectF sbr = splash->boundingRect(); + splash->setPos(-sbr.width() / 2, scene->sceneRect().top() - 2); + frontState->assignProperty(splash, "y", splash->y() - 100.0); + scene->addItem(splash); +//! [17] + +//! [18] + // View + setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + setMinimumSize(50, 50); + setViewportUpdateMode(FullViewportUpdate); + setCacheMode(CacheBackground); + setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform | QPainter::TextAntialiasing); +#ifndef QT_NO_OPENGL + setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers))); +#endif + + stateMachine->start(); +//! [18] +} + +//! [19] +void PadNavigator::resizeEvent(QResizeEvent *event) +{ + QGraphicsView::resizeEvent(event); + fitInView(scene()->sceneRect(), Qt::KeepAspectRatio); +} +//! [19] diff --git a/examples/graphicsview/padnavigator/padnavigator.h b/examples/graphicsview/padnavigator/padnavigator.h new file mode 100644 index 0000000000..0fcfde931f --- /dev/null +++ b/examples/graphicsview/padnavigator/padnavigator.h @@ -0,0 +1,70 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +#ifndef PADNAVIGATOR_H +#define PADNAVIGATOR_H + +#include <QGraphicsView> +#include "ui_form.h" + +QT_BEGIN_NAMESPACE + +class QState; +class QStateMachine; +class Ui_Form; + +QT_END_NAMESPACE + +//! [0] +class PadNavigator : public QGraphicsView +{ + Q_OBJECT +public: + explicit PadNavigator(const QSize &size, QWidget *parent = 0); + +protected: + void resizeEvent(QResizeEvent *event); + +private: + Ui_Form form; +}; +//! [0] + +#endif // PADNAVIGATOR_H diff --git a/examples/graphicsview/padnavigator/padnavigator.pro b/examples/graphicsview/padnavigator/padnavigator.pro new file mode 100644 index 0000000000..de492f3e9e --- /dev/null +++ b/examples/graphicsview/padnavigator/padnavigator.pro @@ -0,0 +1,32 @@ +SOURCES += main.cpp \ + roundrectitem.cpp \ + flippablepad.cpp \ + padnavigator.cpp \ + splashitem.cpp + +HEADERS += \ + roundrectitem.h \ + flippablepad.h \ + padnavigator.h \ + splashitem.h + +RESOURCES += \ + padnavigator.qrc + +FORMS += \ + form.ui + +contains(QT_CONFIG, opengl):QT += opengl + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/graphicsview/padnavigator +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS padnavigator.pro images +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/graphicsview/padnavigator +INSTALLS += target sources + +CONFIG += console + +symbian { + TARGET.UID3 = 0xA000A644 + include($$QT_SOURCE_TREE/examples/symbianpkgrules.pri) +} diff --git a/examples/graphicsview/padnavigator/padnavigator.qrc b/examples/graphicsview/padnavigator/padnavigator.qrc new file mode 100644 index 0000000000..b26ab92ae1 --- /dev/null +++ b/examples/graphicsview/padnavigator/padnavigator.qrc @@ -0,0 +1,13 @@ +<RCC> + <qresource> + <file>images/blue_angle_swirl.jpg</file> + <file>images/artsfftscope.png</file> + <file>images/kontact_contacts.png</file> + <file>images/kontact_journal.png</file> + <file>images/kontact_mail.png</file> + <file>images/kontact_notes.png</file> + <file>images/kopeteavailable.png</file> + <file>images/metacontact_online.png</file> + <file>images/minitools.png</file> + </qresource> +</RCC> diff --git a/examples/graphicsview/padnavigator/roundrectitem.cpp b/examples/graphicsview/padnavigator/roundrectitem.cpp new file mode 100644 index 0000000000..643770f4c6 --- /dev/null +++ b/examples/graphicsview/padnavigator/roundrectitem.cpp @@ -0,0 +1,113 @@ +/**************************************************************************** +** +** 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 "roundrectitem.h" + +#include <QtGui/QtGui> + +//! [0] +RoundRectItem::RoundRectItem(const QRectF &bounds, const QColor &color, + QGraphicsItem *parent) + : QGraphicsObject(parent), fillRect(false), bounds(bounds) +{ + gradient.setStart(bounds.topLeft()); + gradient.setFinalStop(bounds.bottomRight()); + gradient.setColorAt(0, color); + gradient.setColorAt(1, color.dark(200)); + setCacheMode(ItemCoordinateCache); +} +//! [0] + +//! [1] +QPixmap RoundRectItem::pixmap() const +{ + return pix; +} +void RoundRectItem::setPixmap(const QPixmap &pixmap) +{ + pix = pixmap; + update(); +} +//! [1] + +//! [2] +QRectF RoundRectItem::boundingRect() const +{ + return bounds.adjusted(0, 0, 2, 2); +} +//! [2] + +//! [3] +void RoundRectItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, + QWidget *widget) +{ + Q_UNUSED(option); + Q_UNUSED(widget); + painter->setPen(Qt::NoPen); + painter->setBrush(QColor(0, 0, 0, 64)); + painter->drawRoundRect(bounds.translated(2, 2)); +//! [3] +//! [4] + if (fillRect) + painter->setBrush(QApplication::palette().brush(QPalette::Window)); + else + painter->setBrush(gradient); + painter->setPen(QPen(Qt::black, 1)); + painter->drawRoundRect(bounds); +//! [4] +//! [5] + if (!pix.isNull()) { + painter->scale(1.95, 1.95); + painter->drawPixmap(-pix.width() / 2, -pix.height() / 2, pix); + } +} +//! [5] + +//! [6] +bool RoundRectItem::fill() const +{ + return fillRect; +} +void RoundRectItem::setFill(bool fill) +{ + fillRect = fill; + update(); +} +//! [6] diff --git a/examples/graphicsview/padnavigator/roundrectitem.h b/examples/graphicsview/padnavigator/roundrectitem.h new file mode 100644 index 0000000000..a7d014f12a --- /dev/null +++ b/examples/graphicsview/padnavigator/roundrectitem.h @@ -0,0 +1,75 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +#ifndef ROUNDRECTITEM_H +#define ROUNDRECTITEM_H + +#include <QGraphicsObject> +#include <QLinearGradient> + +//! [0] +class RoundRectItem : public QGraphicsObject +{ + Q_OBJECT + Q_PROPERTY(bool fill READ fill WRITE setFill) +public: + RoundRectItem(const QRectF &bounds, const QColor &color, + QGraphicsItem *parent = 0); + + QPixmap pixmap() const; + void setPixmap(const QPixmap &pixmap); + + QRectF boundingRect() const; + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); + + bool fill() const; + void setFill(bool fill); +//! [0] + +//! [1] +private: + QPixmap pix; + bool fillRect; + QRectF bounds; + QLinearGradient gradient; +}; +//! [1] + +#endif // ROUNDRECTITEM_H diff --git a/examples/graphicsview/padnavigator/splashitem.cpp b/examples/graphicsview/padnavigator/splashitem.cpp new file mode 100644 index 0000000000..8cedcd6e60 --- /dev/null +++ b/examples/graphicsview/padnavigator/splashitem.cpp @@ -0,0 +1,83 @@ +/**************************************************************************** +** +** 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 "splashitem.h" + +#include <QtGui/QtGui> + +//! [0] +SplashItem::SplashItem(QGraphicsItem *parent) + : QGraphicsObject(parent) +{ + text = tr("Welcome to the Pad Navigator Example. You can use the" + " keyboard arrows to navigate the icons, and press enter" + " to activate an item. Press any key to begin."); + setCacheMode(DeviceCoordinateCache); +} +//! [0] + +//! [1] +QRectF SplashItem::boundingRect() const +{ + return QRectF(0, 0, 400, 175); +} +//! [1] + +//! [2] +void SplashItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, + QWidget *widget) +{ + Q_UNUSED(option); + Q_UNUSED(widget); + painter->setPen(QPen(Qt::black, 2)); + painter->setBrush(QColor(245, 245, 255, 220)); + painter->setClipRect(boundingRect()); + painter->drawRoundRect(3, -100 + 3, 400 - 6, 250 - 6); + + QRectF textRect = boundingRect().adjusted(10, 10, -10, -10); + int flags = Qt::AlignTop | Qt::AlignLeft | Qt::TextWordWrap; + + QFont font; + font.setPixelSize(18); + painter->setPen(Qt::black); + painter->setFont(font); + painter->drawText(textRect, flags, text); +} +//! [2] diff --git a/examples/graphicsview/padnavigator/splashitem.h b/examples/graphicsview/padnavigator/splashitem.h new file mode 100644 index 0000000000..4d0b58a824 --- /dev/null +++ b/examples/graphicsview/padnavigator/splashitem.h @@ -0,0 +1,61 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +#ifndef SPLASHITEM_H +#define SPLASHITEM_H + +#include <QGraphicsObject> + +//! [0] +class SplashItem : public QGraphicsObject +{ + Q_OBJECT +public: + explicit SplashItem(QGraphicsItem *parent = 0); + + QRectF boundingRect() const; + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); + +private: + QString text; +}; +//! [0] + +#endif // SPLASHITEM_H diff --git a/examples/graphicsview/simpleanchorlayout/main.cpp b/examples/graphicsview/simpleanchorlayout/main.cpp new file mode 100644 index 0000000000..4fa837f540 --- /dev/null +++ b/examples/graphicsview/simpleanchorlayout/main.cpp @@ -0,0 +1,133 @@ +/**************************************************************************** +** +** 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> + +class Widget : public QGraphicsWidget +{ +public: + Widget(const QColor &color, const QColor &textColor, const QString &caption, + QGraphicsItem *parent = 0) + : QGraphicsWidget(parent), caption(caption), color(color), textColor(textColor) + { + } + + void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget * = 0) + { + QFont font; + font.setPixelSize(0.75 * qMin(boundingRect().width(), boundingRect().height())); + + painter->fillRect(boundingRect(), color); + painter->save(); + painter->setFont(font); + painter->setPen(textColor); + painter->drawText(boundingRect(), Qt::AlignCenter, caption); + painter->restore(); + } + +private: + QString caption; + QColor color; + QColor textColor; +}; + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + + QGraphicsScene *scene = new QGraphicsScene(); + + Widget *a = new Widget(Qt::blue, Qt::white, "a"); + a->setPreferredSize(100, 100); + Widget *b = new Widget(Qt::green, Qt::black, "b"); + b->setPreferredSize(100, 100); + Widget *c = new Widget(Qt::red, Qt::black, "c"); + c->setPreferredSize(100, 100); + + QGraphicsAnchorLayout *layout = new QGraphicsAnchorLayout(); +/* + //! [adding a corner anchor in two steps] + layout->addAnchor(a, Qt::AnchorTop, layout, Qt::AnchorTop); + layout->addAnchor(a, Qt::AnchorLeft, layout, Qt::AnchorLeft); + //! [adding a corner anchor in two steps] +*/ + //! [adding a corner anchor] + layout->addCornerAnchors(a, Qt::TopLeftCorner, layout, Qt::TopLeftCorner); + //! [adding a corner anchor] + + //! [adding anchors] + layout->addAnchor(b, Qt::AnchorLeft, a, Qt::AnchorRight); + layout->addAnchor(b, Qt::AnchorTop, a, Qt::AnchorBottom); + //! [adding anchors] + + // Place a third widget below the second. + layout->addAnchor(b, Qt::AnchorBottom, c, Qt::AnchorTop); + +/* + //! [adding anchors to match sizes in two steps] + layout->addAnchor(b, Qt::AnchorLeft, c, Qt::AnchorLeft); + layout->addAnchor(b, Qt::AnchorRight, c, Qt::AnchorRight); + //! [adding anchors to match sizes in two steps] +*/ + + //! [adding anchors to match sizes] + layout->addAnchors(b, c, Qt::Horizontal); + //! [adding anchors to match sizes] + + // Anchor the bottom-right corner of the third widget to the bottom-right + // corner of the layout. + layout->addCornerAnchors(c, Qt::BottomRightCorner, layout, Qt::BottomRightCorner); + + QGraphicsWidget *w = new QGraphicsWidget(0, Qt::Window | Qt::CustomizeWindowHint | Qt::WindowTitleHint); + w->setPos(20, 20); + w->setMinimumSize(100, 100); + w->setPreferredSize(320, 240); + w->setLayout(layout); + w->setWindowTitle(QApplication::translate("simpleanchorlayout", "QGraphicsAnchorLayout in use")); + scene->addItem(w); + + QGraphicsView *view = new QGraphicsView(); + view->setScene(scene); + view->setWindowTitle(QApplication::translate("simpleanchorlayout", "Simple Anchor Layout")); + view->resize(360, 320); + view->show(); + + return app.exec(); +} diff --git a/examples/graphicsview/simpleanchorlayout/simpleanchorlayout.pro b/examples/graphicsview/simpleanchorlayout/simpleanchorlayout.pro new file mode 100644 index 0000000000..f85ad8f3a7 --- /dev/null +++ b/examples/graphicsview/simpleanchorlayout/simpleanchorlayout.pro @@ -0,0 +1,9 @@ +SOURCES = main.cpp + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/graphicsview/simpleanchorlayout +sources.files = $$SOURCES $$HEADERS $$RESOURCES simpleanchorlayout.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/graphicsview/simpleanchorlayout +INSTALLS += target sources + +TARGET = simpleanchorlayout diff --git a/examples/graphicsview/weatheranchorlayout/images/5days.jpg b/examples/graphicsview/weatheranchorlayout/images/5days.jpg Binary files differnew file mode 100644 index 0000000000..fd92ba8ba7 --- /dev/null +++ b/examples/graphicsview/weatheranchorlayout/images/5days.jpg diff --git a/examples/graphicsview/weatheranchorlayout/images/details.jpg b/examples/graphicsview/weatheranchorlayout/images/details.jpg Binary files differnew file mode 100644 index 0000000000..fde0448c69 --- /dev/null +++ b/examples/graphicsview/weatheranchorlayout/images/details.jpg diff --git a/examples/graphicsview/weatheranchorlayout/images/place.jpg b/examples/graphicsview/weatheranchorlayout/images/place.jpg Binary files differnew file mode 100644 index 0000000000..03e5344330 --- /dev/null +++ b/examples/graphicsview/weatheranchorlayout/images/place.jpg diff --git a/examples/graphicsview/weatheranchorlayout/images/tabbar.jpg b/examples/graphicsview/weatheranchorlayout/images/tabbar.jpg Binary files differnew file mode 100644 index 0000000000..7777662901 --- /dev/null +++ b/examples/graphicsview/weatheranchorlayout/images/tabbar.jpg diff --git a/examples/graphicsview/weatheranchorlayout/images/title.jpg b/examples/graphicsview/weatheranchorlayout/images/title.jpg Binary files differnew file mode 100644 index 0000000000..fa84c8156c --- /dev/null +++ b/examples/graphicsview/weatheranchorlayout/images/title.jpg diff --git a/examples/graphicsview/weatheranchorlayout/images/weather-few-clouds.png b/examples/graphicsview/weatheranchorlayout/images/weather-few-clouds.png Binary files differnew file mode 100644 index 0000000000..eea6ce6529 --- /dev/null +++ b/examples/graphicsview/weatheranchorlayout/images/weather-few-clouds.png diff --git a/examples/graphicsview/weatheranchorlayout/main.cpp b/examples/graphicsview/weatheranchorlayout/main.cpp new file mode 100644 index 0000000000..67596ac4bc --- /dev/null +++ b/examples/graphicsview/weatheranchorlayout/main.cpp @@ -0,0 +1,259 @@ +/**************************************************************************** +** +** 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 <QLabel> +#include <QPainter> +#include <QPushButton> +#include <QApplication> + +#include <QGraphicsView> +#include <QGraphicsScene> +#include <QGraphicsWidget> +#include <QGraphicsProxyWidget> +#include <QGraphicsAnchorLayout> +#include <QGraphicsSceneResizeEvent> + + +class PixmapWidget : public QGraphicsLayoutItem +{ + +public: + PixmapWidget(const QPixmap &pix) : QGraphicsLayoutItem() + { + original = new QGraphicsPixmapItem(pix); + setGraphicsItem(original); + original->show(); + r = QRectF(QPointF(0, 0), pix.size()); + } + + ~PixmapWidget() + { + setGraphicsItem(0); + delete original; + } + + void setZValue(qreal z) + { + original->setZValue(z); + } + + void setGeometry (const QRectF &rect) + { + original->scale(rect.width() / r.width(), rect.height() / r.height()); + original->setPos(rect.x(), rect.y()); + r = rect; + } + +protected: + QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint = QSizeF()) const + { + Q_UNUSED(constraint); + QSizeF sh; + switch (which) { + case Qt::MinimumSize: + sh = QSizeF(0, 0); + break; + case Qt::PreferredSize: + sh = QSizeF(50, 50); + break; + case Qt::MaximumSize: + sh = QSizeF(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX); + break; + default: + break; + } + return sh; + } + +private: + QGraphicsPixmapItem *original; + QRectF r; +}; + + +class PlaceWidget : public QGraphicsWidget +{ + Q_OBJECT + +public: + PlaceWidget(const QPixmap &pix) : QGraphicsWidget(), original(pix), scaled(pix) + { + } + + void paint(QPainter *painter, const QStyleOptionGraphicsItem*, QWidget*) + { + QPointF reflection = QPointF(); + reflection.setY(scaled.height() + 2); + + painter->drawPixmap(QPointF(), scaled); + + QPixmap tmp(scaled.size()); + tmp.fill(Qt::transparent); + QPainter p(&tmp); + + // create gradient + QPoint p1(scaled.width() / 2, 0); + QPoint p2(scaled.width() / 2, scaled.height()); + QLinearGradient linearGrad(p1, p2); + linearGrad.setColorAt(0, QColor(0, 0, 0, 0)); + linearGrad.setColorAt(0.65, QColor(0, 0, 0, 127)); + linearGrad.setColorAt(1, QColor(0, 0, 0, 255)); + + // apply 'mask' + p.setBrush(linearGrad); + p.fillRect(0, 0, tmp.width(), tmp.height(), QBrush(linearGrad)); + p.fillRect(0, 0, tmp.width(), tmp.height(), QBrush(linearGrad)); + + // paint the image flipped + p.setCompositionMode(QPainter::CompositionMode_DestinationOver); + p.drawPixmap(0, 0, QPixmap::fromImage(scaled.toImage().mirrored(false, true))); + p.end(); + + painter->drawPixmap(reflection, tmp); + } + + void resizeEvent(QGraphicsSceneResizeEvent *event) + { + QSize newSize = event->newSize().toSize(); + newSize.setHeight(newSize.height() / 2); + scaled = original.scaled(newSize); + } + + QRectF boundingRect() const + { + QSize size(scaled.width(), scaled.height() * 2 + 2); + return QRectF(QPointF(0, 0), size); + } + +private: + QPixmap original; + QPixmap scaled; +}; + + +int main(int argc, char **argv) +{ + Q_INIT_RESOURCE(weatheranchorlayout); + + QApplication app(argc, argv); + + QGraphicsScene scene; + scene.setSceneRect(0, 0, 800, 480); + + // pixmaps widgets + PixmapWidget *title = new PixmapWidget(QPixmap(":/images/title.jpg")); + PlaceWidget *place = new PlaceWidget(QPixmap(":/images/place.jpg")); + PixmapWidget *details = new PixmapWidget(QPixmap(":/images/5days.jpg")); + PixmapWidget *sunnyWeather = new PixmapWidget(QPixmap(":/images/weather-few-clouds.png")); + PixmapWidget *tabbar = new PixmapWidget(QPixmap(":/images/tabbar.jpg")); + + + // setup sizes + title->setPreferredSize(QSizeF(348, 45)); + title->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum); + + place->setPreferredSize(QSizeF(96, 72)); + place->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum); + + details->setMinimumSize(QSizeF(200, 112)); + details->setPreferredSize(QSizeF(200, 112)); + details->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum); + + tabbar->setPreferredSize(QSizeF(70, 24)); + tabbar->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum); + + sunnyWeather->setPreferredSize(QSizeF(128, 97)); + sunnyWeather->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum); + sunnyWeather->setZValue(9999); + + // start anchor layout + QGraphicsAnchorLayout *l = new QGraphicsAnchorLayout; + l->setSpacing(0); + + // setup the main widget + QGraphicsWidget *w = new QGraphicsWidget(0, Qt::Window); + QPalette p; + p.setColor(QPalette::Window, Qt::black); + w->setPalette(p); + w->setPos(20, 20); + w->setLayout(l); + + // vertical anchors + QGraphicsAnchor *anchor = l->addAnchor(title, Qt::AnchorTop, l, Qt::AnchorTop); + anchor = l->addAnchor(place, Qt::AnchorTop, title, Qt::AnchorBottom); + anchor->setSpacing(12); + anchor = l->addAnchor(place, Qt::AnchorBottom, l, Qt::AnchorBottom); + anchor->setSpacing(12); + + anchor = l->addAnchor(sunnyWeather, Qt::AnchorTop, title, Qt::AnchorTop); + anchor = l->addAnchor(sunnyWeather, Qt::AnchorBottom, l, Qt::AnchorVerticalCenter); + + anchor = l->addAnchor(tabbar, Qt::AnchorTop, title, Qt::AnchorBottom); + anchor->setSpacing(5); + anchor = l->addAnchor(details, Qt::AnchorTop, tabbar, Qt::AnchorBottom); + anchor->setSpacing(2); + anchor = l->addAnchor(details, Qt::AnchorBottom, l, Qt::AnchorBottom); + anchor->setSpacing(12); + + // horizontal anchors + anchor = l->addAnchor(l, Qt::AnchorLeft, title, Qt::AnchorLeft); + anchor = l->addAnchor(title, Qt::AnchorRight, l, Qt::AnchorRight); + + anchor = l->addAnchor(place, Qt::AnchorLeft, l, Qt::AnchorLeft); + anchor->setSpacing(15); + anchor = l->addAnchor(place, Qt::AnchorRight, details, Qt::AnchorLeft); + anchor->setSpacing(35); + + anchor = l->addAnchor(sunnyWeather, Qt::AnchorLeft, place, Qt::AnchorHorizontalCenter); + anchor = l->addAnchor(sunnyWeather, Qt::AnchorRight, l, Qt::AnchorHorizontalCenter); + + anchor = l->addAnchor(tabbar, Qt::AnchorHorizontalCenter, details, Qt::AnchorHorizontalCenter); + anchor = l->addAnchor(details, Qt::AnchorRight, l, Qt::AnchorRight); + + // QGV setup + scene.addItem(w); + scene.setBackgroundBrush(Qt::white); + QGraphicsView *view = new QGraphicsView(&scene); + view->show(); + + return app.exec(); +} + +#include "main.moc" diff --git a/examples/graphicsview/weatheranchorlayout/weatheranchorlayout.pro b/examples/graphicsview/weatheranchorlayout/weatheranchorlayout.pro new file mode 100644 index 0000000000..57e154e27a --- /dev/null +++ b/examples/graphicsview/weatheranchorlayout/weatheranchorlayout.pro @@ -0,0 +1,14 @@ +###################################################################### +# Automatically generated by qmake (2.01a) Tue May 12 15:22:25 2009 +###################################################################### + +# Input +SOURCES += main.cpp +RESOURCES += weatheranchorlayout.qrc + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/graphicsview/weatheranchorlayout +sources.files = $$SOURCES $$HEADERS $$RESOURCES weatheranchorlayout.pro images +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/graphicsview/weatheranchorlayout +INSTALLS += target sources + diff --git a/examples/graphicsview/weatheranchorlayout/weatheranchorlayout.qrc b/examples/graphicsview/weatheranchorlayout/weatheranchorlayout.qrc new file mode 100644 index 0000000000..e39f8c0423 --- /dev/null +++ b/examples/graphicsview/weatheranchorlayout/weatheranchorlayout.qrc @@ -0,0 +1,10 @@ +<!DOCTYPE RCC><RCC version="1.0"> +<qresource> + <file>images/5days.jpg</file> + <file>images/title.jpg</file> + <file>images/place.jpg</file> + <file>images/tabbar.jpg</file> + <file>images/details.jpg</file> + <file>images/weather-few-clouds.png</file> +</qresource> +</RCC> |