QShortcut: Brush up the code, preparing the extraction of a base class to QtGui

- Use member initialization
- Introduce nullptr
- Use auto where applicable
- Use range-based for

Task-number: QTBUG-76493
Change-Id: Ic4dbee2d76a65be1f8a4c25f4ca7e4f032443579
Reviewed-by: Frederik Gladhorn <frederik.gladhorn@qt.io>
This commit is contained in:
Friedemann Kleint 2019-07-01 14:35:27 +02:00
parent 3d87ea91af
commit 9ea53c4a98

View File

@ -96,8 +96,7 @@ bool qWidgetShortcutContextMatcher(QObject *object, Qt::ShortcutContext context)
QWindow *qwindow = QGuiApplication::focusWindow();
if (qwindow && qwindow->isActive()) {
while (qwindow) {
QWidgetWindow *widgetWindow = qobject_cast<QWidgetWindow *>(qwindow);
if (widgetWindow) {
if (auto widgetWindow = qobject_cast<QWidgetWindow *>(qwindow)) {
active_window = widgetWindow->widget();
break;
}
@ -110,27 +109,25 @@ bool qWidgetShortcutContextMatcher(QObject *object, Qt::ShortcutContext context)
return false;
#ifndef QT_NO_ACTION
if (QAction *a = qobject_cast<QAction *>(object))
if (auto a = qobject_cast<QAction *>(object))
return correctActionContext(context, a, active_window);
#endif
#if QT_CONFIG(graphicsview)
if (QGraphicsWidget *gw = qobject_cast<QGraphicsWidget *>(object))
if (auto gw = qobject_cast<QGraphicsWidget *>(object))
return correctGraphicsWidgetContext(context, gw, active_window);
#endif
QWidget *w = qobject_cast<QWidget *>(object);
auto w = qobject_cast<QWidget *>(object);
if (!w) {
QShortcut *s = qobject_cast<QShortcut *>(object);
if (s)
if (auto s = qobject_cast<QShortcut *>(object))
w = s->parentWidget();
}
if (!w) {
QWindow *qwindow = qobject_cast<QWindow *>(object);
auto qwindow = qobject_cast<QWindow *>(object);
while (qwindow) {
QWidgetWindow *widget_window = qobject_cast<QWidgetWindow *>(qwindow);
if (widget_window) {
if (auto widget_window = qobject_cast<QWidgetWindow *>(qwindow)) {
w = widget_window->widget();
break;
}
@ -148,7 +145,7 @@ static bool correctWidgetContext(Qt::ShortcutContext context, QWidget *w, QWidge
{
bool visible = w->isVisible();
#if QT_CONFIG(menubar)
if (QMenuBar *menuBar = qobject_cast<QMenuBar *>(w)) {
if (auto menuBar = qobject_cast<QMenuBar *>(w)) {
if (auto *pmb = menuBar->platformMenuBar()) {
if (menuBar->parentWidget()) {
visible = true;
@ -166,7 +163,7 @@ static bool correctWidgetContext(Qt::ShortcutContext context, QWidget *w, QWidge
return false;
if (context == Qt::ApplicationShortcut)
return QApplicationPrivate::tryModalHelper(w, 0); // true, unless w is shadowed by a modal dialog
return QApplicationPrivate::tryModalHelper(w, nullptr); // true, unless w is shadowed by a modal dialog
if (context == Qt::WidgetShortcut)
return w == QApplication::focusWidget();
@ -181,9 +178,9 @@ static bool correctWidgetContext(Qt::ShortcutContext context, QWidget *w, QWidge
// Below is Qt::WindowShortcut context
QWidget *tlw = w->window();
#if QT_CONFIG(graphicsview)
if (QWExtra *topData = static_cast<QWidgetPrivate *>(QObjectPrivate::get(tlw))->extra) {
if (auto topData = static_cast<QWidgetPrivate *>(QObjectPrivate::get(tlw))->extra) {
if (topData->proxyWidget) {
bool res = correctGraphicsWidgetContext(context, (QGraphicsWidget *)topData->proxyWidget, active_window);
bool res = correctGraphicsWidgetContext(context, topData->proxyWidget, active_window);
return res;
}
}
@ -244,9 +241,9 @@ static bool correctGraphicsWidgetContext(Qt::ShortcutContext context, QGraphicsW
// Applicationwide shortcuts are always reachable unless their owner
// is shadowed by modality. In QGV there's no modality concept, but we
// must still check if all views are shadowed.
QList<QGraphicsView *> views = w->scene()->views();
for (int i = 0; i < views.size(); ++i) {
if (QApplicationPrivate::tryModalHelper(views.at(i), 0))
const auto &views = w->scene()->views();
for (auto view : views) {
if (QApplicationPrivate::tryModalHelper(view, nullptr))
return true;
}
return false;
@ -258,7 +255,7 @@ static bool correctGraphicsWidgetContext(Qt::ShortcutContext context, QGraphicsW
if (context == Qt::WidgetWithChildrenShortcut) {
const QGraphicsItem *ti = w->scene()->focusItem();
if (ti && ti->isWidget()) {
const QGraphicsWidget *tw = static_cast<const QGraphicsWidget *>(ti);
const auto *tw = static_cast<const QGraphicsWidget *>(ti);
while (tw && tw != w && (tw->windowType() == Qt::Widget || tw->windowType() == Qt::Popup))
tw = tw->parentWidget();
return tw == w;
@ -269,10 +266,9 @@ static bool correctGraphicsWidgetContext(Qt::ShortcutContext context, QGraphicsW
// Below is Qt::WindowShortcut context
// Find the active view (if any).
QList<QGraphicsView *> views = w->scene()->views();
QGraphicsView *activeView = 0;
for (int i = 0; i < views.size(); ++i) {
QGraphicsView *view = views.at(i);
const auto &views = w->scene()->views();
QGraphicsView *activeView = nullptr;
for (auto view : views) {
if (view->window() == active_window) {
activeView = view;
break;
@ -291,15 +287,14 @@ static bool correctGraphicsWidgetContext(Qt::ShortcutContext context, QGraphicsW
#ifndef QT_NO_ACTION
static bool correctActionContext(Qt::ShortcutContext context, QAction *a, QWidget *active_window)
{
const QList<QWidget *> &widgets = static_cast<QActionPrivate *>(QObjectPrivate::get(a))->widgets;
const QWidgetList &widgets = static_cast<QActionPrivate *>(QObjectPrivate::get(a))->widgets;
#if defined(DEBUG_QSHORTCUTMAP)
if (widgets.isEmpty())
qDebug() << a << "not connected to any widgets; won't trigger";
#endif
for (int i = 0; i < widgets.size(); ++i) {
QWidget *w = widgets.at(i);
for (auto w : widgets) {
#if QT_CONFIG(menu)
if (QMenu *menu = qobject_cast<QMenu *>(w)) {
if (auto menu = qobject_cast<QMenu *>(w)) {
#ifdef Q_OS_DARWIN
// On Mac, menu item shortcuts are processed before reaching any window.
// That means that if a menu action shortcut has not been already processed
@ -325,14 +320,13 @@ static bool correctActionContext(Qt::ShortcutContext context, QAction *a, QWidge
}
#if QT_CONFIG(graphicsview)
const QList<QGraphicsWidget *> &graphicsWidgets = static_cast<QActionPrivate *>(QObjectPrivate::get(a))->graphicsWidgets;
const auto &graphicsWidgets = static_cast<QActionPrivate *>(QObjectPrivate::get(a))->graphicsWidgets;
#if defined(DEBUG_QSHORTCUTMAP)
if (graphicsWidgets.isEmpty())
qDebug() << a << "not connected to any widgets; won't trigger";
#endif
for (int i = 0; i < graphicsWidgets.size(); ++i) {
QGraphicsWidget *w = graphicsWidgets.at(i);
if (correctGraphicsWidgetContext(context, w, active_window))
for (auto graphicsWidget : graphicsWidgets) {
if (correctGraphicsWidgetContext(context, graphicsWidget, active_window))
return true;
}
#endif
@ -433,12 +427,12 @@ class QShortcutPrivate : public QObjectPrivate
{
Q_DECLARE_PUBLIC(QShortcut)
public:
QShortcutPrivate() : sc_context(Qt::WindowShortcut), sc_enabled(true), sc_autorepeat(true), sc_id(0) {}
QShortcutPrivate() = default;
QKeySequence sc_sequence;
Qt::ShortcutContext sc_context;
bool sc_enabled;
bool sc_autorepeat;
int sc_id;
Qt::ShortcutContext sc_context = Qt::WindowShortcut;
bool sc_enabled = true;
bool sc_autorepeat = true;
int sc_id = 0;
QString sc_whatsthis;
void redoGrab(QShortcutMap &map);
};
@ -472,7 +466,7 @@ void QShortcutPrivate::redoGrab(QShortcutMap &map)
QShortcut::QShortcut(QWidget *parent)
: QObject(*new QShortcutPrivate, parent)
{
Q_ASSERT(parent != 0);
Q_ASSERT(parent != nullptr);
}
/*!
@ -667,7 +661,7 @@ bool QShortcut::event(QEvent *e)
Q_D(QShortcut);
bool handled = false;
if (d->sc_enabled && e->type() == QEvent::Shortcut) {
QShortcutEvent *se = static_cast<QShortcutEvent *>(e);
auto se = static_cast<QShortcutEvent *>(e);
if (se->shortcutId() == d->sc_id && se->key() == d->sc_sequence){
#if QT_CONFIG(whatsthis)
if (QWhatsThis::inWhatsThisMode()) {