documentmanager.h 6.48 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
23
24
25
** 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.
**
** 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

30
31
#ifndef DOCUMENTMANAGER_H
#define DOCUMENTMANAGER_H
con's avatar
con committed
32

hjk's avatar
hjk committed
33
#include <coreplugin/id.h>
con's avatar
con committed
34

35
36
#include <QObject>
#include <QPair>
con's avatar
con committed
37
38

QT_BEGIN_NAMESPACE
39
class QStringList;
40
class QAction;
41
class QMainWindow;
42
class QMenu;
con's avatar
con committed
43
44
45
46
47
QT_END_NAMESPACE

namespace Core {

class IContext;
48
class IDocument;
con's avatar
con committed
49

50
class CORE_EXPORT DocumentManager : public QObject
con's avatar
con committed
51
52
53
{
    Q_OBJECT
public:
54
55
56
57
58
    enum FixMode {
        ResolveLinks,
        KeepLinks
    };

hjk's avatar
hjk committed
59
    typedef QPair<QString, Id> RecentFile;
60

61
62
    explicit DocumentManager(QMainWindow *ew);
    virtual ~DocumentManager();
con's avatar
con committed
63

64
    static DocumentManager *instance();
65

con's avatar
con committed
66
    // file pool to monitor
67
68
69
70
    static void addDocuments(const QList<IDocument *> &documents, bool addWatcher = true);
    static void addDocument(IDocument *document, bool addWatcher = true);
    static bool removeDocument(IDocument *document);
    static QList<IDocument *> modifiedDocuments();
con's avatar
con committed
71

72
    static void renamedFile(const QString &from, const QString &to);
dt's avatar
dt committed
73

74
75
    static void expectFileChange(const QString &fileName);
    static void unexpectFileChange(const QString &fileName);
76

con's avatar
con committed
77
    // recent files
78
    static void addToRecentFiles(const QString &fileName, const Id &editorId = Id());
79
    Q_SLOT void clearRecentFiles();
80
    static QList<RecentFile> recentFiles();
81

82
    static void saveSettings();
dt's avatar
dt committed
83

con's avatar
con committed
84
    // current file
85
86
    static void setCurrentFile(const QString &filePath);
    static QString currentFile();
con's avatar
con committed
87
88

    // helper methods
89
    static QString fixFileName(const QString &fileName, FixMode fixmode);
con's avatar
con committed
90

91
    static bool saveDocument(IDocument *document, const QString &fileName = QString(), bool *isReadOnly = 0);
92

93
    static QStringList getOpenFileNames(const QString &filters,
94
95
                                 const QString path = QString(),
                                 QString *selectedFilter = 0);
96
    static QString getSaveFileName(const QString &title, const QString &pathIn,
97
                            const QString &filter = QString(), QString *selectedFilter = 0);
98
    static QString getSaveFileNameWithExtension(const QString &title, const QString &pathIn,
99
                                         const QString &filter);
100
    static QString getSaveAsFileName(const IDocument *document, const QString &filter = QString(),
101
                              QString *selectedFilter = 0);
con's avatar
con committed
102

103
104
    static QList<IDocument *> saveModifiedDocumentsSilently(const QList<IDocument *> &documents, bool *cancelled = 0);
    static QList<IDocument *> saveModifiedDocuments(const QList<IDocument *> &documents,
hjk's avatar
hjk committed
105
                                     bool *cancelled = 0,
106
                                     const QString &message = QString(),
107
                                     const QString &alwaysSaveMessage = QString(),
108
                                     bool *alwaysSave = 0);
con's avatar
con committed
109

110
111
    static QString fileDialogLastVisitedDirectory();
    static void setFileDialogLastVisitedDirectory(const QString &);
112

113
    static QString fileDialogInitialDirectory();
114

115
116
    static bool useProjectsDirectory();
    static void setUseProjectsDirectory(bool);
117

118
119
    static QString projectsDirectory();
    static void setProjectsDirectory(const QString &);
120

121
122
123
    static QString buildDirectory();
    static void setBuildDirectory(const QString &directory);

124
125
    static void populateOpenWithMenu(QMenu *menu, const QString &fileName);

126
127
    /* Used to notify e.g. the code model to update the given files. Does *not*
       lead to any editors to reload or any other editor manager actions. */
128
    static void notifyFilesChangedInternally(const QStringList &files);
129

130
131
132
133
    static void executeOpenWithMenuAction(QAction *action);

public slots:
    void slotExecuteOpenWithMenuAction(QAction *action);
134

con's avatar
con committed
135
136
signals:
    void currentFileChanged(const QString &filePath);
137
138
139
    /* Used to notify e.g. the code model to update the given files. Does *not*
       lead to any editors to reload or any other editor manager actions. */
    void filesChangedInternally(const QStringList &files);
140
141
142
143
    /// emitted if all documents changed their name e.g. due to the file changing on disk
    void allDocumentsRenamed(const QString &from, const QString &to);
    /// emitted if one document changed its name e.g. due to save as
    void documentRenamed(Core::IDocument *document, const QString &from, const QString &to);
con's avatar
con committed
144

145
146
147
protected:
    bool eventFilter(QObject *obj, QEvent *e);

con's avatar
con committed
148
private slots:
149
    void documentDestroyed(QObject *obj);
150
    void fileNameChanged(const QString &oldName, const QString &newName);
con's avatar
con committed
151
152
153
    void checkForNewFileName();
    void checkForReload();
    void changedFile(const QString &file);
154
    void syncWithEditor(const QList<Core::IContext *> &context);
con's avatar
con committed
155
156
};

157
/*! The FileChangeBlocker blocks all change notifications to all IDocument * that
158
159
    match the given filename. And unblocks in the destructor.

160
    To also reload the IDocument in the destructor class set modifiedReload to true
161
162
163
164
165

  */
class CORE_EXPORT FileChangeBlocker
{
public:
Friedemann Kleint's avatar
Friedemann Kleint committed
166
    explicit FileChangeBlocker(const QString &fileName);
167
168
    ~FileChangeBlocker();
private:
Friedemann Kleint's avatar
Friedemann Kleint committed
169
    const QString m_fileName;
170
    Q_DISABLE_COPY(FileChangeBlocker)
171
172
};

con's avatar
con committed
173
174
} // namespace Core

175
Q_DECLARE_METATYPE(Core::DocumentManager::RecentFile)
176

177
#endif // DOCUMENTMANAGER_H