~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to doc/html/painting-basicdrawing-window-cpp.html

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?xml version="1.0" encoding="iso-8859-1"?>
 
2
<!DOCTYPE html
 
3
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
 
4
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 
5
<head>
 
6
    <title>Qt 4.0: window.cpp Example File (painting/basicdrawing/window.cpp)</title>
 
7
    <style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
 
8
a:link { color: #004faf; text-decoration: none }
 
9
a:visited { color: #672967; text-decoration: none }
 
10
td.postheader { font-family: sans-serif }
 
11
tr.address { font-family: sans-serif }
 
12
body { background: #ffffff; color: black; }</style>
 
13
</head>
 
14
<body>
 
15
<table border="0" cellpadding="0" cellspacing="0" width="100%">
 
16
<tr>
 
17
<td align="left" valign="top" width="32"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></td>
 
18
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="annotated.html"><font color="#004faf">Annotated</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">Functions</font></a></td>
 
19
<td align="right" valign="top" width="230"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></td></tr></table><h1 align="center">window.cpp Example File<br /><small><small>painting/basicdrawing/window.cpp</small></small></h1>
 
20
<pre>&nbsp;   /****************************************************************************
 
21
    **
 
22
    ** Copyright (C) 2005-2005 Trolltech AS. All rights reserved.
 
23
    **
 
24
    ** This file is part of the documentation of the Qt Toolkit.
 
25
    **
 
26
    ** This file may be distributed under the terms of the Q Public License
 
27
** as defined by Trolltech AS of Norway and appearing in the file
 
28
** LICENSE.QPL included in the packaging of this file.
 
29
**
 
30
** This file may be distributed and/or modified under the terms of the
 
31
** GNU General Public License version 2 as published by the Free Software
 
32
** Foundation and appearing in the file LICENSE.GPL included in the
 
33
** packaging of this file.
 
34
**
 
35
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
 
36
**   information about Qt Commercial License Agreements.
 
37
** See http://www.trolltech.com/qpl/ for QPL licensing information.
 
38
** See http://www.trolltech.com/gpl/ for GPL licensing information.
 
39
**
 
40
** Contact info@trolltech.com if any conditions of this licensing are
 
41
** not clear to you.
 
42
    **
 
43
    ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 
44
    ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 
45
    **
 
46
    ****************************************************************************/
 
47
 
 
48
    #include &lt;QtGui&gt;
 
49
 
 
50
    #include &quot;renderarea.h&quot;
 
51
    #include &quot;window.h&quot;
 
52
 
 
53
    const int IdRole = Qt::UserRole;
 
54
 
 
55
    Window::Window()
 
56
    {
 
57
        renderArea = new RenderArea;
 
58
 
 
59
        shapeComboBox = new QComboBox;
 
60
        shapeComboBox-&gt;addItem(tr(&quot;Rectangle&quot;), RenderArea::Rect);
 
61
        shapeComboBox-&gt;addItem(tr(&quot;Round Rectangle&quot;), RenderArea::RoundRect);
 
62
        shapeComboBox-&gt;addItem(tr(&quot;Ellipse&quot;), RenderArea::Ellipse);
 
63
        shapeComboBox-&gt;addItem(tr(&quot;Pie&quot;), RenderArea::Pie);
 
64
        shapeComboBox-&gt;addItem(tr(&quot;Chord&quot;), RenderArea::Chord);
 
65
        shapeComboBox-&gt;addItem(tr(&quot;Polygon&quot;), RenderArea::Polygon);
 
66
        shapeComboBox-&gt;addItem(tr(&quot;Path&quot;), RenderArea::Path);
 
67
        shapeComboBox-&gt;addItem(tr(&quot;Line&quot;), RenderArea::Line);
 
68
        shapeComboBox-&gt;addItem(tr(&quot;Polyline&quot;), RenderArea::Polyline);
 
69
        shapeComboBox-&gt;addItem(tr(&quot;Arc&quot;), RenderArea::Arc);
 
70
        shapeComboBox-&gt;addItem(tr(&quot;Points&quot;), RenderArea::Points);
 
71
        shapeComboBox-&gt;addItem(tr(&quot;Text&quot;), RenderArea::Text);
 
72
        shapeComboBox-&gt;addItem(tr(&quot;Pixmap&quot;), RenderArea::Pixmap);
 
73
 
 
74
        shapeLabel = new QLabel(tr(&quot;&amp;Shape:&quot;));
 
75
        shapeLabel-&gt;setBuddy(shapeComboBox);
 
76
 
 
77
        penWidthSpinBox = new QSpinBox;
 
78
        penWidthSpinBox-&gt;setRange(0, 20);
 
79
 
 
80
        penWidthLabel = new QLabel(tr(&quot;Pen &amp;Width:&quot;));
 
81
        penWidthLabel-&gt;setBuddy(penWidthSpinBox);
 
82
 
 
83
        penStyleComboBox = new QComboBox;
 
84
        penStyleComboBox-&gt;addItem(tr(&quot;Solid&quot;), Qt::SolidLine);
 
85
        penStyleComboBox-&gt;addItem(tr(&quot;Dash&quot;), Qt::DashLine);
 
86
        penStyleComboBox-&gt;addItem(tr(&quot;Dot&quot;), Qt::DotLine);
 
87
        penStyleComboBox-&gt;addItem(tr(&quot;Dash Dot&quot;), Qt::DashDotLine);
 
88
        penStyleComboBox-&gt;addItem(tr(&quot;Dash Dot Dot&quot;), Qt::DashDotDotLine);
 
89
        penStyleComboBox-&gt;addItem(tr(&quot;None&quot;), Qt::NoPen);
 
90
 
 
91
        penStyleLabel = new QLabel(tr(&quot;&amp;Pen Style:&quot;));
 
92
        penStyleLabel-&gt;setBuddy(penStyleComboBox);
 
93
 
 
94
        penCapComboBox = new QComboBox;
 
95
        penCapComboBox-&gt;addItem(tr(&quot;Flat&quot;), Qt::FlatCap);
 
96
        penCapComboBox-&gt;addItem(tr(&quot;Square&quot;), Qt::SquareCap);
 
97
        penCapComboBox-&gt;addItem(tr(&quot;Round&quot;), Qt::RoundCap);
 
98
 
 
99
        penCapLabel = new QLabel(tr(&quot;Pen &amp;Cap:&quot;));
 
100
        penCapLabel-&gt;setBuddy(penCapComboBox);
 
101
 
 
102
        penJoinComboBox = new QComboBox;
 
103
        penJoinComboBox-&gt;addItem(tr(&quot;Miter&quot;), Qt::MiterJoin);
 
104
        penJoinComboBox-&gt;addItem(tr(&quot;Bevel&quot;), Qt::BevelJoin);
 
105
        penJoinComboBox-&gt;addItem(tr(&quot;Round&quot;), Qt::RoundJoin);
 
106
 
 
107
        penJoinLabel = new QLabel(tr(&quot;Pen &amp;Join:&quot;));
 
108
        penJoinLabel-&gt;setBuddy(penJoinComboBox);
 
109
 
 
110
        brushStyleComboBox = new QComboBox;
 
111
        brushStyleComboBox-&gt;addItem(tr(&quot;Linear Gradient&quot;),
 
112
                Qt::LinearGradientPattern);
 
113
        brushStyleComboBox-&gt;addItem(tr(&quot;Radial Gradient&quot;),
 
114
                Qt::RadialGradientPattern);
 
115
        brushStyleComboBox-&gt;addItem(tr(&quot;Conical Gradient&quot;),
 
116
                Qt::ConicalGradientPattern);
 
117
        brushStyleComboBox-&gt;addItem(tr(&quot;Texture&quot;), Qt::TexturePattern);
 
118
        brushStyleComboBox-&gt;addItem(tr(&quot;Solid&quot;), Qt::SolidPattern);
 
119
        brushStyleComboBox-&gt;addItem(tr(&quot;Horizontal&quot;), Qt::HorPattern);
 
120
        brushStyleComboBox-&gt;addItem(tr(&quot;Vertical&quot;), Qt::VerPattern);
 
121
        brushStyleComboBox-&gt;addItem(tr(&quot;Cross&quot;), Qt::CrossPattern);
 
122
        brushStyleComboBox-&gt;addItem(tr(&quot;Backward Diagonal&quot;), Qt::BDiagPattern);
 
123
        brushStyleComboBox-&gt;addItem(tr(&quot;Forward Diagonal&quot;), Qt::FDiagPattern);
 
124
        brushStyleComboBox-&gt;addItem(tr(&quot;Diagonal Cross&quot;), Qt::DiagCrossPattern);
 
125
        brushStyleComboBox-&gt;addItem(tr(&quot;Dense 1&quot;), Qt::Dense1Pattern);
 
126
        brushStyleComboBox-&gt;addItem(tr(&quot;Dense 2&quot;), Qt::Dense2Pattern);
 
127
        brushStyleComboBox-&gt;addItem(tr(&quot;Dense 3&quot;), Qt::Dense3Pattern);
 
128
        brushStyleComboBox-&gt;addItem(tr(&quot;Dense 4&quot;), Qt::Dense4Pattern);
 
129
        brushStyleComboBox-&gt;addItem(tr(&quot;Dense 5&quot;), Qt::Dense5Pattern);
 
130
        brushStyleComboBox-&gt;addItem(tr(&quot;Dense 6&quot;), Qt::Dense6Pattern);
 
131
        brushStyleComboBox-&gt;addItem(tr(&quot;Dense 7&quot;), Qt::Dense7Pattern);
 
132
        brushStyleComboBox-&gt;addItem(tr(&quot;None&quot;), Qt::NoBrush);
 
133
 
 
134
        brushStyleLabel = new QLabel(tr(&quot;&amp;Brush Style:&quot;));
 
135
        brushStyleLabel-&gt;setBuddy(brushStyleComboBox);
 
136
 
 
137
        antialiasingCheckBox = new QCheckBox(tr(&quot;&amp;Antialiasing&quot;));
 
138
        transformationsCheckBox = new QCheckBox(tr(&quot;&amp;Transformations&quot;));
 
139
 
 
140
        connect(shapeComboBox, SIGNAL(activated(int)),
 
141
                this, SLOT(shapeChanged()));
 
142
        connect(penWidthSpinBox, SIGNAL(valueChanged(int)),
 
143
                this, SLOT(penChanged()));
 
144
        connect(penStyleComboBox, SIGNAL(activated(int)),
 
145
                this, SLOT(penChanged()));
 
146
        connect(penCapComboBox, SIGNAL(activated(int)),
 
147
                this, SLOT(penChanged()));
 
148
        connect(penJoinComboBox, SIGNAL(activated(int)),
 
149
                this, SLOT(penChanged()));
 
150
        connect(brushStyleComboBox, SIGNAL(activated(int)),
 
151
                this, SLOT(brushChanged()));
 
152
        connect(antialiasingCheckBox, SIGNAL(toggled(bool)),
 
153
                renderArea, SLOT(setAntialiased(bool)));
 
154
        connect(transformationsCheckBox, SIGNAL(toggled(bool)),
 
155
                renderArea, SLOT(setTransformed(bool)));
 
156
 
 
157
        QHBoxLayout *checkBoxLayout = new QHBoxLayout;
 
158
        checkBoxLayout-&gt;addWidget(antialiasingCheckBox);
 
159
        checkBoxLayout-&gt;addWidget(transformationsCheckBox);
 
160
 
 
161
        QGridLayout *mainLayout = new QGridLayout;
 
162
        mainLayout-&gt;addWidget(renderArea, 0, 0, 1, 2);
 
163
        mainLayout-&gt;addWidget(shapeLabel, 1, 0);
 
164
        mainLayout-&gt;addWidget(shapeComboBox, 1, 1);
 
165
        mainLayout-&gt;addWidget(penWidthLabel, 2, 0);
 
166
        mainLayout-&gt;addWidget(penWidthSpinBox, 2, 1);
 
167
        mainLayout-&gt;addWidget(penStyleLabel, 3, 0);
 
168
        mainLayout-&gt;addWidget(penStyleComboBox, 3, 1);
 
169
        mainLayout-&gt;addWidget(penCapLabel, 4, 0);
 
170
        mainLayout-&gt;addWidget(penCapComboBox, 4, 1);
 
171
        mainLayout-&gt;addWidget(penJoinLabel, 5, 0);
 
172
        mainLayout-&gt;addWidget(penJoinComboBox, 5, 1);
 
173
        mainLayout-&gt;addWidget(brushStyleLabel, 6, 0);
 
174
        mainLayout-&gt;addWidget(brushStyleComboBox, 6, 1);
 
175
        mainLayout-&gt;addLayout(checkBoxLayout, 7, 0, 1, 2);
 
176
        setLayout(mainLayout);
 
177
 
 
178
        shapeChanged();
 
179
        penChanged();
 
180
        brushChanged();
 
181
        penChanged();
 
182
        renderArea-&gt;setAntialiased(false);
 
183
        renderArea-&gt;setTransformed(false);
 
184
 
 
185
        setWindowTitle(tr(&quot;Basic Drawing&quot;));
 
186
    }
 
187
 
 
188
    void Window::shapeChanged()
 
189
    {
 
190
        RenderArea::Shape shape =
 
191
            (RenderArea::Shape)shapeComboBox-&gt;itemData(shapeComboBox-&gt;currentIndex(), IdRole).toInt();
 
192
        renderArea-&gt;setShape(shape);
 
193
    }
 
194
 
 
195
    void Window::penChanged()
 
196
    {
 
197
        int width = penWidthSpinBox-&gt;value();
 
198
        Qt::PenStyle style =
 
199
            (Qt::PenStyle)penStyleComboBox-&gt;itemData(penStyleComboBox-&gt;currentIndex(), IdRole).toInt();
 
200
        Qt::PenCapStyle cap =
 
201
            (Qt::PenCapStyle)penCapComboBox-&gt;itemData(penCapComboBox-&gt;currentIndex(), IdRole).toInt();
 
202
        Qt::PenJoinStyle join =
 
203
            (Qt::PenJoinStyle)penJoinComboBox-&gt;itemData(penJoinComboBox-&gt;currentIndex(), IdRole).toInt();
 
204
 
 
205
        renderArea-&gt;setPen(QPen(Qt::blue, width, style, cap, join));
 
206
    }
 
207
 
 
208
    void Window::brushChanged()
 
209
    {
 
210
        Qt::BrushStyle style =
 
211
            (Qt::BrushStyle)brushStyleComboBox-&gt;itemData(brushStyleComboBox-&gt;currentIndex(), IdRole).toInt();
 
212
 
 
213
        if (style == Qt::LinearGradientPattern) {
 
214
            QLinearGradient linearGradient(0, 0, 100, 100);
 
215
            linearGradient.setColorAt(0.0, Qt::white);
 
216
            linearGradient.setColorAt(0.2, Qt::green);
 
217
            linearGradient.setColorAt(1.0, Qt::black);
 
218
            renderArea-&gt;setBrush(linearGradient);
 
219
        } else if (style == Qt::RadialGradientPattern) {
 
220
            QRadialGradient radialGradient(50, 50, 50, 50, 50);
 
221
            radialGradient.setColorAt(0.0, Qt::white);
 
222
            radialGradient.setColorAt(0.2, Qt::green);
 
223
            radialGradient.setColorAt(1.0, Qt::black);
 
224
            renderArea-&gt;setBrush(radialGradient);
 
225
        } else if (style == Qt::ConicalGradientPattern) {
 
226
            QConicalGradient conicalGradient(50, 50, 150);
 
227
            conicalGradient.setColorAt(0.0, Qt::white);
 
228
            conicalGradient.setColorAt(0.2, Qt::green);
 
229
            conicalGradient.setColorAt(1.0, Qt::black);
 
230
            renderArea-&gt;setBrush(conicalGradient);
 
231
        } else if (style == Qt::TexturePattern) {
 
232
            renderArea-&gt;setBrush(QBrush(QPixmap(&quot;:/images/brick.png&quot;)));
 
233
        } else {
 
234
            renderArea-&gt;setBrush(QBrush(Qt::red, style));
 
235
        }
 
236
    }</pre>
 
237
<p /><address><hr /><div align="center">
 
238
<table width="100%" cellspacing="0" border="0"><tr class="address">
 
239
<td width="30%">Copyright &copy; 2005 <a href="trolltech.html">Trolltech</a></td>
 
240
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
 
241
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
 
242
</tr></table></div></address></body>
 
243
</html>