File: outlinemodel.cpp

package info (click to toggle)
kdevelop 4%3A5.3.1-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 52,544 kB
  • sloc: cpp: 254,897; python: 3,380; sh: 1,271; ansic: 657; xml: 221; php: 95; makefile: 36; lisp: 13; sed: 12
file content (220 lines) | stat: -rw-r--r-- 7,205 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
/*
 *   KDevelop outline view
 *   Copyright 2010, 2015 Alex Richardson <alex.richardson@gmx.de>
 *
 *   This library is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU Lesser General Public
 *   License as published by the Free Software Foundation; either
 *   version 2.1 of the License, or (at your option) any later version.
 *
 *   This library 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
 *   Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public
 *   License along with this library; if not, write to the Free Software
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
#include "outlinemodel.h"

#include <language/duchain/duchain.h>
#include <language/duchain/duchainlock.h>
#include <language/duchain/duchainutils.h>
#include <language/duchain/topducontext.h>
#include <language/duchain/ducontext.h>
#include <language/duchain/declaration.h>
#include <interfaces/icore.h>
#include <interfaces/idocument.h>
#include <interfaces/idocumentcontroller.h>

#include <debug.h>
#include "outlinenode.h"

using namespace KDevelop;

OutlineModel::OutlineModel(QObject* parent)
    : QAbstractItemModel(parent)
    , m_lastDoc(nullptr)
{
    auto docController = ICore::self()->documentController();
    // build the initial outline now
    rebuildOutline(docController->activeDocument());
    // we must always have a valid root node
    Q_ASSERT(m_rootNode);

    // we want to rebuild the outline whenever the current document has been reparsed
    connect(DUChain::self(), &DUChain::updateReady,
            this, [this] (const IndexedString& document, const ReferencedTopDUContext& /*topContext*/) {
                if (document == m_lastUrl) {
                    rebuildOutline(m_lastDoc);
                }
            });
    // and also when we switch the current document
    connect(docController, &IDocumentController::documentActivated,
            this, &OutlineModel::rebuildOutline);
    connect(docController, &IDocumentController::documentClosed,
            this, [this](IDocument* doc) {
        if (doc == m_lastDoc) {
            m_lastDoc = nullptr;
            m_lastUrl = IndexedString();
            rebuildOutline(nullptr);
        }
    });
    connect(docController, &IDocumentController::documentUrlChanged,
            this, [this](IDocument* doc) {
        if (doc == m_lastDoc) {
            m_lastUrl = IndexedString(doc->url());
        }
    });

}

OutlineModel::~OutlineModel()
{
}

Qt::ItemFlags OutlineModel::flags(const QModelIndex& index) const
{
    if (!index.isValid()) {
        return Qt::NoItemFlags;
    } else {
        return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
    }
}

int OutlineModel::columnCount(const QModelIndex& parent) const
{
    Q_UNUSED(parent)
    return 1;
}

QVariant OutlineModel::data(const QModelIndex& index, int role) const
{
    if (!index.isValid()) {
        return QVariant();
    }
    if (index.column() != 0) {
        return QVariant();
    }

    OutlineNode* node = static_cast<OutlineNode*>(index.internalPointer());
    Q_ASSERT(node);
    if (role == Qt::DecorationRole) {
        return node->icon();
    }
    if (role == Qt::DisplayRole) {
        return node->text();
    }
    return QVariant();
}

bool OutlineModel::hasChildren(const QModelIndex& parent) const
{
    return rowCount(parent) > 0;
}

int OutlineModel::rowCount(const QModelIndex& parent) const
{
    if (!parent.isValid()) {
        Q_ASSERT(m_rootNode);
        return m_rootNode->childCount();
    } else if (parent.column() != 0) {
        return 0;
    } else {
        const OutlineNode* node = static_cast<const OutlineNode*>(parent.internalPointer());
        return node->childCount();
    }
}

QModelIndex OutlineModel::index(int row, int column, const QModelIndex &parent) const
{
    if (!hasIndex(row, column, parent)) {
        return QModelIndex();
    }
    if (!parent.isValid()) {
        // topLevelItem
        if (row < m_rootNode->childCount()) {
            return createIndex(row, column, const_cast<OutlineNode*>(m_rootNode->childAt(row)));
        }
        return QModelIndex();
    } else {
        if (parent.column() != 0) {
            return QModelIndex(); //only column 0 should have children
        }
        OutlineNode* node = static_cast<OutlineNode*>(parent.internalPointer());
        if (row < node->childCount()) {
            return createIndex(row, column, const_cast<OutlineNode*>(node->childAt(row)));
        }
        return QModelIndex(); // out of range
    }
    return QModelIndex();
}

QModelIndex OutlineModel::parent(const QModelIndex& index) const
{
    if (!index.isValid()) {
        return QModelIndex();
    }

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

    const OutlineNode* parentNode = node->parent();
    Q_ASSERT(parentNode);
    if (parentNode == m_rootNode.get()) {
        return QModelIndex(); //node is a top level item
    }

    // parent node was not m_rootNode -> parent() must be valid
    const OutlineNode* parentParentNode = parentNode->parent();
    Q_ASSERT(parentNode);
    const int row = parentParentNode->indexOf(parentNode);
    return createIndex(row, 0, const_cast<OutlineNode*>(parentNode));
}

void OutlineModel::rebuildOutline(IDocument* doc)
{
    beginResetModel();
    if (!doc) {
        m_rootNode = OutlineNode::dummyNode();
    } else {
        // TODO: do this in a separate thread? Might take a while for large documents
        // and we really shouldn't be blocking the GUI thread!
        DUChainReadLocker lock;
        TopDUContext* topContext = DUChainUtils::standardContextForUrl(doc->url());
        if (topContext) {
            m_rootNode = OutlineNode::fromTopContext(topContext);
        } else {
            m_rootNode = OutlineNode::dummyNode();
        }
    }
    if (doc != m_lastDoc) {
        m_lastUrl = doc ? IndexedString(doc->url()) : IndexedString();
        m_lastDoc = doc;
    }
    endResetModel();
}

void OutlineModel::activate(const QModelIndex& realIndex)
{
    if (!realIndex.isValid()) {
        qCWarning(PLUGIN_OUTLINE) << "attempting to activate invalid item!";
        return;
    }
    OutlineNode* node = static_cast<OutlineNode*>(realIndex.internalPointer());
    KTextEditor::Range range;
    {
        DUChainReadLocker lock;
        const DUChainBase* dcb = node->duChainObject();
        if (!dcb) {
            qCDebug(PLUGIN_OUTLINE) << "No declaration exists for node:" << node->text();
            return;
        }
        //foreground thread == GUI thread? if so then we are fine
        range = dcb->rangeInCurrentRevision();
        //outline view should ALWAYS correspond to currently active document
        Q_ASSERT(dcb->url().toUrl() == ICore::self()->documentController()->activeDocument()->url());
        // lock should be released before activating the document
    }
    ICore::self()->documentController()->activateDocument(m_lastDoc, range);
}