katequickopenmodel.cpp 3.63 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 "katemainwindow.h"
12
#include "kateviewmanager.h"
13
14
15
16

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

17
#include <QFileInfo>
18
19
#include <QMimeDatabase>

20
21
22
KateQuickOpenModel::KateQuickOpenModel(KateMainWindow *mainWindow, QObject *parent)
    : QAbstractTableModel(parent)
    , m_mainWindow(mainWindow)
23
24
25
{
}

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

34
int KateQuickOpenModel::columnCount(const QModelIndex &parent) const
35
36
{
    Q_UNUSED(parent);
Waqar Ahmed's avatar
Waqar Ahmed committed
37
    return 1;
38
39
}

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

46
    const ModelEntry &entry = m_modelEntries.at(idx.row());
Waqar Ahmed's avatar
Waqar Ahmed committed
47
48
49
50
51
52
53
54
    if (role == Role::FileName) {
        return entry.fileName;
    } else if (role == Role::FilePath) {
        return entry.filePath;
    } else if (role == Qt::DisplayRole) {
        // Shouldn't ask for displayrole
        Q_ASSERT(false);
        return {};
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::DecorationRole) {
        return QIcon::fromTheme(QMimeDatabase().mimeTypeForFile(entry.fileName, QMimeDatabase::MatchExtension).iconName());
63
64
    } else if (role == Qt::UserRole) {
        return entry.url;
65
66
    } else if (role == Role::Score) {
        return entry.score;
67
68
69
70
71
72
73
74
75
    }

    return {};
}

void KateQuickOpenModel::refresh()
{
    QObject *projectView = m_mainWindow->pluginView(QStringLiteral("kateprojectplugin"));
    const QList<KTextEditor::View *> sortedViews = m_mainWindow->viewManager()->sortedViews();
Alexander Lohnau's avatar
Alexander Lohnau committed
76
77
78
    const QStringList projectDocs = projectView
        ? (m_listMode == CurrentProject ? projectView->property("projectFiles") : projectView->property("allProjectsFiles")).toStringList()
        : QStringList();
Waqar Ahmed's avatar
Waqar Ahmed committed
79
    const QString projectBase = [this, projectView]() -> QString {
80
        if (!projectView) {
Waqar Ahmed's avatar
Waqar Ahmed committed
81
            return QString();
82
        }
Waqar Ahmed's avatar
Waqar Ahmed committed
83
84
85
86
        QString ret;
        if (m_listMode == CurrentProject) {
            ret = projectView->property("projectBaseDir").toString();
        } else {
87
            ret = projectView->property("allProjectsCommonBaseDir").toString();
Waqar Ahmed's avatar
Waqar Ahmed committed
88
        }
89
        if (!ret.endsWith(QLatin1Char('/'))) {
Waqar Ahmed's avatar
Waqar Ahmed committed
90
            ret.append(QLatin1Char('/'));
91
        }
Waqar Ahmed's avatar
Waqar Ahmed committed
92
93
        return ret;
    }();
94
95

    QVector<ModelEntry> allDocuments;
96
97
98
99
    allDocuments.reserve(sortedViews.size() + projectDocs.size());

    QVector<QUrl> openedDocUrls;
    openedDocUrls.reserve(sortedViews.size());
100

101
    size_t sort_id = static_cast<size_t>(-1);
102
103
    for (auto *view : qAsConst(sortedViews)) {
        auto doc = view->document();
104
105
106
107
        const auto url = doc->url();
        allDocuments.push_back(
            {url, doc->documentName(), url.toDisplayString(QUrl::NormalizePathSegments | QUrl::PreferLocalFile).remove(projectBase), true, sort_id--, -1});
        openedDocUrls.push_back(url);
108
109
    }

110
    for (const auto &file : qAsConst(projectDocs)) {
111
        QFileInfo fi(file);
112
        const auto localFile = QUrl::fromLocalFile(fi.absoluteFilePath());
113
114
115
        if (openedDocUrls.contains(localFile)) {
            continue;
        }
116
        allDocuments.push_back({localFile, fi.fileName(), fi.filePath().remove(projectBase), false, 0, -1});
117
118
119
    }

    beginResetModel();
120
    m_modelEntries = std::move(allDocuments);
121
122
    endResetModel();
}