File: quickopenmodel.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 (500 lines) | stat: -rw-r--r-- 15,359 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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
/* This file is part of the KDE libraries
   Copyright (C) 2007 David Nolden <david.nolden.kdevelop@art-master.de>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License version 2 as published by the Free Software Foundation.

   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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
 */

#include "quickopenmodel.h"
#include "debug.h"

#include <QTreeView>
#include <QTimer>

#include <KTextEditor/CodeCompletionModel>
#include <typeinfo>

#include "expandingtree/expandingtree.h"
#include "projectfilequickopen.h"
#include "duchainitemquickopen.h"

#define QUICKOPEN_USE_ITEM_CACHING

using namespace KDevelop;

QuickOpenModel::QuickOpenModel(QWidget* parent) : ExpandingWidgetModel(parent)
    , m_treeView(nullptr)
    , m_expandingWidgetHeightIncrease(0)
    , m_resetBehindRow(0)
{
    m_resetTimer = new QTimer(this);
    m_resetTimer->setSingleShot(true);
    m_resetTimer->setInterval(0);
    connect(m_resetTimer, &QTimer::timeout, this, &QuickOpenModel::resetTimer);
}

void QuickOpenModel::setExpandingWidgetHeightIncrease(int pixels)
{
    m_expandingWidgetHeightIncrease = pixels;
}

QStringList QuickOpenModel::allScopes() const
{
    QStringList scopes;
    for (const ProviderEntry& provider : m_providers) {
        for (const QString& scope : provider.scopes) {
            if (!scopes.contains(scope)) {
                scopes << scope;
            }
        }
    }

    return scopes;
}

QStringList QuickOpenModel::allTypes() const
{
    QSet<QString> types;
    for (const ProviderEntry& provider : m_providers) {
        types += provider.types;
    }

    return types.values();
}

void QuickOpenModel::registerProvider(const QStringList& scopes, const QStringList& types, KDevelop::QuickOpenDataProviderBase* provider)
{
    ProviderEntry e;
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
    e.scopes = QSet<QString>(scopes.begin(), scopes.end());
    e.types = QSet<QString>(types.begin(), types.end());
#else
    e.scopes = QSet<QString>::fromList(scopes);
    e.types = QSet<QString>::fromList(types);
#endif
    e.provider = provider;

    m_providers << e; //.insert( types, e );

    connect(provider, &QuickOpenDataProviderBase::destroyed, this, &QuickOpenModel::destroyed);

    restart(true);
}

bool QuickOpenModel::removeProvider(KDevelop::QuickOpenDataProviderBase* provider)
{
    bool ret = false;
    for (ProviderList::iterator it = m_providers.begin(); it != m_providers.end(); ++it) {
        if ((*it).provider == provider) {
            m_providers.erase(it);
            disconnect(provider, &QuickOpenDataProviderBase::destroyed, this, &QuickOpenModel::destroyed);
            ret = true;
            break;
        }
    }

    restart(true);

    return ret;
}

void QuickOpenModel::enableProviders(const QStringList& _items, const QStringList& _scopes)
{
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
    const QSet<QString> items(_items.begin(), _items.end());
    const QSet<QString> scopes(_scopes.begin(), _scopes.end());
#else
    const QSet<QString> items = QSet<QString>::fromList(_items);
    const QSet<QString> scopes = QSet<QString>::fromList(_scopes);
#endif
    if (m_enabledItems == items && m_enabledScopes == scopes && !items.isEmpty() && !scopes.isEmpty()) {
        return;
    }
    m_enabledItems = items;
    m_enabledScopes  = scopes;
    qCDebug(PLUGIN_QUICKOPEN) << "params " << items << " " << scopes;

    //We use 2 iterations here: In the first iteration, all providers that implement QuickOpenFileSetInterface are initialized, then the other ones.
    //The reason is that the second group can refer to the first one.
    for (auto& provider : m_providers) {
        if (!qobject_cast<QuickOpenFileSetInterface*>(provider.provider)) {
            continue;
        }
        qCDebug(PLUGIN_QUICKOPEN) << "comparing" << provider.scopes << provider.types;
        if ((scopes.isEmpty() || !(scopes & provider.scopes).isEmpty()) && (!(items & provider.types).isEmpty() || items.isEmpty())) {
            qCDebug(PLUGIN_QUICKOPEN) << "enabling " << provider.types << " " << provider.scopes;
            provider.enabled = true;
            provider.provider->enableData(_items, _scopes);
        } else {
            qCDebug(PLUGIN_QUICKOPEN) << "disabling " << provider.types << " " << provider.scopes;
            provider.enabled = false;
            if ((scopes.isEmpty() || !(scopes & provider.scopes).isEmpty())) {
                provider.provider->enableData(_items, _scopes); //The provider may still provide files
            }
        }
    }

    for (auto & provider : m_providers) {
        if (qobject_cast<QuickOpenFileSetInterface*>(provider.provider)) {
            continue;
        }
        qCDebug(PLUGIN_QUICKOPEN) << "comparing" << provider.scopes << provider.types;
        if ((scopes.isEmpty() || !(scopes & provider.scopes).isEmpty()) && (!(items & provider.types).isEmpty() || items.isEmpty())) {
            qCDebug(PLUGIN_QUICKOPEN) << "enabling " << provider.types << " " << provider.scopes;
            provider.enabled = true;
            provider.provider->enableData(_items, _scopes);
        } else {
            qCDebug(PLUGIN_QUICKOPEN) << "disabling " << provider.types << " " << provider.scopes;
            provider.enabled = false;
        }
    }

    restart(true);
}

