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