vcsbaseeditor.h 10.4 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2
3
4
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
con's avatar
con committed
8
**
9
**
10
** GNU Lesser General Public License Usage
11
**
hjk's avatar
hjk committed
12
13
14
15
16
17
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the GNU Lesser General
** Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18
**
con's avatar
con committed
19
** In addition, as a special exception, Nokia gives you certain additional
hjk's avatar
hjk committed
20
** rights. These rights are described in the Nokia Qt LGPL Exception
con's avatar
con committed
21
22
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
23
24
25
26
27
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
con's avatar
con committed
28
** If you have questions regarding the use of this file, please contact
29
** Nokia at qt-info@nokia.com.
con's avatar
con committed
30
**
31
**************************************************************************/
hjk's avatar
hjk committed
32
33
34

#ifndef VCSBASE_BASEEDITOR_H
#define VCSBASE_BASEEDITOR_H
con's avatar
con committed
35
36
37
38
39

#include "vcsbase_global.h"

#include <texteditor/basetexteditor.h>

40
#include <QSet>
con's avatar
con committed
41
42
43
44
45
46
47

QT_BEGIN_NAMESPACE
class QAction;
class QTextCodec;
class QTextCursor;
QT_END_NAMESPACE

hjk's avatar
hjk committed
48
namespace Core { class IVersionControl; }
49

hjk's avatar
hjk committed
50
namespace VcsBase {
con's avatar
con committed
51

52
53
54
55
namespace Internal {
class ChangeTextCursorHandler;
class VcsBaseEditorWidgetPrivate;
}
56

con's avatar
con committed
57
58
59
class DiffHighlighter;
class BaseAnnotationHighlighter;

60
// Documentation inside
hjk's avatar
hjk committed
61
62
enum EditorContentType
{
con's avatar
con committed
63
64
65
66
67
68
    RegularCommandOutput,
    LogOutput,
    AnnotateOutput,
    DiffOutput
};

hjk's avatar
hjk committed
69
class VCSBASE_EXPORT VcsBaseEditorParameters
70
{
71
public:
con's avatar
con committed
72
    EditorContentType type;
73
74
    const char *id;
    const char *displayName;
con's avatar
con committed
75
76
77
78
79
    const char *context;
    const char *mimeType;
    const char *extension;
};

80
81
82
83
84
85
86
87
88
89
class VCSBASE_EXPORT DiffChunk
{
public:
    bool isValid() const;
    QByteArray asPatch() const;

