aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/projectexplorer/projectnodes.h
blob: 9d159aeea35b74fe6548412e11482cf24aa8b1a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#pragma once

#include "projectexplorer_export.h"

#include <QIcon>
#include <QStringList>

#include <utils/fileutils.h>
#include <utils/id.h>

#include <functional>
#include <optional>
#include <variant>

namespace Utils { class MimeType; }

namespace ProjectExplorer {

class BuildSystem;
class Project;

// File types common for qt projects
enum class FileType : quint16 {
    Unknown = 0,
    Header,
    Source,
    Form,
    StateChart,
    Resource,
    QML,
    Project,
    App,
    Lib,
    FileTypeSize
};

enum class ProductType { App, Lib, Other, None };

enum ProjectAction {
    // Special value to indicate that the actions are handled by the parent
    InheritedFromParent,
    AddSubProject,
    AddExistingProject,
    RemoveSubProject,
    // Let's the user select to which project file
    // the file is added
    AddNewFile,
    AddExistingFile,
    // Add files, which match user defined filters,
    // from an existing directory and its subdirectories
    AddExistingDirectory,
    // Removes a file from the project, optionally also
    // delete it on disc
    RemoveFile,
    // Deletes a file from the file system, informs the project
    // that a file was deleted
    // DeleteFile is a define on windows...
    EraseFile,
    Rename,
    // hides actions that use the path(): Open containing folder, open terminal here and Find in Directory
    HidePathActions,
    HideFileActions,
    HideFolderActions,
};

enum class RemovedFilesFromProject { Ok, Wildcard, Error };

class FileNode;
class FolderNode;
class ProjectNode;
class ContainerNode;

class PROJECTEXPLORER_EXPORT DirectoryIcon
{
public:
    explicit DirectoryIcon(const QString &overlay);

    QIcon icon() const; // only safe in UI thread

private:
    QString m_overlay;
    static QHash<QString, QIcon> m_cache;
};

using IconCreator = std::function<QIcon()>;

// Documentation inside.
class PROJECTEXPLORER_EXPORT Node
{
public:
    enum PriorityLevel {
        DefaultPriority = 0,
        DefaultFilePriority = 100000,
        DefaultFolderPriority = 200000,
        DefaultVirtualFolderPriority = 300000,
        DefaultProjectPriority = 400000,
        DefaultProjectFilePriority = 500000
    };

    virtual ~Node();

    virtual bool isFolderNodeType() const { return false; }
    virtual bool isProjectNodeType() const { return false; }
    virtual bool isVirtualFolderType() const { return false; }

    int priority() const;

    ProjectNode *parentProjectNode() const; // parent project, will be nullptr for the top-level project
    FolderNode *parentFolderNode() const; // parent folder or project

    ProjectNode *managingProject();  // project managing this node.
                                     // result is the container's rootProject node if this is a project container node
                                     // (i.e. possibly null)
                                     // or node if node is a top-level ProjectNode directly below a container
                                     // or node->parentProjectNode() for all other cases.
    const ProjectNode *managingProject() const; // see above.

    Project *getProject() const;

    const Utils::FilePath &filePath() const;  // file system path
    int line() const;
    virtual QString displayName() const;
    virtual QString tooltip() const;
    bool isEnabled() const;
    bool listInProject() const;
    bool isGenerated() const;

    virtual bool supportsAction(ProjectAction action, const Node *node) const;

    void setEnabled(bool enabled);
    void setAbsoluteFilePathAndLine(const Utils::FilePath &filePath, int line);

    virtual FileNode *asFileNode() { return nullptr; }
    virtual const FileNode *asFileNode() const { return nullptr; }
    virtual FolderNode *asFolderNode() { return nullptr; }
    virtual const FolderNode *asFolderNode() const { return nullptr; }
    virtual ProjectNode *asProjectNode() { return nullptr; }
    virtual const ProjectNode *asProjectNode() const { return nullptr; }
    virtual ContainerNode *asContainerNode() { return nullptr; }
    virtual const ContainerNode *asContainerNode() const { return nullptr; }

