| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** All rights reserved.
|
|---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
|---|
| 6 | **
|
|---|
| 7 | ** This file is part of the documentation of the Qt Toolkit.
|
|---|
| 8 | **
|
|---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 10 | ** Commercial Usage
|
|---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 14 | ** a written agreement between you and Nokia.
|
|---|
| 15 | **
|
|---|
| 16 | ** GNU Lesser General Public License Usage
|
|---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 18 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 20 | ** packaging of this file. Please review the following information to
|
|---|
| 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 23 | **
|
|---|
| 24 | ** In addition, as a special exception, Nokia gives you certain additional
|
|---|
| 25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
|---|
| 26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|---|
| 27 | **
|
|---|
| 28 | ** GNU General Public License Usage
|
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 32 | ** packaging of this file. Please review the following information to
|
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
|---|
| 35 | **
|
|---|
| 36 | ** If you have questions regarding the use of this file, please contact
|
|---|
| 37 | ** Nokia at [email protected].
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | /*!
|
|---|
| 43 | \example painting/svggenerator
|
|---|
| 44 | \title SVG Generator Example
|
|---|
| 45 |
|
|---|
| 46 | The SVG Generator example shows how to add SVG file export to applications.
|
|---|
| 47 |
|
|---|
| 48 | \image svggenerator-example.png
|
|---|
| 49 |
|
|---|
| 50 | Scalable Vector Graphics (SVG) is an XML-based language for describing
|
|---|
| 51 | two-dimensional vector graphics. Qt provides classes for rendering and
|
|---|
| 52 | generating SVG drawings. This example allows the user to create a simple
|
|---|
| 53 | picture and save it to an SVG file.
|
|---|
| 54 |
|
|---|
| 55 | The example consists of two classes: \c Window and \c DisplayWidget.
|
|---|
| 56 |
|
|---|
| 57 | The \c Window class contains the application logic and constructs the user
|
|---|
| 58 | interface from a Qt Designer UI file as described in the
|
|---|
| 59 | \l{Using a Designer UI File in Your Application#The Multiple Inheritance Approach}{Qt Designer manual}.
|
|---|
| 60 | It also contains the code to write an SVG file.
|
|---|
| 61 |
|
|---|
| 62 | The \c DisplayWidget class performs all the work of painting a picture on
|
|---|
| 63 | screen. Since we want the SVG to resemble this picture as closely as
|
|---|
| 64 | possible, we make this code available to the \c Window class so that it can
|
|---|
| 65 | be used to generate SVG files.
|
|---|
| 66 |
|
|---|
| 67 | \section1 The DisplayWidget Class
|
|---|
| 68 |
|
|---|
| 69 | The \c DisplayWidget class displays a drawing consisting of a selection of
|
|---|
| 70 | elements chosen by the user. These are defined using \c Shape and
|
|---|
| 71 | \c Background enums that are included within the class definition:
|
|---|
| 72 |
|
|---|
| 73 | \snippet examples/painting/svggenerator/displaywidget.h DisplayWidget class definition
|
|---|
| 74 |
|
|---|
| 75 | Much of this class is used to configure the appearance of the drawing. The
|
|---|
| 76 | \c paintEvent() and \c paint() functions are most relevant to the purpose
|
|---|
| 77 | of this example, so we will describe these here and leave the reader to
|
|---|
| 78 | look at the source code for the example to see how shapes and colors are
|
|---|
| 79 | handled.
|
|---|
| 80 |
|
|---|
| 81 | We reimplement the QWidget::paintEvent() function to display the drawing
|
|---|
| 82 | on screen:
|
|---|
| 83 |
|
|---|
| 84 | \snippet examples/painting/svggenerator/displaywidget.cpp paint event
|
|---|
| 85 |
|
|---|
| 86 | Here, we only construct a QPainter object, begin painting on the device
|
|---|
| 87 | and set a render hint for improved output quality before calling the
|
|---|
| 88 | \c paint() function to perform the painting itself. When this returns,
|
|---|
| 89 | we close the painter and return.
|
|---|
| 90 |
|
|---|
| 91 | The \c paint() function is designed to be used for different painting
|
|---|
| 92 | tasks. In this example, we use it to draw on a \c DisplayWidget instance
|
|---|
| 93 | and on a QSvgGenerator object. We show how the painting is performed to
|
|---|
| 94 | demonstrate that there is nothing device-specific about the process:
|
|---|
| 95 |
|
|---|
| 96 | \snippet examples/painting/svggenerator/displaywidget.cpp paint function
|
|---|
| 97 |
|
|---|
| 98 | \section1 The Window Class
|
|---|
| 99 |
|
|---|
| 100 | The \c Window class represents the example's window, containing the user
|
|---|
| 101 | interface, which has been created using Qt Designer:
|
|---|
| 102 |
|
|---|
| 103 | \snippet examples/painting/svggenerator/window.h Window class definition
|
|---|
| 104 |
|
|---|
| 105 | As with the \c DisplayWidget class, we concentrate on the parts of the code
|
|---|
| 106 | which are concerned with painting and SVG generation. In the \c Window
|
|---|
| 107 | class, the \c saveSvg() function is called whenever the \gui{Save As...}
|
|---|
| 108 | button is clicked; this connection was defined in the \c{window.ui} file
|
|---|
| 109 | using Qt Designer.
|
|---|
| 110 |
|
|---|
| 111 | The start of the \c saveSvg() function performs the task of showing a file
|
|---|
| 112 | dialog so that the user can specify a SVG file to save the drawing to.
|
|---|
| 113 |
|
|---|
| 114 | \snippet examples/painting/svggenerator/window.cpp save SVG
|
|---|
| 115 |
|
|---|
| 116 | In the rest of the function, we set up the generator and configure it to
|
|---|
| 117 | generate output with the appropriate dimensions and write to the
|
|---|
| 118 | user-specified file. We paint on the QSvgGenerator object in the same way
|
|---|
| 119 | that we paint on a widget, calling the \c DisplayWidget::paint() function
|
|---|
| 120 | so that we use exactly the same code that we used to display the drawing.
|
|---|
| 121 |
|
|---|
| 122 | The generation process itself begins with the call to the painter's
|
|---|
| 123 | \l{QPainter::}{begin()} function and ends with call to its
|
|---|
| 124 | \l{QPainter::}{end()} function. The QSvgGenerator paint device relies on
|
|---|
| 125 | the explicit use of these functions to ensure that output is written to
|
|---|
| 126 | the file.
|
|---|
| 127 |
|
|---|
| 128 | \section1 Further Reading
|
|---|
| 129 |
|
|---|
| 130 | The \l{SVG Viewer Example} shows how to display SVG drawings in an
|
|---|
| 131 | application, and can be used to show the contents of SVG files created
|
|---|
| 132 | by this example.
|
|---|
| 133 |
|
|---|
| 134 | See the QtSvg module documentation for more information about SVG and Qt's
|
|---|
| 135 | SVG classes.
|
|---|
| 136 | */
|
|---|