1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt Creator.
7 **
8 ** Commercial License Usage
9 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** accordance with the commercial license agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and The Qt Company. For licensing terms
13 ** and conditions see https://www.qt.io/terms-conditions. For further
14 ** information use the contact form at https://www.qt.io/contact-us.
15 **
16 ** GNU General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU
18 ** General Public License version 3 as published by the Free Software
19 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
20 ** included in the packaging of this file. Please review the following
21 ** information to ensure the GNU General Public License requirements will
22 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 **
24 ****************************************************************************/
25 
26 #pragma once
27 
28 #include "diffeditor_global.h"
29 #include "diffutils.h"
30 
31 #include <QObject>
32 
QT_FORWARD_DECLARE_CLASS(QMenu)33 QT_FORWARD_DECLARE_CLASS(QMenu)
34 
35 namespace Core { class IDocument; }
36 
37 namespace DiffEditor {
38 
39 namespace Internal { class DiffEditorDocument; }
40 
41 class ChunkSelection;
42 
43 class DIFFEDITOR_EXPORT DiffEditorController : public QObject
44 {
45     Q_OBJECT
46 public:
47     explicit DiffEditorController(Core::IDocument *document);
48 
49     void requestReload();
50     bool isReloading() const;
51 
52     QString baseDirectory() const;
53     void setBaseDirectory(const QString &directory);
54     int contextLineCount() const;
55     bool ignoreWhitespace() const;
56 
57     enum PatchOption {
58         NoOption = 0,
59         Revert = 1,
60         AddPrefix = 2
61     };
62     Q_DECLARE_FLAGS(PatchOptions, PatchOption)
63     QString makePatch(int fileIndex, int chunkIndex, const ChunkSelection &selection,
64                       PatchOptions options) const;
65 
66     static Core::IDocument *findOrCreateDocument(const QString &vcsId,
67                                                  const QString &displayName);
68     static DiffEditorController *controller(Core::IDocument *document);
69 
70     void requestChunkActions(QMenu *menu, int fileIndex, int chunkIndex,
71                              const ChunkSelection &selection);
72     bool chunkExists(int fileIndex, int chunkIndex) const;
73     Core::IDocument *document() const;
74 
75     // reloadFinished() should be called inside the reloader (for synchronous reload)
76     // or later (for asynchronous reload)
77     void setReloader(const std::function<void ()> &reloader);
78 
79 signals:
80     void chunkActionsRequested(QMenu *menu, int fileIndex, int chunkIndex,
81                                const ChunkSelection &selection);
82 
83 protected:
84     void reloadFinished(bool success);
85 
86     void setDiffFiles(const QList<FileData> &diffFileList,
87                       const QString &baseDirectory = QString(),
88                       const QString &startupFile = QString());
89     void setDescription(const QString &description);
90     QString description() const;
91     void forceContextLineCount(int lines);
92 
93 private:
94     Internal::DiffEditorDocument *const m_document;
95     bool m_isReloading = false;
96     std::function<void()> m_reloader;
97 
98     friend class Internal::DiffEditorDocument;
99 };
100 
101 Q_DECLARE_OPERATORS_FOR_FLAGS(DiffEditorController::PatchOptions)
102 
103 } // namespace DiffEditor
104