katequickopenmodel.cpp 4.17 KB
Newer Older
Christoph Cullmann's avatar
Christoph Cullmann committed
1
2
/*  SPDX-License-Identifier: LGPL-2.0-or-later

3
4
5
    SPDX-FileCopyrightText: 2018 Tomaz Canabrava <tcanabrava@kde.org>

    SPDX-License-Identifier: LGPL-2.0-or-later
6
7
8
9
*/

#include "katequickopenmodel.h"

10
#include "kateapp.h"
11
#include "kateviewmanager.h"
12
#include "katemainwindow.h"
13
14
15
16

#include <ktexteditor/document.h>
#include <ktexteditor/view.h>

17
18
19
KateQuickOpenModel::KateQuickOpenModel(KateMainWindow *mainWindow, QObject *parent)
    : QAbstractTableModel(parent)
    , m_mainWindow(mainWindow)
20
21
22
{
}

23
int KateQuickOpenModel::rowCount(const QModelIndex &parent) const
24
25
26
27
28
29
30
{
    if (parent.isValid()) {
        return 0;
    }
    return m_modelEntries.size();
}

31
int KateQuickOpenModel::columnCount(const QModelIndex &parent) const
32
33
34
35
36
{
    Q_UNUSED(parent);
    return 2;
}

37
QVariant KateQuickOpenModel::data(const QModelIndex &idx, int role) const
38
{
39
    if (!idx.isValid()) {
40
41
42
        return {};
    }

43
    if (role != Qt::DisplayRole && role != Qt::FontRole && role != Qt::UserRole && role != Role::Score) {
44
45
46
47
48
        return {};
    }

    auto entry = m_modelEntries.at(idx.row());
    if (role == Qt::DisplayRole) {
49
        switch (idx.column()) {
50
51
52
53
        case Columns::FileName:
            return entry.fileName;
        case Columns::FilePath:
            return entry.filePath;
54
55
56
57
58
59
60
        }
    } else if (role == Qt::FontRole) {
        if (entry.bold) {
            QFont font;
            font.setBold(true);
            return font;
        }
61
62
    } else if (role == Qt::UserRole) {
        return entry.url;
63
64
    } else if (role == Role::Score) {
        return entry.score;
65
66
67
68
69
70
71
72
73
74
    }

    return {};
}

void KateQuickOpenModel::refresh()
{
    QObject *projectView = m_mainWindow->pluginView(QStringLiteral("kateprojectplugin"));
    const QList<KTextEditor::View *> sortedViews = m_mainWindow->viewManager()->sortedViews();
    const QList<KTextEditor::Document *> openDocs = KateApp::self()->documentManager()->documentList();
75
    const QStringList projectDocs = projectView ? (m_listMode == CurrentProject ? projectView->property("projectFiles") : projectView->property("allProjectsFiles")).toStringList() : QStringList();
Waqar Ahmed's avatar
Waqar Ahmed committed
76
    const QString projectBase = projectView ? (m_listMode == CurrentProject ? projectView->property("projectBaseDir") : projectView->property("allProjectsCommonBaseDir")).toString() + QStringLiteral("/") : QString();
77
78

    QVector<ModelEntry> allDocuments;
79
    allDocuments.reserve(sortedViews.size() + openDocs.size() + projectDocs.size());
80

81
    size_t sort_id = static_cast<size_t>(-1);
82
83
    for (auto *view : qAsConst(sortedViews)) {
        auto doc = view->document();
84
        allDocuments.push_back({doc->url(), doc->documentName(), doc->url().toDisplayString(QUrl::NormalizePathSegments | QUrl::PreferLocalFile).remove(projectBase), true, sort_id--, -1});
85
86
87
    }

    for (auto *doc : qAsConst(openDocs)) {
88
        const auto normalizedUrl = doc->url().toString(QUrl::NormalizePathSegments | QUrl::PreferLocalFile).remove(projectBase);
89
        allDocuments.push_back({doc->url(), doc->documentName(), normalizedUrl, true, 0, -1});
90
91
    }

92
    for (const auto &file : qAsConst(projectDocs)) {
93
        QFileInfo fi(file);
94
        const auto localFile = QUrl::fromLocalFile(fi.absoluteFilePath());
95
        allDocuments.push_back({localFile, fi.fileName(), fi.filePath().remove(projectBase), false, 0, -1});
96
97
    }

98
    /** Sort the arrays by filePath. */
99
    std::stable_sort(std::begin(allDocuments), std::end(allDocuments), [](const ModelEntry &a, const ModelEntry &b) { return a.filePath < b.filePath; });
100

101
    /** remove Duplicates.
102
103
104
105
     * Note that the stable_sort above guarantees that the items that the
     * bold/sort_id fields of the items added first are correctly preserved.
     */
    allDocuments.erase(std::unique(allDocuments.begin(), allDocuments.end(), [](const ModelEntry &a, const ModelEntry &b) { return a.filePath == b.filePath; }), std::end(allDocuments));
106
107

    /** sort the arrays via boldness (open or not */
108
    std::stable_sort(std::begin(allDocuments), std::end(allDocuments), [](const ModelEntry &a, const ModelEntry &b) {
109
110
        if (a.bold == b.bold)
            return a.sort_id > b.sort_id;
111
112
113
114
115
116
117
        return a.bold > b.bold;
    });

    beginResetModel();
    m_modelEntries = allDocuments;
    endResetModel();
}