11 #include <QFileSystemModel>
12 #include <QInputDialog>
13 #include <QMessageBox>
18 m_renameAction(
"Rename"),
19 m_deleteAction(
"Delete"),
20 m_createFileAction(
"Create File"),
21 m_createFolderAction(
"Create Folder")
23 addAction(&m_renameAction);
24 addAction(&m_deleteAction);
25 addAction(&m_createFileAction);
26 addAction(&m_createFolderAction);
28 connect(&m_renameAction, &QAction::triggered,
this, &ProjectViewContextMenu::renameFile);
29 connect(&m_deleteAction, &QAction::triggered,
this, &ProjectViewContextMenu::deleteElement);
30 connect(&m_createFileAction, &QAction::triggered,
this, &ProjectViewContextMenu::createFile);
32 &m_createFolderAction, &QAction::triggered,
this, &ProjectViewContextMenu::createFolder);
37 m_currentIndex = m_treeView->indexAt(pos);
39 if (m_currentIndex.isValid())
42 m_renameAction.setVisible(
true);
43 m_deleteAction.setVisible(
true);
47 m_createFileAction.setVisible(
true);
48 m_createFolderAction.setVisible(
true);
52 m_createFileAction.setVisible(
false);
53 m_createFolderAction.setVisible(
false);
58 m_renameAction.setVisible(
false);
59 m_deleteAction.setVisible(
false);
60 m_createFileAction.setVisible(
true);
61 m_createFolderAction.setVisible(
true);
66 void ProjectViewContextMenu::renameFile()
const
68 Q_ASSERT(m_currentIndex.isValid());
71 QInputDialog::getText(m_treeView,
72 tr(
"Enter a new name"),
75 *
static_cast<QString *
>(m_currentIndex.internalPointer()),
78 if (ok && !newName.isEmpty())
80 const QString currentPath = m_treeView->
relativePathAt(m_currentIndex);
81 const QString oldFilePath = QFileInfo(currentPath).absoluteFilePath();
82 const QString newFilePath = QFileInfo(currentPath).absoluteDir().filePath(newName);
84 if (!QFile::rename(oldFilePath, newFilePath))
86 QMessageBox::critical(m_treeView,
"Error",
"Failed to rename the file");
88 m_treeView->setEnabled(
true);
92 void ProjectViewContextMenu::deleteElement()
const
94 Q_ASSERT(m_currentIndex.isValid());
95 auto result = QMessageBox::question(
96 m_treeView,
"Confirm deletion ?",
"Are you sure you want to delete this ?");
98 if (
result == QMessageBox::StandardButton::Yes)
100 if (!m_treeView->m_fileSystemModel->remove(m_treeView->currentIndex()))
102 QMessageBox::critical(m_treeView,
"Error",
"Failed to delete");
107 void ProjectViewContextMenu::createFile()
const
110 QString fileName = QInputDialog::getText(
111 m_treeView, tr(
"Enter a new name"), tr(
"new name"), QLineEdit::Normal,
"", &ok);
113 if (ok && !fileName.isEmpty())
116 QFile f(basePath.filePath(fileName));
117 if (!f.open(QFile::OpenModeFlag::NewOnly))
119 QMessageBox::critical(m_treeView,
"Error",
"Failed to create the file");
126 void ProjectViewContextMenu::createFolder()
const
129 QString folderName = QInputDialog::getText(m_treeView,
130 tr(
"Enter a new name"),
136 if (ok && !folderName.isEmpty())
138 if (m_currentIndex.isValid())
140 m_treeView->m_fileSystemModel->mkdir(m_currentIndex, folderName);
144 m_treeView->m_fileSystemModel->rootDirectory().mkdir(folderName);
QString absolutePathAt(const QModelIndex &index) const
QString relativePathAt(const QModelIndex &index) const