File: committoolview.h

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 (290 lines) | stat: -rw-r--r-- 8,572 bytes parent folder | download | duplicates (3)
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
/*
    SPDX-FileCopyrightText: 2020 Jonathan L. Verner <jonathan.verner@matfyz.cz>

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

#ifndef KDEVPLATFORM_PLUGIN_COMMIT_TOOLVIEW_H
#define KDEVPLATFORM_PLUGIN_COMMIT_TOOLVIEW_H

#include "repostatusmodel.h"

#include <interfaces/iuicontroller.h>

#include <KTextEditor/Attribute>

#include <QWidget>

class ActiveStyledDelegate;
class DiffViewsCtrl;
class FilterEmptyItemsProxyModel;
class RepoStatusModel;
class SimpleCommitForm;

class QAction;
class QMenu;
class QModelIndex;
class QLineEdit;
class QTreeView;
class QAbstractProxyModel;
class QUrl;

namespace KDevelop {
    class IBasicVersionControl;
    class IDocument;
    class IProject;
    class VcsJob;
}

namespace KTextEditor {
    class View;
    class Document;
}

/**
 * This implements the git-cola like toolview for preparing commits.
 *
 * The view contains a list of projects. Each project contains four
 * lists:
 *
 *   - the staged changes list lists all files in the project which
 *     have changes staged for commit;
 *   - the unstaged changes list lists all files in the project which
 *     have changes which are not currently staged for commit;
 *   - the conflicts list lists all files which have unresolved (merge)
 *     conflicts; and
 *   - the untracked list which lists all files not tracked in the VCS
 *
 * Clicking on a file in one of the staged/unstaged lists opens a document
 * tab with the diff showing the changes. The user can then select lines/hunks
 * from the diff and remove/add them from the staged changes using the context menu.
 *
 * Double clicking on a file will, instead, stage/unstage all changes in the file
 * or mark the conflicts as resolved or add the file to be tracked in VCS.
 *
 * Above these lists a lineedit and a textedit may be used to prepare a
 * commit message. The commit button will commit the staged changes to the
 * repo. If several projects are listed, the one which is expanded will be
 * used (only one project is allowed to be expaned to show the lists at a time,
 * an expaned project is automatically collapsed when a different one is expanded).
 *
 * @author Jonathan L. Verner <jonathan.verner@matfyz.cz>
 */

class CommitToolView : public QWidget
{
    Q_OBJECT

public:
    enum ShowDiffParams { Activate, NoActivate };

    /**
     * @note: m_statusmodel remains the property of the caller whose
     * responsibility is to delete it (and care must be taken not
     * to delete it before the CommitToolView is deleted)
     */
    CommitToolView(QWidget* parent, RepoStatusModel* m_statusmodel);
    ~CommitToolView() override;

    /**
     * @returns the currently active project (i.e. the one that
     *          is expanded in the treeview)
     */
    KDevelop::IProject* activeProject() const;

    /**
     * @returns the index of the currently active project (i.e. the one that
     *          is expanded in the treeview)
     */
    QStandardItem* activeProjectItem() const;

    /**
     * @returns true if the item pointed to by the repostatusmodel index
     *          idx is the root item of the currently active project.
     */
    bool isActiveProject(const QModelIndex& idx) const;

Q_SIGNALS:

    /**
     * This signal is emitted when the view wants to show a diff
     *
     * @param url the url to display the changes for
     * @param area the type of changes to display
     */
    void showDiff(const QUrl& url, const RepoStatusModel::Areas area);

    /**
     * This signal is emitted when the view wants to show a file
     *
     * @param url the url of the file to show
     */
    void showSource(const QUrl& url);

    /**
     * This signal is emitted when the diff showing changes of type @param area
     * to the file @param url needs to be updated.
     */
    void updateDiff(const QUrl& url, const RepoStatusModel::Areas area);

    /**
     * This signal is emitted when all diffs showing changes to files in
     * project @param project need to be updated.
     */
    void updateProjectDiffs(KDevelop::IProject* project);

