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

Last change on this file since 168 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 5.5 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the examples of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** 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 are unsure which license is appropriate for your use, please
37** contact the sales department 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 painter.translate(+0.5, +0.5);
143 }
144
145//! [10]
146 for (int x = 0; x < width(); x += 100) {
147 for (int y = 0; y < height(); y += 100) {
148 painter.save();
149 painter.translate(x, y);
150//! [10] //! [11]
151 if (transformed) {
152 painter.translate(50, 50);
153 painter.rotate(60.0);
154 painter.scale(0.6, 0.9);
155 painter.translate(-50, -50);
156 }
157//! [11]
158
159//! [12]
160 switch (shape) {
161 case Line:
162 painter.drawLine(rect.bottomLeft(), rect.topRight());
163 break;
164 case Points:
165 painter.drawPoints(points, 4);
166 break;
167 case Polyline:
168 painter.drawPolyline(points, 4);
169 break;
170 case Polygon:
171 painter.drawPolygon(points, 4);
172 break;
173 case Rect:
174 painter.drawRect(rect);
175 break;
176 case RoundedRect:
177 painter.drawRoundedRect(rect, 25, 25, Qt::RelativeSize);
178 break;
179 case Ellipse:
180 painter.drawEllipse(rect);
181 break;
182 case Arc:
183 painter.drawArc(rect, startAngle, arcLength);
184 break;
185 case Chord:
186 painter.drawChord(rect, startAngle, arcLength);
187 break;
188 case Pie:
189 painter.drawPie(rect, startAngle, arcLength);
190 break;
191 case Path:
192 painter.drawPath(path);
193 break;
194 case Text:
195 painter.drawText(rect, Qt::AlignCenter, tr("Qt by\nQt Software"));
196 break;
197 case Pixmap:
198 painter.drawPixmap(10, 10, pixmap);
199 }
200//! [12] //! [13]
201 painter.restore();
202 }
203 }
204
205 painter.setPen(palette().dark().color());
206 painter.setBrush(Qt::NoBrush);
207 painter.drawRect(QRect(0, 0, width() - 1, height() - 1));
208}
209//! [13]
Note: See TracBrowser for help on using the repository browser.