| 1 |
// Copyright (C) 2016 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause |
| 3 |
|
| 4 |
#include "renderarea.h" |
| 5 |
|
| 6 |
#include <QPainter> |
| 7 |
#include <QPainterPath> |
| 8 |
#include <QPainterStateGuard> |
| 9 |
|
| 10 |
//! [0] |
| 11 |
RenderArea::RenderArea(QWidget *parent) |
| 12 |
: QWidget(parent) |
| 13 |
{ |
| 14 |
shape = Polygon; |
| 15 |
antialiased = false; |
| 16 |
transformed = false; |
| 17 |
pixmap.load(":/images/qt-logo.png"); |
| 18 |
|
| 19 |
setBackgroundRole(QPalette::Base); |
| 20 |
setAutoFillBackground(true); |
| 21 |
} |
| 22 |
//! [0] |
| 23 |
|
| 24 |
//! [1] |
| 25 |
QSize RenderArea::minimumSizeHint() const |
| 26 |
{ |
| 27 |
return QSize(100, 100); |
| 28 |
} |
| 29 |
//! [1] |
| 30 |
|
| 31 |
//! [2] |
| 32 |
QSize RenderArea::sizeHint() const |
| 33 |
{ |
| 34 |
return QSize(400, 200); |
| 35 |
} |
| 36 |
//! [2] |
| 37 |
|
| 38 |
//! [3] |
| 39 |
void RenderArea::setShape(Shape shape) |
| 40 |
{ |
| 41 |
this->shape = shape; |
| 42 |
update(); |
| 43 |
} |
| 44 |
//! [3] |
| 45 |
|
| 46 |
//! [4] |
| 47 |
void RenderArea::setPen(const QPen &pen) |
| 48 |
{ |
| 49 |
this->pen = pen; |
| 50 |
update(); |
| 51 |
} |
| 52 |
//! [4] |
| 53 |
|
| 54 |
//! [5] |
| 55 |
void RenderArea::setBrush(const QBrush &brush) |
| 56 |
{ |
| 57 |
this->brush = brush; |
| 58 |
update(); |
| 59 |
} |
| 60 |
//! [5] |
| 61 |
|
| 62 |
//! [6] |
| 63 |
void RenderArea::setAntialiased(bool antialiased) |
| 64 |
{ |
| 65 |
this->antialiased = antialiased; |
| 66 |
update(); |
| 67 |
} |
| 68 |
//! [6] |
| 69 |
|
| 70 |
//! [7] |
| 71 |
void RenderArea::setTransformed(bool transformed) |
| 72 |
{ |
| 73 |
this->transformed = transformed; |
| 74 |
update(); |
| 75 |
} |
| 76 |
//! [7] |
| 77 |
|
| 78 |
//! [8] |
| 79 |
void RenderArea::paintEvent(QPaintEvent * /* event */) |
| 80 |
{ |
| 81 |
static const QPoint points[4] = { |
| 82 |
QPoint(10, 80), |
| 83 |
QPoint(20, 10), |
| 84 |
QPoint(80, 30), |
| 85 |
QPoint(90, 70) |
| 86 |
}; |
| 87 |
|
| 88 |
QRect rect(10, 20, 80, 60); |
| 89 |
|
| 90 |
QPainterPath path; |
| 91 |
path.moveTo(20, 80); |
| 92 |
path.lineTo(20, 30); |
| 93 |
path.cubicTo(80, 0, 50, 50, 80, 80); |
| 94 |
|
| 95 |
int startAngle = 20 * 16; |
| 96 |
int arcLength = 120 * 16; |
| 97 |
//! [8] |
| 98 |
|
| 99 |
//! [9] |
| 100 |
QPainter painter(this); |
| 101 |
painter.setPen(pen); |
| 102 |
painter.setBrush(brush); |
| 103 |
if (antialiased) |
| 104 |
painter.setRenderHint(QPainter::Antialiasing, true); |
| 105 |
//! [9] |
| 106 |
|
| 107 |
//! [10] |
| 108 |
for (int x = 0; x < width(); x += 100) { |
| 109 |
for (int y = 0; y < height(); y += 100) { |
| 110 |
QPainterStateGuard guard(&painter); |
| 111 |
painter.translate(x, y); |
| 112 |
//! [10] //! [11] |
| 113 |
if (transformed) { |
| 114 |
painter.translate(50, 50); |
| 115 |
painter.rotate(60.0); |
| 116 |
painter.scale(0.6, 0.9); |
| 117 |
painter.translate(-50, -50); |
| 118 |
} |
| 119 |
//! [11] |
| 120 |
|
| 121 |
//! [12] |
| 122 |
switch (shape) { |
| 123 |
case Line: |
| 124 |
painter.drawLine(rect.bottomLeft(), rect.topRight()); |
| 125 |
break; |
| 126 |
case Points: |
| 127 |
painter.drawPoints(points, 4); |
| 128 |
break; |
| 129 |
case Polyline: |
| 130 |
painter.drawPolyline(points, 4); |
| 131 |
break; |
| 132 |
case Polygon: |
| 133 |
painter.drawPolygon(points, 4); |
| 134 |
break; |
| 135 |
case Rect: |
| 136 |
painter.drawRect(rect); |
| 137 |
break; |
| 138 |
case RoundedRect: |
| 139 |
painter.drawRoundedRect(rect, 25, 25, Qt::RelativeSize); |
| 140 |
break; |
| 141 |
case Ellipse: |
| 142 |
painter.drawEllipse(rect); |
| 143 |
break; |
| 144 |
case Arc: |
| 145 |
painter.drawArc(rect, startAngle, arcLength); |
| 146 |
break; |
| 147 |
case Chord: |
| 148 |
painter.drawChord(rect, startAngle, arcLength); |
| 149 |
break; |
| 150 |
case Pie: |
| 151 |
painter.drawPie(rect, startAngle, arcLength); |
| 152 |
break; |
| 153 |
case Path: |
| 154 |
painter.drawPath(path); |
| 155 |
break; |
| 156 |
case Text: |
| 157 |
painter.drawText(rect, |
| 158 |
Qt::AlignCenter, |
| 159 |
tr("Qt by\nThe Qt Company")); |
| 160 |
break; |
| 161 |
case Pixmap: |
| 162 |
painter.drawPixmap(10, 10, pixmap); |
| 163 |
} |
| 164 |
//! [12] //! [13] |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
painter.setRenderHint(QPainter::Antialiasing, false); |
| 169 |
painter.setPen(palette().dark().color()); |
| 170 |
painter.setBrush(Qt::NoBrush); |
| 171 |
painter.drawRect(QRect(0, 0, width() - 1, height() - 1)); |
| 172 |
} |
| 173 |
//! [13] |