    /**
     * This signal is emitted when all diffs showing changes to the file
     * @param url need to be updated.
     *
     * @note: In contrast to the updateDiff signal, this also includes diffs
     * showing all changes to the owning project (staged/unstaged)
     */
    void updateUrlDiffs(const QUrl& url);

public Q_SLOTS:
    /**
     * Shows the toolview context menu
     */
    void popupContextMenu(const QPoint& pos);

    /**
     * A handler called when the user double clicks
     * an item in the treeview.
     */
    void dblClicked(const QModelIndex& idx);

    /**
     * A handler called when the user clicks an item
     * in the treeview.
     */
    void clicked(const QModelIndex& idx);

    /**
     * A handler called when a user expands an item
     * in the treeview.
     */
    void activateProject(const QModelIndex& idx);

    /**
     * Stages the staged changes in the given files.
     *
     * @param urls the list of files whose changes to stage
     */
    void stageSelectedFiles(const QList<QUrl>& urls);

    /**
     * Unstages the staged changes in the given files.
     *
     * @param urls the list of files whose changes to unstage
     */
    void unstageSelectedFiles(const QList<QUrl>& urls);

    /**
     * Reverts the uncommited changes in the given files.
     *
     * @param urls the list of files whose changes to revert
     *
     * @note: This is an irreversible and dangerous action,
     * a confirmation dialog is shown before it is applied
     */
    void revertSelectedFiles(const QList<QUrl>& urls);

    /**
     * Runs git commit on the staged changes. The commit message
     * is constructed from the data in the commit form.
     *
     * @note This function assumes that there are some staged changes.
     * @note The extended description of the commit is wrapped at 70 columns
     */
    void commitActiveProject();

private:

    /* Describes an action on selected lines/hunk in a diff */
    enum ApplyAction {
        Stage,
        Unstage,
        Revert,
    };

    /**
     * Updates the toolview layout based on the dock area position:
     *
     * When the toolview is placed on the left/right, all the widgets
     * sit on top of each other; when it is placed on the top/bottom,
     * the commit area (commit header, button, description textedit)
     * will sit to the left of the changes view with the search filter.
     */

    void doLayOut(const Qt::DockWidgetArea area);

    /**
     * A helper function which return the VCS plugin which
     * handles `url`.
     *
     * @param url the url for which the plugin is returned
     *
     * @note: Returns nullptr if no project/VCS plugin for the
     * given url exists.
     */
    KDevelop::IBasicVersionControl* vcsPluginForUrl(const QUrl& url) const;

    /**
     * The model which lists the projects and staged/modified/... files
     * which are shown in the treeview.
     */
    RepoStatusModel* m_statusmodel;

    /**
     * The filtered repostatus model
     */
    FilterEmptyItemsProxyModel* m_proxymodel;

    /** The form for composing the commit message and doing the commit. */
    SimpleCommitForm* m_commitForm = nullptr;

    /** The treeview listing the projects & their staged/modified/... files */
    QTreeView* m_view = nullptr;

    /** The lineedit for filtering the treeview */
    QLineEdit* m_filter = nullptr;

    /****************************************
     * Various contextmenus & their actions *
     ****************************************/

    QMenu
        /** Menu with a single "Refresh" action (shown for projects in the toolview) */
          *m_refreshMenu
        /** Menu with stage/unstage/revert actions (shown for files in the toolview) */
        , *m_toolviewMenu
        ;
    QAction *m_refreshModelAct
          , *m_stageFilesAct
          , *m_unstageFilesAct
          , *m_revertFilesAct
          ;

    /** A style delegate for showing the currently selected project in bold */
    ActiveStyledDelegate* m_styleDelegate;
};

/**
 * A factory for creating CommitToolViews.
 */
class CommitToolViewFactory : public KDevelop::IToolViewFactory
{
public:
    explicit CommitToolViewFactory(RepoStatusModel* statusModel);
    ~CommitToolViewFactory();
    QWidget* create(QWidget* parent = nullptr) override;
    Qt::DockWidgetArea defaultPosition() const override;
    QString id() const override;

private:
    RepoStatusModel* m_statusmodel;
    DiffViewsCtrl* m_diffViewsCtrl;
};

#endif