    virtual QString buildKey() const { return {}; }

    static bool sortByPath(const Node *a, const Node *b);
    void setParentFolderNode(FolderNode *parentFolder);

    void setListInProject(bool l);
    void setIsGenerated(bool g);
    void setPriority(int priority);
    void setLine(int line);

    static FileType fileTypeForMimeType(const Utils::MimeType &mt);
    static FileType fileTypeForFileName(const Utils::FilePath &file);

    Utils::FilePath path() const { return pathOrDirectory(false); }
    Utils::FilePath directory() const { return pathOrDirectory(true); }

protected:
    Node();
    Node(const Node &other) = delete;
    bool operator=(const Node &other) = delete;

    void setFilePath(const Utils::FilePath &filePath);

private:
    Utils::FilePath pathOrDirectory(bool dir) const;

    FolderNode *m_parentFolderNode = nullptr;
    Utils::FilePath m_filePath;
    int m_line = -1;
    int m_priority = DefaultPriority;

    enum NodeFlag : quint16 {
        FlagNone = 0,
        FlagIsEnabled = 1 << 0,
        FlagIsGenerated = 1 << 1,
        FlagListInProject = 1 << 2,
    };
    NodeFlag m_flags = FlagIsEnabled;
};

class PROJECTEXPLORER_EXPORT FileNode : public Node
{
public:
    FileNode(const Utils::FilePath &filePath, const FileType fileType);

    FileNode *clone() const;

    FileType fileType() const;

    FileNode *asFileNode() final { return this; }
    const FileNode *asFileNode() const final { return this; }

    bool supportsAction(ProjectAction action, const Node *node) const override;
    QString displayName() const override;

    bool hasError() const;
    void setHasError(const bool error);
    void setHasError(const bool error) const;

    QIcon icon() const;
    void setIcon(const QIcon icon);

    bool useUnavailableMarker() const;
    void setUseUnavailableMarker(bool useUnavailableMarker);

private:
    FileType m_fileType;
    mutable QIcon m_icon;
    mutable bool m_hasError = false;
    bool m_useUnavailableMarker = false;
};

// Documentation inside.
class PROJECTEXPLORER_EXPORT FolderNode : public Node
{
public:
    explicit FolderNode(const Utils::FilePath &folderPath);

    QString displayName() const override;
    // only safe from UI thread
    QIcon icon() const;

    bool isFolderNodeType() const override { return true; }

    Node *findNode(const std::function<bool(Node *)> &filter);
    QList<Node *> findNodes(const std::function<bool(Node *)> &filter);

    void forEachNode(const std::function<void(FileNode *)> &fileTask,
                     const std::function<void(FolderNode *)> &folderTask = {},
                     const std::function<bool(const FolderNode *)> &folderFilterTask = {}) const;
    void forEachGenericNode(const std::function<void(Node *)> &genericTask) const;
    void forEachProjectNode(const std::function<void(const ProjectNode *)> &genericTask) const;
    void forEachFileNode(const std::function<void(FileNode *)> &fileTask) const;
    void forEachFolderNode(const std::function<void(FolderNode *)> &folderTask) const;
    ProjectNode *findProjectNode(const std::function<bool(const ProjectNode *)> &predicate); // recursive
    FolderNode *findChildFolderNode(const std::function<bool (FolderNode *)> &predicate) const; // non-recursive
    FileNode *findChildFileNode(const std::function<bool (FileNode *)> &predicate) const; // non-recursive
    const QList<Node *> nodes() const;
    FileNode *fileNode(const Utils::FilePath &file) const;
    FolderNode *folderNode(const Utils::FilePath &directory) const;