void QuickOpenModel::textChanged(const QString& str)
{
    if (m_filterText == str) {
        return;
    }

    beginResetModel();

    m_filterText = str;
    for (const ProviderEntry& provider : qAsConst(m_providers)) {
        if (provider.enabled) {
            provider.provider->setFilterText(str);
        }
    }

    m_cachedData.clear();
    clearExpanding();

    //Get the 50 first items, so the data-providers notice changes without ui-glitches due to resetting
    for (int a = 0; a < 50 && a < rowCount(QModelIndex()); ++a) {
        getItem(a, true);
    }

    endResetModel();
}

void QuickOpenModel::restart(bool keepFilterText)
{
    // make sure we do not restart recursively which could lead to
    // recursive loading of provider plugins e.g. (happened for the cpp plugin)
    QMetaObject::invokeMethod(this, "restart_internal", Qt::QueuedConnection,
                              Q_ARG(bool, keepFilterText));
}

void QuickOpenModel::restart_internal(bool keepFilterText)
{
    if (!keepFilterText) {
        m_filterText.clear();
    }

    bool anyEnabled = std::any_of(m_providers.constBegin(), m_providers.constEnd(), [](const ProviderEntry& e) {
        return e.enabled;
    });

    if (!anyEnabled) {
        return;
    }

    for (const ProviderEntry& provider : qAsConst(m_providers)) {
        if (!qobject_cast<QuickOpenFileSetInterface*>(provider.provider)) {
            continue;
        }

        ///Always reset providers that implement QuickOpenFileSetInterface and have some matchign scopes, because they may be needed by other providers.
        if (m_enabledScopes.isEmpty() || !(m_enabledScopes & provider.scopes).isEmpty()) {
            provider.provider->reset();
        }
    }

    for (const ProviderEntry& provider : qAsConst(m_providers)) {
        if (qobject_cast<QuickOpenFileSetInterface*>(provider.provider)) {
            continue;
        }

        if (provider.enabled && provider.provider) {
            provider.provider->reset();
        }
    }

    if (keepFilterText) {
        textChanged(m_filterText);
    } else {
        beginResetModel();
        m_cachedData.clear();
        clearExpanding();
        endResetModel();
    }
}

void QuickOpenModel::destroyed(QObject* obj)
{
    removeProvider(static_cast<KDevelop::QuickOpenDataProviderBase*>(obj));
}

QModelIndex QuickOpenModel::index(int row, int column, const QModelIndex& /*parent*/) const
{
    if (column >= columnCount() || row >= rowCount(QModelIndex())) {
        return QModelIndex();
    }
    if (row < 0 || column < 0) {
        return QModelIndex();
    }
    return createIndex(row, column);
}

QModelIndex QuickOpenModel::parent(const QModelIndex&) const
{
    return QModelIndex();
}

int QuickOpenModel::rowCount(const QModelIndex& i) const
{
    if (i.isValid()) {
        return 0;
    }

    int count = 0;
    for (const ProviderEntry& provider : m_providers) {
        if (provider.enabled) {
            count += provider.provider->itemCount();
        }
    }

    return count;
}

int QuickOpenModel::unfilteredRowCount() const
{
    int count = 0;
    for (const ProviderEntry& provider : m_providers) {
        if (provider.enabled) {
            count += provider.provider->unfilteredItemCount();
        }
    }

    return count;
}

int QuickOpenModel::columnCount() const
{
    return 2;
}

int QuickOpenModel::columnCount(const QModelIndex& index) const
{
    if (index.parent().isValid()) {
        return 0;
    } else {
        return columnCount();
    }
}

