1 /****************************************************************************
2 **
3 ** Copyright (C) 2017 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the Qt Quick Templates 2 module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL3$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPLv3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or later as published by the Free
28 ** Software Foundation and appearing in the file LICENSE.GPL included in
29 ** the packaging of this file. Please review the following information to
30 ** ensure the GNU General Public License version 2.0 requirements will be
31 ** met: http://www.gnu.org/licenses/gpl-2.0.html.
32 **
33 ** $QT_END_LICENSE$
34 **
35 ****************************************************************************/
36 
37 #include "qquickroundbutton_p.h"
38 
39 #include <QtQuickTemplates2/private/qquickbutton_p_p.h>
40 
41 QT_BEGIN_NAMESPACE
42 
43 /*!
44     \qmltype RoundButton
45     \inherits Button
46 //!     \instantiates QQuickRoundButton
47     \inqmlmodule QtQuick.Controls
48     \since 5.8
49     \ingroup qtquickcontrols2-buttons
50     \brief A push-button control with rounded corners that can be clicked by the user.
51 
52     \image qtquickcontrols2-roundbutton.png
53 
54     RoundButton is identical to \l Button, except that it has a \l radius property
55     which allows the corners to be rounded without having to customize the
56     \l background.
57 
58     \snippet qtquickcontrols2-roundbutton.qml 1
59 
60     \sa {Customizing RoundButton}, {Button Controls}
61 */
62 
63 class QQuickRoundButtonPrivate : public QQuickButtonPrivate
64 {
65     Q_DECLARE_PUBLIC(QQuickRoundButton)
66 
67 public:
68     void setRadius(qreal newRadius = -1.0);
69 
70     qreal radius = 0;
71     bool explicitRadius = false;
72 };
73 
setRadius(qreal newRadius)74 void QQuickRoundButtonPrivate::setRadius(qreal newRadius)
75 {
76     Q_Q(QQuickRoundButton);
77     const qreal oldRadius = radius;
78     if (newRadius < 0)
79         radius = qMax<qreal>(0, qMin(width, height) / 2);
80     else
81         radius = newRadius;
82 
83     if (!qFuzzyCompare(radius, oldRadius))
84         emit q->radiusChanged();
85 }
86 
QQuickRoundButton(QQuickItem * parent)87 QQuickRoundButton::QQuickRoundButton(QQuickItem *parent)
88     : QQuickButton(*(new QQuickRoundButtonPrivate), parent)
89 {
90 }
91 
92 /*!
93     \qmlproperty real QtQuick.Controls::RoundButton::radius
94 
95     This property holds the radius of the button.
96 
97     To create a relatively square button that has slightly rounded corners,
98     use a small value, such as \c 3.
99 
100     To create a completely circular button (the default), use a value that is
101     equal to half of the width or height of the button, and make the button's
102     width and height identical.
103 
104     To reset this property back to the default value, set its value to
105     \c undefined.
106 */
radius() const107 qreal QQuickRoundButton::radius() const
108 {
109     Q_D(const QQuickRoundButton);
110     return d->radius;
111 }
112 
setRadius(qreal radius)113 void QQuickRoundButton::setRadius(qreal radius)
114 {
115     Q_D(QQuickRoundButton);
116     d->explicitRadius = true;
117     d->setRadius(radius);
118 }
119 
resetRadius()120 void QQuickRoundButton::resetRadius()
121 {
122     Q_D(QQuickRoundButton);
123     d->explicitRadius = false;
124     d->setRadius();
125 }
126 
geometryChanged(const QRectF & newGeometry,const QRectF & oldGeometry)127 void QQuickRoundButton::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry)
128 {
129     Q_D(QQuickRoundButton);
130     QQuickControl::geometryChanged(newGeometry, oldGeometry);
131     if (!d->explicitRadius)
132         d->setRadius();
133 }
134 
135 QT_END_NAMESPACE
136