aboutsummaryrefslogtreecommitdiffstats
path: root/src/templates/qquickabstractbutton.cpp
blob: 8f3e8af8e58422e0f785bbbd4feb87b4649bcbfa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the Qt Labs Templates module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL3$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPLv3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or later as published by the Free
** Software Foundation and appearing in the file LICENSE.GPL included in
** the packaging of this file. Please review the following information to
** ensure the GNU General Public License version 2.0 requirements will be
** met: http://www.gnu.org/licenses/gpl-2.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qquickabstractbutton_p.h"
#include "qquickabstractbutton_p_p.h"

#include <QtQuick/private/qquickevents_p_p.h>

QT_BEGIN_NAMESPACE

/*!
    \qmltype AbstractButton
    \inherits Control
    \instantiates QQuickAbstractButton
    \inqmlmodule Qt.labs.controls
    \qmlabstract
    \internal
*/

/*!
    \qmlsignal Qt.labs.controls::AbstractButton::pressed(MouseEvent mouse)

    This signal is emitted when the button is interactively pressed by the user.

    The mouse parameter provides information about the press, including the x
    and y position and which button was pressed.
*/

/*!
    \qmlsignal Qt.labs.controls::AbstractButton::released(MouseEvent mouse)

    This signal is emitted when the button is interactively released by the user.

    The mouse parameter provides information about the click, including the x
    and y position of the release of the click, and whether the click was held.
*/

/*!
    \qmlsignal Qt.labs.controls::AbstractButton::canceled()

    This signal is emitted when the button loses mouse grab
    while being pressed, or when it would emit the \l released
    signal but the mouse cursor is not inside the button.
*/

/*!
    \qmlsignal Qt.labs.controls::AbstractButton::clicked(MouseEvent mouse)

    This signal is emitted when the button is interactively clicked by the user.

    The mouse parameter provides information about the click, including the x
    and y position of the release of the click, and whether the click was held.
*/

/*!
    \qmlsignal Qt.labs.controls::AbstractButton::doubleClicked(MouseEvent mouse)

    This signal is emitted when the button is interactively double clicked by the user.

    The mouse parameter provides information about the click, including the x
    and y position of the release of the click, and whether the click was held.
*/

QQuickAbstractButtonPrivate::QQuickAbstractButtonPrivate() :
    pressed(false), checked(false), checkable(false), exclusive(false),
    label(Q_NULLPTR), indicator(Q_NULLPTR)
{
}

QQuickAbstractButton::QQuickAbstractButton(QQuickItem *parent) :
    QQuickControl(*(new QQuickAbstractButtonPrivate), parent)
{
    setActiveFocusOnTab(true);
    setAcceptedMouseButtons(Qt::LeftButton);
    setAccessibleRole(0x0000002B); //QAccessible::Button
}

QQuickAbstractButton::QQuickAbstractButton(QQuickAbstractButtonPrivate &dd, QQuickItem *parent) :
    QQuickControl(dd, parent)
{
    setActiveFocusOnTab(true);
    setAcceptedMouseButtons(Qt::LeftButton);
    setAccessibleRole(0x0000002B); //QAccessible::Button
}

/*!
    \qmlproperty string Qt.labs.controls::AbstractButton::text

    This property holds a textual description of the button.

    \note The text is used for accessibility purposes, so it makes sense to
          set a textual description even if the label item is an image.

    \sa label
*/
QString QQuickAbstractButton::text() const
{
    Q_D(const QQuickAbstractButton);
    return d->text;
}

void QQuickAbstractButton::setText(const QString &text)
{
    Q_D(QQuickAbstractButton);
    if (d->text != text) {
        d->text = text;
        setAccessibleName(text);
        emit textChanged();
    }
}

/*!
    \qmlproperty bool Qt.labs.controls::AbstractButton::pressed

    This property holds whether the button is pressed.
*/
bool QQuickAbstractButton::isPressed() const
{
    Q_D(const QQuickAbstractButton);
    return d->pressed;
}

void QQuickAbstractButton::setPressed(bool isPressed)
{
    Q_D(QQuickAbstractButton);
    if (d->pressed != isPressed) {
        d->pressed = isPressed;
        setAccessibleProperty("pressed", isPressed);
        emit pressedChanged();
    }
}

/*!
    \qmlproperty bool Qt.labs.controls::AbstractButton::checked

    This property holds whether the button is checked.
*/
bool QQuickAbstractButton::isChecked() const
{
    Q_D(const QQuickAbstractButton);
    return d->checked;
}

void QQuickAbstractButton::setChecked(bool checked)
{
    Q_D(QQuickAbstractButton);
    if (d->checked != checked) {
        d->checked = checked;
        setAccessibleProperty("checked", checked);
        emit checkedChanged();
    }
}

/*!
    \qmlproperty bool Qt.labs.controls::AbstractButton::checkable

    This property holds whether the button is checkable.
*/
bool QQuickAbstractButton::isCheckable() const
{
    Q_D(const QQuickAbstractButton);
    return d->checkable;
}

void QQuickAbstractButton::setCheckable(bool checkable)
{
    Q_D(QQuickAbstractButton);
    if (d->checkable != checkable) {
        d->checkable = checkable;
        setAccessibleProperty("checkable", checkable);
        emit checkableChanged();
    }
}

