File indexing completed on 2024-04-28 03:55:59

0001 /*
0002  * SPDX-FileCopyrightText: 2020 Arjen Hiemstra <ahiemstra@heimr.nl>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 
0007 #include "paintedrectangleitem.h"
0008 
0009 #include <QPainter>
0010 #include <cmath>
0011 
0012 PaintedRectangleItem::PaintedRectangleItem(QQuickItem *parent)
0013     : QQuickPaintedItem(parent)
0014 {
0015 }
0016 
0017 void PaintedRectangleItem::setColor(const QColor &color)
0018 {
0019     m_color = color;
0020     update();
0021 }
0022 
0023 void PaintedRectangleItem::setRadius(qreal radius)
0024 {
0025     m_radius = radius;
0026     update();
0027 }
0028 
0029 void PaintedRectangleItem::setBorderColor(const QColor &color)
0030 {
0031     m_borderColor = color;
0032     update();
0033 }
0034 
0035 void PaintedRectangleItem::setBorderWidth(qreal width)
0036 {
0037     m_borderWidth = width;
0038     update();
0039 }
0040 
0041 void PaintedRectangleItem::paint(QPainter *painter)
0042 {
0043     painter->setRenderHint(QPainter::Antialiasing, true);
0044     painter->setPen(Qt::transparent);
0045 
0046     auto radius = std::min(m_radius, std::min(width(), height()) / 2);
0047     auto borderWidth = std::floor(m_borderWidth);
0048 
0049     if (borderWidth > 0.0) {
0050         painter->setBrush(m_borderColor);
0051         painter->drawRoundedRect(0, 0, width(), height(), radius, radius);
0052     }
0053 
0054     painter->setBrush(m_color);
0055     painter->drawRoundedRect(borderWidth, borderWidth, width() - borderWidth * 2, height() - borderWidth * 2, radius, radius);
0056 }
0057 
0058 #include "moc_paintedrectangleitem.cpp"