foldernavigationwidget.cpp 16.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"
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>
37
#include <coreplugin/coreicons.h>
Robert Loehning's avatar
Robert Loehning committed
38
#include <coreplugin/fileutils.h>
39
#include <coreplugin/find/findplugin.h>
con's avatar
con committed
40

41 42
#include <texteditor/findinfiles.h>

43
#include <utils/hostosinfo.h>
con's avatar
con committed
44
#include <utils/pathchooser.h>
45
#include <utils/qtcassert.h>
46
#include <utils/elidinglabel.h>
47
#include <utils/itemviews.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 = 0);
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 83
{
}

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

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

96
// FolderNavigationModel: Shows path as tooltip.
97 98 99 100 101
class FolderNavigationModel : public QFileSystemModel
{
public:
    explicit FolderNavigationModel(QObject *parent = 0);
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
102
    Qt::DropActions supportedDragActions() const;
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
};

FolderNavigationModel::FolderNavigationModel(QObject *parent) :
    QFileSystemModel(parent)
{
}

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

118 119 120 121 122
Qt::DropActions FolderNavigationModel::supportedDragActions() const
{
    return Qt::MoveAction;
}

con's avatar
con committed
123
/*!
124
  \class FolderNavigationWidget
con's avatar
con committed
125 126 127

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

    QVBoxLayout *layout = new QVBoxLayout();
    layout->addWidget(m_title);
159
    layout->addWidget(m_listView);
con's avatar
con committed
160 161 162 163 164
    m_title->setMargin(5);
    layout->setSpacing(0);
    layout->setContentsMargins(0, 0, 0, 0);
    setLayout(layout);

165
    m_toggleSync->setIcon(Core::Icons::LINK.icon());
166 167 168 169
    m_toggleSync->setCheckable(true);
    m_toggleSync->setToolTip(tr("Synchronize with Editor"));
    setAutoSynchronization(true);

con's avatar
con committed
170
    // connections
171 172 173 174 175 176 177 178
    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
179 180 181 182 183 184 185 186 187 188 189 190 191 192
}

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

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

void FolderNavigationWidget::setAutoSynchronization(bool sync)
{
193
    m_toggleSync->setChecked(sync);
con's avatar
con committed
194 195 196 197 198 199
    if (sync == m_autoSync)
        return;

    m_autoSync = sync;

    if (m_autoSync) {
200 201 202
        connect(Core::EditorManager::instance(), &Core::EditorManager::currentEditorChanged,
                this, &FolderNavigationWidget::setCurrentFile);
        setCurrentFile(Core::EditorManager::currentEditor());
con's avatar
con committed
203
    } else {
204 205
        disconnect(Core::EditorManager::instance(), &Core::EditorManager::currentEditorChanged,
                this, &FolderNavigationWidget::setCurrentFile);
con's avatar
con committed
206 207 208
    }
}

209
void FolderNavigationWidget::setCurrentFile(Core::IEditor *editor)
con's avatar
con committed
210
{
211 212 213
    if (!editor)
        return;

214
    const QString filePath = editor->document()->filePath().toString();
215 216 217 218 219 220 221 222 223
    // 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
224

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

bool FolderNavigationWidget::setCurrentDirectory(const QString &directory)
{
240 241 242 243 244 245
    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);
246 247 248 249
    if (!index.isValid()) {
        setCurrentTitle(QString(), QString());
        return false;
    }
250 251 252
    QModelIndex oldRootIndex = m_listView->rootIndex();
    QModelIndex newRootIndex = m_filterModel->mapFromSource(index);
    m_listView->setRootIndex(newRootIndex);
253
    const QDir current(QDir::cleanPath(newDirectory));
254 255
    setCurrentTitle(current.dirName(),
                    QDir::toNativeSeparators(current.absolutePath()));
256 257 258 259 260
    if (oldRootIndex.parent() == newRootIndex) { // cdUp, so select the old directory
        m_listView->setCurrentIndex(oldRootIndex);
        m_listView->scrollTo(oldRootIndex, QAbstractItemView::EnsureVisible);
    }

261
    return !directory.isEmpty();
con's avatar
con committed
262 263
}

264
QString FolderNavigationWidget::currentDirectory() const
con's avatar
con committed
265
{
266
    return m_fileSystemModel->rootPath();
267 268 269 270 271 272 273 274
}

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

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

306
void FolderNavigationWidget::setCurrentTitle(QString dirName, const QString &fullPath)
con's avatar
con committed
307
{
308 309
    if (dirName.isEmpty())
        dirName = fullPath;
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
    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(".."))
331
        return FolderNavigationWidget::tr("Open Parent Folder");
332 333 334 335 336 337 338 339
    return FolderNavigationWidget::tr("Open \"%1\"").arg(fileName);
}

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

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

352 353 354 355 356 357 358
    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)));
    }
359

360
    // Open file dialog to choose a path starting from current
361
    QAction *actionChooseFolder = menu.addAction(tr("Choose Folder..."));
362 363 364 365 366 367 368 369

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

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

379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
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();
}

395 396 397 398 399 400 401 402 403 404 405
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);
}

406 407 408 409 410 411 412 413 414
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;
}

415
// --------------------FolderNavigationWidgetFactory
hjk's avatar
hjk committed
416
FolderNavigationWidgetFactory::FolderNavigationWidgetFactory()
con's avatar
con committed
417
{
418 419 420 421
    setDisplayName(tr("File System"));
    setPriority(400);
    setId("File System");
    setActivationSequence(QKeySequence(Core::UseMacShortcuts ? tr("Meta+Y") : tr("Alt+Y")));
con's avatar
con committed
422 423 424 425 426
}

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

441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
void FolderNavigationWidgetFactory::saveSettings(int position, QWidget *widget)
{
    FolderNavigationWidget *fnw = qobject_cast<FolderNavigationWidget *>(widget);
    QTC_ASSERT(fnw, return);
    QSettings *settings = Core::ICore::settings();
    const QString baseKey = QLatin1String("FolderNavigationWidget.") + QString::number(position);
    settings->setValue(baseKey + QLatin1String(".HiddenFilesFilter"), fnw->hiddenFilesFilter());
    settings->setValue(baseKey + QLatin1String(".SyncWithEditor"), fnw->autoSynchronization());
}

void FolderNavigationWidgetFactory::restoreSettings(int position, QWidget *widget)
{
    FolderNavigationWidget *fnw = qobject_cast<FolderNavigationWidget *>(widget);
    QTC_ASSERT(fnw, return);
    QSettings *settings = Core::ICore::settings();
    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());
}
460 461 462
} // namespace Internal
} // namespace ProjectExplorer

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