    QString fileName;
    QByteArray chunk;
};

hjk's avatar
hjk committed
90
class VCSBASE_EXPORT VcsBaseEditorWidget : public TextEditor::BaseTextEditorWidget
con's avatar
con committed
91
{
Adriaan de Groot's avatar
Adriaan de Groot committed
92
    Q_PROPERTY(QString source READ source WRITE setSource)
93
    Q_PROPERTY(QString diffBaseDirectory READ diffBaseDirectory WRITE setDiffBaseDirectory)
Adriaan de Groot's avatar
Adriaan de Groot committed
94
    Q_PROPERTY(QTextCodec *codec READ codec WRITE setCodec)
95
    Q_PROPERTY(QString annotateRevisionTextFormat READ annotateRevisionTextFormat WRITE setAnnotateRevisionTextFormat)
96
    Q_PROPERTY(QString copyRevisionTextFormat READ copyRevisionTextFormat WRITE setCopyRevisionTextFormat)
97
    Q_PROPERTY(bool isFileLogAnnotateEnabled READ isFileLogAnnotateEnabled WRITE setFileLogAnnotateEnabled)
98
    Q_PROPERTY(bool revertDiffChunkEnabled READ isRevertDiffChunkEnabled WRITE setRevertDiffChunkEnabled)
con's avatar
con committed
99
    Q_OBJECT
100

con's avatar
con committed
101
102
103
protected:
    // Initialization requires calling init() (which in turns calls
    // virtual functions).
hjk's avatar
hjk committed
104
    explicit VcsBaseEditorWidget(const VcsBaseEditorParameters *type,
Tobias Hunger's avatar
Tobias Hunger committed
105
                                 QWidget *parent);
con's avatar
con committed
106
107
108
public:
    void init();

hjk's avatar
hjk committed
109
    ~VcsBaseEditorWidget();
con's avatar
con committed
110

111
112
113
114
115
116
    /* Force read-only: Make it a read-only, temporary file.
     * Should be set to true by version control views. It is not on
     * by default since it should not  trigger when patches are opened as
     * files. */
    void setForceReadOnly(bool b);

con's avatar
con committed
117
118
119
    QString source() const;
    void setSource(const  QString &source);

120
121
122
123
    // Format for "Annotate" revision menu entries. Should contain '%1" placeholder
    QString annotateRevisionTextFormat() const;
    void setAnnotateRevisionTextFormat(const QString &);

124
125
126
127
128
    // Format for "Annotate Previous" revision menu entries. Should contain '%1" placeholder.
    // Defaults to "annotateRevisionTextFormat" if unset.
    QString annotatePreviousRevisionTextFormat() const;
    void setAnnotatePreviousRevisionTextFormat(const QString &);

129
130
131
132
    // Format for "Copy" revision menu entries. Should contain '%1" placeholder
    QString copyRevisionTextFormat() const;
    void setCopyRevisionTextFormat(const QString &);

133
134
135
136
137
    // Enable "Annotate" context menu in file log view
    // (set to true if the source is a single file and the VCS implements it)
    bool isFileLogAnnotateEnabled() const;
    void setFileLogAnnotateEnabled(bool e);

con's avatar
con committed
138
139
140
    QTextCodec *codec() const;
    void setCodec(QTextCodec *);

141
142
143
144
    // Base directory for diff views
    QString diffBaseDirectory() const;
    void setDiffBaseDirectory(const QString &d);

145
146
147
148
    // Diff: Can revert?
    bool isRevertDiffChunkEnabled() const;
    void setRevertDiffChunkEnabled(bool e);

con's avatar
con committed
149
150
151
152
153
    bool isModified() const;

    EditorContentType contentType() const;

    // Utility to find a parameter set by type in an array.
hjk's avatar
hjk committed
154
155
    static  const VcsBaseEditorParameters *
        findType(const VcsBaseEditorParameters *array, int arraySize, EditorContentType et);
con's avatar
con committed
156
157
158
159
160

    // Utility to find the codec for a source (file or directory), querying
    // the editor manager and the project managers (defaults to system codec).
    // The codec should be set on editors displaying diff or annotation
    // output.
161
    static QTextCodec *getCodec(const QString &source);
162
    static QTextCodec *getCodec(const QString &workingDirectory, const QStringList &files);
con's avatar
con committed
163

164
165
    // Utility to return the widget from the IEditor returned by the editor
    // manager which is a BaseTextEditor.
hjk's avatar
hjk committed
166
    static VcsBaseEditorWidget *getVcsBaseEditor(const Core::IEditor *editor);
con's avatar
con committed
167

168
169
170
171
172
173
174
175
176
    // Utility to find the line number of the current editor. Optionally,
    // pass in the file name to match it. To be used when jumping to current
    // line number in a 'annnotate current file' slot, which checks if the
    // current file originates from the current editor or the project selection.
    static int lineNumberOfCurrentEditor(const QString &currentFile = QString());

    //Helper to go to line of editor if it is a text editor
    static bool gotoLineOfEditor(Core::IEditor *e, int lineNumber);

177
178
179
180
    // Convenience functions to determine the source to pass on to a diff
    // editor if one has a call consisting of working directory and file arguments.
    // ('git diff XX' -> 'XX' , 'git diff XX file' -> 'XX/file').
    static QString getSource(const QString &workingDirectory, const QString &fileName);
181
    static QString getSource(const QString &workingDirectory, const QStringList &fileNames);
182
    // Convenience functions to determine an title/id to identify the editor
183
    // from the arguments (','-joined arguments or directory) + revision.
184
185
186
    static QString getTitleId(const QString &workingDirectory,
                              const QStringList &fileNames,
                              const QString &revision = QString());
187
188
189
190

    bool setConfigurationWidget(QWidget *w);
    QWidget *configurationWidget() const;

191
192
193
194
195
196
197
198
    /* Tagging editors: Sometimes, an editor should be re-used, for example, when showing
     * a diff of the same file with different diff-options. In order to be able to find
     * the editor, they get a 'tag' containing type and parameters (dynamic property string). */
    static void tagEditor(Core::IEditor *e, const QString &tag);
    static Core::IEditor* locateEditorByTag(const QString &tag);
    static QString editorTag(EditorContentType t, const QString &workingDirectory, const QStringList &files,
                             const QString &revision = QString());

con's avatar
con committed
199
signals:
200
    // These signals also exist in the opaque editable (IEditor) that is
201
202
    // handled by the editor manager for convenience. They are emitted
    // for LogOutput/AnnotateOutput content types.
con's avatar
con committed
203
    void describeRequested(const QString &source, const QString &change);
204
    void annotateRevisionRequested(const QString &source, const QString &change, int lineNumber);
hjk's avatar
hjk committed
205
206
    void diffChunkApplied(const VcsBase::DiffChunk &dc);
    void diffChunkReverted(const VcsBase::DiffChunk &dc);
con's avatar
con committed
207
208
209
210
211
212
213

public slots:
    // Convenience slot to set data read from stdout, will use the
    // documents' codec to decode
    void setPlainTextData(const QByteArray &data);

protected:
214
    virtual TextEditor::BaseTextEditor *createEditor();
con's avatar
con committed
215
216
217
218
219
220
221
222
223
224
225
226

    void contextMenuEvent(QContextMenuEvent *e);
    void mouseMoveEvent(QMouseEvent *e);
    void mouseReleaseEvent(QMouseEvent *e);
    void mouseDoubleClickEvent(QMouseEvent *e);
    void keyPressEvent(QKeyEvent *);

public slots:
    void setFontSettings(const TextEditor::FontSettings &);

private slots:
    void slotActivateAnnotation();
227
228
229
    void slotPopulateDiffBrowser();
    void slotDiffBrowse(int);
    void slotDiffCursorPositionChanged();
230
    void slotAnnotateRevision();
231
    void slotApplyDiffChunk();
232
    void slotPaste();
con's avatar
con committed
233

234
235
protected:
    /* A helper that can be used to locate a file in a diff in case it
236
237
     * is relative. Tries to derive the directory from base directory,
     * source and version control. */
Tobias Hunger's avatar
Tobias Hunger committed
238
    QString findDiffFile(const QString &f) const;
239

240
    virtual bool canApplyDiffChunk(const DiffChunk &dc) const;
241
    // Revert a patch chunk. Default implementation uses patch.exe
242
    virtual bool applyDiffChunk(const DiffChunk &dc, bool revert = false) const;
243

con's avatar
con committed
244
245
246
247
248
249
250
251
private:
    // Implement to return a set of change identifiers in
    // annotation mode
    virtual QSet<QString> annotationChanges() const = 0;
    // Implement to identify a change number at the cursor position
    virtual QString changeUnderCursor(const QTextCursor &) const = 0;
    // Factory functions for highlighters
    virtual DiffHighlighter *createDiffHighlighter() const = 0;
252
253
    virtual BaseAnnotationHighlighter *createAnnotationHighlighter(const QSet<QString> &changes,
                                                                   const QColor &bg) const = 0;
con's avatar
con committed
254
255
256
    // Implement to return a local file name from the diff file specification
    // (text cursor at position above change hunk)
    virtual QString fileNameFromDiffSpecification(const QTextBlock &diffFileSpec) const = 0;
257
    // Implement to return the previous version[s] of an annotation change
258
259
    // for "Annotate previous version"
    virtual QStringList annotationPreviousVersions(const QString &revision) const;
260
261
    // cut out chunk and determine file name.
    DiffChunk diffChunk(QTextCursor cursor) const;
con's avatar
con committed
262
263
264

    void jumpToChangeFromDiff(QTextCursor cursor);

265
    friend class Internal::ChangeTextCursorHandler;
hjk's avatar
hjk committed
266
    Internal::VcsBaseEditorWidgetPrivate *const d;
con's avatar
con committed
267
268
};

hjk's avatar
hjk committed
269
} // namespace VcsBase
con's avatar
con committed
270

hjk's avatar
hjk committed
271
Q_DECLARE_METATYPE(VcsBase::DiffChunk)
272

hjk's avatar
hjk committed
273
#endif // VCSBASE_BASEEDITOR_H