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