From 7fe9bdb1be6b20c351ff0bd34aff2b1cc8b50323 Mon Sep 17 00:00:00 2001 From: David Boddie Date: Thu, 26 Mar 2009 16:49:43 +0100 Subject: Squashed commit of the following: commit 8af45bfd7efe647963bcedf1736b62ee8568590d Author: David Boddie Date: Thu Mar 26 16:39:31 2009 +0100 Doc: Committed a stray change. Reviewed-by: David Boddie commit 2aa564839e6a4821e9419230a1dd13ef691c679d Merge: f9e590a... db3c3ca... Author: David Boddie Date: Thu Mar 26 16:38:53 2009 +0100 Merge branch '4.5' of ../qt-45 into qt/4.5 commit f9e590a4ef3e134cf2a1606a6fe3307df6935e96 Author: David Boddie Date: Thu Mar 26 16:37:02 2009 +0100 Doc: Simplified the SVG Generator example and updated the list of examples. Committing before merging into the 4.5 branch. Reviewed-by: David Boddie commit ba35080c7d728b9e47ac68e74fe62877285c400f Author: David Boddie Date: Thu Mar 26 14:29:13 2009 +0100 Doc: Added a SVG generator example. This example shows how to paint on an QSvgGenerator device. Reviewed-by: David Boddie --- examples/painting/svggenerator/displaywidget.cpp | 149 ++++++++++++ examples/painting/svggenerator/displaywidget.h | 80 +++++++ examples/painting/svggenerator/forms/window.ui | 249 +++++++++++++++++++++ examples/painting/svggenerator/main.cpp | 52 +++++ .../painting/svggenerator/resources/shapes.dat | Bin 0 -> 2088 bytes examples/painting/svggenerator/svggenerator.pro | 15 ++ examples/painting/svggenerator/window.cpp | 101 +++++++++ examples/painting/svggenerator/window.h | 66 ++++++ 8 files changed, 712 insertions(+) create mode 100644 examples/painting/svggenerator/displaywidget.cpp create mode 100644 examples/painting/svggenerator/displaywidget.h create mode 100644 examples/painting/svggenerator/forms/window.ui create mode 100644 examples/painting/svggenerator/main.cpp create mode 100644 examples/painting/svggenerator/resources/shapes.dat create mode 100644 examples/painting/svggenerator/svggenerator.pro create mode 100644 examples/painting/svggenerator/window.cpp create mode 100644 examples/painting/svggenerator/window.h (limited to 'examples/painting') diff --git a/examples/painting/svggenerator/displaywidget.cpp b/examples/painting/svggenerator/displaywidget.cpp new file mode 100644 index 0000000000..c9733416fb --- /dev/null +++ b/examples/painting/svggenerator/displaywidget.cpp @@ -0,0 +1,149 @@ +/**************************************************************************** +** +** 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 +#include "displaywidget.h" + +DisplayWidget::DisplayWidget(QWidget *parent) + : QWidget(parent) +{ + QPainterPath car; + QPainterPath house; + + QFile file(":resources/shapes.dat"); + file.open(QFile::ReadOnly); + QDataStream stream(&file); + stream >> car >> house >> tree >> moon; + file.close(); + + shapeMap[Car] = car; + shapeMap[House] = house; + + background = Sky; + shapeColor = Qt::darkYellow; + shape = House; +} + +//! [paint event] +void DisplayWidget::paintEvent(QPaintEvent *event) +{ + QPainter painter; + painter.begin(this); + painter.setRenderHint(QPainter::Antialiasing); + paint(painter); + painter.end(); +} +//! [paint event] + +//! [paint function] +void DisplayWidget::paint(QPainter &painter) +{ +//![paint picture] + painter.setClipRect(QRect(0, 0, 200, 200)); + painter.setPen(Qt::NoPen); + + switch (background) { + case Sky: + default: + painter.fillRect(QRect(0, 0, 200, 200), Qt::darkBlue); + painter.translate(145, 10); + painter.setBrush(Qt::white); + painter.drawPath(moon); + painter.translate(-145, -10); + break; + case Trees: + { + painter.fillRect(QRect(0, 0, 200, 200), Qt::darkGreen); + painter.setBrush(Qt::green); + painter.setPen(Qt::black); + for (int y = -55, row = 0; y < 200; y += 50, ++row) { + int xs; + if (row == 2 || row == 3) + xs = 150; + else + xs = 50; + for (int x = 0; x < 200; x += xs) { + painter.save(); + painter.translate(x, y); + painter.drawPath(tree); + painter.restore(); + } + } + break; + } + case Road: + painter.fillRect(QRect(0, 0, 200, 200), Qt::gray); + painter.setPen(QPen(Qt::white, 4, Qt::DashLine)); + painter.drawLine(QLine(0, 35, 200, 35)); + painter.drawLine(QLine(0, 165, 200, 165)); + break; + } + + painter.setBrush(shapeColor); + painter.setPen(Qt::black); + painter.translate(100, 100); + painter.drawPath(shapeMap[shape]); +//![paint picture] +} +//! [paint function] + +QColor DisplayWidget::color() const +{ + return shapeColor; +} + +void DisplayWidget::setBackground(Background background) +{ + this->background = background; + update(); +} + +void DisplayWidget::setColor(const QColor &color) +{ + this->shapeColor = color; + update(); +} + +void DisplayWidget::setShape(Shape shape) +{ + this->shape = shape; + update(); +} diff --git a/examples/painting/svggenerator/displaywidget.h b/examples/painting/svggenerator/displaywidget.h new file mode 100644 index 0000000000..58ad97e324 --- /dev/null +++ b/examples/painting/svggenerator/displaywidget.h @@ -0,0 +1,80 @@ +/**************************************************************************** +** +** 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 DISPLAYWIDGET_H +#define DISPLAYWIDGET_H + +#include +#include +#include + +//! [DisplayWidget class definition] +class DisplayWidget : public QWidget +{ + Q_OBJECT + +public: + enum Shape { House = 0, Car = 1 }; + enum Background { Sky = 0, Trees = 1, Road = 2 }; + + DisplayWidget(QWidget *parent = 0); + QColor color() const; + void paint(QPainter &painter); + +public slots: + void setBackground(Background background); + void setColor(const QColor &color); + void setShape(Shape shape); + +protected: + void paintEvent(QPaintEvent *event); + +private: + Background background; + QColor shapeColor; + Shape shape; + QHash shapeMap; + QPainterPath moon; + QPainterPath tree; +}; +//! [DisplayWidget class definition] + +#endif diff --git a/examples/painting/svggenerator/forms/window.ui b/examples/painting/svggenerator/forms/window.ui new file mode 100644 index 0000000000..bf11908c6c --- /dev/null +++ b/examples/painting/svggenerator/forms/window.ui @@ -0,0 +1,249 @@ + + + Window + + + + 0 + 0 + 339 + 353 + + + + SVG Generator + + + + QLayout::SetFixedSize + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + 200 + 200 + + + + + 200 + 200 + + + + + 200 + 200 + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + &Shape: + + + shapeComboBox + + + + + + + + House + + + + + Car + + + + + + + + &Color: + + + colorButton + + + + + + + Choose Color... + + + + + + + &Background: + + + shapeComboBox_2 + + + + + + + + Sky + + + + + Trees + + + + + Road + + + + + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + Save &As... + + + + + + + + + + DisplayWidget + QWidget +
displaywidget.h
+ 1 +
+
+ + + + shapeComboBox + currentIndexChanged(int) + Window + updateShape(int) + + + 288 + 232 + + + 336 + 234 + + + + + colorButton + clicked() + Window + updateColor() + + + 301 + 262 + + + 337 + 267 + + + + + shapeComboBox_2 + currentIndexChanged(int) + Window + updateBackground(int) + + + 306 + 299 + + + 337 + 311 + + + + + toolButton_2 + clicked() + Window + saveSvg() + + + 298 + 336 + + + 307 + 348 + + + + + + updateBackground(int) + updateColor() + updateShape(int) + saveSvg() + +
diff --git a/examples/painting/svggenerator/main.cpp b/examples/painting/svggenerator/main.cpp new file mode 100644 index 0000000000..fa8b0abb9d --- /dev/null +++ b/examples/painting/svggenerator/main.cpp @@ -0,0 +1,52 @@ +/**************************************************************************** +** +** 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 + +#include "window.h" + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + Window window; + window.show(); + return app.exec(); +} diff --git a/examples/painting/svggenerator/resources/shapes.dat b/examples/painting/svggenerator/resources/shapes.dat new file mode 100644 index 0000000000..d9b981ea58 Binary files /dev/null and b/examples/painting/svggenerator/resources/shapes.dat differ diff --git a/examples/painting/svggenerator/svggenerator.pro b/examples/painting/svggenerator/svggenerator.pro new file mode 100644 index 0000000000..4d08ba4e53 --- /dev/null +++ b/examples/painting/svggenerator/svggenerator.pro @@ -0,0 +1,15 @@ +FORMS = forms/window.ui +HEADERS = displaywidget.h \ + window.h +RESOURCES = svggenerator.qrc +SOURCES = displaywidget.cpp \ + main.cpp \ + window.cpp + +QT += svg + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/painting/svggenerator +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS svggenerator.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/painting/svggenerator +INSTALLS += target sources diff --git a/examples/painting/svggenerator/window.cpp b/examples/painting/svggenerator/window.cpp new file mode 100644 index 0000000000..1965604f04 --- /dev/null +++ b/examples/painting/svggenerator/window.cpp @@ -0,0 +1,101 @@ +/**************************************************************************** +** +** 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 +#include +#include +#include +#include "window.h" +#include "displaywidget.h" + +Window::Window(QWidget *parent) + : QWidget(parent) +{ + setupUi(this); +} + +void Window::updateBackground(int background) +{ + displayWidget->setBackground(DisplayWidget::Background(background)); +} + +void Window::updateColor() +{ + QColor color = QColorDialog::getColor(displayWidget->color()); + if (color.isValid()) + displayWidget->setColor(color); +} + +void Window::updateShape(int shape) +{ + displayWidget->setShape(DisplayWidget::Shape(shape)); +} + +//! [save SVG] +void Window::saveSvg() +{ + QString newPath = QFileDialog::getSaveFileName(this, tr("Save SVG"), + path, tr("SVG files (*.svg)")); + + if (newPath.isEmpty()) + return; + + path = newPath; + +//![configure SVG generator] + QSvgGenerator generator; + generator.setFileName(path); + generator.setSize(QSize(200, 200)); + generator.setViewBox(QRect(0, 0, 200, 200)); + generator.setTitle(tr("SVG Generator Example Drawing")); + generator.setDescription(tr("An SVG drawing created by the SVG Generator " + "Example provided with Qt.")); +//![configure SVG generator] +//![begin painting] + QPainter painter; + painter.begin(&generator); +//![begin painting] + displayWidget->paint(painter); +//![end painting] + painter.end(); +//![end painting] +} +//! [save SVG] diff --git a/examples/painting/svggenerator/window.h b/examples/painting/svggenerator/window.h new file mode 100644 index 0000000000..4bc4d22287 --- /dev/null +++ b/examples/painting/svggenerator/window.h @@ -0,0 +1,66 @@ +/**************************************************************************** +** +** 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 "ui_window.h" + +//! [Window class definition] +class Window : public QWidget, private Ui::Window +{ + Q_OBJECT + +public: + Window(QWidget *parent = 0); + +public slots: + void saveSvg(); + void updateBackground(int background); + void updateColor(); + void updateShape(int shape); + +private: + QString path; +}; +//! [Window class definition] + +#endif -- cgit v1.2.3