ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
ProjectViewContextMenu.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
9 #include "ProjectView.h"
10 
11 #include <QFileSystemModel>
12 #include <QInputDialog>
13 #include <QMessageBox>
14 
16  : QMenu(),
17  m_treeView(view),
18  m_renameAction("Rename"),
19  m_deleteAction("Delete"),
20  m_createFileAction("Create File"),
21  m_createFolderAction("Create Folder")
22 {
23  addAction(&m_renameAction);
24  addAction(&m_deleteAction);
25  addAction(&m_createFileAction);
26  addAction(&m_createFolderAction);
27 
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);
31  connect(
32  &m_createFolderAction, &QAction::triggered, this, &ProjectViewContextMenu::createFolder);
33 }
34 
35 void ProjectViewContextMenu::requested(const QPoint &pos)
36 {
37  m_currentIndex = m_treeView->indexAt(pos);
38 
39  if (m_currentIndex.isValid())
40  {
41 
42  m_renameAction.setVisible(true);
43  m_deleteAction.setVisible(true);
44 
45  if (QFileInfo(m_treeView->absolutePathAt(m_currentIndex)).isDir())
46  {
47  m_createFileAction.setVisible(true);
48  m_createFolderAction.setVisible(true);
49  }
50  else
51  {
52  m_createFileAction.setVisible(false);
53  m_createFolderAction.setVisible(false);
54  }
55  }
56  else
57  {
58  m_renameAction.setVisible(false);
59  m_deleteAction.setVisible(false);
60  m_createFileAction.setVisible(true);
61  m_createFolderAction.setVisible(true);
62  }
63  exec(QCursor::pos());
64 }
65 
66 void ProjectViewContextMenu::renameFile() const
67 {
68  Q_ASSERT(m_currentIndex.isValid());
69  bool ok;
70  QString newName =
71  QInputDialog::getText(m_treeView,
72  tr("Enter a new name"),
73  tr("new name"),
74  QLineEdit::Normal,
75  *static_cast<QString *>(m_currentIndex.internalPointer()),
76  &ok);
77 
78  if (ok && !newName.isEmpty())
79  {
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);
83 
84  if (!QFile::rename(oldFilePath, newFilePath))
85  {
86  QMessageBox::critical(m_treeView, "Error", "Failed to rename the file");
87  }
88  m_treeView->setEnabled(true);
89  }
90 }
91 
92 void ProjectViewContextMenu::deleteElement() const
93 {
94  Q_ASSERT(m_currentIndex.isValid());
95  auto result = QMessageBox::question(
96  m_treeView, "Confirm deletion ?", "Are you sure you want to delete this ?");
97 
98  if (result == QMessageBox::StandardButton::Yes)
99  {
100  if (!m_treeView->m_fileSystemModel->remove(m_treeView->currentIndex()))
101  {
102  QMessageBox::critical(m_treeView, "Error", "Failed to delete");
103  }
104  }
105 }
106 
107 void ProjectViewContextMenu::createFile() const
108 {
109  bool ok;
110  QString fileName = QInputDialog::getText(
111  m_treeView, tr("Enter a new name"), tr("new name"), QLineEdit::Normal, "", &ok);
112 
113  if (ok && !fileName.isEmpty())
114  {
115  QDir basePath(m_treeView->absolutePathAt(m_currentIndex));
116  QFile f(basePath.filePath(fileName));
117  if (!f.open(QFile::OpenModeFlag::NewOnly))
118  {
119  QMessageBox::critical(m_treeView, "Error", "Failed to create the file");
120  return;
121  }
122  f.close();
123  }
124 }
125 
126 void ProjectViewContextMenu::createFolder() const
127 {
128  bool ok;
129  QString folderName = QInputDialog::getText(m_treeView,
130  tr("Enter a new name"),
131  tr("Folder name"),
132  QLineEdit::Normal,
133  "New Folder",
134  &ok);
135 
136  if (ok && !folderName.isEmpty())
137  {
138  if (m_currentIndex.isValid())
139  {
140  m_treeView->m_fileSystemModel->mkdir(m_currentIndex, folderName);
141  }
142  else
143  {
144  m_treeView->m_fileSystemModel->rootDirectory().mkdir(folderName);
145  }
146  }
147 }
core::Tensor result
Definition: VtkUtils.cpp:76
void requested(const QPoint &pos)
ProjectViewContextMenu(ProjectView *view)
QString absolutePathAt(const QModelIndex &index) const
Definition: ProjectView.h:53
QString relativePathAt(const QModelIndex &index) const
Definition: ProjectView.h:48