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