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/painting | |
download | Qt-e5fcad302d86d316390c6b0f62759a067313e8a9.zip Qt-e5fcad302d86d316390c6b0f62759a067313e8a9.tar.gz Qt-e5fcad302d86d316390c6b0f62759a067313e8a9.tar.bz2 |
Long live Qt 4.5!
Diffstat (limited to 'examples/painting')
53 files changed, 4448 insertions, 0 deletions
diff --git a/examples/painting/README b/examples/painting/README new file mode 100644 index 0000000..4f9dca0 --- /dev/null +++ b/examples/painting/README @@ -0,0 +1,42 @@ +Qt's painting system is able to render vector graphics, images, and outline +font-based text with sub-pixel accuracy accuracy using anti-aliasing to +improve rendering quality. + +These examples show the most common techniques that are used when painting +with Qt, from basic concepts such as drawing simple primitives to the use of +transformations. + + +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/painting/basicdrawing/basicdrawing.pro b/examples/painting/basicdrawing/basicdrawing.pro new file mode 100644 index 0000000..4011260 --- /dev/null +++ b/examples/painting/basicdrawing/basicdrawing.pro @@ -0,0 +1,12 @@ +HEADERS = renderarea.h \ + window.h +SOURCES = main.cpp \ + renderarea.cpp \ + window.cpp +RESOURCES = basicdrawing.qrc + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/painting/basicdrawing +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS basicdrawing.pro images +sources.path = $$[QT_INSTALL_EXAMPLES]/painting/basicdrawing +INSTALLS += target sources diff --git a/examples/painting/basicdrawing/basicdrawing.qrc b/examples/painting/basicdrawing/basicdrawing.qrc new file mode 100644 index 0000000..9d8a23a --- /dev/null +++ b/examples/painting/basicdrawing/basicdrawing.qrc @@ -0,0 +1,6 @@ +<!DOCTYPE RCC><RCC version="1.0"> +<qresource> + <file>images/brick.png</file> + <file>images/qt-logo.png</file> +</qresource> +</RCC> diff --git a/examples/painting/basicdrawing/images/brick.png b/examples/painting/basicdrawing/images/brick.png Binary files differnew file mode 100644 index 0000000..87e7df5 --- /dev/null +++ b/examples/painting/basicdrawing/images/brick.png diff --git a/examples/painting/basicdrawing/images/qt-logo.png b/examples/painting/basicdrawing/images/qt-logo.png Binary files differnew file mode 100644 index 0000000..a8b452e --- /dev/null +++ b/examples/painting/basicdrawing/images/qt-logo.png diff --git a/examples/painting/basicdrawing/main.cpp b/examples/painting/basicdrawing/main.cpp new file mode 100644 index 0000000..e7c3da4 --- /dev/null +++ b/examples/painting/basicdrawing/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 <QApplication> + +#include "window.h" + +int main(int argc, char *argv[]) +{ + Q_INIT_RESOURCE(basicdrawing); + + QApplication app(argc, argv); + Window window; + window.show(); + return app.exec(); +} diff --git a/examples/painting/basicdrawing/renderarea.cpp b/examples/painting/basicdrawing/renderarea.cpp new file mode 100644 index 0000000..22e511f --- /dev/null +++ b/examples/painting/basicdrawing/renderarea.cpp @@ -0,0 +1,209 @@ +/**************************************************************************** +** +** 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 "renderarea.h" + +//! [0] +RenderArea::RenderArea(QWidget *parent) + : QWidget(parent) +{ + shape = Polygon; + antialiased = false; + transformed = false; + pixmap.load(":/images/qt-logo.png"); + + setBackgroundRole(QPalette::Base); + setAutoFillBackground(true); +} +//! [0] + +//! [1] +QSize RenderArea::minimumSizeHint() const +{ + return QSize(100, 100); +} +//! [1] + +//! [2] +QSize RenderArea::sizeHint() const +{ + return QSize(400, 200); +} +//! [2] + +//! [3] +void RenderArea::setShape(Shape shape) +{ + this->shape = shape; + update(); +} +//! [3] + +//! [4] +void RenderArea::setPen(const QPen &pen) +{ + this->pen = pen; + update(); +} +//! [4] + +//! [5] +void RenderArea::setBrush(const QBrush &brush) +{ + this->brush = brush; + update(); +} +//! [5] + +//! [6] +void RenderArea::setAntialiased(bool antialiased) +{ + this->antialiased = antialiased; + update(); +} +//! [6] + +//! [7] +void RenderArea::setTransformed(bool transformed) +{ + this->transformed = transformed; + update(); +} +//! [7] + +//! [8] +void RenderArea::paintEvent(QPaintEvent * /* event */) +{ + static const QPoint points[4] = { + QPoint(10, 80), + QPoint(20, 10), + QPoint(80, 30), + QPoint(90, 70) + }; + + QRect rect(10, 20, 80, 60); + + QPainterPath path; + path.moveTo(20, 80); + path.lineTo(20, 30); + path.cubicTo(80, 0, 50, 50, 80, 80); + + int startAngle = 20 * 16; + int arcLength = 120 * 16; +//! [8] + +//! [9] + QPainter painter(this); + painter.setPen(pen); + painter.setBrush(brush); + if (antialiased) { + painter.setRenderHint(QPainter::Antialiasing, true); +//! [9] + painter.translate(+0.5, +0.5); + } + +//! [10] + for (int x = 0; x < width(); x += 100) { + for (int y = 0; y < height(); y += 100) { + painter.save(); + painter.translate(x, y); +//! [10] //! [11] + if (transformed) { + painter.translate(50, 50); + painter.rotate(60.0); + painter.scale(0.6, 0.9); + painter.translate(-50, -50); + } +//! [11] + +//! [12] + switch (shape) { + case Line: + painter.drawLine(rect.bottomLeft(), rect.topRight()); + break; + case Points: + painter.drawPoints(points, 4); + break; + case Polyline: + painter.drawPolyline(points, 4); + break; + case Polygon: + painter.drawPolygon(points, 4); + break; + case Rect: + painter.drawRect(rect); + break; + case RoundedRect: + painter.drawRoundedRect(rect, 25, 25, Qt::RelativeSize); + break; + case Ellipse: + painter.drawEllipse(rect); + break; + case Arc: + painter.drawArc(rect, startAngle, arcLength); + break; + case Chord: + painter.drawChord(rect, startAngle, arcLength); + break; + case Pie: + painter.drawPie(rect, startAngle, arcLength); + break; + case Path: + painter.drawPath(path); + break; + case Text: + painter.drawText(rect, Qt::AlignCenter, tr("Qt by\nQt Software")); + break; + case Pixmap: + painter.drawPixmap(10, 10, pixmap); + } +//! [12] //! [13] + painter.restore(); + } + } + + painter.setPen(palette().dark().color()); + painter.setBrush(Qt::NoBrush); + painter.drawRect(QRect(0, 0, width() - 1, height() - 1)); +} +//! [13] diff --git a/examples/painting/basicdrawing/renderarea.h b/examples/painting/basicdrawing/renderarea.h new file mode 100644 index 0000000..f59e843 --- /dev/null +++ b/examples/painting/basicdrawing/renderarea.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 RENDERAREA_H +#define RENDERAREA_H + +#include <QBrush> +#include <QPen> +#include <QPixmap> +#include <QWidget> + +//! [0] +class RenderArea : public QWidget +{ + Q_OBJECT + +public: + enum Shape { Line, Points, Polyline, Polygon, Rect, RoundedRect, Ellipse, Arc, + Chord, Pie, Path, Text, Pixmap }; + + RenderArea(QWidget *parent = 0); + + QSize minimumSizeHint() const; + QSize sizeHint() const; + +public slots: + void setShape(Shape shape); + void setPen(const QPen &pen); + void setBrush(const QBrush &brush); + void setAntialiased(bool antialiased); + void setTransformed(bool transformed); + +protected: + void paintEvent(QPaintEvent *event); + +private: + Shape shape; + QPen pen; + QBrush brush; + bool antialiased; + bool transformed; + QPixmap pixmap; +}; +//! [0] + +#endif diff --git a/examples/painting/basicdrawing/window.cpp b/examples/painting/basicdrawing/window.cpp new file mode 100644 index 0000000..8640162 --- /dev/null +++ b/examples/painting/basicdrawing/window.cpp @@ -0,0 +1,262 @@ +/**************************************************************************** +** +** 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 "renderarea.h" +#include "window.h" + +//! [0] +const int IdRole = Qt::UserRole; +//! [0] + +//! [1] +Window::Window() +{ + renderArea = new RenderArea; + + shapeComboBox = new QComboBox; + shapeComboBox->addItem(tr("Polygon"), RenderArea::Polygon); + shapeComboBox->addItem(tr("Rectangle"), RenderArea::Rect); + shapeComboBox->addItem(tr("Rounded Rectangle"), RenderArea::RoundedRect); + shapeComboBox->addItem(tr("Ellipse"), RenderArea::Ellipse); + shapeComboBox->addItem(tr("Pie"), RenderArea::Pie); + shapeComboBox->addItem(tr("Chord"), RenderArea::Chord); + shapeComboBox->addItem(tr("Path"), RenderArea::Path); + shapeComboBox->addItem(tr("Line"), RenderArea::Line); + shapeComboBox->addItem(tr("Polyline"), RenderArea::Polyline); + shapeComboBox->addItem(tr("Arc"), RenderArea::Arc); + shapeComboBox->addItem(tr("Points"), RenderArea::Points); + shapeComboBox->addItem(tr("Text"), RenderArea::Text); + shapeComboBox->addItem(tr("Pixmap"), RenderArea::Pixmap); + + shapeLabel = new QLabel(tr("&Shape:")); + shapeLabel->setBuddy(shapeComboBox); +//! [1] + +//! [2] + penWidthSpinBox = new QSpinBox; + penWidthSpinBox->setRange(0, 20); + penWidthSpinBox->setSpecialValueText(tr("0 (cosmetic pen)")); + + penWidthLabel = new QLabel(tr("Pen &Width:")); + penWidthLabel->setBuddy(penWidthSpinBox); +//! [2] + +//! [3] + penStyleComboBox = new QComboBox; + penStyleComboBox->addItem(tr("Solid"), Qt::SolidLine); + penStyleComboBox->addItem(tr("Dash"), Qt::DashLine); + penStyleComboBox->addItem(tr("Dot"), Qt::DotLine); + penStyleComboBox->addItem(tr("Dash Dot"), Qt::DashDotLine); + penStyleComboBox->addItem(tr("Dash Dot Dot"), Qt::DashDotDotLine); + penStyleComboBox->addItem(tr("None"), Qt::NoPen); + + penStyleLabel = new QLabel(tr("&Pen Style:")); + penStyleLabel->setBuddy(penStyleComboBox); + + penCapComboBox = new QComboBox; + penCapComboBox->addItem(tr("Flat"), Qt::FlatCap); + penCapComboBox->addItem(tr("Square"), Qt::SquareCap); + penCapComboBox->addItem(tr("Round"), Qt::RoundCap); + + penCapLabel = new QLabel(tr("Pen &Cap:")); + penCapLabel->setBuddy(penCapComboBox); + + penJoinComboBox = new QComboBox; + penJoinComboBox->addItem(tr("Miter"), Qt::MiterJoin); + penJoinComboBox->addItem(tr("Bevel"), Qt::BevelJoin); + penJoinComboBox->addItem(tr("Round"), Qt::RoundJoin); + + penJoinLabel = new QLabel(tr("Pen &Join:")); + penJoinLabel->setBuddy(penJoinComboBox); +//! [3] + +//! [4] + brushStyleComboBox = new QComboBox; + brushStyleComboBox->addItem(tr("Linear Gradient"), + Qt::LinearGradientPattern); + brushStyleComboBox->addItem(tr("Radial Gradient"), + Qt::RadialGradientPattern); + brushStyleComboBox->addItem(tr("Conical Gradient"), + Qt::ConicalGradientPattern); + brushStyleComboBox->addItem(tr("Texture"), Qt::TexturePattern); + brushStyleComboBox->addItem(tr("Solid"), Qt::SolidPattern); + brushStyleComboBox->addItem(tr("Horizontal"), Qt::HorPattern); + brushStyleComboBox->addItem(tr("Vertical"), Qt::VerPattern); + brushStyleComboBox->addItem(tr("Cross"), Qt::CrossPattern); + brushStyleComboBox->addItem(tr("Backward Diagonal"), Qt::BDiagPattern); + brushStyleComboBox->addItem(tr("Forward Diagonal"), Qt::FDiagPattern); + brushStyleComboBox->addItem(tr("Diagonal Cross"), Qt::DiagCrossPattern); + brushStyleComboBox->addItem(tr("Dense 1"), Qt::Dense1Pattern); + brushStyleComboBox->addItem(tr("Dense 2"), Qt::Dense2Pattern); + brushStyleComboBox->addItem(tr("Dense 3"), Qt::Dense3Pattern); + brushStyleComboBox->addItem(tr("Dense 4"), Qt::Dense4Pattern); + brushStyleComboBox->addItem(tr("Dense 5"), Qt::Dense5Pattern); + brushStyleComboBox->addItem(tr("Dense 6"), Qt::Dense6Pattern); + brushStyleComboBox->addItem(tr("Dense 7"), Qt::Dense7Pattern); + brushStyleComboBox->addItem(tr("None"), Qt::NoBrush); + + brushStyleLabel = new QLabel(tr("&Brush Style:")); + brushStyleLabel->setBuddy(brushStyleComboBox); +//! [4] + +//! [5] + otherOptionsLabel = new QLabel(tr("Other Options:")); +//! [5] //! [6] + antialiasingCheckBox = new QCheckBox(tr("&Antialiasing")); +//! [6] //! [7] + transformationsCheckBox = new QCheckBox(tr("&Transformations")); +//! [7] + +//! [8] + connect(shapeComboBox, SIGNAL(activated(int)), + this, SLOT(shapeChanged())); + connect(penWidthSpinBox, SIGNAL(valueChanged(int)), + this, SLOT(penChanged())); + connect(penStyleComboBox, SIGNAL(activated(int)), + this, SLOT(penChanged())); + connect(penCapComboBox, SIGNAL(activated(int)), + this, SLOT(penChanged())); + connect(penJoinComboBox, SIGNAL(activated(int)), + this, SLOT(penChanged())); + connect(brushStyleComboBox, SIGNAL(activated(int)), + this, SLOT(brushChanged())); + connect(antialiasingCheckBox, SIGNAL(toggled(bool)), + renderArea, SLOT(setAntialiased(bool))); + connect(transformationsCheckBox, SIGNAL(toggled(bool)), + renderArea, SLOT(setTransformed(bool))); +//! [8] + +//! [9] + QGridLayout *mainLayout = new QGridLayout; +//! [9] //! [10] + mainLayout->setColumnStretch(0, 1); + mainLayout->setColumnStretch(3, 1); + mainLayout->addWidget(renderArea, 0, 0, 1, 4); + mainLayout->setRowMinimumHeight(1, 6); + mainLayout->addWidget(shapeLabel, 2, 1, Qt::AlignRight); + mainLayout->addWidget(shapeComboBox, 2, 2); + mainLayout->addWidget(penWidthLabel, 3, 1, Qt::AlignRight); + mainLayout->addWidget(penWidthSpinBox, 3, 2); + mainLayout->addWidget(penStyleLabel, 4, 1, Qt::AlignRight); + mainLayout->addWidget(penStyleComboBox, 4, 2); + mainLayout->addWidget(penCapLabel, 5, 1, Qt::AlignRight); + mainLayout->addWidget(penCapComboBox, 5, 2); + mainLayout->addWidget(penJoinLabel, 6, 1, Qt::AlignRight); + mainLayout->addWidget(penJoinComboBox, 6, 2); + mainLayout->addWidget(brushStyleLabel, 7, 1, Qt::AlignRight); + mainLayout->addWidget(brushStyleComboBox, 7, 2); + mainLayout->setRowMinimumHeight(8, 6); + mainLayout->addWidget(otherOptionsLabel, 9, 1, Qt::AlignRight); + mainLayout->addWidget(antialiasingCheckBox, 9, 2); + mainLayout->addWidget(transformationsCheckBox, 10, 2); + setLayout(mainLayout); + + shapeChanged(); + penChanged(); + brushChanged(); + antialiasingCheckBox->setChecked(true); + + setWindowTitle(tr("Basic Drawing")); +} +//! [10] + +//! [11] +void Window::shapeChanged() +{ + RenderArea::Shape shape = RenderArea::Shape(shapeComboBox->itemData( + shapeComboBox->currentIndex(), IdRole).toInt()); + renderArea->setShape(shape); +} +//! [11] + +//! [12] +void Window::penChanged() +{ + int width = penWidthSpinBox->value(); + Qt::PenStyle style = Qt::PenStyle(penStyleComboBox->itemData( + penStyleComboBox->currentIndex(), IdRole).toInt()); + Qt::PenCapStyle cap = Qt::PenCapStyle(penCapComboBox->itemData( + penCapComboBox->currentIndex(), IdRole).toInt()); + Qt::PenJoinStyle join = Qt::PenJoinStyle(penJoinComboBox->itemData( + penJoinComboBox->currentIndex(), IdRole).toInt()); + + renderArea->setPen(QPen(Qt::blue, width, style, cap, join)); +} +//! [12] + +//! [13] +void Window::brushChanged() +{ + Qt::BrushStyle style = Qt::BrushStyle(brushStyleComboBox->itemData( +//! [13] + brushStyleComboBox->currentIndex(), IdRole).toInt()); + +//! [14] + if (style == Qt::LinearGradientPattern) { + QLinearGradient linearGradient(0, 0, 100, 100); + linearGradient.setColorAt(0.0, Qt::white); + linearGradient.setColorAt(0.2, Qt::green); + linearGradient.setColorAt(1.0, Qt::black); + renderArea->setBrush(linearGradient); +//! [14] //! [15] + } else if (style == Qt::RadialGradientPattern) { + QRadialGradient radialGradient(50, 50, 50, 70, 70); + radialGradient.setColorAt(0.0, Qt::white); + radialGradient.setColorAt(0.2, Qt::green); + radialGradient.setColorAt(1.0, Qt::black); + renderArea->setBrush(radialGradient); + } else if (style == Qt::ConicalGradientPattern) { + QConicalGradient conicalGradient(50, 50, 150); + conicalGradient.setColorAt(0.0, Qt::white); + conicalGradient.setColorAt(0.2, Qt::green); + conicalGradient.setColorAt(1.0, Qt::black); + renderArea->setBrush(conicalGradient); +//! [15] //! [16] + } else if (style == Qt::TexturePattern) { + renderArea->setBrush(QBrush(QPixmap(":/images/brick.png"))); +//! [16] //! [17] + } else { + renderArea->setBrush(QBrush(Qt::green, style)); + } +} +//! [17] diff --git a/examples/painting/basicdrawing/window.h b/examples/painting/basicdrawing/window.h new file mode 100644 index 0000000..7c42aef --- /dev/null +++ b/examples/painting/basicdrawing/window.h @@ -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$ +** +****************************************************************************/ + +#ifndef WINDOW_H +#define WINDOW_H + +#include <QWidget> + +QT_BEGIN_NAMESPACE +class QCheckBox; +class QComboBox; +class QLabel; +class QSpinBox; +QT_END_NAMESPACE +class RenderArea; + +//! [0] +class Window : public QWidget +{ + Q_OBJECT + +public: + Window(); + +private slots: + void shapeChanged(); + void penChanged(); + void brushChanged(); + +private: + RenderArea *renderArea; + QLabel *shapeLabel; + QLabel *penWidthLabel; + QLabel *penStyleLabel; + QLabel *penCapLabel; + QLabel *penJoinLabel; + QLabel *brushStyleLabel; + QLabel *otherOptionsLabel; + QComboBox *shapeComboBox; + QSpinBox *penWidthSpinBox; + QComboBox *penStyleComboBox; + QComboBox *penCapComboBox; + QComboBox *penJoinComboBox; + QComboBox *brushStyleComboBox; + QCheckBox *antialiasingCheckBox; + QCheckBox *transformationsCheckBox; +}; +//! [0] + +#endif diff --git a/examples/painting/concentriccircles/circlewidget.cpp b/examples/painting/concentriccircles/circlewidget.cpp new file mode 100644 index 0000000..ab62a88 --- /dev/null +++ b/examples/painting/concentriccircles/circlewidget.cpp @@ -0,0 +1,125 @@ +/**************************************************************************** +** +** 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 "circlewidget.h" + +#include <stdlib.h> + +//! [0] +CircleWidget::CircleWidget(QWidget *parent) + : QWidget(parent) +{ + floatBased = false; + antialiased = false; + frameNo = 0; + + setBackgroundRole(QPalette::Base); + setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); +} +//! [0] + +//! [1] +void CircleWidget::setFloatBased(bool floatBased) +{ + this->floatBased = floatBased; + update(); +} +//! [1] + +//! [2] +void CircleWidget::setAntialiased(bool antialiased) +{ + this->antialiased = antialiased; + update(); +} +//! [2] + +//! [3] +QSize CircleWidget::minimumSizeHint() const +{ + return QSize(50, 50); +} +//! [3] + +//! [4] +QSize CircleWidget::sizeHint() const +{ + return QSize(180, 180); +} +//! [4] + +//! [5] +void CircleWidget::nextAnimationFrame() +{ + ++frameNo; + update(); +} +//! [5] + +//! [6] +void CircleWidget::paintEvent(QPaintEvent *) +{ + QPainter painter(this); + painter.setRenderHint(QPainter::Antialiasing, antialiased); + painter.translate(width() / 2, height() / 2); +//! [6] + +//! [7] + for (int diameter = 0; diameter < 256; diameter += 9) { + int delta = abs((frameNo % 128) - diameter / 2); + int alpha = 255 - (delta * delta) / 4 - diameter; +//! [7] //! [8] + if (alpha > 0) { + painter.setPen(QPen(QColor(0, diameter / 2, 127, alpha), 3)); + + if (floatBased) { + painter.drawEllipse(QRectF(-diameter / 2.0, -diameter / 2.0, + diameter, diameter)); + } else { + painter.drawEllipse(QRect(-diameter / 2, -diameter / 2, + diameter, diameter)); + } + } + } +} +//! [8] diff --git a/examples/painting/concentriccircles/circlewidget.h b/examples/painting/concentriccircles/circlewidget.h new file mode 100644 index 0000000..c5acaa8 --- /dev/null +++ b/examples/painting/concentriccircles/circlewidget.h @@ -0,0 +1,74 @@ +/**************************************************************************** +** +** 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 CIRCLEWIDGET_H +#define CIRCLEWIDGET_H + +#include <QWidget> + +//! [0] +class CircleWidget : public QWidget +{ + Q_OBJECT + +public: + CircleWidget(QWidget *parent = 0); + + void setFloatBased(bool floatBased); + void setAntialiased(bool antialiased); + + QSize minimumSizeHint() const; + QSize sizeHint() const; + +public slots: + void nextAnimationFrame(); + +protected: + void paintEvent(QPaintEvent *event); + +private: + bool floatBased; + bool antialiased; + int frameNo; +}; +//! [0] + +#endif diff --git a/examples/painting/concentriccircles/concentriccircles.pro b/examples/painting/concentriccircles/concentriccircles.pro new file mode 100644 index 0000000..093ea1c --- /dev/null +++ b/examples/painting/concentriccircles/concentriccircles.pro @@ -0,0 +1,11 @@ +HEADERS = circlewidget.h \ + window.h +SOURCES = circlewidget.cpp \ + main.cpp \ + window.cpp + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/painting/concentriccircles +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS concentriccircles.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/painting/concentriccircles +INSTALLS += target sources diff --git a/examples/painting/concentriccircles/main.cpp b/examples/painting/concentriccircles/main.cpp new file mode 100644 index 0000000..fa8b0ab --- /dev/null +++ b/examples/painting/concentriccircles/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 <QApplication> + +#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/concentriccircles/window.cpp b/examples/painting/concentriccircles/window.cpp new file mode 100644 index 0000000..dd5e6e5 --- /dev/null +++ b/examples/painting/concentriccircles/window.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 <QtGui> + +#include "circlewidget.h" +#include "window.h" + +//! [0] +Window::Window() +{ + aliasedLabel = createLabel(tr("Aliased")); + antialiasedLabel = createLabel(tr("Antialiased")); + intLabel = createLabel(tr("Int")); + floatLabel = createLabel(tr("Float")); + + QGridLayout *layout = new QGridLayout; + layout->addWidget(aliasedLabel, 0, 1); + layout->addWidget(antialiasedLabel, 0, 2); + layout->addWidget(intLabel, 1, 0); + layout->addWidget(floatLabel, 2, 0); +//! [0] + +//! [1] + QTimer *timer = new QTimer(this); + + for (int i = 0; i < 2; ++i) { + for (int j = 0; j < 2; ++j) { + circleWidgets[i][j] = new CircleWidget; + circleWidgets[i][j]->setAntialiased(j != 0); + circleWidgets[i][j]->setFloatBased(i != 0); + + connect(timer, SIGNAL(timeout()), + circleWidgets[i][j], SLOT(nextAnimationFrame())); + + layout->addWidget(circleWidgets[i][j], i + 1, j + 1); + } + } +//! [1] //! [2] + timer->start(100); + setLayout(layout); + + setWindowTitle(tr("Concentric Circles")); +} +//! [2] + +//! [3] +QLabel *Window::createLabel(const QString &text) +{ + QLabel *label = new QLabel(text); + label->setAlignment(Qt::AlignCenter); + label->setMargin(2); + label->setFrameStyle(QFrame::Box | QFrame::Sunken); + return label; +} +//! [3] diff --git a/examples/painting/concentriccircles/window.h b/examples/painting/concentriccircles/window.h new file mode 100644 index 0000000..2b3ad67 --- /dev/null +++ b/examples/painting/concentriccircles/window.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 WINDOW_H +#define WINDOW_H + +#include <QWidget> + +QT_BEGIN_NAMESPACE +class QLabel; +QT_END_NAMESPACE +class CircleWidget; + +//! [0] +class Window : public QWidget +{ + Q_OBJECT + +public: + Window(); + +private: + QLabel *createLabel(const QString &text); + + QLabel *aliasedLabel; + QLabel *antialiasedLabel; + QLabel *intLabel; + QLabel *floatLabel; + CircleWidget *circleWidgets[2][2]; +}; +//! [0] + +#endif diff --git a/examples/painting/fontsampler/fontsampler.pro b/examples/painting/fontsampler/fontsampler.pro new file mode 100644 index 0000000..8b63752 --- /dev/null +++ b/examples/painting/fontsampler/fontsampler.pro @@ -0,0 +1,10 @@ +FORMS = mainwindowbase.ui +HEADERS = mainwindow.h +SOURCES = main.cpp \ + mainwindow.cpp + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/painting/fontsampler +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS fontsampler.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/painting/fontsampler +INSTALLS += target sources diff --git a/examples/painting/fontsampler/main.cpp b/examples/painting/fontsampler/main.cpp new file mode 100644 index 0000000..1d51376 --- /dev/null +++ b/examples/painting/fontsampler/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 <QApplication> + +#include "mainwindow.h" + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + MainWindow window; + window.show(); + return app.exec(); +} diff --git a/examples/painting/fontsampler/mainwindow.cpp b/examples/painting/fontsampler/mainwindow.cpp new file mode 100644 index 0000000..424f175 --- /dev/null +++ b/examples/painting/fontsampler/mainwindow.cpp @@ -0,0 +1,373 @@ +/**************************************************************************** +** +** 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" + +MainWindow::MainWindow(QWidget *parent) + : QMainWindow(parent) +{ + setupUi(this); + + sampleSizes << 32 << 24 << 16 << 14 << 12 << 8 << 4 << 2 << 1; + markedCount = 0; + setupFontTree(); + + connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit())); + connect(fontTree, SIGNAL(currentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)), + this, SLOT(showFont(QTreeWidgetItem *))); + connect(fontTree, SIGNAL(itemChanged(QTreeWidgetItem *, int)), + this, SLOT(updateStyles(QTreeWidgetItem *, int))); + + fontTree->setItemSelected(fontTree->topLevelItem(0), true); + showFont(fontTree->topLevelItem(0)); +} + +void MainWindow::setupFontTree() +{ + QFontDatabase database; + fontTree->setColumnCount(1); + fontTree->setHeaderLabels(QStringList() << tr("Font")); + + foreach (QString family, database.families()) { + const QStringList styles = database.styles(family); + if (styles.isEmpty()) + continue; + + QTreeWidgetItem *familyItem = new QTreeWidgetItem(fontTree); + familyItem->setText(0, family); + familyItem->setCheckState(0, Qt::Unchecked); + + foreach (QString style, styles) { + QTreeWidgetItem *styleItem = new QTreeWidgetItem(familyItem); + styleItem->setText(0, style); + styleItem->setCheckState(0, Qt::Unchecked); + styleItem->setData(0, Qt::UserRole, + QVariant(database.weight(family, style))); + styleItem->setData(0, Qt::UserRole + 1, + QVariant(database.italic(family, style))); + } + } +} + +void MainWindow::on_clearAction_triggered() +{ + QTreeWidgetItem *currentItem = fontTree->currentItem(); + foreach (QTreeWidgetItem *item, fontTree->selectedItems()) + fontTree->setItemSelected(item, false); + fontTree->setItemSelected(currentItem, true); +} + +void MainWindow::on_markAction_triggered() +{ + markUnmarkFonts(Qt::Checked); +} + +void MainWindow::on_unmarkAction_triggered() +{ + markUnmarkFonts(Qt::Unchecked); +} + +void MainWindow::markUnmarkFonts(Qt::CheckState state) +{ + QList<QTreeWidgetItem *> items = fontTree->selectedItems(); + foreach (QTreeWidgetItem *item, items) { + if (item->checkState(0) != state) + item->setCheckState(0, state); + } +} + +void MainWindow::showFont(QTreeWidgetItem *item) +{ + if (!item) + return; + + QString family; + QString style; + int weight; + bool italic; + + if (item->parent()) { + family = item->parent()->text(0); + style = item->text(0); + weight = item->data(0, Qt::UserRole).toInt(); + italic = item->data(0, Qt::UserRole + 1).toBool(); + } else { + family = item->text(0); + style = item->child(0)->text(0); + weight = item->child(0)->data(0, Qt::UserRole).toInt(); + italic = item->child(0)->data(0, Qt::UserRole + 1).toBool(); + } + + QString oldText = textEdit->toPlainText().trimmed(); + bool modified = textEdit->document()->isModified(); + textEdit->clear(); + textEdit->document()->setDefaultFont(QFont(family, 32, weight, italic)); + + QTextCursor cursor = textEdit->textCursor(); + QTextBlockFormat blockFormat; + blockFormat.setAlignment(Qt::AlignCenter); + cursor.insertBlock(blockFormat); + + if (modified) + cursor.insertText(QString(oldText)); + else + cursor.insertText(QString("%1 %2").arg(family).arg(style)); + + textEdit->document()->setModified(modified); +} + +void MainWindow::updateStyles(QTreeWidgetItem *item, int column) +{ + if (!item || column != 0) + return; + + Qt::CheckState state = item->checkState(0); + QTreeWidgetItem *parent = item->parent(); + + if (parent) { + + // Only count style items. + if (state == Qt::Checked) + ++markedCount; + else + --markedCount; + + if (state == Qt::Checked && + parent->checkState(0) == Qt::Unchecked) { + // Mark parent items when child items are checked. + parent->setCheckState(0, Qt::Checked); + + } else if (state == Qt::Unchecked && + parent->checkState(0) == Qt::Checked) { + + bool marked = false; + for (int row = 0; row < parent->childCount(); ++row) { + if (parent->child(row)->checkState(0) == Qt::Checked) { + marked = true; + break; + } + } + // Unmark parent items when all child items are unchecked. + if (!marked) + parent->setCheckState(0, Qt::Unchecked); + } + } else { + int row; + int number = 0; + for (row = 0; row < item->childCount(); ++row) { + if (item->child(row)->checkState(0) == Qt::Checked) + ++number; + } + + // Mark/unmark all child items when marking/unmarking top-level + // items. + if (state == Qt::Checked && number == 0) { + for (row = 0; row < item->childCount(); ++row) { + if (item->child(row)->checkState(0) == Qt::Unchecked) + item->child(row)->setCheckState(0, Qt::Checked); + } + } else if (state == Qt::Unchecked && number > 0) { + for (row = 0; row < item->childCount(); ++row) { + if (item->child(row)->checkState(0) == Qt::Checked) + item->child(row)->setCheckState(0, Qt::Unchecked); + } + } + } + + printAction->setEnabled(markedCount > 0); + printPreviewAction->setEnabled(markedCount > 0); +} + +void MainWindow::on_printAction_triggered() +{ + pageMap = currentPageMap(); + + if (pageMap.count() == 0) + return; + + QPrinter printer(QPrinter::HighResolution); + QPrintDialog dialog(&printer, this); + if (dialog.exec() != QDialog::Accepted) + return; + + int from = printer.fromPage(); + int to = printer.toPage(); + if (from <= 0 && to <= 0) + printer.setFromTo(1, pageMap.keys().count()); + + printDocument(&printer); +} + +void MainWindow::printDocument(QPrinter *printer) +{ + printer->setFromTo(1, pageMap.count()); + + QProgressDialog progress(tr("Preparing font samples..."), tr("&Cancel"), + 0, pageMap.count(), this); + progress.setWindowModality(Qt::ApplicationModal); + progress.setWindowTitle(tr("Font Sampler")); + progress.setMinimum(printer->fromPage() - 1); + progress.setMaximum(printer->toPage()); + + QPainter painter; + painter.begin(printer); + bool firstPage = true; + + for (int page = printer->fromPage(); page <= printer->toPage(); ++page) { + + if (!firstPage) + printer->newPage(); + + qApp->processEvents(); + if (progress.wasCanceled()) + break; + + printPage(page - 1, &painter, printer); + progress.setValue(page); + firstPage = false; + } + + painter.end(); +} + +void MainWindow::on_printPreviewAction_triggered() +{ + pageMap = currentPageMap(); + + if (pageMap.count() == 0) + return; + + QPrinter printer(QPrinter::HighResolution); + QPrintPreviewDialog preview(&printer, this); + connect(&preview, SIGNAL(paintRequested(QPrinter *)), + this, SLOT(printDocument(QPrinter *))); + preview.exec(); +} + +QMap<QString, StyleItems> MainWindow::currentPageMap() +{ + QMap<QString, StyleItems> pageMap; + + for (int row = 0; row < fontTree->topLevelItemCount(); ++row) { + QTreeWidgetItem *familyItem = fontTree->topLevelItem(row); + QString family; + + if (familyItem->checkState(0) == Qt::Checked) { + family = familyItem->text(0); + pageMap[family] = StyleItems(); + } + + for (int childRow = 0; childRow < familyItem->childCount(); ++childRow) { + QTreeWidgetItem *styleItem = familyItem->child(childRow); + if (styleItem->checkState(0) == Qt::Checked) + pageMap[family].append(styleItem); + } + } + + return pageMap; +} + +void MainWindow::printPage(int index, QPainter *painter, QPrinter *printer) +{ + QString family = pageMap.keys()[index]; + StyleItems items = pageMap[family]; + + // Find the dimensions of the text on each page. + qreal width = 0.0; + qreal height = 0.0; + foreach (QTreeWidgetItem *item, items) { + QString style = item->text(0); + int weight = item->data(0, Qt::UserRole).toInt(); + bool italic = item->data(0, Qt::UserRole + 1).toBool(); + + // Calculate the maximum width and total height of the text. + foreach (int size, sampleSizes) { + QFont font(family, size, weight, italic); + font = QFont(font, painter->device()); + QFontMetricsF fontMetrics(font); + QRectF rect = fontMetrics.boundingRect( + QString("%1 %2").arg(family).arg(style)); + width = qMax(rect.width(), width); + height += rect.height(); + } + } + + qreal xScale = printer->pageRect().width() / width; + qreal yScale = printer->pageRect().height() / height; + qreal scale = qMin(xScale, yScale); + + qreal remainingHeight = printer->pageRect().height()/scale - height; + qreal spaceHeight = (remainingHeight/4.0) / (items.count() + 1); + qreal interLineHeight = (remainingHeight/4.0) / (sampleSizes.count() * items.count()); + + painter->save(); + painter->translate(printer->pageRect().width()/2.0, printer->pageRect().height()/2.0); + painter->scale(scale, scale); + painter->setBrush(QBrush(Qt::black)); + + qreal x = -width/2.0; + qreal y = -height/2.0 - remainingHeight/4.0 + spaceHeight; + + foreach (QTreeWidgetItem *item, items) { + QString style = item->text(0); + int weight = item->data(0, Qt::UserRole).toInt(); + bool italic = item->data(0, Qt::UserRole + 1).toBool(); + + // Draw each line of text. + foreach (int size, sampleSizes) { + QFont font(family, size, weight, italic); + font = QFont(font, painter->device()); + QFontMetricsF fontMetrics(font); + QRectF rect = fontMetrics.boundingRect(QString("%1 %2").arg( + font.family()).arg(style)); + y += rect.height(); + painter->setFont(font); + painter->drawText(QPointF(x, y), + QString("%1 %2").arg(family).arg(style)); + y += interLineHeight; + } + y += spaceHeight; + } + + painter->restore(); +} diff --git a/examples/painting/fontsampler/mainwindow.h b/examples/painting/fontsampler/mainwindow.h new file mode 100644 index 0000000..25d7730 --- /dev/null +++ b/examples/painting/fontsampler/mainwindow.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$ +** +****************************************************************************/ + +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include "ui_mainwindowbase.h" + +QT_BEGIN_NAMESPACE +class QTextEdit; +class QTreeWidget; +class QTreeWidgetItem; +QT_END_NAMESPACE + +typedef QList<QTreeWidgetItem *> StyleItems; + +class MainWindow : public QMainWindow, private Ui::MainWindowBase +{ + Q_OBJECT + +public: + MainWindow(QWidget *parent = 0); + +public slots: + void on_clearAction_triggered(); + void on_markAction_triggered(); + void on_printAction_triggered(); + void on_printPreviewAction_triggered(); + void on_unmarkAction_triggered(); + void printDocument(QPrinter *printer); + void printPage(int index, QPainter *painter, QPrinter *printer); + void showFont(QTreeWidgetItem *item); + void updateStyles(QTreeWidgetItem *item, int column); + +private: + QMap<QString, StyleItems> currentPageMap(); + void markUnmarkFonts(Qt::CheckState state); + void setupFontTree(); + + QList<int> sampleSizes; + QMap<QString, StyleItems> pageMap; + int markedCount; +}; + +#endif diff --git a/examples/painting/fontsampler/mainwindowbase.ui b/examples/painting/fontsampler/mainwindowbase.ui new file mode 100644 index 0000000..6545b34 --- /dev/null +++ b/examples/painting/fontsampler/mainwindowbase.ui @@ -0,0 +1,140 @@ +<ui version="4.0" > + <author></author> + <comment></comment> + <exportmacro></exportmacro> + <class>MainWindowBase</class> + <widget class="QMainWindow" name="MainWindowBase" > + <property name="geometry" > + <rect> + <x>0</x> + <y>0</y> + <width>800</width> + <height>345</height> + </rect> + </property> + <property name="windowTitle" > + <string>Font Sampler</string> + </property> + <widget class="QWidget" name="centralwidget" > + <layout class="QVBoxLayout" > + <property name="margin" > + <number>9</number> + </property> + <property name="spacing" > + <number>6</number> + </property> + <item> + <widget class="QTextEdit" name="textEdit" /> + </item> + </layout> + </widget> + <widget class="QMenuBar" name="menubar" > + <property name="geometry" > + <rect> + <x>0</x> + <y>0</y> + <width>800</width> + <height>24</height> + </rect> + </property> + <widget class="QMenu" name="menu_Selection" > + <property name="title" > + <string>&Selection</string> + </property> + <addaction name="markAction" /> + <addaction name="unmarkAction" /> + <addaction name="clearAction" /> + </widget> + <widget class="QMenu" name="menu_File" > + <property name="title" > + <string>&File</string> + </property> + <addaction name="printPreviewAction" /> + <addaction name="printAction" /> + <addaction name="quitAction" /> + </widget> + <addaction name="menu_File" /> + <addaction name="menu_Selection" /> + </widget> + <widget class="QStatusBar" name="statusbar" /> + <widget class="QDockWidget" name="dockWidget" > + <property name="features" > + <set>QDockWidget::DockWidgetFloatable|QDockWidget::DockWidgetMovable|QDockWidget::NoDockWidgetFeatures</set> + </property> + <property name="windowTitle" > + <string>Available Fonts</string> + </property> + <attribute name="dockWidgetArea" > + <number>1</number> + </attribute> + <widget class="QWidget" name="dockWidgetContents" > + <layout class="QVBoxLayout" > + <property name="margin" > + <number>9</number> + </property> + <property name="spacing" > + <number>6</number> + </property> + <item> + <widget class="QTreeWidget" name="fontTree" > + <property name="selectionMode" > + <enum>QAbstractItemView::ExtendedSelection</enum> + </property> + </widget> + </item> + </layout> + </widget> + </widget> + <action name="printAction" > + <property name="enabled" > + <bool>false</bool> + </property> + <property name="text" > + <string>&Print...</string> + </property> + <property name="shortcut" > + <string>Ctrl+P</string> + </property> + </action> + <action name="quitAction" > + <property name="text" > + <string>E&xit</string> + </property> + <property name="shortcut" > + <string>Ctrl+Q</string> + </property> + </action> + <action name="markAction" > + <property name="text" > + <string>&Mark</string> + </property> + <property name="shortcut" > + <string>Ctrl+M</string> + </property> + </action> + <action name="unmarkAction" > + <property name="text" > + <string>&Unmark</string> + </property> + <property name="shortcut" > + <string>Ctrl+U</string> + </property> + </action> + <action name="clearAction" > + <property name="text" > + <string>&Clear</string> + </property> + </action> + <action name="printPreviewAction" > + <property name="enabled" > + <bool>false</bool> + </property> + <property name="text" > + <string>Print Preview...</string> + </property> + </action> + </widget> + <pixmapfunction></pixmapfunction> + <resources/> + <connections/> +</ui> diff --git a/examples/painting/imagecomposition/imagecomposer.cpp b/examples/painting/imagecomposition/imagecomposer.cpp new file mode 100644 index 0000000..c16d85f --- /dev/null +++ b/examples/painting/imagecomposition/imagecomposer.cpp @@ -0,0 +1,209 @@ +/**************************************************************************** +** +** 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 "imagecomposer.h" + +//! [0] +static const QSize resultSize(200, 200); +//! [0] + +//! [1] +ImageComposer::ImageComposer() +{ + sourceButton = new QToolButton; + sourceButton->setIconSize(resultSize); + + operatorComboBox = new QComboBox; + addOp(QPainter::CompositionMode_SourceOver, tr("SourceOver")); + addOp(QPainter::CompositionMode_DestinationOver, tr("DestinationOver")); + addOp(QPainter::CompositionMode_Clear, tr("Clear")); + addOp(QPainter::CompositionMode_Source, tr("Source")); + addOp(QPainter::CompositionMode_Destination, tr("Destination")); + addOp(QPainter::CompositionMode_SourceIn, tr("SourceIn")); + addOp(QPainter::CompositionMode_DestinationIn, tr("DestinationIn")); + addOp(QPainter::CompositionMode_SourceOut, tr("SourceOut")); + addOp(QPainter::CompositionMode_DestinationOut, tr("DestinationOut")); + addOp(QPainter::CompositionMode_SourceAtop, tr("SourceAtop")); + addOp(QPainter::CompositionMode_DestinationAtop, tr("DestinationAtop")); + addOp(QPainter::CompositionMode_Xor, tr("Xor")); + addOp(QPainter::CompositionMode_Plus, tr("Plus")); + addOp(QPainter::CompositionMode_Multiply, tr("Multiply")); + addOp(QPainter::CompositionMode_Screen, tr("Screen")); + addOp(QPainter::CompositionMode_Overlay, tr("Overlay")); + addOp(QPainter::CompositionMode_Darken, tr("Darken")); + addOp(QPainter::CompositionMode_Lighten, tr("Lighten")); + addOp(QPainter::CompositionMode_ColorDodge, tr("ColorDodge")); + addOp(QPainter::CompositionMode_ColorBurn, tr("ColorBurn")); + addOp(QPainter::CompositionMode_HardLight, tr("HardLight")); + addOp(QPainter::CompositionMode_SoftLight, tr("SoftLight")); + addOp(QPainter::CompositionMode_Difference, tr("Difference")); + addOp(QPainter::CompositionMode_Exclusion, tr("Exclusion")); +//! [1] + +//! [2] + destinationButton = new QToolButton; + destinationButton->setIconSize(resultSize); + + equalLabel = new QLabel(tr("=")); + + resultLabel = new QLabel; + resultLabel->setMinimumWidth(resultSize.width()); +//! [2] + +//! [3] + connect(sourceButton, SIGNAL(clicked()), this, SLOT(chooseSource())); + connect(operatorComboBox, SIGNAL(activated(int)), + this, SLOT(recalculateResult())); + connect(destinationButton, SIGNAL(clicked()), + this, SLOT(chooseDestination())); +//! [3] + +//! [4] + QGridLayout *mainLayout = new QGridLayout; + mainLayout->addWidget(sourceButton, 0, 0, 3, 1); + mainLayout->addWidget(operatorComboBox, 1, 1); + mainLayout->addWidget(destinationButton, 0, 2, 3, 1); + mainLayout->addWidget(equalLabel, 1, 3); + mainLayout->addWidget(resultLabel, 0, 4, 3, 1); + mainLayout->setSizeConstraint(QLayout::SetFixedSize); + setLayout(mainLayout); +//! [4] + +//! [5] + resultImage = QImage(resultSize, QImage::Format_ARGB32_Premultiplied); + + loadImage(":/images/butterfly.png", &sourceImage, sourceButton); + loadImage(":/images/checker.png", &destinationImage, destinationButton); + + setWindowTitle(tr("Image Composition")); +} +//! [5] + +//! [6] +void ImageComposer::chooseSource() +{ + chooseImage(tr("Choose Source Image"), &sourceImage, sourceButton); +} +//! [6] + +//! [7] +void ImageComposer::chooseDestination() +{ + chooseImage(tr("Choose Destination Image"), &destinationImage, + destinationButton); +} +//! [7] + +//! [8] +void ImageComposer::recalculateResult() +{ + QPainter::CompositionMode mode = currentMode(); + + QPainter painter(&resultImage); + painter.setCompositionMode(QPainter::CompositionMode_Source); + painter.fillRect(resultImage.rect(), Qt::transparent); + painter.setCompositionMode(QPainter::CompositionMode_SourceOver); + painter.drawImage(0, 0, destinationImage); + painter.setCompositionMode(mode); + painter.drawImage(0, 0, sourceImage); + painter.setCompositionMode(QPainter::CompositionMode_DestinationOver); + painter.fillRect(resultImage.rect(), Qt::white); + painter.end(); + + resultLabel->setPixmap(QPixmap::fromImage(resultImage)); +} +//! [8] + +//! [9] +void ImageComposer::addOp(QPainter::CompositionMode mode, const QString &name) +{ + operatorComboBox->addItem(name, mode); +} +//! [9] + +//! [10] +void ImageComposer::chooseImage(const QString &title, QImage *image, + QToolButton *button) +{ + QString fileName = QFileDialog::getOpenFileName(this, title); + if (!fileName.isEmpty()) + loadImage(fileName, image, button); +} +//! [10] + +//! [11] +void ImageComposer::loadImage(const QString &fileName, QImage *image, + QToolButton *button) +{ + image->load(fileName); + + QImage fixedImage(resultSize, QImage::Format_ARGB32_Premultiplied); + QPainter painter(&fixedImage); + painter.setCompositionMode(QPainter::CompositionMode_Source); + painter.fillRect(fixedImage.rect(), Qt::transparent); + painter.setCompositionMode(QPainter::CompositionMode_SourceOver); + painter.drawImage(imagePos(*image), *image); + painter.end(); + button->setIcon(QPixmap::fromImage(fixedImage)); + + *image = fixedImage; + + recalculateResult(); +} +//! [11] + +//! [12] +QPainter::CompositionMode ImageComposer::currentMode() const +{ + return (QPainter::CompositionMode) + operatorComboBox->itemData(operatorComboBox->currentIndex()).toInt(); +} +//! [12] + +//! [13] +QPoint ImageComposer::imagePos(const QImage &image) const +{ + return QPoint((resultSize.width() - image.width()) / 2, + (resultSize.height() - image.height()) / 2); +} +//! [13] diff --git a/examples/painting/imagecomposition/imagecomposer.h b/examples/painting/imagecomposition/imagecomposer.h new file mode 100644 index 0000000..e8d039f --- /dev/null +++ b/examples/painting/imagecomposition/imagecomposer.h @@ -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$ +** +****************************************************************************/ + +#ifndef IMAGECOMPOSER_H +#define IMAGECOMPOSER_H + +#include <QPainter> +#include <QWidget> + +QT_BEGIN_NAMESPACE +class QComboBox; +class QLabel; +class QToolButton; +QT_END_NAMESPACE + +//! [0] +class ImageComposer : public QWidget +{ + Q_OBJECT + +public: + ImageComposer(); + +private slots: + void chooseSource(); + void chooseDestination(); + void recalculateResult(); +//! [0] + +//! [1] +private: + void addOp(QPainter::CompositionMode mode, const QString &name); + void chooseImage(const QString &title, QImage *image, QToolButton *button); + void loadImage(const QString &fileName, QImage *image, QToolButton *button); + QPainter::CompositionMode currentMode() const; + QPoint imagePos(const QImage &image) const; + + QToolButton *sourceButton; + QToolButton *destinationButton; + QComboBox *operatorComboBox; + QLabel *equalLabel; + QLabel *resultLabel; + + QImage sourceImage; + QImage destinationImage; + QImage resultImage; +}; +//! [1] + +#endif diff --git a/examples/painting/imagecomposition/imagecomposition.pro b/examples/painting/imagecomposition/imagecomposition.pro new file mode 100644 index 0000000..935a1d4 --- /dev/null +++ b/examples/painting/imagecomposition/imagecomposition.pro @@ -0,0 +1,11 @@ +HEADERS = imagecomposer.h +SOURCES = imagecomposer.cpp \ + main.cpp +RESOURCES = imagecomposition.qrc + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/painting/imagecomposition +sources.files = $$SOURCES $$HEADERS $$RESOURCES images *.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/painting/imagecomposition +INSTALLS += target sources + diff --git a/examples/painting/imagecomposition/imagecomposition.qrc b/examples/painting/imagecomposition/imagecomposition.qrc new file mode 100644 index 0000000..ebba7b2 --- /dev/null +++ b/examples/painting/imagecomposition/imagecomposition.qrc @@ -0,0 +1,6 @@ +<!DOCTYPE RCC><RCC version="1.0"> +<qresource> + <file>images/butterfly.png</file> + <file>images/checker.png</file> +</qresource> +</RCC> diff --git a/examples/painting/imagecomposition/images/background.png b/examples/painting/imagecomposition/images/background.png Binary files differnew file mode 100644 index 0000000..f6ad899 --- /dev/null +++ b/examples/painting/imagecomposition/images/background.png diff --git a/examples/painting/imagecomposition/images/blackrectangle.png b/examples/painting/imagecomposition/images/blackrectangle.png Binary files differnew file mode 100644 index 0000000..5f2ecf8 --- /dev/null +++ b/examples/painting/imagecomposition/images/blackrectangle.png diff --git a/examples/painting/imagecomposition/images/butterfly.png b/examples/painting/imagecomposition/images/butterfly.png Binary files differnew file mode 100644 index 0000000..f3e050e --- /dev/null +++ b/examples/painting/imagecomposition/images/butterfly.png diff --git a/examples/painting/imagecomposition/images/checker.png b/examples/painting/imagecomposition/images/checker.png Binary files differnew file mode 100644 index 0000000..546609d --- /dev/null +++ b/examples/painting/imagecomposition/images/checker.png diff --git a/examples/painting/imagecomposition/main.cpp b/examples/painting/imagecomposition/main.cpp new file mode 100644 index 0000000..34527a4 --- /dev/null +++ b/examples/painting/imagecomposition/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 <QApplication> + +#include "imagecomposer.h" + +//! [0] +int main(int argc, char *argv[]) +{ + Q_INIT_RESOURCE(imagecomposition); + + QApplication app(argc, argv); + ImageComposer composer; + composer.show(); + return app.exec(); +} +//! [0] diff --git a/examples/painting/painterpaths/main.cpp b/examples/painting/painterpaths/main.cpp new file mode 100644 index 0000000..fa8b0ab --- /dev/null +++ b/examples/painting/painterpaths/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 <QApplication> + +#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/painterpaths/painterpaths.pro b/examples/painting/painterpaths/painterpaths.pro new file mode 100644 index 0000000..76c9e82 --- /dev/null +++ b/examples/painting/painterpaths/painterpaths.pro @@ -0,0 +1,12 @@ +HEADERS = renderarea.h \ + window.h +SOURCES = main.cpp \ + renderarea.cpp \ + window.cpp +unix:!mac:LIBS += -lm + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/painting/painterpaths +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS painterpaths.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/painting/painterpaths +INSTALLS += target sources diff --git a/examples/painting/painterpaths/renderarea.cpp b/examples/painting/painterpaths/renderarea.cpp new file mode 100644 index 0000000..64d149b --- /dev/null +++ b/examples/painting/painterpaths/renderarea.cpp @@ -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$ +** +****************************************************************************/ + +#include <QtGui> + +#include "renderarea.h" + +//! [0] +RenderArea::RenderArea(const QPainterPath &path, QWidget *parent) + : QWidget(parent), path(path) +{ + penWidth = 1; + rotationAngle = 0; + setBackgroundRole(QPalette::Base); +} +//! [0] + +//! [1] +QSize RenderArea::minimumSizeHint() const +{ + return QSize(50, 50); +} +//! [1] + +//! [2] +QSize RenderArea::sizeHint() const +{ + return QSize(100, 100); +} +//! [2] + +//! [3] +void RenderArea::setFillRule(Qt::FillRule rule) +{ + path.setFillRule(rule); + update(); +} +//! [3] + +//! [4] +void RenderArea::setFillGradient(const QColor &color1, const QColor &color2) +{ + fillColor1 = color1; + fillColor2 = color2; + update(); +} +//! [4] + +//! [5] +void RenderArea::setPenWidth(int width) +{ + penWidth = width; + update(); +} +//! [5] + +//! [6] +void RenderArea::setPenColor(const QColor &color) +{ + penColor = color; + update(); +} +//! [6] + +//! [7] +void RenderArea::setRotationAngle(int degrees) +{ + rotationAngle = degrees; + update(); +} +//! [7] + +//! [8] +void RenderArea::paintEvent(QPaintEvent *) +{ + QPainter painter(this); + painter.setRenderHint(QPainter::Antialiasing); +//! [8] //! [9] + painter.scale(width() / 100.0, height() / 100.0); + painter.translate(50.0, 50.0); + painter.rotate(-rotationAngle); + painter.translate(-50.0, -50.0); + +//! [9] //! [10] + painter.setPen(QPen(penColor, penWidth, Qt::SolidLine, Qt::RoundCap, + Qt::RoundJoin)); + QLinearGradient gradient(0, 0, 0, 100); + gradient.setColorAt(0.0, fillColor1); + gradient.setColorAt(1.0, fillColor2); + painter.setBrush(gradient); + painter.drawPath(path); +} +//! [10] diff --git a/examples/painting/painterpaths/renderarea.h b/examples/painting/painterpaths/renderarea.h new file mode 100644 index 0000000..39b7efd --- /dev/null +++ b/examples/painting/painterpaths/renderarea.h @@ -0,0 +1,81 @@ +/**************************************************************************** +** +** 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 RENDERAREA_H +#define RENDERAREA_H + +#include <QPainterPath> +#include <QWidget> + +//! [0] +class RenderArea : public QWidget +{ + Q_OBJECT + +public: + RenderArea(const QPainterPath &path, QWidget *parent = 0); + + QSize minimumSizeHint() const; + QSize sizeHint() const; + +public slots: + void setFillRule(Qt::FillRule rule); + void setFillGradient(const QColor &color1, const QColor &color2); + void setPenWidth(int width); + void setPenColor(const QColor &color); + void setRotationAngle(int degrees); + +protected: + void paintEvent(QPaintEvent *event); +//! [0] + +//! [1] +private: + QPainterPath path; + QColor fillColor1; + QColor fillColor2; + int penWidth; + QColor penColor; + int rotationAngle; +}; +//! [1] + +#endif diff --git a/examples/painting/painterpaths/window.cpp b/examples/painting/painterpaths/window.cpp new file mode 100644 index 0000000..712e961 --- /dev/null +++ b/examples/painting/painterpaths/window.cpp @@ -0,0 +1,289 @@ +/**************************************************************************** +** +** 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 <math.h> + +#include "renderarea.h" +#include "window.h" + +//! [0] +const float Pi = 3.14159f; +//! [0] + +//! [1] +Window::Window() +{ + QPainterPath rectPath; + rectPath.moveTo(20.0, 30.0); + rectPath.lineTo(80.0, 30.0); + rectPath.lineTo(80.0, 70.0); + rectPath.lineTo(20.0, 70.0); + rectPath.closeSubpath(); +//! [1] + +//! [2] + QPainterPath roundRectPath; + roundRectPath.moveTo(80.0, 35.0); + roundRectPath.arcTo(70.0, 30.0, 10.0, 10.0, 0.0, 90.0); + roundRectPath.lineTo(25.0, 30.0); + roundRectPath.arcTo(20.0, 30.0, 10.0, 10.0, 90.0, 90.0); + roundRectPath.lineTo(20.0, 65.0); + roundRectPath.arcTo(20.0, 60.0, 10.0, 10.0, 180.0, 90.0); + roundRectPath.lineTo(75.0, 70.0); + roundRectPath.arcTo(70.0, 60.0, 10.0, 10.0, 270.0, 90.0); + roundRectPath.closeSubpath(); +//! [2] + +//! [3] + QPainterPath ellipsePath; + ellipsePath.moveTo(80.0, 50.0); + ellipsePath.arcTo(20.0, 30.0, 60.0, 40.0, 0.0, 360.0); +//! [3] + +//! [4] + QPainterPath piePath; + piePath.moveTo(50.0, 50.0); + piePath.arcTo(20.0, 30.0, 60.0, 40.0, 60.0, 240.0); + piePath.closeSubpath(); +//! [4] + +//! [5] + QPainterPath polygonPath; + polygonPath.moveTo(10.0, 80.0); + polygonPath.lineTo(20.0, 10.0); + polygonPath.lineTo(80.0, 30.0); + polygonPath.lineTo(90.0, 70.0); + polygonPath.closeSubpath(); +//! [5] + +//! [6] + QPainterPath groupPath; + groupPath.moveTo(60.0, 40.0); + groupPath.arcTo(20.0, 20.0, 40.0, 40.0, 0.0, 360.0); + groupPath.moveTo(40.0, 40.0); + groupPath.lineTo(40.0, 80.0); + groupPath.lineTo(80.0, 80.0); + groupPath.lineTo(80.0, 40.0); + groupPath.closeSubpath(); +//! [6] + +//! [7] + QPainterPath textPath; + QFont timesFont("Times", 50); + timesFont.setStyleStrategy(QFont::ForceOutline); + textPath.addText(10, 70, timesFont, tr("Qt")); +//! [7] + +//! [8] + QPainterPath bezierPath; + bezierPath.moveTo(20, 30); + bezierPath.cubicTo(80, 0, 50, 50, 80, 80); +//! [8] + +//! [9] + QPainterPath starPath; + starPath.moveTo(90, 50); + for (int i = 1; i < 5; ++i) { + starPath.lineTo(50 + 40 * cos(0.8 * i * Pi), + 50 + 40 * sin(0.8 * i * Pi)); + } + starPath.closeSubpath(); +//! [9] + +//! [10] + renderAreas[0] = new RenderArea(rectPath); + renderAreas[1] = new RenderArea(roundRectPath); + renderAreas[2] = new RenderArea(ellipsePath); + renderAreas[3] = new RenderArea(piePath); + renderAreas[4] = new RenderArea(polygonPath); + renderAreas[5] = new RenderArea(groupPath); + renderAreas[6] = new RenderArea(textPath); + renderAreas[7] = new RenderArea(bezierPath); + renderAreas[8] = new RenderArea(starPath); + Q_ASSERT(NumRenderAreas == 9); +//! [10] + +//! [11] + fillRuleComboBox = new QComboBox; + fillRuleComboBox->addItem(tr("Odd Even"), Qt::OddEvenFill); + fillRuleComboBox->addItem(tr("Winding"), Qt::WindingFill); + + fillRuleLabel = new QLabel(tr("Fill &Rule:")); + fillRuleLabel->setBuddy(fillRuleComboBox); +//! [11] + +//! [12] + fillColor1ComboBox = new QComboBox; + populateWithColors(fillColor1ComboBox); + fillColor1ComboBox->setCurrentIndex( + fillColor1ComboBox->findText("mediumslateblue")); + + fillColor2ComboBox = new QComboBox; + populateWithColors(fillColor2ComboBox); + fillColor2ComboBox->setCurrentIndex( + fillColor2ComboBox->findText("cornsilk")); + + fillGradientLabel = new QLabel(tr("&Fill Gradient:")); + fillGradientLabel->setBuddy(fillColor1ComboBox); + + fillToLabel = new QLabel(tr("to")); + fillToLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); + + penWidthSpinBox = new QSpinBox; + penWidthSpinBox->setRange(0, 20); + + penWidthLabel = new QLabel(tr("&Pen Width:")); + penWidthLabel->setBuddy(penWidthSpinBox); + + penColorComboBox = new QComboBox; + populateWithColors(penColorComboBox); + penColorComboBox->setCurrentIndex( + penColorComboBox->findText("darkslateblue")); + + penColorLabel = new QLabel(tr("Pen &Color:")); + penColorLabel->setBuddy(penColorComboBox); + + rotationAngleSpinBox = new QSpinBox; + rotationAngleSpinBox->setRange(0, 359); + rotationAngleSpinBox->setWrapping(true); + rotationAngleSpinBox->setSuffix("\xB0"); + + rotationAngleLabel = new QLabel(tr("&Rotation Angle:")); + rotationAngleLabel->setBuddy(rotationAngleSpinBox); +//! [12] + +//! [16] + connect(fillRuleComboBox, SIGNAL(activated(int)), + this, SLOT(fillRuleChanged())); + connect(fillColor1ComboBox, SIGNAL(activated(int)), + this, SLOT(fillGradientChanged())); + connect(fillColor2ComboBox, SIGNAL(activated(int)), + this, SLOT(fillGradientChanged())); + connect(penColorComboBox, SIGNAL(activated(int)), + this, SLOT(penColorChanged())); + + for (int i = 0; i < NumRenderAreas; ++i) { + connect(penWidthSpinBox, SIGNAL(valueChanged(int)), + renderAreas[i], SLOT(setPenWidth(int))); + connect(rotationAngleSpinBox, SIGNAL(valueChanged(int)), + renderAreas[i], SLOT(setRotationAngle(int))); + } + +//! [16] //! [17] + QGridLayout *topLayout = new QGridLayout; + for (int i = 0; i < NumRenderAreas; ++i) + topLayout->addWidget(renderAreas[i], i / 3, i % 3); + + QGridLayout *mainLayout = new QGridLayout; + mainLayout->addLayout(topLayout, 0, 0, 1, 4); + mainLayout->addWidget(fillRuleLabel, 1, 0); + mainLayout->addWidget(fillRuleComboBox, 1, 1, 1, 3); + mainLayout->addWidget(fillGradientLabel, 2, 0); + mainLayout->addWidget(fillColor1ComboBox, 2, 1); + mainLayout->addWidget(fillToLabel, 2, 2); + mainLayout->addWidget(fillColor2ComboBox, 2, 3); + mainLayout->addWidget(penWidthLabel, 3, 0); + mainLayout->addWidget(penWidthSpinBox, 3, 1, 1, 3); + mainLayout->addWidget(penColorLabel, 4, 0); + mainLayout->addWidget(penColorComboBox, 4, 1, 1, 3); + mainLayout->addWidget(rotationAngleLabel, 5, 0); + mainLayout->addWidget(rotationAngleSpinBox, 5, 1, 1, 3); + setLayout(mainLayout); +//! [17] + +//! [18] + fillRuleChanged(); + fillGradientChanged(); + penColorChanged(); + penWidthSpinBox->setValue(2); + + setWindowTitle(tr("Painter Paths")); +} +//! [18] + +//! [19] +void Window::fillRuleChanged() +{ + Qt::FillRule rule = (Qt::FillRule)currentItemData(fillRuleComboBox).toInt(); + + for (int i = 0; i < NumRenderAreas; ++i) + renderAreas[i]->setFillRule(rule); +} +//! [19] + +//! [20] +void Window::fillGradientChanged() +{ + QColor color1 = qvariant_cast<QColor>(currentItemData(fillColor1ComboBox)); + QColor color2 = qvariant_cast<QColor>(currentItemData(fillColor2ComboBox)); + + for (int i = 0; i < NumRenderAreas; ++i) + renderAreas[i]->setFillGradient(color1, color2); +} +//! [20] + +//! [21] +void Window::penColorChanged() +{ + QColor color = qvariant_cast<QColor>(currentItemData(penColorComboBox)); + + for (int i = 0; i < NumRenderAreas; ++i) + renderAreas[i]->setPenColor(color); +} +//! [21] + +//! [22] +void Window::populateWithColors(QComboBox *comboBox) +{ + QStringList colorNames = QColor::colorNames(); + foreach (QString name, colorNames) + comboBox->addItem(name, QColor(name)); +} +//! [22] + +//! [23] +QVariant Window::currentItemData(QComboBox *comboBox) +{ + return comboBox->itemData(comboBox->currentIndex()); +} +//! [23] diff --git a/examples/painting/painterpaths/window.h b/examples/painting/painterpaths/window.h new file mode 100644 index 0000000..f2ad0ae --- /dev/null +++ b/examples/painting/painterpaths/window.h @@ -0,0 +1,93 @@ +/**************************************************************************** +** +** 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 <QWidget> + +QT_BEGIN_NAMESPACE +class QComboBox; +class QLabel; +class QSpinBox; +QT_END_NAMESPACE +class RenderArea; + +//! [0] +class Window : public QWidget +{ + Q_OBJECT + +public: + Window(); + +private slots: + void fillRuleChanged(); + void fillGradientChanged(); + void penColorChanged(); +//! [0] + +//! [1] +private: + void populateWithColors(QComboBox *comboBox); + QVariant currentItemData(QComboBox *comboBox); +//! [1] + +//! [2] + enum { NumRenderAreas = 9 }; + + RenderArea *renderAreas[NumRenderAreas]; + QLabel *fillRuleLabel; + QLabel *fillGradientLabel; + QLabel *fillToLabel; + QLabel *penWidthLabel; + QLabel *penColorLabel; + QLabel *rotationAngleLabel; + QComboBox *fillRuleComboBox; + QComboBox *fillColor1ComboBox; + QComboBox *fillColor2ComboBox; + QSpinBox *penWidthSpinBox; + QComboBox *penColorComboBox; + QSpinBox *rotationAngleSpinBox; +}; +//! [2] + +#endif diff --git a/examples/painting/painting.pro b/examples/painting/painting.pro new file mode 100644 index 0000000..b6fad5a --- /dev/null +++ b/examples/painting/painting.pro @@ -0,0 +1,16 @@ +TEMPLATE = subdirs +SUBDIRS = basicdrawing \ + concentriccircles \ + imagecomposition \ + painterpaths \ + transformations + +!wince*: SUBDIRS += fontsampler + +contains(QT_CONFIG, svg): SUBDIRS += svgviewer + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/painting +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS painting.pro README +sources.path = $$[QT_INSTALL_EXAMPLES]/painting +INSTALLS += target sources diff --git a/examples/painting/svgviewer/files/bubbles.svg b/examples/painting/svgviewer/files/bubbles.svg new file mode 100644 index 0000000..65867da --- /dev/null +++ b/examples/painting/svgviewer/files/bubbles.svg @@ -0,0 +1,215 @@ +<?xml version="1.0" standalone="no"?> +<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd"> +<svg width="20cm" height="15cm" viewBox="0 0 800 600" + xmlns="http://www.w3.org/2000/svg" + xmlns:xlink="http://www.w3.org/1999/xlink/" + baseProfile="tiny" version="1.2"> + <title>Spheres</title> + <desc>Semi-transparent bubbles on a colored background.</desc> + <defs> + <!-- Create radial gradients for each bubble. --> + <radialGradient id="blueBubble" gradientUnits="userSpaceOnUse" + cx="0" cy="0" r="100" fx="-50" fy="-50"> + <stop offset="0%" stop-color="white" stop-opacity="1" /> + <stop offset="25%" stop-color="#cdcdff" stop-opacity=".65" /> + <stop offset="100%" stop-color="#cdaacd" stop-opacity=".75" /> + </radialGradient> + <radialGradient id="redBubble" gradientUnits="userSpaceOnUse" + cx="0" cy="0" r="100" fx="-50" fy="-50"> + <stop offset="0%" stop-color="white" stop-opacity="1" /> + <stop offset="25%" stop-color="#ffcdcd" stop-opacity=".65" /> + <stop offset="100%" stop-color="#bbbb99" stop-opacity=".75" /> + </radialGradient> + <radialGradient id="greenBubble" gradientUnits="userSpaceOnUse" + cx="0" cy="0" r="100" fx="-50" fy="-50"> + <stop offset="0%" stop-color="white" stop-opacity="1" /> + <stop offset="25%" stop-color="#cdffcd" stop-opacity=".65" /> + <stop offset="100%" stop-color="#99aaaa" stop-opacity=".75" /> + </radialGradient> + <radialGradient id="yellowBubble" gradientUnits="userSpaceOnUse" + cx="0" cy="0" r="100" fx="-50" fy="-50"> + <stop offset="0%" stop-color="white" stop-opacity="1" /> + <stop offset="25%" stop-color="#ffffcd" stop-opacity=".65" /> + <stop offset="100%" stop-color="#bbbbaa" stop-opacity=".75" /> + </radialGradient> + <radialGradient id="background" gradientUnits="userSpaceOnUse" + cx="0" cy="0" r="400" fx="250" fy="250"> + <stop offset="0%" stop-color="#ffffee" /> + <stop offset="100%" stop-color="#ccccaa" /> + </radialGradient> + <linearGradient id="surface" gradientUnits="userSpaceOnUse" + x1="-100" y1="200" x2="400" y2="200"> + <stop offset="0%" stop-color="#ffffcc" /> + <stop offset="100%" stop-color="#bbbb88" /> + </linearGradient> + + <!-- Create radial gradients for each circle to make them look like + spheres. --> + <radialGradient id="blueSphere" gradientUnits="userSpaceOnUse" + cx="0" cy="0" r="100" fx="-50" fy="-50"> + <stop offset="0%" stop-color="white" /> + <stop offset="75%" stop-color="blue" /> + <stop offset="100%" stop-color="#222244" /> + </radialGradient> + <radialGradient id="redSphere" gradientUnits="userSpaceOnUse" + cx="0" cy="0" r="100" fx="-50" fy="-50"> + <stop offset="0%" stop-color="white" /> + <stop offset="75%" stop-color="red" /> + <stop offset="100%" stop-color="#442222" /> + </radialGradient> + <radialGradient id="greenSphere" gradientUnits="userSpaceOnUse" + cx="0" cy="0" r="100" fx="-50" fy="-50"> + <stop offset="0%" stop-color="white" /> + <stop offset="75%" stop-color="green" /> + <stop offset="100%" stop-color="#113311" /> + </radialGradient> + <radialGradient id="yellowSphere" gradientUnits="userSpaceOnUse" + cx="0" cy="0" r="100" fx="-50" fy="-50"> + <stop offset="0%" stop-color="white" /> + <stop offset="75%" stop-color="yellow" /> + <stop offset="100%" stop-color="#444422" /> + </radialGradient> + <radialGradient id="shadowGrad" gradientUnits="userSpaceOnUse" + cx="0" cy="0" r="100" fx="-50" fy="50"> + <stop offset="0%" stop-color="black" stop-opacity="1.0" /> + <stop offset="100%" stop-color="white" stop-opacity="0.0" /> + </radialGradient> + + <!-- Define a shadow for each sphere. --> + <circle id="shadow" fill="url(#shadowGrad)" cx="0" cy="0" r="100" /> + <g id="bubble"> + <circle fill="black" cx="0" cy="0" r="50" /> + <circle fill="#a6ce39" cx="0" cy="0" r="33" /> + <path fill="black" d="M 37,50 L 50,37 L 12,-1 L 22,-11 L 10,-24 L -24,10 + L -11,22 L -1,12 Z" /> + <circle cx="0" cy="0" r="100" /> + </g> + </defs> + <g> + <rect fill="url(#background)" x="0" y="0" width="800" height="600" /> + </g> + + <g transform="translate(200,700)"> + <use xlink:href="#bubble" fill="url(#blueBubble)" /> + <animateTransform attributeName="transform" type="translate" + values="0,0; 0,-800" begin="1s" dur="10s" fill="freeze" repeatCount="indefinite" /> + </g> + <g transform="translate(315,700)"> + <g transform="scale(0.5,0.5)"> + <use xlink:href="#bubble" fill="url(#redBubble)" /> + </g> + <animateTransform attributeName="transform" type="translate" + values="0,0; 0,-800" begin="3s" dur="7s" fill="freeze" repeatCount="indefinite" /> + </g> + <g transform="translate(80,700)"> + <g transform="scale(0.65,0.65)"> + <use xlink:href="#bubble" fill="url(#greenBubble)" /> + </g> + <animateTransform attributeName="transform" type="translate" + values="0,0; 0,-800" begin="5s" dur="9s" fill="freeze" repeatCount="indefinite" /> + </g> + <g transform="translate(255,700)"> + <g transform="scale(0.3,0.3)"> + <use xlink:href="#bubble" fill="url(#yellowBubble)" /> + </g> + <animateTransform attributeName="transform" type="translate" + values="0,0; 0,-800" begin="2s" dur="6s" fill="freeze" repeatCount="indefinite" /> + </g> + <g transform="translate(565,700)"> + <g transform="scale(0.4,0.4)"> + <use xlink:href="#bubble" fill="url(#blueBubble)" /> + </g> + <animateTransform attributeName="transform" type="translate" + values="0,0; 0,-800" begin="4s" dur="8s" fill="freeze" repeatCount="indefinite" /> + </g> + <g transform="translate(715,700)"> + <g transform="scale(0.6,0.6)"> + <use xlink:href="#bubble" fill="url(#redBubble)" /> + </g> + <animateTransform attributeName="transform" type="translate" + values="0,0; 0,-800" begin="1s" dur="4s" fill="freeze" repeatCount="indefinite" /> + </g> + <g transform="translate(645,700)"> + <g transform="scale(0.375,0.375)"> + <use xlink:href="#bubble" fill="url(#greenBubble)" /> + </g> + <animateTransform attributeName="transform" type="translate" + values="0,0; 0,-800" begin="0s" dur="11s" fill="freeze" repeatCount="indefinite" /> + </g> + <g transform="translate(555,700)"> + <g transform="scale(0.9,0.9)"> + <use xlink:href="#bubble" fill="url(#yellowBubble)" /> + </g> + <animateTransform attributeName="transform" type="translate" + values="0,0; 0,-800" begin="3s" dur="7.5s" fill="freeze" repeatCount="indefinite" /> + </g> + + <g transform="translate(360,700)"> + <g transform="scale(0.5,0.5)"> + <use xlink:href="#bubble" fill="url(#blueBubble)" /> + </g> + <animateTransform attributeName="transform" type="translate" + values="0,0; 0,-800" begin="3s" dur="6s" fill="freeze" repeatCount="indefinite" /> + </g> + <g transform="translate(215,700)"> + <g transform="scale(0.45,0.45)"> + <use xlink:href="#bubble" fill="url(#redBubble)" /> + </g> + <animateTransform attributeName="transform" type="translate" + values="0,0; 0,-800" begin="5.5s" dur="7s" fill="freeze" repeatCount="indefinite" /> + </g> + <g transform="translate(420,700)"> + <g transform="scale(0.75,0.75)"> + <use xlink:href="#bubble" fill="url(#greenBubble)" /> + </g> + <animateTransform attributeName="transform" type="translate" + values="0,0; 0,-800" begin="1s" dur="9s" fill="freeze" repeatCount="indefinite" /> + </g> + <g transform="translate(815,700)"> + <g transform="scale(0.6,0.6)"> + <use xlink:href="#bubble" fill="url(#yellowBubble)" /> + </g> + <animateTransform attributeName="transform" type="translate" + values="0,0; 0,-800" begin="2s" dur="9.5s" fill="freeze" repeatCount="indefinite" /> + </g> + + <g transform="translate(225,375)" > + <g transform="scale(1.0,0.5)" > + <path d="M 0 0 L 350 0 L 450 450 L -100 450 z" + fill="url(#surface)" stroke="none" /> + </g> + </g> + <g transform="translate(200,0)" > + <g transform="translate(200,490) scale(2.0,1.0) rotate(45)" > + <rect fill="#a6ce39" x="-69" y="-69" width="138" height="138" /> + <circle fill="black" cx="0" cy="0" r="50" /> + <circle fill="#a6ce39" cx="0" cy="0" r="33" /> + <path fill="black" d="M 37,50 L 50,37 L 12,-1 L 22,-11 L 10,-24 L -24,10 + L -11,22 L -1,12 Z" /> + <animateTransform attributeName="transform" type="rotate" values="0; 360" + begin="0s" dur="10s" fill="freeze" repeatCount="indefinite" /> + </g> + <g transform="translate(200,375)"> + <use xlink:href="#shadow" transform="translate(25,55) scale(1.0,0.5)" /> + <circle fill="url(#blueSphere)" cx="0" cy="0" r="100" /> + </g> + <g transform="translate(315,440)"> + <g transform="scale(0.5,0.5)"> + <use xlink:href="#shadow" transform="translate(25,55) scale(1.0,0.5)" /> + <circle fill="url(#redSphere)" cx="0" cy="0" r="100" /> + </g> + </g> + <g transform="translate(80,475)"> + <g transform="scale(0.65,0.65)"> + <use xlink:href="#shadow" transform="translate(25,55) scale(1.0,0.5)" /> + <circle fill="url(#greenSphere)" cx="0" cy="0" r="100" /> + </g> + </g> + <g transform="translate(255,525)"> + <g transform="scale(0.3,0.3)"> + <use xlink:href="#shadow" transform="translate(25,55) scale(1.0,0.5)" /> + <circle fill="url(#yellowSphere)" cx="0" cy="0" r="100" /> + </g> + </g> + </g> +</svg> diff --git a/examples/painting/svgviewer/files/cubic.svg b/examples/painting/svgviewer/files/cubic.svg new file mode 100644 index 0000000..492bb72 --- /dev/null +++ b/examples/painting/svgviewer/files/cubic.svg @@ -0,0 +1,77 @@ +<?xml version="1.0" standalone="no"?> +<svg width="10cm" height="10cm" viewBox="0 0 1000 1000" + xmlns="http://www.w3.org/2000/svg" version="1.2" baseProfile="tiny"> + <title>Example cubic02 - cubic Bezier commands in path data</title> + <desc>Picture showing examples of "C" and "S" commands, + along with annotations showing the control points + and end points</desc> + + <rect fill="none" stroke="blue" stroke-width="1" x="1" y="1" width="998" height="998" /> + + <!-- Path 1 --> + <polyline fill="none" stroke="#888888" stroke-width="2" points="100,200 100,100" /> + <polyline fill="none" stroke="#888888" stroke-width="2" points="400,100 400,200" /> + <path fill="none" stroke="red" stroke-width="5" d="M100,200 C100,100 400,100 400,200" /> + <circle fill="none" stroke="#888888" stroke-width="2" cx="100" cy="200" r="10" /> + <circle fill="none" stroke="#888888" stroke-width="2" cx="400" cy="200" r="10" /> + <circle class="CtlPoint" cx="100" cy="100" r="10" /> + <circle class="CtlPoint" cx="400" cy="100" r="10" /> + <text text-anchor="middle" font-size="22" font-family="Verdana" x="250" y="275">M100,200 C100,100 400,100 400,200</text> + + <!-- Path 2 --> + <polyline fill="none" stroke="#888888" stroke-width="2" points="100,500 25,400" /> + <polyline fill="none" stroke="#888888" stroke-width="2" points="475,400 400,500" /> + <path fill="none" stroke="red" stroke-width="5" d="M100,500 C25,400 475,400 400,500" /> + <circle fill="none" stroke="#888888" stroke-width="2" cx="100" cy="500" r="10" /> + <circle fill="none" stroke="#888888" stroke-width="2" cx="400" cy="500" r="10" /> + <circle fill="#888888" stroke="none" cx="25" cy="400" r="10" /> + <circle fill="#888888" stroke="none" cx="475" cy="400" r="10" /> + <text text-anchor="middle" font-size="22" font-family="Verdana" x="250" y="575">M100,500 C25,400 475,400 400,500</text> + + <!-- Path 3 --> + <polyline fill="none" stroke="#888888" stroke-width="2" points="100,800 175,700" /> + <polyline fill="none" stroke="#888888" stroke-width="2" points="325,700 400,800" /> + <path fill="none" stroke="red" stroke-width="5" d="M100,800 C175,700 325,700 400,800" /> + <circle fill="none" stroke="#888888" stroke-width="2" cx="100" cy="800" r="10" /> + <circle fill="none" stroke="#888888" stroke-width="2" cx="400" cy="800" r="10" /> + <circle fill="#888888" stroke="none" cx="175" cy="700" r="10" /> + <circle fill="#888888" stroke="none" cx="325" cy="700" r="10" /> + <text text-anchor="middle" font-size="22" font-family="Verdana" x="250" y="875">M100,800 C175,700 325,700 400,800</text> + + <!-- Path 4 --> + <polyline fill="none" stroke="#888888" stroke-width="2" points="600,200 675,100" /> + <polyline fill="none" stroke="#888888" stroke-width="2" points="975,100 900,200" /> + <path fill="none" stroke="red" stroke-width="5" d="M600,200 C675,100 975,100 900,200" /> + <circle fill="none" stroke="#888888" stroke-width="2" cx="600" cy="200" r="10" /> + <circle fill="none" stroke="#888888" stroke-width="2" cx="900" cy="200" r="10" /> + <circle fill="#888888" stroke="none" cx="675" cy="100" r="10" /> + <circle fill="#888888" stroke="none" cx="975" cy="100" r="10" /> + <text text-anchor="middle" font-size="22" font-family="Verdana" x="750" y="275">M600,200 C675,100 975,100 900,200</text> + + <!-- Path 5 --> + <polyline fill="none" stroke="#888888" stroke-width="2" points="600,500 600,350" /> + <polyline fill="none" stroke="#888888" stroke-width="2" points="900,650 900,500" /> + <path fill="none" stroke="red" stroke-width="5" d="M600,500 C600,350 900,650 900,500" /> + <circle fill="none" stroke="#888888" stroke-width="2" cx="600" cy="500" r="10" /> + <circle fill="none" stroke="#888888" stroke-width="2" cx="900" cy="500" r="10" /> + <circle fill="#888888" stroke="none" cx="600" cy="350" r="10" /> + <circle fill="#888888" stroke="none" cx="900" cy="650" r="10" /> + <text text-anchor="middle" font-size="22" font-family="Verdana" x="750" y="575">M600,500 C600,350 900,650 900,500</text> + + <!-- Path 6 (C and S command) --> + <polyline fill="none" stroke="#888888" stroke-width="2" points="600,800 625,700" /> + <polyline fill="none" stroke="#888888" stroke-width="2" points="725,700 750,800" /> + <polyline fill="none" stroke="#888888" stroke-width="2" points="750,800 775,900" /> + <polyline fill="none" stroke="#888888" stroke-width="2" points="875,900 900,800" /> + <path fill="none" stroke="red" stroke-width="5" d="M600,800 C625,700 725,700 750,800 + S875,900 900,800" /> + <circle fill="none" stroke="#888888" stroke-width="2" cx="600" cy="800" r="10" /> + <circle fill="none" stroke="#888888" stroke-width="2" cx="750" cy="800" r="10" /> + <circle fill="none" stroke="#888888" stroke-width="2" cx="900" cy="800" r="10" /> + <circle fill="#888888" stroke="none" cx="625" cy="700" r="10" /> + <circle fill="#888888" stroke="none" cx="725" cy="700" r="10" /> + <circle fill="#888888" stroke="none" cx="875" cy="900" r="10" /> + <circle fill="none" stroke="blue" stroke-width="4" cx="775" cy="900" r="9" /> + <text text-anchor="middle" font-size="22" font-family="Verdana" x="750" y="945">M600,800 C625,700 725,700 750,800</text> + <text text-anchor="middle" font-size="22" font-family="Verdana" x="750" y="975">S875,900 900,800</text> +</svg> diff --git a/examples/painting/svgviewer/files/spheres.svg b/examples/painting/svgviewer/files/spheres.svg new file mode 100644 index 0000000..b23164b --- /dev/null +++ b/examples/painting/svgviewer/files/spheres.svg @@ -0,0 +1,72 @@ +<?xml version="1.0" standalone="no"?> +<svg width="8cm" height="8cm" viewBox="0 0 400 400" + xmlns="http://www.w3.org/2000/svg" + xmlns:xlink="http://www.w3.org/1999/xlink/" + baseProfile="tiny" version="1.2"> + <title>Spheres</title> + <desc>Gradient filled spheres with different colors.</desc> + <defs> + <!-- Create radial gradients for each circle to make them look like + spheres. --> + <radialGradient id="blueSphere" gradientUnits="userSpaceOnUse" + cx="0" cy="0" r="100" fx="-50" fy="-50"> + <stop offset="0%" stop-color="white" /> + <stop offset="75%" stop-color="blue" /> + <stop offset="100%" stop-color="#222244" /> + </radialGradient> + <radialGradient id="redSphere" gradientUnits="userSpaceOnUse" + cx="0" cy="0" r="100" fx="-50" fy="-50"> + <stop offset="0%" stop-color="white" /> + <stop offset="75%" stop-color="red" /> + <stop offset="100%" stop-color="#442222" /> + </radialGradient> + <radialGradient id="greenSphere" gradientUnits="userSpaceOnUse" + cx="0" cy="0" r="100" fx="-50" fy="-50"> + <stop offset="0%" stop-color="white" /> + <stop offset="75%" stop-color="green" /> + <stop offset="100%" stop-color="#113311" /> + </radialGradient> + <radialGradient id="yellowSphere" gradientUnits="userSpaceOnUse" + cx="0" cy="0" r="100" fx="-50" fy="-50"> + <stop offset="0%" stop-color="white" /> + <stop offset="75%" stop-color="yellow" /> + <stop offset="100%" stop-color="#444422" /> + </radialGradient> + <radialGradient id="shadowGrad" gradientUnits="userSpaceOnUse" + cx="0" cy="0" r="100" fx="-50" fy="50"> + <stop offset="0%" stop-color="black" stop-opacity="1.0" /> + <stop offset="100%" stop-color="white" stop-opacity="0.0" /> + </radialGradient> + + <!-- Define a shadow for each sphere. --> + <circle id="shadow" fill="url(#shadowGrad)" cx="0" cy="0" r="100" /> + </defs> + <g fill="#ffee99" stroke="none" > + <rect x="0" y="0" width="400" height="400" /> + </g> + <g fill="white" stroke="none" > + <rect x="0" y="175" width="400" height="225" /> + </g> + <g transform="translate(200,175)"> + <use xlink:href="#shadow" transform="translate(25,55) scale(1.0,0.5)" /> + <circle fill="url(#blueSphere)" cx="0" cy="0" r="100" /> + </g> + <g transform="translate(315,240)"> + <g transform="scale(0.5,0.5)"> + <use xlink:href="#shadow" transform="translate(25,55) scale(1.0,0.5)" /> + <circle fill="url(#redSphere)" cx="0" cy="0" r="100" /> + </g> + </g> + <g transform="translate(80,275)"> + <g transform="scale(0.65,0.65)"> + <use xlink:href="#shadow" transform="translate(25,55) scale(1.0,0.5)" /> + <circle fill="url(#greenSphere)" cx="0" cy="0" r="100" /> + </g> + </g> + <g transform="translate(255,325)"> + <g transform="scale(0.3,0.3)"> + <use xlink:href="#shadow" transform="translate(25,55) scale(1.0,0.5)" /> + <circle fill="url(#yellowSphere)" cx="0" cy="0" r="100" /> + </g> + </g> +</svg> diff --git a/examples/painting/svgviewer/main.cpp b/examples/painting/svgviewer/main.cpp new file mode 100644 index 0000000..18520f7 --- /dev/null +++ b/examples/painting/svgviewer/main.cpp @@ -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 <QApplication> +#include <QString> +#ifndef QT_NO_OPENGL +#include <QGLFormat> +#endif + +#include "mainwindow.h" + +int main(int argc, char **argv) +{ + Q_INIT_RESOURCE(svgviewer); + + QApplication app(argc, argv); + + MainWindow window; + if (argc == 2) + window.openFile(argv[1]); + else + window.openFile(":/files/bubbles.svg"); + window.show(); + return app.exec(); +} diff --git a/examples/painting/svgviewer/mainwindow.cpp b/examples/painting/svgviewer/mainwindow.cpp new file mode 100644 index 0000000..22393eb --- /dev/null +++ b/examples/painting/svgviewer/mainwindow.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 "mainwindow.h" + +#include <QtGui> + +#include "svgview.h" + +MainWindow::MainWindow() + : QMainWindow() + , m_view(new SvgView) +{ + QMenu *fileMenu = new QMenu(tr("&File"), this); + QAction *openAction = fileMenu->addAction(tr("&Open...")); + openAction->setShortcut(QKeySequence(tr("Ctrl+O"))); + QAction *quitAction = fileMenu->addAction(tr("E&xit")); + quitAction->setShortcut(QKeySequence(tr("Ctrl+Q"))); + + menuBar()->addMenu(fileMenu); + + QMenu *viewMenu = new QMenu(tr("&View"), this); + m_backgroundAction = viewMenu->addAction(tr("&Background")); + m_backgroundAction->setEnabled(false); + m_backgroundAction->setCheckable(true); + m_backgroundAction->setChecked(false); + connect(m_backgroundAction, SIGNAL(toggled(bool)), m_view, SLOT(setViewBackground(bool))); + + m_outlineAction = viewMenu->addAction(tr("&Outline")); + m_outlineAction->setEnabled(false); + m_outlineAction->setCheckable(true); + m_outlineAction->setChecked(true); + connect(m_outlineAction, SIGNAL(toggled(bool)), m_view, SLOT(setViewOutline(bool))); + + menuBar()->addMenu(viewMenu); + + QMenu *rendererMenu = new QMenu(tr("&Renderer"), this); + m_nativeAction = rendererMenu->addAction(tr("&Native")); + m_nativeAction->setCheckable(true); + m_nativeAction->setChecked(true); +#ifndef QT_NO_OPENGL + m_glAction = rendererMenu->addAction(tr("&OpenGL")); + m_glAction->setCheckable(true); +#endif + m_imageAction = rendererMenu->addAction(tr("&Image")); + m_imageAction->setCheckable(true); + +#ifndef QT_NO_OPENGL + rendererMenu->addSeparator(); + m_highQualityAntialiasingAction = rendererMenu->addAction(tr("&High Quality Antialiasing")); + m_highQualityAntialiasingAction->setEnabled(false); + m_highQualityAntialiasingAction->setCheckable(true); + m_highQualityAntialiasingAction->setChecked(false); + connect(m_highQualityAntialiasingAction, SIGNAL(toggled(bool)), m_view, SLOT(setHighQualityAntialiasing(bool))); +#endif + + QActionGroup *rendererGroup = new QActionGroup(this); + rendererGroup->addAction(m_nativeAction); +#ifndef QT_NO_OPENGL + rendererGroup->addAction(m_glAction); +#endif + rendererGroup->addAction(m_imageAction); + + menuBar()->addMenu(rendererMenu); + + connect(openAction, SIGNAL(triggered()), this, SLOT(openFile())); + connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit())); + connect(rendererGroup, SIGNAL(triggered(QAction *)), + this, SLOT(setRenderer(QAction *))); + + setCentralWidget(m_view); + setWindowTitle(tr("SVG Viewer")); +} + +void MainWindow::openFile(const QString &path) +{ + QString fileName; + if (path.isNull()) + fileName = QFileDialog::getOpenFileName(this, tr("Open SVG File"), + m_currentPath, "SVG files (*.svg *.svgz *.svg.gz)"); + else + fileName = path; + + if (!fileName.isEmpty()) { + QFile file(fileName); + if (!file.exists()) { + QMessageBox::critical(this, tr("Open SVG File"), + QString("Could not open file '%1'.").arg(fileName)); + + m_outlineAction->setEnabled(false); + m_backgroundAction->setEnabled(false); + return; + } + + m_view->openFile(file); + + if (!fileName.startsWith(":/")) { + m_currentPath = fileName; + setWindowTitle(tr("%1 - SVGViewer").arg(m_currentPath)); + } + + m_outlineAction->setEnabled(true); + m_backgroundAction->setEnabled(true); + + resize(m_view->sizeHint() + QSize(80, 80 + menuBar()->height())); + } +} + +void MainWindow::setRenderer(QAction *action) +{ +#ifndef QT_NO_OPENGL + m_highQualityAntialiasingAction->setEnabled(false); +#endif + + if (action == m_nativeAction) + m_view->setRenderer(SvgView::Native); +#ifndef QT_NO_OPENGL + else if (action == m_glAction) { + m_highQualityAntialiasingAction->setEnabled(true); + m_view->setRenderer(SvgView::OpenGL); + } +#endif + else if (action == m_imageAction) { + m_view->setRenderer(SvgView::Image); + } +} diff --git a/examples/painting/svgviewer/mainwindow.h b/examples/painting/svgviewer/mainwindow.h new file mode 100644 index 0000000..5df430b --- /dev/null +++ b/examples/painting/svgviewer/mainwindow.h @@ -0,0 +1,81 @@ +/**************************************************************************** +** +** 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 <QString> + +class SvgView; + +QT_BEGIN_NAMESPACE +class QAction; +class QGraphicsView; +class QGraphicsScene; +class QGraphicsRectItem; +QT_END_NAMESPACE + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + MainWindow(); + +public slots: + void openFile(const QString &path = QString()); + void setRenderer(QAction *action); + +private: + QAction *m_nativeAction; + QAction *m_glAction; + QAction *m_imageAction; + QAction *m_highQualityAntialiasingAction; + QAction *m_backgroundAction; + QAction *m_outlineAction; + + SvgView *m_view; + + QString m_currentPath; +}; + +#endif diff --git a/examples/painting/svgviewer/svgview.cpp b/examples/painting/svgviewer/svgview.cpp new file mode 100644 index 0000000..daf7bf0 --- /dev/null +++ b/examples/painting/svgviewer/svgview.cpp @@ -0,0 +1,188 @@ +/**************************************************************************** +** +** 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 "svgview.h" + +#include <QFile> +#include <QWheelEvent> +#include <QMouseEvent> +#include <QGraphicsRectItem> +#include <QGraphicsSvgItem> +#include <QPaintEvent> +#include <qmath.h> + +#ifndef QT_NO_OPENGL +#include <QGLWidget> +#endif + +SvgView::SvgView(QWidget *parent) + : QGraphicsView(parent) + , m_renderer(Native) + , m_svgItem(0) + , m_backgroundItem(0) + , m_outlineItem(0) +{ + setScene(new QGraphicsScene(this)); + setTransformationAnchor(AnchorUnderMouse); + setDragMode(ScrollHandDrag); + + // Prepare background check-board pattern + QPixmap tilePixmap(64, 64); + tilePixmap.fill(Qt::white); + QPainter tilePainter(&tilePixmap); + QColor color(220, 220, 220); + tilePainter.fillRect(0, 0, 32, 32, color); + tilePainter.fillRect(32, 32, 32, 32, color); + tilePainter.end(); + + setBackgroundBrush(tilePixmap); +} + +void SvgView::drawBackground(QPainter *p, const QRectF &) +{ + p->save(); + p->resetTransform(); + p->drawTiledPixmap(viewport()->rect(), backgroundBrush().texture()); + p->restore(); +} + +void SvgView::openFile(const QFile &file) +{ + if (!file.exists()) + return; + + QGraphicsScene *s = scene(); + + bool drawBackground = (m_backgroundItem ? m_backgroundItem->isVisible() : false); + bool drawOutline = (m_outlineItem ? m_outlineItem->isVisible() : true); + + s->clear(); + resetTransform(); + + m_svgItem = new QGraphicsSvgItem(file.fileName()); + m_svgItem->setFlags(QGraphicsItem::ItemClipsToShape); + m_svgItem->setCacheMode(QGraphicsItem::NoCache); + m_svgItem->setZValue(0); + + m_backgroundItem = new QGraphicsRectItem(m_svgItem->boundingRect()); + m_backgroundItem->setBrush(Qt::white); + m_backgroundItem->setPen(Qt::NoPen); + m_backgroundItem->setVisible(drawBackground); + m_backgroundItem->setZValue(-1); + + m_outlineItem = new QGraphicsRectItem(m_svgItem->boundingRect()); + QPen outline(Qt::black, 2, Qt::DashLine); + outline.setCosmetic(true); + m_outlineItem->setPen(outline); + m_outlineItem->setBrush(Qt::NoBrush); + m_outlineItem->setVisible(drawOutline); + m_outlineItem->setZValue(1); + + s->addItem(m_backgroundItem); + s->addItem(m_svgItem); + s->addItem(m_outlineItem); + + s->setSceneRect(m_outlineItem->boundingRect().adjusted(-10, -10, 10, 10)); +} + +void SvgView::setRenderer(RendererType type) +{ + m_renderer = type; + + if (m_renderer == OpenGL) { +#ifndef QT_NO_OPENGL + setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers))); +#endif + } else { + setViewport(new QWidget); + } +} + +void SvgView::setHighQualityAntialiasing(bool highQualityAntialiasing) +{ +#ifndef QT_NO_OPENGL + setRenderHint(QPainter::HighQualityAntialiasing, highQualityAntialiasing); +#else + Q_UNUSED(highQualityAntialiasing); +#endif +} + +void SvgView::setViewBackground(bool enable) +{ + if (!m_backgroundItem) + return; + + m_backgroundItem->setVisible(enable); +} + +void SvgView::setViewOutline(bool enable) +{ + if (!m_outlineItem) + return; + + m_outlineItem->setVisible(enable); +} + +void SvgView::paintEvent(QPaintEvent *event) +{ + if (m_renderer == Image) { + if (m_image.size() != viewport()->size()) { + m_image = QImage(viewport()->size(), QImage::Format_ARGB32_Premultiplied); + } + + QPainter imagePainter(&m_image); + QGraphicsView::render(&imagePainter); + imagePainter.end(); + + QPainter p(viewport()); + p.drawImage(0, 0, m_image); + + } else { + QGraphicsView::paintEvent(event); + } +} + +void SvgView::wheelEvent(QWheelEvent *event) +{ + qreal factor = qPow(1.2, event->delta() / 240.0); + scale(factor, factor); + event->accept(); +} + diff --git a/examples/painting/svgviewer/svgview.h b/examples/painting/svgviewer/svgview.h new file mode 100644 index 0000000..993268a --- /dev/null +++ b/examples/painting/svgviewer/svgview.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 SVGVIEW_H +#define SVGVIEW_H + +#include <QGraphicsView> + +QT_BEGIN_NAMESPACE +class QWheelEvent; +class QPaintEvent; +class QFile; +QT_END_NAMESPACE + +class SvgView : public QGraphicsView +{ + Q_OBJECT + +public: + enum RendererType { Native, OpenGL, Image }; + + SvgView(QWidget *parent = 0); + + void openFile(const QFile &file); + void setRenderer(RendererType type = Native); + void drawBackground(QPainter *p, const QRectF &rect); + +public slots: + void setHighQualityAntialiasing(bool highQualityAntialiasing); + void setViewBackground(bool enable); + void setViewOutline(bool enable); + +protected: + void wheelEvent(QWheelEvent *event); + void paintEvent(QPaintEvent *event); + +private: + RendererType m_renderer; + + QGraphicsItem *m_svgItem; + QGraphicsRectItem *m_backgroundItem; + QGraphicsRectItem *m_outlineItem; + + QImage m_image; +}; +#endif // SVGVIEW_H diff --git a/examples/painting/svgviewer/svgviewer.pro b/examples/painting/svgviewer/svgviewer.pro new file mode 100644 index 0000000..b29e218 --- /dev/null +++ b/examples/painting/svgviewer/svgviewer.pro @@ -0,0 +1,23 @@ +HEADERS = mainwindow.h \ + svgview.h +RESOURCES = svgviewer.qrc +SOURCES = main.cpp \ + mainwindow.cpp \ + svgview.cpp +QT += svg xml + +contains(QT_CONFIG, opengl): QT += opengl + +CONFIG += console + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/painting/svgviewer +sources.files = $$SOURCES $$HEADERS $$RESOURCES svgviewer.pro files +sources.path = $$[QT_INSTALL_EXAMPLES]/painting/svgviewer +INSTALLS += target sources + +wince*: { + addFiles.sources = files\*.svg + addFiles.path = \My Documents + DEPLOYMENT += addFiles +} diff --git a/examples/painting/svgviewer/svgviewer.qrc b/examples/painting/svgviewer/svgviewer.qrc new file mode 100644 index 0000000..db611f5 --- /dev/null +++ b/examples/painting/svgviewer/svgviewer.qrc @@ -0,0 +1,6 @@ +<!DOCTYPE RCC><RCC version="1.0"> +<qresource prefix="/"> + <file>files/bubbles.svg</file> +</qresource> +</RCC> + diff --git a/examples/painting/transformations/main.cpp b/examples/painting/transformations/main.cpp new file mode 100644 index 0000000..fa8b0ab --- /dev/null +++ b/examples/painting/transformations/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 <QApplication> + +#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/transformations/renderarea.cpp b/examples/painting/transformations/renderarea.cpp new file mode 100644 index 0000000..a25869b --- /dev/null +++ b/examples/painting/transformations/renderarea.cpp @@ -0,0 +1,173 @@ +/**************************************************************************** +** +** 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 "renderarea.h" + +//! [0] +RenderArea::RenderArea(QWidget *parent) + : QWidget(parent) +{ + QFont newFont = font(); + newFont.setPixelSize(12); + setFont(newFont); + + QFontMetrics fontMetrics(newFont); + xBoundingRect = fontMetrics.boundingRect(tr("x")); + yBoundingRect = fontMetrics.boundingRect(tr("y")); +} +//! [0] + +//! [1] +void RenderArea::setOperations(const QList<Operation> &operations) +{ + this->operations = operations; + update(); +} +//! [1] + +//! [2] +void RenderArea::setShape(const QPainterPath &shape) +{ + this->shape = shape; + update(); +} +//! [2] + +//! [3] +QSize RenderArea::minimumSizeHint() const +{ + return QSize(182, 182); +} +//! [3] + +//! [4] +QSize RenderArea::sizeHint() const +{ + return QSize(232, 232); +} +//! [4] + +//! [5] +void RenderArea::paintEvent(QPaintEvent *event) +{ + QPainter painter(this); + painter.setRenderHint(QPainter::Antialiasing); + painter.fillRect(event->rect(), QBrush(Qt::white)); + + painter.translate(66, 66); +//! [5] + +//! [6] + painter.save(); + transformPainter(painter); + drawShape(painter); + painter.restore(); +//! [6] + +//! [7] + drawOutline(painter); +//! [7] + +//! [8] + transformPainter(painter); + drawCoordinates(painter); +} +//! [8] + +//! [9] +void RenderArea::drawCoordinates(QPainter &painter) +{ + painter.setPen(Qt::red); + + painter.drawLine(0, 0, 50, 0); + painter.drawLine(48, -2, 50, 0); + painter.drawLine(48, 2, 50, 0); + painter.drawText(60 - xBoundingRect.width() / 2, + 0 + xBoundingRect.height() / 2, tr("x")); + + painter.drawLine(0, 0, 0, 50); + painter.drawLine(-2, 48, 0, 50); + painter.drawLine(2, 48, 0, 50); + painter.drawText(0 - yBoundingRect.width() / 2, + 60 + yBoundingRect.height() / 2, tr("y")); +} +//! [9] + +//! [10] +void RenderArea::drawOutline(QPainter &painter) +{ + painter.setPen(Qt::darkGreen); + painter.setPen(Qt::DashLine); + painter.setBrush(Qt::NoBrush); + painter.drawRect(0, 0, 100, 100); +} +//! [10] + +//! [11] +void RenderArea::drawShape(QPainter &painter) +{ + painter.fillPath(shape, Qt::blue); +} +//! [11] + +//! [12] +void RenderArea::transformPainter(QPainter &painter) +{ + for (int i = 0; i < operations.size(); ++i) { + switch (operations[i]) { + case Translate: + painter.translate(50, 50); + break; + case Scale: + painter.scale(0.75, 0.75); + break; + case Rotate: + painter.rotate(60); + break; + case NoTransformation: + default: + ; + } + } +} +//! [12] diff --git a/examples/painting/transformations/renderarea.h b/examples/painting/transformations/renderarea.h new file mode 100644 index 0000000..04ab0d3 --- /dev/null +++ b/examples/painting/transformations/renderarea.h @@ -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$ +** +****************************************************************************/ + +#ifndef RENDERAREA_H +#define RENDERAREA_H + +#include <QFont> +#include <QList> +#include <QPainterPath> +#include <QRect> +#include <QWidget> + +QT_BEGIN_NAMESPACE +class QPaintEvent; +QT_END_NAMESPACE + +//! [0] +enum Operation { NoTransformation, Translate, Rotate, Scale }; +//! [0] + +//! [1] +class RenderArea : public QWidget +{ + Q_OBJECT + +public: + RenderArea(QWidget *parent = 0); + + void setOperations(const QList<Operation> &operations); + void setShape(const QPainterPath &shape); + + QSize minimumSizeHint() const; + QSize sizeHint() const; + +protected: + void paintEvent(QPaintEvent *event); +//! [1] + +//! [2] +private: + void drawCoordinates(QPainter &painter); + void drawOutline(QPainter &painter); + void drawShape(QPainter &painter); + void transformPainter(QPainter &painter); + + QList<Operation> operations; + QPainterPath shape; + QRect xBoundingRect; + QRect yBoundingRect; +}; +//! [2] + +#endif diff --git a/examples/painting/transformations/transformations.pro b/examples/painting/transformations/transformations.pro new file mode 100644 index 0000000..a8ff610 --- /dev/null +++ b/examples/painting/transformations/transformations.pro @@ -0,0 +1,11 @@ +HEADERS = renderarea.h \ + window.h +SOURCES = main.cpp \ + renderarea.cpp \ + window.cpp + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/painting/transformations +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS transformations.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/painting/transformations +INSTALLS += target sources diff --git a/examples/painting/transformations/window.cpp b/examples/painting/transformations/window.cpp new file mode 100644 index 0000000..7d83654 --- /dev/null +++ b/examples/painting/transformations/window.cpp @@ -0,0 +1,181 @@ +/**************************************************************************** +** +** 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" + +//! [0] +Window::Window() +{ + originalRenderArea = new RenderArea; + + shapeComboBox = new QComboBox; + shapeComboBox->addItem(tr("Clock")); + shapeComboBox->addItem(tr("House")); + shapeComboBox->addItem(tr("Text")); + shapeComboBox->addItem(tr("Truck")); + + QGridLayout *layout = new QGridLayout; + layout->addWidget(originalRenderArea, 0, 0); + layout->addWidget(shapeComboBox, 1, 0); +//! [0] + +//! [1] + for (int i = 0; i < NumTransformedAreas; ++i) { + transformedRenderAreas[i] = new RenderArea; + + operationComboBoxes[i] = new QComboBox; + operationComboBoxes[i]->addItem(tr("No transformation")); + operationComboBoxes[i]->addItem(tr("Rotate by 60\xB0")); + operationComboBoxes[i]->addItem(tr("Scale to 75%")); + operationComboBoxes[i]->addItem(tr("Translate by (50, 50)")); + + connect(operationComboBoxes[i], SIGNAL(activated(int)), + this, SLOT(operationChanged())); + + layout->addWidget(transformedRenderAreas[i], 0, i + 1); + layout->addWidget(operationComboBoxes[i], 1, i + 1); + } +//! [1] + +//! [2] + setLayout(layout); + setupShapes(); + shapeSelected(0); + + setWindowTitle(tr("Transformations")); +} +//! [2] + +//! [3] +void Window::setupShapes() +{ + QPainterPath truck; +//! [3] + truck.setFillRule(Qt::WindingFill); + truck.moveTo(0.0, 87.0); + truck.lineTo(0.0, 60.0); + truck.lineTo(10.0, 60.0); + truck.lineTo(35.0, 35.0); + truck.lineTo(100.0, 35.0); + truck.lineTo(100.0, 87.0); + truck.lineTo(0.0, 87.0); + truck.moveTo(17.0, 60.0); + truck.lineTo(55.0, 60.0); + truck.lineTo(55.0, 40.0); + truck.lineTo(37.0, 40.0); + truck.lineTo(17.0, 60.0); + truck.addEllipse(17.0, 75.0, 25.0, 25.0); + truck.addEllipse(63.0, 75.0, 25.0, 25.0); + +//! [4] + QPainterPath clock; +//! [4] + clock.addEllipse(-50.0, -50.0, 100.0, 100.0); + clock.addEllipse(-48.0, -48.0, 96.0, 96.0); + clock.moveTo(0.0, 0.0); + clock.lineTo(-2.0, -2.0); + clock.lineTo(0.0, -42.0); + clock.lineTo(2.0, -2.0); + clock.lineTo(0.0, 0.0); + clock.moveTo(0.0, 0.0); + clock.lineTo(2.732, -0.732); + clock.lineTo(24.495, 14.142); + clock.lineTo(0.732, 2.732); + clock.lineTo(0.0, 0.0); + +//! [5] + QPainterPath house; +//! [5] + house.moveTo(-45.0, -20.0); + house.lineTo(0.0, -45.0); + house.lineTo(45.0, -20.0); + house.lineTo(45.0, 45.0); + house.lineTo(-45.0, 45.0); + house.lineTo(-45.0, -20.0); + house.addRect(15.0, 5.0, 20.0, 35.0); + house.addRect(-35.0, -15.0, 25.0, 25.0); + +//! [6] + QPainterPath text; +//! [6] + QFont font; + font.setPixelSize(50); + QRect fontBoundingRect = QFontMetrics(font).boundingRect(tr("Qt")); + text.addText(-QPointF(fontBoundingRect.center()), font, tr("Qt")); + +//! [7] + shapes.append(clock); + shapes.append(house); + shapes.append(text); + shapes.append(truck); + + connect(shapeComboBox, SIGNAL(activated(int)), + this, SLOT(shapeSelected(int))); +} +//! [7] + +//! [8] +void Window::operationChanged() +{ + static const Operation operationTable[] = { + NoTransformation, Rotate, Scale, Translate + }; + + QList<Operation> operations; + for (int i = 0; i < NumTransformedAreas; ++i) { + int index = operationComboBoxes[i]->currentIndex(); + operations.append(operationTable[index]); + transformedRenderAreas[i]->setOperations(operations); + } +} +//! [8] + +//! [9] +void Window::shapeSelected(int index) +{ + QPainterPath shape = shapes[index]; + originalRenderArea->setShape(shape); + for (int i = 0; i < NumTransformedAreas; ++i) + transformedRenderAreas[i]->setShape(shape); +} +//! [9] diff --git a/examples/painting/transformations/window.h b/examples/painting/transformations/window.h new file mode 100644 index 0000000..f4f05d6 --- /dev/null +++ b/examples/painting/transformations/window.h @@ -0,0 +1,81 @@ +/**************************************************************************** +** +** 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 <QList> +#include <QPainterPath> +#include <QWidget> + +#include "renderarea.h" + +QT_BEGIN_NAMESPACE +class QComboBox; +QT_END_NAMESPACE + +//! [0] +class Window : public QWidget +{ + Q_OBJECT + +public: + Window(); + +public slots: + void operationChanged(); + void shapeSelected(int index); +//! [0] + +//! [1] +private: + void setupShapes(); + + enum { NumTransformedAreas = 3 }; + RenderArea *originalRenderArea; + RenderArea *transformedRenderAreas[NumTransformedAreas]; + QComboBox *shapeComboBox; + QComboBox *operationComboBoxes[NumTransformedAreas]; + QList<QPainterPath> shapes; +}; +//! [1] + +#endif |