ProjectExplorer: Factor out code for finding file nodes

... with the same base file name. We want to re-use this functionality
in a follow-up patch.

Change-Id: Ia9b5b3f003406c7451d59801c49679d9575d1222
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
Christian Kandeler
2020-04-21 15:43:58 +02:00
parent 7c63a13460
commit 4353084f92
3 changed files with 37 additions and 29 deletions

View File

@@ -214,35 +214,23 @@ bool FlatModel::setData(const QModelIndex &index, const QVariant &value, int rol
// The base name of the file was changed. Go look for other files with the same base name
// and offer to rename them as well.
if (orgFilePath != newFilePath && orgFileInfo.suffix() == newFilePath.toFileInfo().suffix()) {
ProjectNode *productNode = node->parentProjectNode();
while (productNode && !productNode->isProduct())
productNode = productNode->parentProjectNode();
if (productNode) {
const auto filter = [&orgFilePath, &orgFileInfo](const Node *n) {
return n->asFileNode()
&& n->filePath().toFileInfo().dir() == orgFileInfo.dir()
&& n->filePath().toFileInfo().completeBaseName()
== orgFileInfo.completeBaseName()
&& n->filePath() != orgFilePath;
};
const QList<Node *> candidateNodes = productNode->findNodes(filter);
if (!candidateNodes.isEmpty()) {
const QMessageBox::StandardButton reply = QMessageBox::question(
Core::ICore::mainWindow(), tr("Rename More Files?"),
tr("Would you like to rename these files as well?\n %1")
.arg(transform<QStringList>(candidateNodes, [](const Node *n) {
return n->filePath().toFileInfo().fileName();
}).join("\n ")));
if (reply == QMessageBox::Yes) {
for (Node * const n : candidateNodes) {
QString targetFilePath = orgFileInfo.absolutePath() + '/'
+ newFilePath.toFileInfo().completeBaseName();
const QString suffix = n->filePath().toFileInfo().suffix();
if (!suffix.isEmpty())
targetFilePath.append('.').append(suffix);
toRename.emplace_back(std::make_tuple(n, n->filePath(),
FilePath::fromString(targetFilePath)));
}
const QList<Node *> candidateNodes = ProjectTree::siblingsWithSameBaseName(node);
if (!candidateNodes.isEmpty()) {
const QMessageBox::StandardButton reply = QMessageBox::question(
Core::ICore::mainWindow(), tr("Rename More Files?"),
tr("Would you like to rename these files as well?\n %1")
.arg(transform<QStringList>(candidateNodes, [](const Node *n) {
return n->filePath().toFileInfo().fileName();
}).join("\n ")));
if (reply == QMessageBox::Yes) {
for (Node * const n : candidateNodes) {
QString targetFilePath = orgFileInfo.absolutePath() + '/'
+ newFilePath.toFileInfo().completeBaseName();
const QString suffix = n->filePath().toFileInfo().suffix();
if (!suffix.isEmpty())
targetFilePath.append('.').append(suffix);
toRename.emplace_back(std::make_tuple(n, n->filePath(),
FilePath::fromString(targetFilePath)));
}
}
}

View File

@@ -48,6 +48,7 @@
#include <utils/qtcassert.h>
#include <QApplication>
#include <QFileInfo>
#include <QMenu>
#include <QTimer>
@@ -470,6 +471,23 @@ Node *ProjectTree::nodeForFile(const FilePath &fileName)
return node;
}
const QList<Node *> ProjectTree::siblingsWithSameBaseName(const Node *fileNode)
{
ProjectNode *productNode = fileNode->parentProjectNode();
while (productNode && !productNode->isProduct())
productNode = productNode->parentProjectNode();
if (!productNode)
return {};
const QFileInfo fi = fileNode->filePath().toFileInfo();
const auto filter = [&fi](const Node *n) {
return n->asFileNode()
&& n->filePath().toFileInfo().dir() == fi.dir()
&& n->filePath().toFileInfo().completeBaseName() == fi.completeBaseName()
&& n->filePath().toString() != fi.filePath();
};
return productNode->findNodes(filter);
}
void ProjectTree::hideContextMenu()
{
m_focusForContextMenu = nullptr;

View File

@@ -82,6 +82,8 @@ public:
static Project *projectForNode(const Node *node);
static Node *nodeForFile(const Utils::FilePath &fileName);
static const QList<Node *> siblingsWithSameBaseName(const Node *fileNode);
void expandCurrentNodeRecursively();
void collapseAll();