editormanager.h 10.1 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"

35
#include "documentmodel.h"
36

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

40 41 42
#include <QList>
#include <QWidget>
#include <QMenu>
con's avatar
con committed
43 44 45 46 47 48 49 50 51

QT_BEGIN_NAMESPACE
QT_END_NAMESPACE

namespace Core {

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

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

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

namespace Internal {
class EditorClosingCoreListener;
69 70
class EditorView;
class MainWindow;
con's avatar
con committed
71
class OpenEditorsViewFactory;
72 73
class OpenEditorsWindow;
class SplitterOrView;
con's avatar
con committed
74 75 76 77 78 79
} // namespace Internal

class CORE_EXPORT EditorManagerPlaceHolder : public QWidget
{
    Q_OBJECT
public:
80
    explicit EditorManagerPlaceHolder(Core::IMode *mode, QWidget *parent = 0);
con's avatar
con committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94
    ~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
95 96
    typedef QList<IEditorFactory *> EditorFactoryList;
    typedef QList<IExternalEditor *> ExternalEditorList;
con's avatar
con committed
97

hjk's avatar
hjk committed
98
    static EditorManager *instance();
con's avatar
con committed
99

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

mae's avatar
mae committed
102
    enum OpenEditorFlag {
Eike Ziller's avatar
Eike Ziller committed
103
        DoNotChangeCurrentEditor = 1,
104
        IgnoreNavigationHistory = 2,
Eike Ziller's avatar
Eike Ziller committed
105
        DoNotMakeVisible = 4,
106 107
        CanContainLineNumber = 8,
        OpenInOtherSplit = 16
mae's avatar
mae committed
108 109 110
    };
    Q_DECLARE_FLAGS(OpenEditorFlags, OpenEditorFlag)

111
    static QString splitLineNumber(QString *fileName);
hjk's avatar
hjk committed
112
    static IEditor *openEditor(const QString &fileName, const Id &editorId = Id(),
hjk's avatar
hjk committed
113
        OpenEditorFlags flags = 0, bool *newEditor = 0);
114 115 116
    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
117
    static IEditor *openEditorWithContents(const Id &editorId,
118
        QString *titlePattern = 0, const QByteArray &contents = QByteArray());
119

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

con's avatar
con committed
122
    QStringList getOpenFileNames() const;
hjk's avatar
hjk committed
123
    Id getOpenWithEditorId(const QString &fileName, bool *isExternalEditor = 0) const;
con's avatar
con committed
124

125
    static IDocument *currentDocument();
hjk's avatar
hjk committed
126
    static IEditor *currentEditor();
127 128 129
    QList<IEditor *> visibleEditors() const;
    QList<IEditor*> openedEditors() const;

hjk's avatar
hjk committed
130
    static void activateEditor(IEditor *editor, OpenEditorFlags flags = 0);
131
    void activateEditorForEntry(DocumentModel::Entry *entry, OpenEditorFlags flags = 0);
132
    IEditor *activateEditorForDocument(IDocument *document, OpenEditorFlags flags = 0);
133
    IEditor *activateEditorForDocument(Internal::EditorView *view, IDocument *document, OpenEditorFlags flags = 0);
con's avatar
con committed
134

135
    static DocumentModel *documentModel();
136
    static bool closeDocuments(const QList<IDocument *> &documents, bool askAboutModifiedEditors = true);
137
    void closeEditor(DocumentModel::Entry *entry);
138
    void closeOtherEditors(IDocument *document);
139

140
    void addCurrentPositionToNavigationHistory(IEditor *editor = 0, const QByteArray &saveState = QByteArray());
141
    void cutForwardNavigationHistory();
con's avatar
con committed
142 143 144

