foldernavigationwidget.cpp 18.4 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
con's avatar
con committed
2
**
3 4
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
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
** 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
12 13 14
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
15
**
16 17 18 19 20 21 22
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
con's avatar
con committed
23
**
hjk's avatar
hjk committed
24
****************************************************************************/
hjk's avatar
hjk committed
25

con's avatar
con committed
26
#include "foldernavigationwidget.h"
27
#include "projectexplorer.h"
28
#include "projectexplorericons.h"
con's avatar
con committed
29

30
#include <coreplugin/actionmanager/command.h>
31
#include <coreplugin/documentmanager.h>
con's avatar
con committed
32
#include <coreplugin/icore.h>
33
#include <coreplugin/idocument.h>
34
#include <coreplugin/fileiconprovider.h>
con's avatar
con committed
35
#include <coreplugin/editormanager/editormanager.h>
36
#include <coreplugin/editormanager/ieditor.h>
Robert Loehning's avatar
Robert Loehning committed
37
#include <coreplugin/fileutils.h>
38

39
#include <utils/algorithm.h>
40
#include <utils/hostosinfo.h>
41
#include <utils/qtcassert.h>
42
#include <utils/navigationtreeview.h>
Ulf Hermann's avatar
Ulf Hermann committed
43
#include <utils/utilsicons.h>
con's avatar
con committed
44

45 46
#include <QComboBox>
#include <QHeaderView>
47
#include <QSize>
48
#include <QTimer>
49 50 51 52 53 54
#include <QFileSystemModel>
#include <QVBoxLayout>
#include <QToolButton>
#include <QAction>
#include <QMenu>
#include <QContextMenuEvent>
55 56
#include <QDir>
#include <QFileInfo>
con's avatar
con committed
57

58 59
const int PATH_ROLE = Qt::UserRole;
const int ID_ROLE = Qt::UserRole + 1;
60
const int SORT_ROLE = Qt::UserRole + 2;
61

62 63
const char PROJECTSDIRECTORYROOT_ID[] = "A.Projects";

