foldernavigationwidget.cpp 16.2 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"
con's avatar
con committed
28

29 30
#include <extensionsystem/pluginmanager.h>

31
#include <coreplugin/actionmanager/command.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
#include <coreplugin/find/findplugin.h>
con's avatar
con committed
39

40 41
#include <texteditor/findinfiles.h>

42
#include <utils/hostosinfo.h>
con's avatar
con committed
43
#include <utils/pathchooser.h>
44
#include <utils/qtcassert.h>
45
#include <utils/elidinglabel.h>
46
#include <utils/itemviews.h>
Ulf Hermann's avatar
Ulf Hermann committed
47
#include <utils/utilsicons.h>
con's avatar
con committed
48

49 50 51 52 53 54 55 56 57 58
#include <QDebug>
#include <QSize>
#include <QFileSystemModel>
#include <QVBoxLayout>
#include <QToolButton>
#include <QSortFilterProxyModel>
#include <QAction>
#include <QMenu>
#include <QFileDialog>
#include <QContextMenuEvent>
59 60
#include <QDir>
#include <QFileInfo>
con's avatar
con committed
61

62
enum { debug = 0 };
con's avatar
con committed
63 64

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

67 68
// Hide the '.' entry.
class DotRemovalFilter : public QSortFilterProxyModel
hjk's avatar
hjk committed
69 70 71
{
    Q_OBJECT
public:
72
    explicit DotRemovalFilter(QObject *parent = nullptr);
hjk's avatar
hjk committed
73
protected:
74
    virtual bool filterAcceptsRow(int source_row, const QModelIndex &parent) const;
75
    Qt::DropActions supportedDragActions() const;
hjk's avatar
hjk committed
76 77
};

78
DotRemovalFilter::DotRemovalFilter(QObject *parent) : QSortFilterProxyModel(parent)
79
{ }
80 81 82

bool DotRemovalFilter::filterAcceptsRow(int source_row, const QModelIndex &parent) const
{
83
    const QVariant fileName = sourceModel()->data(parent.child(source_row, 0));
84 85 86 87
    if (Utils::HostOsInfo::isAnyUnixHost())
        if (sourceModel()->data(parent) == QLatin1String("/") && fileName == QLatin1String(".."))
            return false;
    return fileName != QLatin1String(".");
88 89
}

90 91 92 93 94
Qt::DropActions DotRemovalFilter::supportedDragActions() const
{
    return sourceModel()->supportedDragActions();
}

95
// FolderNavigationModel: Shows path as tooltip.
96 97 98
class FolderNavigationModel : public QFileSystemModel
{
public:
99
    explicit FolderNavigationModel(QObject *parent = nullptr);
100
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
101
    Qt::DropActions supportedDragActions() const;
102 103
};

104 105
FolderNavigationModel::FolderNavigationModel(QObject *parent) : QFileSystemModel(parent)
{ }
106 107 108 109 110 111 112 113 114

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);
}

115 116 117 118 119
Qt::DropActions FolderNavigationModel::supportedDragActions() const
{
    return Qt::MoveAction;
}

con's avatar
con committed
120
/*!
121
  \class FolderNavigationWidget
con's avatar
con committed
122 123 124

  Shows a file system folder
  */
125 126 127 128 129 130 131
FolderNavigationWidget::FolderNavigationWidget(QWidget *parent) : QWidget(parent),
    m_listView(new Utils::ListView(this)),
    m_fileSystemModel(new FolderNavigationModel(this)),
    m_filterHiddenFilesAction(new QAction(tr("Show Hidden Files"), this)),
    m_filterModel(new DotRemovalFilter(this)),
    m_title(new Utils::ElidingLabel(this)),
    m_toggleSync(new QToolButton(this))
