editormanager.h 9.27 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) 2010 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
** Commercial Usage
10
**
11 12 13 14
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
15
**
16
** GNU Lesser General Public License Usage
17
**
18 19 20 21 22 23
** 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.
24
**
25
** If you are unsure which license is appropriate for your use, please
hjk's avatar
hjk committed
26
** contact the sales department at http://qt.nokia.com/contact.
con's avatar
con committed
27
**
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 <coreplugin/ifile.h> // enumerations
con's avatar
con committed
36 37 38 39 40

#include <QtGui/QWidget>
#include <QtCore/QList>

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

namespace Core {

class EditorGroup;
class IContext;
class ICore;
class IEditor;
class IEditorFactory;
51
class IExternalEditor;
con's avatar
con committed
52 53 54
class MimeType;
class IFile;
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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
} // namespace Internal

class CORE_EXPORT EditorManagerPlaceHolder : public QWidget
{
    Q_OBJECT
public:
    EditorManagerPlaceHolder(Core::IMode *mode, QWidget *parent = 0);
    ~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:
    typedef QList<IEditorFactory*> EditorFactoryList;
100
    typedef QList<IExternalEditor*> ExternalEditorList;
con's avatar
con committed
101 102 103 104 105 106

    explicit EditorManager(ICore *core, QWidget *parent);
    virtual ~EditorManager();
    void init();
    static EditorManager *instance() { return m_instance; }

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 110
    enum OpenEditorFlag {
        NoActivate = 1,
111 112
        IgnoreNavigationHistory = 2,
        NoModeSwitch = 4
mae's avatar
mae committed
113 114 115
    };
    Q_DECLARE_FLAGS(OpenEditorFlags, OpenEditorFlag)

con's avatar
con committed
116
    IEditor *openEditor(const QString &fileName,
117
                        const QString &editorId = QString(),
118 119
                        OpenEditorFlags flags = 0,
                        bool *newEditor = 0);
120
    IEditor *openEditorWithContents(const QString &editorId,
con's avatar
con committed
121 122
                     QString *titlePattern = 0,
                     const QString &contents = QString());
123

124
    bool openExternalEditor(const QString &fileName, const QString &editorId);
125

con's avatar
con committed
126
    QStringList getOpenFileNames() const;
127
    QString getOpenWithEditorId(const QString &fileName, bool *isExternalEditor = 0) const;
con's avatar
con committed
128 129 130 131

    void ensureEditorManagerVisible();
    bool hasEditor(const QString &fileName) const;
    QList<IEditor *> editorsForFileName(const QString &filename) const;
132
    QList<IEditor *> editorsForFile(IFile *file) const;
con's avatar
con committed
133 134

    IEditor *currentEditor() const;
135
    IEditor *activateEditor(IEditor *editor, OpenEditorFlags flags = 0);
mae's avatar
mae committed
136 137
    IEditor *activateEditor(const QModelIndex &index, Internal::EditorView *view = 0, OpenEditorFlags = 0);
    IEditor *activateEditor(Core::Internal::EditorView *view, Core::IFile*file, OpenEditorFlags flags = 0);
con's avatar
con committed
138 139

    QList<IEditor*> openedEditors() const;
140

141
    OpenEditorsModel *openedEditorsModel() const;
142
    void closeEditor(const QModelIndex &index);
143
    void closeOtherEditors(IEditor *editor);
144

con's avatar
con committed
145
    QList<IEditor*> editorsForFiles(QList<IFile*> files) const;
146
    //QList<EditorGroup *> editorGroups() const;
147
    void addCurrentPositionToNavigationHistory(IEditor *editor = 0, const QByteArray &saveState = QByteArray());
148
    void cutForwardNavigationHistory();
con's avatar
con committed
149 150 151

    bool saveEditor(IEditor *editor);

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

    MakeWritableResult makeEditorWritable(IEditor *editor);

    QByteArray saveState() const;
    bool restoreState(const QByteArray &state);

159
    IEditor *restoreEditor(QString fileName, QString editorId, EditorGroup *group);
con's avatar
con committed
160

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

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

167
    void showEditorInfoBar(const QString &id,
con's avatar
con committed
168 169
                           const QString &infoText,
                           const QString &buttonText = QString(),
170 171
                           QObject *object = 0, const char *buttonPressMember = 0,
                           const char *cancelButtonPressMember = 0);
con's avatar
con committed
172

173
    void hideEditorInfoBar(const QString &id);
con's avatar
con committed
174

175
    void showEditorStatusBar(const QString &id,
176 177 178 179
                           const QString &infoText,
                           const QString &buttonText = QString(),
                           QObject *object = 0, const char *member = 0);

180
    void hideEditorStatusBar(const QString &id);
181

con's avatar
con committed
182
    EditorFactoryList editorFactories(const MimeType &mimeType, bool bestMatchOnly = true) const;
183
    ExternalEditorList externalEditors(const MimeType &mimeType, bool bestMatchOnly = true) const;
con's avatar
con committed
184 185 186

