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 examples of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:BSD$
|
---|
10 | ** You may use this file under the terms of the BSD license as follows:
|
---|
11 | **
|
---|
12 | ** "Redistribution and use in source and binary forms, with or without
|
---|
13 | ** modification, are permitted provided that the following conditions are
|
---|
14 | ** met:
|
---|
15 | ** * Redistributions of source code must retain the above copyright
|
---|
16 | ** notice, this list of conditions and the following disclaimer.
|
---|
17 | ** * Redistributions in binary form must reproduce the above copyright
|
---|
18 | ** notice, this list of conditions and the following disclaimer in
|
---|
19 | ** the documentation and/or other materials provided with the
|
---|
20 | ** distribution.
|
---|
21 | ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
---|
22 | ** the names of its contributors may be used to endorse or promote
|
---|
23 | ** products derived from this software without specific prior written
|
---|
24 | ** permission.
|
---|
25 | **
|
---|
26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
---|
37 | ** $QT_END_LICENSE$
|
---|
38 | **
|
---|
39 | ****************************************************************************/
|
---|
40 |
|
---|
41 | #include <QtGui>
|
---|
42 |
|
---|
43 | #include "renderarea.h"
|
---|
44 |
|
---|
45 | //! [0]
|
---|
46 | RenderArea::RenderArea(QWidget *parent)
|
---|
47 | : QWidget(parent)
|
---|
48 | {
|
---|
49 | shape = Polygon;
|
---|
50 | antialiased = false;
|
---|
51 | transformed = false;
|
---|
52 | pixmap.load(":/images/qt-logo.png");
|
---|
53 |
|
---|
54 | setBackgroundRole(QPalette::Base);
|
---|
55 | setAutoFillBackground(true);
|
---|
56 | }
|
---|
57 | //! [0]
|
---|
58 |
|
---|
59 | //! [1]
|
---|
60 | QSize RenderArea::minimumSizeHint() const
|
---|
61 | {
|
---|
62 | return QSize(100, 100);
|
---|
63 | }
|
---|
64 | //! [1]
|
---|
65 |
|
---|
66 | //! [2]
|
---|
67 | QSize RenderArea::sizeHint() const
|
---|
68 | {
|
---|
69 | return QSize(400, 200);
|
---|
70 | }
|
---|
71 | //! [2]
|
---|
72 |
|
---|
73 | //! [3]
|
---|
74 | void RenderArea::setShape(Shape shape)
|
---|
75 | {
|
---|
76 | this->shape = shape;
|
---|
77 | update();
|
---|
78 | }
|
---|
79 | //! [3]
|
---|
80 |
|
---|
81 | //! [4]
|
---|
82 | void RenderArea::setPen(const QPen &pen)
|
---|
83 | {
|
---|
84 | this->pen = pen;
|
---|
85 | update();
|
---|
86 | }
|
---|
87 | //! [4]
|
---|
88 |
|
---|
89 | //! [5]
|
---|
90 | void RenderArea::setBrush(const QBrush &brush)
|
---|
91 | {
|
---|
92 | this->brush = brush;
|
---|
93 | update();
|
---|
94 | }
|
---|
95 | //! [5]
|
---|
96 |
|
---|
97 | //! [6]
|
---|
98 | void RenderArea::setAntialiased(bool antialiased)
|
---|
99 | {
|
---|
100 | this->antialiased = antialiased;
|
---|
101 | update();
|
---|
102 | }
|
---|
103 | //! [6]
|
---|
104 |
|
---|
105 | //! [7]
|
---|
106 | void RenderArea::setTransformed(bool transformed)
|
---|
107 | {
|
---|
108 | this->transformed = transformed;
|
---|
109 | update();
|
---|
110 | }
|
---|
111 | //! [7]
|
---|
112 |
|
---|
113 | //! [8]
|
---|
114 | void RenderArea::paintEvent(QPaintEvent * /* event */)
|
---|
115 | {
|
---|
116 | static const QPoint points[4] = {
|
---|
117 | QPoint(10, 80),
|
---|
118 | QPoint(20, 10),
|
---|
119 | QPoint(80, 30),
|
---|
120 | QPoint(90, 70)
|
---|
121 | };
|
---|
122 |
|
---|
123 | QRect rect(10, 20, 80, 60);
|
---|
124 |
|
---|
125 | QPainterPath path;
|
---|
126 | path.moveTo(20, 80);
|
---|
127 | path.lineTo(20, 30);
|
---|
128 | path.cubicTo(80, 0, 50, 50, 80, 80);
|
---|
129 |
|
---|
130 | int startAngle = 20 * 16;
|
---|
131 | int arcLength = 120 * 16;
|
---|
132 | //! [8]
|
---|
133 |
|
---|
134 | //! [9]
|
---|
135 | QPainter painter(this);
|
---|
136 | painter.setPen(pen);
|
---|
137 | painter.setBrush(brush);
|
---|
138 | if (antialiased)
|
---|
139 | painter.setRenderHint(QPainter::Antialiasing, true);
|
---|
140 | //! [9]
|
---|
141 |
|
---|
142 | //! [10]
|
---|
143 | for (int x = 0; x < width(); x += 100) {
|
---|
144 | for (int y = 0; y < height(); y += 100) {
|
---|
145 | painter.save();
|
---|
146 | painter.translate(x, y);
|
---|
147 | //! [10] //! [11]
|
---|
148 | if (transformed) {
|
---|
149 | painter.translate(50, 50);
|
---|
150 | painter.rotate(60.0);
|
---|
151 | painter.scale(0.6, 0.9);
|
---|
152 | painter.translate(-50, -50);
|
---|
153 | }
|
---|
154 | //! [11]
|
---|
155 |
|
---|
156 | //! [12]
|
---|
157 | switch (shape) {
|
---|
158 | case Line:
|
---|
159 | painter.drawLine(rect.bottomLeft(), rect.topRight());
|
---|
160 | break;
|
---|
161 | case Points:
|
---|
162 | painter.drawPoints(points, 4);
|
---|
163 | break;
|
---|
164 | case Polyline:
|
---|
165 | painter.drawPolyline(points, 4);
|
---|
166 | break;
|
---|
167 | case Polygon:
|
---|
168 | painter.drawPolygon(points, 4);
|
---|
169 | break;
|
---|
170 | case Rect:
|
---|
171 | painter.drawRect(rect);
|
---|
172 | break;
|
---|
173 | case RoundedRect:
|
---|
174 | painter.drawRoundedRect(rect, 25, 25, Qt::RelativeSize);
|
---|
175 | break;
|
---|
176 | case Ellipse:
|
---|
177 | painter.drawEllipse(rect);
|
---|
178 | break;
|
---|
179 | case Arc:
|
---|
180 | painter.drawArc(rect, startAngle, arcLength);
|
---|
181 | break;
|
---|
182 | case Chord:
|
---|
183 | painter.drawChord(rect, startAngle, arcLength);
|
---|
184 | break;
|
---|
185 | case Pie:
|
---|
186 | painter.drawPie(rect, startAngle, arcLength);
|
---|
187 | break;
|
---|
188 | case Path:
|
---|
189 | painter.drawPath(path);
|
---|
190 | break;
|
---|
191 | case Text:
|
---|
192 | painter.drawText(rect, Qt::AlignCenter, tr("Qt by\nNokia"));
|
---|
193 | break;
|
---|
194 | case Pixmap:
|
---|
195 | painter.drawPixmap(10, 10, pixmap);
|
---|
196 | }
|
---|
197 | //! [12] //! [13]
|
---|
198 | painter.restore();
|
---|
199 | }
|
---|
200 | }
|
---|
201 |
|
---|
202 | painter.setRenderHint(QPainter::Antialiasing, false);
|
---|
203 | painter.setPen(palette().dark().color());
|
---|
204 | painter.setBrush(Qt::NoBrush);
|
---|
205 | painter.drawRect(QRect(0, 0, width() - 1, height() - 1));
|
---|
206 | }
|
---|
207 | //! [13]
|
---|