OSDN Git Service

1545f33ccbab32d5af884a38b0d90890660421f3
[qt-creator-jp/qt-creator-jp.git] / src / plugins / projectexplorer / projectnodes.h
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** Commercial Usage
10 **
11 ** Licensees holding valid Qt Commercial licenses may use this file in
12 ** accordance with the Qt Commercial License Agreement provided with the
13 ** Software or, alternatively, in accordance with the terms contained in
14 ** a written agreement between you and Nokia.
15 **
16 ** GNU Lesser General Public License Usage
17 **
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** 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
26 ** contact the sales department at http://qt.nokia.com/contact.
27 **
28 **************************************************************************/
29
30 #ifndef PROJECTNODES_H
31 #define PROJECTNODES_H
32
33 #include <QtCore/QObject>
34 #include <QtCore/QStringList>
35 #include <QtGui/QIcon>
36
37 #include "projectexplorer_export.h"
38
39 QT_BEGIN_NAMESPACE
40 class QFileInfo;
41 QT_END_NAMESPACE
42
43
44 namespace Core {
45     class MimeDatabase;
46 }
47
48 namespace ProjectExplorer {
49
50 //
51 // = Node hierarchy =
52 //
53 //  The nodes are arranged in a tree where leaves are FileNodes and non-leaves are FolderNodes
54 //  A Project is a special Folder that manages the files and normal folders underneath it.
55 //
56 //  The Watcher emits signals for structural changes in the hierarchy.
57 //  A Visitor can be used to traverse all Projects and other Folders.
58 //
59
60 enum NodeType {
61     FileNodeType = 1,
62     FolderNodeType,
63     ProjectNodeType,
64     SessionNodeType
65 };
66
67 // File types common for qt projects
68 enum FileType {
69     UnknownFileType = 0,
70     HeaderType,
71     SourceType,
72     FormType,
73     ResourceType,
74     QMLType,
75     ProjectFileType,
76     FileTypeSize
77 };
78
79 class Node;
80 class FileNode;
81 class FileContainerNode;
82 class FolderNode;
83 class ProjectNode;
84 class NodesWatcher;
85 class NodesVisitor;
86
87 class PROJECTEXPLORER_EXPORT Node : public QObject {
88     Q_OBJECT
89 public:
90     NodeType nodeType() const;
91     ProjectNode *projectNode() const;     // managing project
92     FolderNode *parentFolderNode() const; // parent folder or project
93     QString path() const;                 // file system path
94
95 protected:
96     Node(NodeType nodeType, const QString &path);
97
98     void setNodeType(NodeType type);
99     void setProjectNode(ProjectNode *project);
100     void setParentFolderNode(FolderNode *parentFolder);
101     void setPath(const QString &path);
102
103 private:
104     NodeType m_nodeType;
105     ProjectNode *m_projectNode;
106     FolderNode *m_folderNode;
107     QString m_path;
108 };
109
110 class PROJECTEXPLORER_EXPORT FileNode : public Node {
111     Q_OBJECT
112 public:
113     FileNode(const QString &filePath, const FileType fileType, bool generated);
114
115     FileType fileType() const;
116     bool isGenerated() const;
117
118 private:
119     // managed by ProjectNode
120     friend class ProjectNode;
121
122     FileType m_fileType;
123     bool m_generated;
124 };
125
126 class PROJECTEXPLORER_EXPORT FolderNode : public Node {
127     Q_OBJECT
128 public:
129     explicit FolderNode(const QString &folderPath);
130     virtual ~FolderNode();
131
132     QString displayName() const;
133     QIcon icon() const;
134
135     QList<FileNode*> fileNodes() const;
136     QList<FolderNode*> subFolderNodes() const;
137
138     virtual void accept(NodesVisitor *visitor);
139
140     void setDisplayName(const QString &name);
141     void setIcon(const QIcon &icon);
142
143 protected:
144     QList<FolderNode*> m_subFolderNodes;
145     QList<FileNode*> m_fileNodes;
146
147 private:
148     // managed by ProjectNode
149     friend class ProjectNode;
150     QString m_displayName;
151     mutable QIcon m_icon;
152 };
153
154 class PROJECTEXPLORER_EXPORT ProjectNode : public FolderNode
155 {
156     Q_OBJECT
157
158 public:
159     enum ProjectAction {
160         AddSubProject,
161         RemoveSubProject,
162         // Let's the user select to which project file
163         // the file is added
164         AddNewFile,
165         AddExistingFile,
166         // Removes a file from the project, optionally also
167         // delete it on disc
168         RemoveFile,
169         // Deletes a file from the file system, informs the project
170         // that a file was deleted
171         // DeleteFile is a define on windows...
172         EraseFile,
173         Rename
174     };
175
176     // all subFolders that are projects
177     QList<ProjectNode*> subProjectNodes() const;
178
179     // determines if the project will be shown in the flat view
180     // TODO find a better name
181     virtual bool hasBuildTargets() const = 0;
182
183     virtual QList<ProjectAction> supportedActions(Node *node) const = 0;
184
185     virtual bool addSubProjects(const QStringList &proFilePaths) = 0;
186
187     virtual bool removeSubProjects(const QStringList &proFilePaths) = 0;
188
189     virtual bool addFiles(const FileType fileType,
190                           const QStringList &filePaths,
191                           QStringList *notAdded = 0) = 0;
192     // TODO: Maybe remove fileType, can be detected by project
193     virtual bool removeFiles(const FileType fileType,
194                              const QStringList &filePaths,
195                              QStringList *notRemoved = 0) = 0;
196     virtual bool deleteFiles(const FileType fileType,
197                              const QStringList &filePaths) = 0;
198     virtual bool renameFile(const FileType fileType,
199                              const QString &filePath,
200                              const QString &newFilePath) = 0;
201     // by default returns false
202     virtual bool deploysFolder(const QString &folder) const;
203
204
205     QList<NodesWatcher*> watchers() const;
206     void registerWatcher(NodesWatcher *watcher);
207     void unregisterWatcher(NodesWatcher *watcher);
208
209     void accept(NodesVisitor *visitor);
210
211     static bool sortNodesByPath(Node *n1, Node *n2);
212
213 protected:
214     // this is just the in-memory representation, a subclass
215     // will add the persistent stuff
216     explicit ProjectNode(const QString &projectFilePath);
217
218     // to be called in implementation of
219     // the corresponding public methods
220     void addProjectNodes(const QList<ProjectNode*> &subProjects);
221     void removeProjectNodes(const QList<ProjectNode*> &subProjects);
222
223     void addFolderNodes(const QList<FolderNode*> &subFolders, FolderNode *parentFolder);
224     void removeFolderNodes(const QList<FolderNode*> &subFolders, FolderNode *parentFolder);
225
226     void addFileNodes(const QList<FileNode*> &files, FolderNode *parentFolder);
227     void removeFileNodes(const QList<FileNode*> &files, FolderNode *parentFolder);
228
229 private slots:
230     void watcherDestroyed(QObject *watcher);
231
232 private:
233     QList<ProjectNode*> m_subProjectNodes;
234     QList<NodesWatcher*> m_watchers;
235
236     // let SessionNode call setParentFolderNode
237     friend class SessionNode;
238 };
239
240 class PROJECTEXPLORER_EXPORT SessionNode : public FolderNode {
241     Q_OBJECT
242 public:
243     SessionNode(const QString &sessionFilePath, QObject *parentObject);
244
245     QList<ProjectNode*> projectNodes() const;
246
247     QList<NodesWatcher*> watchers() const;
248     void registerWatcher(NodesWatcher *watcher);
249     void unregisterWatcher(NodesWatcher *watcher);
250
251     void accept(NodesVisitor *visitor);
252
253 protected:
254     void addProjectNodes(const QList<ProjectNode*> &projectNodes);
255     void removeProjectNodes(const QList<ProjectNode*> &projectNodes);
256
257 private slots:
258     void watcherDestroyed(QObject *watcher);
259
260 private:
261     QList<ProjectNode*> m_projectNodes;
262     QList<NodesWatcher*> m_watchers;
263 };
264
265 class PROJECTEXPLORER_EXPORT NodesWatcher : public QObject {
266     Q_OBJECT
267 public:
268     explicit NodesWatcher(QObject *parent = 0);
269
270 signals:
271     // folders & projects
272     void foldersAboutToBeAdded(FolderNode *parentFolder,
273                                const QList<FolderNode*> &newFolders);
274     void foldersAdded();
275
276     void foldersAboutToBeRemoved(FolderNode *parentFolder,
277                                const QList<FolderNode*> &staleFolders);
278     void foldersRemoved();
279
280     // files
281     void filesAboutToBeAdded(FolderNode *folder,
282                                const QList<FileNode*> &newFiles);
283     void filesAdded();
284
285     void filesAboutToBeRemoved(FolderNode *folder,
286                                const QList<FileNode*> &staleFiles);
287     void filesRemoved();
288
289 private:
290
291     // let project & session emit signals
292     friend class ProjectNode;
293     friend class SessionNode;
294 };
295
296
297 } // namespace ProjectExplorer
298
299 // HACK: THERE SHOULD BE ONE PLACE TO MAKE THE FILE ENDING->FILE TYPE ASSOCIATION
300 ProjectExplorer::FileType typeForFileName(const Core::MimeDatabase *db, const QFileInfo &file);
301
302 #endif // PROJECTNODES_H