Commit 08aa8338 authored by Eike Ziller's avatar Eike Ziller

Sort root directories in file system view

Change-Id: I8ce68ffa4752c89de735583640103bc22c2a8bc7
Reviewed-by: Tobias Hunger's avatarTobias Hunger <tobias.hunger@qt.io>
parent cf370d53
......@@ -53,12 +53,15 @@
#include <QDir>
#include <QFileInfo>
const int PATH_ROLE = Qt::UserRole;
const int ID_ROLE = Qt::UserRole + 1;
namespace ProjectExplorer {
namespace Internal {
static FolderNavigationWidgetFactory *m_instance = nullptr;
QVector<FolderNavigationWidgetFactory::DirectoryEntry>
QVector<FolderNavigationWidgetFactory::RootDirectory>
FolderNavigationWidgetFactory::m_rootDirectories;
// FolderNavigationModel: Shows path as tooltip.
......@@ -164,21 +167,26 @@ void FolderNavigationWidget::toggleAutoSynchronization()
setAutoSynchronization(!m_autoSync);
}
void FolderNavigationWidget::addRootDirectory(const QString &displayName,
const Utils::FileName &directory)
void FolderNavigationWidget::addRootDirectory(
const FolderNavigationWidgetFactory::RootDirectory &directory)
{
m_rootSelector->addItem(displayName, qVariantFromValue(directory));
m_rootSelector->setItemData(m_rootSelector->count() - 1,
directory.toUserOutput(),
Qt::ToolTipRole);
// insert sorted
int index = 0;
while (index < m_rootSelector->count()
&& m_rootSelector->itemData(index, ID_ROLE).toString() < directory.id)
++index;
m_rootSelector->insertItem(index, directory.displayName);
m_rootSelector->setItemData(index, qVariantFromValue(directory.path), PATH_ROLE);
m_rootSelector->setItemData(index, directory.id, ID_ROLE);
m_rootSelector->setItemData(index, directory.path.toUserOutput(), Qt::ToolTipRole);
if (m_autoSync) // we might find a better root for current selection now
setCurrentEditor(Core::EditorManager::currentEditor());
}
void FolderNavigationWidget::removeRootDirectory(const Utils::FileName &directory)
void FolderNavigationWidget::removeRootDirectory(const QString &id)
{
for (int i = 0; i < m_rootSelector->count(); ++i) {
if (m_rootSelector->itemData(i).value<Utils::FileName>() == directory) {
if (m_rootSelector->itemData(i, ID_ROLE).toString() == id) {
m_rootSelector->removeItem(i);
break;
}
......@@ -343,14 +351,15 @@ FolderNavigationWidgetFactory::FolderNavigationWidgetFactory()
setPriority(400);
setId("File System");
setActivationSequence(QKeySequence(Core::UseMacShortcuts ? tr("Meta+Y") : tr("Alt+Y")));
addRootDirectory(FolderNavigationWidget::tr("Computer"), Utils::FileName());
addRootDirectory(
{QLatin1String("A.Computer"), FolderNavigationWidget::tr("Computer"), Utils::FileName()});
}
Core::NavigationView FolderNavigationWidgetFactory::createWidget()
{
auto fnw = new FolderNavigationWidget;
for (const DirectoryEntry &root : m_rootDirectories)
fnw->addRootDirectory(root.first, root.second);
for (const RootDirectory &root : m_rootDirectories)
fnw->addRootDirectory(root);
connect(this,
&FolderNavigationWidgetFactory::rootDirectoryAdded,
fnw,
......@@ -392,21 +401,19 @@ void FolderNavigationWidgetFactory::restoreSettings(QSettings *settings, int pos
fnw->setAutoSynchronization(settings->value(baseKey + QLatin1String(".SyncWithEditor"), true).toBool());
}
void FolderNavigationWidgetFactory::addRootDirectory(const QString &displayName,
const Utils::FileName &directory)
void FolderNavigationWidgetFactory::addRootDirectory(const RootDirectory &directory)
{
m_rootDirectories.append(DirectoryEntry(displayName, directory));
emit m_instance->rootDirectoryAdded(displayName, directory);
m_rootDirectories.append(directory);
emit m_instance->rootDirectoryAdded(directory);
}
void FolderNavigationWidgetFactory::removeRootDirectory(const Utils::FileName &directory)
void FolderNavigationWidgetFactory::removeRootDirectory(const QString &id)
{
const int index = Utils::indexOf(m_rootDirectories, [directory](const DirectoryEntry &entry) {
return entry.second == directory;
});
QTC_ASSERT(index >= 0, return);
const int index = Utils::indexOf(m_rootDirectories,
[id](const RootDirectory &entry) { return entry.id == id; });
QTC_ASSERT(index >= 0, return );
m_rootDirectories.removeAt(index);
emit m_instance->rootDirectoryRemoved(directory);
emit m_instance->rootDirectoryRemoved(id);
}
} // namespace Internal
......
......@@ -26,13 +26,13 @@
#pragma once
#include <coreplugin/inavigationwidgetfactory.h>
#include <utils/fileutils.h>
#include <QWidget>
namespace Core { class IEditor; }
namespace Utils {
class FileName;
class NavigationTreeView;
}
......@@ -46,6 +46,34 @@ QT_END_NAMESPACE
namespace ProjectExplorer {
namespace Internal {
class FolderNavigationWidgetFactory : public Core::INavigationWidgetFactory
{
Q_OBJECT
public:
struct RootDirectory {
QString id;
QString displayName;
Utils::FileName path;
};
FolderNavigationWidgetFactory();
Core::NavigationView createWidget() override;
void saveSettings(QSettings *settings, int position, QWidget *widget) override;
void restoreSettings(QSettings *settings, int position, QWidget *widget) override;
static void addRootDirectory(const RootDirectory &directory);
static void removeRootDirectory(const QString &id);
signals:
void rootDirectoryAdded(const RootDirectory &directory);
void rootDirectoryRemoved(const QString &id);
private:
static QVector<RootDirectory> m_rootDirectories;
};
class FolderNavigationWidget : public QWidget
{
Q_OBJECT
......@@ -61,8 +89,8 @@ public:
void setAutoSynchronization(bool sync);
void toggleAutoSynchronization();
void addRootDirectory(const QString &displayName, const Utils::FileName &directory);
void removeRootDirectory(const Utils::FileName &directory);
void addRootDirectory(const FolderNavigationWidgetFactory::RootDirectory &directory);
void removeRootDirectory(const QString &id);
protected:
void contextMenuEvent(QContextMenuEvent *ev) override;
......@@ -86,28 +114,5 @@ private:
friend class FolderNavigationWidgetFactory;
};
class FolderNavigationWidgetFactory : public Core::INavigationWidgetFactory
{
Q_OBJECT
public:
FolderNavigationWidgetFactory();
Core::NavigationView createWidget() override;
void saveSettings(QSettings *settings, int position, QWidget *widget) override;
void restoreSettings(QSettings *settings, int position, QWidget *widget) override;
static void addRootDirectory(const QString &displayName, const Utils::FileName &directory);
static void removeRootDirectory(const Utils::FileName &directory);
signals:
void rootDirectoryAdded(const QString &displayName, const Utils::FileName &directory);
void rootDirectoryRemoved(const Utils::FileName &directory);
private:
using DirectoryEntry = std::pair<QString, Utils::FileName>;
static QVector<DirectoryEntry> m_rootDirectories;
};
} // namespace Internal
} // namespace ProjectExplorer
......@@ -386,8 +386,10 @@ void SessionManager::addProject(Project *pro)
m_instance, [pro]() { m_instance->projectDisplayNameChanged(pro); });
emit m_instance->projectAdded(pro);
FolderNavigationWidgetFactory::addRootDirectory(pro->displayName(),
pro->projectFilePath().parentDir());
FolderNavigationWidgetFactory::addRootDirectory(
{"P." + pro->displayName() + "." + pro->projectFilePath().toString(),
pro->displayName(),
pro->projectFilePath().parentDir()});
configureEditors(pro);
connect(pro, &Project::fileListChanged, [pro](){ configureEditors(pro); });
}
......@@ -742,7 +744,8 @@ void SessionManager::removeProjects(QList<Project *> remove)
m_instance, &SessionManager::clearProjectFileCache);
d->m_projectFileCache.remove(pro);
emit m_instance->projectRemoved(pro);
FolderNavigationWidgetFactory::removeRootDirectory(pro->projectFilePath().parentDir());
FolderNavigationWidgetFactory::removeRootDirectory("P." + pro->displayName() + "."
+ pro->projectFilePath().toString());
delete pro;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment