-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenfilethread.cpp
More file actions
96 lines (86 loc) · 3.32 KB
/
Copy pathopenfilethread.cpp
File metadata and controls
96 lines (86 loc) · 3.32 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
#include <QDir>
#include <filetreeitem.h>
#include "openfilethread.h"
#include "const.h"
openFileThread::openFileThread(const QString &srcPath, int fileCount, QTreeWidget *self, QObject *parent)
:QThread(parent), _threadStop(false), _srcPath(srcPath), _fileCount(fileCount), _self(self), _root(nullptr)
{
}
void openFileThread::OpenFileTree(const QString &srcPath, int &fileCount, QTreeWidget *self)
{
qDebug() << "openFileTree thread is triggered";
QDir srcDir(srcPath);
auto name = srcDir.dirName();
auto *item = new fileTreeItem(self, name, srcPath, TreeItemFile);
item->setData(0, Qt::DisplayRole, name);
item->setData(0, Qt::DecorationRole, QIcon(":/icon/dir.png"));
item->setData(0, Qt::ToolTipRole, srcPath);
_root = item;
RecursiveFileTree(srcPath, fileCount, self, _root, item, nullptr);
}
void openFileThread::run()
{
OpenFileTree(_srcPath, _fileCount, _self);
if (_threadStop && _root && _self)
{
auto index = _self->indexOfTopLevelItem(_root);
delete _self->takeTopLevelItem(index);
return;
}
qDebug() << "11";
emit SigFinishProgress(_fileCount);
qDebug() << "44";
}
void openFileThread::RecursiveFileTree(const QString &srcPath, int &fileCount, QTreeWidget *self, QTreeWidgetItem *root, QTreeWidgetItem *parent, QTreeWidgetItem *preItem)
{
QDir srcDir(srcPath);
srcDir.setFilter(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);
srcDir.setSorting(QDir::Name);
QFileInfoList list = srcDir.entryInfoList();
for (int i = 0; i < list.size(); ++i)
{
if (_threadStop)
return;
QFileInfo fileInfo = list.at(i);
bool IsDir = fileInfo.isDir();
if (IsDir)
{
qDebug() << "recursive in dir";
if (_threadStop)
return;
fileCount++;
emit SigUpdateProgress(fileCount);
auto *item = new fileTreeItem(parent, fileInfo.fileName(), fileInfo.absoluteFilePath(), _root, TreeItemFile);
item->setData(0, Qt::DisplayRole, fileInfo.fileName());
item->setData(0, Qt::DecorationRole, QIcon(":/icon/dir.png"));
item->setData(0, Qt::ToolTipRole, fileInfo.absoluteFilePath());
RecursiveFileTree(fileInfo.absoluteFilePath(), fileCount, self, root, item, preItem);
}
else
{
if (_threadStop)
return;
const QString &suffix = fileInfo.completeSuffix();
if (!(suffix == "png" || suffix == "jpg" || suffix == "jpeg" || suffix == "bmp"))
continue;
fileCount++;
emit SigUpdateProgress(fileCount);
auto *item = new fileTreeItem(parent, fileInfo.fileName(), fileInfo.absoluteFilePath(), _root, TreeItemImg);
item->setData(0, Qt::DisplayRole, fileInfo.fileName());
item->setData(0, Qt::DecorationRole, QIcon(":/icon/pic.png"));
item->setData(0, Qt::ToolTipRole, fileInfo.absoluteFilePath());
if (preItem)
{
auto *preFileItem = dynamic_cast<fileTreeItem*>(preItem);
preFileItem->SetNextItem(item);
}
item->SetNextItem(preItem);
preItem = item;
}
}
emit SigFinishProgress(fileCount);
}
void openFileThread::SlotCancelProgress()
{
this->_threadStop = true;
}