con's avatar
con committed
132
{
133
    m_fileSystemModel->setResolveSymlinks(false);
134
    m_fileSystemModel->setIconProvider(Core::FileIconProvider::iconProvider());
135 136 137
    QDir::Filters filters = QDir::AllDirs | QDir::Files | QDir::Drives
                            | QDir::Readable| QDir::Writable
                            | QDir::Executable | QDir::Hidden;
138 139
    if (Utils::HostOsInfo::isWindowsHost()) // Symlinked directories can cause file watcher warnings on Win32.
        filters |= QDir::NoSymLinks;
140 141
    m_fileSystemModel->setFilter(filters);
    m_filterModel->setSourceModel(m_fileSystemModel);
142 143
    m_filterHiddenFilesAction->setCheckable(true);
    setHiddenFilesFilter(false);
144
    m_listView->setIconSize(QSize(16,16));
145 146 147
    m_listView->setModel(m_filterModel);
    m_listView->setFrameStyle(QFrame::NoFrame);
    m_listView->setAttribute(Qt::WA_MacShowFocusRect, false);
148 149
    m_listView->setDragEnabled(true);
    m_listView->setDragDropMode(QAbstractItemView::DragOnly);
150
    setFocusProxy(m_listView);
con's avatar
con committed
151

152
    auto layout = new QVBoxLayout();
con's avatar
con committed
153
    layout->addWidget(m_title);
154
    layout->addWidget(m_listView);
con's avatar
con committed
155 156 157 158 159
    m_title->setMargin(5);
    layout->setSpacing(0);
    layout->setContentsMargins(0, 0, 0, 0);
    setLayout(layout);

Ulf Hermann's avatar
Ulf Hermann committed
160
    m_toggleSync->setIcon(Utils::Icons::LINK.icon());
161 162 163 164
    m_toggleSync->setCheckable(true);
    m_toggleSync->setToolTip(tr("Synchronize with Editor"));
    setAutoSynchronization(true);

con's avatar
con committed
165
    // connections
166 167 168 169 170 171 172 173
    connect(m_listView, &QAbstractItemView::activated,
            this, &FolderNavigationWidget::slotOpenItem);
    connect(m_filterHiddenFilesAction, &QAction::toggled,
            this, &FolderNavigationWidget::setHiddenFilesFilter);
    connect(m_toggleSync, &QAbstractButton::clicked,
            this, &FolderNavigationWidget::toggleAutoSynchronization);
    connect(m_filterModel, &QAbstractItemModel::layoutChanged,
            this, &FolderNavigationWidget::ensureCurrentIndex);
con's avatar
con committed
174 175 176 177 178 179 180 181 182 183 184 185 186 187
}

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

bool FolderNavigationWidget::autoSynchronization() const
{
    return m_autoSync;
}

void FolderNavigationWidget::setAutoSynchronization(bool sync)
{
188
    m_toggleSync->setChecked(sync);
con's avatar
con committed
189 190 191 192 193 194
    if (sync == m_autoSync)
        return;

    m_autoSync = sync;

    if (m_autoSync) {
195 196 197
        connect(Core::EditorManager::instance(), &Core::EditorManager::currentEditorChanged,
                this, &FolderNavigationWidget::setCurrentFile);
        setCurrentFile(Core::EditorManager::currentEditor());
con's avatar
con committed
198
    } else {
199 200
        disconnect(Core::EditorManager::instance(), &Core::EditorManager::currentEditorChanged,
                this, &FolderNavigationWidget::setCurrentFile);
con's avatar
con committed
201 202 203
    }
}

204
void FolderNavigationWidget::setCurrentFile(Core::IEditor *editor)
con's avatar
con committed
205
{
206 207 208
    if (!editor)
        return;

209
    const QString filePath = editor->document()->filePath().toString();
210 211 212 213 214 215 216 217 218
    // Try to find directory of current file
    bool pathOpened = false;
    if (!filePath.isEmpty())  {
        const QFileInfo fi(filePath);
        if (fi.exists())
            pathOpened = setCurrentDirectory(fi.absolutePath());
    }
    if (!pathOpened)  // Default to home.
        setCurrentDirectory(Utils::PathChooser::homePath());
con's avatar
con committed
219

220 221 222
    // Select the current file.
    if (pathOpened) {
        const QModelIndex fileIndex = m_fileSystemModel->index(filePath);
con's avatar
con committed
223
        if (fileIndex.isValid()) {
224 225
            QItemSelectionModel *selections = m_listView->selectionModel();
            const QModelIndex mainIndex = m_filterModel->mapFromSource(fileIndex);
con's avatar
con committed
226 227
            selections->setCurrentIndex(mainIndex, QItemSelectionModel::SelectCurrent
                                                 | QItemSelectionModel::Clear);
228
            m_listView->scrollTo(mainIndex);
con's avatar
con committed
229
        }
230 231 232 233 234
    }
}

