1 | /* |
2 | * SPDX-FileCopyrightText: 2020 Arjen Hiemstra <ahiemstra@heimr.nl> |
3 | * |
4 | * SPDX-License-Identifier: LGPL-2.0-or-later |
5 | */ |
6 | |
7 | #include "paintedrectangleitem.h" |
8 | |
9 | #include <QPainter> |
10 | #include <cmath> |
11 | |
12 | PaintedRectangleItem::PaintedRectangleItem(QQuickItem *parent) |
13 | : QQuickPaintedItem(parent) |
14 | { |
15 | } |
16 | |
17 | void PaintedRectangleItem::setColor(const QColor &color) |
18 | { |
19 | m_color = color; |
20 | update(); |
21 | } |
22 | |
23 | void PaintedRectangleItem::setRadius(qreal radius) |
24 | { |
25 | m_radius = radius; |
26 | update(); |
27 | } |
28 | |
29 | void PaintedRectangleItem::setBorderColor(const QColor &color) |
30 | { |
31 | m_borderColor = color; |
32 | update(); |
33 | } |
34 | |
35 | void PaintedRectangleItem::setBorderWidth(qreal width) |
36 | { |
37 | m_borderWidth = width; |
38 | update(); |
39 | } |
40 | |
41 | void PaintedRectangleItem::paint(QPainter *painter) |
42 | { |
43 | painter->setRenderHint(hint: QPainter::Antialiasing, on: true); |
44 | painter->setPen(Qt::transparent); |
45 | |
46 | auto radius = std::min(a: m_radius, b: std::min(a: width(), b: height()) / 2); |
47 | auto borderWidth = std::floor(x: m_borderWidth); |
48 | |
49 | if (borderWidth > 0.0) { |
50 | painter->setBrush(m_borderColor); |
51 | painter->drawRoundedRect(x: 0, y: 0, w: width(), h: height(), xRadius: radius, yRadius: radius); |
52 | } |
53 | |
54 | painter->setBrush(m_color); |
55 | painter->drawRoundedRect(x: borderWidth, y: borderWidth, w: width() - borderWidth * 2, h: height() - borderWidth * 2, xRadius: radius, yRadius: radius); |
56 | } |
57 | |
58 | #include "moc_paintedrectangleitem.cpp" |
59 | |