summaryrefslogtreecommitdiffstats
path: root/src/widgets/widgets/qabstractbutton.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/widgets/widgets/qabstractbutton.cpp')
-rw-r--r--src/widgets/widgets/qabstractbutton.cpp171
1 files changed, 68 insertions, 103 deletions
diff --git a/src/widgets/widgets/qabstractbutton.cpp b/src/widgets/widgets/qabstractbutton.cpp
index f30a3bc7b8..3a5fbe1b90 100644
--- a/src/widgets/widgets/qabstractbutton.cpp
+++ b/src/widgets/widgets/qabstractbutton.cpp
@@ -1,41 +1,5 @@
-/****************************************************************************
-**
-** Copyright (C) 2016 The Qt Company Ltd.
-** Contact: https://www.qt.io/licensing/
-**
-** This file is part of the QtWidgets module of the Qt Toolkit.
-**
-** $QT_BEGIN_LICENSE:LGPL$
-** 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 https://www.qt.io/terms-conditions. For further
-** information use the contact form at https://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.LGPL3 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-3.0.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 (at your option) the GNU General
-** Public license version 3 or any later version approved by the KDE Free
-** Qt Foundation. The licenses are as published by the Free Software
-** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
-** included in the packaging of this file. Please review the following
-** information to ensure the GNU General Public License requirements will
-** be met: https://www.gnu.org/licenses/gpl-2.0.html and
-** https://www.gnu.org/licenses/gpl-3.0.html.
-**
-** $QT_END_LICENSE$
-**
-****************************************************************************/
+// Copyright (C) 2020 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#include "private/qabstractbutton_p.h"
@@ -46,15 +10,18 @@
#include "qbuttongroup.h"
#include "private/qbuttongroup_p.h"
#endif
+#include "private/qapplication_p.h"
#include "qabstractbutton_p.h"
#include "qevent.h"
#include "qpainter.h"
#include "qapplication.h"
#include "qstyle.h"
-#include "qaction.h"
-#ifndef QT_NO_ACCESSIBILITY
+#if QT_CONFIG(accessibility)
#include "qaccessible.h"
#endif
+#include <qpa/qplatformtheme.h>
+
+#include <QtCore/qpointer.h>
#include <algorithm>
@@ -176,7 +143,7 @@ QAbstractButtonPrivate::QAbstractButtonPrivate(QSizePolicy::ControlType type)
checkable(false), checked(false), autoRepeat(false), autoExclusive(false),
down(false), blockRefresh(false), pressed(false),
#if QT_CONFIG(buttongroup)
- group(0),
+ group(nullptr),
#endif
autoRepeatDelay(AUTO_REPEAT_DELAY),
autoRepeatInterval(AUTO_REPEAT_INTERVAL),
@@ -190,7 +157,9 @@ QList<QAbstractButton *>QAbstractButtonPrivate::queryButtonList() const
return group->d_func()->buttonList;
#endif
- QList<QAbstractButton*>candidates = parent->findChildren<QAbstractButton *>();
+ if (!parent)
+ return {};
+ QList<QAbstractButton *> candidates = parent->findChildren<QAbstractButton *>();
if (autoExclusive) {
auto isNoMemberOfMyAutoExclusiveGroup = [](QAbstractButton *candidate) {
return !candidate->autoExclusive()
@@ -199,9 +168,7 @@ QList<QAbstractButton *>QAbstractButtonPrivate::queryButtonList() const
#endif
;
};
- candidates.erase(std::remove_if(candidates.begin(), candidates.end(),
- isNoMemberOfMyAutoExclusiveGroup),
- candidates.end());
+ candidates.removeIf(isNoMemberOfMyAutoExclusiveGroup);
}
return candidates;
}
@@ -215,15 +182,15 @@ QAbstractButton *QAbstractButtonPrivate::queryCheckedButton() const
Q_Q(const QAbstractButton);
QList<QAbstractButton *> buttonList = queryButtonList();
- if (!autoExclusive || buttonList.count() == 1) // no group
- return 0;
+ if (!autoExclusive || buttonList.size() == 1) // no group
+ return nullptr;
- for (int i = 0; i < buttonList.count(); ++i) {
+ for (int i = 0; i < buttonList.size(); ++i) {
QAbstractButton *b = buttonList.at(i);
if (b->d_func()->checked && b != q)
return b;
}
- return checked ? const_cast<QAbstractButton *>(q) : 0;
+ return checked ? const_cast<QAbstractButton *>(q) : nullptr;
}
void QAbstractButtonPrivate::notifyChecked()
@@ -245,7 +212,7 @@ void QAbstractButtonPrivate::notifyChecked()
void QAbstractButtonPrivate::moveFocus(int key)
{
- QList<QAbstractButton *> buttonList = queryButtonList();;
+ QList<QAbstractButton *> buttonList = queryButtonList();
#if QT_CONFIG(buttongroup)
bool exclusive = group ? group->d_func()->exclusive : autoExclusive;
#else
@@ -256,16 +223,16 @@ void QAbstractButtonPrivate::moveFocus(int key)
if (!fb || !buttonList.contains(fb))
return;
- QAbstractButton *candidate = 0;
+ QAbstractButton *candidate = nullptr;
int bestScore = -1;
QRect target = f->rect().translated(f->mapToGlobal(QPoint(0,0)));
QPoint goal = target.center();
uint focus_flag = qt_tab_all_widgets() ? Qt::TabFocus : Qt::StrongFocus;
- for (int i = 0; i < buttonList.count(); ++i) {
+ for (int i = 0; i < buttonList.size(); ++i) {
QAbstractButton *button = buttonList.at(i);
if (button != f && button->window() == f->window() && button->isEnabled() && !button->isHidden() &&
- (autoExclusive || (button->focusPolicy() & focus_flag) == focus_flag)) {
+ (exclusive || (button->focusPolicy() & focus_flag) == focus_flag)) {
QRect buttonRect = button->rect().translated(button->mapToGlobal(QPoint(0,0)));
QPoint p = buttonRect.center();
@@ -319,7 +286,7 @@ void QAbstractButtonPrivate::moveFocus(int key)
if (exclusive
#ifdef QT_KEYPAD_NAVIGATION
- && !QApplication::keypadNavigationEnabled()
+ && !QApplicationPrivate::keypadNavigationEnabled()
#endif
&& candidate
&& fb->d_func()->checked
@@ -345,7 +312,7 @@ void QAbstractButtonPrivate::fixFocusPolicy()
return;
QList<QAbstractButton *> buttonList = queryButtonList();
- for (int i = 0; i < buttonList.count(); ++i) {
+ for (int i = 0; i < buttonList.size(); ++i) {
QAbstractButton *b = buttonList.at(i);
if (!b->isCheckable())
continue;
@@ -414,7 +381,7 @@ void QAbstractButtonPrivate::emitClicked()
emit q->clicked(checked);
#if QT_CONFIG(buttongroup)
if (guard && group) {
- emit group->buttonClicked(group->id(q));
+ emit group->idClicked(group->id(q));
if (guard && group)
emit group->buttonClicked(q);
}
@@ -428,7 +395,7 @@ void QAbstractButtonPrivate::emitPressed()
emit q->pressed();
#if QT_CONFIG(buttongroup)
if (guard && group) {
- emit group->buttonPressed(group->id(q));
+ emit group->idPressed(group->id(q));
if (guard && group)
emit group->buttonPressed(q);
}
@@ -442,7 +409,7 @@ void QAbstractButtonPrivate::emitReleased()
emit q->released();
#if QT_CONFIG(buttongroup)
if (guard && group) {
- emit group->buttonReleased(group->id(q));
+ emit group->idReleased(group->id(q));
if (guard && group)
emit group->buttonReleased(q);
}
@@ -456,7 +423,7 @@ void QAbstractButtonPrivate::emitToggled(bool checked)
emit q->toggled(checked);
#if QT_CONFIG(buttongroup)
if (guard && group) {
- emit group->buttonToggled(group->id(q), checked);
+ emit group->idToggled(group->id(q), checked);
if (guard && group)
emit group->buttonToggled(q, checked);
}
@@ -467,7 +434,7 @@ void QAbstractButtonPrivate::emitToggled(bool checked)
Constructs an abstract button with a \a parent.
*/
QAbstractButton::QAbstractButton(QWidget *parent)
- : QWidget(*new QAbstractButtonPrivate, parent, 0)
+ : QWidget(*new QAbstractButtonPrivate, parent, { })
{
Q_D(QAbstractButton);
d->init();
@@ -489,7 +456,7 @@ QAbstractButton::QAbstractButton(QWidget *parent)
/*! \internal
*/
QAbstractButton::QAbstractButton(QAbstractButtonPrivate &dd, QWidget *parent)
- : QWidget(dd, parent, 0)
+ : QWidget(dd, parent, { })
{
Q_D(QAbstractButton);
d->init();
@@ -525,7 +492,7 @@ void QAbstractButton::setText(const QString &text)
d->sizeHint = QSize();
update();
updateGeometry();
-#ifndef QT_NO_ACCESSIBILITY
+#if QT_CONFIG(accessibility)
QAccessibleEvent event(this, QAccessible::NameChanged);
QAccessible::updateAccessibility(&event);
#endif
@@ -648,12 +615,13 @@ void QAbstractButton::setChecked(bool checked)
if (guard)
d->emitToggled(checked);
-
-#ifndef QT_NO_ACCESSIBILITY
- QAccessible::State s;
- s.checked = true;
- QAccessibleStateChangeEvent event(this, s);
- QAccessible::updateAccessibility(&event);
+#if QT_CONFIG(accessibility)
+ if (guard) {
+ QAccessible::State s;
+ s.checked = true;
+ QAccessibleStateChangeEvent event(this, s);
+ QAccessible::updateAccessibility(&event);
+ }
#endif
}
@@ -818,7 +786,7 @@ QButtonGroup *QAbstractButton::group() const
/*!
Performs an animated click: the button is pressed immediately, and
-released \a msec milliseconds later (the default is 100 ms).
+released 100ms later.
Calling this function again before the button is released resets
the release timer.
@@ -829,7 +797,7 @@ This function does nothing if the button is \l{setEnabled()}{disabled.}
\sa click()
*/
-void QAbstractButton::animateClick(int msec)
+void QAbstractButton::animateClick()
{
if (!isEnabled())
return;
@@ -840,7 +808,7 @@ void QAbstractButton::animateClick(int msec)
repaint();
if (!d->animateTimer.isActive())
d->emitPressed();
- d->animateTimer.start(msec, this);
+ d->animateTimer.start(100, this);
}
/*!
@@ -939,9 +907,6 @@ bool QAbstractButton::event(QEvent *e)
case QEvent::HoverEnter:
case QEvent::HoverLeave:
case QEvent::ContextMenu:
-#if QT_CONFIG(wheelevent)
- case QEvent::Wheel:
-#endif
return true;
default:
break;
@@ -976,7 +941,7 @@ void QAbstractButton::mousePressEvent(QMouseEvent *e)
e->ignore();
return;
}
- if (hitButton(e->pos())) {
+ if (hitButton(e->position().toPoint())) {
setDown(true);
d->pressed = true;
repaint();
@@ -1006,7 +971,7 @@ void QAbstractButton::mouseReleaseEvent(QMouseEvent *e)
return;
}
- if (hitButton(e->pos())) {
+ if (hitButton(e->position().toPoint())) {
d->repeatTimer.stop();
d->click();
e->accept();
@@ -1025,7 +990,7 @@ void QAbstractButton::mouseMoveEvent(QMouseEvent *e)
return;
}
- if (hitButton(e->pos()) != d->down) {
+ if (hitButton(e->position().toPoint()) != d->down) {
setDown(!d->down);
repaint();
if (d->down)
@@ -1033,7 +998,7 @@ void QAbstractButton::mouseMoveEvent(QMouseEvent *e)
else
d->emitReleased();
e->accept();
- } else if (!hitButton(e->pos())) {
+ } else if (!hitButton(e->position().toPoint())) {
e->ignore();
}
}
@@ -1043,19 +1008,19 @@ void QAbstractButton::keyPressEvent(QKeyEvent *e)
{
Q_D(QAbstractButton);
bool next = true;
- switch (e->key()) {
- case Qt::Key_Enter:
- case Qt::Key_Return:
- e->ignore();
- break;
- case Qt::Key_Select:
- case Qt::Key_Space:
- if (!e->isAutoRepeat()) {
- setDown(true);
- repaint();
- d->emitPressed();
- }
- break;
+
+ const auto key = e->key();
+ const auto buttonPressKeys = QGuiApplicationPrivate::platformTheme()
+ ->themeHint(QPlatformTheme::ButtonPressKeys)
+ .value<QList<Qt::Key>>();
+ if (buttonPressKeys.contains(key) && !e->isAutoRepeat()) {
+ setDown(true);
+ repaint();
+ d->emitPressed();
+ return;
+ }
+
+ switch (key) {
case Qt::Key_Up:
next = false;
Q_FALLTHROUGH();
@@ -1063,7 +1028,7 @@ void QAbstractButton::keyPressEvent(QKeyEvent *e)
case Qt::Key_Right:
case Qt::Key_Down: {
#ifdef QT_KEYPAD_NAVIGATION
- if ((QApplication::keypadNavigationEnabled()
+ if ((QApplicationPrivate::keypadNavigationEnabled()
&& (e->key() == Qt::Key_Left || e->key() == Qt::Key_Right))
|| (!QApplication::navigationMode() == Qt::NavigationModeKeypadDirectional
|| (e->key() == Qt::Key_Up || e->key() == Qt::Key_Down))) {
@@ -1085,7 +1050,7 @@ void QAbstractButton::keyPressEvent(QKeyEvent *e)
// cleaned up when fixing task 194373. It's here to ensure that we
// keep compatibility outside QAbstractItemView.
d->moveFocus(e->key());
- if (hasFocus()) // nothing happend, propagate
+ if (hasFocus()) // nothing happened, propagate
e->ignore();
} else {
// Prefer parent widget, use this if parent is absent
@@ -1120,15 +1085,15 @@ void QAbstractButton::keyReleaseEvent(QKeyEvent *e)
if (!e->isAutoRepeat())
d->repeatTimer.stop();
- switch (e->key()) {
- case Qt::Key_Select:
- case Qt::Key_Space:
- if (!e->isAutoRepeat() && d->down)
- d->click();
- break;
- default:
- e->ignore();
+ const auto buttonPressKeys = QGuiApplicationPrivate::platformTheme()
+ ->themeHint(QPlatformTheme::ButtonPressKeys)
+ .value<QList<Qt::Key>>();
+ if (buttonPressKeys.contains(e->key()) && !e->isAutoRepeat() && d->down) {
+ d->click();
+ return;
}
+
+ e->ignore();
}
/*!\reimp
@@ -1159,7 +1124,7 @@ void QAbstractButton::focusInEvent(QFocusEvent *e)
{
Q_D(QAbstractButton);
#ifdef QT_KEYPAD_NAVIGATION
- if (!QApplication::keypadNavigationEnabled())
+ if (!QApplicationPrivate::keypadNavigationEnabled())
#endif
d->fixFocusPolicy();
QWidget::focusInEvent(e);
@@ -1271,7 +1236,7 @@ QSize QAbstractButton::iconSize() const
Q_D(const QAbstractButton);
if (d->iconSize.isValid())
return d->iconSize;
- int e = style()->pixelMetric(QStyle::PM_ButtonIconSize, 0, this);
+ int e = style()->pixelMetric(QStyle::PM_ButtonIconSize, nullptr, this);
return QSize(e, e);
}