1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights. These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #ifndef VCSBASE_BASEEDITOR_H
35 #define VCSBASE_BASEEDITOR_H
37 #include "vcsbase_global.h"
39 #include <texteditor/basetexteditor.h>
41 #include <QtCore/QSet>
50 class IVersionControl;
55 struct VCSBaseEditorPrivate;
56 class DiffHighlighter;
57 class BaseAnnotationHighlighter;
59 // Contents of a VCSBaseEditor and its interaction.
60 enum EditorContentType {
61 // No special handling.
63 // Log of a file under revision control. Provide 'click on change'
64 // description and 'Annotate' if is the log of a single file.
66 // <change description>: file line
67 // Color per change number and provide 'click on change' description.
68 // Context menu offers "Annotate previous version".
70 // Diff output. Might includes describe output, which consists of a
71 // header and diffs. Interaction is 'double click in hunk' which
76 // Helper struct used to parametrize an editor with mime type, context
77 // and id. The extension is currently only a suggestion when running
78 // VCS commands with redirection.
79 struct VCSBASE_EXPORT VCSBaseEditorParameters {
80 EditorContentType type;
82 const char *displayName;
85 const char *extension;
88 // Base class for editors showing version control system output
89 // of the type enumerated by EditorContentType.
90 // The source property should contain the file or directory the log
91 // refers to and will be emitted with describeRequested().
92 // This is for VCS that need a current directory.
93 class VCSBASE_EXPORT VCSBaseEditor : public TextEditor::BaseTextEditor
95 Q_PROPERTY(QString source READ source WRITE setSource)
96 Q_PROPERTY(QString diffBaseDirectory READ diffBaseDirectory WRITE setDiffBaseDirectory)
97 Q_PROPERTY(QTextCodec *codec READ codec WRITE setCodec)
98 Q_PROPERTY(QString annotateRevisionTextFormat READ annotateRevisionTextFormat WRITE setAnnotateRevisionTextFormat)
99 Q_PROPERTY(QString copyRevisionTextFormat READ copyRevisionTextFormat WRITE setCopyRevisionTextFormat)
100 Q_PROPERTY(bool isFileLogAnnotateEnabled READ isFileLogAnnotateEnabled WRITE setFileLogAnnotateEnabled)
103 // Initialization requires calling init() (which in turns calls
104 // virtual functions).
105 explicit VCSBaseEditor(const VCSBaseEditorParameters *type,
110 virtual ~VCSBaseEditor();
112 /* Force read-only: Make it a read-only, temporary file.
113 * Should be set to true by version control views. It is not on
114 * by default since it should not trigger when patches are opened as
116 void setForceReadOnly(bool b);
117 bool isForceReadOnly() const;
119 QString source() const;
120 void setSource(const QString &source);
122 // Format for "Annotate" revision menu entries. Should contain '%1" placeholder
123 QString annotateRevisionTextFormat() const;
124 void setAnnotateRevisionTextFormat(const QString &);
126 // Format for "Annotate Previous" revision menu entries. Should contain '%1" placeholder.
127 // Defaults to "annotateRevisionTextFormat" if unset.
128 QString annotatePreviousRevisionTextFormat() const;
129 void setAnnotatePreviousRevisionTextFormat(const QString &);
131 // Format for "Copy" revision menu entries. Should contain '%1" placeholder
132 QString copyRevisionTextFormat() const;
133 void setCopyRevisionTextFormat(const QString &);
135 // Enable "Annotate" context menu in file log view
136 // (set to true if the source is a single file and the VCS implements it)
137 bool isFileLogAnnotateEnabled() const;
138 void setFileLogAnnotateEnabled(bool e);
140 QTextCodec *codec() const;
141 void setCodec(QTextCodec *);
143 // Base directory for diff views
144 QString diffBaseDirectory() const;
145 void setDiffBaseDirectory(const QString &d);
147 bool isModified() const;
149 EditorContentType contentType() const;
151 // Utility to find a parameter set by type in an array.
152 static const VCSBaseEditorParameters *
153 findType(const VCSBaseEditorParameters *array, int arraySize, EditorContentType et);
155 // Utility to find the codec for a source (file or directory), querying
156 // the editor manager and the project managers (defaults to system codec).
157 // The codec should be set on editors displaying diff or annotation
159 static QTextCodec *getCodec(const QString &source);
160 static QTextCodec *getCodec(const QString &workingDirectory, const QStringList &files);
162 // Utility to return the editor from the IEditor returned by the editor
163 // manager which is a BaseTextEditable.
164 static VCSBaseEditor *getVcsBaseEditor(const Core::IEditor *editor);
166 // Utility to find the line number of the current editor. Optionally,
167 // pass in the file name to match it. To be used when jumping to current
168 // line number in a 'annnotate current file' slot, which checks if the
169 // current file originates from the current editor or the project selection.
170 static int lineNumberOfCurrentEditor(const QString ¤tFile = QString());
172 //Helper to go to line of editor if it is a text editor
173 static bool gotoLineOfEditor(Core::IEditor *e, int lineNumber);
175 // Convenience functions to determine the source to pass on to a diff
176 // editor if one has a call consisting of working directory and file arguments.
177 // ('git diff XX' -> 'XX' , 'git diff XX file' -> 'XX/file').
178 static QString getSource(const QString &workingDirectory, const QString &fileName);
179 static QString getSource(const QString &workingDirectory, const QStringList &fileNames);
180 // Convenience functions to determine an title/id to identify the editor
181 // from the arguments (','-joined arguments or directory) + revision.
182 static QString getTitleId(const QString &workingDirectory,
183 const QStringList &fileNames,
184 const QString &revision = QString());
186 bool setConfigurationWidget(QWidget *w);
187 QWidget *configurationWidget() const;
190 // These signals also exist in the opaque editable (IEditor) that is
191 // handled by the editor manager for convenience. They are emitted
192 // for LogOutput/AnnotateOutput content types.
193 void describeRequested(const QString &source, const QString &change);
194 void annotateRevisionRequested(const QString &source, const QString &change, int lineNumber);
197 // Convenience slot to set data read from stdout, will use the
198 // documents' codec to decode
199 void setPlainTextData(const QByteArray &data);
202 virtual TextEditor::BaseTextEditorEditable *createEditableInterface();
204 void contextMenuEvent(QContextMenuEvent *e);
205 void mouseMoveEvent(QMouseEvent *e);
206 void mouseReleaseEvent(QMouseEvent *e);
207 void mouseDoubleClickEvent(QMouseEvent *e);
208 void keyPressEvent(QKeyEvent *);
211 void setFontSettings(const TextEditor::FontSettings &);
215 void slotActivateAnnotation();
216 void slotPopulateDiffBrowser();
217 void slotDiffBrowse(int);
218 void slotDiffCursorPositionChanged();
219 void slotAnnotateRevision();
220 void slotCopyRevision();
223 /* A helper that can be used to locate a file in a diff in case it
224 * is relative. Tries to derive the directory from base directory,
225 * source and version control. */
226 QString findDiffFile(const QString &f, Core::IVersionControl *control = 0) const;
229 // Implement to return a set of change identifiers in
231 virtual QSet<QString> annotationChanges() const = 0;
232 // Implement to identify a change number at the cursor position
233 virtual QString changeUnderCursor(const QTextCursor &) const = 0;
234 // Factory functions for highlighters
235 virtual DiffHighlighter *createDiffHighlighter() const = 0;
236 virtual BaseAnnotationHighlighter *createAnnotationHighlighter(const QSet<QString> &changes) const = 0;
237 // Implement to return a local file name from the diff file specification
238 // (text cursor at position above change hunk)
239 virtual QString fileNameFromDiffSpecification(const QTextBlock &diffFileSpec) const = 0;
240 // Implement to return the previous version[s] of an annotation change
241 // for "Annotate previous version"
242 virtual QStringList annotationPreviousVersions(const QString &revision) const;
244 void jumpToChangeFromDiff(QTextCursor cursor);
245 QAction *createDescribeAction(const QString &change);
246 QAction *createAnnotateAction(const QString &change, bool previous = false);
247 QAction *createCopyRevisionAction(const QString &change);
249 VCSBaseEditorPrivate *d;
252 } // namespace VCSBase
254 #endif // VCSBASE_BASEEDITOR_H