Kirigami2

paintedrectangleitem.cpp
1 /*
2  * SPDX-FileCopyrightText: 2020 Arjen Hiemstra <[email protected]>
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(QPainter::Antialiasing, true);
44  painter->setPen(Qt::transparent);
45 
46  auto radius = std::min(m_radius, std::min(width(), height()) / 2);
47  auto borderWidth = std::floor(m_borderWidth);
48 
49  if (borderWidth > 0.0) {
50  painter->setBrush(m_borderColor);
51  painter->drawRoundedRect(0, 0, width(), height(), radius, radius);
52  }
53 
54  painter->setBrush(m_color);
55  painter->drawRoundedRect(borderWidth, borderWidth, width() - borderWidth * 2, height() - borderWidth * 2, radius, radius);
56 }
void setPen(const QColor &color)
void drawRoundedRect(const QRectF &rect, qreal xRadius, qreal yRadius, Qt::SizeMode mode)
void setBrush(const QBrush &brush)
void update(Part *part, const QByteArray &data, qint64 dataSize)
void setRenderHint(QPainter::RenderHint hint, bool on)
transparent
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sun Jan 29 2023 04:11:03 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.