foldernavigationwidget.cpp 16.9 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
con's avatar
con committed
2
**
3
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
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 12 13 14
** 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://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
15
**
16
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17 18 19 20 21 22 23 24 25
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: 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
con's avatar
con committed
26 27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
hjk's avatar
hjk committed
29

con's avatar
con committed
30
#include "foldernavigationwidget.h"
31
#include "projectexplorer.h"
con's avatar
con committed
32

33 34
#include <extensionsystem/pluginmanager.h>

35
#include <coreplugin/actionmanager/command.h>
con's avatar
con committed
36
#include <coreplugin/icore.h>
37
#include <coreplugin/fileiconprovider.h>
38
#include <coreplugin/documentmanager.h>
con's avatar
con committed
39
#include <coreplugin/editormanager/editormanager.h>
40
#include <coreplugin/coreconstants.h>
Robert Loehning's avatar
Robert Loehning committed
41
#include <coreplugin/fileutils.h>
42
#include <coreplugin/find/findplugin.h>
con's avatar
con committed
43

44 45
#include <texteditor/findinfiles.h>

46
#include <utils/hostosinfo.h>
con's avatar
con committed
47
#include <utils/pathchooser.h>
48
#include <utils/qtcassert.h>
49
#include <utils/elidinglabel.h>
50
#include <utils/itemviews.h>
con's avatar
con committed
51

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

65
enum { debug = 0 };
con's avatar
con committed
66 67

