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 #include "diffeditorconstants.h"
27 #include "diffeditorcontroller.h"
28 #include "diffeditordocument.h"
29
30 #include <coreplugin/editormanager/editormanager.h>
31 #include <coreplugin/editormanager/ieditor.h>
32 #include <coreplugin/icore.h>
33
34 #include <utils/qtcassert.h>
35
36 #include <QStringList>
37
38 namespace DiffEditor {
39
DiffEditorController(Core::IDocument * document)40 DiffEditorController::DiffEditorController(Core::IDocument *document) :
41 QObject(document),
42 m_document(qobject_cast<Internal::DiffEditorDocument *>(document))
43 {
44 QTC_ASSERT(m_document, return);
45 m_document->setController(this);
46 }
47
isReloading() const48 bool DiffEditorController::isReloading() const
49 {
50 return m_isReloading;
51 }
52
baseDirectory() const53 QString DiffEditorController::baseDirectory() const
54 {
55 return m_document->baseDirectory();
56 }
57
setBaseDirectory(const QString & directory)58 void DiffEditorController::setBaseDirectory(const QString &directory)
59 {
60 m_document->setBaseDirectory(directory);
61 }
62
contextLineCount() const63 int DiffEditorController::contextLineCount() const
64 {
65 return m_document->contextLineCount();
66 }
67
ignoreWhitespace() const68 bool DiffEditorController::ignoreWhitespace() const
69 {
70 return m_document->ignoreWhitespace();
71 }
72
makePatch(int fileIndex,int chunkIndex,const ChunkSelection & selection,PatchOptions options) const73 QString DiffEditorController::makePatch(int fileIndex, int chunkIndex,
74 const ChunkSelection &selection,
75 PatchOptions options) const
76 {
77 return m_document->makePatch(fileIndex, chunkIndex, selection,
78 options & Revert, options & AddPrefix);
79 }
80
findOrCreateDocument(const QString & vcsId,const QString & displayName)81 Core::IDocument *DiffEditorController::findOrCreateDocument(const QString &vcsId,
82 const QString &displayName)
83 {
84 QString preferredDisplayName = displayName;
85 Core::IEditor *editor = Core::EditorManager::openEditorWithContents(
86 Constants::DIFF_EDITOR_ID, &preferredDisplayName, QByteArray(), vcsId);
87 return editor ? editor->document() : nullptr;
88 }
89
controller(Core::IDocument * document)90 DiffEditorController *DiffEditorController::controller(Core::IDocument *document)
91 {
92 auto doc = qobject_cast<Internal::DiffEditorDocument *>(document);
93 return doc ? doc->controller() : nullptr;
94 }
95
setDiffFiles(const QList<FileData> & diffFileList,const QString & workingDirectory,const QString & startupFile)96 void DiffEditorController::setDiffFiles(const QList<FileData> &diffFileList,
97 const QString &workingDirectory,
98 const QString &startupFile)
99 {
100 m_document->setDiffFiles(diffFileList, workingDirectory, startupFile);
101 }
102
setDescription(const QString & description)103 void DiffEditorController::setDescription(const QString &description)
104 {
105 m_document->setDescription(description);
106 }
107
description() const108 QString DiffEditorController::description() const
109 {
110 return m_document->description();
111 }
112
113 /**
114 * @brief Force the lines of context to the given number.
115 *
116 * The user will not be able to change the context lines anymore. This needs to be set before
117 * starting any operation or the flag will be ignored by the UI.
118 *
119 * @param lines Lines of context to display.
120 */
forceContextLineCount(int lines)121 void DiffEditorController::forceContextLineCount(int lines)
122 {
123 m_document->forceContextLineCount(lines);
124 }
125
setReloader(const std::function<void ()> & reloader)126 void DiffEditorController::setReloader(const std::function<void ()> &reloader)
127 {
128 m_reloader = reloader;
129 }
130
document() const131 Core::IDocument *DiffEditorController::document() const
132 {
133 return m_document;
134 }
135
136 /**
137 * @brief Request the diff data to be re-read.
138 */
requestReload()139 void DiffEditorController::requestReload()
140 {
141 m_isReloading = true;
142 m_document->beginReload();
143 QTC_ASSERT(m_reloader, reloadFinished(false); return);
144 m_reloader();
145 }
146
reloadFinished(bool success)147 void DiffEditorController::reloadFinished(bool success)
148 {
149 m_document->endReload(success);
150 m_isReloading = false;
151 }
152
requestChunkActions(QMenu * menu,int fileIndex,int chunkIndex,const ChunkSelection & selection)153 void DiffEditorController::requestChunkActions(QMenu *menu, int fileIndex, int chunkIndex,
154 const ChunkSelection &selection)
155 {
156 emit chunkActionsRequested(menu, fileIndex, chunkIndex, selection);
157 }
158
chunkExists(int fileIndex,int chunkIndex) const159 bool DiffEditorController::chunkExists(int fileIndex, int chunkIndex) const
160 {
161 if (!m_document)
162 return false;
163
164 if (fileIndex < 0 || chunkIndex < 0)
165 return false;
166
167 if (fileIndex >= m_document->diffFiles().count())
168 return false;
169
170 const FileData fileData = m_document->diffFiles().at(fileIndex);
171 if (chunkIndex >= fileData.chunks.count())
172 return false;
173
174 return true;
175 }
176
177 } // namespace DiffEditor
178