    bool saveEditor(IEditor *editor);

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

147
    MakeWritableResult makeFileWritable(IDocument *document);
con's avatar
con committed
148 149 150

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

153 154
    void saveSettings();
    void readSettings();
con's avatar
con committed
155 156

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

159
    void showEditorStatusBar(const QString &id,
160 161 162 163
                           const QString &infoText,
                           const QString &buttonText = QString(),
                           QObject *object = 0, const char *member = 0);

164
    void hideEditorStatusBar(const QString &id);
165

hjk's avatar
hjk committed
166 167
    static EditorFactoryList editorFactories(const MimeType &mimeType, bool bestMatchOnly = true);
    static ExternalEditorList externalEditors(const MimeType &mimeType, bool bestMatchOnly = true);
con's avatar
con committed
168

169 170
    void setReloadSetting(IDocument::ReloadSetting behavior);
    IDocument::ReloadSetting reloadSetting() const;
171

172 173 174 175 176
    void setAutoSaveEnabled(bool enabled);
    bool autoSaveEnabled() const;
    void setAutoSaveInterval(int interval);
    int autoSaveInterval() const;

177
    QTextCodec *defaultTextCodec() const;
178

179 180
    static qint64 maxTextFileSize();

181 182 183
    void setWindowTitleAddition(const QString &addition);
    QString windowTitleAddition() const;

Orgad Shaneh's avatar
Orgad Shaneh committed
184 185 186
    static void setWindowTitleVcsTopic(const QString &topic);
    static QString windowTitleVcsTopic();

187 188
    void addSaveAndCloseEditorActions(QMenu *contextMenu, DocumentModel::Entry *entry);
    void addNativeDirActions(QMenu *contextMenu, DocumentModel::Entry *entry);
189

con's avatar
con committed
190 191
signals:
    void currentEditorChanged(Core::IEditor *editor);
192
    void currentDocumentStateChanged();
con's avatar
con committed
193 194 195 196 197 198 199 200
    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);

201 202
    bool saveDocument(Core::IDocument *documentParam = 0);
    bool saveDocumentAs(Core::IDocument *documentParam = 0);
203
    void revertToSaved();
204
    void revertToSaved(IDocument *document);
con's avatar
con committed
205
    void closeEditor();
con's avatar
con committed
206
    void closeOtherEditors();
207
    void doEscapeKeyFocusMoveMagic();
mae's avatar
mae committed
208

209
private slots:
con's avatar
con committed
210 211
    void gotoNextDocHistory();
    void gotoPreviousDocHistory();
212
    void handleContextChange(const QList<Core::IContext *> &context);
con's avatar
con committed
213 214
    void updateActions();
    void makeCurrentEditorWritable();
215
    void vcsOpenCurrentEditor();
216
    void updateWindowTitle();
217
    void handleDocumentStateChange();
hjk's avatar
hjk committed
218
    void updateVariable(const QByteArray &variable);
219
    void autoSave();
mae's avatar
mae committed
220

221 222 223 224
    void saveDocumentFromContextMenu();
    void saveDocumentAsFromContextMenu();
    void revertToSavedFromContextMenu();

225 226 227
    void closeEditorFromContextMenu();
    void closeOtherEditorsFromContextMenu();

Robert Loehning's avatar
Robert Loehning committed
228 229 230
    void showInGraphicalShell();
    void openTerminal();

231 232 233
    void rootDestroyed(QObject *root);
    void setCurrentEditorFromContextChange();

234 235
    void gotoNextSplit();

mae's avatar
mae committed
236
public slots:
237 238
    void goBackInNavigationHistory();
    void goForwardInNavigationHistory();
239
    void split(Qt::Orientation orientation);
240 241
    void split();
    void splitSideBySide();
242
    void splitNewWindow();
243 244 245
    void removeCurrentSplit();
    void removeAllSplits();
    void gotoOtherSplit();
con's avatar
con committed
246 247

private:
248 249 250 251
    explicit EditorManager(QWidget *parent);
    virtual ~EditorManager();
    void init();

hjk's avatar
hjk committed
252
    static IEditor *createEditor(const Id &id = Id(), const QString &fileName = QString());
253
    void addEditor(IEditor *editor);
mae's avatar
mae committed
254
    void removeEditor(IEditor *editor);
con's avatar
con committed
255 256 257

    void restoreEditorState(IEditor *editor);

hjk's avatar
hjk committed
258 259 260
    IEditor *placeEditor(Internal::EditorView *view, IEditor *editor);
    IEditor *duplicateEditor(IEditor *editor);
    IEditor *activateEditor(Internal::EditorView *view, IEditor *editor, OpenEditorFlags flags = 0);
261
    void activateEditorForEntry(Internal::EditorView *view, DocumentModel::Entry *entry, OpenEditorFlags flags = 0);
262
    void activateView(Internal::EditorView *view);
hjk's avatar
hjk committed
263
    IEditor *openEditor(Internal::EditorView *view, const QString &fileName,
hjk's avatar
hjk committed
264
        const Id &id = Id(), OpenEditorFlags flags = 0, bool *newEditor = 0);
265 266

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

hjk's avatar
hjk committed
272 273 274
    void closeEditor(IEditor *editor);
    void closeView(Internal::EditorView *view);
    void emptyView(Internal::EditorView *view);
275
    static void splitNewWindow(Internal::EditorView *view);
mae's avatar
mae committed
276
    IEditor *pickUnusedEditor() const;
277
    void addDocumentToRecentFiles(IDocument *document);
278
    void updateAutoSave();
279
    void setCloseSplitEnabled(Internal::SplitterOrView *splitterOrView, bool enable);
280
    void updateMakeWritableWarning();
281
    void setupSaveActions(IDocument *document, QAction *saveAction, QAction *saveAsAction, QAction *revertToSavedAction);
mae's avatar
mae committed
282

283
    friend class Core::Internal::MainWindow;
mae's avatar
mae committed
284
    friend class Core::Internal::SplitterOrView;
mae's avatar
mae committed
285
    friend class Core::Internal::EditorView;
Lasse Holmstedt's avatar
Lasse Holmstedt committed
286
    friend class Core::EditorToolBar;
con's avatar
con committed
287 288
};

289 290
} // namespace Core

291
Q_DECLARE_OPERATORS_FOR_FLAGS(Core::EditorManager::OpenEditorFlags)
hjk's avatar
hjk committed
292

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