OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / vcsbase / vcsbaseeditor.h
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
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
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
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.
24 **
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.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #ifndef VCSBASE_BASEEDITOR_H
35 #define VCSBASE_BASEEDITOR_H
36
37 #include "vcsbase_global.h"
38
39 #include <texteditor/basetexteditor.h>
40
41 #include <QtCore/QSet>
42
43 QT_BEGIN_NAMESPACE
44 class QAction;
45 class QTextCodec;
46 class QTextCursor;
47 QT_END_NAMESPACE
48
49 namespace Core {
50     class IVersionControl;
51 }
52
53 namespace VCSBase {
54
55 struct VCSBaseEditorPrivate;
56 class DiffHighlighter;
57 class BaseAnnotationHighlighter;
58
59 // Contents of a VCSBaseEditor and its interaction.
60 enum EditorContentType {
61     // No special handling.
62     RegularCommandOutput,
63     // Log of a file under revision control. Provide  'click on change'
64     // description and 'Annotate' if is the log of a single file.
65     LogOutput,
66     // <change description>: file line
67     // Color per change number and provide 'click on change' description.
68     // Context menu offers "Annotate previous version".
69     AnnotateOutput,
70     // Diff output. Might includes describe output, which consists of a
71     // header and diffs. Interaction is 'double click in  hunk' which
72     // opens the file
73     DiffOutput
74 };
75
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;
81     const char *id;
82     const char *displayName;
83     const char *context;
84     const char *mimeType;
85     const char *extension;
86 };
87
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
94 {
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)
101     Q_OBJECT
102 protected:
103     // Initialization requires calling init() (which in turns calls
104     // virtual functions).
105     explicit VCSBaseEditor(const VCSBaseEditorParameters *type,
106                            QWidget *parent);
107 public:
108     void init();
109
110     virtual ~VCSBaseEditor();
111
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
115      * files. */
116     void setForceReadOnly(bool b);
117     bool isForceReadOnly() const;
118
119     QString source() const;
120     void setSource(const  QString &source);
121
122     // Format for "Annotate" revision menu entries. Should contain '%1" placeholder
123     QString annotateRevisionTextFormat() const;
124     void setAnnotateRevisionTextFormat(const QString &);
125
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 &);
130
131     // Format for "Copy" revision menu entries. Should contain '%1" placeholder
132     QString copyRevisionTextFormat() const;
133     void setCopyRevisionTextFormat(const QString &);
134
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);
139
140     QTextCodec *codec() const;
141     void setCodec(QTextCodec *);
142
143     // Base directory for diff views
144     QString diffBaseDirectory() const;
145     void setDiffBaseDirectory(const QString &d);
146
147     bool isModified() const;
148
149     EditorContentType contentType() const;
150
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);
154
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
158     // output.
159     static QTextCodec *getCodec(const QString &source);
160     static QTextCodec *getCodec(const QString &workingDirectory, const QStringList &files);
161
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);
165
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 &currentFile = QString());
171
172     //Helper to go to line of editor if it is a text editor
173     static bool gotoLineOfEditor(Core::IEditor *e, int lineNumber);
174
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());
185
186     bool setConfigurationWidget(QWidget *w);
187     QWidget *configurationWidget() const;
188
189 signals:
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);
195
196 public slots:
197     // Convenience slot to set data read from stdout, will use the
198     // documents' codec to decode
199     void setPlainTextData(const QByteArray &data);
200
201 protected:
202     virtual TextEditor::BaseTextEditorEditable *createEditableInterface();
203
204     void contextMenuEvent(QContextMenuEvent *e);
205     void mouseMoveEvent(QMouseEvent *e);
206     void mouseReleaseEvent(QMouseEvent *e);
207     void mouseDoubleClickEvent(QMouseEvent *e);
208     void keyPressEvent(QKeyEvent *);
209
210 public slots:
211     void setFontSettings(const TextEditor::FontSettings &);
212
213 private slots:
214     void describe();
215     void slotActivateAnnotation();
216     void slotPopulateDiffBrowser();
217     void slotDiffBrowse(int);
218     void slotDiffCursorPositionChanged();
219     void slotAnnotateRevision();
220     void slotCopyRevision();
221
222 protected:
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;
227
228 private:
229     // Implement to return a set of change identifiers in
230     // annotation mode
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;
243
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);
248
249     VCSBaseEditorPrivate *d;
250 };
251
252 } // namespace VCSBase
253
254 #endif // VCSBASE_BASEEDITOR_H