    using FolderNodeFactory = std::function<std::unique_ptr<FolderNode>(const Utils::FilePath &)>;
    void addNestedNodes(std::vector<std::unique_ptr<FileNode>> &&files,
                        const Utils::FilePath &overrideBaseDir = Utils::FilePath(),
                        const FolderNodeFactory &factory
                        = [](const Utils::FilePath &fn) { return std::make_unique<FolderNode>(fn); });
    void addNestedNode(std::unique_ptr<FileNode> &&fileNode,
                       const Utils::FilePath &overrideBaseDir = Utils::FilePath(),
                       const FolderNodeFactory &factory
                       = [](const Utils::FilePath &fn) { return std::make_unique<FolderNode>(fn); });
    virtual void compress();

    // takes ownership of newNode.
    // Will delete newNode if oldNode is not a child of this node.
    bool replaceSubtree(Node *oldNode, std::unique_ptr<Node> &&newNode);

    void setDisplayName(const QString &name);
    // you have to make sure the QIcon is created in the UI thread if you are calling setIcon(QIcon)
    void setIcon(const QIcon &icon);
    void setIcon(const DirectoryIcon &directoryIcon);
    void setIcon(const QString &path);
    void setIcon(const IconCreator &iconCreator);

    class LocationInfo
    {
    public:
        LocationInfo() = default;
        LocationInfo(const QString &dn,
                     const Utils::FilePath &p,
                     const int l = 0,
                     const unsigned int prio = 0)
            : path(p)
            , line(l)
            , priority(prio)
            , displayName(dn)
        {}

        Utils::FilePath path;
        int line = -1;
        unsigned int priority = 0;
        QString displayName;
    };
    void setLocationInfo(const QVector<LocationInfo> &info);
    const QVector<LocationInfo> locationInfo() const;

    QString addFileFilter() const;
    void setAddFileFilter(const QString &filter) { m_addFileFilter = filter; }

    bool supportsAction(ProjectAction action, const Node *node) const override;

    virtual bool addFiles(const Utils::FilePaths &filePaths, Utils::FilePaths *notAdded = nullptr);
    virtual RemovedFilesFromProject removeFiles(const Utils::FilePaths &filePaths,
                                                Utils::FilePaths *notRemoved = nullptr);
    virtual bool deleteFiles(const Utils::FilePaths &filePaths);
    virtual bool canRenameFile(const Utils::FilePath &oldFilePath,
                               const Utils::FilePath &newFilePath);
    virtual bool renameFile(const Utils::FilePath &oldFilePath, const Utils::FilePath &newFilePath);
    virtual bool addDependencies(const QStringList &dependencies);

    class AddNewInformation
    {
    public:
        AddNewInformation(const QString &name, int p)
            :displayName(name), priority(p)
        { }
        QString displayName;
        int priority;
    };

    virtual AddNewInformation addNewInformation(const Utils::FilePaths &files, Node *context) const;


    // determines if node will be shown in the flat view, by default folder and projects aren't shown
    virtual bool showInSimpleTree() const;

    // determines if node will always be shown when hiding empty directories
    bool showWhenEmpty() const;
    void setShowWhenEmpty(bool showWhenEmpty);

    void addNode(std::unique_ptr<Node> &&node);

    bool isEmpty() const;

    FolderNode *asFolderNode() override { return this; }
    const FolderNode *asFolderNode() const override { return this; }

protected:
    virtual void handleSubTreeChanged(FolderNode *node);

    std::vector<std::unique_ptr<Node>> m_nodes;
    QVector<LocationInfo> m_locations;

private:
    std::unique_ptr<Node> takeNode(Node *node);

    QString m_displayName;
    QString m_addFileFilter;
    mutable std::variant<QIcon, DirectoryIcon, QString, IconCreator> m_icon;
    bool m_showWhenEmpty = false;
};

class PROJECTEXPLORER_EXPORT VirtualFolderNode : public FolderNode
{
public:
    explicit VirtualFolderNode(const Utils::FilePath &folderPath);

    bool isFolderNodeType() const override { return false; }
    bool isVirtualFolderType() const override { return true; }

    bool isSourcesOrHeaders() const { return m_isSourcesOrHeaders; }
    void setIsSourcesOrHeaders(bool on) { m_isSourcesOrHeaders = on; }

private:
    bool m_isSourcesOrHeaders = false; // "Sources" or "Headers"
};

// Documentation inside.
class PROJECTEXPLORER_EXPORT ProjectNode : public FolderNode
{
public:
    explicit ProjectNode(const Utils::FilePath &projectFilePath);

