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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
|
/*
actionmodel.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 "actionmodel.h"
#include "actionvalidator.h"
#include <core/enumutil.h>
#include <core/probe.h>
#include <core/util.h>
#include <core/varianthandler.h>
#include <core/problemcollector.h>
#include <core/objectdataprovider.h>
#include <common/objectid.h>
#include <QAction>
#include <QDebug>
#include <QMutex>
#include <QThread>
Q_DECLARE_METATYPE(QAction::Priority)
using namespace GammaRay;
static QString toString(const QList<QKeySequence> &list)
{
QStringList items;
items.reserve(list.size());
for (const auto &item : list) {
items << item.toString();
}
return items.join(QStringLiteral(", "));
}
ActionModel::ActionModel(QObject *parent)
: QAbstractTableModel(parent)
, m_duplicateFinder(new ActionValidator(this))
{
ProblemCollector::registerProblemChecker("gammaray_actioninspector.ShortcutDuplicates",
"Shortcut Duplicates",
"Scans for potential shortcut conflicts in QActions",
[this]() { scanForShortcutDuplicates(); });
}
ActionModel::~ActionModel() = default;
void ActionModel::objectAdded(QObject *object)
{
// see Probe::objectCreated, that promises a valid object in the main thread
Q_ASSERT(QThread::currentThread() == thread());
Q_ASSERT(object);
QAction *const action = qobject_cast<QAction *>(object);
if (!action)
return;
auto it = std::lower_bound(m_actions.begin(), m_actions.end(), action);
Q_ASSERT(it == m_actions.end() || *it != action);
const int row = std::distance(m_actions.begin(), it);
Q_ASSERT(row >= 0 && row <= m_actions.size());
beginInsertRows(QModelIndex(), row, row);
m_actions.insert(it, action);
Q_ASSERT(m_actions.at(row) == action);
m_duplicateFinder->insert(action);
connect(action, &QAction::changed, this, &ActionModel::actionChanged);
endInsertRows();
}
void ActionModel::objectRemoved(QObject *object)
{
Q_ASSERT(thread() == QThread::currentThread());
QAction *const action = reinterpret_cast<QAction *>(object); // never dereference this, just use for comparison
auto it = std::lower_bound(m_actions.begin(),
m_actions.end(),
reinterpret_cast<QAction *>(object));
if (it == m_actions.end() || *it != action)
return;
const int row = std::distance(m_actions.begin(), it);
Q_ASSERT(row >= 0 && row < m_actions.size());
Q_ASSERT(m_actions.at(row) == action);
beginRemoveRows(QModelIndex(), row, row);
m_actions.erase(it);
m_duplicateFinder->remove(action);
endRemoveRows();
}
int ActionModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return ColumnCount;
}
int ActionModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
return m_actions.size();
}
QVariant ActionModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
QMutexLocker lock(Probe::objectLock());
QAction *action = m_actions.at(index.row());
if (!Probe::instance()->isValidObject(action))
return QVariant();
const int column = index.column();
if (role == Qt::DisplayRole) {
switch (column) {
case AddressColumn:
return Util::shortDisplayString(action);
case NameColumn:
return action->text();
case CheckablePropColumn:
return action->isCheckable();
case PriorityPropColumn:
return EnumUtil::enumToString(action->priority(), nullptr, action->metaObject());
case ShortcutsPropColumn:
return toString(action->shortcuts());
default:
return QVariant();
}
} else if (role == Qt::DecorationRole) {
if (column == NameColumn)
return action->icon();
} else if (role == Qt::CheckStateRole) {
switch (column) {
case AddressColumn:
return action->isEnabled() ? Qt::Checked : Qt::Unchecked;
case CheckedPropColumn:
if (action->isCheckable())
return action->isChecked() ? Qt::Checked : Qt::Unchecked;
return QVariant();
}
} else if (role == ShortcutConflictRole && column == ShortcutsPropColumn) {
return m_duplicateFinder->hasAmbiguousShortcut(action);
} else if (role == ActionModel::ObjectRole) {
return QVariant::fromValue<QObject *>(action);
} else if (role == ActionModel::ObjectIdRole && index.column() == 0) {
return QVariant::fromValue(ObjectId(action));
}
return QVariant();
}
Qt::ItemFlags ActionModel::flags(const QModelIndex &index) const
{
const auto f = QAbstractTableModel::flags(index);
if (!index.isValid())
return f;
if (index.column() == AddressColumn)
return f | Qt::ItemIsUserCheckable;
if (index.column() == CheckedPropColumn && m_actions.at(index.row())->isCheckable())
return f | Qt::ItemIsUserCheckable;
return f;
}
bool ActionModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (role == Qt::CheckStateRole && index.isValid()) {
auto action = m_actions.at(index.row());
switch (index.column()) {
case AddressColumn:
action->setEnabled(value.toInt() == Qt::Checked);
return true;
case CheckedPropColumn:
action->setChecked(value.toInt() == Qt::Checked);
return true;
}
}
return QAbstractItemModel::setData(index, value, role);
}
void ActionModel::actionChanged()
{
auto action = qobject_cast<QAction *>(sender());
if (!action)
return;
auto row = m_actions.indexOf(action);
emit dataChanged(index(row, 0), index(row, ActionModel::ShortcutsPropColumn));
}
void ActionModel::scanForShortcutDuplicates() const
{
for (QAction *action : m_actions) {
Q_FOREACH (const QKeySequence &sequence, m_duplicateFinder->findAmbiguousShortcuts(action)) {
Problem p;
p.severity = Problem::Error;
p.description = QStringLiteral("Key sequence %1 is ambiguous.").arg(sequence.toString(QKeySequence::NativeText));
p.problemId = QStringLiteral("gammaray_actioninspector.ShortcutDuplicates:%1").arg(sequence.toString(QKeySequence::PortableText));
p.object = ObjectId(action);
p.locations.push_back(ObjectDataProvider::creationLocation(action));
p.findingCategory = Problem::Scan;
ProblemCollector::addProblem(p);
}
}
}
|