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 // 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. // and offer to rename them as well.
if (orgFilePath != newFilePath && orgFileInfo.suffix() == newFilePath.toFileInfo().suffix()) { if (orgFilePath != newFilePath && orgFileInfo.suffix() == newFilePath.toFileInfo().suffix()) {
ProjectNode *productNode = node->parentProjectNode(); const QList<Node *> candidateNodes = ProjectTree::siblingsWithSameBaseName(node);
while (productNode && !productNode->isProduct()) if (!candidateNodes.isEmpty()) {
productNode = productNode->parentProjectNode(); const QMessageBox::StandardButton reply = QMessageBox::question(
if (productNode) { Core::ICore::mainWindow(), tr("Rename More Files?"),
const auto filter = [&orgFilePath, &orgFileInfo](const Node *n) { tr("Would you like to rename these files as well?\n %1")
return n->asFileNode() .arg(transform<QStringList>(candidateNodes, [](const Node *n) {
&& n->filePath().toFileInfo().dir() == orgFileInfo.dir() return n->filePath().toFileInfo().fileName();
&& n->filePath().toFileInfo().completeBaseName() }).join("\n ")));
== orgFileInfo.completeBaseName() if (reply == QMessageBox::Yes) {
&& n->filePath() != orgFilePath; for (Node * const n : candidateNodes) {
}; QString targetFilePath = orgFileInfo.absolutePath() + '/'
const QList<Node *> candidateNodes = productNode->findNodes(filter); + newFilePath.toFileInfo().completeBaseName();
if (!candidateNodes.isEmpty()) { const QString suffix = n->filePath().toFileInfo().suffix();
const QMessageBox::StandardButton reply = QMessageBox::question( if (!suffix.isEmpty())
Core::ICore::mainWindow(), tr("Rename More Files?"), targetFilePath.append('.').append(suffix);
tr("Would you like to rename these files as well?\n %1") toRename.emplace_back(std::make_tuple(n, n->filePath(),
.arg(transform<QStringList>(candidateNodes, [](const Node *n) { FilePath::fromString(targetFilePath)));
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 <utils/qtcassert.h>
#include <QApplication> #include <QApplication>
#include <QFileInfo>
#include <QMenu> #include <QMenu>
#include <QTimer> #include <QTimer>
@@ -470,6 +471,23 @@ Node *ProjectTree::nodeForFile(const FilePath &fileName)
return node; 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() void ProjectTree::hideContextMenu()
{ {
m_focusForContextMenu = nullptr; m_focusForContextMenu = nullptr;

View File

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