1// Copyright (C) 2017 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qquickroundbutton_p.h"
5
6#include <QtQuickTemplates2/private/qquickbutton_p_p.h>
7
8QT_BEGIN_NAMESPACE
9
10/*!
11 \qmltype RoundButton
12 \inherits Button
13//! \instantiates QQuickRoundButton
14 \inqmlmodule QtQuick.Controls
15 \since 5.8
16 \ingroup qtquickcontrols-buttons
17 \brief A push-button control with rounded corners that can be clicked by the user.
18
19 \image qtquickcontrols-roundbutton.png
20
21 RoundButton is identical to \l Button, except that it has a \l radius property
22 which allows the corners to be rounded without having to customize the
23 \l background.
24
25 \snippet qtquickcontrols-roundbutton.qml 1
26
27 \sa {Customizing RoundButton}, {Button Controls}
28*/
29
30class QQuickRoundButtonPrivate : public QQuickButtonPrivate
31{
32 Q_DECLARE_PUBLIC(QQuickRoundButton)
33
34public:
35 void setRadius(qreal newRadius = -1.0);
36
37 qreal radius = 0;
38 bool explicitRadius = false;
39};
40
41void QQuickRoundButtonPrivate::setRadius(qreal newRadius)
42{
43 Q_Q(QQuickRoundButton);
44 const qreal oldRadius = radius;
45 if (newRadius < 0)
46 radius = qMax<qreal>(a: 0, b: qMin<qreal>(a: width, b: height) / 2);
47 else
48 radius = newRadius;
49
50 if (!qFuzzyCompare(p1: radius, p2: oldRadius))
51 emit q->radiusChanged();
52}
53
54QQuickRoundButton::QQuickRoundButton(QQuickItem *parent)
55 : QQuickButton(*(new QQuickRoundButtonPrivate), parent)
56{
57}
58
59/*!
60 \qmlproperty real QtQuick.Controls::RoundButton::radius
61
62 This property holds the radius of the button.
63
64 To create a relatively square button that has slightly rounded corners,
65 use a small value, such as \c 3.
66
67 To create a completely circular button (the default), use a value that is
68 equal to half of the width or height of the button, and make the button's
69 width and height identical.
70
71 To reset this property back to the default value, set its value to
72 \c undefined.
73*/
74qreal QQuickRoundButton::radius() const
75{
76 Q_D(const QQuickRoundButton);
77 return d->radius;
78}
79
80void QQuickRoundButton::setRadius(qreal radius)
81{
82 Q_D(QQuickRoundButton);
83 d->explicitRadius = true;
84 d->setRadius(radius);
85}
86
87void QQuickRoundButton::resetRadius()
88{
89 Q_D(QQuickRoundButton);
90 d->explicitRadius = false;
91 d->setRadius();
92}
93
94void QQuickRoundButton::geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry)
95{
96 Q_D(QQuickRoundButton);
97 QQuickControl::geometryChange(newGeometry, oldGeometry);
98 if (!d->explicitRadius)
99 d->setRadius();
100}
101
102QT_END_NAMESPACE
103
104#include "moc_qquickroundbutton_p.cpp"
105

source code of qtdeclarative/src/quicktemplates/qquickroundbutton.cpp