File System view: Add "New Folder" to context menu

Task-number: QTCREATORBUG-17358
Change-Id: I64b3d34ca0432369630382c40cf749f3cc1a08df
Reviewed-by: Tobias Hunger <tobias.hunger@qt.io>
This commit is contained in:
Eike Ziller
2018-03-27 15:07:54 +02:00
parent ab7960f6cf
commit 6f91ce1c52
4 changed files with 39 additions and 0 deletions

View File

@@ -726,6 +726,13 @@ bool FileName::operator>=(const FileName &other) const
return other <= *this;
}
FileName FileName::operator+(const QString &s) const
{
FileName result(*this);
result.appendString(s);
return result;
}
/// \returns whether FileName is a child of \a s
bool FileName::isChildOf(const FileName &s) const
{

View File

@@ -84,6 +84,7 @@ public:
bool operator<=(const FileName &other) const;
bool operator>(const FileName &other) const;
bool operator>=(const FileName &other) const;
FileName operator+(const QString &s) const;
bool isChildOf(const FileName &s) const;
bool isChildOf(const QDir &dir) const;

View File

@@ -49,10 +49,12 @@
#include <utils/algorithm.h>
#include <utils/filecrumblabel.h>
#include <utils/fileutils.h>
#include <utils/hostosinfo.h>
#include <utils/navigationtreeview.h>
#include <utils/qtcassert.h>
#include <utils/removefiledialog.h>
#include <utils/stringutils.h>
#include <utils/styledbar.h>
#include <utils/utilsicons.h>
@@ -609,6 +611,26 @@ void FolderNavigationWidget::openProjectsInDirectory(const QModelIndex &index)
Core::ICore::instance()->openFiles(projectFiles);
}
void FolderNavigationWidget::createNewFolder(const QModelIndex &parent)
{
static const QString baseName = tr("New Folder");
// find non-existing name
const QDir dir(m_fileSystemModel->filePath(parent));
const QSet<Utils::FileName> existingItems
= Utils::transform<QSet>(dir.entryList({baseName + '*'}, QDir::AllEntries),
[](const QString &entry) {
return Utils::FileName::fromString(entry);
});
const Utils::FileName name = Utils::makeUniquelyNumbered(Utils::FileName::fromString(baseName),
existingItems);
// create directory and edit
const QModelIndex index = m_fileSystemModel->mkdir(parent, name.toString());
if (!index.isValid())
return;
m_listView->setCurrentIndex(index);
m_listView->edit(index);
}
void FolderNavigationWidget::setCrumblePath(const QModelIndex &index, const QModelIndex &)
{
const int width = m_crumbLabel->width();
@@ -645,6 +667,7 @@ void FolderNavigationWidget::contextMenuEvent(QContextMenuEvent *ev)
QAction *actionOpenFile = nullptr;
QAction *actionOpenProjects = nullptr;
QAction *actionOpenAsProject = nullptr;
QAction *newFolder = nullptr;
const bool isDir = m_fileSystemModel->isDir(current);
const Utils::FileName filePath = hasCurrentItem ? Utils::FileName::fromString(
m_fileSystemModel->filePath(current))
@@ -676,6 +699,7 @@ void FolderNavigationWidget::contextMenuEvent(QContextMenuEvent *ev)
menu.addAction(Core::ActionManager::command(Constants::REMOVEFILE)->action());
if (m_fileSystemModel->flags(current) & Qt::ItemIsEditable)
menu.addAction(Core::ActionManager::command(Constants::RENAMEFILE)->action());
newFolder = menu.addAction(tr("New Folder"));
if (!isDir && Core::DiffService::instance()) {
menu.addAction(
TextEditor::TextDocument::createDiffAgainstCurrentFileAction(&menu, [filePath]() {
@@ -695,6 +719,12 @@ void FolderNavigationWidget::contextMenuEvent(QContextMenuEvent *ev)
ProjectExplorerPlugin::openProject(filePath.toString());
else if (action == actionOpenProjects)
openProjectsInDirectory(current);
else if (action == newFolder) {
if (isDir)
createNewFolder(current);
else
createNewFolder(current.parent());
}
}
bool FolderNavigationWidget::rootAutoSynchronization() const

View File

@@ -127,6 +127,7 @@ private:
void openItem(const QModelIndex &index);
QStringList projectsInDirectory(const QModelIndex &index) const;
void openProjectsInDirectory(const QModelIndex &index);
void createNewFolder(const QModelIndex &parent);
void setCrumblePath(const QModelIndex &index, const QModelIndex &);
Core::IContext *m_context = nullptr;