diff options
author | Simon Hausmann <simon.hausmann@nokia.com> | 2009-03-26 21:44:46 (GMT) |
---|---|---|
committer | Simon Hausmann <simon.hausmann@nokia.com> | 2009-03-26 21:44:46 (GMT) |
commit | 4b3d81c629167a24424119ca4f296a85b78eab05 (patch) | |
tree | 7111ec107798ec49ee528a0a9aee496342c8b8a4 /examples | |
parent | 20ec83dc4c19436092262ab46615c19c9c8b20e7 (diff) | |
parent | 9508cfea4731f86664794455c4d3571e41e180fd (diff) | |
download | Qt-4b3d81c629167a24424119ca4f296a85b78eab05.zip Qt-4b3d81c629167a24424119ca4f296a85b78eab05.tar.gz Qt-4b3d81c629167a24424119ca4f296a85b78eab05.tar.bz2 |
Merge branch '4.5' of git@scm.dev.troll.no:qt/qt
Diffstat (limited to 'examples')
-rw-r--r-- | examples/painting/painting.pro | 2 | ||||
-rw-r--r-- | examples/painting/svggenerator/displaywidget.cpp | 149 | ||||
-rw-r--r-- | examples/painting/svggenerator/displaywidget.h | 80 | ||||
-rw-r--r-- | examples/painting/svggenerator/forms/window.ui | 249 | ||||
-rw-r--r-- | examples/painting/svggenerator/main.cpp | 52 | ||||
-rw-r--r-- | examples/painting/svggenerator/resources/shapes.dat | bin | 0 -> 2088 bytes | |||
-rw-r--r-- | examples/painting/svggenerator/svggenerator.pro | 15 | ||||
-rw-r--r-- | examples/painting/svggenerator/svggenerator.qrc | 5 | ||||
-rw-r--r-- | examples/painting/svggenerator/window.cpp | 101 | ||||
-rw-r--r-- | examples/painting/svggenerator/window.h | 66 |
10 files changed, 718 insertions, 1 deletions
diff --git a/examples/painting/painting.pro b/examples/painting/painting.pro index b6fad5a..6d00720 100644 --- a/examples/painting/painting.pro +++ b/examples/painting/painting.pro @@ -7,7 +7,7 @@ SUBDIRS = basicdrawing \ !wince*: SUBDIRS += fontsampler -contains(QT_CONFIG, svg): SUBDIRS += svgviewer +contains(QT_CONFIG, svg): SUBDIRS += svggenerator svgviewer # install target.path = $$[QT_INSTALL_EXAMPLES]/painting diff --git a/examples/painting/svggenerator/displaywidget.cpp b/examples/painting/svggenerator/displaywidget.cpp new file mode 100644 index 0000000..c973341 --- /dev/null +++ b/examples/painting/svggenerator/displaywidget.cpp @@ -0,0 +1,149 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui> +#include "displaywidget.h" + +DisplayWidget::DisplayWidget(QWidget *parent) + : QWidget(parent) +{ + QPainterPath car; + QPainterPath house; + + QFile file(":resources/shapes.dat"); + file.open(QFile::ReadOnly); + QDataStream stream(&file); + stream >> car >> house >> tree >> moon; + file.close(); + + shapeMap[Car] = car; + shapeMap[House] = house; + + background = Sky; + shapeColor = Qt::darkYellow; + shape = House; +} + +//! [paint event] +void DisplayWidget::paintEvent(QPaintEvent *event) +{ + QPainter painter; + painter.begin(this); + painter.setRenderHint(QPainter::Antialiasing); + paint(painter); + painter.end(); +} +//! [paint event] + +//! [paint function] +void DisplayWidget::paint(QPainter &painter) +{ +//![paint picture] + painter.setClipRect(QRect(0, 0, 200, 200)); + painter.setPen(Qt::NoPen); + + switch (background) { + case Sky: + default: + painter.fillRect(QRect(0, 0, 200, 200), Qt::darkBlue); + painter.translate(145, 10); + painter.setBrush(Qt::white); + painter.drawPath(moon); + painter.translate(-145, -10); + break; + case Trees: + { + painter.fillRect(QRect(0, 0, 200, 200), Qt::darkGreen); + painter.setBrush(Qt::green); + painter.setPen(Qt::black); + for (int y = -55, row = 0; y < 200; y += 50, ++row) { + int xs; + if (row == 2 || row == 3) + xs = 150; + else + xs = 50; + for (int x = 0; x < 200; x += xs) { + painter.save(); + painter.translate(x, y); + painter.drawPath(tree); + painter.restore(); + } + } + break; + } + case Road: + painter.fillRect(QRect(0, 0, 200, 200), Qt::gray); + painter.setPen(QPen(Qt::white, 4, Qt::DashLine)); + painter.drawLine(QLine(0, 35, 200, 35)); + painter.drawLine(QLine(0, 165, 200, 165)); + break; + } + + painter.setBrush(shapeColor); + painter.setPen(Qt::black); + painter.translate(100, 100); + painter.drawPath(shapeMap[shape]); +//![paint picture] +} +//! [paint function] + +QColor DisplayWidget::color() const +{ + return shapeColor; +} + +void DisplayWidget::setBackground(Background background) +{ + this->background = background; + update(); +} + +void DisplayWidget::setColor(const QColor &color) +{ + this->shapeColor = color; + update(); +} + +void DisplayWidget::setShape(Shape shape) +{ + this->shape = shape; + update(); +} diff --git a/examples/painting/svggenerator/displaywidget.h b/examples/painting/svggenerator/displaywidget.h new file mode 100644 index 0000000..58ad97e --- /dev/null +++ b/examples/painting/svggenerator/displaywidget.h @@ -0,0 +1,80 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef DISPLAYWIDGET_H +#define DISPLAYWIDGET_H + +#include <QColor> +#include <QHash> +#include <QWidget> + +//! [DisplayWidget class definition] +class DisplayWidget : public QWidget +{ + Q_OBJECT + +public: + enum Shape { House = 0, Car = 1 }; + enum Background { Sky = 0, Trees = 1, Road = 2 }; + + DisplayWidget(QWidget *parent = 0); + QColor color() const; + void paint(QPainter &painter); + +public slots: + void setBackground(Background background); + void setColor(const QColor &color); + void setShape(Shape shape); + +protected: + void paintEvent(QPaintEvent *event); + +private: + Background background; + QColor shapeColor; + Shape shape; + QHash<Shape,QPainterPath> shapeMap; + QPainterPath moon; + QPainterPath tree; +}; +//! [DisplayWidget class definition] + +#endif diff --git a/examples/painting/svggenerator/forms/window.ui b/examples/painting/svggenerator/forms/window.ui new file mode 100644 index 0000000..bf11908 --- /dev/null +++ b/examples/painting/svggenerator/forms/window.ui @@ -0,0 +1,249 @@ +<?xml version="1.0" encoding="UTF-8"?> +<ui version="4.0"> + <class>Window</class> + <widget class="QWidget" name="Window"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>339</width> + <height>353</height> + </rect> + </property> + <property name="windowTitle"> + <string>SVG Generator</string> + </property> + <layout class="QGridLayout" name="gridLayout_2"> + <property name="sizeConstraint"> + <enum>QLayout::SetFixedSize</enum> + </property> + <item row="0" column="0"> + <spacer name="horizontalSpacer_2"> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + <property name="sizeHint" stdset="0"> + <size> + <width>40</width> + <height>20</height> + </size> + </property> + </spacer> + </item> + <item row="0" column="1"> + <widget class="DisplayWidget" name="displayWidget" native="true"> + <property name="sizePolicy"> + <sizepolicy hsizetype="Fixed" vsizetype="Fixed"> + <horstretch>200</horstretch> + <verstretch>200</verstretch> + </sizepolicy> + </property> + <property name="minimumSize"> + <size> + <width>200</width> + <height>200</height> + </size> + </property> + <property name="maximumSize"> + <size> + <width>200</width> + <height>200</height> + </size> + </property> + </widget> + </item> + <item row="0" column="2"> + <spacer name="horizontalSpacer_3"> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + <property name="sizeHint" stdset="0"> + <size> + <width>40</width> + <height>20</height> + </size> + </property> + </spacer> + </item> + <item row="1" column="0" colspan="3"> + <layout class="QGridLayout" name="gridLayout"> + <item row="0" column="0"> + <widget class="QLabel" name="label"> + <property name="text"> + <string>&Shape:</string> + </property> + <property name="buddy"> + <cstring>shapeComboBox</cstring> + </property> + </widget> + </item> + <item row="0" column="1"> + <widget class="QComboBox" name="shapeComboBox"> + <item> + <property name="text"> + <string>House</string> + </property> + </item> + <item> + <property name="text"> + <string>Car</string> + </property> + </item> + </widget> + </item> + <item row="1" column="0"> + <widget class="QLabel" name="label_2"> + <property name="text"> + <string>&Color:</string> + </property> + <property name="buddy"> + <cstring>colorButton</cstring> + </property> + </widget> + </item> + <item row="1" column="1"> + <widget class="QToolButton" name="colorButton"> + <property name="text"> + <string>Choose Color...</string> + </property> + </widget> + </item> + <item row="2" column="0"> + <widget class="QLabel" name="label_3"> + <property name="text"> + <string>&Background:</string> + </property> + <property name="buddy"> + <cstring>shapeComboBox_2</cstring> + </property> + </widget> + </item> + <item row="2" column="1"> + <widget class="QComboBox" name="shapeComboBox_2"> + <item> + <property name="text"> + <string>Sky</string> + </property> + </item> + <item> + <property name="text"> + <string>Trees</string> + </property> + </item> + <item> + <property name="text"> + <string>Road</string> + </property> + </item> + </widget> + </item> + </layout> + </item> + <item row="2" column="0" colspan="3"> + <layout class="QHBoxLayout" name="horizontalLayout"> + <item> + <spacer name="horizontalSpacer"> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + <property name="sizeHint" stdset="0"> + <size> + <width>40</width> + <height>20</height> + </size> + </property> + </spacer> + </item> + <item> + <widget class="QToolButton" name="toolButton_2"> + <property name="text"> + <string>Save &As...</string> + </property> + </widget> + </item> + </layout> + </item> + </layout> + </widget> + <customwidgets> + <customwidget> + <class>DisplayWidget</class> + <extends>QWidget</extends> + <header>displaywidget.h</header> + <container>1</container> + </customwidget> + </customwidgets> + <resources/> + <connections> + <connection> + <sender>shapeComboBox</sender> + <signal>currentIndexChanged(int)</signal> + <receiver>Window</receiver> + <slot>updateShape(int)</slot> + <hints> + <hint type="sourcelabel"> + <x>288</x> + <y>232</y> + </hint> + <hint type="destinationlabel"> + <x>336</x> + <y>234</y> + </hint> + </hints> + </connection> + <connection> + <sender>colorButton</sender> + <signal>clicked()</signal> + <receiver>Window</receiver> + <slot>updateColor()</slot> + <hints> + <hint type="sourcelabel"> + <x>301</x> + <y>262</y> + </hint> + <hint type="destinationlabel"> + <x>337</x> + <y>267</y> + </hint> + </hints> + </connection> + <connection> + <sender>shapeComboBox_2</sender> + <signal>currentIndexChanged(int)</signal> + <receiver>Window</receiver> + <slot>updateBackground(int)</slot> + <hints> + <hint type="sourcelabel"> + <x>306</x> + <y>299</y> + </hint> + <hint type="destinationlabel"> + <x>337</x> + <y>311</y> + </hint> + </hints> + </connection> + <connection> + <sender>toolButton_2</sender> + <signal>clicked()</signal> + <receiver>Window</receiver> + <slot>saveSvg()</slot> + <hints> + <hint type="sourcelabel"> + <x>298</x> + <y>336</y> + </hint> + <hint type="destinationlabel"> + <x>307</x> + <y>348</y> + </hint> + </hints> + </connection> + </connections> + <slots> + <slot>updateBackground(int)</slot> + <slot>updateColor()</slot> + <slot>updateShape(int)</slot> + <slot>saveSvg()</slot> + </slots> +</ui> diff --git a/examples/painting/svggenerator/main.cpp b/examples/painting/svggenerator/main.cpp new file mode 100644 index 0000000..fa8b0ab --- /dev/null +++ b/examples/painting/svggenerator/main.cpp @@ -0,0 +1,52 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <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/svggenerator/resources/shapes.dat b/examples/painting/svggenerator/resources/shapes.dat Binary files differnew file mode 100644 index 0000000..d9b981e --- /dev/null +++ b/examples/painting/svggenerator/resources/shapes.dat diff --git a/examples/painting/svggenerator/svggenerator.pro b/examples/painting/svggenerator/svggenerator.pro new file mode 100644 index 0000000..4d08ba4 --- /dev/null +++ b/examples/painting/svggenerator/svggenerator.pro @@ -0,0 +1,15 @@ +FORMS = forms/window.ui +HEADERS = displaywidget.h \ + window.h +RESOURCES = svggenerator.qrc +SOURCES = displaywidget.cpp \ + main.cpp \ + window.cpp + +QT += svg + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/painting/svggenerator +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS svggenerator.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/painting/svggenerator +INSTALLS += target sources diff --git a/examples/painting/svggenerator/svggenerator.qrc b/examples/painting/svggenerator/svggenerator.qrc new file mode 100644 index 0000000..061d1f6 --- /dev/null +++ b/examples/painting/svggenerator/svggenerator.qrc @@ -0,0 +1,5 @@ +<!DOCTYPE RCC><RCC version="1.0"> +<qresource> + <file>resources/shapes.dat</file> +</qresource> +</RCC> diff --git a/examples/painting/svggenerator/window.cpp b/examples/painting/svggenerator/window.cpp new file mode 100644 index 0000000..1965604 --- /dev/null +++ b/examples/painting/svggenerator/window.cpp @@ -0,0 +1,101 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QColorDialog> +#include <QFileDialog> +#include <QPainter> +#include <QSvgGenerator> +#include "window.h" +#include "displaywidget.h" + +Window::Window(QWidget *parent) + : QWidget(parent) +{ + setupUi(this); +} + +void Window::updateBackground(int background) +{ + displayWidget->setBackground(DisplayWidget::Background(background)); +} + +void Window::updateColor() +{ + QColor color = QColorDialog::getColor(displayWidget->color()); + if (color.isValid()) + displayWidget->setColor(color); +} + +void Window::updateShape(int shape) +{ + displayWidget->setShape(DisplayWidget::Shape(shape)); +} + +//! [save SVG] +void Window::saveSvg() +{ + QString newPath = QFileDialog::getSaveFileName(this, tr("Save SVG"), + path, tr("SVG files (*.svg)")); + + if (newPath.isEmpty()) + return; + + path = newPath; + +//![configure SVG generator] + QSvgGenerator generator; + generator.setFileName(path); + generator.setSize(QSize(200, 200)); + generator.setViewBox(QRect(0, 0, 200, 200)); + generator.setTitle(tr("SVG Generator Example Drawing")); + generator.setDescription(tr("An SVG drawing created by the SVG Generator " + "Example provided with Qt.")); +//![configure SVG generator] +//![begin painting] + QPainter painter; + painter.begin(&generator); +//![begin painting] + displayWidget->paint(painter); +//![end painting] + painter.end(); +//![end painting] +} +//! [save SVG] diff --git a/examples/painting/svggenerator/window.h b/examples/painting/svggenerator/window.h new file mode 100644 index 0000000..4bc4d22 --- /dev/null +++ b/examples/painting/svggenerator/window.h @@ -0,0 +1,66 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef WINDOW_H +#define WINDOW_H + +#include "ui_window.h" + +//! [Window class definition] +class Window : public QWidget, private Ui::Window +{ + Q_OBJECT + +public: + Window(QWidget *parent = 0); + +public slots: + void saveSvg(); + void updateBackground(int background); + void updateColor(); + void updateShape(int shape); + +private: + QString path; +}; +//! [Window class definition] + +#endif |