editormanager.h 8.53 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2
3
4
**
** This file is part of Qt Creator
**
5
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
7
8
**
** Contact:  Qt Software Information (qt-info@nokia.com)
**
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
26
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
con's avatar
con committed
27
**
28
**************************************************************************/
hjk's avatar
hjk committed
29

con's avatar
con committed
30
31
32
33
34
35
36
37
38
39
40
41
#ifndef EDITORMANAGER_H
#define EDITORMANAGER_H

#include "../core_global.h"

#include <coreplugin/icorelistener.h>

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

QT_BEGIN_NAMESPACE
class QSettings;
42
class QModelIndex;
con's avatar
con committed
43
44
45
46
47
48
49
50
51
52
53
54
QT_END_NAMESPACE

namespace Core {

class EditorGroup;
class IContext;
class ICore;
class IEditor;
class IEditorFactory;
class MimeType;
class IFile;
class IMode;
55
class IVersionControl;
con's avatar
con committed
56
57
58
59
60
61
62
63
64

enum MakeWritableResult {
    OpenedWithVersionControl,
    MadeWritable,
    SavedAs,
    Failed
};

struct EditorManagerPrivate;
65

con's avatar
con committed
66
67
namespace Internal {
class OpenEditorsWindow;
68
class EditorModel;
mae's avatar
mae committed
69
class EditorView;
mae's avatar
mae committed
70
class SplitterOrView;
con's avatar
con committed
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101

class EditorClosingCoreListener;
class OpenEditorsViewFactory;
} // 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;

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

mae's avatar
mae committed
102
103
    enum OpenEditorFlag {
        NoActivate = 1,
104
        IgnoreNavigationHistory = 2
mae's avatar
mae committed
105
106
107
    };
    Q_DECLARE_FLAGS(OpenEditorFlags, OpenEditorFlag)

con's avatar
con committed
108
109
    IEditor *openEditor(const QString &fileName,
                        const QString &editorKind = QString(),
mae's avatar
mae committed
110
111
                        OpenEditorFlags flags = 0);
    
con's avatar
con committed
112
113
114
115
116
117
118
119
120
121
122
123
    QStringList getOpenFileNames() const;
    QString getOpenWithEditorKind(const QString &fileName) const;


    void ensureEditorManagerVisible();
    IEditor *newFile(const QString &editorKind,
                     QString *titlePattern = 0,
                     const QString &contents = QString());
    bool hasEditor(const QString &fileName) const;
    QList<IEditor *> editorsForFileName(const QString &filename) const;

    IEditor *currentEditor() const;
mae's avatar
mae committed
124
    void activateEditor(IEditor *editor, OpenEditorFlags flags = 0);
con's avatar
con committed
125
126

    QList<IEditor*> openedEditors() const;
127
128

    Internal::EditorModel *openedEditorsModel() const;
129
    void activateEditor(const QModelIndex &index, Internal::EditorView *view = 0);
130
    void closeEditor(const QModelIndex &index);
131

132

con's avatar
con committed
133
    QList<IEditor*> editorsForFiles(QList<IFile*> files) const;
134
    //QList<EditorGroup *> editorGroups() const;
con's avatar
con committed
135
    QList<IEditor*> editorHistory() const;
136
    void addCurrentPositionToNavigationHistory(const QByteArray &saveState = QByteArray(), bool compress = false);
con's avatar
con committed
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161

    bool saveEditor(IEditor *editor);

    bool closeEditors(const QList<IEditor *> editorsToClose, bool askAboutModifiedEditors = true);

    MakeWritableResult makeEditorWritable(IEditor *editor);

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

    IEditor *restoreEditor(QString fileName, QString editorKind, EditorGroup *group);

    void saveSettings(QSettings *settings);
    void readSettings(QSettings *settings);

    Internal::OpenEditorsWindow *windowPopup() const;
    void showWindowPopup() const;

    void showEditorInfoBar(const QString &kind,
                           const QString &infoText,
                           const QString &buttonText = QString(),
                           QObject *object = 0, const char *member = 0);

    void hideEditorInfoBar(const QString &kind);

162
163
164
165
166
167
168
    void showEditorStatusBar(const QString &kind,
                           const QString &infoText,
                           const QString &buttonText = QString(),
                           QObject *object = 0, const char *member = 0);

