-
Notifications
You must be signed in to change notification settings - Fork 0
/
filepagefiletree.cpp
120 lines (111 loc) · 4.9 KB
/
filepagefiletree.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include "filepagefiletree.h"
#include <QDebug>
#include <ddialog.h>
#include <QMenu>
#include <QInputDialog>
#include "tools/mtptool.h"
#include "currentdevicemanager.h"
DWIDGET_USE_NAMESPACE
FilePageFileTree::FilePageFileTree(QWidget *parent) : QWidget(parent)
{
mainVBLayout = new QVBoxLayout(this);
treeView = new QTreeView(this);
fileSystemModel = new FileSystemModelFixed;
fileSystemModel->setRootPath(MtpTool::MountPoint.absolutePath());
treeView->setModel(fileSystemModel);
treeView->setRootIndex(fileSystemModel->index(CurrentDeviceManager::getInstance()->getCurrentDevice()->getStoragePath()));
treeView->setColumnWidth(0, 450);
treeView->setIconSize(QSize(30, 30));
treeView->setSelectionMode(QTreeView::ExtendedSelection);
treeView->setContextMenuPolicy(Qt::CustomContextMenu);
connect(treeView, &QTreeView::customContextMenuRequested, this, &FilePageFileTree::showContextMenu);
mainVBLayout->setContentsMargins(0, 0, 0, 0);
mainVBLayout->addWidget(treeView);
this->setLayout(mainVBLayout);
}
void FilePageFileTree::deleteSelected()
{
// TODO: 删除音频文件时通知CurrentDevice
// TODO: 如果文件和文件的父目录同时被选中,只删除父目录
QModelIndexList selectedIndexes = treeView->selectionModel()->selectedRows();
if (selectedIndexes.count() != 0) {
QString delFilesName;
foreach (QModelIndex modelIndex, selectedIndexes) {
delFilesName += fileSystemModel->fileInfo(modelIndex).fileName() + "\n";
}
DDialog delDialog("Delete:", delFilesName, this);
delDialog.addButton("Cancel");
delDialog.addButton("Delete", true, DDialog::ButtonWarning);
if (delDialog.exec() == DDialog::Accepted) {
foreach (QModelIndex modelIndex, selectedIndexes) {
QFileInfo fileInfo = fileSystemModel->fileInfo(modelIndex);
if (fileInfo.isDir()) {
// 删除目录
if (fileSystemModel->rmdir(modelIndex)) {
qDebug() << "(FilePageFileTree::deleteSelected) dir deleted:" << fileInfo.absoluteFilePath();
} else {
qDebug() << "(FilePageFileTree::deleteSelected) dir delete failed:" << fileInfo.absoluteFilePath();
}
} else if (fileInfo.isFile()) {
// 删除文件
if (fileSystemModel->remove(modelIndex)) {
qDebug() << "(FilePageFileTree::deleteSelected) file deleted:" << fileInfo.absoluteFilePath();
} else {
qDebug() << "(FilePageFileTree::deleteSelected) file delete failed:" << fileInfo.absoluteFilePath();
}
}
}
} else {
qDebug() << "(FilePageFileTree::deleteSelected) cancel delete selection.";
}
} else {
qDebug() << "(FilePageFileTree::deleteSelected) no selected items.";
}
}
void FilePageFileTree::showContextMenu()
{
QMenu contextMenu(this);
contextMenu.addAction("New dir");
contextMenu.addAction("New dir in subdirectory");
contextMenu.addSeparator();
contextMenu.addAction("Delete");
QAction *selectedAction = contextMenu.exec(QCursor::pos());
if (selectedAction != 0) {
if (selectedAction->text() == "New dir") {
newDir(false);
} else if (selectedAction->text() == "New dir in subdirectory") {
newDir(true);
} else if (selectedAction->text() == "Delete") {
deleteSelected();
}
}
}
// 新建文件夹并滚动到其位置
void FilePageFileTree::newDir(bool inSub)
{
QString text = QInputDialog::getText(this, "New dir", "input the name of new dir");
if (text.count() != 0) {
qDebug() << "(FilePageFileTree::newDir) new dir name:" << text;
QModelIndexList selectedIndexes = treeView->selectionModel()->selectedRows();
if (selectedIndexes.count() != 0) {
// 如果当前有选中项则取其第一个作为新建目录的index
QModelIndex index = selectedIndexes.first();
if (inSub) {
// 新建选中项的子文件夹
if (fileSystemModel->isDir(index)) {
treeView->scrollTo(fileSystemModel->mkdir(index,text));
} else {
DDialog wDialog("Warning:", "Can not new sub_dir in a file.", this);
wDialog.addButton("ok", true, DDialog::ButtonWarning);
wDialog.exec();
}
} else {
// 新建选中项的同级文件夹
treeView->scrollTo(fileSystemModel->mkdir(index.parent(),text));
}
} else {
// 如果当前没有选中项则使用treeview的root作为新建目录的index
treeView->scrollTo(fileSystemModel->mkdir(treeView->rootIndex(),text));
}
}
}