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
|
/*
bindingmodel.cpp
This file is part of GammaRay, the Qt application inspection and manipulation tool.
SPDX-FileCopyrightText: 2017 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
SPDX-License-Identifier: GPL-2.0-or-later
Contact KDAB at <info@kdab.com> for commercial licensing options.
*/
// Own
#include "bindingmodel.h"
#include <common/objectmodel.h>
#include <common/problem.h>
#include <core/problemcollector.h>
#include <core/objectdataprovider.h>
#include <core/abstractbindingprovider.h>
#include <core/bindingnode.h>
#include <core/util.h>
// Qt
#include <QDebug>
#include <QMetaProperty>
using namespace GammaRay;
BindingModel::BindingModel(QObject *parent)
: QAbstractItemModel(parent)
, m_obj(nullptr)
, m_bindings(nullptr)
{
}
BindingModel::~BindingModel() = default;
void BindingModel::aboutToClear()
{
beginResetModel();
}
void BindingModel::cleared()
{
m_obj = nullptr;
endResetModel();
}
void BindingModel::setObject(QObject *obj, std::vector<std::unique_ptr<BindingNode>> &bindings)
{
if (m_obj == obj)
return;
// TODO use removerows/insertrows instead of reset here
beginResetModel();
m_bindings = &bindings;
m_obj = obj;
endResetModel();
}
void GammaRay::BindingModel::refresh(int row, std::vector<std::unique_ptr<BindingNode>> &&newDependencies)
{
Q_ASSERT(m_bindings);
refresh((*m_bindings)[row].get(), std::move(newDependencies), createIndex(row, 0, (*m_bindings)[row].get()));
}
bool BindingModel::lessThan(const std::unique_ptr<BindingNode> &a, const std::unique_ptr<BindingNode> &b)
{
return a->object() < b->object()
|| (a->object() == b->object() && a->propertyIndex() < b->propertyIndex());
}
void BindingModel::refresh(BindingNode *oldBindingNode, std::vector<std::unique_ptr<BindingNode>> &&newDependencies, const QModelIndex &index)
{
if (oldBindingNode->cachedValue() != oldBindingNode->readValue()) {
oldBindingNode->refreshValue();
emit dataChanged(createIndex(index.row(), ValueColumn, oldBindingNode), createIndex(index.row(), ValueColumn, oldBindingNode));
}
uint oldDepth = oldBindingNode->depth();
// Refresh dependencies
auto &oldDependencies = oldBindingNode->dependencies();
std::sort(newDependencies.begin(), newDependencies.end(), &BindingModel::lessThan);
oldDependencies.reserve(newDependencies.size());
auto oldIt = oldDependencies.begin();
auto newIt = newDependencies.begin();
while (oldIt != oldDependencies.end() && newIt != newDependencies.end()) {
const auto idx = std::distance(oldDependencies.begin(), oldIt);
if (lessThan(*oldIt, *newIt)) { // handle deleted node
const auto firstToRemove = oldIt;
while (oldIt != oldDependencies.end() && lessThan(*oldIt, *newIt)) {
++oldIt;
} // if more than one was removed, find all
const auto count = std::distance(firstToRemove, oldIt);
beginRemoveRows(index, idx, idx + count - 1);
oldIt = oldDependencies.erase(firstToRemove, oldIt);
endRemoveRows();
} else if (lessThan(*newIt, *oldIt)) { // handle added node
int count = 0;
for (auto addIt = newIt; addIt != newDependencies.end() && lessThan(*addIt, *oldIt); ++addIt) {
++count; // count, how many additions we have
}
beginInsertRows(index, idx, idx + count - 1);
for (int i = 0; i < count; ++i) {
(*newIt)->setParent(oldBindingNode);
oldIt = oldDependencies.insert(oldIt, std::move(*newIt));
++oldIt;
++newIt;
}
endInsertRows();
} else { // already known node, no change
refresh(oldIt->get(), std::move(newIt->get()->dependencies()), createIndex(idx, 0, oldIt->get()));
++oldIt;
++newIt;
}
}
if (oldIt == oldDependencies.end() && newIt != newDependencies.end()) {
// Add remaining new items to list and inform the client
const auto idx = std::distance(oldDependencies.begin(), oldIt);
const auto count = std::distance(newIt, newDependencies.end());
beginInsertRows(index, idx, idx + count - 1);
while (newIt != newDependencies.end()) {
(*newIt)->setParent(oldBindingNode);
oldDependencies.push_back(std::move(*newIt));
++newIt;
}
endInsertRows();
} else if (oldIt != oldDependencies.end()) { // Inform the client about the removed rows
const auto idx = std::distance(oldDependencies.begin(), oldIt);
const auto count = std::distance(oldIt, oldDependencies.end());
beginRemoveRows(index, idx, idx + count - 1);
oldIt = oldDependencies.erase(oldIt, oldDependencies.end());
endRemoveRows();
}
if (oldBindingNode->depth() != oldDepth) {
emit dataChanged(createIndex(index.row(), DepthColumn, oldBindingNode), createIndex(index.row(), DepthColumn, oldBindingNode));
}
}
int BindingModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return 4;
}
int BindingModel::rowCount(const QModelIndex &parent) const
{
if (!m_bindings)
return 0;
if (!parent.isValid())
return m_bindings->size();
if (parent.column() != 0)
return 0;
return static_cast<BindingNode *>(parent.internalPointer())->dependencies().size();
}
QVariant BindingModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
BindingNode *binding = static_cast<BindingNode *>(index.internalPointer());
if (!binding)
return QVariant();
if (role == Qt::DisplayRole) {
switch (index.column()) {
case NameColumn: {
return binding->canonicalName();
}
case ValueColumn:
return binding->cachedValue();
case LocationColumn:
return binding->sourceLocation().displayString();
case DepthColumn: {
uint depth = binding->depth();
return depth == std::numeric_limits<uint>::max() ? QString(QChar(0x221E)) : QString::number(depth); // Unicode infinity sign
}
}
} else if (role == ObjectModel::DeclarationLocationRole) {
return QVariant::fromValue(binding->sourceLocation());
}
return QVariant();
}
QMap<int, QVariant> BindingModel::itemData(const QModelIndex &index) const
{
QMap<int, QVariant> d = QAbstractItemModel::itemData(index);
d.insert(ObjectModel::DeclarationLocationRole, data(index, ObjectModel::DeclarationLocationRole));
return d;
}
QVariant BindingModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
switch (section) {
case NameColumn:
return tr("Property");
case ValueColumn:
return tr("Value");
case LocationColumn:
return tr("Source");
case DepthColumn:
return tr("Depth");
}
}
return QAbstractItemModel::headerData(section, orientation, role);
}
QModelIndex GammaRay::BindingModel::index(int row, int column, const QModelIndex &parent) const
{
if (!m_bindings || !hasIndex(row, column, parent)) {
return {};
}
QModelIndex index;
if (parent.isValid()) {
index = createIndex(row, column, static_cast<BindingNode *>(parent.internalPointer())->dependencies()[row].get());
} else {
index = createIndex(row, column, (*m_bindings)[row].get());
}
return index;
}
QModelIndex BindingModel::findEquivalent(const std::vector<std::unique_ptr<BindingNode>> &container, BindingNode *bindingNode) const
{
for (size_t i = 0; i < container.size(); i++) {
if (bindingNode->object() == container[i]->object() && bindingNode->propertyIndex() == container[i]->propertyIndex()) {
return createIndex(i, 0, container[i].get());
}
}
return {};
}
QModelIndex GammaRay::BindingModel::parent(const QModelIndex &child) const
{
if (!m_bindings || !child.isValid())
return {};
BindingNode *parent = static_cast<BindingNode *>(child.internalPointer())->parent();
if (!parent)
return QModelIndex();
BindingNode *grandparent = parent->parent();
if (!grandparent)
return findEquivalent(*m_bindings, parent);
return findEquivalent(grandparent->dependencies(), parent);
}
|