    virtual bool canAddSubProject(const Utils::FilePath &proFilePath) const;
    virtual bool addSubProject(const Utils::FilePath &proFile);
    virtual QStringList subProjectFileNamePatterns() const;
    virtual bool removeSubProject(const Utils::FilePath &proFilePath);
    virtual std::optional<Utils::FilePath> visibleAfterAddFileAction() const {
        return std::nullopt;
    }

    bool isFolderNodeType() const override { return false; }
    bool isProjectNodeType() const override { return true; }
    bool showInSimpleTree() const override { return true; }

    bool addFiles(const Utils::FilePaths &filePaths, Utils::FilePaths *notAdded = nullptr) final;
    RemovedFilesFromProject removeFiles(const Utils::FilePaths &filePaths,
                                        Utils::FilePaths *notRemoved = nullptr) final;
    bool deleteFiles(const Utils::FilePaths &filePaths) final;
    bool canRenameFile(const Utils::FilePath &oldFilePath, const Utils::FilePath &newFilePath) final;
    bool renameFile(const Utils::FilePath &oldFilePath, const Utils::FilePath &newFilePath) final;
    bool addDependencies(const QStringList &dependencies) final;
    bool supportsAction(ProjectAction action, const Node *node) const final;

    // by default returns false
    virtual bool deploysFolder(const QString &folder) const;

    ProjectNode *projectNode(const Utils::FilePath &file) const;

    ProjectNode *asProjectNode() final { return this; }
    const ProjectNode *asProjectNode() const final { return this; }

    virtual QStringList targetApplications() const { return {}; }
    virtual bool parseInProgress() const { return false; }

    virtual bool validParse() const { return false; }
    virtual QVariant data(Utils::Id role) const;
    virtual bool setData(Utils::Id role, const QVariant &value) const;

    bool isProduct() const { return m_productType != ProductType::None; }
    ProductType productType() const { return m_productType; }

    // TODO: Currently used only for "Build for current run config" functionality, but we should
    //       probably use it to centralize the node-specific "Build" functionality that
    //       currently each project manager plugin adds to the context menu by itself.
    //       The function should then move up to the Node class, so it can also serve the
    //       "build single file" case.
    virtual void build() {}

    void setFallbackData(Utils::Id key, const QVariant &value);

protected:
    void setProductType(ProductType type) { m_productType = type; }

    QString m_target;

private:
    BuildSystem *buildSystem() const;

    QHash<Utils::Id, QVariant> m_fallbackData; // Used in data(), unless overridden.
    ProductType m_productType = ProductType::None;
};

class PROJECTEXPLORER_EXPORT ContainerNode : public FolderNode
{
public:
    ContainerNode(Project *project);

    QString displayName() const final;
    bool supportsAction(ProjectAction action, const Node *node) const final;

    bool isFolderNodeType() const override { return false; }
    bool isProjectNodeType() const override { return true; }

    ContainerNode *asContainerNode() final { return this; }
    const ContainerNode *asContainerNode() const final { return this; }

    ProjectNode *rootProjectNode() const;
    Project *project() const { return m_project; }

    void removeAllChildren();

private:
    void handleSubTreeChanged(FolderNode *node) final;

    Project *m_project;
};

class PROJECTEXPLORER_EXPORT ResourceFileNode : public ProjectExplorer::FileNode
{
public:
    ResourceFileNode(const Utils::FilePath &filePath, const QString &qrcPath, const QString &displayName);

    QString displayName() const override;
    QString qrcPath() const;
    bool supportsAction(ProjectExplorer::ProjectAction action, const Node *node) const override;

private:
    QString m_qrcPath;
    QString m_displayName;
};

} // namespace ProjectExplorer

Q_DECLARE_METATYPE(ProjectExplorer::Node *)
Q_DECLARE_METATYPE(ProjectExplorer::FolderNode *)