QVariant QuickOpenModel::data(const QModelIndex& index, int role) const
{
    QuickOpenDataPointer d = getItem(index.row());

    if (!d) {
        return QVariant();
    }

    switch (role) {
    case KTextEditor::CodeCompletionModel::ItemSelected: {
        QString desc = d->htmlDescription();
        if (desc.isEmpty()) {
            return QVariant();
        } else {
            return desc;
        }
    }

    case KTextEditor::CodeCompletionModel::IsExpandable:
        return d->isExpandable();
    case KTextEditor::CodeCompletionModel::ExpandingWidget: {
        QVariant v;
        QWidget* w =  d->expandingWidget();
        if (w && m_expandingWidgetHeightIncrease) {
            w->resize(w->width(), w->height() + m_expandingWidgetHeightIncrease);
        }

        v.setValue<QWidget*>(w);
        return v;
    }
    case ExpandingTree::ProjectPathRole:
        // TODO: put this into the QuickOpenDataBase API
        //       we cannot do this in 5.0, cannot change ABI
        if (auto projectFile = dynamic_cast<const ProjectFileData*>(d.constData())) {
            return QVariant::fromValue(projectFile->projectPath());
        } else if (auto duchainItem = dynamic_cast<const DUChainItemData*>(d.constData())) {
            return QVariant::fromValue(duchainItem->projectPath());
        }
    }

    if (index.column() == 1) { //This column contains the actual content
        switch (role) {
        case Qt::DecorationRole:
            return d->icon();

        case Qt::DisplayRole:
            return d->text();
        case KTextEditor::CodeCompletionModel::HighlightingMethod:
            return KTextEditor::CodeCompletionModel::CustomHighlighting;
        case KTextEditor::CodeCompletionModel::CustomHighlight:
            return d->highlighting();
        }
    } else if (index.column() == 0) { //This column only contains the expanded/not expanded icon
        switch (role) {
        case Qt::DecorationRole:
        {
            if (isExpandable(index)) {
                //Show the expanded/unexpanded handles
                if (isExpanded(index)) {
                    return QIcon::fromTheme(QStringLiteral("arrow-down"));
                } else {
                    return QIcon::fromTheme(QStringLiteral("arrow-right"));
                }
            }
        }
        }
    }

    return ExpandingWidgetModel::data(index, role);
}

void QuickOpenModel::resetTimer()
{
    int currentRow = treeView() ? mapToSource(treeView()->currentIndex()).row() : -1;

    beginResetModel();
    //Remove all cached data behind row m_resetBehindRow
    for (DataList::iterator it = m_cachedData.begin(); it != m_cachedData.end(); ) {
        if (it.key() > m_resetBehindRow) {
            it = m_cachedData.erase(it);
        } else {
            ++it;
        }
    }

    endResetModel();

    if (currentRow != -1) {
        treeView()->setCurrentIndex(mapFromSource(index(currentRow, 0, QModelIndex()))); //Preserve the current index
    }
    m_resetBehindRow = 0;
}

QuickOpenDataPointer QuickOpenModel::getItem(int row, bool noReset) const
{
    ///@todo mix all the models alphabetically here. For now, they are simply ordered.
    ///@todo Deal with unexpected item-counts, like for example in the case of overloaded function-declarations

#ifdef QUICKOPEN_USE_ITEM_CACHING
    const auto dataIt = m_cachedData.constFind(row);
    if (dataIt != m_cachedData.constEnd()) {
        return *dataIt;
    }
#endif
    int rowOffset = 0;

    Q_ASSERT(row < rowCount(QModelIndex()));

    for (const ProviderEntry& provider : m_providers) {
        if (!provider.enabled) {
            continue;
        }
        uint itemCount = provider.provider->itemCount();
        if (( uint )row < itemCount) {
            QuickOpenDataPointer item = provider.provider->data(row);

            if (!noReset && provider.provider->itemCount() != itemCount) {
                qCDebug(PLUGIN_QUICKOPEN) << "item-count in provider has changed, resetting model";
                m_resetTimer->start();
                m_resetBehindRow = rowOffset + row; //Don't reset everything, only everything behind this position
            }

#ifdef QUICKOPEN_USE_ITEM_CACHING
            m_cachedData[row + rowOffset] = item;
#endif
            return item;
        } else {
            row -= provider.provider->itemCount();
            rowOffset += provider.provider->itemCount();
        }
    }

//   qWarning() << "No item for row " <<  row;

    return QuickOpenDataPointer();
}

QSet<IndexedString> QuickOpenModel::fileSet() const
{
    QSet<IndexedString> merged;
    for (const ProviderEntry& provider : m_providers) {
        if (m_enabledScopes.isEmpty() || !(m_enabledScopes & provider.scopes).isEmpty()) {
            if (auto* iface = qobject_cast<QuickOpenFileSetInterface*>(provider.provider)) {
                QSet<IndexedString> ifiles = iface->files();
                //qCDebug(PLUGIN_QUICKOPEN) << "got file-list with" << ifiles.count() << "entries from data-provider" << typeid(*iface).name();
                merged += ifiles;
            }
        }
    }

    return merged;
}

QTreeView* QuickOpenModel::treeView() const
{
    return m_treeView;
}

bool QuickOpenModel::indexIsItem(const QModelIndex& index) const
{
    Q_ASSERT(index.model() == this);
    Q_UNUSED(index);
    return true;
}

void QuickOpenModel::setTreeView(QTreeView* view)
{
    m_treeView = view;
}

int QuickOpenModel::contextMatchQuality(const QModelIndex& /*index*/) const
{
    return -1;
}

bool QuickOpenModel::execute(const QModelIndex& index, QString& filterText)
{
    qCDebug(PLUGIN_QUICKOPEN) << "executing model";
    if (!index.isValid()) {
        qCWarning(PLUGIN_QUICKOPEN) << "Invalid index executed";
        return false;
    }

    QuickOpenDataPointer item = getItem(index.row());

    if (item) {
        return item->execute(filterText);
    } else {
        qCWarning(PLUGIN_QUICKOPEN) << "Got no item for row " << index.row() << " ";
    }

    return false;
}