    void setExternalEditor(const QString &);
    QString externalEditor() const;
mae's avatar
mae committed
187
    QString defaultExternalEditor() const;
con's avatar
con committed
188 189
    QString externalEditorHelpText() const;

190 191
    void setReloadSetting(IFile::ReloadSetting behavior);
    IFile::ReloadSetting reloadSetting() const;
192 193 194 195 196 197 198 199 200 201

    // Helper to display a message dialog when encountering a read-only
    // file, prompting the user about how to make it writeable.
    enum ReadOnlyAction { RO_Cancel, RO_OpenVCS, RO_MakeWriteable, RO_SaveAs };

    static ReadOnlyAction promptReadOnlyFile(const QString &fileName,
                                             const IVersionControl *versionControl,
                                             QWidget *parent,
                                             bool displaySaveAsButton = false);

202 203
    static qint64 maxTextFileSize();

con's avatar
con committed
204 205 206 207 208 209 210 211 212 213 214 215 216
signals:
    void currentEditorChanged(Core::IEditor *editor);
    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);
    void openInExternalEditor();

    bool saveFile(Core::IEditor *editor = 0);
    bool saveFileAs(Core::IEditor *editor = 0);
217
    void revertToSaved();
con's avatar
con committed
218
    void closeEditor();
con's avatar
con committed
219
    void closeOtherEditors();
mae's avatar
mae committed
220

221
private slots:
con's avatar
con committed
222 223
    void gotoNextDocHistory();
    void gotoPreviousDocHistory();
mae's avatar
mae committed
224
    void handleContextChange(Core::IContext *context);
con's avatar
con committed
225 226
    void updateActions();
    void makeCurrentEditorWritable();
mae's avatar
mae committed
227 228

public slots:
229 230
    void goBackInNavigationHistory();
    void goForwardInNavigationHistory();
231
    void split(Qt::Orientation orientation);
232 233
    void split();
    void splitSideBySide();
234 235 236
    void removeCurrentSplit();
    void removeAllSplits();
    void gotoOtherSplit();
con's avatar
con committed
237 238 239 240 241

private:
    QList<IFile *> filesForEditors(QList<IEditor *> editors) const;
    IEditor *createEditor(const QString &mimeType = QString(),
                          const QString &fileName = QString());
mae's avatar
mae committed
242 243
    void addEditor(IEditor *editor, bool isDuplicate = false);
    void removeEditor(IEditor *editor);
con's avatar
con committed
244 245 246

    void restoreEditorState(IEditor *editor);

mae's avatar
mae committed
247
    IEditor *placeEditor(Core::Internal::EditorView *view, Core::IEditor *editor);
mae's avatar
mae committed
248
    Core::IEditor *duplicateEditor(IEditor *editor);
mae's avatar
mae committed
249
    void setCurrentEditor(IEditor *editor, bool ignoreNavigationHistory = false);
mae's avatar
mae committed
250
    void setCurrentView(Core::Internal::SplitterOrView *view);
251
    IEditor *activateEditor(Core::Internal::EditorView *view, Core::IEditor *editor, OpenEditorFlags flags = 0);
252
    IEditor *openEditor(Core::Internal::EditorView *view, const QString &fileName,
253
                        const QString &editorId = QString(),
254 255
                        OpenEditorFlags flags = 0,
                        bool *newEditor = 0);
mae's avatar
mae committed
256
    Core::Internal::SplitterOrView *currentSplitterOrView() const;
mae's avatar
mae committed
257

mae's avatar
mae committed
258
    void closeEditor(Core::IEditor *editor);
259
    void closeDuplicate(Core::IEditor *editor);
mae's avatar
mae committed
260
    void closeView(Core::Internal::EditorView *view);
mae's avatar
mae committed
261
    void emptyView(Core::Internal::EditorView *view);
mae's avatar
mae committed
262
    Core::Internal::EditorView *currentEditorView() const;
mae's avatar
mae committed
263
    IEditor *pickUnusedEditor() const;
mae's avatar
mae committed
264

265

con's avatar
con committed
266 267
    static EditorManager *m_instance;
    EditorManagerPrivate *m_d;
mae's avatar
mae committed
268 269

    friend class Core::Internal::SplitterOrView;
mae's avatar
mae committed
270
    friend class Core::Internal::EditorView;
Lasse Holmstedt's avatar
Lasse Holmstedt committed
271
    friend class Core::EditorToolBar;
con's avatar
con committed
272 273
};

274 275 276
} // namespace Core

Q_DECLARE_OPERATORS_FOR_FLAGS(Core::EditorManager::OpenEditorFlags);
con's avatar
con committed
277
#endif // EDITORMANAGER_H