File: actionvalidator.cpp

package info (click to toggle)
gammaray 3.1.0%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 24,796 kB
  • sloc: cpp: 109,174; ansic: 2,156; sh: 336; python: 274; yacc: 90; lex: 82; xml: 61; makefile: 28; javascript: 9; ruby: 5
file content (188 lines) | stat: -rw-r--r-- 5,544 bytes parent folder | download
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
/*
  actionvalidator.cpp

  This file is part of GammaRay, the Qt application inspection and manipulation tool.

  SPDX-FileCopyrightText: 2012 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
  Author: Kevin Funk <kevin.funk@kdab.com>

  SPDX-License-Identifier: GPL-2.0-or-later

  Contact KDAB at <info@kdab.com> for commercial licensing options.
*/

#include "actionvalidator.h"

#include <QAction>
#include <QMutexLocker>
#include <QWidget>

#include <core/objectdataprovider.h>
#include <core/probe.h>
#include <core/problemcollector.h>
#include <common/problem.h>

using namespace GammaRay;

QT_BEGIN_NAMESPACE
uint qHash(const QKeySequence &sequence)
{
    return qHash(sequence.toString(QKeySequence::PortableText));
}

QT_END_NAMESPACE

ActionValidator::ActionValidator(QObject *parent)
    : QObject(parent)
{
}

QList<QAction *> ActionValidator::actions() const
{
    return m_shortcutActionMap.values();
}

QList<QAction *> ActionValidator::actions(const QKeySequence &sequence) const
{
    return m_shortcutActionMap.values(sequence);
}

void ActionValidator::setActions(const QList<QAction *> &actions)
{
    clearActions();

    m_shortcutActionMap.reserve(actions.size());
    for (QAction *action : actions) {
        insert(action);
    }
}

void ActionValidator::clearActions()
{
    m_shortcutActionMap.clear();
}

void ActionValidator::insert(QAction *action)
{
    Q_ASSERT(action);

    Q_FOREACH (const QKeySequence &sequence, action->shortcuts()) {
        if (m_shortcutActionMap.values(sequence).contains(action))
            continue;

        m_shortcutActionMap.insert(sequence, action);
    }

    // also track object destruction
    connect(action, &QObject::destroyed,
            this, &ActionValidator::handleActionDestroyed);
}



void ActionValidator::remove(QAction *action)
{
    Q_ASSERT(action);

    safeRemove(action);
}

void ActionValidator::safeRemove(QAction *action)
{
    for (auto it = m_shortcutActionMap.cbegin(); it != m_shortcutActionMap.cend(); ++it) {
        const QKeySequence &sequence = it.key();
        if (!m_shortcutActionMap.values(sequence).contains(action))
            continue;

        QList<QAction *> oldValues = m_shortcutActionMap.values(sequence);
        const bool success = oldValues.removeOne(action);
        Q_UNUSED(success);
        Q_ASSERT(success);
        m_shortcutActionMap.replace(sequence, action);
    }
}

void ActionValidator::handleActionDestroyed(QObject *object)
{
    QAction *action = static_cast<QAction *>(object);

    safeRemove(action);
}

bool ActionValidator::hasAmbiguousShortcut(const QAction *action) const
{
    QList<QKeySequence> shortcuts = action->shortcuts();
    return std::any_of(shortcuts.constBegin(), shortcuts.constEnd(),
                       [action, this](const QKeySequence &seq) { return isAmbigous(action, seq); });
}

QVector<QKeySequence> GammaRay::ActionValidator::findAmbiguousShortcuts(const QAction *action) const
{
    QVector<QKeySequence> shortcuts;


    if (!action)
        return shortcuts;

    Q_FOREACH (const QKeySequence &sequence, action->shortcuts()) {
        if (isAmbigous(action, sequence)) {
            shortcuts.push_back(sequence);
        }
    }
    return shortcuts;
}

bool GammaRay::ActionValidator::isAmbigous(const QAction *action, const QKeySequence &sequence) const
{
    Q_ASSERT(action);
    QMutexLocker lock(Probe::objectLock());
    if (!Probe::instance()->isValidObject(action)) {
        return false;
    }

    Q_FOREACH (const QAction *other, m_shortcutActionMap.values(sequence)) {
        if (!other || other == action || !Probe::instance()->isValidObject(other)) {
            continue;
        }
        if (action->shortcutContext() == Qt::ApplicationShortcut
            || other->shortcutContext() == Qt::ApplicationShortcut)
            return true;
        if (action->shortcutContext() == Qt::WindowShortcut || other->shortcutContext() == Qt::WindowShortcut) {
            Q_FOREACH (QWidget *w1, action->associatedWidgets()) {
                Q_FOREACH (QWidget *w2, other->associatedWidgets()) {
                    if (w1->window() == w2->window())
                        return true;
                }
            }
        }
        if (action->shortcutContext() == Qt::WidgetWithChildrenShortcut) {
            Q_FOREACH (QWidget *w1, action->associatedWidgets()) {
                Q_FOREACH (QWidget *w2, other->associatedWidgets()) {
                    for (QWidget *ancestor = w2; ancestor; ancestor = ancestor->parentWidget()) {
                        if (w1 == ancestor)
                            return true;
                    }
                }
            }
        }
        if (other->shortcutContext() == Qt::WidgetWithChildrenShortcut) {
            Q_FOREACH (QWidget *w1, other->associatedWidgets()) {
                Q_FOREACH (QWidget *w2, action->associatedWidgets()) {
                    for (QWidget *ancestor = w2; ancestor; ancestor = ancestor->parentWidget()) {
                        if (w1 == ancestor)
                            return true;
                    }
                }
            }
        }
        if (action->shortcutContext() == Qt::WidgetShortcut && other->shortcutContext() == Qt::WidgetShortcut) {
            Q_FOREACH (QWidget *w1, action->associatedWidgets()) {
                Q_FOREACH (QWidget *w2, other->associatedWidgets()) {
                    if (w1 == w2)
                        return true;
                }
            }
        }
    }
    return false;
}