source: trunk/examples/painting/basicdrawing/renderarea.cpp@ 561

Last change on this file since 561 was 561, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.1 sources.

File size: 5.5 KB
Line 
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 examples 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#include <QtGui>
43
44#include "renderarea.h"
45
46//! [0]
47RenderArea::RenderArea(QWidget *parent)
48 : QWidget(parent)
49{
50 shape = Polygon;
51 antialiased = false;
52 transformed = false;
53 pixmap.load(":/images/qt-logo.png");
54
55 setBackgroundRole(QPalette::Base);
56 setAutoFillBackground(true);
57}
58//! [0]
59
60//! [1]
61QSize RenderArea::minimumSizeHint() const
62{
63 return QSize(100, 100);
64}
65//! [1]
66
67//! [2]
68QSize RenderArea::sizeHint() const
69{
70 return QSize(400, 200);
71}
72//! [2]
73
74//! [3]
75void RenderArea::setShape(Shape shape)
76{
77 this->shape = shape;
78 update();
79}
80//! [3]
81
82//! [4]
83void RenderArea::setPen(const QPen &pen)
84{
85 this->pen = pen;
86 update();
87}
88//! [4]
89
90//! [5]
91void RenderArea::setBrush(const QBrush &brush)
92{
93 this->brush = brush;
94 update();
95}
96//! [5]
97
98//! [6]
99void RenderArea::setAntialiased(bool antialiased)
100{
101 this->antialiased = antialiased;
102 update();
103}
104//! [6]
105
106//! [7]
107void RenderArea::setTransformed(bool transformed)
108{
109 this->transformed = transformed;
110 update();
111}
112//! [7]
113
114//! [8]
115void RenderArea::paintEvent(QPaintEvent * /* event */)
116{
117 static const QPoint points[4] = {
118 QPoint(10, 80),
119 QPoint(20, 10),
120 QPoint(80, 30),
121 QPoint(90, 70)
122 };
123
124 QRect rect(10, 20, 80, 60);
125
126 QPainterPath path;
127 path.moveTo(20, 80);
128 path.lineTo(20, 30);
129 path.cubicTo(80, 0, 50, 50, 80, 80);
130
131 int startAngle = 20 * 16;
132 int arcLength = 120 * 16;
133//! [8]
134
135//! [9]
136 QPainter painter(this);
137 painter.setPen(pen);
138 painter.setBrush(brush);
139 if (antialiased)
140 painter.setRenderHint(QPainter::Antialiasing, true);
141//! [9]
142
143//! [10]
144 for (int x = 0; x < width(); x += 100) {
145 for (int y = 0; y < height(); y += 100) {
146 painter.save();
147 painter.translate(x, y);
148//! [10] //! [11]
149 if (transformed) {
150 painter.translate(50, 50);
151 painter.rotate(60.0);
152 painter.scale(0.6, 0.9);
153 painter.translate(-50, -50);
154 }
155//! [11]
156
157//! [12]
158 switch (shape) {
159 case Line:
160 painter.drawLine(rect.bottomLeft(), rect.topRight());
161 break;
162 case Points:
163 painter.drawPoints(points, 4);
164 break;
165 case Polyline:
166 painter.drawPolyline(points, 4);
167 break;
168 case Polygon:
169 painter.drawPolygon(points, 4);
170 break;
171 case Rect:
172 painter.drawRect(rect);
173 break;
174 case RoundedRect:
175 painter.drawRoundedRect(rect, 25, 25, Qt::RelativeSize);
176 break;
177 case Ellipse:
178 painter.drawEllipse(rect);
179 break;
180 case Arc:
181 painter.drawArc(rect, startAngle, arcLength);
182 break;
183 case Chord:
184 painter.drawChord(rect, startAngle, arcLength);
185 break;
186 case Pie:
187 painter.drawPie(rect, startAngle, arcLength);
188 break;
189 case Path:
190 painter.drawPath(path);
191 break;
192 case Text:
193 painter.drawText(rect, Qt::AlignCenter, tr("Qt by\nNokia"));
194 break;
195 case Pixmap:
196 painter.drawPixmap(10, 10, pixmap);
197 }
198//! [12] //! [13]
199 painter.restore();
200 }
201 }
202
203 painter.setRenderHint(QPainter::Antialiasing, false);
204 painter.setPen(palette().dark().color());
205 painter.setBrush(Qt::NoBrush);
206 painter.drawRect(QRect(0, 0, width() - 1, height() - 1));
207}
208//! [13]
Note: See TracBrowser for help on using the repository browser.