bool FolderNavigationWidget::setCurrentDirectory(const QString &directory)
{
235 236 237 238 239 240
    const QString newDirectory = directory.isEmpty() ? QDir::rootPath() : directory;
    if (debug)
        qDebug() << "setcurdir" << directory << newDirectory;
    // Set the root path on the model instead of changing the top index
    // of the view to cause the model to clean out its file watchers.
    const QModelIndex index = m_fileSystemModel->setRootPath(newDirectory);
241 242 243 244
    if (!index.isValid()) {
        setCurrentTitle(QString(), QString());
        return false;
    }
245 246 247
    QModelIndex oldRootIndex = m_listView->rootIndex();
    QModelIndex newRootIndex = m_filterModel->mapFromSource(index);
    m_listView->setRootIndex(newRootIndex);
248
    const QDir current(QDir::cleanPath(newDirectory));
249 250
    setCurrentTitle(current.dirName(),
                    QDir::toNativeSeparators(current.absolutePath()));
251 252 253 254 255
    if (oldRootIndex.parent() == newRootIndex) { // cdUp, so select the old directory
        m_listView->setCurrentIndex(oldRootIndex);
        m_listView->scrollTo(oldRootIndex, QAbstractItemView::EnsureVisible);
    }

256
    return !directory.isEmpty();
con's avatar
con committed
257 258
}

259
QString FolderNavigationWidget::currentDirectory() const
con's avatar
con committed
260
{
261
    return m_fileSystemModel->rootPath();
262 263 264 265 266 267 268 269
}

void FolderNavigationWidget::slotOpenItem(const QModelIndex &viewIndex)
{
    if (viewIndex.isValid())
        openItem(m_filterModel->mapToSource(viewIndex));
}

270
void FolderNavigationWidget::openItem(const QModelIndex &srcIndex, bool openDirectoryAsProject)
271 272 273 274
{
    const QString fileName = m_fileSystemModel->fileName(srcIndex);
    if (fileName == QLatin1String("."))
        return;
275 276 277
    if (fileName == QLatin1String("..")) {
        // cd up: Special behaviour: The fileInfo of ".." is that of the parent directory.
        const QString parentPath = m_fileSystemModel->fileInfo(srcIndex).absoluteFilePath();
278
        setCurrentDirectory(parentPath);
279 280
        return;
    }
281 282
    const QString path = m_fileSystemModel->filePath(srcIndex);
    if (m_fileSystemModel->isDir(srcIndex)) {
283
        const QFileInfo fi = m_fileSystemModel->fileInfo(srcIndex);
284 285 286 287
        if (!fi.isReadable() || !fi.isExecutable())
            return;
        // Try to find project files in directory and open those.
        if (openDirectoryAsProject) {
288 289 290
            const QStringList projectFiles = FolderNavigationWidget::projectFilesInDirectory(path);
            if (!projectFiles.isEmpty())
                Core::ICore::instance()->openFiles(projectFiles);
291 292 293 294
            return;
        }
        // Change to directory
        setCurrentDirectory(path);
295
        return;
con's avatar
con committed
296
    }
297
    // Open file.
298
    Core::ICore::instance()->openFiles(QStringList(path));
con's avatar
con committed
299 300
}

301
void FolderNavigationWidget::setCurrentTitle(QString dirName, const QString &fullPath)
con's avatar
con committed
302
{
303 304
    if (dirName.isEmpty())
        dirName = fullPath;
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
    m_title->setText(dirName);
    m_title->setToolTip(fullPath);
}

QModelIndex FolderNavigationWidget::currentItem() const
{
    const QModelIndex current = m_listView->currentIndex();
    if (current.isValid())
        return m_filterModel->mapToSource(current);
    return QModelIndex();
}

// Format the text for the "open" action of the context menu according
// to the selectect entry
static inline QString actionOpenText(const QFileSystemModel *model,
                                     const QModelIndex &index)
{
    if (!index.isValid())
        return FolderNavigationWidget::tr("Open");
    const QString fileName = model->fileName(index);
    if (fileName == QLatin1String(".."))
326
        return FolderNavigationWidget::tr("Open Parent Folder");
327 328 329 330 331 332 333 334
    return FolderNavigationWidget::tr("Open \"%1\"").arg(fileName);
}

