From 9ea53c4a98281e4be12164929a7ca5dc7c014280 Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Mon, 1 Jul 2019 14:35:27 +0200 Subject: 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 --- src/widgets/kernel/qshortcut.cpp | 68 ++++++++++++++++++---------------------- 1 file changed, 31 insertions(+), 37 deletions(-) (limited to 'src') diff --git a/src/widgets/kernel/qshortcut.cpp b/src/widgets/kernel/qshortcut.cpp index c418db14ff..a4ebcdfc84 100644 --- a/src/widgets/kernel/qshortcut.cpp +++ b/src/widgets/kernel/qshortcut.cpp @@ -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(qwindow); - if (widgetWindow) { + if (auto widgetWindow = qobject_cast(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(object)) + if (auto a = qobject_cast(object)) return correctActionContext(context, a, active_window); #endif #if QT_CONFIG(graphicsview) - if (QGraphicsWidget *gw = qobject_cast(object)) + if (auto gw = qobject_cast(object)) return correctGraphicsWidgetContext(context, gw, active_window); #endif - QWidget *w = qobject_cast(object); + auto w = qobject_cast(object); if (!w) { - QShortcut *s = qobject_cast(object); - if (s) + if (auto s = qobject_cast(object)) w = s->parentWidget(); } if (!w) { - QWindow *qwindow = qobject_cast(object); + auto qwindow = qobject_cast(object); while (qwindow) { - QWidgetWindow *widget_window = qobject_cast(qwindow); - if (widget_window) { + if (auto widget_window = qobject_cast(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(w)) { + if (auto menuBar = qobject_cast(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(QObjectPrivate::get(tlw))->extra) { + if (auto topData = static_cast(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 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(ti); + const auto *tw = static_cast(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 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 &widgets = static_cast(QObjectPrivate::get(a))->widgets; + const QWidgetList &widgets = static_cast(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(w)) { + if (auto menu = qobject_cast(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 &graphicsWidgets = static_cast(QObjectPrivate::get(a))->graphicsWidgets; + const auto &graphicsWidgets = static_cast(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(e); + auto se = static_cast(e); if (se->shortcutId() == d->sc_id && se->key() == d->sc_sequence){ #if QT_CONFIG(whatsthis) if (QWhatsThis::inWhatsThisMode()) { -- cgit v1.2.3