|
4 | 4 |
|
5 | 5 | #include <QFileDialog>
|
6 | 6 | #include <QFileInfo>
|
| 7 | +#include <QFileSystemModel> |
7 | 8 | #include <QFileIconProvider>
|
8 | 9 | #include <QTreeView>
|
9 | 10 |
|
10 | 11 | Tree::Tree(QSplitter *splitter, MainWindow *mainWindow)
|
11 | 12 | : QObject(splitter),
|
12 |
| - model(new QFileSystemModel()), |
13 |
| - tree(new QTreeView(splitter)), |
14 |
| - mainWindow(mainWindow) |
| 13 | + m_iconProvider(std::make_unique<QFileIconProvider>()), |
| 14 | + m_model(std::make_unique<QFileSystemModel>()), |
| 15 | + m_tree(std::make_unique<QTreeView>(splitter)), |
| 16 | + m_mainWindow(mainWindow) |
15 | 17 | {
|
16 |
| - setupModel(); |
17 |
| - setupTree(); |
| 18 | + setupModel(); |
| 19 | + setupTree(); |
18 | 20 |
|
19 |
| - connect(tree, &QTreeView::doubleClicked, this, &Tree::openFile); |
| 21 | + connect(m_tree.get(), &QTreeView::doubleClicked, this, &Tree::openFile); |
20 | 22 | }
|
21 | 23 |
|
22 | 24 | Tree::~Tree() {}
|
23 | 25 |
|
24 | 26 | void Tree::setupModel()
|
25 | 27 | {
|
26 |
| - model->setRootPath(getDirectoryPath()); |
27 |
| - model->setIconProvider(new QFileIconProvider); |
28 |
| - model->setFilter(QDir::AllEntries | QDir::Hidden | QDir::NoDotAndDotDot); |
| 28 | + m_model->setRootPath(getDirectoryPath()); |
| 29 | + m_model->setIconProvider(m_iconProvider.get()); |
| 30 | + m_model->setFilter(QDir::AllEntries | QDir::Hidden | QDir::NoDotAndDotDot); |
29 | 31 | }
|
30 | 32 |
|
31 | 33 | void Tree::setupTree()
|
32 | 34 | {
|
33 |
| - tree->setModel(model); |
34 |
| - tree->setRootIndex(model->index(model->rootPath())); |
35 |
| - tree->setRootIsDecorated(true); |
36 |
| - tree->setAnimated(true); |
37 |
| - tree->setIndentation(20); |
38 |
| - tree->setSortingEnabled(false); |
39 |
| - tree->sortByColumn(1, Qt::AscendingOrder); |
40 |
| - tree->setHeaderHidden(true); |
41 |
| - |
42 |
| - for (int i = 1; i <= model->columnCount(); ++i) |
43 |
| - { |
44 |
| - tree->setColumnHidden(i, true); |
45 |
| - } |
46 |
| - |
47 |
| - tree->setContextMenuPolicy(Qt::CustomContextMenu); |
48 |
| - connect(tree, &QTreeView::customContextMenuRequested, this, &Tree::showContextMenu); |
| 35 | + m_tree->setModel(m_model.get()); |
| 36 | + m_tree->setRootIndex(m_model->index(m_model->rootPath())); |
| 37 | + m_tree->setRootIsDecorated(true); |
| 38 | + m_tree->setAnimated(true); |
| 39 | + m_tree->setIndentation(20); |
| 40 | + m_tree->setSortingEnabled(false); |
| 41 | + m_tree->sortByColumn(1, Qt::AscendingOrder); |
| 42 | + m_tree->setHeaderHidden(true); |
| 43 | + |
| 44 | + for (int i = 1; i <= m_model->columnCount(); ++i) |
| 45 | + { |
| 46 | + m_tree->setColumnHidden(i, true); |
| 47 | + } |
| 48 | + |
| 49 | + m_tree->setContextMenuPolicy(Qt::CustomContextMenu); |
| 50 | + connect(m_tree.get(), &QTreeView::customContextMenuRequested, this, &Tree::showContextMenu); |
49 | 51 | }
|
50 | 52 |
|
51 |
| -QString Tree::getDirectoryPath() |
| 53 | +QString Tree::getDirectoryPath() const |
52 | 54 | {
|
53 |
| - return QFileDialog::getExistingDirectory( |
54 |
| - nullptr, QObject::tr("Open Directory"), QDir::homePath(), |
55 |
| - QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks); |
| 55 | + return QFileDialog::getExistingDirectory( |
| 56 | + nullptr, QObject::tr("Open Directory"), QDir::homePath(), |
| 57 | + QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks); |
56 | 58 | }
|
57 | 59 |
|
58 | 60 | void Tree::openFile(const QModelIndex &index)
|
59 | 61 | {
|
60 |
| - QString filePath = model->filePath(index); |
61 |
| - QFileInfo fileInfo(filePath); |
62 |
| - |
63 |
| - // Ensure it's a file, not a folder before loading |
64 |
| - if (fileInfo.isFile()) |
65 |
| - { |
66 |
| - mainWindow->loadFileInEditor(filePath); |
67 |
| - } |
| 62 | + QString filePath = m_model->filePath(index); |
| 63 | + QFileInfo fileInfo(filePath); |
| 64 | + |
| 65 | + // Ensure it's a file, not a folder before loading |
| 66 | + if (!fileInfo.exists() || !fileInfo.isFile()) |
| 67 | + { |
| 68 | + qWarning() << "Selected index is not a valid file:" << filePath; |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + m_mainWindow->loadFileInEditor(filePath); |
68 | 73 | }
|
69 | 74 |
|
| 75 | +// Context menu for file operations |
| 76 | +// such as creating new files, folders, renaming, and deleting |
| 77 | +// This function is called when the user right-clicks on the tree view |
70 | 78 | void Tree::showContextMenu(const QPoint &pos)
|
71 | 79 | {
|
72 |
| - // TO_DO: Implement delete a file |
73 |
| - // TO_DO: Implement rename a file |
74 |
| - // TO_DO: Implement create a new file |
75 |
| - // TO_DO: Implement create a new folder |
| 80 | + QMenu contextMenu; |
| 81 | + |
| 82 | + QAction *newFileAction = contextMenu.addAction("New File"); |
| 83 | + QAction *newFolderAction = contextMenu.addAction("New Folder"); |
| 84 | + QAction *renameAction = contextMenu.addAction("Rename"); |
| 85 | + QAction *deleteAction = contextMenu.addAction("Delete"); |
| 86 | + |
| 87 | + QAction *selectedAction = contextMenu.exec(m_tree->viewport()->mapToGlobal(pos)); |
76 | 88 |
|
77 |
| - // use pos param for testing purpose for now |
78 |
| - tree->indexAt(pos); |
| 89 | + if (selectedAction == newFileAction) |
| 90 | + { |
| 91 | + // TO-DO: implement file creation |
| 92 | + } |
| 93 | + else if (selectedAction == newFolderAction) |
| 94 | + { |
| 95 | + // TO-DO: implement folder creation |
| 96 | + } |
| 97 | + else if (selectedAction == renameAction) |
| 98 | + { |
| 99 | + // TO-DO: implement rename file/folder |
| 100 | + } |
| 101 | + else if (selectedAction == deleteAction) |
| 102 | + { |
| 103 | + // TO-DO: implement file deletion |
| 104 | + } |
79 | 105 | }
|
0 commit comments