con's avatar
con committed
64
namespace ProjectExplorer {
hjk's avatar
hjk committed
65 66
namespace Internal {

67
static FolderNavigationWidgetFactory *m_instance = nullptr;
68

69
QVector<FolderNavigationWidgetFactory::RootDirectory>
70
    FolderNavigationWidgetFactory::m_rootDirectories;
71

72
// FolderNavigationModel: Shows path as tooltip.
73 74 75
class FolderNavigationModel : public QFileSystemModel
{
public:
76
    explicit FolderNavigationModel(QObject *parent = nullptr);
77
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
78
    Qt::DropActions supportedDragActions() const;
79 80
};

81 82
FolderNavigationModel::FolderNavigationModel(QObject *parent) : QFileSystemModel(parent)
{ }
83 84 85 86 87 88 89 90 91

QVariant FolderNavigationModel::data(const QModelIndex &index, int role) const
{
    if (role == Qt::ToolTipRole)
        return QDir::toNativeSeparators(QDir::cleanPath(filePath(index)));
    else
        return QFileSystemModel::data(index, role);
}

92 93 94 95 96
Qt::DropActions FolderNavigationModel::supportedDragActions() const
{
    return Qt::MoveAction;
}

97 98 99 100 101 102 103
static void showOnlyFirstColumn(QTreeView *view)
{
    const int columnCount = view->header()->count();
    for (int i = 1; i < columnCount; ++i)
        view->setColumnHidden(i, true);
}

con's avatar
con committed
104
/*!
105 106 107
    \class FolderNavigationWidget

    Shows a file system tree, with the root directory selectable from a dropdown.
con's avatar
con committed
108

109 110
    \internal
*/
111
FolderNavigationWidget::FolderNavigationWidget(QWidget *parent) : QWidget(parent),
112
    m_listView(new Utils::NavigationTreeView(this)),
113 114
    m_fileSystemModel(new FolderNavigationModel(this)),
    m_filterHiddenFilesAction(new QAction(tr("Show Hidden Files"), this)),
115 116
    m_toggleSync(new QToolButton(this)),
    m_rootSelector(new QComboBox)
con's avatar
con committed
117
{
118
    m_fileSystemModel->setResolveSymlinks(false);
119
    m_fileSystemModel->setIconProvider(Core::FileIconProvider::iconProvider());
120
    QDir::Filters filters = QDir::AllEntries | QDir::NoDotAndDotDot;
121 122
    if (Utils::HostOsInfo::isWindowsHost()) // Symlinked directories can cause file watcher warnings on Win32.
        filters |= QDir::NoSymLinks;
123
    m_fileSystemModel->setFilter(filters);
124
    m_fileSystemModel->setRootPath(QString());
125 126
    m_filterHiddenFilesAction->setCheckable(true);
    setHiddenFilesFilter(false);
127
    m_listView->setIconSize(QSize(16,16));
128
    m_listView->setModel(m_fileSystemModel);
129 130
    m_listView->setDragEnabled(true);
    m_listView->setDragDropMode(QAbstractItemView::DragOnly);
131
    showOnlyFirstColumn(m_listView);
132
    setFocusProxy(m_listView);
con's avatar
con committed
133

134
    auto layout = new QVBoxLayout();
135
    layout->addWidget(m_rootSelector);
136
    layout->addWidget(m_listView);
con's avatar
con committed
137 138 139 140
    layout->setSpacing(0);
    layout->setContentsMargins(0, 0, 0, 0);
    setLayout(layout);

Ulf Hermann's avatar
Ulf Hermann committed
141
    m_toggleSync->setIcon(Utils::Icons::LINK.icon());
142 143 144 145
    m_toggleSync->setCheckable(true);
    m_toggleSync->setToolTip(tr("Synchronize with Editor"));
    setAutoSynchronization(true);

con's avatar
con committed
146
    // connections
147
    connect(m_listView, &QAbstractItemView::activated,
148
            this, [this](const QModelIndex &index) { openItem(index); });
149 150 151 152
    connect(m_filterHiddenFilesAction, &QAction::toggled,
            this, &FolderNavigationWidget::setHiddenFilesFilter);
    connect(m_toggleSync, &QAbstractButton::clicked,
            this, &FolderNavigationWidget::toggleAutoSynchronization);
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
    connect(m_rootSelector,
            static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
            this,
            [this](int index) {
                const auto directory = m_rootSelector->itemData(index).value<Utils::FileName>();
                m_rootSelector->setToolTip(directory.toString());
                setRootDirectory(directory);
            });
    connect(m_rootSelector,
            static_cast<void (QComboBox::*)(int)>(&QComboBox::activated),
            this,
            [this] {
                if (m_autoSync && Core::EditorManager::currentEditor())
                    selectFile(Core::EditorManager::currentEditor()->document()->filePath());
            });
con's avatar
con committed
168 169 170 171 172 173 174
}

void FolderNavigationWidget::toggleAutoSynchronization()
{
    setAutoSynchronization(!m_autoSync);
}

175 176 177 178 179 180 181 182 183
static bool itemLessThan(QComboBox *combo,
                         int index,
                         const FolderNavigationWidgetFactory::RootDirectory &directory)
{
    return combo->itemData(index, SORT_ROLE).toInt() < directory.sortValue
           || (combo->itemData(index, SORT_ROLE).toInt() == directory.sortValue
               && combo->itemData(index, Qt::DisplayRole).toString() < directory.displayName);
}

184
void FolderNavigationWidget::insertRootDirectory(
185
    const FolderNavigationWidgetFactory::RootDirectory &directory)
186
{
187 188 189 190 191 192
    // Find existing. Do not remove yet, to not mess up the current selection.
    int previousIndex = 0;
    while (previousIndex < m_rootSelector->count()
           && m_rootSelector->itemData(previousIndex, ID_ROLE).toString() != directory.id)
        ++previousIndex;
    // Insert sorted.
193
    int index = 0;
194
    while (index < m_rootSelector->count() && itemLessThan(m_rootSelector, index, directory))
195
        ++index;
196 197 198
    m_rootSelector->insertItem(index, directory.displayName);
    if (index <= previousIndex) // item was inserted, update previousIndex
        ++previousIndex;
199 200
    m_rootSelector->setItemData(index, qVariantFromValue(directory.path), PATH_ROLE);
    m_rootSelector->setItemData(index, directory.id, ID_ROLE);
201
    m_rootSelector->setItemData(index, directory.sortValue, SORT_ROLE);
202
    m_rootSelector->setItemData(index, directory.path.toUserOutput(), Qt::ToolTipRole);
203
    m_rootSelector->setItemIcon(index, directory.icon);
204 205 206 207
    if (m_rootSelector->currentIndex() == previousIndex)
        m_rootSelector->setCurrentIndex(index);
    if (previousIndex < m_rootSelector->count())
        m_rootSelector->removeItem(previousIndex);
208 209 210 211
    if (m_autoSync) // we might find a better root for current selection now
        setCurrentEditor(Core::EditorManager::currentEditor());
}

212
void FolderNavigationWidget::removeRootDirectory(const QString &id)
213 214
{
    for (int i = 0; i < m_rootSelector->count(); ++i) {
215
        if (m_rootSelector->itemData(i, ID_ROLE).toString() == id) {
216 217 218 219 220 221 222 223
            m_rootSelector->removeItem(i);
            break;
        }
    }
    if (m_autoSync) // we might need to find a new root for current selection
        setCurrentEditor(Core::EditorManager::currentEditor());
}

con's avatar
con committed
224 225 226 227 228 229 230
bool FolderNavigationWidget::autoSynchronization() const
{
    return m_autoSync;
}

void FolderNavigationWidget::setAutoSynchronization(bool sync)
{
231
    m_toggleSync->setChecked(sync);
con's avatar
con committed
232 233 234 235 236 237
    if (sync == m_autoSync)
        return;

    m_autoSync = sync;

    if (m_autoSync) {
238
        connect(Core::EditorManager::instance(), &Core::EditorManager::currentEditorChanged,
239 240
                this, &FolderNavigationWidget::setCurrentEditor);
        setCurrentEditor(Core::EditorManager::currentEditor());
con's avatar
con committed
241
    } else {
242
        disconnect(Core::EditorManager::instance(), &Core::EditorManager::currentEditorChanged,
243
                this, &FolderNavigationWidget::setCurrentEditor);
con's avatar
con committed
244 245 246
    }
}

247
void FolderNavigationWidget::setCurrentEditor(Core::IEditor *editor)
con's avatar
con committed
248
{
249 250
    if (!editor)
        return;
251 252 253 254 255 256
    const Utils::FileName filePath = editor->document()->filePath();
    // switch to most fitting root
    const int bestRootIndex = bestRootForFile(filePath);
    m_rootSelector->setCurrentIndex(bestRootIndex);
    // select
    selectFile(filePath);
257 258
}

259
void FolderNavigationWidget::selectFile(const Utils::FileName &filePath)
260
{
261 262 263 264 265 266 267 268 269 270 271
    const QModelIndex fileIndex = m_fileSystemModel->index(filePath.toString());
    if (fileIndex.isValid()) {
        // TODO This only scrolls to the right position if all directory contents are loaded.
        // Unfortunately listening to directoryLoaded was still not enough (there might also
        // be some delayed sorting involved?).
        // Use magic timer for scrolling.
        m_listView->setCurrentIndex(fileIndex);
        QTimer::singleShot(200, this, [this, filePath] {
            const QModelIndex fileIndex = m_fileSystemModel->index(filePath.toString());
            m_listView->scrollTo(fileIndex);
        });
272
    }
con's avatar
con committed
273 274
}

275
void FolderNavigationWidget::setRootDirectory(const Utils::FileName &directory)
con's avatar
con committed
276
{
277 278
    const QModelIndex index = m_fileSystemModel->setRootPath(directory.toString());
    m_listView->setRootIndex(index);
279 280
}

281
int FolderNavigationWidget::bestRootForFile(const Utils::FileName &filePath)
282
{
283 284 285 286 287 288 289 290 291 292
    int index = 0; // Computer is default
    int commonLength = 0;
    for (int i = 1; i < m_rootSelector->count(); ++i) {
        const auto root = m_rootSelector->itemData(i).value<Utils::FileName>();
        if (filePath.isChildOf(root) && root.length() > commonLength) {
            index = i;
            commonLength = root.length();
        }
    }
    return index;
293 294
}

295
void FolderNavigationWidget::openItem(const QModelIndex &index)
296
{
297 298 299 300
    QTC_ASSERT(index.isValid(), return);
    // signal "activate" is also sent when double-clicking folders
    // but we don't want to do anything in that case
    if (m_fileSystemModel->isDir(index))
301
        return;
302
    const QString path = m_fileSystemModel->filePath(index);
303 304 305 306 307 308 309 310 311 312 313 314 315 316
    Core::EditorManager::openEditor(path);
}

void FolderNavigationWidget::openProjectsInDirectory(const QModelIndex &index)
{
    QTC_ASSERT(index.isValid() && m_fileSystemModel->isDir(index), return);
    const QFileInfo fi = m_fileSystemModel->fileInfo(index);
    if (!fi.isReadable() || !fi.isExecutable())
        return;
    const QString path = m_fileSystemModel->filePath(index);
    // Try to find project files in directory and open those.
    const QStringList projectFiles = FolderNavigationWidget::projectFilesInDirectory(path);
    if (!projectFiles.isEmpty())
        Core::ICore::instance()->openFiles(projectFiles);
317 318 319 320 321 322
}

void FolderNavigationWidget::contextMenuEvent(QContextMenuEvent *ev)
{
    QMenu menu;
    // Open current item
323
    const QModelIndex current = m_listView->currentIndex();
324
    const bool hasCurrentItem = current.isValid();
325 326
    QAction *actionOpenFile = nullptr;
    QAction *actionOpenProjects = nullptr;
327 328 329 330
    QAction *actionOpenAsProject = nullptr;
    const Utils::FileName filePath = hasCurrentItem ? Utils::FileName::fromString(
                                                          m_fileSystemModel->filePath(current))
                                                    : Utils::FileName();
331 332
    if (hasCurrentItem) {
        const QString fileName = m_fileSystemModel->fileName(current);
333
        if (m_fileSystemModel->isDir(current)) {
334
            actionOpenProjects = menu.addAction(tr("Open Project in \"%1\"").arg(fileName));
335
        } else {
336
            actionOpenFile = menu.addAction(tr("Open \"%1\"").arg(fileName));
337 338 339
            if (ProjectExplorerPlugin::isProjectFile(Utils::FileName::fromString(fileName)))
                actionOpenAsProject = menu.addAction(tr("Open Project \"%1\"").arg(fileName));
        }
340
    }
341 342 343 344 345

    // we need dummy DocumentModel::Entry with absolute file path in it
    // to get EditorManager::addNativeDirAndOpenWithActions() working
    Core::DocumentModel::Entry fakeEntry;
    Core::IDocument document;
346
    document.setFilePath(filePath);
347 348 349
    fakeEntry.document = &document;
    Core::EditorManager::addNativeDirAndOpenWithActions(&menu, &fakeEntry);

350 351 352 353 354
    QAction *action = menu.exec(ev->globalPos());
    if (!action)
        return;

    ev->accept();
355
    if (action == actionOpenFile)
356
        openItem(current);
357 358
    else if (action == actionOpenAsProject)
        ProjectExplorerPlugin::openProject(filePath.toString());
359 360
    else if (action == actionOpenProjects)
        openProjectsInDirectory(current);
361 362
}

363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
void FolderNavigationWidget::setHiddenFilesFilter(bool filter)
{
    QDir::Filters filters = m_fileSystemModel->filter();
    if (filter)
        filters |= QDir::Hidden;
    else
        filters &= ~(QDir::Hidden);
    m_fileSystemModel->setFilter(filters);
    m_filterHiddenFilesAction->setChecked(filter);
}

bool FolderNavigationWidget::hiddenFilesFilter() const
{
    return m_filterHiddenFilesAction->isChecked();
}

379 380 381 382 383 384 385 386 387
QStringList FolderNavigationWidget::projectFilesInDirectory(const QString &path)
{
    QDir dir(path);
    QStringList projectFiles;
    foreach (const QFileInfo &i, dir.entryInfoList(ProjectExplorerPlugin::projectFileGlobs(), QDir::Files))
        projectFiles.append(i.absoluteFilePath());
    return projectFiles;
}

388
// --------------------FolderNavigationWidgetFactory
hjk's avatar
hjk committed
389
FolderNavigationWidgetFactory::FolderNavigationWidgetFactory()
con's avatar
con committed
390
{
391
    m_instance = this;
392 393 394 395
    setDisplayName(tr("File System"));
    setPriority(400);
    setId("File System");
    setActivationSequence(QKeySequence(Core::UseMacShortcuts ? tr("Meta+Y") : tr("Alt+Y")));
396 397 398
    insertRootDirectory({QLatin1String("A.Computer"),
                         0 /*sortValue*/,
                         FolderNavigationWidget::tr("Computer"),
399 400
                         Utils::FileName(),
                         Icons::DESKTOP_DEVICE_SMALL.icon()});
401
    insertRootDirectory({QLatin1String("A.Home"),
402
                         10 /*sortValue*/,
403
                         FolderNavigationWidget::tr("Home"),
404 405
                         Utils::FileName::fromString(QDir::homePath()),
                         Utils::Icons::HOME.icon()});
406 407 408 409 410
    updateProjectsDirectoryRoot();
    connect(Core::DocumentManager::instance(),
            &Core::DocumentManager::projectsDirectoryChanged,
            this,
            &FolderNavigationWidgetFactory::updateProjectsDirectoryRoot);
con's avatar
con committed
411 412 413 414
}

Core::NavigationView FolderNavigationWidgetFactory::createWidget()
{
415
    auto fnw = new FolderNavigationWidget;
416
    for (const RootDirectory &root : m_rootDirectories)
417
        fnw->insertRootDirectory(root);
418 419 420
    connect(this,
            &FolderNavigationWidgetFactory::rootDirectoryAdded,
            fnw,
421
            &FolderNavigationWidget::insertRootDirectory);
422 423 424 425 426 427
    connect(this,
            &FolderNavigationWidgetFactory::rootDirectoryRemoved,
            fnw,
            &FolderNavigationWidget::removeRootDirectory);

    Core::NavigationView n;
428
    n.widget = fnw;
429
    auto filter = new QToolButton;
Ulf Hermann's avatar
Ulf Hermann committed
430
    filter->setIcon(Utils::Icons::FILTER.icon());
431 432 433
    filter->setToolTip(tr("Filter Files"));
    filter->setPopupMode(QToolButton::InstantPopup);
    filter->setProperty("noArrow", true);
434
    auto filterMenu = new QMenu(filter);
435 436 437
    filterMenu->addAction(fnw->m_filterHiddenFilesAction);
    filter->setMenu(filterMenu);
    n.dockToolBarWidgets << filter << fnw->m_toggleSync;
con's avatar
con committed
438 439 440
    return n;
}

Serhii Moroz's avatar
Serhii Moroz committed
441
void FolderNavigationWidgetFactory::saveSettings(QSettings *settings, int position, QWidget *widget)
442
{
443
    auto fnw = qobject_cast<FolderNavigationWidget *>(widget);
444 445 446 447 448 449
    QTC_ASSERT(fnw, return);
    const QString baseKey = QLatin1String("FolderNavigationWidget.") + QString::number(position);
    settings->setValue(baseKey + QLatin1String(".HiddenFilesFilter"), fnw->hiddenFilesFilter());
    settings->setValue(baseKey + QLatin1String(".SyncWithEditor"), fnw->autoSynchronization());
}

Serhii Moroz's avatar
Serhii Moroz committed
450
void FolderNavigationWidgetFactory::restoreSettings(QSettings *settings, int position, QWidget *widget)
451
{
452
    auto fnw = qobject_cast<FolderNavigationWidget *>(widget);
453 454 455 456 457
    QTC_ASSERT(fnw, return);
    const QString baseKey = QLatin1String("FolderNavigationWidget.") + QString::number(position);
    fnw->setHiddenFilesFilter(settings->value(baseKey + QLatin1String(".HiddenFilesFilter"), false).toBool());
    fnw->setAutoSynchronization(settings->value(baseKey +  QLatin1String(".SyncWithEditor"), true).toBool());
}
458

459
void FolderNavigationWidgetFactory::insertRootDirectory(const RootDirectory &directory)
460
{
461 462 463
    const int index = rootIndex(directory.id);
    if (index < 0)
        m_rootDirectories.append(directory);
464 465
    else
        m_rootDirectories[index] = directory;
466
    emit m_instance->rootDirectoryAdded(directory);
467 468
}

469
void FolderNavigationWidgetFactory::removeRootDirectory(const QString &id)
470
{
471
    const int index = rootIndex(id);
472
    QTC_ASSERT(index >= 0, return );
473
    m_rootDirectories.removeAt(index);
474
    emit m_instance->rootDirectoryRemoved(id);
475 476
}

477 478 479 480 481 482 483 484 485
int FolderNavigationWidgetFactory::rootIndex(const QString &id)
{
    return Utils::indexOf(m_rootDirectories,
                          [id](const RootDirectory &entry) { return entry.id == id; });
}

void FolderNavigationWidgetFactory::updateProjectsDirectoryRoot()
{
    insertRootDirectory({QLatin1String(PROJECTSDIRECTORYROOT_ID),
486
                         20 /*sortValue*/,
487
                         FolderNavigationWidget::tr("Projects"),
488 489
                         Core::DocumentManager::projectsDirectory(),
                         Utils::Icons::PROJECT.icon()});
490 491
}

492 493
} // namespace Internal
} // namespace ProjectExplorer