File: modelinspector.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 (170 lines) | stat: -rw-r--r-- 6,771 bytes parent folder | download | duplicates (2)
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
/*
  modelinspector.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 "modelinspector.h"

#include "modelmodel.h"
#include "modelcellmodel.h"
#include "modelcontentproxymodel.h"
#include "selectionmodelmodel.h"

#include <core/remote/serverproxymodel.h>
#include <common/objectbroker.h>

#include <QDebug>
#include <QItemSelectionModel>
#include <QSortFilterProxyModel>

using namespace GammaRay;

ModelInspector::ModelInspector(Probe *probe, QObject *parent)
    : ModelInspectorInterface(parent)
    , m_probe(probe)
    , m_modelModel(nullptr)
    , m_selectionModelsModel(new SelectionModelModel(this))
    , m_selectionModelsSelectionModel(nullptr)
    , m_modelContentSelectionModel(nullptr)
    , m_modelContentProxyModel(new ModelContentProxyModel(this))
{
    auto modelModelSource = new ModelModel(this);
    connect(probe, &Probe::objectCreated, modelModelSource, &ModelModel::objectAdded);
    connect(probe, &Probe::objectDestroyed, modelModelSource, &ModelModel::objectRemoved);

    auto modelModelProxy = new ServerProxyModel<QSortFilterProxyModel>(this);
    modelModelProxy->setSourceModel(modelModelSource);
    m_modelModel = modelModelProxy;
    probe->registerModel(QStringLiteral("com.kdab.GammaRay.ModelModel"), m_modelModel);

    m_modelSelectionModel = ObjectBroker::selectionModel(m_modelModel);
    connect(m_modelSelectionModel, &QItemSelectionModel::selectionChanged,
            this, &ModelInspector::modelSelected);
    connect(probe, &Probe::objectSelected, this, &ModelInspector::objectSelected);

    connect(probe, &Probe::objectCreated, m_selectionModelsModel, &SelectionModelModel::objectCreated);
    connect(probe, &Probe::objectDestroyed, m_selectionModelsModel, &SelectionModelModel::objectDestroyed);
    probe->registerModel(QStringLiteral("com.kdab.GammaRay.SelectionModels"), m_selectionModelsModel);
    m_selectionModelsSelectionModel = ObjectBroker::selectionModel(m_selectionModelsModel);
    connect(m_selectionModelsSelectionModel, &QItemSelectionModel::selectionChanged,
            this, &ModelInspector::selectionModelSelected);

    probe->registerModel(QStringLiteral("com.kdab.GammaRay.ModelContent"), m_modelContentProxyModel);
    m_modelContentSelectionModel = ObjectBroker::selectionModel(m_modelContentProxyModel);
    connect(m_modelContentSelectionModel, &QItemSelectionModel::selectionChanged,
            this, &ModelInspector::cellSelectionChanged);

    m_cellModel = new ModelCellModel(this);
    probe->registerModel(QStringLiteral("com.kdab.GammaRay.ModelCellModel"), m_cellModel);

    if (m_probe->needsObjectDiscovery())
        connect(m_probe, &Probe::objectCreated, this, &ModelInspector::objectCreated);
}

void ModelInspector::modelSelected(const QItemSelection &selected)
{
    QModelIndex index;
    if (!selected.isEmpty())
        index = selected.first().topLeft();

    if (index.isValid()) {
        QObject *obj = index.data(ObjectModel::ObjectRole).value<QObject *>();
        QAbstractItemModel *model = qobject_cast<QAbstractItemModel *>(obj);
        Q_ASSERT(model);
        m_selectionModelsModel->setModel(model);
        m_modelContentProxyModel->setSourceModel(model);
    } else {
        m_selectionModelsModel->setModel(nullptr);
        m_modelContentProxyModel->setSourceModel(nullptr);
    }

    // clear the cell info box
    setCurrentCellData(ModelCellData());
    m_cellModel->setModelIndex(QModelIndex());
    m_modelContentSelectionModel->clear();
}

void ModelInspector::objectSelected(QObject *object)
{
    if (auto model = qobject_cast<QAbstractItemModel *>(object)) {
        if (model == m_modelContentProxyModel->sourceModel())
            return;

        const auto indexList = m_modelModel->match(m_modelModel->index(0, 0),
                                                   ObjectModel::ObjectRole,
                                                   QVariant::fromValue<QObject *>(model), 1,
                                                   Qt::MatchExactly | Qt::MatchRecursive | Qt::MatchWrap);
        if (indexList.isEmpty())
            return;

        const auto &index = indexList.first();
        m_modelSelectionModel->select(index, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
    }
    if (auto selModel = qobject_cast<QItemSelectionModel *>(object)) {
        if (!selModel->model())
            return;
        objectSelected(const_cast<QAbstractItemModel *>(selModel->model()));

        const auto indexList = m_selectionModelsModel->match(m_selectionModelsModel->index(0, 0),
                                                             ObjectModel::ObjectRole,
                                                             QVariant::fromValue<QObject *>(selModel), 1,
                                                             Qt::MatchExactly | Qt::MatchRecursive | Qt::MatchWrap);
        if (indexList.isEmpty())
            return;

        const auto &index = indexList.first();
        m_selectionModelsSelectionModel->select(index, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
    }
}

void ModelInspector::cellSelectionChanged(const QItemSelection &selection)
{
    QModelIndex index;
    if (!selection.isEmpty())
        index = selection.at(0).topLeft();

    const QModelIndex sourceIndex = m_modelContentProxyModel->mapToSource(index);
    m_cellModel->setModelIndex(sourceIndex);

    if (!sourceIndex.isValid()) {
        setCurrentCellData(ModelCellData());
        return;
    }

    ModelCellData cellData;
    cellData.row = sourceIndex.row();
    cellData.column = sourceIndex.column();
    cellData.internalId = QString::number(sourceIndex.internalId());
    cellData.internalPtr = Util::addressToString(sourceIndex.internalPointer());
    cellData.flags = sourceIndex.flags();
    setCurrentCellData(cellData);
}

void ModelInspector::objectCreated(QObject *object)
{
    if (!object)
        return;

    if (auto proxy = qobject_cast<QAbstractProxyModel *>(object))
        m_probe->discoverObject(proxy->sourceModel());
}

void ModelInspector::selectionModelSelected(const QItemSelection &selected)
{
    QModelIndex idx;
    if (!selected.isEmpty())
        idx = selected.at(0).topLeft();
    if (!idx.isValid()) {
        m_modelContentProxyModel->setSelectionModel(nullptr);
        return;
    }
    m_modelContentProxyModel->setSelectionModel(qobject_cast<QItemSelectionModel *>(idx.data(ObjectModel::ObjectRole).value<QObject *>()));
}