File: classmodel.cpp

package info (click to toggle)
kdevelop 4%3A5.6.2-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 57,892 kB
  • sloc: cpp: 278,773; javascript: 3,558; python: 3,385; sh: 1,317; ansic: 689; xml: 273; php: 95; makefile: 40; lisp: 13; sed: 12
file content (274 lines) | stat: -rw-r--r-- 7,556 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
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
/*
 * KDevelop Class Browser
 *
 * Copyright 2007-2008 Hamish Rodda <rodda@kde.org>
 * Copyright 2009 Lior Mualem <lior.m.kde@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Library General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include "classmodel.h"
#include "classmodelnode.h"
#include "allclassesfolder.h"
#include "projectfolder.h"
#include "../duchain/declaration.h"
#include <typeinfo>

#include "../../interfaces/icore.h"
#include "../../interfaces/iproject.h"
#include "../../interfaces/iprojectcontroller.h"

using namespace KDevelop;
using namespace ClassModelNodes;

//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////

NodesModelInterface::~NodesModelInterface()
{
}

//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////

ClassModel::ClassModel()
    : m_features(NodesModelInterface::AllProjectsClasses |
        NodesModelInterface::BaseAndDerivedClasses |
        NodesModelInterface::ClassInternals)
{
    m_topNode = new FolderNode(QStringLiteral("Top Node"), this);

    if (features().testFlag(NodesModelInterface::AllProjectsClasses)) {
        m_allClassesNode = new FilteredAllClassesFolder(this);
        m_topNode->addNode(m_allClassesNode);
    }

    connect(ICore::self()->projectController(), &IProjectController::projectClosing,
            this, &ClassModel::removeProjectNode);
    connect(ICore::self()->projectController(), &IProjectController::projectOpened,
            this, &ClassModel::addProjectNode);

    const auto projects = ICore::self()->projectController()->projects();
    for (IProject* project : projects) {
        addProjectNode(project);
    }
}

ClassModel::~ClassModel()
{
    delete m_topNode;
}

void ClassModel::updateFilterString(const QString& a_newFilterString)
{
    m_allClassesNode->updateFilterString(a_newFilterString);
    for (ClassModelNodes::FilteredProjectFolder* folder : qAsConst(m_projectNodes)) {
        folder->updateFilterString(a_newFilterString);
    }
}

void ClassModel::collapsed(const QModelIndex& index)
{
    Node* node = static_cast<Node*>(index.internalPointer());

    node->collapse();
}

void ClassModel::expanded(const QModelIndex& index)
{
    Node* node = static_cast<Node*>(index.internalPointer());

    node->expand();
}

QFlags<Qt::ItemFlag> ClassModel::flags(const QModelIndex&) const
{
    return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
}

int ClassModel::rowCount(const QModelIndex& parent) const
{
    Node* node = m_topNode;

    if (parent.isValid())
        node = static_cast<Node*>(parent.internalPointer());

    return node->children().size();
}

QVariant ClassModel::data(const QModelIndex& index, int role) const
{
    if (!index.isValid())
        return QVariant();

    Node* node = static_cast<Node*>(index.internalPointer());

    if (role == Qt::DisplayRole)
        return node->displayName();

    if (role == Qt::DecorationRole) {
        QIcon icon = node->cachedIcon();
        return icon.isNull() ? QVariant() : icon;
    }

    return QVariant();
}

QVariant ClassModel::headerData(int, Qt::Orientation, int role) const
{
    if (role == Qt::DisplayRole)
        return QStringLiteral("Class");

    return QVariant();
}

int ClassModel::columnCount(const QModelIndex&) const
{
    return 1;
}

bool ClassModel::hasChildren(const QModelIndex& parent) const
{
    if (!parent.isValid())
        return true;

    Node* node = static_cast<Node*>(parent.internalPointer());

    return node->hasChildren();
}

QModelIndex ClassModel::index(int row, int column, const QModelIndex& parent) const
{
    if (row < 0 || column != 0)
        return QModelIndex();

    Node* node = m_topNode;
    if (parent.isValid())
        node = static_cast<Node*>(parent.internalPointer());

    if (row >= node->children().size())
        return QModelIndex();

    return index(node->children()[row]);
}

QModelIndex ClassModel::parent(const QModelIndex& childIndex) const
{
    if (!childIndex.isValid())
        return QModelIndex();

    Node* childNode = static_cast<Node*>(childIndex.internalPointer());

    if (childNode->parent() == m_topNode)
        return QModelIndex();

    return index(childNode->parent());
}

QModelIndex ClassModel::index(ClassModelNodes::Node* a_node) const
{
    if (!a_node) {
        return QModelIndex();
    }

    // If no parent exists, we have an invalid index (root node or not part of a model).
    if (a_node->parent() == nullptr)
        return QModelIndex();

    return createIndex(a_node->row(), 0, a_node);
}

KDevelop::DUChainBase* ClassModel::duObjectForIndex(const QModelIndex& a_index)
{
    if (!a_index.isValid())
        return nullptr;

    Node* node = static_cast<Node*>(a_index.internalPointer());

    if (auto* identifierNode = dynamic_cast<IdentifierNode*>(node))
        return identifierNode->declaration();

    // Non was found.
    return nullptr;
}

QModelIndex ClassModel::indexForIdentifier(const KDevelop::IndexedQualifiedIdentifier& a_id)
{
    ClassNode* node = m_allClassesNode->findClassNode(a_id);
    if (node == nullptr)
        return QModelIndex();

    return index(node);
}

void ClassModel::nodesLayoutAboutToBeChanged(ClassModelNodes::Node*)
{
    emit layoutAboutToBeChanged();
}

void ClassModel::nodesLayoutChanged(ClassModelNodes::Node*)
{
    const QModelIndexList oldIndexList = persistentIndexList();
    QModelIndexList newIndexList;

    newIndexList.reserve(oldIndexList.size());
    for (const QModelIndex& oldIndex : oldIndexList) {
        Node* node = static_cast<Node*>(oldIndex.internalPointer());
        if (node) {
            // Re-map the index.
            newIndexList << createIndex(node->row(), 0, node);
        } else
            newIndexList << oldIndex;
    }

    changePersistentIndexList(oldIndexList, newIndexList);

    emit layoutChanged();
}

void ClassModel::nodesAboutToBeRemoved(ClassModelNodes::Node* a_parent, int a_first, int a_last)
{
    beginRemoveRows(index(a_parent), a_first, a_last);
}

void ClassModel::nodesRemoved(ClassModelNodes::Node*)
{
    endRemoveRows();
}

void ClassModel::nodesAboutToBeAdded(ClassModelNodes::Node* a_parent, int a_pos, int a_size)
{
    beginInsertRows(index(a_parent), a_pos, a_pos + a_size - 1);
}

void ClassModel::nodesAdded(ClassModelNodes::Node*)
{
    endInsertRows();
}

void ClassModel::addProjectNode(IProject* project)
{
    m_projectNodes[project] = new ClassModelNodes::FilteredProjectFolder(this, project);
    nodesLayoutAboutToBeChanged(m_projectNodes[project]);
    m_topNode->addNode(m_projectNodes[project]);
    nodesLayoutChanged(m_projectNodes[project]);
}

void ClassModel::removeProjectNode(IProject* project)
{
    m_topNode->removeNode(m_projectNodes[project]);
    m_projectNodes.remove(project);
}