projecttree.cpp 15.1 KB
Newer Older
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
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** 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://www.qt.io/licensing.  For further information
** use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** 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
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/

#include "projecttree.h"
#include "projecttreewidget.h"
#include "session.h"
#include "project.h"
#include "projectnodes.h"
36
#include "projectexplorerconstants.h"
37
38
39
40
41
42
43

#include <utils/algorithm.h>
#include <coreplugin/icore.h>
#include <coreplugin/editormanager/ieditor.h>
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/infobar.h>
#include <coreplugin/vcsmanager.h>
44
45
#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/actionmanager/actionmanager.h>
46
#include <coreplugin/documentmanager.h>
47
48

#include <QApplication>
49
#include <QTimer>
50
51
52
53
54
55
56
57
58
59
60
61
62
63

namespace {
const char EXTERNAL_FILE_WARNING[] = "ExternalFile";
}

using namespace ProjectExplorer;
using namespace Internal;

ProjectTree *ProjectTree::s_instance = 0;

ProjectTree::ProjectTree(QObject *parent)
    : QObject(parent),
      m_currentNode(0),
      m_currentProject(0),
64
65
      m_resetCurrentNodeFolder(false),
      m_resetCurrentNodeFile(false),
66
67
      m_resetCurrentNodeProject(false),
      m_focusForContextMenu(0)
68
69
70
{
    s_instance = this;

71
    connect(Core::EditorManager::instance(), &Core::EditorManager::currentEditorChanged,
72
73
74
75
            this, &ProjectTree::documentManagerCurrentFileChanged);

    connect(qApp, &QApplication::focusChanged,
            this, &ProjectTree::focusChanged);
76
77
78
79
80
81
82

    connect(SessionManager::instance(), &SessionManager::projectAdded,
            this, &ProjectTree::updateDefaultLocationForNewFiles);
    connect(SessionManager::instance(), &SessionManager::projectRemoved,
            this, &ProjectTree::updateDefaultLocationForNewFiles);
    connect(SessionManager::instance(), &SessionManager::startupProjectChanged,
            this, &ProjectTree::updateDefaultLocationForNewFiles);
83
84
}

85
86
87
88
89
90
91
void ProjectTree::aboutToShutDown()
{
    disconnect(qApp, &QApplication::focusChanged,
               s_instance, &ProjectTree::focusChanged);
    s_instance->update(0, 0);
}

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
ProjectTree *ProjectTree::instance()
{
    return s_instance;
}

Project *ProjectTree::currentProject()
{
    return s_instance->m_currentProject;
}

Node *ProjectTree::currentNode()
{
    return s_instance->m_currentNode;
}

void ProjectTree::registerWidget(ProjectTreeWidget *widget)
{
    s_instance->m_projectTreeWidgets.append(widget);
    if (hasFocus(widget))
        s_instance->updateFromProjectTreeWidget(widget);
}

void ProjectTree::unregisterWidget(ProjectTreeWidget *widget)
{
    s_instance->m_projectTreeWidgets.removeOne(widget);
    if (hasFocus(widget))
        s_instance->updateFromDocumentManager();
}

void ProjectTree::nodeChanged(ProjectTreeWidget *widget)
{
    if (hasFocus(widget))
        s_instance->updateFromProjectTreeWidget(widget);
}

void ProjectTree::focusChanged()
{
    s_instance->updateFromFocus();
}

void ProjectTree::updateFromFocus(bool invalidCurrentNode)
{
134
135
136
137
    ProjectTreeWidget *focus = m_focusForContextMenu;
    if (!focus)
        focus = Utils::findOrDefault(m_projectTreeWidgets,
                                     &ProjectTree::hasFocus);
138
139
140

    if (focus)
        updateFromProjectTreeWidget(focus);
141
    else
142
143
144
145
146
        updateFromDocumentManager(invalidCurrentNode);
}

void ProjectTree::updateFromProjectTreeWidget(ProjectTreeWidget *widget)
{
Nikita Baryshnikov's avatar
Nikita Baryshnikov committed
147
148
    Node *currentNode = widget->currentNode();
    Project *project = projectForNode(currentNode);
149
150
151
152
153
154

    update(currentNode, project);
}

