editormanager.h 10.3 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
con's avatar
con committed
2
**
3
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
con's avatar
con committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
con's avatar
con committed
7
**
hjk's avatar
hjk committed
8
9
10
11
12
13
14
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
15
**
16
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
18
19
20
21
22
** Alternatively, 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.
23
**
hjk's avatar
hjk committed
24
25
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
con's avatar
con committed
26
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
hjk's avatar
hjk committed
29

con's avatar
con committed
30
31
32
33
34
#ifndef EDITORMANAGER_H
#define EDITORMANAGER_H

#include "../core_global.h"

hjk's avatar
hjk committed
35
#include <coreplugin/id.h>
36
#include <coreplugin/idocument.h> // enumerations
con's avatar
con committed
37

38
39
40
#include <QList>
#include <QWidget>
#include <QMenu>
con's avatar
con committed
41
42

QT_BEGIN_NAMESPACE
43
class QModelIndex;
con's avatar
con committed
44
45
46
47
48
49
50
QT_END_NAMESPACE

namespace Core {

class IContext;
class IEditor;
class IEditorFactory;
51
class IExternalEditor;
con's avatar
con committed
52
class MimeType;
53
class IDocument;
con's avatar
con committed
54
class IMode;
55
class IVersionControl;
con's avatar
con committed
56

Lasse Holmstedt's avatar
Lasse Holmstedt committed
57
class EditorToolBar;
Lasse Holmstedt's avatar
Lasse Holmstedt committed
58

con's avatar
con committed
59
60
61
62
63
64
65
66
enum MakeWritableResult {
    OpenedWithVersionControl,
    MadeWritable,
    SavedAs,
    Failed
};

struct EditorManagerPrivate;
67
class OpenEditorsModel;
68

con's avatar
con committed
69
70
namespace Internal {
class OpenEditorsWindow;
mae's avatar
mae committed
71
class EditorView;
mae's avatar
mae committed
72
class SplitterOrView;
con's avatar
con committed
73
74
75

class EditorClosingCoreListener;
class OpenEditorsViewFactory;
mae's avatar
mae committed
76
77


con's avatar
con committed
78
79
80
81
82
83
} // namespace Internal

class CORE_EXPORT EditorManagerPlaceHolder : public QWidget
{
    Q_OBJECT
public:
84
    explicit EditorManagerPlaceHolder(Core::IMode *mode, QWidget *parent = 0);
con's avatar
con committed
85
86
87
88
89
90
91
92
93
94
95
96
97
98
    ~EditorManagerPlaceHolder();
    static EditorManagerPlaceHolder* current();
private slots:
    void currentModeChanged(Core::IMode *);
private:
    Core::IMode *m_mode;
    static EditorManagerPlaceHolder* m_current;
};

class CORE_EXPORT EditorManager : public QWidget
{
    Q_OBJECT

public:
hjk's avatar
hjk committed
99
100
    typedef QList<IEditorFactory *> EditorFactoryList;
    typedef QList<IExternalEditor *> ExternalEditorList;
con's avatar
con committed
101

hjk's avatar
hjk committed
102
    explicit EditorManager(QWidget *parent);
con's avatar
con committed
103
104
    virtual ~EditorManager();
    void init();
hjk's avatar
hjk committed
105
    static EditorManager *instance();
con's avatar
con committed
106

Lasse Holmstedt's avatar
Lasse Holmstedt committed
107
    static EditorToolBar *createToolBar(QWidget *parent = 0);
Lasse Holmstedt's avatar
Lasse Holmstedt committed
108

mae's avatar
mae committed
109
    enum OpenEditorFlag {
Eike Ziller's avatar
Eike Ziller committed
110
        DoNotChangeCurrentEditor = 1,
111
        IgnoreNavigationHistory = 2,
Eike Ziller's avatar
Eike Ziller committed
112
        DoNotMakeVisible = 4,
113
114
        CanContainLineNumber = 8,
        OpenInOtherSplit = 16
mae's avatar
mae committed
115
116
117
    };
    Q_DECLARE_FLAGS(OpenEditorFlags, OpenEditorFlag)

118
    static QString splitLineNumber(QString *fileName);
hjk's avatar
hjk committed
119
    static IEditor *openEditor(const QString &fileName, const Id &editorId = Id(),
hjk's avatar
hjk committed
120
        OpenEditorFlags flags = 0, bool *newEditor = 0);
121
122
123
    static IEditor *openEditorAt(const QString &fileName,  int line, int column = 0,
                                 const Id &editorId = Id(), OpenEditorFlags flags = 0,
                                 bool *newEditor = 0);
hjk's avatar
hjk committed
124
    static IEditor *openEditorWithContents(const Id &editorId,
hjk's avatar
hjk committed
125
        QString *titlePattern = 0, const QString &contents = QString());
126

hjk's avatar
hjk committed
127
    static bool openExternalEditor(const QString &fileName, const Id &editorId);
128

con's avatar
con committed
129
    QStringList getOpenFileNames() const;
hjk's avatar
hjk committed
130
    Id getOpenWithEditorId(const QString &fileName, bool *isExternalEditor = 0) const;
con's avatar
con committed
131
132
133

