| 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 |
|
| 8 |
//! [0] |
| 9 |
RenderArea::RenderArea(const QPainterPath &path, QWidget *parent) |
| 10 |
: QWidget(parent), path(path) |
| 11 |
{ |
| 12 |
penWidth = 1; |
| 13 |
rotationAngle = 0; |
| 14 |
setBackgroundRole(QPalette::Base); |
| 15 |
} |
| 16 |
//! [0] |
| 17 |
|
| 18 |
//! [1] |
| 19 |
QSize RenderArea::minimumSizeHint() const |
| 20 |
{ |
| 21 |
return QSize(50, 50); |
| 22 |
} |
| 23 |
//! [1] |
| 24 |
|
| 25 |
//! [2] |
| 26 |
QSize RenderArea::sizeHint() const |
| 27 |
{ |
| 28 |
return QSize(100, 100); |
| 29 |
} |
| 30 |
//! [2] |
| 31 |
|
| 32 |
//! [3] |
| 33 |
void RenderArea::setFillRule(Qt::FillRule rule) |
| 34 |
{ |
| 35 |
path.setFillRule(rule); |
| 36 |
update(); |
| 37 |
} |
| 38 |
//! [3] |
| 39 |
|
| 40 |
//! [4] |
| 41 |
void RenderArea::setFillGradient(const QColor &color1, const QColor &color2) |
| 42 |
{ |
| 43 |
fillColor1 = color1; |
| 44 |
fillColor2 = color2; |
| 45 |
update(); |
| 46 |
} |
| 47 |
//! [4] |
| 48 |
|
| 49 |
//! [5] |
| 50 |
void RenderArea::setPenWidth(int width) |
| 51 |
{ |
| 52 |
penWidth = width; |
| 53 |
update(); |
| 54 |
} |
| 55 |
//! [5] |
| 56 |
|
| 57 |
//! [6] |
| 58 |
void RenderArea::setPenColor(const QColor &color) |
| 59 |
{ |
| 60 |
penColor = color; |
| 61 |
update(); |
| 62 |
} |
| 63 |
//! [6] |
| 64 |
|
| 65 |
//! [7] |
| 66 |
void RenderArea::setRotationAngle(int degrees) |
| 67 |
{ |
| 68 |
rotationAngle = degrees; |
| 69 |
update(); |
| 70 |
} |
| 71 |
//! [7] |
| 72 |
|
| 73 |
//! [8] |
| 74 |
void RenderArea::paintEvent(QPaintEvent *) |
| 75 |
{ |
| 76 |
QPainter painter(this); |
| 77 |
painter.setRenderHint(QPainter::Antialiasing); |
| 78 |
//! [8] //! [9] |
| 79 |
painter.scale(width() / 100.0, height() / 100.0); |
| 80 |
painter.translate(50.0, 50.0); |
| 81 |
painter.rotate(-rotationAngle); |
| 82 |
painter.translate(-50.0, -50.0); |
| 83 |
|
| 84 |
//! [9] //! [10] |
| 85 |
painter.setPen(QPen(penColor, penWidth, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)); |
| 86 |
QLinearGradient gradient(0, 0, 0, 100); |
| 87 |
gradient.setColorAt(0.0, fillColor1); |
| 88 |
gradient.setColorAt(1.0, fillColor2); |
| 89 |
painter.setBrush(gradient); |
| 90 |
painter.drawPath(path); |
| 91 |
} |
| 92 |
//! [10] |