    void hideEditorStatusBar(const QString &kind);

con's avatar
con committed
169
170
171
172
    EditorFactoryList editorFactories(const MimeType &mimeType, bool bestMatchOnly = true) const;

    void setExternalEditor(const QString &);
    QString externalEditor() const;
mae's avatar
mae committed
173
    QString defaultExternalEditor() const;
con's avatar
con committed
174
175
    QString externalEditorHelpText() const;

176
177
178
179
180
181
182
183
184
185

    // 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);

con's avatar
con committed
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
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();

private slots:
    bool saveFile(Core::IEditor *editor = 0);
    bool saveFileAs(Core::IEditor *editor = 0);
    void closeEditor();
mae's avatar
mae committed
201

con's avatar
con committed
202
203
    void gotoNextDocHistory();
    void gotoPreviousDocHistory();
mae's avatar
mae committed
204
    void handleContextChange(Core::IContext *context);
con's avatar
con committed
205
206
207
208
209
210
    void updateEditorHistory();
    void updateActions();
    void revertToSaved();
    void goBackInNavigationHistory();
    void goForwardInNavigationHistory();
    void makeCurrentEditorWritable();
mae's avatar
mae committed
211
212

public slots:
213
    void split(Qt::Orientation orientation);
214
215
    void split();
    void splitSideBySide();
216
217
218
    void removeCurrentSplit();
    void removeAllSplits();
    void gotoOtherSplit();
con's avatar
con committed
219
220
221
222
223

private:
    QList<IFile *> filesForEditors(QList<IEditor *> editors) const;
    IEditor *createEditor(const QString &mimeType = QString(),
                          const QString &fileName = QString());
mae's avatar
mae committed
224
225
    void addEditor(IEditor *editor, bool isDuplicate = false);
    void removeEditor(IEditor *editor);
con's avatar
con committed
226
227
228

    void restoreEditorState(IEditor *editor);

mae's avatar
mae committed
229
    IEditor *placeEditor(Core::Internal::EditorView *view, Core::IEditor *editor);
mae's avatar
mae committed
230
    Core::IEditor *duplicateEditor(IEditor *editor);
mae's avatar
mae committed
231
    void setCurrentEditor(IEditor *editor, bool ignoreNavigationHistory = false);
mae's avatar
mae committed
232
233
    void setCurrentView(Core::Internal::SplitterOrView *view);
    Core::Internal::SplitterOrView *currentView() const;
mae's avatar
mae committed
234
    void activateEditor(Core::Internal::EditorView *view, Core::IEditor *editor, OpenEditorFlags flags = 0);
mae's avatar
mae committed
235
236
    void closeEditor(Core::IEditor *editor);
    void closeView(Core::Internal::EditorView *view);
mae's avatar
mae committed
237
    void emptyView(Core::Internal::EditorView *view);
238
    Core::Internal::EditorView *currentEditorView();
mae's avatar
mae committed
239
    IEditor *pickUnusedEditor() const;
mae's avatar
mae committed
240

241
242
    void updateCurrentPositionInNavigationHistory();

con's avatar
con committed
243
244
    static EditorManager *m_instance;
    EditorManagerPrivate *m_d;
mae's avatar
mae committed
245
246

    friend class Core::Internal::SplitterOrView;
mae's avatar
mae committed
247
    friend class Core::Internal::EditorView;
con's avatar
con committed
248
249
};

250
251
252
253
} // namespace Core

Q_DECLARE_OPERATORS_FOR_FLAGS(Core::EditorManager::OpenEditorFlags);

mae's avatar
mae committed
254

con's avatar
con committed
255
256
//===================EditorClosingCoreListener======================

257
namespace Core {
con's avatar
con committed
258
259
namespace Internal {

260
261
class EditorClosingCoreListener : public ICoreListener
{
con's avatar
con committed
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
    Q_OBJECT

public:
    EditorClosingCoreListener(EditorManager *em);
    bool editorAboutToClose(IEditor *editor);
    bool coreAboutToClose();

private:
    EditorManager *m_em;
};

} // namespace Internal
} // namespace Core

#endif // EDITORMANAGER_H