bool QQuickAbstractButton::isExclusive() const
{
    Q_D(const QQuickAbstractButton);
    return d->exclusive;
}

void QQuickAbstractButton::setExclusive(bool exclusive)
{
    Q_D(QQuickAbstractButton);
    d->exclusive = exclusive;
}

/*!
    \qmlproperty Item Qt.labs.controls::AbstractButton::indicator

    This property holds the indicator item.
*/
QQuickItem *QQuickAbstractButton::indicator() const
{
    Q_D(const QQuickAbstractButton);
    return d->indicator;
}

void QQuickAbstractButton::setIndicator(QQuickItem *indicator)
{
    Q_D(QQuickAbstractButton);
    if (d->indicator != indicator) {
        delete d->indicator;
        d->indicator = indicator;
        if (indicator) {
            if (!indicator->parentItem())
                indicator->setParentItem(this);
            indicator->setAcceptedMouseButtons(Qt::LeftButton);
        }
        emit indicatorChanged();
    }
}

/*!
    \qmlproperty Item Qt.labs.controls::AbstractButton::label

    This property holds the label item.

    \sa text
*/
QQuickItem *QQuickAbstractButton::label() const
{
    Q_D(const QQuickAbstractButton);
    return d->label;
}

void QQuickAbstractButton::setLabel(QQuickItem *label)
{
    Q_D(QQuickAbstractButton);
    if (d->label != label) {
        delete d->label;
        d->label = label;
        if (label && !label->parentItem())
            label->setParentItem(this);
        emit labelChanged();
    }
}

/*!
    \qmlmethod void Qt.labs.controls::Button::toggle()

    Toggles the checked state of the button.
*/
void QQuickAbstractButton::toggle()
{
    Q_D(QQuickAbstractButton);
    setChecked(!d->checked);
}

void QQuickAbstractButton::focusOutEvent(QFocusEvent *event)
{
    Q_D(QQuickAbstractButton);
    QQuickControl::focusOutEvent(event);
    if (d->pressed) {
        setPressed(false);
        emit canceled();
    }
}

void QQuickAbstractButton::keyPressEvent(QKeyEvent *event)
{
    QQuickControl::keyPressEvent(event);
    if (event->key() == Qt::Key_Space) {
        setPressed(true);

        QQuickMouseEvent me(width() / 2, height() / 2, Qt::NoButton, Qt::NoButton, event->modifiers());
        emit pressed(&me);
        event->setAccepted(me.isAccepted());
    }
}

void QQuickAbstractButton::keyReleaseEvent(QKeyEvent *event)
{
    Q_D(QQuickAbstractButton);
    QQuickControl::keyReleaseEvent(event);
    if (event->key() == Qt::Key_Space) {
        setPressed(false);

        QQuickMouseEvent mre(width() / 2, height() / 2, Qt::NoButton, Qt::NoButton, event->modifiers());
        emit released(&mre);
        QQuickMouseEvent mce(width() / 2, height() / 2, Qt::NoButton, Qt::NoButton, event->modifiers(), true /* isClick */);
        emit clicked(&mce);
        if (d->checkable)
            setChecked(d->exclusive || !d->checked);
        event->setAccepted(mre.isAccepted() || mce.isAccepted());
    }
}

void QQuickAbstractButton::mousePressEvent(QMouseEvent *event)
{
    QQuickControl::mousePressEvent(event);
    setPressed(true);

    QQuickMouseEvent me(event->x(), event->y(), event->button(), event->buttons(), event->modifiers());
    emit pressed(&me);
    event->setAccepted(me.isAccepted());
}

void QQuickAbstractButton::mouseMoveEvent(QMouseEvent *event)
{
    QQuickControl::mouseMoveEvent(event);
    setPressed(contains(event->pos()));
}

void QQuickAbstractButton::mouseReleaseEvent(QMouseEvent *event)
{
    Q_D(QQuickAbstractButton);
    QQuickControl::mouseReleaseEvent(event);
    bool wasPressed = d->pressed;
    setPressed(false);

    if (wasPressed) {
        QQuickMouseEvent mre(event->x(), event->y(), event->button(), event->buttons(), event->modifiers());
        emit released(&mre);
        QQuickMouseEvent mce(event->x(), event->y(), event->button(), event->buttons(), event->modifiers(), true /* isClick */);
        emit clicked(&mce);
        event->setAccepted(mre.isAccepted() || mce.isAccepted());
    } else {
        emit canceled();
    }
    if (d->checkable && contains(event->pos()))
        setChecked(d->exclusive || !d->checked);
}

void QQuickAbstractButton::mouseDoubleClickEvent(QMouseEvent *event)
{
    QQuickControl::mouseDoubleClickEvent(event);

    QQuickMouseEvent me(event->x(), event->y(), event->button(), event->buttons(), event->modifiers(), true /* isClick */);
    emit doubleClicked(&me);
    event->setAccepted(me.isAccepted());
}

void QQuickAbstractButton::mouseUngrabEvent()
{
    Q_D(QQuickAbstractButton);
    QQuickControl::mouseUngrabEvent();
    if (d->pressed) {
        setPressed(false);
        emit canceled();
    }
}

QT_END_NAMESPACE