diff options
author | Lars Knoll <lars.knoll@nokia.com> | 2009-03-23 09:18:55 (GMT) |
---|---|---|
committer | Simon Hausmann <simon.hausmann@nokia.com> | 2009-03-23 09:18:55 (GMT) |
commit | e5fcad302d86d316390c6b0f62759a067313e8a9 (patch) | |
tree | c2afbf6f1066b6ce261f14341cf6d310e5595bc1 /examples/graphicsview | |
download | Qt-e5fcad302d86d316390c6b0f62759a067313e8a9.zip Qt-e5fcad302d86d316390c6b0f62759a067313e8a9.tar.gz Qt-e5fcad302d86d316390c6b0f62759a067313e8a9.tar.bz2 |
Long live Qt 4.5!
Diffstat (limited to 'examples/graphicsview')
272 files changed, 9535 insertions, 0 deletions
diff --git a/examples/graphicsview/README b/examples/graphicsview/README new file mode 100644 index 0000000..6c38c18 --- /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/basicgraphicslayouts/basicgraphicslayouts.pro b/examples/graphicsview/basicgraphicslayouts/basicgraphicslayouts.pro new file mode 100644 index 0000000..a166882 --- /dev/null +++ b/examples/graphicsview/basicgraphicslayouts/basicgraphicslayouts.pro @@ -0,0 +1,12 @@ +HEADERS = layoutitem.h \ + window.h +SOURCES = layoutitem.cpp \ + main.cpp \ + window.cpp +RESOURCES = basicgraphicslayouts.qrc + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/graphicsview/basicgraphicslayouts +sources.files = $$SOURCES $$HEADERS $$RESOURCES basicgraphicslayouts.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/graphicsview/basicgraphicslayouts +INSTALLS += target sources diff --git a/examples/graphicsview/basicgraphicslayouts/basicgraphicslayouts.qrc b/examples/graphicsview/basicgraphicslayouts/basicgraphicslayouts.qrc new file mode 100644 index 0000000..fbe2efa --- /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 0000000..4c72a3f --- /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 0000000..8216b6e --- /dev/null +++ b/examples/graphicsview/basicgraphicslayouts/layoutitem.cpp @@ -0,0 +1,99 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "layoutitem.h" + +//! [0] +LayoutItem::LayoutItem(QGraphicsItem *parent/* = 0*/) + : QGraphicsWidget(parent) +{ + pix = new QPixmap(QLatin1String(":/images/block.png")); + // Do not allow a size smaller than the pixmap with two frames around it. + setMinimumSize(pix->size() + QSize(12, 12)); +} +//! [0] + +LayoutItem::~LayoutItem() +{ + delete 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 = pix->width(); + qreal h = 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, *pix); +} +//! [2] diff --git a/examples/graphicsview/basicgraphicslayouts/layoutitem.h b/examples/graphicsview/basicgraphicslayouts/layoutitem.h new file mode 100644 index 0000000..cbda636 --- /dev/null +++ b/examples/graphicsview/basicgraphicslayouts/layoutitem.h @@ -0,0 +1,62 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef LAYOUTITEM_H +#define LAYOUTITEM_H +#include <QtGui> + +//! [0] +class LayoutItem : public QGraphicsWidget +{ + Q_OBJECT + +public: + LayoutItem(QGraphicsItem *parent = 0); + ~LayoutItem(); + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, + QWidget *widget = 0); + +private: + QPixmap *pix; +}; +//! [0] + +#endif diff --git a/examples/graphicsview/basicgraphicslayouts/main.cpp b/examples/graphicsview/basicgraphicslayouts/main.cpp new file mode 100644 index 0000000..720f6ff --- /dev/null +++ b/examples/graphicsview/basicgraphicslayouts/main.cpp @@ -0,0 +1,59 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $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 0000000..afa98eb --- /dev/null +++ b/examples/graphicsview/basicgraphicslayouts/window.cpp @@ -0,0 +1,91 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $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 0000000..0ecde8a --- /dev/null +++ b/examples/graphicsview/basicgraphicslayouts/window.h @@ -0,0 +1,58 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $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 0000000..77543b5 --- /dev/null +++ b/examples/graphicsview/collidingmice/collidingmice.pro @@ -0,0 +1,14 @@ +HEADERS += \ + mouse.h +SOURCES += \ + main.cpp \ + mouse.cpp + +RESOURCES += \ + mice.qrc + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/graphicsview/collidingmice +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS collidingmice.pro images +sources.path = $$[QT_INSTALL_EXAMPLES]/graphicsview/collidingmice +INSTALLS += target sources diff --git a/examples/graphicsview/collidingmice/images/cheese.jpg b/examples/graphicsview/collidingmice/images/cheese.jpg Binary files differnew file mode 100644 index 0000000..dea5795 --- /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 0000000..4a44481 --- /dev/null +++ b/examples/graphicsview/collidingmice/main.cpp @@ -0,0 +1,88 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $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(); + + return app.exec(); +} +//! [6] diff --git a/examples/graphicsview/collidingmice/mice.qrc b/examples/graphicsview/collidingmice/mice.qrc new file mode 100644 index 0000000..accdb4d --- /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 0000000..1d10574 --- /dev/null +++ b/examples/graphicsview/collidingmice/mouse.cpp @@ -0,0 +1,200 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $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) +{ + rotate(qrand() % (360 * 16)); + startTimer(1000 / 33); +} +//! [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::timerEvent(QTimerEvent *) +{ +//! [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; + + rotate(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 0000000..832ea53 --- /dev/null +++ b/examples/graphicsview/collidingmice/mouse.h @@ -0,0 +1,72 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef MOUSE_H +#define MOUSE_H + +#include <QGraphicsItem> +#include <QObject> + +//! [0] +class Mouse : public QObject, public QGraphicsItem +{ + Q_OBJECT + +public: + Mouse(); + + QRectF boundingRect() const; + QPainterPath shape() const; + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, + QWidget *widget); + +protected: + void timerEvent(QTimerEvent *event); + +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 0000000..0bc2ca8 --- /dev/null +++ b/examples/graphicsview/diagramscene/arrow.cpp @@ -0,0 +1,147 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $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 0000000..f624eb1 --- /dev/null +++ b/examples/graphicsview/diagramscene/arrow.h @@ -0,0 +1,94 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $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; } + + +public slots: + 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 0000000..b31f6b5 --- /dev/null +++ b/examples/graphicsview/diagramscene/diagramitem.cpp @@ -0,0 +1,152 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $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); +} +//! [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 0000000..5409f20 --- /dev/null +++ b/examples/graphicsview/diagramscene/diagramitem.h @@ -0,0 +1,97 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $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 0000000..8065ec5 --- /dev/null +++ b/examples/graphicsview/diagramscene/diagramscene.cpp @@ -0,0 +1,241 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $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 0000000..e33ee4f --- /dev/null +++ b/examples/graphicsview/diagramscene/diagramscene.h @@ -0,0 +1,113 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $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 0000000..fe261bd --- /dev/null +++ b/examples/graphicsview/diagramscene/diagramscene.pro @@ -0,0 +1,20 @@ +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]/graphicsview/diagramscene +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS diagramscene.pro images +sources.path = $$[QT_INSTALL_EXAMPLES]/graphicsview/diagramscene +INSTALLS += target sources + diff --git a/examples/graphicsview/diagramscene/diagramscene.qrc b/examples/graphicsview/diagramscene/diagramscene.qrc new file mode 100644 index 0000000..a111584 --- /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 0000000..008b70f --- /dev/null +++ b/examples/graphicsview/diagramscene/diagramtextitem.cpp @@ -0,0 +1,82 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $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 0000000..6f0ddcb --- /dev/null +++ b/examples/graphicsview/diagramscene/diagramtextitem.h @@ -0,0 +1,79 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $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 0000000..0f93c6b --- /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 0000000..1e293db --- /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 0000000..3db4f8e --- /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 0000000..9c1f3bf --- /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 0000000..986e65e --- /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 0000000..bda2757 --- /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 0000000..df2a147 --- /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 0000000..54c0dae --- /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 0000000..9a438b5 --- /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 0000000..98a821f --- /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 0000000..66933d4 --- /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 0000000..0b0b0aa --- /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 0000000..5aa3b0a --- /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 0000000..b25832c --- /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 0000000..9b8209f --- /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 0000000..4e03074 --- /dev/null +++ b/examples/graphicsview/diagramscene/main.cpp @@ -0,0 +1,56 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $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 0000000..b536a7a --- /dev/null +++ b/examples/graphicsview/diagramscene/mainwindow.cpp @@ -0,0 +1,651 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui> +#include <QLabel> + +#include "mainwindow.h" +#include "diagramitem.h" +#include "diagramscene.h" +#include "diagramtextitem.h" + +const int InsertTextButton = 10; + +//! [0] +MainWindow::MainWindow() +{ + createActions(); + createToolBox(); + createMenus(); + + scene = new DiagramScene(itemMenu); + 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")); +} +//! [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() == DiagramItem::Type) { + qgraphicsitem_cast<DiagramItem *>(item)->removeArrows(); + } + scene->removeItem(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", + qVariantValue<QColor>(textAction->data()))); + textButtonTriggered(); +} +//! [12] + +//! [13] +void MainWindow::itemColorChanged() +{ + fillAction = qobject_cast<QAction *>(sender()); + fillColorToolButton->setIcon(createColorToolButtonIcon( + ":/images/floodfill.png", + qVariantValue<QColor>(fillAction->data()))); + fillButtonTriggered(); +} +//! [13] + +//! [14] +void MainWindow::lineColorChanged() +{ + lineAction = qobject_cast<QAction *>(sender()); + lineColorToolButton->setIcon(createColorToolButtonIcon( + ":/images/linecolor.png", + qVariantValue<QColor>(lineAction->data()))); + lineButtonTriggered(); +} +//! [14] + +//! [15] +void MainWindow::textButtonTriggered() +{ + scene->setTextColor(qVariantValue<QColor>(textAction->data())); +} +//! [15] + +//! [16] +void MainWindow::fillButtonTriggered() +{ + scene->setItemColor(qVariantValue<QColor>(fillAction->data())); +} +//! [16] + +//! [17] +void MainWindow::lineButtonTriggered() +{ + scene->setLineColor(qVariantValue<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; + 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; + 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->setShortcut(tr("Ctrl+X")); + 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(); + fontSizeCombo = new QComboBox(); + connect(fontCombo, SIGNAL(currentFontChanged(const QFont &)), + this, SLOT(currentFontChanged(const 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(const QString &)), + this, SLOT(fontSizeChanged(const 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; + 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(const QString &)), + this, SLOT(sceneScaleChanged(const 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; + 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 0000000..e4dae4f --- /dev/null +++ b/examples/graphicsview/diagramscene/mainwindow.h @@ -0,0 +1,151 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $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 0000000..2d6b145 --- /dev/null +++ b/examples/graphicsview/dragdroprobot/coloritem.cpp @@ -0,0 +1,129 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui> + +#include "coloritem.h" + +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); +} + +QRectF ColorItem::boundingRect() const +{ + return QRectF(-15.5, -15.5, 34, 34); +} + +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); +} + +void ColorItem::mousePressEvent(QGraphicsSceneMouseEvent *event) +{ + if (event->button() != Qt::LeftButton) { + event->ignore(); + return; + } + + setCursor(Qt::ClosedHandCursor); +} + +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); + + 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)); + } 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)); + } + + drag->exec(); + setCursor(Qt::OpenHandCursor); +} + +void ColorItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *) +{ + setCursor(Qt::OpenHandCursor); +} diff --git a/examples/graphicsview/dragdroprobot/coloritem.h b/examples/graphicsview/dragdroprobot/coloritem.h new file mode 100644 index 0000000..67b2c70 --- /dev/null +++ b/examples/graphicsview/dragdroprobot/coloritem.h @@ -0,0 +1,64 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef COLORITEM_H +#define COLORITEM_H + +#include <QGraphicsItem> + +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; +}; + +#endif diff --git a/examples/graphicsview/dragdroprobot/dragdroprobot.pro b/examples/graphicsview/dragdroprobot/dragdroprobot.pro new file mode 100644 index 0000000..769e54a --- /dev/null +++ b/examples/graphicsview/dragdroprobot/dragdroprobot.pro @@ -0,0 +1,18 @@ +HEADERS += \ + coloritem.h \ + robot.h + +SOURCES += \ + coloritem.cpp \ + main.cpp \ + robot.cpp + +RESOURCES += \ + robot.qrc + + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/graphicsview/dragdroprobot +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS dragdroprobot.pro images +sources.path = $$[QT_INSTALL_EXAMPLES]/graphicsview/dragdroprobot +INSTALLS += target sources diff --git a/examples/graphicsview/dragdroprobot/images/head.png b/examples/graphicsview/dragdroprobot/images/head.png Binary files differnew file mode 100644 index 0000000..1e520e0 --- /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 0000000..204305e --- /dev/null +++ b/examples/graphicsview/dragdroprobot/main.cpp @@ -0,0 +1,78 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui> + +#include "coloritem.h" +#include "robot.h" + +#include <math.h> + +int main(int argc, char **argv) +{ + QApplication app(argc, argv); + + qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); + + 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); + + 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(); +} diff --git a/examples/graphicsview/dragdroprobot/robot.cpp b/examples/graphicsview/dragdroprobot/robot.cpp new file mode 100644 index 0000000..c6d8c44 --- /dev/null +++ b/examples/graphicsview/dragdroprobot/robot.cpp @@ -0,0 +1,273 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui> + +#include "robot.h" + +RobotPart::RobotPart(QGraphicsItem *parent) + : QGraphicsItem(parent), color(Qt::lightGray), dragOver(false) +{ + setAcceptDrops(true); +} + +void RobotPart::dragEnterEvent(QGraphicsSceneDragDropEvent *event) +{ + if (event->mimeData()->hasColor() + || (qgraphicsitem_cast<RobotHead *>(this) && event->mimeData()->hasImage())) { + event->setAccepted(true); + dragOver = true; + update(); + } else { + event->setAccepted(false); + } +} + +void RobotPart::dragLeaveEvent(QGraphicsSceneDragDropEvent *event) +{ + Q_UNUSED(event); + dragOver = false; + update(); +} + +void RobotPart::dropEvent(QGraphicsSceneDragDropEvent *event) +{ + dragOver = false; + if (event->mimeData()->hasColor()) + color = qVariantValue<QColor>(event->mimeData()->colorData()); + else if (event->mimeData()->hasImage()) + pixmap = qVariantValue<QPixmap>(event->mimeData()->imageData()); + update(); +} + +RobotHead::RobotHead(QGraphicsItem *parent) + : RobotPart(parent) +{ +} + +QRectF RobotHead::boundingRect() const +{ + return QRectF(-15, -50, 30, 50); +} + +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); + } +} + +int RobotHead::type() const +{ + return Type; +} + +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); +} + +Robot::Robot() +{ + QGraphicsItem *torsoItem = new RobotTorso(this); + QGraphicsItem *headItem = new RobotHead(torsoItem); + QGraphicsItem *upperLeftArmItem = new RobotLimb(torsoItem); + QGraphicsItem *lowerLeftArmItem = new RobotLimb(upperLeftArmItem); + QGraphicsItem *upperRightArmItem = new RobotLimb(torsoItem); + QGraphicsItem *lowerRightArmItem = new RobotLimb(upperRightArmItem); + QGraphicsItem *upperRightLegItem = new RobotLimb(torsoItem); + QGraphicsItem *lowerRightLegItem = new RobotLimb(upperRightLegItem); + QGraphicsItem *upperLeftLegItem = new RobotLimb(torsoItem); + QGraphicsItem *lowerLeftLegItem = new RobotLimb(upperLeftLegItem); + + 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); + + timeLine = new QTimeLine; + + QGraphicsItemAnimation *headAnimation = new QGraphicsItemAnimation; + headAnimation->setItem(headItem); + headAnimation->setTimeLine(timeLine); + headAnimation->setRotationAt(0, 20); + headAnimation->setRotationAt(1, -20); + headAnimation->setScaleAt(1, 1.1, 1.1); + + QGraphicsItemAnimation *upperLeftArmAnimation = new QGraphicsItemAnimation; + upperLeftArmAnimation->setItem(upperLeftArmItem); + upperLeftArmAnimation->setTimeLine(timeLine); + upperLeftArmAnimation->setRotationAt(0, 190); + upperLeftArmAnimation->setRotationAt(1, 180); + + QGraphicsItemAnimation *lowerLeftArmAnimation = new QGraphicsItemAnimation; + lowerLeftArmAnimation->setItem(lowerLeftArmItem); + lowerLeftArmAnimation->setTimeLine(timeLine); + lowerLeftArmAnimation->setRotationAt(0, 50); + lowerLeftArmAnimation->setRotationAt(1, 10); + + QGraphicsItemAnimation *upperRightArmAnimation = new QGraphicsItemAnimation; + upperRightArmAnimation->setItem(upperRightArmItem); + upperRightArmAnimation->setTimeLine(timeLine); + upperRightArmAnimation->setRotationAt(0, 300); + upperRightArmAnimation->setRotationAt(1, 310); + + QGraphicsItemAnimation *lowerRightArmAnimation = new QGraphicsItemAnimation; + lowerRightArmAnimation->setItem(lowerRightArmItem); + lowerRightArmAnimation->setTimeLine(timeLine); + lowerRightArmAnimation->setRotationAt(0, 0); + lowerRightArmAnimation->setRotationAt(1, -70); + + QGraphicsItemAnimation *upperLeftLegAnimation = new QGraphicsItemAnimation; + upperLeftLegAnimation->setItem(upperLeftLegItem); + upperLeftLegAnimation->setTimeLine(timeLine); + upperLeftLegAnimation->setRotationAt(0, 150); + upperLeftLegAnimation->setRotationAt(1, 80); + + QGraphicsItemAnimation *lowerLeftLegAnimation = new QGraphicsItemAnimation; + lowerLeftLegAnimation->setItem(lowerLeftLegItem); + lowerLeftLegAnimation->setTimeLine(timeLine); + lowerLeftLegAnimation->setRotationAt(0, 70); + lowerLeftLegAnimation->setRotationAt(1, 10); + + QGraphicsItemAnimation *upperRightLegAnimation = new QGraphicsItemAnimation; + upperRightLegAnimation->setItem(upperRightLegItem); + upperRightLegAnimation->setTimeLine(timeLine); + upperRightLegAnimation->setRotationAt(0, 40); + upperRightLegAnimation->setRotationAt(1, 120); + + QGraphicsItemAnimation *lowerRightLegAnimation = new QGraphicsItemAnimation; + lowerRightLegAnimation->setItem(lowerRightLegItem); + lowerRightLegAnimation->setTimeLine(timeLine); + lowerRightLegAnimation->setRotationAt(0, 10); + lowerRightLegAnimation->setRotationAt(1, 50); + + QGraphicsItemAnimation *torsoAnimation = new QGraphicsItemAnimation; + torsoAnimation->setItem(torsoItem); + torsoAnimation->setTimeLine(timeLine); + torsoAnimation->setRotationAt(0, 5); + torsoAnimation->setRotationAt(1, -20); + + timeLine->setUpdateInterval(1000 / 25); + timeLine->setCurveShape(QTimeLine::SineCurve); + timeLine->setLoopCount(0); + timeLine->setDuration(2000); + timeLine->start(); +} + +Robot::~Robot() +{ + delete timeLine; +} + +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); +} diff --git a/examples/graphicsview/dragdroprobot/robot.h b/examples/graphicsview/dragdroprobot/robot.h new file mode 100644 index 0000000..c0b6d14 --- /dev/null +++ b/examples/graphicsview/dragdroprobot/robot.h @@ -0,0 +1,110 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef ROBOT_H +#define ROBOT_H + +#include <QGraphicsItem> + +QT_BEGIN_NAMESPACE +class QGraphicsSceneMouseEvent; +class QTimeLine; +QT_END_NAMESPACE + +class RobotPart : public QGraphicsItem +{ +public: + RobotPart(QGraphicsItem *parent = 0); + +protected: + void dragEnterEvent(QGraphicsSceneDragDropEvent *event); + void dragLeaveEvent(QGraphicsSceneDragDropEvent *event); + void dropEvent(QGraphicsSceneDragDropEvent *event); + + QPixmap pixmap; + QColor color; + bool dragOver; +}; + +class RobotHead : public RobotPart +{ +public: + RobotHead(QGraphicsItem *parent = 0); + + QRectF boundingRect() const; + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); + + enum { Type = UserType + 1 }; + int type() const; +}; + +class RobotTorso : public RobotPart +{ +public: + RobotTorso(QGraphicsItem *parent = 0); + + QRectF boundingRect() const; + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); +}; + +class RobotLimb : public RobotPart +{ +public: + RobotLimb(QGraphicsItem *parent = 0); + + QRectF boundingRect() const; + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); +}; + +class Robot : public RobotPart +{ +public: + Robot(); + ~Robot(); + + QRectF boundingRect() const; + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); + +private: + QTimeLine *timeLine; +}; + +#endif diff --git a/examples/graphicsview/dragdroprobot/robot.qrc b/examples/graphicsview/dragdroprobot/robot.qrc new file mode 100644 index 0000000..b0969d2 --- /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 0000000..4018c25 --- /dev/null +++ b/examples/graphicsview/elasticnodes/edge.cpp @@ -0,0 +1,144 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $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; + +Edge::Edge(Node *sourceNode, Node *destNode) + : arrowSize(10) +{ + setAcceptedMouseButtons(0); + source = sourceNode; + dest = destNode; + source->addEdge(this); + dest->addEdge(this); + adjust(); +} + +Edge::~Edge() +{ +} + +Node *Edge::sourceNode() const +{ + return source; +} + +void Edge::setSourceNode(Node *node) +{ + source = node; + adjust(); +} + +Node *Edge::destNode() const +{ + return dest; +} + +void Edge::setDestNode(Node *node) +{ + dest = node; + adjust(); +} + +void Edge::adjust() +{ + if (!source || !dest) + return; + + QLineF line(mapFromItem(source, 0, 0), mapFromItem(dest, 0, 0)); + qreal length = line.length(); + QPointF edgeOffset((line.dx() * 10) / length, (line.dy() * 10) / length); + + prepareGeometryChange(); + sourcePoint = line.p1() + edgeOffset; + destPoint = line.p2() - edgeOffset; +} + +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); +} + +void Edge::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) +{ + if (!source || !dest) + return; + + // Draw the line itself + QLineF line(sourcePoint, destPoint); + painter->setPen(QPen(Qt::black, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)); + painter->drawLine(line); + + // Draw the arrows if there's enough room + 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); +} diff --git a/examples/graphicsview/elasticnodes/edge.h b/examples/graphicsview/elasticnodes/edge.h new file mode 100644 index 0000000..9078870 --- /dev/null +++ b/examples/graphicsview/elasticnodes/edge.h @@ -0,0 +1,78 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef EDGE_H +#define EDGE_H + +#include <QGraphicsItem> + +class Node; + +class Edge : public QGraphicsItem +{ +public: + Edge(Node *sourceNode, Node *destNode); + ~Edge(); + + Node *sourceNode() const; + void setSourceNode(Node *node); + + Node *destNode() const; + void setDestNode(Node *node); + + 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; +}; + +#endif diff --git a/examples/graphicsview/elasticnodes/elasticnodes.pro b/examples/graphicsview/elasticnodes/elasticnodes.pro new file mode 100644 index 0000000..77ca706 --- /dev/null +++ b/examples/graphicsview/elasticnodes/elasticnodes.pro @@ -0,0 +1,16 @@ +HEADERS += \ + edge.h \ + node.h \ + graphwidget.h + +SOURCES += \ + edge.cpp \ + main.cpp \ + node.cpp \ + graphwidget.cpp + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/graphicsview/elasticnodes +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS elasticnodes.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/graphicsview/elasticnodes +INSTALLS += target sources diff --git a/examples/graphicsview/elasticnodes/graphwidget.cpp b/examples/graphicsview/elasticnodes/graphwidget.cpp new file mode 100644 index 0000000..5c5029c --- /dev/null +++ b/examples/graphicsview/elasticnodes/graphwidget.cpp @@ -0,0 +1,224 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "graphwidget.h" +#include "edge.h" +#include "node.h" + +#include <QDebug> +#include <QGraphicsScene> +#include <QWheelEvent> + +#include <math.h> + +GraphWidget::GraphWidget() + : 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); + setResizeAnchor(AnchorViewCenter); + + 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); + + scale(qreal(0.8), qreal(0.8)); + setMinimumSize(400, 400); + setWindowTitle(tr("Elastic Nodes")); +} + +void GraphWidget::itemMoved() +{ + if (!timerId) + timerId = startTimer(1000 / 25); +} + +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); + } +} + +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; + } +} + +void GraphWidget::wheelEvent(QWheelEvent *event) +{ + scaleView(pow((double)2, -event->delta() / 240.0)); +} + +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); +} + +void GraphWidget::scaleView(qreal scaleFactor) +{ + qreal factor = matrix().scale(scaleFactor, scaleFactor).mapRect(QRectF(0, 0, 1, 1)).width(); + if (factor < 0.07 || factor > 100) + return; + + scale(scaleFactor, scaleFactor); +} diff --git a/examples/graphicsview/elasticnodes/graphwidget.h b/examples/graphicsview/elasticnodes/graphwidget.h new file mode 100644 index 0000000..2c86c76 --- /dev/null +++ b/examples/graphicsview/elasticnodes/graphwidget.h @@ -0,0 +1,71 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef GRAPHWIDGET_H +#define GRAPHWIDGET_H + +#include <QtGui/QGraphicsView> + +class Node; + +class GraphWidget : public QGraphicsView +{ + Q_OBJECT + +public: + GraphWidget(); + + 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; +}; + +#endif diff --git a/examples/graphicsview/elasticnodes/main.cpp b/examples/graphicsview/elasticnodes/main.cpp new file mode 100644 index 0000000..8043d58 --- /dev/null +++ b/examples/graphicsview/elasticnodes/main.cpp @@ -0,0 +1,54 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $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 0000000..6942fa0 --- /dev/null +++ b/examples/graphicsview/elasticnodes/node.cpp @@ -0,0 +1,185 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QGraphicsScene> +#include <QGraphicsSceneMouseEvent> +#include <QPainter> +#include <QStyleOption> + +#include "edge.h" +#include "node.h" +#include "graphwidget.h" + +Node::Node(GraphWidget *graphWidget) + : graph(graphWidget) +{ + setFlag(ItemIsMovable); + setCacheMode(DeviceCoordinateCache); + setZValue(1); +} + +void Node::addEdge(Edge *edge) +{ + edgeList << edge; + edge->adjust(); +} + +QList<Edge *> Node::edges() const +{ + return edgeList; +} + +void Node::calculateForces() +{ + if (!scene() || scene()->mouseGrabberItem() == this) { + newPos = pos(); + return; + } + + // 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; + + QLineF line(mapFromItem(node, 0, 0), QPointF(0, 0)); + qreal dx = line.dx(); + qreal dy = line.dy(); + double l = 2.0 * (dx * dx + dy * dy); + if (l > 0) { + xvel += (dx * 150.0) / l; + yvel += (dy * 150.0) / l; + } + } + + // Now subtract all forces pulling items together + double weight = (edgeList.size() + 1) * 10; + foreach (Edge *edge, edgeList) { + QPointF pos; + if (edge->sourceNode() == this) + pos = mapFromItem(edge->destNode(), 0, 0); + else + pos = mapFromItem(edge->sourceNode(), 0, 0); + xvel += pos.x() / weight; + yvel += pos.y() / weight; + } + + if (qAbs(xvel) < 0.1 && qAbs(yvel) < 0.1) + xvel = yvel = 0; + + 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)); +} + +bool Node::advance() +{ + if (newPos == pos()) + return false; + + setPos(newPos); + return true; +} + +QRectF Node::boundingRect() const +{ + qreal adjust = 2; + return QRectF(-10 - adjust, -10 - adjust, + 23 + adjust, 23 + adjust); +} + +QPainterPath Node::shape() const +{ + QPainterPath path; + path.addEllipse(-10, -10, 20, 20); + return path; +} + +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); +} + +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); +} + +void Node::mousePressEvent(QGraphicsSceneMouseEvent *event) +{ + update(); + QGraphicsItem::mousePressEvent(event); +} + +void Node::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) +{ + update(); + QGraphicsItem::mouseReleaseEvent(event); +} diff --git a/examples/graphicsview/elasticnodes/node.h b/examples/graphicsview/elasticnodes/node.h new file mode 100644 index 0000000..42309c4 --- /dev/null +++ b/examples/graphicsview/elasticnodes/node.h @@ -0,0 +1,84 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $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 + +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; +}; + +#endif diff --git a/examples/graphicsview/graphicsview.pro b/examples/graphicsview/graphicsview.pro new file mode 100644 index 0000000..66eb0b4 --- /dev/null +++ b/examples/graphicsview/graphicsview.pro @@ -0,0 +1,18 @@ +TEMPLATE = \ + subdirs +SUBDIRS = \ + elasticnodes \ + collidingmice \ + diagramscene \ + dragdroprobot \ + padnavigator \ + basicgraphicslayouts + +contains(QT_CONFIG, qt3support):SUBDIRS += portedcanvas portedasteroids +contains(DEFINES, QT_NO_CURSOR): SUBDIRS -= dragdroprobot + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/graphicsview +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS graphicsview.pro README +sources.path = $$[QT_INSTALL_EXAMPLES]/graphicsview +INSTALLS += target sources diff --git a/examples/graphicsview/padnavigator/backside.ui b/examples/graphicsview/padnavigator/backside.ui new file mode 100644 index 0000000..afa488c --- /dev/null +++ b/examples/graphicsview/padnavigator/backside.ui @@ -0,0 +1,208 @@ +<ui version="4.0" > + <class>BackSide</class> + <widget class="QWidget" name="BackSide" > + <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 0000000..4db003f --- /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 0000000..5bf0deb --- /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 0000000..6fb4cc8 --- /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 0000000..b1fedb6 --- /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 0000000..672f8fa --- /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 0000000..229bf73 --- /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 0000000..2eaf41a --- /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 0000000..6a398dd --- /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 0000000..0248c9d --- /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 0000000..dc5ff0c --- /dev/null +++ b/examples/graphicsview/padnavigator/main.cpp @@ -0,0 +1,59 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui> +#ifndef QT_NO_OPENGL +# include <QtOpenGL> +#endif + +#include "panel.h" + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + Q_INIT_RESOURCE(padnavigator); + + Panel panel(3, 3); + panel.setFocus(); + panel.show(); + + return app.exec(); +} diff --git a/examples/graphicsview/padnavigator/padnavigator.pro b/examples/graphicsview/padnavigator/padnavigator.pro new file mode 100644 index 0000000..0d094c6 --- /dev/null +++ b/examples/graphicsview/padnavigator/padnavigator.pro @@ -0,0 +1,24 @@ +HEADERS += \ + panel.h \ + roundrectitem.h \ + splashitem.h + +SOURCES += \ + panel.cpp \ + roundrectitem.cpp \ + splashitem.cpp \ + main.cpp + +RESOURCES += \ + padnavigator.qrc + +FORMS += \ + backside.ui + +contains(QT_CONFIG, opengl):QT += opengl + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/graphicsview/padnavigator +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS padnavigator.pro images +sources.path = $$[QT_INSTALL_EXAMPLES]/graphicsview/padnavigator +INSTALLS += target sources diff --git a/examples/graphicsview/padnavigator/padnavigator.qrc b/examples/graphicsview/padnavigator/padnavigator.qrc new file mode 100644 index 0000000..30ee8e1 --- /dev/null +++ b/examples/graphicsview/padnavigator/padnavigator.qrc @@ -0,0 +1,14 @@ +<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> + <file>images/blue_angle_swirl.jpg</file> + </qresource> +</RCC> diff --git a/examples/graphicsview/padnavigator/panel.cpp b/examples/graphicsview/padnavigator/panel.cpp new file mode 100644 index 0000000..28a3cb4 --- /dev/null +++ b/examples/graphicsview/padnavigator/panel.cpp @@ -0,0 +1,238 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "panel.h" +#include "roundrectitem.h" +#include "splashitem.h" +#include "ui_backside.h" + +#ifndef QT_NO_OPENGL +#include <QtOpenGL/QtOpenGL> +#else +#endif +#include <QtGui/QtGui> + +#include <math.h> + +Panel::Panel(int width, int height) + : selectedX(0), + selectedY(0), + width(width), + height(height), + flipped(false), + flipLeft(true) +{ + setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + setCacheMode(CacheBackground); + setViewportUpdateMode(FullViewportUpdate); + setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform + | QPainter::TextAntialiasing); + setBackgroundBrush(QPixmap(":/images/blue_angle_swirl.jpg")); +#ifndef QT_NO_OPENGL + setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers))); +#endif + setMinimumSize(50, 50); + + selectionTimeLine = new QTimeLine(150, this); + flipTimeLine = new QTimeLine(500, this); + + QRectF bounds((-width / 2.0) * 150, (-height / 2.0) * 150, width * 150, height * 150); + + scene = new QGraphicsScene(bounds, this); + scene->setItemIndexMethod(QGraphicsScene::NoIndex); + setScene(scene); + + baseItem = new RoundRectItem(bounds, QColor(226, 255, 92, 64)); + scene->addItem(baseItem); + + QWidget *embed = new QWidget; + ui = new Ui_BackSide; + ui->setupUi(embed); + ui->hostName->setFocus(); + + backItem = new RoundRectItem(bounds, embed->palette().window(), embed); + backItem->setTransform(QTransform().rotate(180, Qt::YAxis)); + backItem->setParentItem(baseItem); + + selectionItem = new RoundRectItem(QRectF(-60, -60, 120, 120), Qt::gray); + selectionItem->setParentItem(baseItem); + selectionItem->setZValue(-1); + selectionItem->setPos(posForLocation(0, 0)); + startPos = selectionItem->pos(); + + grid = new QGraphicsItem **[height]; + + for (int y = 0; y < height; ++y) { + grid[y] = new QGraphicsItem *[width]; + + for (int x = 0; x < width; ++x) { + RoundRectItem *item = new RoundRectItem(QRectF(-54, -54, 108, 108), + QColor(214, 240, 110, 128)); + item->setPos(posForLocation(x, y)); + + item->setParentItem(baseItem); + item->setFlag(QGraphicsItem::ItemIsFocusable); + grid[y][x] = item; + + switch (qrand() % 9) { + case 0: item->setPixmap(QPixmap(":/images/kontact_contacts.png")); break; + case 1: item->setPixmap(QPixmap(":/images/kontact_journal.png")); break; + case 2: item->setPixmap(QPixmap(":/images/kontact_notes.png")); break; + case 3: item->setPixmap(QPixmap(":/images/kopeteavailable.png")); break; + case 4: item->setPixmap(QPixmap(":/images/metacontact_online.png")); break; + case 5: item->setPixmap(QPixmap(":/images/minitools.png")); break; + case 6: item->setPixmap(QPixmap(":/images/kontact_journal.png")); break; + case 7: item->setPixmap(QPixmap(":/images/kontact_contacts.png")); break; + case 8: item->setPixmap(QPixmap(":/images/kopeteavailable.png")); break; + default: + break; + } + + connect(item, SIGNAL(activated()), this, SLOT(flip())); + } + } + + grid[0][0]->setFocus(); + + connect(backItem, SIGNAL(activated()), + this, SLOT(flip())); + connect(selectionTimeLine, SIGNAL(valueChanged(qreal)), + this, SLOT(updateSelectionStep(qreal))); + connect(flipTimeLine, SIGNAL(valueChanged(qreal)), + this, SLOT(updateFlipStep(qreal))); + + splash = new SplashItem; + splash->setZValue(5); + splash->setPos(-splash->rect().width() / 2, scene->sceneRect().top()); + scene->addItem(splash); + + splash->grabKeyboard(); + + updateSelectionStep(0); + + setWindowTitle(tr("Pad Navigator Example")); +} + +Panel::~Panel() +{ + for (int y = 0; y < height; ++y) + delete [] grid[y]; + delete [] grid; +} + +void Panel::keyPressEvent(QKeyEvent *event) +{ + if (splash->isVisible() || event->key() == Qt::Key_Return || flipped) { + QGraphicsView::keyPressEvent(event); + return; + } + + selectedX = (selectedX + width + (event->key() == Qt::Key_Right) - (event->key() == Qt::Key_Left)) % width; + selectedY = (selectedY + height + (event->key() == Qt::Key_Down) - (event->key() == Qt::Key_Up)) % height; + grid[selectedY][selectedX]->setFocus(); + + selectionTimeLine->stop(); + startPos = selectionItem->pos(); + endPos = posForLocation(selectedX, selectedY); + selectionTimeLine->start(); +} + +void Panel::resizeEvent(QResizeEvent *event) +{ + QGraphicsView::resizeEvent(event); + fitInView(scene->sceneRect(), Qt::KeepAspectRatio); +} + +void Panel::updateSelectionStep(qreal val) +{ + QPointF newPos(startPos.x() + (endPos - startPos).x() * val, + startPos.y() + (endPos - startPos).y() * val); + selectionItem->setPos(newPos); + + QTransform transform; + yrot = newPos.x() / 6.0; + xrot = newPos.y() / 6.0; + transform.rotate(newPos.x() / 6.0, Qt::YAxis); + transform.rotate(newPos.y() / 6.0, Qt::XAxis); + baseItem->setTransform(transform); +} + +void Panel::updateFlipStep(qreal val) +{ + qreal finalxrot = xrot - xrot * val; + qreal finalyrot; + if (flipLeft) + finalyrot = yrot - yrot * val - 180 * val; + else + finalyrot = yrot - yrot * val + 180 * val; + QTransform transform; + transform.rotate(finalyrot, Qt::YAxis); + transform.rotate(finalxrot, Qt::XAxis); + qreal scale = 1 - sin(3.14 * val) * 0.3; + transform.scale(scale, scale); + baseItem->setTransform(transform); + if (val == 0) + grid[selectedY][selectedX]->setFocus(); +} + +void Panel::flip() +{ + if (flipTimeLine->state() == QTimeLine::Running) + return; + + if (flipTimeLine->currentValue() == 0) { + flipTimeLine->setDirection(QTimeLine::Forward); + flipTimeLine->start(); + flipped = true; + flipLeft = selectionItem->pos().x() < 0; + } else { + flipTimeLine->setDirection(QTimeLine::Backward); + flipTimeLine->start(); + flipped = false; + } +} + +QPointF Panel::posForLocation(int x, int y) const +{ + return QPointF(x * 150, y * 150) + - QPointF((width - 1) * 75, (height - 1) * 75); +} diff --git a/examples/graphicsview/padnavigator/panel.h b/examples/graphicsview/padnavigator/panel.h new file mode 100644 index 0000000..03876b7 --- /dev/null +++ b/examples/graphicsview/padnavigator/panel.h @@ -0,0 +1,92 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui/qgraphicsview.h> + +QT_BEGIN_NAMESPACE +class QTimeLine; +class Ui_BackSide; +QT_END_NAMESPACE; + +class RoundRectItem; + +class Panel : public QGraphicsView +{ + Q_OBJECT +public: + Panel(int width, int height); + ~Panel(); + +protected: + void keyPressEvent(QKeyEvent *event); + void resizeEvent(QResizeEvent *event); + +private Q_SLOTS: + void updateSelectionStep(qreal val); + void updateFlipStep(qreal val); + void flip(); + +private: + QPointF posForLocation(int x, int y) const; + + QGraphicsScene *scene; + RoundRectItem *selectionItem; + RoundRectItem *baseItem; + RoundRectItem *backItem; + QGraphicsWidget *splash; + QTimeLine *selectionTimeLine; + QTimeLine *flipTimeLine; + int selectedX, selectedY; + + QGraphicsItem ***grid; + + QPointF startPos; + QPointF endPos; + qreal xrot, yrot; + qreal xrot2, yrot2; + + int width; + int height; + bool flipped; + bool flipLeft; + + Ui_BackSide *ui; +}; diff --git a/examples/graphicsview/padnavigator/roundrectitem.cpp b/examples/graphicsview/padnavigator/roundrectitem.cpp new file mode 100644 index 0000000..c5dc35d --- /dev/null +++ b/examples/graphicsview/padnavigator/roundrectitem.cpp @@ -0,0 +1,164 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "roundrectitem.h" + +#include <QtGui/QtGui> + +RoundRectItem::RoundRectItem(const QRectF &rect, const QBrush &brush, QWidget *embeddedWidget) + : QGraphicsRectItem(rect), + brush(brush), + timeLine(75), + lastVal(0), + opa(1), + proxyWidget(0) +{ + connect(&timeLine, SIGNAL(valueChanged(qreal)), + this, SLOT(updateValue(qreal))); + + if (embeddedWidget) { + proxyWidget = new QGraphicsProxyWidget(this); + proxyWidget->setFocusPolicy(Qt::StrongFocus); + proxyWidget->setWidget(embeddedWidget); + proxyWidget->setGeometry(boundingRect().adjusted(25, 25, -25, -25)); + } +} + +void RoundRectItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) +{ + QTransform x = painter->worldTransform(); + + QLineF unit = x.map(QLineF(0, 0, 1, 1)); + if (unit.p1().x() > unit.p2().x() || unit.p1().y() > unit.p2().y()) { + if (proxyWidget && proxyWidget->isVisible()) { + proxyWidget->hide(); + proxyWidget->setGeometry(rect()); + } + return; + } + + if (proxyWidget && !proxyWidget->isVisible()) { + proxyWidget->show(); + proxyWidget->setFocus(); + } + if (proxyWidget && proxyWidget->pos() != QPoint()) + proxyWidget->setGeometry(boundingRect().adjusted(25, 25, -25, -25)); + + painter->setOpacity(opacity()); + painter->setPen(Qt::NoPen); + painter->setBrush(QColor(0, 0, 0, 64)); + painter->drawRoundRect(rect().translated(2, 2)); + + if (!proxyWidget) { + QLinearGradient gradient(rect().topLeft(), rect().bottomRight()); + const QColor col = brush.color(); + gradient.setColorAt(0, col); + gradient.setColorAt(1, col.dark(int(200 + lastVal * 50))); + painter->setBrush(gradient); + } else { + painter->setBrush(brush); + } + + painter->setPen(QPen(Qt::black, 1)); + painter->drawRoundRect(rect()); + if (!pix.isNull()) { + painter->scale(1.95, 1.95); + painter->drawPixmap(-pix.width() / 2, -pix.height() / 2, pix);; + } +} + +QRectF RoundRectItem::boundingRect() const +{ + qreal penW = 0.5; + qreal shadowW = 2.0; + return rect().adjusted(-penW, -penW, penW + shadowW, penW + shadowW); +} + +void RoundRectItem::setPixmap(const QPixmap &pixmap) +{ + pix = pixmap; + if (scene() && isVisible()) + update(); +} + +qreal RoundRectItem::opacity() const +{ + RoundRectItem *parent = parentItem() ? (RoundRectItem *)parentItem() : 0; + return opa + (parent ? parent->opacity() : 0); +} + +void RoundRectItem::setOpacity(qreal opacity) +{ + opa = opacity; + update(); +} + +void RoundRectItem::keyPressEvent(QKeyEvent *event) +{ + if (event->isAutoRepeat() || event->key() != Qt::Key_Return + || (timeLine.state() == QTimeLine::Running && timeLine.direction() == QTimeLine::Forward)) { + QGraphicsRectItem::keyPressEvent(event); + return; + } + + timeLine.stop(); + timeLine.setDirection(QTimeLine::Forward); + timeLine.start(); + emit activated(); +} + +void RoundRectItem::keyReleaseEvent(QKeyEvent *event) +{ + if (event->key() != Qt::Key_Return) { + QGraphicsRectItem::keyReleaseEvent(event); + return; + } + timeLine.stop(); + timeLine.setDirection(QTimeLine::Backward); + timeLine.start(); +} + +void RoundRectItem::updateValue(qreal value) +{ + lastVal = value; + if (!proxyWidget) + setTransform(QTransform().scale(1 - value / 10.0, 1 - value / 10.0)); +} diff --git a/examples/graphicsview/padnavigator/roundrectitem.h b/examples/graphicsview/padnavigator/roundrectitem.h new file mode 100644 index 0000000..33e33d7 --- /dev/null +++ b/examples/graphicsview/padnavigator/roundrectitem.h @@ -0,0 +1,83 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtCore/qobject.h> +#include <QtCore/qtimeline.h> +#include <QtGui/qgraphicsitem.h> +#include <QtGui/qbrush.h> + +QT_BEGIN_NAMESPACE +class QGraphicsProxyWidget; +QT_END_NAMESPACE; + +class RoundRectItem : public QObject, public QGraphicsRectItem +{ + Q_OBJECT +public: + RoundRectItem(const QRectF &rect, const QBrush &brush, QWidget *embeddedWidget = 0); + + void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *); + QRectF boundingRect() const; + + void setPixmap(const QPixmap &pixmap); + + qreal opacity() const; + void setOpacity(qreal opacity); + +Q_SIGNALS: + void activated(); + +protected: + void keyPressEvent(QKeyEvent *event); + void keyReleaseEvent(QKeyEvent *event); + +private slots: + void updateValue(qreal value); + +private: + QBrush brush; + QPixmap pix; + QTimeLine timeLine; + qreal lastVal; + qreal opa; + + QGraphicsProxyWidget *proxyWidget; +}; diff --git a/examples/graphicsview/padnavigator/splashitem.cpp b/examples/graphicsview/padnavigator/splashitem.cpp new file mode 100644 index 0000000..2a374bf --- /dev/null +++ b/examples/graphicsview/padnavigator/splashitem.cpp @@ -0,0 +1,92 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "splashitem.h" + +#include <QtGui/QtGui> + +SplashItem::SplashItem(QGraphicsItem *parent) + : QGraphicsWidget(parent) +{ + opacity = 1.0; + + + timeLine = new QTimeLine(350); + timeLine->setCurveShape(QTimeLine::EaseInCurve); + connect(timeLine, SIGNAL(valueChanged(qreal)), this, SLOT(setValue(qreal))); + + 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. Please press any key to continue."); + resize(400, 175); +} + +void SplashItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) +{ + painter->setOpacity(opacity); + painter->setPen(QPen(Qt::black, 2)); + painter->setBrush(QColor(245, 245, 255, 220)); + painter->setClipRect(rect()); + painter->drawRoundRect(3, -100 + 3, 400 - 6, 250 - 6); + + QRectF textRect = rect().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); +} + +void SplashItem::keyPressEvent(QKeyEvent * /* event */) +{ + if (timeLine->state() == QTimeLine::NotRunning) + timeLine->start(); +} + +void SplashItem::setValue(qreal value) +{ + opacity = 1 - value; + setPos(x(), scene()->sceneRect().top() - rect().height() * value); + if (value == 1) + hide(); +} diff --git a/examples/graphicsview/padnavigator/splashitem.h b/examples/graphicsview/padnavigator/splashitem.h new file mode 100644 index 0000000..982bbe2 --- /dev/null +++ b/examples/graphicsview/padnavigator/splashitem.h @@ -0,0 +1,63 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtCore/qobject.h> +#include <QtCore/qtimeline.h> +#include <QtGui/qgraphicswidget.h> + +class SplashItem : public QGraphicsWidget +{ + Q_OBJECT +public: + SplashItem(QGraphicsItem *parent = 0); + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); + +protected: + void keyPressEvent(QKeyEvent *event); + +private Q_SLOTS: + void setValue(qreal value); + +private: + QTimeLine *timeLine; + QString text; + qreal opacity; +}; diff --git a/examples/graphicsview/portedasteroids/animateditem.cpp b/examples/graphicsview/portedasteroids/animateditem.cpp new file mode 100644 index 0000000..ac41fb5 --- /dev/null +++ b/examples/graphicsview/portedasteroids/animateditem.cpp @@ -0,0 +1,95 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "animateditem.h" + +#include <QtGui/qbitmap.h> +#include <QtGui/qpainter.h> + +AnimatedPixmapItem::AnimatedPixmapItem(const QList<QPixmap> &animation, + QGraphicsScene *scene) + : QGraphicsItem(0, scene), currentFrame(0), vx(0), vy(0) +{ + for (int i = 0; i < animation.size(); ++i) { + QPixmap pixmap = animation.at(i); + Frame frame; + frame.pixmap = pixmap; + frame.shape = QPainterPath(); + frame.boundingRect = pixmap.rect(); + frames << frame; + } +} + +void AnimatedPixmapItem::setFrame(int frame) +{ + if (!frames.isEmpty()) { + prepareGeometryChange(); + currentFrame = frame % frames.size(); + } +} + +void AnimatedPixmapItem::advance(int phase) +{ + if (phase == 1) + moveBy(vx, vy); +} + +QRectF AnimatedPixmapItem::boundingRect() const +{ + return frames.at(currentFrame).boundingRect; +} + +QPainterPath AnimatedPixmapItem::shape() const +{ + const Frame &f = frames.at(currentFrame); + if (f.shape.isEmpty()) { + QPainterPath path; + path.addRegion(f.pixmap.createHeuristicMask()); + const_cast<Frame &>(f).shape = path; + } + return f.shape; +} + +void AnimatedPixmapItem::paint(QPainter *painter, const QStyleOptionGraphicsItem * /*option*/, + QWidget * /*widget*/) +{ + painter->drawPixmap(0, 0, frames.at(currentFrame).pixmap); +} diff --git a/examples/graphicsview/portedasteroids/animateditem.h b/examples/graphicsview/portedasteroids/animateditem.h new file mode 100644 index 0000000..54e8e56 --- /dev/null +++ b/examples/graphicsview/portedasteroids/animateditem.h @@ -0,0 +1,84 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef ANIMATEDPIXMAPITEM_H +#define ANIMATEDPIXMAPITEM_H + +#include <QGraphicsItem> + +class AnimatedPixmapItem : public QGraphicsItem +{ +public: + AnimatedPixmapItem(const QList<QPixmap> &animation, QGraphicsScene *scene = 0); + + void setFrame(int frame); + inline int frame() const + { return currentFrame; } + inline int frameCount() const + { return frames.size(); } + inline QPixmap image(int frame) const + { return frames.isEmpty() ? QPixmap() : frames.at(frame % frames.size()).pixmap; } + inline void setVelocity(qreal xvel, qreal yvel) + { vx = xvel; vy = yvel; } + inline qreal xVelocity() const + { return vx; } + inline qreal yVelocity() const + { return vy; } + + QRectF boundingRect() const; + QPainterPath shape() const; + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); + + void advance(int phase); + +private: + struct Frame { + QPixmap pixmap; + QPainterPath shape; + QRectF boundingRect; + }; + + int currentFrame; + QList<Frame> frames; + qreal vx, vy; +}; + +#endif diff --git a/examples/graphicsview/portedasteroids/bg.png b/examples/graphicsview/portedasteroids/bg.png Binary files differnew file mode 100644 index 0000000..1201172 --- /dev/null +++ b/examples/graphicsview/portedasteroids/bg.png diff --git a/examples/graphicsview/portedasteroids/ledmeter.cpp b/examples/graphicsview/portedasteroids/ledmeter.cpp new file mode 100644 index 0000000..37aee26 --- /dev/null +++ b/examples/graphicsview/portedasteroids/ledmeter.cpp @@ -0,0 +1,161 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +/* + * KAsteroids - Copyright (c) Martin R. Jones 1997 + * + * Part of the KDE project + */ + +#include <qpainter.h> +//Added by qt3to4: +#include <QResizeEvent> +#include <Q3Frame> +#include "ledmeter.h" + +KALedMeter::KALedMeter( QWidget *parent ) : Q3Frame( parent ) +{ + mCRanges.setAutoDelete( TRUE ); + mRange = 100; + mCount = 20; + mCurrentCount = 0; + mValue = 0; + setMinimumWidth( mCount * 2 + frameWidth() ); +} + +void KALedMeter::setRange( int r ) +{ + mRange = r; + if ( mRange < 1 ) + mRange = 1; + setValue( mValue ); + update(); +} + +void KALedMeter::setCount( int c ) +{ + mCount = c; + if ( mCount < 1 ) + mCount = 1; + setMinimumWidth( mCount * 2 + frameWidth() ); + calcColorRanges(); + setValue( mValue ); + update(); +} + +void KALedMeter::setValue( int v ) +{ + mValue = v; + if ( mValue > mRange ) + mValue = mRange; + else if ( mValue < 0 ) + mValue = 0; + int c = ( mValue + mRange / mCount - 1 ) * mCount / mRange; + if ( c != mCurrentCount ) + { + mCurrentCount = c; + update(); + } +} + +void KALedMeter::addColorRange( int pc, const QColor &c ) +{ + ColorRange *cr = new ColorRange; + cr->mPc = pc; + cr->mColor = c; + mCRanges.append( cr ); + calcColorRanges(); +} + +void KALedMeter::resizeEvent( QResizeEvent *e ) +{ + Q3Frame::resizeEvent( e ); + int w = ( width() - frameWidth() - 2 ) / mCount * mCount; + w += frameWidth() + 2; + setFrameRect( QRect( 0, 0, w, height() ) ); +} + +void KALedMeter::drawContents( QPainter *p ) +{ + QRect b = contentsRect(); + + unsigned cidx = 0; + int ncol = mCount; + QColor col = colorGroup().foreground(); + + if ( !mCRanges.isEmpty() ) + { + col = mCRanges.at( cidx )->mColor; + ncol = mCRanges.at( cidx )->mValue; + } + p->setBrush( col ); + p->setPen( col ); + + int lw = b.width() / mCount; + int lx = b.left() + 1; + for ( int i = 0; i < mCurrentCount; i++, lx += lw ) + { + if ( i > ncol ) + { + if ( ++cidx < mCRanges.count() ) + { + col = mCRanges.at( cidx )->mColor; + ncol = mCRanges.at( cidx )->mValue; + p->setBrush( col ); + p->setPen( col ); + } + } + + p->drawRect( lx, b.top() + 1, lw - 1, b.height() - 2 ); + } +} + +void KALedMeter::calcColorRanges() +{ + int prev = 0; + ColorRange *cr; + for ( cr = mCRanges.first(); cr; cr = mCRanges.next() ) + { + cr->mValue = prev + cr->mPc * mCount / 100; + prev = cr->mValue; + } +} + diff --git a/examples/graphicsview/portedasteroids/ledmeter.h b/examples/graphicsview/portedasteroids/ledmeter.h new file mode 100644 index 0000000..117b113 --- /dev/null +++ b/examples/graphicsview/portedasteroids/ledmeter.h @@ -0,0 +1,96 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +/* + * KAsteroids - Copyright (c) Martin R. Jones 1997 + * + * Part of the KDE project + */ + +#ifndef __LEDMETER_H__ +#define __LEDMETER_H__ + +#include <q3frame.h> +#include <q3ptrlist.h> +//Added by qt3to4: +#include <QResizeEvent> + + +class KALedMeter : public Q3Frame +{ + Q_OBJECT +public: + KALedMeter( QWidget *parent ); + + int range() const { return mRange; } + void setRange( int r ); + + int count() const { return mCount; } + void setCount( int c ); + + int value () const { return mValue; } + + void addColorRange( int pc, const QColor &c ); + +public slots: + void setValue( int v ); + +protected: + virtual void resizeEvent( QResizeEvent * ); + virtual void drawContents( QPainter * ); + void calcColorRanges(); + +protected: + struct ColorRange + { + int mPc; + int mValue; + QColor mColor; + }; + + int mRange; + int mCount; + int mCurrentCount; + int mValue; + Q3PtrList<ColorRange> mCRanges; +}; + +#endif diff --git a/examples/graphicsview/portedasteroids/main.cpp b/examples/graphicsview/portedasteroids/main.cpp new file mode 100644 index 0000000..69b5fd5 --- /dev/null +++ b/examples/graphicsview/portedasteroids/main.cpp @@ -0,0 +1,60 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui> + +#include "toplevel.h" + +int main(int argc, char **argv) +{ + Q_INIT_RESOURCE(portedasteroids); + + QApplication app(argc, argv); + + qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); + + KAstTopLevel topLevel; + topLevel.setWindowTitle("Ported Asteroids Game"); + topLevel.show(); + + app.setQuitOnLastWindowClosed(true); + return app.exec(); +} diff --git a/examples/graphicsview/portedasteroids/portedasteroids.pro b/examples/graphicsview/portedasteroids/portedasteroids.pro new file mode 100644 index 0000000..1452e91 --- /dev/null +++ b/examples/graphicsview/portedasteroids/portedasteroids.pro @@ -0,0 +1,19 @@ +TEMPLATE = app +INCLUDEPATH += . + +# Input +HEADERS += ledmeter.h sprites.h toplevel.h view.h +SOURCES += ledmeter.cpp main.cpp toplevel.cpp view.cpp +#The following line was inserted by qt3to4 +QT += qt3support + +HEADERS += animateditem.h +SOURCES += animateditem.cpp + +RESOURCES += portedasteroids.qrc + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/graphicsview/portedasteroids +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS portedasteroids.pro bg.png sounds sprites +sources.path = $$[QT_INSTALL_EXAMPLES]/graphicsview/portedasteroids +INSTALLS += target sources diff --git a/examples/graphicsview/portedasteroids/portedasteroids.qrc b/examples/graphicsview/portedasteroids/portedasteroids.qrc new file mode 100644 index 0000000..1780828 --- /dev/null +++ b/examples/graphicsview/portedasteroids/portedasteroids.qrc @@ -0,0 +1,163 @@ +<!DOCTYPE RCC><RCC version="1.0"> +<qresource prefix="/trolltech/examples/graphicsview/portedasteroids"> +<file>bg.png</file> +<file>sprites/bits/bits0000.png</file> +<file>sprites/bits/bits0001.png</file> +<file>sprites/bits/bits0002.png</file> +<file>sprites/bits/bits0003.png</file> +<file>sprites/bits/bits0004.png</file> +<file>sprites/bits/bits0005.png</file> +<file>sprites/bits/bits0006.png</file> +<file>sprites/bits/bits0007.png</file> +<file>sprites/bits/bits0008.png</file> +<file>sprites/bits/bits0009.png</file> +<file>sprites/bits/bits0010.png</file> +<file>sprites/bits/bits0011.png</file> +<file>sprites/bits/bits0012.png</file> +<file>sprites/bits/bits0013.png</file> +<file>sprites/bits/bits0014.png</file> +<file>sprites/bits/bits0015.png</file> +<file>sprites/ship/ship0000.png</file> +<file>sprites/ship/ship0001.png</file> +<file>sprites/ship/ship0002.png</file> +<file>sprites/ship/ship0003.png</file> +<file>sprites/ship/ship0004.png</file> +<file>sprites/ship/ship0005.png</file> +<file>sprites/ship/ship0006.png</file> +<file>sprites/ship/ship0007.png</file> +<file>sprites/ship/ship0008.png</file> +<file>sprites/ship/ship0009.png</file> +<file>sprites/ship/ship0010.png</file> +<file>sprites/ship/ship0011.png</file> +<file>sprites/ship/ship0012.png</file> +<file>sprites/ship/ship0013.png</file> +<file>sprites/ship/ship0014.png</file> +<file>sprites/ship/ship0015.png</file> +<file>sprites/ship/ship0016.png</file> +<file>sprites/ship/ship0017.png</file> +<file>sprites/ship/ship0018.png</file> +<file>sprites/ship/ship0019.png</file> +<file>sprites/ship/ship0020.png</file> +<file>sprites/ship/ship0021.png</file> +<file>sprites/ship/ship0022.png</file> +<file>sprites/ship/ship0023.png</file> +<file>sprites/ship/ship0024.png</file> +<file>sprites/ship/ship0025.png</file> +<file>sprites/ship/ship0026.png</file> +<file>sprites/ship/ship0027.png</file> +<file>sprites/ship/ship0028.png</file> +<file>sprites/ship/ship0029.png</file> +<file>sprites/ship/ship0030.png</file> +<file>sprites/ship/ship0031.png</file> +<file>sprites/rock1/rock10016.png</file> +<file>sprites/rock1/rock10017.png</file> +<file>sprites/rock1/rock10018.png</file> +<file>sprites/rock1/rock10019.png</file> +<file>sprites/rock1/rock10020.png</file> +<file>sprites/rock1/rock10021.png</file> +<file>sprites/rock1/rock10022.png</file> +<file>sprites/rock1/rock10023.png</file> +<file>sprites/rock1/rock10024.png</file> +<file>sprites/rock1/rock10025.png</file> +<file>sprites/rock1/rock10026.png</file> +<file>sprites/rock1/rock10027.png</file> +<file>sprites/rock1/rock10028.png</file> +<file>sprites/rock1/rock10029.png</file> +<file>sprites/rock1/rock10030.png</file> +<file>sprites/rock1/rock10031.png</file> +<file>sprites/rock1/rock10000.png</file> +<file>sprites/rock1/rock10001.png</file> +<file>sprites/rock1/rock10002.png</file> +<file>sprites/rock1/rock10003.png</file> +<file>sprites/rock1/rock10004.png</file> +<file>sprites/rock1/rock10005.png</file> +<file>sprites/rock1/rock10006.png</file> +<file>sprites/rock1/rock10007.png</file> +<file>sprites/rock1/rock10008.png</file> +<file>sprites/rock1/rock10009.png</file> +<file>sprites/rock1/rock10010.png</file> +<file>sprites/rock1/rock10011.png</file> +<file>sprites/rock1/rock10012.png</file> +<file>sprites/rock1/rock10013.png</file> +<file>sprites/rock1/rock10014.png</file> +<file>sprites/rock1/rock10015.png</file> +<file>sprites/rock2/rock20000.png</file> +<file>sprites/rock2/rock20001.png</file> +<file>sprites/rock2/rock20002.png</file> +<file>sprites/rock2/rock20003.png</file> +<file>sprites/rock2/rock20004.png</file> +<file>sprites/rock2/rock20005.png</file> +<file>sprites/rock2/rock20006.png</file> +<file>sprites/rock2/rock20007.png</file> +<file>sprites/rock2/rock20008.png</file> +<file>sprites/rock2/rock20009.png</file> +<file>sprites/rock2/rock20010.png</file> +<file>sprites/rock2/rock20011.png</file> +<file>sprites/rock2/rock20012.png</file> +<file>sprites/rock2/rock20013.png</file> +<file>sprites/rock2/rock20014.png</file> +<file>sprites/rock2/rock20015.png</file> +<file>sprites/rock2/rock20016.png</file> +<file>sprites/rock2/rock20017.png</file> +<file>sprites/rock2/rock20018.png</file> +<file>sprites/rock2/rock20019.png</file> +<file>sprites/rock2/rock20020.png</file> +<file>sprites/rock2/rock20021.png</file> +<file>sprites/rock2/rock20022.png</file> +<file>sprites/rock2/rock20023.png</file> +<file>sprites/rock2/rock20024.png</file> +<file>sprites/rock2/rock20025.png</file> +<file>sprites/rock2/rock20026.png</file> +<file>sprites/rock2/rock20027.png</file> +<file>sprites/rock2/rock20028.png</file> +<file>sprites/rock2/rock20029.png</file> +<file>sprites/rock2/rock20030.png</file> +<file>sprites/rock2/rock20031.png</file> +<file>sprites/rock3/rock30000.png</file> +<file>sprites/rock3/rock30001.png</file> +<file>sprites/rock3/rock30002.png</file> +<file>sprites/rock3/rock30003.png</file> +<file>sprites/rock3/rock30004.png</file> +<file>sprites/rock3/rock30005.png</file> +<file>sprites/rock3/rock30006.png</file> +<file>sprites/rock3/rock30007.png</file> +<file>sprites/rock3/rock30008.png</file> +<file>sprites/rock3/rock30009.png</file> +<file>sprites/rock3/rock30010.png</file> +<file>sprites/rock3/rock30011.png</file> +<file>sprites/rock3/rock30012.png</file> +<file>sprites/rock3/rock30013.png</file> +<file>sprites/rock3/rock30014.png</file> +<file>sprites/rock3/rock30015.png</file> +<file>sprites/rock3/rock30016.png</file> +<file>sprites/rock3/rock30017.png</file> +<file>sprites/rock3/rock30018.png</file> +<file>sprites/rock3/rock30019.png</file> +<file>sprites/rock3/rock30020.png</file> +<file>sprites/rock3/rock30021.png</file> +<file>sprites/rock3/rock30022.png</file> +<file>sprites/rock3/rock30023.png</file> +<file>sprites/rock3/rock30024.png</file> +<file>sprites/rock3/rock30025.png</file> +<file>sprites/rock3/rock30026.png</file> +<file>sprites/rock3/rock30027.png</file> +<file>sprites/rock3/rock30028.png</file> +<file>sprites/rock3/rock30029.png</file> +<file>sprites/rock3/rock30030.png</file> +<file>sprites/rock3/rock30031.png</file> +<file>sprites/missile/missile.png</file> +<file>sprites/exhaust/exhaust.png</file> +<file>sprites/powerups/shield.png</file> +<file>sprites/powerups/shoot.png</file> +<file>sprites/powerups/teleport.png</file> +<file>sprites/powerups/brake.png</file> +<file>sprites/powerups/energy.png</file> +<file>sprites/shield/shield0000.png</file> +<file>sprites/shield/shield0001.png</file> +<file>sprites/shield/shield0002.png</file> +<file>sprites/shield/shield0003.png</file> +<file>sprites/shield/shield0004.png</file> +<file>sprites/shield/shield0005.png</file> +<file>sprites/shield/shield0006.png</file> +</qresource> +</RCC> diff --git a/examples/graphicsview/portedasteroids/sounds/Explosion.wav b/examples/graphicsview/portedasteroids/sounds/Explosion.wav Binary files differnew file mode 100644 index 0000000..7b140b1 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sounds/Explosion.wav diff --git a/examples/graphicsview/portedasteroids/sprites.h b/examples/graphicsview/portedasteroids/sprites.h new file mode 100644 index 0000000..1483d68 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites.h @@ -0,0 +1,170 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +/* + * KAsteroids - Copyright (c) Martin R. Jones 1997 + * + * Part of the KDE project + */ + +#ifndef __SPRITES_H__ +#define __SPRITES_H__ + +#include "animateditem.h" + +#define ID_ROCK_LARGE 1024 +#define ID_ROCK_MEDIUM 1025 +#define ID_ROCK_SMALL 1026 + +#define ID_MISSILE 1030 + +#define ID_BIT 1040 +#define ID_EXHAUST 1041 + +#define ID_ENERGY_POWERUP 1310 +#define ID_TELEPORT_POWERUP 1311 +#define ID_BRAKE_POWERUP 1312 +#define ID_SHIELD_POWERUP 1313 +#define ID_SHOOT_POWERUP 1314 + +#define ID_SHIP 1350 +#define ID_SHIELD 1351 + +#define MAX_SHIELD_AGE 350 +#define MAX_POWERUP_AGE 500 +#define MAX_MISSILE_AGE 40 + +class KMissile : public AnimatedPixmapItem +{ +public: + KMissile( const QList<QPixmap> &s, QGraphicsScene *c ) : AnimatedPixmapItem( s, c ) + { myAge = 0; } + + virtual int type() const { return ID_MISSILE; } + + void growOlder() { myAge++; } + bool expired() { return myAge > MAX_MISSILE_AGE; } + +private: + int myAge; +}; + +class KBit : public AnimatedPixmapItem +{ +public: + KBit( const QList<QPixmap> &s, QGraphicsScene *c ) : AnimatedPixmapItem( s, c ) + { death = 7; } + + virtual int type() const { return ID_BIT; } + + void setDeath( int d ) { death = d; } + void growOlder() { death--; } + bool expired() { return death <= 0; } + +private: + int death; +}; + +class KExhaust : public AnimatedPixmapItem +{ +public: + KExhaust( const QList<QPixmap> &s, QGraphicsScene *c ) : AnimatedPixmapItem( s, c ) + { death = 1; } + + virtual int type() const { return ID_EXHAUST; } + + void setDeath( int d ) { death = d; } + void growOlder() { death--; } + bool expired() { return death <= 0; } + +private: + int death; +}; + +class KPowerup : public AnimatedPixmapItem +{ +public: + KPowerup( const QList<QPixmap> &s, QGraphicsScene *c, int t ) : AnimatedPixmapItem( s, c ), + myAge( 0 ), _type(t) { } + + virtual int type() const { return _type; } + + void growOlder() { myAge++; } + bool expired() const { return myAge > MAX_POWERUP_AGE; } + +protected: + int myAge; + int _type; +}; + +class KRock : public AnimatedPixmapItem +{ +public: + KRock (const QList<QPixmap> &s, QGraphicsScene *c, int t, int sk, int st) : AnimatedPixmapItem( s, c ) + { _type = t; skip = cskip = sk; step = st; } + + void nextFrame() + { + if (cskip-- <= 0) { + setFrame( (frame()+step+frameCount())%frameCount() ); + cskip = QABS(skip); + } + } + + virtual int type() const { return _type; } + +private: + int _type; + int skip; + int cskip; + int step; +}; + +class KShield : public AnimatedPixmapItem +{ +public: + KShield( QList<QPixmap> &s, QGraphicsScene *c ) + : AnimatedPixmapItem( s, c ) {} + + virtual int type() const { return ID_SHIELD; } +}; + +#endif diff --git a/examples/graphicsview/portedasteroids/sprites/bits/bits.ini b/examples/graphicsview/portedasteroids/sprites/bits/bits.ini new file mode 100644 index 0000000..cb2976f --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/bits/bits.ini @@ -0,0 +1,9 @@ +Cyclic_Animation=On +Width=12 +Height=12 +Final_frame=16 ;; NR_ROTS +Antialias=On +Output_Alpha=On +Output_to_File=On +Output_File_Type=n +Input_File_Name=bits.pov diff --git a/examples/graphicsview/portedasteroids/sprites/bits/bits.pov b/examples/graphicsview/portedasteroids/sprites/bits/bits.pov new file mode 100644 index 0000000..9be7ccb --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/bits/bits.pov @@ -0,0 +1,31 @@ + +#version 3.0 +global_settings { assumed_gamma 2.0 } + +#include "colors.inc" +#include "textures.inc" +#include "metals.inc" + +camera { + location <15, -15, -100> + look_at <0, 0, 0> +} + +light_source { <50, 50, -50> colour White } +light_source { <0, 0, -50> colour White } + +prism { + linear_sweep + linear_spline + 0, + 0.2, + 5, + <2, 0>, <0, 2>, <-1, 1>, <0, -3>, <2, 0> + texture { T_Silver_2A } + + rotate <360*clock, 50, 30> + scale <20, 20, 20> +} + + + diff --git a/examples/graphicsview/portedasteroids/sprites/bits/bits0000.png b/examples/graphicsview/portedasteroids/sprites/bits/bits0000.png Binary files differnew file mode 100644 index 0000000..5ec9d02 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/bits/bits0000.png diff --git a/examples/graphicsview/portedasteroids/sprites/bits/bits0001.png b/examples/graphicsview/portedasteroids/sprites/bits/bits0001.png Binary files differnew file mode 100644 index 0000000..07b4012 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/bits/bits0001.png diff --git a/examples/graphicsview/portedasteroids/sprites/bits/bits0002.png b/examples/graphicsview/portedasteroids/sprites/bits/bits0002.png Binary files differnew file mode 100644 index 0000000..8333792 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/bits/bits0002.png diff --git a/examples/graphicsview/portedasteroids/sprites/bits/bits0003.png b/examples/graphicsview/portedasteroids/sprites/bits/bits0003.png Binary files differnew file mode 100644 index 0000000..9f1fc02 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/bits/bits0003.png diff --git a/examples/graphicsview/portedasteroids/sprites/bits/bits0004.png b/examples/graphicsview/portedasteroids/sprites/bits/bits0004.png Binary files differnew file mode 100644 index 0000000..eb1cc09 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/bits/bits0004.png diff --git a/examples/graphicsview/portedasteroids/sprites/bits/bits0005.png b/examples/graphicsview/portedasteroids/sprites/bits/bits0005.png Binary files differnew file mode 100644 index 0000000..149be63 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/bits/bits0005.png diff --git a/examples/graphicsview/portedasteroids/sprites/bits/bits0006.png b/examples/graphicsview/portedasteroids/sprites/bits/bits0006.png Binary files differnew file mode 100644 index 0000000..4ac75c8 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/bits/bits0006.png diff --git a/examples/graphicsview/portedasteroids/sprites/bits/bits0007.png b/examples/graphicsview/portedasteroids/sprites/bits/bits0007.png Binary files differnew file mode 100644 index 0000000..907241d --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/bits/bits0007.png diff --git a/examples/graphicsview/portedasteroids/sprites/bits/bits0008.png b/examples/graphicsview/portedasteroids/sprites/bits/bits0008.png Binary files differnew file mode 100644 index 0000000..1533268 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/bits/bits0008.png diff --git a/examples/graphicsview/portedasteroids/sprites/bits/bits0009.png b/examples/graphicsview/portedasteroids/sprites/bits/bits0009.png Binary files differnew file mode 100644 index 0000000..05402ba --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/bits/bits0009.png diff --git a/examples/graphicsview/portedasteroids/sprites/bits/bits0010.png b/examples/graphicsview/portedasteroids/sprites/bits/bits0010.png Binary files differnew file mode 100644 index 0000000..ca4f229 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/bits/bits0010.png diff --git a/examples/graphicsview/portedasteroids/sprites/bits/bits0011.png b/examples/graphicsview/portedasteroids/sprites/bits/bits0011.png Binary files differnew file mode 100644 index 0000000..91913c0 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/bits/bits0011.png diff --git a/examples/graphicsview/portedasteroids/sprites/bits/bits0012.png b/examples/graphicsview/portedasteroids/sprites/bits/bits0012.png Binary files differnew file mode 100644 index 0000000..e55d439 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/bits/bits0012.png diff --git a/examples/graphicsview/portedasteroids/sprites/bits/bits0013.png b/examples/graphicsview/portedasteroids/sprites/bits/bits0013.png Binary files differnew file mode 100644 index 0000000..9c73436 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/bits/bits0013.png diff --git a/examples/graphicsview/portedasteroids/sprites/bits/bits0014.png b/examples/graphicsview/portedasteroids/sprites/bits/bits0014.png Binary files differnew file mode 100644 index 0000000..f0463a2 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/bits/bits0014.png diff --git a/examples/graphicsview/portedasteroids/sprites/bits/bits0015.png b/examples/graphicsview/portedasteroids/sprites/bits/bits0015.png Binary files differnew file mode 100644 index 0000000..bce35aa --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/bits/bits0015.png diff --git a/examples/graphicsview/portedasteroids/sprites/exhaust/exhaust.png b/examples/graphicsview/portedasteroids/sprites/exhaust/exhaust.png Binary files differnew file mode 100644 index 0000000..1d9bc33 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/exhaust/exhaust.png diff --git a/examples/graphicsview/portedasteroids/sprites/missile/missile.png b/examples/graphicsview/portedasteroids/sprites/missile/missile.png Binary files differnew file mode 100644 index 0000000..f1a83b2 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/missile/missile.png diff --git a/examples/graphicsview/portedasteroids/sprites/powerups/brake.png b/examples/graphicsview/portedasteroids/sprites/powerups/brake.png Binary files differnew file mode 100644 index 0000000..5f65a11 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/powerups/brake.png diff --git a/examples/graphicsview/portedasteroids/sprites/powerups/energy.png b/examples/graphicsview/portedasteroids/sprites/powerups/energy.png Binary files differnew file mode 100644 index 0000000..4b40074 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/powerups/energy.png diff --git a/examples/graphicsview/portedasteroids/sprites/powerups/shield.png b/examples/graphicsview/portedasteroids/sprites/powerups/shield.png Binary files differnew file mode 100644 index 0000000..6ac6868 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/powerups/shield.png diff --git a/examples/graphicsview/portedasteroids/sprites/powerups/shoot.png b/examples/graphicsview/portedasteroids/sprites/powerups/shoot.png Binary files differnew file mode 100644 index 0000000..5e5bf08 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/powerups/shoot.png diff --git a/examples/graphicsview/portedasteroids/sprites/powerups/teleport.png b/examples/graphicsview/portedasteroids/sprites/powerups/teleport.png Binary files differnew file mode 100644 index 0000000..009c229 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/powerups/teleport.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock1/rock1.ini b/examples/graphicsview/portedasteroids/sprites/rock1/rock1.ini new file mode 100644 index 0000000..e42fc76 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock1/rock1.ini @@ -0,0 +1,9 @@ +Cyclic_Animation=On +Width=48 +Height=48 +Final_frame=32 ;; NR_ROTS +Antialias=On +Output_Alpha=On +Output_to_File=On +Output_File_Type=n +Input_File_Name=rock1.pov diff --git a/examples/graphicsview/portedasteroids/sprites/rock1/rock1.pov b/examples/graphicsview/portedasteroids/sprites/rock1/rock1.pov new file mode 100644 index 0000000..58298c0 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock1/rock1.pov @@ -0,0 +1,26 @@ +#include "colors.inc" +#include "shapes.inc" +#include "textures.inc" +// #include "stones.inc" + +camera { + location <2,2,-6> + up <0, 1, 0> +// right <4/3, 0, 0> + look_at <0,0,0> +} + +object { light_source { <10, 5, -5> color red 1.1 green 1.1 blue 1.0 } } + +#declare Rock = +mesh { + #include "rock.inc" /* collection of triangle or smooth_triangle data */ +} + +object { + Rock + texture { pigment {White} } + scale 1.9 + rotate <60, 45, 360*clock> +} + diff --git a/examples/graphicsview/portedasteroids/sprites/rock1/rock10000.png b/examples/graphicsview/portedasteroids/sprites/rock1/rock10000.png Binary files differnew file mode 100644 index 0000000..5fe70ef --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock1/rock10000.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock1/rock10001.png b/examples/graphicsview/portedasteroids/sprites/rock1/rock10001.png Binary files differnew file mode 100644 index 0000000..ea66dac --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock1/rock10001.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock1/rock10002.png b/examples/graphicsview/portedasteroids/sprites/rock1/rock10002.png Binary files differnew file mode 100644 index 0000000..cb55ed0 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock1/rock10002.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock1/rock10003.png b/examples/graphicsview/portedasteroids/sprites/rock1/rock10003.png Binary files differnew file mode 100644 index 0000000..f82934c --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock1/rock10003.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock1/rock10004.png b/examples/graphicsview/portedasteroids/sprites/rock1/rock10004.png Binary files differnew file mode 100644 index 0000000..04efc2b --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock1/rock10004.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock1/rock10005.png b/examples/graphicsview/portedasteroids/sprites/rock1/rock10005.png Binary files differnew file mode 100644 index 0000000..b9c0d03 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock1/rock10005.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock1/rock10006.png b/examples/graphicsview/portedasteroids/sprites/rock1/rock10006.png Binary files differnew file mode 100644 index 0000000..7f91267 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock1/rock10006.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock1/rock10007.png b/examples/graphicsview/portedasteroids/sprites/rock1/rock10007.png Binary files differnew file mode 100644 index 0000000..b28f219 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock1/rock10007.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock1/rock10008.png b/examples/graphicsview/portedasteroids/sprites/rock1/rock10008.png Binary files differnew file mode 100644 index 0000000..0153d4a --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock1/rock10008.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock1/rock10009.png b/examples/graphicsview/portedasteroids/sprites/rock1/rock10009.png Binary files differnew file mode 100644 index 0000000..c04d827 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock1/rock10009.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock1/rock10010.png b/examples/graphicsview/portedasteroids/sprites/rock1/rock10010.png Binary files differnew file mode 100644 index 0000000..c7d3c40 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock1/rock10010.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock1/rock10011.png b/examples/graphicsview/portedasteroids/sprites/rock1/rock10011.png Binary files differnew file mode 100644 index 0000000..233dfce --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock1/rock10011.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock1/rock10012.png b/examples/graphicsview/portedasteroids/sprites/rock1/rock10012.png Binary files differnew file mode 100644 index 0000000..ec7b5ba --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock1/rock10012.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock1/rock10013.png b/examples/graphicsview/portedasteroids/sprites/rock1/rock10013.png Binary files differnew file mode 100644 index 0000000..22eac3a --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock1/rock10013.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock1/rock10014.png b/examples/graphicsview/portedasteroids/sprites/rock1/rock10014.png Binary files differnew file mode 100644 index 0000000..ad1503d --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock1/rock10014.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock1/rock10015.png b/examples/graphicsview/portedasteroids/sprites/rock1/rock10015.png Binary files differnew file mode 100644 index 0000000..cec22e2 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock1/rock10015.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock1/rock10016.png b/examples/graphicsview/portedasteroids/sprites/rock1/rock10016.png Binary files differnew file mode 100644 index 0000000..c7a96cb --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock1/rock10016.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock1/rock10017.png b/examples/graphicsview/portedasteroids/sprites/rock1/rock10017.png Binary files differnew file mode 100644 index 0000000..c8310bd --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock1/rock10017.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock1/rock10018.png b/examples/graphicsview/portedasteroids/sprites/rock1/rock10018.png Binary files differnew file mode 100644 index 0000000..b8d436b --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock1/rock10018.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock1/rock10019.png b/examples/graphicsview/portedasteroids/sprites/rock1/rock10019.png Binary files differnew file mode 100644 index 0000000..e5d0799 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock1/rock10019.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock1/rock10020.png b/examples/graphicsview/portedasteroids/sprites/rock1/rock10020.png Binary files differnew file mode 100644 index 0000000..440f8d0 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock1/rock10020.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock1/rock10021.png b/examples/graphicsview/portedasteroids/sprites/rock1/rock10021.png Binary files differnew file mode 100644 index 0000000..de3d54d --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock1/rock10021.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock1/rock10022.png b/examples/graphicsview/portedasteroids/sprites/rock1/rock10022.png Binary files differnew file mode 100644 index 0000000..698056d --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock1/rock10022.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock1/rock10023.png b/examples/graphicsview/portedasteroids/sprites/rock1/rock10023.png Binary files differnew file mode 100644 index 0000000..4e6ba04 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock1/rock10023.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock1/rock10024.png b/examples/graphicsview/portedasteroids/sprites/rock1/rock10024.png Binary files differnew file mode 100644 index 0000000..3b42850 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock1/rock10024.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock1/rock10025.png b/examples/graphicsview/portedasteroids/sprites/rock1/rock10025.png Binary files differnew file mode 100644 index 0000000..6813936 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock1/rock10025.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock1/rock10026.png b/examples/graphicsview/portedasteroids/sprites/rock1/rock10026.png Binary files differnew file mode 100644 index 0000000..4a2b171 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock1/rock10026.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock1/rock10027.png b/examples/graphicsview/portedasteroids/sprites/rock1/rock10027.png Binary files differnew file mode 100644 index 0000000..30448fb --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock1/rock10027.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock1/rock10028.png b/examples/graphicsview/portedasteroids/sprites/rock1/rock10028.png Binary files differnew file mode 100644 index 0000000..43ca1a9 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock1/rock10028.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock1/rock10029.png b/examples/graphicsview/portedasteroids/sprites/rock1/rock10029.png Binary files differnew file mode 100644 index 0000000..9d7e888 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock1/rock10029.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock1/rock10030.png b/examples/graphicsview/portedasteroids/sprites/rock1/rock10030.png Binary files differnew file mode 100644 index 0000000..39b2ad2 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock1/rock10030.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock1/rock10031.png b/examples/graphicsview/portedasteroids/sprites/rock1/rock10031.png Binary files differnew file mode 100644 index 0000000..257ec7b --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock1/rock10031.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock2/rock2.ini b/examples/graphicsview/portedasteroids/sprites/rock2/rock2.ini new file mode 100644 index 0000000..d50e6fa --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock2/rock2.ini @@ -0,0 +1,9 @@ +Cyclic_Animation=On +Width=32 +Height=32 +Final_frame=32 ;; NR_ROTS +Antialias=On +Output_Alpha=On +Output_to_File=On +Output_File_Type=n +Input_File_Name=rock2.pov diff --git a/examples/graphicsview/portedasteroids/sprites/rock2/rock2.pov b/examples/graphicsview/portedasteroids/sprites/rock2/rock2.pov new file mode 100644 index 0000000..2f37a20 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock2/rock2.pov @@ -0,0 +1,26 @@ +#include "colors.inc" +#include "shapes.inc" +#include "textures.inc" +// #include "stones.inc" + +camera { + location <2,2,-6> + up <0, 1, 0> +// right <4/3, 0, 0> + look_at <0,0,0> +} + +object { light_source { <10, 5, -5> color red 1.1 green 1.1 blue 1.0 } } + +#declare Rock = +mesh { + #include "rock.inc" /* collection of triangle or smooth_triangle data */ +} + +object { + Rock + texture { pigment {White} } + scale 1.9 + rotate <60, 30, 360*clock> +} + diff --git a/examples/graphicsview/portedasteroids/sprites/rock2/rock20000.png b/examples/graphicsview/portedasteroids/sprites/rock2/rock20000.png Binary files differnew file mode 100644 index 0000000..5cb52bb --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock2/rock20000.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock2/rock20001.png b/examples/graphicsview/portedasteroids/sprites/rock2/rock20001.png Binary files differnew file mode 100644 index 0000000..d765be1 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock2/rock20001.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock2/rock20002.png b/examples/graphicsview/portedasteroids/sprites/rock2/rock20002.png Binary files differnew file mode 100644 index 0000000..84e2380 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock2/rock20002.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock2/rock20003.png b/examples/graphicsview/portedasteroids/sprites/rock2/rock20003.png Binary files differnew file mode 100644 index 0000000..39772b8 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock2/rock20003.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock2/rock20004.png b/examples/graphicsview/portedasteroids/sprites/rock2/rock20004.png Binary files differnew file mode 100644 index 0000000..b983079 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock2/rock20004.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock2/rock20005.png b/examples/graphicsview/portedasteroids/sprites/rock2/rock20005.png Binary files differnew file mode 100644 index 0000000..70bb4d8 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock2/rock20005.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock2/rock20006.png b/examples/graphicsview/portedasteroids/sprites/rock2/rock20006.png Binary files differnew file mode 100644 index 0000000..cf5c2f4 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock2/rock20006.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock2/rock20007.png b/examples/graphicsview/portedasteroids/sprites/rock2/rock20007.png Binary files differnew file mode 100644 index 0000000..479c21d --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock2/rock20007.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock2/rock20008.png b/examples/graphicsview/portedasteroids/sprites/rock2/rock20008.png Binary files differnew file mode 100644 index 0000000..871abca --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock2/rock20008.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock2/rock20009.png b/examples/graphicsview/portedasteroids/sprites/rock2/rock20009.png Binary files differnew file mode 100644 index 0000000..bc31fbb --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock2/rock20009.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock2/rock20010.png b/examples/graphicsview/portedasteroids/sprites/rock2/rock20010.png Binary files differnew file mode 100644 index 0000000..1da9953 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock2/rock20010.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock2/rock20011.png b/examples/graphicsview/portedasteroids/sprites/rock2/rock20011.png Binary files differnew file mode 100644 index 0000000..d542c6e --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock2/rock20011.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock2/rock20012.png b/examples/graphicsview/portedasteroids/sprites/rock2/rock20012.png Binary files differnew file mode 100644 index 0000000..d104ff9 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock2/rock20012.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock2/rock20013.png b/examples/graphicsview/portedasteroids/sprites/rock2/rock20013.png Binary files differnew file mode 100644 index 0000000..e12943d --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock2/rock20013.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock2/rock20014.png b/examples/graphicsview/portedasteroids/sprites/rock2/rock20014.png Binary files differnew file mode 100644 index 0000000..dc7529c --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock2/rock20014.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock2/rock20015.png b/examples/graphicsview/portedasteroids/sprites/rock2/rock20015.png Binary files differnew file mode 100644 index 0000000..0b49c2c --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock2/rock20015.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock2/rock20016.png b/examples/graphicsview/portedasteroids/sprites/rock2/rock20016.png Binary files differnew file mode 100644 index 0000000..adbad98 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock2/rock20016.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock2/rock20017.png b/examples/graphicsview/portedasteroids/sprites/rock2/rock20017.png Binary files differnew file mode 100644 index 0000000..5a8aeef --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock2/rock20017.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock2/rock20018.png b/examples/graphicsview/portedasteroids/sprites/rock2/rock20018.png Binary files differnew file mode 100644 index 0000000..11c6af4 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock2/rock20018.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock2/rock20019.png b/examples/graphicsview/portedasteroids/sprites/rock2/rock20019.png Binary files differnew file mode 100644 index 0000000..026b72b --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock2/rock20019.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock2/rock20020.png b/examples/graphicsview/portedasteroids/sprites/rock2/rock20020.png Binary files differnew file mode 100644 index 0000000..8bc7e20 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock2/rock20020.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock2/rock20021.png b/examples/graphicsview/portedasteroids/sprites/rock2/rock20021.png Binary files differnew file mode 100644 index 0000000..e0a7626 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock2/rock20021.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock2/rock20022.png b/examples/graphicsview/portedasteroids/sprites/rock2/rock20022.png Binary files differnew file mode 100644 index 0000000..5796e12 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock2/rock20022.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock2/rock20023.png b/examples/graphicsview/portedasteroids/sprites/rock2/rock20023.png Binary files differnew file mode 100644 index 0000000..d2cf0c6 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock2/rock20023.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock2/rock20024.png b/examples/graphicsview/portedasteroids/sprites/rock2/rock20024.png Binary files differnew file mode 100644 index 0000000..071ce21 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock2/rock20024.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock2/rock20025.png b/examples/graphicsview/portedasteroids/sprites/rock2/rock20025.png Binary files differnew file mode 100644 index 0000000..ae8101d --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock2/rock20025.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock2/rock20026.png b/examples/graphicsview/portedasteroids/sprites/rock2/rock20026.png Binary files differnew file mode 100644 index 0000000..f2681d4 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock2/rock20026.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock2/rock20027.png b/examples/graphicsview/portedasteroids/sprites/rock2/rock20027.png Binary files differnew file mode 100644 index 0000000..e83fd41 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock2/rock20027.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock2/rock20028.png b/examples/graphicsview/portedasteroids/sprites/rock2/rock20028.png Binary files differnew file mode 100644 index 0000000..5621b4d --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock2/rock20028.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock2/rock20029.png b/examples/graphicsview/portedasteroids/sprites/rock2/rock20029.png Binary files differnew file mode 100644 index 0000000..aec4a34 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock2/rock20029.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock2/rock20030.png b/examples/graphicsview/portedasteroids/sprites/rock2/rock20030.png Binary files differnew file mode 100644 index 0000000..89a8f5f --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock2/rock20030.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock2/rock20031.png b/examples/graphicsview/portedasteroids/sprites/rock2/rock20031.png Binary files differnew file mode 100644 index 0000000..69f5375 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock2/rock20031.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock3/rock3.ini b/examples/graphicsview/portedasteroids/sprites/rock3/rock3.ini new file mode 100644 index 0000000..26a3cf9 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock3/rock3.ini @@ -0,0 +1,9 @@ +Cyclic_Animation=On +Width=20 +Height=20 +Final_frame=32 ;; NR_ROTS +Antialias=On +Output_Alpha=On +Output_to_File=On +Output_File_Type=n +Input_File_Name=rock3.pov diff --git a/examples/graphicsview/portedasteroids/sprites/rock3/rock3.pov b/examples/graphicsview/portedasteroids/sprites/rock3/rock3.pov new file mode 100644 index 0000000..2f37a20 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock3/rock3.pov @@ -0,0 +1,26 @@ +#include "colors.inc" +#include "shapes.inc" +#include "textures.inc" +// #include "stones.inc" + +camera { + location <2,2,-6> + up <0, 1, 0> +// right <4/3, 0, 0> + look_at <0,0,0> +} + +object { light_source { <10, 5, -5> color red 1.1 green 1.1 blue 1.0 } } + +#declare Rock = +mesh { + #include "rock.inc" /* collection of triangle or smooth_triangle data */ +} + +object { + Rock + texture { pigment {White} } + scale 1.9 + rotate <60, 30, 360*clock> +} + diff --git a/examples/graphicsview/portedasteroids/sprites/rock3/rock30000.png b/examples/graphicsview/portedasteroids/sprites/rock3/rock30000.png Binary files differnew file mode 100644 index 0000000..de1205a --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock3/rock30000.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock3/rock30001.png b/examples/graphicsview/portedasteroids/sprites/rock3/rock30001.png Binary files differnew file mode 100644 index 0000000..12ebc00 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock3/rock30001.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock3/rock30002.png b/examples/graphicsview/portedasteroids/sprites/rock3/rock30002.png Binary files differnew file mode 100644 index 0000000..ebb899e --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock3/rock30002.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock3/rock30003.png b/examples/graphicsview/portedasteroids/sprites/rock3/rock30003.png Binary files differnew file mode 100644 index 0000000..1ff7a06 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock3/rock30003.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock3/rock30004.png b/examples/graphicsview/portedasteroids/sprites/rock3/rock30004.png Binary files differnew file mode 100644 index 0000000..5b505bf --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock3/rock30004.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock3/rock30005.png b/examples/graphicsview/portedasteroids/sprites/rock3/rock30005.png Binary files differnew file mode 100644 index 0000000..f9680dc --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock3/rock30005.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock3/rock30006.png b/examples/graphicsview/portedasteroids/sprites/rock3/rock30006.png Binary files differnew file mode 100644 index 0000000..e29746a --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock3/rock30006.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock3/rock30007.png b/examples/graphicsview/portedasteroids/sprites/rock3/rock30007.png Binary files differnew file mode 100644 index 0000000..f01baa6 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock3/rock30007.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock3/rock30008.png b/examples/graphicsview/portedasteroids/sprites/rock3/rock30008.png Binary files differnew file mode 100644 index 0000000..cd10711 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock3/rock30008.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock3/rock30009.png b/examples/graphicsview/portedasteroids/sprites/rock3/rock30009.png Binary files differnew file mode 100644 index 0000000..4f32955 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock3/rock30009.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock3/rock30010.png b/examples/graphicsview/portedasteroids/sprites/rock3/rock30010.png Binary files differnew file mode 100644 index 0000000..bb61d54 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock3/rock30010.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock3/rock30011.png b/examples/graphicsview/portedasteroids/sprites/rock3/rock30011.png Binary files differnew file mode 100644 index 0000000..8486e5a --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock3/rock30011.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock3/rock30012.png b/examples/graphicsview/portedasteroids/sprites/rock3/rock30012.png Binary files differnew file mode 100644 index 0000000..d9ae419 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock3/rock30012.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock3/rock30013.png b/examples/graphicsview/portedasteroids/sprites/rock3/rock30013.png Binary files differnew file mode 100644 index 0000000..ce69400 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock3/rock30013.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock3/rock30014.png b/examples/graphicsview/portedasteroids/sprites/rock3/rock30014.png Binary files differnew file mode 100644 index 0000000..981e92c --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock3/rock30014.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock3/rock30015.png b/examples/graphicsview/portedasteroids/sprites/rock3/rock30015.png Binary files differnew file mode 100644 index 0000000..b8fb00c --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock3/rock30015.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock3/rock30016.png b/examples/graphicsview/portedasteroids/sprites/rock3/rock30016.png Binary files differnew file mode 100644 index 0000000..72bc42f --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock3/rock30016.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock3/rock30017.png b/examples/graphicsview/portedasteroids/sprites/rock3/rock30017.png Binary files differnew file mode 100644 index 0000000..c89f358 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock3/rock30017.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock3/rock30018.png b/examples/graphicsview/portedasteroids/sprites/rock3/rock30018.png Binary files differnew file mode 100644 index 0000000..e1ba724 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock3/rock30018.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock3/rock30019.png b/examples/graphicsview/portedasteroids/sprites/rock3/rock30019.png Binary files differnew file mode 100644 index 0000000..5f004a7 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock3/rock30019.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock3/rock30020.png b/examples/graphicsview/portedasteroids/sprites/rock3/rock30020.png Binary files differnew file mode 100644 index 0000000..58009bf --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock3/rock30020.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock3/rock30021.png b/examples/graphicsview/portedasteroids/sprites/rock3/rock30021.png Binary files differnew file mode 100644 index 0000000..8d9549c --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock3/rock30021.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock3/rock30022.png b/examples/graphicsview/portedasteroids/sprites/rock3/rock30022.png Binary files differnew file mode 100644 index 0000000..1e8a1c2 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock3/rock30022.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock3/rock30023.png b/examples/graphicsview/portedasteroids/sprites/rock3/rock30023.png Binary files differnew file mode 100644 index 0000000..9b960d6 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock3/rock30023.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock3/rock30024.png b/examples/graphicsview/portedasteroids/sprites/rock3/rock30024.png Binary files differnew file mode 100644 index 0000000..6c15f2b --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock3/rock30024.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock3/rock30025.png b/examples/graphicsview/portedasteroids/sprites/rock3/rock30025.png Binary files differnew file mode 100644 index 0000000..12b05da --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock3/rock30025.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock3/rock30026.png b/examples/graphicsview/portedasteroids/sprites/rock3/rock30026.png Binary files differnew file mode 100644 index 0000000..16190e9 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock3/rock30026.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock3/rock30027.png b/examples/graphicsview/portedasteroids/sprites/rock3/rock30027.png Binary files differnew file mode 100644 index 0000000..a862501 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock3/rock30027.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock3/rock30028.png b/examples/graphicsview/portedasteroids/sprites/rock3/rock30028.png Binary files differnew file mode 100644 index 0000000..e3e0c18 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock3/rock30028.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock3/rock30029.png b/examples/graphicsview/portedasteroids/sprites/rock3/rock30029.png Binary files differnew file mode 100644 index 0000000..ec70b84 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock3/rock30029.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock3/rock30030.png b/examples/graphicsview/portedasteroids/sprites/rock3/rock30030.png Binary files differnew file mode 100644 index 0000000..da48df1 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock3/rock30030.png diff --git a/examples/graphicsview/portedasteroids/sprites/rock3/rock30031.png b/examples/graphicsview/portedasteroids/sprites/rock3/rock30031.png Binary files differnew file mode 100644 index 0000000..c1dc1b9 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/rock3/rock30031.png diff --git a/examples/graphicsview/portedasteroids/sprites/shield/shield0000.png b/examples/graphicsview/portedasteroids/sprites/shield/shield0000.png Binary files differnew file mode 100644 index 0000000..7233254 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/shield/shield0000.png diff --git a/examples/graphicsview/portedasteroids/sprites/shield/shield0001.png b/examples/graphicsview/portedasteroids/sprites/shield/shield0001.png Binary files differnew file mode 100644 index 0000000..2b0bc9d --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/shield/shield0001.png diff --git a/examples/graphicsview/portedasteroids/sprites/shield/shield0002.png b/examples/graphicsview/portedasteroids/sprites/shield/shield0002.png Binary files differnew file mode 100644 index 0000000..cdc0fe3 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/shield/shield0002.png diff --git a/examples/graphicsview/portedasteroids/sprites/shield/shield0003.png b/examples/graphicsview/portedasteroids/sprites/shield/shield0003.png Binary files differnew file mode 100644 index 0000000..979ed5a --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/shield/shield0003.png diff --git a/examples/graphicsview/portedasteroids/sprites/shield/shield0004.png b/examples/graphicsview/portedasteroids/sprites/shield/shield0004.png Binary files differnew file mode 100644 index 0000000..0bb93a2 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/shield/shield0004.png diff --git a/examples/graphicsview/portedasteroids/sprites/shield/shield0005.png b/examples/graphicsview/portedasteroids/sprites/shield/shield0005.png Binary files differnew file mode 100644 index 0000000..32210bd --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/shield/shield0005.png diff --git a/examples/graphicsview/portedasteroids/sprites/shield/shield0006.png b/examples/graphicsview/portedasteroids/sprites/shield/shield0006.png Binary files differnew file mode 100644 index 0000000..76c9920 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/shield/shield0006.png diff --git a/examples/graphicsview/portedasteroids/sprites/ship/ship.ini b/examples/graphicsview/portedasteroids/sprites/ship/ship.ini new file mode 100644 index 0000000..479cc20 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/ship/ship.ini @@ -0,0 +1,9 @@ +Cyclic_Animation=On +Width=42 +Height=42 +Final_frame=32 ;; NR_ROTS +Antialias=On +Output_Alpha=On +Output_to_File=On +Output_File_Type=n +Input_File_Name=ship.pov diff --git a/examples/graphicsview/portedasteroids/sprites/ship/ship.pov b/examples/graphicsview/portedasteroids/sprites/ship/ship.pov new file mode 100644 index 0000000..8f185cd --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/ship/ship.pov @@ -0,0 +1,128 @@ + +#version 3.0 +global_settings { assumed_gamma 2.0 } + +#include "colors.inc" +#include "textures.inc" +#include "metals.inc" + +camera { + orthographic + up <0, 130, 0> + right <130, 0, 0> + location <0, 0, -130> + look_at <0, 0, 0> +} + +light_source { <50, 25, -25> colour White } +light_source { <0, 0, -100> colour Gray80 } + +#declare ShipColor = color red 1.0 green 1.0 blue 0.9 + +#declare BaseTexture = +texture { + pigment { ShipColor } +} + +#declare Grubby = +texture { + pigment { + bozo + color_map { + [0.0 color rgbt <1, 1, 1, 1>] + [0.8 color rgbt <0.9, 0.9, 0.9, 0.5>] + [1.0 color rgbt <0.8, 0.8, 0.8, 0.5>] + } + turbulence 2.0 + scale 3 + } +} + +#declare ShipTexture = +texture { BaseTexture } +texture { Grubby } + +union { + cone { + <12, 0, 0>, 0.5 + <11, 0, 0>, 1.0 + texture { ShipTexture } + } + cone { + <11, 0, 0>, 1.0 + <8, 0, 0>, 2.0 + texture { ShipTexture } + } + cone { + <8, 0, 0>, 2.0 + <3.5, 0, 0>, 3.8 + texture { ShipTexture } + } + difference { + cone { + <8, 0, -0.01>, 2.0 + <3.5, 0, -0.01>, 3.8 + pigment { color Gray20 } + } + box { + <9, -4.0, -3.7>, + <2, 4.0, 10> + rotate <0, -18, 0> + } + box { + <6.5, -4.0, -8>, + <10, 4.0, 8> + } + box { + <2, -4.0, -8>, + <4.5, 4.0, 8> + } + } + cone { + <3.5, 0, 0>, 3.8 + <2, 0, 0>, 4.0 + texture { ShipTexture } + } + cylinder { + <2, 0, 0>, + <-9, 0, 0>, + 4.0 + texture { ShipTexture } + } + cone { + <-9, 0, 0>, 4.0 + <-10, 0, 0>, 3.5 + texture { ShipTexture } + } + prism { + linear_sweep + linear_spline + 0, + 0.5, + 4, + <7.5, 0>, <-7.5, 10>, <-7.5, -10>, <7.5, 0> + rotate <90, 0, 0> + texture { T_Silver_2A } + texture { ShipTexture } + } + prism { + linear_sweep + linear_spline + -0.5, + 0.5, + 4, + <4, 0>, <-7.5, 5>, <-7.5, -5>, <4, 0> + pigment { color White } + } + cone { + <-12, 0, 0>, 3.0 + <-10, 0, 0>, 2.0 + texture { T_Silver_2A } + pigment { color Gray60 } + } + + rotate <0, 0,-360*clock> + scale <5, 5, 5> +} + + diff --git a/examples/graphicsview/portedasteroids/sprites/ship/ship0000.png b/examples/graphicsview/portedasteroids/sprites/ship/ship0000.png Binary files differnew file mode 100644 index 0000000..a8f1bb3 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/ship/ship0000.png diff --git a/examples/graphicsview/portedasteroids/sprites/ship/ship0001.png b/examples/graphicsview/portedasteroids/sprites/ship/ship0001.png Binary files differnew file mode 100644 index 0000000..861f130 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/ship/ship0001.png diff --git a/examples/graphicsview/portedasteroids/sprites/ship/ship0002.png b/examples/graphicsview/portedasteroids/sprites/ship/ship0002.png Binary files differnew file mode 100644 index 0000000..418ced1 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/ship/ship0002.png diff --git a/examples/graphicsview/portedasteroids/sprites/ship/ship0003.png b/examples/graphicsview/portedasteroids/sprites/ship/ship0003.png Binary files differnew file mode 100644 index 0000000..50a386b --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/ship/ship0003.png diff --git a/examples/graphicsview/portedasteroids/sprites/ship/ship0004.png b/examples/graphicsview/portedasteroids/sprites/ship/ship0004.png Binary files differnew file mode 100644 index 0000000..f2bc0bc --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/ship/ship0004.png diff --git a/examples/graphicsview/portedasteroids/sprites/ship/ship0005.png b/examples/graphicsview/portedasteroids/sprites/ship/ship0005.png Binary files differnew file mode 100644 index 0000000..7280485 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/ship/ship0005.png diff --git a/examples/graphicsview/portedasteroids/sprites/ship/ship0006.png b/examples/graphicsview/portedasteroids/sprites/ship/ship0006.png Binary files differnew file mode 100644 index 0000000..51d26e9 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/ship/ship0006.png diff --git a/examples/graphicsview/portedasteroids/sprites/ship/ship0007.png b/examples/graphicsview/portedasteroids/sprites/ship/ship0007.png Binary files differnew file mode 100644 index 0000000..682c042 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/ship/ship0007.png diff --git a/examples/graphicsview/portedasteroids/sprites/ship/ship0008.png b/examples/graphicsview/portedasteroids/sprites/ship/ship0008.png Binary files differnew file mode 100644 index 0000000..003d7fa --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/ship/ship0008.png diff --git a/examples/graphicsview/portedasteroids/sprites/ship/ship0009.png b/examples/graphicsview/portedasteroids/sprites/ship/ship0009.png Binary files differnew file mode 100644 index 0000000..538b879 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/ship/ship0009.png diff --git a/examples/graphicsview/portedasteroids/sprites/ship/ship0010.png b/examples/graphicsview/portedasteroids/sprites/ship/ship0010.png Binary files differnew file mode 100644 index 0000000..17999cd --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/ship/ship0010.png diff --git a/examples/graphicsview/portedasteroids/sprites/ship/ship0011.png b/examples/graphicsview/portedasteroids/sprites/ship/ship0011.png Binary files differnew file mode 100644 index 0000000..1a4bbe9 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/ship/ship0011.png diff --git a/examples/graphicsview/portedasteroids/sprites/ship/ship0012.png b/examples/graphicsview/portedasteroids/sprites/ship/ship0012.png Binary files differnew file mode 100644 index 0000000..2523d85 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/ship/ship0012.png diff --git a/examples/graphicsview/portedasteroids/sprites/ship/ship0013.png b/examples/graphicsview/portedasteroids/sprites/ship/ship0013.png Binary files differnew file mode 100644 index 0000000..8bd5971 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/ship/ship0013.png diff --git a/examples/graphicsview/portedasteroids/sprites/ship/ship0014.png b/examples/graphicsview/portedasteroids/sprites/ship/ship0014.png Binary files differnew file mode 100644 index 0000000..a164ab7 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/ship/ship0014.png diff --git a/examples/graphicsview/portedasteroids/sprites/ship/ship0015.png b/examples/graphicsview/portedasteroids/sprites/ship/ship0015.png Binary files differnew file mode 100644 index 0000000..ca8e0b9 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/ship/ship0015.png diff --git a/examples/graphicsview/portedasteroids/sprites/ship/ship0016.png b/examples/graphicsview/portedasteroids/sprites/ship/ship0016.png Binary files differnew file mode 100644 index 0000000..cfaf8c4 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/ship/ship0016.png diff --git a/examples/graphicsview/portedasteroids/sprites/ship/ship0017.png b/examples/graphicsview/portedasteroids/sprites/ship/ship0017.png Binary files differnew file mode 100644 index 0000000..8049617 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/ship/ship0017.png diff --git a/examples/graphicsview/portedasteroids/sprites/ship/ship0018.png b/examples/graphicsview/portedasteroids/sprites/ship/ship0018.png Binary files differnew file mode 100644 index 0000000..4a3421b --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/ship/ship0018.png diff --git a/examples/graphicsview/portedasteroids/sprites/ship/ship0019.png b/examples/graphicsview/portedasteroids/sprites/ship/ship0019.png Binary files differnew file mode 100644 index 0000000..786d375 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/ship/ship0019.png diff --git a/examples/graphicsview/portedasteroids/sprites/ship/ship0020.png b/examples/graphicsview/portedasteroids/sprites/ship/ship0020.png Binary files differnew file mode 100644 index 0000000..bd4365c --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/ship/ship0020.png diff --git a/examples/graphicsview/portedasteroids/sprites/ship/ship0021.png b/examples/graphicsview/portedasteroids/sprites/ship/ship0021.png Binary files differnew file mode 100644 index 0000000..eb358c1 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/ship/ship0021.png diff --git a/examples/graphicsview/portedasteroids/sprites/ship/ship0022.png b/examples/graphicsview/portedasteroids/sprites/ship/ship0022.png Binary files differnew file mode 100644 index 0000000..eb865e1 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/ship/ship0022.png diff --git a/examples/graphicsview/portedasteroids/sprites/ship/ship0023.png b/examples/graphicsview/portedasteroids/sprites/ship/ship0023.png Binary files differnew file mode 100644 index 0000000..3738ce2 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/ship/ship0023.png diff --git a/examples/graphicsview/portedasteroids/sprites/ship/ship0024.png b/examples/graphicsview/portedasteroids/sprites/ship/ship0024.png Binary files differnew file mode 100644 index 0000000..a7c1adc --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/ship/ship0024.png diff --git a/examples/graphicsview/portedasteroids/sprites/ship/ship0025.png b/examples/graphicsview/portedasteroids/sprites/ship/ship0025.png Binary files differnew file mode 100644 index 0000000..2ca3aae --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/ship/ship0025.png diff --git a/examples/graphicsview/portedasteroids/sprites/ship/ship0026.png b/examples/graphicsview/portedasteroids/sprites/ship/ship0026.png Binary files differnew file mode 100644 index 0000000..4661ca5 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/ship/ship0026.png diff --git a/examples/graphicsview/portedasteroids/sprites/ship/ship0027.png b/examples/graphicsview/portedasteroids/sprites/ship/ship0027.png Binary files differnew file mode 100644 index 0000000..f5c9203 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/ship/ship0027.png diff --git a/examples/graphicsview/portedasteroids/sprites/ship/ship0028.png b/examples/graphicsview/portedasteroids/sprites/ship/ship0028.png Binary files differnew file mode 100644 index 0000000..08353d4 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/ship/ship0028.png diff --git a/examples/graphicsview/portedasteroids/sprites/ship/ship0029.png b/examples/graphicsview/portedasteroids/sprites/ship/ship0029.png Binary files differnew file mode 100644 index 0000000..8685d31 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/ship/ship0029.png diff --git a/examples/graphicsview/portedasteroids/sprites/ship/ship0030.png b/examples/graphicsview/portedasteroids/sprites/ship/ship0030.png Binary files differnew file mode 100644 index 0000000..78dd469 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/ship/ship0030.png diff --git a/examples/graphicsview/portedasteroids/sprites/ship/ship0031.png b/examples/graphicsview/portedasteroids/sprites/ship/ship0031.png Binary files differnew file mode 100644 index 0000000..6cd0643 --- /dev/null +++ b/examples/graphicsview/portedasteroids/sprites/ship/ship0031.png diff --git a/examples/graphicsview/portedasteroids/toplevel.cpp b/examples/graphicsview/portedasteroids/toplevel.cpp new file mode 100644 index 0000000..1aecc84 --- /dev/null +++ b/examples/graphicsview/portedasteroids/toplevel.cpp @@ -0,0 +1,543 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +/* + * KAsteroids - Copyright (c) Martin R. Jones 1997 + * + * Part of the KDE project + */ +// --- toplevel.cpp --- +#include <q3accel.h> +#include <qlabel.h> +#include <qlayout.h> +#include <qlcdnumber.h> +#include <qpushbutton.h> + +#include <qapplication.h> +//Added by qt3to4: +#include <Q3HBoxLayout> +#include <QShowEvent> +#include <Q3Frame> +#include <QPixmap> +#include <QHideEvent> +#include <QKeyEvent> +#include <Q3VBoxLayout> + +#include "toplevel.h" +#include "ledmeter.h" + + +#define SB_SCORE 1 +#define SB_LEVEL 2 +#define SB_SHIPS 3 + +struct SLevel +{ + int nrocks; + double rockSpeed; +}; + +#define MAX_LEVELS 16 + +SLevel levels[MAX_LEVELS] = +{ + { 1, 0.4 }, + { 1, 0.6 }, + { 2, 0.5 }, + { 2, 0.7 }, + { 2, 0.8 }, + { 3, 0.6 }, + { 3, 0.7 }, + { 3, 0.8 }, + { 4, 0.6 }, + { 4, 0.7 }, + { 4, 0.8 }, + { 5, 0.7 }, + { 5, 0.8 }, + { 5, 0.9 }, + { 5, 1.0 } +}; + +const char *soundEvents[] = +{ + "ShipDestroyed", + "RockDestroyed", + 0 +}; + +const char *soundDefaults[] = +{ + "Explosion.wav", + "ploop.wav", + 0 +}; + + +KAstTopLevel::KAstTopLevel( QWidget *parent, const char *name ) + : Q3MainWindow( parent, name, 0 ) +{ + QWidget *border = new QWidget( this ); + border->setBackgroundColor( Qt::black ); + setCentralWidget( border ); + + Q3VBoxLayout *borderLayout = new Q3VBoxLayout( border ); + borderLayout->addStretch( 1 ); + + QWidget *mainWin = new QWidget( border ); + mainWin->setFixedSize(640, 480); + borderLayout->addWidget( mainWin, 0, Qt::AlignHCenter ); + + borderLayout->addStretch( 1 ); + + view = new KAsteroidsView( mainWin ); + view->setFocusPolicy( Qt::StrongFocus ); + connect( view, SIGNAL( shipKilled() ), SLOT( slotShipKilled() ) ); + connect( view, SIGNAL( rockHit(int) ), SLOT( slotRockHit(int) ) ); + connect( view, SIGNAL( rocksRemoved() ), SLOT( slotRocksRemoved() ) ); + connect( view, SIGNAL( updateVitals() ), SLOT( slotUpdateVitals() ) ); + + Q3VBoxLayout *vb = new Q3VBoxLayout( mainWin ); + Q3HBoxLayout *hb = new Q3HBoxLayout; + Q3HBoxLayout *hbd = new Q3HBoxLayout; + vb->addLayout( hb ); + + QFont labelFont( "helvetica", 24 ); + QColorGroup grp( Qt::darkGreen, Qt::black, QColor( 128, 128, 128 ), + QColor( 64, 64, 64 ), Qt::black, Qt::darkGreen, Qt::black ); + QPalette pal( grp, grp, grp ); + + mainWin->setPalette( pal ); + + hb->addSpacing( 10 ); + + QLabel *label; + label = new QLabel( tr("Score"), mainWin ); + label->setFont( labelFont ); + label->setPalette( pal ); + label->setFixedWidth( label->sizeHint().width() ); + hb->addWidget( label ); + + scoreLCD = new QLCDNumber( 6, mainWin ); + scoreLCD->setFrameStyle( Q3Frame::NoFrame ); + scoreLCD->setSegmentStyle( QLCDNumber::Flat ); + scoreLCD->setFixedWidth( 150 ); + scoreLCD->setPalette( pal ); + hb->addWidget( scoreLCD ); + hb->addStretch( 10 ); + + label = new QLabel( tr("Level"), mainWin ); + label->setFont( labelFont ); + label->setPalette( pal ); + label->setFixedWidth( label->sizeHint().width() ); + hb->addWidget( label ); + + levelLCD = new QLCDNumber( 2, mainWin ); + levelLCD->setFrameStyle( Q3Frame::NoFrame ); + levelLCD->setSegmentStyle( QLCDNumber::Flat ); + levelLCD->setFixedWidth( 70 ); + levelLCD->setPalette( pal ); + hb->addWidget( levelLCD ); + hb->addStretch( 10 ); + + label = new QLabel( tr("Ships"), mainWin ); + label->setFont( labelFont ); + label->setFixedWidth( label->sizeHint().width() ); + label->setPalette( pal ); + hb->addWidget( label ); + + shipsLCD = new QLCDNumber( 1, mainWin ); + shipsLCD->setFrameStyle( Q3Frame::NoFrame ); + shipsLCD->setSegmentStyle( QLCDNumber::Flat ); + shipsLCD->setFixedWidth( 40 ); + shipsLCD->setPalette( pal ); + hb->addWidget( shipsLCD ); + + hb->addStrut( 30 ); + + vb->addWidget( view, 10 ); + +// -- bottom layout: + vb->addLayout( hbd ); + + QFont smallFont( "helvetica", 14 ); + hbd->addSpacing( 10 ); + + QString sprites_prefix = ":/trolltech/examples/graphicsview/portedasteroids/sprites/"; +/* + label = new QLabel( tr( "T" ), mainWin ); + label->setFont( smallFont ); + label->setFixedWidth( label->sizeHint().width() ); + label->setPalette( pal ); + hbd->addWidget( label ); + + teleportsLCD = new QLCDNumber( 1, mainWin ); + teleportsLCD->setFrameStyle( QFrame::NoFrame ); + teleportsLCD->setSegmentStyle( QLCDNumber::Flat ); + teleportsLCD->setPalette( pal ); + teleportsLCD->setFixedHeight( 20 ); + hbd->addWidget( teleportsLCD ); + + hbd->addSpacing( 10 ); +*/ + QPixmap pm( sprites_prefix + "powerups/brake.png" ); + label = new QLabel( mainWin ); + label->setPixmap( pm ); + label->setFixedWidth( label->sizeHint().width() ); + label->setPalette( pal ); + hbd->addWidget( label ); + + brakesLCD = new QLCDNumber( 1, mainWin ); + brakesLCD->setFrameStyle( Q3Frame::NoFrame ); + brakesLCD->setSegmentStyle( QLCDNumber::Flat ); + brakesLCD->setPalette( pal ); + brakesLCD->setFixedHeight( 20 ); + hbd->addWidget( brakesLCD ); + + hbd->addSpacing( 10 ); + + pm.load( sprites_prefix + "powerups/shield.png" ); + label = new QLabel( mainWin ); + label->setPixmap( pm ); + label->setFixedWidth( label->sizeHint().width() ); + label->setPalette( pal ); + hbd->addWidget( label ); + + shieldLCD = new QLCDNumber( 1, mainWin ); + shieldLCD->setFrameStyle( Q3Frame::NoFrame ); + shieldLCD->setSegmentStyle( QLCDNumber::Flat ); + shieldLCD->setPalette( pal ); + shieldLCD->setFixedHeight( 20 ); + hbd->addWidget( shieldLCD ); + + hbd->addSpacing( 10 ); + + pm.load( sprites_prefix + "powerups/shoot.png" ); + label = new QLabel( mainWin ); + label->setPixmap( pm ); + label->setFixedWidth( label->sizeHint().width() ); + label->setPalette( pal ); + hbd->addWidget( label ); + + shootLCD = new QLCDNumber( 1, mainWin ); + shootLCD->setFrameStyle( Q3Frame::NoFrame ); + shootLCD->setSegmentStyle( QLCDNumber::Flat ); + shootLCD->setPalette( pal ); + shootLCD->setFixedHeight( 20 ); + hbd->addWidget( shootLCD ); + + hbd->addStretch( 1 ); + + label = new QLabel( tr( "Fuel" ), mainWin ); + label->setFont( smallFont ); + label->setFixedWidth( label->sizeHint().width() + 10 ); + label->setPalette( pal ); + hbd->addWidget( label ); + + powerMeter = new KALedMeter( mainWin ); + powerMeter->setFrameStyle( Q3Frame::Box | Q3Frame::Plain ); + powerMeter->setRange( MAX_POWER_LEVEL ); + powerMeter->addColorRange( 10, Qt::darkRed ); + powerMeter->addColorRange( 20, QColor(160, 96, 0) ); + powerMeter->addColorRange( 70, Qt::darkGreen ); + powerMeter->setCount( 40 ); + powerMeter->setPalette( pal ); + powerMeter->setFixedSize( 200, 12 ); + hbd->addWidget( powerMeter ); + + shipsRemain = 3; + showHiscores = FALSE; + + actions.insert( Qt::Key_Up, Thrust ); + actions.insert( Qt::Key_Left, RotateLeft ); + actions.insert( Qt::Key_Right, RotateRight ); + actions.insert( Qt::Key_Space, Shoot ); + actions.insert( Qt::Key_Z, Teleport ); + actions.insert( Qt::Key_X, Brake ); + actions.insert( Qt::Key_S, Shield ); + actions.insert( Qt::Key_P, Pause ); + actions.insert( Qt::Key_L, Launch ); + actions.insert( Qt::Key_N, NewGame ); + + view->showText( tr( "Press N to start playing" ), Qt::yellow ); +} + +KAstTopLevel::~KAstTopLevel() +{ +} + +void KAstTopLevel::playSound( const char * ) +{ +} + +void KAstTopLevel::keyPressEvent( QKeyEvent *event ) +{ + if ( event->isAutoRepeat() || !actions.contains( event->key() ) ) + { + event->ignore(); + return; + } + + Action a = actions[ event->key() ]; + + switch ( a ) + { + case RotateLeft: + view->rotateLeft( TRUE ); + break; + + case RotateRight: + view->rotateRight( TRUE ); + break; + + case Thrust: + view->thrust( TRUE ); + break; + + case Shoot: + view->shoot( TRUE ); + break; + + case Shield: + view->setShield( TRUE ); + break; + + case Teleport: + view->teleport( TRUE ); + break; + + case Brake: + view->brake( TRUE ); + break; + + default: + event->ignore(); + return; + } + event->accept(); +} + +void KAstTopLevel::keyReleaseEvent( QKeyEvent *event ) +{ + if ( event->isAutoRepeat() || !actions.contains( event->key() ) ) + { + event->ignore(); + return; + } + + Action a = actions[ event->key() ]; + + switch ( a ) + { + case RotateLeft: + view->rotateLeft( FALSE ); + break; + + case RotateRight: + view->rotateRight( FALSE ); + break; + + case Thrust: + view->thrust( FALSE ); + break; + + case Shoot: + view->shoot( FALSE ); + break; + + case Brake: + view->brake( FALSE ); + break; + + case Shield: + view->setShield( FALSE ); + break; + + case Teleport: + view->teleport( FALSE ); + break; + + case Launch: + if ( waitShip ) + { + view->newShip(); + waitShip = FALSE; + view->hideText(); + } + else + { + event->ignore(); + return; + } + break; + + case NewGame: + slotNewGame(); + break; +/* + case Pause: + { + view->pause( TRUE ); + QMessageBox::information( this, + tr("KAsteroids is paused"), + tr("Paused") ); + view->pause( FALSE ); + } + break; +*/ + default: + event->ignore(); + return; + } + + event->accept(); +} + +void KAstTopLevel::showEvent( QShowEvent *e ) +{ + Q3MainWindow::showEvent( e ); + view->pause( FALSE ); + view->setFocus(); +} + +void KAstTopLevel::hideEvent( QHideEvent *e ) +{ + Q3MainWindow::hideEvent( e ); + view->pause( TRUE ); +} + +void KAstTopLevel::slotNewGame() +{ + score = 0; + shipsRemain = SB_SHIPS; + scoreLCD->display( 0 ); + level = 0; + levelLCD->display( level+1 ); + shipsLCD->display( shipsRemain-1 ); + view->newGame(); + view->setRockSpeed( levels[0].rockSpeed ); + view->addRocks( levels[0].nrocks ); +// view->showText( tr( "Press L to launch." ), yellow ); + view->newShip(); + waitShip = FALSE; + view->hideText(); + isPaused = FALSE; +} + +void KAstTopLevel::slotShipKilled() +{ + shipsRemain--; + shipsLCD->display( shipsRemain-1 ); + + playSound( "ShipDestroyed" ); + + if ( shipsRemain ) + { + waitShip = TRUE; + view->showText( tr( "Ship Destroyed. Press L to launch."), Qt::yellow ); + } + else + { + view->showText( tr("Game Over!"), Qt::red ); + view->endGame(); + doStats(); +// highscore->addEntry( score, level, showHiscores ); + } +} + +void KAstTopLevel::slotRockHit( int size ) +{ + switch ( size ) + { + case 0: + score += 10; + break; + + case 1: + score += 20; + break; + + default: + score += 40; + } + + playSound( "RockDestroyed" ); + + scoreLCD->display( score ); +} + +void KAstTopLevel::slotRocksRemoved() +{ + level++; + + if ( level >= MAX_LEVELS ) + level = MAX_LEVELS - 1; + + view->setRockSpeed( levels[level-1].rockSpeed ); + view->addRocks( levels[level-1].nrocks ); + + levelLCD->display( level+1 ); +} + +void KAstTopLevel::doStats() +{ + QString r( "0.00" ); + if ( view->shots() ) + r = QString::number( (double)view->hits() / view->shots() * 100.0, + 'g', 2 ); + +/* multi-line text broken in Qt 3 + QString s = tr( "Game Over\n\nShots fired:\t%1\n Hit:\t%2\n Missed:\t%3\nHit ratio:\t%4 %\n\nPress N for a new game" ) + .arg(view->shots()).arg(view->hits()) + .arg(view->shots() - view->hits()) + .arg(r); +*/ + + view->showText( "Game Over. Press N for a new game.", Qt::yellow, FALSE ); +} + +void KAstTopLevel::slotUpdateVitals() +{ + brakesLCD->display( view->brakeCount() ); + shieldLCD->display( view->shieldCount() ); + shootLCD->display( view->shootCount() ); +// teleportsLCD->display( view->teleportCount() ); + powerMeter->setValue( view->power() ); +} diff --git a/examples/graphicsview/portedasteroids/toplevel.h b/examples/graphicsview/portedasteroids/toplevel.h new file mode 100644 index 0000000..67781da --- /dev/null +++ b/examples/graphicsview/portedasteroids/toplevel.h @@ -0,0 +1,126 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +/* + * KAsteroids - Copyright (c) Martin R. Jones 1997 + * + * Part of the KDE project + */ + +#ifndef __KAST_TOPLEVEL_H__ +#define __KAST_TOPLEVEL_H__ + +#include <q3mainwindow.h> +#include <q3dict.h> +#include <qmap.h> +//Added by qt3to4: +#include <QShowEvent> +#include <QHideEvent> +#include <QKeyEvent> + +#include "view.h" + + +class KALedMeter; +QT_BEGIN_NAMESPACE +class QLCDNumber; +QT_END_NAMESPACE + +class KAstTopLevel : public Q3MainWindow +{ + Q_OBJECT +public: + KAstTopLevel( QWidget *parent=0, const char *name=0 ); + virtual ~KAstTopLevel(); + +private: + void playSound( const char *snd ); + void readSoundMapping(); + void doStats(); + +protected: + virtual void showEvent( QShowEvent * ); + virtual void hideEvent( QHideEvent * ); + virtual void keyPressEvent( QKeyEvent *event ); + virtual void keyReleaseEvent( QKeyEvent *event ); + +private slots: + void slotNewGame(); + + void slotShipKilled(); + void slotRockHit( int size ); + void slotRocksRemoved(); + + void slotUpdateVitals(); + +private: + KAsteroidsView *view; + QLCDNumber *scoreLCD; + QLCDNumber *levelLCD; + QLCDNumber *shipsLCD; + + QLCDNumber *teleportsLCD; +// QLCDNumber *bombsLCD; + QLCDNumber *brakesLCD; + QLCDNumber *shieldLCD; + QLCDNumber *shootLCD; + KALedMeter *powerMeter; + + bool sound; + Q3Dict<QString> soundDict; + + // waiting for user to press Enter to launch a ship + bool waitShip; + bool isPaused; + + int shipsRemain; + int score; + int level; + bool showHiscores; + + enum Action { Launch, Thrust, RotateLeft, RotateRight, Shoot, Teleport, + Brake, Shield, Pause, NewGame }; + + QMap<int,Action> actions; +}; + +#endif + diff --git a/examples/graphicsview/portedasteroids/view.cpp b/examples/graphicsview/portedasteroids/view.cpp new file mode 100644 index 0000000..fc6956b --- /dev/null +++ b/examples/graphicsview/portedasteroids/view.cpp @@ -0,0 +1,967 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +/* + * KAsteroids - Copyright (c) Martin R. Jones 1997 + * + * Part of the KDE project + */ + +#include <stdlib.h> +#include <math.h> +#include <qapplication.h> +#include <qnamespace.h> +#include <q3accel.h> +#include <qmessagebox.h> +#include <q3scrollview.h> +#include <qdir.h> +#include <QGraphicsItem> +//Added by qt3to4: +#include <QTimerEvent> +#include <QPixmap> +#include <QResizeEvent> +#include <QShowEvent> + +#include "view.h" + +#define IMG_BACKGROUND ":/trolltech/examples/graphicsview/portedasteroids/bg.png" + +#define REFRESH_DELAY 33 +#define SHIP_SPEED 0.3 +#define MISSILE_SPEED 10.0 +#define SHIP_STEPS 64 +#define ROTATE_RATE 2 +#define SHIELD_ON_COST 1 +#define SHIELD_HIT_COST 30 +#define BRAKE_ON_COST 4 + +#define MAX_ROCK_SPEED 2.5 +#define MAX_POWERUP_SPEED 1.5 +#define MAX_SHIP_SPEED 12 +#define MAX_BRAKES 5 +#define MAX_SHIELDS 5 +#define MAX_FIREPOWER 5 + +#define TEXT_SPEED 4 + +#define PI_X_2 6.283185307 +#ifndef M_PI +#define M_PI 3.141592654 +#endif + +static struct +{ + int id; + const char *path; + int frames; +} +kas_animations [] = +{ + { ID_ROCK_LARGE, "rock1/rock1%1.png", 32 }, + { ID_ROCK_MEDIUM, "rock2/rock2%1.png", 32 }, + { ID_ROCK_SMALL, "rock3/rock3%1.png", 32 }, + { ID_SHIP, "ship/ship%1.png", 32 }, + { ID_MISSILE, "missile/missile.png", 1 }, + { ID_BIT, "bits/bits%1.png", 16 }, + { ID_EXHAUST, "exhaust/exhaust.png", 1 }, + { ID_ENERGY_POWERUP, "powerups/energy.png", 1 }, +// { ID_TELEPORT_POWERUP, "powerups/teleport%1.png", 12 }, + { ID_BRAKE_POWERUP, "powerups/brake.png", 1 }, + { ID_SHIELD_POWERUP, "powerups/shield.png", 1 }, + { ID_SHOOT_POWERUP, "powerups/shoot.png", 1 }, + { ID_SHIELD, "shield/shield%1.png", 6 }, + { 0, 0, 0 } +}; + +KAsteroidsView::KAsteroidsView( QWidget *parent, const char *name ) + : QWidget( parent, name ), + field(0, 0, 640, 440), + view(&field,this) +{ + view.setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff ); + view.setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff ); + view.setCacheMode(QGraphicsView::CacheBackground); + view.setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate); + view.setOptimizationFlags(QGraphicsView::DontClipPainter + | QGraphicsView::DontSavePainterState + | QGraphicsView::DontAdjustForAntialiasing); + view.viewport()->setFocusProxy( this ); + rocks.setAutoDelete( TRUE ); + missiles.setAutoDelete( TRUE ); + bits.setAutoDelete( TRUE ); + powerups.setAutoDelete( TRUE ); + exhaust.setAutoDelete( TRUE ); + + QPixmap pm( IMG_BACKGROUND ); + field.setBackgroundBrush( pm ); + + textSprite = new QGraphicsTextItem( 0, &field ); + QFont font( "helvetica", 18 ); + textSprite->setFont( font ); + textSprite->setCacheMode(QGraphicsItem::DeviceCoordinateCache); + + shield = 0; + shieldOn = FALSE; + refreshRate = REFRESH_DELAY; + + initialized = readSprites(); + + shieldTimer = new QTimer( this ); + connect( shieldTimer, SIGNAL(timeout()), this, SLOT(hideShield()) ); + mTimerId = -1; + + shipPower = MAX_POWER_LEVEL; + vitalsChanged = TRUE; + can_destroy_powerups = FALSE; + + mPaused = TRUE; + + if ( !initialized ) { + textSprite->setHtml( tr("<font color=red>Error: Cannot read sprite images</font>") ); + textSprite->setPos( (field.width()-textSprite->boundingRect().width()) / 2, + (field.height()-textSprite->boundingRect().height()) / 2 ); + } +} + +// - - - + +KAsteroidsView::~KAsteroidsView() +{ +} + +// - - - + +void KAsteroidsView::reset() +{ + if ( !initialized ) + return; + rocks.clear(); + missiles.clear(); + bits.clear(); + powerups.clear(); + exhaust.clear(); + + shotsFired = 0; + shotsHit = 0; + + rockSpeed = 1.0; + powerupSpeed = 1.0; + mFrameNum = 0; + mPaused = FALSE; + + ship->hide(); + shield->hide(); +/* + if ( mTimerId >= 0 ) { + killTimer( mTimerId ); + mTimerId = -1; + } +*/ +} + +// - -- + +void KAsteroidsView::newGame() +{ + if ( !initialized ) + return; + if ( shieldOn ) + { + shield->hide(); + shieldOn = FALSE; + } + reset(); + if ( mTimerId < 0 ) + mTimerId = startTimer( REFRESH_DELAY ); + emit updateVitals(); +} + +// - - - + +void KAsteroidsView::endGame() +{ +} + +void KAsteroidsView::pause( bool p ) +{ + if ( !initialized ) + return; + if ( !mPaused && p ) { + if ( mTimerId >= 0 ) { + killTimer( mTimerId ); + mTimerId = -1; + } + } else if ( mPaused && !p ) + mTimerId = startTimer( REFRESH_DELAY ); + mPaused = p; +} + +// - - - + +void KAsteroidsView::newShip() +{ + if ( !initialized ) + return; + ship->setPos( width()/2, height()/2 ); + ship->setFrame( 0 ); + shield->setPos( width()/2, height()/2 ); + shield->setFrame( 0 ); + ship->setVelocity( 0.0, 0.0 ); + shipDx = 0; + shipDy = 0; + shipAngle = 0; + rotateL = FALSE; + rotateR = FALSE; + thrustShip = FALSE; + shootShip = FALSE; + brakeShip = FALSE; + teleportShip = FALSE; + shieldOn = TRUE; + shootDelay = 0; + shipPower = MAX_POWER_LEVEL; + rotateRate = ROTATE_RATE; + rotateSlow = 0; + + mBrakeCount = 0; + mTeleportCount = 0; + mShootCount = 0; + + ship->show(); + shield->show(); + mShieldCount = 1; // just in case the ship appears on a rock. + shieldTimer->start( 1000, TRUE ); +} + +void KAsteroidsView::setShield( bool s ) +{ + if ( !initialized ) + return; + if ( shieldTimer->isActive() && !s ) { + shieldTimer->stop(); + hideShield(); + } else { + shieldOn = s && mShieldCount; + } +} + +void KAsteroidsView::brake( bool b ) +{ + if ( !initialized ) + return; + if ( mBrakeCount ) + { + if ( brakeShip && !b ) + { + rotateL = FALSE; + rotateR = FALSE; + thrustShip = FALSE; + rotateRate = ROTATE_RATE; + } + + brakeShip = b; + } +} + +// - - - + +bool KAsteroidsView::readSprites() +{ + QString sprites_prefix = ":/trolltech/examples/graphicsview/portedasteroids/sprites/"; + + int i = 0; + while ( kas_animations[i].id ) + { + QList<QPixmap> anim; + QString wildcard = sprites_prefix + kas_animations[i].path; + wildcard.replace("%1", "*"); + QFileInfo fi(wildcard); + foreach (QString entry, QDir(fi.path(), fi.fileName()).entryList()) + anim << QPixmap(fi.path() + "/" + entry); + animation.insert( kas_animations[i].id, anim ); + i++; + } + + ship = new AnimatedPixmapItem( animation[ID_SHIP], &field ); + ship->hide(); + + shield = new KShield( animation[ID_SHIELD], &field ); + shield->hide(); + + return (!ship->image(0).isNull() && !shield->image(0).isNull()); +} + +// - - - + +void KAsteroidsView::addRocks( int num ) +{ + if ( !initialized ) + return; + for ( int i = 0; i < num; i++ ) + { + KRock *rock = new KRock( animation[ID_ROCK_LARGE], &field, + ID_ROCK_LARGE, randInt(2), randInt(2) ? -1 : 1 ); + double dx = (2.0 - randDouble()*4.0) * rockSpeed; + double dy = (2.0 - randDouble()*4.0) * rockSpeed; + rock->setVelocity( dx, dy ); + rock->setFrame( randInt( rock->frameCount() ) ); + if ( dx > 0 ) + { + if ( dy > 0 ) + rock->setPos( 5, 5 ); + else + rock->setPos( 5, field.height() - 25 ); + rock->setFrame( 0 ); + } + else + { + if ( dy > 0 ) + rock->setPos( field.width() - 25, 5 ); + else + rock->setPos( field.width() - 25, field.height() - 25 ); + rock->setFrame( 0 ); + } + rock->show(); + rocks.append( rock ); + } +} + +// - - - + +void KAsteroidsView::showText( const QString &text, const QColor &color, bool scroll ) +{ + if ( !initialized ) + return; + textSprite->setHtml( QString("<font color=#%1%2%3>%4</font>") + .arg(color.red(), 2, 16, QLatin1Char('0')) + .arg(color.green(), 2, 16, QLatin1Char('0')) + .arg(color.blue(), 2, 16, QLatin1Char('0')) + .arg(text) ); + Q_UNUSED(color); + // ### Porting: no such thing textSprite->setColor( color ); + + if ( scroll ) { + textSprite->setPos( (field.width()-textSprite->boundingRect().width()) / 2, + -textSprite->boundingRect().height() ); + textDy = TEXT_SPEED; + } else { + textSprite->setPos( (field.width()-textSprite->boundingRect().width()) / 2, + (field.height()-textSprite->boundingRect().height()) / 2 ); + textDy = 0; + } + textSprite->show(); +} + +// - - - + +void KAsteroidsView::hideText() +{ + textDy = -TEXT_SPEED; +} + +// - - - + +void KAsteroidsView::resizeEvent(QResizeEvent* event) +{ + QWidget::resizeEvent(event); + field.setSceneRect(0, 0, width()-4, height()-4); + view.resize(width(),height()); +} + +// - - - + +void KAsteroidsView::timerEvent( QTimerEvent * ) +{ + field.advance(); + + AnimatedPixmapItem *rock; + + // move rocks forward + for ( rock = rocks.first(); rock; rock = rocks.next() ) { + ((KRock *)rock)->nextFrame(); + wrapSprite( rock ); + } + + wrapSprite( ship ); + + // check for missile collision with rocks. + processMissiles(); + + // these are generated when a ship explodes + for ( KBit *bit = bits.first(); bit; bit = bits.next() ) + { + if ( bit->expired() ) + { + bits.removeRef( bit ); + } + else + { + bit->growOlder(); + bit->setFrame( ( bit->frame()+1 ) % bit->frameCount() ); + } + } + + for ( KExhaust *e = exhaust.first(); e; e = exhaust.next() ) + exhaust.removeRef( e ); + + // move / rotate ship. + // check for collision with a rock. + processShip(); + + // move powerups and check for collision with player and missiles + processPowerups(); + + if ( textSprite->isVisible() ) + { + if ( textDy < 0 && + textSprite->boundingRect().y() <= -textSprite->boundingRect().height() ) { + textSprite->hide(); + } else { + textSprite->moveBy( 0, textDy ); + } + + if ( textSprite->sceneBoundingRect().y() > (field.height()-textSprite->boundingRect().height())/2 ) + textDy = 0; + } + + if ( vitalsChanged && !(mFrameNum % 10) ) { + emit updateVitals(); + vitalsChanged = FALSE; + } + + mFrameNum++; +} + +void KAsteroidsView::wrapSprite( QGraphicsItem *s ) +{ + int x = int(s->x() + s->boundingRect().width() / 2); + int y = int(s->y() + s->boundingRect().height() / 2); + + if ( x > field.width() ) + s->setPos( s->x() - field.width(), s->y() ); + else if ( x < 0 ) + s->setPos( field.width() + s->x(), s->y() ); + + if ( y > field.height() ) + s->setPos( s->x(), s->y() - field.height() ); + else if ( y < 0 ) + s->setPos( s->x(), field.height() + s->y() ); +} + +// - - - + +void KAsteroidsView::rockHit( AnimatedPixmapItem *hit ) +{ + KPowerup *nPup = 0; + int rnd = int(randDouble()*30.0) % 30; + switch( rnd ) + { + case 4: + case 5: + nPup = new KPowerup( animation[ID_ENERGY_POWERUP], &field, + ID_ENERGY_POWERUP ); + break; + case 10: +// nPup = new KPowerup( animation[ID_TELEPORT_POWERUP], &field, +// ID_TELEPORT_POWERUP ); + break; + case 15: + nPup = new KPowerup( animation[ID_BRAKE_POWERUP], &field, + ID_BRAKE_POWERUP ); + break; + case 20: + nPup = new KPowerup( animation[ID_SHIELD_POWERUP], &field, + ID_SHIELD_POWERUP ); + break; + case 24: + case 25: + nPup = new KPowerup( animation[ID_SHOOT_POWERUP], &field, + ID_SHOOT_POWERUP ); + break; + } + if ( nPup ) + { + double r = 0.5 - randDouble(); + nPup->setPos( hit->x(), hit->y() ); + nPup->setFrame( 0 ); + nPup->setVelocity( hit->xVelocity() + r, hit->yVelocity() + r ); + powerups.append( nPup ); + } + + if ( hit->type() == ID_ROCK_LARGE || hit->type() == ID_ROCK_MEDIUM ) + { + // break into smaller rocks + double addx[4] = { 1.0, 1.0, -1.0, -1.0 }; + double addy[4] = { -1.0, 1.0, -1.0, 1.0 }; + + double dx = hit->xVelocity(); + double dy = hit->yVelocity(); + + double maxRockSpeed = MAX_ROCK_SPEED * rockSpeed; + if ( dx > maxRockSpeed ) + dx = maxRockSpeed; + else if ( dx < -maxRockSpeed ) + dx = -maxRockSpeed; + if ( dy > maxRockSpeed ) + dy = maxRockSpeed; + else if ( dy < -maxRockSpeed ) + dy = -maxRockSpeed; + + AnimatedPixmapItem *nrock; + + for ( int i = 0; i < 4; i++ ) + { + double r = rockSpeed/2 - randDouble()*rockSpeed; + if ( hit->type() == ID_ROCK_LARGE ) + { + nrock = new KRock( animation[ID_ROCK_MEDIUM], &field, + ID_ROCK_MEDIUM, randInt(2), randInt(2) ? -1 : 1 ); + emit rockHit( 0 ); + } + else + { + nrock = new KRock( animation[ID_ROCK_SMALL], &field, + ID_ROCK_SMALL, randInt(2), randInt(2) ? -1 : 1 ); + emit rockHit( 1 ); + } + + nrock->setPos( hit->x(), hit->y() ); + nrock->setFrame( 0 ); + nrock->setVelocity( dx+addx[i]*rockSpeed+r, dy+addy[i]*rockSpeed+r ); + nrock->setFrame( randInt( nrock->frameCount() ) ); + rocks.append( nrock ); + } + } + else if ( hit->type() == ID_ROCK_SMALL ) + emit rockHit( 2 ); + rocks.removeRef( hit ); + if ( rocks.count() == 0 ) + emit rocksRemoved(); +} + +void KAsteroidsView::reducePower( int val ) +{ + shipPower -= val; + if ( shipPower <= 0 ) + { + shipPower = 0; + thrustShip = FALSE; + if ( shieldOn ) + { + shieldOn = FALSE; + shield->hide(); + } + } + vitalsChanged = TRUE; +} + +void KAsteroidsView::addExhaust( double x, double y, double dx, + double dy, int count ) +{ + for ( int i = 0; i < count; i++ ) + { + KExhaust *e = new KExhaust( animation[ID_EXHAUST], &field ); + e->setPos( x + 2 - randDouble()*4, y + 2 - randDouble()*4 ); + e->setVelocity( dx, dy ); + exhaust.append( e ); + } +} + +void KAsteroidsView::processMissiles() +{ + KMissile *missile; + + // if a missile has hit a rock, remove missile and break rock into smaller + // rocks or remove completely. + Q3PtrListIterator<KMissile> it(missiles); + + for ( ; it.current(); ++it ) + { + missile = it.current(); + missile->growOlder(); + + if ( missile->expired() ) + { + missiles.removeRef( missile ); + continue; + } + + wrapSprite( missile ); + + QList<QGraphicsItem *> hits = missile->collidingItems(Qt::IntersectsItemBoundingRect); + QList<QGraphicsItem *>::Iterator hit; + for ( hit = hits.begin(); hit != hits.end(); ++hit ) + { + if ( (*hit)->type() >= ID_ROCK_LARGE && + (*hit)->type() <= ID_ROCK_SMALL && (*hit)->collidesWithItem(missile) ) + { + shotsHit++; + rockHit( static_cast<AnimatedPixmapItem *>(*hit) ); + missiles.removeRef( missile ); + break; + } + } + } +} + +// - - - + +void KAsteroidsView::processShip() +{ + if ( ship->isVisible() ) + { + if ( shieldOn ) + { + shield->show(); + reducePower( SHIELD_ON_COST ); + static int sf = 0; + sf++; + + if ( sf % 2 ) + shield->setFrame( (shield->frame()+1) % shield->frameCount() ); + shield->setPos( ship->x() - 9, ship->y() - 9 ); + + QList<QGraphicsItem *> hits = shield->collidingItems(Qt::IntersectsItemBoundingRect); + QList<QGraphicsItem *>::Iterator it; + for ( it = hits.begin(); it != hits.end(); ++it ) + { + if ( (*it)->type() >= ID_ROCK_LARGE && + (*it)->type() <= ID_ROCK_SMALL && (*it)->collidesWithItem(shield) ) + { + int factor; + switch ( (*it)->type() ) + { + case ID_ROCK_LARGE: + factor = 3; + break; + + case ID_ROCK_MEDIUM: + factor = 2; + break; + + default: + factor = 1; + } + + if ( factor > mShieldCount ) + { + // shield not strong enough + shieldOn = FALSE; + break; + } + rockHit( static_cast<AnimatedPixmapItem *>(*it) ); + // the more shields we have the less costly + reducePower( factor * (SHIELD_HIT_COST - mShieldCount*2) ); + } + } + } + + if ( !shieldOn ) + { + shield->hide(); + QList<QGraphicsItem *> hits = ship->collidingItems(Qt::IntersectsItemBoundingRect); + QList<QGraphicsItem *>::Iterator it; + for ( it = hits.begin(); it != hits.end(); ++it ) + { + if ( (*it)->type() >= ID_ROCK_LARGE && + (*it)->type() <= ID_ROCK_SMALL && (*it)->collidesWithItem(ship)) + { + KBit *bit; + for ( int i = 0; i < 12; i++ ) + { + bit = new KBit( animation[ID_BIT], &field ); + bit->setPos( ship->x() + 5 - randDouble() * 10, + ship->y() + 5 - randDouble() * 10 ); + bit->setFrame( randInt(bit->frameCount()) ); + bit->setVelocity( 1-randDouble()*2, + 1-randDouble()*2 ); + bit->setDeath( 60 + randInt(60) ); + bits.append( bit ); + } + ship->hide(); + shield->hide(); + emit shipKilled(); + break; + } + } + } + + + if ( rotateSlow ) + rotateSlow--; + + if ( rotateL ) + { + shipAngle -= rotateSlow ? 1 : rotateRate; + if ( shipAngle < 0 ) + shipAngle += SHIP_STEPS; + } + + if ( rotateR ) + { + shipAngle += rotateSlow ? 1 : rotateRate; + if ( shipAngle >= SHIP_STEPS ) + shipAngle -= SHIP_STEPS; + } + + double angle = shipAngle * PI_X_2 / SHIP_STEPS; + double cosangle = cos( angle ); + double sinangle = sin( angle ); + + if ( brakeShip ) + { + thrustShip = FALSE; + rotateL = FALSE; + rotateR = FALSE; + rotateRate = ROTATE_RATE; + if ( fabs(shipDx) < 2.5 && fabs(shipDy) < 2.5 ) + { + shipDx = 0.0; + shipDy = 0.0; + ship->setVelocity( shipDx, shipDy ); + brakeShip = FALSE; + } + else + { + double motionAngle = atan2( -shipDy, -shipDx ); + if ( angle > M_PI ) + angle -= PI_X_2; + double angleDiff = angle - motionAngle; + if ( angleDiff > M_PI ) + angleDiff = PI_X_2 - angleDiff; + else if ( angleDiff < -M_PI ) + angleDiff = PI_X_2 + angleDiff; + double fdiff = fabs( angleDiff ); + if ( fdiff > 0.08 ) + { + if ( angleDiff > 0 ) + rotateL = TRUE; + else if ( angleDiff < 0 ) + rotateR = TRUE; + if ( fdiff > 0.6 ) + rotateRate = mBrakeCount + 1; + else if ( fdiff > 0.4 ) + rotateRate = 2; + else + rotateRate = 1; + + if ( rotateRate > 5 ) + rotateRate = 5; + } + else if ( fabs(shipDx) > 1 || fabs(shipDy) > 1 ) + { + thrustShip = TRUE; + // we'll make braking a bit faster + shipDx += cosangle/6 * (mBrakeCount - 1); + shipDy += sinangle/6 * (mBrakeCount - 1); + reducePower( BRAKE_ON_COST ); + addExhaust( ship->x() + 20 - cosangle*22, + ship->y() + 20 - sinangle*22, + shipDx-cosangle, shipDy-sinangle, + mBrakeCount+1 ); + } + } + } + + if ( thrustShip ) + { + // The ship has a terminal velocity, but trying to go faster + // still uses fuel (can go faster diagonally - don't care). + double thrustx = cosangle/4; + double thrusty = sinangle/4; + if ( fabs(shipDx + thrustx) < MAX_SHIP_SPEED ) + shipDx += thrustx; + if ( fabs(shipDy + thrusty) < MAX_SHIP_SPEED ) + shipDy += thrusty; + ship->setVelocity( shipDx, shipDy ); + reducePower( 1 ); + addExhaust( ship->x() + 20 - cosangle*20, + ship->y() + 20 - sinangle*20, + shipDx-cosangle, shipDy-sinangle, 3 ); + } + + ship->setFrame( shipAngle >> 1 ); + + if ( shootShip ) + { + if ( !shootDelay && (int)missiles.count() < mShootCount + 2 ) + { + KMissile *missile = new KMissile( animation[ID_MISSILE], &field ); + missile->setPos( 21+ship->x()+cosangle*21, + 21+ship->y()+sinangle*21 ); + missile->setFrame( 0 ); + missile->setVelocity( shipDx + cosangle*MISSILE_SPEED, + shipDy + sinangle*MISSILE_SPEED ); + missiles.append( missile ); + shotsFired++; + reducePower( 1 ); + + shootDelay = 5; + } + + if ( shootDelay ) + shootDelay--; + } + + if ( teleportShip ) + { + int ra = qrand() % 10; + if( ra == 0 ) + ra += qrand() % 20; + int xra = ra * 60 + ( (qrand() % 20) * (qrand() % 20) ); + int yra = ra * 50 - ( (qrand() % 20) * (qrand() % 20) ); + ship->setPos( xra, yra ); + } + + vitalsChanged = TRUE; + } +} + +// - - - + +void KAsteroidsView::processPowerups() +{ + if ( !powerups.isEmpty() ) + { + // if player gets the powerup remove it from the screen, if option + // "Can destroy powerups" is enabled and a missile hits the powerup + // destroy it + + KPowerup *pup; + Q3PtrListIterator<KPowerup> it( powerups ); + + for( ; it.current(); ++it ) + { + pup = it.current(); + pup->growOlder(); + + if( pup->expired() ) + { + powerups.removeRef( pup ); + continue; + } + + wrapSprite( pup ); + + QList<QGraphicsItem *> hits = pup->collidingItems(); + QList<QGraphicsItem *>::Iterator it; + for ( it = hits.begin(); it != hits.end(); ++it ) + { + if ( (*it) == ship ) + { + switch( pup->type() ) + { + case ID_ENERGY_POWERUP: + shipPower += 150; + if ( shipPower > MAX_POWER_LEVEL ) + shipPower = MAX_POWER_LEVEL; + break; + case ID_TELEPORT_POWERUP: + mTeleportCount++; + break; + case ID_BRAKE_POWERUP: + if ( mBrakeCount < MAX_BRAKES ) + mBrakeCount++; + break; + case ID_SHIELD_POWERUP: + if ( mShieldCount < MAX_SHIELDS ) + mShieldCount++; + break; + case ID_SHOOT_POWERUP: + if ( mShootCount < MAX_FIREPOWER ) + mShootCount++; + break; + } + + powerups.removeRef( pup ); + vitalsChanged = TRUE; + } + else if ( (*it) == shield ) + { + powerups.removeRef( pup ); + } + else if ( (*it)->type() == ID_MISSILE ) + { + if ( can_destroy_powerups ) + { + powerups.removeRef( pup ); + } + } + } + } + } // -- if( powerups.isEmpty() ) +} + +// - - - + +void KAsteroidsView::hideShield() +{ + shield->hide(); + mShieldCount = 0; + shieldOn = FALSE; +} + +double KAsteroidsView::randDouble() +{ + int v = qrand(); + return (double)v / (double)RAND_MAX; +} + +int KAsteroidsView::randInt( int range ) +{ + return qrand() % range; +} + +void KAsteroidsView::showEvent( QShowEvent *e ) +{ +#if defined( QT_LICENSE_PROFESSIONAL ) + static bool wasThere = FALSE; + + if ( !wasThere ) { + wasThere = TRUE; + QMessageBox::information( this, tr("QGraphicsView demo"), + tr("This game has been implemented using the QGraphicsView class.\n" + "The QGraphicsView class is not part of the Light Platform Edition. Please \n" + "contact Qt Software if you want to upgrade to the Full Platform Edition.") ); + } +#endif + + QWidget::showEvent( e ); +} diff --git a/examples/graphicsview/portedasteroids/view.h b/examples/graphicsview/portedasteroids/view.h new file mode 100644 index 0000000..d055f29 --- /dev/null +++ b/examples/graphicsview/portedasteroids/view.h @@ -0,0 +1,184 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +/* + * KAsteroids - Copyright (c) Martin R. Jones 1997 + * + * Part of the KDE project + */ + +#ifndef __AST_VIEW_H__ +#define __AST_VIEW_H__ + +#include <qwidget.h> +#include <q3ptrlist.h> +#include <q3intdict.h> +#include <qtimer.h> +#include <QGraphicsScene> +#include <QGraphicsView> +//Added by qt3to4: +#include <QTimerEvent> +#include <QShowEvent> +#include <QResizeEvent> +#include "sprites.h" + +#define MAX_POWER_LEVEL 1000 + +class KAsteroidsView : public QWidget +{ + Q_OBJECT +public: + KAsteroidsView( QWidget *parent = 0, const char *name = 0 ); + virtual ~KAsteroidsView(); + + int refreshRate; + + void reset(); + void setRockSpeed( double rs ) { rockSpeed = rs; } + void addRocks( int num ); + void newGame(); + void endGame(); + void newShip(); + + void rotateLeft( bool r ) { rotateL = r; rotateSlow = 5; } + void rotateRight( bool r ) { rotateR = r; rotateSlow = 5; } + void thrust( bool t ) { thrustShip = t && shipPower > 0; } + void shoot( bool s ) { shootShip = s; shootDelay = 0; } + void setShield( bool s ); + void teleport( bool te) { teleportShip = te && mTeleportCount; } + void brake( bool b ); + void pause( bool p); + + void showText( const QString &text, const QColor &color, bool scroll=TRUE ); + void hideText(); + + int shots() const { return shotsFired; } + int hits() const { return shotsHit; } + int power() const { return shipPower; } + + int teleportCount() const { return mTeleportCount; } + int brakeCount() const { return mBrakeCount; } + int shieldCount() const { return mShieldCount; } + int shootCount() const { return mShootCount; } + +signals: + void shipKilled(); + void rockHit( int size ); + void rocksRemoved(); + void updateVitals(); + +private slots: + void hideShield(); + +protected: + bool readSprites(); + void wrapSprite( QGraphicsItem * ); + void rockHit( AnimatedPixmapItem * ); + void reducePower( int val ); + void addExhaust( double x, double y, double dx, double dy, int count ); + void processMissiles(); + void processShip(); + void processPowerups(); + void processShield(); + double randDouble(); + int randInt( int range ); + + virtual void resizeEvent( QResizeEvent *event ); + virtual void timerEvent( QTimerEvent * ); + + void showEvent( QShowEvent * ); + +private: + QGraphicsScene field; + QGraphicsView view; + QMap<int, QList<QPixmap> > animation; + Q3PtrList<AnimatedPixmapItem> rocks; + Q3PtrList<KMissile> missiles; + Q3PtrList<KBit> bits; + Q3PtrList<KExhaust> exhaust; + Q3PtrList<KPowerup> powerups; + KShield *shield; + AnimatedPixmapItem *ship; + QGraphicsTextItem *textSprite; + + bool rotateL; + bool rotateR; + bool thrustShip; + bool shootShip; + bool teleportShip; + bool brakeShip; + bool pauseShip; + bool shieldOn; + + bool vitalsChanged; + + int shipAngle; + int rotateSlow; + int rotateRate; + int shipPower; + + int shotsFired; + int shotsHit; + int shootDelay; + + int mBrakeCount; + int mShieldCount; + int mTeleportCount; + int mShootCount; + + double shipDx; + double shipDy; + + int textDy; + int mFrameNum; + bool mPaused; + int mTimerId; + + double rockSpeed; + double powerupSpeed; + + bool can_destroy_powerups; + + QTimer *shieldTimer; + bool initialized; +}; + +#endif diff --git a/examples/graphicsview/portedcanvas/blendshadow.cpp b/examples/graphicsview/portedcanvas/blendshadow.cpp new file mode 100644 index 0000000..42cd0e3 --- /dev/null +++ b/examples/graphicsview/portedcanvas/blendshadow.cpp @@ -0,0 +1,94 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <qimage.h> +#include <qcolor.h> + +static inline int blendComponent( int v, int av, int s, int as ) +{ + return as*s + av*v -(av*as*s)/255; +} + +static inline QRgb blendShade( QRgb v, QRgb s ) +{ + //shadow image is already reduced and blurred + int as = qAlpha(s); + int av = qAlpha(v); + if ( as == 0 || av == 255 ) + return v; + + int a = as + av -(as*av)/255; + + int r = blendComponent( qRed(v),av, qRed(s), as)/a; + int g = blendComponent( qGreen(v),av, qGreen(s), as)/a; + int b = blendComponent( qBlue(v),av, qBlue(s), as)/a; + + return qRgba(r,g,b,a); +} + + + +int main( int*, char**) +{ + QImage image( "out.png" ); + image.convertDepth( 32 ); + QImage shade( "outshade.png" ); + shade.convertDepth( 32 ); + int dx = 10; + int dy = 5; + + int w = image.width(); + int h = image.height(); + + QImage img( w+dx, h+dy, 32 ); + img.setAlphaBuffer( TRUE ); + + for ( int y = 0; y < h+dy; y++ ) { + for ( int x = 0; x < w+dx; x++ ) { + QRgb sh = (x<dx||y<dy) ? 0 : shade.pixel( x-dx, y-dy ); + QRgb pixel = (x<w&y<h) ? image.pixel( x, y ) : 0; + img.setPixel( x, y, blendShade( pixel, sh ) ); + } + } + img.save("blend.png", "PNG" ); +} + + diff --git a/examples/graphicsview/portedcanvas/butterfly.png b/examples/graphicsview/portedcanvas/butterfly.png Binary files differnew file mode 100644 index 0000000..f3e050e --- /dev/null +++ b/examples/graphicsview/portedcanvas/butterfly.png diff --git a/examples/graphicsview/portedcanvas/canvas.cpp b/examples/graphicsview/portedcanvas/canvas.cpp new file mode 100644 index 0000000..c8718dc --- /dev/null +++ b/examples/graphicsview/portedcanvas/canvas.cpp @@ -0,0 +1,733 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <qdatetime.h> +#include <qmainwindow.h> +#include <qstatusbar.h> +#include <qmessagebox.h> +#include <qmenubar.h> +#include <qapplication.h> +#include <qpainter.h> +#include <qprinter.h> +#include <qlabel.h> +#include <qimage.h> +#include <q3progressdialog.h> +#include <Q3PointArray> +#include <Q3PtrList> +#include <QPixmap> +#include <Q3PopupMenu> +#include <QMouseEvent> +#include <Q3MemArray> +#include "canvas.h" +#include <QStyleOptionGraphicsItem> +#include <qdebug.h> +#include <stdlib.h> + +// We use a global variable to save memory - all the brushes and pens in +// the mesh are shared. +static QBrush *tb = 0; +static QPen *tp = 0; + +class EdgeItem; +class NodeItem; + +class EdgeItem: public QGraphicsLineItem +{ +public: + EdgeItem( NodeItem*, NodeItem* ); + void setFromPoint( int x, int y ) ; + void setToPoint( int x, int y ); + static int count() { return c; } +private: + static int c; +}; + +static const int imageRTTI = 984376; + + +class ImageItem: public QGraphicsRectItem +{ +public: + ImageItem( QImage img ); + int rtti () const { return imageRTTI; } +protected: + void paint( QPainter *, const QStyleOptionGraphicsItem *option, QWidget *widget ); +private: + QImage image; + QPixmap pixmap; +}; + + +ImageItem::ImageItem( QImage img ) + : image(img) +{ + setRect(0, 0, image.width(), image.height()); + setFlag(ItemIsMovable); +#if !defined(Q_WS_QWS) + pixmap.convertFromImage(image, Qt::OrderedAlphaDither); +#endif +} + +void ImageItem::paint( QPainter *p, const QStyleOptionGraphicsItem *option, QWidget * ) +{ +// On Qt/Embedded, we can paint a QImage as fast as a QPixmap, +// but on other platforms, we need to use a QPixmap. +#if defined(Q_WS_QWS) + p->drawImage( option->exposedRect, image, option->exposedRect, Qt::OrderedAlphaDither ); +#else + p->drawPixmap( option->exposedRect, pixmap, option->exposedRect ); +#endif +} + +class NodeItem: public QGraphicsEllipseItem +{ +public: + NodeItem(); + ~NodeItem() {} + + void addInEdge( EdgeItem *edge ) { inList.append( edge ); } + void addOutEdge( EdgeItem *edge ) { outList.append( edge ); } + +protected: + QVariant itemChange(GraphicsItemChange change, const QVariant &value); + + + // QPoint center() { return boundingRect().center(); } +private: + Q3PtrList<EdgeItem> inList; + Q3PtrList<EdgeItem> outList; +}; + + +int EdgeItem::c = 0; + +EdgeItem::EdgeItem( NodeItem *from, NodeItem *to ) + : QGraphicsLineItem( ) +{ + c++; + setPen( *tp ); + from->addOutEdge( this ); + to->addInEdge( this ); + setLine( QLineF(int(from->x()), int(from->y()), int(to->x()), int(to->y()) )); + setZValue( 127 ); + setBoundingRegionGranularity(0.05); +} + +void EdgeItem::setFromPoint( int x, int y ) +{ + setLine(QLineF( x,y, line().p2().x(), line().p2().y() )); +} + +void EdgeItem::setToPoint( int x, int y ) +{ + setLine(QLineF( line().p1().x(), line().p1().y(), x, y )); +} + +QVariant NodeItem::itemChange(GraphicsItemChange change, const QVariant &value) +{ + if (change == ItemPositionHasChanged) { + Q3PtrListIterator<EdgeItem> it1( inList ); + EdgeItem *edge; + while (( edge = it1.current() )) { + ++it1; + edge->setToPoint( int(x()), int(y()) ); + } + Q3PtrListIterator<EdgeItem> it2( outList ); + while (( edge = it2.current() )) { + ++it2; + edge->setFromPoint( int(x()), int(y()) ); + } + } + + return QGraphicsEllipseItem::itemChange(change, value); +} + +NodeItem::NodeItem( ) + : QGraphicsEllipseItem( QRectF(-3, -3, 6, 6) ) +{ + setPen( *tp ); + setBrush( *tb ); + setZValue( 128 ); + setFlag(ItemIsMovable); +} + +FigureEditor::FigureEditor( + QGraphicsScene& c, QWidget* parent, + const char* name, Qt::WindowFlags f) : + QGraphicsView(&c,parent) +{ + setObjectName(name); + setWindowFlags(f); + setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform); +} + +void FigureEditor::clear() +{ + scene()->clear(); +} + +BouncyLogo::BouncyLogo() : + xvel(0), yvel(0) +{ + setPixmap(QPixmap(":/trolltech/examples/graphicsview/portedcanvas/qt-trans.xpm")); +} + +const int logo_rtti = 1234; + +int BouncyLogo::type() const +{ + return logo_rtti; +} + +QPainterPath BouncyLogo::shape() const +{ + QPainterPath path; + path.addRect(boundingRect()); + return path; +} + +void BouncyLogo::initPos() +{ + initSpeed(); + int trial=1000; + do { + setPos(qrand()%int(scene()->width()),qrand()%int(scene()->height())); + advance(0); + } while (trial-- && xvel==0.0 && yvel==0.0); +} + +void BouncyLogo::initSpeed() +{ + const double speed = 4.0; + double d = (double)(qrand()%1024) / 1024.0; + xvel = d*speed*2-speed; + yvel = (1-d)*speed*2-speed; +} + +void BouncyLogo::advance(int stage) +{ + switch ( stage ) { + case 0: { + double vx = xvel; + double vy = yvel; + + if ( vx == 0.0 && vy == 0.0 ) { + // stopped last turn + initSpeed(); + vx = xvel; + vy = yvel; + } + + double nx = x() + vx; + double ny = y() + vy; + + if ( nx < 0 || nx >= scene()->width() ) + vx = -vx; + if ( ny < 0 || ny >= scene()->height() ) + vy = -vy; + + for (int bounce=0; bounce<4; bounce++) { + QList<QGraphicsItem *> l=scene()->collidingItems(this); + for (QList<QGraphicsItem *>::Iterator it=l.begin(); it!=l.end(); ++it) { + QGraphicsItem *hit = *it; + QPainterPath advancedShape = QMatrix().translate(xvel, yvel).map(shape()); + if ( hit->type()==logo_rtti && hit->collidesWithPath(mapToItem(hit, advancedShape)) ) { + switch ( bounce ) { + case 0: + vx = -vx; + break; + case 1: + vy = -vy; + vx = -vx; + break; + case 2: + vx = -vx; + break; + case 3: + // Stop for this turn + vx = 0; + vy = 0; + break; + } + xvel = vx; + yvel = vy; + break; + } + } + } + + if ( x()+vx < 0 || x()+vx >= scene()->width() ) + vx = 0; + if ( y()+vy < 0 || y()+vy >= scene()->height() ) + vy = 0; + + xvel = vx; + yvel = vy; + } break; + case 1: + moveBy(xvel, yvel); + break; + } +} + +static uint mainCount = 0; +static QImage *butterflyimg; +static QImage *logoimg; + +Main::Main(QGraphicsScene& c, QWidget* parent, const char* name, Qt::WindowFlags f) : + Q3MainWindow(parent,name,f), + canvas(c) +{ + editor = new FigureEditor(canvas,this); + QMenuBar* menu = menuBar(); + + Q3PopupMenu* file = new Q3PopupMenu( menu ); + file->insertItem("&Fill canvas", this, SLOT(init()), Qt::CTRL+Qt::Key_F); + file->insertItem("&Erase canvas", this, SLOT(clear()), Qt::CTRL+Qt::Key_E); + file->insertItem("&New view", this, SLOT(newView()), Qt::CTRL+Qt::Key_N); + file->insertSeparator(); + file->insertItem("&Print...", this, SLOT(print()), Qt::CTRL+Qt::Key_P); + file->insertSeparator(); + file->insertItem("E&xit", qApp, SLOT(quit()), Qt::CTRL+Qt::Key_Q); + menu->insertItem("&File", file); + + Q3PopupMenu* edit = new Q3PopupMenu( menu ); + edit->insertItem("Add &Circle", this, SLOT(addCircle()), Qt::ALT+Qt::Key_C); + edit->insertItem("Add &Hexagon", this, SLOT(addHexagon()), Qt::ALT+Qt::Key_H); + edit->insertItem("Add &Polygon", this, SLOT(addPolygon()), Qt::ALT+Qt::Key_P); + edit->insertItem("Add Spl&ine", this, SLOT(addSpline()), Qt::ALT+Qt::Key_I); + edit->insertItem("Add &Text", this, SLOT(addText()), Qt::ALT+Qt::Key_T); + edit->insertItem("Add &Line", this, SLOT(addLine()), Qt::ALT+Qt::Key_L); + edit->insertItem("Add &Rectangle", this, SLOT(addRectangle()), Qt::ALT+Qt::Key_R); + edit->insertItem("Add &Sprite", this, SLOT(addSprite()), Qt::ALT+Qt::Key_S); + edit->insertItem("Create &Mesh", this, SLOT(addMesh()), Qt::ALT+Qt::Key_M ); + edit->insertItem("Add &Alpha-blended image", this, SLOT(addButterfly()), Qt::ALT+Qt::Key_A); + menu->insertItem("&Edit", edit); + + Q3PopupMenu* view = new Q3PopupMenu( menu ); + view->insertItem("&Enlarge", this, SLOT(enlarge()), Qt::SHIFT+Qt::CTRL+Qt::Key_Plus); + view->insertItem("Shr&ink", this, SLOT(shrink()), Qt::SHIFT+Qt::CTRL+Qt::Key_Minus); + view->insertSeparator(); + view->insertItem("&Rotate clockwise", this, SLOT(rotateClockwise()), Qt::CTRL+Qt::Key_PageDown); + view->insertItem("Rotate &counterclockwise", this, SLOT(rotateCounterClockwise()), Qt::CTRL+Qt::Key_PageUp); + view->insertItem("&Zoom in", this, SLOT(zoomIn()), Qt::CTRL+Qt::Key_Plus); + view->insertItem("Zoom &out", this, SLOT(zoomOut()), Qt::CTRL+Qt::Key_Minus); + view->insertItem("Translate left", this, SLOT(moveL()), Qt::CTRL+Qt::Key_Left); + view->insertItem("Translate right", this, SLOT(moveR()), Qt::CTRL+Qt::Key_Right); + view->insertItem("Translate up", this, SLOT(moveU()), Qt::CTRL+Qt::Key_Up); + view->insertItem("Translate down", this, SLOT(moveD()), Qt::CTRL+Qt::Key_Down); + view->insertItem("&Mirror", this, SLOT(mirror()), Qt::CTRL+Qt::Key_Home); + menu->insertItem("&View", view); + + menu->insertSeparator(); + + Q3PopupMenu* help = new Q3PopupMenu( menu ); + help->insertItem("&About", this, SLOT(help()), Qt::Key_F1); + help->setItemChecked(dbf_id, TRUE); + menu->insertItem("&Help",help); + + statusBar(); + + setCentralWidget(editor); + + printer = 0; + + init(); +} + +void Main::init() +{ + clear(); + + static int r=24; + qsrand(++r); + + mainCount++; + butterflyimg = 0; + logoimg = 0; + + int i; + for ( i=0; i < int(canvas.width()) / 56; i++) { + addButterfly(); + } + for ( i=0; i < int(canvas.width()) / 85; i++) { + addHexagon(); + } + for ( i=0; i < int(canvas.width()) / 128; i++) { + addLogo(); + } +} + +Main::~Main() +{ + delete printer; + if ( !--mainCount ) { + delete[] butterflyimg; + butterflyimg = 0; + delete[] logoimg; + logoimg = 0; + } +} + +void Main::newView() +{ + // Open a new view... have it delete when closed. + Main *m = new Main(canvas, 0, 0, Qt::WDestructiveClose); + m->show(); +} + +void Main::clear() +{ + editor->clear(); +} + +void Main::help() +{ + static QMessageBox* about = new QMessageBox( "Qt Canvas Example", + "<h3>The QCanvas classes example</h3>" + "<ul>" + "<li> Press ALT-S for some sprites." + "<li> Press ALT-C for some circles." + "<li> Press ALT-L for some lines." + "<li> Drag the objects around." + "<li> Read the code!" + "</ul>", QMessageBox::Information, 1, 0, 0, this, 0, FALSE ); + about->setButtonText( 1, "Dismiss" ); + about->show(); +} + +void Main::aboutQt() +{ + QMessageBox::aboutQt( this, "Qt Canvas Example" ); +} + +void Main::enlarge() +{ + canvas.setSceneRect(0, 0, canvas.width()*4/3, canvas.height()*4/3); +} + +void Main::shrink() +{ + canvas.setSceneRect(0, 0, qMax(canvas.width()*3/4, qreal(1.0)), qMax(canvas.height()*3/4, qreal(1.0))); +} + +void Main::rotateClockwise() +{ + editor->rotate( 22.5 ); +} + +void Main::rotateCounterClockwise() +{ + editor->rotate( -22.5 ); +} + +void Main::zoomIn() +{ + editor->scale( 2.0, 2.0 ); +} + +void Main::zoomOut() +{ + editor->scale( 0.5, 0.5 ); +} + +void Main::mirror() +{ + editor->scale( -1, 1 ); +} + +void Main::moveL() +{ + editor->translate( -16, 0 ); +} + +void Main::moveR() +{ + editor->translate( +16, 0 ); +} + +void Main::moveU() +{ + editor->translate( 0, -16 ); +} + +void Main::moveD() +{ + editor->translate( 0, +16 ); +} + +void Main::print() +{ + if ( !printer ) printer = new QPrinter; + if ( printer->setup(this) ) { + QPainter pp(printer); + canvas.render(&pp); + } +} + + +void Main::addSprite() +{ + BouncyLogo* i = new BouncyLogo; + canvas.addItem(i); + i->initPos(); + i->setZValue(qrand()%256); +} + +QString butterfly_fn; +QString logo_fn; + + +void Main::addButterfly() +{ + if ( butterfly_fn.isEmpty() ) + return; + if ( !butterflyimg ) { + butterflyimg = new QImage[4]; + butterflyimg[0].load( butterfly_fn ); + butterflyimg[1] = butterflyimg[0].smoothScale( int(butterflyimg[0].width()*0.75), + int(butterflyimg[0].height()*0.75) ); + butterflyimg[2] = butterflyimg[0].smoothScale( int(butterflyimg[0].width()*0.5), + int(butterflyimg[0].height()*0.5) ); + butterflyimg[3] = butterflyimg[0].smoothScale( int(butterflyimg[0].width()*0.25), + int(butterflyimg[0].height()*0.25) ); + } + QAbstractGraphicsShapeItem* i = new ImageItem(butterflyimg[qrand()%4]); + canvas.addItem(i); + i->setPos(qrand()%int(canvas.width()-butterflyimg->width()), + qrand()%int(canvas.height()-butterflyimg->height())); + i->setZValue(qrand()%256+250); +} + +void Main::addLogo() +{ + if ( logo_fn.isEmpty() ) + return; + if ( !logoimg ) { + logoimg = new QImage[4]; + logoimg[0].load( logo_fn ); + logoimg[1] = logoimg[0].smoothScale( int(logoimg[0].width()*0.75), + int(logoimg[0].height()*0.75) ); + logoimg[2] = logoimg[0].smoothScale( int(logoimg[0].width()*0.5), + int(logoimg[0].height()*0.5) ); + logoimg[3] = logoimg[0].smoothScale( int(logoimg[0].width()*0.25), + int(logoimg[0].height()*0.25) ); + } + QAbstractGraphicsShapeItem* i = new ImageItem(logoimg[qrand()%4]); + canvas.addItem(i); + i->setPos(qrand()%int(canvas.width()-logoimg->width()), + qrand()%int(canvas.height()-logoimg->width())); + i->setZValue(qrand()%256+256); +} + + + +void Main::addCircle() +{ + QAbstractGraphicsShapeItem* i = canvas.addEllipse(QRectF(0,0,50,50)); + i->setFlag(QGraphicsItem::ItemIsMovable); + i->setPen(Qt::NoPen); + i->setBrush( QColor(qrand()%32*8,qrand()%32*8,qrand()%32*8) ); + i->setPos(qrand()%int(canvas.width()),qrand()%int(canvas.height())); + i->setZValue(qrand()%256); +} + +void Main::addHexagon() +{ + const int size = int(canvas.width() / 25); + Q3PointArray pa(6); + pa[0] = QPoint(2*size,0); + pa[1] = QPoint(size,-size*173/100); + pa[2] = QPoint(-size,-size*173/100); + pa[3] = QPoint(-2*size,0); + pa[4] = QPoint(-size,size*173/100); + pa[5] = QPoint(size,size*173/100); + QGraphicsPolygonItem* i = canvas.addPolygon(pa); + i->setFlag(QGraphicsItem::ItemIsMovable); + i->setPen(Qt::NoPen); + i->setBrush( QColor(qrand()%32*8,qrand()%32*8,qrand()%32*8) ); + i->setPos(qrand()%int(canvas.width()),qrand()%int(canvas.height())); + i->setZValue(qrand()%256); +} + +void Main::addPolygon() +{ + const int size = int(canvas.width()/2); + Q3PointArray pa(6); + pa[0] = QPoint(0,0); + pa[1] = QPoint(size,size/5); + pa[2] = QPoint(size*4/5,size); + pa[3] = QPoint(size/6,size*5/4); + pa[4] = QPoint(size*3/4,size*3/4); + pa[5] = QPoint(size*3/4,size/4); + QGraphicsPolygonItem* i = canvas.addPolygon(pa); + i->setFlag(QGraphicsItem::ItemIsMovable); + i->setPen(Qt::NoPen); + i->setBrush( QColor(qrand()%32*8,qrand()%32*8,qrand()%32*8) ); + i->setPos(qrand()%int(canvas.width()),qrand()%int(canvas.height())); + i->setZValue(qrand()%256); +} + +void Main::addSpline() +{ + const int size = int(canvas.width()/6); + + Q3PointArray pa(12); + pa[0] = QPoint(0,0); + pa[1] = QPoint(size/2,0); + pa[2] = QPoint(size,size/2); + pa[3] = QPoint(size,size); + pa[4] = QPoint(size,size*3/2); + pa[5] = QPoint(size/2,size*2); + pa[6] = QPoint(0,size*2); + pa[7] = QPoint(-size/2,size*2); + pa[8] = QPoint(size/4,size*3/2); + pa[9] = QPoint(0,size); + pa[10]= QPoint(-size/4,size/2); + pa[11]= QPoint(-size/2,0); + + QPainterPath path; + path.moveTo(pa[0]); + for (int i = 1; i < pa.size(); i += 3) + path.cubicTo(pa[i], pa[(i + 1) % pa.size()], pa[(i + 2) % pa.size()]); + + QGraphicsPathItem* item = canvas.addPath(path); + item->setFlag(QGraphicsItem::ItemIsMovable); + item->setPen(Qt::NoPen); + item->setBrush( QColor(qrand()%32*8,qrand()%32*8,qrand()%32*8) ); + item->setPos(qrand()%int(canvas.width()),qrand()%int(canvas.height())); + item->setZValue(qrand()%256); +} + +void Main::addText() +{ + QGraphicsTextItem* i = canvas.addText("QCanvasText"); + i->setFlag(QGraphicsItem::ItemIsMovable); + i->setPos(qrand()%int(canvas.width()),qrand()%int(canvas.height())); + i->setZValue(qrand()%256); +} + +void Main::addLine() +{ + QGraphicsLineItem* i = canvas.addLine(QLineF( qrand()%int(canvas.width()), qrand()%int(canvas.height()), + qrand()%int(canvas.width()), qrand()%int(canvas.height()) )); + i->setFlag(QGraphicsItem::ItemIsMovable); + i->setPen( QPen(QColor(qrand()%32*8,qrand()%32*8,qrand()%32*8), 6) ); + i->setZValue(qrand()%256); +} + +void Main::addMesh() +{ + int x0 = 0; + int y0 = 0; + + if ( !tb ) tb = new QBrush( Qt::red ); + if ( !tp ) tp = new QPen( Qt::black ); + + int nodecount = 0; + + int w = int(canvas.width()); + int h = int(canvas.height()); + + const int dist = 30; + int rows = h / dist; + int cols = w / dist; + +#ifndef QT_NO_PROGRESSDIALOG + Q3ProgressDialog progress( "Creating mesh...", "Abort", rows, + this, "progress", TRUE ); +#endif + + canvas.update(); + + Q3MemArray<NodeItem*> lastRow(cols); + for ( int j = 0; j < rows; j++ ) { + int n = j%2 ? cols-1 : cols; + NodeItem *prev = 0; + for ( int i = 0; i < n; i++ ) { + NodeItem *el = new NodeItem; + canvas.addItem(el); + nodecount++; + int r = qrand(); + int xrand = r %20; + int yrand = (r/20) %20; + el->setPos( xrand + x0 + i*dist + (j%2 ? dist/2 : 0 ), + yrand + y0 + j*dist ); + + if ( j > 0 ) { + if ( i < cols-1 ) + canvas.addItem(new EdgeItem( lastRow[i], el)); + if ( j%2 ) + canvas.addItem(new EdgeItem( lastRow[i+1], el)); + else if ( i > 0 ) + canvas.addItem(new EdgeItem( lastRow[i-1], el)); + } + if ( prev ) { + canvas.addItem(new EdgeItem( prev, el)); + } + if ( i > 0 ) lastRow[i-1] = prev; + prev = el; + } + lastRow[n-1]=prev; +#ifndef QT_NO_PROGRESSDIALOG + progress.setProgress( j ); + if ( progress.wasCancelled() ) + break; +#endif + } +#ifndef QT_NO_PROGRESSDIALOG + progress.setProgress( rows ); +#endif + // qDebug( "%d nodes, %d edges", nodecount, EdgeItem::count() ); +} + +void Main::addRectangle() +{ + QAbstractGraphicsShapeItem *i = canvas.addRect( QRectF(qrand()%int(canvas.width()), + qrand()%int(canvas.height()), + canvas.width()/5, + canvas.width()/5) ); + i->setFlag(QGraphicsItem::ItemIsMovable); + int z = qrand()%256; + i->setBrush( QColor(z,z,z) ); + i->setPen( QPen(QColor(qrand()%32*8,qrand()%32*8,qrand()%32*8), 6) ); + i->setZValue(z); +} diff --git a/examples/graphicsview/portedcanvas/canvas.doc b/examples/graphicsview/portedcanvas/canvas.doc new file mode 100644 index 0000000..f6d77ad --- /dev/null +++ b/examples/graphicsview/portedcanvas/canvas.doc @@ -0,0 +1,29 @@ +/*! \page canvas-example.html + + \ingroup examples + \title Canvas Example + + This example shows a QCanvas and some \l{QCanvasItem}s in action. + You can do a lot more with QCanvas than we show here, but the + example provides a taste of what can be done. + + <hr> + + Header file: + + \include canvas/canvas.h + + <hr> + + Implementation: + + \include canvas/canvas.cpp + + <hr> + + Main: + + \include canvas/main.cpp +*/ + + diff --git a/examples/graphicsview/portedcanvas/canvas.h b/examples/graphicsview/portedcanvas/canvas.h new file mode 100644 index 0000000..769edbf --- /dev/null +++ b/examples/graphicsview/portedcanvas/canvas.h @@ -0,0 +1,131 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef EXAMPLE_H +#define EXAMPLE_H + +#include <q3popupmenu.h> +#include <q3mainwindow.h> +#include <q3intdict.h> +#include <QMouseEvent> +#include <QGraphicsView> +#include <QGraphicsScene> +#include <QGraphicsItem> + +class BouncyLogo : public QGraphicsPixmapItem { + void initSpeed(); +public: + BouncyLogo(); + void advance(int); + int type() const; + + QPainterPath shape() const; + + void initPos(); +private: + qreal xvel; + qreal yvel; +}; + + +class FigureEditor : public QGraphicsView { + Q_OBJECT + +public: + FigureEditor(QGraphicsScene&, QWidget* parent=0, const char* name=0, Qt::WindowFlags f=0); + void clear(); + +signals: + void status(const QString&); +}; + +class Main : public Q3MainWindow { + Q_OBJECT + +public: + Main(QGraphicsScene&, QWidget* parent=0, const char* name=0, Qt::WindowFlags f=0); + ~Main(); + +public slots: + void help(); + +private slots: + void aboutQt(); + void newView(); + void clear(); + void init(); + + void addSprite(); + void addCircle(); + void addHexagon(); + void addPolygon(); + void addSpline(); + void addText(); + void addLine(); + void addRectangle(); + void addMesh(); + void addLogo(); + void addButterfly(); + + void enlarge(); + void shrink(); + void rotateClockwise(); + void rotateCounterClockwise(); + void zoomIn(); + void zoomOut(); + void mirror(); + void moveL(); + void moveR(); + void moveU(); + void moveD(); + + void print(); + +private: + QGraphicsScene& canvas; + FigureEditor *editor; + + Q3PopupMenu* options; + QPrinter* printer; + int dbf_id; +}; + +#endif diff --git a/examples/graphicsview/portedcanvas/main.cpp b/examples/graphicsview/portedcanvas/main.cpp new file mode 100644 index 0000000..b68c4cb --- /dev/null +++ b/examples/graphicsview/portedcanvas/main.cpp @@ -0,0 +1,92 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <qstatusbar.h> +#include <qmessagebox.h> +#include <qmenubar.h> +#include <qapplication.h> +#include <qdesktopwidget.h> +#include <qimage.h> +#include <qtimer.h> + +#include "canvas.h" + +#include <stdlib.h> + +extern QString butterfly_fn; +extern QString logo_fn; + +int main(int argc, char** argv) +{ + Q_INIT_RESOURCE(portedcanvas); + + QApplication app(argc,argv); + + if ( argc > 1 ) + butterfly_fn = argv[1]; + else + butterfly_fn = ":/trolltech/examples/graphicsview/portedcanvas/butterfly.png"; + + if ( argc > 2 ) + logo_fn = argv[2]; + else + logo_fn = ":/trolltech/examples/graphicsview/portedcanvas/qtlogo.png"; + + QGraphicsScene canvas; + canvas.setSceneRect(0, 0, 800, 600); + Main m(canvas); + m.resize(m.sizeHint()); + m.setCaption("Ported Canvas Example"); + if ( QApplication::desktop()->width() > m.width() + 10 + && QApplication::desktop()->height() > m.height() +30 ) + m.show(); + else + m.showMaximized(); + + QTimer timer; + QObject::connect(&timer, SIGNAL(timeout()), &canvas, SLOT(advance())); + timer.start(30); + + QObject::connect( qApp, SIGNAL(lastWindowClosed()), qApp, SLOT(quit()) ); + + return app.exec(); +} + diff --git a/examples/graphicsview/portedcanvas/makeimg.cpp b/examples/graphicsview/portedcanvas/makeimg.cpp new file mode 100644 index 0000000..93a8efe --- /dev/null +++ b/examples/graphicsview/portedcanvas/makeimg.cpp @@ -0,0 +1,133 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <qimage.h> +#include <qcolor.h> + +static inline int blendComponent( int v, int av, int s, int as ) +{ + //shadow gets a color inversely proportional to the + //alpha value + s = (s*(255-as))/255; + //then do standard blending + return as*s + av*v -(av*as*s)/255; +} + +static inline QRgb blendShade( QRgb v, QRgb s ) +{ + //pick a number: shadow is 1/3 of object + int as = qAlpha(s)/3; + int av = qAlpha(v); + if ( as == 0 || av == 255 ) + return v; + + int a = as + av -(as*av)/255; + + + int r = blendComponent( qRed(v),av, qRed(s), as)/a; + int g = blendComponent( qGreen(v),av, qGreen(s), as)/a; + int b = blendComponent( qBlue(v),av, qBlue(s), as)/a; + + return qRgba(r,g,b,a); +} + +int main( int*, char**) +{ + QImage *img; + + img = new QImage( "in.png" ); + int w,h; + int y; + img->setAlphaBuffer( TRUE ); + *img = img->convertDepth( 32 ); + w = img->width(); + h = img->height(); +#if 0 + for ( y = 0; y < h; y ++ ) { + uint *line = (uint*)img->scanLine( y ); + for ( int x = 0; x < w; x++ ) { + uint pixel = line[x]; + int r = qRed(pixel); + int g = qGreen(pixel); + int b = qBlue(pixel); + int min = QMIN( r, QMIN( g, b ) ); + int max = QMAX( r, QMAX( g, b ) ); + r -= min; + g -= min; + b -= min; + if ( max !=min ) { + r = (r*255)/(max-min); + g = (g*255)/(max-min); + b = (b*255)/(max-min); + } + int a = 255-min; + a -= (max-min)/3; //hack more transparency for colors. + line[x] = qRgba( r, g, b, a ); + } + } +#endif + *img = img->smoothScale( w/2, h/2 ); + + qDebug( "saving out.png"); + img->save( "out.png", "PNG" ); + + w = img->width(); + h = img->height(); + + QImage *img2 = new QImage( w, h, 32 ); + img2->setAlphaBuffer( TRUE ); + for ( y = 0; y < h; y++ ) { + for ( int x = 0; x < w; x++ ) { + QRgb shader = img->pixel( x, y ); + + int as = qAlpha(shader)/3; + + int r = (qRed(shader)*(255-as))/255; + int g = (qGreen(shader)*(255-as))/255; + int b = (qBlue(shader)*(255-as))/255; + + img2->setPixel( x, y, qRgba(r,g,b,as) ); + } + } + + img2->save( "outshade.png", "PNG" ); + +} diff --git a/examples/graphicsview/portedcanvas/portedcanvas.pro b/examples/graphicsview/portedcanvas/portedcanvas.pro new file mode 100644 index 0000000..71e3d84 --- /dev/null +++ b/examples/graphicsview/portedcanvas/portedcanvas.pro @@ -0,0 +1,16 @@ +TEMPLATE = app +TARGET = portedcanvas + +CONFIG += qt warn_on + +HEADERS = canvas.h +SOURCES = canvas.cpp main.cpp +QT += qt3support + +RESOURCES += portedcanvas.qrc + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/graphicsview/portedcanvas +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS portedcanvas.pro *.png *.xpm *.doc +sources.path = $$[QT_INSTALL_EXAMPLES]/graphicsview/portedcanvas +INSTALLS += target sources diff --git a/examples/graphicsview/portedcanvas/portedcanvas.qrc b/examples/graphicsview/portedcanvas/portedcanvas.qrc new file mode 100644 index 0000000..cf2e024 --- /dev/null +++ b/examples/graphicsview/portedcanvas/portedcanvas.qrc @@ -0,0 +1,7 @@ +<!DOCTYPE RCC><RCC version="1.0"> +<qresource prefix="/trolltech/examples/graphicsview/portedcanvas"> +<file>qt-trans.xpm</file> +<file>butterfly.png</file> +<file>qtlogo.png</file> +</qresource> +</RCC> diff --git a/examples/graphicsview/portedcanvas/qt-trans.xpm b/examples/graphicsview/portedcanvas/qt-trans.xpm new file mode 100644 index 0000000..225f684 --- /dev/null +++ b/examples/graphicsview/portedcanvas/qt-trans.xpm @@ -0,0 +1,54 @@ +/* XPM */ +static char *qtlogo_xpm[] = { +/* width height ncolors chars_per_pixel */ +"54 34 13 1", +/* colors */ +" c #000000", +". c #999999", +"X c #333366", +"o c #6666CC", +"O c #333333", +"@ c #666699", +"# c #000066", +"$ c #666666", +"% c #3333CC", +"& c #000033", +"* c #9999CC", +"= c #333399", +"+ c None", +/* pixels */ +"+++++++++++++++++++.$OOO$.++++++++++++++++++++++++++++", +"+++++++++++++++++$ O.+++++++++++++++++++++++++", +"+++++++++++++++.O $++++++++++++++++++++++++", +"++++++++++++++. $.++.$ O+++++++++++++++++++++++", +"+++++++++++++. O.+++++++$ O++++++++++++++++++++++", +"+++++++++++++O ++++++++++$ $+++++++++++++++++++++", +"++++++++++++$ .+++++++++++O .++++++++++++++++++++", +"+++++++++++. O+++++++++++++ O++++++.+++++++++++++", +"+++++++++++$ .+++++++++++++$ .+++.O +++++++++++++", +"+++++++++++ +++++++++++++++ O+++. +++++++++++++", +"++++++++++. &Xoooo*++++++++++$ +++. +++++++++++++", +"++++++++++@=%%%%%%%%%%*+++++++. .++. +++++++++++++", +"+++++++**oooooo**++*o%%%%o+++++ $++O +++++++++++++", +"+++++*****$OOX@oooo*++*%%%%%*++O $+. OOO$++++++++*", +"++..+.++....$O$+*ooooo*+*o%%%%%O O+$ $$O.++++++**+", +"***+*+++++$$....+++*oooo**+*o%%# O++O ++++++***o*++", +"*+++++++++O $.....++**oooo**+*X &o*O ++++*ooo*++++", +"++++++++++$ O++.....++**oooo*X &%%& ..*o%%*++++++", +"++++++++++$ ++++.....+++**ooO $*o& @oo*+++++++++", +"++++++++++. .++++++.....+++*O Xo*O .++++++++++++", +"+++++++++++ O+++++++++...... .++O +++++++++++++", +"+++++++++++O +++.$$$.++++++. O+++O +++++++++++++", +"+++++++++++. $$OO O.++++O .+++O +++++++++++++", +"++++++++++++O .+++.O $++. O++++O +++++++++++++", +"++++++++++++. O+++++O $+O +++++O +++++++++++++", +"+++++++++++++. O+++++O O .+++++O .++++++++++++", +"++++++++++++++$ .++++O .++++.+$ O+.$.++++++++", +"+++++++++++++++. O$$O .+++++... +++++++++", +"+++++++++++++++++$ O+++++$$+.O O$.+++++++++", +"+++++++++++++++++++$OO O$.O O.++. .++++++++++++++++", +"+++++++++++++++++++++++++++. OO .++++++++++++++++", +"++++++++++++++++++++++++++++. O+++++++++++++++++", +"+++++++++++++++++++++++++++++. .+++++++++++++++++", +"++++++++++++++++++++++++++++++.O O.++++++++++++++++++" +}; diff --git a/examples/graphicsview/portedcanvas/qtlogo.png b/examples/graphicsview/portedcanvas/qtlogo.png Binary files differnew file mode 100644 index 0000000..5aac08a --- /dev/null +++ b/examples/graphicsview/portedcanvas/qtlogo.png |