void ProjectTree::documentManagerCurrentFileChanged()
{
155
    updateFromFocus();
156
157
158
159
160
161
162
}

Project *ProjectTree::projectForNode(Node *node)
{
    if (!node)
        return 0;

163
    FolderNode *rootProjectNode = dynamic_cast<FolderNode*>(node);
164
165
166
167
168
169
170
171
172
173
174
175
176
    if (!rootProjectNode)
        rootProjectNode = node->parentFolderNode();

    while (rootProjectNode && rootProjectNode->parentFolderNode() != SessionManager::sessionNode())
        rootProjectNode = rootProjectNode->parentFolderNode();

    Q_ASSERT(rootProjectNode);

    return Utils::findOrDefault(SessionManager::projects(), Utils::equal(&Project::rootProjectNode, rootProjectNode));
}

void ProjectTree::updateFromDocumentManager(bool invalidCurrentNode)
{
177
178
    Core::IDocument *document = Core::EditorManager::currentDocument();
    const QString &fileName = document ? document->filePath().toString() : QString();
179

Nikita Baryshnikov's avatar
Nikita Baryshnikov committed
180
    Node *currentNode = 0;
181
182
183
184
185
    if (!invalidCurrentNode && m_currentNode && m_currentNode->path() == fileName)
        currentNode = m_currentNode;
    else
        currentNode = ProjectTreeWidget::nodeForFile(fileName);

Nikita Baryshnikov's avatar
Nikita Baryshnikov committed
186
    Project *project = projectForNode(currentNode);
187
188
189
190
191
192
193
194
195
196

    update(currentNode, project);
    foreach (ProjectTreeWidget *widget, m_projectTreeWidgets)
        widget->sync(currentNode);
}

void ProjectTree::update(Node *node, Project *project)
{
    if (project != m_currentProject) {
        if (m_currentProject) {
197
            disconnect(m_currentProject, &Project::projectContextUpdated,
198
                       this, &ProjectTree::updateContext);
199
            disconnect(m_currentProject, &Project::projectLanguagesUpdated,
200
201
202
203
204
205
                       this, &ProjectTree::updateContext);
        }

        m_currentProject = project;

        if (m_currentProject) {
206
            connect(m_currentProject, &Project::projectContextUpdated,
207
                    this, &ProjectTree::updateContext);
208
            connect(m_currentProject, &Project::projectLanguagesUpdated,
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
                    this, &ProjectTree::updateContext);
        }
    }

    if (!node && Core::EditorManager::currentDocument()) {
        connect(Core::EditorManager::currentDocument(), &Core::IDocument::changed,
                this, &ProjectTree::updateExternalFileWarning,
                Qt::UniqueConnection);
    }

    if (node != m_currentNode) {
        m_currentNode = node;
        emit currentNodeChanged(m_currentNode, project);
    }

224
225
    emit currentProjectChanged(m_currentProject);

226
    updateDefaultLocationForNewFiles();
227
228
229
    updateContext();
}

230
231
232
233
234
235
236
237
238
239
void ProjectTree::updateDefaultLocationForNewFiles()
{
    if (m_currentProject)
        Core::DocumentManager::setDefaultLocationForNewFiles(m_currentProject->projectDirectory().toString());
    else if (SessionManager::startupProject())
        Core::DocumentManager::setDefaultLocationForNewFiles(SessionManager::startupProject()->projectDirectory().toString());
    else
        Core::DocumentManager::setDefaultLocationForNewFiles(QString());
}

240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
void ProjectTree::updateContext()
{
    Core::Context oldContext;
    oldContext.add(m_lastProjectContext);

    Core::Context newContext;
    if (m_currentProject) {
        newContext.add(m_currentProject->projectContext());
        newContext.add(m_currentProject->projectLanguages());

        m_lastProjectContext = newContext;
    } else {
        m_lastProjectContext = Core::Context();
    }

    Core::ICore::updateAdditionalContexts(oldContext, newContext);
}

258
259
void ProjectTree::emitNodeUpdated(Node *node)
{
260
261
    if (!isInNodeHierarchy(node))
        return;
262
263
264
265
266
    emit nodeUpdated(node);
}