    bool hasEditor(const QString &fileName) const;
    QList<IEditor *> editorsForFileName(const QString &filename) const;
134
    QList<IEditor *> editorsForDocument(IDocument *document) const;
con's avatar
con committed
135

hjk's avatar
hjk committed
136
    static IEditor *currentEditor();
137
138
139
    QList<IEditor *> visibleEditors() const;
    QList<IEditor*> openedEditors() const;

hjk's avatar
hjk committed
140
    static void activateEditor(IEditor *editor, OpenEditorFlags flags = 0);
141
    void activateEditorForIndex(const QModelIndex &index, OpenEditorFlags = 0);
142
    IEditor *activateEditorForDocument(Internal::EditorView *view, IDocument *document, OpenEditorFlags flags = 0);
con's avatar
con committed
143

144
    OpenEditorsModel *openedEditorsModel() const;
145
    void closeEditor(const QModelIndex &index);
146
    void closeOtherEditors(IEditor *editor);
147

148
    QList<IEditor*> editorsForDocuments(QList<IDocument *> documents) const;
149
    void addCurrentPositionToNavigationHistory(IEditor *editor = 0, const QByteArray &saveState = QByteArray());
150
    void cutForwardNavigationHistory();
con's avatar
con committed
151
152
153

    bool saveEditor(IEditor *editor);

154
    bool closeEditors(const QList<IEditor *> &editorsToClose, bool askAboutModifiedEditors = true);
con's avatar
con committed
155

156
    MakeWritableResult makeFileWritable(IDocument *document);
con's avatar
con committed
157
158
159

    QByteArray saveState() const;
    bool restoreState(const QByteArray &state);
hjk's avatar
hjk committed
160
    bool hasSplitter() const;
con's avatar
con committed
161

162
163
    void saveSettings();
    void readSettings();
con's avatar
con committed
164
165

    Internal::OpenEditorsWindow *windowPopup() const;
166
    void showPopupOrSelectDocument() const;
con's avatar
con committed
167

168
    void showEditorStatusBar(const QString &id,
169
170
171
172
                           const QString &infoText,
                           const QString &buttonText = QString(),
                           QObject *object = 0, const char *member = 0);

173
    void hideEditorStatusBar(const QString &id);
174

hjk's avatar
hjk committed
175
176
    static EditorFactoryList editorFactories(const MimeType &mimeType, bool bestMatchOnly = true);
    static ExternalEditorList externalEditors(const MimeType &mimeType, bool bestMatchOnly = true);
con's avatar
con committed
177

178
179
    void setReloadSetting(IDocument::ReloadSetting behavior);
    IDocument::ReloadSetting reloadSetting() const;
180

181
182
183
184
185
    void setAutoSaveEnabled(bool enabled);
    bool autoSaveEnabled() const;
    void setAutoSaveInterval(int interval);
    int autoSaveInterval() const;

186
    QTextCodec *defaultTextCodec() const;
187

188
189
    static qint64 maxTextFileSize();

190
191
192
    void setWindowTitleAddition(const QString &addition);
    QString windowTitleAddition() const;

193
    void addSaveAndCloseEditorActions(QMenu *contextMenu, const QModelIndex &editorIndex);
Robert Loehning's avatar
Robert Loehning committed
194
    void addNativeDirActions(QMenu *contextMenu, const QModelIndex &editorIndex);
195

con's avatar
con committed
196
197
signals:
    void currentEditorChanged(Core::IEditor *editor);
198
    void currentEditorStateChanged(Core::IEditor *editor);
con's avatar
con committed
199
200
201
202
203
204
205
206
    void editorCreated(Core::IEditor *editor, const QString &fileName);
    void editorOpened(Core::IEditor *editor);
    void editorAboutToClose(Core::IEditor *editor);
    void editorsClosed(QList<Core::IEditor *> editors);

public slots:
    bool closeAllEditors(bool askAboutModifiedEditors = true);

207
208
    bool saveDocument(Core::IDocument *documentParam = 0);
    bool saveDocumentAs(Core::IDocument *documentParam = 0);
209
    void revertToSaved();
210
    void revertToSaved(Core::IEditor *editor);
con's avatar
con committed
211
    void closeEditor();
con's avatar
con committed
212
    void closeOtherEditors();
213
    void doEscapeKeyFocusMoveMagic();
mae's avatar
mae committed
214

215
private slots:
con's avatar
con committed
216
217
    void gotoNextDocHistory();
    void gotoPreviousDocHistory();
218
    void handleContextChange(const QList<Core::IContext *> &context);
con's avatar
con committed
219
220
    void updateActions();
    void makeCurrentEditorWritable();
221
    void vcsOpenCurrentEditor();
222
    void updateWindowTitle();
223
    void handleEditorStateChange();
hjk's avatar
hjk committed
224
    void updateVariable(const QByteArray &variable);
225
    void autoSave();
mae's avatar
mae committed
226

227
228
229
230
    void saveDocumentFromContextMenu();
    void saveDocumentAsFromContextMenu();
    void revertToSavedFromContextMenu();

231
232
233
    void closeEditorFromContextMenu();
    void closeOtherEditorsFromContextMenu();

Robert Loehning's avatar
Robert Loehning committed
234
235
236
    void showInGraphicalShell();
    void openTerminal();

237
238
239
    void rootDestroyed(QObject *root);
    void setCurrentEditorFromContextChange();

240
241
    void gotoNextSplit();

mae's avatar
mae committed
242
public slots:
243
244
    void goBackInNavigationHistory();
    void goForwardInNavigationHistory();
245
    void split(Qt::Orientation orientation);
246
247
    void split();
    void splitSideBySide();
248
    void splitNewWindow();
249
250
251
    void removeCurrentSplit();
    void removeAllSplits();
    void gotoOtherSplit();
con's avatar
con committed
252
253

private:
254
    QList<IDocument *> documentsForEditors(QList<IEditor *> editors) const;
hjk's avatar
hjk committed
255
    static IEditor *createEditor(const Id &id = Id(), const QString &fileName = QString());
mae's avatar
mae committed
256
257
    void addEditor(IEditor *editor, bool isDuplicate = false);
    void removeEditor(IEditor *editor);
con's avatar
con committed
258
259
260

