ProjectExplorer: Added duplicate file action.

Task-number: QTCREATORBUG-15952
Change-Id: Idab6f120e8324b21db261ad4f0db54dd73cda16e
Reviewed-by: Tobias Hunger <tobias.hunger@theqtcompany.com>
This commit is contained in:
Vikas Pachdha
2016-04-08 16:25:29 +02:00
parent e161b5d1cd
commit d3dd0265b3
5 changed files with 53 additions and 1 deletions

View File

@@ -254,6 +254,7 @@ public:
void searchOnFileSystem(); void searchOnFileSystem();
void showInGraphicalShell(); void showInGraphicalShell();
void removeFile(); void removeFile();
void duplicateFile();
void deleteFile(); void deleteFile();
void handleRenameFile(); void handleRenameFile();
void handleSetStartupProject(); void handleSetStartupProject();
@@ -321,6 +322,7 @@ public:
QAction *m_addExistingDirectoryAction; QAction *m_addExistingDirectoryAction;
QAction *m_addNewSubprojectAction; QAction *m_addNewSubprojectAction;
QAction *m_removeFileAction; QAction *m_removeFileAction;
QAction *m_duplicateFileAction;
QAction *m_removeProjectAction; QAction *m_removeProjectAction;
QAction *m_deleteFileAction; QAction *m_deleteFileAction;
QAction *m_renameFileAction; QAction *m_renameFileAction;
@@ -1028,6 +1030,12 @@ bool ProjectExplorerPlugin::initialize(const QStringList &arguments, QString *er
cmd->setDefaultKeySequence(QKeySequence::Delete); cmd->setDefaultKeySequence(QKeySequence::Delete);
mfileContextMenu->addAction(cmd, Constants::G_FILE_OTHER); mfileContextMenu->addAction(cmd, Constants::G_FILE_OTHER);
// duplicate file action
dd->m_duplicateFileAction = new QAction(tr("Duplicate File..."), this);
cmd = ActionManager::registerAction(dd->m_duplicateFileAction, Constants::DUPLICATEFILE,
projecTreeContext);
mfileContextMenu->addAction(cmd, Constants::G_FILE_OTHER);
//: Remove project from parent profile (Project explorer view); will not physically delete any files. //: Remove project from parent profile (Project explorer view); will not physically delete any files.
dd->m_removeProjectAction = new QAction(tr("Remove Project..."), this); dd->m_removeProjectAction = new QAction(tr("Remove Project..."), this);
cmd = ActionManager::registerAction(dd->m_removeProjectAction, Constants::REMOVEPROJECT, cmd = ActionManager::registerAction(dd->m_removeProjectAction, Constants::REMOVEPROJECT,
@@ -1237,6 +1245,8 @@ bool ProjectExplorerPlugin::initialize(const QStringList &arguments, QString *er
dd, &ProjectExplorerPluginPrivate::openTerminalHere); dd, &ProjectExplorerPluginPrivate::openTerminalHere);
connect(dd->m_removeFileAction, &QAction::triggered, connect(dd->m_removeFileAction, &QAction::triggered,
dd, &ProjectExplorerPluginPrivate::removeFile); dd, &ProjectExplorerPluginPrivate::removeFile);
connect(dd->m_duplicateFileAction, &QAction::triggered,
dd, &ProjectExplorerPluginPrivate::duplicateFile);
connect(dd->m_deleteFileAction, &QAction::triggered, connect(dd->m_deleteFileAction, &QAction::triggered,
dd, &ProjectExplorerPluginPrivate::deleteFile); dd, &ProjectExplorerPluginPrivate::deleteFile);
connect(dd->m_renameFileAction, &QAction::triggered, connect(dd->m_renameFileAction, &QAction::triggered,
@@ -2915,6 +2925,7 @@ void ProjectExplorerPluginPrivate::updateContextMenuActions()
m_addNewSubprojectAction->setEnabled(false); m_addNewSubprojectAction->setEnabled(false);
m_removeProjectAction->setEnabled(false); m_removeProjectAction->setEnabled(false);
m_removeFileAction->setEnabled(false); m_removeFileAction->setEnabled(false);
m_duplicateFileAction->setEnabled(false);
m_deleteFileAction->setEnabled(false); m_deleteFileAction->setEnabled(false);
m_renameFileAction->setEnabled(false); m_renameFileAction->setEnabled(false);
@@ -2924,6 +2935,7 @@ void ProjectExplorerPluginPrivate::updateContextMenuActions()
m_addNewSubprojectAction->setVisible(true); m_addNewSubprojectAction->setVisible(true);
m_removeProjectAction->setVisible(true); m_removeProjectAction->setVisible(true);
m_removeFileAction->setVisible(true); m_removeFileAction->setVisible(true);
m_duplicateFileAction->setVisible(false);
m_deleteFileAction->setVisible(true); m_deleteFileAction->setVisible(true);
m_runActionContextMenu->setVisible(false); m_runActionContextMenu->setVisible(false);
@@ -2988,6 +3000,9 @@ void ProjectExplorerPluginPrivate::updateContextMenuActions()
m_removeFileAction->setVisible(!enableDelete || enableRemove); m_removeFileAction->setVisible(!enableDelete || enableRemove);
m_renameFileAction->setEnabled(actions.contains(Rename)); m_renameFileAction->setEnabled(actions.contains(Rename));
m_duplicateFileAction->setVisible(actions.contains(DuplicateFile));
m_duplicateFileAction->setEnabled(actions.contains(DuplicateFile));
EditorManager::populateOpenWithMenu(m_openWithMenu, EditorManager::populateOpenWithMenu(m_openWithMenu,
ProjectTree::currentNode()->filePath().toString()); ProjectTree::currentNode()->filePath().toString());
} }
@@ -3188,6 +3203,37 @@ void ProjectExplorerPluginPrivate::removeFile()
} }
} }
void ProjectExplorerPluginPrivate::duplicateFile()
{
Node *currentNode = ProjectTree::currentNode();
QTC_ASSERT(currentNode && currentNode->nodeType() == FileNodeType, return);
FileNode *fileNode = currentNode->asFileNode();
QString filePath = currentNode->filePath().toString();
QFileInfo sourceFileInfo(filePath);
QString baseName = sourceFileInfo.baseName();
QString newFilePath = filePath;
int copyTokenIndex = filePath.lastIndexOf(baseName)+baseName.length();
newFilePath.insert(copyTokenIndex, tr("_copy"));
// Build a new file name till a non-existing file is not found.
uint counter = 0;
while (QFileInfo::exists(newFilePath)) {
newFilePath = filePath;
newFilePath.insert(copyTokenIndex, tr("_copy%1").arg(++counter));
}
// Create a copy and add the file to the parent folder node.
FolderNode *folderNode = fileNode->parentFolderNode();
Q_ASSERT(folderNode);
if (!(QFile::copy(filePath, newFilePath) && folderNode->addFiles(QStringList(newFilePath)))) {
QMessageBox::warning(ICore::mainWindow(), tr("Duplicating File Failed"),
tr("Could not duplicate the file %1.")
.arg(QDir::toNativeSeparators(filePath)));
}
}
void ProjectExplorerPluginPrivate::deleteFile() void ProjectExplorerPluginPrivate::deleteFile()
{ {
Node *currentNode = ProjectTree::currentNode(); Node *currentNode = ProjectTree::currentNode();

View File

@@ -76,6 +76,7 @@ const char SEARCHONFILESYSTEM[] = "ProjectExplorer.SearchOnFileSystem";
const char SHOWINGRAPHICALSHELL[] = "ProjectExplorer.ShowInGraphicalShell"; const char SHOWINGRAPHICALSHELL[] = "ProjectExplorer.ShowInGraphicalShell";
const char OPENTERMIANLHERE[] = "ProjectExplorer.OpenTerminalHere"; const char OPENTERMIANLHERE[] = "ProjectExplorer.OpenTerminalHere";
const char REMOVEFILE[] = "ProjectExplorer.RemoveFile"; const char REMOVEFILE[] = "ProjectExplorer.RemoveFile";
const char DUPLICATEFILE[] = "ProjectExplorer.DuplicateFile";
const char DELETEFILE[] = "ProjectExplorer.DeleteFile"; const char DELETEFILE[] = "ProjectExplorer.DeleteFile";
const char RENAMEFILE[] = "ProjectExplorer.RenameFile"; const char RENAMEFILE[] = "ProjectExplorer.RenameFile";
const char SETSTARTUP[] = "ProjectExplorer.SetStartup"; const char SETSTARTUP[] = "ProjectExplorer.SetStartup";

View File

@@ -83,6 +83,7 @@ enum ProjectAction {
// DeleteFile is a define on windows... // DeleteFile is a define on windows...
EraseFile, EraseFile,
Rename, Rename,
DuplicateFile,
// hides actions that use the path(): Open containing folder, open terminal here and Find in Directory // hides actions that use the path(): Open containing folder, open terminal here and Find in Directory
HidePathActions, HidePathActions,
HideFileActions, HideFileActions,

View File

@@ -950,8 +950,10 @@ QList<ProjectAction> QmakePriFileNode::supportedActions(Node *node) const
FileNode *fileNode = node->asFileNode(); FileNode *fileNode = node->asFileNode();
if ((fileNode && fileNode->fileType() != ProjectFileType) if ((fileNode && fileNode->fileType() != ProjectFileType)
|| dynamic_cast<ResourceEditor::ResourceTopLevelNode *>(node)) || dynamic_cast<ResourceEditor::ResourceTopLevelNode *>(node)) {
actions << Rename; actions << Rename;
actions << DuplicateFile;
}
Target *target = m_project->activeTarget(); Target *target = m_project->activeTarget();

View File

@@ -385,6 +385,7 @@ QList<ProjectExplorer::ProjectAction> ResourceFolderNode::supportedActions(Proje
<< ProjectExplorer::AddExistingFile << ProjectExplorer::AddExistingFile
<< ProjectExplorer::AddExistingDirectory << ProjectExplorer::AddExistingDirectory
<< ProjectExplorer::RemoveFile << ProjectExplorer::RemoveFile
<< ProjectExplorer::DuplicateFile
<< ProjectExplorer::Rename // Note: only works for the filename, works akwardly for relative file paths << ProjectExplorer::Rename // Note: only works for the filename, works akwardly for relative file paths
<< ProjectExplorer::HidePathActions; // hides open terminal etc. << ProjectExplorer::HidePathActions; // hides open terminal etc.
@@ -643,6 +644,7 @@ QList<ProjectExplorer::ProjectAction> SimpleResourceFolderNode::supportedActions
<< ProjectExplorer::AddExistingFile << ProjectExplorer::AddExistingFile
<< ProjectExplorer::AddExistingDirectory << ProjectExplorer::AddExistingDirectory
<< ProjectExplorer::RemoveFile << ProjectExplorer::RemoveFile
<< ProjectExplorer::DuplicateFile
<< ProjectExplorer::Rename // Note: only works for the filename, works akwardly for relative file paths << ProjectExplorer::Rename // Note: only works for the filename, works akwardly for relative file paths
<< ProjectExplorer::InheritedFromParent; // do not add to list of projects when adding new file << ProjectExplorer::InheritedFromParent; // do not add to list of projects when adding new file