void FolderNavigationWidget::contextMenuEvent(QContextMenuEvent *ev)
{
    QMenu menu;
    // Open current item
    const QModelIndex current = currentItem();
335
    const bool hasCurrentItem = current.isValid();
336
    QAction *actionOpen = menu.addAction(actionOpenText(m_fileSystemModel, current));
337
    actionOpen->setEnabled(hasCurrentItem);
338 339 340 341 342 343 344 345 346

    // we need dummy DocumentModel::Entry with absolute file path in it
    // to get EditorManager::addNativeDirAndOpenWithActions() working
    Core::DocumentModel::Entry fakeEntry;
    Core::IDocument document;
    document.setFilePath(Utils::FileName::fromString(m_fileSystemModel->filePath(current)));
    fakeEntry.document = &document;
    Core::EditorManager::addNativeDirAndOpenWithActions(&menu, &fakeEntry);

347 348 349 350 351 352 353
    const bool isDirectory = hasCurrentItem && m_fileSystemModel->isDir(current);
    QAction *actionOpenDirectoryAsProject = 0;
    if (isDirectory && m_fileSystemModel->fileName(current) != QLatin1String("..")) {
        actionOpenDirectoryAsProject =
            menu.addAction(tr("Open Project in \"%1\"")
                           .arg(m_fileSystemModel->fileName(current)));
    }
354

355
    // Open file dialog to choose a path starting from current
356
    QAction *actionChooseFolder = menu.addAction(tr("Choose Folder..."));
357 358 359 360 361 362 363 364

    QAction *action = menu.exec(ev->globalPos());
    if (!action)
        return;

    ev->accept();
    if (action == actionOpen) { // Handle open file.
        openItem(current);
365
    } else if (action == actionOpenDirectoryAsProject) {
366
        openItem(current, true);
367
    } else if (action == actionChooseFolder) { // Open file dialog
368
        const QString newPath = QFileDialog::getExistingDirectory(this, tr("Choose Folder"), currentDirectory());
369 370
        if (!newPath.isEmpty())
            setCurrentDirectory(newPath);
371
    }
372 373
}

374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
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();
}

390 391 392 393 394 395 396 397 398 399 400
void FolderNavigationWidget::ensureCurrentIndex()
{
    QModelIndex index = m_listView->currentIndex();
    if (!index.isValid()
            || index.parent() != m_listView->rootIndex()) {
        index = m_listView->rootIndex().child(0, 0);
        m_listView->setCurrentIndex(index);
    }
    m_listView->scrollTo(index);
}

401 402 403 404 405 406 407 408 409
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;
}

410
// --------------------FolderNavigationWidgetFactory
hjk's avatar
hjk committed
411
FolderNavigationWidgetFactory::FolderNavigationWidgetFactory()
con's avatar
con committed
412
{
413 414 415 416
    setDisplayName(tr("File System"));
    setPriority(400);
    setId("File System");
    setActivationSequence(QKeySequence(Core::UseMacShortcuts ? tr("Meta+Y") : tr("Alt+Y")));
con's avatar
con committed
417 418 419 420 421
}

Core::NavigationView FolderNavigationWidgetFactory::createWidget()
{
    Core::NavigationView n;
422
    auto fnw = new FolderNavigationWidget;
423
    n.widget = fnw;
424
    auto filter = new QToolButton;
Ulf Hermann's avatar
Ulf Hermann committed
425
    filter->setIcon(Utils::Icons::FILTER.icon());
426 427 428
    filter->setToolTip(tr("Filter Files"));
    filter->setPopupMode(QToolButton::InstantPopup);
    filter->setProperty("noArrow", true);
429
    auto filterMenu = new QMenu(filter);
430 431 432
    filterMenu->addAction(fnw->m_filterHiddenFilesAction);
    filter->setMenu(filterMenu);
    n.dockToolBarWidgets << filter << fnw->m_toggleSync;
con's avatar
con committed
433 434 435
    return n;
}

Serhii Moroz's avatar
Serhii Moroz committed
436
void FolderNavigationWidgetFactory::saveSettings(QSettings *settings, int position, QWidget *widget)
437
{
438
    auto fnw = qobject_cast<FolderNavigationWidget *>(widget);
439 440 441 442 443 444
    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
445
void FolderNavigationWidgetFactory::restoreSettings(QSettings *settings, int position, QWidget *widget)
446
{
447
    auto fnw = qobject_cast<FolderNavigationWidget *>(widget);
448 449 450 451 452
    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());
}
453 454 455
} // namespace Internal
} // namespace ProjectExplorer

con's avatar
con committed
456
#include "foldernavigationwidget.moc"