File: vcschangesview.cpp

package info (click to toggle)
kdevelop 4%3A25.04.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 73,508 kB
  • sloc: cpp: 291,803; python: 4,322; javascript: 3,518; sh: 1,316; ansic: 703; xml: 414; php: 95; lisp: 66; makefile: 31; sed: 12
file content (171 lines) | stat: -rw-r--r-- 5,921 bytes parent folder | download | duplicates (2)
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
/*
    SPDX-FileCopyrightText: 2010 Aleix Pol Gonzalez <aleixpol@kde.org>

    SPDX-License-Identifier: LGPL-2.0-or-later
*/

#include "vcschangesview.h"
#include <interfaces/icore.h>
#include <interfaces/context.h>
#include <interfaces/iproject.h>
#include <interfaces/idocumentcontroller.h>
#include <interfaces/iplugincontroller.h>
#include <interfaces/iprojectcontroller.h>
#include <interfaces/contextmenuextension.h>
#include <vcs/vcsstatusinfo.h>
#include <project/projectchangesmodel.h>
#include <project/projectmodel.h>
#include <project/projectutils.h>

#include <QIcon>
#include <QMenu>
#include <QPointer>

#include <KActionCollection>
#include <KLocalizedString>

#include "vcschangesviewplugin.h"

using namespace KDevelop;

VcsChangesView::VcsChangesView(VcsProjectIntegrationPlugin* plugin, QWidget* parent)
    : QTreeView(parent)
{
    setRootIsDecorated(false);
    setEditTriggers(QAbstractItemView::NoEditTriggers);
    setSelectionMode(ContiguousSelection);
    setContextMenuPolicy(Qt::CustomContextMenu);
    setTextElideMode(Qt::ElideLeft);
    setWordWrap(true);
    setWindowIcon(QIcon::fromTheme(QStringLiteral("exchange-positions"), windowIcon()));
    
    connect(this, &VcsChangesView::customContextMenuRequested, this, &VcsChangesView::popupContextMenu);

    const auto pluginActions = plugin->actionCollection()->actions();
    for (QAction* action : pluginActions) {
        addAction(action);
    }
    
    QAction* action = plugin->actionCollection()->action(QStringLiteral("locate_document"));
    connect(action, &QAction::triggered, this, &VcsChangesView::selectCurrentDocument);
    connect(this, &VcsChangesView::doubleClicked, this, &VcsChangesView::openSelected);
}

static void appendActions(QMenu* menu, const QList<QAction*>& actions)
{
    menu->addSeparator();
    menu->addActions(actions);
}

void VcsChangesView::popupContextMenu( const QPoint &pos )
{
    QList<QUrl> urls;
    QList<IProject*> projects;
    const QModelIndexList selectionIdxs = selectedIndexes();
    if(selectionIdxs.isEmpty())
        return;
    
    for (const QModelIndex& idx : selectionIdxs) {
        if(idx.column()==0) {
            if(idx.parent().isValid())
                urls += idx.data(KDevelop::VcsFileChangesModel::VcsStatusInfoRole).value<VcsStatusInfo>().url();
            else {
                IProject* project = ICore::self()->projectController()->findProjectByName(idx.data(ProjectChangesModel::ProjectNameRole).toString());
                if (project) {
                    projects += project;
                } else {
                    qWarning() << "Couldn't find a project for project: " << idx.data(ProjectChangesModel::ProjectNameRole).toString();
                }
            }
        }
    }

    QPointer<QMenu> menu = new QMenu(this);
    QAction* refreshAction = menu->addAction(QIcon::fromTheme(QStringLiteral("view-refresh")), i18nc("@action:inmenu", "Refresh"));
    QList<ContextMenuExtension> extensions;
    if(!urls.isEmpty()) {
        KDevelop::FileContext context(urls);
        extensions = ICore::self()->pluginController()->queryPluginsForContextMenuExtensions(&context, menu);
    } else {
        QList<ProjectBaseItem*> items;
        items.reserve(projects.size());
        for (IProject* p : std::as_const(projects)) {
            items += p->projectItem();
        }

        KDevelop::ProjectItemContextImpl context(items);
        extensions = ICore::self()->pluginController()->queryPluginsForContextMenuExtensions(&context, menu);
        
    }

    QList<QAction*> buildActions;
    QList<QAction*> vcsActions;
    QList<QAction*> extActions;
    QList<QAction*> projectActions;
    QList<QAction*> fileActions;
    QList<QAction*> runActions;
    for (const ContextMenuExtension& ext : std::as_const(extensions)) {
        buildActions += ext.actions(ContextMenuExtension::BuildGroup);
        fileActions += ext.actions(ContextMenuExtension::FileGroup);
        projectActions += ext.actions(ContextMenuExtension::ProjectGroup);
        vcsActions += ext.actions(ContextMenuExtension::VcsGroup);
        extActions += ext.actions(ContextMenuExtension::ExtensionGroup);
        runActions += ext.actions(ContextMenuExtension::RunGroup);
    }

    appendActions(menu, buildActions);
    appendActions(menu, runActions );
    appendActions(menu, fileActions);
    appendActions(menu, vcsActions);
    appendActions(menu, extActions);
    appendActions(menu, projectActions);

    if ( !menu->isEmpty() ) {
        QAction* res = menu->exec(viewport()->mapToGlobal(pos));
        if(res == refreshAction) {
            if(!urls.isEmpty())
                emit reload(urls);
            else
                emit reload(projects);
        }
    }
    delete menu;
}

void VcsChangesView::selectCurrentDocument()
{
    IDocument* doc = ICore::self()->documentController()->activeDocument();
    if(!doc)
        return;
    
    QUrl url = doc->url();
    IProject* p = ICore::self()->projectController()->findProjectForUrl(url);
    QModelIndex idx = (p ?
        model()->match(model()->index(0, 0), ProjectChangesModel::UrlRole,
                       url, 1, Qt::MatchExactly).value(0) :
        QModelIndex());
    if(idx.isValid()) {
        expand(idx.parent());
        setCurrentIndex(idx);
    } else
        collapseAll();
}

void VcsChangesView::setModel(QAbstractItemModel* model)
{
    connect(model, &QAbstractItemModel::rowsInserted, this, &VcsChangesView::expand);
    QTreeView::setModel(model);
}

void VcsChangesView::openSelected(const QModelIndex& index)
{
    if(!index.parent().isValid()) //then it's a project
        return;
    QModelIndex idx = index.sibling(index.row(), 1);
    VcsStatusInfo info = idx.data(ProjectChangesModel::VcsStatusInfoRole).value<VcsStatusInfo>();
    QUrl url = info.url();
    
    ICore::self()->documentController()->openDocument(url);
}

#include "moc_vcschangesview.cpp"