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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
|
/*
objecttreemodel.cpp
This file is part of GammaRay, the Qt application inspection and manipulation tool.
SPDX-FileCopyrightText: 2010 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
Author: Volker Krause <volker.krause@kdab.com>
SPDX-License-Identifier: GPL-2.0-or-later
Contact KDAB at <info@kdab.com> for commercial licensing options.
*/
#include "objecttreemodel.h"
#include "probe.h"
#include <QEvent>
#include <QMutex>
#include <QThread>
#include <QCoreApplication>
#include <algorithm>
#include <iostream>
#define IF_DEBUG(x)
extern void dumpObject(QObject *);
using namespace std;
using namespace GammaRay;
ObjectTreeModel::ObjectTreeModel(Probe *probe)
: ObjectModelBase<QAbstractItemModel>(probe)
{
connect(probe, &Probe::objectCreated,
this, &ObjectTreeModel::objectAdded);
connect(probe, &Probe::objectDestroyed,
this, &ObjectTreeModel::objectRemoved);
connect(probe, &Probe::objectReparented,
this, &ObjectTreeModel::objectReparented);
connect(probe, &Probe::objectFavorited,
this, &ObjectTreeModel::objectFavorited);
connect(probe, &Probe::objectUnfavorited,
this, &ObjectTreeModel::objectUnfavorited);
}
QPair<int, QVariant> ObjectTreeModel::defaultSelectedItem()
{
// select the qApp object (if any) in the object model
return QPair<int, QVariant>(ObjectModel::ObjectRole, QVariant::fromValue<QObject *>(qApp));
}
static inline QObject *parentObject(QObject *obj)
{
return obj->parent();
}
void ObjectTreeModel::objectAdded(QObject *obj)
{
// see Probe::objectCreated, that promises a valid object in the main thread here
Q_ASSERT(thread() == QThread::currentThread());
Q_ASSERT(Probe::instance()->isValidObject(obj));
IF_DEBUG(cout << "tree obj added: " << hex << obj << " p: " << parentObject(obj) << endl;)
Q_ASSERT(!obj->parent() || Probe::instance()->isValidObject(parentObject(obj)));
if (indexForObject(obj).isValid()) {
IF_DEBUG(cout << "tree double obj added: " << hex << obj << endl;)
return;
}
// this is ugly, but apparently it can happen
// that an object gets created without parent
// then later the delayed signal comes in
// so catch this gracefully by first adding the
// parent if required
QModelIndex index = indexForObject(parentObject(obj));
if (parentObject(obj)) {
if (!index.isValid()) {
IF_DEBUG(cout << "tree: handle parent first" << endl;)
objectAdded(parentObject(obj));
index = indexForObject(parentObject(obj));
}
}
// either we get a proper parent and hence valid index or there is no parent
Q_ASSERT(index.isValid() || !parentObject(obj));
QVector<QObject *> &children = m_parentChildMap[parentObject(obj)];
auto it = std::lower_bound(children.begin(), children.end(), obj);
const int row = std::distance(children.begin(), it);
beginInsertRows(index, row, row);
children.insert(it, obj);
m_childParentMap.insert(obj, parentObject(obj));
endInsertRows();
}
void ObjectTreeModel::objectRemoved(QObject *obj)
{
// slot, hence should always land in main thread due to auto connection
Q_ASSERT(thread() == QThread::currentThread());
IF_DEBUG(cout
<< "tree removed: "
<< hex << obj << " "
<< hex << obj->parent() << dec << " "
<< m_parentChildMap.value(obj->parent()).size() << " "
<< m_parentChildMap.contains(obj) << endl;)
auto parentIt = m_childParentMap.constFind(obj);
if (parentIt == m_childParentMap.cend()) {
Q_ASSERT(!m_parentChildMap.contains(obj));
return;
}
QObject *parentObj = parentIt.value();
const QModelIndex parentIndex = indexForObject(parentObj);
if (parentObj && !parentIndex.isValid())
return;
QVector<QObject *> &siblings = m_parentChildMap[parentObj];
auto it = std::lower_bound(siblings.begin(), siblings.end(), obj);
if (it == siblings.end() || *it != obj)
return;
const int row = std::distance(siblings.begin(), it);
beginRemoveRows(parentIndex, row, row);
siblings.erase(it);
m_childParentMap.erase(parentIt);
m_parentChildMap.remove(obj);
m_favorites.remove(obj);
endRemoveRows();
}
void ObjectTreeModel::objectReparented(QObject *obj)
{
// slot, hence should always land in main thread due to auto connection
Q_ASSERT(thread() == QThread::currentThread());
IF_DEBUG(cout << "object reparented: " << hex << obj << dec << endl;)
QMutexLocker objectLock(Probe::objectLock());
if (!Probe::instance()->isValidObject(obj)) {
objectRemoved(obj);
return;
}
// we didn't know obj yet
auto parentIt = m_childParentMap.constFind(obj);
if (parentIt == m_childParentMap.cend()) {
Q_ASSERT(!m_parentChildMap.contains(obj));
objectAdded(obj);
return;
}
QObject *oldParent = parentIt.value();
const auto sourceParent = indexForObject(oldParent);
if ((oldParent && !sourceParent.isValid()) || (oldParent == parentObject(obj)))
return;
QVector<QObject *> &oldSiblings = m_parentChildMap[oldParent];
auto oldIt = std::lower_bound(oldSiblings.begin(),
oldSiblings.end(), obj);
if (oldIt == oldSiblings.end() || *oldIt != obj)
return;
const int sourceRow = std::distance(oldSiblings.begin(), oldIt);
IF_DEBUG(cout << "actually reparenting! " << hex << obj << " old parent: " << oldParent << " new parent: " << parentObject(obj) << dec << endl;)
const auto destParent = indexForObject(parentObject(obj));
Q_ASSERT(destParent.isValid() || !parentObject(obj));
QVector<QObject *> &newSiblings = m_parentChildMap[parentObject(obj)];
auto newIt = std::lower_bound(newSiblings.begin(),
newSiblings.end(), obj);
const int destRow = std::distance(newSiblings.begin(), newIt);
beginMoveRows(sourceParent, sourceRow, sourceRow, destParent, destRow);
oldSiblings.erase(oldIt);
newSiblings.insert(newIt, obj);
m_childParentMap.insert(obj, parentObject(obj));
endMoveRows();
}
void ObjectTreeModel::objectFavorited(QObject *obj)
{
auto index = indexForObject(obj);
if (!index.isValid()) {
return;
}
m_favorites.insert(obj);
Q_EMIT dataChanged(index, index, { ObjectModel::IsFavoriteRole });
}
void ObjectTreeModel::objectUnfavorited(QObject *obj)
{
auto index = indexForObject(obj);
if (!index.isValid()) {
return;
}
Q_ASSERT(m_favorites.contains(obj));
m_favorites.remove(obj);
Q_EMIT dataChanged(index, index, { ObjectModel::IsFavoriteRole });
}
QVariant ObjectTreeModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
QObject *obj = reinterpret_cast<QObject *>(index.internalPointer());
QMutexLocker lock(Probe::objectLock());
if (Probe::instance()->isValidObject(obj)) {
if (role == ObjectModel::IsFavoriteRole) {
return m_favorites.contains(obj);
}
return dataForObject(obj, index, role);
}
if (role == Qt::DisplayRole) {
if (index.column() == 0)
return Util::addressToString(obj);
return tr("<deleted>");
}
return QVariant();
}
int ObjectTreeModel::rowCount(const QModelIndex &parent) const
{
if (parent.column() == 1)
return 0;
QObject *parentObj = reinterpret_cast<QObject *>(parent.internalPointer());
return m_parentChildMap.value(parentObj).size();
}
QModelIndex ObjectTreeModel::parent(const QModelIndex &child) const
{
QObject *childObj = reinterpret_cast<QObject *>(child.internalPointer());
return indexForObject(m_childParentMap.value(childObj));
}
QModelIndex ObjectTreeModel::index(int row, int column, const QModelIndex &parent) const
{
QObject *parentObj = reinterpret_cast<QObject *>(parent.internalPointer());
const QVector<QObject *> children = m_parentChildMap.value(parentObj);
if (row < 0 || column < 0 || row >= children.size() || column >= columnCount())
return {};
return createIndex(row, column, children.at(row));
}
QModelIndex ObjectTreeModel::indexForObject(QObject *object) const
{
if (!object)
return {};
// Find the parent
auto parentIt = m_childParentMap.constFind(object);
if (parentIt == m_childParentMap.cend())
return {};
// Find all children of this parent
auto childrenIt = m_parentChildMap.constFind(parentIt.value());
if (childrenIt == m_parentChildMap.cend())
return {};
const QVector<QObject *> &siblings = childrenIt.value();
// Find where @p object is
auto it = std::lower_bound(siblings.constBegin(), siblings.constEnd(), object);
if (it == siblings.constEnd() || *it != object)
return QModelIndex();
const int row = std::distance(siblings.constBegin(), it);
return createIndex(row, 0, *it);
}
|