    void restoreEditorState(IEditor *editor);

hjk's avatar
hjk committed
261
262
263
    IEditor *placeEditor(Internal::EditorView *view, IEditor *editor);
    IEditor *duplicateEditor(IEditor *editor);
    IEditor *activateEditor(Internal::EditorView *view, IEditor *editor, OpenEditorFlags flags = 0);
264
    void activateEditorForIndex(Internal::EditorView *view, const QModelIndex &index, OpenEditorFlags = 0);
265
    void activateView(Internal::EditorView *view);
hjk's avatar
hjk committed
266
    IEditor *openEditor(Internal::EditorView *view, const QString &fileName,
hjk's avatar
hjk committed
267
        const Id &id = Id(), OpenEditorFlags flags = 0, bool *newEditor = 0);
268
269

    void setCurrentEditor(IEditor *editor, bool ignoreNavigationHistory = false);
270
    void setCurrentView(Internal::EditorView *view);
271
272
    Internal::EditorView *currentEditorView() const;
    static Internal::EditorView *viewForEditor(IEditor *editor);
273
    static Internal::SplitterOrView *findRoot(const Internal::EditorView *view, int *rootIndex = 0);
mae's avatar
mae committed
274

hjk's avatar
hjk committed
275
276
277
278
    void closeEditor(IEditor *editor);
    void closeDuplicate(IEditor *editor);
    void closeView(Internal::EditorView *view);
    void emptyView(Internal::EditorView *view);
279
    static void splitNewWindow(Internal::EditorView *view);
280
    IEditor *pickUnusedEditor(Internal::EditorView **foundView = 0) const;
281
    void addDocumentToRecentFiles(IDocument *document);
282
    void updateAutoSave();
283
    void setCloseSplitEnabled(Internal::SplitterOrView *splitterOrView, bool enable);
284
    void updateMakeWritableWarning();
285
286
    QString fileNameForEditor(IEditor *editor);
    void setupSaveActions(IEditor *editor, QAction *saveAction, QAction *saveAsAction, QAction *revertToSavedAction);
mae's avatar
mae committed
287

hjk's avatar
hjk committed
288
    EditorManagerPrivate *d;
mae's avatar
mae committed
289
290

    friend class Core::Internal::SplitterOrView;
mae's avatar
mae committed
291
    friend class Core::Internal::EditorView;
Lasse Holmstedt's avatar
Lasse Holmstedt committed
292
    friend class Core::EditorToolBar;
con's avatar
con committed
293
294
};

295
296
} // namespace Core

297
Q_DECLARE_OPERATORS_FOR_FLAGS(Core::EditorManager::OpenEditorFlags)
hjk's avatar
hjk committed
298

con's avatar
con committed
299
#endif // EDITORMANAGER_H