-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfiletreewidget.cpp
More file actions
194 lines (170 loc) · 6.3 KB
/
Copy pathfiletreewidget.cpp
File metadata and controls
194 lines (170 loc) · 6.3 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include "filetreewidget.h"
#include <QDir>
#include <filetreeitem.h>
#include "const.h"
#include <QHeaderView>
#include <QGuiApplication>
#include <QMenu>
#include <QFileDialog>
#include "removefiledialog.h"
fileTreeWidget::fileTreeWidget(QWidget *parent):QTreeWidget(parent)
, _tmpItem(nullptr), _dialogProgress(nullptr), _selectedItem(nullptr), _threadCreateFile(nullptr), _threadOpenFile(nullptr)
{
this->header()->hide();
connect(this, &fileTreeWidget::itemPressed, this, &fileTreeWidget::SlotItemPressed);
_actionImport = new QAction(QIcon(":/icon/import.png"), tr("Import Files"), this);
_actionSetStart = new QAction(QIcon(":/icon/core.png"), tr("Setting File"), this);
_actionCloseFile= new QAction(QIcon(":/icon/close.png"), tr("Close File"), this);
_actionSliderShow = new QAction(QIcon(":/icon/slidershow.png"), tr("Play"), this);
connect(_actionImport, &QAction::triggered, this, &fileTreeWidget::SlotImport);
connect(_actionCloseFile, &QAction::triggered, this, &fileTreeWidget::SlotCloseFile);
}
void fileTreeWidget::AddFileToTree(const QString &name, const QString &path)
{
QDir dir(path);
QString filePath = dir.absoluteFilePath(name);
if (_setPath.find(filePath) != _setPath.end())
return;
QDir fileDir(filePath);
if (!fileDir.exists())
{
bool enable = fileDir.mkpath(filePath);
if (!enable)
return;
}
_setPath.insert(filePath);
auto *item = new fileTreeItem(this, name, filePath, TreeItemFile);
item->setData(0, Qt::DisplayRole, name);
item->setData(0, Qt::DecorationRole, QIcon(":/icon/dir.png"));
item->setData(0, Qt::ToolTipRole, filePath);
this->addTopLevelItem(item);
}
void fileTreeWidget::SlotItemPressed(QTreeWidgetItem *pressedItem, int column)
{
if (QGuiApplication::mouseButtons() == Qt::LeftButton)
{
auto *treePressedItem = dynamic_cast<fileTreeItem*>(pressedItem);
int itemType = treePressedItem->type();
if (itemType == TreeItemImg)
{
emit SigShowSelectedImg(treePressedItem->GetPath());
_selectedItem = pressedItem;
}
}
if (QGuiApplication::mouseButtons() == Qt::RightButton)
{
QMenu menu(this);
int itemType = pressedItem->type();
if (itemType == TreeItemFile)
{
_tmpItem = pressedItem;
menu.addAction(_actionImport);
menu.addAction(_actionSetStart);
menu.addAction(_actionCloseFile);
menu.addAction(_actionSliderShow);
menu.exec(QCursor::pos());
}
}
}
void fileTreeWidget::SlotImport()
{
QFileDialog fileDialog;
fileDialog.setFileMode(QFileDialog::Directory);
fileDialog.setWindowTitle(tr("Choose Import Directory"));
QString path = "";
if (!_tmpItem)
{
qDebug() << "tmpItem is empty\n";
return;
}
path = dynamic_cast<fileTreeItem*>(_tmpItem)->GetPath();
fileDialog.setDirectory(path);
fileDialog.setViewMode(QFileDialog::Detail);
QStringList fileNames;
if (fileDialog.exec())
{
fileNames = fileDialog.selectedFiles();
}
if (fileNames.isEmpty())
return;
QString importPath = fileNames.at(0);
int fileCount = 0;
_dialogProgress = new QProgressDialog(this);
_threadCreateFile = std::make_shared<fileTreeThread>(std::ref(importPath), std::ref(path), _tmpItem, _tmpItem, fileCount, this, nullptr);
connect(_threadCreateFile.get(), &fileTreeThread::SigUpdateProgress, this, &fileTreeWidget::SlotUpdateProgress);
connect(_threadCreateFile.get(), &fileTreeThread::SigFinishProgress, this, &fileTreeWidget::SlotFinishProgress);
connect(_dialogProgress, &QProgressDialog::canceled, this, &fileTreeWidget::SlotCancelProgress);
connect(this, &fileTreeWidget::SigCancelProgress, _threadCreateFile.get(), &fileTreeThread::SlotCancelProgress);
_threadCreateFile->start();
_dialogProgress->setWindowTitle("Loading...");
_dialogProgress->setFixedWidth(PROGRESS_WIDTH);
_dialogProgress->setRange(0, PROGRESS_WIDTH);
_dialogProgress->exec();
}
void fileTreeWidget::SlotCloseFile()
{
RemoveFileDialog removeFileDialog;
auto res = removeFileDialog.exec();
if (res != QDialog::Accepted)
return;
bool bRemove = removeFileDialog.IsRemoved();
auto indexTmpItem = this->indexOfTopLevelItem(_tmpItem);
auto *c_fileTreeItem = dynamic_cast<fileTreeItem*>(_tmpItem);
//auto *c_selectedItem = dynamic_cast<fileTreeItem*>(_tmpItem);
auto deletePath = c_fileTreeItem->GetPath();
_setPath.remove(deletePath);
if (bRemove)
{
QDir deleteDir(deletePath);
deleteDir.removeRecursively();
}
delete this->takeTopLevelItem(indexTmpItem);
_tmpItem = nullptr;
}
void fileTreeWidget::SlotUpdateProgress(int count)
{
if (!_dialogProgress)
return;
if (count >= PROGRESS_MAX)
{
_dialogProgress->setValue(count % PROGRESS_MAX);
}
else
{
_dialogProgress->setValue(count);
}
}
void fileTreeWidget::SlotFinishProgress()
{
qDebug() << "aa";
if (!_dialogProgress)
return;
_dialogProgress->setValue(PROGRESS_MAX);
_dialogProgress->deleteLater();
}
void fileTreeWidget::SlotCancelProgress()
{
emit SigCancelProgress();
delete _dialogProgress;
_dialogProgress = nullptr;
}
void fileTreeWidget::SlotOpenFile(const QString &path)
{
if (_setPath.find(path) != _setPath.end())
return;
_setPath.insert(path);
int fileCount = 0;
QDir fileDir(path);
QString fileName = fileDir.dirName();
_threadOpenFile = std::make_shared<openFileThread>(path, fileCount, this, nullptr);
_dialogProgress = new QProgressDialog(this);
connect(_threadOpenFile.get(), &openFileThread::SigUpdateProgress, this, &fileTreeWidget::SlotUpdateProgress);
connect(_threadOpenFile.get(), &openFileThread::SigFinishProgress, this, &fileTreeWidget::SlotFinishProgress);
connect(_dialogProgress, &QProgressDialog::canceled, this, &fileTreeWidget::SlotCancelProgress);
connect(this, &fileTreeWidget::SigCancelProgress, _threadOpenFile.get(), &openFileThread::SlotCancelProgress);
_threadOpenFile->start();
_dialogProgress->setWindowTitle("Loading...");
_dialogProgress->setFixedWidth(PROGRESS_WIDTH);
_dialogProgress->setRange(0, PROGRESS_WIDTH);
_dialogProgress->exec();
}