foldernavigationwidget.cpp 15.2 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
con's avatar
con committed
2
**
3
** Copyright (C) 2013 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 31
#include "foldernavigationwidget.h"

32 33
#include <extensionsystem/pluginmanager.h>

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

42 43 44
#include <find/findplugin.h>
#include <texteditor/findinfiles.h>

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

50 51 52 53 54 55 56 57 58 59 60
#include <QDebug>
#include <QSize>
#include <QFileSystemModel>
#include <QVBoxLayout>
#include <QToolButton>
#include <QListView>
#include <QSortFilterProxyModel>
#include <QAction>
#include <QMenu>
#include <QFileDialog>
#include <QContextMenuEvent>
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;
hjk's avatar
hjk committed
75 76
};

77
DotRemovalFilter::DotRemovalFilter(QObject *parent) : QSortFilterProxyModel(parent)
78 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
// FolderNavigationModel: Shows path as tooltip.
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
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
111
/*!
112
  \class FolderNavigationWidget
con's avatar
con committed
113 114 115

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

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

151 152 153 154 155
    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
156
    // connections
Robert Loehning's avatar
Robert Loehning committed
157 158
    connect(m_listView, SIGNAL(activated(QModelIndex)),
            this, SLOT(slotOpenItem(QModelIndex)));
159 160
    connect(m_filterHiddenFilesAction, SIGNAL(toggled(bool)), this, SLOT(setHiddenFilesFilter(bool)));
    connect(m_toggleSync, SIGNAL(clicked(bool)), this, SLOT(toggleAutoSynchronization()));
con's avatar
con committed
161 162 163 164 165 166 167 168 169 170 171 172 173 174
}

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

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

void FolderNavigationWidget::setAutoSynchronization(bool sync)
{
175
    m_toggleSync->setChecked(sync);
con's avatar
con committed
176 177 178 179 180 181
    if (sync == m_autoSync)
        return;

    m_autoSync = sync;

    if (m_autoSync) {
182
        connect(Core::DocumentManager::instance(), SIGNAL(currentFileChanged(QString)),
hjk's avatar
hjk committed
183
                this, SLOT(setCurrentFile(QString)));
184
        setCurrentFile(Core::DocumentManager::currentFile());
con's avatar
con committed
185
    } else {
186
        disconnect(Core::DocumentManager::instance(), SIGNAL(currentFileChanged(QString)),
hjk's avatar
hjk committed
187
                this, SLOT(setCurrentFile(QString)));
con's avatar
con committed
188 189 190 191 192
    }
}

void FolderNavigationWidget::setCurrentFile(const QString &filePath)
{
193 194 195 196 197 198 199 200 201
    // 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
202

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

bool FolderNavigationWidget::setCurrentDirectory(const QString &directory)
{
218 219 220 221 222 223
    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);
224 225 226 227
    if (!index.isValid()) {
        setCurrentTitle(QString(), QString());
        return false;
    }
228
    m_listView->setRootIndex(m_filterModel->mapFromSource(index));
229
    const QDir current(QDir::cleanPath(newDirectory));
230 231
    setCurrentTitle(current.dirName(),
                    QDir::toNativeSeparators(current.absolutePath()));
232
    return !directory.isEmpty();
con's avatar
con committed
233 234
}

235
QString FolderNavigationWidget::currentDirectory() const
con's avatar
con committed
236
{
237
    return m_fileSystemModel->rootPath();
238 239 240 241 242 243 244 245 246 247 248 249 250
}

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

void FolderNavigationWidget::openItem(const QModelIndex &srcIndex)
{
    const QString fileName = m_fileSystemModel->fileName(srcIndex);
    if (fileName == QLatin1String("."))
        return;
251 252 253
    if (fileName == QLatin1String("..")) {
        // cd up: Special behaviour: The fileInfo of ".." is that of the parent directory.
        const QString parentPath = m_fileSystemModel->fileInfo(srcIndex).absoluteFilePath();
254
        setCurrentDirectory(parentPath);
255 256 257
        return;
    }
    if (m_fileSystemModel->isDir(srcIndex)) { // Change to directory
258 259 260
        const QFileInfo fi = m_fileSystemModel->fileInfo(srcIndex);
        if (fi.isReadable() && fi.isExecutable())
            setCurrentDirectory(m_fileSystemModel->filePath(srcIndex));
261
        return;
con's avatar
con committed
262
    }
263
    // Open file.
Eike Ziller's avatar
Eike Ziller committed
264
    Core::EditorManager::openEditor(m_fileSystemModel->filePath(srcIndex));
con's avatar
con committed
265 266
}

267
void FolderNavigationWidget::setCurrentTitle(QString dirName, const QString &fullPath)
con's avatar
con committed
268
{
269 270
    if (dirName.isEmpty())
        dirName = fullPath;
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
    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(".."))
Leena Miettinen's avatar
Leena Miettinen committed
292
        return FolderNavigationWidget::tr("Open Parent Folder");
293 294 295 296 297 298 299 300
    return FolderNavigationWidget::tr("Open \"%1\"").arg(fileName);
}

void FolderNavigationWidget::contextMenuEvent(QContextMenuEvent *ev)
{
    QMenu menu;
    // Open current item
    const QModelIndex current = currentItem();
301
    const bool hasCurrentItem = current.isValid();
302
    QAction *actionOpen = menu.addAction(actionOpenText(m_fileSystemModel, current));
303 304
    actionOpen->setEnabled(hasCurrentItem);
    // Explorer & teminal
305
    QAction *actionExplorer = menu.addAction(Core::FileUtils::msgGraphicalShellAction());
306
    actionExplorer->setEnabled(hasCurrentItem);
307
    QAction *actionTerminal = menu.addAction(Core::FileUtils::msgTerminalAction());
308
    actionTerminal->setEnabled(hasCurrentItem);
309

310 311
    QAction *actionFind = menu.addAction(msgFindOnFileSystem());
    actionFind->setEnabled(hasCurrentItem);
312 313 314
    // open with...
    if (!m_fileSystemModel->isDir(current)) {
        QMenu *openWith = menu.addMenu(tr("Open with"));
315
        Core::DocumentManager::populateOpenWithMenu(openWith,
316
                                                m_fileSystemModel->filePath(current));
317 318
    }

319
    // Open file dialog to choose a path starting from current
Leena Miettinen's avatar
Leena Miettinen committed
320
    QAction *actionChooseFolder = menu.addAction(tr("Choose Folder..."));
321 322 323 324 325 326 327 328 329 330 331

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

    ev->accept();
    if (action == actionOpen) { // Handle open file.
        openItem(current);
        return;
    }
    if (action == actionChooseFolder) { // Open file dialog
Leena Miettinen's avatar
Leena Miettinen committed
332
        const QString newPath = QFileDialog::getExistingDirectory(this, tr("Choose Folder"), currentDirectory());
333 334
        if (!newPath.isEmpty())
            setCurrentDirectory(newPath);
335 336 337
        return;
    }
    if (action == actionTerminal) {
338
        Core::FileUtils::openTerminal(m_fileSystemModel->filePath(current));
339 340 341
        return;
    }
    if (action == actionExplorer) {
342
        Core::FileUtils::showInGraphicalShell(this, m_fileSystemModel->filePath(current));
343 344
        return;
    }
345
    if (action == actionFind) {
346
        TextEditor::FindInFiles::findOnFileSystem(m_fileSystemModel->filePath(current));
347 348
        return;
    }
349
    Core::DocumentManager::executeOpenWithMenuAction(action);
350 351
}

352 353 354 355 356
QString FolderNavigationWidget::msgFindOnFileSystem()
{
    return tr("Find in this directory...");
}

357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
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();
}

373
// --------------------FolderNavigationWidgetFactory
hjk's avatar
hjk committed
374
FolderNavigationWidgetFactory::FolderNavigationWidgetFactory()
con's avatar
con committed
375 376 377 378 379 380 381
{
}

FolderNavigationWidgetFactory::~FolderNavigationWidgetFactory()
{
}

382
QString FolderNavigationWidgetFactory::displayName() const
con's avatar
con committed
383 384 385 386
{
    return tr("File System");
}

387 388 389 390 391
int FolderNavigationWidgetFactory::priority() const
{
    return 400;
}

392
Core::Id FolderNavigationWidgetFactory::id() const
393
{
394
    return "File System";
395 396 397
}

QKeySequence FolderNavigationWidgetFactory::activationSequence() const
con's avatar
con committed
398
{
399
    return QKeySequence(Core::UseMacShortcuts ? tr("Meta+Y") : tr("Alt+Y"));
con's avatar
con committed
400 401 402 403 404
}

Core::NavigationView FolderNavigationWidgetFactory::createWidget()
{
    Core::NavigationView n;
405 406 407 408 409 410 411 412 413 414 415
    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
416 417 418
    return n;
}

419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
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());
}
438 439 440
} // namespace Internal
} // namespace ProjectExplorer

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