void ProjectTree::emitAboutToChangeShowInSimpleTree(FolderNode *node)
{
267
268
    if (!isInNodeHierarchy(node))
        return;
269
270
271
272
273
    emit aboutToChangeShowInSimpleTree(node);
}

void ProjectTree::emitShowInSimpleTreeChanged(FolderNode *node)
{
274
275
    if (!isInNodeHierarchy(node))
        return;
276
277
278
279
280
    emit showInSimpleTreeChanged(node);
}

void ProjectTree::emitFoldersAboutToBeAdded(FolderNode *parentFolder, const QList<FolderNode *> &newFolders)
{
281
282
283
    if (!isInNodeHierarchy(parentFolder))
        return;

284
285
286
    emit foldersAboutToBeAdded(parentFolder, newFolders);
}

287
void ProjectTree::emitFoldersAdded(FolderNode *folder)
288
{
289
290
291
    if (!isInNodeHierarchy(folder))
        return;

292
293
294
295
296
297
298
299
300
    emit foldersAdded();

    if (Utils::anyOf(m_projectTreeWidgets, &ProjectTreeWidget::hasFocus))
        return;

    updateFromDocumentManager();
}

void ProjectTree::emitFoldersAboutToBeRemoved(FolderNode *parentFolder, const QList<FolderNode *> &staleFolders)
301
{
302
303
304
    if (!isInNodeHierarchy(parentFolder))
        return;

305
306
    Node *n = ProjectTree::currentNode();
    while (n) {
307
308
        if (FolderNode *fn = dynamic_cast<FolderNode *>(n)) {
            if (staleFolders.contains(fn)) {
309
310
                ProjectNode *pn = n->projectNode();
                // Make sure the node we are switching too isn't going to be removed also
311
                while (staleFolders.contains(pn))
312
313
                    pn = pn->parentFolderNode()->projectNode();
                m_resetCurrentNodeFolder = true;
314
                break;
315
316
317
318
            }
        }
        n = n->parentFolderNode();
    }
319
    emit foldersAboutToBeRemoved(parentFolder, staleFolders);
320
321
}

322
void ProjectTree::emitFoldersRemoved(FolderNode *folder)
323
{
324
325
326
    if (!isInNodeHierarchy(folder))
        return;

327
328
329
330
331
332
    emit foldersRemoved();

    if (m_resetCurrentNodeFolder) {
        updateFromFocus(true);
        m_resetCurrentNodeFolder = false;
    }
333
334
}

335
void ProjectTree::emitFilesAboutToBeAdded(FolderNode *folder, const QList<FileNode *> &newFiles)
336
{
337
338
    if (!isInNodeHierarchy(folder))
        return;
339
340
341
    emit filesAboutToBeAdded(folder, newFiles);
}

342
void ProjectTree::emitFilesAdded(FolderNode *folder)
343
{
344
345
346
    if (!isInNodeHierarchy(folder))
        return;

347
348
349
350
351
352
    emit filesAdded();

    if (Utils::anyOf(m_projectTreeWidgets, &ProjectTreeWidget::hasFocus))
        return;

    updateFromDocumentManager();
353
354
}

355
void ProjectTree::emitFilesAboutToBeRemoved(FolderNode *folder, const QList<FileNode *> &staleFiles)
356
{
357
358
359
    if (!isInNodeHierarchy(folder))
        return;

360
361
    if (FileNode *fileNode = dynamic_cast<FileNode *>(m_currentNode))
        if (staleFiles.contains(fileNode))
362
            m_resetCurrentNodeFile = true;
363
364

    emit filesAboutToBeRemoved(folder, staleFiles);
365
366
}

367
void ProjectTree::emitFilesRemoved(FolderNode *folder)
368
{
369
370
    if (!isInNodeHierarchy(folder))
        return;
371
372
373
374
375
376
    emit filesRemoved();

    if (m_resetCurrentNodeFile) {
        updateFromFocus(true);
        m_resetCurrentNodeFile = false;
    }
377
378
}

379
void ProjectTree::emitNodeSortKeyAboutToChange(Node *node)
380
{
381
382
    if (!isInNodeHierarchy(node))
        return;
383
    emit nodeSortKeyAboutToChange(node);
384
385
}

