forked from cloose/CuteMarkEd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileexplorerwidget.cpp
More file actions
62 lines (47 loc) · 1.64 KB
/
fileexplorerwidget.cpp
File metadata and controls
62 lines (47 loc) · 1.64 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
#include "fileexplorerwidget.h"
#include "ui_fileexplorerwidget.h"
#include <QFileSystemModel>
#include <QSortFilterProxyModel>
class FileSortFilterProxyModel : public QSortFilterProxyModel
{
public:
FileSortFilterProxyModel(QObject *parent = 0) : QSortFilterProxyModel(parent) {}
protected:
bool lessThan(const QModelIndex &left, const QModelIndex &right) const
{
QFileSystemModel *model = static_cast<QFileSystemModel*>(this->sourceModel());
QFileInfo leftInfo = model->fileInfo(left);
QFileInfo rightInfo = model->fileInfo(right);
if (leftInfo.isDir() == rightInfo.isDir())
return (leftInfo.filePath().compare(rightInfo.filePath(), Qt::CaseInsensitive) < 0);
return leftInfo.isDir();
}
};
FileExplorerWidget::FileExplorerWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::FileExplorerWidget),
model(new QFileSystemModel(this)),
sortModel(new FileSortFilterProxyModel(this))
{
ui->setupUi(this);
model->setRootPath("");
sortModel->setDynamicSortFilter(true);
sortModel->setSourceModel(model);
ui->fileTreeView->setModel(sortModel);
ui->fileTreeView->hideColumn(1);
ui->fileTreeView->sortByColumn(0, Qt::AscendingOrder);
connect(ui->fileTreeView, SIGNAL(doubleClicked(QModelIndex)),
SLOT(fileOpen(QModelIndex)));
}
FileExplorerWidget::~FileExplorerWidget()
{
delete ui;
}
void FileExplorerWidget::fileOpen(const QModelIndex &index)
{
QFileInfo info = model->fileInfo(sortModel->mapToSource(index));
if (info.isFile()) {
const QString filePath = info.filePath();
emit fileSelected(filePath);
}
}