namespace ProjectExplorer {
hjk's avatar
hjk committed
68 69
namespace Internal {

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

80
DotRemovalFilter::DotRemovalFilter(QObject *parent) : QSortFilterProxyModel(parent)
81 82 83 84 85
{
}

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

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

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

con's avatar
con committed
114
/*!
115
  \class FolderNavigationWidget
con's avatar
con committed
116 117 118

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

    QVBoxLayout *layout = new QVBoxLayout();
    layout->addWidget(m_title);
148
    layout->addWidget(m_listView);
con's avatar
con committed
149 150 151 152 153
    m_title->setMargin(5);
    layout->setSpacing(0);
    layout->setContentsMargins(0, 0, 0, 0);
    setLayout(layout);

154 155 156 157 158
    m_toggleSync->setIcon(QIcon(QLatin1String(Core::Constants::ICON_LINK)));
    m_toggleSync->setCheckable(true);
    m_toggleSync->setToolTip(tr("Synchronize with Editor"));
    setAutoSynchronization(true);

con's avatar
con committed
159
    // connections
Robert Loehning's avatar
Robert Loehning committed
160 161
    connect(m_listView, SIGNAL(activated(QModelIndex)),
            this, SLOT(slotOpenItem(QModelIndex)));
162 163
    connect(m_filterHiddenFilesAction, SIGNAL(toggled(bool)), this, SLOT(setHiddenFilesFilter(bool)));
    connect(m_toggleSync, SIGNAL(clicked(bool)), this, SLOT(toggleAutoSynchronization()));
164 165
    connect(m_filterModel, SIGNAL(layoutChanged()),
            this, SLOT(ensureCurrentIndex()));
con's avatar
con committed
166 167 168 169 170 171 172 173 174 175 176 177 178 179
}

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

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

void FolderNavigationWidget::setAutoSynchronization(bool sync)
{
180
    m_toggleSync->setChecked(sync);
con's avatar
con committed
181 182 183 184 185 186
    if (sync == m_autoSync)
        return;

    m_autoSync = sync;

    if (m_autoSync) {
187
        connect(Core::DocumentManager::instance(), SIGNAL(currentFileChanged(QString)),
hjk's avatar
hjk committed
188
                this, SLOT(setCurrentFile(QString)));
189
        setCurrentFile(Core::DocumentManager::currentFile());
con's avatar
con committed
190
    } else {
191
        disconnect(Core::DocumentManager::instance(), SIGNAL(currentFileChanged(QString)),
hjk's avatar
hjk committed
192
                this, SLOT(setCurrentFile(QString)));
con's avatar
con committed
193 194 195 196 197
    }
}

void FolderNavigationWidget::setCurrentFile(const QString &filePath)
{
198 199 200 201 202 203 204 205 206
    // 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
207

208 209 210
    // Select the current file.
    if (pathOpened) {
        const QModelIndex fileIndex = m_fileSystemModel->index(filePath);
con's avatar
con committed
211
        if (fileIndex.isValid()) {
212 213
            QItemSelectionModel *selections = m_listView->selectionModel();
            const QModelIndex mainIndex = m_filterModel->mapFromSource(fileIndex);
con's avatar
con committed
214 215
            selections->setCurrentIndex(mainIndex, QItemSelectionModel::SelectCurrent
                                                 | QItemSelectionModel::Clear);
216
            m_listView->scrollTo(mainIndex);
con's avatar
con committed
217
        }
218 219 220 221 222
    }
}

bool FolderNavigationWidget::setCurrentDirectory(const QString &directory)
{
223 224 225 226 227 228
    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);
229 230 231 232
    if (!index.isValid()) {
        setCurrentTitle(QString(), QString());
        return false;
    }
233 234 235
    QModelIndex oldRootIndex = m_listView->rootIndex();
    QModelIndex newRootIndex = m_filterModel->mapFromSource(index);
    m_listView->setRootIndex(newRootIndex);
236
    const QDir current(QDir::cleanPath(newDirectory));
237 238
    setCurrentTitle(current.dirName(),
                    QDir::toNativeSeparators(current.absolutePath()));
239 240 241 242 243
    if (oldRootIndex.parent() == newRootIndex) { // cdUp, so select the old directory
        m_listView->setCurrentIndex(oldRootIndex);
        m_listView->scrollTo(oldRootIndex, QAbstractItemView::EnsureVisible);
    }

244
    return !directory.isEmpty();
con's avatar
con committed
245 246
}

247
QString FolderNavigationWidget::currentDirectory() const
con's avatar
con committed
248
{
249
    return m_fileSystemModel->rootPath();
250 251 252 253 254 255 256 257
}

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

258
void FolderNavigationWidget::openItem(const QModelIndex &srcIndex, bool openDirectoryAsProject)
259 260 261 262
{
    const QString fileName = m_fileSystemModel->fileName(srcIndex);
    if (fileName == QLatin1String("."))
        return;
263 264 265
    if (fileName == QLatin1String("..")) {
        // cd up: Special behaviour: The fileInfo of ".." is that of the parent directory.
        const QString parentPath = m_fileSystemModel->fileInfo(srcIndex).absoluteFilePath();
266
        setCurrentDirectory(parentPath);
267 268
        return;
    }
269 270
    const QString path = m_fileSystemModel->filePath(srcIndex);
    if (m_fileSystemModel->isDir(srcIndex)) {
271
        const QFileInfo fi = m_fileSystemModel->fileInfo(srcIndex);
272 273 274 275 276 277 278 279 280 281 282 283 284 285
        if (!fi.isReadable() || !fi.isExecutable())
            return;
        // Try to find project files in directory and open those.
        if (openDirectoryAsProject) {
            QDir dir(path);
            QStringList proFiles;
            foreach (const QFileInfo &i, dir.entryInfoList(ProjectExplorerPlugin::projectFileGlobs(), QDir::Files))
                proFiles.append(i.absoluteFilePath());
            if (!proFiles.isEmpty())
                Core::ICore::instance()->openFiles(proFiles);
            return;
        }
        // Change to directory
        setCurrentDirectory(path);
286
        return;
con's avatar
con committed
287
    }
288
    // Open file.
289
    Core::ICore::instance()->openFiles(QStringList(path));
con's avatar
con committed
290 291
}

292
void FolderNavigationWidget::setCurrentTitle(QString dirName, const QString &fullPath)
con's avatar
con committed
293
{
294 295
    if (dirName.isEmpty())
        dirName = fullPath;
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
    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(".."))
317
        return FolderNavigationWidget::tr("Open Parent Folder");
318 319 320 321 322 323 324 325
    return FolderNavigationWidget::tr("Open \"%1\"").arg(fileName);
}

void FolderNavigationWidget::contextMenuEvent(QContextMenuEvent *ev)
{
    QMenu menu;
    // Open current item
    const QModelIndex current = currentItem();
326
    const bool hasCurrentItem = current.isValid();
327
    QAction *actionOpen = menu.addAction(actionOpenText(m_fileSystemModel, current));
328
    actionOpen->setEnabled(hasCurrentItem);
329 330 331 332 333 334 335
    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)));
    }
336
    // Explorer & teminal
337
    QAction *actionExplorer = menu.addAction(Core::FileUtils::msgGraphicalShellAction());
338
    actionExplorer->setEnabled(hasCurrentItem);
339
    QAction *actionTerminal = menu.addAction(Core::FileUtils::msgTerminalAction());
340
    actionTerminal->setEnabled(hasCurrentItem);
341

342
    QAction *actionFind = menu.addAction(Core::FileUtils::msgFindInDirectory());
343
    actionFind->setEnabled(hasCurrentItem);
344
    // open with...
345
    if (hasCurrentItem && !isDirectory) {
346
        QMenu *openWith = menu.addMenu(tr("Open With"));
347
        Core::DocumentManager::populateOpenWithMenu(openWith,
348
                                                m_fileSystemModel->filePath(current));
349 350
    }

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

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

    ev->accept();
    if (action == actionOpen) { // Handle open file.
        openItem(current);
        return;
    }
363 364 365 366
    if (action == actionOpenDirectoryAsProject) {
        openItem(current, true);
        return;
    }
367
    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
        return;
    }
    if (action == actionTerminal) {
374
        Core::FileUtils::openTerminal(m_fileSystemModel->filePath(current));
375 376 377
        return;
    }
    if (action == actionExplorer) {
378
        Core::FileUtils::showInGraphicalShell(this, m_fileSystemModel->filePath(current));
379 380
        return;
    }
381
    if (action == actionFind) {
382
        TextEditor::FindInFiles::findOnFileSystem(m_fileSystemModel->filePath(current));
383 384
        return;
    }
385
    Core::DocumentManager::executeOpenWithMenuAction(action);
386 387
}

388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
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();
}

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

415
// --------------------FolderNavigationWidgetFactory
hjk's avatar
hjk committed
416
FolderNavigationWidgetFactory::FolderNavigationWidgetFactory()
con's avatar
con committed
417 418 419 420 421 422 423
{
}

FolderNavigationWidgetFactory::~FolderNavigationWidgetFactory()
{
}

424
QString FolderNavigationWidgetFactory::displayName() const
con's avatar
con committed
425 426 427 428
{
    return tr("File System");
}

429 430 431 432 433
int FolderNavigationWidgetFactory::priority() const
{
    return 400;
}

434
Core::Id FolderNavigationWidgetFactory::id() const
435
{
436
    return "File System";
437 438 439
}

QKeySequence FolderNavigationWidgetFactory::activationSequence() const
con's avatar
con committed
440
{
441
    return QKeySequence(Core::UseMacShortcuts ? tr("Meta+Y") : tr("Alt+Y"));
con's avatar
con committed
442 443 444 445 446
}

Core::NavigationView FolderNavigationWidgetFactory::createWidget()
{
    Core::NavigationView n;
447 448 449 450 451 452 453 454 455 456 457
    FolderNavigationWidget *fnw = new FolderNavigationWidget;
    n.widget = fnw;
    QToolButton *filter = new QToolButton;
    filter->setIcon(QIcon(QLatin1String(Core::Constants::ICON_FILTER)));
    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
458 459 460
    return n;
}

461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
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());
}
480 481 482
} // namespace Internal
} // namespace ProjectExplorer

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