386
void ProjectTree::emitNodeSortKeyChanged(Node *node)
387
{
388
389
    if (!isInNodeHierarchy(node))
        return;
390
    emit nodeSortKeyChanged();
391
392
}

393
394
395
396
397
398
void ProjectTree::collapseAll()
{
    if (m_focusForContextMenu)
        m_focusForContextMenu->collapseAll();
}

399
400
401
402
403
404
405
406
407
408
409
410
411
void ProjectTree::updateExternalFileWarning()
{
    Core::IDocument *document = qobject_cast<Core::IDocument *>(sender());
    if (!document || document->filePath().isEmpty())
        return;
    Core::InfoBar *infoBar = document->infoBar();
    Core::Id externalFileId(EXTERNAL_FILE_WARNING);
    if (!document->isModified()) {
        infoBar->removeInfo(externalFileId);
        return;
    }
    if (!infoBar->canInfoBeAdded(externalFileId))
        return;
412
    const Utils::FileName fileName = document->filePath();
413
414
415
416
    const QList<Project *> projects = SessionManager::projects();
    if (projects.isEmpty())
        return;
    foreach (Project *project, projects) {
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
        Utils::FileName projectDir = project->projectDirectory();
        if (projectDir.isEmpty())
            continue;
        if (fileName.isChildOf(projectDir))
            return;
        // External file. Test if it under the same VCS
        QString topLevel;
        if (Core::VcsManager::findVersionControlForDirectory(projectDir.toString(), &topLevel)
                && fileName.isChildOf(Utils::FileName::fromString(topLevel))) {
            return;
        }
    }
    infoBar->addInfo(Core::InfoBarEntry(externalFileId,
                                        tr("<b>Warning:</b> This file is outside the project directory."),
                                        Core::InfoBarEntry::GlobalSuppressionEnabled));
}

Nikita Baryshnikov's avatar
Nikita Baryshnikov committed
434
bool ProjectTree::hasFocus(ProjectTreeWidget *widget)
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
    return widget
            && ((widget->focusWidget() && widget->focusWidget()->hasFocus())
                || s_instance->m_focusForContextMenu == widget);
}

void ProjectTree::showContextMenu(ProjectTreeWidget *focus, const QPoint &globalPos, Node *node)
{
    QMenu *contextMenu = 0;

    if (!node)
        node = SessionManager::sessionNode();
    if (node->nodeType() != SessionNodeType) {
        Project *project = SessionManager::projectForNode(node);

        emit s_instance->aboutToShowContextMenu(project, node);
        switch (node->nodeType()) {
        case ProjectNodeType:
            if (node->parentFolderNode() == SessionManager::sessionNode())
                contextMenu = Core::ActionManager::actionContainer(Constants::M_PROJECTCONTEXT)->menu();
            else
                contextMenu = Core::ActionManager::actionContainer(Constants::M_SUBPROJECTCONTEXT)->menu();
            break;
        case VirtualFolderNodeType:
        case FolderNodeType:
            contextMenu = Core::ActionManager::actionContainer(Constants::M_FOLDERCONTEXT)->menu();
            break;
        case FileNodeType:
            contextMenu = Core::ActionManager::actionContainer(Constants::M_FILECONTEXT)->menu();
            break;
        default:
            qWarning("ProjectExplorerPlugin::showContextMenu - Missing handler for node type");
        }
    } else { // session item
        emit s_instance->aboutToShowContextMenu(0, node);

        contextMenu = Core::ActionManager::actionContainer(Constants::M_SESSIONCONTEXT)->menu();
    }

    if (contextMenu && contextMenu->actions().count() > 0) {
        contextMenu->popup(globalPos);
        s_instance->m_focusForContextMenu = focus;
        connect(contextMenu, &QMenu::aboutToHide,
                s_instance, &ProjectTree::hideContextMenu,
                Qt::UniqueConnection);
    }
}

void ProjectTree::hideContextMenu()
{
    m_focusForContextMenu = 0;
486
}
487
488
489
490
491
492
493
494
495
496
497

bool ProjectTree::isInNodeHierarchy(Node *n)
{
    Node *sessionNode = SessionManager::sessionNode();
    do {
        if (n == sessionNode)
            return true;
        n = n->parentFolderNode();
    } while (n);
    return false;
}